Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3749 was 3721, checked in by gkronber, 15 years ago

Cleaned resources in plugin infrastructure. #994 (Clean up plugin infrastructure project)

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