Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3608 was 3547, checked in by gkronber, 15 years ago

Implemented review comments in plugin manager. #989 (Implement review comments in plugin infrastructure)

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.Drawing;
26using System.Data;
27using System.Linq;
28using System.Text;
29using System.Windows.Forms;
30using HeuristicLab.PluginInfrastructure.Manager;
31
32namespace HeuristicLab.PluginInfrastructure.Advanced {
33  internal partial class BasicUpdateView : InstallationManagerControl {
34    private BackgroundWorker updatePluginsBackgroundWorker;
35    private const string CheckingPluginsMessage = "Checking for updated plugins...";
36    private const string NoUpdatesAvailableMessage = "No updates available.";
37
38    private PluginManager pluginManager;
39    public PluginManager PluginManager {
40      get { return pluginManager; }
41      set { pluginManager = value; }
42    }
43
44    private InstallationManager installationManager;
45    public InstallationManager InstallationManager {
46      get { return installationManager; }
47      set { installationManager = value; }
48    }
49
50    public BasicUpdateView() {
51      InitializeComponent();
52      updatePluginsBackgroundWorker = new BackgroundWorker();
53      updatePluginsBackgroundWorker.DoWork += new DoWorkEventHandler(updatePluginsBackgroundWorker_DoWork);
54      updatePluginsBackgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(updatePluginsBackgroundWorker_RunWorkerCompleted);
55    }
56
57    #region event handlers for update plugins backgroundworker
58    void updatePluginsBackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
59      if (e.Error != null) {
60        StatusView.ShowError("Connection Error",
61          "There was an error while connecting to the server." + Environment.NewLine +
62           "Please check your connection settings and user credentials.");
63      } else if (e.Cancelled) {
64        StatusView.ShowMessage(NoUpdatesAvailableMessage);
65      }
66      StatusView.RemoveMessage(CheckingPluginsMessage);
67      StatusView.HideProgressIndicator();
68      StatusView.UnlockUI();
69    }
70
71    void updatePluginsBackgroundWorker_DoWork(object sender, DoWorkEventArgs e) {
72      IEnumerable<IPluginDescription> installedPlugins = (IEnumerable<IPluginDescription>)e.Argument;
73      var remotePlugins = installationManager.GetRemotePluginList();
74      // if there is a local plugin with same name and same major and minor version then it's an update
75      var pluginsToUpdate = from remotePlugin in remotePlugins
76                            let matchingLocalPlugins = from installedPlugin in installedPlugins
77                                                       where installedPlugin.Name == remotePlugin.Name
78                                                       where installedPlugin.Version.Major == remotePlugin.Version.Major
79                                                       where installedPlugin.Version.Minor == remotePlugin.Version.Minor
80                                                       where IsNewerThan(remotePlugin, installedPlugin)
81                                                       select installedPlugin
82                            where matchingLocalPlugins.Count() > 0
83                            select remotePlugin;
84      if (pluginsToUpdate.Count() > 0) {
85        installationManager.Update(pluginsToUpdate);
86        pluginManager.DiscoverAndCheckPlugins();
87        e.Cancel = false;
88      } else {
89        e.Cancel = true;
90      }
91    }
92
93    private bool IsNewerThan(IPluginDescription plugin1, IPluginDescription plugin2) {
94      // newer: build version is higher, or if build version is the same revision is higher
95      if (plugin1.Version.Build < plugin2.Version.Build) return false;
96      else if (plugin1.Version.Build > plugin2.Version.Build) return true;
97      else return plugin1.Version.Revision > plugin2.Version.Revision;
98    }
99
100    #endregion
101    private void updateAndInstallButton_Click(object sender, EventArgs e) {
102      var installedPlugins = pluginManager.Plugins.OfType<IPluginDescription>().ToList();
103      updatePluginsBackgroundWorker.RunWorkerAsync(installedPlugins);
104      StatusView.LockUI();
105      StatusView.ShowProgressIndicator();
106      StatusView.RemoveMessage(NoUpdatesAvailableMessage);
107      StatusView.ShowMessage(CheckingPluginsMessage);
108    }
109  }
110}
Note: See TracBrowser for help on using the repository browser.