PageRenderTime 52ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/Client/Setup/SelectSiteAndUrlDialog.cs

#
C# | 388 lines | 302 code | 51 blank | 35 comment | 17 complexity | 408fe4bd3681d192c534014102d5aec6 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. //-----------------------------------------------------------------------
  2. // <copyright>
  3. // Copyright (C) Ruslan Yakushev for the PHP Manager for IIS project.
  4. //
  5. // This file is subject to the terms and conditions of the Microsoft Public License (MS-PL).
  6. // See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL for more details.
  7. // </copyright>
  8. //-----------------------------------------------------------------------
  9. //#define VSDesigner
  10. using System;
  11. using System.Collections;
  12. using System.Collections.Generic;
  13. using System.ComponentModel;
  14. using System.Windows.Forms;
  15. using Microsoft.Web.Management.Client;
  16. using Microsoft.Web.Management.Client.Win32;
  17. using Microsoft.Web.Management.Server;
  18. namespace Web.Management.PHP.Setup
  19. {
  20. internal sealed class SelectSiteAndUrlDialog :
  21. #if VSDesigner
  22. Form
  23. #else
  24. TaskForm
  25. #endif
  26. {
  27. private readonly Guid _preferenceServiceGuid = new Guid("68a2a947-eeb6-40d9-9e5a-977bf7753bce");
  28. private const string ServerSelectSitePreferenceKey = "ServerSitePreferenceKey";
  29. private const string ServerSelectUrlPreferenceKey = "ServerUrlPreferenceKey";
  30. private readonly PHPModule _module;
  31. private readonly Connection _connection;
  32. private string _preferenceUrl;
  33. private string _preferenceSite;
  34. private PreferencesStore _store;
  35. private ManagementPanel _contentPanel;
  36. private ComboBox _urlsComboBox;
  37. private ComboBox _sitesComboBox;
  38. private Label _urlsLabel;
  39. private Label _sitesLabel;
  40. /// <summary>
  41. /// Required designer variable.
  42. /// </summary>
  43. private readonly IContainer components = null;
  44. public SelectSiteAndUrlDialog(PHPModule module, Connection connection)
  45. : base(module)
  46. {
  47. _module = module;
  48. _connection = connection;
  49. InitializeComponent();
  50. InitializeUI();
  51. Update();
  52. }
  53. protected override bool CanAccept
  54. {
  55. get
  56. {
  57. return _urlsComboBox.SelectedIndex >= 0;
  58. }
  59. }
  60. protected override bool CanShowHelp
  61. {
  62. get
  63. {
  64. return true;
  65. }
  66. }
  67. private PreferencesStore PreferencesStore
  68. {
  69. get
  70. {
  71. if (_store == null)
  72. {
  73. var prefService = (IPreferencesService)GetService(typeof(IPreferencesService));
  74. if (prefService != null)
  75. {
  76. _store = prefService.GetPreferencesStore(_preferenceServiceGuid);
  77. }
  78. }
  79. return _store;
  80. }
  81. }
  82. public string RelativePath
  83. {
  84. get
  85. {
  86. if (_connection.ConfigurationPath.PathType != ConfigurationPathType.Server)
  87. {
  88. return _connection.ConfigurationPath.GetEffectiveConfigurationPath(ManagementScope.Site);
  89. }
  90. return String.Empty;
  91. }
  92. }
  93. public string SelectedUrl
  94. {
  95. get
  96. {
  97. return _urlsComboBox.SelectedItem as string;
  98. }
  99. }
  100. public string SiteName
  101. {
  102. get
  103. {
  104. return _sitesComboBox.SelectedItem as string;
  105. }
  106. }
  107. /// <summary>
  108. /// Clean up any resources being used.
  109. /// </summary>
  110. /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
  111. protected override void Dispose(bool disposing)
  112. {
  113. if (disposing && (components != null))
  114. {
  115. components.Dispose();
  116. }
  117. base.Dispose(disposing);
  118. }
  119. private static string GetSitePreferenceKey(Connection connection)
  120. {
  121. return connection.ConfigurationPath.SiteName + "/" + connection.ConfigurationPath.GetEffectiveConfigurationPath(ManagementScope.Site);
  122. }
  123. #region Windows Form Designer generated code
  124. /// <summary>
  125. /// Required method for Designer support - do not modify
  126. /// the contents of this method with the code editor.
  127. /// </summary>
  128. private void InitializeComponent()
  129. {
  130. _urlsComboBox = new System.Windows.Forms.ComboBox();
  131. _sitesComboBox = new System.Windows.Forms.ComboBox();
  132. _urlsLabel = new System.Windows.Forms.Label();
  133. _sitesLabel = new System.Windows.Forms.Label();
  134. SuspendLayout();
  135. //
  136. // _domainsComboBox
  137. //
  138. _urlsComboBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
  139. _urlsComboBox.FormattingEnabled = true;
  140. _urlsComboBox.Location = new System.Drawing.Point(0, 90);
  141. _urlsComboBox.Name = "_domainsComboBox";
  142. _urlsComboBox.Size = new System.Drawing.Size(385, 21);
  143. _urlsComboBox.TabIndex = 3;
  144. _urlsComboBox.SelectedIndexChanged += new System.EventHandler(OnUrlsComboBoxSelectedIndexChanged);
  145. //
  146. // _sitesComboBox
  147. //
  148. _sitesComboBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
  149. _sitesComboBox.FormattingEnabled = true;
  150. _sitesComboBox.Location = new System.Drawing.Point(0, 25);
  151. _sitesComboBox.Name = "_sitesComboBox";
  152. _sitesComboBox.Size = new System.Drawing.Size(385, 21);
  153. _sitesComboBox.TabIndex = 1;
  154. _sitesComboBox.SelectedIndexChanged += new System.EventHandler(OnSitesComboBoxSelectedIndexChanged);
  155. //
  156. // _domainsLabel
  157. //
  158. _urlsLabel.Location = new System.Drawing.Point(0, 65);
  159. _urlsLabel.Name = "_domainsLabel";
  160. _urlsLabel.Size = new System.Drawing.Size(385, 22);
  161. _urlsLabel.TabIndex = 2;
  162. _urlsLabel.Text = Resources.SelectSiteAndUrlDialogSelectAUrl;
  163. //
  164. // _sitesLabel
  165. //
  166. _sitesLabel.Location = new System.Drawing.Point(0, 0);
  167. _sitesLabel.Name = "_sitesLabel";
  168. _sitesLabel.Size = new System.Drawing.Size(385, 22);
  169. _sitesLabel.TabIndex = 0;
  170. _sitesLabel.Text = Resources.SelectSiteAndUrlDialogSelectASite;
  171. //
  172. // SelectSiteDomainDialog
  173. //
  174. ClientSize = new System.Drawing.Size(414, 192);
  175. Controls.Add(_sitesComboBox);
  176. Controls.Add(_sitesLabel);
  177. Controls.Add(_urlsLabel);
  178. Controls.Add(_urlsComboBox);
  179. Name = "SelectSiteDomainDialog";
  180. ResumeLayout(false);
  181. #if VSDesigner
  182. PerformLayout();
  183. #endif
  184. }
  185. #endregion
  186. public void InitializeUI()
  187. {
  188. _contentPanel = new ManagementPanel();
  189. _contentPanel.SuspendLayout();
  190. _contentPanel.Location = new System.Drawing.Point(0, 0);
  191. _contentPanel.Dock = DockStyle.Fill;
  192. _contentPanel.Controls.Add(_urlsLabel);
  193. _contentPanel.Controls.Add(_urlsComboBox);
  194. _contentPanel.Controls.Add(_sitesLabel);
  195. _contentPanel.Controls.Add(_sitesComboBox);
  196. _contentPanel.ResumeLayout(false);
  197. _contentPanel.PerformLayout();
  198. Text = Resources.SelectSiteAndUrlDialogTitle;
  199. SetContent(_contentPanel);
  200. UpdateTaskForm();
  201. }
  202. private void LoadServerPreferences(PreferencesStore store)
  203. {
  204. _preferenceSite = store.GetValue(ServerSelectSitePreferenceKey, String.Empty);
  205. _preferenceUrl = store.GetValue(ServerSelectUrlPreferenceKey, String.Empty);
  206. }
  207. private void LoadSitePreferences(PreferencesStore store)
  208. {
  209. _preferenceUrl = store.GetValue(GetSitePreferenceKey(_connection), String.Empty);
  210. }
  211. protected override void OnAccept()
  212. {
  213. if (_connection.ConfigurationPath.PathType == ConfigurationPathType.Server)
  214. {
  215. SaveServerPreferences(PreferencesStore);
  216. }
  217. else
  218. {
  219. SaveSitePreferences(PreferencesStore);
  220. }
  221. DialogResult = DialogResult.OK;
  222. Close();
  223. }
  224. protected override void OnLoad(EventArgs e)
  225. {
  226. base.OnLoad(e);
  227. if (_connection.ConfigurationPath.PathType == ConfigurationPathType.Server)
  228. {
  229. LoadServerPreferences(PreferencesStore);
  230. StartAsyncTask(OnSiteWorkerDoWork, OnSiteWorkerDoWorkCompleted);
  231. }
  232. else
  233. {
  234. LoadSitePreferences(PreferencesStore);
  235. _sitesComboBox.Items.Add(_connection.ConfigurationPath.SiteName);
  236. _sitesComboBox.SelectedIndex = 0;
  237. _sitesComboBox.Enabled = false;
  238. }
  239. }
  240. private void OnSitesComboBoxSelectedIndexChanged(object sender, EventArgs e)
  241. {
  242. StartAsyncTask(OnUrlsWorkerDoWork, OnUrlsWorkerDoWorkCompleted, null, _sitesComboBox.SelectedItem);
  243. }
  244. private void OnSiteWorkerDoWork(object sender, DoWorkEventArgs e)
  245. {
  246. e.Result = _module.Proxy.GetSites();
  247. }
  248. private void OnSiteWorkerDoWorkCompleted(object sender, RunWorkerCompletedEventArgs e)
  249. {
  250. _sitesComboBox.BeginUpdate();
  251. _sitesComboBox.SuspendLayout();
  252. try
  253. {
  254. _sitesComboBox.Items.Clear();
  255. var sites = e.Result as ArrayList;
  256. sites.Sort();
  257. foreach (string siteName in sites)
  258. {
  259. _sitesComboBox.Items.Add(siteName);
  260. }
  261. if (!String.IsNullOrEmpty(_preferenceSite))
  262. {
  263. var selectedIndex = _sitesComboBox.Items.IndexOf(_preferenceSite);
  264. if (selectedIndex >= 0)
  265. {
  266. _sitesComboBox.SelectedIndex = selectedIndex;
  267. }
  268. }
  269. }
  270. catch (Exception ex)
  271. {
  272. DisplayErrorMessage(ex, Resources.ResourceManager);
  273. }
  274. finally
  275. {
  276. _sitesComboBox.ResumeLayout();
  277. _sitesComboBox.EndUpdate();
  278. }
  279. _sitesComboBox.Focus();
  280. }
  281. private void OnUrlsComboBoxSelectedIndexChanged(object sender, EventArgs e)
  282. {
  283. Update();
  284. }
  285. private void OnUrlsWorkerDoWork(object sender, DoWorkEventArgs e)
  286. {
  287. var siteName = (string)e.Argument;
  288. e.Result = Helper.GetUrlListFromBindings(_connection.ScopePath.ServerName, _module.Proxy.GetSiteBindings(siteName), RelativePath);
  289. }
  290. private void OnUrlsWorkerDoWorkCompleted(object sender, RunWorkerCompletedEventArgs e)
  291. {
  292. _urlsComboBox.BeginUpdate();
  293. _urlsComboBox.SuspendLayout();
  294. try
  295. {
  296. _urlsComboBox.Items.Clear();
  297. var domains = e.Result as List<string>;
  298. foreach (string domain in domains)
  299. {
  300. _urlsComboBox.Items.Add(domain);
  301. }
  302. _urlsComboBox.SelectedIndex = 0;
  303. if (!String.IsNullOrEmpty(_preferenceUrl))
  304. {
  305. int selectedIndex = _urlsComboBox.Items.IndexOf(_preferenceUrl);
  306. if (selectedIndex > 0)
  307. {
  308. _urlsComboBox.SelectedIndex = selectedIndex;
  309. }
  310. }
  311. }
  312. catch (Exception ex)
  313. {
  314. DisplayErrorMessage(ex, Resources.ResourceManager);
  315. }
  316. finally
  317. {
  318. _urlsComboBox.ResumeLayout();
  319. _urlsComboBox.EndUpdate();
  320. }
  321. _urlsComboBox.Focus();
  322. }
  323. private void SaveServerPreferences(PreferencesStore store)
  324. {
  325. store.SetValue(ServerSelectSitePreferenceKey, SiteName, String.Empty);
  326. store.SetValue(ServerSelectUrlPreferenceKey, SelectedUrl, String.Empty);
  327. }
  328. private void SaveSitePreferences(PreferencesStore store)
  329. {
  330. store.SetValue(GetSitePreferenceKey(_connection), SelectedUrl, String.Empty);
  331. }
  332. protected override void ShowHelp()
  333. {
  334. Helper.Browse(Globals.SelectSiteDomainOnlineHelp);
  335. }
  336. }
  337. }