PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/stable-1.2.0/Client/Setup/SelectSiteAndUrlDialog.cs

#
C# | 391 lines | 305 code | 51 blank | 35 comment | 17 complexity | dfce5651551f8bca3d3b5b6c0de269c8 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 Guid PreferenceServiceGuid = new Guid("68a2a947-eeb6-40d9-9e5a-977bf7753bce");
  28. private const string ServerSelectSitePreferenceKey = "ServerSitePreferenceKey";
  29. private const string ServerSelectUrlPreferenceKey = "ServerUrlPreferenceKey";
  30. private PHPModule _module;
  31. private 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 System.ComponentModel.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. IPreferencesService 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. else
  91. {
  92. return String.Empty;
  93. }
  94. }
  95. }
  96. public string SelectedUrl
  97. {
  98. get
  99. {
  100. return _urlsComboBox.SelectedItem as string;
  101. }
  102. }
  103. public string SiteName
  104. {
  105. get
  106. {
  107. return _sitesComboBox.SelectedItem as string;
  108. }
  109. }
  110. /// <summary>
  111. /// Clean up any resources being used.
  112. /// </summary>
  113. /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
  114. protected override void Dispose(bool disposing)
  115. {
  116. if (disposing && (components != null))
  117. {
  118. components.Dispose();
  119. }
  120. base.Dispose(disposing);
  121. }
  122. private static string GetSitePreferenceKey(Connection connection)
  123. {
  124. return connection.ConfigurationPath.SiteName + "/" + connection.ConfigurationPath.GetEffectiveConfigurationPath(ManagementScope.Site);
  125. }
  126. #region Windows Form Designer generated code
  127. /// <summary>
  128. /// Required method for Designer support - do not modify
  129. /// the contents of this method with the code editor.
  130. /// </summary>
  131. private void InitializeComponent()
  132. {
  133. this._urlsComboBox = new System.Windows.Forms.ComboBox();
  134. this._sitesComboBox = new System.Windows.Forms.ComboBox();
  135. this._urlsLabel = new System.Windows.Forms.Label();
  136. this._sitesLabel = new System.Windows.Forms.Label();
  137. this.SuspendLayout();
  138. //
  139. // _domainsComboBox
  140. //
  141. this._urlsComboBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
  142. this._urlsComboBox.FormattingEnabled = true;
  143. this._urlsComboBox.Location = new System.Drawing.Point(0, 90);
  144. this._urlsComboBox.Name = "_domainsComboBox";
  145. this._urlsComboBox.Size = new System.Drawing.Size(385, 21);
  146. this._urlsComboBox.TabIndex = 3;
  147. this._urlsComboBox.SelectedIndexChanged += new System.EventHandler(this.OnUrlsComboBoxSelectedIndexChanged);
  148. //
  149. // _sitesComboBox
  150. //
  151. this._sitesComboBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
  152. this._sitesComboBox.FormattingEnabled = true;
  153. this._sitesComboBox.Location = new System.Drawing.Point(0, 25);
  154. this._sitesComboBox.Name = "_sitesComboBox";
  155. this._sitesComboBox.Size = new System.Drawing.Size(385, 21);
  156. this._sitesComboBox.TabIndex = 1;
  157. this._sitesComboBox.SelectedIndexChanged += new System.EventHandler(this.OnSitesComboBoxSelectedIndexChanged);
  158. //
  159. // _domainsLabel
  160. //
  161. this._urlsLabel.Location = new System.Drawing.Point(0, 65);
  162. this._urlsLabel.Name = "_domainsLabel";
  163. this._urlsLabel.Size = new System.Drawing.Size(385, 22);
  164. this._urlsLabel.TabIndex = 2;
  165. this._urlsLabel.Text = Resources.SelectSiteAndUrlDialogSelectAUrl;
  166. //
  167. // _sitesLabel
  168. //
  169. this._sitesLabel.Location = new System.Drawing.Point(0, 0);
  170. this._sitesLabel.Name = "_sitesLabel";
  171. this._sitesLabel.Size = new System.Drawing.Size(385, 22);
  172. this._sitesLabel.TabIndex = 0;
  173. this._sitesLabel.Text = Resources.SelectSiteAndUrlDialogSelectASite;
  174. //
  175. // SelectSiteDomainDialog
  176. //
  177. this.ClientSize = new System.Drawing.Size(414, 192);
  178. this.Controls.Add(this._sitesComboBox);
  179. this.Controls.Add(this._sitesLabel);
  180. this.Controls.Add(this._urlsLabel);
  181. this.Controls.Add(this._urlsComboBox);
  182. this.Name = "SelectSiteDomainDialog";
  183. this.ResumeLayout(false);
  184. #if VSDesigner
  185. this.PerformLayout();
  186. #endif
  187. }
  188. #endregion
  189. public void InitializeUI()
  190. {
  191. this._contentPanel = new ManagementPanel();
  192. _contentPanel.SuspendLayout();
  193. this._contentPanel.Location = new System.Drawing.Point(0, 0);
  194. this._contentPanel.Dock = DockStyle.Fill;
  195. this._contentPanel.Controls.Add(_urlsLabel);
  196. this._contentPanel.Controls.Add(_urlsComboBox);
  197. this._contentPanel.Controls.Add(_sitesLabel);
  198. this._contentPanel.Controls.Add(_sitesComboBox);
  199. this._contentPanel.ResumeLayout(false);
  200. this._contentPanel.PerformLayout();
  201. this.Text = Resources.SelectSiteAndUrlDialogTitle;
  202. SetContent(_contentPanel);
  203. UpdateTaskForm();
  204. }
  205. private void LoadServerPreferences(PreferencesStore store)
  206. {
  207. _preferenceSite = store.GetValue(ServerSelectSitePreferenceKey, String.Empty);
  208. _preferenceUrl = store.GetValue(ServerSelectUrlPreferenceKey, String.Empty);
  209. }
  210. private void LoadSitePreferences(PreferencesStore store)
  211. {
  212. _preferenceUrl = store.GetValue(GetSitePreferenceKey(_connection), String.Empty);
  213. }
  214. protected override void OnAccept()
  215. {
  216. if (_connection.ConfigurationPath.PathType == ConfigurationPathType.Server)
  217. {
  218. SaveServerPreferences(PreferencesStore);
  219. }
  220. else
  221. {
  222. SaveSitePreferences(PreferencesStore);
  223. }
  224. this.DialogResult = DialogResult.OK;
  225. this.Close();
  226. }
  227. protected override void OnLoad(EventArgs e)
  228. {
  229. base.OnLoad(e);
  230. if (_connection.ConfigurationPath.PathType == ConfigurationPathType.Server)
  231. {
  232. LoadServerPreferences(PreferencesStore);
  233. StartAsyncTask(OnSiteWorkerDoWork, OnSiteWorkerDoWorkCompleted);
  234. }
  235. else
  236. {
  237. LoadSitePreferences(PreferencesStore);
  238. _sitesComboBox.Items.Add(_connection.ConfigurationPath.SiteName);
  239. _sitesComboBox.SelectedIndex = 0;
  240. _sitesComboBox.Enabled = false;
  241. }
  242. }
  243. private void OnSitesComboBoxSelectedIndexChanged(object sender, EventArgs e)
  244. {
  245. StartAsyncTask(OnUrlsWorkerDoWork, OnUrlsWorkerDoWorkCompleted, null, (string)_sitesComboBox.SelectedItem);
  246. }
  247. private void OnSiteWorkerDoWork(object sender, DoWorkEventArgs e)
  248. {
  249. e.Result = _module.Proxy.GetSites();
  250. }
  251. private void OnSiteWorkerDoWorkCompleted(object sender, RunWorkerCompletedEventArgs e)
  252. {
  253. _sitesComboBox.BeginUpdate();
  254. _sitesComboBox.SuspendLayout();
  255. try
  256. {
  257. _sitesComboBox.Items.Clear();
  258. ArrayList sites = e.Result as ArrayList;
  259. sites.Sort();
  260. foreach (string siteName in sites)
  261. {
  262. _sitesComboBox.Items.Add(siteName);
  263. }
  264. if (!String.IsNullOrEmpty(_preferenceSite))
  265. {
  266. int selectedIndex = _sitesComboBox.Items.IndexOf(_preferenceSite);
  267. if (selectedIndex >= 0)
  268. {
  269. _sitesComboBox.SelectedIndex = selectedIndex;
  270. }
  271. }
  272. }
  273. catch (Exception ex)
  274. {
  275. DisplayErrorMessage(ex, Resources.ResourceManager);
  276. }
  277. finally
  278. {
  279. _sitesComboBox.ResumeLayout();
  280. _sitesComboBox.EndUpdate();
  281. }
  282. _sitesComboBox.Focus();
  283. }
  284. private void OnUrlsComboBoxSelectedIndexChanged(object sender, System.EventArgs e)
  285. {
  286. Update();
  287. }
  288. private void OnUrlsWorkerDoWork(object sender, DoWorkEventArgs e)
  289. {
  290. string siteName = (string)e.Argument;
  291. e.Result = Helper.GetUrlListFromBindings(_connection.ScopePath.ServerName, _module.Proxy.GetSiteBindings(siteName), this.RelativePath);
  292. }
  293. private void OnUrlsWorkerDoWorkCompleted(object sender, RunWorkerCompletedEventArgs e)
  294. {
  295. _urlsComboBox.BeginUpdate();
  296. _urlsComboBox.SuspendLayout();
  297. try
  298. {
  299. _urlsComboBox.Items.Clear();
  300. List<string> domains = e.Result as List<string>;
  301. foreach (string domain in domains)
  302. {
  303. _urlsComboBox.Items.Add(domain);
  304. }
  305. _urlsComboBox.SelectedIndex = 0;
  306. if (!String.IsNullOrEmpty(_preferenceUrl))
  307. {
  308. int selectedIndex = _urlsComboBox.Items.IndexOf(_preferenceUrl);
  309. if (selectedIndex > 0)
  310. {
  311. _urlsComboBox.SelectedIndex = selectedIndex;
  312. }
  313. }
  314. }
  315. catch (Exception ex)
  316. {
  317. DisplayErrorMessage(ex, Resources.ResourceManager);
  318. }
  319. finally
  320. {
  321. _urlsComboBox.ResumeLayout();
  322. _urlsComboBox.EndUpdate();
  323. }
  324. _urlsComboBox.Focus();
  325. }
  326. private void SaveServerPreferences(PreferencesStore store)
  327. {
  328. store.SetValue(ServerSelectSitePreferenceKey, SiteName, String.Empty);
  329. store.SetValue(ServerSelectUrlPreferenceKey, SelectedUrl, String.Empty);
  330. }
  331. private void SaveSitePreferences(PreferencesStore store)
  332. {
  333. store.SetValue(GetSitePreferenceKey(_connection), SelectedUrl, String.Empty);
  334. }
  335. protected override void ShowHelp()
  336. {
  337. Helper.Browse(Globals.SelectSiteDomainOnlineHelp);
  338. }
  339. }
  340. }