[Monodevelop-patches-list] r1984 - in trunk/MonoDevelop/Core/src/Main/Base: . Services/File

commit-watcher at mono-cvs.ximian.com commit-watcher at mono-cvs.ximian.com
Wed Oct 20 00:11:43 EDT 2004


Author: jluke
Date: 2004-10-20 00:11:43 -0400 (Wed, 20 Oct 2004)
New Revision: 1984

Added:
   trunk/MonoDevelop/Core/src/Main/Base/Services/File/RecentFiles.cs
Removed:
   trunk/MonoDevelop/Core/src/Main/Base/Services/File/FdoRecentFiles.cs
   trunk/MonoDevelop/Core/src/Main/Base/Services/File/RecentItem.cs
   trunk/MonoDevelop/Core/src/Main/Base/Services/File/RingBuffer.cs
Modified:
   trunk/MonoDevelop/Core/src/Main/Base/ChangeLog
   trunk/MonoDevelop/Core/src/Main/Base/Makefile.am
Log:
2004-10-20  John Luke  <john.luke at gmail.com>

        * Makefile.am: remove RingBuffer, add RecentFiles (not used yet)
        * Services/File/FdoRecentFiles.cs
        * Services/File/RecentItem.cs
        * Services/File/RingBuffer.cs: remove old unused stuff
        * Services/File/RecentFiles.cs: new in-progress implementation

not enabled, it shouldnt effect anything



Modified: trunk/MonoDevelop/Core/src/Main/Base/ChangeLog
===================================================================
--- trunk/MonoDevelop/Core/src/Main/Base/ChangeLog	2004-10-18 00:02:37 UTC (rev 1983)
+++ trunk/MonoDevelop/Core/src/Main/Base/ChangeLog	2004-10-20 04:11:43 UTC (rev 1984)
@@ -1,3 +1,11 @@
+2004-10-20  John Luke  <john.luke at gmail.com>
+
+	* Makefile.am: remove RingBuffer, add RecentFiles (not used yet)
+	* Services/File/FdoRecentFiles.cs
+	* Services/File/RecentItem.cs
+	* Services/File/RingBuffer.cs: remove old unused stuff
+	* Services/File/RecentFiles.cs: new in-progress implementation
+
 2004-10-15  Todd Berman  <tberman at off.net>
 
 	* Internal/Project/Combine/CombineEntry.cs: Before running, copy

Modified: trunk/MonoDevelop/Core/src/Main/Base/Makefile.am
===================================================================
--- trunk/MonoDevelop/Core/src/Main/Base/Makefile.am	2004-10-18 00:02:37 UTC (rev 1983)
+++ trunk/MonoDevelop/Core/src/Main/Base/Makefile.am	2004-10-20 04:11:43 UTC (rev 1984)
@@ -170,7 +170,7 @@
 ./Services/File/IFileService.cs \
 ./Services/File/DefaultFileService.cs \
 ./Services/File/FileEventArgs.cs \
-./Services/File/RingBuffer.cs \
+./Services/File/RecentFiles.cs \
 ./Services/File/RecentOpen.cs \
 ./Services/MenuService/MenuService.cs \
 ./Services/DisplayBinding/DisplayBindingService.cs \

Deleted: trunk/MonoDevelop/Core/src/Main/Base/Services/File/FdoRecentFiles.cs
===================================================================
--- trunk/MonoDevelop/Core/src/Main/Base/Services/File/FdoRecentFiles.cs	2004-10-18 00:02:37 UTC (rev 1983)
+++ trunk/MonoDevelop/Core/src/Main/Base/Services/File/FdoRecentFiles.cs	2004-10-20 04:11:43 UTC (rev 1984)
@@ -1,151 +0,0 @@
-//
-// Author: John Luke  <jluke at cfl.rr.com>
-//
-// Copyright 2004 John Luke
-//
-
-using System;
-using System.Collections;
-using System.IO;
-using System.Xml;
-using System.Xml.XPath;
-
-// implementation of the freedesktop.org Recent Files spec
-// http://freedesktop.org/Standards/recent-file-spec/recent-file-spec-0.2.html
-
-namespace MonoDevelop.Services
-{
-	public class FdoRecentFiles
-	{
-		// MD only wants to save last 10 in its group
-        ArrayList lastFiles = new ArrayList (10); // max 10
-        ArrayList lastProjects = new ArrayList (10); // max 10
-        ArrayList allRecents = new ArrayList (10); // max 500
-
-		XPathDocument doc;
-
-		public event EventHandler RecentFileChanged;
-        public event EventHandler RecentProjectChanged;
-
-		public FdoRecentFiles ()
-		{
-			// The document should be stored in "~/.recently-used",
-			string recentFile = Path.Combine (Environment.GetEnvironmentVariable ("HOME"), ".recently_used");
-			//Console.WriteLine (recentFile);
-
-			if (File.Exists (recentFile))
-			{
-				// use POSIX lockf ()
-				doc = new XPathDocument (recentFile);
-
-				XPathNavigator nav = doc.CreateNavigator ();
-				XPathNodeIterator xni = nav.Select ("/RecentFiles/RecentItem");
-				Console.WriteLine ("Total Items {0}", xni.Count);
-			}
-			else
-			{
-				// use POSIX lockf ()
-				Console.WriteLine ("{0} does not exist.", recentFile);
-				// create it
-			}
-
-			//FileSystemWatcher watcher = new FileSystemWatcher (recentFile);
-			//watcher.Changed += new FileSystemEventHandler (OnWatcherChanged);
-		}
-
-		void OnWatcherChanged (object o, FileSystemEventArgs args)
-		{
-			// TODO
-			// decide if projects or files changed or both
-			Console.WriteLine ("on watcher changed");
-		}
-
-		void OnRecentFileChange ()
-        {
-            if (RecentFileChanged != null)
-			{
-                RecentFileChanged (this, null);
-            }
-        }
-                                                                       
-        void OnRecentProjectChange ()
-        {
-            if (RecentProjectChanged != null)
-			{
-                RecentProjectChanged (this, null);
-            }
-        }
-
-		public ArrayList RecentFiles
-		{
-            get
-			{
-				return lastFiles;
-            }
-        }
-                                                                       
-        public ArrayList RecentProjects
-		{
-            get
-			{
-                return lastProjects;
-            }
-        }
-
-		// new entries seem to go on top
-		// but it is not explicitly stated as so
-		public void AddFile (string file_uri)
-		{
-			// uri must be unique
-			// or just update timestamp and group
-			foreach (RecentItem recentItem in allRecents)
-			{
-				if (recentItem.Uri == file_uri)
-				{
-					recentItem.Update (false);
-					lastFiles.Add (recentItem);
-					return;
-				}
-			}
-
-			RecentItem ri = new RecentItem (file_uri);
-			ri.Group = "MonoDevelop Files";
-			lastFiles.Add (ri);
-		}
-
-		public void AddProject (string file_uri)
-		{
-			// uri must be unique
-			// or just update timestamp and group
-			foreach (RecentItem recentItem in allRecents)
-			{
-				if (recentItem.Uri == file_uri)
-				{
-					recentItem.Update (true);
-					lastProjects.Add (recentItem);
-					return;
-				}
-			}
-
-			RecentItem ri = new RecentItem (file_uri);
-			ri.Group = "MonoDevelop Projects";
-			lastProjects.Add (ri);
-		}
-
-		// spec doesn't mention removal
-		public void ClearFiles ()
-		{
-			lastFiles.Clear ();
-			// remove from allRecents
-			// write the file
-		}
-
-		// spec doesn't mention removal
-		public void ClearProjects ()
-		{
-			lastProjects.Clear ();
-			// remove from allRecents
-			// write the file
-		}
-	}
-}

Added: trunk/MonoDevelop/Core/src/Main/Base/Services/File/RecentFiles.cs
===================================================================
--- trunk/MonoDevelop/Core/src/Main/Base/Services/File/RecentFiles.cs	2004-10-18 00:02:37 UTC (rev 1983)
+++ trunk/MonoDevelop/Core/src/Main/Base/Services/File/RecentFiles.cs	2004-10-20 04:11:43 UTC (rev 1984)
@@ -0,0 +1,356 @@
+/*
+Copyright (c) 2004 John Luke
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the "Software"), to deal in
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
+of the Software, and to permit persons to whom the Software is furnished to do
+so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+*/
+
+using System;
+using System.Collections;
+using System.IO;
+using System.Text;
+using System.Xml;
+using System.Xml.Serialization;
+
+// implementation of the freedesktop.org Recent Files spec
+// http://freedesktop.org/Standards/recent-file-spec/recent-file-spec-0.2.html
+
+namespace Freedesktop.RecentFiles
+{
+	// FIXME: make sure locking is ok
+	// ex. should we survive ctl-c in middle of read/write
+	// do we fail gracefully if someone else has a lock
+    public class RecentFiles
+	{
+		private static XmlSerializer serializer;
+
+		// expose this so consumers can watch it with FileSystemWatcher for changes
+		public static readonly string RecentFileStore = Path.Combine (Environment.GetFolderPath (Environment.SpecialFolder.Personal), ".recently-used");
+
+		static RecentFiles ()
+		{
+			serializer = new XmlSerializer (typeof (RecentFiles));
+		}
+
+		// required by serializer
+		public RecentFiles ()
+		{
+		}
+
+		// currently only emits on our changes
+		// we should probably use FSW and send those events here
+		// for now you have to do it on your own
+		public event EventHandler Changed;
+
+        [XmlElement ("RecentItem")]
+        public RecentItem[] RecentItems;
+
+		// FIXME: maybe not write until Save at the End
+		public void AddItem (RecentItem item)
+		{
+			if (RecentItems == null)
+			{
+				RecentItems = new RecentItem [] {item};
+				Save ();
+				return;
+			}
+
+			// check for already existing URI
+			// if it does update timestamp and return unchanged;
+			// FIXME: but, what about new Groups? private changing?
+			foreach (RecentItem ri in RecentItems)
+			{
+				if (item.Uri == ri.Uri)
+				{
+					ri.Timestamp = item.Timestamp;
+					Save ();
+					return;
+				}
+			}
+
+			int count = RecentItems.Length;
+			RecentItem[] newItems;
+			if (count < 500)
+			{
+				newItems = new RecentItem[count + 1];
+				RecentItems.CopyTo (newItems, 0);
+				newItems[count + 1] = item;
+			}
+			else
+			{
+				// FIXME: crashes
+				newItems = new RecentItem[500];
+				// here we chop off the beginning (oldest)
+				Array.Copy (RecentItems, count - 500, newItems, 0, 499);
+				newItems[500] = item;
+			}
+
+			RecentItems = newItems;
+			Save ();
+		}
+
+		public void Clear ()
+		{
+			RecentItems = new RecentItem [0];
+			Save ();
+		}
+
+		public void ClearGroup (string group)
+		{
+			if (this.RecentItems == null)
+				return;
+
+			ArrayList list = new ArrayList ();
+			foreach (RecentItem ri in RecentItems)
+			{
+				// FIXME: needs to Handle 2 groups becoming 1 group
+				foreach (string g in ri.Groups)
+				{
+					if (g != group)
+						list.Add (ri);
+				}
+			}
+
+			RecentItem[] items = new RecentItem [list.Count];
+			list.CopyTo (items);
+			RecentItems = items;
+			Save ();
+		}
+
+		private void ClearMissing ()
+		{
+			ArrayList list = new ArrayList ();
+			foreach (RecentItem ri in RecentItems)
+			{
+				// we cant test !file:// Uris can we?
+				if (!ri.Uri.StartsWith ("file://"))
+					list.Add (ri);
+				else if (File.Exists (ri.Uri.Substring (7)))
+					list.Add (ri);
+			}
+
+			RecentItem[] items = new RecentItem [list.Count];
+			list.CopyTo (items);
+			RecentItems = items;
+			Save ();
+		}
+
+		private void EmitChangedEvent ()
+		{
+			if (Changed != null)
+				Changed (this, EventArgs.Empty);	
+		}
+
+		public static RecentFiles GetInstance ()
+		{
+			try
+			{
+				XmlTextReader reader = new XmlTextReader (RecentFileStore);
+				RecentFiles rf = (RecentFiles) serializer.Deserialize (reader);
+				reader.Close ();
+				return rf;
+			}
+			catch (IOException e)
+			{
+				// FIXME: this is wrong, because if we later save it, we blow away what was there
+				// somehow we should ask for the lock or wait for it...
+				return new RecentFiles ();
+			}
+			catch
+			{
+				// FIXME: this is wrong, because if we later save it, we blow away what was there
+				return new RecentFiles ();
+			}
+		}
+
+		public RecentItem[] GetItemsInGroup (string group)
+		{
+			if (RecentItems == null)
+				return null;
+
+			ArrayList list = new ArrayList ();
+			// disable for now
+			//ClearMissing ();
+
+			foreach (RecentItem ri in RecentItems)
+			{
+				foreach (string g in ri.Groups)
+				{
+					if (g == group)
+						list.Add (ri);
+				}
+			}
+
+			RecentItem[] items = new RecentItem [list.Count];
+			list.CopyTo (items);
+			return items;
+		}
+
+		public void RemoveItem (string uri)
+		{
+			ArrayList list = new ArrayList ();
+			foreach (RecentItem ri in RecentItems)
+			{
+				if (ri.Uri != uri)
+					list.Add (ri);
+			}
+
+			RecentItem[] items = new RecentItem [list.Count];
+			list.CopyTo (items);
+			RecentItems = items;
+			Save ();
+		}
+
+		public void RenameItem (string oldUri, string newUri)
+		{
+			foreach (RecentItem ri in RecentItems)
+			{
+				if (ri.Uri == oldUri)
+				{
+					ri.Uri = newUri;
+					ri.Timestamp = RecentItem.NewTimestamp;
+					Save ();
+					return;
+				}
+			}
+		}
+
+		// Save implies EmitChangedEvent (otherwise why would we save?)
+		private void Save ()
+		{
+			// if we specifically set Encoding UTF 8 here it writes the BOM
+			// which confuses others (egg-recent-files) I guess
+			XmlTextWriter writer = new XmlTextWriter (new StreamWriter (RecentFileStore));
+			writer.Formatting = Formatting.Indented;
+			serializer.Serialize (writer, this);
+			writer.Close ();
+			EmitChangedEvent ();
+		}
+
+		public override string ToString ()
+		{
+			if (this.RecentItems == null)
+				return "0 recent files";
+
+			StringBuilder sb = new StringBuilder ();
+			foreach (RecentItem ri in this.RecentItems)
+			{
+				sb.Append (ri.Uri);
+				sb.Append (" ");
+				sb.Append (ri.MimeType);
+				sb.Append (" ");
+				sb.Append (ri.Timestamp);
+				sb.Append ("\n");
+			}
+			sb.Append (RecentItems.Length);
+			sb.Append (" total recent files\n");
+			return sb.ToString ();
+		}
+    }
+
+    public class RecentItem
+	{
+        [XmlElement ("URI")]
+        public string Uri;
+
+        [XmlElement ("Mime-Type")]
+        public string MimeType;
+
+        public string Timestamp;
+
+        public string Private;
+
+        [System.Xml.Serialization.XmlArrayItem(ElementName="Group",IsNullable=false)]
+        public string[] Groups;
+
+		// required by serialization
+		public RecentItem ()
+		{
+		}
+
+		public RecentItem (string uri, string mimetype) : this (uri, mimetype, null)
+		{
+		}
+
+		public RecentItem (string uri, string mimetype, string group)
+		{
+			Uri = uri;
+			MimeType = mimetype;
+			Timestamp = NewTimestamp;
+
+			if (group != null)
+			{
+				this.Groups = new string[] {group};
+			}
+		}
+
+		public void AddGroup (string group)
+		{
+			if (this.Groups == null)
+			{
+				Groups = new string[] {group};
+				return;
+			}
+
+			// if it already has this group no need to add it
+			foreach (string g in Groups)
+			{
+				if (g == group)
+					return;
+			}
+
+			int length = this.Groups.Length + 1;
+			string[] groups = new string [length];
+			this.Groups.CopyTo (groups, 0);
+			groups[length] = group;
+		}
+
+		public static string NewTimestamp
+		{
+			get {
+				// from the unix epoch
+				return ((int) (DateTime.UtcNow - new DateTime (1970, 1, 1, 0, 0, 0, 0)).TotalSeconds).ToString ();
+			}
+		}
+
+		// FIXME
+		public void RemoveGroup (string group)
+		{
+			if (this.Groups == null)
+				return;
+
+			ArrayList groups = new ArrayList ();
+			foreach (string g in Groups)
+			{
+				if (g != group)
+					groups.Add (g);
+			}
+
+			string[] newGroups = new string [groups.Count];
+			groups.CopyTo (newGroups, 0);
+			this.Groups = newGroups;
+		}
+
+		// some apps might depend on this, even though they shouldn't
+		public override string ToString ()
+		{
+			return this.Uri;
+		}
+    }
+}
+

Deleted: trunk/MonoDevelop/Core/src/Main/Base/Services/File/RecentItem.cs
===================================================================
--- trunk/MonoDevelop/Core/src/Main/Base/Services/File/RecentItem.cs	2004-10-18 00:02:37 UTC (rev 1983)
+++ trunk/MonoDevelop/Core/src/Main/Base/Services/File/RecentItem.cs	2004-10-20 04:11:43 UTC (rev 1984)
@@ -1,68 +0,0 @@
-//
-// Author: John Luke  <jluke at cfl.rr.com>
-//
-// Copyright 2004 John Luke
-//
-
-// support class for FdoRecentFiles.cs
-
-using System;
-using MonoDevelop.Gui.Utils;
-
-namespace MonoDevelop.Services
-{
-	public class RecentItem
-	{
-		// must be a valid uri ex. file://
-		private string uri;
-		private string mime;
-		// the number of seconds sinced the Epoch when the item was added to the list.
-		private string timestamp;
-		private readonly DateTime epoch = new DateTime (1970, 1, 1, 0, 0, 0, 0);
-		// may need to change to allow for > 1
-		// lets wait til it's needed though
-		private string group;
-		
-		// these 3 are required
-		public RecentItem (string uri)
-		{
-			// TODO: uri sanity check
-			this.uri = uri;
-			this.mime = Vfs.GetMimeType (uri);
-			DateTime now = DateTime.UtcNow;
-			this.timestamp = ((int) (now - epoch).TotalSeconds).ToString ();
-		}
-
-		// update the group and timestamp
-		public void Update (bool project)
-		{
-			DateTime now = DateTime.UtcNow;
-			this.timestamp = ((int) (now - epoch).TotalSeconds).ToString ();
-			if (project)
-				this.group = "MonoDevelop Projects";
-			else
-				this.group = "MonoDevelop Files";
-		}
-
-		public string Mime
-		{
-			get { return mime; }
-		}
-
-		public string Uri
-		{
-			get { return uri; }
-		}
-
-		public string Timestamp
-		{
-			get { return timestamp; }
-		}
-
-		public string Group
-		{
-			get { return group; }
-			set { group = value; }
-		}
-	}
-}

Deleted: trunk/MonoDevelop/Core/src/Main/Base/Services/File/RingBuffer.cs
===================================================================
--- trunk/MonoDevelop/Core/src/Main/Base/Services/File/RingBuffer.cs	2004-10-18 00:02:37 UTC (rev 1983)
+++ trunk/MonoDevelop/Core/src/Main/Base/Services/File/RingBuffer.cs	2004-10-20 04:11:43 UTC (rev 1984)
@@ -1,117 +0,0 @@
-/*
-// <file>
-//     <copyright see="prj:///doc/copyright.txt"/>
-//     <license see="prj:///doc/license.txt"/>
-//     <owner name="Mike Krüger" email="mike at icsharpcode.net"/>
-//     <version value="$version"/>
-// </file>
-
-using System;
-using System.Xml;
-using System.Diagnostics;
-using System.Collections;
-using System.IO;
-
-using MonoDevelop.Core.Properties;
-
-using MonoDevelop.Services;
-
-namespace MonoDevelop.Services
-{
-	public class RingBuffer : IXmlConvertable
-	{
-		int maxLength     = 10;
-		ArrayList entries = new ArrayList();
-		
-		public ArrayList Entries {
-			get {
-				return entries;
-			}
-		}
-		
-		public RingBuffer(int maxLength)
-		{
-			this.maxLength = maxLength;
-		}
-		
-		protected RingBuffer(XmlElement element)
-		{
-			maxLength = Int32.Parse(element.Attributes["maxlength"].InnerText);
-			
-			foreach (XmlNode node in element.ChildNodes) {
-				DictionaryEntry newEntry = new DictionaryEntry(node.Attributes["key"].InnerText, node.Attributes["value"].InnerText);
-				if (!FilterEntry(newEntry)) {
-					entries.Add(newEntry);
-				}
-			}
-		}
-		
-		protected virtual bool FilterEntry(DictionaryEntry entry)
-		{
-			return false;
-		}
-		
-		public void AddEntry(string key, string val)
-		{
-			for (int i = 0; i < entries.Count; ++i) {
-				DictionaryEntry entry = (DictionaryEntry)entries[i];
-				if (entry.Key.ToString() == key) {
-					entries.RemoveAt(i);
-					--i;
-				}
-			}
-			
-			while (entries.Count >= maxLength) {
-				entries.RemoveAt(entries.Count - 1);
-			}
-			
-			if (entries.Count > 0) {
-				entries.Insert(0, name);
-			} else {
-				entries.Add(name);
-			}
-			
-			OnChanged(EventArgs.Empty);
-		}
-		
-		public object FromXmlElement(XmlElement element)
-		{
-			return new RingBuffer(element);
-		}
-		
-		public XmlElement ToXmlElement(XmlDocument doc)
-		{
-			XmlElement entries = doc.CreateElement("Entries");
-			
-			XmlAttribute lengthAttribute = doc.CreateAttribute("maxlength");
-			lengthAttribute.InnerText = maxLength.ToString();
-			entries.Attributes.Append(lengthAttribute);
-			
-			foreach (DictionaryEntry entry in entries) {
-				XmlElement entryElement = doc.CreateElement("Entry");
-				
-				XmlAttribute keyAttribute = doc.CreateAttribute("key");
-				keyAttribute.InnerText = entry.Key.ToString();
-				entryElement.Attributes.Append(keyAttribute);
-				
-				XmlAttribute valueAttribute = doc.CreateAttribute("value");
-				valueAttribute.InnerText = entry.Value.ToString();
-				entryElement.Attributes.Append(valueAttribute);
-				
-				entries.AppendChild(entryElement);
-			}
-			
-			return entries;
-		}
-		
-		protected virtual void OnChanged(EventArgs e)
-		{
-			if (Changed != null) {
-				Changed(this, e);
-			}
-		}
-		
-		public event EventHandler Changed;
-	}
-}
-*/




More information about the Monodevelop-patches-list mailing list