New test.
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / FileDialog.cs
index 17158f7575698170712ea3b0de41d31e2207c667..b5078c50c0398f28815fb28a59a43550254b12ac 100644 (file)
@@ -34,7 +34,7 @@ using System.IO;
 using System.Collections;
 using System.Collections.Specialized;
 using System.Xml;
-using Microsoft.Win32;
+using System.Threading;
 
 namespace System.Windows.Forms {
        #region FileDialog
@@ -53,16 +53,17 @@ namespace System.Windows.Forms {
                private bool addExtension = true;
                private bool checkFileExists = false;
                private bool checkPathExists = true;
-               private string defaultExt = "";
+               private string defaultExt = String.Empty;
                private bool dereferenceLinks = true;
-               private string fileName = "";
+               private string fileName = String.Empty;
                private string[] fileNames;
-               private string filter;
+               private string filter = "";
                private int filterIndex = 1;
-               private string initialDirectory = "";
+               private int setFilterIndex = 1;
+               private string initialDirectory = String.Empty;
                private bool restoreDirectory = false;
                private bool showHelp = false;
-               private string title = "";
+               private string title = String.Empty;
                private bool validateNames = true;
                
                private Button cancelButton;
@@ -87,7 +88,7 @@ namespace System.Windows.Forms {
                
                private bool multiSelect = false;
                
-               private string restoreDirectoryString = "";
+               private string restoreDirectoryString = String.Empty;
                
                internal FileDialogType fileDialogType;
                
@@ -100,49 +101,40 @@ namespace System.Windows.Forms {
                
                internal FileFilter fileFilter;
                
-               private string lastFolder = "";
+               private string lastFolder = String.Empty;
                
                private MWFVFS vfs;
                
-               private RegistryKey rootRegistryKey;
-               private RegistryKey filedialogRegistryKey;
-               private string registryKeyName = @"SOFTWARE\Mono.MWF\FileDialog";
-               
-               private int platform = (int) Environment.OSVersion.Platform;
-               private bool running_windows = false;
+               private readonly string filedialog_string = "FileDialog";
+               private readonly string lastfolder_string = "LastFolder";
+               private readonly string width_string = "Width";
+               private readonly string height_string = "Height";
+               private readonly string filenames_string = "FileNames";
+               private readonly string x_string = "X";
+               private readonly string y_string = "Y";
                
                internal FileDialog ()
                {
                        vfs = new MWFVFS ();
                        
-                       if ((platform != 4) && (platform != 128))
-                               running_windows = true;
+                       Size formConfigSize = Size.Empty;
+                       Point formConfigLocation = Point.Empty;
+                       string[] configFileNames = null;
                        
-                       Size formRegistrySize = Size.Empty;
-                       Point formRegistryLocation = Point.Empty;
-                       string[] registryFileNames = null;
+                       object formWidth = MWFConfig.GetValue (filedialog_string, width_string);
                        
-                       if (!running_windows) {
-                               rootRegistryKey = Microsoft.Win32.Registry.CurrentUser;
-                               filedialogRegistryKey = rootRegistryKey.OpenSubKey (registryKeyName);
-                               
-                               if (filedialogRegistryKey != null) {
-                                       object formWidth = filedialogRegistryKey.GetValue ("Width");
-                                       
-                                       object formHeight = filedialogRegistryKey.GetValue ("Height");
-                                       
-                                       if (formHeight != null && formWidth != null)
-                                               formRegistrySize = new Size ((int)formWidth, (int)formHeight);
-                                       
-                                       object formLocationX = filedialogRegistryKey.GetValue ("X");
-                                       object formLocationY = filedialogRegistryKey.GetValue ("Y");
-                                       
-                                       if (formLocationX != null && formLocationY != null)
-                                               formRegistryLocation = new Point ((int)formLocationX, (int)formLocationY);
-                                       
-                                       registryFileNames = (string[])filedialogRegistryKey.GetValue ("FileNames");
-                               }
-                       }
+                       object formHeight = MWFConfig.GetValue (filedialog_string, height_string);
+                       
+                       if (formHeight != null && formWidth != null)
+                               formConfigSize = new Size ((int)formWidth, (int)formHeight);
+                       
+                       object formLocationX = MWFConfig.GetValue (filedialog_string, x_string);
+                       object formLocationY = MWFConfig.GetValue (filedialog_string, y_string);
+                       
+                       if (formLocationX != null && formLocationY != null)
+                               formConfigLocation = new Point ((int)formLocationX, (int)formLocationY);
+                       
+                       configFileNames = (string[])MWFConfig.GetValue (filedialog_string, filenames_string);
                        
                        fileTypeComboBox = new ComboBox ();
                        backToolBarButton = new ToolBarButton ();
@@ -245,13 +237,13 @@ namespace System.Windows.Forms {
                        fileNameComboBox.MaxDropDownItems = 10;
                        fileNameComboBox.Items.Add (" ");
                        
-                       if (registryFileNames != null) {
+                       if (configFileNames != null) {
                                fileNameComboBox.Items.Clear ();
                                
-                               foreach (string registryFileName in registryFileNames) {
-                                       if (registryFileName != null)
-                                               if (registryFileName.Trim ().Length > 0)
-                                                       fileNameComboBox.Items.Add (registryFileName);
+                               foreach (string configFileName in configFileNames) {
+                                       if (configFileName != null)
+                                               if (configFileName.Trim ().Length > 0)
+                                                       fileNameComboBox.Items.Add (configFileName);
                                }
                        }
                        
@@ -266,6 +258,7 @@ namespace System.Windows.Forms {
                        
                        // fileTypeComboBox
                        fileTypeComboBox.Anchor = ((AnchorStyles)(((AnchorStyles.Bottom | AnchorStyles.Left) | AnchorStyles.Right)));
+                       fileTypeComboBox.DropDownStyle = ComboBoxStyle.DropDownList;
                        fileTypeComboBox.Location = new Point (195, 356);
                        fileTypeComboBox.Size = new Size (245, 21);
                        fileTypeComboBox.TabIndex = 2;
@@ -330,10 +323,11 @@ namespace System.Windows.Forms {
                        form.SizeGripStyle = SizeGripStyle.Show;
                        
                        form.MaximizeBox = true;
+                       form.MinimizeBox = true;
                        form.FormBorderStyle = FormBorderStyle.Sizable;
                        form.MinimumSize = new Size (554, 405);
                        
-                       form.Size =  new Size (554, 405); // 384
+                       form.ClientSize =  new Size (554, 405); // 384
                        
                        form.Controls.Add (smallButtonToolBar);
                        form.Controls.Add (cancelButton);
@@ -349,12 +343,12 @@ namespace System.Windows.Forms {
                        
                        form.ResumeLayout (false);
                        
-                       if (formRegistrySize != Size.Empty) {
-                               form.Size = formRegistrySize;
+                       if (formConfigSize != Size.Empty) {
+                               form.Size = formConfigSize;
                        }
                        
-                       if (formRegistryLocation != Point.Empty) {
-                               form.Location = formRegistryLocation;
+                       if (formConfigLocation != Point.Empty) {
+                               form.Location = formConfigLocation;
                        }
                        
                        openSaveButton.Click += new EventHandler (OnClickOpenSaveButton);
@@ -448,8 +442,19 @@ namespace System.Windows.Forms {
                        
                        set {
                                if (value != null) {
-                                       if (SetFileName (value))
+                                       if (SetFileName (value)) {
                                                fileName = value;
+                                               if (fileNames == null) {
+                                                       fileNames = new string [1];
+                                                       fileNames [0] = value;
+                                               }
+                                       }
+                               } else {
+                                       fileName = String.Empty;
+                                       fileNames = new string [0];
+                                       // this does not match ms exactly,
+                                       // but there is no other way to clear that %#&?ยง combobox
+                                       fileNameComboBox.Text = " ";
                                }
                        }
                }
@@ -477,12 +482,18 @@ namespace System.Windows.Forms {
                        }
                        
                        set {
-                               if (value == null)
-                                       throw new NullReferenceException ("Filter");
-                               
-                               filter = value;
-                               
-                               fileFilter = new FileFilter (filter);
+                               if (value == null) {
+                                       filter = "";
+                                       if (fileFilter != null)
+                                               fileFilter.FilterArrayList.Clear ();
+                               } else {
+                                       if (FileFilter.CheckFilter (value)) {
+                                               filter = value;
+                                               
+                                               fileFilter = new FileFilter (filter);
+                                       } else
+                                               throw new ArgumentException ();
+                               }
                                
                                UpdateFilters ();
                        }
@@ -491,14 +502,16 @@ namespace System.Windows.Forms {
                [DefaultValue(1)]
                public int FilterIndex {
                        get {
-                               return filterIndex;
+                               return setFilterIndex;
                        }
                        
                        set {
-                               if (fileFilter != null && fileFilter.FilterArrayList.Count > value)
-                                       return;  // FIXME: throw an exception ?
+                               setFilterIndex = value;
                                
-                               filterIndex = value;
+                               if (value < 1)
+                                       filterIndex = 1;
+                               else
+                                       filterIndex = value;
                                
                                SelectFilter ();
                        }
@@ -583,16 +596,15 @@ namespace System.Windows.Forms {
                        addExtension = true;
                        checkFileExists = false;
                        checkPathExists = true;
-                       defaultExt = "";
+                       defaultExt = String.Empty;
                        dereferenceLinks = true;
-                       fileName = "";
-                       fileNames = null;
-                       Filter = "";
-                       filterIndex = 1;
-                       initialDirectory = "";
+                       FileName = String.Empty;
+                       Filter = String.Empty;
+                       FilterIndex = 1;
+                       initialDirectory = String.Empty;
                        restoreDirectory = false;
                        ShowHelp = false;
-                       Title = "";
+                       Title = String.Empty;
                        validateNames = true;
                        
                        UpdateFilters ();
@@ -600,7 +612,7 @@ namespace System.Windows.Forms {
                
                public override string ToString ()
                {
-                       return base.ToString ();
+                       return String.Format("{0}: Title: {1}, FileName: {2}", base.ToString (), Title, fileName);
                }
                
                public event CancelEventHandler FileOk {
@@ -630,8 +642,7 @@ namespace System.Windows.Forms {
                
                protected void OnFileOk (CancelEventArgs e)
                {
-                       if (!running_windows)
-                               WriteRegistryValues (e);
+                       WriteConfigValues (e);
                        
                        CancelEventHandler fo = (CancelEventHandler) Events [EventFileOk];
                        if (fo != null)
@@ -640,7 +651,7 @@ namespace System.Windows.Forms {
                
                protected  override bool RunDialog (IntPtr hWndOwner)
                {
-                       ReadRegistryValues ();
+                       ReadConfigValues ();
                        
                        form.Refresh ();
                        
@@ -698,15 +709,15 @@ namespace System.Windows.Forms {
                
                private void SelectFilter ()
                {
-                       if (FilterIndex > mwfFileView.FilterArrayList.Count)
+                       if (mwfFileView.FilterArrayList == null || filterIndex > mwfFileView.FilterArrayList.Count)
                                return;
                        
                        do_not_call_OnSelectedIndexChangedFileTypeComboBox = true;
                        fileTypeComboBox.BeginUpdate ();
-                       fileTypeComboBox.SelectedIndex = FilterIndex - 1;
+                       fileTypeComboBox.SelectedIndex = filterIndex - 1;
                        fileTypeComboBox.EndUpdate ();
                        
-                       mwfFileView.FilterIndex = FilterIndex;
+                       mwfFileView.FilterIndex = filterIndex;
                }
                
                private bool SetFileName (string fname)
@@ -762,7 +773,7 @@ namespace System.Windows.Forms {
                                }
                        }
                        
-                       string internalfullfilename = "";
+                       string internalfullfilename = String.Empty;
                        
                        if (!multiSelect) {
                                string fileFromComboBox = fileNameComboBox.Text.Trim ();
@@ -831,8 +842,8 @@ namespace System.Windows.Forms {
                                
                                if (fileDialogType == FileDialogType.SaveFileDialog) {
                                        if (addExtension) {
-                                               string extension_to_use = "";
-                                               string filter_exentsion = "";
+                                               string extension_to_use = String.Empty;
+                                               string filter_exentsion = String.Empty;
                                                
                                                if (fileFilter != null) {
                                                        FilterStruct filterstruct = (FilterStruct)fileFilter.FilterArrayList [filterIndex - 1];
@@ -851,13 +862,14 @@ namespace System.Windows.Forms {
                                                        }
                                                }
                                                
-                                               if (filter_exentsion != "")
+                                               if (filter_exentsion != String.Empty)
                                                        extension_to_use = filter_exentsion;
                                                else
-                                               if (defaultExt != "")
+                                               if (defaultExt != String.Empty)
                                                        extension_to_use = "." + defaultExt;
                                                
-                                               internalfullfilename += extension_to_use;
+                                               if (!internalfullfilename.EndsWith (extension_to_use))
+                                                       internalfullfilename += extension_to_use;
                                        }
                                }
                                
@@ -1007,6 +1019,9 @@ namespace System.Windows.Forms {
                
                private void UpdateFilters ()
                {
+                       if (fileFilter == null)
+                               fileFilter = new FileFilter ();
+                       
                        ArrayList filters = fileFilter.FilterArrayList;
                        
                        fileTypeComboBox.BeginUpdate ();
@@ -1017,13 +1032,14 @@ namespace System.Windows.Forms {
                                fileTypeComboBox.Items.Add (fs.filterName);
                        }
                        
-                       fileTypeComboBox.SelectedIndex = FilterIndex - 1;
+                       if (filters.Count > 0 && FilterIndex <= filters.Count)
+                               fileTypeComboBox.SelectedIndex = filterIndex - 1;
                        
                        fileTypeComboBox.EndUpdate ();
                        
                        mwfFileView.FilterArrayList = filters;
                        
-                       mwfFileView.FilterIndex = FilterIndex;
+                       mwfFileView.FilterIndex = filterIndex;
                }
                
                private void ResizeAndRelocateForHelpOrReadOnly ()
@@ -1059,45 +1075,38 @@ namespace System.Windows.Forms {
                        form.ResumeLayout ();
                }
                
-               private void WriteRegistryValues (CancelEventArgs ce)
+               private void WriteConfigValues (CancelEventArgs ce)
                {
-                       try {
-                               filedialogRegistryKey = rootRegistryKey.OpenSubKey (registryKeyName);
+                       MWFConfig.SetValue (filedialog_string, width_string, form.Width);
+                       MWFConfig.SetValue (filedialog_string, height_string, form.Height);
+                       MWFConfig.SetValue (filedialog_string, x_string, form.Location.X);
+                       MWFConfig.SetValue (filedialog_string, y_string, form.Location.Y);
+                       
+                       if (!ce.Cancel) {
+                               MWFConfig.SetValue (filedialog_string, lastfolder_string, lastFolder);
                                
-                               if (filedialogRegistryKey == null)
-                                       filedialogRegistryKey = rootRegistryKey.CreateSubKey (registryKeyName);
+                               string[] fileNameCBItems = new string [fileNameComboBox.Items.Count];
                                
-                               filedialogRegistryKey.SetValue ("Width", form.Width);
-                               filedialogRegistryKey.SetValue ("Height", form.Height);
-                               filedialogRegistryKey.SetValue ("X", form.Location.X);
-                               filedialogRegistryKey.SetValue ("Y", form.Location.Y);
+                               fileNameComboBox.Items.CopyTo (fileNameCBItems, 0);
                                
-                               if (!ce.Cancel) {
-                                       filedialogRegistryKey.SetValue ("LastFolder", lastFolder);
-                                       
-                                       string[] fileNameCBItems = new string [fileNameComboBox.Items.Count];
-                                       
-                                       fileNameComboBox.Items.CopyTo (fileNameCBItems, 0);
-                                       
-                                       filedialogRegistryKey.SetValue ("FileNames", fileNameCBItems);
-                               }
-                       } catch (Exception) {}
+                               MWFConfig.SetValue (filedialog_string, filenames_string, fileNameCBItems);
+                       }
                }
                
-               private void ReadRegistryValues ()
+               private void ReadConfigValues ()
                {
-                       rootRegistryKey = Microsoft.Win32.Registry.CurrentUser;
-                       filedialogRegistryKey = rootRegistryKey.OpenSubKey (registryKeyName);
+                       lastFolder = (string)MWFConfig.GetValue (filedialog_string, lastfolder_string);
                        
-                       if (!running_windows)
-                               if (filedialogRegistryKey != null) {
-                                       lastFolder = (string)filedialogRegistryKey.GetValue ("LastFolder");
+                       if (lastFolder != null && lastFolder.IndexOf ("://") == -1) {
+                               if (!Directory.Exists (lastFolder)) {
+                                       lastFolder = MWFVFS.DesktopPrefix;
                                }
+                       }
                        
-                       if (initialDirectory != "")
+                       if (initialDirectory != String.Empty)
                                lastFolder = initialDirectory;
                        else
-                       if (lastFolder == null || lastFolder == "")
+                       if (lastFolder == null || lastFolder == String.Empty)
                                lastFolder = Environment.CurrentDirectory;
                        
                        if (RestoreDirectory)
@@ -1534,8 +1543,6 @@ namespace System.Windows.Forms {
                
                private EventHandler on_directory_changed;
                
-               private bool currentpath_internal_change = false;
-               
                private Stack folderStack = new Stack();
                
                private static readonly int indent = 6;
@@ -1590,8 +1597,6 @@ namespace System.Windows.Forms {
                        set {
                                currentPath = value;
                                
-                               currentpath_internal_change = true;
-                               
                                CreateComboList ();
                        }
                        get {
@@ -1734,17 +1739,27 @@ namespace System.Windows.Forms {
                        Color foreColor = e.ForeColor;
                        
                        int xPos = dcbi.XPos;
-                       
+
                        if ((e.State & DrawItemState.ComboBoxEdit) != 0)
                                xPos = 0;
-                       else
-                       if ((e.State & DrawItemState.Selected) == DrawItemState.Selected) {
-                               backColor = ThemeEngine.Current.ColorHighlight;
+
+                       gr.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (backColor),
+                                       new Rectangle (0, 0, bmp.Width, bmp.Height));
+                       
+                       if ((e.State & DrawItemState.Selected) == DrawItemState.Selected &&
+                                       (!DroppedDown || (e.State & DrawItemState.ComboBoxEdit) != DrawItemState.ComboBoxEdit)) {
                                foreColor = ThemeEngine.Current.ColorHighlightText;
+
+                               int w = (int) gr.MeasureString (dcbi.Name, e.Font).Width;
+
+                               gr.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (ThemeEngine.Current.ColorHighlight),
+                                               new Rectangle (xPos + 23, 1, w + 3, e.Bounds.Height - 2));
+                               if ((e.State & DrawItemState.Focus) == DrawItemState.Focus) {
+                                       ControlPaint.DrawFocusRectangle (gr, new Rectangle (xPos + 22, 0, w + 5,
+                                                       e.Bounds.Height), foreColor, ThemeEngine.Current.ColorHighlight);
+                               }
                        }
-                       
-                       gr.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (backColor), new Rectangle (0, 0, bmp.Width, bmp.Height));
-                       
+
                        gr.DrawString (dcbi.Name, e.Font , ThemeEngine.Current.ResPool.GetSolidBrush (foreColor), new Point (24 + xPos, (bmp.Height - e.Font.Height) / 2));
                        gr.DrawImage (dcbi.ImageList.Images [dcbi.ImageIndex], new Rectangle (new Point (xPos + 2, 0), new Size (16, 16)));
                        
@@ -1759,15 +1774,13 @@ namespace System.Windows.Forms {
                                DirComboBoxItem dcbi = Items [SelectedIndex] as DirComboBoxItem;
                                
                                currentPath = dcbi.Path;
-                               // call DirectoryChange event only if the user changes the index with the ComboBox
-                               
-                               if (!currentpath_internal_change) {
-                                       if (on_directory_changed != null)
-                                               on_directory_changed (this, EventArgs.Empty);
-                               }
                        }
-                       
-                       currentpath_internal_change = false;
+               }
+               
+               protected override void OnSelectionChangeCommitted (EventArgs e)
+               {
+                       if (on_directory_changed != null)
+                               on_directory_changed (this, EventArgs.Empty);
                }
                
                public event EventHandler DirectoryChanged {
@@ -1795,8 +1808,9 @@ namespace System.Windows.Forms {
                private void SplitFilters (string filter)
                {
                        string[] split = filter.Split (new char [] {';'});
-                       
-                       filters.AddRange (split);
+                       foreach (string s in split) {
+                               filters.Add (s.Trim ());
+                       }
                }
        }
        #endregion
@@ -1811,13 +1825,26 @@ namespace System.Windows.Forms {
                public FileFilter ()
                {}
                
-               public FileFilter (string filter)
+               public FileFilter (string filter) : base ()
                {
                        this.filter = filter;
                        
                        SplitFilter ();
                }
                
+               public static bool CheckFilter (string val)
+               {
+                       if (val.Length == 0)
+                               return true;
+                       
+                       string[] filters = val.Split (new char [] {'|'});
+                       
+                       if ((filters.Length % 2) != 0)
+                               return false;
+                       
+                       return true;
+               }
+               
                public ArrayList FilterArrayList {
                        set {
                                filterArrayList = value;
@@ -1844,17 +1871,11 @@ namespace System.Windows.Forms {
                {
                        filterArrayList.Clear ();
                        
-                       if (filter == null)
-                               throw new NullReferenceException ("Filter");
-                       
                        if (filter.Length == 0)
                                return;
                        
                        string[] filters = filter.Split (new char [] {'|'});
                        
-                       if ((filters.Length % 2) != 0)
-                               throw new ArgumentException ("Filter");
-                       
                        for (int i = 0; i < filters.Length; i += 2) {
                                FilterStruct filterStruct = new FilterStruct (filters [i], filters [i + 1]);
                                
@@ -1927,9 +1948,12 @@ namespace System.Windows.Forms {
                private int old_menuitem_index;
                private bool do_update_view = false;
                
+               private int platform = (int) Environment.OSVersion.Platform;
+               
                public MWFFileView (MWFVFS vfs)
                {
                        this.vfs = vfs;
+                       vfs.RegisterUpdateDelegate (new MWFVFS.UpdateDelegate (RealFileViewUpdate), this);
                        
                        SuspendLayout ();
                        
@@ -1997,8 +2021,6 @@ namespace System.Windows.Forms {
                        
                        ResumeLayout (false);
                        
-//                     currentFolder = Environment.CurrentDirectory;
-                       
                        KeyDown += new KeyEventHandler (MWF_KeyDown);
                }
                
@@ -2063,7 +2085,7 @@ namespace System.Windows.Forms {
                        set {
                                filterIndex = value;
                                if (Visible)
-                                       UpdateFileView (currentFolder);
+                                       UpdateFileView ();
                        }
                        
                        get {
@@ -2110,7 +2132,8 @@ namespace System.Windows.Forms {
                
                public void CreateNewFolder ()
                {
-                       if (currentFolder == MWFVFS.RecentlyUsedPrefix)
+                       if (currentFolder == MWFVFS.MyComputerPrefix ||
+                           currentFolder == MWFVFS.RecentlyUsedPrefix)
                                return;
                        
                        FSEntry fsEntry = new FSEntry ();
@@ -2125,7 +2148,7 @@ namespace System.Windows.Forms {
                        TextEntryDialog ted = new TextEntryDialog ();
                        ted.IconPictureBoxImage = MimeIconEngine.LargeIcons.Images.GetImage (fsEntry.IconIndex);
                        
-                       string folder = "";
+                       string folder = String.Empty;
                        
                        if (currentFolderFSEntry.RealName != null)
                                folder = currentFolderFSEntry.RealName;
@@ -2137,11 +2160,19 @@ namespace System.Windows.Forms {
                        if (Directory.Exists (Path.Combine (folder, tmp_filename))) {
                                int i = 1;
                                
-                               tmp_filename = tmp_filename + " (" + i + ")";
+                               if ((platform == 4) || (platform == 128)) {
+                                       tmp_filename = tmp_filename + "-" + i;
+                               } else {
+                                       tmp_filename = tmp_filename + " (" + i + ")";
+                               }
                                
                                while (Directory.Exists (Path.Combine (folder, tmp_filename))) {
                                        i++;
-                                       tmp_filename = "New Folder" + " (" + i + ")";
+                                       if ((platform == 4) || (platform == 128)) {
+                                               tmp_filename = "New Folder" + "-" + i;
+                                       } else {
+                                               tmp_filename = "New Folder" + " (" + i + ")";
+                                       }
                                }
                        }
                        
@@ -2230,7 +2261,7 @@ namespace System.Windows.Forms {
                        EndUpdate ();
 
                        try {
-                               UpdateFileView (folder);
+                               UpdateFileView ();
                        } catch (Exception e) {
                                if (should_push)
                                        PopDir ();
@@ -2238,18 +2269,18 @@ namespace System.Windows.Forms {
                        }
                }
                
-               public void UpdateFileView (string folder)
+               public void UpdateFileView ()
                {
-                       ArrayList directoriesArrayList;
-                       ArrayList fileArrayList;
-                       
                        if (filterArrayList != null && filterArrayList.Count != 0) {
                                FilterStruct fs = (FilterStruct)filterArrayList [filterIndex - 1];
                                
-                               vfs.GetFolderContent (fs.filters, out directoriesArrayList, out fileArrayList);
+                               vfs.GetFolderContent (fs.filters);
                        } else
-                               vfs.GetFolderContent (out directoriesArrayList, out fileArrayList);
-                       
+                               vfs.GetFolderContent ();
+               }
+               
+               public void RealFileViewUpdate (ArrayList directoriesArrayList, ArrayList fileArrayList)
+               {
                        BeginUpdate ();
                        
                        Items.Clear ();
@@ -2319,7 +2350,7 @@ namespace System.Windows.Forms {
                                } else
                                if (o is ToolBarButton) {
                                        ToolBarButton t = o as ToolBarButton;
-                                       t.Enabled = (directoryStack.Count > 1);
+                                       t.Enabled = (directoryStack.Count > 0);
                                }
                        }
                }
@@ -2394,7 +2425,7 @@ namespace System.Windows.Forms {
                protected override void OnSelectedIndexChanged (EventArgs e)
                {
                        if (SelectedItems.Count > 0) {
-                               selectedFilesString = "";
+                               selectedFilesString = String.Empty;
                                
                                if (SelectedItems.Count == 1) {
                                        FileViewListViewItem listViewItem = SelectedItems [0] as FileViewListViewItem;
@@ -2449,7 +2480,8 @@ namespace System.Windows.Forms {
                                        
                                        toolTip.Active = true;
                                }
-                       }
+                       } else
+                               toolTip.Active = false;
                        
                        base.OnMouseMove (e);
                }
@@ -2461,7 +2493,7 @@ namespace System.Windows.Forms {
                        if (senderMenuItem == showHiddenFilesMenuItem) {
                                senderMenuItem.Checked = !senderMenuItem.Checked;
                                showHiddenFiles = senderMenuItem.Checked;
-                               UpdateFileView (currentFolder);
+                               UpdateFileView ();
                        }
                }
                
@@ -2549,7 +2581,7 @@ namespace System.Windows.Forms {
                        
                        switch (fsEntry.FileType) {
                                case FSEntry.FSEntryType.Directory:
-                                       SubItems.Add ("");
+                                       SubItems.Add (String.Empty);
                                        SubItems.Add ("Directory");
                                        SubItems.Add (fsEntry.LastAccessTime.ToShortDateString () + " " + fsEntry.LastAccessTime.ToShortTimeString ()); 
                                        break;
@@ -2567,12 +2599,12 @@ namespace System.Windows.Forms {
                                        SubItems.Add (fsEntry.LastAccessTime.ToShortDateString () + " " + fsEntry.LastAccessTime.ToShortTimeString ()); 
                                        break;
                                case FSEntry.FSEntryType.Device:
-                                       SubItems.Add ("");
+                                       SubItems.Add (String.Empty);
                                        SubItems.Add ("Device");
                                        SubItems.Add (fsEntry.LastAccessTime.ToShortDateString () + " " + fsEntry.LastAccessTime.ToShortTimeString ()); 
                                        break;
                                case FSEntry.FSEntryType.RemovableDevice:
-                                       SubItems.Add ("");
+                                       SubItems.Add (String.Empty);
                                        SubItems.Add ("RemovableDevice");
                                        SubItems.Add (fsEntry.LastAccessTime.ToShortDateString () + " " + fsEntry.LastAccessTime.ToShortTimeString ()); 
                                        break;
@@ -2650,7 +2682,7 @@ namespace System.Windows.Forms {
                        newNameTextBox.Location = new Point (16, 128);
                        newNameTextBox.Size = new Size (200, 20);
                        newNameTextBox.TabIndex = 5;
-                       newNameTextBox.Text = "";
+                       newNameTextBox.Text = String.Empty;
                        
                        // okButton
                        okButton.DialogResult = DialogResult.OK;
@@ -2714,6 +2746,15 @@ namespace System.Windows.Forms {
                
                public static Hashtable MyComputerDevicesPrefix = new Hashtable ();
                
+               public delegate void UpdateDelegate (ArrayList folders, ArrayList files);
+               private UpdateDelegate updateDelegate;
+               private Control calling_control;
+               
+               private ThreadStart get_folder_content_thread_start;
+               private Thread worker;
+               private WorkerThread workerThread = null;
+               private StringCollection the_filters;
+               
                public MWFVFS ()
                {
                        if ((platform == 4) || (platform == 128)) {
@@ -2728,14 +2769,80 @@ namespace System.Windows.Forms {
                        return fileSystem.ChangeDirectory (folder);
                }
                
-               public void GetFolderContent (out ArrayList folders_out, out ArrayList files_out)
+               public void GetFolderContent ()
                {
-                       fileSystem.GetFolderContent (null, out folders_out, out files_out);
+                       GetFolderContent (null);
                }
                
-               public void GetFolderContent (StringCollection filters, out ArrayList folders_out, out ArrayList files_out)
+               public void GetFolderContent (StringCollection filters)
                {
-                       fileSystem.GetFolderContent (filters, out folders_out, out files_out);
+                       the_filters = filters;
+
+                       if (workerThread != null) {
+                               workerThread.Stop ();
+                               workerThread = null;
+                       }
+
+                       // Added next line to ensure the control is created before BeginInvoke is called on it
+                       calling_control.CreateControl();
+                       workerThread = new WorkerThread (fileSystem, the_filters, updateDelegate, calling_control);
+                       
+                       get_folder_content_thread_start = new ThreadStart (workerThread.GetFolderContentThread);
+                       worker = new Thread (get_folder_content_thread_start);
+                       worker.IsBackground = true;
+                       worker.Start();
+               }
+               
+               internal class WorkerThread
+               {
+                       private FileSystem fileSystem;
+                       private StringCollection the_filters;
+                       private UpdateDelegate updateDelegate;
+                       private Control calling_control;
+                       private readonly object lockobject = new object ();
+                       private bool stopped = false;
+                       
+                       public WorkerThread (FileSystem fileSystem, StringCollection the_filters, UpdateDelegate updateDelegate, Control calling_control)
+                       {
+                               this.fileSystem = fileSystem;
+                               this.the_filters = the_filters;
+                               this.updateDelegate = updateDelegate;
+                               this.calling_control = calling_control;
+                       }
+                       
+                       public void GetFolderContentThread()
+                       {
+                               ArrayList folders;
+                               ArrayList files;
+                               
+                               fileSystem.GetFolderContent (the_filters, out folders, out files);
+                               
+                               if (stopped)
+                                       return;
+                               
+                               if (updateDelegate != null) {
+                                       lock (this) {
+                                               object[] objectArray = new object[2];
+                                               
+                                               objectArray[0] = folders;
+                                               objectArray[1] = files;
+                                               
+                                               calling_control.BeginInvoke (updateDelegate, objectArray);
+                                       }
+                               }
+                       }
+                       
+                       public void Stop ()
+                       {
+                               lock (lockobject) {
+                                       stopped = true;
+                               }
+                       }
+               }
+               
+               public ArrayList GetFoldersOnly ()
+               {
+                       return fileSystem.GetFoldersOnly ();
                }
                
                public void WriteRecentlyUsedFiles (string filename)
@@ -2779,15 +2886,22 @@ namespace System.Windows.Forms {
                {
                        return fileSystem.GetParent ();
                }
+               
+               public void RegisterUpdateDelegate(UpdateDelegate updateDelegate, Control control)
+               {
+                       this.updateDelegate = updateDelegate;
+                       calling_control = control;
+               }
        }
        #endregion
        
        #region FileSystem
        internal abstract class FileSystem
        {
-               protected string currentTopFolder = "";
+               protected string currentTopFolder = String.Empty;
                protected FSEntry currentFolderFSEntry = null;
                protected FSEntry currentTopFolderFSEntry = null;
+               private FileInfoComparer fileInfoComparer = new FileInfoComparer ();
                
                public FSEntry ChangeDirectory (string folder)
                {
@@ -2886,34 +3000,104 @@ namespace System.Windows.Forms {
                        }
                }
                
+               public ArrayList GetFoldersOnly ()
+               {
+                       ArrayList directories_out = new ArrayList ();
+                       
+                       if (currentFolderFSEntry.FullName == MWFVFS.DesktopPrefix) {
+                               FSEntry personalFSEntry = GetPersonalFSEntry ();
+                               
+                               directories_out.Add (personalFSEntry);
+                               
+                               FSEntry myComputerFSEntry = GetMyComputerFSEntry ();
+                               
+                               directories_out.Add (myComputerFSEntry);
+                               
+                               FSEntry myNetworkFSEntry = GetMyNetworkFSEntry ();
+                               
+                               directories_out.Add (myNetworkFSEntry);
+                               
+                               ArrayList d_out = GetNormalFolders (ThemeEngine.Current.Places (UIIcon.PlacesDesktop));
+                               directories_out.AddRange (d_out);
+                               
+                       } else
+                       if (currentFolderFSEntry.FullName == MWFVFS.RecentlyUsedPrefix) {
+                               //files_out = GetRecentlyUsedFiles ();
+                       } else
+                       if (currentFolderFSEntry.FullName == MWFVFS.MyComputerPrefix) {
+                               directories_out.AddRange (GetMyComputerContent ());
+                       } else
+                       if (currentFolderFSEntry.FullName == MWFVFS.PersonalPrefix || currentFolderFSEntry.FullName == MWFVFS.MyComputerPersonalPrefix) {
+                               ArrayList d_out = GetNormalFolders (ThemeEngine.Current.Places (UIIcon.PlacesPersonal));
+                               directories_out.AddRange (d_out);
+                       } else
+                       if (currentFolderFSEntry.FullName == MWFVFS.MyNetworkPrefix) {
+                               directories_out.AddRange (GetMyNetworkContent ());
+                       } else {
+                               directories_out = GetNormalFolders (currentFolderFSEntry.FullName);
+                       }
+                       return directories_out;
+               }
+               
                protected void GetNormalFolderContent (string from_folder, StringCollection filters, out ArrayList directories_out, out ArrayList files_out)
                {
                        DirectoryInfo dirinfo = new DirectoryInfo (from_folder);
                        
                        directories_out = new ArrayList ();
                        
-                       DirectoryInfo[] dirs = dirinfo.GetDirectories ();
+                       DirectoryInfo[] dirs = null;
                        
-                       for (int i = 0; i < dirs.Length; i++) {
-                               directories_out.Add (GetDirectoryFSEntry (dirs [i], currentTopFolderFSEntry));
-                       }
+                       try {
+                               dirs = dirinfo.GetDirectories ();
+                       } catch (Exception) {}
+                       
+                       if (dirs != null)
+                               for (int i = 0; i < dirs.Length; i++) {
+                                       directories_out.Add (GetDirectoryFSEntry (dirs [i], currentTopFolderFSEntry));
+                               }
                        
                        files_out = new ArrayList ();
                        
                        ArrayList files = new ArrayList ();
                        
-                       if (filters == null) {
-                               files.AddRange (dirinfo.GetFiles ());
-                       } else {
-                               foreach (string s in filters)
-                                       files.AddRange (dirinfo.GetFiles (s));
-                       }
+                       try {
+                               if (filters == null) {
+                                       files.AddRange (dirinfo.GetFiles ());
+                               } else {
+                                       foreach (string s in filters)
+                                               files.AddRange (dirinfo.GetFiles (s));
+                                       
+                                       files.Sort (fileInfoComparer);
+                               }
+                       } catch (Exception) {}
                        
                        for (int i = 0; i < files.Count; i++) {
-                               files_out.Add (GetFileFSEntry (files [i] as FileInfo));
+                               FSEntry fs = GetFileFSEntry (files [i] as FileInfo);
+                               if (fs != null)
+                                       files_out.Add (fs);
                        }
                }
                
+               protected ArrayList GetNormalFolders (string from_folder)
+               {
+                       DirectoryInfo dirinfo = new DirectoryInfo (from_folder);
+                       
+                       ArrayList directories_out = new ArrayList ();
+                       
+                       DirectoryInfo[] dirs = null;
+                       
+                       try {
+                               dirs = dirinfo.GetDirectories ();
+                       } catch (Exception) {}
+                       
+                       if (dirs != null)
+                               for (int i = 0; i < dirs.Length; i++) {
+                                       directories_out.Add (GetDirectoryFSEntry (dirs [i], currentTopFolderFSEntry));
+                               }
+                       
+                       return directories_out;
+               }
+               
                protected virtual FSEntry GetDirectoryFSEntry (DirectoryInfo dirinfo, FSEntry topFolderFSEntry)
                {
                        FSEntry fs = new FSEntry ();
@@ -2931,6 +3115,11 @@ namespace System.Windows.Forms {
                
                protected virtual FSEntry GetFileFSEntry (FileInfo fileinfo)
                {
+                       // *sigh* FileInfo gives us no usable information for links to directories
+                       // so, return null
+                       if ((fileinfo.Attributes & FileAttributes.Directory) == FileAttributes.Directory)
+                               return null;
+                       
                        FSEntry fs = new FSEntry ();
                        
                        fs.Attributes = fileinfo.Attributes;
@@ -2938,14 +3127,19 @@ namespace System.Windows.Forms {
                        fs.Name = fileinfo.Name;
                        fs.FileType = FSEntry.FSEntryType.File;
                        fs.IconIndex = MimeIconEngine.GetIconIndexForFile (fileinfo.FullName);
+                       fs.FileSize = fileinfo.Length;
                        fs.LastAccessTime = fileinfo.LastAccessTime;
-                       // the following catches broken symbolic links
-                       if ((int)fs.Attributes != 0)
-                               fs.FileSize = fileinfo.Length;
                        
                        return fs;
                }
                
+               internal class FileInfoComparer : IComparer
+               {
+                       public int Compare (object fileInfo1, object fileInfo2)
+                       {
+                               return String.Compare (((FileInfo)fileInfo1).Name, ((FileInfo)fileInfo2).Name);
+                       }
+               }
                
                protected abstract FSEntry GetDesktopFSEntry ();
                
@@ -2980,8 +3174,17 @@ namespace System.Windows.Forms {
                private FSEntry mycomputerFSEntry = null;
                private FSEntry mynetworkFSEntry = null;
                
+               private string personal_folder;
+               private string recently_used_path;
+               private string full_kde_recent_document_dir;
+               
                public UnixFileSystem ()
                {
+                       personal_folder = ThemeEngine.Current.Places (UIIcon.PlacesPersonal);
+                       recently_used_path = Path.Combine (personal_folder, ".recently-used");
+                       
+                       full_kde_recent_document_dir = personal_folder + "/.kde/share/apps/RecentDocuments";
+                       
                        desktopFSEntry = new FSEntry ();
                        
                        desktopFSEntry.Attributes = FileAttributes.Directory;
@@ -2989,7 +3192,7 @@ namespace System.Windows.Forms {
                        desktopFSEntry.Name = "Desktop";
                        desktopFSEntry.RealName = ThemeEngine.Current.Places (UIIcon.PlacesDesktop);
                        desktopFSEntry.FileType = FSEntry.FSEntryType.Directory;
-                       desktopFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("deskop/desktop");
+                       desktopFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("desktop/desktop");
                        desktopFSEntry.LastAccessTime = DateTime.Now;
                        
                        recentlyusedFSEntry = new FSEntry ();
@@ -3046,9 +3249,6 @@ namespace System.Windows.Forms {
                
                public override void WriteRecentlyUsedFiles (string fileToAdd)
                {
-                       string personal_folder = ThemeEngine.Current.Places (UIIcon.PlacesPersonal);
-                       string recently_used_path = Path.Combine (personal_folder, ".recently-used");
-                       
                        if (File.Exists (recently_used_path) && new FileInfo (recently_used_path).Length > 0) {
                                XmlDocument xml_doc = new XmlDocument ();
                                xml_doc.Load (recently_used_path);
@@ -3116,7 +3316,7 @@ namespace System.Windows.Forms {
                                }
                        } else {
                                XmlDocument xml_doc = new XmlDocument ();
-                               xml_doc.AppendChild (xml_doc.CreateXmlDeclaration ("1.0", "", ""));
+                               xml_doc.AppendChild (xml_doc.CreateXmlDeclaration ("1.0", String.Empty, String.Empty));
                                
                                XmlElement recentFiles_element = xml_doc.CreateElement ("RecentFiles");
                                
@@ -3164,8 +3364,6 @@ namespace System.Windows.Forms {
                public override ArrayList GetRecentlyUsedFiles ()
                {
                        // check for GNOME and KDE
-                       string personal_folder = ThemeEngine.Current.Places (UIIcon.PlacesPersonal);
-                       string recently_used_path = Path.Combine (personal_folder, ".recently-used");
                        
                        ArrayList files_al = new ArrayList ();
                        
@@ -3178,8 +3376,11 @@ namespace System.Windows.Forms {
                                                        xtr.Read ();
                                                        Uri uri = new Uri (xtr.Value);
                                                        if (!files_al.Contains (uri.LocalPath))
-                                                               if (File.Exists (uri.LocalPath))
-                                                                       files_al.Add (GetFileFSEntry (new FileInfo (uri.LocalPath)));
+                                                               if (File.Exists (uri.LocalPath)) {
+                                                                       FSEntry fs = GetFileFSEntry (new FileInfo (uri.LocalPath));
+                                                                       if (fs != null)
+                                                                               files_al.Add (fs);
+                                                               }
                                                }
                                        }
                                        xtr.Close ();
@@ -3189,8 +3390,6 @@ namespace System.Windows.Forms {
                        }
                        
                        // KDE
-                       string full_kde_recent_document_dir = personal_folder + "/.kde/share/apps/RecentDocuments";
-                       
                        if (Directory.Exists (full_kde_recent_document_dir)) {
                                string[] files = Directory.GetFiles (full_kde_recent_document_dir, "*.desktop");
                                
@@ -3203,13 +3402,16 @@ namespace System.Windows.Forms {
                                                line = line.Trim ();
                                                
                                                if (line.StartsWith ("URL=")) {
-                                                       line = line.Replace ("URL=", "");
+                                                       line = line.Replace ("URL=", String.Empty);
                                                        line = line.Replace ("$HOME", personal_folder);
                                                        
                                                        Uri uri = new Uri (line);
                                                        if (!files_al.Contains (uri.LocalPath))
-                                                               if (File.Exists (uri.LocalPath))
-                                                                       files_al.Add (GetFileFSEntry (new FileInfo (uri.LocalPath)));
+                                                               if (File.Exists (uri.LocalPath)) {
+                                                                       FSEntry fs = GetFileFSEntry (new FileInfo (uri.LocalPath));
+                                                                       if (fs != null)
+                                                                               files_al.Add (fs);
+                                                               }
                                                        break;
                                                }
                                                
@@ -3381,7 +3583,7 @@ namespace System.Windows.Forms {
                        desktopFSEntry.Name = "Desktop";
                        desktopFSEntry.RealName = ThemeEngine.Current.Places (UIIcon.PlacesDesktop);
                        desktopFSEntry.FileType = FSEntry.FSEntryType.Directory;
-                       desktopFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("deskop/desktop");
+                       desktopFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("desktop/desktop");
                        desktopFSEntry.LastAccessTime = DateTime.Now;
                        
                        recentlyusedFSEntry = new FSEntry ();
@@ -3452,7 +3654,9 @@ namespace System.Windows.Forms {
                        FileInfo[] fileinfos = di.GetFiles ();
                        
                        foreach (FileInfo fi in fileinfos) {
-                               al.Add (GetFileFSEntry (fi));
+                               FSEntry fs = GetFileFSEntry (fi);
+                               if (fs != null)
+                                       al.Add (fs);
                        }
                        
                        return al;
@@ -3860,7 +4064,7 @@ namespace System.Windows.Forms {
                                Mount mount = new Mount ();
                                
                                if (split [0].StartsWith ("/dev/"))
-                                       mount.device_short = split [0].Replace ("/dev/", "");
+                                       mount.device_short = split [0].Replace ("/dev/", String.Empty);
                                else 
                                        mount.device_short = split [0];
                                
@@ -3932,6 +4136,512 @@ namespace System.Windows.Forms {
                }
        }
        #endregion
+               
+       #region MWFConfig
+       // easy to use class to store and read internal MWF config settings.
+       // the config values are stored in the users home dir as a hidden xml file called "mwf_config".
+       // currently supports int, string, byte, color and arrays (including byte arrays)
+       // don't forget, when you read a value you still have to cast this value.
+       //
+       // usage:
+       // MWFConfig.SetValue ("SomeClass", "What", value);
+       // object o = MWFConfig.GetValue ("SomeClass", "What");
+       //
+       // example:
+       // 
+       // string[] configFileNames = (string[])MWFConfig.GetValue ("FileDialog", "FileNames");
+       // MWFConfig.SetValue ("FileDialog", "LastFolder", "/home/user");
+       
+       internal class MWFConfig
+       {
+               private static MWFConfigInstance Instance = new MWFConfigInstance ();
+               
+               private static object lock_object = new object();
+               
+               public static object GetValue (string class_name, string value_name)
+               {
+                       lock (lock_object) {
+                               return Instance.GetValue (class_name, value_name);
+                       }
+               }
+               
+               public static void SetValue (string class_name, string value_name, object value)
+               {
+                       lock (lock_object) {
+                               Instance.SetValue (class_name, value_name, value);
+                       }
+               }
+               
+               public static void Flush ()
+               {
+                       lock (lock_object) {
+                               Instance.Flush ();
+                       }
+               }
+               
+               public static void RemoveClass (string class_name)
+               {
+                       lock (lock_object) {
+                               Instance.RemoveClass (class_name);
+                       }
+               }
+               
+               public static void RemoveClassValue (string class_name, string value_name)
+               {
+                       lock (lock_object) {
+                               Instance.RemoveClassValue (class_name, value_name);
+                       }
+               }
+               
+               public static void RemoveAllClassValues (string class_name)
+               {
+                       lock (lock_object) {
+                               Instance.RemoveAllClassValues (class_name);
+                       }
+               }
+       
+               internal class MWFConfigInstance
+               {
+                       Hashtable classes_hashtable = new Hashtable ();
+                       string path;
+                       static string full_file_name;
+                       XmlTextReader xtr;
+                       XmlTextWriter xtw;
+                       static string default_file_name;
+                       readonly string configName = "MWFConfig";
+                       static int platform = (int) Environment.OSVersion.Platform;
+
+                       static bool IsUnix ()
+                       {
+                               return (platform == 4 || platform == 128);
+                       }
+
+                       static MWFConfigInstance ()
+                       {
+                               string b = "mwf_config";
+                               string dir = Environment.GetFolderPath (Environment.SpecialFolder.Personal);
+                                       
+                               if (IsUnix ()){
+                                       dir = Path.Combine (dir, ".mono");
+                                       try {
+                                               Directory.CreateDirectory (dir);
+                                       } catch {}
+                               } 
+
+                               default_file_name = Path.Combine (dir, b);
+                               full_file_name = default_file_name;
+                       }
+                       
+                       public MWFConfigInstance ()
+                       {
+                               Open (default_file_name);
+                       }
+                       
+                       // only for testing
+                       public MWFConfigInstance (string filename)
+                       {
+                               path = Path.GetDirectoryName (filename);
+                               if (path == null || path == String.Empty) {
+                                       path = Environment.GetFolderPath (Environment.SpecialFolder.Personal);
+                                       
+                                       full_file_name = Path.Combine (path, filename);
+                               }  else 
+                                       full_file_name = filename;
+
+                               Open (full_file_name);
+                       }                               
+                       
+                       ~MWFConfigInstance ()
+                       {
+                               Flush ();
+                       }
+                       
+                       public object GetValue (string class_name, string value_name)
+                       {
+                               ClassEntry class_entry = classes_hashtable [class_name] as ClassEntry;
+                               
+                               if (class_entry != null)
+                                       return class_entry.GetValue (value_name);
+                               
+                               return null;
+                       }
+                       
+                       public void SetValue (string class_name, string value_name, object value)
+                       {
+                               ClassEntry class_entry = classes_hashtable [class_name] as ClassEntry;
+                               
+                               if (class_entry == null) {
+                                       class_entry = new ClassEntry ();
+                                       class_entry.ClassName = class_name;
+                                       classes_hashtable [class_name] = class_entry;
+                               }
+                               
+                               class_entry.SetValue (value_name, value);
+                       }
+                       
+                       private void Open (string filename)
+                       {
+                               try {
+                                       xtr = new XmlTextReader (filename);
+                                       
+                                       ReadConfig ();
+                                       
+                                       xtr.Close ();
+                               } catch (Exception) {
+                               }
+                       }
+                       
+                       public void Flush ()
+                       {
+                               try {
+                                       xtw = new XmlTextWriter (full_file_name, null);
+                                       xtw.Formatting = Formatting.Indented;
+                                       
+                                       WriteConfig ();
+                                       
+                                       xtw.Close ();
+
+                                       if (!IsUnix ())
+                                               File.SetAttributes (full_file_name, FileAttributes.Hidden);
+                               } catch (Exception){
+                               }
+                       }
+                       
+                       public void RemoveClass (string class_name)
+                       {
+                               ClassEntry class_entry = classes_hashtable [class_name] as ClassEntry;
+                               
+                               if (class_entry != null) {
+                                       class_entry.RemoveAllClassValues ();
+                                       
+                                       classes_hashtable.Remove (class_name);
+                               }
+                       }
+                       
+                       public void RemoveClassValue (string class_name, string value_name)
+                       {
+                               ClassEntry class_entry = classes_hashtable [class_name] as ClassEntry;
+                               
+                               if (class_entry != null) {
+                                       class_entry.RemoveClassValue (value_name);
+                               }
+                       }
+                       
+                       public void RemoveAllClassValues (string class_name)
+                       {
+                               ClassEntry class_entry = classes_hashtable [class_name] as ClassEntry;
+                               
+                               if (class_entry != null) {
+                                       class_entry.RemoveAllClassValues ();
+                               }
+                       }
+                       
+                       private void ReadConfig ()
+                       {
+                               if (!CheckForMWFConfig ())
+                                       return;
+                               
+                               while (xtr.Read ()) {
+                                       switch (xtr.NodeType) {
+                                               case XmlNodeType.Element:
+                                                       ClassEntry class_entry = classes_hashtable [xtr.Name] as ClassEntry;
+                                                       
+                                                       if (class_entry == null) {
+                                                               class_entry = new ClassEntry ();
+                                                               class_entry.ClassName = xtr.Name;
+                                                               classes_hashtable [xtr.Name] = class_entry;
+                                                       }
+                                                       
+                                                       class_entry.ReadXml (xtr);
+                                                       break;
+                                       }
+                               }
+                       }
+                       
+                       private bool CheckForMWFConfig ()
+                       {
+                               if (xtr.Read ()) {
+                                       if (xtr.NodeType == XmlNodeType.Element) {
+                                               if (xtr.Name == configName)
+                                                       return true;
+                                       }
+                               }
+                               
+                               return false;
+                       }
+                       
+                       private void WriteConfig ()
+                       {
+                               if (classes_hashtable.Count == 0)
+                                       return;
+                               
+                               xtw.WriteStartElement (configName);
+                               foreach (DictionaryEntry entry in classes_hashtable) {
+                                       ClassEntry class_entry = entry.Value as ClassEntry;
+                                       
+                                       class_entry.WriteXml (xtw);
+                               }
+                               xtw.WriteEndElement ();
+                       }
+                       
+                       internal class ClassEntry
+                       {
+                               private Hashtable classvalues_hashtable = new Hashtable ();
+                               private string className;
+                               
+                               public string ClassName {
+                                       set {
+                                               className = value;
+                                       }
+                                       
+                                       get {
+                                               return className;
+                                       }
+                               }
+                               
+                               public void SetValue (string value_name, object value)
+                               {
+                                       ClassValue class_value = classvalues_hashtable [value_name] as ClassValue;
+                                       
+                                       if (class_value == null) {
+                                               class_value = new ClassValue ();
+                                               class_value.Name = value_name;
+                                               classvalues_hashtable [value_name] = class_value;
+                                       }
+                                       
+                                       class_value.SetValue (value);
+                               }
+                               
+                               public object GetValue (string value_name)
+                               {
+                                       ClassValue class_value = classvalues_hashtable [value_name] as ClassValue;
+                                       
+                                       if (class_value == null) {
+                                               return null;
+                                       }
+                                       
+                                       return class_value.GetValue ();
+                               }
+                               
+                               public void RemoveAllClassValues ()
+                               {
+                                       classvalues_hashtable.Clear ();
+                               }
+                               
+                               public void RemoveClassValue (string value_name)
+                               {
+                                       ClassValue class_value = classvalues_hashtable [value_name] as ClassValue;
+                                       
+                                       if (class_value != null) {
+                                               classvalues_hashtable.Remove (value_name);
+                                       }
+                               }
+                               
+                               public void ReadXml (XmlTextReader xtr)
+                               {
+                                       while (xtr.Read ()) {
+                                               switch (xtr.NodeType) {
+                                                       case XmlNodeType.Element:
+                                                               string name = xtr.GetAttribute ("name");
+                                                               
+                                                               ClassValue class_value = classvalues_hashtable [name] as ClassValue;
+                                                               
+                                                               if (class_value == null) {
+                                                                       class_value = new ClassValue ();
+                                                                       class_value.Name = name;
+                                                                       classvalues_hashtable [name] = class_value;
+                                                               }
+                                                               
+                                                               class_value.ReadXml (xtr);
+                                                               break;
+                                                               
+                                                       case XmlNodeType.EndElement:
+                                                               return;
+                                               }
+                                       }
+                               }
+                               
+                               public void WriteXml (XmlTextWriter xtw)
+                               {
+                                       if (classvalues_hashtable.Count == 0)
+                                               return;
+                                       
+                                       xtw.WriteStartElement (className);
+                                       
+                                       foreach (DictionaryEntry entry in classvalues_hashtable) {
+                                               ClassValue class_value = entry.Value as ClassValue;
+                                               
+                                               class_value.WriteXml (xtw);
+                                       }
+                                       
+                                       xtw.WriteEndElement ();
+                               }
+                       }
+                       
+                       internal class ClassValue
+                       {
+                               private object value;
+                               private string name;
+                               
+                               public string Name {
+                                       set {
+                                               name = value;
+                                       }
+                                       
+                                       get {
+                                               return name;
+                                       }
+                               }
+                               
+                               public void SetValue (object value)
+                               {
+                                       this.value = value;
+                               }
+                               public object GetValue ()
+                               {
+                                       return value;
+                               }
+                               
+                               public void ReadXml (XmlTextReader xtr)
+                               {
+                                       string type;
+                                       string single_value;
+                                       
+                                       type = xtr.GetAttribute ("type");
+                                       
+                                       if (type == "byte_array" || type.IndexOf ("-array") == -1) {
+                                               single_value = xtr.ReadString ();
+                                               
+                                               if (type == "string") {
+                                                       value = single_value;
+                                               } else
+                                               if (type == "int") {
+                                                       value = Int32.Parse (single_value);
+                                               } else
+                                               if (type == "byte") {
+                                                       value = Byte.Parse (single_value);
+                                               } else
+                                               if (type == "color") {
+                                                       int color = Int32.Parse (single_value);
+                                                       value = Color.FromArgb (color);
+                                               } else
+                                               if (type == "byte-array") {
+                                                       byte[] b_array = Convert.FromBase64String (single_value);
+                                                       value = b_array;
+                                               }
+                                       } else {
+                                               ReadXmlArrayValues (xtr, type);
+                                       }
+                               }
+                               
+                               private void ReadXmlArrayValues (XmlTextReader xtr, string type)
+                               {
+                                       ArrayList al = new ArrayList ();
+                                       
+                                       while (xtr.Read ()) {
+                                               switch (xtr.NodeType) {
+                                                       case XmlNodeType.Element:
+                                                               string single_value = xtr.ReadString ();
+                                                               
+                                                               if (type == "int-array") {
+                                                                       int int_val = Int32.Parse (single_value);
+                                                                       al.Add (int_val);
+                                                               } else
+                                                               if (type == "string-array") {
+                                                                       string str_val = single_value;
+                                                                       al.Add (str_val);
+                                                               }
+                                                               break;
+                                                               
+                                                       case XmlNodeType.EndElement:
+                                                               if (xtr.Name == "value") {
+                                                                       if (type == "int-array") {
+                                                                               value = al.ToArray (typeof(int));
+                                                                       } else
+                                                                       if (type == "string-array") {
+                                                                               value = al.ToArray (typeof(string));
+                                                                       } 
+                                                                       return;
+                                                               }
+                                                               break;
+                                               }
+                                       }
+                               }
+                               
+                               public void WriteXml (XmlTextWriter xtw)
+                               {
+                                       xtw.WriteStartElement ("value");
+                                       xtw.WriteAttributeString ("name", name);
+                                       if (value is Array) {
+                                               WriteArrayContent (xtw);
+                                       } else {
+                                               WriteSingleContent (xtw);
+                                       }
+                                       xtw.WriteEndElement ();
+                               }
+                               
+                               private void WriteSingleContent (XmlTextWriter xtw)
+                               {
+                                       string type_string = String.Empty;
+                                       
+                                       if (value is string)
+                                               type_string = "string";
+                                       else
+                                       if (value is int)
+                                               type_string = "int";
+                                       else
+                                       if (value is byte)
+                                               type_string = "byte";
+                                       else
+                                       if (value is Color)
+                                               type_string = "color";
+                                       
+                                       xtw.WriteAttributeString ("type", type_string);
+                                       
+                                       if (value is Color)
+                                               xtw.WriteString (((Color)value).ToArgb ().ToString ());
+                                       else
+                                               xtw.WriteString (value.ToString ());
+                               }
+                               
+                               private void WriteArrayContent (XmlTextWriter xtw)
+                               {
+                                       string type_string = String.Empty;
+                                       string type_name = String.Empty;
+                                       
+                                       if (value is string[]) {
+                                               type_string = "string-array";
+                                               type_name = "string";
+                                       } else
+                                       if (value is int[]) {
+                                               type_string = "int-array";
+                                               type_name = "int";
+                                       } else
+                                       if (value is byte[]) {
+                                               type_string = "byte-array";
+                                               type_name = "byte";
+                                       }
+                                       
+                                       xtw.WriteAttributeString ("type", type_string);
+                                       
+                                       if (type_string != "byte-array") {
+                                               Array array = value as Array;
+                                               
+                                               foreach (object o in array) {
+                                                       xtw.WriteStartElement (type_name);
+                                                       xtw.WriteString (o.ToString ());
+                                                       xtw.WriteEndElement ();
+                                               }
+                                       } else {
+                                               byte[] b_array = value as byte [];
+                                               
+                                               xtw.WriteString (Convert.ToBase64String (b_array, 0, b_array.Length));
+                                       }
+                               }
+                       }
+               }
+       }
+       #endregion
 }