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