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 string initialDirectory;
67 private bool restoreDirectory;
68 private bool showHelp;
70 private bool validateNames = true;
72 private bool supportMultiDottedExtensions;
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 private FileFilter fileFilter;
110 private string[] configFileNames = null;
111 private string lastFolder = String.Empty;
115 private const string filedialog_string = "FileDialog";
116 private const string lastfolder_string = "LastFolder";
117 private const string width_string = "Width";
118 private const string height_string = "Height";
119 private const string filenames_string = "FileNames";
120 private const string x_string = "X";
121 private const string y_string = "Y";
123 private bool disable_form_closed_event;
125 internal FileDialog ()
129 Size formConfigSize = Size.Empty;
130 Point formConfigLocation = Point.Empty;
132 object formWidth = MWFConfig.GetValue (filedialog_string, width_string);
134 object formHeight = MWFConfig.GetValue (filedialog_string, height_string);
136 if (formHeight != null && formWidth != null)
137 formConfigSize = new Size ((int)formWidth, (int)formHeight);
139 object formLocationX = MWFConfig.GetValue (filedialog_string, x_string);
140 object formLocationY = MWFConfig.GetValue (filedialog_string, y_string);
142 if (formLocationX != null && formLocationY != null)
143 formConfigLocation = new Point ((int)formLocationX, (int)formLocationY);
145 configFileNames = (string[])MWFConfig.GetValue (filedialog_string, filenames_string);
147 fileTypeComboBox = new ComboBox ();
148 backToolBarButton = new ToolBarButton ();
149 newdirToolBarButton = new ToolBarButton ();
150 searchSaveLabel = new Label ();
151 mwfFileView = new MWFFileView (vfs);
152 fileNameLabel = new Label ();
153 fileNameComboBox = new ComboBox ();
154 dirComboBox = new DirComboBox (vfs);
155 smallButtonToolBar = new ToolBar ();
156 menueToolBarButton = new ToolBarButton ();
157 fileTypeLabel = new Label ();
158 openSaveButton = new Button ();
159 form.AcceptButton = openSaveButton;
160 helpButton = new Button ();
161 popupButtonPanel = new PopupButtonPanel ();
162 upToolBarButton = new ToolBarButton ();
163 cancelButton = new Button ();
164 form.CancelButton = cancelButton;
165 imageListTopToolbar = new ImageList ();
166 menueToolBarButtonContextMenu = new ContextMenu ();
167 readonlyCheckBox = new CheckBox ();
169 form.SuspendLayout ();
171 //imageListTopToolbar
172 imageListTopToolbar.ColorDepth = ColorDepth.Depth32Bit;
173 imageListTopToolbar.ImageSize = new Size (16, 16); // 16, 16
174 imageListTopToolbar.Images.Add (ResourceImageLoader.Get ("go-previous.png"));
175 imageListTopToolbar.Images.Add (ResourceImageLoader.Get ("go-top.png"));
176 imageListTopToolbar.Images.Add (ResourceImageLoader.Get ("folder-new.png"));
177 imageListTopToolbar.Images.Add (ResourceImageLoader.Get ("preferences-system-windows.png"));
178 imageListTopToolbar.TransparentColor = Color.Transparent;
181 searchSaveLabel.FlatStyle = FlatStyle.System;
182 searchSaveLabel.Location = new Point (7, 8);
183 searchSaveLabel.Size = new Size (72, 21);
184 searchSaveLabel.TextAlign = ContentAlignment.MiddleRight;
187 dirComboBox.Anchor = ((AnchorStyles)(((AnchorStyles.Top | AnchorStyles.Left) | AnchorStyles.Right)));
188 dirComboBox.DropDownStyle = ComboBoxStyle.DropDownList;
189 dirComboBox.Location = new Point (99, 8);
190 dirComboBox.Size = new Size (260, 21);
191 dirComboBox.TabIndex = 7;
193 // smallButtonToolBar
194 smallButtonToolBar.Anchor = ((AnchorStyles)((AnchorStyles.Top | AnchorStyles.Right)));
195 smallButtonToolBar.Appearance = ToolBarAppearance.Flat;
196 smallButtonToolBar.AutoSize = false;
197 smallButtonToolBar.Buttons.AddRange (new ToolBarButton [] {
201 menueToolBarButton});
202 smallButtonToolBar.ButtonSize = new Size (24, 24); // 21, 16
203 smallButtonToolBar.Divider = false;
204 smallButtonToolBar.Dock = DockStyle.None;
205 smallButtonToolBar.DropDownArrows = true;
206 smallButtonToolBar.ImageList = imageListTopToolbar;
207 smallButtonToolBar.Location = new Point (372, 6);
208 smallButtonToolBar.ShowToolTips = true;
209 smallButtonToolBar.Size = new Size (140, 28);
210 smallButtonToolBar.TabIndex = 8;
211 smallButtonToolBar.TextAlign = ToolBarTextAlign.Right;
214 popupButtonPanel.Dock = DockStyle.None;
215 popupButtonPanel.Location = new Point (7, 37);
216 popupButtonPanel.TabIndex = 9;
219 mwfFileView.Anchor = ((AnchorStyles)((((AnchorStyles.Top | AnchorStyles.Bottom) | AnchorStyles.Left) | AnchorStyles.Right)));
220 mwfFileView.Location = new Point (99, 37);
221 mwfFileView.Size = new Size (449, 282);
222 mwfFileView.Columns.Add (" Name", 170, HorizontalAlignment.Left);
223 mwfFileView.Columns.Add ("Size ", 80, HorizontalAlignment.Right);
224 mwfFileView.Columns.Add (" Type", 100, HorizontalAlignment.Left);
225 mwfFileView.Columns.Add (" Last Access", 150, HorizontalAlignment.Left);
226 mwfFileView.AllowColumnReorder = true;
227 mwfFileView.MultiSelect = false;
228 mwfFileView.TabIndex = 10;
229 mwfFileView.RegisterSender (dirComboBox);
230 mwfFileView.RegisterSender (popupButtonPanel);
233 fileNameLabel.Anchor = ((AnchorStyles)((AnchorStyles.Bottom | AnchorStyles.Left)));
234 fileNameLabel.FlatStyle = FlatStyle.System;
235 fileNameLabel.Location = new Point (102, 330);
236 fileNameLabel.Size = new Size (70, 21);
237 fileNameLabel.Text = "File name:";
238 fileNameLabel.TextAlign = ContentAlignment.MiddleLeft;
241 fileNameComboBox.Anchor = ((AnchorStyles)(((AnchorStyles.Bottom | AnchorStyles.Left) | AnchorStyles.Right)));
242 fileNameComboBox.Location = new Point (195, 330);
243 fileNameComboBox.Size = new Size (245, 21);
244 fileNameComboBox.TabIndex = 1;
245 fileNameComboBox.MaxDropDownItems = MaxFileNameItems;
246 UpdateRecentFiles ();
249 fileTypeLabel.Anchor = ((AnchorStyles)((AnchorStyles.Bottom | AnchorStyles.Left)));
250 fileTypeLabel.FlatStyle = FlatStyle.System;
251 fileTypeLabel.Location = new Point (102, 356);
252 fileTypeLabel.Size = new Size (90, 21);
253 fileTypeLabel.Text = "Files of type:";
254 fileTypeLabel.TextAlign = ContentAlignment.MiddleLeft;
257 fileTypeComboBox.Anchor = ((AnchorStyles)(((AnchorStyles.Bottom | AnchorStyles.Left) | AnchorStyles.Right)));
258 fileTypeComboBox.DropDownStyle = ComboBoxStyle.DropDownList;
259 fileTypeComboBox.Location = new Point (195, 356);
260 fileTypeComboBox.Size = new Size (245, 21);
261 fileTypeComboBox.TabIndex = 2;
264 backToolBarButton.ImageIndex = 0;
265 backToolBarButton.Enabled = false;
266 backToolBarButton.Style = ToolBarButtonStyle.PushButton;
267 mwfFileView.AddControlToEnableDisableByDirStack (backToolBarButton);
270 upToolBarButton.ImageIndex = 1;
271 upToolBarButton.Style = ToolBarButtonStyle.PushButton;
272 mwfFileView.SetFolderUpToolBarButton (upToolBarButton);
274 // newdirToolBarButton
275 newdirToolBarButton.ImageIndex = 2;
276 newdirToolBarButton.Style = ToolBarButtonStyle.PushButton;
278 // menueToolBarButton
279 menueToolBarButton.ImageIndex = 3;
280 menueToolBarButton.DropDownMenu = menueToolBarButtonContextMenu;
281 menueToolBarButton.Style = ToolBarButtonStyle.DropDownButton;
283 // menueToolBarButtonContextMenu
284 menueToolBarButtonContextMenu.MenuItems.AddRange (mwfFileView.ViewMenuItems);
287 openSaveButton.Anchor = ((AnchorStyles)((AnchorStyles.Bottom | AnchorStyles.Right)));
288 openSaveButton.FlatStyle = FlatStyle.System;
289 openSaveButton.Location = new Point (475, 330);
290 openSaveButton.Size = new Size (72, 21);
291 openSaveButton.TabIndex = 4;
292 openSaveButton.FlatStyle = FlatStyle.System;
295 cancelButton.Anchor = ((AnchorStyles)((AnchorStyles.Bottom | AnchorStyles.Right)));
296 cancelButton.FlatStyle = FlatStyle.System;
297 cancelButton.Location = new Point (475, 356);
298 cancelButton.Size = new Size (72, 21);
299 cancelButton.TabIndex = 5;
300 cancelButton.Text = "Cancel";
301 cancelButton.FlatStyle = FlatStyle.System;
304 helpButton.Anchor = ((AnchorStyles)((AnchorStyles.Bottom | AnchorStyles.Right)));
305 helpButton.FlatStyle = FlatStyle.System;
306 helpButton.Location = new Point (475, 350);
307 helpButton.Size = new Size (72, 21);
308 helpButton.TabIndex = 6;
309 helpButton.Text = "Help";
310 helpButton.FlatStyle = FlatStyle.System;
313 readonlyCheckBox.Anchor = ((AnchorStyles)(((AnchorStyles.Bottom | AnchorStyles.Left) | AnchorStyles.Right)));
314 readonlyCheckBox.Text = "Open Readonly";
315 readonlyCheckBox.Location = new Point (195, 350);
316 readonlyCheckBox.Size = new Size (245, 21);
317 readonlyCheckBox.TabIndex = 3;
318 readonlyCheckBox.FlatStyle = FlatStyle.System;
320 form.SizeGripStyle = SizeGripStyle.Show;
322 form.MaximizeBox = true;
323 form.MinimizeBox = true;
324 form.FormBorderStyle = FormBorderStyle.Sizable;
325 form.MinimumSize = new Size (554, 405);
327 form.ClientSize = new Size (554, 405); // 384
329 form.Controls.Add (smallButtonToolBar);
330 form.Controls.Add (cancelButton);
331 form.Controls.Add (openSaveButton);
332 form.Controls.Add (mwfFileView);
333 form.Controls.Add (fileTypeLabel);
334 form.Controls.Add (fileNameLabel);
335 form.Controls.Add (fileTypeComboBox);
336 form.Controls.Add (fileNameComboBox);
337 form.Controls.Add (dirComboBox);
338 form.Controls.Add (searchSaveLabel);
339 form.Controls.Add (popupButtonPanel);
341 form.ResumeLayout (false);
343 if (formConfigSize != Size.Empty) {
344 form.Size = formConfigSize;
347 if (formConfigLocation != Point.Empty) {
348 form.Location = formConfigLocation;
351 openSaveButton.Click += new EventHandler (OnClickOpenSaveButton);
352 cancelButton.Click += new EventHandler (OnClickCancelButton);
353 helpButton.Click += new EventHandler (OnClickHelpButton);
355 smallButtonToolBar.ButtonClick += new ToolBarButtonClickEventHandler (OnClickSmallButtonToolBar);
357 fileTypeComboBox.SelectedIndexChanged += new EventHandler (OnSelectedIndexChangedFileTypeComboBox);
359 mwfFileView.SelectedFileChanged += new EventHandler (OnSelectedFileChangedFileView);
360 mwfFileView.ForceDialogEnd += new EventHandler (OnForceDialogEndFileView);
361 mwfFileView.SelectedFilesChanged += new EventHandler (OnSelectedFilesChangedFileView);
363 dirComboBox.DirectoryChanged += new EventHandler (OnDirectoryChangedDirComboBox);
364 popupButtonPanel.DirectoryChanged += new EventHandler (OnDirectoryChangedPopupButtonPanel);
366 readonlyCheckBox.CheckedChanged += new EventHandler (OnCheckCheckChanged);
368 form.FormClosed += new FormClosedEventHandler (OnFileDialogFormClosed);
370 form.Closed += new EventHandler (OnFileDialogFormClosed);
375 public bool AddExtension {
381 addExtension = value;
385 [DefaultValue(false)]
386 public virtual bool CheckFileExists {
388 return checkFileExists;
392 checkFileExists = value;
397 public bool CheckPathExists {
399 return checkPathExists;
403 checkPathExists = value;
408 public string DefaultExt {
410 if (defaultExt == null)
415 if (value != null && value.Length > 0) {
416 // remove leading dot
417 if (value [0] == '.')
418 value = value.Substring (1);
424 // in MS.NET it doesn't make a difference if
425 // DerefenceLinks is true or false
426 // if the selected file is a link FileDialog
427 // always returns the link
429 public bool DereferenceLinks {
431 return dereferenceLinks;
435 dereferenceLinks = value;
440 public string FileName {
442 if (fileNames == null || fileNames.Length == 0)
445 if (fileNames [0].Length == 0)
449 // skip check for illegal characters if the filename was set
450 // through FileDialog API
451 if (!checkForIllegalChars)
452 return fileNames [0];
455 // ensure filename contains only valid characters
456 Path.GetFullPath (fileNames [0]);
457 // but return filename as is
458 return fileNames [0];
464 fileNames = new string [1] { value };
466 fileNames = new string [0];
470 // skip check for illegal characters if the filename was set
471 // through FileDialog API
472 checkForIllegalChars = false;
478 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
479 public string[] FileNames {
481 if (fileNames == null || fileNames.Length == 0) {
482 return new string [0];
485 string[] new_filenames = new string [fileNames.Length];
486 fileNames.CopyTo (new_filenames, 0);
489 // skip check for illegal characters if the filename was set
490 // through FileDialog API
491 if (!checkForIllegalChars)
492 return new_filenames;
495 foreach (string fileName in new_filenames) {
496 // ensure filename contains only valid characters
497 Path.GetFullPath (fileName);
499 return new_filenames;
505 public string Filter {
513 if (fileFilter != null)
514 fileFilter.FilterArrayList.Clear ();
516 if (FileFilter.CheckFilter (value)) {
519 fileFilter = new FileFilter (filter);
521 throw new ArgumentException ("The provided filter string"
522 + " is invalid. The filter string should contain a"
523 + " description of the filter, followed by the "
524 + " vertical bar (|) and the filter pattern. The"
525 + " strings for different filtering options should"
526 + " also be separated by the vertical bar. Example:"
527 + " Text files (*.txt)|*.txt|All files (*.*)|*.*");
535 public int FilterIndex {
545 public string InitialDirectory {
547 if (initialDirectory == null)
549 return initialDirectory;
552 initialDirectory = value;
556 [DefaultValue(false)]
557 public bool RestoreDirectory {
559 return restoreDirectory;
563 restoreDirectory = value;
567 [DefaultValue(false)]
568 public bool ShowHelp {
575 ResizeAndRelocateForHelpOrReadOnly ();
580 [DefaultValue(false)]
581 public bool SupportMultiDottedExtensions {
583 return supportMultiDottedExtensions;
587 supportMultiDottedExtensions = value;
594 public string Title {
605 // this one is a hard one ;)
607 // - up to MAX_PATH characters (windef.h) = 260
608 // - no trailing dots or spaces
611 // NTFS/Posix filename:
612 // - up to 32,768 Unicode characters
613 // - trailing periods or spaces
617 public bool ValidateNames {
619 return validateNames;
623 validateNames = value;
627 public override void Reset ()
630 checkFileExists = false;
631 checkPathExists = true;
633 dereferenceLinks = true;
635 Filter = String.Empty;
637 InitialDirectory = null;
638 restoreDirectory = false;
640 SupportMultiDottedExtensions = false;
644 validateNames = true;
649 public override string ToString ()
651 return String.Format("{0}: Title: {1}, FileName: {2}", base.ToString (), Title, FileName);
654 public event CancelEventHandler FileOk {
655 add { Events.AddHandler (EventFileOk, value); }
656 remove { Events.RemoveHandler (EventFileOk, value); }
659 protected virtual IntPtr Instance {
667 // This is just for internal use with MSs version, so it doesn't need to be implemented
668 // as it can't really be accessed anyways
669 protected int Options {
673 internal virtual string DialogTitle {
680 protected override IntPtr HookProc (IntPtr hWnd, int msg, IntPtr wparam, IntPtr lparam)
682 throw new NotImplementedException ();
685 protected void OnFileOk (CancelEventArgs e)
687 WriteConfigValues (e);
689 CancelEventHandler fo = (CancelEventHandler) Events [EventFileOk];
693 Mime.CleanFileCache ();
695 disable_form_closed_event = true;
698 protected override bool RunDialog (IntPtr hWndOwner)
701 form.Text = DialogTitle;
703 // avoid using the FileNames property to skip the invalid characters
705 string fileName = null;
706 if (fileNames != null && fileNames.Length != 0)
707 fileName = fileNames [0];
709 fileName = string.Empty;
715 SetFileAndDirectory (fileName);
716 fileNameComboBox.Select ();
721 internal virtual bool ShowReadOnly {
723 showReadOnly = value;
724 ResizeAndRelocateForHelpOrReadOnly ();
732 internal virtual bool ReadOnlyChecked {
734 readOnlyChecked = value;
735 readonlyCheckBox.Checked = value;
739 return readOnlyChecked;
743 internal bool BMultiSelect {
746 mwfFileView.MultiSelect = value;
754 internal string OpenSaveButtonText {
756 openSaveButton.Text = value;
760 internal string SearchSaveLabel {
762 searchSaveLabel.Text = value;
766 internal string FileTypeLabel {
768 fileTypeLabel.Text = value;
772 private void SelectFilter ()
774 int filter_to_select = (filterIndex - 1);
776 if (mwfFileView.FilterArrayList == null || mwfFileView.FilterArrayList.Count == 0) {
777 filter_to_select = -1;
779 if (filter_to_select < 0 || filter_to_select >= mwfFileView.FilterArrayList.Count)
780 filter_to_select = 0;
783 do_not_call_OnSelectedIndexChangedFileTypeComboBox = true;
784 fileTypeComboBox.BeginUpdate ();
785 fileTypeComboBox.SelectedIndex = filter_to_select;
786 fileTypeComboBox.EndUpdate ();
787 do_not_call_OnSelectedIndexChangedFileTypeComboBox = false;
788 mwfFileView.FilterIndex = filter_to_select + 1;
791 private void SetFileAndDirectory (string fname)
793 if (fname.Length != 0) {
794 bool rooted = Path.IsPathRooted (fname);
796 mwfFileView.ChangeDirectory (null, lastFolder);
797 fileNameComboBox.Text = fname;
799 string dirname = Path.GetDirectoryName (fname);
800 if (dirname != null && dirname.Length > 0 && Directory.Exists (dirname)) {
801 fileNameComboBox.Text = Path.GetFileName (fname);
802 mwfFileView.ChangeDirectory (null, dirname);
804 fileNameComboBox.Text = fname;
805 mwfFileView.ChangeDirectory (null, lastFolder);
809 mwfFileView.ChangeDirectory (null, lastFolder);
810 fileNameComboBox.Text = null;
814 void OnClickOpenSaveButton (object sender, EventArgs e)
817 // for filenames typed or selected by user, enable check for
818 // illegal characters in filename(s)
819 checkForIllegalChars = true;
822 if (fileDialogType == FileDialogType.OpenFileDialog) {
823 ListView.SelectedListViewItemCollection sl = mwfFileView.SelectedItems;
824 if (sl.Count > 0 && sl [0] != null) {
826 FileViewListViewItem item = sl [0] as FileViewListViewItem;
827 FSEntry fsEntry = item.FSEntry;
829 if (fsEntry.Attributes == FileAttributes.Directory) {
830 mwfFileView.ChangeDirectory (null, fsEntry.FullName);
834 foreach (FileViewListViewItem item in sl) {
835 FSEntry fsEntry = item.FSEntry;
836 if (fsEntry.Attributes == FileAttributes.Directory) {
837 mwfFileView.ChangeDirectory (null, fsEntry.FullName);
845 ArrayList files = new ArrayList ();
846 FileNamesTokenizer tokenizer = new FileNamesTokenizer (
847 fileNameComboBox.Text, multiSelect);
848 tokenizer.GetNextFile ();
849 while (tokenizer.CurrentToken != TokenType.EOF) {
850 string fileName = tokenizer.TokenText;
851 string internalfullfilename;
853 if (!Path.IsPathRooted (fileName)) {
854 // on unix currentRealFolder for "Recently used files" is null,
855 // because recently used files don't get saved as links in a directory
856 // recently used files get saved in a xml file
857 if (mwfFileView.CurrentRealFolder != null)
858 fileName = Path.Combine (mwfFileView.CurrentRealFolder, fileName);
860 if (mwfFileView.CurrentFSEntry != null) {
861 fileName = mwfFileView.CurrentFSEntry.FullName;
865 FileInfo fileInfo = new FileInfo (fileName);
867 if (fileInfo.Exists || fileDialogType == FileDialogType.SaveFileDialog) {
868 internalfullfilename = fileName;
870 DirectoryInfo dirInfo = new DirectoryInfo (fileName);
871 if (dirInfo.Exists) {
872 mwfFileView.ChangeDirectory (null, dirInfo.FullName);
873 fileNameComboBox.Text = null;
876 internalfullfilename = fileName;
881 string current_extension = Path.GetExtension (fileName);
882 if (current_extension.Length == 0) {
883 string filter_extension = string.Empty;
885 if (AddFilterExtension (internalfullfilename))
886 filter_extension = GetExtension (internalfullfilename);
888 if (filter_extension.Length == 0 && DefaultExt.Length > 0) {
889 filter_extension = "." + DefaultExt;
891 if (checkFileExists) {
892 // ignore DefaultExt if file not exist
893 if (!File.Exists (internalfullfilename + filter_extension))
894 filter_extension = string.Empty;
898 internalfullfilename += filter_extension;
902 if (checkFileExists) {
903 if (!File.Exists (internalfullfilename)) {
904 string message = "\"" + internalfullfilename + "\" doesn't exist. Please verify that you have entered the correct file name.";
905 MessageBox.Show (message, openSaveButton.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
910 if (fileDialogType == FileDialogType.SaveFileDialog) {
911 if (overwritePrompt) {
912 if (File.Exists (internalfullfilename)) {
913 string message = "\"" + internalfullfilename + "\" exists. Overwrite ?";
914 DialogResult dr = MessageBox.Show (message, openSaveButton.Text, MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
915 if (dr == DialogResult.Cancel)
921 if (!File.Exists (internalfullfilename)) {
922 string message = "\"" + internalfullfilename + "\" doesn't exist. Create ?";
923 DialogResult dr = MessageBox.Show (message, openSaveButton.Text, MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
924 if (dr == DialogResult.Cancel)
930 files.Add (internalfullfilename);
931 tokenizer.GetNextFile ();
934 if (files.Count > 0) {
935 fileNames = new string [files.Count];
936 for (int i = 0; i < files.Count; i++) {
937 string fileName = (string) files [i];
938 fileNames [i] = fileName;
939 mwfFileView.WriteRecentlyUsed (fileName);
941 if (!File.Exists (fileName))
942 // ignore files that do not exist
945 if (fileNameComboBox.Items.IndexOf (fileName) == -1)
946 fileNameComboBox.Items.Insert (0, fileName);
949 // remove items above the maximum items that we want to display
950 while (fileNameComboBox.Items.Count > MaxFileNameItems)
951 fileNameComboBox.Items.RemoveAt (MaxFileNameItems);
956 if (checkPathExists && mwfFileView.CurrentRealFolder != null) {
957 if (!Directory.Exists (mwfFileView.CurrentRealFolder)) {
958 string message = "\"" + mwfFileView.CurrentRealFolder + "\" doesn't exist. Please verify that you have entered the correct directory name.";
959 MessageBox.Show (message, openSaveButton.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
961 if (InitialDirectory.Length == 0 || !Directory.Exists (InitialDirectory))
962 mwfFileView.ChangeDirectory (null, lastFolder);
964 mwfFileView.ChangeDirectory (null, InitialDirectory);
969 if (restoreDirectory) {
970 lastFolder = restoreDirectoryString;
972 lastFolder = mwfFileView.CurrentFolder;
975 // update value of FilterIndex with user-selected filter
976 filterIndex = fileTypeComboBox.SelectedIndex + 1;
978 CancelEventArgs cancelEventArgs = new CancelEventArgs ();
980 cancelEventArgs.Cancel = false;
982 OnFileOk (cancelEventArgs);
984 form.DialogResult = DialogResult.OK;
987 bool AddFilterExtension (string fileName)
989 if (fileDialogType == FileDialogType.OpenFileDialog) {
990 if (DefaultExt.Length == 0)
993 if (checkFileExists) {
994 // if CheckFileExists is true, only add filter extension if
995 // file with DefaultExt does not exist
996 string fullFileName = fileName + "." + DefaultExt;
997 return !File.Exists (fullFileName);
999 // if CheckFileExists is false, only add filter extension
1000 // if specified file does not exist
1001 return !File.Exists (fileName);
1008 string GetExtension (string fileName)
1010 string filter_extension = String.Empty;
1012 if (fileFilter == null || fileTypeComboBox.SelectedIndex == -1)
1013 return filter_extension;
1015 FilterStruct filterstruct = (FilterStruct) fileFilter.FilterArrayList
1016 [fileTypeComboBox.SelectedIndex];
1018 for (int i = 0; i < filterstruct.filters.Count; i++) {
1019 string extension = filterstruct.filters [i];
1021 if (extension.StartsWith ("*"))
1022 extension = extension.Remove (0, 1);
1024 if (extension.IndexOf ('*') != -1)
1028 if (!supportMultiDottedExtensions) {
1030 int lastdot = extension.LastIndexOf('.');
1032 if (extension.LastIndexOf('.', lastdot - 1) != -1) {
1033 extension = extension.Remove(0, lastdot);
1040 if (!checkFileExists) {
1041 filter_extension = extension;
1045 if (fileDialogType == FileDialogType.SaveFileDialog) {
1046 // when DefaultExt is set, only consider first filter
1047 // extension (and do not check if file exists)
1048 if (DefaultExt.Length > 0) {
1049 filter_extension = extension;
1054 // MSDN: If the CheckFileExists property is true,
1055 // the dialog box adds the first extension from the
1056 // current file filter that matches an existing file
1057 string fullfilename = fileName + extension;
1058 if (File.Exists (fullfilename)) {
1059 filter_extension = extension;
1062 if (fileDialogType == FileDialogType.SaveFileDialog) {
1063 // when DefaultExt is set, only consider first filter
1065 if (DefaultExt.Length > 0) {
1066 filter_extension = extension;
1073 return filter_extension;
1076 void OnClickCancelButton (object sender, EventArgs e)
1078 if (restoreDirectory)
1079 mwfFileView.CurrentFolder = restoreDirectoryString;
1081 CancelEventArgs cancelEventArgs = new CancelEventArgs ();
1083 cancelEventArgs.Cancel = true;
1085 OnFileOk (cancelEventArgs);
1087 form.DialogResult = DialogResult.Cancel;
1090 void OnClickHelpButton (object sender, EventArgs e)
1095 void OnClickSmallButtonToolBar (object sender, ToolBarButtonClickEventArgs e)
1097 if (e.Button == upToolBarButton) {
1098 mwfFileView.OneDirUp ();
1100 if (e.Button == backToolBarButton) {
1101 mwfFileView.PopDir ();
1103 if (e.Button == newdirToolBarButton) {
1104 mwfFileView.CreateNewFolder ();
1108 void OnSelectedIndexChangedFileTypeComboBox (object sender, EventArgs e)
1110 if (do_not_call_OnSelectedIndexChangedFileTypeComboBox) {
1111 do_not_call_OnSelectedIndexChangedFileTypeComboBox = false;
1115 UpdateRecentFiles ();
1117 mwfFileView.FilterIndex = fileTypeComboBox.SelectedIndex + 1;
1120 void OnSelectedFileChangedFileView (object sender, EventArgs e)
1122 fileNameComboBox.Text = mwfFileView.CurrentFSEntry.Name;
1125 void OnSelectedFilesChangedFileView (object sender, EventArgs e)
1127 string selectedFiles = mwfFileView.SelectedFilesString;
1128 if (selectedFiles != null && selectedFiles.Length != 0)
1129 fileNameComboBox.Text = selectedFiles;
1132 void OnForceDialogEndFileView (object sender, EventArgs e)
1134 OnClickOpenSaveButton (this, EventArgs.Empty);
1137 void OnDirectoryChangedDirComboBox (object sender, EventArgs e)
1139 mwfFileView.ChangeDirectory (sender, dirComboBox.CurrentFolder);
1142 void OnDirectoryChangedPopupButtonPanel (object sender, EventArgs e)
1144 mwfFileView.ChangeDirectory (sender, popupButtonPanel.CurrentFolder);
1147 void OnCheckCheckChanged (object sender, EventArgs e)
1149 ReadOnlyChecked = readonlyCheckBox.Checked;
1153 void OnFileDialogFormClosed (object sender, FormClosedEventArgs e)
1155 HandleFormClosedEvent (sender);
1158 void OnFileDialogFormClosed (object sender, EventArgs e)
1160 HandleFormClosedEvent (sender);
1164 void HandleFormClosedEvent (object sender)
1166 if (!disable_form_closed_event)
1167 OnClickCancelButton (sender, EventArgs.Empty);
1169 disable_form_closed_event = false;
1172 private void UpdateFilters ()
1174 if (fileFilter == null)
1175 fileFilter = new FileFilter ();
1177 ArrayList filters = fileFilter.FilterArrayList;
1179 fileTypeComboBox.BeginUpdate ();
1181 fileTypeComboBox.Items.Clear ();
1183 foreach (FilterStruct fs in filters) {
1184 fileTypeComboBox.Items.Add (fs.filterName);
1187 fileTypeComboBox.EndUpdate ();
1189 mwfFileView.FilterArrayList = filters;
1192 private void UpdateRecentFiles ()
1194 fileNameComboBox.Items.Clear ();
1195 if (configFileNames != null) {
1196 foreach (string configFileName in configFileNames) {
1197 if (configFileName == null || configFileName.Trim ().Length == 0)
1199 // add no more than 10 items
1200 if (fileNameComboBox.Items.Count >= MaxFileNameItems)
1202 fileNameComboBox.Items.Add (configFileName);
1207 private void ResizeAndRelocateForHelpOrReadOnly ()
1209 form.SuspendLayout ();
1210 if (ShowHelp || ShowReadOnly) {
1211 mwfFileView.Size = new Size (449, 250);
1212 fileNameLabel.Location = new Point (102, 298);
1213 fileNameComboBox.Location = new Point (195, 298);
1214 fileTypeLabel.Location = new Point (102, 324);
1215 fileTypeComboBox.Location = new Point (195, 324);
1216 openSaveButton.Location = new Point (475, 298);
1217 cancelButton.Location = new Point (475, 324);
1219 mwfFileView.Size = new Size (449, 282);
1220 fileNameLabel.Location = new Point (102, 330);
1221 fileNameComboBox.Location = new Point (195, 330);
1222 fileTypeLabel.Location = new Point (102, 356);
1223 fileTypeComboBox.Location = new Point (195, 356);
1224 openSaveButton.Location = new Point (475, 330);
1225 cancelButton.Location = new Point (475, 356);
1229 form.Controls.Add (helpButton);
1231 form.Controls.Remove (helpButton);
1234 form.Controls.Add (readonlyCheckBox);
1236 form.Controls.Remove (readonlyCheckBox);
1237 form.ResumeLayout ();
1240 private void WriteConfigValues (CancelEventArgs ce)
1242 MWFConfig.SetValue (filedialog_string, width_string, form.Width);
1243 MWFConfig.SetValue (filedialog_string, height_string, form.Height);
1244 MWFConfig.SetValue (filedialog_string, x_string, form.Location.X);
1245 MWFConfig.SetValue (filedialog_string, y_string, form.Location.Y);
1248 MWFConfig.SetValue (filedialog_string, lastfolder_string, lastFolder);
1250 string[] fileNameCBItems = new string [fileNameComboBox.Items.Count];
1252 fileNameComboBox.Items.CopyTo (fileNameCBItems, 0);
1254 MWFConfig.SetValue (filedialog_string, filenames_string, fileNameCBItems);
1258 private void ReadConfigValues ()
1260 lastFolder = (string)MWFConfig.GetValue (filedialog_string, lastfolder_string);
1262 if (lastFolder != null && lastFolder.IndexOf ("://") == -1) {
1263 if (!Directory.Exists (lastFolder)) {
1264 lastFolder = MWFVFS.DesktopPrefix;
1268 if (InitialDirectory.Length > 0 && Directory.Exists (InitialDirectory))
1269 lastFolder = InitialDirectory;
1271 if (lastFolder == null || lastFolder.Length == 0)
1272 lastFolder = Environment.CurrentDirectory;
1274 if (RestoreDirectory)
1275 restoreDirectoryString = lastFolder;
1278 class FileNamesTokenizer
1280 public FileNamesTokenizer (string text, bool allowMultiple)
1284 _tokenType = TokenType.BOF;
1285 _allowMultiple = allowMultiple;
1288 public TokenType CurrentToken {
1289 get { return _tokenType; }
1292 public string TokenText {
1293 get { return _tokenText; }
1296 public bool AllowMultiple {
1297 get { return _allowMultiple; }
1300 private int ReadChar ()
1302 if (_position < _text.Length) {
1303 return _text [_position++];
1309 private int PeekChar ()
1311 if (_position < _text.Length) {
1312 return _text [_position];
1318 private void SkipWhitespaceAndQuotes ()
1322 while ((ch = PeekChar ()) != -1) {
1323 if ((char) ch != '"' && !char.IsWhiteSpace ((char) ch))
1329 public void GetNextFile ()
1331 if (_tokenType == TokenType.EOF)
1332 throw new Exception ("");
1336 SkipWhitespaceAndQuotes ();
1338 if (PeekChar () == -1) {
1339 _tokenType = TokenType.EOF;
1343 _tokenType = TokenType.FileName;
1344 StringBuilder sb = new StringBuilder ();
1346 while ((ch = PeekChar ()) != -1) {
1347 if ((char) ch == '"') {
1351 int pos = _position;
1353 SkipWhitespaceAndQuotes ();
1354 if (PeekChar () == -1) {
1358 sb.Append ((char) ch);
1360 sb.Append ((char) ReadChar ());
1364 _tokenText = sb.ToString ();
1367 private readonly bool _allowMultiple;
1368 private int _position;
1369 private readonly string _text;
1370 private TokenType _tokenType;
1371 private string _tokenText;
1374 internal enum TokenType
1383 #region PopupButtonPanel
1384 internal class PopupButtonPanel : Control, IUpdateFolder
1387 internal class PopupButton : Control
1389 internal enum PopupButtonState
1392 private Image image = null;
1393 private PopupButtonState popupButtonState = PopupButtonState.Normal;
1394 private StringFormat text_format = new StringFormat();
1395 private Rectangle text_rect = Rectangle.Empty;
1397 public PopupButton ()
1399 text_format.Alignment = StringAlignment.Center;
1400 text_format.LineAlignment = StringAlignment.Near;
1402 SetStyle (ControlStyles.DoubleBuffer, true);
1403 SetStyle (ControlStyles.AllPaintingInWmPaint, true);
1404 SetStyle (ControlStyles.UserPaint, true);
1405 SetStyle (ControlStyles.Selectable, false);
1408 public Image Image {
1419 public PopupButtonState ButtonState {
1421 popupButtonState = value;
1426 return popupButtonState;
1430 protected override void OnPaint (PaintEventArgs pe)
1437 private void Draw (PaintEventArgs pe)
1439 Graphics gr = pe.Graphics;
1441 gr.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (BackColor), ClientRectangle);
1444 if (image != null) {
1445 int i_x = (ClientSize.Width - image.Width) / 2;
1447 gr.DrawImage (image, i_x, i_y);
1450 if (Text != String.Empty) {
1451 if (text_rect == Rectangle.Empty)
1452 text_rect = new Rectangle (0, Height - 30, Width, Height - 30);
1454 gr.DrawString (Text, Font, Brushes.White, text_rect, text_format);
1457 switch (popupButtonState) {
1458 case PopupButtonState.Up:
1459 gr.DrawLine (ThemeEngine.Current.ResPool.GetPen (Color.White), 0, 0, ClientSize.Width - 1, 0);
1460 gr.DrawLine (ThemeEngine.Current.ResPool.GetPen (Color.White), 0, 0, 0, ClientSize.Height - 1);
1461 gr.DrawLine (ThemeEngine.Current.ResPool.GetPen (Color.Black), ClientSize.Width - 1, 0, ClientSize.Width - 1, ClientSize.Height - 1);
1462 gr.DrawLine (ThemeEngine.Current.ResPool.GetPen (Color.Black), 0, ClientSize.Height - 1, ClientSize.Width - 1, ClientSize.Height - 1);
1465 case PopupButtonState.Down:
1466 gr.DrawLine (ThemeEngine.Current.ResPool.GetPen (Color.Black), 0, 0, ClientSize.Width - 1, 0);
1467 gr.DrawLine (ThemeEngine.Current.ResPool.GetPen (Color.Black), 0, 0, 0, ClientSize.Height - 1);
1468 gr.DrawLine (ThemeEngine.Current.ResPool.GetPen (Color.White), ClientSize.Width - 1, 0, ClientSize.Width - 1, ClientSize.Height - 1);
1469 gr.DrawLine (ThemeEngine.Current.ResPool.GetPen (Color.White), 0, ClientSize.Height - 1, ClientSize.Width - 1, ClientSize.Height - 1);
1474 protected override void OnMouseEnter (EventArgs e)
1476 if (popupButtonState != PopupButtonState.Down)
1477 popupButtonState = PopupButtonState.Up;
1479 PopupButtonPanel panel = Parent as PopupButtonPanel;
1481 if (panel.focusButton != null && panel.focusButton.ButtonState == PopupButtonState.Up) {
1482 panel.focusButton.ButtonState = PopupButtonState.Normal;
1483 panel.focusButton = null;
1486 base.OnMouseEnter (e);
1489 protected override void OnMouseLeave (EventArgs e)
1491 if (popupButtonState == PopupButtonState.Up)
1492 popupButtonState = PopupButtonState.Normal;
1494 base.OnMouseLeave (e);
1497 protected override void OnClick (EventArgs e)
1499 popupButtonState = PopupButtonState.Down;
1506 private PopupButton recentlyusedButton;
1507 private PopupButton desktopButton;
1508 private PopupButton personalButton;
1509 private PopupButton mycomputerButton;
1510 private PopupButton networkButton;
1512 private PopupButton lastPopupButton = null;
1513 private PopupButton focusButton = null;
1515 private string currentPath;
1517 private int currentFocusIndex;
1519 public PopupButtonPanel ()
1523 BackColor = Color.FromArgb (128, 128, 128);
1524 Size = new Size (85, 336);
1526 recentlyusedButton = new PopupButton ();
1527 desktopButton = new PopupButton ();
1528 personalButton = new PopupButton ();
1529 mycomputerButton = new PopupButton ();
1530 networkButton = new PopupButton ();
1532 recentlyusedButton.Size = new Size (81, 64);
1533 recentlyusedButton.Image = ThemeEngine.Current.Images (UIIcon.PlacesRecentDocuments, 32);
1534 recentlyusedButton.BackColor = BackColor;
1535 recentlyusedButton.ForeColor = Color.Black;
1536 recentlyusedButton.Location = new Point (2, 2);
1537 recentlyusedButton.Text = "Recently\nused";
1538 recentlyusedButton.Click += new EventHandler (OnClickButton);
1540 desktopButton.Image = ThemeEngine.Current.Images (UIIcon.PlacesDesktop, 32);
1541 desktopButton.BackColor = BackColor;
1542 desktopButton.ForeColor = Color.Black;
1543 desktopButton.Size = new Size (81, 64);
1544 desktopButton.Location = new Point (2, 66);
1545 desktopButton.Text = "Desktop";
1546 desktopButton.Click += new EventHandler (OnClickButton);
1548 personalButton.Image = ThemeEngine.Current.Images (UIIcon.PlacesPersonal, 32);
1549 personalButton.BackColor = BackColor;
1550 personalButton.ForeColor = Color.Black;
1551 personalButton.Size = new Size (81, 64);
1552 personalButton.Location = new Point (2, 130);
1553 personalButton.Text = "Personal";
1554 personalButton.Click += new EventHandler (OnClickButton);
1556 mycomputerButton.Image = ThemeEngine.Current.Images (UIIcon.PlacesMyComputer, 32);
1557 mycomputerButton.BackColor = BackColor;
1558 mycomputerButton.ForeColor = Color.Black;
1559 mycomputerButton.Size = new Size (81, 64);
1560 mycomputerButton.Location = new Point (2, 194);
1561 mycomputerButton.Text = "My Computer";
1562 mycomputerButton.Click += new EventHandler (OnClickButton);
1564 networkButton.Image = ThemeEngine.Current.Images (UIIcon.PlacesMyNetwork, 32);
1565 networkButton.BackColor = BackColor;
1566 networkButton.ForeColor = Color.Black;
1567 networkButton.Size = new Size (81, 64);
1568 networkButton.Location = new Point (2, 258);
1569 networkButton.Text = "My Network";
1570 networkButton.Click += new EventHandler (OnClickButton);
1572 Controls.Add (recentlyusedButton);
1573 Controls.Add (desktopButton);
1574 Controls.Add (personalButton);
1575 Controls.Add (mycomputerButton);
1576 Controls.Add (networkButton);
1578 ResumeLayout (false);
1580 KeyDown += new KeyEventHandler (Key_Down);
1582 SetStyle (ControlStyles.StandardClick, false);
1585 void OnClickButton (object sender, EventArgs e)
1587 if (lastPopupButton != null && lastPopupButton != sender as PopupButton)
1588 lastPopupButton.ButtonState = PopupButton.PopupButtonState.Normal;
1589 lastPopupButton = sender as PopupButton;
1591 if (sender == recentlyusedButton) {
1592 currentPath = MWFVFS.RecentlyUsedPrefix;
1594 if (sender == desktopButton) {
1595 currentPath = MWFVFS.DesktopPrefix;
1597 if (sender == personalButton) {
1598 currentPath = MWFVFS.PersonalPrefix;
1600 if (sender == mycomputerButton) {
1601 currentPath = MWFVFS.MyComputerPrefix;
1603 if (sender == networkButton) {
1604 currentPath = MWFVFS.MyNetworkPrefix;
1607 EventHandler eh = (EventHandler)(Events [PDirectoryChangedEvent]);
1609 eh (this, EventArgs.Empty);
1612 public string CurrentFolder {
1614 string currentPath = value;
1615 if (currentPath == MWFVFS.RecentlyUsedPrefix) {
1616 if (lastPopupButton != recentlyusedButton) {
1617 if (lastPopupButton != null)
1618 lastPopupButton.ButtonState = PopupButton.PopupButtonState.Normal;
1619 recentlyusedButton.ButtonState = PopupButton.PopupButtonState.Down;
1620 lastPopupButton = recentlyusedButton;
1623 if (currentPath == MWFVFS.DesktopPrefix) {
1624 if (lastPopupButton != desktopButton) {
1625 if (lastPopupButton != null)
1626 lastPopupButton.ButtonState = PopupButton.PopupButtonState.Normal;
1627 desktopButton.ButtonState = PopupButton.PopupButtonState.Down;
1628 lastPopupButton = desktopButton;
1631 if (currentPath == MWFVFS.PersonalPrefix) {
1632 if (lastPopupButton != personalButton) {
1633 if (lastPopupButton != null)
1634 lastPopupButton.ButtonState = PopupButton.PopupButtonState.Normal;
1635 personalButton.ButtonState = PopupButton.PopupButtonState.Down;
1636 lastPopupButton = personalButton;
1639 if (currentPath == MWFVFS.MyComputerPrefix) {
1640 if (lastPopupButton != mycomputerButton) {
1641 if (lastPopupButton != null)
1642 lastPopupButton.ButtonState = PopupButton.PopupButtonState.Normal;
1643 mycomputerButton.ButtonState = PopupButton.PopupButtonState.Down;
1644 lastPopupButton = mycomputerButton;
1647 if (currentPath == MWFVFS.MyNetworkPrefix) {
1648 if (lastPopupButton != networkButton) {
1649 if (lastPopupButton != null)
1650 lastPopupButton.ButtonState = PopupButton.PopupButtonState.Normal;
1651 networkButton.ButtonState = PopupButton.PopupButtonState.Down;
1652 lastPopupButton = networkButton;
1655 if (lastPopupButton != null) {
1656 lastPopupButton.ButtonState = PopupButton.PopupButtonState.Normal;
1657 lastPopupButton = null;
1666 protected override void OnPaint (PaintEventArgs e)
1668 ControlPaint.DrawBorder3D (e.Graphics, ClientRectangle, Border3DStyle.Sunken);
1672 protected override void OnGotFocus (EventArgs e)
1674 if (lastPopupButton != recentlyusedButton) {
1675 recentlyusedButton.ButtonState = PopupButton.PopupButtonState.Up;
1676 focusButton = recentlyusedButton;
1678 currentFocusIndex = 0;
1680 base.OnGotFocus (e);
1683 protected override void OnLostFocus (EventArgs e)
1685 if (focusButton != null && focusButton.ButtonState != PopupButton.PopupButtonState.Down)
1686 focusButton.ButtonState = PopupButton.PopupButtonState.Normal;
1687 base.OnLostFocus (e);
1690 protected override bool IsInputKey (Keys key)
1700 return base.IsInputKey (key);
1703 private void Key_Down (object sender, KeyEventArgs e)
1705 bool update_focus = false;
1707 if (e.KeyCode == Keys.Left || e.KeyCode == Keys.Up) {
1708 currentFocusIndex --;
1710 if (currentFocusIndex < 0)
1711 currentFocusIndex = Controls.Count - 1;
1713 update_focus = true;
1715 if (e.KeyCode == Keys.Down || e.KeyCode == Keys.Right) {
1716 currentFocusIndex++;
1718 if (currentFocusIndex == Controls.Count)
1719 currentFocusIndex = 0;
1721 update_focus = true;
1723 if (e.KeyCode == Keys.Enter) {
1724 focusButton.ButtonState = PopupButton.PopupButtonState.Down;
1725 OnClickButton (focusButton, EventArgs.Empty);
1729 PopupButton newfocusButton = Controls [currentFocusIndex] as PopupButton;
1730 if (focusButton != null && focusButton.ButtonState != PopupButton.PopupButtonState.Down)
1731 focusButton.ButtonState = PopupButton.PopupButtonState.Normal;
1732 if (newfocusButton.ButtonState != PopupButton.PopupButtonState.Down)
1733 newfocusButton.ButtonState = PopupButton.PopupButtonState.Up;
1734 focusButton = newfocusButton;
1740 static object PDirectoryChangedEvent = new object ();
1742 public event EventHandler DirectoryChanged {
1743 add { Events.AddHandler (PDirectoryChangedEvent, value); }
1744 remove { Events.RemoveHandler (PDirectoryChangedEvent, value); }
1750 internal class DirComboBox : ComboBox, IUpdateFolder
1752 #region DirComboBoxItem
1753 internal class DirComboBoxItem
1755 private int imageIndex;
1756 private string name;
1757 private string path;
1759 private ImageList imageList;
1761 public DirComboBoxItem (ImageList imageList, int imageIndex, string name, string path, int xPos)
1763 this.imageList = imageList;
1764 this.imageIndex = imageIndex;
1770 public int ImageIndex {
1776 public string Name {
1782 public string Path {
1794 public ImageList ImageList {
1806 private ImageList imageList = new ImageList();
1808 private string currentPath;
1810 private Stack folderStack = new Stack();
1812 private static readonly int indent = 6;
1814 private DirComboBoxItem recentlyUsedDirComboboxItem;
1815 private DirComboBoxItem desktopDirComboboxItem;
1816 private DirComboBoxItem personalDirComboboxItem;
1817 private DirComboBoxItem myComputerDirComboboxItem;
1818 private DirComboBoxItem networkDirComboboxItem;
1820 private ArrayList myComputerItems = new ArrayList ();
1822 private DirComboBoxItem mainParentDirComboBoxItem = null;
1823 private DirComboBoxItem real_parent = null;
1827 public DirComboBox (MWFVFS vfs)
1833 DrawMode = DrawMode.OwnerDrawFixed;
1835 imageList.ColorDepth = ColorDepth.Depth32Bit;
1836 imageList.ImageSize = new Size (16, 16);
1837 imageList.Images.Add (ThemeEngine.Current.Images (UIIcon.PlacesRecentDocuments, 16));
1838 imageList.Images.Add (ThemeEngine.Current.Images (UIIcon.PlacesDesktop, 16));
1839 imageList.Images.Add (ThemeEngine.Current.Images (UIIcon.PlacesPersonal, 16));
1840 imageList.Images.Add (ThemeEngine.Current.Images (UIIcon.PlacesMyComputer, 16));
1841 imageList.Images.Add (ThemeEngine.Current.Images (UIIcon.PlacesMyNetwork, 16));
1842 imageList.Images.Add (ThemeEngine.Current.Images (UIIcon.NormalFolder, 16));
1843 imageList.TransparentColor = Color.Transparent;
1845 recentlyUsedDirComboboxItem = new DirComboBoxItem (imageList, 0, "Recently used", MWFVFS.RecentlyUsedPrefix, 0);
1846 desktopDirComboboxItem = new DirComboBoxItem (imageList, 1, "Desktop", MWFVFS.DesktopPrefix, 0);
1847 personalDirComboboxItem = new DirComboBoxItem (imageList, 2, "Personal folder", MWFVFS.PersonalPrefix, indent);
1848 myComputerDirComboboxItem = new DirComboBoxItem (imageList, 3, "My Computer", MWFVFS.MyComputerPrefix, indent);
1849 networkDirComboboxItem = new DirComboBoxItem (imageList, 4, "My Network", MWFVFS.MyNetworkPrefix, indent);
1851 ArrayList al = this.vfs.GetMyComputerContent ();
1853 foreach (FSEntry fsEntry in al) {
1854 myComputerItems.Add (new DirComboBoxItem (MimeIconEngine.LargeIcons, fsEntry.IconIndex, fsEntry.Name, fsEntry.FullName, indent * 2));
1860 mainParentDirComboBoxItem = myComputerDirComboboxItem;
1862 ResumeLayout (false);
1865 public string CurrentFolder {
1867 currentPath = value;
1876 private void CreateComboList ()
1879 DirComboBoxItem selection = null;
1881 if (currentPath == MWFVFS.RecentlyUsedPrefix) {
1882 mainParentDirComboBoxItem = recentlyUsedDirComboboxItem;
1883 selection = recentlyUsedDirComboboxItem;
1884 } else if (currentPath == MWFVFS.DesktopPrefix) {
1885 selection = desktopDirComboboxItem;
1886 mainParentDirComboBoxItem = desktopDirComboboxItem;
1887 } else if (currentPath == MWFVFS.PersonalPrefix) {
1888 selection = personalDirComboboxItem;
1889 mainParentDirComboBoxItem = personalDirComboboxItem;
1890 } else if (currentPath == MWFVFS.MyComputerPrefix) {
1891 selection = myComputerDirComboboxItem;
1892 mainParentDirComboBoxItem = myComputerDirComboboxItem;
1893 } else if (currentPath == MWFVFS.MyNetworkPrefix) {
1894 selection = networkDirComboboxItem;
1895 mainParentDirComboBoxItem = networkDirComboboxItem;
1897 foreach (DirComboBoxItem dci in myComputerItems) {
1898 if (dci.Path == currentPath) {
1899 mainParentDirComboBoxItem = selection = dci;
1909 Items.Add (recentlyUsedDirComboboxItem);
1910 Items.Add (desktopDirComboboxItem);
1911 Items.Add (personalDirComboboxItem);
1912 Items.Add (myComputerDirComboboxItem);
1913 Items.AddRange (myComputerItems);
1914 Items.Add (networkDirComboboxItem);
1916 if (selection == null)
1917 real_parent = CreateFolderStack ();
1919 if (real_parent != null) {
1920 int local_indent = 0;
1922 if (real_parent == desktopDirComboboxItem)
1925 if (real_parent == personalDirComboboxItem || real_parent == networkDirComboboxItem)
1930 selection = AppendToParent (local_indent, real_parent);
1935 if (selection != null)
1936 SelectedItem = selection;
1939 private DirComboBoxItem CreateFolderStack ()
1941 folderStack.Clear ();
1943 DirectoryInfo di = new DirectoryInfo (currentPath);
1945 folderStack.Push (di);
1947 bool ignoreCase = !XplatUI.RunningOnUnix;
1949 while (di.Parent != null) {
1952 if (mainParentDirComboBoxItem != personalDirComboboxItem && string.Compare (di.FullName, ThemeEngine.Current.Places (UIIcon.PlacesDesktop), ignoreCase) == 0)
1953 return desktopDirComboboxItem;
1955 if (mainParentDirComboBoxItem == personalDirComboboxItem) {
1956 if (string.Compare (di.FullName, ThemeEngine.Current.Places (UIIcon.PlacesPersonal), ignoreCase) == 0)
1957 return personalDirComboboxItem;
1959 foreach (DirComboBoxItem dci in myComputerItems) {
1960 if (string.Compare (dci.Path, di.FullName, ignoreCase) == 0) {
1965 folderStack.Push (di);
1971 private DirComboBoxItem AppendToParent (int nr_indents, DirComboBoxItem parentDirComboBoxItem)
1973 DirComboBoxItem selection = null;
1975 int index = Items.IndexOf (parentDirComboBoxItem) + 1;
1977 int xPos = indent * nr_indents;
1979 while (folderStack.Count != 0) {
1980 DirectoryInfo dii = folderStack.Pop () as DirectoryInfo;
1982 DirComboBoxItem dci = new DirComboBoxItem (imageList, 5, dii.Name, dii.FullName, xPos);
1984 Items.Insert (index, dci);
1993 protected override void OnDrawItem (DrawItemEventArgs e)
1998 DirComboBoxItem dcbi = Items [e.Index] as DirComboBoxItem;
2000 Bitmap bmp = new Bitmap (e.Bounds.Width, e.Bounds.Height, e.Graphics);
2001 Graphics gr = Graphics.FromImage (bmp);
2003 Color backColor = e.BackColor;
2004 Color foreColor = e.ForeColor;
2006 int xPos = dcbi.XPos;
2008 if ((e.State & DrawItemState.ComboBoxEdit) != 0)
2011 gr.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (backColor),
2012 new Rectangle (0, 0, bmp.Width, bmp.Height));
2014 if ((e.State & DrawItemState.Selected) == DrawItemState.Selected &&
2015 (!DroppedDown || (e.State & DrawItemState.ComboBoxEdit) != DrawItemState.ComboBoxEdit)) {
2016 foreColor = ThemeEngine.Current.ColorHighlightText;
2018 int w = (int) gr.MeasureString (dcbi.Name, e.Font).Width;
2020 gr.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (ThemeEngine.Current.ColorHighlight),
2021 new Rectangle (xPos + 23, 1, w + 3, e.Bounds.Height - 2));
2022 if ((e.State & DrawItemState.Focus) == DrawItemState.Focus) {
2023 ControlPaint.DrawFocusRectangle (gr, new Rectangle (xPos + 22, 0, w + 5,
2024 e.Bounds.Height), foreColor, ThemeEngine.Current.ColorHighlight);
2028 gr.DrawString (dcbi.Name, e.Font , ThemeEngine.Current.ResPool.GetSolidBrush (foreColor), new Point (24 + xPos, (bmp.Height - e.Font.Height) / 2));
2029 gr.DrawImage (dcbi.ImageList.Images [dcbi.ImageIndex], new Rectangle (new Point (xPos + 2, 0), new Size (16, 16)));
2031 e.Graphics.DrawImage (bmp, e.Bounds.X, e.Bounds.Y);
2036 protected override void OnSelectedIndexChanged (EventArgs e)
2038 if (Items.Count > 0) {
2039 DirComboBoxItem dcbi = Items [SelectedIndex] as DirComboBoxItem;
2041 currentPath = dcbi.Path;
2045 protected override void OnSelectionChangeCommitted (EventArgs e)
2047 EventHandler eh = (EventHandler)(Events [CDirectoryChangedEvent]);
2049 eh (this, EventArgs.Empty);
2052 static object CDirectoryChangedEvent = new object ();
2054 public event EventHandler DirectoryChanged {
2055 add { Events.AddHandler (CDirectoryChangedEvent, value); }
2056 remove { Events.RemoveHandler (CDirectoryChangedEvent, value); }
2061 #region FilterStruct
2062 internal struct FilterStruct
2064 public string filterName;
2065 public StringCollection filters;
2067 public FilterStruct (string filterName, string filter)
2069 this.filterName = filterName;
2071 filters = new StringCollection ();
2073 SplitFilters (filter);
2076 private void SplitFilters (string filter)
2078 string[] split = filter.Split (new char [] {';'});
2079 foreach (string s in split) {
2080 filters.Add (s.Trim ());
2087 internal class FileFilter
2089 private ArrayList filterArrayList = new ArrayList();
2091 private string filter;
2093 public FileFilter ()
2096 public FileFilter (string filter) : base ()
2098 this.filter = filter;
2103 public static bool CheckFilter (string val)
2105 if (val.Length == 0)
2108 string[] filters = val.Split (new char [] {'|'});
2110 if ((filters.Length % 2) != 0)
2116 public ArrayList FilterArrayList {
2118 filterArrayList = value;
2122 return filterArrayList;
2126 public string Filter {
2138 private void SplitFilter ()
2140 filterArrayList.Clear ();
2142 if (filter.Length == 0)
2145 string[] filters = filter.Split (new char [] {'|'});
2147 for (int i = 0; i < filters.Length; i += 2) {
2148 FilterStruct filterStruct = new FilterStruct (filters [i], filters [i + 1]);
2150 filterArrayList.Add (filterStruct);
2158 internal class MWFFileView : ListView
2160 private ArrayList filterArrayList;
2162 private bool showHiddenFiles = false;
2164 private string selectedFilesString;
2166 private int filterIndex = 1;
2168 private ToolTip toolTip;
2169 private int oldItemIndexForToolTip = -1;
2171 private ContextMenu contextMenu;
2173 private MenuItem menuItemView;
2174 private MenuItem menuItemNew;
2176 private MenuItem smallIconMenutItem;
2177 private MenuItem tilesMenutItem;
2178 private MenuItem largeIconMenutItem;
2179 private MenuItem listMenutItem;
2180 private MenuItem detailsMenutItem;
2182 private MenuItem newFolderMenuItem;
2183 private MenuItem showHiddenFilesMenuItem;
2185 private int previousCheckedMenuItemIndex;
2187 private ArrayList viewMenuItemClones = new ArrayList ();
2189 private FSEntry currentFSEntry = null;
2191 private string currentFolder;
2192 private string currentRealFolder;
2193 private FSEntry currentFolderFSEntry;
2195 // store DirectoryInfo for a back button for example
2196 private Stack directoryStack = new Stack();
2198 // list of controls(components to enable or disable depending on current directoryStack.Count
2199 private ArrayList dirStackControlsOrComponents = new ArrayList ();
2201 private ToolBarButton folderUpToolBarButton;
2203 private ArrayList registered_senders = new ArrayList ();
2205 private bool should_push = true;
2209 private View old_view;
2211 private int old_menuitem_index;
2212 private bool do_update_view = false;
2214 public MWFFileView (MWFVFS vfs)
2217 this.vfs.RegisterUpdateDelegate (new MWFVFS.UpdateDelegate (RealFileViewUpdate), this);
2221 contextMenu = new ContextMenu ();
2223 toolTip = new ToolTip ();
2224 toolTip.InitialDelay = 300;
2225 toolTip.ReshowDelay = 0;
2230 menuItemView = new MenuItem ("View");
2232 smallIconMenutItem = new MenuItem ("Small Icon", new EventHandler (OnClickViewMenuSubItem));
2233 smallIconMenutItem.RadioCheck = true;
2234 menuItemView.MenuItems.Add (smallIconMenutItem);
2236 tilesMenutItem = new MenuItem ("Tiles", new EventHandler (OnClickViewMenuSubItem));
2237 tilesMenutItem.RadioCheck = true;
2238 menuItemView.MenuItems.Add (tilesMenutItem);
2240 largeIconMenutItem = new MenuItem ("Large Icon", new EventHandler (OnClickViewMenuSubItem));
2241 largeIconMenutItem.RadioCheck = true;
2242 menuItemView.MenuItems.Add (largeIconMenutItem);
2244 listMenutItem = new MenuItem ("List", new EventHandler (OnClickViewMenuSubItem));
2245 listMenutItem.RadioCheck = true;
2246 listMenutItem.Checked = true;
2247 menuItemView.MenuItems.Add (listMenutItem);
2248 previousCheckedMenuItemIndex = listMenutItem.Index;
2250 detailsMenutItem = new MenuItem ("Details", new EventHandler (OnClickViewMenuSubItem));
2251 detailsMenutItem.RadioCheck = true;
2252 menuItemView.MenuItems.Add (detailsMenutItem);
2254 contextMenu.MenuItems.Add (menuItemView);
2256 contextMenu.MenuItems.Add (new MenuItem ("-"));
2259 menuItemNew = new MenuItem ("New");
2261 newFolderMenuItem = new MenuItem ("New Folder", new EventHandler (OnClickNewFolderMenuItem));
2262 menuItemNew.MenuItems.Add (newFolderMenuItem);
2264 contextMenu.MenuItems.Add (menuItemNew);
2266 contextMenu.MenuItems.Add (new MenuItem ("-"));
2268 // Show hidden files menu item
2269 showHiddenFilesMenuItem = new MenuItem ("Show hidden files", new EventHandler (OnClickContextMenu));
2270 showHiddenFilesMenuItem.Checked = showHiddenFiles;
2271 contextMenu.MenuItems.Add (showHiddenFilesMenuItem);
2275 SmallImageList = MimeIconEngine.SmallIcons;
2276 LargeImageList = MimeIconEngine.LargeIcons;
2278 View = old_view = View.List;
2281 ContextMenu = contextMenu;
2283 ResumeLayout (false);
2285 KeyDown += new KeyEventHandler (MWF_KeyDown);
2288 public string CurrentFolder {
2290 return currentFolder;
2293 currentFolder = value;
2297 public string CurrentRealFolder {
2299 return currentRealFolder;
2303 public FSEntry CurrentFSEntry {
2305 return currentFSEntry;
2309 public MenuItem[] ViewMenuItems {
2311 MenuItem[] menuItemClones = new MenuItem [] {
2312 smallIconMenutItem.CloneMenu (),
2313 tilesMenutItem.CloneMenu (),
2314 largeIconMenutItem.CloneMenu (),
2315 listMenutItem.CloneMenu (),
2316 detailsMenutItem.CloneMenu ()
2319 viewMenuItemClones.Add (menuItemClones);
2321 return menuItemClones;
2325 public ArrayList FilterArrayList {
2327 filterArrayList = value;
2331 return filterArrayList;
2335 public bool ShowHiddenFiles {
2337 showHiddenFiles = value;
2341 return showHiddenFiles;
2345 public int FilterIndex {
2347 filterIndex = value;
2357 public string SelectedFilesString {
2359 selectedFilesString = value;
2363 return selectedFilesString;
2367 public void PushDir ()
2369 if (currentFolder != null)
2370 directoryStack.Push (currentFolder);
2372 EnableOrDisableDirstackObjects ();
2375 public void PopDir ()
2377 if (directoryStack.Count == 0)
2380 string new_folder = directoryStack.Pop () as string;
2382 EnableOrDisableDirstackObjects ();
2384 should_push = false;
2386 ChangeDirectory (null, new_folder);
2389 public void RegisterSender (IUpdateFolder iud)
2391 registered_senders.Add (iud);
2394 public void CreateNewFolder ()
2396 if (currentFolder == MWFVFS.MyComputerPrefix ||
2397 currentFolder == MWFVFS.RecentlyUsedPrefix)
2400 FSEntry fsEntry = new FSEntry ();
2401 fsEntry.Attributes = FileAttributes.Directory;
2402 fsEntry.FileType = FSEntry.FSEntryType.Directory;
2403 fsEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("inode/directory");
2404 fsEntry.LastAccessTime = DateTime.Now;
2406 // FIXME: when ListView.LabelEdit is available use it
2407 // listViewItem.BeginEdit();
2409 TextEntryDialog ted = new TextEntryDialog ();
2410 ted.IconPictureBoxImage = MimeIconEngine.LargeIcons.Images.GetImage (fsEntry.IconIndex);
2412 string folder = String.Empty;
2414 if (currentFolderFSEntry.RealName != null)
2415 folder = currentFolderFSEntry.RealName;
2417 folder = currentFolder;
2419 string tmp_filename = "New Folder";
2421 if (Directory.Exists (Path.Combine (folder, tmp_filename))) {
2424 if (XplatUI.RunningOnUnix) {
2425 tmp_filename = tmp_filename + "-" + i;
2427 tmp_filename = tmp_filename + " (" + i + ")";
2430 while (Directory.Exists (Path.Combine (folder, tmp_filename))) {
2432 if (XplatUI.RunningOnUnix) {
2433 tmp_filename = "New Folder" + "-" + i;
2435 tmp_filename = "New Folder" + " (" + i + ")";
2440 ted.FileName = tmp_filename;
2442 if (ted.ShowDialog () == DialogResult.OK) {
2443 string new_folder = Path.Combine (folder, ted.FileName);
2445 if (vfs.CreateFolder (new_folder)) {
2446 fsEntry.FullName = new_folder;
2447 fsEntry.Name = ted.FileName;
2449 FileViewListViewItem listViewItem = new FileViewListViewItem (fsEntry);
2452 Items.Add (listViewItem);
2455 listViewItem.EnsureVisible ();
2460 public void SetSelectedIndexTo (string fname)
2462 foreach (FileViewListViewItem item in Items) {
2463 if (item.Text == fname) {
2465 SelectedItems.Clear ();
2466 item.Selected = true;
2473 public void OneDirUp ()
2475 string parent_folder = vfs.GetParent ();
2476 if (parent_folder != null)
2477 ChangeDirectory (null, parent_folder);
2480 public void ChangeDirectory (object sender, string folder)
2482 if (folder == MWFVFS.DesktopPrefix || folder == MWFVFS.RecentlyUsedPrefix)
2483 folderUpToolBarButton.Enabled = false;
2485 folderUpToolBarButton.Enabled = true;
2487 foreach (IUpdateFolder iuf in registered_senders) {
2488 iuf.CurrentFolder = folder;
2496 currentFolderFSEntry = vfs.ChangeDirectory (folder);
2498 currentFolder = folder;
2500 if (currentFolder.IndexOf ("://") != -1)
2501 currentRealFolder = currentFolderFSEntry.RealName;
2503 currentRealFolder = currentFolder;
2508 SelectedItems.Clear ();
2510 if (folder == MWFVFS.RecentlyUsedPrefix) {
2512 View = View.Details;
2513 old_menuitem_index = previousCheckedMenuItemIndex;
2514 UpdateMenuItems (detailsMenutItem);
2515 do_update_view = true;
2517 if (View != old_view && do_update_view) {
2518 UpdateMenuItems (menuItemView.MenuItems [old_menuitem_index]);
2520 do_update_view = false;
2526 } catch (Exception e) {
2529 MessageBox.Show (e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
2533 public void UpdateFileView ()
2535 if (filterArrayList != null && filterArrayList.Count != 0) {
2536 FilterStruct fs = (FilterStruct)filterArrayList [filterIndex - 1];
2538 vfs.GetFolderContent (fs.filters);
2540 vfs.GetFolderContent ();
2543 public void RealFileViewUpdate (ArrayList directoriesArrayList, ArrayList fileArrayList)
2548 SelectedItems.Clear ();
2550 foreach (FSEntry directoryFSEntry in directoriesArrayList) {
2551 if (!ShowHiddenFiles)
2552 if (directoryFSEntry.Name.StartsWith (".") || directoryFSEntry.Attributes == FileAttributes.Hidden)
2555 FileViewListViewItem listViewItem = new FileViewListViewItem (directoryFSEntry);
2557 Items.Add (listViewItem);
2560 StringCollection collection = new StringCollection ();
2562 foreach (FSEntry fsEntry in fileArrayList) {
2564 // remove duplicates. that can happen when you read recently used files for example
2565 if (collection.Contains (fsEntry.Name)) {
2567 string fileName = fsEntry.Name;
2569 if (collection.Contains (fileName)) {
2572 while (collection.Contains (fileName + "(" + i + ")")) {
2576 fileName = fileName + "(" + i + ")";
2579 fsEntry.Name = fileName;
2582 collection.Add (fsEntry.Name);
2584 DoOneFSEntry (fsEntry);
2589 collection.Clear ();
2592 directoriesArrayList.Clear ();
2593 fileArrayList.Clear ();
2596 public void AddControlToEnableDisableByDirStack (object control)
2598 dirStackControlsOrComponents.Add (control);
2601 public void SetFolderUpToolBarButton (ToolBarButton tb)
2603 folderUpToolBarButton = tb;
2606 public void WriteRecentlyUsed (string fullfilename)
2608 vfs.WriteRecentlyUsedFiles (fullfilename);
2611 private void EnableOrDisableDirstackObjects ()
2613 foreach (object o in dirStackControlsOrComponents) {
2615 Control c = o as Control;
2616 c.Enabled = (directoryStack.Count > 1);
2618 if (o is ToolBarButton) {
2619 ToolBarButton t = o as ToolBarButton;
2620 t.Enabled = (directoryStack.Count > 0);
2625 private void DoOneFSEntry (FSEntry fsEntry)
2627 if (!ShowHiddenFiles)
2628 if (fsEntry.Name.StartsWith (".") || fsEntry.Attributes == FileAttributes.Hidden)
2631 FileViewListViewItem listViewItem = new FileViewListViewItem (fsEntry);
2633 Items.Add (listViewItem);
2636 private void MWF_KeyDown (object sender, KeyEventArgs e)
2638 if (e.KeyCode == Keys.Back) {
2643 protected override void OnClick (EventArgs e)
2646 if (SelectedItems.Count > 0) {
2647 FileViewListViewItem listViewItem = SelectedItems [0] as FileViewListViewItem;
2649 FSEntry fsEntry = listViewItem.FSEntry;
2651 if (fsEntry.FileType == FSEntry.FSEntryType.File) {
2652 currentFSEntry = fsEntry;
2654 EventHandler eh = (EventHandler)(Events [MSelectedFileChangedEvent]);
2656 eh (this, EventArgs.Empty);
2664 protected override void OnDoubleClick (EventArgs e)
2666 if (SelectedItems.Count > 0) {
2667 FileViewListViewItem listViewItem = SelectedItems [0] as FileViewListViewItem;
2669 FSEntry fsEntry = listViewItem.FSEntry;
2671 if (fsEntry.Attributes == FileAttributes.Directory) {
2673 ChangeDirectory (null, fsEntry.FullName);
2675 EventHandler eh = (EventHandler)(Events [MDirectoryChangedEvent]);
2677 eh (this, EventArgs.Empty);
2679 currentFSEntry = fsEntry;
2681 EventHandler eh = (EventHandler)(Events [MSelectedFileChangedEvent]);
2683 eh (this, EventArgs.Empty);
2685 eh = (EventHandler)(Events [MForceDialogEndEvent]);
2687 eh (this, EventArgs.Empty);
2693 base.OnDoubleClick (e);
2696 protected override void OnSelectedIndexChanged (EventArgs e)
2698 if (SelectedItems.Count > 0) {
2699 selectedFilesString = String.Empty;
2701 if (SelectedItems.Count == 1) {
2702 FileViewListViewItem listViewItem = SelectedItems [0] as FileViewListViewItem;
2704 FSEntry fsEntry = listViewItem.FSEntry;
2706 if (fsEntry.Attributes != FileAttributes.Directory)
2707 selectedFilesString = SelectedItems [0].Text;
2709 foreach (FileViewListViewItem lvi in SelectedItems) {
2710 FSEntry fsEntry = lvi.FSEntry;
2712 if (fsEntry.Attributes != FileAttributes.Directory)
2713 selectedFilesString = selectedFilesString + "\"" + lvi.Text + "\" ";
2717 EventHandler eh = (EventHandler)(Events [MSelectedFilesChangedEvent]);
2719 eh (this, EventArgs.Empty);
2722 base.OnSelectedIndexChanged (e);
2725 protected override void OnMouseMove (MouseEventArgs e)
2727 FileViewListViewItem item = GetItemAt (e.X, e.Y) as FileViewListViewItem;
2730 int currentItemIndex = item.Index;
2732 if (currentItemIndex != oldItemIndexForToolTip) {
2733 oldItemIndexForToolTip = currentItemIndex;
2735 if (toolTip != null && toolTip.Active)
2736 toolTip.Active = false;
2738 FSEntry fsEntry = item.FSEntry;
2740 string output = String.Empty;
2742 if (fsEntry.FileType == FSEntry.FSEntryType.Directory)
2743 output = "Directory: " + fsEntry.FullName;
2744 else if (fsEntry.FileType == FSEntry.FSEntryType.Device)
2745 output = "Device: "+ fsEntry.FullName;
2746 else if (fsEntry.FileType == FSEntry.FSEntryType.Network)
2747 output = "Network: " + fsEntry.FullName;
2749 output = "File: " + fsEntry.FullName;
2751 toolTip.SetToolTip (this, output);
2753 toolTip.Active = true;
2756 toolTip.Active = false;
2758 base.OnMouseMove (e);
2761 void OnClickContextMenu (object sender, EventArgs e)
2763 MenuItem senderMenuItem = sender as MenuItem;
2765 if (senderMenuItem == showHiddenFilesMenuItem) {
2766 senderMenuItem.Checked = !senderMenuItem.Checked;
2767 showHiddenFiles = senderMenuItem.Checked;
2772 void OnClickViewMenuSubItem (object sender, EventArgs e)
2774 MenuItem senderMenuItem = (MenuItem)sender;
2776 UpdateMenuItems (senderMenuItem);
2780 switch (senderMenuItem.Index) {
2782 View = View.SmallIcon;
2785 View = View.LargeIcon;
2788 View = View.LargeIcon;
2794 View = View.Details;
2801 protected override void OnBeforeLabelEdit (LabelEditEventArgs e)
2803 FileViewListViewItem listViewItem = SelectedItems [0] as FileViewListViewItem;
2804 FSEntry fsEntry = listViewItem.FSEntry;
2806 // only allow editing of files or directories
2807 if (fsEntry.FileType != FSEntry.FSEntryType.Directory &&
2808 fsEntry.FileType != FSEntry.FSEntryType.File)
2809 e.CancelEdit = true;
2811 base.OnBeforeLabelEdit (e);
2814 protected override void OnAfterLabelEdit (LabelEditEventArgs e)
2816 base.OnAfterLabelEdit (e);
2818 // no changes were made
2819 if (e.Label == null || Items [e.Item].Text == e.Label)
2822 FileViewListViewItem listViewItem = SelectedItems [0] as FileViewListViewItem;
2823 FSEntry fsEntry = listViewItem.FSEntry;
2825 string folder = (currentFolderFSEntry.RealName != null) ?
2826 currentFolderFSEntry.RealName : currentFolder;
2828 switch (fsEntry.FileType) {
2829 case FSEntry.FSEntryType.Directory:
2830 string sourceDir = (fsEntry.RealName != null) ? fsEntry.RealName : fsEntry.FullName;
2831 string destDir = Path.Combine (folder, e.Label);
2832 if (!vfs.MoveFolder (sourceDir, destDir)) {
2833 e.CancelEdit = true;
2835 if (fsEntry.RealName != null)
2836 fsEntry.RealName = destDir;
2838 fsEntry.FullName = destDir;
2841 case FSEntry.FSEntryType.File:
2842 string sourceFile = (fsEntry.RealName != null) ? fsEntry.RealName : fsEntry.FullName;
2843 string destFile = Path.Combine (folder, e.Label);
2844 if (!vfs.MoveFile (sourceFile, destFile)) {
2845 e.CancelEdit = true;
2847 if (fsEntry.RealName != null)
2848 fsEntry.RealName = destFile;
2850 fsEntry.FullName = destFile;
2856 private void UpdateMenuItems (MenuItem senderMenuItem)
2858 menuItemView.MenuItems [previousCheckedMenuItemIndex].Checked = false;
2859 menuItemView.MenuItems [senderMenuItem.Index].Checked = true;
2861 foreach (MenuItem[] items in viewMenuItemClones) {
2862 items [previousCheckedMenuItemIndex].Checked = false;
2863 items [senderMenuItem.Index].Checked = true;
2866 previousCheckedMenuItemIndex = senderMenuItem.Index;
2869 void OnClickNewFolderMenuItem (object sender, EventArgs e)
2874 static object MSelectedFileChangedEvent = new object ();
2875 static object MSelectedFilesChangedEvent = new object ();
2876 static object MDirectoryChangedEvent = new object ();
2877 static object MForceDialogEndEvent = new object ();
2879 public event EventHandler SelectedFileChanged {
2880 add { Events.AddHandler (MSelectedFileChangedEvent, value); }
2881 remove { Events.RemoveHandler (MSelectedFileChangedEvent, value); }
2884 public event EventHandler SelectedFilesChanged {
2885 add { Events.AddHandler (MSelectedFilesChangedEvent, value); }
2886 remove { Events.RemoveHandler (MSelectedFilesChangedEvent, value); }
2889 public event EventHandler DirectoryChanged {
2890 add { Events.AddHandler (MDirectoryChangedEvent, value); }
2891 remove { Events.RemoveHandler (MDirectoryChangedEvent, value); }
2894 public event EventHandler ForceDialogEnd {
2895 add { Events.AddHandler (MForceDialogEndEvent, value); }
2896 remove { Events.RemoveHandler (MForceDialogEndEvent, value); }
2901 #region FileListViewItem
2902 internal class FileViewListViewItem : ListViewItem
2904 private FSEntry fsEntry;
2906 public FileViewListViewItem (FSEntry fsEntry)
2908 this.fsEntry = fsEntry;
2910 ImageIndex = fsEntry.IconIndex;
2912 Text = fsEntry.Name;
2914 switch (fsEntry.FileType) {
2915 case FSEntry.FSEntryType.Directory:
2916 SubItems.Add (String.Empty);
2917 SubItems.Add ("Directory");
2918 SubItems.Add (fsEntry.LastAccessTime.ToShortDateString () + " " + fsEntry.LastAccessTime.ToShortTimeString ());
2920 case FSEntry.FSEntryType.File:
2923 if (fsEntry.FileSize > 1024)
2924 fileLen = fsEntry.FileSize / 1024;
2925 } catch (Exception) {
2929 SubItems.Add (fileLen.ToString () + " KB");
2930 SubItems.Add ("File");
2931 SubItems.Add (fsEntry.LastAccessTime.ToShortDateString () + " " + fsEntry.LastAccessTime.ToShortTimeString ());
2933 case FSEntry.FSEntryType.Device:
2934 SubItems.Add (String.Empty);
2935 SubItems.Add ("Device");
2936 SubItems.Add (fsEntry.LastAccessTime.ToShortDateString () + " " + fsEntry.LastAccessTime.ToShortTimeString ());
2938 case FSEntry.FSEntryType.RemovableDevice:
2939 SubItems.Add (String.Empty);
2940 SubItems.Add ("RemovableDevice");
2941 SubItems.Add (fsEntry.LastAccessTime.ToShortDateString () + " " + fsEntry.LastAccessTime.ToShortTimeString ());
2948 public FSEntry FSEntry {
2960 #region IUpdateFolder
2961 internal interface IUpdateFolder
2963 string CurrentFolder {get; set;}
2967 #region TextEntryDialog
2968 // FIXME: When ListView.LabelEdit is implemented remove me
2969 internal class TextEntryDialog : Form
2971 private Label label1;
2972 private Button okButton;
2973 private TextBox newNameTextBox;
2974 private PictureBox iconPictureBox;
2975 private Button cancelButton;
2976 private GroupBox groupBox1;
2978 public TextEntryDialog ()
2980 groupBox1 = new GroupBox ();
2981 cancelButton = new Button ();
2982 iconPictureBox = new PictureBox ();
2983 newNameTextBox = new TextBox ();
2984 okButton = new Button ();
2985 label1 = new Label ();
2986 groupBox1.SuspendLayout ();
2990 groupBox1.Controls.Add (newNameTextBox);
2991 groupBox1.Controls.Add (label1);
2992 groupBox1.Controls.Add (iconPictureBox);
2993 groupBox1.Location = new Point (8, 8);
2994 groupBox1.Size = new Size (232, 160);
2995 groupBox1.TabIndex = 5;
2996 groupBox1.TabStop = false;
2997 groupBox1.Text = "New Name";
3000 cancelButton.DialogResult = DialogResult.Cancel;
3001 cancelButton.Location = new Point (168, 176);
3002 cancelButton.TabIndex = 4;
3003 cancelButton.Text = "Cancel";
3006 iconPictureBox.BorderStyle = BorderStyle.Fixed3D;
3007 iconPictureBox.Location = new Point (86, 24);
3008 iconPictureBox.Size = new Size (60, 60);
3009 iconPictureBox.TabIndex = 3;
3010 iconPictureBox.TabStop = false;
3011 iconPictureBox.SizeMode = PictureBoxSizeMode.CenterImage;
3014 newNameTextBox.Location = new Point (16, 128);
3015 newNameTextBox.Size = new Size (200, 20);
3016 newNameTextBox.TabIndex = 5;
3017 newNameTextBox.Text = String.Empty;
3020 okButton.DialogResult = DialogResult.OK;
3021 okButton.Location = new Point (80, 176);
3022 okButton.TabIndex = 3;
3023 okButton.Text = "OK";
3026 label1.Location = new Point (16, 96);
3027 label1.Size = new Size (200, 23);
3028 label1.TabIndex = 4;
3029 label1.Text = "Enter Name:";
3030 label1.TextAlign = ContentAlignment.MiddleCenter;
3033 AcceptButton = okButton;
3034 AutoScaleBaseSize = new Size (5, 13);
3035 CancelButton = cancelButton;
3036 ClientSize = new Size (248, 205);
3037 Controls.Add (groupBox1);
3038 Controls.Add (cancelButton);
3039 Controls.Add (okButton);
3040 FormBorderStyle = FormBorderStyle.FixedDialog;
3041 Text = "New Folder or File";
3042 groupBox1.ResumeLayout (false);
3043 ResumeLayout (false);
3045 newNameTextBox.Select ();
3048 public Image IconPictureBoxImage {
3050 iconPictureBox.Image = value;
3054 public string FileName {
3056 return newNameTextBox.Text;
3059 newNameTextBox.Text = value;
3066 internal class MWFVFS
3068 private FileSystem fileSystem;
3070 public static readonly string DesktopPrefix = "Desktop://";
3071 public static readonly string PersonalPrefix = "Personal://";
3072 public static readonly string MyComputerPrefix = "MyComputer://";
3073 public static readonly string RecentlyUsedPrefix = "RecentlyUsed://";
3074 public static readonly string MyNetworkPrefix = "MyNetwork://";
3075 public static readonly string MyComputerPersonalPrefix = "MyComputerPersonal://";
3077 public static Hashtable MyComputerDevicesPrefix = new Hashtable ();
3079 public delegate void UpdateDelegate (ArrayList folders, ArrayList files);
3080 private UpdateDelegate updateDelegate;
3081 private Control calling_control;
3083 private ThreadStart get_folder_content_thread_start;
3084 private Thread worker;
3085 private WorkerThread workerThread = null;
3086 private StringCollection the_filters;
3090 if (XplatUI.RunningOnUnix) {
3091 fileSystem = new UnixFileSystem ();
3093 fileSystem = new WinFileSystem ();
3097 public FSEntry ChangeDirectory (string folder)
3099 return fileSystem.ChangeDirectory (folder);
3102 public void GetFolderContent ()
3104 GetFolderContent (null);
3107 public void GetFolderContent (StringCollection filters)
3109 the_filters = filters;
3111 if (workerThread != null) {
3112 workerThread.Stop ();
3113 workerThread = null;
3116 // Added next line to ensure the control is created before BeginInvoke is called on it
3117 calling_control.CreateControl();
3118 workerThread = new WorkerThread (fileSystem, the_filters, updateDelegate, calling_control);
3120 get_folder_content_thread_start = new ThreadStart (workerThread.GetFolderContentThread);
3121 worker = new Thread (get_folder_content_thread_start);
3122 worker.IsBackground = true;
3126 internal class WorkerThread
3128 private FileSystem fileSystem;
3129 private StringCollection the_filters;
3130 private UpdateDelegate updateDelegate;
3131 private Control calling_control;
3132 private readonly object lockobject = new object ();
3133 private bool stopped = false;
3135 public WorkerThread (FileSystem fileSystem, StringCollection the_filters, UpdateDelegate updateDelegate, Control calling_control)
3137 this.fileSystem = fileSystem;
3138 this.the_filters = the_filters;
3139 this.updateDelegate = updateDelegate;
3140 this.calling_control = calling_control;
3143 public void GetFolderContentThread()
3148 fileSystem.GetFolderContent (the_filters, out folders, out files);
3153 if (updateDelegate != null) {
3155 object[] objectArray = new object[2];
3157 objectArray[0] = folders;
3158 objectArray[1] = files;
3160 calling_control.BeginInvoke (updateDelegate, objectArray);
3173 public ArrayList GetFoldersOnly ()
3175 return fileSystem.GetFoldersOnly ();
3178 public void WriteRecentlyUsedFiles (string filename)
3180 fileSystem.WriteRecentlyUsedFiles (filename);
3183 public ArrayList GetRecentlyUsedFiles ()
3185 return fileSystem.GetRecentlyUsedFiles ();
3188 public ArrayList GetMyComputerContent ()
3190 return fileSystem.GetMyComputerContent ();
3193 public ArrayList GetMyNetworkContent ()
3195 return fileSystem.GetMyNetworkContent ();
3198 public bool CreateFolder (string new_folder)
3201 if (Directory.Exists (new_folder)) {
3202 string message = "Folder \"" + new_folder + "\" already exists.";
3203 MessageBox.Show (message, new_folder, MessageBoxButtons.OK,
3204 MessageBoxIcon.Error);
3207 Directory.CreateDirectory (new_folder);
3208 } catch (Exception e) {
3209 MessageBox.Show (e.Message, new_folder, MessageBoxButtons.OK, MessageBoxIcon.Error);
3216 public bool MoveFolder (string sourceDirName, string destDirName)
3219 if (Directory.Exists (destDirName)) {
3220 string message = "Cannot rename " + Path.GetFileName (sourceDirName)
3221 + ": A folder with the name you specified already exists."
3222 + " Specify a different folder name.";
3223 MessageBox.Show (message, "Error Renaming Folder", MessageBoxButtons.OK,
3224 MessageBoxIcon.Error);
3227 Directory.Move (sourceDirName, destDirName);
3228 } catch (Exception e) {
3229 MessageBox.Show (e.Message, "Error Renaming Folder",
3230 MessageBoxButtons.OK, MessageBoxIcon.Error);
3237 public bool MoveFile (string sourceFileName, string destFileName)
3240 if (File.Exists (destFileName)) {
3241 string message = "Cannot rename " + Path.GetFileName (sourceFileName)
3242 + ": A file with the name you specified already exists."
3243 + " Specify a different file name.";
3244 MessageBox.Show (message, "Error Renaming File",
3245 MessageBoxButtons.OK, MessageBoxIcon.Error);
3248 File.Move (sourceFileName, destFileName);
3249 } catch (Exception e) {
3250 MessageBox.Show (e.Message, "Error Renaming Folder",
3251 MessageBoxButtons.OK, MessageBoxIcon.Error);
3258 public string GetParent ()
3260 return fileSystem.GetParent ();
3263 public void RegisterUpdateDelegate(UpdateDelegate updateDelegate, Control control)
3265 this.updateDelegate = updateDelegate;
3266 calling_control = control;
3272 internal abstract class FileSystem
3274 protected string currentTopFolder = String.Empty;
3275 protected FSEntry currentFolderFSEntry = null;
3276 protected FSEntry currentTopFolderFSEntry = null;
3277 private FileInfoComparer fileInfoComparer = new FileInfoComparer ();
3279 public FSEntry ChangeDirectory (string folder)
3281 if (folder == MWFVFS.DesktopPrefix) {
3282 currentTopFolder = MWFVFS.DesktopPrefix;
3283 currentTopFolderFSEntry = currentFolderFSEntry = GetDesktopFSEntry ();
3285 if (folder == MWFVFS.PersonalPrefix) {
3286 currentTopFolder = MWFVFS.PersonalPrefix;
3287 currentTopFolderFSEntry = currentFolderFSEntry = GetPersonalFSEntry ();
3289 if (folder == MWFVFS.MyComputerPersonalPrefix) {
3290 currentTopFolder = MWFVFS.MyComputerPersonalPrefix;
3291 currentTopFolderFSEntry = currentFolderFSEntry = GetMyComputerPersonalFSEntry ();
3293 if (folder == MWFVFS.RecentlyUsedPrefix) {
3294 currentTopFolder = MWFVFS.RecentlyUsedPrefix;
3295 currentTopFolderFSEntry = currentFolderFSEntry = GetRecentlyUsedFSEntry ();
3297 if (folder == MWFVFS.MyComputerPrefix) {
3298 currentTopFolder = MWFVFS.MyComputerPrefix;
3299 currentTopFolderFSEntry = currentFolderFSEntry = GetMyComputerFSEntry ();
3301 if (folder == MWFVFS.MyNetworkPrefix) {
3302 currentTopFolder = MWFVFS.MyNetworkPrefix;
3303 currentTopFolderFSEntry = currentFolderFSEntry = GetMyNetworkFSEntry ();
3307 foreach (DictionaryEntry entry in MWFVFS.MyComputerDevicesPrefix) {
3308 FSEntry fsEntry = entry.Value as FSEntry;
3309 if (folder == fsEntry.FullName) {
3310 currentTopFolder = entry.Key as string;
3311 currentTopFolderFSEntry = currentFolderFSEntry = fsEntry;
3318 currentFolderFSEntry = GetDirectoryFSEntry (new DirectoryInfo (folder), currentTopFolderFSEntry);
3322 return currentFolderFSEntry;
3325 public string GetParent ()
3327 return currentFolderFSEntry.Parent;
3330 // directories_out and files_out contain FSEntry objects
3331 public void GetFolderContent (StringCollection filters, out ArrayList directories_out, out ArrayList files_out)
3333 directories_out = new ArrayList ();
3334 files_out = new ArrayList ();
3336 if (currentFolderFSEntry.FullName == MWFVFS.DesktopPrefix) {
3337 FSEntry personalFSEntry = GetPersonalFSEntry ();
3339 directories_out.Add (personalFSEntry);
3341 FSEntry myComputerFSEntry = GetMyComputerFSEntry ();
3343 directories_out.Add (myComputerFSEntry);
3345 FSEntry myNetworkFSEntry = GetMyNetworkFSEntry ();
3347 directories_out.Add (myNetworkFSEntry);
3349 ArrayList d_out = null;
3350 ArrayList f_out = null;
3351 GetNormalFolderContent (ThemeEngine.Current.Places (UIIcon.PlacesDesktop), filters, out d_out, out f_out);
3352 directories_out.AddRange (d_out);
3353 files_out.AddRange (f_out);
3356 if (currentFolderFSEntry.FullName == MWFVFS.RecentlyUsedPrefix) {
3357 files_out = GetRecentlyUsedFiles ();
3359 if (currentFolderFSEntry.FullName == MWFVFS.MyComputerPrefix) {
3360 directories_out.AddRange (GetMyComputerContent ());
3362 if (currentFolderFSEntry.FullName == MWFVFS.PersonalPrefix || currentFolderFSEntry.FullName == MWFVFS.MyComputerPersonalPrefix) {
3363 ArrayList d_out = null;
3364 ArrayList f_out = null;
3365 GetNormalFolderContent (ThemeEngine.Current.Places (UIIcon.PlacesPersonal), filters, out d_out, out f_out);
3366 directories_out.AddRange (d_out);
3367 files_out.AddRange (f_out);
3369 if (currentFolderFSEntry.FullName == MWFVFS.MyNetworkPrefix) {
3370 directories_out.AddRange (GetMyNetworkContent ());
3372 GetNormalFolderContent (currentFolderFSEntry.FullName, filters, out directories_out, out files_out);
3376 public ArrayList GetFoldersOnly ()
3378 ArrayList directories_out = new ArrayList ();
3380 if (currentFolderFSEntry.FullName == MWFVFS.DesktopPrefix) {
3381 FSEntry personalFSEntry = GetPersonalFSEntry ();
3383 directories_out.Add (personalFSEntry);
3385 FSEntry myComputerFSEntry = GetMyComputerFSEntry ();
3387 directories_out.Add (myComputerFSEntry);
3389 FSEntry myNetworkFSEntry = GetMyNetworkFSEntry ();
3391 directories_out.Add (myNetworkFSEntry);
3393 ArrayList d_out = GetNormalFolders (ThemeEngine.Current.Places (UIIcon.PlacesDesktop));
3394 directories_out.AddRange (d_out);
3397 if (currentFolderFSEntry.FullName == MWFVFS.RecentlyUsedPrefix) {
3398 //files_out = GetRecentlyUsedFiles ();
3400 if (currentFolderFSEntry.FullName == MWFVFS.MyComputerPrefix) {
3401 directories_out.AddRange (GetMyComputerContent ());
3403 if (currentFolderFSEntry.FullName == MWFVFS.PersonalPrefix || currentFolderFSEntry.FullName == MWFVFS.MyComputerPersonalPrefix) {
3404 ArrayList d_out = GetNormalFolders (ThemeEngine.Current.Places (UIIcon.PlacesPersonal));
3405 directories_out.AddRange (d_out);
3407 if (currentFolderFSEntry.FullName == MWFVFS.MyNetworkPrefix) {
3408 directories_out.AddRange (GetMyNetworkContent ());
3410 directories_out = GetNormalFolders (currentFolderFSEntry.FullName);
3412 return directories_out;
3415 protected void GetNormalFolderContent (string from_folder, StringCollection filters, out ArrayList directories_out, out ArrayList files_out)
3417 DirectoryInfo dirinfo = new DirectoryInfo (from_folder);
3419 directories_out = new ArrayList ();
3421 DirectoryInfo[] dirs = null;
3424 dirs = dirinfo.GetDirectories ();
3425 } catch (Exception) {}
3428 for (int i = 0; i < dirs.Length; i++) {
3429 directories_out.Add (GetDirectoryFSEntry (dirs [i], currentTopFolderFSEntry));
3432 files_out = new ArrayList ();
3434 ArrayList files = new ArrayList ();
3437 if (filters == null) {
3438 files.AddRange (dirinfo.GetFiles ());
3440 foreach (string s in filters)
3441 files.AddRange (dirinfo.GetFiles (s));
3443 files.Sort (fileInfoComparer);
3445 } catch (Exception) {}
3447 for (int i = 0; i < files.Count; i++) {
3448 FSEntry fs = GetFileFSEntry (files [i] as FileInfo);
3454 protected ArrayList GetNormalFolders (string from_folder)
3456 DirectoryInfo dirinfo = new DirectoryInfo (from_folder);
3458 ArrayList directories_out = new ArrayList ();
3460 DirectoryInfo[] dirs = null;
3463 dirs = dirinfo.GetDirectories ();
3464 } catch (Exception) {}
3467 for (int i = 0; i < dirs.Length; i++) {
3468 directories_out.Add (GetDirectoryFSEntry (dirs [i], currentTopFolderFSEntry));
3471 return directories_out;
3474 protected virtual FSEntry GetDirectoryFSEntry (DirectoryInfo dirinfo, FSEntry topFolderFSEntry)
3476 FSEntry fs = new FSEntry ();
3478 fs.Attributes = dirinfo.Attributes;
3479 fs.FullName = dirinfo.FullName;
3480 fs.Name = dirinfo.Name;
3481 fs.MainTopNode = topFolderFSEntry;
3482 fs.FileType = FSEntry.FSEntryType.Directory;
3483 fs.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("inode/directory");
3484 fs.LastAccessTime = dirinfo.LastAccessTime;
3489 protected virtual FSEntry GetFileFSEntry (FileInfo fileinfo)
3491 // *sigh* FileInfo gives us no usable information for links to directories
3493 if ((fileinfo.Attributes & FileAttributes.Directory) == FileAttributes.Directory)
3496 FSEntry fs = new FSEntry ();
3498 fs.Attributes = fileinfo.Attributes;
3499 fs.FullName = fileinfo.FullName;
3500 fs.Name = fileinfo.Name;
3501 fs.FileType = FSEntry.FSEntryType.File;
3502 fs.IconIndex = MimeIconEngine.GetIconIndexForFile (fileinfo.FullName);
3503 fs.FileSize = fileinfo.Length;
3504 fs.LastAccessTime = fileinfo.LastAccessTime;
3509 internal class FileInfoComparer : IComparer
3511 public int Compare (object fileInfo1, object fileInfo2)
3513 return String.Compare (((FileInfo)fileInfo1).Name, ((FileInfo)fileInfo2).Name);
3517 protected abstract FSEntry GetDesktopFSEntry ();
3519 protected abstract FSEntry GetRecentlyUsedFSEntry ();
3521 protected abstract FSEntry GetPersonalFSEntry ();
3523 protected abstract FSEntry GetMyComputerPersonalFSEntry ();
3525 protected abstract FSEntry GetMyComputerFSEntry ();
3527 protected abstract FSEntry GetMyNetworkFSEntry ();
3529 public abstract void WriteRecentlyUsedFiles (string fileToAdd);
3531 public abstract ArrayList GetRecentlyUsedFiles ();
3533 public abstract ArrayList GetMyComputerContent ();
3535 public abstract ArrayList GetMyNetworkContent ();
3539 #region UnixFileSystem
3540 internal class UnixFileSystem : FileSystem
3542 private MasterMount masterMount = new MasterMount ();
3543 private FSEntry desktopFSEntry = null;
3544 private FSEntry recentlyusedFSEntry = null;
3545 private FSEntry personalFSEntry = null;
3546 private FSEntry mycomputerpersonalFSEntry = null;
3547 private FSEntry mycomputerFSEntry = null;
3548 private FSEntry mynetworkFSEntry = null;
3550 private string personal_folder;
3551 private string recently_used_path;
3552 private string full_kde_recent_document_dir;
3554 public UnixFileSystem ()
3556 personal_folder = ThemeEngine.Current.Places (UIIcon.PlacesPersonal);
3557 recently_used_path = Path.Combine (personal_folder, ".recently-used");
3559 full_kde_recent_document_dir = personal_folder + "/.kde/share/apps/RecentDocuments";
3561 desktopFSEntry = new FSEntry ();
3563 desktopFSEntry.Attributes = FileAttributes.Directory;
3564 desktopFSEntry.FullName = MWFVFS.DesktopPrefix;
3565 desktopFSEntry.Name = "Desktop";
3566 desktopFSEntry.RealName = ThemeEngine.Current.Places (UIIcon.PlacesDesktop);
3567 desktopFSEntry.FileType = FSEntry.FSEntryType.Directory;
3568 desktopFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("desktop/desktop");
3569 desktopFSEntry.LastAccessTime = DateTime.Now;
3571 recentlyusedFSEntry = new FSEntry ();
3573 recentlyusedFSEntry.Attributes = FileAttributes.Directory;
3574 recentlyusedFSEntry.FullName = MWFVFS.RecentlyUsedPrefix;
3575 recentlyusedFSEntry.Name = "Recently Used";
3576 recentlyusedFSEntry.FileType = FSEntry.FSEntryType.Directory;
3577 recentlyusedFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("recently/recently");
3578 recentlyusedFSEntry.LastAccessTime = DateTime.Now;
3580 personalFSEntry = new FSEntry ();
3582 personalFSEntry.Attributes = FileAttributes.Directory;
3583 personalFSEntry.FullName = MWFVFS.PersonalPrefix;
3584 personalFSEntry.Name = "Personal";
3585 personalFSEntry.MainTopNode = GetDesktopFSEntry ();
3586 personalFSEntry.RealName = ThemeEngine.Current.Places (UIIcon.PlacesPersonal);
3587 personalFSEntry.FileType = FSEntry.FSEntryType.Directory;
3588 personalFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("directory/home");
3589 personalFSEntry.LastAccessTime = DateTime.Now;
3591 mycomputerpersonalFSEntry = new FSEntry ();
3593 mycomputerpersonalFSEntry.Attributes = FileAttributes.Directory;
3594 mycomputerpersonalFSEntry.FullName = MWFVFS.MyComputerPersonalPrefix;
3595 mycomputerpersonalFSEntry.Name = "Personal";
3596 mycomputerpersonalFSEntry.MainTopNode = GetMyComputerFSEntry ();
3597 mycomputerpersonalFSEntry.RealName = ThemeEngine.Current.Places (UIIcon.PlacesPersonal);
3598 mycomputerpersonalFSEntry.FileType = FSEntry.FSEntryType.Directory;
3599 mycomputerpersonalFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("directory/home");
3600 mycomputerpersonalFSEntry.LastAccessTime = DateTime.Now;
3602 mycomputerFSEntry = new FSEntry ();
3604 mycomputerFSEntry.Attributes = FileAttributes.Directory;
3605 mycomputerFSEntry.FullName = MWFVFS.MyComputerPrefix;
3606 mycomputerFSEntry.Name = "My Computer";
3607 mycomputerFSEntry.MainTopNode = GetDesktopFSEntry ();
3608 mycomputerFSEntry.FileType = FSEntry.FSEntryType.Directory;
3609 mycomputerFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("workplace/workplace");
3610 mycomputerFSEntry.LastAccessTime = DateTime.Now;
3612 mynetworkFSEntry = new FSEntry ();
3614 mynetworkFSEntry.Attributes = FileAttributes.Directory;
3615 mynetworkFSEntry.FullName = MWFVFS.MyNetworkPrefix;
3616 mynetworkFSEntry.Name = "My Network";
3617 mynetworkFSEntry.MainTopNode = GetDesktopFSEntry ();
3618 mynetworkFSEntry.FileType = FSEntry.FSEntryType.Directory;
3619 mynetworkFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("network/network");
3620 mynetworkFSEntry.LastAccessTime = DateTime.Now;
3623 public override void WriteRecentlyUsedFiles (string fileToAdd)
3625 if (File.Exists (recently_used_path) && new FileInfo (recently_used_path).Length > 0) {
3626 XmlDocument xml_doc = new XmlDocument ();
3627 xml_doc.Load (recently_used_path);
3629 XmlNode grand_parent_node = xml_doc.SelectSingleNode ("RecentFiles");
3631 if (grand_parent_node != null) {
3632 // create a new element
3633 XmlElement new_recent_item_node = xml_doc.CreateElement ("RecentItem");
3635 XmlElement new_child = xml_doc.CreateElement ("URI");
3636 UriBuilder ub = new UriBuilder ();
3637 ub.Path = fileToAdd;
3640 XmlText new_text_child = xml_doc.CreateTextNode (ub.ToString ());
3641 new_child.AppendChild (new_text_child);
3643 new_recent_item_node.AppendChild (new_child);
3645 new_child = xml_doc.CreateElement ("Mime-Type");
3646 new_text_child = xml_doc.CreateTextNode (Mime.GetMimeTypeForFile (fileToAdd));
3647 new_child.AppendChild (new_text_child);
3649 new_recent_item_node.AppendChild (new_child);
3651 new_child = xml_doc.CreateElement ("Timestamp");
3652 long seconds = (long)(DateTime.UtcNow - new DateTime (1970, 1, 1)).TotalSeconds;
3653 new_text_child = xml_doc.CreateTextNode (seconds.ToString ());
3654 new_child.AppendChild (new_text_child);
3656 new_recent_item_node.AppendChild (new_child);
3658 new_child = xml_doc.CreateElement ("Groups");
3660 new_recent_item_node.AppendChild (new_child);
3662 // now search the nodes in grand_parent_node for another instance of the new uri and if found remove it
3663 // so that the new node is the first one
3664 foreach (XmlNode n in grand_parent_node.ChildNodes) {
3665 XmlNode uri_node = n.SelectSingleNode ("URI");
3666 if (uri_node != null) {
3667 XmlNode uri_node_child = uri_node.FirstChild;
3668 if (uri_node_child is XmlText)
3669 if (ub.ToString () == ((XmlText)uri_node_child).Data) {
3670 grand_parent_node.RemoveChild (n);
3676 // prepend the new recent item to the grand parent node list
3677 grand_parent_node.PrependChild (new_recent_item_node);
3679 // limit the # of RecentItems to 10
3680 if (grand_parent_node.ChildNodes.Count > 10) {
3681 while (grand_parent_node.ChildNodes.Count > 10)
3682 grand_parent_node.RemoveChild (grand_parent_node.LastChild);
3686 xml_doc.Save (recently_used_path);
3687 } catch (Exception) {
3691 XmlDocument xml_doc = new XmlDocument ();
3692 xml_doc.AppendChild (xml_doc.CreateXmlDeclaration ("1.0", String.Empty, String.Empty));
3694 XmlElement recentFiles_element = xml_doc.CreateElement ("RecentFiles");
3696 XmlElement new_recent_item_node = xml_doc.CreateElement ("RecentItem");
3698 XmlElement new_child = xml_doc.CreateElement ("URI");
3699 UriBuilder ub = new UriBuilder ();
3700 ub.Path = fileToAdd;
3703 XmlText new_text_child = xml_doc.CreateTextNode (ub.ToString ());
3704 new_child.AppendChild (new_text_child);
3706 new_recent_item_node.AppendChild (new_child);
3708 new_child = xml_doc.CreateElement ("Mime-Type");
3709 new_text_child = xml_doc.CreateTextNode (Mime.GetMimeTypeForFile (fileToAdd));
3710 new_child.AppendChild (new_text_child);
3712 new_recent_item_node.AppendChild (new_child);
3714 new_child = xml_doc.CreateElement ("Timestamp");
3715 long seconds = (long)(DateTime.UtcNow - new DateTime (1970, 1, 1)).TotalSeconds;
3716 new_text_child = xml_doc.CreateTextNode (seconds.ToString ());
3717 new_child.AppendChild (new_text_child);
3719 new_recent_item_node.AppendChild (new_child);
3721 new_child = xml_doc.CreateElement ("Groups");
3723 new_recent_item_node.AppendChild (new_child);
3725 recentFiles_element.AppendChild (new_recent_item_node);
3727 xml_doc.AppendChild (recentFiles_element);
3730 xml_doc.Save (recently_used_path);
3731 } catch (Exception) {
3736 // return an ArrayList with FSEntry objects
3737 public override ArrayList GetRecentlyUsedFiles ()
3739 // check for GNOME and KDE
3741 ArrayList files_al = new ArrayList ();
3744 if (File.Exists (recently_used_path)) {
3746 XmlTextReader xtr = new XmlTextReader (recently_used_path);
3747 while (xtr.Read ()) {
3748 if (xtr.NodeType == XmlNodeType.Element && xtr.Name.ToUpper () == "URI") {
3750 Uri uri = new Uri (xtr.Value);
3751 if (!files_al.Contains (uri.LocalPath))
3752 if (File.Exists (uri.LocalPath)) {
3753 FSEntry fs = GetFileFSEntry (new FileInfo (uri.LocalPath));
3760 } catch (Exception) {
3766 if (Directory.Exists (full_kde_recent_document_dir)) {
3767 string[] files = Directory.GetFiles (full_kde_recent_document_dir, "*.desktop");
3769 foreach (string file_name in files) {
3770 StreamReader sr = new StreamReader (file_name);
3772 string line = sr.ReadLine ();
3774 while (line != null) {
3775 line = line.Trim ();
3777 if (line.StartsWith ("URL=")) {
3778 line = line.Replace ("URL=", String.Empty);
3779 line = line.Replace ("$HOME", personal_folder);
3781 Uri uri = new Uri (line);
3782 if (!files_al.Contains (uri.LocalPath))
3783 if (File.Exists (uri.LocalPath)) {
3784 FSEntry fs = GetFileFSEntry (new FileInfo (uri.LocalPath));
3791 line = sr.ReadLine ();
3802 // return an ArrayList with FSEntry objects
3803 public override ArrayList GetMyComputerContent ()
3805 ArrayList my_computer_content_arraylist = new ArrayList ();
3807 if (masterMount.ProcMountAvailable) {
3808 masterMount.GetMounts ();
3810 foreach (MasterMount.Mount mount in masterMount.Block_devices) {
3811 FSEntry fsEntry = new FSEntry ();
3812 fsEntry.FileType = FSEntry.FSEntryType.Device;
3814 fsEntry.FullName = mount.mount_point;
3816 fsEntry.Name = "HDD (" + mount.fsType + ", " + mount.device_short + ")";
3818 fsEntry.FsType = mount.fsType;
3819 fsEntry.DeviceShort = mount.device_short;
3821 fsEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("harddisk/harddisk");
3823 fsEntry.Attributes = FileAttributes.Directory;
3825 fsEntry.MainTopNode = GetMyComputerFSEntry ();
3827 my_computer_content_arraylist.Add (fsEntry);
3829 if (!MWFVFS.MyComputerDevicesPrefix.Contains (fsEntry.FullName + "://"))
3830 MWFVFS.MyComputerDevicesPrefix.Add (fsEntry.FullName + "://", fsEntry);
3833 foreach (MasterMount.Mount mount in masterMount.Removable_devices) {
3834 FSEntry fsEntry = new FSEntry ();
3835 fsEntry.FileType = FSEntry.FSEntryType.RemovableDevice;
3837 fsEntry.FullName = mount.mount_point;
3839 bool is_dvd_cdrom = mount.fsType == MasterMount.FsTypes.usbfs ? false : true;
3840 string type_name = is_dvd_cdrom ? "DVD/CD-Rom" : "USB";
3841 string mime_type = is_dvd_cdrom ? "cdrom/cdrom" : "removable/removable";
3843 fsEntry.Name = type_name +" (" + mount.device_short + ")";
3845 fsEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType (mime_type);
3847 fsEntry.FsType = mount.fsType;
3848 fsEntry.DeviceShort = mount.device_short;
3850 fsEntry.Attributes = FileAttributes.Directory;
3852 fsEntry.MainTopNode = GetMyComputerFSEntry ();
3854 my_computer_content_arraylist.Add (fsEntry);
3856 string contain_string = fsEntry.FullName + "://";
3857 if (!MWFVFS.MyComputerDevicesPrefix.Contains (contain_string))
3858 MWFVFS.MyComputerDevicesPrefix.Add (contain_string, fsEntry);
3862 my_computer_content_arraylist.Add (GetMyComputerPersonalFSEntry ());
3864 return my_computer_content_arraylist;
3867 public override ArrayList GetMyNetworkContent ()
3869 ArrayList fsEntries = new ArrayList ();
3871 foreach (MasterMount.Mount mount in masterMount.Network_devices) {
3872 FSEntry fsEntry = new FSEntry ();
3873 fsEntry.FileType = FSEntry.FSEntryType.Network;
3875 fsEntry.FullName = mount.mount_point;
3877 fsEntry.FsType = mount.fsType;
3878 fsEntry.DeviceShort = mount.device_short;
3880 fsEntry.Name = "Network (" + mount.fsType + ", " + mount.device_short + ")";
3882 switch (mount.fsType) {
3883 case MasterMount.FsTypes.nfs:
3884 fsEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("nfs/nfs");
3886 case MasterMount.FsTypes.smbfs:
3887 fsEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("smb/smb");
3889 case MasterMount.FsTypes.ncpfs:
3890 fsEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("network/network");
3892 case MasterMount.FsTypes.cifs:
3893 fsEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("network/network");
3899 fsEntry.Attributes = FileAttributes.Directory;
3901 fsEntry.MainTopNode = GetMyNetworkFSEntry ();
3903 fsEntries.Add (fsEntry);
3908 protected override FSEntry GetDesktopFSEntry ()
3910 return desktopFSEntry;
3913 protected override FSEntry GetRecentlyUsedFSEntry ()
3915 return recentlyusedFSEntry;
3918 protected override FSEntry GetPersonalFSEntry ()
3920 return personalFSEntry;
3923 protected override FSEntry GetMyComputerPersonalFSEntry ()
3925 return mycomputerpersonalFSEntry;
3928 protected override FSEntry GetMyComputerFSEntry ()
3930 return mycomputerFSEntry;
3933 protected override FSEntry GetMyNetworkFSEntry ()
3935 return mynetworkFSEntry;
3940 #region WinFileSystem
3941 internal class WinFileSystem : FileSystem
3943 private FSEntry desktopFSEntry = null;
3944 private FSEntry recentlyusedFSEntry = null;
3945 private FSEntry personalFSEntry = null;
3946 private FSEntry mycomputerpersonalFSEntry = null;
3947 private FSEntry mycomputerFSEntry = null;
3948 private FSEntry mynetworkFSEntry = null;
3950 public WinFileSystem ()
3952 desktopFSEntry = new FSEntry ();
3954 desktopFSEntry.Attributes = FileAttributes.Directory;
3955 desktopFSEntry.FullName = MWFVFS.DesktopPrefix;
3956 desktopFSEntry.Name = "Desktop";
3957 desktopFSEntry.RealName = ThemeEngine.Current.Places (UIIcon.PlacesDesktop);
3958 desktopFSEntry.FileType = FSEntry.FSEntryType.Directory;
3959 desktopFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("desktop/desktop");
3960 desktopFSEntry.LastAccessTime = DateTime.Now;
3962 recentlyusedFSEntry = new FSEntry ();
3964 recentlyusedFSEntry.Attributes = FileAttributes.Directory;
3965 recentlyusedFSEntry.FullName = MWFVFS.RecentlyUsedPrefix;
3966 recentlyusedFSEntry.RealName = ThemeEngine.Current.Places (UIIcon.PlacesRecentDocuments);
3967 recentlyusedFSEntry.Name = "Recently Used";
3968 recentlyusedFSEntry.FileType = FSEntry.FSEntryType.Directory;
3969 recentlyusedFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("recently/recently");
3970 recentlyusedFSEntry.LastAccessTime = DateTime.Now;
3972 personalFSEntry = new FSEntry ();
3974 personalFSEntry.Attributes = FileAttributes.Directory;
3975 personalFSEntry.FullName = MWFVFS.PersonalPrefix;
3976 personalFSEntry.Name = "Personal";
3977 personalFSEntry.MainTopNode = GetDesktopFSEntry ();
3978 personalFSEntry.RealName = ThemeEngine.Current.Places (UIIcon.PlacesPersonal);
3979 personalFSEntry.FileType = FSEntry.FSEntryType.Directory;
3980 personalFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("directory/home");
3981 personalFSEntry.LastAccessTime = DateTime.Now;
3983 mycomputerpersonalFSEntry = new FSEntry ();
3985 mycomputerpersonalFSEntry.Attributes = FileAttributes.Directory;
3986 mycomputerpersonalFSEntry.FullName = MWFVFS.MyComputerPersonalPrefix;
3987 mycomputerpersonalFSEntry.Name = "Personal";
3988 mycomputerpersonalFSEntry.MainTopNode = GetMyComputerFSEntry ();
3989 mycomputerpersonalFSEntry.RealName = ThemeEngine.Current.Places (UIIcon.PlacesPersonal);
3990 mycomputerpersonalFSEntry.FileType = FSEntry.FSEntryType.Directory;
3991 mycomputerpersonalFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("directory/home");
3992 mycomputerpersonalFSEntry.LastAccessTime = DateTime.Now;
3994 mycomputerFSEntry = new FSEntry ();
3996 mycomputerFSEntry.Attributes = FileAttributes.Directory;
3997 mycomputerFSEntry.FullName = MWFVFS.MyComputerPrefix;
3998 mycomputerFSEntry.Name = "My Computer";
3999 mycomputerFSEntry.MainTopNode = GetDesktopFSEntry ();
4000 mycomputerFSEntry.FileType = FSEntry.FSEntryType.Directory;
4001 mycomputerFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("workplace/workplace");
4002 mycomputerFSEntry.LastAccessTime = DateTime.Now;
4004 mynetworkFSEntry = new FSEntry ();
4006 mynetworkFSEntry.Attributes = FileAttributes.Directory;
4007 mynetworkFSEntry.FullName = MWFVFS.MyNetworkPrefix;
4008 mynetworkFSEntry.Name = "My Network";
4009 mynetworkFSEntry.MainTopNode = GetDesktopFSEntry ();
4010 mynetworkFSEntry.FileType = FSEntry.FSEntryType.Directory;
4011 mynetworkFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("network/network");
4012 mynetworkFSEntry.LastAccessTime = DateTime.Now;
4015 public override void WriteRecentlyUsedFiles (string fileToAdd)
4017 // TODO: Implement this method
4018 // use SHAddToRecentDocs ?
4021 public override ArrayList GetRecentlyUsedFiles ()
4023 ArrayList al = new ArrayList ();
4025 DirectoryInfo di = new DirectoryInfo (recentlyusedFSEntry.RealName);
4027 FileInfo[] fileinfos = di.GetFiles ();
4029 foreach (FileInfo fi in fileinfos) {
4030 FSEntry fs = GetFileFSEntry (fi);
4038 public override ArrayList GetMyComputerContent ()
4040 string[] logical_drives = Directory.GetLogicalDrives ();
4042 ArrayList my_computer_content_arraylist = new ArrayList ();
4044 foreach (string drive in logical_drives) {
4045 FSEntry fsEntry = new FSEntry ();
4046 fsEntry.FileType = FSEntry.FSEntryType.Device;
4048 fsEntry.FullName = drive;
4050 fsEntry.Name = drive;
4052 fsEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("harddisk/harddisk");
4054 fsEntry.Attributes = FileAttributes.Directory;
4056 fsEntry.MainTopNode = GetMyComputerFSEntry ();
4058 my_computer_content_arraylist.Add (fsEntry);
4060 string contain_string = fsEntry.FullName + "://";
4061 if (!MWFVFS.MyComputerDevicesPrefix.Contains (contain_string))
4062 MWFVFS.MyComputerDevicesPrefix.Add (contain_string, fsEntry);
4065 my_computer_content_arraylist.Add (GetMyComputerPersonalFSEntry ());
4067 return my_computer_content_arraylist;
4070 public override ArrayList GetMyNetworkContent ()
4072 // TODO: Implement this method
4073 return new ArrayList ();
4075 protected override FSEntry GetDesktopFSEntry ()
4077 return desktopFSEntry;
4080 protected override FSEntry GetRecentlyUsedFSEntry ()
4082 return recentlyusedFSEntry;
4085 protected override FSEntry GetPersonalFSEntry ()
4087 return personalFSEntry;
4090 protected override FSEntry GetMyComputerPersonalFSEntry ()
4092 return mycomputerpersonalFSEntry;
4095 protected override FSEntry GetMyComputerFSEntry ()
4097 return mycomputerFSEntry;
4100 protected override FSEntry GetMyNetworkFSEntry ()
4102 return mynetworkFSEntry;
4108 internal class FSEntry
4110 public enum FSEntryType
4122 private MasterMount.FsTypes fsType;
4123 private string device_short;
4124 private string fullName;
4125 private string name;
4126 private string realName = null;
4127 private FileAttributes attributes = FileAttributes.Normal;
4128 private long fileSize;
4129 private FSEntryType fileType;
4130 private DateTime lastAccessTime;
4131 private FSEntry mainTopNode = null;
4133 private int iconIndex;
4135 private string parent;
4137 public MasterMount.FsTypes FsType {
4147 public string DeviceShort {
4149 device_short = value;
4153 return device_short;
4157 public string FullName {
4167 public string Name {
4177 public string RealName {
4187 public FileAttributes Attributes {
4197 public long FileSize {
4207 public FSEntryType FileType {
4217 public DateTime LastAccessTime {
4219 lastAccessTime = value;
4223 return lastAccessTime;
4227 public int IconIndex {
4237 public FSEntry MainTopNode {
4239 mainTopNode = value;
4247 public string Parent {
4253 parent = GetParent ();
4259 private string GetParent ()
4261 if (fullName == MWFVFS.PersonalPrefix) {
4262 return MWFVFS.DesktopPrefix;
4264 if (fullName == MWFVFS.MyComputerPersonalPrefix) {
4265 return MWFVFS.MyComputerPrefix;
4267 if (fullName == MWFVFS.MyComputerPrefix) {
4268 return MWFVFS.DesktopPrefix;
4270 if (fullName == MWFVFS.MyNetworkPrefix) {
4271 return MWFVFS.DesktopPrefix;
4273 if (fullName == MWFVFS.DesktopPrefix) {
4276 if (fullName == MWFVFS.RecentlyUsedPrefix) {
4279 foreach (DictionaryEntry entry in MWFVFS.MyComputerDevicesPrefix) {
4280 FSEntry fsEntry = entry.Value as FSEntry;
4281 if (fullName == fsEntry.FullName) {
4282 return fsEntry.MainTopNode.FullName;
4286 DirectoryInfo dirInfo = new DirectoryInfo (fullName);
4288 DirectoryInfo dirInfoParent = dirInfo.Parent;
4290 if (dirInfoParent != null) {
4291 FSEntry fsEntry = MWFVFS.MyComputerDevicesPrefix [dirInfoParent.FullName + "://"] as FSEntry;
4293 if (fsEntry != null) {
4294 return fsEntry.FullName;
4297 if (mainTopNode != null) {
4298 if (dirInfoParent.FullName == ThemeEngine.Current.Places (UIIcon.PlacesDesktop) &&
4299 mainTopNode.FullName == MWFVFS.DesktopPrefix) {
4300 return mainTopNode.FullName;
4302 if (dirInfoParent.FullName == ThemeEngine.Current.Places (UIIcon.PlacesPersonal) &&
4303 mainTopNode.FullName == MWFVFS.PersonalPrefix) {
4304 return mainTopNode.FullName;
4306 if (dirInfoParent.FullName == ThemeEngine.Current.Places (UIIcon.PlacesPersonal) &&
4307 mainTopNode.FullName == MWFVFS.MyComputerPersonalPrefix) {
4308 return mainTopNode.FullName;
4312 return dirInfoParent.FullName;
4322 // Alexsantas little *nix helper
4323 internal class MasterMount
4326 internal enum FsTypes
4350 internal struct Mount
4352 public string device_or_filesystem;
4353 public string device_short;
4354 public string mount_point;
4355 public FsTypes fsType;
4358 bool proc_mount_available = false;
4360 ArrayList block_devices = new ArrayList ();
4361 ArrayList network_devices = new ArrayList ();
4362 ArrayList removable_devices = new ArrayList ();
4364 private MountComparer mountComparer = new MountComparer ();
4366 public MasterMount ()
4368 // maybe check if the current user can access /proc/mounts
4369 if (XplatUI.RunningOnUnix)
4370 if (File.Exists ("/proc/mounts"))
4371 proc_mount_available = true;
4374 public ArrayList Block_devices {
4376 return block_devices;
4380 public ArrayList Network_devices {
4382 return network_devices;
4386 public ArrayList Removable_devices {
4388 return removable_devices;
4392 public bool ProcMountAvailable {
4394 return proc_mount_available;
4398 public void GetMounts ()
4400 if (!proc_mount_available)
4403 block_devices.Clear ();
4404 network_devices.Clear ();
4405 removable_devices.Clear ();
4408 StreamReader sr = new StreamReader ("/proc/mounts");
4410 string line = sr.ReadLine ();
4412 ArrayList lines = new ArrayList ();
4413 while (line != null) {
4414 if (lines.IndexOf (line) == -1) { // Avoid duplicates
4415 ProcessProcMountLine (line);
4418 line = sr.ReadLine ();
4423 block_devices.Sort (mountComparer);
4424 network_devices.Sort (mountComparer);
4425 removable_devices.Sort (mountComparer);
4431 private void ProcessProcMountLine (string line)
4433 string[] split = line.Split (new char [] {' '});
4435 if (split != null && split.Length > 0) {
4436 Mount mount = new Mount ();
4438 if (split [0].StartsWith ("/dev/"))
4439 mount.device_short = split [0].Replace ("/dev/", String.Empty);
4441 mount.device_short = split [0];
4443 mount.device_or_filesystem = split [0];
4444 mount.mount_point = split [1];
4446 // TODO: other removable devices, floppy
4450 if (split [2] == "nfs") {
4451 mount.fsType = FsTypes.nfs;
4452 network_devices.Add (mount);
4453 } else if (split [2] == "smbfs") {
4454 mount.fsType = FsTypes.smbfs;
4455 network_devices.Add (mount);
4456 } else if (split [2] == "cifs") {
4457 mount.fsType = FsTypes.cifs;
4458 network_devices.Add (mount);
4459 } else if (split [2] == "ncpfs") {
4460 mount.fsType = FsTypes.ncpfs;
4461 network_devices.Add (mount);
4463 } else if (split [2] == "iso9660") { //cdrom
4464 mount.fsType = FsTypes.iso9660;
4465 removable_devices.Add (mount);
4466 } else if (split [2] == "usbfs") { //usb ? not tested
4467 mount.fsType = FsTypes.usbfs;
4468 removable_devices.Add (mount);
4470 } else if (split [0].StartsWith ("/")) { //block devices
4471 if (split [1].StartsWith ("/dev/")) // root static, do not add
4474 if (split [2] == "ext2")
4475 mount.fsType = FsTypes.ext2;
4476 else if (split [2] == "ext3")
4477 mount.fsType = FsTypes.ext3;
4478 else if (split [2] == "reiserfs")
4479 mount.fsType = FsTypes.reiserfs;
4480 else if (split [2] == "xfs")
4481 mount.fsType = FsTypes.xfs;
4482 else if (split [2] == "vfat")
4483 mount.fsType = FsTypes.vfat;
4484 else if (split [2] == "ntfs")
4485 mount.fsType = FsTypes.ntfs;
4486 else if (split [2] == "msdos")
4487 mount.fsType = FsTypes.msdos;
4488 else if (split [2] == "umsdos")
4489 mount.fsType = FsTypes.umsdos;
4490 else if (split [2] == "hpfs")
4491 mount.fsType = FsTypes.hpfs;
4492 else if (split [2] == "minix")
4493 mount.fsType = FsTypes.minix;
4494 else if (split [2] == "jfs")
4495 mount.fsType = FsTypes.jfs;
4497 block_devices.Add (mount);
4502 public class MountComparer : IComparer
4504 public int Compare (object mount1, object mount2)
4506 return String.Compare (((Mount)mount1).device_short, ((Mount)mount2).device_short);
4513 // easy to use class to store and read internal MWF config settings.
4514 // the config values are stored in the users home dir as a hidden xml file called "mwf_config".
4515 // currently supports int, string, byte, color and arrays (including byte arrays)
4516 // don't forget, when you read a value you still have to cast this value.
4519 // MWFConfig.SetValue ("SomeClass", "What", value);
4520 // object o = MWFConfig.GetValue ("SomeClass", "What");
4524 // string[] configFileNames = (string[])MWFConfig.GetValue ("FileDialog", "FileNames");
4525 // MWFConfig.SetValue ("FileDialog", "LastFolder", "/home/user");
4527 internal class MWFConfig
4529 private static MWFConfigInstance Instance = new MWFConfigInstance ();
4531 private static object lock_object = new object();
4533 public static object GetValue (string class_name, string value_name)
4535 lock (lock_object) {
4536 return Instance.GetValue (class_name, value_name);
4540 public static void SetValue (string class_name, string value_name, object value)
4542 lock (lock_object) {
4543 Instance.SetValue (class_name, value_name, value);
4547 public static void Flush ()
4549 lock (lock_object) {
4554 public static void RemoveClass (string class_name)
4556 lock (lock_object) {
4557 Instance.RemoveClass (class_name);
4561 public static void RemoveClassValue (string class_name, string value_name)
4563 lock (lock_object) {
4564 Instance.RemoveClassValue (class_name, value_name);
4568 public static void RemoveAllClassValues (string class_name)
4570 lock (lock_object) {
4571 Instance.RemoveAllClassValues (class_name);
4575 internal class MWFConfigInstance
4577 Hashtable classes_hashtable = new Hashtable ();
4578 static string full_file_name;
4579 static string default_file_name;
4580 readonly string configName = "MWFConfig";
4582 static MWFConfigInstance ()
4584 string b = "mwf_config";
4585 string dir = Environment.GetFolderPath (Environment.SpecialFolder.Personal);
4587 if (XplatUI.RunningOnUnix) {
4588 dir = Path.Combine (dir, ".mono");
4590 Directory.CreateDirectory (dir);
4594 default_file_name = Path.Combine (dir, b);
4595 full_file_name = default_file_name;
4598 public MWFConfigInstance ()
4600 Open (default_file_name);
4604 public MWFConfigInstance (string filename)
4606 string path = Path.GetDirectoryName (filename);
4607 if (path == null || path == String.Empty) {
4608 path = Environment.GetFolderPath (Environment.SpecialFolder.Personal);
4610 full_file_name = Path.Combine (path, filename);
4612 full_file_name = filename;
4614 Open (full_file_name);
4617 ~MWFConfigInstance ()
4622 public object GetValue (string class_name, string value_name)
4624 ClassEntry class_entry = classes_hashtable [class_name] as ClassEntry;
4626 if (class_entry != null)
4627 return class_entry.GetValue (value_name);
4632 public void SetValue (string class_name, string value_name, object value)
4634 ClassEntry class_entry = classes_hashtable [class_name] as ClassEntry;
4636 if (class_entry == null) {
4637 class_entry = new ClassEntry ();
4638 class_entry.ClassName = class_name;
4639 classes_hashtable [class_name] = class_entry;
4642 class_entry.SetValue (value_name, value);
4645 private void Open (string filename)
4648 XmlTextReader xtr = new XmlTextReader (filename);
4653 } catch (Exception) {
4657 public void Flush ()
4660 XmlTextWriter xtw = new XmlTextWriter (full_file_name, null);
4661 xtw.Formatting = Formatting.Indented;
4667 if (!XplatUI.RunningOnUnix)
4668 File.SetAttributes (full_file_name, FileAttributes.Hidden);
4669 } catch (Exception){
4673 public void RemoveClass (string class_name)
4675 ClassEntry class_entry = classes_hashtable [class_name] as ClassEntry;
4677 if (class_entry != null) {
4678 class_entry.RemoveAllClassValues ();
4680 classes_hashtable.Remove (class_name);
4684 public void RemoveClassValue (string class_name, string value_name)
4686 ClassEntry class_entry = classes_hashtable [class_name] as ClassEntry;
4688 if (class_entry != null) {
4689 class_entry.RemoveClassValue (value_name);
4693 public void RemoveAllClassValues (string class_name)
4695 ClassEntry class_entry = classes_hashtable [class_name] as ClassEntry;
4697 if (class_entry != null) {
4698 class_entry.RemoveAllClassValues ();
4702 private void ReadConfig (XmlTextReader xtr)
4704 if (!CheckForMWFConfig (xtr))
4707 while (xtr.Read ()) {
4708 switch (xtr.NodeType) {
4709 case XmlNodeType.Element:
4710 ClassEntry class_entry = classes_hashtable [xtr.Name] as ClassEntry;
4712 if (class_entry == null) {
4713 class_entry = new ClassEntry ();
4714 class_entry.ClassName = xtr.Name;
4715 classes_hashtable [xtr.Name] = class_entry;
4718 class_entry.ReadXml (xtr);
4724 private bool CheckForMWFConfig (XmlTextReader xtr)
4727 if (xtr.NodeType == XmlNodeType.Element) {
4728 if (xtr.Name == configName)
4736 private void WriteConfig (XmlTextWriter xtw)
4738 if (classes_hashtable.Count == 0)
4741 xtw.WriteStartElement (configName);
4742 foreach (DictionaryEntry entry in classes_hashtable) {
4743 ClassEntry class_entry = entry.Value as ClassEntry;
4745 class_entry.WriteXml (xtw);
4747 xtw.WriteEndElement ();
4750 internal class ClassEntry
4752 private Hashtable classvalues_hashtable = new Hashtable ();
4753 private string className;
4755 public string ClassName {
4765 public void SetValue (string value_name, object value)
4767 ClassValue class_value = classvalues_hashtable [value_name] as ClassValue;
4769 if (class_value == null) {
4770 class_value = new ClassValue ();
4771 class_value.Name = value_name;
4772 classvalues_hashtable [value_name] = class_value;
4775 class_value.SetValue (value);
4778 public object GetValue (string value_name)
4780 ClassValue class_value = classvalues_hashtable [value_name] as ClassValue;
4782 if (class_value == null) {
4786 return class_value.GetValue ();
4789 public void RemoveAllClassValues ()
4791 classvalues_hashtable.Clear ();
4794 public void RemoveClassValue (string value_name)
4796 ClassValue class_value = classvalues_hashtable [value_name] as ClassValue;
4798 if (class_value != null) {
4799 classvalues_hashtable.Remove (value_name);
4803 public void ReadXml (XmlTextReader xtr)
4805 while (xtr.Read ()) {
4806 switch (xtr.NodeType) {
4807 case XmlNodeType.Element:
4808 string name = xtr.GetAttribute ("name");
4810 ClassValue class_value = classvalues_hashtable [name] as ClassValue;
4812 if (class_value == null) {
4813 class_value = new ClassValue ();
4814 class_value.Name = name;
4815 classvalues_hashtable [name] = class_value;
4818 class_value.ReadXml (xtr);
4821 case XmlNodeType.EndElement:
4827 public void WriteXml (XmlTextWriter xtw)
4829 if (classvalues_hashtable.Count == 0)
4832 xtw.WriteStartElement (className);
4834 foreach (DictionaryEntry entry in classvalues_hashtable) {
4835 ClassValue class_value = entry.Value as ClassValue;
4837 class_value.WriteXml (xtw);
4840 xtw.WriteEndElement ();
4844 internal class ClassValue
4846 private object value;
4847 private string name;
4849 public string Name {
4859 public void SetValue (object value)
4863 public object GetValue ()
4868 public void ReadXml (XmlTextReader xtr)
4871 string single_value;
4873 type = xtr.GetAttribute ("type");
4875 if (type == "byte_array" || type.IndexOf ("-array") == -1) {
4876 single_value = xtr.ReadString ();
4878 if (type == "string") {
4879 value = single_value;
4881 if (type == "int") {
4882 value = Int32.Parse (single_value);
4884 if (type == "byte") {
4885 value = Byte.Parse (single_value);
4887 if (type == "color") {
4888 int color = Int32.Parse (single_value);
4889 value = Color.FromArgb (color);
4891 if (type == "byte-array") {
4892 byte[] b_array = Convert.FromBase64String (single_value);
4896 ReadXmlArrayValues (xtr, type);
4900 private void ReadXmlArrayValues (XmlTextReader xtr, string type)
4902 ArrayList al = new ArrayList ();
4904 while (xtr.Read ()) {
4905 switch (xtr.NodeType) {
4906 case XmlNodeType.Element:
4907 string single_value = xtr.ReadString ();
4909 if (type == "int-array") {
4910 int int_val = Int32.Parse (single_value);
4913 if (type == "string-array") {
4914 string str_val = single_value;
4919 case XmlNodeType.EndElement:
4920 if (xtr.Name == "value") {
4921 if (type == "int-array") {
4922 value = al.ToArray (typeof(int));
4924 if (type == "string-array") {
4925 value = al.ToArray (typeof(string));
4934 public void WriteXml (XmlTextWriter xtw)
4936 xtw.WriteStartElement ("value");
4937 xtw.WriteAttributeString ("name", name);
4938 if (value is Array) {
4939 WriteArrayContent (xtw);
4941 WriteSingleContent (xtw);
4943 xtw.WriteEndElement ();
4946 private void WriteSingleContent (XmlTextWriter xtw)
4948 string type_string = String.Empty;
4950 if (value is string)
4951 type_string = "string";
4954 type_string = "int";
4957 type_string = "byte";
4960 type_string = "color";
4962 xtw.WriteAttributeString ("type", type_string);
4965 xtw.WriteString (((Color)value).ToArgb ().ToString ());
4967 xtw.WriteString (value.ToString ());
4970 private void WriteArrayContent (XmlTextWriter xtw)
4972 string type_string = String.Empty;
4973 string type_name = String.Empty;
4975 if (value is string[]) {
4976 type_string = "string-array";
4977 type_name = "string";
4979 if (value is int[]) {
4980 type_string = "int-array";
4983 if (value is byte[]) {
4984 type_string = "byte-array";
4988 xtw.WriteAttributeString ("type", type_string);
4990 if (type_string != "byte-array") {
4991 Array array = value as Array;
4993 foreach (object o in array) {
4994 xtw.WriteStartElement (type_name);
4995 xtw.WriteString (o.ToString ());
4996 xtw.WriteEndElement ();
4999 byte[] b_array = value as byte [];
5001 xtw.WriteString (Convert.ToBase64String (b_array, 0, b_array.Length));