Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.PluginInfrastructure/Advanced/LocalPluginsView.cs @ 3606

Last change on this file since 3606 was 3600, checked in by gkronber, 15 years ago

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

File size: 10.6 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 LocalPluginsView : 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 LocalPluginsView()
61      : base() {
62      InitializeComponent();
63      enabledPluginsGroup = localPluginsListView.Groups["activePluginsGroup"];
64      disabledPluginsGroup = localPluginsListView.Groups["disabledPluginsGroup"];
65
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      } else if (e.Cancelled) {
104        StatusView.ShowMessage(NoUpdatesAvailableMessage);
105      }
106      StatusView.RemoveMessage(CheckingPluginsMessage);
107      StatusView.HideProgressIndicator();
108      StatusView.UnlockUI();
109    }
110
111    void updatePluginsBackgroundWorker_DoWork(object sender, DoWorkEventArgs e) {
112      IEnumerable<IPluginDescription> selectedPlugins = (IEnumerable<IPluginDescription>)e.Argument;
113      var remotePlugins = installationManager.GetRemotePluginList();
114      // if there is a local plugin with same name and same major and minor version then it's an update
115      var pluginsToUpdate = from remotePlugin in remotePlugins
116                            let matchingLocalPlugins = from installedPlugin in selectedPlugins
117                                                       where installedPlugin.Name == remotePlugin.Name
118                                                       where installedPlugin.Version.Major == remotePlugin.Version.Major
119                                                       where installedPlugin.Version.Minor == remotePlugin.Version.Minor
120                                                       where IsNewerThan(remotePlugin, installedPlugin)
121                                                       select installedPlugin
122                            where matchingLocalPlugins.Count() > 0
123                            select remotePlugin;
124      if (pluginsToUpdate.Count() > 0) {
125        installationManager.Update(pluginsToUpdate);
126        pluginManager.DiscoverAndCheckPlugins();
127        e.Cancel = false;
128      } else {
129        e.Cancel = true;
130      }
131    }
132
133    private bool IsNewerThan(IPluginDescription plugin1, IPluginDescription plugin2) {
134      // newer: build version is higher, or if build version is the same revision is higher
135      if (plugin1.Version.Build < plugin2.Version.Build) return false;
136      else if (plugin1.Version.Build > plugin2.Version.Build) return true;
137      else return plugin1.Version.Revision > plugin2.Version.Revision;
138    }
139    #endregion
140
141    private void UpdateControl() {
142      ClearPluginList();
143      if (pluginManager != null) {
144        localPluginsListView.SuppressItemCheckedEvents = true;
145        foreach (var plugin in pluginManager.Plugins) {
146          var item = CreateListViewItem(plugin);
147          if (plugin.PluginState == PluginState.Enabled) {
148            item.Group = enabledPluginsGroup;
149          } else if (plugin.PluginState == PluginState.Disabled) {
150            item.Group = disabledPluginsGroup;
151          }
152          localPluginsListView.Items.Add(item);
153        }
154        localPluginsListView.SuppressItemCheckedEvents = false;
155      }
156      removeButton.Enabled = localPluginsListView.CheckedItems.Count > 0;
157      updateSelectedButton.Enabled = localPluginsListView.CheckedItems.Count > 0;
158      foreach (ColumnHeader column in localPluginsListView.Columns) {
159        if (localPluginsListView.Items.Count > 0)
160          column.AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
161        else column.AutoResize(ColumnHeaderAutoResizeStyle.HeaderSize);
162      }
163    }
164
165    private void ClearPluginList() {
166      List<ListViewItem> itemsToRemove = new List<ListViewItem>(localPluginsListView.Items.OfType<ListViewItem>());
167      itemsToRemove.ForEach(item => localPluginsListView.Items.Remove(item));
168    }
169
170    private ListViewItem CreateListViewItem(PluginDescription plugin) {
171      ListViewItem item = new ListViewItem(new string[] { plugin.Name, plugin.Version.ToString(), plugin.Description });
172      item.Tag = plugin;
173      return item;
174    }
175
176    private void pluginsListView_ItemChecked(object sender, ItemCheckedEventArgs e) {
177      // checked items are marked for removal
178      if (e.Item.Checked) {
179        List<ListViewItem> modifiedItems = new List<ListViewItem>();
180        foreach (ListViewItem item in localPluginsListView.SelectedItems) {
181          var plugin = (IPluginDescription)item.Tag;
182          modifiedItems.Add(item);
183          // also uncheck all dependent plugins
184          foreach (ListViewItem dependentItem in localPluginsListView.Items) {
185            var dependent = (IPluginDescription)dependentItem.Tag;
186            if (!dependentItem.Checked && (from dep in dependent.Dependencies
187                                           where dep.Name == plugin.Name
188                                           where dep.Version == plugin.Version
189                                           select dep).Any()) {
190              modifiedItems.Add(dependentItem);
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}
Note: See TracBrowser for help on using the repository browser.