Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.PluginInfrastructure/3.3/Advanced/InstalledPluginsView.cs @ 6230

Last change on this file since 6230 was 5445, checked in by swagner, 14 years ago

Updated year of copyrights (#1406)

File size: 10.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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 System.Windows.Forms;
27using HeuristicLab.PluginInfrastructure.Manager;
28
29namespace HeuristicLab.PluginInfrastructure.Advanced {
30  internal partial class InstalledPluginsView : InstallationManagerControl {
31    private const string CheckingPluginsMessage = "Checking for updated plugins...";
32    private const string NoUpdatesAvailableMessage = "No updates available.";
33    private BackgroundWorker removePluginsBackgroundWorker;
34    private BackgroundWorker updatePluginsBackgroundWorker;
35
36    private ListViewGroup enabledPluginsGroup;
37    private ListViewGroup disabledPluginsGroup;
38
39    private PluginManager pluginManager;
40    public PluginManager PluginManager {
41      get { return pluginManager; }
42      set {
43        pluginManager = value;
44        UpdateControl();
45      }
46    }
47
48    private InstallationManager installationManager;
49    public InstallationManager InstallationManager {
50      get { return installationManager; }
51      set { installationManager = value; }
52    }
53
54    public InstalledPluginsView()
55      : base() {
56      InitializeComponent();
57      enabledPluginsGroup = localPluginsListView.Groups["activePluginsGroup"];
58      disabledPluginsGroup = localPluginsListView.Groups["disabledPluginsGroup"];
59      pluginImageList.Images.Add(HeuristicLab.PluginInfrastructure.Resources.Plugin);
60      removePluginsBackgroundWorker = new BackgroundWorker();
61      removePluginsBackgroundWorker.DoWork += new DoWorkEventHandler(removePluginsBackgroundWorker_DoWork);
62      removePluginsBackgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(removePluginsBackgroundWorker_RunWorkerCompleted);
63      updatePluginsBackgroundWorker = new BackgroundWorker();
64      updatePluginsBackgroundWorker.DoWork += new DoWorkEventHandler(updatePluginsBackgroundWorker_DoWork);
65      updatePluginsBackgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(updatePluginsBackgroundWorker_RunWorkerCompleted);
66
67      UpdateControl();
68    }
69
70
71
72    #region event handlers for plugin removal background worker
73    void removePluginsBackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
74      if (e.Error != null) {
75        StatusView.ShowError("File Deletion Error", "There was problem while deleting files." + Environment.NewLine + e.Error.Message);
76      }
77      UpdateControl();
78      StatusView.HideProgressIndicator();
79      StatusView.UnlockUI();
80    }
81
82    void removePluginsBackgroundWorker_DoWork(object sender, DoWorkEventArgs e) {
83      IEnumerable<IPluginDescription> pluginsToRemove = (IEnumerable<IPluginDescription>)e.Argument;
84      if (pluginsToRemove.Count() > 0) {
85        installationManager.Remove(pluginsToRemove);
86        pluginManager.DiscoverAndCheckPlugins();
87      }
88    }
89    #endregion
90
91    #region event handlers for update plugins backgroundworker
92    void updatePluginsBackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
93      if (e.Error != null) {
94        StatusView.ShowError("Connection Error",
95          "There was an error while connecting to the server." + Environment.NewLine +
96           "Please check your connection settings and user credentials.");
97      }
98      StatusView.RemoveMessage(CheckingPluginsMessage);
99      StatusView.HideProgressIndicator();
100      UpdateControl();
101      StatusView.UnlockUI();
102    }
103
104    void updatePluginsBackgroundWorker_DoWork(object sender, DoWorkEventArgs e) {
105      IEnumerable<IPluginDescription> selectedPlugins = (IEnumerable<IPluginDescription>)e.Argument;
106      var remotePlugins = installationManager.GetRemotePluginList();
107      // if there is a local plugin with same name and same major and minor version then it's an update
108      var pluginsToUpdate = from remotePlugin in remotePlugins
109                            let matchingLocalPlugins = from installedPlugin in selectedPlugins
110                                                       where installedPlugin.Name == remotePlugin.Name
111                                                       where installedPlugin.Version.Major == remotePlugin.Version.Major
112                                                       where installedPlugin.Version.Minor == remotePlugin.Version.Minor
113                                                       where IsNewerThan(remotePlugin, installedPlugin)
114                                                       select installedPlugin
115                            where matchingLocalPlugins.Count() > 0
116                            select remotePlugin;
117      if (pluginsToUpdate.Count() > 0) {
118        installationManager.Update(pluginsToUpdate);
119      }
120      pluginManager.DiscoverAndCheckPlugins();
121    }
122
123    // compares for two plugins with same major and minor version if plugin1 is newer than plugin2
124    private static bool IsNewerThan(IPluginDescription plugin1, IPluginDescription plugin2) {
125      // newer: build version is higher, or if build version is the same revision is higher
126      return plugin1.Version.Build > plugin2.Version.Build ||
127        (plugin1.Version.Build == plugin2.Version.Build && plugin1.Version.Revision > plugin2.Version.Revision);
128    }
129    #endregion
130
131    private void UpdateControl() {
132      ClearPluginList();
133      if (pluginManager != null) {
134        localPluginsListView.SuppressItemCheckedEvents = true;
135        foreach (var plugin in pluginManager.Plugins) {
136          var item = CreateListViewItem(plugin);
137          if (plugin.PluginState == PluginState.Enabled) {
138            item.Group = enabledPluginsGroup;
139          } else if (plugin.PluginState == PluginState.Disabled) {
140            item.Group = disabledPluginsGroup;
141          }
142          localPluginsListView.Items.Add(item);
143        }
144        localPluginsListView.SuppressItemCheckedEvents = false;
145      }
146      removeButton.Enabled = localPluginsListView.CheckedItems.Count > 0;
147      updateSelectedButton.Enabled = localPluginsListView.CheckedItems.Count > 0;
148      Util.ResizeColumns(localPluginsListView.Columns.OfType<ColumnHeader>());
149    }
150
151    private void ClearPluginList() {
152      List<ListViewItem> itemsToRemove = new List<ListViewItem>(localPluginsListView.Items.OfType<ListViewItem>());
153      itemsToRemove.ForEach(item => localPluginsListView.Items.Remove(item));
154    }
155
156    private static ListViewItem CreateListViewItem(PluginDescription plugin) {
157      ListViewItem item = new ListViewItem(new string[] { plugin.Name, plugin.Version.ToString(), plugin.Description });
158      item.Tag = plugin;
159      item.ImageIndex = 0;
160      return item;
161    }
162
163    private void pluginsListView_ItemChecked(object sender, ItemCheckedEventArgs e) {
164      // checked items are marked for removal
165      if (e.Item.Checked) {
166        List<ListViewItem> modifiedItems = new List<ListViewItem>();
167        foreach (ListViewItem item in localPluginsListView.SelectedItems) {
168          modifiedItems.Add(item);
169          int oldItemsCount = 0;
170          while (oldItemsCount < modifiedItems.Count) {
171            oldItemsCount = modifiedItems.Count;
172            var oldModifiedItems = new List<ListViewItem>(modifiedItems);
173            foreach (var modifiedItem in oldModifiedItems) {
174              var plugin = (IPluginDescription)modifiedItem.Tag;
175              // also check all dependent plugins
176              foreach (ListViewItem dependentItem in localPluginsListView.Items) {
177                var dependent = (IPluginDescription)dependentItem.Tag;
178                if (!modifiedItems.Contains(dependentItem) &&
179                  !dependentItem.Checked && (from dep in dependent.Dependencies
180                                             where dep.Name == plugin.Name
181                                             where dep.Version == plugin.Version
182                                             select dep).Any()) {
183                  modifiedItems.Add(dependentItem);
184                }
185              }
186            }
187          }
188        }
189        localPluginsListView.CheckItems(modifiedItems);
190      } else {
191        List<ListViewItem> modifiedItems = new List<ListViewItem>();
192        foreach (ListViewItem item in localPluginsListView.SelectedItems) {
193          modifiedItems.Add(item);
194        }
195        localPluginsListView.UncheckItems(modifiedItems);
196      }
197      OnItemsCheckedChanged(EventArgs.Empty);
198    }
199
200    private void localPluginsListView_ItemActivate(object sender, EventArgs e) {
201      if (localPluginsListView.SelectedItems.Count > 0) {
202        var plugin = (PluginDescription)localPluginsListView.SelectedItems[0].Tag;
203        PluginView pluginView = new PluginView(plugin);
204        pluginView.Show(this);
205      }
206    }
207
208    private void OnItemsCheckedChanged(EventArgs eventArgs) {
209      removeButton.Enabled = localPluginsListView.CheckedItems.Count > 0;
210      updateSelectedButton.Enabled = localPluginsListView.CheckedItems.Count > 0;
211    }
212
213    private void updateSelectedButton_Click(object sender, EventArgs e) {
214      StatusView.LockUI();
215      StatusView.ShowProgressIndicator();
216      StatusView.RemoveMessage(NoUpdatesAvailableMessage);
217      StatusView.ShowMessage(CheckingPluginsMessage);
218      var checkedPlugins = localPluginsListView.CheckedItems.OfType<ListViewItem>()
219        .Select(item => item.Tag)
220        .OfType<IPluginDescription>()
221        .ToList();
222      updatePluginsBackgroundWorker.RunWorkerAsync(checkedPlugins);
223    }
224
225    private void removeButton_Click(object sender, EventArgs e) {
226      StatusView.LockUI();
227      StatusView.ShowProgressIndicator();
228      var checkedPlugins = localPluginsListView.CheckedItems.OfType<ListViewItem>()
229        .Select(item => item.Tag)
230        .OfType<IPluginDescription>()
231        .ToList();
232      removePluginsBackgroundWorker.RunWorkerAsync(checkedPlugins);
233    }
234
235    private void refreshButton_Click(object sender, EventArgs e) {
236      StatusView.LockUI();
237      StatusView.ShowProgressIndicator();
238      // refresh = update empty list of plugins (plugins are reloaded)
239      updatePluginsBackgroundWorker.RunWorkerAsync(new IPluginDescription[0]);
240    }
241  }
242}
Note: See TracBrowser for help on using the repository browser.