[2802] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.ComponentModel;
|
---|
| 4 | using System.Drawing;
|
---|
| 5 | using System.Data;
|
---|
| 6 | using System.Linq;
|
---|
| 7 | using System.Text;
|
---|
| 8 | using System.Windows.Forms;
|
---|
| 9 | using HeuristicLab.MainForm;
|
---|
| 10 | using HeuristicLab.PluginInfrastructure;
|
---|
[2816] | 11 | using PluginDeploymentService = HeuristicLab.PluginInfrastructure.Advanced.DeploymentService;
|
---|
| 12 | using HeuristicLab.PluginInfrastructure.Manager;
|
---|
| 13 | using System.ServiceModel;
|
---|
| 14 | using ICSharpCode.SharpZipLib.Zip;
|
---|
| 15 | using System.IO;
|
---|
[2802] | 16 |
|
---|
| 17 | namespace HeuristicLab.DeploymentService.AdminClient {
|
---|
| 18 | public partial class PluginListView : HeuristicLab.MainForm.WindowsForms.View {
|
---|
[2816] | 19 | private Dictionary<IPluginDescription, PluginDeploymentService.PluginDescription> localAndServerPlugins;
|
---|
[2802] | 20 | private BackgroundWorker pluginUploadWorker;
|
---|
[2816] | 21 | private BackgroundWorker updateServerPluginsWorker;
|
---|
[2802] | 22 |
|
---|
| 23 | public PluginListView() {
|
---|
| 24 | InitializeComponent();
|
---|
[2860] | 25 | imageList.Images.Add(HeuristicLab.Common.Resources.VS2008ImageLibrary.Assembly);
|
---|
| 26 | imageList.Images.Add(HeuristicLab.Common.Resources.VS2008ImageLibrary.ArrowUp);
|
---|
[2802] | 27 | Caption = "Plugins";
|
---|
| 28 |
|
---|
[2816] | 29 | localAndServerPlugins = new Dictionary<IPluginDescription, PluginDeploymentService.PluginDescription>();
|
---|
| 30 |
|
---|
[2860] | 31 | #region initialize backgroundworkers
|
---|
[2802] | 32 | pluginUploadWorker = new BackgroundWorker();
|
---|
| 33 | pluginUploadWorker.DoWork += new DoWorkEventHandler(pluginUploadWorker_DoWork);
|
---|
| 34 | pluginUploadWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(pluginUploadWorker_RunWorkerCompleted);
|
---|
| 35 |
|
---|
[2816] | 36 | updateServerPluginsWorker = new BackgroundWorker();
|
---|
| 37 | updateServerPluginsWorker.DoWork += new DoWorkEventHandler(updateServerPluginsWorker_DoWork);
|
---|
| 38 | updateServerPluginsWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(updateServerPluginsWorker_RunWorkerCompleted);
|
---|
[2860] | 39 | #endregion
|
---|
[2802] | 40 | }
|
---|
| 41 |
|
---|
[2860] | 42 | #region refresh plugins from server backgroundworker
|
---|
[2816] | 43 | void updateServerPluginsWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
|
---|
| 44 | if (!e.Cancelled && e.Result != null) {
|
---|
[2860] | 45 | // refresh local plugins
|
---|
| 46 | localAndServerPlugins.Clear();
|
---|
| 47 | foreach (var plugin in ApplicationManager.Manager.Plugins) {
|
---|
| 48 | localAndServerPlugins.Add(plugin, null);
|
---|
| 49 | }
|
---|
| 50 | // refresh server plugins (find matching local plugins)
|
---|
[2816] | 51 | var plugins = (PluginDeploymentService.PluginDescription[])e.Result;
|
---|
| 52 | foreach (var plugin in plugins) {
|
---|
| 53 | var matchingLocalPlugin = (from localPlugin in localAndServerPlugins.Keys
|
---|
| 54 | where localPlugin.Name == plugin.Name
|
---|
| 55 | where localPlugin.Version == localPlugin.Version
|
---|
| 56 | select localPlugin).SingleOrDefault();
|
---|
| 57 | if (matchingLocalPlugin != null) {
|
---|
| 58 | localAndServerPlugins[matchingLocalPlugin] = plugin;
|
---|
| 59 | }
|
---|
| 60 | }
|
---|
[2860] | 61 | // refresh the list view with plugins
|
---|
| 62 | listView.Items.Clear();
|
---|
| 63 | foreach (var pair in localAndServerPlugins) {
|
---|
| 64 | var item = MakeListViewItem(pair.Key);
|
---|
| 65 | listView.Items.Add(item);
|
---|
| 66 | }
|
---|
| 67 | UpdateControlsConnectedState();
|
---|
[2816] | 68 | } else {
|
---|
[2860] | 69 | UpdateControlsDisconnectedState();
|
---|
[2816] | 70 | }
|
---|
[2860] | 71 | // make sure cursor is set correctly
|
---|
[2816] | 72 | Cursor = Cursors.Default;
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | void updateServerPluginsWorker_DoWork(object sender, DoWorkEventArgs e) {
|
---|
| 76 | try {
|
---|
[3006] | 77 | var client = PluginDeploymentService.UpdateClientFactory.CreateClient();
|
---|
| 78 | e.Result = client.GetPlugins();
|
---|
| 79 | e.Cancel = false;
|
---|
[2816] | 80 | }
|
---|
[2860] | 81 | catch (EndpointNotFoundException) {
|
---|
[2816] | 82 | e.Result = null;
|
---|
| 83 | e.Cancel = true;
|
---|
| 84 | }
|
---|
[2860] | 85 | catch (FaultException) {
|
---|
[2816] | 86 | e.Result = null;
|
---|
| 87 | e.Cancel = true;
|
---|
| 88 | }
|
---|
| 89 | }
|
---|
[2860] | 90 | #endregion
|
---|
[2816] | 91 |
|
---|
[2860] | 92 | #region upload plugins to server backgroundworker
|
---|
[2802] | 93 | void pluginUploadWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
|
---|
[2860] | 94 | Cursor = Cursors.Default;
|
---|
| 95 | if (e.Cancelled) {
|
---|
| 96 | UpdateControlsDisconnectedState();
|
---|
| 97 | } else {
|
---|
| 98 | UpdateControlsConnectedState();
|
---|
| 99 | // start another async call to refresh plugin information from server
|
---|
| 100 | RefreshPluginsAsync();
|
---|
| 101 | }
|
---|
[2802] | 102 | }
|
---|
| 103 |
|
---|
| 104 | void pluginUploadWorker_DoWork(object sender, DoWorkEventArgs e) {
|
---|
[2860] | 105 | try {
|
---|
| 106 | var selectedPlugins = (IEnumerable<IPluginDescription>)e.Argument;
|
---|
[3006] | 107 | PluginDeploymentService.AdminClient adminClient = PluginDeploymentService.AdminClientFactory.CreateClient();
|
---|
[2802] | 108 |
|
---|
[3006] | 109 | foreach (var plugin in IteratePlugins(selectedPlugins)) {
|
---|
| 110 | adminClient.DeployPlugin(MakePluginDescription(plugin), CreateZipPackage(plugin));
|
---|
[2816] | 111 | }
|
---|
[2860] | 112 | e.Cancel = false;
|
---|
[2802] | 113 | }
|
---|
[2860] | 114 | catch (EndpointNotFoundException) {
|
---|
| 115 | e.Cancel = true;
|
---|
| 116 | }
|
---|
| 117 | catch (FaultException) {
|
---|
| 118 | e.Cancel = true;
|
---|
| 119 | }
|
---|
[2802] | 120 | }
|
---|
[2860] | 121 | #endregion
|
---|
[2802] | 122 |
|
---|
[2860] | 123 |
|
---|
| 124 | #region button events
|
---|
| 125 | private void uploadButton_Click(object sender, EventArgs e) {
|
---|
| 126 | var selectedPlugins = from item in listView.Items.Cast<ListViewItem>()
|
---|
| 127 | where item.Checked
|
---|
| 128 | where item.Tag is IPluginDescription
|
---|
| 129 | select item.Tag as IPluginDescription;
|
---|
| 130 | if (selectedPlugins.Count() > 0) {
|
---|
| 131 | Cursor = Cursors.AppStarting;
|
---|
| 132 | DisableControl();
|
---|
| 133 | pluginUploadWorker.RunWorkerAsync(selectedPlugins.ToList());
|
---|
| 134 | }
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | private void connectButton_Click(object sender, EventArgs e) {
|
---|
[3006] | 138 | var connectionSetupView = new ConnectionSetupView();
|
---|
| 139 | connectionSetupView.Show();
|
---|
[2860] | 140 | }
|
---|
| 141 |
|
---|
| 142 | private void refreshButton_Click(object sender, EventArgs e) {
|
---|
| 143 | DisableControl();
|
---|
| 144 | RefreshPluginsAsync();
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | #endregion
|
---|
| 148 |
|
---|
| 149 | #region item list events
|
---|
| 150 | private void listView_ItemActivate(object sender, EventArgs e) {
|
---|
| 151 | foreach (var item in listView.SelectedItems) {
|
---|
| 152 | var plugin = (IPluginDescription)((ListViewItem)item).Tag;
|
---|
| 153 | var compView = new PluginComparisonView(plugin, localAndServerPlugins[plugin]);
|
---|
| 154 | compView.Show();
|
---|
| 155 | }
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | private void listView_ItemChecked(object sender, ItemCheckedEventArgs e) {
|
---|
| 159 | // also check all dependencies
|
---|
| 160 | if (e.Item.Checked) {
|
---|
| 161 | uploadButton.Enabled = true;
|
---|
| 162 | var plugin = (IPluginDescription)e.Item.Tag;
|
---|
| 163 | foreach (var dep in plugin.Dependencies) {
|
---|
| 164 | var depItem = FindItemForPlugin(dep);
|
---|
| 165 | if (!depItem.Checked) depItem.Checked = true;
|
---|
| 166 | }
|
---|
| 167 | } else {
|
---|
| 168 | uploadButton.Enabled = (from i in listView.Items.Cast<ListViewItem>()
|
---|
| 169 | where i.Checked
|
---|
| 170 | select i).Any();
|
---|
| 171 | // also uncheck all dependent plugins
|
---|
| 172 | var plugin = (IPluginDescription)e.Item.Tag;
|
---|
| 173 | foreach (ListViewItem item in listView.Items) {
|
---|
| 174 | var dep = (IPluginDescription)item.Tag;
|
---|
| 175 | if (dep.Dependencies.Contains(plugin) && item.Checked) {
|
---|
| 176 | item.Checked = false;
|
---|
| 177 | }
|
---|
| 178 | }
|
---|
| 179 | }
|
---|
| 180 | }
|
---|
| 181 | #endregion
|
---|
| 182 |
|
---|
| 183 | #region helper methods
|
---|
[2816] | 184 | private byte[] CreateZipPackage(IPluginDescription plugin) {
|
---|
| 185 | using (MemoryStream stream = new MemoryStream()) {
|
---|
| 186 | ZipFile zipFile = new ZipFile(stream);
|
---|
| 187 | zipFile.BeginUpdate();
|
---|
| 188 | foreach (var file in plugin.Files) {
|
---|
| 189 | zipFile.Add(file.Name);
|
---|
| 190 | }
|
---|
| 191 | zipFile.CommitUpdate();
|
---|
| 192 | stream.Seek(0, SeekOrigin.Begin);
|
---|
| 193 | return stream.GetBuffer();
|
---|
| 194 | }
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | private ListViewItem MakeListViewItem(IPluginDescription plugin) {
|
---|
| 198 | ListViewItem item;
|
---|
| 199 | if (localAndServerPlugins[plugin] != null) {
|
---|
| 200 | item = new ListViewItem(new string[] { plugin.Name, plugin.Version.ToString(), localAndServerPlugins[plugin].Version.ToString() });
|
---|
| 201 | } else {
|
---|
| 202 | item = new ListViewItem(new string[] { plugin.Name, plugin.Version.ToString(), string.Empty });
|
---|
[2802] | 203 | }
|
---|
| 204 | item.Tag = plugin;
|
---|
[2860] | 205 | item.Checked = false;
|
---|
[2802] | 206 | return item;
|
---|
| 207 | }
|
---|
| 208 |
|
---|
[2860] | 209 | private ListViewItem FindItemForPlugin(IPluginDescription dep) {
|
---|
| 210 | return (from i in listView.Items.Cast<ListViewItem>()
|
---|
| 211 | where i.Tag == dep
|
---|
| 212 | select i).Single();
|
---|
[2802] | 213 | }
|
---|
| 214 |
|
---|
[2860] | 215 | private PluginDeploymentService.PluginDescription MakePluginDescription(IPluginDescription plugin) {
|
---|
| 216 | var dependencies = from dep in plugin.Dependencies
|
---|
| 217 | select MakePluginDescription(dep);
|
---|
[3006] | 218 | return new PluginDeploymentService.PluginDescription(plugin.Name, plugin.Version, dependencies, plugin.ContactName, plugin.ContactEmail, plugin.LicenseText);
|
---|
[2816] | 219 | }
|
---|
| 220 |
|
---|
[2860] | 221 | private IEnumerable<IPluginDescription> IteratePlugins(IEnumerable<IPluginDescription> plugins) {
|
---|
| 222 | foreach (var plugin in plugins) {
|
---|
| 223 | foreach (var dependency in IteratePlugins(plugin.Dependencies)) {
|
---|
| 224 | yield return dependency;
|
---|
| 225 | }
|
---|
| 226 | yield return plugin;
|
---|
[2802] | 227 | }
|
---|
| 228 | }
|
---|
[2816] | 229 |
|
---|
[2860] | 230 | // start background process to refresh the plugin list (local and server)
|
---|
| 231 | private void RefreshPluginsAsync() {
|
---|
[2816] | 232 | Cursor = Cursors.AppStarting;
|
---|
[2860] | 233 | DisableControl();
|
---|
[2816] | 234 | updateServerPluginsWorker.RunWorkerAsync();
|
---|
| 235 | }
|
---|
| 236 |
|
---|
[2860] | 237 | // is called by all methods that start a background process
|
---|
| 238 | // controls must be enabled manuall again when the backgroundworker finishes
|
---|
| 239 | private void DisableControl() {
|
---|
| 240 | MainFormManager.GetMainForm<MainForm>().ShowProgressBar();
|
---|
| 241 | foreach (Control ctrl in Controls)
|
---|
| 242 | ctrl.Enabled = false;
|
---|
[2816] | 243 | }
|
---|
| 244 |
|
---|
[2860] | 245 | private void UpdateControlsDisconnectedState() {
|
---|
[3006] | 246 | connectionSetupButton.Text = "Connect";
|
---|
[2860] | 247 | refreshButton.Enabled = false;
|
---|
[2816] | 248 |
|
---|
| 249 | localAndServerPlugins.Clear();
|
---|
| 250 | listView.Items.Clear();
|
---|
| 251 | listView.Enabled = false;
|
---|
| 252 | uploadButton.Enabled = false;
|
---|
[2860] | 253 | MainFormManager.GetMainForm<MainForm>().HideProgressBar();
|
---|
[2816] | 254 | }
|
---|
[2860] | 255 |
|
---|
| 256 | private void UpdateControlsConnectedState() {
|
---|
[3006] | 257 | connectionSetupButton.Text = "Disconnect";
|
---|
| 258 | connectionSetupButton.Enabled = true;
|
---|
[2860] | 259 | refreshButton.Enabled = true;
|
---|
| 260 | listView.Enabled = true;
|
---|
| 261 | uploadButton.Enabled = false;
|
---|
| 262 | MainFormManager.GetMainForm<MainForm>().HideProgressBar();
|
---|
| 263 | }
|
---|
| 264 |
|
---|
| 265 |
|
---|
| 266 | #endregion
|
---|
[2802] | 267 | }
|
---|
| 268 | }
|
---|