diff --git a/src/base/search/searchpluginmanager.cpp b/src/base/search/searchpluginmanager.cpp index 11fd2f1b0a93..8c7ffb53e50b 100644 --- a/src/base/search/searchpluginmanager.cpp +++ b/src/base/search/searchpluginmanager.cpp @@ -248,60 +248,96 @@ void SearchPluginManager::installPlugin(const QString &source) } } -void SearchPluginManager::installPlugin_impl(const QString &name, const Path &path) +void SearchPluginManager::installPlugin_impl(const QString &name, const Path &srcPath) { - const SearchPluginVersion newVersion = getPluginVersion(path); + const SearchPluginVersion incomingVersion = getPluginVersion(srcPath); const SearchPluginInfo *plugin = pluginInfo(name); - if (plugin && !(plugin->version < newVersion)) + if (plugin && (plugin->version >= incomingVersion)) { - LogMsg(tr("Plugin already at version %1, which is greater than %2").arg(plugin->version.toString(), newVersion.toString()), Log::INFO); + LogMsg(tr("Same or newer version of search plugin is already installed. Plugin name: \"%1\". Current version: %2. Incoming version: %3") + .arg(plugin->name, plugin->version.toString(), incomingVersion.toString()), Log::INFO); emit pluginUpdateFailed(name, tr("A more recent version of this plugin is already installed.")); return; } - // Process with install + // Proceed to install const Path destPath = pluginPath(name); const Path backupPath = destPath + u".bak"; - bool updated = false; - if (destPath.exists()) + const bool hasExistingPlugin = destPath.exists(); + bool hasBackup = false; + + if (destPath != srcPath) { + // Plugin is not at the dest path, otherwise there is nothing to do here + // Backup in case install fails - Utils::Fs::copyFile(destPath, backupPath); - Utils::Fs::removeFile(destPath); - updated = true; + if (hasExistingPlugin) + { + hasBackup = Utils::Fs::copyFile(destPath, backupPath); + Utils::Fs::removeFile(destPath); + } + + // Copy the plugin to dest path + if (!Utils::Fs::copyFile(srcPath, destPath)) + { + // Roll back + Utils::Fs::removeFile(destPath); + if (hasBackup) + { + // restore backup + if (Utils::Fs::copyFile(backupPath, destPath)) + Utils::Fs::removeFile(backupPath); + else + Utils::Fs::removeFile(destPath); + } + + const QString errMsg = tr("Search plugin installation failed."); + if (hasExistingPlugin) + emit pluginUpdateFailed(name, errMsg); + else + emit pluginInstallationFailed(name, errMsg); + + return; + } } - // Copy the plugin - Utils::Fs::copyFile(path, destPath); + // Update supported plugins update(); - // Check if this was correctly installed - if (!m_plugins.contains(name)) + + // Check if it was correctly installed + if (m_plugins.contains(name)) { - // Remove broken file - Utils::Fs::removeFile(destPath); - LogMsg(tr("Plugin %1 is not supported.").arg(name), Log::INFO); - if (updated) - { - // restore backup - Utils::Fs::copyFile(backupPath, destPath); + // installation successful + LogMsg(tr("Search plugin has been updated. Plugin name: \"%1\". Version: %2.").arg(name, incomingVersion.toString()), Log::INFO); + + if (hasBackup) Utils::Fs::removeFile(backupPath); - // Update supported plugins - update(); - emit pluginUpdateFailed(name, tr("Plugin is not supported.")); - } - else - { - emit pluginInstallationFailed(name, tr("Plugin is not supported.")); - } } else { - // Install was successful, remove backup - if (updated) + LogMsg(tr("Search plugin installation failed. Plugin name: \"%1\"").arg(name), Log::INFO); + + // Roll back + Utils::Fs::removeFile(destPath); + if (hasBackup) { - LogMsg(tr("Plugin %1 has been successfully updated.").arg(name), Log::INFO); - Utils::Fs::removeFile(backupPath); + // restore backup + if (Utils::Fs::copyFile(backupPath, destPath)) + { + Utils::Fs::removeFile(backupPath); + update(); // Update supported plugins + } + else + { + Utils::Fs::removeFile(destPath); + } } + + const QString errMsg = tr("Plugin is not supported."); + if (hasExistingPlugin) + emit pluginUpdateFailed(name, errMsg); + else + emit pluginInstallationFailed(name, errMsg); } } diff --git a/src/base/search/searchpluginmanager.h b/src/base/search/searchpluginmanager.h index f2968e1cf35e..397ca0b53a6d 100644 --- a/src/base/search/searchpluginmanager.h +++ b/src/base/search/searchpluginmanager.h @@ -112,7 +112,7 @@ class SearchPluginManager final : public QObject void update(); void updateNova(); void parseVersionInfo(const QByteArray &info); - void installPlugin_impl(const QString &name, const Path &path); + void installPlugin_impl(const QString &name, const Path &srcPath); bool isUpdateNeeded(const QString &pluginName, const SearchPluginVersion &newVersion) const; void versionInfoDownloadFinished(const Net::DownloadResult &result);