Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.PluginInfrastructure/Advanced/InstallationManagerForm.cs @ 3600

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

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

File size: 10.2 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
21using System;
22using System.Collections.Generic;
23using System.ComponentModel;
24using System.Data;
25using System.Drawing;
26using System.Linq;
27using System.Text;
28using System.Windows.Forms;
29using System.IO;
30using HeuristicLab.PluginInfrastructure.Manager;
31using System.Diagnostics;
32
33namespace HeuristicLab.PluginInfrastructure.Advanced {
34  internal partial class InstallationManagerForm : Form, IStatusView {
35    private InstallationManager installationManager;
36    private PluginManager pluginManager;
37    private string pluginDir;
38
39    public InstallationManagerForm(PluginManager pluginManager) {
40      InitializeComponent();
41      FileVersionInfo pluginInfrastructureVersion = FileVersionInfo.GetVersionInfo(GetType().Assembly.Location);
42      Text = "HeuristicLab Plugin Manager " + pluginInfrastructureVersion.FileVersion;
43     
44      this.pluginManager = pluginManager;
45
46      pluginManager.PluginLoaded += pluginManager_PluginLoaded;
47      pluginManager.PluginUnloaded += pluginManager_PluginUnloaded;
48      pluginManager.Initializing += pluginManager_Initializing;
49      pluginManager.Initialized += pluginManager_Initialized;
50
51      pluginDir = Application.StartupPath;
52
53      installationManager = new InstallationManager(pluginDir);
54      installationManager.PluginInstalled += new EventHandler<PluginInfrastructureEventArgs>(installationManager_PluginInstalled);
55      installationManager.PluginRemoved += new EventHandler<PluginInfrastructureEventArgs>(installationManager_PluginRemoved);
56      installationManager.PluginUpdated += new EventHandler<PluginInfrastructureEventArgs>(installationManager_PluginUpdated);
57      installationManager.PreInstallPlugin += new EventHandler<PluginInfrastructureCancelEventArgs>(installationManager_PreInstallPlugin);
58      installationManager.PreRemovePlugin += new EventHandler<PluginInfrastructureCancelEventArgs>(installationManager_PreRemovePlugin);
59      installationManager.PreUpdatePlugin += new EventHandler<PluginInfrastructureCancelEventArgs>(installationManager_PreUpdatePlugin);
60
61      // show or hide controls for uploading plugins based on setting
62      if (!HeuristicLab.PluginInfrastructure.Properties.Settings.Default.ShowPluginUploadControls) {
63        tabControl.Controls.Remove(uploadPluginsTabPage);
64        tabControl.Controls.Remove(manageProductsTabPage);
65      } else {
66        pluginEditor.PluginManager = pluginManager;
67      }
68
69      localPluginsView.StatusView = this;
70      localPluginsView.PluginManager = pluginManager;
71      localPluginsView.InstallationManager = installationManager;
72
73      basicUpdateView.StatusView = this;
74      basicUpdateView.PluginManager = pluginManager;
75      basicUpdateView.InstallationManager = installationManager;
76
77      remotePluginInstaller.StatusView = this;
78      remotePluginInstaller.InstallationManager = installationManager;
79      remotePluginInstaller.PluginManager = pluginManager;
80    }
81
82
83    #region plugin manager event handlers
84    void pluginManager_Initialized(object sender, PluginInfrastructureEventArgs e) {
85      SetStatusStrip("Initialized PluginInfrastructure");
86    }
87
88    void pluginManager_Initializing(object sender, PluginInfrastructureEventArgs e) {
89      SetStatusStrip("Initializing PluginInfrastructure");
90    }
91
92    void pluginManager_PluginUnloaded(object sender, PluginInfrastructureEventArgs e) {
93      SetStatusStrip("Unloaded " + e.Entity);
94    }
95
96    void pluginManager_PluginLoaded(object sender, PluginInfrastructureEventArgs e) {
97      SetStatusStrip("Loaded " + e.Entity);
98    }
99    #endregion
100
101    #region installation manager event handlers
102    void installationManager_PreUpdatePlugin(object sender, PluginInfrastructureCancelEventArgs e) {
103      if (e.Plugins.Count() > 0) {
104        e.Cancel = ConfirmUpdateAction(e.Plugins) == false;
105      }
106    }
107
108    void installationManager_PreRemovePlugin(object sender, PluginInfrastructureCancelEventArgs e) {
109      if (e.Plugins.Count() > 0) {
110        e.Cancel = ConfirmRemoveAction(e.Plugins) == false;
111      }
112    }
113
114    void installationManager_PreInstallPlugin(object sender, PluginInfrastructureCancelEventArgs e) {
115      if (e.Plugins.Count() > 0)
116        if (ConfirmInstallAction(e.Plugins) == true) {
117          SetStatusStrip("Installing " + e.Plugins.Aggregate("", (a, b) => a.ToString() + "; " + b.ToString()));
118          e.Cancel = false;
119        } else {
120          e.Cancel = true;
121          SetStatusStrip("Install canceled");
122        }
123    }
124
125    void installationManager_PluginUpdated(object sender, PluginInfrastructureEventArgs e) {
126      SetStatusStrip("Updated " + e.Entity);
127    }
128
129    void installationManager_PluginRemoved(object sender, PluginInfrastructureEventArgs e) {
130      SetStatusStrip("Removed " + e.Entity);
131    }
132
133    void installationManager_PluginInstalled(object sender, PluginInfrastructureEventArgs e) {
134      SetStatusStrip("Installed " + e.Entity);
135    }
136    #endregion
137
138    #region button events
139    private void connectionSettingsToolStripMenuItem_Click(object sender, EventArgs e) {
140      new ConnectionSetupView().ShowDialog();
141    }
142    #endregion
143
144    #region confirmation dialogs
145    private bool ConfirmRemoveAction(IEnumerable<IPluginDescription> plugins) {
146      StringBuilder strBuilder = new StringBuilder();
147      foreach (var plugin in plugins) {
148        foreach (var file in plugin.Files) {
149          strBuilder.AppendLine(Path.GetFileName(file.Name));
150        }
151      }
152      return (new ConfirmationDialog("Confirm Delete", "Do you want to delete following files?", strBuilder.ToString())).ShowDialog() == DialogResult.OK;
153    }
154
155    private bool ConfirmUpdateAction(IEnumerable<IPluginDescription> plugins) {
156      StringBuilder strBuilder = new StringBuilder();
157      foreach (var plugin in plugins) {
158        strBuilder.AppendLine(plugin.ToString());
159      }
160      return (new ConfirmationDialog("Confirm Update", "Do you want to update following plugins?", strBuilder.ToString())).ShowDialog() == DialogResult.OK;
161    }
162
163    private bool ConfirmInstallAction(IEnumerable<IPluginDescription> plugins) {
164      foreach (var plugin in plugins) {
165        if (!string.IsNullOrEmpty(plugin.LicenseText)) {
166          var licenseConfirmationBox = new LicenseConfirmationBox(plugin);
167          if (licenseConfirmationBox.ShowDialog() != DialogResult.OK)
168            return false;
169        }
170      }
171      return true;
172    }
173
174
175    #endregion
176
177    #region helper methods
178    private void SetStatusStrip(string msg) {
179      if (InvokeRequired) Invoke((Action<string>)SetStatusStrip, msg);
180      else {
181        toolStripStatusLabel.Text = msg;
182        logTextBox.Text += DateTime.Now + ": " + msg + Environment.NewLine;
183      }
184    }
185
186    #endregion
187
188
189    protected override void OnClosing(CancelEventArgs e) {
190      installationManager.PluginInstalled -= new EventHandler<PluginInfrastructureEventArgs>(installationManager_PluginInstalled);
191      installationManager.PluginRemoved -= new EventHandler<PluginInfrastructureEventArgs>(installationManager_PluginRemoved);
192      installationManager.PluginUpdated -= new EventHandler<PluginInfrastructureEventArgs>(installationManager_PluginUpdated);
193      installationManager.PreInstallPlugin -= new EventHandler<PluginInfrastructureCancelEventArgs>(installationManager_PreInstallPlugin);
194      installationManager.PreRemovePlugin -= new EventHandler<PluginInfrastructureCancelEventArgs>(installationManager_PreRemovePlugin);
195      installationManager.PreUpdatePlugin -= new EventHandler<PluginInfrastructureCancelEventArgs>(installationManager_PreUpdatePlugin);
196      base.OnClosing(e);
197    }
198
199    #region IStatusView Members
200
201    public void ShowProgressIndicator(double percentProgress) {
202      if (percentProgress < 0.0 || percentProgress > 1.0) throw new ArgumentException();
203      toolStripProgressBar.Visible = true;
204      toolStripProgressBar.Style = ProgressBarStyle.Continuous;
205      int range = toolStripProgressBar.Maximum - toolStripProgressBar.Minimum;
206      toolStripProgressBar.Value = (int)(percentProgress * range + toolStripProgressBar.Minimum);
207    }
208
209    public void ShowProgressIndicator() {
210      toolStripProgressBar.Visible = true;
211      toolStripProgressBar.Style = ProgressBarStyle.Marquee;
212    }
213
214    public void HideProgressIndicator() {
215      toolStripProgressBar.Visible = false;
216    }
217
218    public void ShowMessage(string message) {
219      if (toolStripStatusLabel.Text == string.Empty)
220        toolStripStatusLabel.Text = message;
221      else
222        toolStripStatusLabel.Text += "; " + message;
223    }
224
225    public void RemoveMessage(string message) {
226      if (toolStripStatusLabel.Text.IndexOf("; " + message) > 0) {
227        toolStripStatusLabel.Text = toolStripStatusLabel.Text.Replace("; " + message, "");
228      }
229      toolStripStatusLabel.Text = toolStripStatusLabel.Text.Replace(message, "");
230      toolStripStatusLabel.Text = toolStripStatusLabel.Text.TrimStart(' ', ';');
231    }
232    public void LockUI() {
233      Cursor = Cursors.AppStarting;
234      tabControl.Enabled = false;
235    }
236    public void UnlockUI() {
237      tabControl.Enabled = true;
238      Cursor = Cursors.Default;
239    }
240    public void ShowError(string shortMessage, string description) {
241      logTextBox.Text += DateTime.Now + ": " + shortMessage + Environment.NewLine + description + Environment.NewLine;
242      MessageBox.Show(description, shortMessage);
243    }
244    #endregion
245
246  }
247}
Note: See TracBrowser for help on using the repository browser.