Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 9952 was 9456, checked in by swagner, 12 years ago

Updated copyright year and added some missing license headers (#1889)

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