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;
39 using System.Threading;
42 namespace System.Windows.Forms
45 [DefaultProperty ("FileName")]
46 [DefaultEvent ("FileOk")]
47 public abstract class FileDialog : CommonDialog
49 protected static readonly object EventFileOk = new object ();
50 private static int MaxFileNameItems = 10;
52 internal enum FileDialogType
58 private bool addExtension = true;
59 private bool checkFileExists;
60 private bool checkPathExists = true;
61 private string defaultExt;
62 private bool dereferenceLinks = true;
63 private string[] fileNames;
64 private string filter = "";
65 private int filterIndex = 1;
66 private int setFilterIndex = 1;
67 private string initialDirectory;
68 private bool restoreDirectory;
69 private bool showHelp;
71 private bool validateNames = true;
73 private bool checkForIllegalChars = true;
76 private Button cancelButton;
77 private ToolBarButton upToolBarButton;
78 private PopupButtonPanel popupButtonPanel;
79 private Button openSaveButton;
80 private Button helpButton;
81 private Label fileTypeLabel;
82 private ToolBarButton menueToolBarButton;
83 private ContextMenu menueToolBarButtonContextMenu;
84 private ToolBar smallButtonToolBar;
85 private DirComboBox dirComboBox;
86 private ComboBox fileNameComboBox;
87 private Label fileNameLabel;
88 private MWFFileView mwfFileView;
89 private Label searchSaveLabel;
90 private ToolBarButton newdirToolBarButton;
91 private ToolBarButton backToolBarButton;
92 private ComboBox fileTypeComboBox;
93 private ImageList imageListTopToolbar;
94 private CheckBox readonlyCheckBox;
96 private bool multiSelect;
98 private string restoreDirectoryString = String.Empty;
100 internal FileDialogType fileDialogType;
102 private bool do_not_call_OnSelectedIndexChangedFileTypeComboBox;
104 private bool showReadOnly;
105 private bool readOnlyChecked;
106 internal bool createPrompt;
107 internal bool overwritePrompt = true;
109 internal FileFilter fileFilter;
111 private string lastFolder = String.Empty;
115 private readonly string filedialog_string = "FileDialog";
116 private readonly string lastfolder_string = "LastFolder";
117 private readonly string width_string = "Width";
118 private readonly string height_string = "Height";
119 private readonly string filenames_string = "FileNames";
120 private readonly string x_string = "X";
121 private readonly string y_string = "Y";
123 private bool disable_form_closed_event = false;
125 internal FileDialog ()
129 Size formConfigSize = Size.Empty;
130 Point formConfigLocation = Point.Empty;
131 string[] configFileNames = null;
133 object formWidth = MWFConfig.GetValue (filedialog_string, width_string);
135 object formHeight = MWFConfig.GetValue (filedialog_string, height_string);
137 if (formHeight != null && formWidth != null)
138 formConfigSize = new Size ((int)formWidth, (int)formHeight);
140 object formLocationX = MWFConfig.GetValue (filedialog_string, x_string);
141 object formLocationY = MWFConfig.GetValue (filedialog_string, y_string);
143 if (formLocationX != null && formLocationY != null)
144 formConfigLocation = new Point ((int)formLocationX, (int)formLocationY);
146 configFileNames = (string[])MWFConfig.GetValue (filedialog_string, filenames_string);
148 fileTypeComboBox = new ComboBox ();
149 backToolBarButton = new ToolBarButton ();
150 newdirToolBarButton = new ToolBarButton ();
151 searchSaveLabel = new Label ();
152 mwfFileView = new MWFFileView (vfs);
153 fileNameLabel = new Label ();
154 fileNameComboBox = new ComboBox ();
155 dirComboBox = new DirComboBox (vfs);
156 smallButtonToolBar = new ToolBar ();
157 menueToolBarButton = new ToolBarButton ();
158 fileTypeLabel = new Label ();
159 openSaveButton = new Button ();
160 form.AcceptButton = openSaveButton;
161 helpButton = new Button ();
162 popupButtonPanel = new PopupButtonPanel ();
163 upToolBarButton = new ToolBarButton ();
164 cancelButton = new Button ();
165 form.CancelButton = cancelButton;
166 imageListTopToolbar = new ImageList ();
167 menueToolBarButtonContextMenu = new ContextMenu ();
168 readonlyCheckBox = new CheckBox ();
170 form.SuspendLayout ();
172 //imageListTopToolbar
173 imageListTopToolbar.ColorDepth = ColorDepth.Depth32Bit;
174 imageListTopToolbar.ImageSize = new Size (16, 16); // 16, 16
175 imageListTopToolbar.Images.Add (ResourceImageLoader.Get ("go-previous.png"));
176 imageListTopToolbar.Images.Add (ResourceImageLoader.Get ("go-top.png"));
177 imageListTopToolbar.Images.Add (ResourceImageLoader.Get ("folder-new.png"));
178 imageListTopToolbar.Images.Add (ResourceImageLoader.Get ("preferences-system-windows.png"));
179 imageListTopToolbar.TransparentColor = Color.Transparent;
182 searchSaveLabel.FlatStyle = FlatStyle.System;
183 searchSaveLabel.Location = new Point (7, 8);
184 searchSaveLabel.Size = new Size (72, 21);
185 searchSaveLabel.TextAlign = ContentAlignment.MiddleRight;
188 dirComboBox.Anchor = ((AnchorStyles)(((AnchorStyles.Top | AnchorStyles.Left) | AnchorStyles.Right)));
189 dirComboBox.DropDownStyle = ComboBoxStyle.DropDownList;
190 dirComboBox.Location = new Point (99, 8);
191 dirComboBox.Size = new Size (260, 21);
192 dirComboBox.TabIndex = 7;
194 // smallButtonToolBar
195 smallButtonToolBar.Anchor = ((AnchorStyles)((AnchorStyles.Top | AnchorStyles.Right)));
196 smallButtonToolBar.Appearance = ToolBarAppearance.Flat;
197 smallButtonToolBar.AutoSize = false;
198 smallButtonToolBar.Buttons.AddRange (new ToolBarButton [] {
202 menueToolBarButton});
203 smallButtonToolBar.ButtonSize = new Size (24, 24); // 21, 16
204 smallButtonToolBar.Divider = false;
205 smallButtonToolBar.Dock = DockStyle.None;
206 smallButtonToolBar.DropDownArrows = true;
207 smallButtonToolBar.ImageList = imageListTopToolbar;
208 smallButtonToolBar.Location = new Point (372, 6);
209 smallButtonToolBar.ShowToolTips = true;
210 smallButtonToolBar.Size = new Size (140, 28);
211 smallButtonToolBar.TabIndex = 8;
212 smallButtonToolBar.TextAlign = ToolBarTextAlign.Right;
215 popupButtonPanel.Dock = DockStyle.None;
216 popupButtonPanel.Location = new Point (7, 37);
217 popupButtonPanel.TabIndex = 9;
220 mwfFileView.Anchor = ((AnchorStyles)((((AnchorStyles.Top | AnchorStyles.Bottom) | AnchorStyles.Left) | AnchorStyles.Right)));
221 mwfFileView.Location = new Point (99, 37);
222 mwfFileView.Size = new Size (449, 282);
223 mwfFileView.Columns.Add (" Name", 170, HorizontalAlignment.Left);
224 mwfFileView.Columns.Add ("Size ", 80, HorizontalAlignment.Right);
225 mwfFileView.Columns.Add (" Type", 100, HorizontalAlignment.Left);
226 mwfFileView.Columns.Add (" Last Access", 150, HorizontalAlignment.Left);
227 mwfFileView.AllowColumnReorder = true;
228 mwfFileView.MultiSelect = false;
229 mwfFileView.TabIndex = 10;
230 mwfFileView.RegisterSender (dirComboBox);
231 mwfFileView.RegisterSender (popupButtonPanel);
234 fileNameLabel.Anchor = ((AnchorStyles)((AnchorStyles.Bottom | AnchorStyles.Left)));
235 fileNameLabel.FlatStyle = FlatStyle.System;
236 fileNameLabel.Location = new Point (102, 330);
237 fileNameLabel.Size = new Size (70, 21);
238 fileNameLabel.Text = "File name:";
239 fileNameLabel.TextAlign = ContentAlignment.MiddleLeft;
242 fileNameComboBox.Anchor = ((AnchorStyles)(((AnchorStyles.Bottom | AnchorStyles.Left) | AnchorStyles.Right)));
243 fileNameComboBox.Location = new Point (195, 330);
244 fileNameComboBox.Size = new Size (245, 21);
245 fileNameComboBox.TabIndex = 1;
246 fileNameComboBox.MaxDropDownItems = MaxFileNameItems;
248 if (configFileNames != null) {
249 foreach (string configFileName in configFileNames) {
250 if (configFileName == null || configFileName.Trim ().Length == 0)
252 // add no more than 10 items
253 if (fileNameComboBox.Items.Count >= MaxFileNameItems)
255 fileNameComboBox.Items.Add (configFileName);
260 fileTypeLabel.Anchor = ((AnchorStyles)((AnchorStyles.Bottom | AnchorStyles.Left)));
261 fileTypeLabel.FlatStyle = FlatStyle.System;
262 fileTypeLabel.Location = new Point (102, 356);
263 fileTypeLabel.Size = new Size (90, 21);
264 fileTypeLabel.Text = "Files of type:";
265 fileTypeLabel.TextAlign = ContentAlignment.MiddleLeft;
268 fileTypeComboBox.Anchor = ((AnchorStyles)(((AnchorStyles.Bottom | AnchorStyles.Left) | AnchorStyles.Right)));
269 fileTypeComboBox.DropDownStyle = ComboBoxStyle.DropDownList;
270 fileTypeComboBox.Location = new Point (195, 356);
271 fileTypeComboBox.Size = new Size (245, 21);
272 fileTypeComboBox.TabIndex = 2;
275 backToolBarButton.ImageIndex = 0;
276 backToolBarButton.Enabled = false;
277 backToolBarButton.Style = ToolBarButtonStyle.PushButton;
278 mwfFileView.AddControlToEnableDisableByDirStack (backToolBarButton);
281 upToolBarButton.ImageIndex = 1;
282 upToolBarButton.Style = ToolBarButtonStyle.PushButton;
283 mwfFileView.SetFolderUpToolBarButton (upToolBarButton);
285 // newdirToolBarButton
286 newdirToolBarButton.ImageIndex = 2;
287 newdirToolBarButton.Style = ToolBarButtonStyle.PushButton;
289 // menueToolBarButton
290 menueToolBarButton.ImageIndex = 3;
291 menueToolBarButton.DropDownMenu = menueToolBarButtonContextMenu;
292 menueToolBarButton.Style = ToolBarButtonStyle.DropDownButton;
294 // menueToolBarButtonContextMenu
295 menueToolBarButtonContextMenu.MenuItems.AddRange (mwfFileView.ViewMenuItems);
298 openSaveButton.Anchor = ((AnchorStyles)((AnchorStyles.Bottom | AnchorStyles.Right)));
299 openSaveButton.FlatStyle = FlatStyle.System;
300 openSaveButton.Location = new Point (475, 330);
301 openSaveButton.Size = new Size (72, 21);
302 openSaveButton.TabIndex = 4;
303 openSaveButton.FlatStyle = FlatStyle.System;
306 cancelButton.Anchor = ((AnchorStyles)((AnchorStyles.Bottom | AnchorStyles.Right)));
307 cancelButton.FlatStyle = FlatStyle.System;
308 cancelButton.Location = new Point (475, 356);
309 cancelButton.Size = new Size (72, 21);
310 cancelButton.TabIndex = 5;
311 cancelButton.Text = "Cancel";
312 cancelButton.FlatStyle = FlatStyle.System;
315 helpButton.Anchor = ((AnchorStyles)((AnchorStyles.Bottom | AnchorStyles.Right)));
316 helpButton.FlatStyle = FlatStyle.System;
317 helpButton.Location = new Point (475, 350);
318 helpButton.Size = new Size (72, 21);
319 helpButton.TabIndex = 6;
320 helpButton.Text = "Help";
321 helpButton.FlatStyle = FlatStyle.System;
324 readonlyCheckBox.Anchor = ((AnchorStyles)(((AnchorStyles.Bottom | AnchorStyles.Left) | AnchorStyles.Right)));
325 readonlyCheckBox.Text = "Open Readonly";
326 readonlyCheckBox.Location = new Point (195, 350);
327 readonlyCheckBox.Size = new Size (245, 21);
328 readonlyCheckBox.TabIndex = 3;
329 readonlyCheckBox.FlatStyle = FlatStyle.System;
331 form.SizeGripStyle = SizeGripStyle.Show;
333 form.MaximizeBox = true;
334 form.MinimizeBox = true;
335 form.FormBorderStyle = FormBorderStyle.Sizable;
336 form.MinimumSize = new Size (554, 405);
338 form.ClientSize = new Size (554, 405); // 384
340 form.Controls.Add (smallButtonToolBar);
341 form.Controls.Add (cancelButton);
342 form.Controls.Add (openSaveButton);
343 form.Controls.Add (mwfFileView);
344 form.Controls.Add (fileTypeLabel);
345 form.Controls.Add (fileNameLabel);
346 form.Controls.Add (fileTypeComboBox);
347 form.Controls.Add (fileNameComboBox);
348 form.Controls.Add (dirComboBox);
349 form.Controls.Add (searchSaveLabel);
350 form.Controls.Add (popupButtonPanel);
352 form.ResumeLayout (false);
354 if (formConfigSize != Size.Empty) {
355 form.Size = formConfigSize;
358 if (formConfigLocation != Point.Empty) {
359 form.Location = formConfigLocation;
362 openSaveButton.Click += new EventHandler (OnClickOpenSaveButton);
363 cancelButton.Click += new EventHandler (OnClickCancelButton);
364 helpButton.Click += new EventHandler (OnClickHelpButton);
366 smallButtonToolBar.ButtonClick += new ToolBarButtonClickEventHandler (OnClickSmallButtonToolBar);
368 fileTypeComboBox.SelectedIndexChanged += new EventHandler (OnSelectedIndexChangedFileTypeComboBox);
370 mwfFileView.SelectedFileChanged += new EventHandler (OnSelectedFileChangedFileView);
371 mwfFileView.ForceDialogEnd += new EventHandler (OnForceDialogEndFileView);
372 mwfFileView.SelectedFilesChanged += new EventHandler (OnSelectedFilesChangedFileView);
374 dirComboBox.DirectoryChanged += new EventHandler (OnDirectoryChangedDirComboBox);
375 popupButtonPanel.DirectoryChanged += new EventHandler (OnDirectoryChangedPopupButtonPanel);
377 readonlyCheckBox.CheckedChanged += new EventHandler (OnCheckCheckChanged);
379 form.FormClosed += new FormClosedEventHandler (OnFileDialogFormClosed);
381 form.Closed += new EventHandler (OnFileDialogFormClosed);
386 public bool AddExtension {
392 addExtension = value;
396 [DefaultValue(false)]
397 public virtual bool CheckFileExists {
399 return checkFileExists;
403 checkFileExists = value;
408 public bool CheckPathExists {
410 return checkPathExists;
414 checkPathExists = value;
419 public string DefaultExt {
421 if (defaultExt == null)
426 if (value != null && value.Length > 0) {
427 // remove leading dot
428 if (value [0] == '.')
429 value = value.Substring (1);
435 // in MS.NET it doesn't make a difference if
436 // DerefenceLinks is true or false
437 // if the selected file is a link FileDialog
438 // always returns the link
440 public bool DereferenceLinks {
442 return dereferenceLinks;
446 dereferenceLinks = value;
451 public string FileName {
453 if (fileNames == null || fileNames.Length == 0)
456 if (fileNames [0].Length == 0)
460 // skip check for illegal characters if the filename was set
461 // through FileDialog API
462 if (!checkForIllegalChars)
463 return fileNames [0];
466 // ensure filename contains only valid characters
467 Path.GetFullPath (fileNames [0]);
468 // but return filename as is
469 return fileNames [0];
475 fileNames = new string [1] { value };
477 fileNames = new string [0];
481 // skip check for illegal characters if the filename was set
482 // through FileDialog API
483 checkForIllegalChars = false;
489 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
490 public string[] FileNames {
492 if (fileNames == null || fileNames.Length == 0) {
493 return new string [0];
496 string[] new_filenames = new string [fileNames.Length];
497 fileNames.CopyTo (new_filenames, 0);
500 // skip check for illegal characters if the filename was set
501 // through FileDialog API
502 if (!checkForIllegalChars)
503 return new_filenames;
506 foreach (string fileName in new_filenames) {
507 // ensure filename contains only valid characters
508 Path.GetFullPath (fileName);
510 return new_filenames;
516 public string Filter {
524 if (fileFilter != null)
525 fileFilter.FilterArrayList.Clear ();
527 if (FileFilter.CheckFilter (value)) {
530 fileFilter = new FileFilter (filter);
532 throw new ArgumentException ("The provided filter string"
533 + " is invalid. The filter string should contain a"
534 + " description of the filter, followed by the "
535 + " vertical bar (|) and the filter pattern. The"
536 + " strings for different filtering options should"
537 + " also be separated by the vertical bar. Example:"
538 + " Text files (*.txt)|*.txt|All files (*.*)|*.*");
546 public int FilterIndex {
548 return setFilterIndex;
552 setFilterIndex = value;
564 public string InitialDirectory {
566 if (initialDirectory == null)
568 return initialDirectory;
571 initialDirectory = value;
575 [DefaultValue(false)]
576 public bool RestoreDirectory {
578 return restoreDirectory;
582 restoreDirectory = value;
586 [DefaultValue(false)]
587 public bool ShowHelp {
594 ResizeAndRelocateForHelpOrReadOnly ();
600 public string Title {
611 // this one is a hard one ;)
613 // - up to MAX_PATH characters (windef.h) = 260
614 // - no trailing dots or spaces
617 // NTFS/Posix filename:
618 // - up to 32,768 Unicode characters
619 // - trailing periods or spaces
623 public bool ValidateNames {
625 return validateNames;
629 validateNames = value;
633 public override void Reset ()
636 checkFileExists = false;
637 checkPathExists = true;
639 dereferenceLinks = true;
641 Filter = String.Empty;
643 InitialDirectory = null;
644 restoreDirectory = false;
647 validateNames = true;
652 public override string ToString ()
654 return String.Format("{0}: Title: {1}, FileName: {2}", base.ToString (), Title, FileName);
657 public event CancelEventHandler FileOk {
658 add { Events.AddHandler (EventFileOk, value); }
659 remove { Events.RemoveHandler (EventFileOk, value); }
662 protected virtual IntPtr Instance {
670 // This is just for internal use with MSs version, so it doesn't need to be implemented
671 // as it can't really be accessed anyways
672 protected int Options {
676 internal virtual string DialogTitle {
683 protected override IntPtr HookProc (IntPtr hWnd, int msg, IntPtr wparam, IntPtr lparam)
685 throw new NotImplementedException ();
688 protected void OnFileOk (CancelEventArgs e)
690 WriteConfigValues (e);
692 CancelEventHandler fo = (CancelEventHandler) Events [EventFileOk];
696 Mime.CleanFileCache ();
698 disable_form_closed_event = true;
701 protected override bool RunDialog (IntPtr hWndOwner)
704 form.Text = DialogTitle;
706 // avoid using the FileNames property to skip the invalid characters
708 string fileName = null;
709 if (fileNames != null && fileNames.Length != 0)
710 fileName = fileNames [0];
712 fileName = string.Empty;
716 SetFileAndDirectory (fileName);
717 fileNameComboBox.Select ();
722 internal virtual bool ShowReadOnly {
724 showReadOnly = value;
725 ResizeAndRelocateForHelpOrReadOnly ();
733 internal virtual bool ReadOnlyChecked {
735 readOnlyChecked = value;
736 readonlyCheckBox.Checked = value;
740 return readOnlyChecked;
744 internal bool BMultiSelect {
747 mwfFileView.MultiSelect = value;
755 internal string OpenSaveButtonText {
757 openSaveButton.Text = value;
761 internal string SearchSaveLabel {
763 searchSaveLabel.Text = value;
767 internal string FileTypeLabel {
769 fileTypeLabel.Text = value;
773 private void SelectFilter ()
775 if (mwfFileView.FilterArrayList == null || filterIndex > mwfFileView.FilterArrayList.Count)
778 do_not_call_OnSelectedIndexChangedFileTypeComboBox = true;
779 fileTypeComboBox.BeginUpdate ();
780 fileTypeComboBox.SelectedIndex = filterIndex - 1;
781 fileTypeComboBox.EndUpdate ();
782 do_not_call_OnSelectedIndexChangedFileTypeComboBox = false;
783 mwfFileView.FilterIndex = filterIndex;
786 private void SetFileAndDirectory (string fname)
788 if (fname.Length != 0) {
789 bool rooted = Path.IsPathRooted (fname);
791 mwfFileView.ChangeDirectory (null, lastFolder);
792 fileNameComboBox.Text = fname;
794 string dirname = Path.GetDirectoryName (fname);
795 if (dirname != null && dirname.Length > 0 && Directory.Exists (dirname)) {
796 fileNameComboBox.Text = Path.GetFileName (fname);
797 mwfFileView.ChangeDirectory (null, dirname);
799 fileNameComboBox.Text = fname;
800 mwfFileView.ChangeDirectory (null, lastFolder);
804 mwfFileView.ChangeDirectory (null, lastFolder);
805 fileNameComboBox.Text = null;
809 void OnClickOpenSaveButton (object sender, EventArgs e)
812 // for filenames typed or selected by user, enable check for
813 // illegal characters in filename(s)
814 checkForIllegalChars = true;
817 if (fileDialogType == FileDialogType.OpenFileDialog) {
818 ListView.SelectedListViewItemCollection sl = mwfFileView.SelectedItems;
819 if (sl.Count > 0 && sl [0] != null) {
821 FileViewListViewItem item = sl [0] as FileViewListViewItem;
822 FSEntry fsEntry = item.FSEntry;
824 if (fsEntry.Attributes == FileAttributes.Directory) {
825 mwfFileView.ChangeDirectory (null, fsEntry.FullName);
829 foreach (FileViewListViewItem item in sl) {
830 FSEntry fsEntry = item.FSEntry;
831 if (fsEntry.Attributes == FileAttributes.Directory) {
832 mwfFileView.ChangeDirectory (null, fsEntry.FullName);
840 ArrayList files = new ArrayList ();
841 FileNamesTokenizer tokenizer = new FileNamesTokenizer (
842 fileNameComboBox.Text, multiSelect);
843 tokenizer.GetNextFile ();
844 while (tokenizer.CurrentToken != TokenType.EOF) {
845 string fileName = tokenizer.TokenText;
846 string internalfullfilename;
848 if (!Path.IsPathRooted (fileName)) {
849 // on unix currentRealFolder for "Recently used files" is null,
850 // because recently used files don't get saved as links in a directory
851 // recently used files get saved in a xml file
852 if (mwfFileView.CurrentRealFolder != null)
853 fileName = Path.Combine (mwfFileView.CurrentRealFolder, fileName);
855 if (mwfFileView.CurrentFSEntry != null) {
856 fileName = mwfFileView.CurrentFSEntry.FullName;
860 FileInfo fileInfo = new FileInfo (fileName);
862 if (fileInfo.Exists || fileDialogType == FileDialogType.SaveFileDialog) {
863 internalfullfilename = fileName;
865 DirectoryInfo dirInfo = new DirectoryInfo (fileName);
866 if (dirInfo.Exists) {
867 mwfFileView.ChangeDirectory (null, dirInfo.FullName);
868 fileNameComboBox.Text = null;
871 internalfullfilename = fileName;
875 if (fileDialogType == FileDialogType.OpenFileDialog) {
876 if (checkFileExists) {
877 if (!File.Exists (internalfullfilename)) {
878 string message = "\"" + internalfullfilename + "\" doesn't exist. Please verify that you have entered the correct file name.";
879 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);
888 if (dr == DialogResult.Cancel)
894 if (!File.Exists (internalfullfilename)) {
895 string message = "\"" + internalfullfilename + "\" doesn't exist. Create ?";
896 DialogResult dr = MessageBox.Show (message, openSaveButton.Text, MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
897 if (dr == DialogResult.Cancel)
903 string extension_to_use = String.Empty;
904 string filter_exentsion = String.Empty;
906 if (fileFilter != null) {
907 FilterStruct filterstruct = (FilterStruct) fileFilter.FilterArrayList [filterIndex - 1];
909 for (int i = 0; i < filterstruct.filters.Count; i++) {
910 string extension = filterstruct.filters [i];
912 if (extension.StartsWith ("*"))
913 extension = extension.Remove (0, 1);
915 if (extension.IndexOf ('*') != -1)
918 filter_exentsion = extension;
923 if (filter_exentsion != String.Empty)
924 extension_to_use = filter_exentsion;
926 if (DefaultExt.Length > 0)
927 extension_to_use = "." + DefaultExt;
929 if (!internalfullfilename.EndsWith (extension_to_use))
930 internalfullfilename += extension_to_use;
934 files.Add (internalfullfilename);
935 tokenizer.GetNextFile ();
938 if (files.Count > 0) {
939 fileNames = new string [files.Count];
940 for (int i = 0; i < files.Count; i++) {
941 string fileName = (string) files [i];
942 fileNames [i] = fileName;
943 mwfFileView.WriteRecentlyUsed (fileName);
945 if (!File.Exists (fileName))
946 // ignore files that do not exist
949 if (fileNameComboBox.Items.IndexOf (fileName) == -1)
950 fileNameComboBox.Items.Insert (0, fileName);
953 // remove items above the maximum items that we want to display
954 while (fileNameComboBox.Items.Count > MaxFileNameItems)
955 fileNameComboBox.Items.RemoveAt (MaxFileNameItems);
958 if (checkPathExists && mwfFileView.CurrentRealFolder != null) {
959 if (!Directory.Exists (mwfFileView.CurrentRealFolder)) {
960 string message = "\"" + mwfFileView.CurrentRealFolder + "\" doesn't exist. Please verify that you have entered the correct directory name.";
961 MessageBox.Show (message, openSaveButton.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
963 if (InitialDirectory.Length == 0 || !Directory.Exists (InitialDirectory))
964 mwfFileView.ChangeDirectory (null, lastFolder);
966 mwfFileView.ChangeDirectory (null, InitialDirectory);
971 if (restoreDirectory) {
972 lastFolder = restoreDirectoryString;
974 lastFolder = mwfFileView.CurrentFolder;
977 CancelEventArgs cancelEventArgs = new CancelEventArgs ();
979 cancelEventArgs.Cancel = false;
981 OnFileOk (cancelEventArgs);
983 form.DialogResult = DialogResult.OK;
986 void OnClickCancelButton (object sender, EventArgs e)
988 if (restoreDirectory)
989 mwfFileView.CurrentFolder = restoreDirectoryString;
991 CancelEventArgs cancelEventArgs = new CancelEventArgs ();
993 cancelEventArgs.Cancel = true;
995 OnFileOk (cancelEventArgs);
997 form.DialogResult = DialogResult.Cancel;
1000 void OnClickHelpButton (object sender, EventArgs e)
1005 void OnClickSmallButtonToolBar (object sender, ToolBarButtonClickEventArgs e)
1007 if (e.Button == upToolBarButton) {
1008 mwfFileView.OneDirUp ();
1010 if (e.Button == backToolBarButton) {
1011 mwfFileView.PopDir ();
1013 if (e.Button == newdirToolBarButton) {
1014 mwfFileView.CreateNewFolder ();
1018 void OnSelectedIndexChangedFileTypeComboBox (object sender, EventArgs e)
1020 if (do_not_call_OnSelectedIndexChangedFileTypeComboBox) {
1021 do_not_call_OnSelectedIndexChangedFileTypeComboBox = false;
1025 filterIndex = fileTypeComboBox.SelectedIndex + 1;
1027 mwfFileView.FilterIndex = filterIndex;
1030 void OnSelectedFileChangedFileView (object sender, EventArgs e)
1032 fileNameComboBox.Text = mwfFileView.CurrentFSEntry.Name;
1035 void OnSelectedFilesChangedFileView (object sender, EventArgs e)
1037 string selectedFiles = mwfFileView.SelectedFilesString;
1038 if (selectedFiles != null && selectedFiles.Length != 0)
1039 fileNameComboBox.Text = selectedFiles;
1042 void OnForceDialogEndFileView (object sender, EventArgs e)
1044 OnClickOpenSaveButton (this, EventArgs.Empty);
1047 void OnDirectoryChangedDirComboBox (object sender, EventArgs e)
1049 mwfFileView.ChangeDirectory (sender, dirComboBox.CurrentFolder);
1052 void OnDirectoryChangedPopupButtonPanel (object sender, EventArgs e)
1054 mwfFileView.ChangeDirectory (sender, popupButtonPanel.CurrentFolder);
1057 void OnCheckCheckChanged (object sender, EventArgs e)
1059 ReadOnlyChecked = readonlyCheckBox.Checked;
1063 void OnFileDialogFormClosed (object sender, FormClosedEventArgs e)
1065 HandleFormClosedEvent (sender);
1068 void OnFileDialogFormClosed (object sender, EventArgs e)
1070 HandleFormClosedEvent (sender);
1074 void HandleFormClosedEvent (object sender)
1076 if (!disable_form_closed_event)
1077 OnClickCancelButton (sender, EventArgs.Empty);
1079 disable_form_closed_event = false;
1082 private void UpdateFilters ()
1084 if (fileFilter == null)
1085 fileFilter = new FileFilter ();
1087 ArrayList filters = fileFilter.FilterArrayList;
1089 fileTypeComboBox.BeginUpdate ();
1091 fileTypeComboBox.Items.Clear ();
1093 foreach (FilterStruct fs in filters) {
1094 fileTypeComboBox.Items.Add (fs.filterName);
1097 if (filters.Count > 0 && FilterIndex <= filters.Count)
1098 fileTypeComboBox.SelectedIndex = filterIndex - 1;
1100 fileTypeComboBox.EndUpdate ();
1102 mwfFileView.FilterArrayList = filters;
1104 mwfFileView.FilterIndex = filterIndex;
1107 private void ResizeAndRelocateForHelpOrReadOnly ()
1109 form.SuspendLayout ();
1110 if (ShowHelp || ShowReadOnly) {
1111 mwfFileView.Size = new Size (449, 250);
1112 fileNameLabel.Location = new Point (102, 298);
1113 fileNameComboBox.Location = new Point (195, 298);
1114 fileTypeLabel.Location = new Point (102, 324);
1115 fileTypeComboBox.Location = new Point (195, 324);
1116 openSaveButton.Location = new Point (475, 298);
1117 cancelButton.Location = new Point (475, 324);
1119 mwfFileView.Size = new Size (449, 282);
1120 fileNameLabel.Location = new Point (102, 330);
1121 fileNameComboBox.Location = new Point (195, 330);
1122 fileTypeLabel.Location = new Point (102, 356);
1123 fileTypeComboBox.Location = new Point (195, 356);
1124 openSaveButton.Location = new Point (475, 330);
1125 cancelButton.Location = new Point (475, 356);
1129 form.Controls.Add (helpButton);
1131 form.Controls.Remove (helpButton);
1134 form.Controls.Add (readonlyCheckBox);
1136 form.Controls.Remove (readonlyCheckBox);
1137 form.ResumeLayout ();
1140 private void WriteConfigValues (CancelEventArgs ce)
1142 MWFConfig.SetValue (filedialog_string, width_string, form.Width);
1143 MWFConfig.SetValue (filedialog_string, height_string, form.Height);
1144 MWFConfig.SetValue (filedialog_string, x_string, form.Location.X);
1145 MWFConfig.SetValue (filedialog_string, y_string, form.Location.Y);
1148 MWFConfig.SetValue (filedialog_string, lastfolder_string, lastFolder);
1150 string[] fileNameCBItems = new string [fileNameComboBox.Items.Count];
1152 fileNameComboBox.Items.CopyTo (fileNameCBItems, 0);
1154 MWFConfig.SetValue (filedialog_string, filenames_string, fileNameCBItems);
1158 private void ReadConfigValues ()
1160 lastFolder = (string)MWFConfig.GetValue (filedialog_string, lastfolder_string);
1162 if (lastFolder != null && lastFolder.IndexOf ("://") == -1) {
1163 if (!Directory.Exists (lastFolder)) {
1164 lastFolder = MWFVFS.DesktopPrefix;
1168 if (InitialDirectory.Length > 0 && Directory.Exists (InitialDirectory))
1169 lastFolder = InitialDirectory;
1171 if (lastFolder == null || lastFolder.Length == 0)
1172 lastFolder = Environment.CurrentDirectory;
1174 if (RestoreDirectory)
1175 restoreDirectoryString = lastFolder;
1178 class FileNamesTokenizer
1180 public FileNamesTokenizer (string text, bool allowMultiple)
1184 _tokenType = TokenType.BOF;
1185 _allowMultiple = allowMultiple;
1188 public TokenType CurrentToken {
1189 get { return _tokenType; }
1192 public string TokenText {
1193 get { return _tokenText; }
1196 public bool AllowMultiple {
1197 get { return _allowMultiple; }
1200 private int ReadChar ()
1202 if (_position < _text.Length) {
1203 return _text [_position++];
1209 private int PeekChar ()
1211 if (_position < _text.Length) {
1212 return _text [_position];
1218 private void SkipWhitespaceAndQuotes ()
1222 while ((ch = PeekChar ()) != -1) {
1223 if ((char) ch != '"' && !char.IsWhiteSpace ((char) ch))
1229 public void GetNextFile ()
1231 if (_tokenType == TokenType.EOF)
1232 throw new Exception ("");
1236 SkipWhitespaceAndQuotes ();
1238 if (PeekChar () == -1) {
1239 _tokenType = TokenType.EOF;
1243 _tokenType = TokenType.FileName;
1244 StringBuilder sb = new StringBuilder ();
1246 while ((ch = PeekChar ()) != -1) {
1247 if ((char) ch == '"') {
1251 int pos = _position;
1253 SkipWhitespaceAndQuotes ();
1254 if (PeekChar () == -1) {
1258 sb.Append ((char) ch);
1260 sb.Append ((char) ReadChar ());
1264 _tokenText = sb.ToString ();
1267 private readonly bool _allowMultiple;
1268 private int _position;
1269 private readonly string _text;
1270 private TokenType _tokenType;
1271 private string _tokenText;
1274 internal enum TokenType
1283 #region PopupButtonPanel
1284 internal class PopupButtonPanel : Control, IUpdateFolder
1287 internal class PopupButton : Control
1289 internal enum PopupButtonState
1292 private Image image = null;
1293 private PopupButtonState popupButtonState = PopupButtonState.Normal;
1294 private StringFormat text_format = new StringFormat();
1295 private Rectangle text_rect = Rectangle.Empty;
1297 public PopupButton ()
1299 text_format.Alignment = StringAlignment.Center;
1300 text_format.LineAlignment = StringAlignment.Near;
1302 SetStyle (ControlStyles.DoubleBuffer, true);
1303 SetStyle (ControlStyles.AllPaintingInWmPaint, true);
1304 SetStyle (ControlStyles.UserPaint, true);
1305 SetStyle (ControlStyles.Selectable, false);
1308 public Image Image {
1319 public PopupButtonState ButtonState {
1321 popupButtonState = value;
1326 return popupButtonState;
1330 protected override void OnPaint (PaintEventArgs pe)
1337 private void Draw (PaintEventArgs pe)
1339 Graphics gr = pe.Graphics;
1341 gr.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (BackColor), ClientRectangle);
1344 if (image != null) {
1345 int i_x = (ClientSize.Width - image.Width) / 2;
1347 gr.DrawImage (image, i_x, i_y);
1350 if (Text != String.Empty) {
1351 if (text_rect == Rectangle.Empty)
1352 text_rect = new Rectangle (0, Height - 30, Width, Height - 30);
1354 gr.DrawString (Text, Font, Brushes.White, text_rect, text_format);
1357 switch (popupButtonState) {
1358 case PopupButtonState.Up:
1359 gr.DrawLine (ThemeEngine.Current.ResPool.GetPen (Color.White), 0, 0, ClientSize.Width - 1, 0);
1360 gr.DrawLine (ThemeEngine.Current.ResPool.GetPen (Color.White), 0, 0, 0, ClientSize.Height - 1);
1361 gr.DrawLine (ThemeEngine.Current.ResPool.GetPen (Color.Black), ClientSize.Width - 1, 0, ClientSize.Width - 1, ClientSize.Height - 1);
1362 gr.DrawLine (ThemeEngine.Current.ResPool.GetPen (Color.Black), 0, ClientSize.Height - 1, ClientSize.Width - 1, ClientSize.Height - 1);
1365 case PopupButtonState.Down:
1366 gr.DrawLine (ThemeEngine.Current.ResPool.GetPen (Color.Black), 0, 0, ClientSize.Width - 1, 0);
1367 gr.DrawLine (ThemeEngine.Current.ResPool.GetPen (Color.Black), 0, 0, 0, ClientSize.Height - 1);
1368 gr.DrawLine (ThemeEngine.Current.ResPool.GetPen (Color.White), ClientSize.Width - 1, 0, ClientSize.Width - 1, ClientSize.Height - 1);
1369 gr.DrawLine (ThemeEngine.Current.ResPool.GetPen (Color.White), 0, ClientSize.Height - 1, ClientSize.Width - 1, ClientSize.Height - 1);
1374 protected override void OnMouseEnter (EventArgs e)
1376 if (popupButtonState != PopupButtonState.Down)
1377 popupButtonState = PopupButtonState.Up;
1379 PopupButtonPanel panel = Parent as PopupButtonPanel;
1381 if (panel.focusButton != null && panel.focusButton.ButtonState == PopupButtonState.Up) {
1382 panel.focusButton.ButtonState = PopupButtonState.Normal;
1383 panel.focusButton = null;
1386 base.OnMouseEnter (e);
1389 protected override void OnMouseLeave (EventArgs e)
1391 if (popupButtonState == PopupButtonState.Up)
1392 popupButtonState = PopupButtonState.Normal;
1394 base.OnMouseLeave (e);
1397 protected override void OnClick (EventArgs e)
1399 popupButtonState = PopupButtonState.Down;
1406 private PopupButton recentlyusedButton;
1407 private PopupButton desktopButton;
1408 private PopupButton personalButton;
1409 private PopupButton mycomputerButton;
1410 private PopupButton networkButton;
1412 private PopupButton lastPopupButton = null;
1413 private PopupButton focusButton = null;
1415 private string currentPath;
1417 private int currentFocusIndex;
1419 public PopupButtonPanel ()
1423 BackColor = Color.FromArgb (128, 128, 128);
1424 Size = new Size (85, 336);
1426 recentlyusedButton = new PopupButton ();
1427 desktopButton = new PopupButton ();
1428 personalButton = new PopupButton ();
1429 mycomputerButton = new PopupButton ();
1430 networkButton = new PopupButton ();
1432 recentlyusedButton.Size = new Size (81, 64);
1433 recentlyusedButton.Image = ThemeEngine.Current.Images (UIIcon.PlacesRecentDocuments, 32);
1434 recentlyusedButton.BackColor = BackColor;
1435 recentlyusedButton.ForeColor = Color.Black;
1436 recentlyusedButton.Location = new Point (2, 2);
1437 recentlyusedButton.Text = "Recently\nused";
1438 recentlyusedButton.Click += new EventHandler (OnClickButton);
1440 desktopButton.Image = ThemeEngine.Current.Images (UIIcon.PlacesDesktop, 32);
1441 desktopButton.BackColor = BackColor;
1442 desktopButton.ForeColor = Color.Black;
1443 desktopButton.Size = new Size (81, 64);
1444 desktopButton.Location = new Point (2, 66);
1445 desktopButton.Text = "Desktop";
1446 desktopButton.Click += new EventHandler (OnClickButton);
1448 personalButton.Image = ThemeEngine.Current.Images (UIIcon.PlacesPersonal, 32);
1449 personalButton.BackColor = BackColor;
1450 personalButton.ForeColor = Color.Black;
1451 personalButton.Size = new Size (81, 64);
1452 personalButton.Location = new Point (2, 130);
1453 personalButton.Text = "Personal";
1454 personalButton.Click += new EventHandler (OnClickButton);
1456 mycomputerButton.Image = ThemeEngine.Current.Images (UIIcon.PlacesMyComputer, 32);
1457 mycomputerButton.BackColor = BackColor;
1458 mycomputerButton.ForeColor = Color.Black;
1459 mycomputerButton.Size = new Size (81, 64);
1460 mycomputerButton.Location = new Point (2, 194);
1461 mycomputerButton.Text = "My Computer";
1462 mycomputerButton.Click += new EventHandler (OnClickButton);
1464 networkButton.Image = ThemeEngine.Current.Images (UIIcon.PlacesMyNetwork, 32);
1465 networkButton.BackColor = BackColor;
1466 networkButton.ForeColor = Color.Black;
1467 networkButton.Size = new Size (81, 64);
1468 networkButton.Location = new Point (2, 258);
1469 networkButton.Text = "My Network";
1470 networkButton.Click += new EventHandler (OnClickButton);
1472 Controls.Add (recentlyusedButton);
1473 Controls.Add (desktopButton);
1474 Controls.Add (personalButton);
1475 Controls.Add (mycomputerButton);
1476 Controls.Add (networkButton);
1478 ResumeLayout (false);
1480 KeyDown += new KeyEventHandler (Key_Down);
1482 SetStyle (ControlStyles.StandardClick, false);
1485 void OnClickButton (object sender, EventArgs e)
1487 if (lastPopupButton != null && lastPopupButton != sender as PopupButton)
1488 lastPopupButton.ButtonState = PopupButton.PopupButtonState.Normal;
1489 lastPopupButton = sender as PopupButton;
1491 if (sender == recentlyusedButton) {
1492 currentPath = MWFVFS.RecentlyUsedPrefix;
1494 if (sender == desktopButton) {
1495 currentPath = MWFVFS.DesktopPrefix;
1497 if (sender == personalButton) {
1498 currentPath = MWFVFS.PersonalPrefix;
1500 if (sender == mycomputerButton) {
1501 currentPath = MWFVFS.MyComputerPrefix;
1503 if (sender == networkButton) {
1504 currentPath = MWFVFS.MyNetworkPrefix;
1507 EventHandler eh = (EventHandler)(Events [PDirectoryChangedEvent]);
1509 eh (this, EventArgs.Empty);
1512 public string CurrentFolder {
1514 string currentPath = value;
1515 if (currentPath == MWFVFS.RecentlyUsedPrefix) {
1516 if (lastPopupButton != recentlyusedButton) {
1517 if (lastPopupButton != null)
1518 lastPopupButton.ButtonState = PopupButton.PopupButtonState.Normal;
1519 recentlyusedButton.ButtonState = PopupButton.PopupButtonState.Down;
1520 lastPopupButton = recentlyusedButton;
1523 if (currentPath == MWFVFS.DesktopPrefix) {
1524 if (lastPopupButton != desktopButton) {
1525 if (lastPopupButton != null)
1526 lastPopupButton.ButtonState = PopupButton.PopupButtonState.Normal;
1527 desktopButton.ButtonState = PopupButton.PopupButtonState.Down;
1528 lastPopupButton = desktopButton;
1531 if (currentPath == MWFVFS.PersonalPrefix) {
1532 if (lastPopupButton != personalButton) {
1533 if (lastPopupButton != null)
1534 lastPopupButton.ButtonState = PopupButton.PopupButtonState.Normal;
1535 personalButton.ButtonState = PopupButton.PopupButtonState.Down;
1536 lastPopupButton = personalButton;
1539 if (currentPath == MWFVFS.MyComputerPrefix) {
1540 if (lastPopupButton != mycomputerButton) {
1541 if (lastPopupButton != null)
1542 lastPopupButton.ButtonState = PopupButton.PopupButtonState.Normal;
1543 mycomputerButton.ButtonState = PopupButton.PopupButtonState.Down;
1544 lastPopupButton = mycomputerButton;
1547 if (currentPath == MWFVFS.MyNetworkPrefix) {
1548 if (lastPopupButton != networkButton) {
1549 if (lastPopupButton != null)
1550 lastPopupButton.ButtonState = PopupButton.PopupButtonState.Normal;
1551 networkButton.ButtonState = PopupButton.PopupButtonState.Down;
1552 lastPopupButton = networkButton;
1555 if (lastPopupButton != null) {
1556 lastPopupButton.ButtonState = PopupButton.PopupButtonState.Normal;
1557 lastPopupButton = null;
1566 protected override void OnPaint (PaintEventArgs e)
1568 ControlPaint.DrawBorder3D (e.Graphics, ClientRectangle, Border3DStyle.Sunken);
1572 protected override void OnGotFocus (EventArgs e)
1574 if (lastPopupButton != recentlyusedButton) {
1575 recentlyusedButton.ButtonState = PopupButton.PopupButtonState.Up;
1576 focusButton = recentlyusedButton;
1578 currentFocusIndex = 0;
1580 base.OnGotFocus (e);
1583 protected override void OnLostFocus (EventArgs e)
1585 if (focusButton != null && focusButton.ButtonState != PopupButton.PopupButtonState.Down)
1586 focusButton.ButtonState = PopupButton.PopupButtonState.Normal;
1587 base.OnLostFocus (e);
1590 protected override bool IsInputKey (Keys key)
1600 return base.IsInputKey (key);
1603 private void Key_Down (object sender, KeyEventArgs e)
1605 bool update_focus = false;
1607 if (e.KeyCode == Keys.Left || e.KeyCode == Keys.Up) {
1608 currentFocusIndex --;
1610 if (currentFocusIndex < 0)
1611 currentFocusIndex = Controls.Count - 1;
1613 update_focus = true;
1615 if (e.KeyCode == Keys.Down || e.KeyCode == Keys.Right) {
1616 currentFocusIndex++;
1618 if (currentFocusIndex == Controls.Count)
1619 currentFocusIndex = 0;
1621 update_focus = true;
1623 if (e.KeyCode == Keys.Enter) {
1624 focusButton.ButtonState = PopupButton.PopupButtonState.Down;
1625 OnClickButton (focusButton, EventArgs.Empty);
1629 PopupButton newfocusButton = Controls [currentFocusIndex] as PopupButton;
1630 if (focusButton != null && focusButton.ButtonState != PopupButton.PopupButtonState.Down)
1631 focusButton.ButtonState = PopupButton.PopupButtonState.Normal;
1632 if (newfocusButton.ButtonState != PopupButton.PopupButtonState.Down)
1633 newfocusButton.ButtonState = PopupButton.PopupButtonState.Up;
1634 focusButton = newfocusButton;
1640 static object PDirectoryChangedEvent = new object ();
1642 public event EventHandler DirectoryChanged {
1643 add { Events.AddHandler (PDirectoryChangedEvent, value); }
1644 remove { Events.RemoveHandler (PDirectoryChangedEvent, value); }
1650 internal class DirComboBox : ComboBox, IUpdateFolder
1652 #region DirComboBoxItem
1653 internal class DirComboBoxItem
1655 private int imageIndex;
1656 private string name;
1657 private string path;
1659 private ImageList imageList;
1661 public DirComboBoxItem (ImageList imageList, int imageIndex, string name, string path, int xPos)
1663 this.imageList = imageList;
1664 this.imageIndex = imageIndex;
1670 public int ImageIndex {
1676 public string Name {
1682 public string Path {
1694 public ImageList ImageList {
1706 private ImageList imageList = new ImageList();
1708 private string currentPath;
1710 private Stack folderStack = new Stack();
1712 private static readonly int indent = 6;
1714 private DirComboBoxItem recentlyUsedDirComboboxItem;
1715 private DirComboBoxItem desktopDirComboboxItem;
1716 private DirComboBoxItem personalDirComboboxItem;
1717 private DirComboBoxItem myComputerDirComboboxItem;
1718 private DirComboBoxItem networkDirComboboxItem;
1720 private ArrayList myComputerItems = new ArrayList ();
1722 private DirComboBoxItem mainParentDirComboBoxItem = null;
1723 private DirComboBoxItem real_parent = null;
1727 public DirComboBox (MWFVFS vfs)
1733 DrawMode = DrawMode.OwnerDrawFixed;
1735 imageList.ColorDepth = ColorDepth.Depth32Bit;
1736 imageList.ImageSize = new Size (16, 16);
1737 imageList.Images.Add (ThemeEngine.Current.Images (UIIcon.PlacesRecentDocuments, 16));
1738 imageList.Images.Add (ThemeEngine.Current.Images (UIIcon.PlacesDesktop, 16));
1739 imageList.Images.Add (ThemeEngine.Current.Images (UIIcon.PlacesPersonal, 16));
1740 imageList.Images.Add (ThemeEngine.Current.Images (UIIcon.PlacesMyComputer, 16));
1741 imageList.Images.Add (ThemeEngine.Current.Images (UIIcon.PlacesMyNetwork, 16));
1742 imageList.Images.Add (ThemeEngine.Current.Images (UIIcon.NormalFolder, 16));
1743 imageList.TransparentColor = Color.Transparent;
1745 recentlyUsedDirComboboxItem = new DirComboBoxItem (imageList, 0, "Recently used", MWFVFS.RecentlyUsedPrefix, 0);
1746 desktopDirComboboxItem = new DirComboBoxItem (imageList, 1, "Desktop", MWFVFS.DesktopPrefix, 0);
1747 personalDirComboboxItem = new DirComboBoxItem (imageList, 2, "Personal folder", MWFVFS.PersonalPrefix, indent);
1748 myComputerDirComboboxItem = new DirComboBoxItem (imageList, 3, "My Computer", MWFVFS.MyComputerPrefix, indent);
1749 networkDirComboboxItem = new DirComboBoxItem (imageList, 4, "My Network", MWFVFS.MyNetworkPrefix, indent);
1751 ArrayList al = this.vfs.GetMyComputerContent ();
1753 foreach (FSEntry fsEntry in al) {
1754 myComputerItems.Add (new DirComboBoxItem (MimeIconEngine.LargeIcons, fsEntry.IconIndex, fsEntry.Name, fsEntry.FullName, indent * 2));
1760 mainParentDirComboBoxItem = myComputerDirComboboxItem;
1762 ResumeLayout (false);
1765 public string CurrentFolder {
1767 currentPath = value;
1776 private void CreateComboList ()
1779 DirComboBoxItem selection = null;
1781 if (currentPath == MWFVFS.RecentlyUsedPrefix) {
1782 mainParentDirComboBoxItem = recentlyUsedDirComboboxItem;
1783 selection = recentlyUsedDirComboboxItem;
1785 if (currentPath == MWFVFS.DesktopPrefix) {
1786 selection = desktopDirComboboxItem;
1787 mainParentDirComboBoxItem = desktopDirComboboxItem;
1789 if (currentPath == MWFVFS.PersonalPrefix) {
1790 selection = personalDirComboboxItem;
1791 mainParentDirComboBoxItem = personalDirComboboxItem;
1793 if (currentPath == MWFVFS.MyComputerPrefix) {
1794 selection = myComputerDirComboboxItem;
1795 mainParentDirComboBoxItem = myComputerDirComboboxItem;
1797 if (currentPath == MWFVFS.MyNetworkPrefix) {
1798 selection = networkDirComboboxItem;
1799 mainParentDirComboBoxItem = networkDirComboboxItem;
1801 foreach (DirComboBoxItem dci in myComputerItems) {
1802 if (dci.Path == currentPath) {
1803 mainParentDirComboBoxItem = selection = dci;
1813 Items.Add (recentlyUsedDirComboboxItem);
1814 Items.Add (desktopDirComboboxItem);
1815 Items.Add (personalDirComboboxItem);
1816 Items.Add (myComputerDirComboboxItem);
1817 Items.AddRange (myComputerItems);
1818 Items.Add (networkDirComboboxItem);
1820 if (selection == null) {
1821 real_parent = CreateFolderStack ();
1824 if (real_parent != null) {
1825 int local_indent = 0;
1827 if (real_parent == desktopDirComboboxItem)
1830 if (real_parent == personalDirComboboxItem || real_parent == networkDirComboboxItem)
1835 selection = AppendToParent (local_indent, real_parent);
1840 if (selection != null)
1841 SelectedItem = selection;
1844 private DirComboBoxItem CreateFolderStack ()
1846 folderStack.Clear ();
1848 DirectoryInfo di = new DirectoryInfo (currentPath);
1850 folderStack.Push (di);
1852 while (di.Parent != null) {
1855 if (mainParentDirComboBoxItem != personalDirComboboxItem && di.FullName == ThemeEngine.Current.Places (UIIcon.PlacesDesktop))
1856 return desktopDirComboboxItem;
1858 if (mainParentDirComboBoxItem == personalDirComboboxItem) {
1859 if (di.FullName == ThemeEngine.Current.Places (UIIcon.PlacesPersonal))
1860 return personalDirComboboxItem;
1862 foreach (DirComboBoxItem dci in myComputerItems) {
1863 if (dci.Path == di.FullName) {
1869 folderStack.Push (di);
1875 private DirComboBoxItem AppendToParent (int nr_indents, DirComboBoxItem parentDirComboBoxItem)
1877 DirComboBoxItem selection = null;
1879 int index = Items.IndexOf (parentDirComboBoxItem) + 1;
1881 int xPos = indent * nr_indents;
1883 while (folderStack.Count != 0) {
1884 DirectoryInfo dii = folderStack.Pop () as DirectoryInfo;
1886 DirComboBoxItem dci = new DirComboBoxItem (imageList, 5, dii.Name, dii.FullName, xPos);
1888 Items.Insert (index, dci);
1897 protected override void OnDrawItem (DrawItemEventArgs e)
1902 DirComboBoxItem dcbi = Items [e.Index] as DirComboBoxItem;
1904 Bitmap bmp = new Bitmap (e.Bounds.Width, e.Bounds.Height, e.Graphics);
1905 Graphics gr = Graphics.FromImage (bmp);
1907 Color backColor = e.BackColor;
1908 Color foreColor = e.ForeColor;
1910 int xPos = dcbi.XPos;
1912 if ((e.State & DrawItemState.ComboBoxEdit) != 0)
1915 gr.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (backColor),
1916 new Rectangle (0, 0, bmp.Width, bmp.Height));
1918 if ((e.State & DrawItemState.Selected) == DrawItemState.Selected &&
1919 (!DroppedDown || (e.State & DrawItemState.ComboBoxEdit) != DrawItemState.ComboBoxEdit)) {
1920 foreColor = ThemeEngine.Current.ColorHighlightText;
1922 int w = (int) gr.MeasureString (dcbi.Name, e.Font).Width;
1924 gr.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (ThemeEngine.Current.ColorHighlight),
1925 new Rectangle (xPos + 23, 1, w + 3, e.Bounds.Height - 2));
1926 if ((e.State & DrawItemState.Focus) == DrawItemState.Focus) {
1927 ControlPaint.DrawFocusRectangle (gr, new Rectangle (xPos + 22, 0, w + 5,
1928 e.Bounds.Height), foreColor, ThemeEngine.Current.ColorHighlight);
1932 gr.DrawString (dcbi.Name, e.Font , ThemeEngine.Current.ResPool.GetSolidBrush (foreColor), new Point (24 + xPos, (bmp.Height - e.Font.Height) / 2));
1933 gr.DrawImage (dcbi.ImageList.Images [dcbi.ImageIndex], new Rectangle (new Point (xPos + 2, 0), new Size (16, 16)));
1935 e.Graphics.DrawImage (bmp, e.Bounds.X, e.Bounds.Y);
1940 protected override void OnSelectedIndexChanged (EventArgs e)
1942 if (Items.Count > 0) {
1943 DirComboBoxItem dcbi = Items [SelectedIndex] as DirComboBoxItem;
1945 currentPath = dcbi.Path;
1949 protected override void OnSelectionChangeCommitted (EventArgs e)
1951 EventHandler eh = (EventHandler)(Events [CDirectoryChangedEvent]);
1953 eh (this, EventArgs.Empty);
1956 static object CDirectoryChangedEvent = new object ();
1958 public event EventHandler DirectoryChanged {
1959 add { Events.AddHandler (CDirectoryChangedEvent, value); }
1960 remove { Events.RemoveHandler (CDirectoryChangedEvent, value); }
1965 #region FilterStruct
1966 internal struct FilterStruct
1968 public string filterName;
1969 public StringCollection filters;
1971 public FilterStruct (string filterName, string filter)
1973 this.filterName = filterName;
1975 filters = new StringCollection ();
1977 SplitFilters (filter);
1980 private void SplitFilters (string filter)
1982 string[] split = filter.Split (new char [] {';'});
1983 foreach (string s in split) {
1984 filters.Add (s.Trim ());
1991 internal class FileFilter
1993 private ArrayList filterArrayList = new ArrayList();
1995 private string filter;
1997 public FileFilter ()
2000 public FileFilter (string filter) : base ()
2002 this.filter = filter;
2007 public static bool CheckFilter (string val)
2009 if (val.Length == 0)
2012 string[] filters = val.Split (new char [] {'|'});
2014 if ((filters.Length % 2) != 0)
2020 public ArrayList FilterArrayList {
2022 filterArrayList = value;
2026 return filterArrayList;
2030 public string Filter {
2042 private void SplitFilter ()
2044 filterArrayList.Clear ();
2046 if (filter.Length == 0)
2049 string[] filters = filter.Split (new char [] {'|'});
2051 for (int i = 0; i < filters.Length; i += 2) {
2052 FilterStruct filterStruct = new FilterStruct (filters [i], filters [i + 1]);
2054 filterArrayList.Add (filterStruct);
2062 internal class MWFFileView : ListView
2064 private ArrayList filterArrayList;
2066 private bool showHiddenFiles = false;
2068 private string selectedFilesString;
2070 private int filterIndex = 1;
2072 private ToolTip toolTip;
2073 private int oldItemIndexForToolTip = -1;
2075 private ContextMenu contextMenu;
2077 private MenuItem menuItemView;
2078 private MenuItem menuItemNew;
2080 private MenuItem smallIconMenutItem;
2081 private MenuItem tilesMenutItem;
2082 private MenuItem largeIconMenutItem;
2083 private MenuItem listMenutItem;
2084 private MenuItem detailsMenutItem;
2086 private MenuItem newFolderMenuItem;
2087 private MenuItem showHiddenFilesMenuItem;
2089 private int previousCheckedMenuItemIndex;
2091 private ArrayList viewMenuItemClones = new ArrayList ();
2093 private FSEntry currentFSEntry = null;
2095 private string currentFolder;
2096 private string currentRealFolder;
2097 private FSEntry currentFolderFSEntry;
2099 // store DirectoryInfo for a back button for example
2100 private Stack directoryStack = new Stack();
2102 // list of controls(components to enable or disable depending on current directoryStack.Count
2103 private ArrayList dirStackControlsOrComponents = new ArrayList ();
2105 private ToolBarButton folderUpToolBarButton;
2107 private ArrayList registered_senders = new ArrayList ();
2109 private bool should_push = true;
2113 private View old_view;
2115 private int old_menuitem_index;
2116 private bool do_update_view = false;
2118 private int platform = (int) Environment.OSVersion.Platform;
2120 public MWFFileView (MWFVFS vfs)
2123 this.vfs.RegisterUpdateDelegate (new MWFVFS.UpdateDelegate (RealFileViewUpdate), this);
2127 contextMenu = new ContextMenu ();
2129 toolTip = new ToolTip ();
2130 toolTip.InitialDelay = 300;
2131 toolTip.ReshowDelay = 0;
2136 menuItemView = new MenuItem ("View");
2138 smallIconMenutItem = new MenuItem ("Small Icon", new EventHandler (OnClickViewMenuSubItem));
2139 smallIconMenutItem.RadioCheck = true;
2140 menuItemView.MenuItems.Add (smallIconMenutItem);
2142 tilesMenutItem = new MenuItem ("Tiles", new EventHandler (OnClickViewMenuSubItem));
2143 tilesMenutItem.RadioCheck = true;
2144 menuItemView.MenuItems.Add (tilesMenutItem);
2146 largeIconMenutItem = new MenuItem ("Large Icon", new EventHandler (OnClickViewMenuSubItem));
2147 largeIconMenutItem.RadioCheck = true;
2148 menuItemView.MenuItems.Add (largeIconMenutItem);
2150 listMenutItem = new MenuItem ("List", new EventHandler (OnClickViewMenuSubItem));
2151 listMenutItem.RadioCheck = true;
2152 listMenutItem.Checked = true;
2153 menuItemView.MenuItems.Add (listMenutItem);
2154 previousCheckedMenuItemIndex = listMenutItem.Index;
2156 detailsMenutItem = new MenuItem ("Details", new EventHandler (OnClickViewMenuSubItem));
2157 detailsMenutItem.RadioCheck = true;
2158 menuItemView.MenuItems.Add (detailsMenutItem);
2160 contextMenu.MenuItems.Add (menuItemView);
2162 contextMenu.MenuItems.Add (new MenuItem ("-"));
2165 menuItemNew = new MenuItem ("New");
2167 newFolderMenuItem = new MenuItem ("New Folder", new EventHandler (OnClickNewFolderMenuItem));
2168 menuItemNew.MenuItems.Add (newFolderMenuItem);
2170 contextMenu.MenuItems.Add (menuItemNew);
2172 contextMenu.MenuItems.Add (new MenuItem ("-"));
2174 // Show hidden files menu item
2175 showHiddenFilesMenuItem = new MenuItem ("Show hidden files", new EventHandler (OnClickContextMenu));
2176 showHiddenFilesMenuItem.Checked = showHiddenFiles;
2177 contextMenu.MenuItems.Add (showHiddenFilesMenuItem);
2181 SmallImageList = MimeIconEngine.SmallIcons;
2182 LargeImageList = MimeIconEngine.LargeIcons;
2184 View = old_view = View.List;
2187 ContextMenu = contextMenu;
2189 ResumeLayout (false);
2191 KeyDown += new KeyEventHandler (MWF_KeyDown);
2194 public string CurrentFolder {
2196 return currentFolder;
2199 currentFolder = value;
2203 public string CurrentRealFolder {
2205 return currentRealFolder;
2209 public FSEntry CurrentFSEntry {
2211 return currentFSEntry;
2215 public MenuItem[] ViewMenuItems {
2217 MenuItem[] menuItemClones = new MenuItem [] {
2218 smallIconMenutItem.CloneMenu (),
2219 tilesMenutItem.CloneMenu (),
2220 largeIconMenutItem.CloneMenu (),
2221 listMenutItem.CloneMenu (),
2222 detailsMenutItem.CloneMenu ()
2225 viewMenuItemClones.Add (menuItemClones);
2227 return menuItemClones;
2231 public ArrayList FilterArrayList {
2233 filterArrayList = value;
2237 return filterArrayList;
2241 public bool ShowHiddenFiles {
2243 showHiddenFiles = value;
2247 return showHiddenFiles;
2251 public int FilterIndex {
2253 filterIndex = value;
2263 public string SelectedFilesString {
2265 selectedFilesString = value;
2269 return selectedFilesString;
2273 public void PushDir ()
2275 if (currentFolder != null)
2276 directoryStack.Push (currentFolder);
2278 EnableOrDisableDirstackObjects ();
2281 public void PopDir ()
2283 if (directoryStack.Count == 0)
2286 string new_folder = directoryStack.Pop () as string;
2288 EnableOrDisableDirstackObjects ();
2290 should_push = false;
2292 ChangeDirectory (null, new_folder);
2295 public void RegisterSender (IUpdateFolder iud)
2297 registered_senders.Add (iud);
2300 public void CreateNewFolder ()
2302 if (currentFolder == MWFVFS.MyComputerPrefix ||
2303 currentFolder == MWFVFS.RecentlyUsedPrefix)
2306 FSEntry fsEntry = new FSEntry ();
2307 fsEntry.Attributes = FileAttributes.Directory;
2308 fsEntry.FileType = FSEntry.FSEntryType.Directory;
2309 fsEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("inode/directory");
2310 fsEntry.LastAccessTime = DateTime.Now;
2312 // FIXME: when ListView.LabelEdit is available use it
2313 // listViewItem.BeginEdit();
2315 TextEntryDialog ted = new TextEntryDialog ();
2316 ted.IconPictureBoxImage = MimeIconEngine.LargeIcons.Images.GetImage (fsEntry.IconIndex);
2318 string folder = String.Empty;
2320 if (currentFolderFSEntry.RealName != null)
2321 folder = currentFolderFSEntry.RealName;
2323 folder = currentFolder;
2325 string tmp_filename = "New Folder";
2327 if (Directory.Exists (Path.Combine (folder, tmp_filename))) {
2330 if ((platform == 4) || (platform == 128)) {
2331 tmp_filename = tmp_filename + "-" + i;
2333 tmp_filename = tmp_filename + " (" + i + ")";
2336 while (Directory.Exists (Path.Combine (folder, tmp_filename))) {
2338 if ((platform == 4) || (platform == 128)) {
2339 tmp_filename = "New Folder" + "-" + i;
2341 tmp_filename = "New Folder" + " (" + i + ")";
2346 ted.FileName = tmp_filename;
2348 if (ted.ShowDialog () == DialogResult.OK) {
2349 string new_folder = Path.Combine (folder, ted.FileName);
2351 if (vfs.CreateFolder (new_folder)) {
2352 fsEntry.FullName = new_folder;
2353 fsEntry.Name = ted.FileName;
2355 FileViewListViewItem listViewItem = new FileViewListViewItem (fsEntry);
2358 Items.Add (listViewItem);
2361 listViewItem.EnsureVisible ();
2366 public void SetSelectedIndexTo (string fname)
2368 foreach (FileViewListViewItem item in Items) {
2369 if (item.Text == fname) {
2371 SelectedItems.Clear ();
2372 item.Selected = true;
2379 public void OneDirUp ()
2381 string parent_folder = vfs.GetParent ();
2382 if (parent_folder != null)
2383 ChangeDirectory (null, parent_folder);
2386 public void ChangeDirectory (object sender, string folder)
2388 if (folder == MWFVFS.DesktopPrefix || folder == MWFVFS.RecentlyUsedPrefix)
2389 folderUpToolBarButton.Enabled = false;
2391 folderUpToolBarButton.Enabled = true;
2393 foreach (IUpdateFolder iuf in registered_senders) {
2394 iuf.CurrentFolder = folder;
2402 currentFolderFSEntry = vfs.ChangeDirectory (folder);
2404 currentFolder = folder;
2406 if (currentFolder.IndexOf ("://") != -1)
2407 currentRealFolder = currentFolderFSEntry.RealName;
2409 currentRealFolder = currentFolder;
2414 SelectedItems.Clear ();
2416 if (folder == MWFVFS.RecentlyUsedPrefix) {
2418 View = View.Details;
2419 old_menuitem_index = previousCheckedMenuItemIndex;
2420 UpdateMenuItems (detailsMenutItem);
2421 do_update_view = true;
2423 if (View != old_view && do_update_view) {
2424 UpdateMenuItems (menuItemView.MenuItems [old_menuitem_index]);
2426 do_update_view = false;
2432 } catch (Exception e) {
2435 MessageBox.Show (e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
2439 public void UpdateFileView ()
2441 if (filterArrayList != null && filterArrayList.Count != 0) {
2442 FilterStruct fs = (FilterStruct)filterArrayList [filterIndex - 1];
2444 vfs.GetFolderContent (fs.filters);
2446 vfs.GetFolderContent ();
2449 public void RealFileViewUpdate (ArrayList directoriesArrayList, ArrayList fileArrayList)
2454 SelectedItems.Clear ();
2456 foreach (FSEntry directoryFSEntry in directoriesArrayList) {
2457 if (!ShowHiddenFiles)
2458 if (directoryFSEntry.Name.StartsWith (".") || directoryFSEntry.Attributes == FileAttributes.Hidden)
2461 FileViewListViewItem listViewItem = new FileViewListViewItem (directoryFSEntry);
2463 Items.Add (listViewItem);
2466 StringCollection collection = new StringCollection ();
2468 foreach (FSEntry fsEntry in fileArrayList) {
2470 // remove duplicates. that can happen when you read recently used files for example
2471 if (collection.Contains (fsEntry.Name)) {
2473 string fileName = fsEntry.Name;
2475 if (collection.Contains (fileName)) {
2478 while (collection.Contains (fileName + "(" + i + ")")) {
2482 fileName = fileName + "(" + i + ")";
2485 fsEntry.Name = fileName;
2488 collection.Add (fsEntry.Name);
2490 DoOneFSEntry (fsEntry);
2495 collection.Clear ();
2498 directoriesArrayList.Clear ();
2499 fileArrayList.Clear ();
2502 public void AddControlToEnableDisableByDirStack (object control)
2504 dirStackControlsOrComponents.Add (control);
2507 public void SetFolderUpToolBarButton (ToolBarButton tb)
2509 folderUpToolBarButton = tb;
2512 public void WriteRecentlyUsed (string fullfilename)
2514 vfs.WriteRecentlyUsedFiles (fullfilename);
2517 private void EnableOrDisableDirstackObjects ()
2519 foreach (object o in dirStackControlsOrComponents) {
2521 Control c = o as Control;
2522 c.Enabled = (directoryStack.Count > 1);
2524 if (o is ToolBarButton) {
2525 ToolBarButton t = o as ToolBarButton;
2526 t.Enabled = (directoryStack.Count > 0);
2531 private void DoOneFSEntry (FSEntry fsEntry)
2533 if (!ShowHiddenFiles)
2534 if (fsEntry.Name.StartsWith (".") || fsEntry.Attributes == FileAttributes.Hidden)
2537 FileViewListViewItem listViewItem = new FileViewListViewItem (fsEntry);
2539 Items.Add (listViewItem);
2542 private void MWF_KeyDown (object sender, KeyEventArgs e)
2544 if (e.KeyCode == Keys.Back) {
2549 protected override void OnClick (EventArgs e)
2552 if (SelectedItems.Count > 0) {
2553 FileViewListViewItem listViewItem = SelectedItems [0] as FileViewListViewItem;
2555 FSEntry fsEntry = listViewItem.FSEntry;
2557 if (fsEntry.FileType == FSEntry.FSEntryType.File) {
2558 currentFSEntry = fsEntry;
2560 EventHandler eh = (EventHandler)(Events [MSelectedFileChangedEvent]);
2562 eh (this, EventArgs.Empty);
2570 protected override void OnDoubleClick (EventArgs e)
2572 if (SelectedItems.Count > 0) {
2573 FileViewListViewItem listViewItem = SelectedItems [0] as FileViewListViewItem;
2575 FSEntry fsEntry = listViewItem.FSEntry;
2577 if (fsEntry.Attributes == FileAttributes.Directory) {
2579 ChangeDirectory (null, fsEntry.FullName);
2581 EventHandler eh = (EventHandler)(Events [MDirectoryChangedEvent]);
2583 eh (this, EventArgs.Empty);
2585 currentFSEntry = fsEntry;
2587 EventHandler eh = (EventHandler)(Events [MSelectedFileChangedEvent]);
2589 eh (this, EventArgs.Empty);
2591 eh = (EventHandler)(Events [MForceDialogEndEvent]);
2593 eh (this, EventArgs.Empty);
2599 base.OnDoubleClick (e);
2602 protected override void OnSelectedIndexChanged (EventArgs e)
2604 if (SelectedItems.Count > 0) {
2605 selectedFilesString = String.Empty;
2607 if (SelectedItems.Count == 1) {
2608 FileViewListViewItem listViewItem = SelectedItems [0] as FileViewListViewItem;
2610 FSEntry fsEntry = listViewItem.FSEntry;
2612 if (fsEntry.Attributes != FileAttributes.Directory)
2613 selectedFilesString = SelectedItems [0].Text;
2615 foreach (FileViewListViewItem lvi in SelectedItems) {
2616 FSEntry fsEntry = lvi.FSEntry;
2618 if (fsEntry.Attributes != FileAttributes.Directory)
2619 selectedFilesString = selectedFilesString + "\"" + lvi.Text + "\" ";
2623 EventHandler eh = (EventHandler)(Events [MSelectedFilesChangedEvent]);
2625 eh (this, EventArgs.Empty);
2628 base.OnSelectedIndexChanged (e);
2631 protected override void OnMouseMove (MouseEventArgs e)
2633 FileViewListViewItem item = GetItemAt (e.X, e.Y) as FileViewListViewItem;
2636 int currentItemIndex = item.Index;
2638 if (currentItemIndex != oldItemIndexForToolTip) {
2639 oldItemIndexForToolTip = currentItemIndex;
2641 if (toolTip != null && toolTip.Active)
2642 toolTip.Active = false;
2644 FSEntry fsEntry = item.FSEntry;
2646 string output = String.Empty;
2648 if (fsEntry.FileType == FSEntry.FSEntryType.Directory)
2649 output = "Directory: " + fsEntry.FullName;
2650 else if (fsEntry.FileType == FSEntry.FSEntryType.Device)
2651 output = "Device: "+ fsEntry.FullName;
2652 else if (fsEntry.FileType == FSEntry.FSEntryType.Network)
2653 output = "Network: " + fsEntry.FullName;
2655 output = "File: " + fsEntry.FullName;
2657 toolTip.SetToolTip (this, output);
2659 toolTip.Active = true;
2662 toolTip.Active = false;
2664 base.OnMouseMove (e);
2667 void OnClickContextMenu (object sender, EventArgs e)
2669 MenuItem senderMenuItem = sender as MenuItem;
2671 if (senderMenuItem == showHiddenFilesMenuItem) {
2672 senderMenuItem.Checked = !senderMenuItem.Checked;
2673 showHiddenFiles = senderMenuItem.Checked;
2678 void OnClickViewMenuSubItem (object sender, EventArgs e)
2680 MenuItem senderMenuItem = (MenuItem)sender;
2682 UpdateMenuItems (senderMenuItem);
2686 switch (senderMenuItem.Index) {
2688 View = View.SmallIcon;
2691 View = View.LargeIcon;
2694 View = View.LargeIcon;
2700 View = View.Details;
2707 protected override void OnBeforeLabelEdit (LabelEditEventArgs e)
2709 FileViewListViewItem listViewItem = SelectedItems [0] as FileViewListViewItem;
2710 FSEntry fsEntry = listViewItem.FSEntry;
2712 // only allow editing of files or directories
2713 if (fsEntry.FileType != FSEntry.FSEntryType.Directory &&
2714 fsEntry.FileType != FSEntry.FSEntryType.File)
2715 e.CancelEdit = true;
2717 base.OnBeforeLabelEdit (e);
2720 protected override void OnAfterLabelEdit (LabelEditEventArgs e)
2722 base.OnAfterLabelEdit (e);
2724 // no changes were made
2725 if (e.Label == null || Items [e.Item].Text == e.Label)
2728 FileViewListViewItem listViewItem = SelectedItems [0] as FileViewListViewItem;
2729 FSEntry fsEntry = listViewItem.FSEntry;
2731 string folder = (currentFolderFSEntry.RealName != null) ?
2732 currentFolderFSEntry.RealName : currentFolder;
2734 switch (fsEntry.FileType) {
2735 case FSEntry.FSEntryType.Directory:
2736 string sourceDir = (fsEntry.RealName != null) ? fsEntry.RealName : fsEntry.FullName;
2737 string destDir = Path.Combine (folder, e.Label);
2738 if (!vfs.MoveFolder (sourceDir, destDir)) {
2739 e.CancelEdit = true;
2741 if (fsEntry.RealName != null)
2742 fsEntry.RealName = destDir;
2744 fsEntry.FullName = destDir;
2747 case FSEntry.FSEntryType.File:
2748 string sourceFile = (fsEntry.RealName != null) ? fsEntry.RealName : fsEntry.FullName;
2749 string destFile = Path.Combine (folder, e.Label);
2750 if (!vfs.MoveFile (sourceFile, destFile)) {
2751 e.CancelEdit = true;
2753 if (fsEntry.RealName != null)
2754 fsEntry.RealName = destFile;
2756 fsEntry.FullName = destFile;
2762 private void UpdateMenuItems (MenuItem senderMenuItem)
2764 menuItemView.MenuItems [previousCheckedMenuItemIndex].Checked = false;
2765 menuItemView.MenuItems [senderMenuItem.Index].Checked = true;
2767 foreach (MenuItem[] items in viewMenuItemClones) {
2768 items [previousCheckedMenuItemIndex].Checked = false;
2769 items [senderMenuItem.Index].Checked = true;
2772 previousCheckedMenuItemIndex = senderMenuItem.Index;
2775 void OnClickNewFolderMenuItem (object sender, EventArgs e)
2780 static object MSelectedFileChangedEvent = new object ();
2781 static object MSelectedFilesChangedEvent = new object ();
2782 static object MDirectoryChangedEvent = new object ();
2783 static object MForceDialogEndEvent = new object ();
2785 public event EventHandler SelectedFileChanged {
2786 add { Events.AddHandler (MSelectedFileChangedEvent, value); }
2787 remove { Events.RemoveHandler (MSelectedFileChangedEvent, value); }
2790 public event EventHandler SelectedFilesChanged {
2791 add { Events.AddHandler (MSelectedFilesChangedEvent, value); }
2792 remove { Events.RemoveHandler (MSelectedFilesChangedEvent, value); }
2795 public event EventHandler DirectoryChanged {
2796 add { Events.AddHandler (MDirectoryChangedEvent, value); }
2797 remove { Events.RemoveHandler (MDirectoryChangedEvent, value); }
2800 public event EventHandler ForceDialogEnd {
2801 add { Events.AddHandler (MForceDialogEndEvent, value); }
2802 remove { Events.RemoveHandler (MForceDialogEndEvent, value); }
2807 #region FileListViewItem
2808 internal class FileViewListViewItem : ListViewItem
2810 private FSEntry fsEntry;
2812 public FileViewListViewItem (FSEntry fsEntry)
2814 this.fsEntry = fsEntry;
2816 ImageIndex = fsEntry.IconIndex;
2818 Text = fsEntry.Name;
2820 switch (fsEntry.FileType) {
2821 case FSEntry.FSEntryType.Directory:
2822 SubItems.Add (String.Empty);
2823 SubItems.Add ("Directory");
2824 SubItems.Add (fsEntry.LastAccessTime.ToShortDateString () + " " + fsEntry.LastAccessTime.ToShortTimeString ());
2826 case FSEntry.FSEntryType.File:
2829 if (fsEntry.FileSize > 1024)
2830 fileLen = fsEntry.FileSize / 1024;
2831 } catch (Exception) {
2835 SubItems.Add (fileLen.ToString () + " KB");
2836 SubItems.Add ("File");
2837 SubItems.Add (fsEntry.LastAccessTime.ToShortDateString () + " " + fsEntry.LastAccessTime.ToShortTimeString ());
2839 case FSEntry.FSEntryType.Device:
2840 SubItems.Add (String.Empty);
2841 SubItems.Add ("Device");
2842 SubItems.Add (fsEntry.LastAccessTime.ToShortDateString () + " " + fsEntry.LastAccessTime.ToShortTimeString ());
2844 case FSEntry.FSEntryType.RemovableDevice:
2845 SubItems.Add (String.Empty);
2846 SubItems.Add ("RemovableDevice");
2847 SubItems.Add (fsEntry.LastAccessTime.ToShortDateString () + " " + fsEntry.LastAccessTime.ToShortTimeString ());
2854 public FSEntry FSEntry {
2866 #region IUpdateFolder
2867 internal interface IUpdateFolder
2869 string CurrentFolder {get; set;}
2873 #region TextEntryDialog
2874 // FIXME: When ListView.LabelEdit is implemented remove me
2875 internal class TextEntryDialog : Form
2877 private Label label1;
2878 private Button okButton;
2879 private TextBox newNameTextBox;
2880 private PictureBox iconPictureBox;
2881 private Button cancelButton;
2882 private GroupBox groupBox1;
2884 public TextEntryDialog ()
2886 groupBox1 = new GroupBox ();
2887 cancelButton = new Button ();
2888 iconPictureBox = new PictureBox ();
2889 newNameTextBox = new TextBox ();
2890 okButton = new Button ();
2891 label1 = new Label ();
2892 groupBox1.SuspendLayout ();
2896 groupBox1.Controls.Add (newNameTextBox);
2897 groupBox1.Controls.Add (label1);
2898 groupBox1.Controls.Add (iconPictureBox);
2899 groupBox1.Location = new Point (8, 8);
2900 groupBox1.Size = new Size (232, 160);
2901 groupBox1.TabIndex = 5;
2902 groupBox1.TabStop = false;
2903 groupBox1.Text = "New Name";
2906 cancelButton.DialogResult = DialogResult.Cancel;
2907 cancelButton.Location = new Point (168, 176);
2908 cancelButton.TabIndex = 4;
2909 cancelButton.Text = "Cancel";
2912 iconPictureBox.BorderStyle = BorderStyle.Fixed3D;
2913 iconPictureBox.Location = new Point (86, 24);
2914 iconPictureBox.Size = new Size (60, 60);
2915 iconPictureBox.TabIndex = 3;
2916 iconPictureBox.TabStop = false;
2917 iconPictureBox.SizeMode = PictureBoxSizeMode.CenterImage;
2920 newNameTextBox.Location = new Point (16, 128);
2921 newNameTextBox.Size = new Size (200, 20);
2922 newNameTextBox.TabIndex = 5;
2923 newNameTextBox.Text = String.Empty;
2926 okButton.DialogResult = DialogResult.OK;
2927 okButton.Location = new Point (80, 176);
2928 okButton.TabIndex = 3;
2929 okButton.Text = "OK";
2932 label1.Location = new Point (16, 96);
2933 label1.Size = new Size (200, 23);
2934 label1.TabIndex = 4;
2935 label1.Text = "Enter Name:";
2936 label1.TextAlign = ContentAlignment.MiddleCenter;
2939 AcceptButton = okButton;
2940 AutoScaleBaseSize = new Size (5, 13);
2941 CancelButton = cancelButton;
2942 ClientSize = new Size (248, 205);
2943 Controls.Add (groupBox1);
2944 Controls.Add (cancelButton);
2945 Controls.Add (okButton);
2946 FormBorderStyle = FormBorderStyle.FixedDialog;
2947 Text = "New Folder or File";
2948 groupBox1.ResumeLayout (false);
2949 ResumeLayout (false);
2951 newNameTextBox.Select ();
2954 public Image IconPictureBoxImage {
2956 iconPictureBox.Image = value;
2960 public string FileName {
2962 return newNameTextBox.Text;
2965 newNameTextBox.Text = value;
2972 internal class MWFVFS
2974 private FileSystem fileSystem;
2976 private int platform = (int) Environment.OSVersion.Platform;
2978 public static readonly string DesktopPrefix = "Desktop://";
2979 public static readonly string PersonalPrefix = "Personal://";
2980 public static readonly string MyComputerPrefix = "MyComputer://";
2981 public static readonly string RecentlyUsedPrefix = "RecentlyUsed://";
2982 public static readonly string MyNetworkPrefix = "MyNetwork://";
2983 public static readonly string MyComputerPersonalPrefix = "MyComputerPersonal://";
2985 public static Hashtable MyComputerDevicesPrefix = new Hashtable ();
2987 public delegate void UpdateDelegate (ArrayList folders, ArrayList files);
2988 private UpdateDelegate updateDelegate;
2989 private Control calling_control;
2991 private ThreadStart get_folder_content_thread_start;
2992 private Thread worker;
2993 private WorkerThread workerThread = null;
2994 private StringCollection the_filters;
2998 if ((platform == 4) || (platform == 128)) {
2999 fileSystem = new UnixFileSystem ();
3001 fileSystem = new WinFileSystem ();
3005 public FSEntry ChangeDirectory (string folder)
3007 return fileSystem.ChangeDirectory (folder);
3010 public void GetFolderContent ()
3012 GetFolderContent (null);
3015 public void GetFolderContent (StringCollection filters)
3017 the_filters = filters;
3019 if (workerThread != null) {
3020 workerThread.Stop ();
3021 workerThread = null;
3024 // Added next line to ensure the control is created before BeginInvoke is called on it
3025 calling_control.CreateControl();
3026 workerThread = new WorkerThread (fileSystem, the_filters, updateDelegate, calling_control);
3028 get_folder_content_thread_start = new ThreadStart (workerThread.GetFolderContentThread);
3029 worker = new Thread (get_folder_content_thread_start);
3030 worker.IsBackground = true;
3034 internal class WorkerThread
3036 private FileSystem fileSystem;
3037 private StringCollection the_filters;
3038 private UpdateDelegate updateDelegate;
3039 private Control calling_control;
3040 private readonly object lockobject = new object ();
3041 private bool stopped = false;
3043 public WorkerThread (FileSystem fileSystem, StringCollection the_filters, UpdateDelegate updateDelegate, Control calling_control)
3045 this.fileSystem = fileSystem;
3046 this.the_filters = the_filters;
3047 this.updateDelegate = updateDelegate;
3048 this.calling_control = calling_control;
3051 public void GetFolderContentThread()
3056 fileSystem.GetFolderContent (the_filters, out folders, out files);
3061 if (updateDelegate != null) {
3063 object[] objectArray = new object[2];
3065 objectArray[0] = folders;
3066 objectArray[1] = files;
3068 calling_control.BeginInvoke (updateDelegate, objectArray);
3081 public ArrayList GetFoldersOnly ()
3083 return fileSystem.GetFoldersOnly ();
3086 public void WriteRecentlyUsedFiles (string filename)
3088 fileSystem.WriteRecentlyUsedFiles (filename);
3091 public ArrayList GetRecentlyUsedFiles ()
3093 return fileSystem.GetRecentlyUsedFiles ();
3096 public ArrayList GetMyComputerContent ()
3098 return fileSystem.GetMyComputerContent ();
3101 public ArrayList GetMyNetworkContent ()
3103 return fileSystem.GetMyNetworkContent ();
3106 public bool CreateFolder (string new_folder)
3109 if (Directory.Exists (new_folder)) {
3110 string message = "Folder \"" + new_folder + "\" already exists.";
3111 MessageBox.Show (message, new_folder, MessageBoxButtons.OK,
3112 MessageBoxIcon.Error);
3115 Directory.CreateDirectory (new_folder);
3116 } catch (Exception e) {
3117 MessageBox.Show (e.Message, new_folder, MessageBoxButtons.OK, MessageBoxIcon.Error);
3124 public bool MoveFolder (string sourceDirName, string destDirName)
3127 if (Directory.Exists (destDirName)) {
3128 string message = "Cannot rename " + Path.GetFileName (sourceDirName)
3129 + ": A folder with the name you specified already exists."
3130 + " Specify a different folder name.";
3131 MessageBox.Show (message, "Error Renaming Folder", MessageBoxButtons.OK,
3132 MessageBoxIcon.Error);
3135 Directory.Move (sourceDirName, destDirName);
3136 } catch (Exception e) {
3137 MessageBox.Show (e.Message, "Error Renaming Folder",
3138 MessageBoxButtons.OK, MessageBoxIcon.Error);
3145 public bool MoveFile (string sourceFileName, string destFileName)
3148 if (File.Exists (destFileName)) {
3149 string message = "Cannot rename " + Path.GetFileName (sourceFileName)
3150 + ": A file with the name you specified already exists."
3151 + " Specify a different file name.";
3152 MessageBox.Show (message, "Error Renaming File",
3153 MessageBoxButtons.OK, MessageBoxIcon.Error);
3156 File.Move (sourceFileName, destFileName);
3157 } catch (Exception e) {
3158 MessageBox.Show (e.Message, "Error Renaming Folder",
3159 MessageBoxButtons.OK, MessageBoxIcon.Error);
3166 public string GetParent ()
3168 return fileSystem.GetParent ();
3171 public void RegisterUpdateDelegate(UpdateDelegate updateDelegate, Control control)
3173 this.updateDelegate = updateDelegate;
3174 calling_control = control;
3180 internal abstract class FileSystem
3182 protected string currentTopFolder = String.Empty;
3183 protected FSEntry currentFolderFSEntry = null;
3184 protected FSEntry currentTopFolderFSEntry = null;
3185 private FileInfoComparer fileInfoComparer = new FileInfoComparer ();
3187 public FSEntry ChangeDirectory (string folder)
3189 if (folder == MWFVFS.DesktopPrefix) {
3190 currentTopFolder = MWFVFS.DesktopPrefix;
3191 currentTopFolderFSEntry = currentFolderFSEntry = GetDesktopFSEntry ();
3193 if (folder == MWFVFS.PersonalPrefix) {
3194 currentTopFolder = MWFVFS.PersonalPrefix;
3195 currentTopFolderFSEntry = currentFolderFSEntry = GetPersonalFSEntry ();
3197 if (folder == MWFVFS.MyComputerPersonalPrefix) {
3198 currentTopFolder = MWFVFS.MyComputerPersonalPrefix;
3199 currentTopFolderFSEntry = currentFolderFSEntry = GetMyComputerPersonalFSEntry ();
3201 if (folder == MWFVFS.RecentlyUsedPrefix) {
3202 currentTopFolder = MWFVFS.RecentlyUsedPrefix;
3203 currentTopFolderFSEntry = currentFolderFSEntry = GetRecentlyUsedFSEntry ();
3205 if (folder == MWFVFS.MyComputerPrefix) {
3206 currentTopFolder = MWFVFS.MyComputerPrefix;
3207 currentTopFolderFSEntry = currentFolderFSEntry = GetMyComputerFSEntry ();
3209 if (folder == MWFVFS.MyNetworkPrefix) {
3210 currentTopFolder = MWFVFS.MyNetworkPrefix;
3211 currentTopFolderFSEntry = currentFolderFSEntry = GetMyNetworkFSEntry ();
3215 foreach (DictionaryEntry entry in MWFVFS.MyComputerDevicesPrefix) {
3216 FSEntry fsEntry = entry.Value as FSEntry;
3217 if (folder == fsEntry.FullName) {
3218 currentTopFolder = entry.Key as string;
3219 currentTopFolderFSEntry = currentFolderFSEntry = fsEntry;
3226 currentFolderFSEntry = GetDirectoryFSEntry (new DirectoryInfo (folder), currentTopFolderFSEntry);
3230 return currentFolderFSEntry;
3233 public string GetParent ()
3235 return currentFolderFSEntry.Parent;
3238 // directories_out and files_out contain FSEntry objects
3239 public void GetFolderContent (StringCollection filters, out ArrayList directories_out, out ArrayList files_out)
3241 directories_out = new ArrayList ();
3242 files_out = new ArrayList ();
3244 if (currentFolderFSEntry.FullName == MWFVFS.DesktopPrefix) {
3245 FSEntry personalFSEntry = GetPersonalFSEntry ();
3247 directories_out.Add (personalFSEntry);
3249 FSEntry myComputerFSEntry = GetMyComputerFSEntry ();
3251 directories_out.Add (myComputerFSEntry);
3253 FSEntry myNetworkFSEntry = GetMyNetworkFSEntry ();
3255 directories_out.Add (myNetworkFSEntry);
3257 ArrayList d_out = null;
3258 ArrayList f_out = null;
3259 GetNormalFolderContent (ThemeEngine.Current.Places (UIIcon.PlacesDesktop), filters, out d_out, out f_out);
3260 directories_out.AddRange (d_out);
3261 files_out.AddRange (f_out);
3264 if (currentFolderFSEntry.FullName == MWFVFS.RecentlyUsedPrefix) {
3265 files_out = GetRecentlyUsedFiles ();
3267 if (currentFolderFSEntry.FullName == MWFVFS.MyComputerPrefix) {
3268 directories_out.AddRange (GetMyComputerContent ());
3270 if (currentFolderFSEntry.FullName == MWFVFS.PersonalPrefix || currentFolderFSEntry.FullName == MWFVFS.MyComputerPersonalPrefix) {
3271 ArrayList d_out = null;
3272 ArrayList f_out = null;
3273 GetNormalFolderContent (ThemeEngine.Current.Places (UIIcon.PlacesPersonal), filters, out d_out, out f_out);
3274 directories_out.AddRange (d_out);
3275 files_out.AddRange (f_out);
3277 if (currentFolderFSEntry.FullName == MWFVFS.MyNetworkPrefix) {
3278 directories_out.AddRange (GetMyNetworkContent ());
3280 GetNormalFolderContent (currentFolderFSEntry.FullName, filters, out directories_out, out files_out);
3284 public ArrayList GetFoldersOnly ()
3286 ArrayList directories_out = new ArrayList ();
3288 if (currentFolderFSEntry.FullName == MWFVFS.DesktopPrefix) {
3289 FSEntry personalFSEntry = GetPersonalFSEntry ();
3291 directories_out.Add (personalFSEntry);
3293 FSEntry myComputerFSEntry = GetMyComputerFSEntry ();
3295 directories_out.Add (myComputerFSEntry);
3297 FSEntry myNetworkFSEntry = GetMyNetworkFSEntry ();
3299 directories_out.Add (myNetworkFSEntry);
3301 ArrayList d_out = GetNormalFolders (ThemeEngine.Current.Places (UIIcon.PlacesDesktop));
3302 directories_out.AddRange (d_out);
3305 if (currentFolderFSEntry.FullName == MWFVFS.RecentlyUsedPrefix) {
3306 //files_out = GetRecentlyUsedFiles ();
3308 if (currentFolderFSEntry.FullName == MWFVFS.MyComputerPrefix) {
3309 directories_out.AddRange (GetMyComputerContent ());
3311 if (currentFolderFSEntry.FullName == MWFVFS.PersonalPrefix || currentFolderFSEntry.FullName == MWFVFS.MyComputerPersonalPrefix) {
3312 ArrayList d_out = GetNormalFolders (ThemeEngine.Current.Places (UIIcon.PlacesPersonal));
3313 directories_out.AddRange (d_out);
3315 if (currentFolderFSEntry.FullName == MWFVFS.MyNetworkPrefix) {
3316 directories_out.AddRange (GetMyNetworkContent ());
3318 directories_out = GetNormalFolders (currentFolderFSEntry.FullName);
3320 return directories_out;
3323 protected void GetNormalFolderContent (string from_folder, StringCollection filters, out ArrayList directories_out, out ArrayList files_out)
3325 DirectoryInfo dirinfo = new DirectoryInfo (from_folder);
3327 directories_out = new ArrayList ();
3329 DirectoryInfo[] dirs = null;
3332 dirs = dirinfo.GetDirectories ();
3333 } catch (Exception) {}
3336 for (int i = 0; i < dirs.Length; i++) {
3337 directories_out.Add (GetDirectoryFSEntry (dirs [i], currentTopFolderFSEntry));
3340 files_out = new ArrayList ();
3342 ArrayList files = new ArrayList ();
3345 if (filters == null) {
3346 files.AddRange (dirinfo.GetFiles ());
3348 foreach (string s in filters)
3349 files.AddRange (dirinfo.GetFiles (s));
3351 files.Sort (fileInfoComparer);
3353 } catch (Exception) {}
3355 for (int i = 0; i < files.Count; i++) {
3356 FSEntry fs = GetFileFSEntry (files [i] as FileInfo);
3362 protected ArrayList GetNormalFolders (string from_folder)
3364 DirectoryInfo dirinfo = new DirectoryInfo (from_folder);
3366 ArrayList directories_out = new ArrayList ();
3368 DirectoryInfo[] dirs = null;
3371 dirs = dirinfo.GetDirectories ();
3372 } catch (Exception) {}
3375 for (int i = 0; i < dirs.Length; i++) {
3376 directories_out.Add (GetDirectoryFSEntry (dirs [i], currentTopFolderFSEntry));
3379 return directories_out;
3382 protected virtual FSEntry GetDirectoryFSEntry (DirectoryInfo dirinfo, FSEntry topFolderFSEntry)
3384 FSEntry fs = new FSEntry ();
3386 fs.Attributes = dirinfo.Attributes;
3387 fs.FullName = dirinfo.FullName;
3388 fs.Name = dirinfo.Name;
3389 fs.MainTopNode = topFolderFSEntry;
3390 fs.FileType = FSEntry.FSEntryType.Directory;
3391 fs.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("inode/directory");
3392 fs.LastAccessTime = dirinfo.LastAccessTime;
3397 protected virtual FSEntry GetFileFSEntry (FileInfo fileinfo)
3399 // *sigh* FileInfo gives us no usable information for links to directories
3401 if ((fileinfo.Attributes & FileAttributes.Directory) == FileAttributes.Directory)
3404 FSEntry fs = new FSEntry ();
3406 fs.Attributes = fileinfo.Attributes;
3407 fs.FullName = fileinfo.FullName;
3408 fs.Name = fileinfo.Name;
3409 fs.FileType = FSEntry.FSEntryType.File;
3410 fs.IconIndex = MimeIconEngine.GetIconIndexForFile (fileinfo.FullName);
3411 fs.FileSize = fileinfo.Length;
3412 fs.LastAccessTime = fileinfo.LastAccessTime;
3417 internal class FileInfoComparer : IComparer
3419 public int Compare (object fileInfo1, object fileInfo2)
3421 return String.Compare (((FileInfo)fileInfo1).Name, ((FileInfo)fileInfo2).Name);
3425 protected abstract FSEntry GetDesktopFSEntry ();
3427 protected abstract FSEntry GetRecentlyUsedFSEntry ();
3429 protected abstract FSEntry GetPersonalFSEntry ();
3431 protected abstract FSEntry GetMyComputerPersonalFSEntry ();
3433 protected abstract FSEntry GetMyComputerFSEntry ();
3435 protected abstract FSEntry GetMyNetworkFSEntry ();
3437 public abstract void WriteRecentlyUsedFiles (string fileToAdd);
3439 public abstract ArrayList GetRecentlyUsedFiles ();
3441 public abstract ArrayList GetMyComputerContent ();
3443 public abstract ArrayList GetMyNetworkContent ();
3447 #region UnixFileSystem
3448 internal class UnixFileSystem : FileSystem
3450 private MasterMount masterMount = new MasterMount ();
3451 private FSEntry desktopFSEntry = null;
3452 private FSEntry recentlyusedFSEntry = null;
3453 private FSEntry personalFSEntry = null;
3454 private FSEntry mycomputerpersonalFSEntry = null;
3455 private FSEntry mycomputerFSEntry = null;
3456 private FSEntry mynetworkFSEntry = null;
3458 private string personal_folder;
3459 private string recently_used_path;
3460 private string full_kde_recent_document_dir;
3462 public UnixFileSystem ()
3464 personal_folder = ThemeEngine.Current.Places (UIIcon.PlacesPersonal);
3465 recently_used_path = Path.Combine (personal_folder, ".recently-used");
3467 full_kde_recent_document_dir = personal_folder + "/.kde/share/apps/RecentDocuments";
3469 desktopFSEntry = new FSEntry ();
3471 desktopFSEntry.Attributes = FileAttributes.Directory;
3472 desktopFSEntry.FullName = MWFVFS.DesktopPrefix;
3473 desktopFSEntry.Name = "Desktop";
3474 desktopFSEntry.RealName = ThemeEngine.Current.Places (UIIcon.PlacesDesktop);
3475 desktopFSEntry.FileType = FSEntry.FSEntryType.Directory;
3476 desktopFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("desktop/desktop");
3477 desktopFSEntry.LastAccessTime = DateTime.Now;
3479 recentlyusedFSEntry = new FSEntry ();
3481 recentlyusedFSEntry.Attributes = FileAttributes.Directory;
3482 recentlyusedFSEntry.FullName = MWFVFS.RecentlyUsedPrefix;
3483 recentlyusedFSEntry.Name = "Recently Used";
3484 recentlyusedFSEntry.FileType = FSEntry.FSEntryType.Directory;
3485 recentlyusedFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("recently/recently");
3486 recentlyusedFSEntry.LastAccessTime = DateTime.Now;
3488 personalFSEntry = new FSEntry ();
3490 personalFSEntry.Attributes = FileAttributes.Directory;
3491 personalFSEntry.FullName = MWFVFS.PersonalPrefix;
3492 personalFSEntry.Name = "Personal";
3493 personalFSEntry.MainTopNode = GetDesktopFSEntry ();
3494 personalFSEntry.RealName = ThemeEngine.Current.Places (UIIcon.PlacesPersonal);
3495 personalFSEntry.FileType = FSEntry.FSEntryType.Directory;
3496 personalFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("directory/home");
3497 personalFSEntry.LastAccessTime = DateTime.Now;
3499 mycomputerpersonalFSEntry = new FSEntry ();
3501 mycomputerpersonalFSEntry.Attributes = FileAttributes.Directory;
3502 mycomputerpersonalFSEntry.FullName = MWFVFS.MyComputerPersonalPrefix;
3503 mycomputerpersonalFSEntry.Name = "Personal";
3504 mycomputerpersonalFSEntry.MainTopNode = GetMyComputerFSEntry ();
3505 mycomputerpersonalFSEntry.RealName = ThemeEngine.Current.Places (UIIcon.PlacesPersonal);
3506 mycomputerpersonalFSEntry.FileType = FSEntry.FSEntryType.Directory;
3507 mycomputerpersonalFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("directory/home");
3508 mycomputerpersonalFSEntry.LastAccessTime = DateTime.Now;
3510 mycomputerFSEntry = new FSEntry ();
3512 mycomputerFSEntry.Attributes = FileAttributes.Directory;
3513 mycomputerFSEntry.FullName = MWFVFS.MyComputerPrefix;
3514 mycomputerFSEntry.Name = "My Computer";
3515 mycomputerFSEntry.MainTopNode = GetDesktopFSEntry ();
3516 mycomputerFSEntry.FileType = FSEntry.FSEntryType.Directory;
3517 mycomputerFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("workplace/workplace");
3518 mycomputerFSEntry.LastAccessTime = DateTime.Now;
3520 mynetworkFSEntry = new FSEntry ();
3522 mynetworkFSEntry.Attributes = FileAttributes.Directory;
3523 mynetworkFSEntry.FullName = MWFVFS.MyNetworkPrefix;
3524 mynetworkFSEntry.Name = "My Network";
3525 mynetworkFSEntry.MainTopNode = GetDesktopFSEntry ();
3526 mynetworkFSEntry.FileType = FSEntry.FSEntryType.Directory;
3527 mynetworkFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("network/network");
3528 mynetworkFSEntry.LastAccessTime = DateTime.Now;
3531 public override void WriteRecentlyUsedFiles (string fileToAdd)
3533 if (File.Exists (recently_used_path) && new FileInfo (recently_used_path).Length > 0) {
3534 XmlDocument xml_doc = new XmlDocument ();
3535 xml_doc.Load (recently_used_path);
3537 XmlNode grand_parent_node = xml_doc.SelectSingleNode ("RecentFiles");
3539 if (grand_parent_node != null) {
3540 // create a new element
3541 XmlElement new_recent_item_node = xml_doc.CreateElement ("RecentItem");
3543 XmlElement new_child = xml_doc.CreateElement ("URI");
3544 UriBuilder ub = new UriBuilder ();
3545 ub.Path = fileToAdd;
3548 XmlText new_text_child = xml_doc.CreateTextNode (ub.ToString ());
3549 new_child.AppendChild (new_text_child);
3551 new_recent_item_node.AppendChild (new_child);
3553 new_child = xml_doc.CreateElement ("Mime-Type");
3554 new_text_child = xml_doc.CreateTextNode (Mime.GetMimeTypeForFile (fileToAdd));
3555 new_child.AppendChild (new_text_child);
3557 new_recent_item_node.AppendChild (new_child);
3559 new_child = xml_doc.CreateElement ("Timestamp");
3560 long seconds = (long)(DateTime.UtcNow - new DateTime (1970, 1, 1)).TotalSeconds;
3561 new_text_child = xml_doc.CreateTextNode (seconds.ToString ());
3562 new_child.AppendChild (new_text_child);
3564 new_recent_item_node.AppendChild (new_child);
3566 new_child = xml_doc.CreateElement ("Groups");
3568 new_recent_item_node.AppendChild (new_child);
3570 // now search the nodes in grand_parent_node for another instance of the new uri and if found remove it
3571 // so that the new node is the first one
3572 foreach (XmlNode n in grand_parent_node.ChildNodes) {
3573 XmlNode uri_node = n.SelectSingleNode ("URI");
3574 if (uri_node != null) {
3575 XmlNode uri_node_child = uri_node.FirstChild;
3576 if (uri_node_child is XmlText)
3577 if (ub.ToString () == ((XmlText)uri_node_child).Data) {
3578 grand_parent_node.RemoveChild (n);
3584 // prepend the new recent item to the grand parent node list
3585 grand_parent_node.PrependChild (new_recent_item_node);
3587 // limit the # of RecentItems to 10
3588 if (grand_parent_node.ChildNodes.Count > 10) {
3589 while (grand_parent_node.ChildNodes.Count > 10)
3590 grand_parent_node.RemoveChild (grand_parent_node.LastChild);
3594 xml_doc.Save (recently_used_path);
3595 } catch (Exception) {
3599 XmlDocument xml_doc = new XmlDocument ();
3600 xml_doc.AppendChild (xml_doc.CreateXmlDeclaration ("1.0", String.Empty, String.Empty));
3602 XmlElement recentFiles_element = xml_doc.CreateElement ("RecentFiles");
3604 XmlElement new_recent_item_node = xml_doc.CreateElement ("RecentItem");
3606 XmlElement new_child = xml_doc.CreateElement ("URI");
3607 UriBuilder ub = new UriBuilder ();
3608 ub.Path = fileToAdd;
3611 XmlText new_text_child = xml_doc.CreateTextNode (ub.ToString ());
3612 new_child.AppendChild (new_text_child);
3614 new_recent_item_node.AppendChild (new_child);
3616 new_child = xml_doc.CreateElement ("Mime-Type");
3617 new_text_child = xml_doc.CreateTextNode (Mime.GetMimeTypeForFile (fileToAdd));
3618 new_child.AppendChild (new_text_child);
3620 new_recent_item_node.AppendChild (new_child);
3622 new_child = xml_doc.CreateElement ("Timestamp");
3623 long seconds = (long)(DateTime.UtcNow - new DateTime (1970, 1, 1)).TotalSeconds;
3624 new_text_child = xml_doc.CreateTextNode (seconds.ToString ());
3625 new_child.AppendChild (new_text_child);
3627 new_recent_item_node.AppendChild (new_child);
3629 new_child = xml_doc.CreateElement ("Groups");
3631 new_recent_item_node.AppendChild (new_child);
3633 recentFiles_element.AppendChild (new_recent_item_node);
3635 xml_doc.AppendChild (recentFiles_element);
3638 xml_doc.Save (recently_used_path);
3639 } catch (Exception) {
3644 // return an ArrayList with FSEntry objects
3645 public override ArrayList GetRecentlyUsedFiles ()
3647 // check for GNOME and KDE
3649 ArrayList files_al = new ArrayList ();
3652 if (File.Exists (recently_used_path)) {
3654 XmlTextReader xtr = new XmlTextReader (recently_used_path);
3655 while (xtr.Read ()) {
3656 if (xtr.NodeType == XmlNodeType.Element && xtr.Name.ToUpper () == "URI") {
3658 Uri uri = new Uri (xtr.Value);
3659 if (!files_al.Contains (uri.LocalPath))
3660 if (File.Exists (uri.LocalPath)) {
3661 FSEntry fs = GetFileFSEntry (new FileInfo (uri.LocalPath));
3668 } catch (Exception) {
3674 if (Directory.Exists (full_kde_recent_document_dir)) {
3675 string[] files = Directory.GetFiles (full_kde_recent_document_dir, "*.desktop");
3677 foreach (string file_name in files) {
3678 StreamReader sr = new StreamReader (file_name);
3680 string line = sr.ReadLine ();
3682 while (line != null) {
3683 line = line.Trim ();
3685 if (line.StartsWith ("URL=")) {
3686 line = line.Replace ("URL=", String.Empty);
3687 line = line.Replace ("$HOME", personal_folder);
3689 Uri uri = new Uri (line);
3690 if (!files_al.Contains (uri.LocalPath))
3691 if (File.Exists (uri.LocalPath)) {
3692 FSEntry fs = GetFileFSEntry (new FileInfo (uri.LocalPath));
3699 line = sr.ReadLine ();
3710 // return an ArrayList with FSEntry objects
3711 public override ArrayList GetMyComputerContent ()
3713 ArrayList my_computer_content_arraylist = new ArrayList ();
3715 if (masterMount.ProcMountAvailable) {
3716 masterMount.GetMounts ();
3718 foreach (MasterMount.Mount mount in masterMount.Block_devices) {
3719 FSEntry fsEntry = new FSEntry ();
3720 fsEntry.FileType = FSEntry.FSEntryType.Device;
3722 fsEntry.FullName = mount.mount_point;
3724 fsEntry.Name = "HDD (" + mount.fsType + ", " + mount.device_short + ")";
3726 fsEntry.FsType = mount.fsType;
3727 fsEntry.DeviceShort = mount.device_short;
3729 fsEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("harddisk/harddisk");
3731 fsEntry.Attributes = FileAttributes.Directory;
3733 fsEntry.MainTopNode = GetMyComputerFSEntry ();
3735 my_computer_content_arraylist.Add (fsEntry);
3737 if (!MWFVFS.MyComputerDevicesPrefix.Contains (fsEntry.FullName + "://"))
3738 MWFVFS.MyComputerDevicesPrefix.Add (fsEntry.FullName + "://", fsEntry);
3741 foreach (MasterMount.Mount mount in masterMount.Removable_devices) {
3742 FSEntry fsEntry = new FSEntry ();
3743 fsEntry.FileType = FSEntry.FSEntryType.RemovableDevice;
3745 fsEntry.FullName = mount.mount_point;
3747 bool is_dvd_cdrom = mount.fsType == MasterMount.FsTypes.usbfs ? false : true;
3748 string type_name = is_dvd_cdrom ? "DVD/CD-Rom" : "USB";
3749 string mime_type = is_dvd_cdrom ? "cdrom/cdrom" : "removable/removable";
3751 fsEntry.Name = type_name +" (" + mount.device_short + ")";
3753 fsEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType (mime_type);
3755 fsEntry.FsType = mount.fsType;
3756 fsEntry.DeviceShort = mount.device_short;
3758 fsEntry.Attributes = FileAttributes.Directory;
3760 fsEntry.MainTopNode = GetMyComputerFSEntry ();
3762 my_computer_content_arraylist.Add (fsEntry);
3764 string contain_string = fsEntry.FullName + "://";
3765 if (!MWFVFS.MyComputerDevicesPrefix.Contains (contain_string))
3766 MWFVFS.MyComputerDevicesPrefix.Add (contain_string, fsEntry);
3770 my_computer_content_arraylist.Add (GetMyComputerPersonalFSEntry ());
3772 return my_computer_content_arraylist;
3775 public override ArrayList GetMyNetworkContent ()
3777 ArrayList fsEntries = new ArrayList ();
3779 foreach (MasterMount.Mount mount in masterMount.Network_devices) {
3780 FSEntry fsEntry = new FSEntry ();
3781 fsEntry.FileType = FSEntry.FSEntryType.Network;
3783 fsEntry.FullName = mount.mount_point;
3785 fsEntry.FsType = mount.fsType;
3786 fsEntry.DeviceShort = mount.device_short;
3788 fsEntry.Name = "Network (" + mount.fsType + ", " + mount.device_short + ")";
3790 switch (mount.fsType) {
3791 case MasterMount.FsTypes.nfs:
3792 fsEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("nfs/nfs");
3794 case MasterMount.FsTypes.smbfs:
3795 fsEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("smb/smb");
3797 case MasterMount.FsTypes.ncpfs:
3798 fsEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("network/network");
3800 case MasterMount.FsTypes.cifs:
3801 fsEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("network/network");
3807 fsEntry.Attributes = FileAttributes.Directory;
3809 fsEntry.MainTopNode = GetMyNetworkFSEntry ();
3811 fsEntries.Add (fsEntry);
3816 protected override FSEntry GetDesktopFSEntry ()
3818 return desktopFSEntry;
3821 protected override FSEntry GetRecentlyUsedFSEntry ()
3823 return recentlyusedFSEntry;
3826 protected override FSEntry GetPersonalFSEntry ()
3828 return personalFSEntry;
3831 protected override FSEntry GetMyComputerPersonalFSEntry ()
3833 return mycomputerpersonalFSEntry;
3836 protected override FSEntry GetMyComputerFSEntry ()
3838 return mycomputerFSEntry;
3841 protected override FSEntry GetMyNetworkFSEntry ()
3843 return mynetworkFSEntry;
3848 #region WinFileSystem
3849 internal class WinFileSystem : FileSystem
3851 private FSEntry desktopFSEntry = null;
3852 private FSEntry recentlyusedFSEntry = null;
3853 private FSEntry personalFSEntry = null;
3854 private FSEntry mycomputerpersonalFSEntry = null;
3855 private FSEntry mycomputerFSEntry = null;
3856 private FSEntry mynetworkFSEntry = null;
3858 public WinFileSystem ()
3860 desktopFSEntry = new FSEntry ();
3862 desktopFSEntry.Attributes = FileAttributes.Directory;
3863 desktopFSEntry.FullName = MWFVFS.DesktopPrefix;
3864 desktopFSEntry.Name = "Desktop";
3865 desktopFSEntry.RealName = ThemeEngine.Current.Places (UIIcon.PlacesDesktop);
3866 desktopFSEntry.FileType = FSEntry.FSEntryType.Directory;
3867 desktopFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("desktop/desktop");
3868 desktopFSEntry.LastAccessTime = DateTime.Now;
3870 recentlyusedFSEntry = new FSEntry ();
3872 recentlyusedFSEntry.Attributes = FileAttributes.Directory;
3873 recentlyusedFSEntry.FullName = MWFVFS.RecentlyUsedPrefix;
3874 recentlyusedFSEntry.RealName = ThemeEngine.Current.Places (UIIcon.PlacesRecentDocuments);
3875 recentlyusedFSEntry.Name = "Recently Used";
3876 recentlyusedFSEntry.FileType = FSEntry.FSEntryType.Directory;
3877 recentlyusedFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("recently/recently");
3878 recentlyusedFSEntry.LastAccessTime = DateTime.Now;
3880 personalFSEntry = new FSEntry ();
3882 personalFSEntry.Attributes = FileAttributes.Directory;
3883 personalFSEntry.FullName = MWFVFS.PersonalPrefix;
3884 personalFSEntry.Name = "Personal";
3885 personalFSEntry.MainTopNode = GetDesktopFSEntry ();
3886 personalFSEntry.RealName = ThemeEngine.Current.Places (UIIcon.PlacesPersonal);
3887 personalFSEntry.FileType = FSEntry.FSEntryType.Directory;
3888 personalFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("directory/home");
3889 personalFSEntry.LastAccessTime = DateTime.Now;
3891 mycomputerpersonalFSEntry = new FSEntry ();
3893 mycomputerpersonalFSEntry.Attributes = FileAttributes.Directory;
3894 mycomputerpersonalFSEntry.FullName = MWFVFS.MyComputerPersonalPrefix;
3895 mycomputerpersonalFSEntry.Name = "Personal";
3896 mycomputerpersonalFSEntry.MainTopNode = GetMyComputerFSEntry ();
3897 mycomputerpersonalFSEntry.RealName = ThemeEngine.Current.Places (UIIcon.PlacesPersonal);
3898 mycomputerpersonalFSEntry.FileType = FSEntry.FSEntryType.Directory;
3899 mycomputerpersonalFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("directory/home");
3900 mycomputerpersonalFSEntry.LastAccessTime = DateTime.Now;
3902 mycomputerFSEntry = new FSEntry ();
3904 mycomputerFSEntry.Attributes = FileAttributes.Directory;
3905 mycomputerFSEntry.FullName = MWFVFS.MyComputerPrefix;
3906 mycomputerFSEntry.Name = "My Computer";
3907 mycomputerFSEntry.MainTopNode = GetDesktopFSEntry ();
3908 mycomputerFSEntry.FileType = FSEntry.FSEntryType.Directory;
3909 mycomputerFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("workplace/workplace");
3910 mycomputerFSEntry.LastAccessTime = DateTime.Now;
3912 mynetworkFSEntry = new FSEntry ();
3914 mynetworkFSEntry.Attributes = FileAttributes.Directory;
3915 mynetworkFSEntry.FullName = MWFVFS.MyNetworkPrefix;
3916 mynetworkFSEntry.Name = "My Network";
3917 mynetworkFSEntry.MainTopNode = GetDesktopFSEntry ();
3918 mynetworkFSEntry.FileType = FSEntry.FSEntryType.Directory;
3919 mynetworkFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("network/network");
3920 mynetworkFSEntry.LastAccessTime = DateTime.Now;
3923 public override void WriteRecentlyUsedFiles (string fileToAdd)
3925 // TODO: Implement this method
3926 // use SHAddToRecentDocs ?
3929 public override ArrayList GetRecentlyUsedFiles ()
3931 ArrayList al = new ArrayList ();
3933 DirectoryInfo di = new DirectoryInfo (recentlyusedFSEntry.RealName);
3935 FileInfo[] fileinfos = di.GetFiles ();
3937 foreach (FileInfo fi in fileinfos) {
3938 FSEntry fs = GetFileFSEntry (fi);
3946 public override ArrayList GetMyComputerContent ()
3948 string[] logical_drives = Directory.GetLogicalDrives ();
3950 ArrayList my_computer_content_arraylist = new ArrayList ();
3952 foreach (string drive in logical_drives) {
3953 FSEntry fsEntry = new FSEntry ();
3954 fsEntry.FileType = FSEntry.FSEntryType.Device;
3956 fsEntry.FullName = drive;
3958 fsEntry.Name = drive;
3960 fsEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("harddisk/harddisk");
3962 fsEntry.Attributes = FileAttributes.Directory;
3964 fsEntry.MainTopNode = GetMyComputerFSEntry ();
3966 my_computer_content_arraylist.Add (fsEntry);
3968 string contain_string = fsEntry.FullName + "://";
3969 if (!MWFVFS.MyComputerDevicesPrefix.Contains (contain_string))
3970 MWFVFS.MyComputerDevicesPrefix.Add (contain_string, fsEntry);
3973 my_computer_content_arraylist.Add (GetMyComputerPersonalFSEntry ());
3975 return my_computer_content_arraylist;
3978 public override ArrayList GetMyNetworkContent ()
3980 // TODO: Implement this method
3981 return new ArrayList ();
3983 protected override FSEntry GetDesktopFSEntry ()
3985 return desktopFSEntry;
3988 protected override FSEntry GetRecentlyUsedFSEntry ()
3990 return recentlyusedFSEntry;
3993 protected override FSEntry GetPersonalFSEntry ()
3995 return personalFSEntry;
3998 protected override FSEntry GetMyComputerPersonalFSEntry ()
4000 return mycomputerpersonalFSEntry;
4003 protected override FSEntry GetMyComputerFSEntry ()
4005 return mycomputerFSEntry;
4008 protected override FSEntry GetMyNetworkFSEntry ()
4010 return mynetworkFSEntry;
4016 internal class FSEntry
4018 public enum FSEntryType
4030 private MasterMount.FsTypes fsType;
4031 private string device_short;
4032 private string fullName;
4033 private string name;
4034 private string realName = null;
4035 private FileAttributes attributes = FileAttributes.Normal;
4036 private long fileSize;
4037 private FSEntryType fileType;
4038 private DateTime lastAccessTime;
4039 private FSEntry mainTopNode = null;
4041 private int iconIndex;
4043 private string parent;
4045 public MasterMount.FsTypes FsType {
4055 public string DeviceShort {
4057 device_short = value;
4061 return device_short;
4065 public string FullName {
4075 public string Name {
4085 public string RealName {
4095 public FileAttributes Attributes {
4105 public long FileSize {
4115 public FSEntryType FileType {
4125 public DateTime LastAccessTime {
4127 lastAccessTime = value;
4131 return lastAccessTime;
4135 public int IconIndex {
4145 public FSEntry MainTopNode {
4147 mainTopNode = value;
4155 public string Parent {
4161 parent = GetParent ();
4167 private string GetParent ()
4169 if (fullName == MWFVFS.PersonalPrefix) {
4170 return MWFVFS.DesktopPrefix;
4172 if (fullName == MWFVFS.MyComputerPersonalPrefix) {
4173 return MWFVFS.MyComputerPrefix;
4175 if (fullName == MWFVFS.MyComputerPrefix) {
4176 return MWFVFS.DesktopPrefix;
4178 if (fullName == MWFVFS.MyNetworkPrefix) {
4179 return MWFVFS.DesktopPrefix;
4181 if (fullName == MWFVFS.DesktopPrefix) {
4184 if (fullName == MWFVFS.RecentlyUsedPrefix) {
4187 foreach (DictionaryEntry entry in MWFVFS.MyComputerDevicesPrefix) {
4188 FSEntry fsEntry = entry.Value as FSEntry;
4189 if (fullName == fsEntry.FullName) {
4190 return fsEntry.MainTopNode.FullName;
4194 DirectoryInfo dirInfo = new DirectoryInfo (fullName);
4196 DirectoryInfo dirInfoParent = dirInfo.Parent;
4198 if (dirInfoParent != null) {
4199 FSEntry fsEntry = MWFVFS.MyComputerDevicesPrefix [dirInfoParent.FullName + "://"] as FSEntry;
4201 if (fsEntry != null) {
4202 return fsEntry.FullName;
4205 if (mainTopNode != null) {
4206 if (dirInfoParent.FullName == ThemeEngine.Current.Places (UIIcon.PlacesDesktop) &&
4207 mainTopNode.FullName == MWFVFS.DesktopPrefix) {
4208 return mainTopNode.FullName;
4210 if (dirInfoParent.FullName == ThemeEngine.Current.Places (UIIcon.PlacesPersonal) &&
4211 mainTopNode.FullName == MWFVFS.PersonalPrefix) {
4212 return mainTopNode.FullName;
4214 if (dirInfoParent.FullName == ThemeEngine.Current.Places (UIIcon.PlacesPersonal) &&
4215 mainTopNode.FullName == MWFVFS.MyComputerPersonalPrefix) {
4216 return mainTopNode.FullName;
4220 return dirInfoParent.FullName;
4230 // Alexsantas little *nix helper
4231 internal class MasterMount
4234 internal enum FsTypes
4258 internal struct Mount
4260 public string device_or_filesystem;
4261 public string device_short;
4262 public string mount_point;
4263 public FsTypes fsType;
4266 bool proc_mount_available = false;
4268 ArrayList block_devices = new ArrayList ();
4269 ArrayList network_devices = new ArrayList ();
4270 ArrayList removable_devices = new ArrayList ();
4272 private int platform = (int) Environment.OSVersion.Platform;
4273 private MountComparer mountComparer = new MountComparer ();
4275 public MasterMount ()
4277 // maybe check if the current user can access /proc/mounts
4278 if ((platform == 4) || (platform == 128))
4279 if (File.Exists ("/proc/mounts"))
4280 proc_mount_available = true;
4283 public ArrayList Block_devices {
4285 return block_devices;
4289 public ArrayList Network_devices {
4291 return network_devices;
4295 public ArrayList Removable_devices {
4297 return removable_devices;
4301 public bool ProcMountAvailable {
4303 return proc_mount_available;
4307 public void GetMounts ()
4309 if (!proc_mount_available)
4312 block_devices.Clear ();
4313 network_devices.Clear ();
4314 removable_devices.Clear ();
4317 StreamReader sr = new StreamReader ("/proc/mounts");
4319 string line = sr.ReadLine ();
4321 ArrayList lines = new ArrayList ();
4322 while (line != null) {
4323 if (lines.IndexOf (line) == -1) { // Avoid duplicates
4324 ProcessProcMountLine (line);
4327 line = sr.ReadLine ();
4332 block_devices.Sort (mountComparer);
4333 network_devices.Sort (mountComparer);
4334 removable_devices.Sort (mountComparer);
4340 private void ProcessProcMountLine (string line)
4342 string[] split = line.Split (new char [] {' '});
4344 if (split != null && split.Length > 0) {
4345 Mount mount = new Mount ();
4347 if (split [0].StartsWith ("/dev/"))
4348 mount.device_short = split [0].Replace ("/dev/", String.Empty);
4350 mount.device_short = split [0];
4352 mount.device_or_filesystem = split [0];
4353 mount.mount_point = split [1];
4355 // TODO: other removable devices, floppy
4359 if (split [2] == "nfs") {
4360 mount.fsType = FsTypes.nfs;
4361 network_devices.Add (mount);
4362 } else if (split [2] == "smbfs") {
4363 mount.fsType = FsTypes.smbfs;
4364 network_devices.Add (mount);
4365 } else if (split [2] == "cifs") {
4366 mount.fsType = FsTypes.cifs;
4367 network_devices.Add (mount);
4368 } else if (split [2] == "ncpfs") {
4369 mount.fsType = FsTypes.ncpfs;
4370 network_devices.Add (mount);
4372 } else if (split [2] == "iso9660") { //cdrom
4373 mount.fsType = FsTypes.iso9660;
4374 removable_devices.Add (mount);
4375 } else if (split [2] == "usbfs") { //usb ? not tested
4376 mount.fsType = FsTypes.usbfs;
4377 removable_devices.Add (mount);
4379 } else if (split [0].StartsWith ("/")) { //block devices
4380 if (split [1].StartsWith ("/dev/")) // root static, do not add
4383 if (split [2] == "ext2")
4384 mount.fsType = FsTypes.ext2;
4385 else if (split [2] == "ext3")
4386 mount.fsType = FsTypes.ext3;
4387 else if (split [2] == "reiserfs")
4388 mount.fsType = FsTypes.reiserfs;
4389 else if (split [2] == "xfs")
4390 mount.fsType = FsTypes.xfs;
4391 else if (split [2] == "vfat")
4392 mount.fsType = FsTypes.vfat;
4393 else if (split [2] == "ntfs")
4394 mount.fsType = FsTypes.ntfs;
4395 else if (split [2] == "msdos")
4396 mount.fsType = FsTypes.msdos;
4397 else if (split [2] == "umsdos")
4398 mount.fsType = FsTypes.umsdos;
4399 else if (split [2] == "hpfs")
4400 mount.fsType = FsTypes.hpfs;
4401 else if (split [2] == "minix")
4402 mount.fsType = FsTypes.minix;
4403 else if (split [2] == "jfs")
4404 mount.fsType = FsTypes.jfs;
4406 block_devices.Add (mount);
4411 public class MountComparer : IComparer
4413 public int Compare (object mount1, object mount2)
4415 return String.Compare (((Mount)mount1).device_short, ((Mount)mount2).device_short);
4422 // easy to use class to store and read internal MWF config settings.
4423 // the config values are stored in the users home dir as a hidden xml file called "mwf_config".
4424 // currently supports int, string, byte, color and arrays (including byte arrays)
4425 // don't forget, when you read a value you still have to cast this value.
4428 // MWFConfig.SetValue ("SomeClass", "What", value);
4429 // object o = MWFConfig.GetValue ("SomeClass", "What");
4433 // string[] configFileNames = (string[])MWFConfig.GetValue ("FileDialog", "FileNames");
4434 // MWFConfig.SetValue ("FileDialog", "LastFolder", "/home/user");
4436 internal class MWFConfig
4438 private static MWFConfigInstance Instance = new MWFConfigInstance ();
4440 private static object lock_object = new object();
4442 public static object GetValue (string class_name, string value_name)
4444 lock (lock_object) {
4445 return Instance.GetValue (class_name, value_name);
4449 public static void SetValue (string class_name, string value_name, object value)
4451 lock (lock_object) {
4452 Instance.SetValue (class_name, value_name, value);
4456 public static void Flush ()
4458 lock (lock_object) {
4463 public static void RemoveClass (string class_name)
4465 lock (lock_object) {
4466 Instance.RemoveClass (class_name);
4470 public static void RemoveClassValue (string class_name, string value_name)
4472 lock (lock_object) {
4473 Instance.RemoveClassValue (class_name, value_name);
4477 public static void RemoveAllClassValues (string class_name)
4479 lock (lock_object) {
4480 Instance.RemoveAllClassValues (class_name);
4484 internal class MWFConfigInstance
4486 Hashtable classes_hashtable = new Hashtable ();
4487 static string full_file_name;
4488 static string default_file_name;
4489 readonly string configName = "MWFConfig";
4490 static int platform = (int) Environment.OSVersion.Platform;
4492 static bool IsUnix ()
4494 return (platform == 4 || platform == 128);
4497 static MWFConfigInstance ()
4499 string b = "mwf_config";
4500 string dir = Environment.GetFolderPath (Environment.SpecialFolder.Personal);
4503 dir = Path.Combine (dir, ".mono");
4505 Directory.CreateDirectory (dir);
4509 default_file_name = Path.Combine (dir, b);
4510 full_file_name = default_file_name;
4513 public MWFConfigInstance ()
4515 Open (default_file_name);
4519 public MWFConfigInstance (string filename)
4521 string path = Path.GetDirectoryName (filename);
4522 if (path == null || path == String.Empty) {
4523 path = Environment.GetFolderPath (Environment.SpecialFolder.Personal);
4525 full_file_name = Path.Combine (path, filename);
4527 full_file_name = filename;
4529 Open (full_file_name);
4532 ~MWFConfigInstance ()
4537 public object GetValue (string class_name, string value_name)
4539 ClassEntry class_entry = classes_hashtable [class_name] as ClassEntry;
4541 if (class_entry != null)
4542 return class_entry.GetValue (value_name);
4547 public void SetValue (string class_name, string value_name, object value)
4549 ClassEntry class_entry = classes_hashtable [class_name] as ClassEntry;
4551 if (class_entry == null) {
4552 class_entry = new ClassEntry ();
4553 class_entry.ClassName = class_name;
4554 classes_hashtable [class_name] = class_entry;
4557 class_entry.SetValue (value_name, value);
4560 private void Open (string filename)
4563 XmlTextReader xtr = new XmlTextReader (filename);
4568 } catch (Exception) {
4572 public void Flush ()
4575 XmlTextWriter xtw = new XmlTextWriter (full_file_name, null);
4576 xtw.Formatting = Formatting.Indented;
4583 File.SetAttributes (full_file_name, FileAttributes.Hidden);
4584 } catch (Exception){
4588 public void RemoveClass (string class_name)
4590 ClassEntry class_entry = classes_hashtable [class_name] as ClassEntry;
4592 if (class_entry != null) {
4593 class_entry.RemoveAllClassValues ();
4595 classes_hashtable.Remove (class_name);
4599 public void RemoveClassValue (string class_name, string value_name)
4601 ClassEntry class_entry = classes_hashtable [class_name] as ClassEntry;
4603 if (class_entry != null) {
4604 class_entry.RemoveClassValue (value_name);
4608 public void RemoveAllClassValues (string class_name)
4610 ClassEntry class_entry = classes_hashtable [class_name] as ClassEntry;
4612 if (class_entry != null) {
4613 class_entry.RemoveAllClassValues ();
4617 private void ReadConfig (XmlTextReader xtr)
4619 if (!CheckForMWFConfig (xtr))
4622 while (xtr.Read ()) {
4623 switch (xtr.NodeType) {
4624 case XmlNodeType.Element:
4625 ClassEntry class_entry = classes_hashtable [xtr.Name] as ClassEntry;
4627 if (class_entry == null) {
4628 class_entry = new ClassEntry ();
4629 class_entry.ClassName = xtr.Name;
4630 classes_hashtable [xtr.Name] = class_entry;
4633 class_entry.ReadXml (xtr);
4639 private bool CheckForMWFConfig (XmlTextReader xtr)
4642 if (xtr.NodeType == XmlNodeType.Element) {
4643 if (xtr.Name == configName)
4651 private void WriteConfig (XmlTextWriter xtw)
4653 if (classes_hashtable.Count == 0)
4656 xtw.WriteStartElement (configName);
4657 foreach (DictionaryEntry entry in classes_hashtable) {
4658 ClassEntry class_entry = entry.Value as ClassEntry;
4660 class_entry.WriteXml (xtw);
4662 xtw.WriteEndElement ();
4665 internal class ClassEntry
4667 private Hashtable classvalues_hashtable = new Hashtable ();
4668 private string className;
4670 public string ClassName {
4680 public void SetValue (string value_name, object value)
4682 ClassValue class_value = classvalues_hashtable [value_name] as ClassValue;
4684 if (class_value == null) {
4685 class_value = new ClassValue ();
4686 class_value.Name = value_name;
4687 classvalues_hashtable [value_name] = class_value;
4690 class_value.SetValue (value);
4693 public object GetValue (string value_name)
4695 ClassValue class_value = classvalues_hashtable [value_name] as ClassValue;
4697 if (class_value == null) {
4701 return class_value.GetValue ();
4704 public void RemoveAllClassValues ()
4706 classvalues_hashtable.Clear ();
4709 public void RemoveClassValue (string value_name)
4711 ClassValue class_value = classvalues_hashtable [value_name] as ClassValue;
4713 if (class_value != null) {
4714 classvalues_hashtable.Remove (value_name);
4718 public void ReadXml (XmlTextReader xtr)
4720 while (xtr.Read ()) {
4721 switch (xtr.NodeType) {
4722 case XmlNodeType.Element:
4723 string name = xtr.GetAttribute ("name");
4725 ClassValue class_value = classvalues_hashtable [name] as ClassValue;
4727 if (class_value == null) {
4728 class_value = new ClassValue ();
4729 class_value.Name = name;
4730 classvalues_hashtable [name] = class_value;
4733 class_value.ReadXml (xtr);
4736 case XmlNodeType.EndElement:
4742 public void WriteXml (XmlTextWriter xtw)
4744 if (classvalues_hashtable.Count == 0)
4747 xtw.WriteStartElement (className);
4749 foreach (DictionaryEntry entry in classvalues_hashtable) {
4750 ClassValue class_value = entry.Value as ClassValue;
4752 class_value.WriteXml (xtw);
4755 xtw.WriteEndElement ();
4759 internal class ClassValue
4761 private object value;
4762 private string name;
4764 public string Name {
4774 public void SetValue (object value)
4778 public object GetValue ()
4783 public void ReadXml (XmlTextReader xtr)
4786 string single_value;
4788 type = xtr.GetAttribute ("type");
4790 if (type == "byte_array" || type.IndexOf ("-array") == -1) {
4791 single_value = xtr.ReadString ();
4793 if (type == "string") {
4794 value = single_value;
4796 if (type == "int") {
4797 value = Int32.Parse (single_value);
4799 if (type == "byte") {
4800 value = Byte.Parse (single_value);
4802 if (type == "color") {
4803 int color = Int32.Parse (single_value);
4804 value = Color.FromArgb (color);
4806 if (type == "byte-array") {
4807 byte[] b_array = Convert.FromBase64String (single_value);
4811 ReadXmlArrayValues (xtr, type);
4815 private void ReadXmlArrayValues (XmlTextReader xtr, string type)
4817 ArrayList al = new ArrayList ();
4819 while (xtr.Read ()) {
4820 switch (xtr.NodeType) {
4821 case XmlNodeType.Element:
4822 string single_value = xtr.ReadString ();
4824 if (type == "int-array") {
4825 int int_val = Int32.Parse (single_value);
4828 if (type == "string-array") {
4829 string str_val = single_value;
4834 case XmlNodeType.EndElement:
4835 if (xtr.Name == "value") {
4836 if (type == "int-array") {
4837 value = al.ToArray (typeof(int));
4839 if (type == "string-array") {
4840 value = al.ToArray (typeof(string));
4849 public void WriteXml (XmlTextWriter xtw)
4851 xtw.WriteStartElement ("value");
4852 xtw.WriteAttributeString ("name", name);
4853 if (value is Array) {
4854 WriteArrayContent (xtw);
4856 WriteSingleContent (xtw);
4858 xtw.WriteEndElement ();
4861 private void WriteSingleContent (XmlTextWriter xtw)
4863 string type_string = String.Empty;
4865 if (value is string)
4866 type_string = "string";
4869 type_string = "int";
4872 type_string = "byte";
4875 type_string = "color";
4877 xtw.WriteAttributeString ("type", type_string);
4880 xtw.WriteString (((Color)value).ToArgb ().ToString ());
4882 xtw.WriteString (value.ToString ());
4885 private void WriteArrayContent (XmlTextWriter xtw)
4887 string type_string = String.Empty;
4888 string type_name = String.Empty;
4890 if (value is string[]) {
4891 type_string = "string-array";
4892 type_name = "string";
4894 if (value is int[]) {
4895 type_string = "int-array";
4898 if (value is byte[]) {
4899 type_string = "byte-array";
4903 xtw.WriteAttributeString ("type", type_string);
4905 if (type_string != "byte-array") {
4906 Array array = value as Array;
4908 foreach (object o in array) {
4909 xtw.WriteStartElement (type_name);
4910 xtw.WriteString (o.ToString ());
4911 xtw.WriteEndElement ();
4914 byte[] b_array = value as byte [];
4916 xtw.WriteString (Convert.ToBase64String (b_array, 0, b_array.Length));