Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.2/sources/HeuristicLab.PluginInfrastructure/Advanced/InstallationManagerForm.cs @ 4483

Last change on this file since 4483 was 3573, checked in by gkronber, 15 years ago

Implemented a list of review comments by swagner. #989 (Implement review comments in plugin infrastructure)

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