@@ -248,60 +248,96 @@ void SearchPluginManager::installPlugin(const QString &source)
248248 }
249249}
250250
251- void SearchPluginManager::installPlugin_impl (const QString &name, const Path &path )
251+ void SearchPluginManager::installPlugin_impl (const QString &name, const Path &srcPath )
252252{
253- const SearchPluginVersion newVersion = getPluginVersion (path );
253+ const SearchPluginVersion incomingVersion = getPluginVersion (srcPath );
254254 const SearchPluginInfo *plugin = pluginInfo (name);
255- if (plugin && ! (plugin->version < newVersion ))
255+ if (plugin && (plugin->version >= incomingVersion ))
256256 {
257- LogMsg (tr (" Plugin already at version %1, which is greater than %2" ).arg (plugin->version .toString (), newVersion.toString ()), Log::INFO );
257+ LogMsg (tr (" Same or newer version of search plugin is already installed. Plugin name: \" %1\" . Current version: %2. Incoming version: %3" )
258+ .arg (plugin->name , plugin->version .toString (), incomingVersion.toString ()), Log::INFO );
258259 emit pluginUpdateFailed (name, tr (" A more recent version of this plugin is already installed." ));
259260 return ;
260261 }
261262
262- // Process with install
263+ // Proceed to install
263264 const Path destPath = pluginPath (name);
264265 const Path backupPath = destPath + u" .bak" ;
265- bool updated = false ;
266- if (destPath.exists ())
266+ const bool hasExistingPlugin = destPath.exists ();
267+ bool hasBackup = false ;
268+
269+ if (destPath != srcPath)
267270 {
271+ // Plugin is not at the dest path, otherwise there is nothing to do here
272+
268273 // Backup in case install fails
269- Utils::Fs::copyFile (destPath, backupPath);
270- Utils::Fs::removeFile (destPath);
271- updated = true ;
274+ if (hasExistingPlugin)
275+ {
276+ hasBackup = Utils::Fs::copyFile (destPath, backupPath);
277+ Utils::Fs::removeFile (destPath);
278+ }
279+
280+ // Copy the plugin to dest path
281+ if (!Utils::Fs::copyFile (srcPath, destPath))
282+ {
283+ // Roll back
284+ Utils::Fs::removeFile (destPath);
285+ if (hasBackup)
286+ {
287+ // restore backup
288+ if (Utils::Fs::copyFile (backupPath, destPath))
289+ Utils::Fs::removeFile (backupPath);
290+ else
291+ Utils::Fs::removeFile (destPath);
292+ }
293+
294+ const QString errMsg = tr (" Search plugin installation failed." );
295+ if (hasExistingPlugin)
296+ emit pluginUpdateFailed (name, errMsg);
297+ else
298+ emit pluginInstallationFailed (name, errMsg);
299+
300+ return ;
301+ }
272302 }
273- // Copy the plugin
274- Utils::Fs::copyFile (path, destPath);
303+
275304 // Update supported plugins
276305 update ();
277- // Check if this was correctly installed
278- if (!m_plugins.contains (name))
306+
307+ // Check if it was correctly installed
308+ if (m_plugins.contains (name))
279309 {
280- // Remove broken file
281- Utils::Fs::removeFile (destPath);
282- LogMsg (tr (" Plugin %1 is not supported." ).arg (name), Log::INFO );
283- if (updated)
284- {
285- // restore backup
286- Utils::Fs::copyFile (backupPath, destPath);
310+ // installation successful
311+ LogMsg (tr (" Search plugin has been updated. Plugin name: \" %1\" . Version: %2." ).arg (name, incomingVersion.toString ()), Log::INFO );
312+
313+ if (hasBackup)
287314 Utils::Fs::removeFile (backupPath);
288- // Update supported plugins
289- update ();
290- emit pluginUpdateFailed (name, tr (" Plugin is not supported." ));
291- }
292- else
293- {
294- emit pluginInstallationFailed (name, tr (" Plugin is not supported." ));
295- }
296315 }
297316 else
298317 {
299- // Install was successful, remove backup
300- if (updated)
318+ LogMsg (tr (" Search plugin installation failed. Plugin name: \" %1\" " ).arg (name), Log::INFO );
319+
320+ // Roll back
321+ Utils::Fs::removeFile (destPath);
322+ if (hasBackup)
301323 {
302- LogMsg (tr (" Plugin %1 has been successfully updated." ).arg (name), Log::INFO );
303- Utils::Fs::removeFile (backupPath);
324+ // restore backup
325+ if (Utils::Fs::copyFile (backupPath, destPath))
326+ {
327+ Utils::Fs::removeFile (backupPath);
328+ update (); // Update supported plugins
329+ }
330+ else
331+ {
332+ Utils::Fs::removeFile (destPath);
333+ }
304334 }
335+
336+ const QString errMsg = tr (" Plugin is not supported." );
337+ if (hasExistingPlugin)
338+ emit pluginUpdateFailed (name, errMsg);
339+ else
340+ emit pluginInstallationFailed (name, errMsg);
305341 }
306342}
307343
0 commit comments