Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.2/sources/HeuristicLab.PluginInfrastructure/Advanced/LocalPluginsView.cs @ 10159

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

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

File size: 10.3 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    }
159
160    private void ClearPluginList() {
161      List<ListViewItem> itemsToRemove = new List<ListViewItem>(localPluginsListView.Items.OfType<ListViewItem>());
162      itemsToRemove.ForEach(item => localPluginsListView.Items.Remove(item));
163    }
164
165    private ListViewItem CreateListViewItem(PluginDescription plugin) {
166      ListViewItem item = new ListViewItem(new string[] { plugin.Name, plugin.Version.ToString(), plugin.Description });
167      item.Tag = plugin;
168      return item;
169    }
170
171    private void pluginsListView_ItemChecked(object sender, ItemCheckedEventArgs e) {
172      // checked items are marked for removal
173      if (e.Item.Checked) {
174        List<ListViewItem> modifiedItems = new List<ListViewItem>();
175        foreach (ListViewItem item in localPluginsListView.SelectedItems) {
176          var plugin = (IPluginDescription)item.Tag;
177          modifiedItems.Add(item);
178          // also uncheck all dependent plugins
179          foreach (ListViewItem dependentItem in localPluginsListView.Items) {
180            var dependent = (IPluginDescription)dependentItem.Tag;
181            if (!dependentItem.Checked && (from dep in dependent.Dependencies
182                                           where dep.Name == plugin.Name
183                                           where dep.Version == plugin.Version
184                                           select dep).Any()) {
185              modifiedItems.Add(dependentItem);
186            }
187          }
188        }
189        localPluginsListView.CheckItems(modifiedItems);
190      } else {
191        List<ListViewItem> modifiedItems = new List<ListViewItem>();
192        foreach (ListViewItem item in localPluginsListView.SelectedItems) {
193          var plugin = (IPluginDescription)item.Tag;
194          modifiedItems.Add(item);
195        }
196        localPluginsListView.UncheckItems(modifiedItems);
197      }
198      OnItemsCheckedChanged(EventArgs.Empty);
199    }
200
201    private void localPluginsListView_ItemActivate(object sender, EventArgs e) {
202      if (localPluginsListView.SelectedItems.Count > 0) {
203        var plugin = (PluginDescription)localPluginsListView.SelectedItems[0].Tag;
204        PluginView pluginView = new PluginView(plugin);
205        pluginView.Show();
206      }
207    }
208
209    private void OnItemsCheckedChanged(EventArgs eventArgs) {
210      removeButton.Enabled = localPluginsListView.CheckedItems.Count > 0;
211      updateSelectedButton.Enabled = localPluginsListView.CheckedItems.Count > 0;
212    }
213
214    private void updateSelectedButton_Click(object sender, EventArgs e) {
215      StatusView.LockUI();
216      StatusView.ShowProgressIndicator();
217      StatusView.RemoveMessage(NoUpdatesAvailableMessage);
218      StatusView.ShowMessage(CheckingPluginsMessage);
219      var checkedPlugins = localPluginsListView.CheckedItems.OfType<ListViewItem>()
220        .Select(item => item.Tag)
221        .OfType<IPluginDescription>()
222        .ToList();
223      updatePluginsBackgroundWorker.RunWorkerAsync(checkedPlugins);
224    }
225
226    private void removeButton_Click(object sender, EventArgs e) {
227      StatusView.LockUI();
228      StatusView.ShowProgressIndicator();
229      var checkedPlugins = localPluginsListView.CheckedItems.OfType<ListViewItem>()
230        .Select(item => item.Tag)
231        .OfType<IPluginDescription>()
232        .ToList();
233      removePluginsBackgroundWorker.RunWorkerAsync(checkedPlugins);
234    }
235  }
236}
Note: See TracBrowser for help on using the repository browser.