+
+ public event EventHandler ForceDialogEnd {
+ add { on_force_dialog_end += value; }
+ remove { on_force_dialog_end -= value; }
+ }
+ }
+ #endregion
+
+ #region FileListViewItem
+ internal class FileViewListViewItem : ListViewItem
+ {
+ private FSEntry fsEntry;
+
+ public FileViewListViewItem (FSEntry fsEntry)
+ {
+ this.fsEntry = fsEntry;
+
+ ImageIndex = fsEntry.IconIndex;
+
+ Text = fsEntry.Name;
+
+ switch (fsEntry.FileType) {
+ case FSEntry.FSEntryType.Directory:
+ SubItems.Add (String.Empty);
+ SubItems.Add ("Directory");
+ SubItems.Add (fsEntry.LastAccessTime.ToShortDateString () + " " + fsEntry.LastAccessTime.ToShortTimeString ());
+ break;
+ case FSEntry.FSEntryType.File:
+ long fileLen = 1;
+ try {
+ if (fsEntry.FileSize > 1024)
+ fileLen = fsEntry.FileSize / 1024;
+ } catch (Exception) {
+ fileLen = 1;
+ }
+
+ SubItems.Add (fileLen.ToString () + " KB");
+ SubItems.Add ("File");
+ SubItems.Add (fsEntry.LastAccessTime.ToShortDateString () + " " + fsEntry.LastAccessTime.ToShortTimeString ());
+ break;
+ case FSEntry.FSEntryType.Device:
+ SubItems.Add (String.Empty);
+ SubItems.Add ("Device");
+ SubItems.Add (fsEntry.LastAccessTime.ToShortDateString () + " " + fsEntry.LastAccessTime.ToShortTimeString ());
+ break;
+ case FSEntry.FSEntryType.RemovableDevice:
+ SubItems.Add (String.Empty);
+ SubItems.Add ("RemovableDevice");
+ SubItems.Add (fsEntry.LastAccessTime.ToShortDateString () + " " + fsEntry.LastAccessTime.ToShortTimeString ());
+ break;
+ default:
+ break;
+ }
+ }
+
+ public FSEntry FSEntry {
+ set {
+ fsEntry = value;
+ }
+
+ get {
+ return fsEntry;
+ }
+ }
+ }
+ #endregion
+
+ #region IUpdateFolder
+ internal interface IUpdateFolder
+ {
+ string CurrentFolder {get; set;}
+ }
+ #endregion
+
+ #region TextEntryDialog
+ // FIXME: When ListView.LabelEdit is implemented remove me
+ internal class TextEntryDialog : Form
+ {
+ private Label label1;
+ private Button okButton;
+ private TextBox newNameTextBox;
+ private PictureBox iconPictureBox;
+ private Button cancelButton;
+ private GroupBox groupBox1;
+
+ public TextEntryDialog ()
+ {
+ groupBox1 = new GroupBox ();
+ cancelButton = new Button ();
+ iconPictureBox = new PictureBox ();
+ newNameTextBox = new TextBox ();
+ okButton = new Button ();
+ label1 = new Label ();
+ groupBox1.SuspendLayout ();
+ SuspendLayout ();
+
+ // groupBox1
+ groupBox1.Controls.Add (newNameTextBox);
+ groupBox1.Controls.Add (label1);
+ groupBox1.Controls.Add (iconPictureBox);
+ groupBox1.Location = new Point (8, 8);
+ groupBox1.Size = new Size (232, 160);
+ groupBox1.TabIndex = 5;
+ groupBox1.TabStop = false;
+ groupBox1.Text = "New Name";
+
+ // cancelButton
+ cancelButton.DialogResult = DialogResult.Cancel;
+ cancelButton.Location = new Point (168, 176);
+ cancelButton.TabIndex = 4;
+ cancelButton.Text = "Cancel";
+
+ // iconPictureBox
+ iconPictureBox.BorderStyle = BorderStyle.Fixed3D;
+ iconPictureBox.Location = new Point (86, 24);
+ iconPictureBox.Size = new Size (60, 60);
+ iconPictureBox.TabIndex = 3;
+ iconPictureBox.TabStop = false;
+ iconPictureBox.SizeMode = PictureBoxSizeMode.CenterImage;
+
+ // newNameTextBox
+ newNameTextBox.Location = new Point (16, 128);
+ newNameTextBox.Size = new Size (200, 20);
+ newNameTextBox.TabIndex = 5;
+ newNameTextBox.Text = String.Empty;
+
+ // okButton
+ okButton.DialogResult = DialogResult.OK;
+ okButton.Location = new Point (80, 176);
+ okButton.TabIndex = 3;
+ okButton.Text = "OK";
+
+ // label1
+ label1.Location = new Point (16, 96);
+ label1.Size = new Size (200, 23);
+ label1.TabIndex = 4;
+ label1.Text = "Enter Name:";
+ label1.TextAlign = ContentAlignment.MiddleCenter;
+
+ // MainForm
+ AcceptButton = okButton;
+ AutoScaleBaseSize = new Size (5, 13);
+ CancelButton = cancelButton;
+ ClientSize = new Size (248, 205);
+ Controls.Add (groupBox1);
+ Controls.Add (cancelButton);
+ Controls.Add (okButton);
+ FormBorderStyle = FormBorderStyle.FixedDialog;
+ Text = "New Folder or File";
+ groupBox1.ResumeLayout (false);
+ ResumeLayout (false);
+
+ newNameTextBox.Select ();
+ }
+
+ public Image IconPictureBoxImage {
+ set {
+ iconPictureBox.Image = value;
+ }
+ }
+
+ public string FileName {
+ get {
+ return newNameTextBox.Text;
+ }
+ set {
+ newNameTextBox.Text = value;
+ }
+ }
+ }
+ #endregion
+
+ #region MWFVFS
+ internal class MWFVFS
+ {
+ private FileSystem fileSystem;
+
+ private int platform = (int) Environment.OSVersion.Platform;
+
+ public static readonly string DesktopPrefix = "Desktop://";
+ public static readonly string PersonalPrefix = "Personal://";
+ public static readonly string MyComputerPrefix = "MyComputer://";
+ public static readonly string RecentlyUsedPrefix = "RecentlyUsed://";
+ public static readonly string MyNetworkPrefix = "MyNetwork://";
+ public static readonly string MyComputerPersonalPrefix = "MyComputerPersonal://";
+
+ 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)) {
+ fileSystem = new UnixFileSystem ();
+ } else {
+ fileSystem = new WinFileSystem ();
+ }
+ }
+
+ public FSEntry ChangeDirectory (string folder)
+ {
+ return fileSystem.ChangeDirectory (folder);
+ }
+
+ public void GetFolderContent ()
+ {
+ GetFolderContent (null);
+ }
+
+ public void GetFolderContent (StringCollection filters)
+ {
+ 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)
+ {
+ fileSystem.WriteRecentlyUsedFiles (filename);
+ }
+
+ public ArrayList GetRecentlyUsedFiles ()
+ {
+ return fileSystem.GetRecentlyUsedFiles ();
+ }
+
+ public ArrayList GetMyComputerContent ()
+ {
+ return fileSystem.GetMyComputerContent ();
+ }
+
+ public ArrayList GetMyNetworkContent ()
+ {
+ return fileSystem.GetMyNetworkContent ();
+ }
+
+ public bool CreateFolder (string new_folder)
+ {
+ try {
+ if (Directory.Exists (new_folder)) {
+ string message = "Folder \"" + new_folder + "\" already exists.";
+ MessageBox.Show (message, new_folder, MessageBoxButtons.OK, MessageBoxIcon.Warning);
+ return false;
+ } else
+ Directory.CreateDirectory (new_folder);
+ } catch (Exception e) {
+ MessageBox.Show (e.Message, new_folder, MessageBoxButtons.OK, MessageBoxIcon.Error);
+ return false;
+ }
+
+ return true;
+ }
+
+ public string GetParent ()
+ {
+ 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 = String.Empty;
+ protected FSEntry currentFolderFSEntry = null;
+ protected FSEntry currentTopFolderFSEntry = null;
+ private FileInfoComparer fileInfoComparer = new FileInfoComparer ();
+
+ public FSEntry ChangeDirectory (string folder)
+ {
+ if (folder == MWFVFS.DesktopPrefix) {
+ currentTopFolder = MWFVFS.DesktopPrefix;
+ currentTopFolderFSEntry = currentFolderFSEntry = GetDesktopFSEntry ();
+ } else
+ if (folder == MWFVFS.PersonalPrefix) {
+ currentTopFolder = MWFVFS.PersonalPrefix;
+ currentTopFolderFSEntry = currentFolderFSEntry = GetPersonalFSEntry ();
+ } else
+ if (folder == MWFVFS.MyComputerPersonalPrefix) {
+ currentTopFolder = MWFVFS.MyComputerPersonalPrefix;
+ currentTopFolderFSEntry = currentFolderFSEntry = GetMyComputerPersonalFSEntry ();
+ } else
+ if (folder == MWFVFS.RecentlyUsedPrefix) {
+ currentTopFolder = MWFVFS.RecentlyUsedPrefix;
+ currentTopFolderFSEntry = currentFolderFSEntry = GetRecentlyUsedFSEntry ();
+ } else
+ if (folder == MWFVFS.MyComputerPrefix) {
+ currentTopFolder = MWFVFS.MyComputerPrefix;
+ currentTopFolderFSEntry = currentFolderFSEntry = GetMyComputerFSEntry ();
+ } else
+ if (folder == MWFVFS.MyNetworkPrefix) {
+ currentTopFolder = MWFVFS.MyNetworkPrefix;
+ currentTopFolderFSEntry = currentFolderFSEntry = GetMyNetworkFSEntry ();
+ } else {
+ bool found = false;
+
+ foreach (DictionaryEntry entry in MWFVFS.MyComputerDevicesPrefix) {
+ FSEntry fsEntry = entry.Value as FSEntry;
+ if (folder == fsEntry.FullName) {
+ currentTopFolder = entry.Key as string;
+ currentTopFolderFSEntry = currentFolderFSEntry = fsEntry;
+ found = true;
+ break;
+ }
+ }
+
+ if (!found) {
+ currentFolderFSEntry = GetDirectoryFSEntry (new DirectoryInfo (folder), currentTopFolderFSEntry);
+ }
+ }
+
+ return currentFolderFSEntry;
+ }
+
+ public string GetParent ()
+ {
+ return currentFolderFSEntry.Parent;
+ }
+
+ // directories_out and files_out contain FSEntry objects
+ public void GetFolderContent (StringCollection filters, out ArrayList directories_out, out ArrayList files_out)
+ {
+ directories_out = new ArrayList ();
+ files_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 = null;
+ ArrayList f_out = null;
+ GetNormalFolderContent (ThemeEngine.Current.Places (UIIcon.PlacesDesktop), filters, out d_out, out f_out);
+ directories_out.AddRange (d_out);
+ files_out.AddRange (f_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 = null;
+ ArrayList f_out = null;
+ GetNormalFolderContent (ThemeEngine.Current.Places (UIIcon.PlacesPersonal), filters, out d_out, out f_out);
+ directories_out.AddRange (d_out);
+ files_out.AddRange (f_out);
+ } else
+ if (currentFolderFSEntry.FullName == MWFVFS.MyNetworkPrefix) {
+ directories_out.AddRange (GetMyNetworkContent ());
+ } else {
+ GetNormalFolderContent (currentFolderFSEntry.FullName, filters, out directories_out, out files_out);
+ }
+ }
+
+ 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 = 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));
+ }
+
+ files_out = new ArrayList ();
+
+ ArrayList files = new ArrayList ();
+
+ 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++) {
+ 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 ();
+
+ fs.Attributes = dirinfo.Attributes;
+ fs.FullName = dirinfo.FullName;
+ fs.Name = dirinfo.Name;
+ fs.MainTopNode = topFolderFSEntry;
+ fs.FileType = FSEntry.FSEntryType.Directory;
+ fs.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("inode/directory");
+ fs.LastAccessTime = dirinfo.LastAccessTime;
+
+ return fs;
+ }
+
+ 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;
+ fs.FullName = fileinfo.FullName;
+ fs.Name = fileinfo.Name;
+ fs.FileType = FSEntry.FSEntryType.File;
+ fs.IconIndex = MimeIconEngine.GetIconIndexForFile (fileinfo.FullName);
+ fs.FileSize = fileinfo.Length;
+ fs.LastAccessTime = fileinfo.LastAccessTime;
+
+ 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 ();
+
+ protected abstract FSEntry GetRecentlyUsedFSEntry ();
+
+ protected abstract FSEntry GetPersonalFSEntry ();
+
+ protected abstract FSEntry GetMyComputerPersonalFSEntry ();
+
+ protected abstract FSEntry GetMyComputerFSEntry ();
+
+ protected abstract FSEntry GetMyNetworkFSEntry ();
+
+ public abstract void WriteRecentlyUsedFiles (string fileToAdd);
+
+ public abstract ArrayList GetRecentlyUsedFiles ();
+
+ public abstract ArrayList GetMyComputerContent ();
+
+ public abstract ArrayList GetMyNetworkContent ();
+ }
+ #endregion
+
+ #region UnixFileSystem
+ internal class UnixFileSystem : FileSystem
+ {
+ private MasterMount masterMount = new MasterMount ();
+ private FSEntry desktopFSEntry = null;
+ private FSEntry recentlyusedFSEntry = null;
+ private FSEntry personalFSEntry = null;
+ private FSEntry mycomputerpersonalFSEntry = null;
+ 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;
+ desktopFSEntry.FullName = MWFVFS.DesktopPrefix;
+ desktopFSEntry.Name = "Desktop";
+ desktopFSEntry.RealName = ThemeEngine.Current.Places (UIIcon.PlacesDesktop);
+ desktopFSEntry.FileType = FSEntry.FSEntryType.Directory;
+ desktopFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("desktop/desktop");
+ desktopFSEntry.LastAccessTime = DateTime.Now;
+
+ recentlyusedFSEntry = new FSEntry ();
+
+ recentlyusedFSEntry.Attributes = FileAttributes.Directory;
+ recentlyusedFSEntry.FullName = MWFVFS.RecentlyUsedPrefix;
+ recentlyusedFSEntry.Name = "Recently Used";
+ recentlyusedFSEntry.FileType = FSEntry.FSEntryType.Directory;
+ recentlyusedFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("recently/recently");
+ recentlyusedFSEntry.LastAccessTime = DateTime.Now;
+
+ personalFSEntry = new FSEntry ();
+
+ personalFSEntry.Attributes = FileAttributes.Directory;
+ personalFSEntry.FullName = MWFVFS.PersonalPrefix;
+ personalFSEntry.Name = "Personal";
+ personalFSEntry.MainTopNode = GetDesktopFSEntry ();
+ personalFSEntry.RealName = ThemeEngine.Current.Places (UIIcon.PlacesPersonal);
+ personalFSEntry.FileType = FSEntry.FSEntryType.Directory;
+ personalFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("directory/home");
+ personalFSEntry.LastAccessTime = DateTime.Now;
+
+ mycomputerpersonalFSEntry = new FSEntry ();
+
+ mycomputerpersonalFSEntry.Attributes = FileAttributes.Directory;
+ mycomputerpersonalFSEntry.FullName = MWFVFS.MyComputerPersonalPrefix;
+ mycomputerpersonalFSEntry.Name = "Personal";
+ mycomputerpersonalFSEntry.MainTopNode = GetMyComputerFSEntry ();
+ mycomputerpersonalFSEntry.RealName = ThemeEngine.Current.Places (UIIcon.PlacesPersonal);
+ mycomputerpersonalFSEntry.FileType = FSEntry.FSEntryType.Directory;
+ mycomputerpersonalFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("directory/home");
+ mycomputerpersonalFSEntry.LastAccessTime = DateTime.Now;
+
+ mycomputerFSEntry = new FSEntry ();
+
+ mycomputerFSEntry.Attributes = FileAttributes.Directory;
+ mycomputerFSEntry.FullName = MWFVFS.MyComputerPrefix;
+ mycomputerFSEntry.Name = "My Computer";
+ mycomputerFSEntry.MainTopNode = GetDesktopFSEntry ();
+ mycomputerFSEntry.FileType = FSEntry.FSEntryType.Directory;
+ mycomputerFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("workplace/workplace");
+ mycomputerFSEntry.LastAccessTime = DateTime.Now;
+
+ mynetworkFSEntry = new FSEntry ();
+
+ mynetworkFSEntry.Attributes = FileAttributes.Directory;
+ mynetworkFSEntry.FullName = MWFVFS.MyNetworkPrefix;
+ mynetworkFSEntry.Name = "My Network";
+ mynetworkFSEntry.MainTopNode = GetDesktopFSEntry ();
+ mynetworkFSEntry.FileType = FSEntry.FSEntryType.Directory;
+ mynetworkFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("network/network");
+ mynetworkFSEntry.LastAccessTime = DateTime.Now;
+ }
+
+ public override void WriteRecentlyUsedFiles (string fileToAdd)
+ {
+ if (File.Exists (recently_used_path) && new FileInfo (recently_used_path).Length > 0) {
+ XmlDocument xml_doc = new XmlDocument ();
+ xml_doc.Load (recently_used_path);
+
+ XmlNode grand_parent_node = xml_doc.SelectSingleNode ("RecentFiles");
+
+ if (grand_parent_node != null) {
+ // create a new element
+ XmlElement new_recent_item_node = xml_doc.CreateElement ("RecentItem");
+
+ XmlElement new_child = xml_doc.CreateElement ("URI");
+ UriBuilder ub = new UriBuilder ();
+ ub.Path = fileToAdd;
+ ub.Host = null;
+ ub.Scheme = "file";
+ XmlText new_text_child = xml_doc.CreateTextNode (ub.ToString ());
+ new_child.AppendChild (new_text_child);
+
+ new_recent_item_node.AppendChild (new_child);
+
+ new_child = xml_doc.CreateElement ("Mime-Type");
+ new_text_child = xml_doc.CreateTextNode (Mime.GetMimeTypeForFile (fileToAdd));
+ new_child.AppendChild (new_text_child);
+
+ new_recent_item_node.AppendChild (new_child);
+
+ new_child = xml_doc.CreateElement ("Timestamp");
+ long seconds = (long)(DateTime.UtcNow - new DateTime (1970, 1, 1)).TotalSeconds;
+ new_text_child = xml_doc.CreateTextNode (seconds.ToString ());
+ new_child.AppendChild (new_text_child);
+
+ new_recent_item_node.AppendChild (new_child);
+
+ new_child = xml_doc.CreateElement ("Groups");
+
+ new_recent_item_node.AppendChild (new_child);
+
+ // now search the nodes in grand_parent_node for another instance of the new uri and if found remove it
+ // so that the new node is the first one
+ foreach (XmlNode n in grand_parent_node.ChildNodes) {
+ XmlNode uri_node = n.SelectSingleNode ("URI");
+ if (uri_node != null) {
+ XmlNode uri_node_child = uri_node.FirstChild;
+ if (uri_node_child is XmlText)
+ if (ub.ToString () == ((XmlText)uri_node_child).Data) {
+ grand_parent_node.RemoveChild (n);
+ break;
+ }
+ }
+ }
+
+ // prepend the new recent item to the grand parent node list
+ grand_parent_node.PrependChild (new_recent_item_node);
+
+ // limit the # of RecentItems to 10
+ if (grand_parent_node.ChildNodes.Count > 10) {
+ while (grand_parent_node.ChildNodes.Count > 10)
+ grand_parent_node.RemoveChild (grand_parent_node.LastChild);
+ }
+
+ try {
+ xml_doc.Save (recently_used_path);
+ } catch (Exception) {
+ }
+ }
+ } else {
+ XmlDocument xml_doc = new XmlDocument ();
+ xml_doc.AppendChild (xml_doc.CreateXmlDeclaration ("1.0", String.Empty, String.Empty));
+
+ XmlElement recentFiles_element = xml_doc.CreateElement ("RecentFiles");
+
+ XmlElement new_recent_item_node = xml_doc.CreateElement ("RecentItem");
+
+ XmlElement new_child = xml_doc.CreateElement ("URI");
+ UriBuilder ub = new UriBuilder ();
+ ub.Path = fileToAdd;
+ ub.Host = null;
+ ub.Scheme = "file";
+ XmlText new_text_child = xml_doc.CreateTextNode (ub.ToString ());
+ new_child.AppendChild (new_text_child);
+
+ new_recent_item_node.AppendChild (new_child);
+
+ new_child = xml_doc.CreateElement ("Mime-Type");
+ new_text_child = xml_doc.CreateTextNode (Mime.GetMimeTypeForFile (fileToAdd));
+ new_child.AppendChild (new_text_child);
+
+ new_recent_item_node.AppendChild (new_child);
+
+ new_child = xml_doc.CreateElement ("Timestamp");
+ long seconds = (long)(DateTime.UtcNow - new DateTime (1970, 1, 1)).TotalSeconds;
+ new_text_child = xml_doc.CreateTextNode (seconds.ToString ());
+ new_child.AppendChild (new_text_child);
+
+ new_recent_item_node.AppendChild (new_child);
+
+ new_child = xml_doc.CreateElement ("Groups");
+
+ new_recent_item_node.AppendChild (new_child);
+
+ recentFiles_element.AppendChild (new_recent_item_node);
+
+ xml_doc.AppendChild (recentFiles_element);
+
+ try {
+ xml_doc.Save (recently_used_path);
+ } catch (Exception) {
+ }
+ }
+ }
+
+ // return an ArrayList with FSEntry objects
+ public override ArrayList GetRecentlyUsedFiles ()
+ {
+ // check for GNOME and KDE
+
+ ArrayList files_al = new ArrayList ();
+
+ // GNOME
+ if (File.Exists (recently_used_path)) {
+ try {
+ XmlTextReader xtr = new XmlTextReader (recently_used_path);
+ while (xtr.Read ()) {
+ if (xtr.NodeType == XmlNodeType.Element && xtr.Name.ToUpper () == "URI") {
+ xtr.Read ();
+ Uri uri = new Uri (xtr.Value);
+ if (!files_al.Contains (uri.LocalPath))
+ if (File.Exists (uri.LocalPath)) {
+ FSEntry fs = GetFileFSEntry (new FileInfo (uri.LocalPath));
+ if (fs != null)
+ files_al.Add (fs);
+ }
+ }
+ }
+ xtr.Close ();
+ } catch (Exception) {
+
+ }
+ }
+
+ // KDE
+ if (Directory.Exists (full_kde_recent_document_dir)) {
+ string[] files = Directory.GetFiles (full_kde_recent_document_dir, "*.desktop");
+
+ foreach (string file_name in files) {
+ StreamReader sr = new StreamReader (file_name);
+
+ string line = sr.ReadLine ();
+
+ while (line != null) {
+ line = line.Trim ();
+
+ if (line.StartsWith ("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)) {
+ FSEntry fs = GetFileFSEntry (new FileInfo (uri.LocalPath));
+ if (fs != null)
+ files_al.Add (fs);
+ }
+ break;
+ }
+
+ line = sr.ReadLine ();
+ }
+
+ sr.Close ();
+ }
+ }
+
+
+ return files_al;
+ }
+
+ // return an ArrayList with FSEntry objects
+ public override ArrayList GetMyComputerContent ()
+ {
+ ArrayList my_computer_content_arraylist = new ArrayList ();
+
+ if (masterMount.ProcMountAvailable) {
+ masterMount.GetMounts ();
+
+ foreach (MasterMount.Mount mount in masterMount.Block_devices) {
+ FSEntry fsEntry = new FSEntry ();
+ fsEntry.FileType = FSEntry.FSEntryType.Device;
+
+ fsEntry.FullName = mount.mount_point;
+
+ fsEntry.Name = "HDD (" + mount.fsType + ", " + mount.device_short + ")";
+
+ fsEntry.FsType = mount.fsType;
+ fsEntry.DeviceShort = mount.device_short;
+
+ fsEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("harddisk/harddisk");
+
+ fsEntry.Attributes = FileAttributes.Directory;
+
+ fsEntry.MainTopNode = GetMyComputerFSEntry ();
+
+ my_computer_content_arraylist.Add (fsEntry);
+
+ if (!MWFVFS.MyComputerDevicesPrefix.Contains (fsEntry.FullName + "://"))
+ MWFVFS.MyComputerDevicesPrefix.Add (fsEntry.FullName + "://", fsEntry);
+ }
+
+ foreach (MasterMount.Mount mount in masterMount.Removable_devices) {
+ FSEntry fsEntry = new FSEntry ();
+ fsEntry.FileType = FSEntry.FSEntryType.RemovableDevice;
+
+ fsEntry.FullName = mount.mount_point;
+
+ bool is_dvd_cdrom = mount.fsType == MasterMount.FsTypes.usbfs ? false : true;
+ string type_name = is_dvd_cdrom ? "DVD/CD-Rom" : "USB";
+ string mime_type = is_dvd_cdrom ? "cdrom/cdrom" : "removable/removable";
+
+ fsEntry.Name = type_name +" (" + mount.device_short + ")";
+
+ fsEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType (mime_type);
+
+ fsEntry.FsType = mount.fsType;
+ fsEntry.DeviceShort = mount.device_short;
+
+ fsEntry.Attributes = FileAttributes.Directory;
+
+ fsEntry.MainTopNode = GetMyComputerFSEntry ();
+
+ my_computer_content_arraylist.Add (fsEntry);
+
+ string contain_string = fsEntry.FullName + "://";
+ if (!MWFVFS.MyComputerDevicesPrefix.Contains (contain_string))
+ MWFVFS.MyComputerDevicesPrefix.Add (contain_string, fsEntry);
+ }
+ }
+
+ my_computer_content_arraylist.Add (GetMyComputerPersonalFSEntry ());
+
+ return my_computer_content_arraylist;
+ }
+
+ public override ArrayList GetMyNetworkContent ()
+ {
+ ArrayList fsEntries = new ArrayList ();
+
+ foreach (MasterMount.Mount mount in masterMount.Network_devices) {
+ FSEntry fsEntry = new FSEntry ();
+ fsEntry.FileType = FSEntry.FSEntryType.Network;
+
+ fsEntry.FullName = mount.mount_point;
+
+ fsEntry.FsType = mount.fsType;
+ fsEntry.DeviceShort = mount.device_short;
+
+ fsEntry.Name = "Network (" + mount.fsType + ", " + mount.device_short + ")";
+
+ switch (mount.fsType) {
+ case MasterMount.FsTypes.nfs:
+ fsEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("nfs/nfs");
+ break;
+ case MasterMount.FsTypes.smbfs:
+ fsEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("smb/smb");
+ break;
+ case MasterMount.FsTypes.ncpfs:
+ fsEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("network/network");
+ break;
+ case MasterMount.FsTypes.cifs:
+ fsEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("network/network");
+ break;
+ default:
+ break;
+ }
+
+ fsEntry.Attributes = FileAttributes.Directory;
+
+ fsEntry.MainTopNode = GetMyNetworkFSEntry ();
+
+ fsEntries.Add (fsEntry);
+ }
+ return fsEntries;
+ }
+
+ protected override FSEntry GetDesktopFSEntry ()
+ {
+ return desktopFSEntry;
+ }
+
+ protected override FSEntry GetRecentlyUsedFSEntry ()
+ {
+ return recentlyusedFSEntry;
+ }
+
+ protected override FSEntry GetPersonalFSEntry ()
+ {
+ return personalFSEntry;
+ }
+
+ protected override FSEntry GetMyComputerPersonalFSEntry ()
+ {
+ return mycomputerpersonalFSEntry;
+ }
+
+ protected override FSEntry GetMyComputerFSEntry ()
+ {
+ return mycomputerFSEntry;
+ }
+
+ protected override FSEntry GetMyNetworkFSEntry ()
+ {
+ return mynetworkFSEntry;
+ }
+ }
+ #endregion
+
+ #region WinFileSystem
+ internal class WinFileSystem : FileSystem
+ {
+ private FSEntry desktopFSEntry = null;
+ private FSEntry recentlyusedFSEntry = null;
+ private FSEntry personalFSEntry = null;
+ private FSEntry mycomputerpersonalFSEntry = null;
+ private FSEntry mycomputerFSEntry = null;
+ private FSEntry mynetworkFSEntry = null;
+
+ public WinFileSystem ()
+ {
+ desktopFSEntry = new FSEntry ();
+
+ desktopFSEntry.Attributes = FileAttributes.Directory;
+ desktopFSEntry.FullName = MWFVFS.DesktopPrefix;
+ desktopFSEntry.Name = "Desktop";
+ desktopFSEntry.RealName = ThemeEngine.Current.Places (UIIcon.PlacesDesktop);
+ desktopFSEntry.FileType = FSEntry.FSEntryType.Directory;
+ desktopFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("desktop/desktop");
+ desktopFSEntry.LastAccessTime = DateTime.Now;
+
+ recentlyusedFSEntry = new FSEntry ();
+
+ recentlyusedFSEntry.Attributes = FileAttributes.Directory;
+ recentlyusedFSEntry.FullName = MWFVFS.RecentlyUsedPrefix;
+ recentlyusedFSEntry.RealName = ThemeEngine.Current.Places (UIIcon.PlacesRecentDocuments);
+ recentlyusedFSEntry.Name = "Recently Used";
+ recentlyusedFSEntry.FileType = FSEntry.FSEntryType.Directory;
+ recentlyusedFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("recently/recently");
+ recentlyusedFSEntry.LastAccessTime = DateTime.Now;
+
+ personalFSEntry = new FSEntry ();
+
+ personalFSEntry.Attributes = FileAttributes.Directory;
+ personalFSEntry.FullName = MWFVFS.PersonalPrefix;
+ personalFSEntry.Name = "Personal";
+ personalFSEntry.MainTopNode = GetDesktopFSEntry ();
+ personalFSEntry.RealName = ThemeEngine.Current.Places (UIIcon.PlacesPersonal);
+ personalFSEntry.FileType = FSEntry.FSEntryType.Directory;
+ personalFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("directory/home");
+ personalFSEntry.LastAccessTime = DateTime.Now;
+
+ mycomputerpersonalFSEntry = new FSEntry ();
+
+ mycomputerpersonalFSEntry.Attributes = FileAttributes.Directory;
+ mycomputerpersonalFSEntry.FullName = MWFVFS.MyComputerPersonalPrefix;
+ mycomputerpersonalFSEntry.Name = "Personal";
+ mycomputerpersonalFSEntry.MainTopNode = GetMyComputerFSEntry ();
+ mycomputerpersonalFSEntry.RealName = ThemeEngine.Current.Places (UIIcon.PlacesPersonal);
+ mycomputerpersonalFSEntry.FileType = FSEntry.FSEntryType.Directory;
+ mycomputerpersonalFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("directory/home");
+ mycomputerpersonalFSEntry.LastAccessTime = DateTime.Now;
+
+ mycomputerFSEntry = new FSEntry ();
+
+ mycomputerFSEntry.Attributes = FileAttributes.Directory;
+ mycomputerFSEntry.FullName = MWFVFS.MyComputerPrefix;
+ mycomputerFSEntry.Name = "My Computer";
+ mycomputerFSEntry.MainTopNode = GetDesktopFSEntry ();
+ mycomputerFSEntry.FileType = FSEntry.FSEntryType.Directory;
+ mycomputerFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("workplace/workplace");
+ mycomputerFSEntry.LastAccessTime = DateTime.Now;
+
+ mynetworkFSEntry = new FSEntry ();
+
+ mynetworkFSEntry.Attributes = FileAttributes.Directory;
+ mynetworkFSEntry.FullName = MWFVFS.MyNetworkPrefix;
+ mynetworkFSEntry.Name = "My Network";
+ mynetworkFSEntry.MainTopNode = GetDesktopFSEntry ();
+ mynetworkFSEntry.FileType = FSEntry.FSEntryType.Directory;
+ mynetworkFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("network/network");
+ mynetworkFSEntry.LastAccessTime = DateTime.Now;
+ }
+
+ public override void WriteRecentlyUsedFiles (string fileToAdd)
+ {
+ // TODO: Implement this method
+ // use SHAddToRecentDocs ?
+ }
+
+ public override ArrayList GetRecentlyUsedFiles ()
+ {
+ ArrayList al = new ArrayList ();
+
+ DirectoryInfo di = new DirectoryInfo (recentlyusedFSEntry.RealName);
+
+ FileInfo[] fileinfos = di.GetFiles ();
+
+ foreach (FileInfo fi in fileinfos) {
+ FSEntry fs = GetFileFSEntry (fi);
+ if (fs != null)
+ al.Add (fs);
+ }
+
+ return al;
+ }
+
+ public override ArrayList GetMyComputerContent ()
+ {
+ string[] logical_drives = Directory.GetLogicalDrives ();
+
+ ArrayList my_computer_content_arraylist = new ArrayList ();
+
+ foreach (string drive in logical_drives) {
+ FSEntry fsEntry = new FSEntry ();
+ fsEntry.FileType = FSEntry.FSEntryType.Device;
+
+ fsEntry.FullName = drive;
+
+ fsEntry.Name = drive;
+
+ fsEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("harddisk/harddisk");
+
+ fsEntry.Attributes = FileAttributes.Directory;
+
+ fsEntry.MainTopNode = GetMyComputerFSEntry ();
+
+ my_computer_content_arraylist.Add (fsEntry);
+
+ string contain_string = fsEntry.FullName + "://";
+ if (!MWFVFS.MyComputerDevicesPrefix.Contains (contain_string))
+ MWFVFS.MyComputerDevicesPrefix.Add (contain_string, fsEntry);
+ }
+
+ my_computer_content_arraylist.Add (GetMyComputerPersonalFSEntry ());
+
+ return my_computer_content_arraylist;
+ }
+
+ public override ArrayList GetMyNetworkContent ()
+ {
+ // TODO: Implement this method
+ return new ArrayList ();
+ }
+ protected override FSEntry GetDesktopFSEntry ()
+ {
+ return desktopFSEntry;
+ }
+
+ protected override FSEntry GetRecentlyUsedFSEntry ()
+ {
+ return recentlyusedFSEntry;
+ }
+
+ protected override FSEntry GetPersonalFSEntry ()
+ {
+ return personalFSEntry;
+ }
+
+ protected override FSEntry GetMyComputerPersonalFSEntry ()
+ {
+ return mycomputerpersonalFSEntry;
+ }
+
+ protected override FSEntry GetMyComputerFSEntry ()
+ {
+ return mycomputerFSEntry;
+ }
+
+ protected override FSEntry GetMyNetworkFSEntry ()
+ {
+ return mynetworkFSEntry;
+ }
+ }
+ #endregion
+
+ #region FSEntry
+ internal class FSEntry
+ {
+ public enum FSEntryType
+ {
+ Desktop,
+ RecentlyUsed,
+ MyComputer,
+ File,
+ Directory,
+ Device,
+ RemovableDevice,
+ Network
+ }
+
+ private MasterMount.FsTypes fsType;
+ private string device_short;
+ private string fullName;
+ private string name;
+ private string realName = null;
+ private FileAttributes attributes = FileAttributes.Normal;
+ private long fileSize;
+ private FSEntryType fileType;
+ private DateTime lastAccessTime;
+ private FSEntry mainTopNode = null;
+
+ private int iconIndex;
+
+ private string parent;
+
+ public MasterMount.FsTypes FsType {
+ set {
+ fsType = value;
+ }
+
+ get {
+ return fsType;
+ }
+ }
+
+ public string DeviceShort {
+ set {
+ device_short = value;
+ }
+
+ get {
+ return device_short;
+ }
+ }
+
+ public string FullName {
+ set {
+ fullName = value;
+ }
+
+ get {
+ return fullName;
+ }
+ }
+
+ public string Name {
+ set {
+ name = value;
+ }
+
+ get {
+ return name;
+ }
+ }
+
+ public string RealName {
+ set {
+ realName = value;
+ }
+
+ get {
+ return realName;
+ }
+ }
+
+ public FileAttributes Attributes {
+ set {
+ attributes = value;
+ }
+
+ get {
+ return attributes;
+ }
+ }
+
+ public long FileSize {
+ set {
+ fileSize = value;
+ }
+
+ get {
+ return fileSize;
+ }
+ }
+
+ public FSEntryType FileType {
+ set {
+ fileType = value;
+ }
+
+ get {
+ return fileType;
+ }
+ }
+
+ public DateTime LastAccessTime {
+ set {
+ lastAccessTime = value;
+ }
+
+ get {
+ return lastAccessTime;
+ }
+ }
+
+ public int IconIndex {
+ set {
+ iconIndex = value;
+ }
+
+ get {
+ return iconIndex;
+ }
+ }
+
+ public FSEntry MainTopNode {
+ set {
+ mainTopNode = value;
+ }
+
+ get {
+ return mainTopNode;
+ }
+ }
+
+ public string Parent {
+ set {
+ parent = value;
+ }
+
+ get {
+ parent = GetParent ();
+
+ return parent;
+ }
+ }
+
+ private string GetParent ()
+ {
+ if (fullName == MWFVFS.PersonalPrefix) {
+ return MWFVFS.DesktopPrefix;
+ } else
+ if (fullName == MWFVFS.MyComputerPersonalPrefix) {
+ return MWFVFS.MyComputerPrefix;
+ } else
+ if (fullName == MWFVFS.MyComputerPrefix) {
+ return MWFVFS.DesktopPrefix;
+ } else
+ if (fullName == MWFVFS.MyNetworkPrefix) {
+ return MWFVFS.DesktopPrefix;
+ } else
+ if (fullName == MWFVFS.DesktopPrefix) {
+ return null;
+ } else
+ if (fullName == MWFVFS.RecentlyUsedPrefix) {
+ return null;
+ } else {
+ foreach (DictionaryEntry entry in MWFVFS.MyComputerDevicesPrefix) {
+ FSEntry fsEntry = entry.Value as FSEntry;
+ if (fullName == fsEntry.FullName) {
+ return fsEntry.MainTopNode.FullName;
+ }
+ }
+
+ DirectoryInfo dirInfo = new DirectoryInfo (fullName);
+
+ DirectoryInfo dirInfoParent = dirInfo.Parent;
+
+ if (dirInfoParent != null) {
+ FSEntry fsEntry = MWFVFS.MyComputerDevicesPrefix [dirInfoParent.FullName + "://"] as FSEntry;
+
+ if (fsEntry != null) {
+ return fsEntry.FullName;
+ }
+
+ if (mainTopNode != null) {
+ if (dirInfoParent.FullName == ThemeEngine.Current.Places (UIIcon.PlacesDesktop) &&
+ mainTopNode.FullName == MWFVFS.DesktopPrefix) {
+ return mainTopNode.FullName;
+ } else
+ if (dirInfoParent.FullName == ThemeEngine.Current.Places (UIIcon.PlacesPersonal) &&
+ mainTopNode.FullName == MWFVFS.PersonalPrefix) {
+ return mainTopNode.FullName;
+ } else
+ if (dirInfoParent.FullName == ThemeEngine.Current.Places (UIIcon.PlacesPersonal) &&
+ mainTopNode.FullName == MWFVFS.MyComputerPersonalPrefix) {
+ return mainTopNode.FullName;
+ }
+ }
+
+ return dirInfoParent.FullName;
+ }
+ }
+
+ return null;
+ }
+ }
+ #endregion
+
+ #region MasterMount
+ // Alexsantas little *nix helper
+ internal class MasterMount
+ {
+ // add more...
+ internal enum FsTypes
+ {
+ none,
+ ext2,
+ ext3,
+ hpfs,
+ iso9660,
+ jfs,
+ minix,
+ msdos,
+ ntfs,
+ reiserfs,
+ ufs,
+ umsdos,
+ vfat,
+ sysv,
+ xfs,
+ ncpfs,
+ nfs,
+ smbfs,
+ usbfs,
+ cifs
+ }
+
+ internal struct Mount
+ {
+ public string device_or_filesystem;
+ public string device_short;
+ public string mount_point;
+ public FsTypes fsType;
+ }
+
+ bool proc_mount_available = false;
+
+ ArrayList block_devices = new ArrayList ();
+ ArrayList network_devices = new ArrayList ();
+ ArrayList removable_devices = new ArrayList ();
+
+ private int platform = (int) Environment.OSVersion.Platform;
+ private MountComparer mountComparer = new MountComparer ();
+
+ public MasterMount ()
+ {
+ // maybe check if the current user can access /proc/mounts
+ if ((platform == 4) || (platform == 128))
+ if (File.Exists ("/proc/mounts"))
+ proc_mount_available = true;
+ }
+
+ public ArrayList Block_devices {
+ get {
+ return block_devices;
+ }
+ }
+
+ public ArrayList Network_devices {
+ get {
+ return network_devices;
+ }
+ }
+
+ public ArrayList Removable_devices {
+ get {
+ return removable_devices;
+ }
+ }
+
+ public bool ProcMountAvailable {
+ get {
+ return proc_mount_available;
+ }
+ }
+
+ public void GetMounts ()
+ {
+ if (!proc_mount_available)
+ return;
+
+ block_devices.Clear ();
+ network_devices.Clear ();
+ removable_devices.Clear ();
+
+ try {
+ StreamReader sr = new StreamReader ("/proc/mounts");
+
+ string line = sr.ReadLine ();
+
+ ArrayList lines = new ArrayList ();
+ while (line != null) {
+ if (lines.IndexOf (line) == -1) { // Avoid duplicates
+ ProcessProcMountLine (line);
+ lines.Add (line);
+ }
+ line = sr.ReadLine ();
+ }
+
+ sr.Close ();
+
+ block_devices.Sort (mountComparer);
+ network_devices.Sort (mountComparer);
+ removable_devices.Sort (mountComparer);
+ } catch {
+ // bla
+ }
+ }
+
+ private void ProcessProcMountLine (string line)
+ {
+ string[] split = line.Split (new char [] {' '});
+
+ if (split != null && split.Length > 0) {
+ Mount mount = new Mount ();
+
+ if (split [0].StartsWith ("/dev/"))
+ mount.device_short = split [0].Replace ("/dev/", String.Empty);
+ else
+ mount.device_short = split [0];
+
+ mount.device_or_filesystem = split [0];
+ mount.mount_point = split [1];
+
+ // TODO: other removable devices, floppy
+ // ssh
+
+ // network mount
+ if (split [2] == "nfs") {
+ mount.fsType = FsTypes.nfs;
+ network_devices.Add (mount);
+ } else if (split [2] == "smbfs") {
+ mount.fsType = FsTypes.smbfs;
+ network_devices.Add (mount);
+ } else if (split [2] == "cifs") {
+ mount.fsType = FsTypes.cifs;
+ network_devices.Add (mount);
+ } else if (split [2] == "ncpfs") {
+ mount.fsType = FsTypes.ncpfs;
+ network_devices.Add (mount);
+
+ } else if (split [2] == "iso9660") { //cdrom
+ mount.fsType = FsTypes.iso9660;
+ removable_devices.Add (mount);
+ } else if (split [2] == "usbfs") { //usb ? not tested
+ mount.fsType = FsTypes.usbfs;
+ removable_devices.Add (mount);
+
+ } else if (split [0].StartsWith ("/")) { //block devices
+ if (split [1].StartsWith ("/dev/")) // root static, do not add
+ return;
+
+ if (split [2] == "ext2")
+ mount.fsType = FsTypes.ext2;
+ else if (split [2] == "ext3")
+ mount.fsType = FsTypes.ext3;
+ else if (split [2] == "reiserfs")
+ mount.fsType = FsTypes.reiserfs;
+ else if (split [2] == "xfs")
+ mount.fsType = FsTypes.xfs;
+ else if (split [2] == "vfat")
+ mount.fsType = FsTypes.vfat;
+ else if (split [2] == "ntfs")
+ mount.fsType = FsTypes.ntfs;
+ else if (split [2] == "msdos")
+ mount.fsType = FsTypes.msdos;
+ else if (split [2] == "umsdos")
+ mount.fsType = FsTypes.umsdos;
+ else if (split [2] == "hpfs")
+ mount.fsType = FsTypes.hpfs;
+ else if (split [2] == "minix")
+ mount.fsType = FsTypes.minix;
+ else if (split [2] == "jfs")
+ mount.fsType = FsTypes.jfs;
+
+ block_devices.Add (mount);
+ }
+ }
+ }
+
+ public class MountComparer : IComparer
+ {
+ public int Compare (object mount1, object mount2)
+ {
+ return String.Compare (((Mount)mount1).device_short, ((Mount)mount2).device_short);
+ }
+ }
+ }
+ #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));
+ }
+ }
+ }
+ }