Refactoring
This commit is contained in:
parent
148bf249f8
commit
71bb96fe2f
@ -566,53 +566,10 @@ bool Installer::commit()
|
|||||||
for (const auto &action : actions_) {
|
for (const auto &action : actions_) {
|
||||||
const auto &file = action.second;
|
const auto &file = action.second;
|
||||||
|
|
||||||
if (action.first == Action::Remove) {
|
if (action.first == Action::Remove)
|
||||||
QFile f(file.expandedPath);
|
remove(file);
|
||||||
if (!f.exists())
|
else if (action.first == Action::Install)
|
||||||
continue;
|
install(file);
|
||||||
if (!f.remove()) {
|
|
||||||
errors_.append(QObject::tr("Failed to remove file %1. Error %2")
|
|
||||||
.arg(f.fileName(), f.errorString()));
|
|
||||||
}
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (action.first == Action::Install) {
|
|
||||||
auto installDir = QFileInfo(file.expandedPath).absoluteDir();
|
|
||||||
if (!installDir.exists() && !installDir.mkpath(".")) {
|
|
||||||
errors_.append(QObject::tr("Failed to create path %1")
|
|
||||||
.arg(installDir.absolutePath()));
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
{
|
|
||||||
QFile existing(file.expandedPath);
|
|
||||||
if (existing.exists() && !existing.remove()) {
|
|
||||||
errors_.append(QObject::tr("Failed to remove file %1. Error %2")
|
|
||||||
.arg(existing.fileName(), existing.errorString()));
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
QFile f(file.downloadPath);
|
|
||||||
if (!f.rename(file.expandedPath)) {
|
|
||||||
errors_.append(
|
|
||||||
QObject::tr("Failed to move file %1 to %2. Error %3")
|
|
||||||
.arg(f.fileName(), file.expandedPath, f.errorString()));
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (!file.versionDate.isValid())
|
|
||||||
continue;
|
|
||||||
|
|
||||||
if (!f.open(QFile::WriteOnly | QFile::Append) ||
|
|
||||||
!f.setFileTime(file.versionDate,
|
|
||||||
QFile::FileTime::FileModificationTime)) {
|
|
||||||
errors_.append(QObject::tr("Failed to set modification time of "
|
|
||||||
"file %1 to %2. Error %3")
|
|
||||||
.arg(f.fileName(),
|
|
||||||
file.versionDate.toString(Qt::ISODate),
|
|
||||||
f.errorString()));
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return errors_.isEmpty();
|
return errors_.isEmpty();
|
||||||
@ -624,34 +581,92 @@ bool Installer::checkIsPossible()
|
|||||||
|
|
||||||
for (const auto &action : actions_) {
|
for (const auto &action : actions_) {
|
||||||
const auto &file = action.second;
|
const auto &file = action.second;
|
||||||
QFileInfo installDir(QFileInfo(file.expandedPath).absolutePath());
|
|
||||||
|
|
||||||
if (action.first == Action::Remove) {
|
if (action.first == Action::Remove)
|
||||||
if (!QFile::exists(file.expandedPath))
|
checkRemove(file);
|
||||||
continue;
|
else if (action.first == Action::Install)
|
||||||
if (installDir.exists() && !installDir.isWritable()) {
|
checkInstall(file);
|
||||||
errors_.append(QObject::tr("Directory is not writable %1")
|
|
||||||
.arg(installDir.absolutePath()));
|
|
||||||
}
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (action.first == Action::Install) {
|
|
||||||
if (!QFileInfo::exists(file.downloadPath)) {
|
|
||||||
errors_.append(QObject::tr("Downloaded file not exists %1")
|
|
||||||
.arg(file.downloadPath));
|
|
||||||
}
|
|
||||||
if (installDir.exists() && !installDir.isWritable()) {
|
|
||||||
errors_.append(QObject::tr("Directory is not writable %1")
|
|
||||||
.arg(installDir.absolutePath()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
errors_.removeDuplicates();
|
errors_.removeDuplicates();
|
||||||
|
|
||||||
return errors_.isEmpty();
|
return errors_.isEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Installer::checkRemove(const File &file)
|
||||||
|
{
|
||||||
|
QFileInfo installDir(QFileInfo(file.expandedPath).absolutePath());
|
||||||
|
if (!QFile::exists(file.expandedPath))
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (installDir.exists() && !installDir.isWritable()) {
|
||||||
|
errors_.append(QObject::tr("Directory is not writable %1")
|
||||||
|
.arg(installDir.absolutePath()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Installer::checkInstall(const File &file)
|
||||||
|
{
|
||||||
|
if (!QFileInfo::exists(file.downloadPath)) {
|
||||||
|
errors_.append(
|
||||||
|
QObject::tr("Downloaded file not exists %1").arg(file.downloadPath));
|
||||||
|
// no return
|
||||||
|
}
|
||||||
|
|
||||||
|
QFileInfo installDir(QFileInfo(file.expandedPath).absolutePath());
|
||||||
|
if (installDir.exists() && !installDir.isWritable()) {
|
||||||
|
errors_.append(QObject::tr("Directory is not writable %1")
|
||||||
|
.arg(installDir.absolutePath()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Installer::remove(const File &file)
|
||||||
|
{
|
||||||
|
QFile f(file.expandedPath);
|
||||||
|
if (!f.exists())
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (!f.remove()) {
|
||||||
|
errors_.append(QObject::tr("Failed to remove file %1. Error %2")
|
||||||
|
.arg(f.fileName(), f.errorString()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Installer::install(const File &file)
|
||||||
|
{
|
||||||
|
auto installDir = QFileInfo(file.expandedPath).absoluteDir();
|
||||||
|
if (!installDir.exists() && !installDir.mkpath(".")) {
|
||||||
|
errors_.append(
|
||||||
|
QObject::tr("Failed to create path %1").arg(installDir.absolutePath()));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
QFile existing(file.expandedPath);
|
||||||
|
if (existing.exists() && !existing.remove()) {
|
||||||
|
errors_.append(QObject::tr("Failed to remove file %1. Error %2")
|
||||||
|
.arg(existing.fileName(), existing.errorString()));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
QFile f(file.downloadPath);
|
||||||
|
if (!f.rename(file.expandedPath)) {
|
||||||
|
errors_.append(QObject::tr("Failed to move file %1 to %2. Error %3")
|
||||||
|
.arg(f.fileName(), file.expandedPath, f.errorString()));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!file.versionDate.isValid())
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (!f.open(QFile::WriteOnly | QFile::Append) ||
|
||||||
|
!f.setFileTime(file.versionDate, QFile::FileTime::FileModificationTime)) {
|
||||||
|
errors_.append(QObject::tr("Failed to set modification time of "
|
||||||
|
"file %1 to %2. Error %3")
|
||||||
|
.arg(f.fileName(),
|
||||||
|
file.versionDate.toString(Qt::ISODate),
|
||||||
|
f.errorString()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
QString Installer::errorString() const
|
QString Installer::errorString() const
|
||||||
{
|
{
|
||||||
return errors_.join('\n');
|
return errors_.join('\n');
|
||||||
|
@ -94,6 +94,10 @@ public:
|
|||||||
QString errorString() const;
|
QString errorString() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
void remove(const File& file);
|
||||||
|
void install(const File& file);
|
||||||
|
void checkRemove(const File& file);
|
||||||
|
void checkInstall(const File& file);
|
||||||
bool checkIsPossible();
|
bool checkIsPossible();
|
||||||
|
|
||||||
UserActions actions_;
|
UserActions actions_;
|
||||||
|
Loading…
Reference in New Issue
Block a user