Free cookie consent management tool by TermsFeed Policy Generator

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

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

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