cvsimport
[beagle.git] / Util / NautilusTools.cs
blob892d45e38fd67e44e7e03c1d3971daf9d5d27a56
1 //
2 // NautilusTools.cs
3 //
4 // Copyright (C) 2004 Joe Gasiorek
5 // Copyright (C) 2004 Novell, Inc.
6 //
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining a
10 // copy of this software and associated documentation files (the "Software"),
11 // to deal in the Software without restriction, including without limitation
12 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 // and/or sell copies of the Software, and to permit persons to whom the
14 // Software is furnished to do so, subject to the following conditions:
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 // DEALINGS IN THE SOFTWARE.
28 using System;
29 using System.IO;
30 using System.Text;
31 using System.Xml;
32 using System.Collections;
33 using System.Collections.Specialized;
34 using System.Text.RegularExpressions;
35 using System.Runtime.InteropServices;
37 namespace Beagle.Util {
39 public class NautilusTools {
41 private class XmlDocCacheItem {
42 public XmlDocument doc;
43 public DateTime timestamp;
46 static private Hashtable cache = new Hashtable ();
48 private NautilusTools () { } // This class is static
50 static private string GetMetaFileName (string path)
52 string nautilusDir = Environment.GetEnvironmentVariable ("HOME") +
53 "/.nautilus/metafiles/file:%2F%2F";
55 if (path.StartsWith ("file://"))
56 path = path.Substring ("file://".Length);
58 path = Path.GetFullPath (path);
60 if (! Directory.Exists (path))
61 path = Path.GetDirectoryName (path);
63 path = path.Replace ("/", "%2F");
65 string name = nautilusDir + path + ".xml";
67 // If the filename is too long, ignore it.
68 if (Path.GetFileName (name).Length > 255)
69 return null;
71 return File.Exists (name) ? name : null;
74 static public DateTime GetMetaFileTime (string path)
76 path = GetMetaFileName (path);
77 return path != null ? File.GetLastWriteTime (path) : new DateTime ();
80 static private XmlNode GetMetaFileNode (string path)
82 XmlDocument doc = GetMetaFileDoc (path);
84 string name = Path.GetFileName (path);
85 if (name == null)
86 return null;
87 string xpath = String.Format ("/directory/file[@name=\"{0}\"]", StringFu.HexEscape (name));
88 return doc.SelectSingleNode (xpath);
91 static private XmlDocument GetMetaFileDoc (string path)
93 string metaFile = GetMetaFileName (path);
94 if (metaFile == null)
95 return null;
97 DateTime lastWrite = File.GetLastWriteTime (metaFile);
100 XmlDocCacheItem cached = (XmlDocCacheItem) cache [metaFile];
101 XmlDocument doc;
102 if (cached == null || lastWrite > cached.timestamp) {
103 doc = new XmlDocument ();
104 doc.Load (new StreamReader (metaFile));
106 cached = new XmlDocCacheItem ();
107 cached.doc = doc;
108 cached.timestamp = lastWrite;
109 cache [metaFile] = cached;
111 } else {
112 doc = cached.doc;
115 return doc;
118 static public string GetEmblem (string path)
120 XmlNode node = GetMetaFileNode (path);
121 if (node == null)
122 return null;
123 XmlNode subnode = node.SelectSingleNode ("keyword");
124 if (subnode == null)
125 return null;
127 XmlNode attr = subnode.Attributes.GetNamedItem ("name");
128 return attr != null ? attr.Value : null;
131 static public string GetNotes (string path)
133 XmlNode node = GetMetaFileNode (path);
134 if (node == null)
135 return null;
136 XmlNode attr = node.Attributes.GetNamedItem ("annotation");
137 return attr != null ? attr.Value : null;
140 static public IEnumerable GetFiles (string path)
142 XmlDocument doc = GetMetaFileDoc (path);
144 if (doc == null)
145 yield break;
147 XmlNodeList list = doc.SelectNodes ("/directory/file[@name]");
148 foreach (XmlNode node in list) {
149 string filename = (string) node.Attributes.GetNamedItem ("name").Value;
150 //filename = filename.Replace ("%20" , " ");
151 yield return Path.Combine (path, filename);
154 yield break;