PageRenderTime 70ms CodeModel.GetById 18ms app.highlight 45ms RepoModel.GetById 1ms app.codeStats 1ms

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

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