Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4495 was 4482, checked in by gkronber, 14 years ago

Preparations for next HL release. #1203

File size: 10.8 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 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      StatusView.UnlockUI();
101    }
102
103    void updatePluginsBackgroundWorker_DoWork(object sender, DoWorkEventArgs e) {
104      IEnumerable<IPluginDescription> selectedPlugins = (IEnumerable<IPluginDescription>)e.Argument;
105      var remotePlugins = installationManager.GetRemotePluginList();
106      // if there is a local plugin with same name and same major and minor version then it's an update
107      var pluginsToUpdate = from remotePlugin in remotePlugins
108                            let matchingLocalPlugins = from installedPlugin in selectedPlugins
109                                                       where installedPlugin.Name == remotePlugin.Name
110                                                       where installedPlugin.Version.Major == remotePlugin.Version.Major
111                                                       where installedPlugin.Version.Minor == remotePlugin.Version.Minor
112                                                       where IsNewerThan(remotePlugin, installedPlugin)
113                                                       select installedPlugin
114                            where matchingLocalPlugins.Count() > 0
115                            select remotePlugin;
116      if (pluginsToUpdate.Count() > 0) {
117        installationManager.Update(pluginsToUpdate);
118      }
119      pluginManager.DiscoverAndCheckPlugins();
120    }
121
122    private static bool IsNewerThan(IPluginDescription plugin1, IPluginDescription plugin2) {
123      // newer: build version is higher, or if build version is the same revision is higher
124      if (plugin1.Version.Build < plugin2.Version.Build) return false;
125      else if (plugin1.Version.Build > plugin2.Version.Build) return true;
126      else return plugin1.Version.Revision > plugin2.Version.Revision;
127    }
128    #endregion
129
130    private void UpdateControl() {
131      ClearPluginList();
132      if (pluginManager != null) {
133        localPluginsListView.SuppressItemCheckedEvents = true;
134        foreach (var plugin in pluginManager.Plugins) {
135          var item = CreateListViewItem(plugin);
136          if (plugin.PluginState == PluginState.Enabled) {
137            item.Group = enabledPluginsGroup;
138          } else if (plugin.PluginState == PluginState.Disabled) {
139            item.Group = disabledPluginsGroup;
140          }
141          localPluginsListView.Items.Add(item);
142        }
143        localPluginsListView.SuppressItemCheckedEvents = false;
144      }
145      removeButton.Enabled = localPluginsListView.CheckedItems.Count > 0;
146      updateSelectedButton.Enabled = localPluginsListView.CheckedItems.Count > 0;
147      Util.ResizeColumns(localPluginsListView.Columns.OfType<ColumnHeader>());
148    }
149
150    private void ClearPluginList() {
151      List<ListViewItem> itemsToRemove = new List<ListViewItem>(localPluginsListView.Items.OfType<ListViewItem>());
152      itemsToRemove.ForEach(item => localPluginsListView.Items.Remove(item));
153    }
154
155    private static ListViewItem CreateListViewItem(PluginDescription plugin) {
156      ListViewItem item = new ListViewItem(new string[] { plugin.Name, plugin.Version.ToString(), plugin.Description });
157      item.Tag = plugin;
158      item.ImageIndex = 0;
159      return item;
160    }
161
162    private void pluginsListView_ItemChecked(object sender, ItemCheckedEventArgs e) {
163      // checked items are marked for removal
164      if (e.Item.Checked) {
165        List<ListViewItem> modifiedItems = new List<ListViewItem>();
166        foreach (ListViewItem item in localPluginsListView.SelectedItems) {
167          modifiedItems.Add(item);
168          int oldItemsCount = 0;
169          while (oldItemsCount < modifiedItems.Count) {
170            oldItemsCount = modifiedItems.Count;
171            var oldModifiedItems = new List<ListViewItem>(modifiedItems);
172            foreach (var modifiedItem in oldModifiedItems) {
173              var plugin = (IPluginDescription)modifiedItem.Tag;
174              // also check all dependent plugins
175              foreach (ListViewItem dependentItem in localPluginsListView.Items) {
176                var dependent = (IPluginDescription)dependentItem.Tag;
177                if (!modifiedItems.Contains(dependentItem) &&
178                  !dependentItem.Checked && (from dep in dependent.Dependencies
179                                             where dep.Name == plugin.Name
180                                             where dep.Version == plugin.Version
181                                             select dep).Any()) {
182                  modifiedItems.Add(dependentItem);
183                }
184              }
185            }
186          }
187        }
188        localPluginsListView.CheckItems(modifiedItems);
189      } else {
190        List<ListViewItem> modifiedItems = new List<ListViewItem>();
191        foreach (ListViewItem item in localPluginsListView.SelectedItems) {
192          modifiedItems.Add(item);
193        }
194        localPluginsListView.UncheckItems(modifiedItems);
195      }
196      OnItemsCheckedChanged(EventArgs.Empty);
197    }
198
199    private void localPluginsListView_ItemActivate(object sender, EventArgs e) {
200      if (localPluginsListView.SelectedItems.Count > 0) {
201        var plugin = (PluginDescription)localPluginsListView.SelectedItems[0].Tag;
202        PluginView pluginView = new PluginView(plugin);
203        pluginView.Show(this);
204      }
205    }
206
207    private void OnItemsCheckedChanged(EventArgs eventArgs) {
208      removeButton.Enabled = localPluginsListView.CheckedItems.Count > 0;
209      updateSelectedButton.Enabled = localPluginsListView.CheckedItems.Count > 0;
210    }
211
212    private void updateSelectedButton_Click(object sender, EventArgs e) {
213      StatusView.LockUI();
214      StatusView.ShowProgressIndicator();
215      StatusView.RemoveMessage(NoUpdatesAvailableMessage);
216      StatusView.ShowMessage(CheckingPluginsMessage);
217      var checkedPlugins = localPluginsListView.CheckedItems.OfType<ListViewItem>()
218        .Select(item => item.Tag)
219        .OfType<IPluginDescription>()
220        .ToList();
221      updatePluginsBackgroundWorker.RunWorkerAsync(checkedPlugins);
222    }
223
224    private void removeButton_Click(object sender, EventArgs e) {
225      StatusView.LockUI();
226      StatusView.ShowProgressIndicator();
227      var checkedPlugins = localPluginsListView.CheckedItems.OfType<ListViewItem>()
228        .Select(item => item.Tag)
229        .OfType<IPluginDescription>()
230        .ToList();
231      removePluginsBackgroundWorker.RunWorkerAsync(checkedPlugins);
232    }
233
234    private void refreshButton_Click(object sender, EventArgs e) {
235      StatusView.LockUI();
236      StatusView.ShowProgressIndicator();
237      // refresh = update empty list of plugins (plugins are reloaded)
238      updatePluginsBackgroundWorker.RunWorkerAsync(new IPluginDescription[0]);
239    }
240  }
241}
Note: See TracBrowser for help on using the repository browser.