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;
|
---|
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;
|
---|
16 |
|
---|
17 | namespace HeuristicLab.DeploymentService.AdminClient {
|
---|
18 | public partial class PluginListView : HeuristicLab.MainForm.WindowsForms.View {
|
---|
19 | private Dictionary<IPluginDescription, PluginDeploymentService.PluginDescription> localAndServerPlugins;
|
---|
20 | private BackgroundWorker pluginUploadWorker;
|
---|
21 | private BackgroundWorker updateServerPluginsWorker;
|
---|
22 |
|
---|
23 | public PluginListView() {
|
---|
24 | InitializeComponent();
|
---|
25 | imageList.Images.Add(HeuristicLab.Common.Resources.VS2008ImageLibrary.Assembly);
|
---|
26 | imageList.Images.Add(HeuristicLab.Common.Resources.VS2008ImageLibrary.ArrowUp);
|
---|
27 | Caption = "Plugins";
|
---|
28 |
|
---|
29 | localAndServerPlugins = new Dictionary<IPluginDescription, PluginDeploymentService.PluginDescription>();
|
---|
30 |
|
---|
31 | #region initialize backgroundworkers
|
---|
32 | pluginUploadWorker = new BackgroundWorker();
|
---|
33 | pluginUploadWorker.DoWork += new DoWorkEventHandler(pluginUploadWorker_DoWork);
|
---|
34 | pluginUploadWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(pluginUploadWorker_RunWorkerCompleted);
|
---|
35 |
|
---|
36 | updateServerPluginsWorker = new BackgroundWorker();
|
---|
37 | updateServerPluginsWorker.DoWork += new DoWorkEventHandler(updateServerPluginsWorker_DoWork);
|
---|
38 | updateServerPluginsWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(updateServerPluginsWorker_RunWorkerCompleted);
|
---|
39 | #endregion
|
---|
40 | }
|
---|
41 |
|
---|
42 | #region refresh plugins from server backgroundworker
|
---|
43 | void updateServerPluginsWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
|
---|
44 | if (!e.Cancelled && e.Result != null) {
|
---|
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)
|
---|
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 | }
|
---|
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();
|
---|
68 | } else {
|
---|
69 | UpdateControlsDisconnectedState();
|
---|
70 | }
|
---|
71 | // make sure cursor is set correctly
|
---|
72 | Cursor = Cursors.Default;
|
---|
73 | }
|
---|
74 |
|
---|
75 | void updateServerPluginsWorker_DoWork(object sender, DoWorkEventArgs e) {
|
---|
76 | try {
|
---|
77 | var client = PluginDeploymentService.UpdateClientFactory.CreateClient();
|
---|
78 | e.Result = client.GetPlugins();
|
---|
79 | e.Cancel = false;
|
---|
80 | }
|
---|
81 | catch (EndpointNotFoundException) {
|
---|
82 | e.Result = null;
|
---|
83 | e.Cancel = true;
|
---|
84 | }
|
---|
85 | catch (FaultException) {
|
---|
86 | e.Result = null;
|
---|
87 | e.Cancel = true;
|
---|
88 | }
|
---|
89 | }
|
---|
90 | #endregion
|
---|
91 |
|
---|
92 | #region upload plugins to server backgroundworker
|
---|
93 | void pluginUploadWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
|
---|
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 | }
|
---|
102 | }
|
---|
103 |
|
---|
104 | void pluginUploadWorker_DoWork(object sender, DoWorkEventArgs e) {
|
---|
105 | try {
|
---|
106 | var selectedPlugins = (IEnumerable<IPluginDescription>)e.Argument;
|
---|
107 | PluginDeploymentService.AdminClient adminClient = PluginDeploymentService.AdminClientFactory.CreateClient();
|
---|
108 |
|
---|
109 | foreach (var plugin in IteratePlugins(selectedPlugins)) {
|
---|
110 | adminClient.DeployPlugin(MakePluginDescription(plugin), CreateZipPackage(plugin));
|
---|
111 | }
|
---|
112 | e.Cancel = false;
|
---|
113 | }
|
---|
114 | catch (EndpointNotFoundException) {
|
---|
115 | e.Cancel = true;
|
---|
116 | }
|
---|
117 | catch (FaultException) {
|
---|
118 | e.Cancel = true;
|
---|
119 | }
|
---|
120 | }
|
---|
121 | #endregion
|
---|
122 |
|
---|
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) {
|
---|
138 | var connectionSetupView = new ConnectionSetupView();
|
---|
139 | connectionSetupView.Show();
|
---|
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
|
---|
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 });
|
---|
203 | }
|
---|
204 | item.Tag = plugin;
|
---|
205 | item.Checked = false;
|
---|
206 | return item;
|
---|
207 | }
|
---|
208 |
|
---|
209 | private ListViewItem FindItemForPlugin(IPluginDescription dep) {
|
---|
210 | return (from i in listView.Items.Cast<ListViewItem>()
|
---|
211 | where i.Tag == dep
|
---|
212 | select i).Single();
|
---|
213 | }
|
---|
214 |
|
---|
215 | private PluginDeploymentService.PluginDescription MakePluginDescription(IPluginDescription plugin) {
|
---|
216 | var dependencies = from dep in plugin.Dependencies
|
---|
217 | select MakePluginDescription(dep);
|
---|
218 | return new PluginDeploymentService.PluginDescription(plugin.Name, plugin.Version, dependencies, plugin.ContactName, plugin.ContactEmail, plugin.LicenseText);
|
---|
219 | }
|
---|
220 |
|
---|
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;
|
---|
227 | }
|
---|
228 | }
|
---|
229 |
|
---|
230 | // start background process to refresh the plugin list (local and server)
|
---|
231 | private void RefreshPluginsAsync() {
|
---|
232 | Cursor = Cursors.AppStarting;
|
---|
233 | DisableControl();
|
---|
234 | updateServerPluginsWorker.RunWorkerAsync();
|
---|
235 | }
|
---|
236 |
|
---|
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;
|
---|
243 | }
|
---|
244 |
|
---|
245 | private void UpdateControlsDisconnectedState() {
|
---|
246 | connectionSetupButton.Text = "Connect";
|
---|
247 | refreshButton.Enabled = false;
|
---|
248 |
|
---|
249 | localAndServerPlugins.Clear();
|
---|
250 | listView.Items.Clear();
|
---|
251 | listView.Enabled = false;
|
---|
252 | uploadButton.Enabled = false;
|
---|
253 | MainFormManager.GetMainForm<MainForm>().HideProgressBar();
|
---|
254 | }
|
---|
255 |
|
---|
256 | private void UpdateControlsConnectedState() {
|
---|
257 | connectionSetupButton.Text = "Disconnect";
|
---|
258 | connectionSetupButton.Enabled = true;
|
---|
259 | refreshButton.Enabled = true;
|
---|
260 | listView.Enabled = true;
|
---|
261 | uploadButton.Enabled = false;
|
---|
262 | MainFormManager.GetMainForm<MainForm>().HideProgressBar();
|
---|
263 | }
|
---|
264 |
|
---|
265 |
|
---|
266 | #endregion
|
---|
267 | }
|
---|
268 | }
|
---|