PageRenderTime 46ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/beta-1.0.2/Client/Settings/ErrorReportingPage.cs

#
C# | 546 lines | 443 code | 62 blank | 41 comment | 51 complexity | 648e4f89a8c5dac5d583fd5bbaededa1 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.Windows.Forms;
  14. using Microsoft.Web.Management.Client;
  15. using Microsoft.Web.Management.Client.Win32;
  16. using Web.Management.PHP.Config;
  17. namespace Web.Management.PHP.Settings
  18. {
  19. [ModulePageIdentifier(Globals.ErrorReportingPageIdentifier)]
  20. internal sealed class ErrorReportingPage : ModuleDialogPage, IModuleChildPage
  21. {
  22. enum ErrorReportingPreset { Undefined = 0, Development = 1, Production = 2 };
  23. private string _errorLogFile = String.Empty;
  24. private ErrorReportingPreset _errorReportingPreset = ErrorReportingPreset.Undefined;
  25. private bool _hasChanges;
  26. private readonly string[] SettingNames = {
  27. "error_reporting",
  28. "display_errors",
  29. "track_errors",
  30. "html_errors",
  31. "log_errors",
  32. "fastcgi.logging"
  33. };
  34. private readonly string[] SettingsDevValues = {
  35. "E_ALL | E_STRICT",
  36. "On",
  37. "On",
  38. "On",
  39. "On",
  40. "1"
  41. };
  42. private readonly string[] SettingsProdValues = {
  43. "E_ALL & ~E_DEPRECATED",
  44. "Off",
  45. "Off",
  46. "Off",
  47. "On",
  48. "0"
  49. };
  50. private Label _devMachineLabel;
  51. private Label _selectServerTypeLabel;
  52. private RadioButton _devMachineRadioButton;
  53. private RadioButton _prodMachineRadioButton;
  54. private Label _prodMachineLabel;
  55. private Label _errorLogFileLabel;
  56. private TextBox _errorLogFileTextBox;
  57. private Button _errorLogBrowseButton;
  58. private GroupBox _serverTypeGroupBox;
  59. private PageTaskList _taskList;
  60. private IModulePage _parentPage;
  61. protected override bool CanApplyChanges
  62. {
  63. get
  64. {
  65. return _hasChanges && (_devMachineRadioButton.Checked || _prodMachineRadioButton.Checked);
  66. }
  67. }
  68. protected override bool HasChanges
  69. {
  70. get
  71. {
  72. return _hasChanges;
  73. }
  74. }
  75. internal bool IsReadOnly
  76. {
  77. get
  78. {
  79. return Connection.ConfigurationPath.PathType == Microsoft.Web.Management.Server.ConfigurationPathType.Site &&
  80. !Connection.IsUserServerAdministrator;
  81. }
  82. }
  83. private new PHPModule Module
  84. {
  85. get
  86. {
  87. return (PHPModule)base.Module;
  88. }
  89. }
  90. public IModulePage ParentPage
  91. {
  92. get
  93. {
  94. return _parentPage;
  95. }
  96. set
  97. {
  98. _parentPage = value;
  99. }
  100. }
  101. protected override TaskListCollection Tasks
  102. {
  103. get
  104. {
  105. TaskListCollection tasks = base.Tasks;
  106. if (_taskList == null)
  107. {
  108. _taskList = new PageTaskList(this);
  109. }
  110. tasks.Add(_taskList);
  111. return tasks;
  112. }
  113. }
  114. protected override bool ApplyChanges()
  115. {
  116. bool appliedChanges = false;
  117. string[] settingValues = null;
  118. Debug.Assert(_devMachineRadioButton.Checked || _prodMachineRadioButton.Checked);
  119. if (_devMachineRadioButton.Checked)
  120. {
  121. settingValues = SettingsDevValues;
  122. }
  123. else if (_prodMachineRadioButton.Checked)
  124. {
  125. settingValues = SettingsProdValues;
  126. }
  127. RemoteObjectCollection<PHPIniSetting> settings = new RemoteObjectCollection<PHPIniSetting>();
  128. for (int i = 0; i < settingValues.Length; i++)
  129. {
  130. settings.Add(new PHPIniSetting(SettingNames[i], settingValues[i], "PHP"));
  131. }
  132. try
  133. {
  134. Module.Proxy.AddOrUpdateSettings(settings);
  135. appliedChanges = true;
  136. // Update the values used for determining if changes have been made
  137. _errorLogFile = _errorLogFileTextBox.Text;
  138. if (_devMachineRadioButton.Checked)
  139. {
  140. _errorReportingPreset = ErrorReportingPreset.Development;
  141. }
  142. else if (_prodMachineRadioButton.Checked)
  143. {
  144. _errorReportingPreset = ErrorReportingPreset.Production;
  145. }
  146. _hasChanges = false;
  147. }
  148. catch (Exception ex)
  149. {
  150. DisplayErrorMessage(ex, Resources.ResourceManager);
  151. }
  152. finally
  153. {
  154. Update();
  155. }
  156. return appliedChanges;
  157. }
  158. protected override void CancelChanges()
  159. {
  160. if (_errorReportingPreset == ErrorReportingPreset.Development)
  161. {
  162. _devMachineRadioButton.Checked = true;
  163. }
  164. else if (_errorReportingPreset == ErrorReportingPreset.Production)
  165. {
  166. _prodMachineRadioButton.Checked = true;
  167. }
  168. else
  169. {
  170. _devMachineRadioButton.Checked = false;
  171. _prodMachineRadioButton.Checked = false;
  172. }
  173. _errorLogFileTextBox.Text = _errorLogFile;
  174. _hasChanges = false;
  175. Update();
  176. }
  177. private void GetSettings()
  178. {
  179. StartAsyncTask(Resources.AllSettingsPageGettingSettings, OnGetSettings, OnGetSettingsCompleted);
  180. }
  181. private void GoBack()
  182. {
  183. Navigate(typeof(PHPPage));
  184. }
  185. protected override void Initialize(object navigationData)
  186. {
  187. base.Initialize(navigationData);
  188. InitializeComponent();
  189. InitializeUI();
  190. }
  191. private void InitializeComponent()
  192. {
  193. this._serverTypeGroupBox = new System.Windows.Forms.GroupBox();
  194. this._prodMachineLabel = new System.Windows.Forms.Label();
  195. this._prodMachineRadioButton = new System.Windows.Forms.RadioButton();
  196. this._devMachineLabel = new System.Windows.Forms.Label();
  197. this._selectServerTypeLabel = new System.Windows.Forms.Label();
  198. this._devMachineRadioButton = new System.Windows.Forms.RadioButton();
  199. this._errorLogFileLabel = new System.Windows.Forms.Label();
  200. this._errorLogFileTextBox = new System.Windows.Forms.TextBox();
  201. this._errorLogBrowseButton = new System.Windows.Forms.Button();
  202. this._serverTypeGroupBox.SuspendLayout();
  203. this.SuspendLayout();
  204. //
  205. // _serverTypeGroupBox
  206. //
  207. this._serverTypeGroupBox.Controls.Add(this._prodMachineLabel);
  208. this._serverTypeGroupBox.Controls.Add(this._prodMachineRadioButton);
  209. this._serverTypeGroupBox.Controls.Add(this._devMachineLabel);
  210. this._serverTypeGroupBox.Controls.Add(this._selectServerTypeLabel);
  211. this._serverTypeGroupBox.Controls.Add(this._devMachineRadioButton);
  212. this._serverTypeGroupBox.Location = new System.Drawing.Point(4, 12);
  213. this._serverTypeGroupBox.Name = "_serverTypeGroupBox";
  214. this._serverTypeGroupBox.Size = new System.Drawing.Size(503, 222);
  215. this._serverTypeGroupBox.TabIndex = 0;
  216. this._serverTypeGroupBox.TabStop = false;
  217. this._serverTypeGroupBox.Text = Resources.ErrorReportingPageServerType;
  218. //
  219. // _prodMachineLabel
  220. //
  221. this._prodMachineLabel.Location = new System.Drawing.Point(37, 150);
  222. this._prodMachineLabel.Name = "_prodMachineLabel";
  223. this._prodMachineLabel.Size = new System.Drawing.Size(447, 48);
  224. this._prodMachineLabel.TabIndex = 4;
  225. this._prodMachineLabel.Text = Resources.ErrorReportingPageProdMachineDesc;
  226. //
  227. // _prodMachineRadioButton
  228. //
  229. this._prodMachineRadioButton.AutoSize = true;
  230. this._prodMachineRadioButton.Location = new System.Drawing.Point(20, 130);
  231. this._prodMachineRadioButton.Name = "_prodMachineRadioButton";
  232. this._prodMachineRadioButton.Size = new System.Drawing.Size(119, 17);
  233. this._prodMachineRadioButton.TabIndex = 3;
  234. this._prodMachineRadioButton.TabStop = true;
  235. this._prodMachineRadioButton.Text = Resources.ErrorReportingPageProdMachine;
  236. this._prodMachineRadioButton.UseVisualStyleBackColor = true;
  237. this._prodMachineRadioButton.CheckedChanged += new System.EventHandler(this.OnProdMachineRadioButtonCheckedChanged);
  238. //
  239. // _devMachineLabel
  240. //
  241. this._devMachineLabel.Location = new System.Drawing.Point(37, 75);
  242. this._devMachineLabel.Name = "_devMachineLabel";
  243. this._devMachineLabel.Size = new System.Drawing.Size(447, 46);
  244. this._devMachineLabel.TabIndex = 2;
  245. this._devMachineLabel.Text = Resources.ErrorReportingPageDevMachineDesc;
  246. //
  247. // _selectServerTypeLabel
  248. //
  249. this._selectServerTypeLabel.Location = new System.Drawing.Point(6, 20);
  250. this._selectServerTypeLabel.Name = "_selectServerTypeLabel";
  251. this._selectServerTypeLabel.Size = new System.Drawing.Size(458, 23);
  252. this._selectServerTypeLabel.TabIndex = 0;
  253. this._selectServerTypeLabel.Text = Resources.ErrorReportingPageSelectServerType;
  254. //
  255. // _devMachineRadioButton
  256. //
  257. this._devMachineRadioButton.AutoSize = true;
  258. this._devMachineRadioButton.Location = new System.Drawing.Point(20, 55);
  259. this._devMachineRadioButton.Name = "_devMachineRadioButton";
  260. this._devMachineRadioButton.Size = new System.Drawing.Size(131, 17);
  261. this._devMachineRadioButton.TabIndex = 1;
  262. this._devMachineRadioButton.TabStop = true;
  263. this._devMachineRadioButton.Text = Resources.ErrorReportingPageDevMachine;
  264. this._devMachineRadioButton.UseVisualStyleBackColor = true;
  265. this._devMachineRadioButton.CheckedChanged += new System.EventHandler(this.OnDevMachineRadioButtonCheckedChanged);
  266. //
  267. // _errorLogFileLabel
  268. //
  269. this._errorLogFileLabel.AutoSize = true;
  270. this._errorLogFileLabel.Location = new System.Drawing.Point(3, 253);
  271. this._errorLogFileLabel.Name = "_errorLogFileLabel";
  272. this._errorLogFileLabel.Size = new System.Drawing.Size(65, 13);
  273. this._errorLogFileLabel.TabIndex = 1;
  274. this._errorLogFileLabel.Text = Resources.ErrorReportingErrorLogFile;
  275. //
  276. // _errorLogFileTextBox
  277. //
  278. this._errorLogFileTextBox.Location = new System.Drawing.Point(7, 269);
  279. this._errorLogFileTextBox.Name = "_errorLogFileTextBox";
  280. this._errorLogFileTextBox.Size = new System.Drawing.Size(469, 20);
  281. this._errorLogFileTextBox.TabIndex = 2;
  282. this._errorLogFileTextBox.TextChanged += new System.EventHandler(this.OnErrorLogFileTextBoxTextChanged);
  283. //
  284. // _errorLogBrowseButton
  285. //
  286. this._errorLogBrowseButton.Location = new System.Drawing.Point(482, 267);
  287. this._errorLogBrowseButton.Name = "_errorLogBrowseButton";
  288. this._errorLogBrowseButton.Size = new System.Drawing.Size(25, 23);
  289. this._errorLogBrowseButton.TabIndex = 3;
  290. this._errorLogBrowseButton.Text = "...";
  291. this._errorLogBrowseButton.UseVisualStyleBackColor = true;
  292. this._errorLogBrowseButton.Click += new System.EventHandler(this.OnErrorLogBrowseButtonClick);
  293. //
  294. // ErrorReportingPage
  295. //
  296. this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
  297. this.AutoScroll = true;
  298. this.Controls.Add(this._errorLogBrowseButton);
  299. this.Controls.Add(this._errorLogFileTextBox);
  300. this.Controls.Add(this._errorLogFileLabel);
  301. this.Controls.Add(this._serverTypeGroupBox);
  302. this.Name = "ErrorReportingPage";
  303. this.Size = new System.Drawing.Size(510, 360);
  304. this._serverTypeGroupBox.ResumeLayout(false);
  305. this._serverTypeGroupBox.PerformLayout();
  306. this.ResumeLayout(false);
  307. this.PerformLayout();
  308. }
  309. private void InitializeUI()
  310. {
  311. if (this.IsReadOnly)
  312. {
  313. _devMachineRadioButton.Enabled = false;
  314. _devMachineLabel.Enabled = false;
  315. _prodMachineRadioButton.Enabled = false;
  316. _prodMachineLabel.Enabled = false;
  317. _errorLogFileTextBox.Enabled = false;
  318. _errorLogBrowseButton.Enabled = false;
  319. }
  320. // Only show the auto suggest if it is a local connection.
  321. // Otherwise do not show auto suggest and also hide the browse button.
  322. if (Connection.IsLocalConnection)
  323. {
  324. this._errorLogFileTextBox.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.Suggest;
  325. this._errorLogFileTextBox.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.FileSystem;
  326. }
  327. else
  328. {
  329. this._errorLogBrowseButton.Visible = false;
  330. }
  331. }
  332. protected override void OnActivated(bool initialActivation)
  333. {
  334. base.OnActivated(initialActivation);
  335. if (initialActivation)
  336. {
  337. GetSettings();
  338. }
  339. }
  340. private void OnDevMachineRadioButtonCheckedChanged(object sender, EventArgs e)
  341. {
  342. bool oldHasChanges = _hasChanges;
  343. if (_errorReportingPreset == ErrorReportingPreset.Development)
  344. {
  345. _hasChanges = !_devMachineRadioButton.Checked;
  346. }
  347. else
  348. {
  349. _hasChanges = _devMachineRadioButton.Checked;
  350. }
  351. if (oldHasChanges != _hasChanges)
  352. {
  353. Update();
  354. }
  355. }
  356. private void OnErrorLogBrowseButtonClick(object sender, EventArgs e)
  357. {
  358. using (SaveFileDialog dlg = new SaveFileDialog())
  359. {
  360. dlg.Title = Resources.ErrorLogSaveDialogTitle;
  361. dlg.InitialDirectory = Environment.ExpandEnvironmentVariables("%SystemDrive%");
  362. dlg.Filter = Resources.ErrorLogSaveDialogFilter;
  363. if (dlg.ShowDialog() == DialogResult.OK)
  364. {
  365. _errorLogFileTextBox.Text = dlg.FileName;
  366. }
  367. }
  368. }
  369. private void OnErrorLogFileTextBoxTextChanged(object sender, EventArgs e)
  370. {
  371. if (!String.Equals(_errorLogFileTextBox.Text, _errorLogFile, StringComparison.OrdinalIgnoreCase))
  372. {
  373. _hasChanges = true;
  374. Update();
  375. }
  376. }
  377. private void OnGetSettings(object sender, DoWorkEventArgs e)
  378. {
  379. e.Result = Module.Proxy.GetPHPIniSettings();
  380. }
  381. private void OnGetSettingsCompleted(object sender, RunWorkerCompletedEventArgs e)
  382. {
  383. try
  384. {
  385. object o = e.Result;
  386. PHPIniFile file = new PHPIniFile();
  387. file.SetData(o);
  388. UpdateUI(file);
  389. }
  390. catch (Exception ex)
  391. {
  392. DisplayErrorMessage(ex, Resources.ResourceManager);
  393. }
  394. }
  395. private void OnProdMachineRadioButtonCheckedChanged(object sender, EventArgs e)
  396. {
  397. bool oldHasChanges = _hasChanges;
  398. if (_errorReportingPreset == ErrorReportingPreset.Production)
  399. {
  400. _hasChanges = !_prodMachineRadioButton.Checked;
  401. }
  402. else
  403. {
  404. _hasChanges = _prodMachineRadioButton.Checked;
  405. }
  406. if (oldHasChanges != _hasChanges)
  407. {
  408. Update();
  409. }
  410. }
  411. protected override bool ShowHelp()
  412. {
  413. return ShowOnlineHelp();
  414. }
  415. protected override bool ShowOnlineHelp()
  416. {
  417. return Helper.Browse(Globals.ErrorReportingOnlineHelp);
  418. }
  419. private void UpdateUI(PHPIniFile file)
  420. {
  421. PHPIniSetting setting = file.GetSetting(SettingNames[0]);
  422. string[] settingValues = null;
  423. if (setting != null)
  424. {
  425. if (String.Equals(setting.Value, SettingsDevValues[0]))
  426. {
  427. _errorReportingPreset = ErrorReportingPreset.Development;
  428. settingValues = SettingsDevValues;
  429. }
  430. else if (String.Equals(setting.Value, SettingsProdValues[0]))
  431. {
  432. _errorReportingPreset = ErrorReportingPreset.Production;
  433. settingValues = SettingsProdValues;
  434. }
  435. int i = 1;
  436. while (_errorReportingPreset != ErrorReportingPreset.Undefined && i < SettingNames.Length)
  437. {
  438. setting = file.GetSetting(SettingNames[i]);
  439. if (setting == null || !String.Equals(setting.Value, settingValues[i]))
  440. {
  441. _errorReportingPreset = ErrorReportingPreset.Undefined;
  442. }
  443. i = i + 1;
  444. }
  445. }
  446. if (_errorReportingPreset == ErrorReportingPreset.Development)
  447. {
  448. _devMachineRadioButton.Checked = true;
  449. }
  450. else if (_errorReportingPreset == ErrorReportingPreset.Production)
  451. {
  452. _prodMachineRadioButton.Checked = true;
  453. }
  454. setting = file.GetSetting("error_log");
  455. if (setting != null)
  456. {
  457. _errorLogFile = setting.Value;
  458. _errorLogFileTextBox.Text = setting.Value;
  459. }
  460. }
  461. private class PageTaskList : TaskList
  462. {
  463. private ErrorReportingPage _page;
  464. public PageTaskList(ErrorReportingPage page)
  465. {
  466. _page = page;
  467. }
  468. public override System.Collections.ICollection GetTaskItems()
  469. {
  470. List<TaskItem> tasks = new List<TaskItem>();
  471. if (_page.IsReadOnly)
  472. {
  473. tasks.Add(new MessageTaskItem(MessageTaskItemType.Information, Resources.AllPagesPageIsReadOnly, "Information"));
  474. }
  475. tasks.Add(new MethodTaskItem("GoBack", Resources.AllPagesGoBackTask, "Tasks", null, Resources.GoBack16));
  476. return tasks;
  477. }
  478. public void GoBack()
  479. {
  480. _page.GoBack();
  481. }
  482. }
  483. }
  484. }