1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 // Copyright (c) 2004-2006 Novell, Inc. (http://www.novell.com)
24 // Alexander Olk xenomorph2@onlinehome.de
27 // NOT COMPLETE - work in progress
31 using System.ComponentModel;
32 using System.Resources;
34 using System.Collections;
35 using System.Collections.Specialized;
37 using System.Threading;
39 namespace System.Windows.Forms {
41 [DefaultProperty ("FileName")]
42 [DefaultEvent ("FileOk")]
43 public abstract class FileDialog : CommonDialog
45 protected static readonly object EventFileOk = new object ();
47 internal enum FileDialogType
53 private bool addExtension = true;
54 private bool checkFileExists = false;
55 private bool checkPathExists = true;
56 private string defaultExt = "";
57 private bool dereferenceLinks = true;
58 private string fileName = "";
59 private string[] fileNames;
60 private string filter;
61 private int filterIndex = 1;
62 private string initialDirectory = "";
63 private bool restoreDirectory = false;
64 private bool showHelp = false;
65 private string title = "";
66 private bool validateNames = true;
68 private Button cancelButton;
69 private ToolBarButton upToolBarButton;
70 private PopupButtonPanel popupButtonPanel;
71 private Button openSaveButton;
72 private Button helpButton;
73 private Label fileTypeLabel;
74 private ToolBarButton menueToolBarButton;
75 private ContextMenu menueToolBarButtonContextMenu;
76 private ToolBar smallButtonToolBar;
77 private DirComboBox dirComboBox;
78 private ComboBox fileNameComboBox;
79 private Label fileNameLabel;
80 private MWFFileView mwfFileView;
81 private Label searchSaveLabel;
82 private ToolBarButton newdirToolBarButton;
83 private ToolBarButton backToolBarButton;
84 private ComboBox fileTypeComboBox;
85 private ImageList imageListTopToolbar;
86 private CheckBox readonlyCheckBox;
88 private bool multiSelect = false;
90 private string restoreDirectoryString = "";
92 internal FileDialogType fileDialogType;
94 private bool do_not_call_OnSelectedIndexChangedFileTypeComboBox = false;
96 private bool showReadOnly = false;
97 private bool readOnlyChecked = false;
98 internal bool createPrompt = false;
99 internal bool overwritePrompt = true;
101 internal FileFilter fileFilter;
103 private string lastFolder = "";
107 private readonly string filedialog_string = "FileDialog";
108 private readonly string lastfolder_string = "LastFolder";
109 private readonly string width_string = "Width";
110 private readonly string height_string = "Height";
111 private readonly string filenames_string = "FileNames";
112 private readonly string x_string = "X";
113 private readonly string y_string = "Y";
115 internal FileDialog ()
119 Size formConfigSize = Size.Empty;
120 Point formConfigLocation = Point.Empty;
121 string[] configFileNames = null;
123 object formWidth = MWFConfig.GetValue (filedialog_string, width_string);
125 object formHeight = MWFConfig.GetValue (filedialog_string, height_string);
127 if (formHeight != null && formWidth != null)
128 formConfigSize = new Size ((int)formWidth, (int)formHeight);
130 object formLocationX = MWFConfig.GetValue (filedialog_string, x_string);
131 object formLocationY = MWFConfig.GetValue (filedialog_string, y_string);
133 if (formLocationX != null && formLocationY != null)
134 formConfigLocation = new Point ((int)formLocationX, (int)formLocationY);
136 configFileNames = (string[])MWFConfig.GetValue (filedialog_string, filenames_string);
138 fileTypeComboBox = new ComboBox ();
139 backToolBarButton = new ToolBarButton ();
140 newdirToolBarButton = new ToolBarButton ();
141 searchSaveLabel = new Label ();
142 mwfFileView = new MWFFileView (vfs);
143 fileNameLabel = new Label ();
144 fileNameComboBox = new ComboBox ();
145 dirComboBox = new DirComboBox (vfs);
146 smallButtonToolBar = new ToolBar ();
147 menueToolBarButton = new ToolBarButton ();
148 fileTypeLabel = new Label ();
149 openSaveButton = new Button ();
150 form.AcceptButton = openSaveButton;
151 helpButton = new Button ();
152 popupButtonPanel = new PopupButtonPanel ();
153 upToolBarButton = new ToolBarButton ();
154 cancelButton = new Button ();
155 form.CancelButton = cancelButton;
156 imageListTopToolbar = new ImageList ();
157 menueToolBarButtonContextMenu = new ContextMenu ();
158 readonlyCheckBox = new CheckBox ();
160 form.SuspendLayout ();
162 //imageListTopToolbar
163 imageListTopToolbar.ColorDepth = ColorDepth.Depth32Bit;
164 imageListTopToolbar.ImageSize = new Size (16, 16); // 16, 16
165 imageListTopToolbar.Images.Add (ResourceImageLoader.Get ("go-previous.png"));
166 imageListTopToolbar.Images.Add (ResourceImageLoader.Get ("go-top.png"));
167 imageListTopToolbar.Images.Add (ResourceImageLoader.Get ("folder-new.png"));
168 imageListTopToolbar.Images.Add (ResourceImageLoader.Get ("preferences-system-windows.png"));
169 imageListTopToolbar.TransparentColor = Color.Transparent;
172 searchSaveLabel.FlatStyle = FlatStyle.System;
173 searchSaveLabel.Location = new Point (7, 8);
174 searchSaveLabel.Size = new Size (72, 21);
175 searchSaveLabel.TextAlign = ContentAlignment.MiddleRight;
178 dirComboBox.Anchor = ((AnchorStyles)(((AnchorStyles.Top | AnchorStyles.Left) | AnchorStyles.Right)));
179 dirComboBox.DropDownStyle = ComboBoxStyle.DropDownList;
180 dirComboBox.Location = new Point (99, 8);
181 dirComboBox.Size = new Size (260, 21);
182 dirComboBox.TabIndex = 7;
184 // smallButtonToolBar
185 smallButtonToolBar.Anchor = ((AnchorStyles)((AnchorStyles.Top | AnchorStyles.Right)));
186 smallButtonToolBar.Appearance = ToolBarAppearance.Flat;
187 smallButtonToolBar.AutoSize = false;
188 smallButtonToolBar.Buttons.AddRange (new ToolBarButton [] {
192 menueToolBarButton});
193 smallButtonToolBar.ButtonSize = new Size (24, 24); // 21, 16
194 smallButtonToolBar.Divider = false;
195 smallButtonToolBar.Dock = DockStyle.None;
196 smallButtonToolBar.DropDownArrows = true;
197 smallButtonToolBar.ImageList = imageListTopToolbar;
198 smallButtonToolBar.Location = new Point (372, 6);
199 smallButtonToolBar.ShowToolTips = true;
200 smallButtonToolBar.Size = new Size (140, 28);
201 smallButtonToolBar.TabIndex = 8;
202 smallButtonToolBar.TextAlign = ToolBarTextAlign.Right;
205 popupButtonPanel.Dock = DockStyle.None;
206 popupButtonPanel.Location = new Point (7, 37);
207 popupButtonPanel.TabIndex = 9;
210 mwfFileView.Anchor = ((AnchorStyles)((((AnchorStyles.Top | AnchorStyles.Bottom) | AnchorStyles.Left) | AnchorStyles.Right)));
211 mwfFileView.Location = new Point (99, 37);
212 mwfFileView.Size = new Size (449, 282);
213 mwfFileView.Columns.Add (" Name", 170, HorizontalAlignment.Left);
214 mwfFileView.Columns.Add ("Size ", 80, HorizontalAlignment.Right);
215 mwfFileView.Columns.Add (" Type", 100, HorizontalAlignment.Left);
216 mwfFileView.Columns.Add (" Last Access", 150, HorizontalAlignment.Left);
217 mwfFileView.AllowColumnReorder = true;
218 mwfFileView.MultiSelect = false;
219 mwfFileView.TabIndex = 10;
220 mwfFileView.RegisterSender (dirComboBox);
221 mwfFileView.RegisterSender (popupButtonPanel);
224 fileNameLabel.Anchor = ((AnchorStyles)((AnchorStyles.Bottom | AnchorStyles.Left)));
225 fileNameLabel.FlatStyle = FlatStyle.System;
226 fileNameLabel.Location = new Point (102, 330);
227 fileNameLabel.Size = new Size (70, 21);
228 fileNameLabel.Text = "Filename:";
229 fileNameLabel.TextAlign = ContentAlignment.MiddleLeft;
232 fileNameComboBox.Anchor = ((AnchorStyles)(((AnchorStyles.Bottom | AnchorStyles.Left) | AnchorStyles.Right)));
233 fileNameComboBox.Location = new Point (195, 330);
234 fileNameComboBox.Size = new Size (245, 21);
235 fileNameComboBox.TabIndex = 1;
236 fileNameComboBox.MaxDropDownItems = 10;
237 fileNameComboBox.Items.Add (" ");
239 if (configFileNames != null) {
240 fileNameComboBox.Items.Clear ();
242 foreach (string configFileName in configFileNames) {
243 if (configFileName != null)
244 if (configFileName.Trim ().Length > 0)
245 fileNameComboBox.Items.Add (configFileName);
251 fileTypeLabel.Anchor = ((AnchorStyles)((AnchorStyles.Bottom | AnchorStyles.Left)));
252 fileTypeLabel.FlatStyle = FlatStyle.System;
253 fileTypeLabel.Location = new Point (102, 356);
254 fileTypeLabel.Size = new Size (70, 21);
255 fileTypeLabel.Text = "Filetype:";
256 fileTypeLabel.TextAlign = ContentAlignment.MiddleLeft;
259 fileTypeComboBox.Anchor = ((AnchorStyles)(((AnchorStyles.Bottom | AnchorStyles.Left) | AnchorStyles.Right)));
260 fileTypeComboBox.DropDownStyle = ComboBoxStyle.DropDownList;
261 fileTypeComboBox.Location = new Point (195, 356);
262 fileTypeComboBox.Size = new Size (245, 21);
263 fileTypeComboBox.TabIndex = 2;
266 backToolBarButton.ImageIndex = 0;
267 backToolBarButton.Enabled = false;
268 backToolBarButton.Style = ToolBarButtonStyle.PushButton;
269 mwfFileView.AddControlToEnableDisableByDirStack (backToolBarButton);
272 upToolBarButton.ImageIndex = 1;
273 upToolBarButton.Style = ToolBarButtonStyle.PushButton;
274 mwfFileView.SetFolderUpToolBarButton (upToolBarButton);
276 // newdirToolBarButton
277 newdirToolBarButton.ImageIndex = 2;
278 newdirToolBarButton.Style = ToolBarButtonStyle.PushButton;
280 // menueToolBarButton
281 menueToolBarButton.ImageIndex = 3;
282 menueToolBarButton.DropDownMenu = menueToolBarButtonContextMenu;
283 menueToolBarButton.Style = ToolBarButtonStyle.DropDownButton;
285 // menueToolBarButtonContextMenu
286 menueToolBarButtonContextMenu.MenuItems.AddRange (mwfFileView.ViewMenuItems);
289 openSaveButton.Anchor = ((AnchorStyles)((AnchorStyles.Bottom | AnchorStyles.Right)));
290 openSaveButton.FlatStyle = FlatStyle.System;
291 openSaveButton.Location = new Point (475, 330);
292 openSaveButton.Size = new Size (72, 21);
293 openSaveButton.TabIndex = 4;
294 openSaveButton.FlatStyle = FlatStyle.System;
297 cancelButton.Anchor = ((AnchorStyles)((AnchorStyles.Bottom | AnchorStyles.Right)));
298 cancelButton.FlatStyle = FlatStyle.System;
299 cancelButton.Location = new Point (475, 356);
300 cancelButton.Size = new Size (72, 21);
301 cancelButton.TabIndex = 5;
302 cancelButton.Text = "Cancel";
303 cancelButton.FlatStyle = FlatStyle.System;
306 helpButton.Anchor = ((AnchorStyles)((AnchorStyles.Bottom | AnchorStyles.Right)));
307 helpButton.FlatStyle = FlatStyle.System;
308 helpButton.Location = new Point (475, 350);
309 helpButton.Size = new Size (72, 21);
310 helpButton.TabIndex = 6;
311 helpButton.Text = "Help";
312 helpButton.FlatStyle = FlatStyle.System;
315 readonlyCheckBox.Anchor = ((AnchorStyles)(((AnchorStyles.Bottom | AnchorStyles.Left) | AnchorStyles.Right)));
316 readonlyCheckBox.Text = "Open Readonly";
317 readonlyCheckBox.Location = new Point (195, 350);
318 readonlyCheckBox.Size = new Size (245, 21);
319 readonlyCheckBox.TabIndex = 3;
320 readonlyCheckBox.FlatStyle = FlatStyle.System;
322 form.SizeGripStyle = SizeGripStyle.Show;
324 form.MaximizeBox = true;
325 form.MinimizeBox = true;
326 form.FormBorderStyle = FormBorderStyle.Sizable;
327 form.MinimumSize = new Size (554, 405);
329 form.ClientSize = new Size (554, 405); // 384
331 form.Controls.Add (smallButtonToolBar);
332 form.Controls.Add (cancelButton);
333 form.Controls.Add (openSaveButton);
334 form.Controls.Add (mwfFileView);
335 form.Controls.Add (fileTypeLabel);
336 form.Controls.Add (fileNameLabel);
337 form.Controls.Add (fileTypeComboBox);
338 form.Controls.Add (fileNameComboBox);
339 form.Controls.Add (dirComboBox);
340 form.Controls.Add (searchSaveLabel);
341 form.Controls.Add (popupButtonPanel);
343 form.ResumeLayout (false);
345 if (formConfigSize != Size.Empty) {
346 form.Size = formConfigSize;
349 if (formConfigLocation != Point.Empty) {
350 form.Location = formConfigLocation;
353 openSaveButton.Click += new EventHandler (OnClickOpenSaveButton);
354 cancelButton.Click += new EventHandler (OnClickCancelButton);
355 helpButton.Click += new EventHandler (OnClickHelpButton);
357 smallButtonToolBar.ButtonClick += new ToolBarButtonClickEventHandler (OnClickSmallButtonToolBar);
359 fileTypeComboBox.SelectedIndexChanged += new EventHandler (OnSelectedIndexChangedFileTypeComboBox);
361 mwfFileView.SelectedFileChanged += new EventHandler (OnSelectedFileChangedFileView);
362 mwfFileView.ForceDialogEnd += new EventHandler (OnForceDialogEndFileView);
363 mwfFileView.SelectedFilesChanged += new EventHandler (OnSelectedFilesChangedFileView);
365 dirComboBox.DirectoryChanged += new EventHandler (OnDirectoryChangedDirComboBox);
366 popupButtonPanel.DirectoryChanged += new EventHandler (OnDirectoryChangedPopupButtonPanel);
368 readonlyCheckBox.CheckedChanged += new EventHandler (OnCheckCheckChanged);
372 public bool AddExtension {
378 addExtension = value;
382 [DefaultValue(false)]
383 public virtual bool CheckFileExists {
385 return checkFileExists;
389 checkFileExists = value;
394 public bool CheckPathExists {
396 return checkPathExists;
400 checkPathExists = value;
405 public string DefaultExt {
413 // if there is a dot remove it and everything before it
414 if (defaultExt.LastIndexOf ('.') != - 1) {
415 string[] split = defaultExt.Split (new char [] { '.' });
416 defaultExt = split [split.Length - 1];
421 // in MS.NET it doesn't make a difference if
422 // DerefenceLinks is true or false
423 // if the selected file is a link FileDialog
424 // always returns the link
426 public bool DereferenceLinks {
428 return dereferenceLinks;
432 dereferenceLinks = value;
437 public string FileName {
444 if (SetFileName (value))
451 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
452 public string[] FileNames {
454 if (fileNames == null || fileNames.Length == 0) {
455 string[] null_nada_nothing_filenames = new string [0];
456 return null_nada_nothing_filenames;
459 string[] new_filenames = new string [fileNames.Length];
460 fileNames.CopyTo (new_filenames, 0);
461 return new_filenames;
467 public string Filter {
474 throw new NullReferenceException ("Filter");
478 fileFilter = new FileFilter (filter);
485 public int FilterIndex {
491 if (fileFilter != null && fileFilter.FilterArrayList.Count > value)
492 return; // FIXME: throw an exception ?
501 public string InitialDirectory {
503 return initialDirectory;
507 if (Directory.Exists (value)) {
508 initialDirectory = value;
510 mwfFileView.ChangeDirectory (null, initialDirectory);
515 [DefaultValue(false)]
516 public bool RestoreDirectory {
518 return restoreDirectory;
522 restoreDirectory = value;
526 [DefaultValue(false)]
527 public bool ShowHelp {
534 ResizeAndRelocateForHelpOrReadOnly ();
540 public string Title {
552 // this one is a hard one ;)
554 // - up to MAX_PATH characters (windef.h) = 260
555 // - no trailing dots or spaces
558 // NTFS/Posix filename:
559 // - up to 32,768 Unicode characters
560 // - trailing periods or spaces
564 public bool ValidateNames {
566 return validateNames;
570 validateNames = value;
574 public override void Reset ()
577 checkFileExists = false;
578 checkPathExists = true;
580 dereferenceLinks = true;
585 initialDirectory = "";
586 restoreDirectory = false;
589 validateNames = true;
594 public override string ToString ()
596 return base.ToString ();
599 public event CancelEventHandler FileOk {
600 add { Events.AddHandler (EventFileOk, value); }
601 remove { Events.RemoveHandler (EventFileOk, value); }
604 protected virtual IntPtr Instance {
612 // This is just for internal use with MSs version, so it doesn't need to be implemented
613 // as it can't really be accessed anyways
614 protected int Options {
619 protected override IntPtr HookProc (IntPtr hWnd, int msg, IntPtr wparam, IntPtr lparam)
621 throw new NotImplementedException ();
624 protected void OnFileOk (CancelEventArgs e)
626 WriteConfigValues (e);
628 CancelEventHandler fo = (CancelEventHandler) Events [EventFileOk];
633 protected override bool RunDialog (IntPtr hWndOwner)
639 mwfFileView.ChangeDirectory (null, lastFolder);
641 fileNameComboBox.Select ();
646 internal virtual bool ShowReadOnly {
648 showReadOnly = value;
649 ResizeAndRelocateForHelpOrReadOnly ();
657 internal virtual bool ReadOnlyChecked {
659 readOnlyChecked = value;
660 readonlyCheckBox.Checked = value;
664 return readOnlyChecked;
668 internal bool BMultiSelect {
671 mwfFileView.MultiSelect = value;
679 internal string OpenSaveButtonText {
681 openSaveButton.Text = value;
685 internal string SearchSaveLabel {
687 searchSaveLabel.Text = value;
691 private void SelectFilter ()
693 if (FilterIndex > mwfFileView.FilterArrayList.Count)
696 do_not_call_OnSelectedIndexChangedFileTypeComboBox = true;
697 fileTypeComboBox.BeginUpdate ();
698 fileTypeComboBox.SelectedIndex = FilterIndex - 1;
699 fileTypeComboBox.EndUpdate ();
701 mwfFileView.FilterIndex = FilterIndex;
704 private bool SetFileName (string fname)
706 bool rooted = Path.IsPathRooted (fname);
709 string dir = mwfFileView.CurrentRealFolder ;
711 dir = Environment.CurrentDirectory;
713 if (File.Exists (Path.Combine (dir, fname))) {
714 fileNameComboBox.Text = fname;
715 mwfFileView.SetSelectedIndexTo (fname);
720 if (File.Exists (fname)) {
721 fileNameComboBox.Text = Path.GetFileName (fname);
722 mwfFileView.ChangeDirectory (null, Path.GetDirectoryName (fname));
723 mwfFileView.SetSelectedIndexTo (fname);
732 void OnClickOpenSaveButton (object sender, EventArgs e)
734 if (fileDialogType == FileDialogType.OpenFileDialog) {
735 ListView.SelectedListViewItemCollection sl = mwfFileView.SelectedItems;
736 if (sl.Count > 0 && sl [0] != null) {
738 FileViewListViewItem item = sl [0] as FileViewListViewItem;
739 FSEntry fsEntry = item.FSEntry;
741 if (fsEntry.Attributes == FileAttributes.Directory) {
742 mwfFileView.ChangeDirectory (null, fsEntry.FullName);
746 foreach (FileViewListViewItem item in sl) {
747 FSEntry fsEntry = item.FSEntry;
748 if (fsEntry.Attributes == FileAttributes.Directory) {
749 mwfFileView.ChangeDirectory (null, fsEntry.FullName);
757 string internalfullfilename = "";
760 string fileFromComboBox = fileNameComboBox.Text.Trim ();
762 if (fileFromComboBox.Length > 0) {
763 if (!Path.IsPathRooted (fileFromComboBox)) {
764 // on unix currentRealFolder for "Recently used files" is null,
765 // because recently used files don't get saved as links in a directory
766 // recently used files get saved in a xml file
767 if (mwfFileView.CurrentRealFolder != null)
768 fileFromComboBox = Path.Combine (mwfFileView.CurrentRealFolder, fileFromComboBox);
770 if (mwfFileView.CurrentFSEntry != null) {
771 fileFromComboBox = mwfFileView.CurrentFSEntry.FullName;
775 FileInfo fileInfo = new FileInfo (fileFromComboBox);
777 if (fileInfo.Exists || fileDialogType == FileDialogType.SaveFileDialog) {
778 internalfullfilename = fileFromComboBox;
780 DirectoryInfo dirInfo = new DirectoryInfo (fileFromComboBox);
781 if (dirInfo.Exists) {
782 mwfFileView.ChangeDirectory (null, dirInfo.FullName);
784 fileNameComboBox.Text = " ";
787 internalfullfilename = fileFromComboBox;
793 if (fileDialogType == FileDialogType.OpenFileDialog) {
794 if (checkFileExists) {
795 if (!File.Exists (internalfullfilename)) {
796 string message = "\"" + internalfullfilename + "\" doesn't exist. Please verify that you have entered the correct file name.";
797 MessageBox.Show (message, openSaveButton.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
803 if (overwritePrompt) {
804 if (File.Exists (internalfullfilename)) {
805 string message = "\"" + internalfullfilename + "\" exists. Overwrite ?";
806 DialogResult dr = MessageBox.Show (message, openSaveButton.Text, MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
808 if (dr == DialogResult.Cancel)
814 if (!File.Exists (internalfullfilename)) {
815 string message = "\"" + internalfullfilename + "\" doesn't exist. Create ?";
816 DialogResult dr = MessageBox.Show (message, openSaveButton.Text, MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
818 if (dr == DialogResult.Cancel)
824 if (fileDialogType == FileDialogType.SaveFileDialog) {
826 string extension_to_use = "";
827 string filter_exentsion = "";
829 if (fileFilter != null) {
830 FilterStruct filterstruct = (FilterStruct)fileFilter.FilterArrayList [filterIndex - 1];
832 for (int i = 0; i < filterstruct.filters.Count; i++) {
833 string extension = filterstruct.filters [i];
835 if (extension.StartsWith ("*"))
836 extension = extension.Remove (0, 1);
838 if (extension.IndexOf ('*') != -1)
841 filter_exentsion = extension;
846 if (filter_exentsion != "")
847 extension_to_use = filter_exentsion;
849 if (defaultExt != "")
850 extension_to_use = "." + defaultExt;
852 internalfullfilename += extension_to_use;
856 fileNames = new string [1];
858 fileNames [0] = internalfullfilename;
860 fileName = internalfullfilename;
862 mwfFileView.WriteRecentlyUsed (internalfullfilename);
863 } else // multiSelect = true
864 if (fileDialogType != FileDialogType.SaveFileDialog) {
865 if (mwfFileView.SelectedItems.Count > 0) {
866 // first remove all selected directories
867 ArrayList al = new ArrayList ();
869 foreach (FileViewListViewItem lvi in mwfFileView.SelectedItems) {
870 FSEntry fsEntry = lvi.FSEntry;
872 if (fsEntry.Attributes != FileAttributes.Directory) {
877 fileName = ((FSEntry)al [0]).FullName;
879 fileNames = new string [al.Count];
881 for (int i = 0; i < al.Count; i++) {
882 fileNames [i] = ((FSEntry)al [i]).FullName;
887 if (checkPathExists && mwfFileView.CurrentRealFolder != null) {
888 if (!Directory.Exists (mwfFileView.CurrentRealFolder)) {
889 string message = "\"" + mwfFileView.CurrentRealFolder + "\" doesn't exist. Please verify that you have entered the correct directory name.";
890 MessageBox.Show (message, openSaveButton.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
892 if (initialDirectory == String.Empty)
893 mwfFileView.ChangeDirectory (null, lastFolder);
895 mwfFileView.ChangeDirectory (null, initialDirectory);
901 if (restoreDirectory) {
902 lastFolder = restoreDirectoryString;
904 lastFolder = mwfFileView.CurrentFolder;
907 if (fileNameComboBox.Items.Count > 0) {
908 if (fileNameComboBox.Items.IndexOf (fileName) == -1) {
909 fileNameComboBox.Items.Insert (0, fileName);
912 fileNameComboBox.Items.Add (fileName);
914 if (fileNameComboBox.Items.Count == 11)
915 fileNameComboBox.Items.RemoveAt (10);
917 CancelEventArgs cancelEventArgs = new CancelEventArgs ();
919 cancelEventArgs.Cancel = false;
921 OnFileOk (cancelEventArgs);
923 form.DialogResult = DialogResult.OK;
926 void OnClickCancelButton (object sender, EventArgs e)
928 if (restoreDirectory)
929 mwfFileView.CurrentFolder = restoreDirectoryString;
931 CancelEventArgs cancelEventArgs = new CancelEventArgs ();
933 cancelEventArgs.Cancel = true;
935 OnFileOk (cancelEventArgs);
937 form.DialogResult = DialogResult.Cancel;
940 void OnClickHelpButton (object sender, EventArgs e)
945 void OnClickSmallButtonToolBar (object sender, ToolBarButtonClickEventArgs e)
947 if (e.Button == upToolBarButton) {
948 mwfFileView.OneDirUp ();
950 if (e.Button == backToolBarButton) {
951 mwfFileView.PopDir ();
953 if (e.Button == newdirToolBarButton) {
954 mwfFileView.CreateNewFolder ();
958 void OnSelectedIndexChangedFileTypeComboBox (object sender, EventArgs e)
960 if (do_not_call_OnSelectedIndexChangedFileTypeComboBox) {
961 do_not_call_OnSelectedIndexChangedFileTypeComboBox = false;
965 filterIndex = fileTypeComboBox.SelectedIndex + 1;
967 mwfFileView.FilterIndex = filterIndex;
970 void OnSelectedFileChangedFileView (object sender, EventArgs e)
972 fileNameComboBox.Text = mwfFileView.CurrentFSEntry.Name;
975 void OnSelectedFilesChangedFileView (object sender, EventArgs e)
977 fileNameComboBox.Text = mwfFileView.SelectedFilesString;
980 void OnForceDialogEndFileView (object sender, EventArgs e)
982 OnClickOpenSaveButton (this, EventArgs.Empty);
985 void OnDirectoryChangedDirComboBox (object sender, EventArgs e)
987 mwfFileView.ChangeDirectory (sender, dirComboBox.CurrentFolder);
990 void OnDirectoryChangedPopupButtonPanel (object sender, EventArgs e)
992 mwfFileView.ChangeDirectory (sender, popupButtonPanel.CurrentFolder);
995 void OnCheckCheckChanged (object sender, EventArgs e)
997 ReadOnlyChecked = readonlyCheckBox.Checked;
1000 private void UpdateFilters ()
1002 ArrayList filters = fileFilter.FilterArrayList;
1004 fileTypeComboBox.BeginUpdate ();
1006 fileTypeComboBox.Items.Clear ();
1008 foreach (FilterStruct fs in filters) {
1009 fileTypeComboBox.Items.Add (fs.filterName);
1012 fileTypeComboBox.SelectedIndex = FilterIndex - 1;
1014 fileTypeComboBox.EndUpdate ();
1016 mwfFileView.FilterArrayList = filters;
1018 mwfFileView.FilterIndex = FilterIndex;
1021 private void ResizeAndRelocateForHelpOrReadOnly ()
1023 form.SuspendLayout ();
1024 if (ShowHelp || ShowReadOnly) {
1025 mwfFileView.Size = new Size (449, 250);
1026 fileNameLabel.Location = new Point (102, 298);
1027 fileNameComboBox.Location = new Point (195, 298);
1028 fileTypeLabel.Location = new Point (102, 324);
1029 fileTypeComboBox.Location = new Point (195, 324);
1030 openSaveButton.Location = new Point (475, 298);
1031 cancelButton.Location = new Point (475, 324);
1033 mwfFileView.Size = new Size (449, 282);
1034 fileNameLabel.Location = new Point (102, 330);
1035 fileNameComboBox.Location = new Point (195, 330);
1036 fileTypeLabel.Location = new Point (102, 356);
1037 fileTypeComboBox.Location = new Point (195, 356);
1038 openSaveButton.Location = new Point (475, 330);
1039 cancelButton.Location = new Point (475, 356);
1043 form.Controls.Add (helpButton);
1045 form.Controls.Remove (helpButton);
1048 form.Controls.Add (readonlyCheckBox);
1050 form.Controls.Remove (readonlyCheckBox);
1051 form.ResumeLayout ();
1054 private void WriteConfigValues (CancelEventArgs ce)
1056 MWFConfig.SetValue (filedialog_string, width_string, form.Width);
1057 MWFConfig.SetValue (filedialog_string, height_string, form.Height);
1058 MWFConfig.SetValue (filedialog_string, x_string, form.Location.X);
1059 MWFConfig.SetValue (filedialog_string, y_string, form.Location.Y);
1062 MWFConfig.SetValue (filedialog_string, lastfolder_string, lastFolder);
1064 string[] fileNameCBItems = new string [fileNameComboBox.Items.Count];
1066 fileNameComboBox.Items.CopyTo (fileNameCBItems, 0);
1068 MWFConfig.SetValue (filedialog_string, filenames_string, fileNameCBItems);
1072 private void ReadConfigValues ()
1074 lastFolder = (string)MWFConfig.GetValue (filedialog_string, lastfolder_string);
1076 if (lastFolder != null && lastFolder.IndexOf ("://") == -1) {
1077 if (!Directory.Exists (lastFolder)) {
1078 lastFolder = MWFVFS.DesktopPrefix;
1082 if (initialDirectory != "")
1083 lastFolder = initialDirectory;
1085 if (lastFolder == null || lastFolder == "")
1086 lastFolder = Environment.CurrentDirectory;
1088 if (RestoreDirectory)
1089 restoreDirectoryString = lastFolder;
1094 #region PopupButtonPanel
1095 internal class PopupButtonPanel : Control, IUpdateFolder
1098 internal class PopupButton : Control
1100 internal enum PopupButtonState
1103 private Image image = null;
1104 private PopupButtonState popupButtonState = PopupButtonState.Normal;
1105 private StringFormat text_format = new StringFormat();
1106 private Rectangle text_rect = Rectangle.Empty;
1108 public PopupButton ()
1110 text_format.Alignment = StringAlignment.Center;
1111 text_format.LineAlignment = StringAlignment.Near;
1113 SetStyle (ControlStyles.DoubleBuffer, true);
1114 SetStyle (ControlStyles.AllPaintingInWmPaint, true);
1115 SetStyle (ControlStyles.UserPaint, true);
1116 SetStyle (ControlStyles.Selectable, false);
1119 public Image Image {
1130 public PopupButtonState ButtonState {
1132 popupButtonState = value;
1137 return popupButtonState;
1141 protected override void OnPaint (PaintEventArgs pe)
1148 private void Draw (PaintEventArgs pe)
1150 Graphics gr = pe.Graphics;
1152 gr.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (BackColor), ClientRectangle);
1155 if (image != null) {
1156 int i_x = (ClientSize.Width - image.Width) / 2;
1158 gr.DrawImage (image, i_x, i_y);
1161 if (Text != String.Empty) {
1162 if (text_rect == Rectangle.Empty)
1163 text_rect = new Rectangle (0, Height - 30, Width, Height - 30);
1165 gr.DrawString (Text, Font, ThemeEngine.Current.ResPool.GetSolidBrush (ForeColor), text_rect, text_format);
1168 switch (popupButtonState) {
1169 case PopupButtonState.Up:
1170 gr.DrawLine (ThemeEngine.Current.ResPool.GetPen (Color.White), 0, 0, ClientSize.Width - 1, 0);
1171 gr.DrawLine (ThemeEngine.Current.ResPool.GetPen (Color.White), 0, 0, 0, ClientSize.Height - 1);
1172 gr.DrawLine (ThemeEngine.Current.ResPool.GetPen (Color.Black), ClientSize.Width - 1, 0, ClientSize.Width - 1, ClientSize.Height - 1);
1173 gr.DrawLine (ThemeEngine.Current.ResPool.GetPen (Color.Black), 0, ClientSize.Height - 1, ClientSize.Width - 1, ClientSize.Height - 1);
1176 case PopupButtonState.Down:
1177 gr.DrawLine (ThemeEngine.Current.ResPool.GetPen (Color.Black), 0, 0, ClientSize.Width - 1, 0);
1178 gr.DrawLine (ThemeEngine.Current.ResPool.GetPen (Color.Black), 0, 0, 0, ClientSize.Height - 1);
1179 gr.DrawLine (ThemeEngine.Current.ResPool.GetPen (Color.White), ClientSize.Width - 1, 0, ClientSize.Width - 1, ClientSize.Height - 1);
1180 gr.DrawLine (ThemeEngine.Current.ResPool.GetPen (Color.White), 0, ClientSize.Height - 1, ClientSize.Width - 1, ClientSize.Height - 1);
1185 protected override void OnMouseEnter (EventArgs e)
1187 if (popupButtonState != PopupButtonState.Down)
1188 popupButtonState = PopupButtonState.Up;
1190 PopupButtonPanel panel = Parent as PopupButtonPanel;
1192 if (panel.focusButton != null && panel.focusButton.ButtonState == PopupButtonState.Up) {
1193 panel.focusButton.ButtonState = PopupButtonState.Normal;
1194 panel.focusButton = null;
1197 base.OnMouseEnter (e);
1200 protected override void OnMouseLeave (EventArgs e)
1202 if (popupButtonState == PopupButtonState.Up)
1203 popupButtonState = PopupButtonState.Normal;
1205 base.OnMouseLeave (e);
1208 protected override void OnClick (EventArgs e)
1210 popupButtonState = PopupButtonState.Down;
1217 private EventHandler on_directory_changed;
1219 private PopupButton recentlyusedButton;
1220 private PopupButton desktopButton;
1221 private PopupButton personalButton;
1222 private PopupButton mycomputerButton;
1223 private PopupButton networkButton;
1225 private PopupButton lastPopupButton = null;
1226 private PopupButton focusButton = null;
1228 private string currentPath;
1230 private int currentFocusIndex;
1232 public PopupButtonPanel ()
1236 //BackColor = Color.FromArgb (128, 128, 128);
1237 Size = new Size (85, 336);
1239 recentlyusedButton = new PopupButton ();
1240 desktopButton = new PopupButton ();
1241 personalButton = new PopupButton ();
1242 mycomputerButton = new PopupButton ();
1243 networkButton = new PopupButton ();
1245 recentlyusedButton.Size = new Size (81, 64);
1246 recentlyusedButton.Image = ThemeEngine.Current.Images (UIIcon.PlacesRecentDocuments, 32);
1247 recentlyusedButton.BackColor = BackColor;
1248 recentlyusedButton.ForeColor = Color.Black;
1249 recentlyusedButton.Location = new Point (2, 2);
1250 recentlyusedButton.Text = "Recently\nused";
1251 recentlyusedButton.Click += new EventHandler (OnClickButton);
1253 desktopButton.Image = ThemeEngine.Current.Images (UIIcon.PlacesDesktop, 32);
1254 desktopButton.BackColor = BackColor;
1255 desktopButton.ForeColor = Color.Black;
1256 desktopButton.Size = new Size (81, 64);
1257 desktopButton.Location = new Point (2, 66);
1258 desktopButton.Text = "Desktop";
1259 desktopButton.Click += new EventHandler (OnClickButton);
1261 personalButton.Image = ThemeEngine.Current.Images (UIIcon.PlacesPersonal, 32);
1262 personalButton.BackColor = BackColor;
1263 personalButton.ForeColor = Color.Black;
1264 personalButton.Size = new Size (81, 64);
1265 personalButton.Location = new Point (2, 130);
1266 personalButton.Text = "Personal";
1267 personalButton.Click += new EventHandler (OnClickButton);
1269 mycomputerButton.Image = ThemeEngine.Current.Images (UIIcon.PlacesMyComputer, 32);
1270 mycomputerButton.BackColor = BackColor;
1271 mycomputerButton.ForeColor = Color.Black;
1272 mycomputerButton.Size = new Size (81, 64);
1273 mycomputerButton.Location = new Point (2, 194);
1274 mycomputerButton.Text = "My Computer";
1275 mycomputerButton.Click += new EventHandler (OnClickButton);
1277 networkButton.Image = ThemeEngine.Current.Images (UIIcon.PlacesMyNetwork, 32);
1278 networkButton.BackColor = BackColor;
1279 networkButton.ForeColor = Color.Black;
1280 networkButton.Size = new Size (81, 64);
1281 networkButton.Location = new Point (2, 258);
1282 networkButton.Text = "My Network";
1283 networkButton.Click += new EventHandler (OnClickButton);
1285 Controls.Add (recentlyusedButton);
1286 Controls.Add (desktopButton);
1287 Controls.Add (personalButton);
1288 Controls.Add (mycomputerButton);
1289 Controls.Add (networkButton);
1291 ResumeLayout (false);
1293 KeyDown += new KeyEventHandler (Key_Down);
1295 SetStyle (ControlStyles.StandardClick, false);
1298 void OnClickButton (object sender, EventArgs e)
1300 if (lastPopupButton != null && lastPopupButton != sender as PopupButton)
1301 lastPopupButton.ButtonState = PopupButton.PopupButtonState.Normal;
1302 lastPopupButton = sender as PopupButton;
1304 if (sender == recentlyusedButton) {
1305 currentPath = MWFVFS.RecentlyUsedPrefix;
1307 if (sender == desktopButton) {
1308 currentPath = MWFVFS.DesktopPrefix;
1310 if (sender == personalButton) {
1311 currentPath = MWFVFS.PersonalPrefix;
1313 if (sender == mycomputerButton) {
1314 currentPath = MWFVFS.MyComputerPrefix;
1316 if (sender == networkButton) {
1317 currentPath = MWFVFS.MyNetworkPrefix;
1320 if (on_directory_changed != null)
1321 on_directory_changed (this, EventArgs.Empty);
1324 public string CurrentFolder {
1326 string currentPath = value;
1327 if (currentPath == MWFVFS.RecentlyUsedPrefix) {
1328 if (lastPopupButton != recentlyusedButton) {
1329 if (lastPopupButton != null)
1330 lastPopupButton.ButtonState = PopupButton.PopupButtonState.Normal;
1331 recentlyusedButton.ButtonState = PopupButton.PopupButtonState.Down;
1332 lastPopupButton = recentlyusedButton;
1335 if (currentPath == MWFVFS.DesktopPrefix) {
1336 if (lastPopupButton != desktopButton) {
1337 if (lastPopupButton != null)
1338 lastPopupButton.ButtonState = PopupButton.PopupButtonState.Normal;
1339 desktopButton.ButtonState = PopupButton.PopupButtonState.Down;
1340 lastPopupButton = desktopButton;
1343 if (currentPath == MWFVFS.PersonalPrefix) {
1344 if (lastPopupButton != personalButton) {
1345 if (lastPopupButton != null)
1346 lastPopupButton.ButtonState = PopupButton.PopupButtonState.Normal;
1347 personalButton.ButtonState = PopupButton.PopupButtonState.Down;
1348 lastPopupButton = personalButton;
1351 if (currentPath == MWFVFS.MyComputerPrefix) {
1352 if (lastPopupButton != mycomputerButton) {
1353 if (lastPopupButton != null)
1354 lastPopupButton.ButtonState = PopupButton.PopupButtonState.Normal;
1355 mycomputerButton.ButtonState = PopupButton.PopupButtonState.Down;
1356 lastPopupButton = mycomputerButton;
1359 if (currentPath == MWFVFS.MyNetworkPrefix) {
1360 if (lastPopupButton != networkButton) {
1361 if (lastPopupButton != null)
1362 lastPopupButton.ButtonState = PopupButton.PopupButtonState.Normal;
1363 networkButton.ButtonState = PopupButton.PopupButtonState.Down;
1364 lastPopupButton = networkButton;
1367 if (lastPopupButton != null) {
1368 lastPopupButton.ButtonState = PopupButton.PopupButtonState.Normal;
1369 lastPopupButton = null;
1378 protected override void OnPaint (PaintEventArgs e)
1380 ControlPaint.DrawBorder3D (e.Graphics, ClientRectangle, Border3DStyle.Sunken);
1384 protected override void OnGotFocus (EventArgs e)
1386 if (lastPopupButton != recentlyusedButton) {
1387 recentlyusedButton.ButtonState = PopupButton.PopupButtonState.Up;
1388 focusButton = recentlyusedButton;
1390 currentFocusIndex = 0;
1392 base.OnGotFocus (e);
1395 protected override void OnLostFocus (EventArgs e)
1397 if (focusButton != null && focusButton.ButtonState != PopupButton.PopupButtonState.Down)
1398 focusButton.ButtonState = PopupButton.PopupButtonState.Normal;
1399 base.OnLostFocus (e);
1402 protected override bool IsInputKey (Keys key)
1412 return base.IsInputKey (key);
1415 private void Key_Down (object sender, KeyEventArgs e)
1417 bool update_focus = false;
1419 if (e.KeyCode == Keys.Left || e.KeyCode == Keys.Up) {
1420 currentFocusIndex --;
1422 if (currentFocusIndex < 0)
1423 currentFocusIndex = Controls.Count - 1;
1425 update_focus = true;
1427 if (e.KeyCode == Keys.Down || e.KeyCode == Keys.Right) {
1428 currentFocusIndex++;
1430 if (currentFocusIndex == Controls.Count)
1431 currentFocusIndex = 0;
1433 update_focus = true;
1435 if (e.KeyCode == Keys.Enter) {
1436 focusButton.ButtonState = PopupButton.PopupButtonState.Down;
1437 OnClickButton (focusButton, EventArgs.Empty);
1441 PopupButton newfocusButton = Controls [currentFocusIndex] as PopupButton;
1442 if (focusButton != null && focusButton.ButtonState != PopupButton.PopupButtonState.Down)
1443 focusButton.ButtonState = PopupButton.PopupButtonState.Normal;
1444 if (newfocusButton.ButtonState != PopupButton.PopupButtonState.Down)
1445 newfocusButton.ButtonState = PopupButton.PopupButtonState.Up;
1446 focusButton = newfocusButton;
1452 public event EventHandler DirectoryChanged {
1453 add { on_directory_changed += value; }
1454 remove { on_directory_changed -= value; }
1460 internal class DirComboBox : ComboBox, IUpdateFolder
1462 #region DirComboBoxItem
1463 internal class DirComboBoxItem
1465 private int imageIndex;
1466 private string name;
1467 private string path;
1469 private ImageList imageList;
1471 public DirComboBoxItem (ImageList imageList, int imageIndex, string name, string path, int xPos)
1473 this.imageList = imageList;
1474 this.imageIndex = imageIndex;
1480 public int ImageIndex {
1486 public string Name {
1492 public string Path {
1504 public ImageList ImageList {
1516 private ImageList imageList = new ImageList();
1518 private string currentPath;
1520 private EventHandler on_directory_changed;
1522 private bool currentpath_internal_change = false;
1524 private Stack folderStack = new Stack();
1526 private static readonly int indent = 6;
1528 private DirComboBoxItem recentlyUsedDirComboboxItem;
1529 private DirComboBoxItem desktopDirComboboxItem;
1530 private DirComboBoxItem personalDirComboboxItem;
1531 private DirComboBoxItem myComputerDirComboboxItem;
1532 private DirComboBoxItem networkDirComboboxItem;
1534 private ArrayList myComputerItems = new ArrayList ();
1536 private DirComboBoxItem mainParentDirComboBoxItem = null;
1537 private DirComboBoxItem real_parent = null;
1541 public DirComboBox (MWFVFS vfs)
1545 DrawMode = DrawMode.OwnerDrawFixed;
1547 imageList.ColorDepth = ColorDepth.Depth32Bit;
1548 imageList.ImageSize = new Size (16, 16);
1549 imageList.Images.Add (ThemeEngine.Current.Images (UIIcon.PlacesRecentDocuments, 16));
1550 imageList.Images.Add (ThemeEngine.Current.Images (UIIcon.PlacesDesktop, 16));
1551 imageList.Images.Add (ThemeEngine.Current.Images (UIIcon.PlacesPersonal, 16));
1552 imageList.Images.Add (ThemeEngine.Current.Images (UIIcon.PlacesMyComputer, 16));
1553 imageList.Images.Add (ThemeEngine.Current.Images (UIIcon.PlacesMyNetwork, 16));
1554 imageList.Images.Add (ThemeEngine.Current.Images (UIIcon.NormalFolder, 16));
1555 imageList.TransparentColor = Color.Transparent;
1557 recentlyUsedDirComboboxItem = new DirComboBoxItem (imageList, 0, "Recently used", MWFVFS.RecentlyUsedPrefix, 0);
1558 desktopDirComboboxItem = new DirComboBoxItem (imageList, 1, "Desktop", MWFVFS.DesktopPrefix, 0);
1559 personalDirComboboxItem = new DirComboBoxItem (imageList, 2, "Personal folder", MWFVFS.PersonalPrefix, indent);
1560 myComputerDirComboboxItem = new DirComboBoxItem (imageList, 3, "My Computer", MWFVFS.MyComputerPrefix, indent);
1561 networkDirComboboxItem = new DirComboBoxItem (imageList, 4, "My Network", MWFVFS.MyNetworkPrefix, indent);
1563 ArrayList al = vfs.GetMyComputerContent ();
1565 foreach (FSEntry fsEntry in al) {
1566 myComputerItems.Add (new DirComboBoxItem (MimeIconEngine.LargeIcons, fsEntry.IconIndex, fsEntry.Name, fsEntry.FullName, indent * 2));
1569 mainParentDirComboBoxItem = myComputerDirComboboxItem;
1571 ResumeLayout (false);
1574 public string CurrentFolder {
1576 currentPath = value;
1578 currentpath_internal_change = true;
1587 private void CreateComboList ()
1590 DirComboBoxItem selection = null;
1592 if (currentPath == MWFVFS.RecentlyUsedPrefix) {
1593 mainParentDirComboBoxItem = recentlyUsedDirComboboxItem;
1594 selection = recentlyUsedDirComboboxItem;
1596 if (currentPath == MWFVFS.DesktopPrefix) {
1597 selection = desktopDirComboboxItem;
1598 mainParentDirComboBoxItem = desktopDirComboboxItem;
1600 if (currentPath == MWFVFS.PersonalPrefix) {
1601 selection = personalDirComboboxItem;
1602 mainParentDirComboBoxItem = personalDirComboboxItem;
1604 if (currentPath == MWFVFS.MyComputerPrefix) {
1605 selection = myComputerDirComboboxItem;
1606 mainParentDirComboBoxItem = myComputerDirComboboxItem;
1608 if (currentPath == MWFVFS.MyNetworkPrefix) {
1609 selection = networkDirComboboxItem;
1610 mainParentDirComboBoxItem = networkDirComboboxItem;
1612 foreach (DirComboBoxItem dci in myComputerItems) {
1613 if (dci.Path == currentPath) {
1614 mainParentDirComboBoxItem = selection = dci;
1624 Items.Add (recentlyUsedDirComboboxItem);
1625 Items.Add (desktopDirComboboxItem);
1626 Items.Add (personalDirComboboxItem);
1627 Items.Add (myComputerDirComboboxItem);
1628 Items.AddRange (myComputerItems);
1629 Items.Add (networkDirComboboxItem);
1631 if (selection == null) {
1632 real_parent = CreateFolderStack ();
1635 if (real_parent != null) {
1636 int local_indent = 0;
1638 if (real_parent == desktopDirComboboxItem)
1641 if (real_parent == personalDirComboboxItem || real_parent == networkDirComboboxItem)
1646 selection = AppendToParent (local_indent, real_parent);
1651 if (selection != null)
1652 SelectedItem = selection;
1655 private DirComboBoxItem CreateFolderStack ()
1657 folderStack.Clear ();
1659 DirectoryInfo di = new DirectoryInfo (currentPath);
1661 folderStack.Push (di);
1663 while (di.Parent != null) {
1666 if (mainParentDirComboBoxItem != personalDirComboboxItem && di.FullName == ThemeEngine.Current.Places (UIIcon.PlacesDesktop))
1667 return desktopDirComboboxItem;
1669 if (mainParentDirComboBoxItem == personalDirComboboxItem) {
1670 if (di.FullName == ThemeEngine.Current.Places (UIIcon.PlacesPersonal))
1671 return personalDirComboboxItem;
1673 foreach (DirComboBoxItem dci in myComputerItems) {
1674 if (dci.Path == di.FullName) {
1680 folderStack.Push (di);
1686 private DirComboBoxItem AppendToParent (int nr_indents, DirComboBoxItem parentDirComboBoxItem)
1688 DirComboBoxItem selection = null;
1690 int index = Items.IndexOf (parentDirComboBoxItem) + 1;
1692 int xPos = indent * nr_indents;
1694 while (folderStack.Count != 0) {
1695 DirectoryInfo dii = folderStack.Pop () as DirectoryInfo;
1697 DirComboBoxItem dci = new DirComboBoxItem (imageList, 5, dii.Name, dii.FullName, xPos);
1699 Items.Insert (index, dci);
1708 protected override void OnDrawItem (DrawItemEventArgs e)
1713 DirComboBoxItem dcbi = Items [e.Index] as DirComboBoxItem;
1715 Bitmap bmp = new Bitmap (e.Bounds.Width, e.Bounds.Height, e.Graphics);
1716 Graphics gr = Graphics.FromImage (bmp);
1718 Color backColor = e.BackColor;
1719 Color foreColor = e.ForeColor;
1721 int xPos = dcbi.XPos;
1723 if ((e.State & DrawItemState.ComboBoxEdit) != 0)
1726 gr.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (backColor),
1727 new Rectangle (0, 0, bmp.Width, bmp.Height));
1729 if ((e.State & DrawItemState.Selected) == DrawItemState.Selected &&
1730 (!DroppedDown || (e.State & DrawItemState.ComboBoxEdit) != DrawItemState.ComboBoxEdit)) {
1731 foreColor = ThemeEngine.Current.ColorHighlightText;
1733 int w = (int) gr.MeasureString (dcbi.Name, e.Font).Width;
1735 gr.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (ThemeEngine.Current.ColorHighlight),
1736 new Rectangle (xPos + 23, 1, w + 3, e.Bounds.Height - 2));
1737 if ((e.State & DrawItemState.Focus) == DrawItemState.Focus) {
1738 ControlPaint.DrawFocusRectangle (gr, new Rectangle (xPos + 22, 0, w + 5,
1739 e.Bounds.Height), foreColor, ThemeEngine.Current.ColorHighlight);
1743 gr.DrawString (dcbi.Name, e.Font , ThemeEngine.Current.ResPool.GetSolidBrush (foreColor), new Point (24 + xPos, (bmp.Height - e.Font.Height) / 2));
1744 gr.DrawImage (dcbi.ImageList.Images [dcbi.ImageIndex], new Rectangle (new Point (xPos + 2, 0), new Size (16, 16)));
1746 e.Graphics.DrawImage (bmp, e.Bounds.X, e.Bounds.Y);
1751 protected override void OnSelectedIndexChanged (EventArgs e)
1753 if (Items.Count > 0) {
1754 DirComboBoxItem dcbi = Items [SelectedIndex] as DirComboBoxItem;
1756 currentPath = dcbi.Path;
1757 // call DirectoryChange event only if the user changes the index with the ComboBox
1759 if (!currentpath_internal_change) {
1760 if (on_directory_changed != null)
1761 on_directory_changed (this, EventArgs.Empty);
1765 currentpath_internal_change = false;
1768 public event EventHandler DirectoryChanged {
1769 add { on_directory_changed += value; }
1770 remove { on_directory_changed -= value; }
1775 #region FilterStruct
1776 internal struct FilterStruct
1778 public string filterName;
1779 public StringCollection filters;
1781 public FilterStruct (string filterName, string filter)
1783 this.filterName = filterName;
1785 filters = new StringCollection ();
1787 SplitFilters (filter);
1790 private void SplitFilters (string filter)
1792 string[] split = filter.Split (new char [] {';'});
1794 filters.AddRange (split);
1800 internal class FileFilter
1802 private ArrayList filterArrayList = new ArrayList();
1804 private string filter;
1806 public FileFilter ()
1809 public FileFilter (string filter)
1811 this.filter = filter;
1816 public ArrayList FilterArrayList {
1818 filterArrayList = value;
1822 return filterArrayList;
1826 public string Filter {
1838 private void SplitFilter ()
1840 filterArrayList.Clear ();
1843 throw new NullReferenceException ("Filter");
1845 if (filter.Length == 0)
1848 string[] filters = filter.Split (new char [] {'|'});
1850 if ((filters.Length % 2) != 0)
1851 throw new ArgumentException ("Filter");
1853 for (int i = 0; i < filters.Length; i += 2) {
1854 FilterStruct filterStruct = new FilterStruct (filters [i], filters [i + 1]);
1856 filterArrayList.Add (filterStruct);
1864 internal class MWFFileView : ListView
1866 private ArrayList filterArrayList;
1868 private bool showHiddenFiles = false;
1870 private EventHandler on_selected_file_changed;
1871 private EventHandler on_selected_files_changed;
1872 private EventHandler on_directory_changed;
1873 private EventHandler on_force_dialog_end;
1875 private string selectedFilesString;
1877 private int filterIndex = 1;
1879 private ToolTip toolTip;
1880 private int oldItemIndexForToolTip = -1;
1882 private ContextMenu contextMenu;
1884 private MenuItem menuItemView;
1885 private MenuItem menuItemNew;
1887 private MenuItem smallIconMenutItem;
1888 private MenuItem tilesMenutItem;
1889 private MenuItem largeIconMenutItem;
1890 private MenuItem listMenutItem;
1891 private MenuItem detailsMenutItem;
1893 private MenuItem newFolderMenuItem;
1894 private MenuItem showHiddenFilesMenuItem;
1896 private int previousCheckedMenuItemIndex;
1898 private ArrayList viewMenuItemClones = new ArrayList ();
1900 private FSEntry currentFSEntry = null;
1902 private string currentFolder;
1903 private string currentRealFolder;
1904 private FSEntry currentFolderFSEntry;
1906 // store DirectoryInfo for a back button for example
1907 private Stack directoryStack = new Stack();
1909 // list of controls(components to enable or disable depending on current directoryStack.Count
1910 private ArrayList dirStackControlsOrComponents = new ArrayList ();
1912 private ToolBarButton folderUpToolBarButton;
1914 private ArrayList registered_senders = new ArrayList ();
1916 private bool should_push = true;
1920 private View old_view;
1922 private int old_menuitem_index;
1923 private bool do_update_view = false;
1925 private int platform = (int) Environment.OSVersion.Platform;
1927 public MWFFileView (MWFVFS vfs)
1930 vfs.RegisterUpdateDelegate (new MWFVFS.UpdateDelegate (RealFileViewUpdate), this);
1934 contextMenu = new ContextMenu ();
1936 toolTip = new ToolTip ();
1937 toolTip.InitialDelay = 300;
1938 toolTip.ReshowDelay = 0;
1943 menuItemView = new MenuItem ("View");
1945 smallIconMenutItem = new MenuItem ("Small Icon", new EventHandler (OnClickViewMenuSubItem));
1946 smallIconMenutItem.RadioCheck = true;
1947 menuItemView.MenuItems.Add (smallIconMenutItem);
1949 tilesMenutItem = new MenuItem ("Tiles", new EventHandler (OnClickViewMenuSubItem));
1950 tilesMenutItem.RadioCheck = true;
1951 menuItemView.MenuItems.Add (tilesMenutItem);
1953 largeIconMenutItem = new MenuItem ("Large Icon", new EventHandler (OnClickViewMenuSubItem));
1954 largeIconMenutItem.RadioCheck = true;
1955 menuItemView.MenuItems.Add (largeIconMenutItem);
1957 listMenutItem = new MenuItem ("List", new EventHandler (OnClickViewMenuSubItem));
1958 listMenutItem.RadioCheck = true;
1959 listMenutItem.Checked = true;
1960 menuItemView.MenuItems.Add (listMenutItem);
1961 previousCheckedMenuItemIndex = listMenutItem.Index;
1963 detailsMenutItem = new MenuItem ("Details", new EventHandler (OnClickViewMenuSubItem));
1964 detailsMenutItem.RadioCheck = true;
1965 menuItemView.MenuItems.Add (detailsMenutItem);
1967 contextMenu.MenuItems.Add (menuItemView);
1969 contextMenu.MenuItems.Add (new MenuItem ("-"));
1972 menuItemNew = new MenuItem ("New");
1974 newFolderMenuItem = new MenuItem ("New Folder", new EventHandler (OnClickNewFolderMenuItem));
1975 menuItemNew.MenuItems.Add (newFolderMenuItem);
1977 contextMenu.MenuItems.Add (menuItemNew);
1979 contextMenu.MenuItems.Add (new MenuItem ("-"));
1981 // Show hidden files menu item
1982 showHiddenFilesMenuItem = new MenuItem ("Show hidden files", new EventHandler (OnClickContextMenu));
1983 showHiddenFilesMenuItem.Checked = showHiddenFiles;
1984 contextMenu.MenuItems.Add (showHiddenFilesMenuItem);
1988 SmallImageList = MimeIconEngine.SmallIcons;
1989 LargeImageList = MimeIconEngine.LargeIcons;
1991 View = old_view = View.List;
1994 ContextMenu = contextMenu;
1996 ResumeLayout (false);
1998 KeyDown += new KeyEventHandler (MWF_KeyDown);
2001 public string CurrentFolder {
2003 return currentFolder;
2006 currentFolder = value;
2010 public string CurrentRealFolder {
2012 return currentRealFolder;
2016 public FSEntry CurrentFSEntry {
2018 return currentFSEntry;
2022 public MenuItem[] ViewMenuItems {
2024 MenuItem[] menuItemClones = new MenuItem [] {
2025 smallIconMenutItem.CloneMenu (),
2026 tilesMenutItem.CloneMenu (),
2027 largeIconMenutItem.CloneMenu (),
2028 listMenutItem.CloneMenu (),
2029 detailsMenutItem.CloneMenu ()
2032 viewMenuItemClones.Add (menuItemClones);
2034 return menuItemClones;
2038 public ArrayList FilterArrayList {
2040 filterArrayList = value;
2044 return filterArrayList;
2048 public bool ShowHiddenFiles {
2050 showHiddenFiles = value;
2054 return showHiddenFiles;
2058 public int FilterIndex {
2060 filterIndex = value;
2070 public string SelectedFilesString {
2072 selectedFilesString = value;
2076 return selectedFilesString;
2080 public void PushDir ()
2082 if (currentFolder != null)
2083 directoryStack.Push (currentFolder);
2085 EnableOrDisableDirstackObjects ();
2088 public void PopDir ()
2090 if (directoryStack.Count == 0)
2093 string new_folder = directoryStack.Pop () as string;
2095 EnableOrDisableDirstackObjects ();
2097 should_push = false;
2099 ChangeDirectory (null, new_folder);
2102 public void RegisterSender (IUpdateFolder iud)
2104 registered_senders.Add (iud);
2107 public void CreateNewFolder ()
2109 if (currentFolder == MWFVFS.MyComputerPrefix ||
2110 currentFolder == MWFVFS.RecentlyUsedPrefix)
2113 FSEntry fsEntry = new FSEntry ();
2114 fsEntry.Attributes = FileAttributes.Directory;
2115 fsEntry.FileType = FSEntry.FSEntryType.Directory;
2116 fsEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("inode/directory");
2117 fsEntry.LastAccessTime = DateTime.Now;
2119 // FIXME: when ListView.LabelEdit is available use it
2120 // listViewItem.BeginEdit();
2122 TextEntryDialog ted = new TextEntryDialog ();
2123 ted.IconPictureBoxImage = MimeIconEngine.LargeIcons.Images.GetImage (fsEntry.IconIndex);
2127 if (currentFolderFSEntry.RealName != null)
2128 folder = currentFolderFSEntry.RealName;
2130 folder = currentFolder;
2132 string tmp_filename = "New Folder";
2134 if (Directory.Exists (Path.Combine (folder, tmp_filename))) {
2137 if ((platform == 4) || (platform == 128)) {
2138 tmp_filename = tmp_filename + "-" + i;
2140 tmp_filename = tmp_filename + " (" + i + ")";
2143 while (Directory.Exists (Path.Combine (folder, tmp_filename))) {
2145 if ((platform == 4) || (platform == 128)) {
2146 tmp_filename = "New Folder" + "-" + i;
2148 tmp_filename = "New Folder" + " (" + i + ")";
2153 ted.FileName = tmp_filename;
2155 if (ted.ShowDialog () == DialogResult.OK) {
2156 string new_folder = Path.Combine (folder, ted.FileName);
2158 if (vfs.CreateFolder (new_folder)) {
2159 fsEntry.FullName = new_folder;
2160 fsEntry.Name = ted.FileName;
2162 FileViewListViewItem listViewItem = new FileViewListViewItem (fsEntry);
2165 Items.Add (listViewItem);
2168 listViewItem.EnsureVisible ();
2173 public void SetSelectedIndexTo (string fname)
2175 foreach (FileViewListViewItem item in Items) {
2176 if (item.Text == fname) {
2178 SelectedItems.Clear ();
2179 item.Selected = true;
2186 public void OneDirUp ()
2188 string parent_folder = vfs.GetParent ();
2189 if (parent_folder != null)
2190 ChangeDirectory (null, parent_folder);
2193 public void ChangeDirectory (object sender, string folder)
2195 if (folder == MWFVFS.DesktopPrefix || folder == MWFVFS.RecentlyUsedPrefix)
2196 folderUpToolBarButton.Enabled = false;
2198 folderUpToolBarButton.Enabled = true;
2200 foreach (IUpdateFolder iuf in registered_senders) {
2201 iuf.CurrentFolder = folder;
2209 currentFolderFSEntry = vfs.ChangeDirectory (folder);
2211 currentFolder = folder;
2213 if (currentFolder.IndexOf ("://") != -1)
2214 currentRealFolder = currentFolderFSEntry.RealName;
2216 currentRealFolder = currentFolder;
2221 SelectedItems.Clear ();
2223 if (folder == MWFVFS.RecentlyUsedPrefix) {
2225 View = View.Details;
2226 old_menuitem_index = previousCheckedMenuItemIndex;
2227 UpdateMenuItems (detailsMenutItem);
2228 do_update_view = true;
2230 if (View != old_view && do_update_view) {
2231 UpdateMenuItems (menuItemView.MenuItems [old_menuitem_index]);
2233 do_update_view = false;
2239 } catch (Exception e) {
2242 MessageBox.Show (e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
2246 public void UpdateFileView ()
2248 if (filterArrayList != null && filterArrayList.Count != 0) {
2249 FilterStruct fs = (FilterStruct)filterArrayList [filterIndex - 1];
2251 vfs.GetFolderContent (fs.filters);
2253 vfs.GetFolderContent ();
2256 public void RealFileViewUpdate (ArrayList directoriesArrayList, ArrayList fileArrayList)
2261 SelectedItems.Clear ();
2263 foreach (FSEntry directoryFSEntry in directoriesArrayList) {
2264 if (!ShowHiddenFiles)
2265 if (directoryFSEntry.Name.StartsWith (".") || directoryFSEntry.Attributes == FileAttributes.Hidden)
2268 FileViewListViewItem listViewItem = new FileViewListViewItem (directoryFSEntry);
2270 Items.Add (listViewItem);
2273 StringCollection collection = new StringCollection ();
2275 foreach (FSEntry fsEntry in fileArrayList) {
2277 // remove duplicates. that can happen when you read recently used files for example
2278 if (collection.Contains (fsEntry.Name)) {
2280 string fileName = fsEntry.Name;
2282 if (collection.Contains (fileName)) {
2285 while (collection.Contains (fileName + "(" + i + ")")) {
2289 fileName = fileName + "(" + i + ")";
2292 fsEntry.Name = fileName;
2295 collection.Add (fsEntry.Name);
2297 DoOneFSEntry (fsEntry);
2303 public void AddControlToEnableDisableByDirStack (object control)
2305 dirStackControlsOrComponents.Add (control);
2308 public void SetFolderUpToolBarButton (ToolBarButton tb)
2310 folderUpToolBarButton = tb;
2313 public void WriteRecentlyUsed (string fullfilename)
2315 vfs.WriteRecentlyUsedFiles (fullfilename);
2318 private void EnableOrDisableDirstackObjects ()
2320 foreach (object o in dirStackControlsOrComponents) {
2322 Control c = o as Control;
2323 c.Enabled = (directoryStack.Count > 1);
2325 if (o is ToolBarButton) {
2326 ToolBarButton t = o as ToolBarButton;
2327 t.Enabled = (directoryStack.Count > 0);
2332 private void DoOneFSEntry (FSEntry fsEntry)
2334 if (!ShowHiddenFiles)
2335 if (fsEntry.Name.StartsWith (".") || fsEntry.Attributes == FileAttributes.Hidden)
2338 FileViewListViewItem listViewItem = new FileViewListViewItem (fsEntry);
2340 Items.Add (listViewItem);
2343 private void MWF_KeyDown (object sender, KeyEventArgs e)
2345 if (e.KeyCode == Keys.Back) {
2350 protected override void OnClick (EventArgs e)
2353 if (SelectedItems.Count > 0) {
2354 FileViewListViewItem listViewItem = SelectedItems [0] as FileViewListViewItem;
2356 FSEntry fsEntry = listViewItem.FSEntry;
2358 if (fsEntry.FileType == FSEntry.FSEntryType.File) {
2359 currentFSEntry = fsEntry;
2361 if (on_selected_file_changed != null)
2362 on_selected_file_changed (this, EventArgs.Empty);
2370 protected override void OnDoubleClick (EventArgs e)
2372 if (SelectedItems.Count > 0) {
2373 FileViewListViewItem listViewItem = SelectedItems [0] as FileViewListViewItem;
2375 FSEntry fsEntry = listViewItem.FSEntry;
2377 if (fsEntry.Attributes == FileAttributes.Directory) {
2379 ChangeDirectory (null, fsEntry.FullName);
2381 if (on_directory_changed != null)
2382 on_directory_changed (this, EventArgs.Empty);
2384 currentFSEntry = fsEntry;
2386 if (on_selected_file_changed != null)
2387 on_selected_file_changed (this, EventArgs.Empty);
2389 if (on_force_dialog_end != null)
2390 on_force_dialog_end (this, EventArgs.Empty);
2396 base.OnDoubleClick (e);
2399 protected override void OnSelectedIndexChanged (EventArgs e)
2401 if (SelectedItems.Count > 0) {
2402 selectedFilesString = "";
2404 if (SelectedItems.Count == 1) {
2405 FileViewListViewItem listViewItem = SelectedItems [0] as FileViewListViewItem;
2407 FSEntry fsEntry = listViewItem.FSEntry;
2409 if (fsEntry.Attributes != FileAttributes.Directory)
2410 selectedFilesString = SelectedItems [0].Text;
2412 foreach (FileViewListViewItem lvi in SelectedItems) {
2413 FSEntry fsEntry = lvi.FSEntry;
2415 if (fsEntry.Attributes != FileAttributes.Directory)
2416 selectedFilesString = selectedFilesString + "\"" + lvi.Text + " ";
2420 if (on_selected_files_changed != null)
2421 on_selected_files_changed (this, EventArgs.Empty);
2424 base.OnSelectedIndexChanged (e);
2427 protected override void OnMouseMove (MouseEventArgs e)
2429 FileViewListViewItem item = GetItemAt (e.X, e.Y) as FileViewListViewItem;
2432 int currentItemIndex = item.Index;
2434 if (currentItemIndex != oldItemIndexForToolTip) {
2435 oldItemIndexForToolTip = currentItemIndex;
2437 if (toolTip != null && toolTip.Active)
2438 toolTip.Active = false;
2440 FSEntry fsEntry = item.FSEntry;
2442 string output = String.Empty;
2444 if (fsEntry.FileType == FSEntry.FSEntryType.Directory)
2445 output = "Directory: " + fsEntry.FullName;
2446 else if (fsEntry.FileType == FSEntry.FSEntryType.Device)
2447 output = "Device: "+ fsEntry.FullName;
2448 else if (fsEntry.FileType == FSEntry.FSEntryType.Network)
2449 output = "Network: " + fsEntry.FullName;
2451 output = "File: " + fsEntry.FullName;
2453 toolTip.SetToolTip (this, output);
2455 toolTip.Active = true;
2459 base.OnMouseMove (e);
2462 void OnClickContextMenu (object sender, EventArgs e)
2464 MenuItem senderMenuItem = sender as MenuItem;
2466 if (senderMenuItem == showHiddenFilesMenuItem) {
2467 senderMenuItem.Checked = !senderMenuItem.Checked;
2468 showHiddenFiles = senderMenuItem.Checked;
2473 void OnClickViewMenuSubItem (object sender, EventArgs e)
2475 MenuItem senderMenuItem = (MenuItem)sender;
2477 UpdateMenuItems (senderMenuItem);
2481 switch (senderMenuItem.Index) {
2483 View = View.SmallIcon;
2486 View = View.LargeIcon;
2489 View = View.LargeIcon;
2495 View = View.Details;
2502 private void UpdateMenuItems (MenuItem senderMenuItem)
2504 menuItemView.MenuItems [previousCheckedMenuItemIndex].Checked = false;
2505 menuItemView.MenuItems [senderMenuItem.Index].Checked = true;
2507 foreach (MenuItem[] items in viewMenuItemClones) {
2508 items [previousCheckedMenuItemIndex].Checked = false;
2509 items [senderMenuItem.Index].Checked = true;
2512 previousCheckedMenuItemIndex = senderMenuItem.Index;
2515 void OnClickNewFolderMenuItem (object sender, EventArgs e)
2520 public event EventHandler SelectedFileChanged {
2521 add { on_selected_file_changed += value; }
2522 remove { on_selected_file_changed -= value; }
2525 public event EventHandler SelectedFilesChanged {
2526 add { on_selected_files_changed += value; }
2527 remove { on_selected_files_changed -= value; }
2530 public event EventHandler DirectoryChanged {
2531 add { on_directory_changed += value; }
2532 remove { on_directory_changed -= value; }
2535 public event EventHandler ForceDialogEnd {
2536 add { on_force_dialog_end += value; }
2537 remove { on_force_dialog_end -= value; }
2542 #region FileListViewItem
2543 internal class FileViewListViewItem : ListViewItem
2545 private FSEntry fsEntry;
2547 public FileViewListViewItem (FSEntry fsEntry)
2549 this.fsEntry = fsEntry;
2551 ImageIndex = fsEntry.IconIndex;
2553 Text = fsEntry.Name;
2555 switch (fsEntry.FileType) {
2556 case FSEntry.FSEntryType.Directory:
2558 SubItems.Add ("Directory");
2559 SubItems.Add (fsEntry.LastAccessTime.ToShortDateString () + " " + fsEntry.LastAccessTime.ToShortTimeString ());
2561 case FSEntry.FSEntryType.File:
2564 if (fsEntry.FileSize > 1024)
2565 fileLen = fsEntry.FileSize / 1024;
2566 } catch (Exception) {
2570 SubItems.Add (fileLen.ToString () + " KB");
2571 SubItems.Add ("File");
2572 SubItems.Add (fsEntry.LastAccessTime.ToShortDateString () + " " + fsEntry.LastAccessTime.ToShortTimeString ());
2574 case FSEntry.FSEntryType.Device:
2576 SubItems.Add ("Device");
2577 SubItems.Add (fsEntry.LastAccessTime.ToShortDateString () + " " + fsEntry.LastAccessTime.ToShortTimeString ());
2579 case FSEntry.FSEntryType.RemovableDevice:
2581 SubItems.Add ("RemovableDevice");
2582 SubItems.Add (fsEntry.LastAccessTime.ToShortDateString () + " " + fsEntry.LastAccessTime.ToShortTimeString ());
2589 public FSEntry FSEntry {
2601 #region IUpdateFolder
2602 internal interface IUpdateFolder
2604 string CurrentFolder {get; set;}
2608 #region TextEntryDialog
2609 // FIXME: When ListView.LabelEdit is implemented remove me
2610 internal class TextEntryDialog : Form
2612 private Label label1;
2613 private Button okButton;
2614 private TextBox newNameTextBox;
2615 private PictureBox iconPictureBox;
2616 private Button cancelButton;
2617 private GroupBox groupBox1;
2619 public TextEntryDialog ()
2621 groupBox1 = new GroupBox ();
2622 cancelButton = new Button ();
2623 iconPictureBox = new PictureBox ();
2624 newNameTextBox = new TextBox ();
2625 okButton = new Button ();
2626 label1 = new Label ();
2627 groupBox1.SuspendLayout ();
2631 groupBox1.Controls.Add (newNameTextBox);
2632 groupBox1.Controls.Add (label1);
2633 groupBox1.Controls.Add (iconPictureBox);
2634 groupBox1.Location = new Point (8, 8);
2635 groupBox1.Size = new Size (232, 160);
2636 groupBox1.TabIndex = 5;
2637 groupBox1.TabStop = false;
2638 groupBox1.Text = "New Name";
2641 cancelButton.DialogResult = DialogResult.Cancel;
2642 cancelButton.Location = new Point (168, 176);
2643 cancelButton.TabIndex = 4;
2644 cancelButton.Text = "Cancel";
2647 iconPictureBox.BorderStyle = BorderStyle.Fixed3D;
2648 iconPictureBox.Location = new Point (86, 24);
2649 iconPictureBox.Size = new Size (60, 60);
2650 iconPictureBox.TabIndex = 3;
2651 iconPictureBox.TabStop = false;
2652 iconPictureBox.SizeMode = PictureBoxSizeMode.CenterImage;
2655 newNameTextBox.Location = new Point (16, 128);
2656 newNameTextBox.Size = new Size (200, 20);
2657 newNameTextBox.TabIndex = 5;
2658 newNameTextBox.Text = "";
2661 okButton.DialogResult = DialogResult.OK;
2662 okButton.Location = new Point (80, 176);
2663 okButton.TabIndex = 3;
2664 okButton.Text = "OK";
2667 label1.Location = new Point (16, 96);
2668 label1.Size = new Size (200, 23);
2669 label1.TabIndex = 4;
2670 label1.Text = "Enter Name:";
2671 label1.TextAlign = ContentAlignment.MiddleCenter;
2674 AcceptButton = okButton;
2675 AutoScaleBaseSize = new Size (5, 13);
2676 CancelButton = cancelButton;
2677 ClientSize = new Size (248, 205);
2678 Controls.Add (groupBox1);
2679 Controls.Add (cancelButton);
2680 Controls.Add (okButton);
2681 FormBorderStyle = FormBorderStyle.FixedDialog;
2682 Text = "New Folder or File";
2683 groupBox1.ResumeLayout (false);
2684 ResumeLayout (false);
2686 newNameTextBox.Select ();
2689 public Image IconPictureBoxImage {
2691 iconPictureBox.Image = value;
2695 public string FileName {
2697 return newNameTextBox.Text;
2700 newNameTextBox.Text = value;
2707 internal class MWFVFS
2709 private FileSystem fileSystem;
2711 private int platform = (int) Environment.OSVersion.Platform;
2713 public static readonly string DesktopPrefix = "Desktop://";
2714 public static readonly string PersonalPrefix = "Personal://";
2715 public static readonly string MyComputerPrefix = "MyComputer://";
2716 public static readonly string RecentlyUsedPrefix = "RecentlyUsed://";
2717 public static readonly string MyNetworkPrefix = "MyNetwork://";
2718 public static readonly string MyComputerPersonalPrefix = "MyComputerPersonal://";
2720 public static Hashtable MyComputerDevicesPrefix = new Hashtable ();
2722 public delegate void UpdateDelegate (ArrayList folders, ArrayList files);
2723 private UpdateDelegate updateDelegate;
2724 private Control calling_control;
2726 private ThreadStart get_folder_content_thread_start;
2727 private Thread worker;
2728 private WorkerThread workerThread = null;
2729 private StringCollection the_filters;
2733 if ((platform == 4) || (platform == 128)) {
2734 fileSystem = new UnixFileSystem ();
2736 fileSystem = new WinFileSystem ();
2740 public FSEntry ChangeDirectory (string folder)
2742 return fileSystem.ChangeDirectory (folder);
2745 public void GetFolderContent ()
2747 GetFolderContent (null);
2750 public void GetFolderContent (StringCollection filters)
2752 the_filters = filters;
2754 if (workerThread != null) {
2755 workerThread.Stop ();
2756 workerThread = null;
2759 // Added next line to ensure the control is created before BeginInvoke is called on it
2760 calling_control.CreateControl();
2761 workerThread = new WorkerThread (fileSystem, the_filters, updateDelegate, calling_control);
2763 get_folder_content_thread_start = new ThreadStart (workerThread.GetFolderContentThread);
2764 worker = new Thread (get_folder_content_thread_start);
2765 worker.IsBackground = true;
2769 internal class WorkerThread
2771 private FileSystem fileSystem;
2772 private StringCollection the_filters;
2773 private UpdateDelegate updateDelegate;
2774 private Control calling_control;
2775 private readonly object lockobject = new object ();
2776 private bool stopped = false;
2778 public WorkerThread (FileSystem fileSystem, StringCollection the_filters, UpdateDelegate updateDelegate, Control calling_control)
2780 this.fileSystem = fileSystem;
2781 this.the_filters = the_filters;
2782 this.updateDelegate = updateDelegate;
2783 this.calling_control = calling_control;
2786 public void GetFolderContentThread()
2791 fileSystem.GetFolderContent (the_filters, out folders, out files);
2796 if (updateDelegate != null) {
2798 object[] objectArray = new object[2];
2800 objectArray[0] = folders;
2801 objectArray[1] = files;
2803 calling_control.BeginInvoke (updateDelegate, objectArray);
2816 public ArrayList GetFoldersOnly ()
2818 return fileSystem.GetFoldersOnly ();
2821 public void WriteRecentlyUsedFiles (string filename)
2823 fileSystem.WriteRecentlyUsedFiles (filename);
2826 public ArrayList GetRecentlyUsedFiles ()
2828 return fileSystem.GetRecentlyUsedFiles ();
2831 public ArrayList GetMyComputerContent ()
2833 return fileSystem.GetMyComputerContent ();
2836 public ArrayList GetMyNetworkContent ()
2838 return fileSystem.GetMyNetworkContent ();
2841 public bool CreateFolder (string new_folder)
2844 if (Directory.Exists (new_folder)) {
2845 string message = "Folder \"" + new_folder + "\" already exists.";
2846 MessageBox.Show (message, new_folder, MessageBoxButtons.OK, MessageBoxIcon.Warning);
2849 Directory.CreateDirectory (new_folder);
2850 } catch (Exception e) {
2851 MessageBox.Show (e.Message, new_folder, MessageBoxButtons.OK, MessageBoxIcon.Error);
2858 public string GetParent ()
2860 return fileSystem.GetParent ();
2863 public void RegisterUpdateDelegate(UpdateDelegate updateDelegate, Control control)
2865 this.updateDelegate = updateDelegate;
2866 calling_control = control;
2872 internal abstract class FileSystem
2874 protected string currentTopFolder = "";
2875 protected FSEntry currentFolderFSEntry = null;
2876 protected FSEntry currentTopFolderFSEntry = null;
2878 public FSEntry ChangeDirectory (string folder)
2880 if (folder == MWFVFS.DesktopPrefix) {
2881 currentTopFolder = MWFVFS.DesktopPrefix;
2882 currentTopFolderFSEntry = currentFolderFSEntry = GetDesktopFSEntry ();
2884 if (folder == MWFVFS.PersonalPrefix) {
2885 currentTopFolder = MWFVFS.PersonalPrefix;
2886 currentTopFolderFSEntry = currentFolderFSEntry = GetPersonalFSEntry ();
2888 if (folder == MWFVFS.MyComputerPersonalPrefix) {
2889 currentTopFolder = MWFVFS.MyComputerPersonalPrefix;
2890 currentTopFolderFSEntry = currentFolderFSEntry = GetMyComputerPersonalFSEntry ();
2892 if (folder == MWFVFS.RecentlyUsedPrefix) {
2893 currentTopFolder = MWFVFS.RecentlyUsedPrefix;
2894 currentTopFolderFSEntry = currentFolderFSEntry = GetRecentlyUsedFSEntry ();
2896 if (folder == MWFVFS.MyComputerPrefix) {
2897 currentTopFolder = MWFVFS.MyComputerPrefix;
2898 currentTopFolderFSEntry = currentFolderFSEntry = GetMyComputerFSEntry ();
2900 if (folder == MWFVFS.MyNetworkPrefix) {
2901 currentTopFolder = MWFVFS.MyNetworkPrefix;
2902 currentTopFolderFSEntry = currentFolderFSEntry = GetMyNetworkFSEntry ();
2906 foreach (DictionaryEntry entry in MWFVFS.MyComputerDevicesPrefix) {
2907 FSEntry fsEntry = entry.Value as FSEntry;
2908 if (folder == fsEntry.FullName) {
2909 currentTopFolder = entry.Key as string;
2910 currentTopFolderFSEntry = currentFolderFSEntry = fsEntry;
2917 currentFolderFSEntry = GetDirectoryFSEntry (new DirectoryInfo (folder), currentTopFolderFSEntry);
2921 return currentFolderFSEntry;
2924 public string GetParent ()
2926 return currentFolderFSEntry.Parent;
2929 // directories_out and files_out contain FSEntry objects
2930 public void GetFolderContent (StringCollection filters, out ArrayList directories_out, out ArrayList files_out)
2932 directories_out = new ArrayList ();
2933 files_out = new ArrayList ();
2935 if (currentFolderFSEntry.FullName == MWFVFS.DesktopPrefix) {
2936 FSEntry personalFSEntry = GetPersonalFSEntry ();
2938 directories_out.Add (personalFSEntry);
2940 FSEntry myComputerFSEntry = GetMyComputerFSEntry ();
2942 directories_out.Add (myComputerFSEntry);
2944 FSEntry myNetworkFSEntry = GetMyNetworkFSEntry ();
2946 directories_out.Add (myNetworkFSEntry);
2948 ArrayList d_out = null;
2949 ArrayList f_out = null;
2950 GetNormalFolderContent (ThemeEngine.Current.Places (UIIcon.PlacesDesktop), filters, out d_out, out f_out);
2951 directories_out.AddRange (d_out);
2952 files_out.AddRange (f_out);
2955 if (currentFolderFSEntry.FullName == MWFVFS.RecentlyUsedPrefix) {
2956 files_out = GetRecentlyUsedFiles ();
2958 if (currentFolderFSEntry.FullName == MWFVFS.MyComputerPrefix) {
2959 directories_out.AddRange (GetMyComputerContent ());
2961 if (currentFolderFSEntry.FullName == MWFVFS.PersonalPrefix || currentFolderFSEntry.FullName == MWFVFS.MyComputerPersonalPrefix) {
2962 ArrayList d_out = null;
2963 ArrayList f_out = null;
2964 GetNormalFolderContent (ThemeEngine.Current.Places (UIIcon.PlacesPersonal), filters, out d_out, out f_out);
2965 directories_out.AddRange (d_out);
2966 files_out.AddRange (f_out);
2968 if (currentFolderFSEntry.FullName == MWFVFS.MyNetworkPrefix) {
2969 directories_out.AddRange (GetMyNetworkContent ());
2971 GetNormalFolderContent (currentFolderFSEntry.FullName, filters, out directories_out, out files_out);
2975 public ArrayList GetFoldersOnly ()
2977 ArrayList directories_out = new ArrayList ();
2979 if (currentFolderFSEntry.FullName == MWFVFS.DesktopPrefix) {
2980 FSEntry personalFSEntry = GetPersonalFSEntry ();
2982 directories_out.Add (personalFSEntry);
2984 FSEntry myComputerFSEntry = GetMyComputerFSEntry ();
2986 directories_out.Add (myComputerFSEntry);
2988 FSEntry myNetworkFSEntry = GetMyNetworkFSEntry ();
2990 directories_out.Add (myNetworkFSEntry);
2992 ArrayList d_out = GetNormalFolders (ThemeEngine.Current.Places (UIIcon.PlacesDesktop));
2993 directories_out.AddRange (d_out);
2996 if (currentFolderFSEntry.FullName == MWFVFS.RecentlyUsedPrefix) {
2997 //files_out = GetRecentlyUsedFiles ();
2999 if (currentFolderFSEntry.FullName == MWFVFS.MyComputerPrefix) {
3000 directories_out.AddRange (GetMyComputerContent ());
3002 if (currentFolderFSEntry.FullName == MWFVFS.PersonalPrefix || currentFolderFSEntry.FullName == MWFVFS.MyComputerPersonalPrefix) {
3003 ArrayList d_out = GetNormalFolders (ThemeEngine.Current.Places (UIIcon.PlacesPersonal));
3004 directories_out.AddRange (d_out);
3006 if (currentFolderFSEntry.FullName == MWFVFS.MyNetworkPrefix) {
3007 directories_out.AddRange (GetMyNetworkContent ());
3009 directories_out = GetNormalFolders (currentFolderFSEntry.FullName);
3011 return directories_out;
3014 protected void GetNormalFolderContent (string from_folder, StringCollection filters, out ArrayList directories_out, out ArrayList files_out)
3016 DirectoryInfo dirinfo = new DirectoryInfo (from_folder);
3018 directories_out = new ArrayList ();
3020 DirectoryInfo[] dirs = null;
3023 dirs = dirinfo.GetDirectories ();
3024 } catch (Exception) {}
3027 for (int i = 0; i < dirs.Length; i++) {
3028 directories_out.Add (GetDirectoryFSEntry (dirs [i], currentTopFolderFSEntry));
3031 files_out = new ArrayList ();
3033 ArrayList files = new ArrayList ();
3036 if (filters == null) {
3037 files.AddRange (dirinfo.GetFiles ());
3039 foreach (string s in filters)
3040 files.AddRange (dirinfo.GetFiles (s));
3042 } catch (Exception) {}
3044 for (int i = 0; i < files.Count; i++) {
3045 files_out.Add (GetFileFSEntry (files [i] as FileInfo));
3049 protected ArrayList GetNormalFolders (string from_folder)
3051 DirectoryInfo dirinfo = new DirectoryInfo (from_folder);
3053 ArrayList directories_out = new ArrayList ();
3055 DirectoryInfo[] dirs = null;
3058 dirs = dirinfo.GetDirectories ();
3059 } catch (Exception) {}
3062 for (int i = 0; i < dirs.Length; i++) {
3063 directories_out.Add (GetDirectoryFSEntry (dirs [i], currentTopFolderFSEntry));
3066 return directories_out;
3069 protected virtual FSEntry GetDirectoryFSEntry (DirectoryInfo dirinfo, FSEntry topFolderFSEntry)
3071 FSEntry fs = new FSEntry ();
3073 fs.Attributes = dirinfo.Attributes;
3074 fs.FullName = dirinfo.FullName;
3075 fs.Name = dirinfo.Name;
3076 fs.MainTopNode = topFolderFSEntry;
3077 fs.FileType = FSEntry.FSEntryType.Directory;
3078 fs.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("inode/directory");
3079 fs.LastAccessTime = dirinfo.LastAccessTime;
3084 protected virtual FSEntry GetFileFSEntry (FileInfo fileinfo)
3086 FSEntry fs = new FSEntry ();
3088 fs.Attributes = fileinfo.Attributes;
3089 fs.FullName = fileinfo.FullName;
3090 fs.Name = fileinfo.Name;
3091 fs.FileType = FSEntry.FSEntryType.File;
3092 fs.IconIndex = MimeIconEngine.GetIconIndexForFile (fileinfo.FullName);
3093 fs.LastAccessTime = fileinfo.LastAccessTime;
3094 // the following catches broken symbolic links
3095 if ((int)fs.Attributes != 0)
3096 fs.FileSize = fileinfo.Length;
3102 protected abstract FSEntry GetDesktopFSEntry ();
3104 protected abstract FSEntry GetRecentlyUsedFSEntry ();
3106 protected abstract FSEntry GetPersonalFSEntry ();
3108 protected abstract FSEntry GetMyComputerPersonalFSEntry ();
3110 protected abstract FSEntry GetMyComputerFSEntry ();
3112 protected abstract FSEntry GetMyNetworkFSEntry ();
3114 public abstract void WriteRecentlyUsedFiles (string fileToAdd);
3116 public abstract ArrayList GetRecentlyUsedFiles ();
3118 public abstract ArrayList GetMyComputerContent ();
3120 public abstract ArrayList GetMyNetworkContent ();
3124 #region UnixFileSystem
3125 internal class UnixFileSystem : FileSystem
3127 private MasterMount masterMount = new MasterMount ();
3128 private FSEntry desktopFSEntry = null;
3129 private FSEntry recentlyusedFSEntry = null;
3130 private FSEntry personalFSEntry = null;
3131 private FSEntry mycomputerpersonalFSEntry = null;
3132 private FSEntry mycomputerFSEntry = null;
3133 private FSEntry mynetworkFSEntry = null;
3135 private string personal_folder;
3136 private string recently_used_path;
3137 private string full_kde_recent_document_dir;
3139 public UnixFileSystem ()
3141 personal_folder = ThemeEngine.Current.Places (UIIcon.PlacesPersonal);
3142 recently_used_path = Path.Combine (personal_folder, ".recently-used");
3144 full_kde_recent_document_dir = personal_folder + "/.kde/share/apps/RecentDocuments";
3146 desktopFSEntry = new FSEntry ();
3148 desktopFSEntry.Attributes = FileAttributes.Directory;
3149 desktopFSEntry.FullName = MWFVFS.DesktopPrefix;
3150 desktopFSEntry.Name = "Desktop";
3151 desktopFSEntry.RealName = ThemeEngine.Current.Places (UIIcon.PlacesDesktop);
3152 desktopFSEntry.FileType = FSEntry.FSEntryType.Directory;
3153 desktopFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("desktop/desktop");
3154 desktopFSEntry.LastAccessTime = DateTime.Now;
3156 recentlyusedFSEntry = new FSEntry ();
3158 recentlyusedFSEntry.Attributes = FileAttributes.Directory;
3159 recentlyusedFSEntry.FullName = MWFVFS.RecentlyUsedPrefix;
3160 recentlyusedFSEntry.Name = "Recently Used";
3161 recentlyusedFSEntry.FileType = FSEntry.FSEntryType.Directory;
3162 recentlyusedFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("recently/recently");
3163 recentlyusedFSEntry.LastAccessTime = DateTime.Now;
3165 personalFSEntry = new FSEntry ();
3167 personalFSEntry.Attributes = FileAttributes.Directory;
3168 personalFSEntry.FullName = MWFVFS.PersonalPrefix;
3169 personalFSEntry.Name = "Personal";
3170 personalFSEntry.MainTopNode = GetDesktopFSEntry ();
3171 personalFSEntry.RealName = ThemeEngine.Current.Places (UIIcon.PlacesPersonal);
3172 personalFSEntry.FileType = FSEntry.FSEntryType.Directory;
3173 personalFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("directory/home");
3174 personalFSEntry.LastAccessTime = DateTime.Now;
3176 mycomputerpersonalFSEntry = new FSEntry ();
3178 mycomputerpersonalFSEntry.Attributes = FileAttributes.Directory;
3179 mycomputerpersonalFSEntry.FullName = MWFVFS.MyComputerPersonalPrefix;
3180 mycomputerpersonalFSEntry.Name = "Personal";
3181 mycomputerpersonalFSEntry.MainTopNode = GetMyComputerFSEntry ();
3182 mycomputerpersonalFSEntry.RealName = ThemeEngine.Current.Places (UIIcon.PlacesPersonal);
3183 mycomputerpersonalFSEntry.FileType = FSEntry.FSEntryType.Directory;
3184 mycomputerpersonalFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("directory/home");
3185 mycomputerpersonalFSEntry.LastAccessTime = DateTime.Now;
3187 mycomputerFSEntry = new FSEntry ();
3189 mycomputerFSEntry.Attributes = FileAttributes.Directory;
3190 mycomputerFSEntry.FullName = MWFVFS.MyComputerPrefix;
3191 mycomputerFSEntry.Name = "My Computer";
3192 mycomputerFSEntry.MainTopNode = GetDesktopFSEntry ();
3193 mycomputerFSEntry.FileType = FSEntry.FSEntryType.Directory;
3194 mycomputerFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("workplace/workplace");
3195 mycomputerFSEntry.LastAccessTime = DateTime.Now;
3197 mynetworkFSEntry = new FSEntry ();
3199 mynetworkFSEntry.Attributes = FileAttributes.Directory;
3200 mynetworkFSEntry.FullName = MWFVFS.MyNetworkPrefix;
3201 mynetworkFSEntry.Name = "My Network";
3202 mynetworkFSEntry.MainTopNode = GetDesktopFSEntry ();
3203 mynetworkFSEntry.FileType = FSEntry.FSEntryType.Directory;
3204 mynetworkFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("network/network");
3205 mynetworkFSEntry.LastAccessTime = DateTime.Now;
3208 public override void WriteRecentlyUsedFiles (string fileToAdd)
3210 if (File.Exists (recently_used_path) && new FileInfo (recently_used_path).Length > 0) {
3211 XmlDocument xml_doc = new XmlDocument ();
3212 xml_doc.Load (recently_used_path);
3214 XmlNode grand_parent_node = xml_doc.SelectSingleNode ("RecentFiles");
3216 if (grand_parent_node != null) {
3217 // create a new element
3218 XmlElement new_recent_item_node = xml_doc.CreateElement ("RecentItem");
3220 XmlElement new_child = xml_doc.CreateElement ("URI");
3221 UriBuilder ub = new UriBuilder ();
3222 ub.Path = fileToAdd;
3225 XmlText new_text_child = xml_doc.CreateTextNode (ub.ToString ());
3226 new_child.AppendChild (new_text_child);
3228 new_recent_item_node.AppendChild (new_child);
3230 new_child = xml_doc.CreateElement ("Mime-Type");
3231 new_text_child = xml_doc.CreateTextNode (Mime.GetMimeTypeForFile (fileToAdd));
3232 new_child.AppendChild (new_text_child);
3234 new_recent_item_node.AppendChild (new_child);
3236 new_child = xml_doc.CreateElement ("Timestamp");
3237 long seconds = (long)(DateTime.UtcNow - new DateTime (1970, 1, 1)).TotalSeconds;
3238 new_text_child = xml_doc.CreateTextNode (seconds.ToString ());
3239 new_child.AppendChild (new_text_child);
3241 new_recent_item_node.AppendChild (new_child);
3243 new_child = xml_doc.CreateElement ("Groups");
3245 new_recent_item_node.AppendChild (new_child);
3247 // now search the nodes in grand_parent_node for another instance of the new uri and if found remove it
3248 // so that the new node is the first one
3249 foreach (XmlNode n in grand_parent_node.ChildNodes) {
3250 XmlNode uri_node = n.SelectSingleNode ("URI");
3251 if (uri_node != null) {
3252 XmlNode uri_node_child = uri_node.FirstChild;
3253 if (uri_node_child is XmlText)
3254 if (ub.ToString () == ((XmlText)uri_node_child).Data) {
3255 grand_parent_node.RemoveChild (n);
3261 // prepend the new recent item to the grand parent node list
3262 grand_parent_node.PrependChild (new_recent_item_node);
3264 // limit the # of RecentItems to 10
3265 if (grand_parent_node.ChildNodes.Count > 10) {
3266 while (grand_parent_node.ChildNodes.Count > 10)
3267 grand_parent_node.RemoveChild (grand_parent_node.LastChild);
3271 xml_doc.Save (recently_used_path);
3272 } catch (Exception) {
3276 XmlDocument xml_doc = new XmlDocument ();
3277 xml_doc.AppendChild (xml_doc.CreateXmlDeclaration ("1.0", "", ""));
3279 XmlElement recentFiles_element = xml_doc.CreateElement ("RecentFiles");
3281 XmlElement new_recent_item_node = xml_doc.CreateElement ("RecentItem");
3283 XmlElement new_child = xml_doc.CreateElement ("URI");
3284 UriBuilder ub = new UriBuilder ();
3285 ub.Path = fileToAdd;
3288 XmlText new_text_child = xml_doc.CreateTextNode (ub.ToString ());
3289 new_child.AppendChild (new_text_child);
3291 new_recent_item_node.AppendChild (new_child);
3293 new_child = xml_doc.CreateElement ("Mime-Type");
3294 new_text_child = xml_doc.CreateTextNode (Mime.GetMimeTypeForFile (fileToAdd));
3295 new_child.AppendChild (new_text_child);
3297 new_recent_item_node.AppendChild (new_child);
3299 new_child = xml_doc.CreateElement ("Timestamp");
3300 long seconds = (long)(DateTime.UtcNow - new DateTime (1970, 1, 1)).TotalSeconds;
3301 new_text_child = xml_doc.CreateTextNode (seconds.ToString ());
3302 new_child.AppendChild (new_text_child);
3304 new_recent_item_node.AppendChild (new_child);
3306 new_child = xml_doc.CreateElement ("Groups");
3308 new_recent_item_node.AppendChild (new_child);
3310 recentFiles_element.AppendChild (new_recent_item_node);
3312 xml_doc.AppendChild (recentFiles_element);
3315 xml_doc.Save (recently_used_path);
3316 } catch (Exception) {
3321 // return an ArrayList with FSEntry objects
3322 public override ArrayList GetRecentlyUsedFiles ()
3324 // check for GNOME and KDE
3326 ArrayList files_al = new ArrayList ();
3329 if (File.Exists (recently_used_path)) {
3331 XmlTextReader xtr = new XmlTextReader (recently_used_path);
3332 while (xtr.Read ()) {
3333 if (xtr.NodeType == XmlNodeType.Element && xtr.Name.ToUpper () == "URI") {
3335 Uri uri = new Uri (xtr.Value);
3336 if (!files_al.Contains (uri.LocalPath))
3337 if (File.Exists (uri.LocalPath))
3338 files_al.Add (GetFileFSEntry (new FileInfo (uri.LocalPath)));
3342 } catch (Exception) {
3348 if (Directory.Exists (full_kde_recent_document_dir)) {
3349 string[] files = Directory.GetFiles (full_kde_recent_document_dir, "*.desktop");
3351 foreach (string file_name in files) {
3352 StreamReader sr = new StreamReader (file_name);
3354 string line = sr.ReadLine ();
3356 while (line != null) {
3357 line = line.Trim ();
3359 if (line.StartsWith ("URL=")) {
3360 line = line.Replace ("URL=", "");
3361 line = line.Replace ("$HOME", personal_folder);
3363 Uri uri = new Uri (line);
3364 if (!files_al.Contains (uri.LocalPath))
3365 if (File.Exists (uri.LocalPath))
3366 files_al.Add (GetFileFSEntry (new FileInfo (uri.LocalPath)));
3370 line = sr.ReadLine ();
3381 // return an ArrayList with FSEntry objects
3382 public override ArrayList GetMyComputerContent ()
3384 ArrayList my_computer_content_arraylist = new ArrayList ();
3386 if (masterMount.ProcMountAvailable) {
3387 masterMount.GetMounts ();
3389 foreach (MasterMount.Mount mount in masterMount.Block_devices) {
3390 FSEntry fsEntry = new FSEntry ();
3391 fsEntry.FileType = FSEntry.FSEntryType.Device;
3393 fsEntry.FullName = mount.mount_point;
3395 fsEntry.Name = "HDD (" + mount.fsType + ", " + mount.device_short + ")";
3397 fsEntry.FsType = mount.fsType;
3398 fsEntry.DeviceShort = mount.device_short;
3400 fsEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("harddisk/harddisk");
3402 fsEntry.Attributes = FileAttributes.Directory;
3404 fsEntry.MainTopNode = GetMyComputerFSEntry ();
3406 my_computer_content_arraylist.Add (fsEntry);
3408 if (!MWFVFS.MyComputerDevicesPrefix.Contains (fsEntry.FullName + "://"))
3409 MWFVFS.MyComputerDevicesPrefix.Add (fsEntry.FullName + "://", fsEntry);
3412 foreach (MasterMount.Mount mount in masterMount.Removable_devices) {
3413 FSEntry fsEntry = new FSEntry ();
3414 fsEntry.FileType = FSEntry.FSEntryType.RemovableDevice;
3416 fsEntry.FullName = mount.mount_point;
3418 bool is_dvd_cdrom = mount.fsType == MasterMount.FsTypes.usbfs ? false : true;
3419 string type_name = is_dvd_cdrom ? "DVD/CD-Rom" : "USB";
3420 string mime_type = is_dvd_cdrom ? "cdrom/cdrom" : "removable/removable";
3422 fsEntry.Name = type_name +" (" + mount.device_short + ")";
3424 fsEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType (mime_type);
3426 fsEntry.FsType = mount.fsType;
3427 fsEntry.DeviceShort = mount.device_short;
3429 fsEntry.Attributes = FileAttributes.Directory;
3431 fsEntry.MainTopNode = GetMyComputerFSEntry ();
3433 my_computer_content_arraylist.Add (fsEntry);
3435 string contain_string = fsEntry.FullName + "://";
3436 if (!MWFVFS.MyComputerDevicesPrefix.Contains (contain_string))
3437 MWFVFS.MyComputerDevicesPrefix.Add (contain_string, fsEntry);
3441 my_computer_content_arraylist.Add (GetMyComputerPersonalFSEntry ());
3443 return my_computer_content_arraylist;
3446 public override ArrayList GetMyNetworkContent ()
3448 ArrayList fsEntries = new ArrayList ();
3450 foreach (MasterMount.Mount mount in masterMount.Network_devices) {
3451 FSEntry fsEntry = new FSEntry ();
3452 fsEntry.FileType = FSEntry.FSEntryType.Network;
3454 fsEntry.FullName = mount.mount_point;
3456 fsEntry.FsType = mount.fsType;
3457 fsEntry.DeviceShort = mount.device_short;
3459 fsEntry.Name = "Network (" + mount.fsType + ", " + mount.device_short + ")";
3461 switch (mount.fsType) {
3462 case MasterMount.FsTypes.nfs:
3463 fsEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("nfs/nfs");
3465 case MasterMount.FsTypes.smbfs:
3466 fsEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("smb/smb");
3468 case MasterMount.FsTypes.ncpfs:
3469 fsEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("network/network");
3471 case MasterMount.FsTypes.cifs:
3472 fsEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("network/network");
3478 fsEntry.Attributes = FileAttributes.Directory;
3480 fsEntry.MainTopNode = GetMyNetworkFSEntry ();
3482 fsEntries.Add (fsEntry);
3487 protected override FSEntry GetDesktopFSEntry ()
3489 return desktopFSEntry;
3492 protected override FSEntry GetRecentlyUsedFSEntry ()
3494 return recentlyusedFSEntry;
3497 protected override FSEntry GetPersonalFSEntry ()
3499 return personalFSEntry;
3502 protected override FSEntry GetMyComputerPersonalFSEntry ()
3504 return mycomputerpersonalFSEntry;
3507 protected override FSEntry GetMyComputerFSEntry ()
3509 return mycomputerFSEntry;
3512 protected override FSEntry GetMyNetworkFSEntry ()
3514 return mynetworkFSEntry;
3519 #region WinFileSystem
3520 internal class WinFileSystem : FileSystem
3522 private FSEntry desktopFSEntry = null;
3523 private FSEntry recentlyusedFSEntry = null;
3524 private FSEntry personalFSEntry = null;
3525 private FSEntry mycomputerpersonalFSEntry = null;
3526 private FSEntry mycomputerFSEntry = null;
3527 private FSEntry mynetworkFSEntry = null;
3529 public WinFileSystem ()
3531 desktopFSEntry = new FSEntry ();
3533 desktopFSEntry.Attributes = FileAttributes.Directory;
3534 desktopFSEntry.FullName = MWFVFS.DesktopPrefix;
3535 desktopFSEntry.Name = "Desktop";
3536 desktopFSEntry.RealName = ThemeEngine.Current.Places (UIIcon.PlacesDesktop);
3537 desktopFSEntry.FileType = FSEntry.FSEntryType.Directory;
3538 desktopFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("desktop/desktop");
3539 desktopFSEntry.LastAccessTime = DateTime.Now;
3541 recentlyusedFSEntry = new FSEntry ();
3543 recentlyusedFSEntry.Attributes = FileAttributes.Directory;
3544 recentlyusedFSEntry.FullName = MWFVFS.RecentlyUsedPrefix;
3545 recentlyusedFSEntry.RealName = ThemeEngine.Current.Places (UIIcon.PlacesRecentDocuments);
3546 recentlyusedFSEntry.Name = "Recently Used";
3547 recentlyusedFSEntry.FileType = FSEntry.FSEntryType.Directory;
3548 recentlyusedFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("recently/recently");
3549 recentlyusedFSEntry.LastAccessTime = DateTime.Now;
3551 personalFSEntry = new FSEntry ();
3553 personalFSEntry.Attributes = FileAttributes.Directory;
3554 personalFSEntry.FullName = MWFVFS.PersonalPrefix;
3555 personalFSEntry.Name = "Personal";
3556 personalFSEntry.MainTopNode = GetDesktopFSEntry ();
3557 personalFSEntry.RealName = ThemeEngine.Current.Places (UIIcon.PlacesPersonal);
3558 personalFSEntry.FileType = FSEntry.FSEntryType.Directory;
3559 personalFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("directory/home");
3560 personalFSEntry.LastAccessTime = DateTime.Now;
3562 mycomputerpersonalFSEntry = new FSEntry ();
3564 mycomputerpersonalFSEntry.Attributes = FileAttributes.Directory;
3565 mycomputerpersonalFSEntry.FullName = MWFVFS.MyComputerPersonalPrefix;
3566 mycomputerpersonalFSEntry.Name = "Personal";
3567 mycomputerpersonalFSEntry.MainTopNode = GetMyComputerFSEntry ();
3568 mycomputerpersonalFSEntry.RealName = ThemeEngine.Current.Places (UIIcon.PlacesPersonal);
3569 mycomputerpersonalFSEntry.FileType = FSEntry.FSEntryType.Directory;
3570 mycomputerpersonalFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("directory/home");
3571 mycomputerpersonalFSEntry.LastAccessTime = DateTime.Now;
3573 mycomputerFSEntry = new FSEntry ();
3575 mycomputerFSEntry.Attributes = FileAttributes.Directory;
3576 mycomputerFSEntry.FullName = MWFVFS.MyComputerPrefix;
3577 mycomputerFSEntry.Name = "My Computer";
3578 mycomputerFSEntry.MainTopNode = GetDesktopFSEntry ();
3579 mycomputerFSEntry.FileType = FSEntry.FSEntryType.Directory;
3580 mycomputerFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("workplace/workplace");
3581 mycomputerFSEntry.LastAccessTime = DateTime.Now;
3583 mynetworkFSEntry = new FSEntry ();
3585 mynetworkFSEntry.Attributes = FileAttributes.Directory;
3586 mynetworkFSEntry.FullName = MWFVFS.MyNetworkPrefix;
3587 mynetworkFSEntry.Name = "My Network";
3588 mynetworkFSEntry.MainTopNode = GetDesktopFSEntry ();
3589 mynetworkFSEntry.FileType = FSEntry.FSEntryType.Directory;
3590 mynetworkFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("network/network");
3591 mynetworkFSEntry.LastAccessTime = DateTime.Now;
3594 public override void WriteRecentlyUsedFiles (string fileToAdd)
3596 // TODO: Implement this method
3597 // use SHAddToRecentDocs ?
3600 public override ArrayList GetRecentlyUsedFiles ()
3602 ArrayList al = new ArrayList ();
3604 DirectoryInfo di = new DirectoryInfo (recentlyusedFSEntry.RealName);
3606 FileInfo[] fileinfos = di.GetFiles ();
3608 foreach (FileInfo fi in fileinfos) {
3609 al.Add (GetFileFSEntry (fi));
3615 public override ArrayList GetMyComputerContent ()
3617 string[] logical_drives = Directory.GetLogicalDrives ();
3619 ArrayList my_computer_content_arraylist = new ArrayList ();
3621 foreach (string drive in logical_drives) {
3622 FSEntry fsEntry = new FSEntry ();
3623 fsEntry.FileType = FSEntry.FSEntryType.Device;
3625 fsEntry.FullName = drive;
3627 fsEntry.Name = drive;
3629 fsEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("harddisk/harddisk");
3631 fsEntry.Attributes = FileAttributes.Directory;
3633 fsEntry.MainTopNode = GetMyComputerFSEntry ();
3635 my_computer_content_arraylist.Add (fsEntry);
3637 string contain_string = fsEntry.FullName + "://";
3638 if (!MWFVFS.MyComputerDevicesPrefix.Contains (contain_string))
3639 MWFVFS.MyComputerDevicesPrefix.Add (contain_string, fsEntry);
3642 my_computer_content_arraylist.Add (GetMyComputerPersonalFSEntry ());
3644 return my_computer_content_arraylist;
3647 public override ArrayList GetMyNetworkContent ()
3649 // TODO: Implement this method
3650 return new ArrayList ();
3652 protected override FSEntry GetDesktopFSEntry ()
3654 return desktopFSEntry;
3657 protected override FSEntry GetRecentlyUsedFSEntry ()
3659 return recentlyusedFSEntry;
3662 protected override FSEntry GetPersonalFSEntry ()
3664 return personalFSEntry;
3667 protected override FSEntry GetMyComputerPersonalFSEntry ()
3669 return mycomputerpersonalFSEntry;
3672 protected override FSEntry GetMyComputerFSEntry ()
3674 return mycomputerFSEntry;
3677 protected override FSEntry GetMyNetworkFSEntry ()
3679 return mynetworkFSEntry;
3685 internal class FSEntry
3687 public enum FSEntryType
3699 private MasterMount.FsTypes fsType;
3700 private string device_short;
3701 private string fullName;
3702 private string name;
3703 private string realName = null;
3704 private FileAttributes attributes = FileAttributes.Normal;
3705 private long fileSize;
3706 private FSEntryType fileType;
3707 private DateTime lastAccessTime;
3708 private FSEntry mainTopNode = null;
3710 private int iconIndex;
3712 private string parent;
3714 public MasterMount.FsTypes FsType {
3724 public string DeviceShort {
3726 device_short = value;
3730 return device_short;
3734 public string FullName {
3744 public string Name {
3754 public string RealName {
3764 public FileAttributes Attributes {
3774 public long FileSize {
3784 public FSEntryType FileType {
3794 public DateTime LastAccessTime {
3796 lastAccessTime = value;
3800 return lastAccessTime;
3804 public int IconIndex {
3814 public FSEntry MainTopNode {
3816 mainTopNode = value;
3824 public string Parent {
3830 parent = GetParent ();
3836 private string GetParent ()
3838 if (fullName == MWFVFS.PersonalPrefix) {
3839 return MWFVFS.DesktopPrefix;
3841 if (fullName == MWFVFS.MyComputerPersonalPrefix) {
3842 return MWFVFS.MyComputerPrefix;
3844 if (fullName == MWFVFS.MyComputerPrefix) {
3845 return MWFVFS.DesktopPrefix;
3847 if (fullName == MWFVFS.MyNetworkPrefix) {
3848 return MWFVFS.DesktopPrefix;
3850 if (fullName == MWFVFS.DesktopPrefix) {
3853 if (fullName == MWFVFS.RecentlyUsedPrefix) {
3856 foreach (DictionaryEntry entry in MWFVFS.MyComputerDevicesPrefix) {
3857 FSEntry fsEntry = entry.Value as FSEntry;
3858 if (fullName == fsEntry.FullName) {
3859 return fsEntry.MainTopNode.FullName;
3863 DirectoryInfo dirInfo = new DirectoryInfo (fullName);
3865 DirectoryInfo dirInfoParent = dirInfo.Parent;
3867 if (dirInfoParent != null) {
3868 FSEntry fsEntry = MWFVFS.MyComputerDevicesPrefix [dirInfoParent.FullName + "://"] as FSEntry;
3870 if (fsEntry != null) {
3871 return fsEntry.FullName;
3874 if (mainTopNode != null) {
3875 if (dirInfoParent.FullName == ThemeEngine.Current.Places (UIIcon.PlacesDesktop) &&
3876 mainTopNode.FullName == MWFVFS.DesktopPrefix) {
3877 return mainTopNode.FullName;
3879 if (dirInfoParent.FullName == ThemeEngine.Current.Places (UIIcon.PlacesPersonal) &&
3880 mainTopNode.FullName == MWFVFS.PersonalPrefix) {
3881 return mainTopNode.FullName;
3883 if (dirInfoParent.FullName == ThemeEngine.Current.Places (UIIcon.PlacesPersonal) &&
3884 mainTopNode.FullName == MWFVFS.MyComputerPersonalPrefix) {
3885 return mainTopNode.FullName;
3889 return dirInfoParent.FullName;
3899 // Alexsantas little *nix helper
3900 internal class MasterMount
3903 internal enum FsTypes
3927 internal struct Mount
3929 public string device_or_filesystem;
3930 public string device_short;
3931 public string mount_point;
3932 public FsTypes fsType;
3935 bool proc_mount_available = false;
3937 ArrayList block_devices = new ArrayList ();
3938 ArrayList network_devices = new ArrayList ();
3939 ArrayList removable_devices = new ArrayList ();
3941 private int platform = (int) Environment.OSVersion.Platform;
3942 private MountComparer mountComparer = new MountComparer ();
3944 public MasterMount ()
3946 // maybe check if the current user can access /proc/mounts
3947 if ((platform == 4) || (platform == 128))
3948 if (File.Exists ("/proc/mounts"))
3949 proc_mount_available = true;
3952 public ArrayList Block_devices {
3954 return block_devices;
3958 public ArrayList Network_devices {
3960 return network_devices;
3964 public ArrayList Removable_devices {
3966 return removable_devices;
3970 public bool ProcMountAvailable {
3972 return proc_mount_available;
3976 public void GetMounts ()
3978 if (!proc_mount_available)
3981 block_devices.Clear ();
3982 network_devices.Clear ();
3983 removable_devices.Clear ();
3986 StreamReader sr = new StreamReader ("/proc/mounts");
3988 string line = sr.ReadLine ();
3990 ArrayList lines = new ArrayList ();
3991 while (line != null) {
3992 if (lines.IndexOf (line) == -1) { // Avoid duplicates
3993 ProcessProcMountLine (line);
3996 line = sr.ReadLine ();
4001 block_devices.Sort (mountComparer);
4002 network_devices.Sort (mountComparer);
4003 removable_devices.Sort (mountComparer);
4009 private void ProcessProcMountLine (string line)
4011 string[] split = line.Split (new char [] {' '});
4013 if (split != null && split.Length > 0) {
4014 Mount mount = new Mount ();
4016 if (split [0].StartsWith ("/dev/"))
4017 mount.device_short = split [0].Replace ("/dev/", "");
4019 mount.device_short = split [0];
4021 mount.device_or_filesystem = split [0];
4022 mount.mount_point = split [1];
4024 // TODO: other removable devices, floppy
4028 if (split [2] == "nfs") {
4029 mount.fsType = FsTypes.nfs;
4030 network_devices.Add (mount);
4031 } else if (split [2] == "smbfs") {
4032 mount.fsType = FsTypes.smbfs;
4033 network_devices.Add (mount);
4034 } else if (split [2] == "cifs") {
4035 mount.fsType = FsTypes.cifs;
4036 network_devices.Add (mount);
4037 } else if (split [2] == "ncpfs") {
4038 mount.fsType = FsTypes.ncpfs;
4039 network_devices.Add (mount);
4041 } else if (split [2] == "iso9660") { //cdrom
4042 mount.fsType = FsTypes.iso9660;
4043 removable_devices.Add (mount);
4044 } else if (split [2] == "usbfs") { //usb ? not tested
4045 mount.fsType = FsTypes.usbfs;
4046 removable_devices.Add (mount);
4048 } else if (split [0].StartsWith ("/")) { //block devices
4049 if (split [1].StartsWith ("/dev/")) // root static, do not add
4052 if (split [2] == "ext2")
4053 mount.fsType = FsTypes.ext2;
4054 else if (split [2] == "ext3")
4055 mount.fsType = FsTypes.ext3;
4056 else if (split [2] == "reiserfs")
4057 mount.fsType = FsTypes.reiserfs;
4058 else if (split [2] == "xfs")
4059 mount.fsType = FsTypes.xfs;
4060 else if (split [2] == "vfat")
4061 mount.fsType = FsTypes.vfat;
4062 else if (split [2] == "ntfs")
4063 mount.fsType = FsTypes.ntfs;
4064 else if (split [2] == "msdos")
4065 mount.fsType = FsTypes.msdos;
4066 else if (split [2] == "umsdos")
4067 mount.fsType = FsTypes.umsdos;
4068 else if (split [2] == "hpfs")
4069 mount.fsType = FsTypes.hpfs;
4070 else if (split [2] == "minix")
4071 mount.fsType = FsTypes.minix;
4072 else if (split [2] == "jfs")
4073 mount.fsType = FsTypes.jfs;
4075 block_devices.Add (mount);
4080 public class MountComparer : IComparer
4082 public int Compare (object mount1, object mount2)
4084 return String.Compare (((Mount)mount1).device_short, ((Mount)mount2).device_short);
4091 // easy to use class to store and read internal MWF config settings.
4092 // the config values are stored in the users home dir as a hidden xml file called "mwf_config".
4093 // currently supports int, string, byte, color and arrays (including byte arrays)
4094 // don't forget, when you read a value you still have to cast this value.
4097 // MWFConfig.SetValue ("SomeClass", "What", value);
4098 // object o = MWFConfig.GetValue ("SomeClass", "What");
4102 // string[] configFileNames = (string[])MWFConfig.GetValue ("FileDialog", "FileNames");
4103 // MWFConfig.SetValue ("FileDialog", "LastFolder", "/home/user");
4105 internal class MWFConfig
4107 private static MWFConfigInstance Instance = new MWFConfigInstance ();
4109 private static object lock_object = new object();
4111 public static object GetValue (string class_name, string value_name)
4113 lock (lock_object) {
4114 return Instance.GetValue (class_name, value_name);
4118 public static void SetValue (string class_name, string value_name, object value)
4120 lock (lock_object) {
4121 Instance.SetValue (class_name, value_name, value);
4125 public static void Flush ()
4127 lock (lock_object) {
4132 public static void RemoveClass (string class_name)
4134 lock (lock_object) {
4135 Instance.RemoveClass (class_name);
4139 public static void RemoveClassValue (string class_name, string value_name)
4141 lock (lock_object) {
4142 Instance.RemoveClassValue (class_name, value_name);
4146 public static void RemoveAllClassValues (string class_name)
4148 lock (lock_object) {
4149 Instance.RemoveAllClassValues (class_name);
4153 internal class MWFConfigInstance
4155 Hashtable classes_hashtable = new Hashtable ();
4157 static string full_file_name;
4160 static string default_file_name;
4161 readonly string configName = "MWFConfig";
4162 static int platform = (int) Environment.OSVersion.Platform;
4164 static bool IsUnix ()
4166 return (platform == 4 || platform == 128);
4169 static MWFConfigInstance ()
4171 string b = "mwf_config";
4172 string dir = Environment.GetFolderPath (Environment.SpecialFolder.Personal);
4175 dir = Path.Combine (dir, ".mono");
4177 Directory.CreateDirectory (dir);
4181 default_file_name = Path.Combine (dir, b);
4182 full_file_name = default_file_name;
4185 public MWFConfigInstance ()
4187 Open (default_file_name);
4191 public MWFConfigInstance (string filename)
4193 path = Path.GetDirectoryName (filename);
4194 if (path == null || path == String.Empty) {
4195 path = Environment.GetFolderPath (Environment.SpecialFolder.Personal);
4197 full_file_name = Path.Combine (path, filename);
4199 full_file_name = filename;
4201 Open (full_file_name);
4204 ~MWFConfigInstance ()
4209 public object GetValue (string class_name, string value_name)
4211 ClassEntry class_entry = classes_hashtable [class_name] as ClassEntry;
4213 if (class_entry != null)
4214 return class_entry.GetValue (value_name);
4219 public void SetValue (string class_name, string value_name, object value)
4221 ClassEntry class_entry = classes_hashtable [class_name] as ClassEntry;
4223 if (class_entry == null) {
4224 class_entry = new ClassEntry ();
4225 class_entry.ClassName = class_name;
4226 classes_hashtable [class_name] = class_entry;
4229 class_entry.SetValue (value_name, value);
4232 private void Open (string filename)
4235 xtr = new XmlTextReader (filename);
4240 } catch (Exception) {
4244 public void Flush ()
4247 xtw = new XmlTextWriter (full_file_name, null);
4248 xtw.Formatting = Formatting.Indented;
4255 File.SetAttributes (full_file_name, FileAttributes.Hidden);
4256 } catch (Exception){
4260 public void RemoveClass (string class_name)
4262 ClassEntry class_entry = classes_hashtable [class_name] as ClassEntry;
4264 if (class_entry != null) {
4265 class_entry.RemoveAllClassValues ();
4267 classes_hashtable.Remove (class_name);
4271 public void RemoveClassValue (string class_name, string value_name)
4273 ClassEntry class_entry = classes_hashtable [class_name] as ClassEntry;
4275 if (class_entry != null) {
4276 class_entry.RemoveClassValue (value_name);
4280 public void RemoveAllClassValues (string class_name)
4282 ClassEntry class_entry = classes_hashtable [class_name] as ClassEntry;
4284 if (class_entry != null) {
4285 class_entry.RemoveAllClassValues ();
4289 private void ReadConfig ()
4291 if (!CheckForMWFConfig ())
4294 while (xtr.Read ()) {
4295 switch (xtr.NodeType) {
4296 case XmlNodeType.Element:
4297 ClassEntry class_entry = classes_hashtable [xtr.Name] as ClassEntry;
4299 if (class_entry == null) {
4300 class_entry = new ClassEntry ();
4301 class_entry.ClassName = xtr.Name;
4302 classes_hashtable [xtr.Name] = class_entry;
4305 class_entry.ReadXml (xtr);
4311 private bool CheckForMWFConfig ()
4314 if (xtr.NodeType == XmlNodeType.Element) {
4315 if (xtr.Name == configName)
4323 private void WriteConfig ()
4325 if (classes_hashtable.Count == 0)
4328 xtw.WriteStartElement (configName);
4329 foreach (DictionaryEntry entry in classes_hashtable) {
4330 ClassEntry class_entry = entry.Value as ClassEntry;
4332 class_entry.WriteXml (xtw);
4334 xtw.WriteEndElement ();
4337 internal class ClassEntry
4339 private Hashtable classvalues_hashtable = new Hashtable ();
4340 private string className;
4342 public string ClassName {
4352 public void SetValue (string value_name, object value)
4354 ClassValue class_value = classvalues_hashtable [value_name] as ClassValue;
4356 if (class_value == null) {
4357 class_value = new ClassValue ();
4358 class_value.Name = value_name;
4359 classvalues_hashtable [value_name] = class_value;
4362 class_value.SetValue (value);
4365 public object GetValue (string value_name)
4367 ClassValue class_value = classvalues_hashtable [value_name] as ClassValue;
4369 if (class_value == null) {
4373 return class_value.GetValue ();
4376 public void RemoveAllClassValues ()
4378 classvalues_hashtable.Clear ();
4381 public void RemoveClassValue (string value_name)
4383 ClassValue class_value = classvalues_hashtable [value_name] as ClassValue;
4385 if (class_value != null) {
4386 classvalues_hashtable.Remove (value_name);
4390 public void ReadXml (XmlTextReader xtr)
4392 while (xtr.Read ()) {
4393 switch (xtr.NodeType) {
4394 case XmlNodeType.Element:
4395 string name = xtr.GetAttribute ("name");
4397 ClassValue class_value = classvalues_hashtable [name] as ClassValue;
4399 if (class_value == null) {
4400 class_value = new ClassValue ();
4401 class_value.Name = name;
4402 classvalues_hashtable [name] = class_value;
4405 class_value.ReadXml (xtr);
4408 case XmlNodeType.EndElement:
4414 public void WriteXml (XmlTextWriter xtw)
4416 if (classvalues_hashtable.Count == 0)
4419 xtw.WriteStartElement (className);
4421 foreach (DictionaryEntry entry in classvalues_hashtable) {
4422 ClassValue class_value = entry.Value as ClassValue;
4424 class_value.WriteXml (xtw);
4427 xtw.WriteEndElement ();
4431 internal class ClassValue
4433 private object value;
4434 private string name;
4436 public string Name {
4446 public void SetValue (object value)
4450 public object GetValue ()
4455 public void ReadXml (XmlTextReader xtr)
4458 string single_value;
4460 type = xtr.GetAttribute ("type");
4462 if (type == "byte_array" || type.IndexOf ("-array") == -1) {
4463 single_value = xtr.ReadString ();
4465 if (type == "string") {
4466 value = single_value;
4468 if (type == "int") {
4469 value = Int32.Parse (single_value);
4471 if (type == "byte") {
4472 value = Byte.Parse (single_value);
4474 if (type == "color") {
4475 int color = Int32.Parse (single_value);
4476 value = Color.FromArgb (color);
4478 if (type == "byte-array") {
4479 byte[] b_array = Convert.FromBase64String (single_value);
4483 ReadXmlArrayValues (xtr, type);
4487 private void ReadXmlArrayValues (XmlTextReader xtr, string type)
4489 ArrayList al = new ArrayList ();
4491 while (xtr.Read ()) {
4492 switch (xtr.NodeType) {
4493 case XmlNodeType.Element:
4494 string single_value = xtr.ReadString ();
4496 if (type == "int-array") {
4497 int int_val = Int32.Parse (single_value);
4500 if (type == "string-array") {
4501 string str_val = single_value;
4506 case XmlNodeType.EndElement:
4507 if (xtr.Name == "value") {
4508 if (type == "int-array") {
4509 value = al.ToArray (typeof(int));
4511 if (type == "string-array") {
4512 value = al.ToArray (typeof(string));
4521 public void WriteXml (XmlTextWriter xtw)
4523 xtw.WriteStartElement ("value");
4524 xtw.WriteAttributeString ("name", name);
4525 if (value is Array) {
4526 WriteArrayContent (xtw);
4528 WriteSingleContent (xtw);
4530 xtw.WriteEndElement ();
4533 private void WriteSingleContent (XmlTextWriter xtw)
4535 string type_string = "";
4537 if (value is string)
4538 type_string = "string";
4541 type_string = "int";
4544 type_string = "byte";
4547 type_string = "color";
4549 xtw.WriteAttributeString ("type", type_string);
4552 xtw.WriteString (((Color)value).ToArgb ().ToString ());
4554 xtw.WriteString (value.ToString ());
4557 private void WriteArrayContent (XmlTextWriter xtw)
4559 string type_string = "";
4560 string type_name = "";
4562 if (value is string[]) {
4563 type_string = "string-array";
4564 type_name = "string";
4566 if (value is int[]) {
4567 type_string = "int-array";
4570 if (value is byte[]) {
4571 type_string = "byte-array";
4575 xtw.WriteAttributeString ("type", type_string);
4577 if (type_string != "byte-array") {
4578 Array array = value as Array;
4580 foreach (object o in array) {
4581 xtw.WriteStartElement (type_name);
4582 xtw.WriteString (o.ToString ());
4583 xtw.WriteEndElement ();
4586 byte[] b_array = value as byte [];
4588 xtw.WriteString (Convert.ToBase64String (b_array, 0, b_array.Length));