PageRenderTime 51ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/stable-1.2.0/Client/Settings/AllSettingsPage.cs

#
C# | 568 lines | 475 code | 79 blank | 14 comment | 57 complexity | d49c5988f569a0478fbc95f3c8305dce 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. using System;
  10. using System.Collections.Generic;
  11. using System.ComponentModel;
  12. using System.Diagnostics;
  13. using System.Globalization;
  14. using System.IO;
  15. using System.Windows.Forms;
  16. using Microsoft.Web.Management.Client;
  17. using Microsoft.Web.Management.Client.Win32;
  18. using Web.Management.PHP.Config;
  19. namespace Web.Management.PHP.Settings
  20. {
  21. [ModulePageIdentifier(Globals.PHPSettingsPageIdentifier)]
  22. internal sealed class AllSettingsPage : ModuleListPage, IModuleChildPage
  23. {
  24. private ColumnHeader _nameColumn;
  25. private ColumnHeader _valueColumn;
  26. private ColumnHeader _sectionColumn;
  27. private ModuleListPageGrouping _sectionGrouping;
  28. private PageTaskList _taskList;
  29. private ModuleListPageSearchField[] _searchFields;
  30. private PHPIniFile _file;
  31. private const string NameString = "Name";
  32. private const string ValueString = "Value";
  33. private const string SectionString = "Section";
  34. private string _filterBy;
  35. private string _filterValue;
  36. private IModulePage _parentPage;
  37. // This is used to remember the name of added/updated setting so that
  38. // it can be re-selected during refresh.
  39. private string _updatedSettingName;
  40. protected override bool CanRefresh
  41. {
  42. get
  43. {
  44. return true;
  45. }
  46. }
  47. protected override bool CanSearch
  48. {
  49. get
  50. {
  51. return true;
  52. }
  53. }
  54. protected override ModuleListPageGrouping DefaultGrouping
  55. {
  56. get
  57. {
  58. return Groupings[0];
  59. }
  60. }
  61. public override ModuleListPageGrouping[] Groupings
  62. {
  63. get
  64. {
  65. if (_sectionGrouping == null)
  66. {
  67. _sectionGrouping = new ModuleListPageGrouping(SectionString, Resources.AllSettingsPageSectionField);
  68. }
  69. return new ModuleListPageGrouping[] { _sectionGrouping };
  70. }
  71. }
  72. internal bool IsReadOnly
  73. {
  74. get
  75. {
  76. return Connection.ConfigurationPath.PathType == Microsoft.Web.Management.Server.ConfigurationPathType.Site &&
  77. !Connection.IsUserServerAdministrator;
  78. }
  79. }
  80. private new PHPModule Module
  81. {
  82. get
  83. {
  84. return (PHPModule)base.Module;
  85. }
  86. }
  87. public IModulePage ParentPage
  88. {
  89. get
  90. {
  91. return _parentPage;
  92. }
  93. set
  94. {
  95. _parentPage = value;
  96. }
  97. }
  98. protected override ModuleListPageSearchField[] SearchFields
  99. {
  100. get
  101. {
  102. if (_searchFields == null)
  103. {
  104. _searchFields = new ModuleListPageSearchField[]{
  105. new ModuleListPageSearchField(NameString, Resources.AllSettingsPageNameField),
  106. new ModuleListPageSearchField(ValueString, Resources.AllSettingsPageValueField),
  107. new ModuleListPageSearchField(SectionString, Resources.AllSettingsPageSectionField)};
  108. }
  109. return _searchFields;
  110. }
  111. }
  112. private PHPSettingItem SelectedItem
  113. {
  114. get
  115. {
  116. if (ListView.SelectedIndices.Count == 1)
  117. {
  118. return ListView.SelectedItems[0] as PHPSettingItem;
  119. }
  120. return null;
  121. }
  122. }
  123. protected override TaskListCollection Tasks
  124. {
  125. get
  126. {
  127. TaskListCollection tasks = base.Tasks;
  128. if (_taskList == null)
  129. {
  130. _taskList = new PageTaskList(this);
  131. }
  132. tasks.Add(_taskList);
  133. return tasks;
  134. }
  135. }
  136. private void AddPHPSetting()
  137. {
  138. using (AddEditSettingDialog dlg = new AddEditSettingDialog(Module, GetListOfSections()))
  139. {
  140. if (ShowDialog(dlg) == DialogResult.OK)
  141. {
  142. // Save the name of added setting so that we can select it after refresh
  143. _updatedSettingName = dlg.SettingName;
  144. Refresh();
  145. }
  146. }
  147. }
  148. private void EditPHPSetting()
  149. {
  150. if (SelectedItem != null && !this.IsReadOnly)
  151. {
  152. using (AddEditSettingDialog dlg = new AddEditSettingDialog(Module, SelectedItem.Setting))
  153. {
  154. if (ShowDialog(dlg) == DialogResult.OK)
  155. {
  156. // Save the name of edited setting so that we can select it after refresh
  157. _updatedSettingName = dlg.SettingName;
  158. Refresh();
  159. }
  160. }
  161. }
  162. }
  163. protected override ListViewGroup[] GetGroups(ModuleListPageGrouping grouping)
  164. {
  165. Dictionary<string, ListViewGroup> groups = new Dictionary<string, ListViewGroup>();
  166. if (grouping == _sectionGrouping)
  167. {
  168. ListView.ListViewItemCollection items = ListView.Items;
  169. for (int i = 0; i < items.Count; i++)
  170. {
  171. PHPSettingItem item = (PHPSettingItem)items[i];
  172. string sectionName = item.SectionName;
  173. if (String.IsNullOrEmpty(sectionName)) {
  174. continue;
  175. }
  176. if (!groups.ContainsKey(sectionName))
  177. {
  178. ListViewGroup sectionGroup = new ListViewGroup(sectionName, sectionName);
  179. groups.Add(sectionName, sectionGroup);
  180. }
  181. }
  182. }
  183. ListViewGroup[] result = new ListViewGroup[groups.Count];
  184. groups.Values.CopyTo(result, 0);
  185. return result;
  186. }
  187. private IList<string> GetListOfSections()
  188. {
  189. SortedList<string, object> sections = new SortedList<string, object>();
  190. foreach (PHPSettingItem item in ListView.Items)
  191. {
  192. string section = item.Setting.Section;
  193. if (String.IsNullOrEmpty(section))
  194. {
  195. continue;
  196. }
  197. if (!sections.ContainsKey(section))
  198. {
  199. sections.Add(item.Setting.Section, null);
  200. }
  201. }
  202. return sections.Keys;
  203. }
  204. private void GetSettings()
  205. {
  206. StartAsyncTask(Resources.AllSettingsPageGettingSettings, OnGetSettings, OnGetSettingsCompleted);
  207. }
  208. private void GoBack()
  209. {
  210. Navigate(typeof(PHPPage));
  211. }
  212. protected override void InitializeListPage()
  213. {
  214. _nameColumn = new ColumnHeader();
  215. _nameColumn.Text = Resources.AllSettingsPageNameField;
  216. _nameColumn.Width = 180;
  217. _valueColumn = new ColumnHeader();
  218. _valueColumn.Text = Resources.AllSettingsPageValueField;
  219. _valueColumn.Width = 180;
  220. _sectionColumn = new ColumnHeader();
  221. _sectionColumn.Text = Resources.AllSettingsPageSectionField;
  222. _sectionColumn.Width = 100;
  223. ListView.Columns.AddRange(new ColumnHeader[] { _nameColumn, _valueColumn, _sectionColumn });
  224. ListView.MultiSelect = false;
  225. ListView.SelectedIndexChanged += new EventHandler(OnListViewSelectedIndexChanged);
  226. ListView.KeyUp += new KeyEventHandler(OnListViewKeyUp);
  227. ListView.ItemActivate += new EventHandler(OnListViewItemActivate);
  228. }
  229. private void LoadPHPIni(PHPIniFile file)
  230. {
  231. try
  232. {
  233. ListView.SuspendLayout();
  234. ListView.Items.Clear();
  235. foreach (PHPIniSetting setting in file.Settings)
  236. {
  237. if (_filterBy != null && _filterValue != null) {
  238. if (_filterBy == NameString &&
  239. setting.Name.IndexOf(_filterValue, StringComparison.OrdinalIgnoreCase) == -1)
  240. {
  241. continue;
  242. }
  243. else if (_filterBy == ValueString &&
  244. setting.Value.IndexOf(_filterValue, StringComparison.OrdinalIgnoreCase) == -1)
  245. {
  246. continue;
  247. }
  248. else if (_filterBy == SectionString &&
  249. setting.Section.IndexOf(_filterValue, StringComparison.OrdinalIgnoreCase) == -1)
  250. {
  251. continue;
  252. }
  253. }
  254. ListView.Items.Add(new PHPSettingItem(setting));
  255. }
  256. if (SelectedGrouping != null)
  257. {
  258. Group(SelectedGrouping);
  259. }
  260. }
  261. finally
  262. {
  263. ListView.ResumeLayout();
  264. }
  265. }
  266. protected override void OnActivated(bool initialActivation)
  267. {
  268. base.OnActivated(initialActivation);
  269. if (initialActivation)
  270. {
  271. GetSettings();
  272. }
  273. }
  274. private void OnGetSettings(object sender, DoWorkEventArgs e)
  275. {
  276. e.Result = Module.Proxy.GetPHPIniSettings();
  277. }
  278. private void OnGetSettingsCompleted(object sender, RunWorkerCompletedEventArgs e)
  279. {
  280. try
  281. {
  282. object o = e.Result;
  283. _file = new PHPIniFile();
  284. _file.SetData(o);
  285. LoadPHPIni(_file);
  286. // If updated setting name was saved then use it to re-select it after refresh
  287. if (!String.IsNullOrEmpty(_updatedSettingName))
  288. {
  289. SelectSettingByName(_updatedSettingName);
  290. _updatedSettingName = null;
  291. }
  292. }
  293. catch (Exception ex)
  294. {
  295. DisplayErrorMessage(ex, Resources.ResourceManager);
  296. }
  297. }
  298. protected override void OnGroup(ModuleListPageGrouping grouping)
  299. {
  300. ListView.SuspendLayout();
  301. try
  302. {
  303. foreach (PHPSettingItem item in ListView.Items)
  304. {
  305. if (grouping == _sectionGrouping)
  306. {
  307. item.Group = ListView.Groups[item.SectionName];
  308. }
  309. }
  310. }
  311. finally
  312. {
  313. ListView.ResumeLayout();
  314. }
  315. }
  316. private void OnListViewItemActivate(object sender, EventArgs e)
  317. {
  318. EditPHPSetting();
  319. }
  320. private void OnListViewKeyUp(object sender, KeyEventArgs e)
  321. {
  322. if (e.KeyData == Keys.Delete)
  323. {
  324. RemovePHPSetting();
  325. e.Handled = true;
  326. }
  327. }
  328. private void OnListViewSelectedIndexChanged(object sender, EventArgs e)
  329. {
  330. Update();
  331. }
  332. protected override void OnSearch(ModuleListPageSearchOptions options)
  333. {
  334. if (options.ShowAll)
  335. {
  336. _filterBy = null;
  337. _filterValue = null;
  338. LoadPHPIni(_file);
  339. }
  340. else
  341. {
  342. _filterBy = options.Field.Name;
  343. _filterValue = options.Text;
  344. LoadPHPIni(_file);
  345. }
  346. }
  347. internal void OpenPHPIniFile()
  348. {
  349. try
  350. {
  351. string physicalPath = Module.Proxy.GetPHPIniPhysicalPath();
  352. if (!String.IsNullOrEmpty(physicalPath) &&
  353. String.Equals(Path.GetExtension(physicalPath), ".ini", StringComparison.OrdinalIgnoreCase) &&
  354. File.Exists(physicalPath))
  355. {
  356. Process.Start(physicalPath);
  357. }
  358. else
  359. {
  360. ShowMessage(String.Format(CultureInfo.CurrentCulture, Resources.ErrorFileDoesNotExist, physicalPath), MessageBoxButtons.OK, MessageBoxIcon.Information);
  361. }
  362. }
  363. catch (Exception ex)
  364. {
  365. DisplayErrorMessage(ex, Resources.ResourceManager);
  366. }
  367. }
  368. protected override void Refresh()
  369. {
  370. GetSettings();
  371. }
  372. private void RemovePHPSetting()
  373. {
  374. PHPSettingItem item = SelectedItem;
  375. if (item != null)
  376. {
  377. if (ShowMessage(Resources.PHPIniSettingDeleteConfirmation, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question) == DialogResult.Yes)
  378. {
  379. try
  380. {
  381. Module.Proxy.RemoveSetting(item.Setting);
  382. ListView.Items.Remove(item);
  383. }
  384. catch(Exception ex)
  385. {
  386. DisplayErrorMessage(ex, Resources.ResourceManager);
  387. }
  388. }
  389. }
  390. }
  391. // Used to select an item by name after refresh
  392. private void SelectSettingByName(string name)
  393. {
  394. foreach (PHPSettingItem item in ListView.Items)
  395. {
  396. if (String.Equals(item.Setting.Name, name, StringComparison.OrdinalIgnoreCase))
  397. {
  398. item.Selected = true;
  399. item.EnsureVisible();
  400. break;
  401. }
  402. }
  403. }
  404. protected override bool ShowHelp()
  405. {
  406. return ShowOnlineHelp();
  407. }
  408. protected override bool ShowOnlineHelp()
  409. {
  410. return Helper.Browse(Globals.AllSettingsOnlineHelp);
  411. }
  412. private class PageTaskList : TaskList
  413. {
  414. private AllSettingsPage _page;
  415. public PageTaskList(AllSettingsPage page)
  416. {
  417. _page = page;
  418. }
  419. public void AddSetting()
  420. {
  421. _page.AddPHPSetting();
  422. }
  423. public void EditSetting()
  424. {
  425. _page.EditPHPSetting();
  426. }
  427. public override System.Collections.ICollection GetTaskItems()
  428. {
  429. List<TaskItem> tasks = new List<TaskItem>();
  430. if (_page.IsReadOnly)
  431. {
  432. tasks.Add(new MessageTaskItem(MessageTaskItemType.Information, Resources.AllPagesPageIsReadOnly, "Information"));
  433. }
  434. else
  435. {
  436. tasks.Add(new MethodTaskItem("AddSetting", Resources.AllPagesAddTask, "Edit"));
  437. if (_page.SelectedItem != null)
  438. {
  439. tasks.Add(new MethodTaskItem("EditSetting", Resources.AllSettingsPageEditTask, "Edit", null));
  440. tasks.Add(new MethodTaskItem("RemoveSetting", Resources.AllSettingsPageRemoveTask, "Edit", null, Resources.Delete16));
  441. }
  442. if (_page.Connection.IsLocalConnection)
  443. {
  444. tasks.Add(new MethodTaskItem("OpenPHPIniFile", Resources.AllPagesOpenPHPIniTask, "Tasks", null));
  445. }
  446. }
  447. tasks.Add(new MethodTaskItem("GoBack", Resources.AllPagesGoBackTask, "Tasks", null, Resources.GoBack16));
  448. return tasks;
  449. }
  450. public void GoBack()
  451. {
  452. _page.GoBack();
  453. }
  454. public void OpenPHPIniFile()
  455. {
  456. _page.OpenPHPIniFile();
  457. }
  458. public void RemoveSetting()
  459. {
  460. _page.RemovePHPSetting();
  461. }
  462. }
  463. private class PHPSettingItem : ListViewItem
  464. {
  465. private PHPIniSetting _setting;
  466. public PHPSettingItem(PHPIniSetting setting)
  467. {
  468. _setting = setting;
  469. Text = _setting.Name;
  470. SubItems.Add(_setting.Value);
  471. SubItems.Add(_setting.Section);
  472. }
  473. public string SectionName
  474. {
  475. get
  476. {
  477. return _setting.Section;
  478. }
  479. }
  480. public PHPIniSetting Setting
  481. {
  482. get
  483. {
  484. return _setting;
  485. }
  486. }
  487. }
  488. }
  489. }