Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.PluginInfrastructure/3.3/Advanced/BasicUpdateView.cs @ 5444

Last change on this file since 5444 was 4527, checked in by gkronber, 14 years ago

Fixed #1216 via Invoke() call.

File size: 5.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.ComponentModel;
25using System.Linq;
26using HeuristicLab.PluginInfrastructure.Manager;
27
28namespace HeuristicLab.PluginInfrastructure.Advanced {
29  internal partial class BasicUpdateView : InstallationManagerControl {
30    private BackgroundWorker updatePluginsBackgroundWorker;
31    private const string CheckingPluginsMessage = "Checking for updated plugins...";
32    private const string NoUpdatesAvailableMessage = "No updates available.";
33
34    private PluginManager pluginManager;
35    public PluginManager PluginManager {
36      get { return pluginManager; }
37      set { pluginManager = value; }
38    }
39
40    private InstallationManager installationManager;
41    public InstallationManager InstallationManager {
42      get { return installationManager; }
43      set { installationManager = value; }
44    }
45
46    public BasicUpdateView() {
47      InitializeComponent();
48      updatePluginsBackgroundWorker = new BackgroundWorker();
49      updatePluginsBackgroundWorker.DoWork += new DoWorkEventHandler(updatePluginsBackgroundWorker_DoWork);
50      updatePluginsBackgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(updatePluginsBackgroundWorker_RunWorkerCompleted);
51    }
52
53    #region event handlers for update plugins backgroundworker
54    void updatePluginsBackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
55      if (e.Error != null) {
56        StatusView.ShowError("Connection Error",
57          "There was an error while connecting to the server." + Environment.NewLine +
58           "Please check your connection settings and user credentials.");
59      } else if (e.Cancelled) {
60        StatusView.ShowMessage(NoUpdatesAvailableMessage);
61      }
62      StatusView.RemoveMessage(CheckingPluginsMessage);
63      StatusView.HideProgressIndicator();
64      StatusView.UnlockUI();
65    }
66
67    void updatePluginsBackgroundWorker_DoWork(object sender, DoWorkEventArgs e) {
68      IEnumerable<IPluginDescription> installedPlugins = (IEnumerable<IPluginDescription>)e.Argument;
69      var remotePlugins = installationManager.GetRemotePluginList();
70      // if there is a local plugin with same name and same major and minor version then it's an update
71      var pluginsToUpdate = from remotePlugin in remotePlugins
72                            let matchingLocalPlugins = from installedPlugin in installedPlugins
73                                                       where installedPlugin.Name == remotePlugin.Name
74                                                       where installedPlugin.Version.Major == remotePlugin.Version.Major
75                                                       where installedPlugin.Version.Minor == remotePlugin.Version.Minor
76                                                       where IsNewerThan(remotePlugin, installedPlugin)
77                                                       select installedPlugin
78                            where matchingLocalPlugins.Count() > 0
79                            select remotePlugin;
80      if (pluginsToUpdate.Count() > 0) {
81        installationManager.Update(pluginsToUpdate);
82        pluginManager.DiscoverAndCheckPlugins();
83        e.Cancel = false;
84      } else {
85        e.Cancel = true;
86      }
87    }
88
89    // compares for two plugins with same major and minor version if plugin1 is newer than plugin2
90    private static bool IsNewerThan(IPluginDescription plugin1, IPluginDescription plugin2) {
91      // newer: build version is higher, or if build version is the same revision is higher
92      return plugin1.Version.Build > plugin2.Version.Build ||
93        (plugin1.Version.Build == plugin2.Version.Build && plugin1.Version.Revision > plugin2.Version.Revision);
94    }
95
96    #endregion
97    private void updateAndInstallButton_Click(object sender, EventArgs e) {
98      var installedPlugins = pluginManager.Plugins.OfType<IPluginDescription>().ToList();
99      updatePluginsBackgroundWorker.RunWorkerAsync(installedPlugins);
100      StatusView.LockUI();
101      StatusView.ShowProgressIndicator();
102      StatusView.RemoveMessage(NoUpdatesAvailableMessage);
103      StatusView.ShowMessage(CheckingPluginsMessage);
104    }
105  }
106}
Note: See TracBrowser for help on using the repository browser.