Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.PluginInfrastructure/3.3/Starter/StarterForm.cs @ 6554

Last change on this file since 6554 was 6554, checked in by gkronber, 13 years ago

#1529: remove 'updates available' icon after installing updates.

File size: 12.0 KB
RevLine 
[2]1#region License Information
2/* HeuristicLab
[5445]3 * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2]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
21
22using System;
23using System.Collections.Generic;
24using System.Diagnostics;
[4068]25using System.IO;
26using System.Linq;
[2]27using System.Threading;
[4068]28using System.Windows.Forms;
29using HeuristicLab.PluginInfrastructure.Advanced;
[2481]30using HeuristicLab.PluginInfrastructure.Manager;
[6413]31using System.Threading.Tasks;
[2]32
[2527]33namespace HeuristicLab.PluginInfrastructure.Starter {
[3092]34  /// <summary>
35  /// The starter form is responsible for initializing the plugin infrastructure
36  /// and shows a list of installed applications.
37  /// </summary>
[2507]38  public partial class StarterForm : Form {
[6413]39    private const string pluginManagerItemName = "Plugin Manager";
40    private const string updatePluginsItemName = "Updates Available";
[2]41
[6413]42
[2]43    private ListViewItem pluginManagerListViewItem;
[1392]44    private bool abortRequested;
[2488]45    private PluginManager pluginManager;
[3113]46    private SplashScreen splashScreen;
[6413]47    private bool updatesAvailable = false;
[3092]48    /// <summary>
49    /// Initializes an instance of the starter form.
50    /// The starter form shows a splashscreen and initializes the plugin infrastructure.
51    /// </summary>
[2507]52    public StarterForm()
[2481]53      : base() {
54      InitializeComponent();
[3748]55      largeImageList.Images.Add(HeuristicLab.PluginInfrastructure.Resources.HeuristicLab.ToBitmap());
[6413]56      largeImageList.Images.Add(HeuristicLab.PluginInfrastructure.Resources.UpdateAvailable.ToBitmap());
[3748]57      smallImageList.Images.Add(HeuristicLab.PluginInfrastructure.Resources.HeuristicLab.ToBitmap());
[6413]58      smallImageList.Images.Add(HeuristicLab.PluginInfrastructure.Resources.UpdateAvailable.ToBitmap());
[3600]59      FileVersionInfo pluginInfrastructureVersion = FileVersionInfo.GetVersionInfo(GetType().Assembly.Location);
60      Text = "HeuristicLab " + pluginInfrastructureVersion.FileVersion;
[2481]61
[2748]62      string pluginPath = Path.GetFullPath(Application.StartupPath);
[2495]63      pluginManager = new PluginManager(pluginPath);
[3113]64      splashScreen = new SplashScreen(pluginManager, 1000);
[3736]65      splashScreen.Show(this, "Loading HeuristicLab...");
[2]66
[2497]67      pluginManager.DiscoverAndCheckPlugins();
[3474]68      UpdateApplicationsList();
[6413]69
[6554]70      CheckUpdatesAvailableAsync();
[2]71    }
72
[6554]73    private void CheckUpdatesAvailableAsync() {
74      string pluginPath = Path.GetFullPath(Application.StartupPath);
[6413]75      var task = Task.Factory.StartNew<bool>(() => {
76        var installationManager = new InstallationManager(pluginPath);
77        IEnumerable<IPluginDescription> installedPlugins = pluginManager.Plugins.OfType<IPluginDescription>();
78        var remotePlugins = installationManager.GetRemotePluginList();
79        // if there is a local plugin with same name and same major and minor version then it's an update
80        var pluginsToUpdate = from remotePlugin in remotePlugins
81                              let matchingLocalPlugins = from installedPlugin in installedPlugins
82                                                         where installedPlugin.Name == remotePlugin.Name
83                                                         where installedPlugin.Version.Major == remotePlugin.Version.Major
84                                                         where installedPlugin.Version.Minor == remotePlugin.Version.Minor
85                                                         where Util.IsNewerThan(remotePlugin, installedPlugin)
86                                                         select installedPlugin
87                              where matchingLocalPlugins.Count() > 0
88                              select remotePlugin;
89        return pluginsToUpdate.Count() > 0;
90      });
91      task.ContinueWith(t => {
[6515]92        try {
93          t.Wait();
94          updatesAvailable = t.Result;
95          UpdateApplicationsList();
96        }
97        catch (AggregateException ae) {
98          ae.Handle(ex => {
99            if (ex is InstallationManagerException) {
100              // this is expected when no internet connection is available => do nothing
101              return true;
[6518]102            } else {
103              return false;
[6515]104            }
105          });
106        }
107      });
[6413]108    }
109
[3092]110    /// <summary>
111    /// Creates a new StarterForm and tries to start application with <paramref name="appName"/> immediately.
112    /// </summary>
113    /// <param name="appName">Name of the application</param>
[2507]114    public StarterForm(string appName)
115      : this() {
116      var appDesc = (from desc in pluginManager.Applications
117                     where desc.Name == appName
[2748]118                     select desc).SingleOrDefault();
[2507]119      if (appDesc != null) {
120        StartApplication(appDesc);
121      } else {
122        MessageBox.Show("Cannot start application " + appName + ".",
123                        "HeuristicLab",
124                        MessageBoxButtons.OK,
125                        MessageBoxIcon.Warning);
126      }
127    }
128
[2]129    private void applicationsListView_ItemActivate(object sender, EventArgs e) {
[1394]130      if (applicationsListView.SelectedItems.Count > 0) {
[2]131        ListViewItem selected = applicationsListView.SelectedItems[0];
[6413]132        if (selected.Text == pluginManagerItemName) {
[2922]133          if (pluginManager.Plugins.Any(x => x.PluginState == PluginState.Loaded)) {
134            MessageBox.Show("Installation Manager cannot be started while another HeuristicLab application is active." + Environment.NewLine +
[3573]135              "Please stop all active HeuristicLab applications and try again.", "Plugin Manager",
136              MessageBoxButtons.OK, MessageBoxIcon.Information);
[2922]137          } else {
138            try {
139              Cursor = Cursors.AppStarting;
[4482]140              using (InstallationManagerForm form = new InstallationManagerForm(pluginManager)) {
141                form.ShowDialog(this);
142              }
[3474]143              UpdateApplicationsList();
[2922]144            }
145            finally {
146              Cursor = Cursors.Arrow;
147            }
[1394]148          }
[6413]149        } else if (selected.Text == updatePluginsItemName) {
150          if (pluginManager.Plugins.Any(x => x.PluginState == PluginState.Loaded)) {
151            MessageBox.Show("Updating is not possible while another HeuristicLab application is active." + Environment.NewLine +
152              "Please stop all active HeuristicLab applications and try again.", "Update plugins",
153              MessageBoxButtons.OK, MessageBoxIcon.Information);
154          } else {
155            try {
156              Cursor = Cursors.AppStarting;
157              using (PluginUpdaterForm form = new PluginUpdaterForm(pluginManager)) {
158                form.ShowDialog(this);
159              }
[6554]160              updatesAvailable = false;
161              CheckUpdatesAvailableAsync();
[6413]162              UpdateApplicationsList();
163            }
164            finally {
165              Cursor = Cursors.Arrow;
166            }
167          }
[2]168        } else {
[2481]169          ApplicationDescription app = (ApplicationDescription)applicationsListView.SelectedItems[0].Tag;
[2507]170          StartApplication(app);
[2]171        }
172      }
173    }
174
[3474]175    private void UpdateApplicationsList() {
[6413]176      if (InvokeRequired) Invoke((Action)UpdateApplicationsList);
177      else {
178        applicationsListView.Items.Clear();
179        AddPluginManagerItem();
180        AddUpdatePluginsItem();
[3474]181
[6413]182        foreach (ApplicationDescription info in pluginManager.Applications) {
183          ListViewItem item = new ListViewItem(info.Name, 0);
184          item.Tag = info;
185          item.Group = applicationsListView.Groups["Applications"];
186          item.SubItems.Add(new ListViewItem.ListViewSubItem(item, info.Version.ToString()));
187          item.SubItems.Add(new ListViewItem.ListViewSubItem(item, info.Description));
188          item.ToolTipText = info.Description;
189          applicationsListView.Items.Add(item);
190        }
191        foreach (ColumnHeader column in applicationsListView.Columns) {
192          if (applicationsListView.Items.Count > 0)
193            column.AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
194          else column.AutoResize(ColumnHeaderAutoResizeStyle.HeaderSize);
195        }
196      }
197    }
[3474]198
[6413]199    private void AddPluginManagerItem() {
[6518]200      FileVersionInfo pluginInfrastructureVersion = FileVersionInfo.GetVersionInfo(GetType().Assembly.Location);
201      pluginManagerListViewItem = new ListViewItem(pluginManagerItemName, 0);
202      pluginManagerListViewItem.Group = applicationsListView.Groups["Plugin Management"];
203      pluginManagerListViewItem.SubItems.Add(new ListViewItem.ListViewSubItem(pluginManagerListViewItem, pluginInfrastructureVersion.FileVersion));
204      pluginManagerListViewItem.SubItems.Add(new ListViewItem.ListViewSubItem(pluginManagerListViewItem, "Install, upgrade or delete plugins"));
205      pluginManagerListViewItem.ToolTipText = "Install, upgrade or delete plugins";
[6413]206
[6518]207      applicationsListView.Items.Add(pluginManagerListViewItem);
[6413]208    }
209
210    private void AddUpdatePluginsItem() {
211      if (updatesAvailable) {
212        var updateListViewItem = new ListViewItem(updatePluginsItemName, 1);
213        updateListViewItem.Group = applicationsListView.Groups["Plugin Management"];
214        updateListViewItem.SubItems.Add(new ListViewItem.ListViewSubItem(updateListViewItem, ""));
215        updateListViewItem.SubItems.Add(new ListViewItem.ListViewSubItem(updateListViewItem, "Download and install updates"));
216        updateListViewItem.ToolTipText = "Download and install updates";
217
218        applicationsListView.Items.Add(updateListViewItem);
[3600]219      }
[3474]220    }
221
[2507]222    private void StartApplication(ApplicationDescription app) {
[3113]223      splashScreen.Show("Loading " + app.Name);
[2507]224      Thread t = new Thread(delegate() {
225        bool stopped = false;
226        do {
227          try {
[2922]228            if (!abortRequested) {
[2507]229              pluginManager.Run(app);
[2922]230            }
[2507]231            stopped = true;
232          }
233          catch (Exception ex) {
234            stopped = false;
[3758]235            ThreadPool.QueueUserWorkItem(delegate(object exception) { ErrorHandling.ShowErrorDialog(this, (Exception)exception); }, ex);
[2507]236            Thread.Sleep(5000); // sleep 5 seconds before autorestart
237          }
238        } while (!abortRequested && !stopped && app.AutoRestart);
239      });
240      t.SetApartmentState(ApartmentState.STA); // needed for the AdvancedOptimizationFrontent
241      t.Start();
242    }
243
[3573]244    private void applicationsListView_SelectedIndexChanged(object sender, EventArgs e) {
245      startButton.Enabled = applicationsListView.SelectedItems.Count > 0;
[2922]246    }
247
[2]248    private void largeIconsButton_Click(object sender, EventArgs e) {
249      applicationsListView.View = View.LargeIcon;
250    }
251
252    private void detailsButton_Click(object sender, EventArgs e) {
253      applicationsListView.View = View.Details;
[3600]254      foreach (ColumnHeader column in applicationsListView.Columns) {
255        if (applicationsListView.Items.Count > 0)
256          column.AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
257        else column.AutoResize(ColumnHeaderAutoResizeStyle.HeaderSize);
258      }
[2]259    }
260
[1392]261    private void MainForm_FormClosing(object sender, FormClosingEventArgs e) {
[4515]262      splashScreen.Close();
[1392]263      abortRequested = true;
264    }
[3573]265
[3736]266    private void aboutButton_Click(object sender, EventArgs e) {
267      List<IPluginDescription> plugins = new List<IPluginDescription>(pluginManager.Plugins.OfType<IPluginDescription>());
[4482]268      using (var dialog = new AboutDialog(plugins)) {
269        dialog.ShowDialog();
270      }
[3736]271    }
[2]272  }
273}
Note: See TracBrowser for help on using the repository browser.