Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 69 additions & 33 deletions src/base/search/searchpluginmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't you cancel installation in case when creating backup failed?

Utils::Fs::removeFile(destPath);
}

// Copy the plugin to dest path
if (!Utils::Fs::copyFile(srcPath, destPath))
{
// Roll back
Utils::Fs::removeFile(destPath);
Comment thread
glassez marked this conversation as resolved.
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;
Comment thread
Chocobo1 marked this conversation as resolved.
}
}
// 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);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/base/search/searchpluginmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading