source: stable/HeuristicLab.PluginInfrastructure/3.3/Advanced/PluginUpdaterForm.cs @ 11121

Last change on this file since 11121 was 11121, checked in by ascheibe, 5 years ago

#2153 merged r11082, r11113, r11117 into stable

File size: 11.3 KB
RevLine 
[6413]1#region License Information
2/* HeuristicLab
[9456]3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[6413]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
21using System;
22using System.Collections.Generic;
23using System.ComponentModel;
24using System.IO;
25using System.Linq;
26using System.Text;
27using System.Windows.Forms;
28using HeuristicLab.PluginInfrastructure.Manager;
29
30namespace HeuristicLab.PluginInfrastructure.Advanced {
31  internal partial class PluginUpdaterForm : Form, IStatusView {
32    private InstallationManager installationManager;
33    private PluginManager pluginManager;
34    private string pluginDir;
35    private BackgroundWorker updatePluginsBackgroundWorker;
36
37    public PluginUpdaterForm(PluginManager pluginManager)
38      : base() {
39      InitializeComponent();
[11121]40      Text = "HeuristicLab Plugin Manager " + AssemblyHelpers.GetFileVersion(GetType().Assembly);
[6413]41      pluginManager.PluginLoaded += pluginManager_PluginLoaded;
42      pluginManager.PluginUnloaded += pluginManager_PluginUnloaded;
43      pluginManager.Initializing += pluginManager_Initializing;
44      pluginManager.Initialized += pluginManager_Initialized;
45
46      pluginDir = Application.StartupPath;
47
48      installationManager = new InstallationManager(pluginDir);
49      installationManager.PluginInstalled += new EventHandler<PluginInfrastructureEventArgs>(installationManager_PluginInstalled);
50      installationManager.PluginRemoved += new EventHandler<PluginInfrastructureEventArgs>(installationManager_PluginRemoved);
51      installationManager.PluginUpdated += new EventHandler<PluginInfrastructureEventArgs>(installationManager_PluginUpdated);
52      installationManager.PreInstallPlugin += new EventHandler<PluginInfrastructureCancelEventArgs>(installationManager_PreInstallPlugin);
53      installationManager.PreRemovePlugin += new EventHandler<PluginInfrastructureCancelEventArgs>(installationManager_PreRemovePlugin);
54      installationManager.PreUpdatePlugin += new EventHandler<PluginInfrastructureCancelEventArgs>(installationManager_PreUpdatePlugin);
55
56      this.pluginManager = pluginManager;
57
58      updatePluginsBackgroundWorker = new BackgroundWorker();
59      updatePluginsBackgroundWorker.DoWork += new DoWorkEventHandler(updatePluginsBackgroundWorker_DoWork);
60      updatePluginsBackgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(updatePluginsBackgroundWorker_RunWorkerCompleted);
61    }
62
63
64    private void PluginUpdaterForm_Load(object sender, EventArgs e) {
65      updatePluginsBackgroundWorker.RunWorkerAsync();
66      ShowProgressIndicator();
67      ShowMessage("Downloading and installing plugins...");
68    }
69
70    #region event handlers for update plugins backgroundworker
71    void updatePluginsBackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
72      if (e.Error != null) {
73        ShowError("Installation Error", "There was a problem while downloading and installing updates." + Environment.NewLine + "Please check your connection settings.");
74      } else {
75        Close();
76      }
77    }
78
79    void updatePluginsBackgroundWorker_DoWork(object sender, DoWorkEventArgs e) {
80      IEnumerable<IPluginDescription> installedPlugins = pluginManager.Plugins.OfType<IPluginDescription>().ToList();
81      var remotePlugins = installationManager.GetRemotePluginList();
82      // if there is a local plugin with same name and same major and minor version then it's an update
83      var pluginsToUpdate = from remotePlugin in remotePlugins
84                            let matchingLocalPlugins = from installedPlugin in installedPlugins
85                                                       where installedPlugin.Name == remotePlugin.Name
86                                                       where installedPlugin.Version.Major == remotePlugin.Version.Major
87                                                       where installedPlugin.Version.Minor == remotePlugin.Version.Minor
88                                                       where Util.IsNewerThan(remotePlugin, installedPlugin)
89                                                       select installedPlugin
90                            where matchingLocalPlugins.Count() > 0
91                            select remotePlugin;
92      if (pluginsToUpdate.Count() > 0) {
93        bool canceled;
94        installationManager.Update(pluginsToUpdate, out canceled);
95        if (!canceled)
96          pluginManager.DiscoverAndCheckPlugins();
97        e.Cancel = false;
98      }
99    }
100    #endregion
101
102    #region plugin manager event handlers
103    private void pluginManager_Initialized(object sender, PluginInfrastructureEventArgs e) {
104      SetStatusStrip("Initialized PluginInfrastructure");
105    }
106
107    private void pluginManager_Initializing(object sender, PluginInfrastructureEventArgs e) {
108      SetStatusStrip("Initializing PluginInfrastructure");
109    }
110
111    private void pluginManager_PluginUnloaded(object sender, PluginInfrastructureEventArgs e) {
112      SetStatusStrip("Unloaded " + e.Entity);
113    }
114
115    private void pluginManager_PluginLoaded(object sender, PluginInfrastructureEventArgs e) {
116      SetStatusStrip("Loaded " + e.Entity);
117    }
118    #endregion
119
120    #region installation manager event handlers
121    private void installationManager_PreUpdatePlugin(object sender, PluginInfrastructureCancelEventArgs e) {
122      if (e.Plugins.Count() > 0) {
123        e.Cancel = (bool)Invoke((Func<IEnumerable<IPluginDescription>, bool>)ConfirmUpdateAction, e.Plugins) == false;
124      }
125    }
126
127    private void installationManager_PreRemovePlugin(object sender, PluginInfrastructureCancelEventArgs e) {
128      if (e.Plugins.Count() > 0) {
129        e.Cancel = (bool)Invoke((Func<IEnumerable<IPluginDescription>, bool>)ConfirmRemoveAction, e.Plugins) == false;
130      }
131    }
132
133    private void installationManager_PreInstallPlugin(object sender, PluginInfrastructureCancelEventArgs e) {
134      if (e.Plugins.Count() > 0)
135        if ((bool)Invoke((Func<IEnumerable<IPluginDescription>, bool>)ConfirmInstallAction, e.Plugins) == true) {
136          SetStatusStrip("Installing " + e.Plugins.Aggregate("", (a, b) => a.ToString() + "; " + b.ToString()));
137          e.Cancel = false;
138        } else {
139          e.Cancel = true;
140          SetStatusStrip("Install canceled");
141        }
142    }
143
144    private void installationManager_PluginUpdated(object sender, PluginInfrastructureEventArgs e) {
145      SetStatusStrip("Updated " + e.Entity);
146    }
147
148    private void installationManager_PluginRemoved(object sender, PluginInfrastructureEventArgs e) {
149      SetStatusStrip("Removed " + e.Entity);
150    }
151
152    private void installationManager_PluginInstalled(object sender, PluginInfrastructureEventArgs e) {
153      SetStatusStrip("Installed " + e.Entity);
154    }
155    #endregion
156
157    #region confirmation dialogs
158    private bool ConfirmRemoveAction(IEnumerable<IPluginDescription> plugins) {
159      StringBuilder strBuilder = new StringBuilder();
160      foreach (var plugin in plugins) {
161        foreach (var file in plugin.Files) {
162          strBuilder.AppendLine(Path.GetFileName(file.Name));
163        }
164      }
165      using (var confirmationDialog = new ConfirmationDialog("Confirm Delete", "Do you want to delete following files?", strBuilder.ToString())) {
166        return (confirmationDialog.ShowDialog(this)) == DialogResult.OK;
167      }
168    }
169
170    private bool ConfirmUpdateAction(IEnumerable<IPluginDescription> plugins) {
171      StringBuilder strBuilder = new StringBuilder();
172      foreach (var plugin in plugins) {
173        strBuilder.AppendLine(plugin.ToString());
174      }
175      using (var confirmationDialog = new ConfirmationDialog("Confirm Update", "Do you want to update following plugins?", strBuilder.ToString())) {
176        return (confirmationDialog.ShowDialog(this)) == DialogResult.OK;
177      }
178    }
179
180    private bool ConfirmInstallAction(IEnumerable<IPluginDescription> plugins) {
181      foreach (var plugin in plugins) {
182        if (!string.IsNullOrEmpty(plugin.LicenseText)) {
183          using (var licenseConfirmationBox = new LicenseConfirmationDialog(plugin)) {
184            if (licenseConfirmationBox.ShowDialog(this) != DialogResult.OK)
185              return false;
186          }
187        }
188      }
189      return true;
190    }
191
192
193    #endregion
194
195    #region helper methods
196    private void SetStatusStrip(string msg) {
197      if (InvokeRequired) Invoke((Action<string>)SetStatusStrip, msg);
198      else {
199        statusLabel.Text = msg;
200      }
201    }
202
203    #endregion
204
205
206    protected override void OnClosing(CancelEventArgs e) {
207      installationManager.PluginInstalled -= new EventHandler<PluginInfrastructureEventArgs>(installationManager_PluginInstalled);
208      installationManager.PluginRemoved -= new EventHandler<PluginInfrastructureEventArgs>(installationManager_PluginRemoved);
209      installationManager.PluginUpdated -= new EventHandler<PluginInfrastructureEventArgs>(installationManager_PluginUpdated);
210      installationManager.PreInstallPlugin -= new EventHandler<PluginInfrastructureCancelEventArgs>(installationManager_PreInstallPlugin);
211      installationManager.PreRemovePlugin -= new EventHandler<PluginInfrastructureCancelEventArgs>(installationManager_PreRemovePlugin);
212      installationManager.PreUpdatePlugin -= new EventHandler<PluginInfrastructureCancelEventArgs>(installationManager_PreUpdatePlugin);
213      base.OnClosing(e);
214    }
215
216    #region IStatusView Members
217
218    public void ShowProgressIndicator(double percentProgress) {
219      if (percentProgress < 0.0 || percentProgress > 1.0) throw new ArgumentException("percentProgress");
220      progressBar.Visible = true;
221      progressBar.Style = ProgressBarStyle.Continuous;
222      int range = progressBar.Maximum - progressBar.Minimum;
223      progressBar.Value = (int)(percentProgress * range + progressBar.Minimum);
224    }
225
226    public void ShowProgressIndicator() {
227      progressBar.Visible = true;
228      progressBar.Style = ProgressBarStyle.Marquee;
229    }
230
231    public void HideProgressIndicator() {
232      progressBar.Visible = false;
233    }
234
235    public void ShowMessage(string message) {
236      statusLabel.Text = message;
237    }
238
239    public void RemoveMessage(string message) {
240      statusLabel.Text = string.Empty;
241    }
242
243    public void LockUI() {
244      Cursor = Cursors.AppStarting;
245    }
246    public void UnlockUI() {
247      Cursor = Cursors.Default;
248    }
249    public void ShowError(string shortMessage, string description) {
250      progressBar.Visible = false;
251      okButton.Visible = true;
252      statusLabel.Text = shortMessage + Environment.NewLine + description;
253      this.Cursor = Cursors.Default;
254    }
255    #endregion
256
257    private void okButton_Click(object sender, EventArgs e) {
258      Close();
259    }
260  }
261}
Note: See TracBrowser for help on using the repository browser.