PageRenderTime 40ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

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