00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 using System;
00013 using System.Collections.Generic;
00014 using System.Reflection;
00015 using System.Runtime.InteropServices;
00016 using System.Windows.Forms;
00017 using Mcs.Epm.MicrosoftProject.mpFx.Client.Shared.Properties;
00018
00019 namespace Mcs.Epm.MicrosoftProject.mpFx.Client.Shared.Forms
00020 {
00021
00022
00023
00024 public partial class PluginGalleryBrowserForm : Form
00025 {
00026 #region Win32 Imports
00027
00028 [DllImport("user32.dll")]
00029 private static extern int SendMessage(IntPtr hWnd,
00030 int Msg, int wParam, int lParam);
00031
00032 [DllImport("user32.dll")]
00033 private static extern bool ReleaseCapture();
00034
00035 #endregion
00036
00037 #region Private Constants
00038
00039 public const int HT_CAPTION = 0x2;
00040 private const int WM_NCLBUTTONDOWN = 0xA1;
00041
00042 #endregion
00043
00044 #region Instance Data
00045
00046 private readonly PluginHost _PluginHost;
00047 private object _PluginPublisher;
00048
00049 #endregion
00050
00051 #region Constructors
00052
00053 public PluginGalleryBrowserForm(PluginHost pluginHost)
00054 {
00055 InitializeComponent();
00056
00057 _PluginHost = pluginHost;
00058 pluginsListView.View = Settings.Default.LastGalleryView;
00059 }
00060
00061 #endregion
00062
00063 #region Private Methods
00064
00065 private void DoDrag(object sender,MouseEventArgs e)
00066 {
00067 if (e.Button == MouseButtons.Left)
00068 {
00069 ReleaseCapture();
00070 SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
00071 }
00072 }
00073
00074 private void LoginAsGalleryManager()
00075 {
00076 bool isLoggedIn = false;
00077 SetStatusText("Logging in...");
00078 try
00079 {
00080 object plugin = _PluginHost.Plugins[new Guid("{2EB58C93-D11E-47c1-BB16-4C865D527A10}")];
00081 Type pluginPublisherType = plugin.GetType().Assembly.GetType("Mcs.Epm.MicrosoftProject.mpFx.Client.Plugins.PluginPlublisher.PluginPublisher");
00082 if (pluginPublisherType == null)
00083 {
00084 MessageBox.Show(this, "Plugin not found", Resources.AppTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
00085 return;
00086 }
00087 _PluginPublisher = Activator.CreateInstance(pluginPublisherType);
00088
00089 object returnValue = _PluginPublisher.GetType().InvokeMember("Login",
00090 BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly,
00091 null,
00092 _PluginPublisher,
00093 new[] {this});
00094
00095 isLoggedIn = Convert.ToBoolean(returnValue);
00096
00097 if (isLoggedIn)
00098 {
00099 returnValue = _PluginPublisher.GetType().InvokeMember("Location",
00100 BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly,
00101 null,
00102 _PluginPublisher,
00103 null);
00104
00105 _PluginHost.Gallery.Location = returnValue.ToString();
00106
00107 returnValue = _PluginPublisher.GetType().InvokeMember("UserName",
00108 BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly,
00109 null,
00110 _PluginPublisher,
00111 null);
00112
00113 _PluginHost.Gallery.UserName = returnValue.ToString();
00114
00115 returnValue = _PluginPublisher.GetType().InvokeMember("Password",
00116 BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly,
00117 null,
00118 _PluginPublisher,
00119 null);
00120
00121 _PluginHost.Gallery.Password = returnValue.ToString();
00122 }
00123 }
00124 finally
00125 {
00126 publisherToolStripSeparator.Visible = isLoggedIn;
00127 galleryManagerToolStripLabel.Visible = isLoggedIn;
00128 addToGalleryToolStripButton.Visible = isLoggedIn;
00129 removeFromGalleryToolStripButton.Visible = isLoggedIn;
00130 loginToolStripSeparator.Visible = isLoggedIn;
00131
00132 SetStatusText(string.Empty);
00133 }
00134 }
00135
00136 private void SetStatusText(string text)
00137 {
00138 if (text == string.Empty)
00139 {
00140 statusToolStripLabel.Visible = false;
00141 }
00142 else
00143 {
00144 statusToolStripLabel.Visible = true;
00145 }
00146 statusToolStripLabel.Text = text;
00147 Application.DoEvents();
00148 }
00149
00150 private void LoadGallery()
00151 {
00152 View currentView = pluginsListView.View;
00153 SetStatusText("Loading Gallery...");
00154
00155 pluginsListView.Items.Clear();
00156
00157 _PluginHost.Gallery.Load();
00158
00159 foreach (KeyValuePair<Guid, PluginDescriptor> pluginDescriptor in _PluginHost.Gallery.Plugins)
00160 {
00161 ListViewGroup listViewGroup = pluginsListView.Groups[pluginDescriptor.Value.Tag];
00162
00163 if (listViewGroup == null)
00164 {
00165 listViewGroup = new ListViewGroup();
00166 listViewGroup.Name = pluginDescriptor.Value.Tag;
00167 listViewGroup.Header = pluginDescriptor.Value.Tag;
00168
00169 pluginsListView.Groups.Add(listViewGroup);
00170 }
00171
00172 ListViewItem listViewItem = new ListViewItem(pluginDescriptor.Value.Guid.ToString());
00173
00174 listViewItem.Group = listViewGroup;
00175 listViewItem.Text = pluginDescriptor.Value.Name;
00176 listViewItem.SubItems.Add(pluginDescriptor.Value.Description);
00177 listViewItem.SubItems.Add(pluginDescriptor.Value.Version.ToString());
00178 listViewItem.SubItems.Add(pluginDescriptor.Value.Author);
00179
00180 if (_PluginHost.Plugins.ContainsKey(pluginDescriptor.Value.Guid))
00181 {
00182 if (_PluginHost.Plugins[pluginDescriptor.Value.Guid].Version >= pluginDescriptor.Value.Version)
00183 {
00184 listViewItem.ImageKey = "lightning.png";
00185 }
00186 else if (_PluginHost.Plugins[pluginDescriptor.Value.Guid].Version < pluginDescriptor.Value.Version)
00187 {
00188 listViewItem.ImageKey = "asterisk_yellow.png";
00189 }
00190 }
00191 else
00192 {
00193 listViewItem.ImageKey = "add.png";
00194 }
00195 listViewItem.Tag = pluginDescriptor;
00196 pluginsListView.Items.Add(listViewItem);
00197
00198 }
00199
00200 pluginsListView.ShowGroups = true;
00201 pluginsListView.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
00202
00203 if (pluginsListView.Items.Count > 0)
00204 {
00205 pluginsListView.Items[0].Selected = true;
00206 LoadPluginInformation();
00207 }
00208 SetStatusText(string.Empty);
00209 statusToolStripLabel.Visible = false;
00210
00211 pluginsListView.View = currentView;
00212 }
00213
00214 private void SetListView()
00215 {
00216 if (detailsToolStripMenuItem.Checked)
00217 {
00218 detailsToolStripMenuItem.Checked = false;
00219 iconsToolStripMenuItem.Checked = true;
00220 pluginsListView.View = View.SmallIcon;
00221 }
00222 else
00223 {
00224 detailsToolStripMenuItem.Checked = true;
00225 iconsToolStripMenuItem.Checked = false;
00226 pluginsListView.View = View.Details;
00227 }
00228
00229 pluginsListView.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
00230 }
00231
00232 private void LoadPluginInformation()
00233 {
00234 installToolStripButton.Enabled = false;
00235 updateToolStripButton.Enabled = false;
00236
00237 if (pluginsListView.SelectedIndices.Count == 0)
00238 {
00239 return;
00240 }
00241
00242 KeyValuePair<Guid, PluginDescriptor> keyValue = (KeyValuePair<Guid, PluginDescriptor>)pluginsListView.SelectedItems[0].Tag;
00243
00244 if (!_PluginHost.Plugins.ContainsKey(keyValue.Value.Guid))
00245 {
00246 installToolStripButton.Enabled = true;
00247 }
00248 else
00249 {
00250 if (_PluginHost.Plugins[keyValue.Value.Guid].Version < keyValue.Value.Version)
00251 {
00252 updateToolStripButton.Enabled = true;
00253 }
00254 }
00255 SetStatusText("Loading preview...");
00256 webBrowser.Navigate(keyValue.Value.Preview);
00257 }
00258
00259 private void InstallPlugin()
00260 {
00261 try
00262 {
00263 KeyValuePair<Guid, PluginDescriptor> keyValue = (KeyValuePair<Guid, PluginDescriptor>)pluginsListView.SelectedItems[0].Tag;
00264
00265 if (keyValue.Value != null)
00266 {
00267 _PluginHost.Gallery.InstallLocal(keyValue.Value.Guid);
00268 }
00269
00270 LoadGallery();
00271 }
00272 catch (Exception exception)
00273 {
00274 MessageBox.Show(this, exception.Message, Resources.AppTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
00275 }
00276 }
00277
00278 #endregion
00279
00280 #region Event Handlers
00281
00282 private void PluginGalleryBrowserForm_Shown(object sender, EventArgs e)
00283 {
00284 Cursor = Cursors.WaitCursor;
00285
00286 webBrowser.DocumentCompleted += webBrowser_DocumentCompleted;
00287
00288 try
00289 {
00290
00291
00292 loginGalleryToolStripButton.Visible = _PluginHost.Plugins.ContainsKey(new Guid("{2EB58C93-D11E-47c1-BB16-4C865D527A10}"));
00293
00294 LoadGallery();
00295 }
00296 catch (Exception exception)
00297 {
00298 MessageBox.Show(this, exception.Message, Resources.AppTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
00299 Close();
00300 }
00301
00302 Cursor = Cursors.Default;
00303 }
00304
00305 void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
00306 {
00307 SetStatusText(string.Empty);
00308 }
00309
00310 private void detailsToolStripMenuItem_Click(object sender, EventArgs e)
00311 {
00312 SetListView();
00313 }
00314
00315 private void iconsToolStripMenuItem_Click(object sender, EventArgs e)
00316 {
00317 SetListView();
00318 }
00319
00320 private void pluginsListView_SelectedIndexChanged(object sender, EventArgs e)
00321 {
00322 LoadPluginInformation();
00323 }
00324
00325 private void loginGalleryToolStripButton_Click(object sender, EventArgs e)
00326 {
00327 try
00328 {
00329 LoginAsGalleryManager();
00330 }
00331 catch (Exception exception)
00332 {
00333 MessageBox.Show(this, exception.Message, Resources.AppTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
00334 }
00335 }
00336
00337 private void addToGalleryToolStripButton_Click(object sender, EventArgs e)
00338 {
00339 object returnValue = _PluginPublisher.GetType().InvokeMember("AddPlugin",
00340 BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly,
00341 null,
00342 _PluginPublisher,
00343 new IDisposable[] { this, _PluginHost });
00344
00345 if (Convert.ToBoolean(returnValue))
00346 {
00347 LoadGallery();
00348 }
00349 }
00350
00351 private void closeToolStripLabel_Click(object sender, EventArgs e)
00352 {
00353 Close();
00354 }
00355
00356 private void installToolStripButton_Click(object sender, EventArgs e)
00357 {
00358 InstallPlugin();
00359 }
00360
00361 private void PluginGalleryBrowserForm_FormClosed(object sender, FormClosedEventArgs e)
00362 {
00363 Settings.Default.LastGalleryView = pluginsListView.View;
00364 Settings.Default.Save();
00365 }
00366
00367 private void PluginGalleryBrowserForm_DoubleClick(object sender, EventArgs e)
00368 {
00369 if (WindowState == FormWindowState.Normal)
00370 {
00371 WindowState = FormWindowState.Maximized;
00372 }
00373 else
00374 {
00375 WindowState = FormWindowState.Normal;
00376 }
00377 }
00378
00379 private void pluginsListView_DoubleClick(object sender, EventArgs e)
00380 {
00381 if (installToolStripButton.Enabled)
00382 {
00383 InstallPlugin();
00384 }
00385 }
00386
00387 #endregion
00388 }
00389 }