PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/stable-1.0.3/Client/Settings/ErrorReportingPage.cs

#
C# | 561 lines | 531 code | 22 blank | 8 comment | 10 complexity | 5a07c35ac650d8a862f51073e0ac87de 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. string errorLogValue = '"' + _errorLogFileTextBox.Text.Trim(new char [] {' ', '"'}) + '"';
  133. settings.Add(new PHPIniSetting("error_log", errorLogValue, "PHP"));
  134. try
  135. {
  136. Module.Proxy.AddOrUpdateSettings(settings);
  137. appliedChanges = true;
  138. // Update the values used for determining if changes have been made
  139. _errorLogFile = _errorLogFileTextBox.Text;
  140. if (_devMachineRadioButton.Checked)
  141. {
  142. _errorReportingPreset = ErrorReportingPreset.Development;
  143. }
  144. else if (_prodMachineRadioButton.Checked)
  145. {
  146. _errorReportingPreset = ErrorReportingPreset.Production;
  147. }
  148. _hasChanges = false;
  149. }
  150. catch (Exception ex)
  151. {
  152. DisplayErrorMessage(ex, Resources.ResourceManager);
  153. }
  154. finally
  155. {
  156. Update();
  157. }
  158. return appliedChanges;
  159. }
  160. protected override void CancelChanges()
  161. {
  162. if (_errorReportingPreset == ErrorReportingPreset.Development)
  163. {
  164. _devMachineRadioButton.Checked = true;
  165. }
  166. else if (_errorReportingPreset == ErrorReportingPreset.Production)
  167. {
  168. _prodMachineRadioButton.Checked = true;
  169. }
  170. else
  171. {
  172. _devMachineRadioButton.Checked = false;
  173. _prodMachineRadioButton.Checked = false;
  174. }
  175. _errorLogFileTextBox.Text = _errorLogFile;
  176. _hasChanges = false;
  177. Update();
  178. }
  179. private void GetSettings()
  180. {
  181. StartAsyncTask(Resources.AllSettingsPageGettingSettings, OnGetSettings, OnGetSettingsCompleted);
  182. }
  183. private void GoBack()
  184. {
  185. Navigate(typeof(PHPPage));
  186. }
  187. protected override void Initialize(object navigationData)
  188. {
  189. base.Initialize(navigationData);
  190. InitializeComponent();
  191. InitializeUI();
  192. }
  193. private void InitializeComponent()
  194. {
  195. this._serverTypeGroupBox = new System.Windows.Forms.GroupBox();
  196. this._prodMachineLabel = new System.Windows.Forms.Label();
  197. this._prodMachineRadioButton = new System.Windows.Forms.RadioButton();
  198. this._devMachineLabel = new System.Windows.Forms.Label();
  199. this._selectServerTypeLabel = new System.Windows.Forms.Label();
  200. this._devMachineRadioButton = new System.Windows.Forms.RadioButton();
  201. this._errorLogFileLabel = new System.Windows.Forms.Label();
  202. this._errorLogFileTextBox = new System.Windows.Forms.TextBox();
  203. this._errorLogBrowseButton = new System.Windows.Forms.Button();
  204. this._serverTypeGroupBox.SuspendLayout();
  205. this.SuspendLayout();
  206. //
  207. // _serverTypeGroupBox
  208. //
  209. this._serverTypeGroupBox.Controls.Add(this._prodMachineLabel);
  210. this._serverTypeGroupBox.Controls.Add(this._prodMachineRadioButton);
  211. this._serverTypeGroupBox.Controls.Add(this._devMachineLabel);
  212. this._serverTypeGroupBox.Controls.Add(this._selectServerTypeLabel);
  213. this._serverTypeGroupBox.Controls.Add(this._devMachineRadioButton);
  214. this._serverTypeGroupBox.Location = new System.Drawing.Point(4, 12);
  215. this._serverTypeGroupBox.Name = "_serverTypeGroupBox";
  216. this._serverTypeGroupBox.Size = new System.Drawing.Size(472, 222);
  217. this._serverTypeGroupBox.TabIndex = 0;
  218. this._serverTypeGroupBox.TabStop = false;
  219. this._serverTypeGroupBox.Text = Resources.ErrorReportingPageServerType;
  220. //
  221. // _prodMachineLabel
  222. //
  223. this._prodMachineLabel.Location = new System.Drawing.Point(37, 150);
  224. this._prodMachineLabel.Name = "_prodMachineLabel";
  225. this._prodMachineLabel.Size = new System.Drawing.Size(413, 48);
  226. this._prodMachineLabel.TabIndex = 4;
  227. this._prodMachineLabel.Text = Resources.ErrorReportingPageProdMachineDesc;
  228. //
  229. // _prodMachineRadioButton
  230. //
  231. this._prodMachineRadioButton.AutoSize = true;
  232. this._prodMachineRadioButton.Location = new System.Drawing.Point(20, 130);
  233. this._prodMachineRadioButton.Name = "_prodMachineRadioButton";
  234. this._prodMachineRadioButton.Size = new System.Drawing.Size(119, 17);
  235. this._prodMachineRadioButton.TabIndex = 3;
  236. this._prodMachineRadioButton.TabStop = true;
  237. this._prodMachineRadioButton.Text = Resources.ErrorReportingPageProdMachine;
  238. this._prodMachineRadioButton.UseVisualStyleBackColor = true;
  239. this._prodMachineRadioButton.CheckedChanged += new System.EventHandler(this.OnProdMachineRadioButtonCheckedChanged);
  240. //
  241. // _devMachineLabel
  242. //
  243. this._devMachineLabel.Location = new System.Drawing.Point(37, 75);
  244. this._devMachineLabel.Name = "_devMachineLabel";
  245. this._devMachineLabel.Size = new System.Drawing.Size(413, 46);
  246. this._devMachineLabel.TabIndex = 2;
  247. this._devMachineLabel.Text = Resources.ErrorReportingPageDevMachineDesc;
  248. //
  249. // _selectServerTypeLabel
  250. //
  251. this._selectServerTypeLabel.Location = new System.Drawing.Point(6, 20);
  252. this._selectServerTypeLabel.Name = "_selectServerTypeLabel";
  253. this._selectServerTypeLabel.Size = new System.Drawing.Size(458, 23);
  254. this._selectServerTypeLabel.TabIndex = 0;
  255. this._selectServerTypeLabel.Text = Resources.ErrorReportingPageSelectServerType;
  256. //
  257. // _devMachineRadioButton
  258. //
  259. this._devMachineRadioButton.AutoSize = true;
  260. this._devMachineRadioButton.Location = new System.Drawing.Point(20, 55);
  261. this._devMachineRadioButton.Name = "_devMachineRadioButton";
  262. this._devMachineRadioButton.Size = new System.Drawing.Size(131, 17);
  263. this._devMachineRadioButton.TabIndex = 1;
  264. this._devMachineRadioButton.TabStop = true;
  265. this._devMachineRadioButton.Text = Resources.ErrorReportingPageDevMachine;
  266. this._devMachineRadioButton.UseVisualStyleBackColor = true;
  267. this._devMachineRadioButton.CheckedChanged += new System.EventHandler(this.OnDevMachineRadioButtonCheckedChanged);
  268. //
  269. // _errorLogFileLabel
  270. //
  271. this._errorLogFileLabel.AutoSize = true;
  272. this._errorLogFileLabel.Location = new System.Drawing.Point(3, 253);
  273. this._errorLogFileLabel.Name = "_errorLogFileLabel";
  274. this._errorLogFileLabel.Size = new System.Drawing.Size(65, 13);
  275. this._errorLogFileLabel.TabIndex = 1;
  276. this._errorLogFileLabel.Text = Resources.ErrorReportingErrorLogFile;
  277. //
  278. // _errorLogFileTextBox
  279. //
  280. this._errorLogFileTextBox.Location = new System.Drawing.Point(7, 269);
  281. this._errorLogFileTextBox.Name = "_errorLogFileTextBox";
  282. this._errorLogFileTextBox.Size = new System.Drawing.Size(438, 20);
  283. this._errorLogFileTextBox.TabIndex = 2;
  284. this._errorLogFileTextBox.TextChanged += new System.EventHandler(this.OnErrorLogFileTextBoxTextChanged);
  285. //
  286. // _errorLogBrowseButton
  287. //
  288. this._errorLogBrowseButton.Location = new System.Drawing.Point(451, 267);
  289. this._errorLogBrowseButton.Name = "_errorLogBrowseButton";
  290. this._errorLogBrowseButton.Size = new System.Drawing.Size(25, 23);
  291. this._errorLogBrowseButton.TabIndex = 3;
  292. this._errorLogBrowseButton.Text = "...";
  293. this._errorLogBrowseButton.UseVisualStyleBackColor = true;
  294. this._errorLogBrowseButton.Click += new System.EventHandler(this.OnErrorLogBrowseButtonClick);
  295. //
  296. // ErrorReportingPage
  297. //
  298. this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
  299. this.AutoScroll = true;
  300. this.Controls.Add(this._errorLogBrowseButton);
  301. this.Controls.Add(this._errorLogFileTextBox);
  302. this.Controls.Add(this._errorLogFileLabel);
  303. this.Controls.Add(this._serverTypeGroupBox);
  304. this.Name = "ErrorReportingPage";
  305. this.Size = new System.Drawing.Size(480, 360);
  306. this._serverTypeGroupBox.ResumeLayout(false);
  307. this._serverTypeGroupBox.PerformLayout();
  308. this.ResumeLayout(false);
  309. this.PerformLayout();
  310. }
  311. private void InitializeUI()
  312. {
  313. if (this.IsReadOnly)
  314. {
  315. _devMachineRadioButton.Enabled = false;
  316. _devMachineLabel.Enabled = false;
  317. _prodMachineRadioButton.Enabled = false;
  318. _prodMachineLabel.Enabled = false;
  319. _errorLogFileTextBox.Enabled = false;
  320. _errorLogBrowseButton.Enabled = false;
  321. }
  322. // Only show the auto suggest if it is a local connection.
  323. // Otherwise do not show auto suggest and also hide the browse button.
  324. if (Connection.IsLocalConnection)
  325. {
  326. this._errorLogFileTextBox.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.Suggest;
  327. this._errorLogFileTextBox.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.FileSystem;
  328. }
  329. else
  330. {
  331. this._errorLogBrowseButton.Visible = false;
  332. }
  333. }
  334. protected override void OnActivated(bool initialActivation)
  335. {
  336. base.OnActivated(initialActivation);
  337. if (initialActivation)
  338. {
  339. GetSettings();
  340. }
  341. }
  342. private void OnDevMachineRadioButtonCheckedChanged(object sender, EventArgs e)
  343. {
  344. bool oldHasChanges = _hasChanges;
  345. if (_errorReportingPreset == ErrorReportingPreset.Development)
  346. {
  347. _hasChanges = !_devMachineRadioButton.Checked;
  348. }
  349. else
  350. {
  351. _hasChanges = _devMachineRadioButton.Checked;
  352. }
  353. _hasChanges = _hasChanges || oldHasChanges;
  354. if (_hasChanges)
  355. {
  356. Update();
  357. }
  358. }
  359. private void OnErrorLogBrowseButtonClick(object sender, EventArgs e)
  360. {
  361. using (SaveFileDialog dlg = new SaveFileDialog())
  362. {
  363. dlg.Title = Resources.ErrorLogSaveDialogTitle;
  364. dlg.Filter = Resources.ErrorLogSaveDialogFilter;
  365. if (!String.IsNullOrEmpty(_errorLogFileTextBox.Text))
  366. {
  367. dlg.InitialDirectory = System.IO.Path.GetDirectoryName(_errorLogFileTextBox.Text.Trim());
  368. }
  369. else
  370. {
  371. dlg.InitialDirectory = Environment.ExpandEnvironmentVariables("%SystemDrive%");
  372. }
  373. if (dlg.ShowDialog() == DialogResult.OK)
  374. {
  375. _errorLogFileTextBox.Text = dlg.FileName;
  376. }
  377. }
  378. }
  379. private void OnErrorLogFileTextBoxTextChanged(object sender, EventArgs e)
  380. {
  381. if (!String.Equals(_errorLogFileTextBox.Text, _errorLogFile, StringComparison.OrdinalIgnoreCase))
  382. {
  383. _hasChanges = true;
  384. Update();
  385. }
  386. }
  387. private void OnGetSettings(object sender, DoWorkEventArgs e)
  388. {
  389. e.Result = Module.Proxy.GetPHPIniSettings();
  390. }
  391. private void OnGetSettingsCompleted(object sender, RunWorkerCompletedEventArgs e)
  392. {
  393. try
  394. {
  395. object o = e.Result;
  396. PHPIniFile file = new PHPIniFile();
  397. file.SetData(o);
  398. UpdateUI(file);
  399. }
  400. catch (Exception ex)
  401. {
  402. DisplayErrorMessage(ex, Resources.ResourceManager);
  403. }
  404. }
  405. private void OnProdMachineRadioButtonCheckedChanged(object sender, EventArgs e)
  406. {
  407. bool oldHasChanges = _hasChanges;
  408. if (_errorReportingPreset == ErrorReportingPreset.Production)
  409. {
  410. _hasChanges = !_prodMachineRadioButton.Checked;
  411. }
  412. else
  413. {
  414. _hasChanges = _prodMachineRadioButton.Checked;
  415. }
  416. _hasChanges = _hasChanges || oldHasChanges;
  417. if (_hasChanges)
  418. {
  419. Update();
  420. }
  421. }
  422. protected override bool ShowHelp()
  423. {
  424. return ShowOnlineHelp();
  425. }
  426. protected override bool ShowOnlineHelp()
  427. {
  428. return Helper.Browse(Globals.ErrorReportingOnlineHelp);
  429. }
  430. private void UpdateUI(PHPIniFile file)
  431. {
  432. PHPIniSetting setting = file.GetSetting(SettingNames[0]);
  433. string[] settingValues = null;
  434. if (setting != null)
  435. {
  436. if (String.Equals(setting.TrimmedValue, SettingsDevValues[0]))
  437. {
  438. _errorReportingPreset = ErrorReportingPreset.Development;
  439. settingValues = SettingsDevValues;
  440. }
  441. else if (String.Equals(setting.TrimmedValue, SettingsProdValues[0]))
  442. {
  443. _errorReportingPreset = ErrorReportingPreset.Production;
  444. settingValues = SettingsProdValues;
  445. }
  446. int i = 1;
  447. while (_errorReportingPreset != ErrorReportingPreset.Undefined && i < SettingNames.Length)
  448. {
  449. setting = file.GetSetting(SettingNames[i]);
  450. if (setting == null || !String.Equals(setting.TrimmedValue, settingValues[i]))
  451. {
  452. _errorReportingPreset = ErrorReportingPreset.Undefined;
  453. }
  454. i = i + 1;
  455. }
  456. }
  457. if (_errorReportingPreset == ErrorReportingPreset.Development)
  458. {
  459. _devMachineRadioButton.Checked = true;
  460. }
  461. else if (_errorReportingPreset == ErrorReportingPreset.Production)
  462. {
  463. _prodMachineRadioButton.Checked = true;
  464. }
  465. setting = file.GetSetting("error_log");
  466. if (setting != null)
  467. {
  468. _errorLogFile = setting.TrimmedValue;
  469. _errorLogFileTextBox.Text = setting.TrimmedValue;
  470. }
  471. }
  472. private class PageTaskList : TaskList
  473. {
  474. private ErrorReportingPage _page;
  475. public PageTaskList(ErrorReportingPage page)
  476. {
  477. _page = page;
  478. }
  479. public override System.Collections.ICollection GetTaskItems()
  480. {
  481. List<TaskItem> tasks = new List<TaskItem>();
  482. if (_page.IsReadOnly)
  483. {
  484. tasks.Add(new MessageTaskItem(MessageTaskItemType.Information, Resources.AllPagesPageIsReadOnly, "Information"));
  485. }
  486. tasks.Add(new MethodTaskItem("GoBack", Resources.AllPagesGoBackTask, "Tasks", null, Resources.GoBack16));
  487. return tasks;
  488. }
  489. public void GoBack()
  490. {
  491. _page.GoBack();
  492. }
  493. }
  494. }
  495. }