Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/03/10 18:08:26 (14 years ago)
Author:
gkronber
Message:

Worked on GUI for plugin management. #891 (Refactor GUI for plugin management)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.PluginInfrastructure/Advanced/InstallationManagerForm.cs

    r2753 r2922  
    1212namespace HeuristicLab.PluginInfrastructure.Advanced {
    1313  public partial class InstallationManagerForm : Form {
     14    private class UpdateOrInstallPluginsBackgroundWorkerArgument {
     15      public string ConnectionString { get; set; }
     16      public IEnumerable<IPluginDescription> PluginsToUpdate { get; set; }
     17      public IEnumerable<IPluginDescription> PluginsToInstall { get; set; }
     18    }
     19
     20    private class RemovePluginsBackgroundWorkerArgument {
     21      public IEnumerable<IPluginDescription> PluginsToRemove { get; set; }
     22    }
     23
     24    private class RefreshBackgroundWorkerResult {
     25      public IEnumerable<IPluginDescription> RemotePlugins { get; set; }
     26      public IEnumerable<DeploymentService.ProductDescription> RemoteProducts { get; set; }
     27    }
     28
    1429    private InstallationManager installationManager;
     30    private BackgroundWorker refreshServerPluginsBackgroundWorker;
     31    private BackgroundWorker updateOrInstallPluginsBackgroundWorker;
     32    private BackgroundWorker removePluginsBackgroundWorker;
     33    private BackgroundWorker refreshLocalPluginsBackgroundWorker;
     34    private string pluginDir;
    1535
    1636    public InstallationManagerForm() {
    1737      InitializeComponent();
    18       this.installationManager = new InstallationManager(Path.GetDirectoryName(Application.ExecutablePath));
    19 
    20       UpdatePluginsList();
    21     }
    22 
    23     private void UpdatePluginsList() {
    24       foreach (var plugin in installationManager.Plugins) {
    25         pluginsListView.Items.Add(CreatePluginItem(plugin));
    26       }
    27     }
    28 
    29     private static ListViewItem CreatePluginItem(IPluginDescription plugin) {
    30       ListViewItem item = new ListViewItem();
    31       item.Tag = plugin;
    32       item.Text = plugin.Name + "-" + plugin.Version.ToString();
    33       return item;
    34     }
    35 
    36     private void pluginsListView_SelectedIndexChanged(object sender, EventArgs e) {
    37       if (pluginsListView.SelectedItems.Count > 0) {
    38         ListViewItem selecteditem = pluginsListView.SelectedItems[0];
    39         IPluginDescription desc = (IPluginDescription)selecteditem.Tag;
    40         UpdateDetailsBox((PluginDescription)desc);
    41       }
    42     }
    43 
    44     private void UpdateDetailsBox(PluginDescription desc) {
    45       detailsTextBox.Text = installationManager.GetInformation(desc.Name);
     38
     39      pluginDir = Application.StartupPath;
     40
     41      #region initialize background workers
     42      refreshServerPluginsBackgroundWorker = new BackgroundWorker();
     43      refreshServerPluginsBackgroundWorker.DoWork += new DoWorkEventHandler(refreshServerPluginsBackgroundWorker_DoWork);
     44      refreshServerPluginsBackgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(refreshServerPluginsBackgroundWorker_RunWorkerCompleted);
     45
     46      updateOrInstallPluginsBackgroundWorker = new BackgroundWorker();
     47      updateOrInstallPluginsBackgroundWorker.DoWork += new DoWorkEventHandler(updateOrInstallPluginsBackgroundWorker_DoWork);
     48      updateOrInstallPluginsBackgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(updateOrInstallPluginsBackgroundWorker_RunWorkerCompleted);
     49
     50      removePluginsBackgroundWorker = new BackgroundWorker();
     51      removePluginsBackgroundWorker.DoWork += new DoWorkEventHandler(removePluginsBackgroundWorker_DoWork);
     52      removePluginsBackgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(removePluginsBackgroundWorker_RunWorkerCompleted);
     53
     54      refreshLocalPluginsBackgroundWorker = new BackgroundWorker();
     55      refreshLocalPluginsBackgroundWorker.DoWork += new DoWorkEventHandler(refreshLocalPluginsBackgroundWorker_DoWork);
     56      refreshLocalPluginsBackgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(refreshLocalPluginsBackgroundWorker_RunWorkerCompleted);
     57      #endregion
     58
     59      // get default connection string
     60      using (var client = new DeploymentService.UpdateClient()) {
     61        serverUrlTextBox.Text = client.Endpoint.Address.ToString();
     62      }
     63
     64      installationManager = new InstallationManager(pluginDir);
     65      installationManager.PluginInstalled += new EventHandler<PluginInfrastructureEventArgs>(installationManager_PluginInstalled);
     66      installationManager.PluginRemoved += new EventHandler<PluginInfrastructureEventArgs>(installationManager_PluginRemoved);
     67      installationManager.PluginUpdated += new EventHandler<PluginInfrastructureEventArgs>(installationManager_PluginUpdated);
     68      installationManager.PreInstallPlugin += new EventHandler<PluginInfrastructureCancelEventArgs>(installationManager_PreInstallPlugin);
     69      installationManager.PreRemovePlugin += new EventHandler<PluginInfrastructureCancelEventArgs>(installationManager_PreRemovePlugin);
     70      installationManager.PreUpdatePlugin += new EventHandler<PluginInfrastructureCancelEventArgs>(installationManager_PreUpdatePlugin);
     71
     72      RefreshLocalPluginListAsync();
     73    }
     74
     75    #region event handlers for refresh local plugin list backgroundworker
     76    private IEnumerable<PluginDescription> ReloadLocalPlugins() {
     77      PluginManager pluginManager = new PluginManager(Application.StartupPath);
     78      pluginManager.PluginLoaded += pluginManager_PluginLoaded;
     79      pluginManager.PluginUnloaded += pluginManager_PluginUnloaded;
     80      pluginManager.Initializing += pluginManager_Initializing;
     81      pluginManager.Initialized += pluginManager_Initialized;
     82
     83      pluginManager.DiscoverAndCheckPlugins();
     84
     85      pluginManager.PluginLoaded -= pluginManager_PluginLoaded;
     86      pluginManager.PluginUnloaded -= pluginManager_PluginUnloaded;
     87      pluginManager.Initializing -= pluginManager_Initializing;
     88      pluginManager.Initialized -= pluginManager_Initialized;
     89
     90      return pluginManager.Plugins;
     91    }
     92    void refreshLocalPluginsBackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
     93      if (!e.Cancelled && e.Error == null) {
     94        UpdateLocalPluginList((IEnumerable<PluginDescription>)e.Result);
     95        UpdateControlsConnected();
     96      }
     97    }
     98
     99    void refreshLocalPluginsBackgroundWorker_DoWork(object sender, DoWorkEventArgs e) {
     100      var plugins = ReloadLocalPlugins();
     101      e.Result = plugins;
     102    }
     103    #endregion
     104
     105    #region event handlers for plugin removal background worker
     106    void removePluginsBackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
     107      if (!e.Cancelled && e.Error == null) {
     108        RefreshLocalPluginListAsync();
     109        UpdateControlsConnected();
     110      }
     111    }
     112
     113    void removePluginsBackgroundWorker_DoWork(object sender, DoWorkEventArgs e) {
     114      IEnumerable<IPluginDescription> pluginsToRemove = (IEnumerable<IPluginDescription>)e.Argument;
     115      installationManager.Remove(pluginsToRemove);
     116    }
     117    #endregion
     118
     119    #region event handlers for plugin update background worker
     120    void updateOrInstallPluginsBackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
     121      if (!e.Cancelled && e.Error == null) {
     122        RefreshLocalPluginListAsync();
     123        RefreshRemotePluginListAsync();
     124        UpdateControlsConnected();
     125      } else {
     126        UpdateControlsDisconnected();
     127      }
     128    }
     129
     130    void updateOrInstallPluginsBackgroundWorker_DoWork(object sender, DoWorkEventArgs e) {
     131      UpdateOrInstallPluginsBackgroundWorkerArgument info = (UpdateOrInstallPluginsBackgroundWorkerArgument)e.Argument;
     132      installationManager.Install(info.ConnectionString, info.PluginsToInstall);
     133      installationManager.Update(info.ConnectionString, info.PluginsToUpdate);
     134    }
     135    #endregion
     136
     137    #region event handlers for refresh server plugins background worker
     138    void refreshServerPluginsBackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
     139      if (!e.Cancelled && e.Result != null) {
     140        RefreshBackgroundWorkerResult refreshResult = (RefreshBackgroundWorkerResult)e.Result;
     141        UpdateRemotePluginList(refreshResult.RemoteProducts, refreshResult.RemotePlugins);
     142        UpdateControlsConnected();
     143      } else {
     144        UpdateControlsDisconnected();
     145      }
     146    }
     147
     148    void refreshServerPluginsBackgroundWorker_DoWork(object sender, DoWorkEventArgs e) {
     149      string connectionString = (string)e.Argument;
     150
     151      RefreshBackgroundWorkerResult result = new RefreshBackgroundWorkerResult();
     152      result.RemotePlugins = installationManager.GetRemotePluginList(connectionString);
     153      result.RemoteProducts = installationManager.GetRemoteProductList(connectionString);
     154      e.Cancel = false;
     155      e.Result = result;
     156    }
     157
     158
     159
     160    #endregion
     161
     162    #region plugin manager event handlers
     163    void pluginManager_Initialized(object sender, PluginInfrastructureEventArgs e) {
     164      SetStatusStrip("Initialized PluginInfrastructure");
     165    }
     166
     167    void pluginManager_Initializing(object sender, PluginInfrastructureEventArgs e) {
     168      SetStatusStrip("Initializing PluginInfrastructure");
     169    }
     170
     171    void pluginManager_PluginUnloaded(object sender, PluginInfrastructureEventArgs e) {
     172      SetStatusStrip("Unloaded " + e.Entity);
     173    }
     174
     175    void pluginManager_PluginLoaded(object sender, PluginInfrastructureEventArgs e) {
     176      SetStatusStrip("Loaded " + e.Entity);
     177    }
     178    #endregion
     179
     180    #region installation manager event handlers
     181    void installationManager_PreUpdatePlugin(object sender, PluginInfrastructureCancelEventArgs e) {
     182      if (ConfirmUpdateAction(e.Entities) == true) e.Cancel = false;
     183      else e.Cancel = true;
     184    }
     185
     186    void installationManager_PreRemovePlugin(object sender, PluginInfrastructureCancelEventArgs e) {
     187      if (ConfirmRemoveAction(e.Entities) == true) e.Cancel = false;
     188      else e.Cancel = true;
     189    }
     190
     191    void installationManager_PreInstallPlugin(object sender, PluginInfrastructureCancelEventArgs e) {
     192      if (e.Entities.Count() > 0)
     193        SetStatusStrip("Installing " + e.Entities.Aggregate((a, b) => a + Environment.NewLine + b));
     194    }
     195
     196    void installationManager_PluginUpdated(object sender, PluginInfrastructureEventArgs e) {
     197      SetStatusStrip("Updated " + e.Entity);
     198    }
     199
     200    void installationManager_PluginRemoved(object sender, PluginInfrastructureEventArgs e) {
     201      SetStatusStrip("Removed " + e.Entity);
     202    }
     203
     204    void installationManager_PluginInstalled(object sender, PluginInfrastructureEventArgs e) {
     205      SetStatusStrip("Installed " + e.Entity);
     206    }
     207    #endregion
     208
     209    private void SetStatusStrip(string msg) {
     210      if (InvokeRequired) Invoke((Action<string>)SetStatusStrip, msg);
     211      else {
     212        toolStripStatusLabel.Text = msg;
     213        logTextBox.Text += DateTime.Now + ": " + msg + Environment.NewLine;
     214      }
     215    }
     216
     217    #region button events
     218
     219    private void refreshButton_Click(object sender, EventArgs e) {
     220      RefreshRemotePluginListAsync();
     221    }
     222
     223    private void updateButton_Click(object sender, EventArgs e) {
     224      Cursor = Cursors.AppStarting;
     225      toolStripProgressBar.Visible = true;
     226      DisableControls();
     227      var updateOrInstallInfo = new UpdateOrInstallPluginsBackgroundWorkerArgument();
     228      // if there is a local plugin with same name and same major and minor version then it's an update
     229      var pluginsToUpdate = from remotePlugin in remotePluginInstaller.CheckedPlugins
     230                            let matchingLocalPlugins = from localPlugin in localPluginManager.Plugins
     231                                                       where localPlugin.Name == remotePlugin.Name
     232                                                       where localPlugin.Version.Major == remotePlugin.Version.Major
     233                                                       where localPlugin.Version.Minor == remotePlugin.Version.Minor
     234                                                       select localPlugin
     235                            where matchingLocalPlugins.Count() > 0
     236                            select remotePlugin;
     237
     238      // otherwise install a new plugin
     239      var pluginsToInstall = remotePluginInstaller.CheckedPlugins.Except(pluginsToUpdate);
     240
     241      updateOrInstallInfo.ConnectionString = serverUrlTextBox.Text;
     242      updateOrInstallInfo.PluginsToInstall = pluginsToInstall;
     243      updateOrInstallInfo.PluginsToUpdate = pluginsToUpdate;
     244      updateOrInstallPluginsBackgroundWorker.RunWorkerAsync(updateOrInstallInfo);
     245    }
     246
     247    private void removeButton_Click(object sender, EventArgs e) {
     248      Cursor = Cursors.AppStarting;
     249      toolStripProgressBar.Visible = true;
     250      DisableControls();
     251      removePluginsBackgroundWorker.RunWorkerAsync(localPluginManager.CheckedPlugins);
     252    }
     253
     254    #endregion
     255
     256    #region confirmation dialogs
     257    private bool ConfirmRemoveAction(IEnumerable<string> fileNames) {
     258      StringBuilder strBuilder = new StringBuilder();
     259      strBuilder.AppendLine("Delete files:");
     260      foreach (var fileName in fileNames) {
     261        strBuilder.AppendLine(fileName);
     262      }
     263      return MessageBox.Show(strBuilder.ToString(), "Confirm Delete", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK;
     264    }
     265
     266    private bool ConfirmUpdateAction(IEnumerable<string> plugins) {
     267      StringBuilder strBuilder = new StringBuilder();
     268      strBuilder.AppendLine("Update plugins:");
     269      foreach (var plugin in plugins) {
     270        strBuilder.AppendLine(plugin);
     271      }
     272      return MessageBox.Show(strBuilder.ToString(), "Confirm Update", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK;
     273    }
     274
     275    #endregion
     276
     277    #region helper methods
     278
     279    private void UpdateLocalPluginList(IEnumerable<PluginDescription> plugins) {
     280      localPluginManager.Plugins = plugins;
     281    }
     282
     283    private void UpdateRemotePluginList(
     284      IEnumerable<DeploymentService.ProductDescription> remoteProducts,
     285      IEnumerable<IPluginDescription> remotePlugins) {
     286
     287      var mostRecentRemotePlugins = from remote in remotePlugins
     288                                    where !remotePlugins.Any(x => x.Name == remote.Name && x.Version > remote.Version) // same name and higher version
     289                                    select remote;
     290
     291      var newPlugins = from remote in mostRecentRemotePlugins
     292                       let matchingLocal = (from local in localPluginManager.Plugins
     293                                            where local.Name == remote.Name
     294                                            where local.Version < remote.Version
     295                                            select local).FirstOrDefault()
     296                       where matchingLocal != null
     297                       select remote;
     298
     299      remotePluginInstaller.NewPlugins = newPlugins;
     300      remotePluginInstaller.Products = remoteProducts;
     301      remotePluginInstaller.AllPlugins = remotePlugins;
     302    }
     303
     304    private void RefreshRemotePluginListAsync() {
     305      Cursor = Cursors.AppStarting;
     306      toolStripProgressBar.Visible = true;
     307      DisableControls();
     308      refreshServerPluginsBackgroundWorker.RunWorkerAsync(serverUrlTextBox.Text);
     309    }
     310
     311    private void RefreshLocalPluginListAsync() {
     312      Cursor = Cursors.AppStarting;
     313      toolStripProgressBar.Visible = true;
     314      DisableControls();
     315      refreshLocalPluginsBackgroundWorker.RunWorkerAsync();
     316    }
     317
     318    private void UpdateControlsDisconnected() {
     319      //localPluginsListView.Enabled = false;
     320      //ClearPluginsList(remotePluginsListView);
     321      refreshButton.Enabled = true;
     322      serverUrlTextBox.Enabled = true;
     323      toolStripProgressBar.Visible = false;
     324      Cursor = Cursors.Default;
     325    }
     326
     327    private void UpdateControlsConnected() {
     328      refreshButton.Enabled = true;
     329      serverUrlTextBox.Enabled = true;
     330      toolStripProgressBar.Visible = false;
     331      Cursor = Cursors.Default;
     332    }
     333
     334    private void DisableControls() {
     335      refreshButton.Enabled = false;
     336      Cursor = Cursors.Default;
     337    }
     338    #endregion
     339
     340    private void localPluginManager_ItemChecked(object sender, ItemCheckedEventArgs e) {
     341      removeButton.Enabled = localPluginManager.CheckedPlugins.Count() > 0;
     342    }
     343
     344    private void remotePluginInstaller_ItemChecked(object sender, ItemCheckedEventArgs e) {
     345      installButton.Enabled = remotePluginInstaller.CheckedPlugins.Count() > 0;
    46346    }
    47347  }
Note: See TracChangeset for help on using the changeset viewer.