Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.PluginInfrastructure/3.3/Advanced/InstallationManagerForm.cs @ 15986

Last change on this file since 15986 was 15584, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers on stable

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