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