Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GP.Grammar.Editor/HeuristicLab.PluginInfrastructure/3.3/Advanced/InstallationManagerForm.cs @ 6319

Last change on this file since 6319 was 5445, checked in by swagner, 14 years ago

Updated year of copyrights (#1406)

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