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) 2006 Alexander Olk
24 // Alexander Olk alex.olk@googlemail.com
25 // Gert Driesen (drieseng@users.sourceforge.net)
28 // Keyboard shortcuts (DEL, F5, F2)
32 using System.Collections;
33 using System.Collections.Specialized;
34 using System.ComponentModel;
37 using System.Resources;
38 using System.Threading;
41 namespace System.Windows.Forms
44 [DefaultProperty ("FileName")]
45 [DefaultEvent ("FileOk")]
46 public abstract class FileDialog : CommonDialog
48 protected static readonly object EventFileOk = new object ();
49 private static int MaxFileNameItems = 10;
51 internal enum FileDialogType
57 private bool addExtension = true;
58 private bool checkFileExists;
59 private bool checkPathExists = true;
60 private string defaultExt;
61 private bool dereferenceLinks = true;
62 private string[] fileNames;
63 private string filter = "";
64 private int filterIndex = 1;
65 private int setFilterIndex = 1;
66 private string initialDirectory;
67 private bool restoreDirectory;
68 private bool showHelp;
70 private bool validateNames = true;
72 private bool checkForIllegalChars = true;
75 private Button cancelButton;
76 private ToolBarButton upToolBarButton;
77 private PopupButtonPanel popupButtonPanel;
78 private Button openSaveButton;
79 private Button helpButton;
80 private Label fileTypeLabel;
81 private ToolBarButton menueToolBarButton;
82 private ContextMenu menueToolBarButtonContextMenu;
83 private ToolBar smallButtonToolBar;
84 private DirComboBox dirComboBox;
85 private ComboBox fileNameComboBox;
86 private Label fileNameLabel;
87 private MWFFileView mwfFileView;
88 private Label searchSaveLabel;
89 private ToolBarButton newdirToolBarButton;
90 private ToolBarButton backToolBarButton;
91 private ComboBox fileTypeComboBox;
92 private ImageList imageListTopToolbar;
93 private CheckBox readonlyCheckBox;
95 private bool multiSelect;
97 private string restoreDirectoryString = String.Empty;
99 internal FileDialogType fileDialogType;
101 private bool do_not_call_OnSelectedIndexChangedFileTypeComboBox;
103 private bool showReadOnly;
104 private bool readOnlyChecked;
105 internal bool createPrompt;
106 internal bool overwritePrompt = true;
108 internal FileFilter fileFilter;
110 private string lastFolder = String.Empty;
114 private readonly string filedialog_string = "FileDialog";
115 private readonly string lastfolder_string = "LastFolder";
116 private readonly string width_string = "Width";
117 private readonly string height_string = "Height";
118 private readonly string filenames_string = "FileNames";
119 private readonly string x_string = "X";
120 private readonly string y_string = "Y";
122 private bool disable_form_closed_event = false;
124 internal FileDialog ()
128 Size formConfigSize = Size.Empty;
129 Point formConfigLocation = Point.Empty;
130 string[] configFileNames = null;
132 object formWidth = MWFConfig.GetValue (filedialog_string, width_string);
134 object formHeight = MWFConfig.GetValue (filedialog_string, height_string);
136 if (formHeight != null && formWidth != null)
137 formConfigSize = new Size ((int)formWidth, (int)formHeight);
139 object formLocationX = MWFConfig.GetValue (filedialog_string, x_string);
140 object formLocationY = MWFConfig.GetValue (filedialog_string, y_string);
142 if (formLocationX != null && formLocationY != null)
143 formConfigLocation = new Point ((int)formLocationX, (int)formLocationY);
145 configFileNames = (string[])MWFConfig.GetValue (filedialog_string, filenames_string);
147 fileTypeComboBox = new ComboBox ();
148 backToolBarButton = new ToolBarButton ();
149 newdirToolBarButton = new ToolBarButton ();
150 searchSaveLabel = new Label ();
151 mwfFileView = new MWFFileView (vfs);
152 fileNameLabel = new Label ();
153 fileNameComboBox = new ComboBox ();
154 dirComboBox = new DirComboBox (vfs);
155 smallButtonToolBar = new ToolBar ();
156 menueToolBarButton = new ToolBarButton ();
157 fileTypeLabel = new Label ();
158 openSaveButton = new Button ();
159 form.AcceptButton = openSaveButton;
160 helpButton = new Button ();
161 popupButtonPanel = new PopupButtonPanel ();
162 upToolBarButton = new ToolBarButton ();
163 cancelButton = new Button ();
164 form.CancelButton = cancelButton;
165 imageListTopToolbar = new ImageList ();
166 menueToolBarButtonContextMenu = new ContextMenu ();
167 readonlyCheckBox = new CheckBox ();
169 form.SuspendLayout ();
171 //imageListTopToolbar
172 imageListTopToolbar.ColorDepth = ColorDepth.Depth32Bit;
173 imageListTopToolbar.ImageSize = new Size (16, 16); // 16, 16
174 imageListTopToolbar.Images.Add (ResourceImageLoader.Get ("go-previous.png"));
175 imageListTopToolbar.Images.Add (ResourceImageLoader.Get ("go-top.png"));
176 imageListTopToolbar.Images.Add (ResourceImageLoader.Get ("folder-new.png"));
177 imageListTopToolbar.Images.Add (ResourceImageLoader.Get ("preferences-system-windows.png"));
178 imageListTopToolbar.TransparentColor = Color.Transparent;
181 searchSaveLabel.FlatStyle = FlatStyle.System;
182 searchSaveLabel.Location = new Point (7, 8);
183 searchSaveLabel.Size = new Size (72, 21);
184 searchSaveLabel.TextAlign = ContentAlignment.MiddleRight;
187 dirComboBox.Anchor = ((AnchorStyles)(((AnchorStyles.Top | AnchorStyles.Left) | AnchorStyles.Right)));
188 dirComboBox.DropDownStyle = ComboBoxStyle.DropDownList;
189 dirComboBox.Location = new Point (99, 8);
190 dirComboBox.Size = new Size (260, 21);
191 dirComboBox.TabIndex = 7;
193 // smallButtonToolBar
194 smallButtonToolBar.Anchor = ((AnchorStyles)((AnchorStyles.Top | AnchorStyles.Right)));
195 smallButtonToolBar.Appearance = ToolBarAppearance.Flat;
196 smallButtonToolBar.AutoSize = false;
197 smallButtonToolBar.Buttons.AddRange (new ToolBarButton [] {
201 menueToolBarButton});
202 smallButtonToolBar.ButtonSize = new Size (24, 24); // 21, 16
203 smallButtonToolBar.Divider = false;
204 smallButtonToolBar.Dock = DockStyle.None;
205 smallButtonToolBar.DropDownArrows = true;
206 smallButtonToolBar.ImageList = imageListTopToolbar;
207 smallButtonToolBar.Location = new Point (372, 6);
208 smallButtonToolBar.ShowToolTips = true;
209 smallButtonToolBar.Size = new Size (140, 28);
210 smallButtonToolBar.TabIndex = 8;
211 smallButtonToolBar.TextAlign = ToolBarTextAlign.Right;
214 popupButtonPanel.Dock = DockStyle.None;
215 popupButtonPanel.Location = new Point (7, 37);
216 popupButtonPanel.TabIndex = 9;
219 mwfFileView.Anchor = ((AnchorStyles)((((AnchorStyles.Top | AnchorStyles.Bottom) | AnchorStyles.Left) | AnchorStyles.Right)));
220 mwfFileView.Location = new Point (99, 37);
221 mwfFileView.Size = new Size (449, 282);
222 mwfFileView.Columns.Add (" Name", 170, HorizontalAlignment.Left);
223 mwfFileView.Columns.Add ("Size ", 80, HorizontalAlignment.Right);
224 mwfFileView.Columns.Add (" Type", 100, HorizontalAlignment.Left);
225 mwfFileView.Columns.Add (" Last Access", 150, HorizontalAlignment.Left);
226 mwfFileView.AllowColumnReorder = true;
227 mwfFileView.MultiSelect = false;
228 mwfFileView.TabIndex = 10;
229 mwfFileView.RegisterSender (dirComboBox);
230 mwfFileView.RegisterSender (popupButtonPanel);
233 fileNameLabel.Anchor = ((AnchorStyles)((AnchorStyles.Bottom | AnchorStyles.Left)));
234 fileNameLabel.FlatStyle = FlatStyle.System;
235 fileNameLabel.Location = new Point (102, 330);
236 fileNameLabel.Size = new Size (70, 21);
237 fileNameLabel.Text = "File name:";
238 fileNameLabel.TextAlign = ContentAlignment.MiddleLeft;
241 fileNameComboBox.Anchor = ((AnchorStyles)(((AnchorStyles.Bottom | AnchorStyles.Left) | AnchorStyles.Right)));
242 fileNameComboBox.Location = new Point (195, 330);
243 fileNameComboBox.Size = new Size (245, 21);
244 fileNameComboBox.TabIndex = 1;
245 fileNameComboBox.MaxDropDownItems = MaxFileNameItems;
247 if (configFileNames != null) {
248 foreach (string configFileName in configFileNames) {
249 if (configFileName == null || configFileName.Trim ().Length == 0)
251 // add no more than 10 items
252 if (fileNameComboBox.Items.Count >= MaxFileNameItems)
254 fileNameComboBox.Items.Add (configFileName);
259 fileTypeLabel.Anchor = ((AnchorStyles)((AnchorStyles.Bottom | AnchorStyles.Left)));
260 fileTypeLabel.FlatStyle = FlatStyle.System;
261 fileTypeLabel.Location = new Point (102, 356);
262 fileTypeLabel.Size = new Size (90, 21);
263 fileTypeLabel.Text = "Files of type:";
264 fileTypeLabel.TextAlign = ContentAlignment.MiddleLeft;
267 fileTypeComboBox.Anchor = ((AnchorStyles)(((AnchorStyles.Bottom | AnchorStyles.Left) | AnchorStyles.Right)));
268 fileTypeComboBox.DropDownStyle = ComboBoxStyle.DropDownList;
269 fileTypeComboBox.Location = new Point (195, 356);
270 fileTypeComboBox.Size = new Size (245, 21);
271 fileTypeComboBox.TabIndex = 2;
274 backToolBarButton.ImageIndex = 0;
275 backToolBarButton.Enabled = false;
276 backToolBarButton.Style = ToolBarButtonStyle.PushButton;
277 mwfFileView.AddControlToEnableDisableByDirStack (backToolBarButton);
280 upToolBarButton.ImageIndex = 1;
281 upToolBarButton.Style = ToolBarButtonStyle.PushButton;
282 mwfFileView.SetFolderUpToolBarButton (upToolBarButton);
284 // newdirToolBarButton
285 newdirToolBarButton.ImageIndex = 2;
286 newdirToolBarButton.Style = ToolBarButtonStyle.PushButton;
288 // menueToolBarButton
289 menueToolBarButton.ImageIndex = 3;
290 menueToolBarButton.DropDownMenu = menueToolBarButtonContextMenu;
291 menueToolBarButton.Style = ToolBarButtonStyle.DropDownButton;
293 // menueToolBarButtonContextMenu
294 menueToolBarButtonContextMenu.MenuItems.AddRange (mwfFileView.ViewMenuItems);
297 openSaveButton.Anchor = ((AnchorStyles)((AnchorStyles.Bottom | AnchorStyles.Right)));
298 openSaveButton.FlatStyle = FlatStyle.System;
299 openSaveButton.Location = new Point (475, 330);
300 openSaveButton.Size = new Size (72, 21);
301 openSaveButton.TabIndex = 4;
302 openSaveButton.FlatStyle = FlatStyle.System;
305 cancelButton.Anchor = ((AnchorStyles)((AnchorStyles.Bottom | AnchorStyles.Right)));
306 cancelButton.FlatStyle = FlatStyle.System;
307 cancelButton.Location = new Point (475, 356);
308 cancelButton.Size = new Size (72, 21);
309 cancelButton.TabIndex = 5;
310 cancelButton.Text = "Cancel";
311 cancelButton.FlatStyle = FlatStyle.System;
314 helpButton.Anchor = ((AnchorStyles)((AnchorStyles.Bottom | AnchorStyles.Right)));
315 helpButton.FlatStyle = FlatStyle.System;
316 helpButton.Location = new Point (475, 350);
317 helpButton.Size = new Size (72, 21);
318 helpButton.TabIndex = 6;
319 helpButton.Text = "Help";
320 helpButton.FlatStyle = FlatStyle.System;
323 readonlyCheckBox.Anchor = ((AnchorStyles)(((AnchorStyles.Bottom | AnchorStyles.Left) | AnchorStyles.Right)));
324 readonlyCheckBox.Text = "Open Readonly";
325 readonlyCheckBox.Location = new Point (195, 350);
326 readonlyCheckBox.Size = new Size (245, 21);
327 readonlyCheckBox.TabIndex = 3;
328 readonlyCheckBox.FlatStyle = FlatStyle.System;
330 form.SizeGripStyle = SizeGripStyle.Show;
332 form.MaximizeBox = true;
333 form.MinimizeBox = true;
334 form.FormBorderStyle = FormBorderStyle.Sizable;
335 form.MinimumSize = new Size (554, 405);
337 form.ClientSize = new Size (554, 405); // 384
339 form.Controls.Add (smallButtonToolBar);
340 form.Controls.Add (cancelButton);
341 form.Controls.Add (openSaveButton);
342 form.Controls.Add (mwfFileView);
343 form.Controls.Add (fileTypeLabel);
344 form.Controls.Add (fileNameLabel);
345 form.Controls.Add (fileTypeComboBox);
346 form.Controls.Add (fileNameComboBox);
347 form.Controls.Add (dirComboBox);
348 form.Controls.Add (searchSaveLabel);
349 form.Controls.Add (popupButtonPanel);
351 form.ResumeLayout (false);
353 if (formConfigSize != Size.Empty) {
354 form.Size = formConfigSize;
357 if (formConfigLocation != Point.Empty) {
358 form.Location = formConfigLocation;
361 openSaveButton.Click += new EventHandler (OnClickOpenSaveButton);
362 cancelButton.Click += new EventHandler (OnClickCancelButton);
363 helpButton.Click += new EventHandler (OnClickHelpButton);
365 smallButtonToolBar.ButtonClick += new ToolBarButtonClickEventHandler (OnClickSmallButtonToolBar);
367 fileTypeComboBox.SelectedIndexChanged += new EventHandler (OnSelectedIndexChangedFileTypeComboBox);
369 mwfFileView.SelectedFileChanged += new EventHandler (OnSelectedFileChangedFileView);
370 mwfFileView.ForceDialogEnd += new EventHandler (OnForceDialogEndFileView);
371 mwfFileView.SelectedFilesChanged += new EventHandler (OnSelectedFilesChangedFileView);
373 dirComboBox.DirectoryChanged += new EventHandler (OnDirectoryChangedDirComboBox);
374 popupButtonPanel.DirectoryChanged += new EventHandler (OnDirectoryChangedPopupButtonPanel);
376 readonlyCheckBox.CheckedChanged += new EventHandler (OnCheckCheckChanged);
378 form.FormClosed += new FormClosedEventHandler (OnFileDialogFormClosed);
380 form.Closed += new EventHandler (OnFileDialogFormClosed);
385 public bool AddExtension {
391 addExtension = value;
395 [DefaultValue(false)]
396 public virtual bool CheckFileExists {
398 return checkFileExists;
402 checkFileExists = value;
407 public bool CheckPathExists {
409 return checkPathExists;
413 checkPathExists = value;
418 public string DefaultExt {
420 if (defaultExt == null)
425 if (value != null && value.Length > 0) {
426 // remove leading dot
427 if (value [0] == '.')
428 value = value.Substring (1);
434 // in MS.NET it doesn't make a difference if
435 // DerefenceLinks is true or false
436 // if the selected file is a link FileDialog
437 // always returns the link
439 public bool DereferenceLinks {
441 return dereferenceLinks;
445 dereferenceLinks = value;
450 public string FileName {
452 if (fileNames == null || fileNames.Length == 0)
455 if (fileNames [0].Length == 0)
459 // skip check for illegal characters if the filename was set
460 // through FileDialog API
461 if (!checkForIllegalChars)
462 return fileNames [0];
465 // ensure filename contains only valid characters
466 Path.GetFullPath (fileNames [0]);
467 // but return filename as is
468 return fileNames [0];
474 fileNames = new string [1] { value };
476 fileNames = new string [0];
480 // skip check for illegal characters if the filename was set
481 // through FileDialog API
482 checkForIllegalChars = false;
488 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
489 public string[] FileNames {
491 if (fileNames == null || fileNames.Length == 0) {
492 return new string [0];
495 string[] new_filenames = new string [fileNames.Length];
496 fileNames.CopyTo (new_filenames, 0);
499 // skip check for illegal characters if the filename was set
500 // through FileDialog API
501 if (!checkForIllegalChars)
502 return new_filenames;
505 foreach (string fileName in new_filenames) {
506 // ensure filename contains only valid characters
507 Path.GetFullPath (fileName);
509 return new_filenames;
515 public string Filter {
523 if (fileFilter != null)
524 fileFilter.FilterArrayList.Clear ();
526 if (FileFilter.CheckFilter (value)) {
529 fileFilter = new FileFilter (filter);
531 throw new ArgumentException ("The provided filter string"
532 + " is invalid. The filter string should contain a"
533 + " description of the filter, followed by the "
534 + " vertical bar (|) and the filter pattern. The"
535 + " strings for different filtering options should"
536 + " also be separated by the vertical bar. Example:"
537 + " Text files (*.txt)|*.txt|All files (*.*)|*.*");
545 public int FilterIndex {
547 return setFilterIndex;
551 setFilterIndex = value;
563 public string InitialDirectory {
565 if (initialDirectory == null)
567 return initialDirectory;
570 initialDirectory = value;
574 [DefaultValue(false)]
575 public bool RestoreDirectory {
577 return restoreDirectory;
581 restoreDirectory = value;
585 [DefaultValue(false)]
586 public bool ShowHelp {
593 ResizeAndRelocateForHelpOrReadOnly ();
599 public string Title {
610 // this one is a hard one ;)
612 // - up to MAX_PATH characters (windef.h) = 260
613 // - no trailing dots or spaces
616 // NTFS/Posix filename:
617 // - up to 32,768 Unicode characters
618 // - trailing periods or spaces
622 public bool ValidateNames {
624 return validateNames;
628 validateNames = value;
632 public override void Reset ()
635 checkFileExists = false;
636 checkPathExists = true;
638 dereferenceLinks = true;
640 Filter = String.Empty;
642 InitialDirectory = null;
643 restoreDirectory = false;
646 validateNames = true;
651 public override string ToString ()
653 return String.Format("{0}: Title: {1}, FileName: {2}", base.ToString (), Title, FileName);
656 public event CancelEventHandler FileOk {
657 add { Events.AddHandler (EventFileOk, value); }
658 remove { Events.RemoveHandler (EventFileOk, value); }
661 protected virtual IntPtr Instance {
669 // This is just for internal use with MSs version, so it doesn't need to be implemented
670 // as it can't really be accessed anyways
671 protected int Options {
675 internal virtual string DialogTitle {
682 protected override IntPtr HookProc (IntPtr hWnd, int msg, IntPtr wparam, IntPtr lparam)
684 throw new NotImplementedException ();
687 protected void OnFileOk (CancelEventArgs e)
689 WriteConfigValues (e);
691 CancelEventHandler fo = (CancelEventHandler) Events [EventFileOk];
695 Mime.CleanFileCache ();
697 disable_form_closed_event = true;
700 protected override bool RunDialog (IntPtr hWndOwner)
703 form.Text = DialogTitle;
705 // avoid using the FileNames property to skip the invalid characters
707 string fileName = null;
708 if (fileNames != null && fileNames.Length != 0)
709 fileName = fileNames [0];
711 fileName = string.Empty;
715 SetFileAndDirectory (fileName);
716 fileNameComboBox.Select ();
721 internal virtual bool ShowReadOnly {
723 showReadOnly = value;
724 ResizeAndRelocateForHelpOrReadOnly ();
732 internal virtual bool ReadOnlyChecked {
734 readOnlyChecked = value;
735 readonlyCheckBox.Checked = value;
739 return readOnlyChecked;
743 internal bool BMultiSelect {
746 mwfFileView.MultiSelect = value;
754 internal string OpenSaveButtonText {
756 openSaveButton.Text = value;
760 internal string SearchSaveLabel {
762 searchSaveLabel.Text = value;
766 internal string FileTypeLabel {
768 fileTypeLabel.Text = value;
772 private void SelectFilter ()
774 if (mwfFileView.FilterArrayList == null || filterIndex > mwfFileView.FilterArrayList.Count)
777 do_not_call_OnSelectedIndexChangedFileTypeComboBox = true;
778 fileTypeComboBox.BeginUpdate ();
779 fileTypeComboBox.SelectedIndex = filterIndex - 1;
780 fileTypeComboBox.EndUpdate ();
781 do_not_call_OnSelectedIndexChangedFileTypeComboBox = false;
782 mwfFileView.FilterIndex = filterIndex;
785 private void SetFileAndDirectory (string fname)
787 if (fname.Length != 0) {
788 bool rooted = Path.IsPathRooted (fname);
790 mwfFileView.ChangeDirectory (null, lastFolder);
791 fileNameComboBox.Text = fname;
793 string dirname = Path.GetDirectoryName (fname);
794 if (dirname != null && dirname.Length > 0 && Directory.Exists (dirname)) {
795 fileNameComboBox.Text = Path.GetFileName (fname);
796 mwfFileView.ChangeDirectory (null, dirname);
798 fileNameComboBox.Text = fname;
799 mwfFileView.ChangeDirectory (null, lastFolder);
803 mwfFileView.ChangeDirectory (null, lastFolder);
804 fileNameComboBox.Text = null;
808 void OnClickOpenSaveButton (object sender, EventArgs e)
811 // for filenames typed or selected by user, enable check for
812 // illegal characters in filename(s)
813 checkForIllegalChars = true;
816 if (fileDialogType == FileDialogType.OpenFileDialog) {
817 ListView.SelectedListViewItemCollection sl = mwfFileView.SelectedItems;
818 if (sl.Count > 0 && sl [0] != null) {
820 FileViewListViewItem item = sl [0] as FileViewListViewItem;
821 FSEntry fsEntry = item.FSEntry;
823 if (fsEntry.Attributes == FileAttributes.Directory) {
824 mwfFileView.ChangeDirectory (null, fsEntry.FullName);
828 foreach (FileViewListViewItem item in sl) {
829 FSEntry fsEntry = item.FSEntry;
830 if (fsEntry.Attributes == FileAttributes.Directory) {
831 mwfFileView.ChangeDirectory (null, fsEntry.FullName);
839 string internalfullfilename = String.Empty;
842 string fileFromComboBox = fileNameComboBox.Text.Trim ();
844 if (fileFromComboBox.Length > 0) {
845 if (!Path.IsPathRooted (fileFromComboBox)) {
846 // on unix currentRealFolder for "Recently used files" is null,
847 // because recently used files don't get saved as links in a directory
848 // recently used files get saved in a xml file
849 if (mwfFileView.CurrentRealFolder != null)
850 fileFromComboBox = Path.Combine (mwfFileView.CurrentRealFolder, fileFromComboBox);
852 if (mwfFileView.CurrentFSEntry != null) {
853 fileFromComboBox = mwfFileView.CurrentFSEntry.FullName;
857 FileInfo fileInfo = new FileInfo (fileFromComboBox);
859 if (fileInfo.Exists || fileDialogType == FileDialogType.SaveFileDialog) {
860 internalfullfilename = fileFromComboBox;
862 DirectoryInfo dirInfo = new DirectoryInfo (fileFromComboBox);
863 if (dirInfo.Exists) {
864 mwfFileView.ChangeDirectory (null, dirInfo.FullName);
865 fileNameComboBox.Text = null;
868 internalfullfilename = fileFromComboBox;
874 if (fileDialogType == FileDialogType.OpenFileDialog) {
875 if (checkFileExists) {
876 if (!File.Exists (internalfullfilename)) {
877 string message = "\"" + internalfullfilename + "\" doesn't exist. Please verify that you have entered the correct file name.";
878 MessageBox.Show (message, openSaveButton.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
884 if (overwritePrompt) {
885 if (File.Exists (internalfullfilename)) {
886 string message = "\"" + internalfullfilename + "\" exists. Overwrite ?";
887 DialogResult dr = MessageBox.Show (message, openSaveButton.Text, MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
889 if (dr == DialogResult.Cancel)
895 if (!File.Exists (internalfullfilename)) {
896 string message = "\"" + internalfullfilename + "\" doesn't exist. Create ?";
897 DialogResult dr = MessageBox.Show (message, openSaveButton.Text, MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
899 if (dr == DialogResult.Cancel)
905 if (fileDialogType == FileDialogType.SaveFileDialog) {
907 string extension_to_use = String.Empty;
908 string filter_exentsion = String.Empty;
910 if (fileFilter != null) {
911 FilterStruct filterstruct = (FilterStruct)fileFilter.FilterArrayList [filterIndex - 1];
913 for (int i = 0; i < filterstruct.filters.Count; i++) {
914 string extension = filterstruct.filters [i];
916 if (extension.StartsWith ("*"))
917 extension = extension.Remove (0, 1);
919 if (extension.IndexOf ('*') != -1)
922 filter_exentsion = extension;
927 if (filter_exentsion != String.Empty)
928 extension_to_use = filter_exentsion;
930 if (DefaultExt.Length > 0)
931 extension_to_use = "." + DefaultExt;
933 if (!internalfullfilename.EndsWith (extension_to_use))
934 internalfullfilename += extension_to_use;
938 // do not use the FileName setter internally, as this will
939 // cause the illegal character check to be skipped, and we
940 // do not want this for filename users have entered
941 fileNames = new string [1] { internalfullfilename };
943 mwfFileView.WriteRecentlyUsed (internalfullfilename);
944 } else // multiSelect = true
945 if (fileDialogType != FileDialogType.SaveFileDialog) {
946 if (mwfFileView.SelectedItems.Count > 0) {
947 // first remove all selected directories
948 ArrayList al = new ArrayList ();
950 foreach (FileViewListViewItem lvi in mwfFileView.SelectedItems) {
951 FSEntry fsEntry = lvi.FSEntry;
953 if (fsEntry.Attributes != FileAttributes.Directory) {
958 fileNames = new string [al.Count];
960 for (int i = 0; i < al.Count; i++) {
961 fileNames [i] = ((FSEntry)al [i]).FullName;
966 if (checkPathExists && mwfFileView.CurrentRealFolder != null) {
967 if (!Directory.Exists (mwfFileView.CurrentRealFolder)) {
968 string message = "\"" + mwfFileView.CurrentRealFolder + "\" doesn't exist. Please verify that you have entered the correct directory name.";
969 MessageBox.Show (message, openSaveButton.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
971 if (InitialDirectory.Length == 0 || !Directory.Exists (InitialDirectory))
972 mwfFileView.ChangeDirectory (null, lastFolder);
974 mwfFileView.ChangeDirectory (null, InitialDirectory);
979 if (restoreDirectory) {
980 lastFolder = restoreDirectoryString;
982 lastFolder = mwfFileView.CurrentFolder;
985 foreach (string fileName in fileNames) {
986 if (!File.Exists (fileName))
987 // ignore files that do not exist
990 if (fileNameComboBox.Items.IndexOf (fileName) == -1)
991 fileNameComboBox.Items.Insert (0, fileName);
994 // remove items above the maximum items that we want to display
995 while (fileNameComboBox.Items.Count > MaxFileNameItems)
996 fileNameComboBox.Items.RemoveAt (MaxFileNameItems);
998 CancelEventArgs cancelEventArgs = new CancelEventArgs ();
1000 cancelEventArgs.Cancel = false;
1002 OnFileOk (cancelEventArgs);
1004 form.DialogResult = DialogResult.OK;
1007 void OnClickCancelButton (object sender, EventArgs e)
1009 if (restoreDirectory)
1010 mwfFileView.CurrentFolder = restoreDirectoryString;
1012 CancelEventArgs cancelEventArgs = new CancelEventArgs ();
1014 cancelEventArgs.Cancel = true;
1016 OnFileOk (cancelEventArgs);
1018 form.DialogResult = DialogResult.Cancel;
1021 void OnClickHelpButton (object sender, EventArgs e)
1026 void OnClickSmallButtonToolBar (object sender, ToolBarButtonClickEventArgs e)
1028 if (e.Button == upToolBarButton) {
1029 mwfFileView.OneDirUp ();
1031 if (e.Button == backToolBarButton) {
1032 mwfFileView.PopDir ();
1034 if (e.Button == newdirToolBarButton) {
1035 mwfFileView.CreateNewFolder ();
1039 void OnSelectedIndexChangedFileTypeComboBox (object sender, EventArgs e)
1041 if (do_not_call_OnSelectedIndexChangedFileTypeComboBox) {
1042 do_not_call_OnSelectedIndexChangedFileTypeComboBox = false;
1046 filterIndex = fileTypeComboBox.SelectedIndex + 1;
1048 mwfFileView.FilterIndex = filterIndex;
1051 void OnSelectedFileChangedFileView (object sender, EventArgs e)
1053 fileNameComboBox.Text = mwfFileView.CurrentFSEntry.Name;
1056 void OnSelectedFilesChangedFileView (object sender, EventArgs e)
1058 string selectedFiles = mwfFileView.SelectedFilesString;
1059 if (selectedFiles != null && selectedFiles.Length != 0)
1060 fileNameComboBox.Text = selectedFiles;
1063 void OnForceDialogEndFileView (object sender, EventArgs e)
1065 OnClickOpenSaveButton (this, EventArgs.Empty);
1068 void OnDirectoryChangedDirComboBox (object sender, EventArgs e)
1070 mwfFileView.ChangeDirectory (sender, dirComboBox.CurrentFolder);
1073 void OnDirectoryChangedPopupButtonPanel (object sender, EventArgs e)
1075 mwfFileView.ChangeDirectory (sender, popupButtonPanel.CurrentFolder);
1078 void OnCheckCheckChanged (object sender, EventArgs e)
1080 ReadOnlyChecked = readonlyCheckBox.Checked;
1084 void OnFileDialogFormClosed (object sender, FormClosedEventArgs e)
1086 HandleFormClosedEvent (sender);
1089 void OnFileDialogFormClosed (object sender, EventArgs e)
1091 HandleFormClosedEvent (sender);
1095 void HandleFormClosedEvent (object sender)
1097 if (!disable_form_closed_event)
1098 OnClickCancelButton (sender, EventArgs.Empty);
1100 disable_form_closed_event = false;
1103 private void UpdateFilters ()
1105 if (fileFilter == null)
1106 fileFilter = new FileFilter ();
1108 ArrayList filters = fileFilter.FilterArrayList;
1110 fileTypeComboBox.BeginUpdate ();
1112 fileTypeComboBox.Items.Clear ();
1114 foreach (FilterStruct fs in filters) {
1115 fileTypeComboBox.Items.Add (fs.filterName);
1118 if (filters.Count > 0 && FilterIndex <= filters.Count)
1119 fileTypeComboBox.SelectedIndex = filterIndex - 1;
1121 fileTypeComboBox.EndUpdate ();
1123 mwfFileView.FilterArrayList = filters;
1125 mwfFileView.FilterIndex = filterIndex;
1128 private void ResizeAndRelocateForHelpOrReadOnly ()
1130 form.SuspendLayout ();
1131 if (ShowHelp || ShowReadOnly) {
1132 mwfFileView.Size = new Size (449, 250);
1133 fileNameLabel.Location = new Point (102, 298);
1134 fileNameComboBox.Location = new Point (195, 298);
1135 fileTypeLabel.Location = new Point (102, 324);
1136 fileTypeComboBox.Location = new Point (195, 324);
1137 openSaveButton.Location = new Point (475, 298);
1138 cancelButton.Location = new Point (475, 324);
1140 mwfFileView.Size = new Size (449, 282);
1141 fileNameLabel.Location = new Point (102, 330);
1142 fileNameComboBox.Location = new Point (195, 330);
1143 fileTypeLabel.Location = new Point (102, 356);
1144 fileTypeComboBox.Location = new Point (195, 356);
1145 openSaveButton.Location = new Point (475, 330);
1146 cancelButton.Location = new Point (475, 356);
1150 form.Controls.Add (helpButton);
1152 form.Controls.Remove (helpButton);
1155 form.Controls.Add (readonlyCheckBox);
1157 form.Controls.Remove (readonlyCheckBox);
1158 form.ResumeLayout ();
1161 private void WriteConfigValues (CancelEventArgs ce)
1163 MWFConfig.SetValue (filedialog_string, width_string, form.Width);
1164 MWFConfig.SetValue (filedialog_string, height_string, form.Height);
1165 MWFConfig.SetValue (filedialog_string, x_string, form.Location.X);
1166 MWFConfig.SetValue (filedialog_string, y_string, form.Location.Y);
1169 MWFConfig.SetValue (filedialog_string, lastfolder_string, lastFolder);
1171 string[] fileNameCBItems = new string [fileNameComboBox.Items.Count];
1173 fileNameComboBox.Items.CopyTo (fileNameCBItems, 0);
1175 MWFConfig.SetValue (filedialog_string, filenames_string, fileNameCBItems);
1179 private void ReadConfigValues ()
1181 lastFolder = (string)MWFConfig.GetValue (filedialog_string, lastfolder_string);
1183 if (lastFolder != null && lastFolder.IndexOf ("://") == -1) {
1184 if (!Directory.Exists (lastFolder)) {
1185 lastFolder = MWFVFS.DesktopPrefix;
1189 if (InitialDirectory.Length > 0 && Directory.Exists (InitialDirectory))
1190 lastFolder = InitialDirectory;
1192 if (lastFolder == null || lastFolder.Length == 0)
1193 lastFolder = Environment.CurrentDirectory;
1195 if (RestoreDirectory)
1196 restoreDirectoryString = lastFolder;
1201 #region PopupButtonPanel
1202 internal class PopupButtonPanel : Control, IUpdateFolder
1205 internal class PopupButton : Control
1207 internal enum PopupButtonState
1210 private Image image = null;
1211 private PopupButtonState popupButtonState = PopupButtonState.Normal;
1212 private StringFormat text_format = new StringFormat();
1213 private Rectangle text_rect = Rectangle.Empty;
1215 public PopupButton ()
1217 text_format.Alignment = StringAlignment.Center;
1218 text_format.LineAlignment = StringAlignment.Near;
1220 SetStyle (ControlStyles.DoubleBuffer, true);
1221 SetStyle (ControlStyles.AllPaintingInWmPaint, true);
1222 SetStyle (ControlStyles.UserPaint, true);
1223 SetStyle (ControlStyles.Selectable, false);
1226 public Image Image {
1237 public PopupButtonState ButtonState {
1239 popupButtonState = value;
1244 return popupButtonState;
1248 protected override void OnPaint (PaintEventArgs pe)
1255 private void Draw (PaintEventArgs pe)
1257 Graphics gr = pe.Graphics;
1259 gr.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (BackColor), ClientRectangle);
1262 if (image != null) {
1263 int i_x = (ClientSize.Width - image.Width) / 2;
1265 gr.DrawImage (image, i_x, i_y);
1268 if (Text != String.Empty) {
1269 if (text_rect == Rectangle.Empty)
1270 text_rect = new Rectangle (0, Height - 30, Width, Height - 30);
1272 gr.DrawString (Text, Font, Brushes.White, text_rect, text_format);
1275 switch (popupButtonState) {
1276 case PopupButtonState.Up:
1277 gr.DrawLine (ThemeEngine.Current.ResPool.GetPen (Color.White), 0, 0, ClientSize.Width - 1, 0);
1278 gr.DrawLine (ThemeEngine.Current.ResPool.GetPen (Color.White), 0, 0, 0, ClientSize.Height - 1);
1279 gr.DrawLine (ThemeEngine.Current.ResPool.GetPen (Color.Black), ClientSize.Width - 1, 0, ClientSize.Width - 1, ClientSize.Height - 1);
1280 gr.DrawLine (ThemeEngine.Current.ResPool.GetPen (Color.Black), 0, ClientSize.Height - 1, ClientSize.Width - 1, ClientSize.Height - 1);
1283 case PopupButtonState.Down:
1284 gr.DrawLine (ThemeEngine.Current.ResPool.GetPen (Color.Black), 0, 0, ClientSize.Width - 1, 0);
1285 gr.DrawLine (ThemeEngine.Current.ResPool.GetPen (Color.Black), 0, 0, 0, ClientSize.Height - 1);
1286 gr.DrawLine (ThemeEngine.Current.ResPool.GetPen (Color.White), ClientSize.Width - 1, 0, ClientSize.Width - 1, ClientSize.Height - 1);
1287 gr.DrawLine (ThemeEngine.Current.ResPool.GetPen (Color.White), 0, ClientSize.Height - 1, ClientSize.Width - 1, ClientSize.Height - 1);
1292 protected override void OnMouseEnter (EventArgs e)
1294 if (popupButtonState != PopupButtonState.Down)
1295 popupButtonState = PopupButtonState.Up;
1297 PopupButtonPanel panel = Parent as PopupButtonPanel;
1299 if (panel.focusButton != null && panel.focusButton.ButtonState == PopupButtonState.Up) {
1300 panel.focusButton.ButtonState = PopupButtonState.Normal;
1301 panel.focusButton = null;
1304 base.OnMouseEnter (e);
1307 protected override void OnMouseLeave (EventArgs e)
1309 if (popupButtonState == PopupButtonState.Up)
1310 popupButtonState = PopupButtonState.Normal;
1312 base.OnMouseLeave (e);
1315 protected override void OnClick (EventArgs e)
1317 popupButtonState = PopupButtonState.Down;
1324 private PopupButton recentlyusedButton;
1325 private PopupButton desktopButton;
1326 private PopupButton personalButton;
1327 private PopupButton mycomputerButton;
1328 private PopupButton networkButton;
1330 private PopupButton lastPopupButton = null;
1331 private PopupButton focusButton = null;
1333 private string currentPath;
1335 private int currentFocusIndex;
1337 public PopupButtonPanel ()
1341 BackColor = Color.FromArgb (128, 128, 128);
1342 Size = new Size (85, 336);
1344 recentlyusedButton = new PopupButton ();
1345 desktopButton = new PopupButton ();
1346 personalButton = new PopupButton ();
1347 mycomputerButton = new PopupButton ();
1348 networkButton = new PopupButton ();
1350 recentlyusedButton.Size = new Size (81, 64);
1351 recentlyusedButton.Image = ThemeEngine.Current.Images (UIIcon.PlacesRecentDocuments, 32);
1352 recentlyusedButton.BackColor = BackColor;
1353 recentlyusedButton.ForeColor = Color.Black;
1354 recentlyusedButton.Location = new Point (2, 2);
1355 recentlyusedButton.Text = "Recently\nused";
1356 recentlyusedButton.Click += new EventHandler (OnClickButton);
1358 desktopButton.Image = ThemeEngine.Current.Images (UIIcon.PlacesDesktop, 32);
1359 desktopButton.BackColor = BackColor;
1360 desktopButton.ForeColor = Color.Black;
1361 desktopButton.Size = new Size (81, 64);
1362 desktopButton.Location = new Point (2, 66);
1363 desktopButton.Text = "Desktop";
1364 desktopButton.Click += new EventHandler (OnClickButton);
1366 personalButton.Image = ThemeEngine.Current.Images (UIIcon.PlacesPersonal, 32);
1367 personalButton.BackColor = BackColor;
1368 personalButton.ForeColor = Color.Black;
1369 personalButton.Size = new Size (81, 64);
1370 personalButton.Location = new Point (2, 130);
1371 personalButton.Text = "Personal";
1372 personalButton.Click += new EventHandler (OnClickButton);
1374 mycomputerButton.Image = ThemeEngine.Current.Images (UIIcon.PlacesMyComputer, 32);
1375 mycomputerButton.BackColor = BackColor;
1376 mycomputerButton.ForeColor = Color.Black;
1377 mycomputerButton.Size = new Size (81, 64);
1378 mycomputerButton.Location = new Point (2, 194);
1379 mycomputerButton.Text = "My Computer";
1380 mycomputerButton.Click += new EventHandler (OnClickButton);
1382 networkButton.Image = ThemeEngine.Current.Images (UIIcon.PlacesMyNetwork, 32);
1383 networkButton.BackColor = BackColor;
1384 networkButton.ForeColor = Color.Black;
1385 networkButton.Size = new Size (81, 64);
1386 networkButton.Location = new Point (2, 258);
1387 networkButton.Text = "My Network";
1388 networkButton.Click += new EventHandler (OnClickButton);
1390 Controls.Add (recentlyusedButton);
1391 Controls.Add (desktopButton);
1392 Controls.Add (personalButton);
1393 Controls.Add (mycomputerButton);
1394 Controls.Add (networkButton);
1396 ResumeLayout (false);
1398 KeyDown += new KeyEventHandler (Key_Down);
1400 SetStyle (ControlStyles.StandardClick, false);
1403 void OnClickButton (object sender, EventArgs e)
1405 if (lastPopupButton != null && lastPopupButton != sender as PopupButton)
1406 lastPopupButton.ButtonState = PopupButton.PopupButtonState.Normal;
1407 lastPopupButton = sender as PopupButton;
1409 if (sender == recentlyusedButton) {
1410 currentPath = MWFVFS.RecentlyUsedPrefix;
1412 if (sender == desktopButton) {
1413 currentPath = MWFVFS.DesktopPrefix;
1415 if (sender == personalButton) {
1416 currentPath = MWFVFS.PersonalPrefix;
1418 if (sender == mycomputerButton) {
1419 currentPath = MWFVFS.MyComputerPrefix;
1421 if (sender == networkButton) {
1422 currentPath = MWFVFS.MyNetworkPrefix;
1425 EventHandler eh = (EventHandler)(Events [PDirectoryChangedEvent]);
1427 eh (this, EventArgs.Empty);
1430 public string CurrentFolder {
1432 string currentPath = value;
1433 if (currentPath == MWFVFS.RecentlyUsedPrefix) {
1434 if (lastPopupButton != recentlyusedButton) {
1435 if (lastPopupButton != null)
1436 lastPopupButton.ButtonState = PopupButton.PopupButtonState.Normal;
1437 recentlyusedButton.ButtonState = PopupButton.PopupButtonState.Down;
1438 lastPopupButton = recentlyusedButton;
1441 if (currentPath == MWFVFS.DesktopPrefix) {
1442 if (lastPopupButton != desktopButton) {
1443 if (lastPopupButton != null)
1444 lastPopupButton.ButtonState = PopupButton.PopupButtonState.Normal;
1445 desktopButton.ButtonState = PopupButton.PopupButtonState.Down;
1446 lastPopupButton = desktopButton;
1449 if (currentPath == MWFVFS.PersonalPrefix) {
1450 if (lastPopupButton != personalButton) {
1451 if (lastPopupButton != null)
1452 lastPopupButton.ButtonState = PopupButton.PopupButtonState.Normal;
1453 personalButton.ButtonState = PopupButton.PopupButtonState.Down;
1454 lastPopupButton = personalButton;
1457 if (currentPath == MWFVFS.MyComputerPrefix) {
1458 if (lastPopupButton != mycomputerButton) {
1459 if (lastPopupButton != null)
1460 lastPopupButton.ButtonState = PopupButton.PopupButtonState.Normal;
1461 mycomputerButton.ButtonState = PopupButton.PopupButtonState.Down;
1462 lastPopupButton = mycomputerButton;
1465 if (currentPath == MWFVFS.MyNetworkPrefix) {
1466 if (lastPopupButton != networkButton) {
1467 if (lastPopupButton != null)
1468 lastPopupButton.ButtonState = PopupButton.PopupButtonState.Normal;
1469 networkButton.ButtonState = PopupButton.PopupButtonState.Down;
1470 lastPopupButton = networkButton;
1473 if (lastPopupButton != null) {
1474 lastPopupButton.ButtonState = PopupButton.PopupButtonState.Normal;
1475 lastPopupButton = null;
1484 protected override void OnPaint (PaintEventArgs e)
1486 ControlPaint.DrawBorder3D (e.Graphics, ClientRectangle, Border3DStyle.Sunken);
1490 protected override void OnGotFocus (EventArgs e)
1492 if (lastPopupButton != recentlyusedButton) {
1493 recentlyusedButton.ButtonState = PopupButton.PopupButtonState.Up;
1494 focusButton = recentlyusedButton;
1496 currentFocusIndex = 0;
1498 base.OnGotFocus (e);
1501 protected override void OnLostFocus (EventArgs e)
1503 if (focusButton != null && focusButton.ButtonState != PopupButton.PopupButtonState.Down)
1504 focusButton.ButtonState = PopupButton.PopupButtonState.Normal;
1505 base.OnLostFocus (e);
1508 protected override bool IsInputKey (Keys key)
1518 return base.IsInputKey (key);
1521 private void Key_Down (object sender, KeyEventArgs e)
1523 bool update_focus = false;
1525 if (e.KeyCode == Keys.Left || e.KeyCode == Keys.Up) {
1526 currentFocusIndex --;
1528 if (currentFocusIndex < 0)
1529 currentFocusIndex = Controls.Count - 1;
1531 update_focus = true;
1533 if (e.KeyCode == Keys.Down || e.KeyCode == Keys.Right) {
1534 currentFocusIndex++;
1536 if (currentFocusIndex == Controls.Count)
1537 currentFocusIndex = 0;
1539 update_focus = true;
1541 if (e.KeyCode == Keys.Enter) {
1542 focusButton.ButtonState = PopupButton.PopupButtonState.Down;
1543 OnClickButton (focusButton, EventArgs.Empty);
1547 PopupButton newfocusButton = Controls [currentFocusIndex] as PopupButton;
1548 if (focusButton != null && focusButton.ButtonState != PopupButton.PopupButtonState.Down)
1549 focusButton.ButtonState = PopupButton.PopupButtonState.Normal;
1550 if (newfocusButton.ButtonState != PopupButton.PopupButtonState.Down)
1551 newfocusButton.ButtonState = PopupButton.PopupButtonState.Up;
1552 focusButton = newfocusButton;
1558 static object PDirectoryChangedEvent = new object ();
1560 public event EventHandler DirectoryChanged {
1561 add { Events.AddHandler (PDirectoryChangedEvent, value); }
1562 remove { Events.RemoveHandler (PDirectoryChangedEvent, value); }
1568 internal class DirComboBox : ComboBox, IUpdateFolder
1570 #region DirComboBoxItem
1571 internal class DirComboBoxItem
1573 private int imageIndex;
1574 private string name;
1575 private string path;
1577 private ImageList imageList;
1579 public DirComboBoxItem (ImageList imageList, int imageIndex, string name, string path, int xPos)
1581 this.imageList = imageList;
1582 this.imageIndex = imageIndex;
1588 public int ImageIndex {
1594 public string Name {
1600 public string Path {
1612 public ImageList ImageList {
1624 private ImageList imageList = new ImageList();
1626 private string currentPath;
1628 private Stack folderStack = new Stack();
1630 private static readonly int indent = 6;
1632 private DirComboBoxItem recentlyUsedDirComboboxItem;
1633 private DirComboBoxItem desktopDirComboboxItem;
1634 private DirComboBoxItem personalDirComboboxItem;
1635 private DirComboBoxItem myComputerDirComboboxItem;
1636 private DirComboBoxItem networkDirComboboxItem;
1638 private ArrayList myComputerItems = new ArrayList ();
1640 private DirComboBoxItem mainParentDirComboBoxItem = null;
1641 private DirComboBoxItem real_parent = null;
1645 public DirComboBox (MWFVFS vfs)
1651 DrawMode = DrawMode.OwnerDrawFixed;
1653 imageList.ColorDepth = ColorDepth.Depth32Bit;
1654 imageList.ImageSize = new Size (16, 16);
1655 imageList.Images.Add (ThemeEngine.Current.Images (UIIcon.PlacesRecentDocuments, 16));
1656 imageList.Images.Add (ThemeEngine.Current.Images (UIIcon.PlacesDesktop, 16));
1657 imageList.Images.Add (ThemeEngine.Current.Images (UIIcon.PlacesPersonal, 16));
1658 imageList.Images.Add (ThemeEngine.Current.Images (UIIcon.PlacesMyComputer, 16));
1659 imageList.Images.Add (ThemeEngine.Current.Images (UIIcon.PlacesMyNetwork, 16));
1660 imageList.Images.Add (ThemeEngine.Current.Images (UIIcon.NormalFolder, 16));
1661 imageList.TransparentColor = Color.Transparent;
1663 recentlyUsedDirComboboxItem = new DirComboBoxItem (imageList, 0, "Recently used", MWFVFS.RecentlyUsedPrefix, 0);
1664 desktopDirComboboxItem = new DirComboBoxItem (imageList, 1, "Desktop", MWFVFS.DesktopPrefix, 0);
1665 personalDirComboboxItem = new DirComboBoxItem (imageList, 2, "Personal folder", MWFVFS.PersonalPrefix, indent);
1666 myComputerDirComboboxItem = new DirComboBoxItem (imageList, 3, "My Computer", MWFVFS.MyComputerPrefix, indent);
1667 networkDirComboboxItem = new DirComboBoxItem (imageList, 4, "My Network", MWFVFS.MyNetworkPrefix, indent);
1669 ArrayList al = this.vfs.GetMyComputerContent ();
1671 foreach (FSEntry fsEntry in al) {
1672 myComputerItems.Add (new DirComboBoxItem (MimeIconEngine.LargeIcons, fsEntry.IconIndex, fsEntry.Name, fsEntry.FullName, indent * 2));
1678 mainParentDirComboBoxItem = myComputerDirComboboxItem;
1680 ResumeLayout (false);
1683 public string CurrentFolder {
1685 currentPath = value;
1694 private void CreateComboList ()
1697 DirComboBoxItem selection = null;
1699 if (currentPath == MWFVFS.RecentlyUsedPrefix) {
1700 mainParentDirComboBoxItem = recentlyUsedDirComboboxItem;
1701 selection = recentlyUsedDirComboboxItem;
1703 if (currentPath == MWFVFS.DesktopPrefix) {
1704 selection = desktopDirComboboxItem;
1705 mainParentDirComboBoxItem = desktopDirComboboxItem;
1707 if (currentPath == MWFVFS.PersonalPrefix) {
1708 selection = personalDirComboboxItem;
1709 mainParentDirComboBoxItem = personalDirComboboxItem;
1711 if (currentPath == MWFVFS.MyComputerPrefix) {
1712 selection = myComputerDirComboboxItem;
1713 mainParentDirComboBoxItem = myComputerDirComboboxItem;
1715 if (currentPath == MWFVFS.MyNetworkPrefix) {
1716 selection = networkDirComboboxItem;
1717 mainParentDirComboBoxItem = networkDirComboboxItem;
1719 foreach (DirComboBoxItem dci in myComputerItems) {
1720 if (dci.Path == currentPath) {
1721 mainParentDirComboBoxItem = selection = dci;
1731 Items.Add (recentlyUsedDirComboboxItem);
1732 Items.Add (desktopDirComboboxItem);
1733 Items.Add (personalDirComboboxItem);
1734 Items.Add (myComputerDirComboboxItem);
1735 Items.AddRange (myComputerItems);
1736 Items.Add (networkDirComboboxItem);
1738 if (selection == null) {
1739 real_parent = CreateFolderStack ();
1742 if (real_parent != null) {
1743 int local_indent = 0;
1745 if (real_parent == desktopDirComboboxItem)
1748 if (real_parent == personalDirComboboxItem || real_parent == networkDirComboboxItem)
1753 selection = AppendToParent (local_indent, real_parent);
1758 if (selection != null)
1759 SelectedItem = selection;
1762 private DirComboBoxItem CreateFolderStack ()
1764 folderStack.Clear ();
1766 DirectoryInfo di = new DirectoryInfo (currentPath);
1768 folderStack.Push (di);
1770 while (di.Parent != null) {
1773 if (mainParentDirComboBoxItem != personalDirComboboxItem && di.FullName == ThemeEngine.Current.Places (UIIcon.PlacesDesktop))
1774 return desktopDirComboboxItem;
1776 if (mainParentDirComboBoxItem == personalDirComboboxItem) {
1777 if (di.FullName == ThemeEngine.Current.Places (UIIcon.PlacesPersonal))
1778 return personalDirComboboxItem;
1780 foreach (DirComboBoxItem dci in myComputerItems) {
1781 if (dci.Path == di.FullName) {
1787 folderStack.Push (di);
1793 private DirComboBoxItem AppendToParent (int nr_indents, DirComboBoxItem parentDirComboBoxItem)
1795 DirComboBoxItem selection = null;
1797 int index = Items.IndexOf (parentDirComboBoxItem) + 1;
1799 int xPos = indent * nr_indents;
1801 while (folderStack.Count != 0) {
1802 DirectoryInfo dii = folderStack.Pop () as DirectoryInfo;
1804 DirComboBoxItem dci = new DirComboBoxItem (imageList, 5, dii.Name, dii.FullName, xPos);
1806 Items.Insert (index, dci);
1815 protected override void OnDrawItem (DrawItemEventArgs e)
1820 DirComboBoxItem dcbi = Items [e.Index] as DirComboBoxItem;
1822 Bitmap bmp = new Bitmap (e.Bounds.Width, e.Bounds.Height, e.Graphics);
1823 Graphics gr = Graphics.FromImage (bmp);
1825 Color backColor = e.BackColor;
1826 Color foreColor = e.ForeColor;
1828 int xPos = dcbi.XPos;
1830 if ((e.State & DrawItemState.ComboBoxEdit) != 0)
1833 gr.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (backColor),
1834 new Rectangle (0, 0, bmp.Width, bmp.Height));
1836 if ((e.State & DrawItemState.Selected) == DrawItemState.Selected &&
1837 (!DroppedDown || (e.State & DrawItemState.ComboBoxEdit) != DrawItemState.ComboBoxEdit)) {
1838 foreColor = ThemeEngine.Current.ColorHighlightText;
1840 int w = (int) gr.MeasureString (dcbi.Name, e.Font).Width;
1842 gr.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (ThemeEngine.Current.ColorHighlight),
1843 new Rectangle (xPos + 23, 1, w + 3, e.Bounds.Height - 2));
1844 if ((e.State & DrawItemState.Focus) == DrawItemState.Focus) {
1845 ControlPaint.DrawFocusRectangle (gr, new Rectangle (xPos + 22, 0, w + 5,
1846 e.Bounds.Height), foreColor, ThemeEngine.Current.ColorHighlight);
1850 gr.DrawString (dcbi.Name, e.Font , ThemeEngine.Current.ResPool.GetSolidBrush (foreColor), new Point (24 + xPos, (bmp.Height - e.Font.Height) / 2));
1851 gr.DrawImage (dcbi.ImageList.Images [dcbi.ImageIndex], new Rectangle (new Point (xPos + 2, 0), new Size (16, 16)));
1853 e.Graphics.DrawImage (bmp, e.Bounds.X, e.Bounds.Y);
1858 protected override void OnSelectedIndexChanged (EventArgs e)
1860 if (Items.Count > 0) {
1861 DirComboBoxItem dcbi = Items [SelectedIndex] as DirComboBoxItem;
1863 currentPath = dcbi.Path;
1867 protected override void OnSelectionChangeCommitted (EventArgs e)
1869 EventHandler eh = (EventHandler)(Events [CDirectoryChangedEvent]);
1871 eh (this, EventArgs.Empty);
1874 static object CDirectoryChangedEvent = new object ();
1876 public event EventHandler DirectoryChanged {
1877 add { Events.AddHandler (CDirectoryChangedEvent, value); }
1878 remove { Events.RemoveHandler (CDirectoryChangedEvent, value); }
1883 #region FilterStruct
1884 internal struct FilterStruct
1886 public string filterName;
1887 public StringCollection filters;
1889 public FilterStruct (string filterName, string filter)
1891 this.filterName = filterName;
1893 filters = new StringCollection ();
1895 SplitFilters (filter);
1898 private void SplitFilters (string filter)
1900 string[] split = filter.Split (new char [] {';'});
1901 foreach (string s in split) {
1902 filters.Add (s.Trim ());
1909 internal class FileFilter
1911 private ArrayList filterArrayList = new ArrayList();
1913 private string filter;
1915 public FileFilter ()
1918 public FileFilter (string filter) : base ()
1920 this.filter = filter;
1925 public static bool CheckFilter (string val)
1927 if (val.Length == 0)
1930 string[] filters = val.Split (new char [] {'|'});
1932 if ((filters.Length % 2) != 0)
1938 public ArrayList FilterArrayList {
1940 filterArrayList = value;
1944 return filterArrayList;
1948 public string Filter {
1960 private void SplitFilter ()
1962 filterArrayList.Clear ();
1964 if (filter.Length == 0)
1967 string[] filters = filter.Split (new char [] {'|'});
1969 for (int i = 0; i < filters.Length; i += 2) {
1970 FilterStruct filterStruct = new FilterStruct (filters [i], filters [i + 1]);
1972 filterArrayList.Add (filterStruct);
1980 internal class MWFFileView : ListView
1982 private ArrayList filterArrayList;
1984 private bool showHiddenFiles = false;
1986 private string selectedFilesString;
1988 private int filterIndex = 1;
1990 private ToolTip toolTip;
1991 private int oldItemIndexForToolTip = -1;
1993 private ContextMenu contextMenu;
1995 private MenuItem menuItemView;
1996 private MenuItem menuItemNew;
1998 private MenuItem smallIconMenutItem;
1999 private MenuItem tilesMenutItem;
2000 private MenuItem largeIconMenutItem;
2001 private MenuItem listMenutItem;
2002 private MenuItem detailsMenutItem;
2004 private MenuItem newFolderMenuItem;
2005 private MenuItem showHiddenFilesMenuItem;
2007 private int previousCheckedMenuItemIndex;
2009 private ArrayList viewMenuItemClones = new ArrayList ();
2011 private FSEntry currentFSEntry = null;
2013 private string currentFolder;
2014 private string currentRealFolder;
2015 private FSEntry currentFolderFSEntry;
2017 // store DirectoryInfo for a back button for example
2018 private Stack directoryStack = new Stack();
2020 // list of controls(components to enable or disable depending on current directoryStack.Count
2021 private ArrayList dirStackControlsOrComponents = new ArrayList ();
2023 private ToolBarButton folderUpToolBarButton;
2025 private ArrayList registered_senders = new ArrayList ();
2027 private bool should_push = true;
2031 private View old_view;
2033 private int old_menuitem_index;
2034 private bool do_update_view = false;
2036 private int platform = (int) Environment.OSVersion.Platform;
2038 public MWFFileView (MWFVFS vfs)
2041 this.vfs.RegisterUpdateDelegate (new MWFVFS.UpdateDelegate (RealFileViewUpdate), this);
2045 contextMenu = new ContextMenu ();
2047 toolTip = new ToolTip ();
2048 toolTip.InitialDelay = 300;
2049 toolTip.ReshowDelay = 0;
2054 menuItemView = new MenuItem ("View");
2056 smallIconMenutItem = new MenuItem ("Small Icon", new EventHandler (OnClickViewMenuSubItem));
2057 smallIconMenutItem.RadioCheck = true;
2058 menuItemView.MenuItems.Add (smallIconMenutItem);
2060 tilesMenutItem = new MenuItem ("Tiles", new EventHandler (OnClickViewMenuSubItem));
2061 tilesMenutItem.RadioCheck = true;
2062 menuItemView.MenuItems.Add (tilesMenutItem);
2064 largeIconMenutItem = new MenuItem ("Large Icon", new EventHandler (OnClickViewMenuSubItem));
2065 largeIconMenutItem.RadioCheck = true;
2066 menuItemView.MenuItems.Add (largeIconMenutItem);
2068 listMenutItem = new MenuItem ("List", new EventHandler (OnClickViewMenuSubItem));
2069 listMenutItem.RadioCheck = true;
2070 listMenutItem.Checked = true;
2071 menuItemView.MenuItems.Add (listMenutItem);
2072 previousCheckedMenuItemIndex = listMenutItem.Index;
2074 detailsMenutItem = new MenuItem ("Details", new EventHandler (OnClickViewMenuSubItem));
2075 detailsMenutItem.RadioCheck = true;
2076 menuItemView.MenuItems.Add (detailsMenutItem);
2078 contextMenu.MenuItems.Add (menuItemView);
2080 contextMenu.MenuItems.Add (new MenuItem ("-"));
2083 menuItemNew = new MenuItem ("New");
2085 newFolderMenuItem = new MenuItem ("New Folder", new EventHandler (OnClickNewFolderMenuItem));
2086 menuItemNew.MenuItems.Add (newFolderMenuItem);
2088 contextMenu.MenuItems.Add (menuItemNew);
2090 contextMenu.MenuItems.Add (new MenuItem ("-"));
2092 // Show hidden files menu item
2093 showHiddenFilesMenuItem = new MenuItem ("Show hidden files", new EventHandler (OnClickContextMenu));
2094 showHiddenFilesMenuItem.Checked = showHiddenFiles;
2095 contextMenu.MenuItems.Add (showHiddenFilesMenuItem);
2099 SmallImageList = MimeIconEngine.SmallIcons;
2100 LargeImageList = MimeIconEngine.LargeIcons;
2102 View = old_view = View.List;
2105 ContextMenu = contextMenu;
2107 ResumeLayout (false);
2109 KeyDown += new KeyEventHandler (MWF_KeyDown);
2112 public string CurrentFolder {
2114 return currentFolder;
2117 currentFolder = value;
2121 public string CurrentRealFolder {
2123 return currentRealFolder;
2127 public FSEntry CurrentFSEntry {
2129 return currentFSEntry;
2133 public MenuItem[] ViewMenuItems {
2135 MenuItem[] menuItemClones = new MenuItem [] {
2136 smallIconMenutItem.CloneMenu (),
2137 tilesMenutItem.CloneMenu (),
2138 largeIconMenutItem.CloneMenu (),
2139 listMenutItem.CloneMenu (),
2140 detailsMenutItem.CloneMenu ()
2143 viewMenuItemClones.Add (menuItemClones);
2145 return menuItemClones;
2149 public ArrayList FilterArrayList {
2151 filterArrayList = value;
2155 return filterArrayList;
2159 public bool ShowHiddenFiles {
2161 showHiddenFiles = value;
2165 return showHiddenFiles;
2169 public int FilterIndex {
2171 filterIndex = value;
2181 public string SelectedFilesString {
2183 selectedFilesString = value;
2187 return selectedFilesString;
2191 public void PushDir ()
2193 if (currentFolder != null)
2194 directoryStack.Push (currentFolder);
2196 EnableOrDisableDirstackObjects ();
2199 public void PopDir ()
2201 if (directoryStack.Count == 0)
2204 string new_folder = directoryStack.Pop () as string;
2206 EnableOrDisableDirstackObjects ();
2208 should_push = false;
2210 ChangeDirectory (null, new_folder);
2213 public void RegisterSender (IUpdateFolder iud)
2215 registered_senders.Add (iud);
2218 public void CreateNewFolder ()
2220 if (currentFolder == MWFVFS.MyComputerPrefix ||
2221 currentFolder == MWFVFS.RecentlyUsedPrefix)
2224 FSEntry fsEntry = new FSEntry ();
2225 fsEntry.Attributes = FileAttributes.Directory;
2226 fsEntry.FileType = FSEntry.FSEntryType.Directory;
2227 fsEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("inode/directory");
2228 fsEntry.LastAccessTime = DateTime.Now;
2230 // FIXME: when ListView.LabelEdit is available use it
2231 // listViewItem.BeginEdit();
2233 TextEntryDialog ted = new TextEntryDialog ();
2234 ted.IconPictureBoxImage = MimeIconEngine.LargeIcons.Images.GetImage (fsEntry.IconIndex);
2236 string folder = String.Empty;
2238 if (currentFolderFSEntry.RealName != null)
2239 folder = currentFolderFSEntry.RealName;
2241 folder = currentFolder;
2243 string tmp_filename = "New Folder";
2245 if (Directory.Exists (Path.Combine (folder, tmp_filename))) {
2248 if ((platform == 4) || (platform == 128)) {
2249 tmp_filename = tmp_filename + "-" + i;
2251 tmp_filename = tmp_filename + " (" + i + ")";
2254 while (Directory.Exists (Path.Combine (folder, tmp_filename))) {
2256 if ((platform == 4) || (platform == 128)) {
2257 tmp_filename = "New Folder" + "-" + i;
2259 tmp_filename = "New Folder" + " (" + i + ")";
2264 ted.FileName = tmp_filename;
2266 if (ted.ShowDialog () == DialogResult.OK) {
2267 string new_folder = Path.Combine (folder, ted.FileName);
2269 if (vfs.CreateFolder (new_folder)) {
2270 fsEntry.FullName = new_folder;
2271 fsEntry.Name = ted.FileName;
2273 FileViewListViewItem listViewItem = new FileViewListViewItem (fsEntry);
2276 Items.Add (listViewItem);
2279 listViewItem.EnsureVisible ();
2284 public void SetSelectedIndexTo (string fname)
2286 foreach (FileViewListViewItem item in Items) {
2287 if (item.Text == fname) {
2289 SelectedItems.Clear ();
2290 item.Selected = true;
2297 public void OneDirUp ()
2299 string parent_folder = vfs.GetParent ();
2300 if (parent_folder != null)
2301 ChangeDirectory (null, parent_folder);
2304 public void ChangeDirectory (object sender, string folder)
2306 if (folder == MWFVFS.DesktopPrefix || folder == MWFVFS.RecentlyUsedPrefix)
2307 folderUpToolBarButton.Enabled = false;
2309 folderUpToolBarButton.Enabled = true;
2311 foreach (IUpdateFolder iuf in registered_senders) {
2312 iuf.CurrentFolder = folder;
2320 currentFolderFSEntry = vfs.ChangeDirectory (folder);
2322 currentFolder = folder;
2324 if (currentFolder.IndexOf ("://") != -1)
2325 currentRealFolder = currentFolderFSEntry.RealName;
2327 currentRealFolder = currentFolder;
2332 SelectedItems.Clear ();
2334 if (folder == MWFVFS.RecentlyUsedPrefix) {
2336 View = View.Details;
2337 old_menuitem_index = previousCheckedMenuItemIndex;
2338 UpdateMenuItems (detailsMenutItem);
2339 do_update_view = true;
2341 if (View != old_view && do_update_view) {
2342 UpdateMenuItems (menuItemView.MenuItems [old_menuitem_index]);
2344 do_update_view = false;
2350 } catch (Exception e) {
2353 MessageBox.Show (e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
2357 public void UpdateFileView ()
2359 if (filterArrayList != null && filterArrayList.Count != 0) {
2360 FilterStruct fs = (FilterStruct)filterArrayList [filterIndex - 1];
2362 vfs.GetFolderContent (fs.filters);
2364 vfs.GetFolderContent ();
2367 public void RealFileViewUpdate (ArrayList directoriesArrayList, ArrayList fileArrayList)
2372 SelectedItems.Clear ();
2374 foreach (FSEntry directoryFSEntry in directoriesArrayList) {
2375 if (!ShowHiddenFiles)
2376 if (directoryFSEntry.Name.StartsWith (".") || directoryFSEntry.Attributes == FileAttributes.Hidden)
2379 FileViewListViewItem listViewItem = new FileViewListViewItem (directoryFSEntry);
2381 Items.Add (listViewItem);
2384 StringCollection collection = new StringCollection ();
2386 foreach (FSEntry fsEntry in fileArrayList) {
2388 // remove duplicates. that can happen when you read recently used files for example
2389 if (collection.Contains (fsEntry.Name)) {
2391 string fileName = fsEntry.Name;
2393 if (collection.Contains (fileName)) {
2396 while (collection.Contains (fileName + "(" + i + ")")) {
2400 fileName = fileName + "(" + i + ")";
2403 fsEntry.Name = fileName;
2406 collection.Add (fsEntry.Name);
2408 DoOneFSEntry (fsEntry);
2413 collection.Clear ();
2416 directoriesArrayList.Clear ();
2417 fileArrayList.Clear ();
2420 public void AddControlToEnableDisableByDirStack (object control)
2422 dirStackControlsOrComponents.Add (control);
2425 public void SetFolderUpToolBarButton (ToolBarButton tb)
2427 folderUpToolBarButton = tb;
2430 public void WriteRecentlyUsed (string fullfilename)
2432 vfs.WriteRecentlyUsedFiles (fullfilename);
2435 private void EnableOrDisableDirstackObjects ()
2437 foreach (object o in dirStackControlsOrComponents) {
2439 Control c = o as Control;
2440 c.Enabled = (directoryStack.Count > 1);
2442 if (o is ToolBarButton) {
2443 ToolBarButton t = o as ToolBarButton;
2444 t.Enabled = (directoryStack.Count > 0);
2449 private void DoOneFSEntry (FSEntry fsEntry)
2451 if (!ShowHiddenFiles)
2452 if (fsEntry.Name.StartsWith (".") || fsEntry.Attributes == FileAttributes.Hidden)
2455 FileViewListViewItem listViewItem = new FileViewListViewItem (fsEntry);
2457 Items.Add (listViewItem);
2460 private void MWF_KeyDown (object sender, KeyEventArgs e)
2462 if (e.KeyCode == Keys.Back) {
2467 protected override void OnClick (EventArgs e)
2470 if (SelectedItems.Count > 0) {
2471 FileViewListViewItem listViewItem = SelectedItems [0] as FileViewListViewItem;
2473 FSEntry fsEntry = listViewItem.FSEntry;
2475 if (fsEntry.FileType == FSEntry.FSEntryType.File) {
2476 currentFSEntry = fsEntry;
2478 EventHandler eh = (EventHandler)(Events [MSelectedFileChangedEvent]);
2480 eh (this, EventArgs.Empty);
2488 protected override void OnDoubleClick (EventArgs e)
2490 if (SelectedItems.Count > 0) {
2491 FileViewListViewItem listViewItem = SelectedItems [0] as FileViewListViewItem;
2493 FSEntry fsEntry = listViewItem.FSEntry;
2495 if (fsEntry.Attributes == FileAttributes.Directory) {
2497 ChangeDirectory (null, fsEntry.FullName);
2499 EventHandler eh = (EventHandler)(Events [MDirectoryChangedEvent]);
2501 eh (this, EventArgs.Empty);
2503 currentFSEntry = fsEntry;
2505 EventHandler eh = (EventHandler)(Events [MSelectedFileChangedEvent]);
2507 eh (this, EventArgs.Empty);
2509 eh = (EventHandler)(Events [MForceDialogEndEvent]);
2511 eh (this, EventArgs.Empty);
2517 base.OnDoubleClick (e);
2520 protected override void OnSelectedIndexChanged (EventArgs e)
2522 if (SelectedItems.Count > 0) {
2523 selectedFilesString = String.Empty;
2525 if (SelectedItems.Count == 1) {
2526 FileViewListViewItem listViewItem = SelectedItems [0] as FileViewListViewItem;
2528 FSEntry fsEntry = listViewItem.FSEntry;
2530 if (fsEntry.Attributes != FileAttributes.Directory)
2531 selectedFilesString = SelectedItems [0].Text;
2533 foreach (FileViewListViewItem lvi in SelectedItems) {
2534 FSEntry fsEntry = lvi.FSEntry;
2536 if (fsEntry.Attributes != FileAttributes.Directory)
2537 selectedFilesString = selectedFilesString + "\"" + lvi.Text + "\" ";
2541 EventHandler eh = (EventHandler)(Events [MSelectedFilesChangedEvent]);
2543 eh (this, EventArgs.Empty);
2546 base.OnSelectedIndexChanged (e);
2549 protected override void OnMouseMove (MouseEventArgs e)
2551 FileViewListViewItem item = GetItemAt (e.X, e.Y) as FileViewListViewItem;
2554 int currentItemIndex = item.Index;
2556 if (currentItemIndex != oldItemIndexForToolTip) {
2557 oldItemIndexForToolTip = currentItemIndex;
2559 if (toolTip != null && toolTip.Active)
2560 toolTip.Active = false;
2562 FSEntry fsEntry = item.FSEntry;
2564 string output = String.Empty;
2566 if (fsEntry.FileType == FSEntry.FSEntryType.Directory)
2567 output = "Directory: " + fsEntry.FullName;
2568 else if (fsEntry.FileType == FSEntry.FSEntryType.Device)
2569 output = "Device: "+ fsEntry.FullName;
2570 else if (fsEntry.FileType == FSEntry.FSEntryType.Network)
2571 output = "Network: " + fsEntry.FullName;
2573 output = "File: " + fsEntry.FullName;
2575 toolTip.SetToolTip (this, output);
2577 toolTip.Active = true;
2580 toolTip.Active = false;
2582 base.OnMouseMove (e);
2585 void OnClickContextMenu (object sender, EventArgs e)
2587 MenuItem senderMenuItem = sender as MenuItem;
2589 if (senderMenuItem == showHiddenFilesMenuItem) {
2590 senderMenuItem.Checked = !senderMenuItem.Checked;
2591 showHiddenFiles = senderMenuItem.Checked;
2596 void OnClickViewMenuSubItem (object sender, EventArgs e)
2598 MenuItem senderMenuItem = (MenuItem)sender;
2600 UpdateMenuItems (senderMenuItem);
2604 switch (senderMenuItem.Index) {
2606 View = View.SmallIcon;
2609 View = View.LargeIcon;
2612 View = View.LargeIcon;
2618 View = View.Details;
2625 protected override void OnBeforeLabelEdit (LabelEditEventArgs e)
2627 FileViewListViewItem listViewItem = SelectedItems [0] as FileViewListViewItem;
2628 FSEntry fsEntry = listViewItem.FSEntry;
2630 // only allow editing of files or directories
2631 if (fsEntry.FileType != FSEntry.FSEntryType.Directory &&
2632 fsEntry.FileType != FSEntry.FSEntryType.File)
2633 e.CancelEdit = true;
2635 base.OnBeforeLabelEdit (e);
2638 protected override void OnAfterLabelEdit (LabelEditEventArgs e)
2640 base.OnAfterLabelEdit (e);
2642 // no changes were made
2643 if (e.Label == null || Items [e.Item].Text == e.Label)
2646 FileViewListViewItem listViewItem = SelectedItems [0] as FileViewListViewItem;
2647 FSEntry fsEntry = listViewItem.FSEntry;
2649 string folder = (currentFolderFSEntry.RealName != null) ?
2650 currentFolderFSEntry.RealName : currentFolder;
2652 switch (fsEntry.FileType) {
2653 case FSEntry.FSEntryType.Directory:
2654 string sourceDir = (fsEntry.RealName != null) ? fsEntry.RealName : fsEntry.FullName;
2655 string destDir = Path.Combine (folder, e.Label);
2656 if (!vfs.MoveFolder (sourceDir, destDir)) {
2657 e.CancelEdit = true;
2659 if (fsEntry.RealName != null)
2660 fsEntry.RealName = destDir;
2662 fsEntry.FullName = destDir;
2665 case FSEntry.FSEntryType.File:
2666 string sourceFile = (fsEntry.RealName != null) ? fsEntry.RealName : fsEntry.FullName;
2667 string destFile = Path.Combine (folder, e.Label);
2668 if (!vfs.MoveFile (sourceFile, destFile)) {
2669 e.CancelEdit = true;
2671 if (fsEntry.RealName != null)
2672 fsEntry.RealName = destFile;
2674 fsEntry.FullName = destFile;
2680 private void UpdateMenuItems (MenuItem senderMenuItem)
2682 menuItemView.MenuItems [previousCheckedMenuItemIndex].Checked = false;
2683 menuItemView.MenuItems [senderMenuItem.Index].Checked = true;
2685 foreach (MenuItem[] items in viewMenuItemClones) {
2686 items [previousCheckedMenuItemIndex].Checked = false;
2687 items [senderMenuItem.Index].Checked = true;
2690 previousCheckedMenuItemIndex = senderMenuItem.Index;
2693 void OnClickNewFolderMenuItem (object sender, EventArgs e)
2698 static object MSelectedFileChangedEvent = new object ();
2699 static object MSelectedFilesChangedEvent = new object ();
2700 static object MDirectoryChangedEvent = new object ();
2701 static object MForceDialogEndEvent = new object ();
2703 public event EventHandler SelectedFileChanged {
2704 add { Events.AddHandler (MSelectedFileChangedEvent, value); }
2705 remove { Events.RemoveHandler (MSelectedFileChangedEvent, value); }
2708 public event EventHandler SelectedFilesChanged {
2709 add { Events.AddHandler (MSelectedFilesChangedEvent, value); }
2710 remove { Events.RemoveHandler (MSelectedFilesChangedEvent, value); }
2713 public event EventHandler DirectoryChanged {
2714 add { Events.AddHandler (MDirectoryChangedEvent, value); }
2715 remove { Events.RemoveHandler (MDirectoryChangedEvent, value); }
2718 public event EventHandler ForceDialogEnd {
2719 add { Events.AddHandler (MForceDialogEndEvent, value); }
2720 remove { Events.RemoveHandler (MForceDialogEndEvent, value); }
2725 #region FileListViewItem
2726 internal class FileViewListViewItem : ListViewItem
2728 private FSEntry fsEntry;
2730 public FileViewListViewItem (FSEntry fsEntry)
2732 this.fsEntry = fsEntry;
2734 ImageIndex = fsEntry.IconIndex;
2736 Text = fsEntry.Name;
2738 switch (fsEntry.FileType) {
2739 case FSEntry.FSEntryType.Directory:
2740 SubItems.Add (String.Empty);
2741 SubItems.Add ("Directory");
2742 SubItems.Add (fsEntry.LastAccessTime.ToShortDateString () + " " + fsEntry.LastAccessTime.ToShortTimeString ());
2744 case FSEntry.FSEntryType.File:
2747 if (fsEntry.FileSize > 1024)
2748 fileLen = fsEntry.FileSize / 1024;
2749 } catch (Exception) {
2753 SubItems.Add (fileLen.ToString () + " KB");
2754 SubItems.Add ("File");
2755 SubItems.Add (fsEntry.LastAccessTime.ToShortDateString () + " " + fsEntry.LastAccessTime.ToShortTimeString ());
2757 case FSEntry.FSEntryType.Device:
2758 SubItems.Add (String.Empty);
2759 SubItems.Add ("Device");
2760 SubItems.Add (fsEntry.LastAccessTime.ToShortDateString () + " " + fsEntry.LastAccessTime.ToShortTimeString ());
2762 case FSEntry.FSEntryType.RemovableDevice:
2763 SubItems.Add (String.Empty);
2764 SubItems.Add ("RemovableDevice");
2765 SubItems.Add (fsEntry.LastAccessTime.ToShortDateString () + " " + fsEntry.LastAccessTime.ToShortTimeString ());
2772 public FSEntry FSEntry {
2784 #region IUpdateFolder
2785 internal interface IUpdateFolder
2787 string CurrentFolder {get; set;}
2791 #region TextEntryDialog
2792 // FIXME: When ListView.LabelEdit is implemented remove me
2793 internal class TextEntryDialog : Form
2795 private Label label1;
2796 private Button okButton;
2797 private TextBox newNameTextBox;
2798 private PictureBox iconPictureBox;
2799 private Button cancelButton;
2800 private GroupBox groupBox1;
2802 public TextEntryDialog ()
2804 groupBox1 = new GroupBox ();
2805 cancelButton = new Button ();
2806 iconPictureBox = new PictureBox ();
2807 newNameTextBox = new TextBox ();
2808 okButton = new Button ();
2809 label1 = new Label ();
2810 groupBox1.SuspendLayout ();
2814 groupBox1.Controls.Add (newNameTextBox);
2815 groupBox1.Controls.Add (label1);
2816 groupBox1.Controls.Add (iconPictureBox);
2817 groupBox1.Location = new Point (8, 8);
2818 groupBox1.Size = new Size (232, 160);
2819 groupBox1.TabIndex = 5;
2820 groupBox1.TabStop = false;
2821 groupBox1.Text = "New Name";
2824 cancelButton.DialogResult = DialogResult.Cancel;
2825 cancelButton.Location = new Point (168, 176);
2826 cancelButton.TabIndex = 4;
2827 cancelButton.Text = "Cancel";
2830 iconPictureBox.BorderStyle = BorderStyle.Fixed3D;
2831 iconPictureBox.Location = new Point (86, 24);
2832 iconPictureBox.Size = new Size (60, 60);
2833 iconPictureBox.TabIndex = 3;
2834 iconPictureBox.TabStop = false;
2835 iconPictureBox.SizeMode = PictureBoxSizeMode.CenterImage;
2838 newNameTextBox.Location = new Point (16, 128);
2839 newNameTextBox.Size = new Size (200, 20);
2840 newNameTextBox.TabIndex = 5;
2841 newNameTextBox.Text = String.Empty;
2844 okButton.DialogResult = DialogResult.OK;
2845 okButton.Location = new Point (80, 176);
2846 okButton.TabIndex = 3;
2847 okButton.Text = "OK";
2850 label1.Location = new Point (16, 96);
2851 label1.Size = new Size (200, 23);
2852 label1.TabIndex = 4;
2853 label1.Text = "Enter Name:";
2854 label1.TextAlign = ContentAlignment.MiddleCenter;
2857 AcceptButton = okButton;
2858 AutoScaleBaseSize = new Size (5, 13);
2859 CancelButton = cancelButton;
2860 ClientSize = new Size (248, 205);
2861 Controls.Add (groupBox1);
2862 Controls.Add (cancelButton);
2863 Controls.Add (okButton);
2864 FormBorderStyle = FormBorderStyle.FixedDialog;
2865 Text = "New Folder or File";
2866 groupBox1.ResumeLayout (false);
2867 ResumeLayout (false);
2869 newNameTextBox.Select ();
2872 public Image IconPictureBoxImage {
2874 iconPictureBox.Image = value;
2878 public string FileName {
2880 return newNameTextBox.Text;
2883 newNameTextBox.Text = value;
2890 internal class MWFVFS
2892 private FileSystem fileSystem;
2894 private int platform = (int) Environment.OSVersion.Platform;
2896 public static readonly string DesktopPrefix = "Desktop://";
2897 public static readonly string PersonalPrefix = "Personal://";
2898 public static readonly string MyComputerPrefix = "MyComputer://";
2899 public static readonly string RecentlyUsedPrefix = "RecentlyUsed://";
2900 public static readonly string MyNetworkPrefix = "MyNetwork://";
2901 public static readonly string MyComputerPersonalPrefix = "MyComputerPersonal://";
2903 public static Hashtable MyComputerDevicesPrefix = new Hashtable ();
2905 public delegate void UpdateDelegate (ArrayList folders, ArrayList files);
2906 private UpdateDelegate updateDelegate;
2907 private Control calling_control;
2909 private ThreadStart get_folder_content_thread_start;
2910 private Thread worker;
2911 private WorkerThread workerThread = null;
2912 private StringCollection the_filters;
2916 if ((platform == 4) || (platform == 128)) {
2917 fileSystem = new UnixFileSystem ();
2919 fileSystem = new WinFileSystem ();
2923 public FSEntry ChangeDirectory (string folder)
2925 return fileSystem.ChangeDirectory (folder);
2928 public void GetFolderContent ()
2930 GetFolderContent (null);
2933 public void GetFolderContent (StringCollection filters)
2935 the_filters = filters;
2937 if (workerThread != null) {
2938 workerThread.Stop ();
2939 workerThread = null;
2942 // Added next line to ensure the control is created before BeginInvoke is called on it
2943 calling_control.CreateControl();
2944 workerThread = new WorkerThread (fileSystem, the_filters, updateDelegate, calling_control);
2946 get_folder_content_thread_start = new ThreadStart (workerThread.GetFolderContentThread);
2947 worker = new Thread (get_folder_content_thread_start);
2948 worker.IsBackground = true;
2952 internal class WorkerThread
2954 private FileSystem fileSystem;
2955 private StringCollection the_filters;
2956 private UpdateDelegate updateDelegate;
2957 private Control calling_control;
2958 private readonly object lockobject = new object ();
2959 private bool stopped = false;
2961 public WorkerThread (FileSystem fileSystem, StringCollection the_filters, UpdateDelegate updateDelegate, Control calling_control)
2963 this.fileSystem = fileSystem;
2964 this.the_filters = the_filters;
2965 this.updateDelegate = updateDelegate;
2966 this.calling_control = calling_control;
2969 public void GetFolderContentThread()
2974 fileSystem.GetFolderContent (the_filters, out folders, out files);
2979 if (updateDelegate != null) {
2981 object[] objectArray = new object[2];
2983 objectArray[0] = folders;
2984 objectArray[1] = files;
2986 calling_control.BeginInvoke (updateDelegate, objectArray);
2999 public ArrayList GetFoldersOnly ()
3001 return fileSystem.GetFoldersOnly ();
3004 public void WriteRecentlyUsedFiles (string filename)
3006 fileSystem.WriteRecentlyUsedFiles (filename);
3009 public ArrayList GetRecentlyUsedFiles ()
3011 return fileSystem.GetRecentlyUsedFiles ();
3014 public ArrayList GetMyComputerContent ()
3016 return fileSystem.GetMyComputerContent ();
3019 public ArrayList GetMyNetworkContent ()
3021 return fileSystem.GetMyNetworkContent ();
3024 public bool CreateFolder (string new_folder)
3027 if (Directory.Exists (new_folder)) {
3028 string message = "Folder \"" + new_folder + "\" already exists.";
3029 MessageBox.Show (message, new_folder, MessageBoxButtons.OK,
3030 MessageBoxIcon.Error);
3033 Directory.CreateDirectory (new_folder);
3034 } catch (Exception e) {
3035 MessageBox.Show (e.Message, new_folder, MessageBoxButtons.OK, MessageBoxIcon.Error);
3042 public bool MoveFolder (string sourceDirName, string destDirName)
3045 if (Directory.Exists (destDirName)) {
3046 string message = "Cannot rename " + Path.GetFileName (sourceDirName)
3047 + ": A folder with the name you specified already exists."
3048 + " Specify a different folder name.";
3049 MessageBox.Show (message, "Error Renaming Folder", MessageBoxButtons.OK,
3050 MessageBoxIcon.Error);
3053 Directory.Move (sourceDirName, destDirName);
3054 } catch (Exception e) {
3055 MessageBox.Show (e.Message, "Error Renaming Folder",
3056 MessageBoxButtons.OK, MessageBoxIcon.Error);
3063 public bool MoveFile (string sourceFileName, string destFileName)
3066 if (File.Exists (destFileName)) {
3067 string message = "Cannot rename " + Path.GetFileName (sourceFileName)
3068 + ": A file with the name you specified already exists."
3069 + " Specify a different file name.";
3070 MessageBox.Show (message, "Error Renaming File",
3071 MessageBoxButtons.OK, MessageBoxIcon.Error);
3074 File.Move (sourceFileName, destFileName);
3075 } catch (Exception e) {
3076 MessageBox.Show (e.Message, "Error Renaming Folder",
3077 MessageBoxButtons.OK, MessageBoxIcon.Error);
3084 public string GetParent ()
3086 return fileSystem.GetParent ();
3089 public void RegisterUpdateDelegate(UpdateDelegate updateDelegate, Control control)
3091 this.updateDelegate = updateDelegate;
3092 calling_control = control;
3098 internal abstract class FileSystem
3100 protected string currentTopFolder = String.Empty;
3101 protected FSEntry currentFolderFSEntry = null;
3102 protected FSEntry currentTopFolderFSEntry = null;
3103 private FileInfoComparer fileInfoComparer = new FileInfoComparer ();
3105 public FSEntry ChangeDirectory (string folder)
3107 if (folder == MWFVFS.DesktopPrefix) {
3108 currentTopFolder = MWFVFS.DesktopPrefix;
3109 currentTopFolderFSEntry = currentFolderFSEntry = GetDesktopFSEntry ();
3111 if (folder == MWFVFS.PersonalPrefix) {
3112 currentTopFolder = MWFVFS.PersonalPrefix;
3113 currentTopFolderFSEntry = currentFolderFSEntry = GetPersonalFSEntry ();
3115 if (folder == MWFVFS.MyComputerPersonalPrefix) {
3116 currentTopFolder = MWFVFS.MyComputerPersonalPrefix;
3117 currentTopFolderFSEntry = currentFolderFSEntry = GetMyComputerPersonalFSEntry ();
3119 if (folder == MWFVFS.RecentlyUsedPrefix) {
3120 currentTopFolder = MWFVFS.RecentlyUsedPrefix;
3121 currentTopFolderFSEntry = currentFolderFSEntry = GetRecentlyUsedFSEntry ();
3123 if (folder == MWFVFS.MyComputerPrefix) {
3124 currentTopFolder = MWFVFS.MyComputerPrefix;
3125 currentTopFolderFSEntry = currentFolderFSEntry = GetMyComputerFSEntry ();
3127 if (folder == MWFVFS.MyNetworkPrefix) {
3128 currentTopFolder = MWFVFS.MyNetworkPrefix;
3129 currentTopFolderFSEntry = currentFolderFSEntry = GetMyNetworkFSEntry ();
3133 foreach (DictionaryEntry entry in MWFVFS.MyComputerDevicesPrefix) {
3134 FSEntry fsEntry = entry.Value as FSEntry;
3135 if (folder == fsEntry.FullName) {
3136 currentTopFolder = entry.Key as string;
3137 currentTopFolderFSEntry = currentFolderFSEntry = fsEntry;
3144 currentFolderFSEntry = GetDirectoryFSEntry (new DirectoryInfo (folder), currentTopFolderFSEntry);
3148 return currentFolderFSEntry;
3151 public string GetParent ()
3153 return currentFolderFSEntry.Parent;
3156 // directories_out and files_out contain FSEntry objects
3157 public void GetFolderContent (StringCollection filters, out ArrayList directories_out, out ArrayList files_out)
3159 directories_out = new ArrayList ();
3160 files_out = new ArrayList ();
3162 if (currentFolderFSEntry.FullName == MWFVFS.DesktopPrefix) {
3163 FSEntry personalFSEntry = GetPersonalFSEntry ();
3165 directories_out.Add (personalFSEntry);
3167 FSEntry myComputerFSEntry = GetMyComputerFSEntry ();
3169 directories_out.Add (myComputerFSEntry);
3171 FSEntry myNetworkFSEntry = GetMyNetworkFSEntry ();
3173 directories_out.Add (myNetworkFSEntry);
3175 ArrayList d_out = null;
3176 ArrayList f_out = null;
3177 GetNormalFolderContent (ThemeEngine.Current.Places (UIIcon.PlacesDesktop), filters, out d_out, out f_out);
3178 directories_out.AddRange (d_out);
3179 files_out.AddRange (f_out);
3182 if (currentFolderFSEntry.FullName == MWFVFS.RecentlyUsedPrefix) {
3183 files_out = GetRecentlyUsedFiles ();
3185 if (currentFolderFSEntry.FullName == MWFVFS.MyComputerPrefix) {
3186 directories_out.AddRange (GetMyComputerContent ());
3188 if (currentFolderFSEntry.FullName == MWFVFS.PersonalPrefix || currentFolderFSEntry.FullName == MWFVFS.MyComputerPersonalPrefix) {
3189 ArrayList d_out = null;
3190 ArrayList f_out = null;
3191 GetNormalFolderContent (ThemeEngine.Current.Places (UIIcon.PlacesPersonal), filters, out d_out, out f_out);
3192 directories_out.AddRange (d_out);
3193 files_out.AddRange (f_out);
3195 if (currentFolderFSEntry.FullName == MWFVFS.MyNetworkPrefix) {
3196 directories_out.AddRange (GetMyNetworkContent ());
3198 GetNormalFolderContent (currentFolderFSEntry.FullName, filters, out directories_out, out files_out);
3202 public ArrayList GetFoldersOnly ()
3204 ArrayList directories_out = new ArrayList ();
3206 if (currentFolderFSEntry.FullName == MWFVFS.DesktopPrefix) {
3207 FSEntry personalFSEntry = GetPersonalFSEntry ();
3209 directories_out.Add (personalFSEntry);
3211 FSEntry myComputerFSEntry = GetMyComputerFSEntry ();
3213 directories_out.Add (myComputerFSEntry);
3215 FSEntry myNetworkFSEntry = GetMyNetworkFSEntry ();
3217 directories_out.Add (myNetworkFSEntry);
3219 ArrayList d_out = GetNormalFolders (ThemeEngine.Current.Places (UIIcon.PlacesDesktop));
3220 directories_out.AddRange (d_out);
3223 if (currentFolderFSEntry.FullName == MWFVFS.RecentlyUsedPrefix) {
3224 //files_out = GetRecentlyUsedFiles ();
3226 if (currentFolderFSEntry.FullName == MWFVFS.MyComputerPrefix) {
3227 directories_out.AddRange (GetMyComputerContent ());
3229 if (currentFolderFSEntry.FullName == MWFVFS.PersonalPrefix || currentFolderFSEntry.FullName == MWFVFS.MyComputerPersonalPrefix) {
3230 ArrayList d_out = GetNormalFolders (ThemeEngine.Current.Places (UIIcon.PlacesPersonal));
3231 directories_out.AddRange (d_out);
3233 if (currentFolderFSEntry.FullName == MWFVFS.MyNetworkPrefix) {
3234 directories_out.AddRange (GetMyNetworkContent ());
3236 directories_out = GetNormalFolders (currentFolderFSEntry.FullName);
3238 return directories_out;
3241 protected void GetNormalFolderContent (string from_folder, StringCollection filters, out ArrayList directories_out, out ArrayList files_out)
3243 DirectoryInfo dirinfo = new DirectoryInfo (from_folder);
3245 directories_out = new ArrayList ();
3247 DirectoryInfo[] dirs = null;
3250 dirs = dirinfo.GetDirectories ();
3251 } catch (Exception) {}
3254 for (int i = 0; i < dirs.Length; i++) {
3255 directories_out.Add (GetDirectoryFSEntry (dirs [i], currentTopFolderFSEntry));
3258 files_out = new ArrayList ();
3260 ArrayList files = new ArrayList ();
3263 if (filters == null) {
3264 files.AddRange (dirinfo.GetFiles ());
3266 foreach (string s in filters)
3267 files.AddRange (dirinfo.GetFiles (s));
3269 files.Sort (fileInfoComparer);
3271 } catch (Exception) {}
3273 for (int i = 0; i < files.Count; i++) {
3274 FSEntry fs = GetFileFSEntry (files [i] as FileInfo);
3280 protected ArrayList GetNormalFolders (string from_folder)
3282 DirectoryInfo dirinfo = new DirectoryInfo (from_folder);
3284 ArrayList directories_out = new ArrayList ();
3286 DirectoryInfo[] dirs = null;
3289 dirs = dirinfo.GetDirectories ();
3290 } catch (Exception) {}
3293 for (int i = 0; i < dirs.Length; i++) {
3294 directories_out.Add (GetDirectoryFSEntry (dirs [i], currentTopFolderFSEntry));
3297 return directories_out;
3300 protected virtual FSEntry GetDirectoryFSEntry (DirectoryInfo dirinfo, FSEntry topFolderFSEntry)
3302 FSEntry fs = new FSEntry ();
3304 fs.Attributes = dirinfo.Attributes;
3305 fs.FullName = dirinfo.FullName;
3306 fs.Name = dirinfo.Name;
3307 fs.MainTopNode = topFolderFSEntry;
3308 fs.FileType = FSEntry.FSEntryType.Directory;
3309 fs.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("inode/directory");
3310 fs.LastAccessTime = dirinfo.LastAccessTime;
3315 protected virtual FSEntry GetFileFSEntry (FileInfo fileinfo)
3317 // *sigh* FileInfo gives us no usable information for links to directories
3319 if ((fileinfo.Attributes & FileAttributes.Directory) == FileAttributes.Directory)
3322 FSEntry fs = new FSEntry ();
3324 fs.Attributes = fileinfo.Attributes;
3325 fs.FullName = fileinfo.FullName;
3326 fs.Name = fileinfo.Name;
3327 fs.FileType = FSEntry.FSEntryType.File;
3328 fs.IconIndex = MimeIconEngine.GetIconIndexForFile (fileinfo.FullName);
3329 fs.FileSize = fileinfo.Length;
3330 fs.LastAccessTime = fileinfo.LastAccessTime;
3335 internal class FileInfoComparer : IComparer
3337 public int Compare (object fileInfo1, object fileInfo2)
3339 return String.Compare (((FileInfo)fileInfo1).Name, ((FileInfo)fileInfo2).Name);
3343 protected abstract FSEntry GetDesktopFSEntry ();
3345 protected abstract FSEntry GetRecentlyUsedFSEntry ();
3347 protected abstract FSEntry GetPersonalFSEntry ();
3349 protected abstract FSEntry GetMyComputerPersonalFSEntry ();
3351 protected abstract FSEntry GetMyComputerFSEntry ();
3353 protected abstract FSEntry GetMyNetworkFSEntry ();
3355 public abstract void WriteRecentlyUsedFiles (string fileToAdd);
3357 public abstract ArrayList GetRecentlyUsedFiles ();
3359 public abstract ArrayList GetMyComputerContent ();
3361 public abstract ArrayList GetMyNetworkContent ();
3365 #region UnixFileSystem
3366 internal class UnixFileSystem : FileSystem
3368 private MasterMount masterMount = new MasterMount ();
3369 private FSEntry desktopFSEntry = null;
3370 private FSEntry recentlyusedFSEntry = null;
3371 private FSEntry personalFSEntry = null;
3372 private FSEntry mycomputerpersonalFSEntry = null;
3373 private FSEntry mycomputerFSEntry = null;
3374 private FSEntry mynetworkFSEntry = null;
3376 private string personal_folder;
3377 private string recently_used_path;
3378 private string full_kde_recent_document_dir;
3380 public UnixFileSystem ()
3382 personal_folder = ThemeEngine.Current.Places (UIIcon.PlacesPersonal);
3383 recently_used_path = Path.Combine (personal_folder, ".recently-used");
3385 full_kde_recent_document_dir = personal_folder + "/.kde/share/apps/RecentDocuments";
3387 desktopFSEntry = new FSEntry ();
3389 desktopFSEntry.Attributes = FileAttributes.Directory;
3390 desktopFSEntry.FullName = MWFVFS.DesktopPrefix;
3391 desktopFSEntry.Name = "Desktop";
3392 desktopFSEntry.RealName = ThemeEngine.Current.Places (UIIcon.PlacesDesktop);
3393 desktopFSEntry.FileType = FSEntry.FSEntryType.Directory;
3394 desktopFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("desktop/desktop");
3395 desktopFSEntry.LastAccessTime = DateTime.Now;
3397 recentlyusedFSEntry = new FSEntry ();
3399 recentlyusedFSEntry.Attributes = FileAttributes.Directory;
3400 recentlyusedFSEntry.FullName = MWFVFS.RecentlyUsedPrefix;
3401 recentlyusedFSEntry.Name = "Recently Used";
3402 recentlyusedFSEntry.FileType = FSEntry.FSEntryType.Directory;
3403 recentlyusedFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("recently/recently");
3404 recentlyusedFSEntry.LastAccessTime = DateTime.Now;
3406 personalFSEntry = new FSEntry ();
3408 personalFSEntry.Attributes = FileAttributes.Directory;
3409 personalFSEntry.FullName = MWFVFS.PersonalPrefix;
3410 personalFSEntry.Name = "Personal";
3411 personalFSEntry.MainTopNode = GetDesktopFSEntry ();
3412 personalFSEntry.RealName = ThemeEngine.Current.Places (UIIcon.PlacesPersonal);
3413 personalFSEntry.FileType = FSEntry.FSEntryType.Directory;
3414 personalFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("directory/home");
3415 personalFSEntry.LastAccessTime = DateTime.Now;
3417 mycomputerpersonalFSEntry = new FSEntry ();
3419 mycomputerpersonalFSEntry.Attributes = FileAttributes.Directory;
3420 mycomputerpersonalFSEntry.FullName = MWFVFS.MyComputerPersonalPrefix;
3421 mycomputerpersonalFSEntry.Name = "Personal";
3422 mycomputerpersonalFSEntry.MainTopNode = GetMyComputerFSEntry ();
3423 mycomputerpersonalFSEntry.RealName = ThemeEngine.Current.Places (UIIcon.PlacesPersonal);
3424 mycomputerpersonalFSEntry.FileType = FSEntry.FSEntryType.Directory;
3425 mycomputerpersonalFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("directory/home");
3426 mycomputerpersonalFSEntry.LastAccessTime = DateTime.Now;
3428 mycomputerFSEntry = new FSEntry ();
3430 mycomputerFSEntry.Attributes = FileAttributes.Directory;
3431 mycomputerFSEntry.FullName = MWFVFS.MyComputerPrefix;
3432 mycomputerFSEntry.Name = "My Computer";
3433 mycomputerFSEntry.MainTopNode = GetDesktopFSEntry ();
3434 mycomputerFSEntry.FileType = FSEntry.FSEntryType.Directory;
3435 mycomputerFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("workplace/workplace");
3436 mycomputerFSEntry.LastAccessTime = DateTime.Now;
3438 mynetworkFSEntry = new FSEntry ();
3440 mynetworkFSEntry.Attributes = FileAttributes.Directory;
3441 mynetworkFSEntry.FullName = MWFVFS.MyNetworkPrefix;
3442 mynetworkFSEntry.Name = "My Network";
3443 mynetworkFSEntry.MainTopNode = GetDesktopFSEntry ();
3444 mynetworkFSEntry.FileType = FSEntry.FSEntryType.Directory;
3445 mynetworkFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("network/network");
3446 mynetworkFSEntry.LastAccessTime = DateTime.Now;
3449 public override void WriteRecentlyUsedFiles (string fileToAdd)
3451 if (File.Exists (recently_used_path) && new FileInfo (recently_used_path).Length > 0) {
3452 XmlDocument xml_doc = new XmlDocument ();
3453 xml_doc.Load (recently_used_path);
3455 XmlNode grand_parent_node = xml_doc.SelectSingleNode ("RecentFiles");
3457 if (grand_parent_node != null) {
3458 // create a new element
3459 XmlElement new_recent_item_node = xml_doc.CreateElement ("RecentItem");
3461 XmlElement new_child = xml_doc.CreateElement ("URI");
3462 UriBuilder ub = new UriBuilder ();
3463 ub.Path = fileToAdd;
3466 XmlText new_text_child = xml_doc.CreateTextNode (ub.ToString ());
3467 new_child.AppendChild (new_text_child);
3469 new_recent_item_node.AppendChild (new_child);
3471 new_child = xml_doc.CreateElement ("Mime-Type");
3472 new_text_child = xml_doc.CreateTextNode (Mime.GetMimeTypeForFile (fileToAdd));
3473 new_child.AppendChild (new_text_child);
3475 new_recent_item_node.AppendChild (new_child);
3477 new_child = xml_doc.CreateElement ("Timestamp");
3478 long seconds = (long)(DateTime.UtcNow - new DateTime (1970, 1, 1)).TotalSeconds;
3479 new_text_child = xml_doc.CreateTextNode (seconds.ToString ());
3480 new_child.AppendChild (new_text_child);
3482 new_recent_item_node.AppendChild (new_child);
3484 new_child = xml_doc.CreateElement ("Groups");
3486 new_recent_item_node.AppendChild (new_child);
3488 // now search the nodes in grand_parent_node for another instance of the new uri and if found remove it
3489 // so that the new node is the first one
3490 foreach (XmlNode n in grand_parent_node.ChildNodes) {
3491 XmlNode uri_node = n.SelectSingleNode ("URI");
3492 if (uri_node != null) {
3493 XmlNode uri_node_child = uri_node.FirstChild;
3494 if (uri_node_child is XmlText)
3495 if (ub.ToString () == ((XmlText)uri_node_child).Data) {
3496 grand_parent_node.RemoveChild (n);
3502 // prepend the new recent item to the grand parent node list
3503 grand_parent_node.PrependChild (new_recent_item_node);
3505 // limit the # of RecentItems to 10
3506 if (grand_parent_node.ChildNodes.Count > 10) {
3507 while (grand_parent_node.ChildNodes.Count > 10)
3508 grand_parent_node.RemoveChild (grand_parent_node.LastChild);
3512 xml_doc.Save (recently_used_path);
3513 } catch (Exception) {
3517 XmlDocument xml_doc = new XmlDocument ();
3518 xml_doc.AppendChild (xml_doc.CreateXmlDeclaration ("1.0", String.Empty, String.Empty));
3520 XmlElement recentFiles_element = xml_doc.CreateElement ("RecentFiles");
3522 XmlElement new_recent_item_node = xml_doc.CreateElement ("RecentItem");
3524 XmlElement new_child = xml_doc.CreateElement ("URI");
3525 UriBuilder ub = new UriBuilder ();
3526 ub.Path = fileToAdd;
3529 XmlText new_text_child = xml_doc.CreateTextNode (ub.ToString ());
3530 new_child.AppendChild (new_text_child);
3532 new_recent_item_node.AppendChild (new_child);
3534 new_child = xml_doc.CreateElement ("Mime-Type");
3535 new_text_child = xml_doc.CreateTextNode (Mime.GetMimeTypeForFile (fileToAdd));
3536 new_child.AppendChild (new_text_child);
3538 new_recent_item_node.AppendChild (new_child);
3540 new_child = xml_doc.CreateElement ("Timestamp");
3541 long seconds = (long)(DateTime.UtcNow - new DateTime (1970, 1, 1)).TotalSeconds;
3542 new_text_child = xml_doc.CreateTextNode (seconds.ToString ());
3543 new_child.AppendChild (new_text_child);
3545 new_recent_item_node.AppendChild (new_child);
3547 new_child = xml_doc.CreateElement ("Groups");
3549 new_recent_item_node.AppendChild (new_child);
3551 recentFiles_element.AppendChild (new_recent_item_node);
3553 xml_doc.AppendChild (recentFiles_element);
3556 xml_doc.Save (recently_used_path);
3557 } catch (Exception) {
3562 // return an ArrayList with FSEntry objects
3563 public override ArrayList GetRecentlyUsedFiles ()
3565 // check for GNOME and KDE
3567 ArrayList files_al = new ArrayList ();
3570 if (File.Exists (recently_used_path)) {
3572 XmlTextReader xtr = new XmlTextReader (recently_used_path);
3573 while (xtr.Read ()) {
3574 if (xtr.NodeType == XmlNodeType.Element && xtr.Name.ToUpper () == "URI") {
3576 Uri uri = new Uri (xtr.Value);
3577 if (!files_al.Contains (uri.LocalPath))
3578 if (File.Exists (uri.LocalPath)) {
3579 FSEntry fs = GetFileFSEntry (new FileInfo (uri.LocalPath));
3586 } catch (Exception) {
3592 if (Directory.Exists (full_kde_recent_document_dir)) {
3593 string[] files = Directory.GetFiles (full_kde_recent_document_dir, "*.desktop");
3595 foreach (string file_name in files) {
3596 StreamReader sr = new StreamReader (file_name);
3598 string line = sr.ReadLine ();
3600 while (line != null) {
3601 line = line.Trim ();
3603 if (line.StartsWith ("URL=")) {
3604 line = line.Replace ("URL=", String.Empty);
3605 line = line.Replace ("$HOME", personal_folder);
3607 Uri uri = new Uri (line);
3608 if (!files_al.Contains (uri.LocalPath))
3609 if (File.Exists (uri.LocalPath)) {
3610 FSEntry fs = GetFileFSEntry (new FileInfo (uri.LocalPath));
3617 line = sr.ReadLine ();
3628 // return an ArrayList with FSEntry objects
3629 public override ArrayList GetMyComputerContent ()
3631 ArrayList my_computer_content_arraylist = new ArrayList ();
3633 if (masterMount.ProcMountAvailable) {
3634 masterMount.GetMounts ();
3636 foreach (MasterMount.Mount mount in masterMount.Block_devices) {
3637 FSEntry fsEntry = new FSEntry ();
3638 fsEntry.FileType = FSEntry.FSEntryType.Device;
3640 fsEntry.FullName = mount.mount_point;
3642 fsEntry.Name = "HDD (" + mount.fsType + ", " + mount.device_short + ")";
3644 fsEntry.FsType = mount.fsType;
3645 fsEntry.DeviceShort = mount.device_short;
3647 fsEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("harddisk/harddisk");
3649 fsEntry.Attributes = FileAttributes.Directory;
3651 fsEntry.MainTopNode = GetMyComputerFSEntry ();
3653 my_computer_content_arraylist.Add (fsEntry);
3655 if (!MWFVFS.MyComputerDevicesPrefix.Contains (fsEntry.FullName + "://"))
3656 MWFVFS.MyComputerDevicesPrefix.Add (fsEntry.FullName + "://", fsEntry);
3659 foreach (MasterMount.Mount mount in masterMount.Removable_devices) {
3660 FSEntry fsEntry = new FSEntry ();
3661 fsEntry.FileType = FSEntry.FSEntryType.RemovableDevice;
3663 fsEntry.FullName = mount.mount_point;
3665 bool is_dvd_cdrom = mount.fsType == MasterMount.FsTypes.usbfs ? false : true;
3666 string type_name = is_dvd_cdrom ? "DVD/CD-Rom" : "USB";
3667 string mime_type = is_dvd_cdrom ? "cdrom/cdrom" : "removable/removable";
3669 fsEntry.Name = type_name +" (" + mount.device_short + ")";
3671 fsEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType (mime_type);
3673 fsEntry.FsType = mount.fsType;
3674 fsEntry.DeviceShort = mount.device_short;
3676 fsEntry.Attributes = FileAttributes.Directory;
3678 fsEntry.MainTopNode = GetMyComputerFSEntry ();
3680 my_computer_content_arraylist.Add (fsEntry);
3682 string contain_string = fsEntry.FullName + "://";
3683 if (!MWFVFS.MyComputerDevicesPrefix.Contains (contain_string))
3684 MWFVFS.MyComputerDevicesPrefix.Add (contain_string, fsEntry);
3688 my_computer_content_arraylist.Add (GetMyComputerPersonalFSEntry ());
3690 return my_computer_content_arraylist;
3693 public override ArrayList GetMyNetworkContent ()
3695 ArrayList fsEntries = new ArrayList ();
3697 foreach (MasterMount.Mount mount in masterMount.Network_devices) {
3698 FSEntry fsEntry = new FSEntry ();
3699 fsEntry.FileType = FSEntry.FSEntryType.Network;
3701 fsEntry.FullName = mount.mount_point;
3703 fsEntry.FsType = mount.fsType;
3704 fsEntry.DeviceShort = mount.device_short;
3706 fsEntry.Name = "Network (" + mount.fsType + ", " + mount.device_short + ")";
3708 switch (mount.fsType) {
3709 case MasterMount.FsTypes.nfs:
3710 fsEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("nfs/nfs");
3712 case MasterMount.FsTypes.smbfs:
3713 fsEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("smb/smb");
3715 case MasterMount.FsTypes.ncpfs:
3716 fsEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("network/network");
3718 case MasterMount.FsTypes.cifs:
3719 fsEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("network/network");
3725 fsEntry.Attributes = FileAttributes.Directory;
3727 fsEntry.MainTopNode = GetMyNetworkFSEntry ();
3729 fsEntries.Add (fsEntry);
3734 protected override FSEntry GetDesktopFSEntry ()
3736 return desktopFSEntry;
3739 protected override FSEntry GetRecentlyUsedFSEntry ()
3741 return recentlyusedFSEntry;
3744 protected override FSEntry GetPersonalFSEntry ()
3746 return personalFSEntry;
3749 protected override FSEntry GetMyComputerPersonalFSEntry ()
3751 return mycomputerpersonalFSEntry;
3754 protected override FSEntry GetMyComputerFSEntry ()
3756 return mycomputerFSEntry;
3759 protected override FSEntry GetMyNetworkFSEntry ()
3761 return mynetworkFSEntry;
3766 #region WinFileSystem
3767 internal class WinFileSystem : FileSystem
3769 private FSEntry desktopFSEntry = null;
3770 private FSEntry recentlyusedFSEntry = null;
3771 private FSEntry personalFSEntry = null;
3772 private FSEntry mycomputerpersonalFSEntry = null;
3773 private FSEntry mycomputerFSEntry = null;
3774 private FSEntry mynetworkFSEntry = null;
3776 public WinFileSystem ()
3778 desktopFSEntry = new FSEntry ();
3780 desktopFSEntry.Attributes = FileAttributes.Directory;
3781 desktopFSEntry.FullName = MWFVFS.DesktopPrefix;
3782 desktopFSEntry.Name = "Desktop";
3783 desktopFSEntry.RealName = ThemeEngine.Current.Places (UIIcon.PlacesDesktop);
3784 desktopFSEntry.FileType = FSEntry.FSEntryType.Directory;
3785 desktopFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("desktop/desktop");
3786 desktopFSEntry.LastAccessTime = DateTime.Now;
3788 recentlyusedFSEntry = new FSEntry ();
3790 recentlyusedFSEntry.Attributes = FileAttributes.Directory;
3791 recentlyusedFSEntry.FullName = MWFVFS.RecentlyUsedPrefix;
3792 recentlyusedFSEntry.RealName = ThemeEngine.Current.Places (UIIcon.PlacesRecentDocuments);
3793 recentlyusedFSEntry.Name = "Recently Used";
3794 recentlyusedFSEntry.FileType = FSEntry.FSEntryType.Directory;
3795 recentlyusedFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("recently/recently");
3796 recentlyusedFSEntry.LastAccessTime = DateTime.Now;
3798 personalFSEntry = new FSEntry ();
3800 personalFSEntry.Attributes = FileAttributes.Directory;
3801 personalFSEntry.FullName = MWFVFS.PersonalPrefix;
3802 personalFSEntry.Name = "Personal";
3803 personalFSEntry.MainTopNode = GetDesktopFSEntry ();
3804 personalFSEntry.RealName = ThemeEngine.Current.Places (UIIcon.PlacesPersonal);
3805 personalFSEntry.FileType = FSEntry.FSEntryType.Directory;
3806 personalFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("directory/home");
3807 personalFSEntry.LastAccessTime = DateTime.Now;
3809 mycomputerpersonalFSEntry = new FSEntry ();
3811 mycomputerpersonalFSEntry.Attributes = FileAttributes.Directory;
3812 mycomputerpersonalFSEntry.FullName = MWFVFS.MyComputerPersonalPrefix;
3813 mycomputerpersonalFSEntry.Name = "Personal";
3814 mycomputerpersonalFSEntry.MainTopNode = GetMyComputerFSEntry ();
3815 mycomputerpersonalFSEntry.RealName = ThemeEngine.Current.Places (UIIcon.PlacesPersonal);
3816 mycomputerpersonalFSEntry.FileType = FSEntry.FSEntryType.Directory;
3817 mycomputerpersonalFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("directory/home");
3818 mycomputerpersonalFSEntry.LastAccessTime = DateTime.Now;
3820 mycomputerFSEntry = new FSEntry ();
3822 mycomputerFSEntry.Attributes = FileAttributes.Directory;
3823 mycomputerFSEntry.FullName = MWFVFS.MyComputerPrefix;
3824 mycomputerFSEntry.Name = "My Computer";
3825 mycomputerFSEntry.MainTopNode = GetDesktopFSEntry ();
3826 mycomputerFSEntry.FileType = FSEntry.FSEntryType.Directory;
3827 mycomputerFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("workplace/workplace");
3828 mycomputerFSEntry.LastAccessTime = DateTime.Now;
3830 mynetworkFSEntry = new FSEntry ();
3832 mynetworkFSEntry.Attributes = FileAttributes.Directory;
3833 mynetworkFSEntry.FullName = MWFVFS.MyNetworkPrefix;
3834 mynetworkFSEntry.Name = "My Network";
3835 mynetworkFSEntry.MainTopNode = GetDesktopFSEntry ();
3836 mynetworkFSEntry.FileType = FSEntry.FSEntryType.Directory;
3837 mynetworkFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("network/network");
3838 mynetworkFSEntry.LastAccessTime = DateTime.Now;
3841 public override void WriteRecentlyUsedFiles (string fileToAdd)
3843 // TODO: Implement this method
3844 // use SHAddToRecentDocs ?
3847 public override ArrayList GetRecentlyUsedFiles ()
3849 ArrayList al = new ArrayList ();
3851 DirectoryInfo di = new DirectoryInfo (recentlyusedFSEntry.RealName);
3853 FileInfo[] fileinfos = di.GetFiles ();
3855 foreach (FileInfo fi in fileinfos) {
3856 FSEntry fs = GetFileFSEntry (fi);
3864 public override ArrayList GetMyComputerContent ()
3866 string[] logical_drives = Directory.GetLogicalDrives ();
3868 ArrayList my_computer_content_arraylist = new ArrayList ();
3870 foreach (string drive in logical_drives) {
3871 FSEntry fsEntry = new FSEntry ();
3872 fsEntry.FileType = FSEntry.FSEntryType.Device;
3874 fsEntry.FullName = drive;
3876 fsEntry.Name = drive;
3878 fsEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("harddisk/harddisk");
3880 fsEntry.Attributes = FileAttributes.Directory;
3882 fsEntry.MainTopNode = GetMyComputerFSEntry ();
3884 my_computer_content_arraylist.Add (fsEntry);
3886 string contain_string = fsEntry.FullName + "://";
3887 if (!MWFVFS.MyComputerDevicesPrefix.Contains (contain_string))
3888 MWFVFS.MyComputerDevicesPrefix.Add (contain_string, fsEntry);
3891 my_computer_content_arraylist.Add (GetMyComputerPersonalFSEntry ());
3893 return my_computer_content_arraylist;
3896 public override ArrayList GetMyNetworkContent ()
3898 // TODO: Implement this method
3899 return new ArrayList ();
3901 protected override FSEntry GetDesktopFSEntry ()
3903 return desktopFSEntry;
3906 protected override FSEntry GetRecentlyUsedFSEntry ()
3908 return recentlyusedFSEntry;
3911 protected override FSEntry GetPersonalFSEntry ()
3913 return personalFSEntry;
3916 protected override FSEntry GetMyComputerPersonalFSEntry ()
3918 return mycomputerpersonalFSEntry;
3921 protected override FSEntry GetMyComputerFSEntry ()
3923 return mycomputerFSEntry;
3926 protected override FSEntry GetMyNetworkFSEntry ()
3928 return mynetworkFSEntry;
3934 internal class FSEntry
3936 public enum FSEntryType
3948 private MasterMount.FsTypes fsType;
3949 private string device_short;
3950 private string fullName;
3951 private string name;
3952 private string realName = null;
3953 private FileAttributes attributes = FileAttributes.Normal;
3954 private long fileSize;
3955 private FSEntryType fileType;
3956 private DateTime lastAccessTime;
3957 private FSEntry mainTopNode = null;
3959 private int iconIndex;
3961 private string parent;
3963 public MasterMount.FsTypes FsType {
3973 public string DeviceShort {
3975 device_short = value;
3979 return device_short;
3983 public string FullName {
3993 public string Name {
4003 public string RealName {
4013 public FileAttributes Attributes {
4023 public long FileSize {
4033 public FSEntryType FileType {
4043 public DateTime LastAccessTime {
4045 lastAccessTime = value;
4049 return lastAccessTime;
4053 public int IconIndex {
4063 public FSEntry MainTopNode {
4065 mainTopNode = value;
4073 public string Parent {
4079 parent = GetParent ();
4085 private string GetParent ()
4087 if (fullName == MWFVFS.PersonalPrefix) {
4088 return MWFVFS.DesktopPrefix;
4090 if (fullName == MWFVFS.MyComputerPersonalPrefix) {
4091 return MWFVFS.MyComputerPrefix;
4093 if (fullName == MWFVFS.MyComputerPrefix) {
4094 return MWFVFS.DesktopPrefix;
4096 if (fullName == MWFVFS.MyNetworkPrefix) {
4097 return MWFVFS.DesktopPrefix;
4099 if (fullName == MWFVFS.DesktopPrefix) {
4102 if (fullName == MWFVFS.RecentlyUsedPrefix) {
4105 foreach (DictionaryEntry entry in MWFVFS.MyComputerDevicesPrefix) {
4106 FSEntry fsEntry = entry.Value as FSEntry;
4107 if (fullName == fsEntry.FullName) {
4108 return fsEntry.MainTopNode.FullName;
4112 DirectoryInfo dirInfo = new DirectoryInfo (fullName);
4114 DirectoryInfo dirInfoParent = dirInfo.Parent;
4116 if (dirInfoParent != null) {
4117 FSEntry fsEntry = MWFVFS.MyComputerDevicesPrefix [dirInfoParent.FullName + "://"] as FSEntry;
4119 if (fsEntry != null) {
4120 return fsEntry.FullName;
4123 if (mainTopNode != null) {
4124 if (dirInfoParent.FullName == ThemeEngine.Current.Places (UIIcon.PlacesDesktop) &&
4125 mainTopNode.FullName == MWFVFS.DesktopPrefix) {
4126 return mainTopNode.FullName;
4128 if (dirInfoParent.FullName == ThemeEngine.Current.Places (UIIcon.PlacesPersonal) &&
4129 mainTopNode.FullName == MWFVFS.PersonalPrefix) {
4130 return mainTopNode.FullName;
4132 if (dirInfoParent.FullName == ThemeEngine.Current.Places (UIIcon.PlacesPersonal) &&
4133 mainTopNode.FullName == MWFVFS.MyComputerPersonalPrefix) {
4134 return mainTopNode.FullName;
4138 return dirInfoParent.FullName;
4148 // Alexsantas little *nix helper
4149 internal class MasterMount
4152 internal enum FsTypes
4176 internal struct Mount
4178 public string device_or_filesystem;
4179 public string device_short;
4180 public string mount_point;
4181 public FsTypes fsType;
4184 bool proc_mount_available = false;
4186 ArrayList block_devices = new ArrayList ();
4187 ArrayList network_devices = new ArrayList ();
4188 ArrayList removable_devices = new ArrayList ();
4190 private int platform = (int) Environment.OSVersion.Platform;
4191 private MountComparer mountComparer = new MountComparer ();
4193 public MasterMount ()
4195 // maybe check if the current user can access /proc/mounts
4196 if ((platform == 4) || (platform == 128))
4197 if (File.Exists ("/proc/mounts"))
4198 proc_mount_available = true;
4201 public ArrayList Block_devices {
4203 return block_devices;
4207 public ArrayList Network_devices {
4209 return network_devices;
4213 public ArrayList Removable_devices {
4215 return removable_devices;
4219 public bool ProcMountAvailable {
4221 return proc_mount_available;
4225 public void GetMounts ()
4227 if (!proc_mount_available)
4230 block_devices.Clear ();
4231 network_devices.Clear ();
4232 removable_devices.Clear ();
4235 StreamReader sr = new StreamReader ("/proc/mounts");
4237 string line = sr.ReadLine ();
4239 ArrayList lines = new ArrayList ();
4240 while (line != null) {
4241 if (lines.IndexOf (line) == -1) { // Avoid duplicates
4242 ProcessProcMountLine (line);
4245 line = sr.ReadLine ();
4250 block_devices.Sort (mountComparer);
4251 network_devices.Sort (mountComparer);
4252 removable_devices.Sort (mountComparer);
4258 private void ProcessProcMountLine (string line)
4260 string[] split = line.Split (new char [] {' '});
4262 if (split != null && split.Length > 0) {
4263 Mount mount = new Mount ();
4265 if (split [0].StartsWith ("/dev/"))
4266 mount.device_short = split [0].Replace ("/dev/", String.Empty);
4268 mount.device_short = split [0];
4270 mount.device_or_filesystem = split [0];
4271 mount.mount_point = split [1];
4273 // TODO: other removable devices, floppy
4277 if (split [2] == "nfs") {
4278 mount.fsType = FsTypes.nfs;
4279 network_devices.Add (mount);
4280 } else if (split [2] == "smbfs") {
4281 mount.fsType = FsTypes.smbfs;
4282 network_devices.Add (mount);
4283 } else if (split [2] == "cifs") {
4284 mount.fsType = FsTypes.cifs;
4285 network_devices.Add (mount);
4286 } else if (split [2] == "ncpfs") {
4287 mount.fsType = FsTypes.ncpfs;
4288 network_devices.Add (mount);
4290 } else if (split [2] == "iso9660") { //cdrom
4291 mount.fsType = FsTypes.iso9660;
4292 removable_devices.Add (mount);
4293 } else if (split [2] == "usbfs") { //usb ? not tested
4294 mount.fsType = FsTypes.usbfs;
4295 removable_devices.Add (mount);
4297 } else if (split [0].StartsWith ("/")) { //block devices
4298 if (split [1].StartsWith ("/dev/")) // root static, do not add
4301 if (split [2] == "ext2")
4302 mount.fsType = FsTypes.ext2;
4303 else if (split [2] == "ext3")
4304 mount.fsType = FsTypes.ext3;
4305 else if (split [2] == "reiserfs")
4306 mount.fsType = FsTypes.reiserfs;
4307 else if (split [2] == "xfs")
4308 mount.fsType = FsTypes.xfs;
4309 else if (split [2] == "vfat")
4310 mount.fsType = FsTypes.vfat;
4311 else if (split [2] == "ntfs")
4312 mount.fsType = FsTypes.ntfs;
4313 else if (split [2] == "msdos")
4314 mount.fsType = FsTypes.msdos;
4315 else if (split [2] == "umsdos")
4316 mount.fsType = FsTypes.umsdos;
4317 else if (split [2] == "hpfs")
4318 mount.fsType = FsTypes.hpfs;
4319 else if (split [2] == "minix")
4320 mount.fsType = FsTypes.minix;
4321 else if (split [2] == "jfs")
4322 mount.fsType = FsTypes.jfs;
4324 block_devices.Add (mount);
4329 public class MountComparer : IComparer
4331 public int Compare (object mount1, object mount2)
4333 return String.Compare (((Mount)mount1).device_short, ((Mount)mount2).device_short);
4340 // easy to use class to store and read internal MWF config settings.
4341 // the config values are stored in the users home dir as a hidden xml file called "mwf_config".
4342 // currently supports int, string, byte, color and arrays (including byte arrays)
4343 // don't forget, when you read a value you still have to cast this value.
4346 // MWFConfig.SetValue ("SomeClass", "What", value);
4347 // object o = MWFConfig.GetValue ("SomeClass", "What");
4351 // string[] configFileNames = (string[])MWFConfig.GetValue ("FileDialog", "FileNames");
4352 // MWFConfig.SetValue ("FileDialog", "LastFolder", "/home/user");
4354 internal class MWFConfig
4356 private static MWFConfigInstance Instance = new MWFConfigInstance ();
4358 private static object lock_object = new object();
4360 public static object GetValue (string class_name, string value_name)
4362 lock (lock_object) {
4363 return Instance.GetValue (class_name, value_name);
4367 public static void SetValue (string class_name, string value_name, object value)
4369 lock (lock_object) {
4370 Instance.SetValue (class_name, value_name, value);
4374 public static void Flush ()
4376 lock (lock_object) {
4381 public static void RemoveClass (string class_name)
4383 lock (lock_object) {
4384 Instance.RemoveClass (class_name);
4388 public static void RemoveClassValue (string class_name, string value_name)
4390 lock (lock_object) {
4391 Instance.RemoveClassValue (class_name, value_name);
4395 public static void RemoveAllClassValues (string class_name)
4397 lock (lock_object) {
4398 Instance.RemoveAllClassValues (class_name);
4402 internal class MWFConfigInstance
4404 Hashtable classes_hashtable = new Hashtable ();
4405 static string full_file_name;
4406 static string default_file_name;
4407 readonly string configName = "MWFConfig";
4408 static int platform = (int) Environment.OSVersion.Platform;
4410 static bool IsUnix ()
4412 return (platform == 4 || platform == 128);
4415 static MWFConfigInstance ()
4417 string b = "mwf_config";
4418 string dir = Environment.GetFolderPath (Environment.SpecialFolder.Personal);
4421 dir = Path.Combine (dir, ".mono");
4423 Directory.CreateDirectory (dir);
4427 default_file_name = Path.Combine (dir, b);
4428 full_file_name = default_file_name;
4431 public MWFConfigInstance ()
4433 Open (default_file_name);
4437 public MWFConfigInstance (string filename)
4439 string path = Path.GetDirectoryName (filename);
4440 if (path == null || path == String.Empty) {
4441 path = Environment.GetFolderPath (Environment.SpecialFolder.Personal);
4443 full_file_name = Path.Combine (path, filename);
4445 full_file_name = filename;
4447 Open (full_file_name);
4450 ~MWFConfigInstance ()
4455 public object GetValue (string class_name, string value_name)
4457 ClassEntry class_entry = classes_hashtable [class_name] as ClassEntry;
4459 if (class_entry != null)
4460 return class_entry.GetValue (value_name);
4465 public void SetValue (string class_name, string value_name, object value)
4467 ClassEntry class_entry = classes_hashtable [class_name] as ClassEntry;
4469 if (class_entry == null) {
4470 class_entry = new ClassEntry ();
4471 class_entry.ClassName = class_name;
4472 classes_hashtable [class_name] = class_entry;
4475 class_entry.SetValue (value_name, value);
4478 private void Open (string filename)
4481 XmlTextReader xtr = new XmlTextReader (filename);
4486 } catch (Exception) {
4490 public void Flush ()
4493 XmlTextWriter xtw = new XmlTextWriter (full_file_name, null);
4494 xtw.Formatting = Formatting.Indented;
4501 File.SetAttributes (full_file_name, FileAttributes.Hidden);
4502 } catch (Exception){
4506 public void RemoveClass (string class_name)
4508 ClassEntry class_entry = classes_hashtable [class_name] as ClassEntry;
4510 if (class_entry != null) {
4511 class_entry.RemoveAllClassValues ();
4513 classes_hashtable.Remove (class_name);
4517 public void RemoveClassValue (string class_name, string value_name)
4519 ClassEntry class_entry = classes_hashtable [class_name] as ClassEntry;
4521 if (class_entry != null) {
4522 class_entry.RemoveClassValue (value_name);
4526 public void RemoveAllClassValues (string class_name)
4528 ClassEntry class_entry = classes_hashtable [class_name] as ClassEntry;
4530 if (class_entry != null) {
4531 class_entry.RemoveAllClassValues ();
4535 private void ReadConfig (XmlTextReader xtr)
4537 if (!CheckForMWFConfig (xtr))
4540 while (xtr.Read ()) {
4541 switch (xtr.NodeType) {
4542 case XmlNodeType.Element:
4543 ClassEntry class_entry = classes_hashtable [xtr.Name] as ClassEntry;
4545 if (class_entry == null) {
4546 class_entry = new ClassEntry ();
4547 class_entry.ClassName = xtr.Name;
4548 classes_hashtable [xtr.Name] = class_entry;
4551 class_entry.ReadXml (xtr);
4557 private bool CheckForMWFConfig (XmlTextReader xtr)
4560 if (xtr.NodeType == XmlNodeType.Element) {
4561 if (xtr.Name == configName)
4569 private void WriteConfig (XmlTextWriter xtw)
4571 if (classes_hashtable.Count == 0)
4574 xtw.WriteStartElement (configName);
4575 foreach (DictionaryEntry entry in classes_hashtable) {
4576 ClassEntry class_entry = entry.Value as ClassEntry;
4578 class_entry.WriteXml (xtw);
4580 xtw.WriteEndElement ();
4583 internal class ClassEntry
4585 private Hashtable classvalues_hashtable = new Hashtable ();
4586 private string className;
4588 public string ClassName {
4598 public void SetValue (string value_name, object value)
4600 ClassValue class_value = classvalues_hashtable [value_name] as ClassValue;
4602 if (class_value == null) {
4603 class_value = new ClassValue ();
4604 class_value.Name = value_name;
4605 classvalues_hashtable [value_name] = class_value;
4608 class_value.SetValue (value);
4611 public object GetValue (string value_name)
4613 ClassValue class_value = classvalues_hashtable [value_name] as ClassValue;
4615 if (class_value == null) {
4619 return class_value.GetValue ();
4622 public void RemoveAllClassValues ()
4624 classvalues_hashtable.Clear ();
4627 public void RemoveClassValue (string value_name)
4629 ClassValue class_value = classvalues_hashtable [value_name] as ClassValue;
4631 if (class_value != null) {
4632 classvalues_hashtable.Remove (value_name);
4636 public void ReadXml (XmlTextReader xtr)
4638 while (xtr.Read ()) {
4639 switch (xtr.NodeType) {
4640 case XmlNodeType.Element:
4641 string name = xtr.GetAttribute ("name");
4643 ClassValue class_value = classvalues_hashtable [name] as ClassValue;
4645 if (class_value == null) {
4646 class_value = new ClassValue ();
4647 class_value.Name = name;
4648 classvalues_hashtable [name] = class_value;
4651 class_value.ReadXml (xtr);
4654 case XmlNodeType.EndElement:
4660 public void WriteXml (XmlTextWriter xtw)
4662 if (classvalues_hashtable.Count == 0)
4665 xtw.WriteStartElement (className);
4667 foreach (DictionaryEntry entry in classvalues_hashtable) {
4668 ClassValue class_value = entry.Value as ClassValue;
4670 class_value.WriteXml (xtw);
4673 xtw.WriteEndElement ();
4677 internal class ClassValue
4679 private object value;
4680 private string name;
4682 public string Name {
4692 public void SetValue (object value)
4696 public object GetValue ()
4701 public void ReadXml (XmlTextReader xtr)
4704 string single_value;
4706 type = xtr.GetAttribute ("type");
4708 if (type == "byte_array" || type.IndexOf ("-array") == -1) {
4709 single_value = xtr.ReadString ();
4711 if (type == "string") {
4712 value = single_value;
4714 if (type == "int") {
4715 value = Int32.Parse (single_value);
4717 if (type == "byte") {
4718 value = Byte.Parse (single_value);
4720 if (type == "color") {
4721 int color = Int32.Parse (single_value);
4722 value = Color.FromArgb (color);
4724 if (type == "byte-array") {
4725 byte[] b_array = Convert.FromBase64String (single_value);
4729 ReadXmlArrayValues (xtr, type);
4733 private void ReadXmlArrayValues (XmlTextReader xtr, string type)
4735 ArrayList al = new ArrayList ();
4737 while (xtr.Read ()) {
4738 switch (xtr.NodeType) {
4739 case XmlNodeType.Element:
4740 string single_value = xtr.ReadString ();
4742 if (type == "int-array") {
4743 int int_val = Int32.Parse (single_value);
4746 if (type == "string-array") {
4747 string str_val = single_value;
4752 case XmlNodeType.EndElement:
4753 if (xtr.Name == "value") {
4754 if (type == "int-array") {
4755 value = al.ToArray (typeof(int));
4757 if (type == "string-array") {
4758 value = al.ToArray (typeof(string));
4767 public void WriteXml (XmlTextWriter xtw)
4769 xtw.WriteStartElement ("value");
4770 xtw.WriteAttributeString ("name", name);
4771 if (value is Array) {
4772 WriteArrayContent (xtw);
4774 WriteSingleContent (xtw);
4776 xtw.WriteEndElement ();
4779 private void WriteSingleContent (XmlTextWriter xtw)
4781 string type_string = String.Empty;
4783 if (value is string)
4784 type_string = "string";
4787 type_string = "int";
4790 type_string = "byte";
4793 type_string = "color";
4795 xtw.WriteAttributeString ("type", type_string);
4798 xtw.WriteString (((Color)value).ToArgb ().ToString ());
4800 xtw.WriteString (value.ToString ());
4803 private void WriteArrayContent (XmlTextWriter xtw)
4805 string type_string = String.Empty;
4806 string type_name = String.Empty;
4808 if (value is string[]) {
4809 type_string = "string-array";
4810 type_name = "string";
4812 if (value is int[]) {
4813 type_string = "int-array";
4816 if (value is byte[]) {
4817 type_string = "byte-array";
4821 xtw.WriteAttributeString ("type", type_string);
4823 if (type_string != "byte-array") {
4824 Array array = value as Array;
4826 foreach (object o in array) {
4827 xtw.WriteStartElement (type_name);
4828 xtw.WriteString (o.ToString ());
4829 xtw.WriteEndElement ();
4832 byte[] b_array = value as byte [];
4834 xtw.WriteString (Convert.ToBase64String (b_array, 0, b_array.Length));