New e-d-s backend which indexes all local addressbooks and calendars.
[beagle.git] / Util / NautilusTools.cs
blob7bb7c62e8d8ca80044dc15c450a70ed6d21cf597
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);
57 path = Path.GetDirectoryName (Path.GetFullPath (path));
58 path = path.Replace ("/", "%2F");
60 string name = nautilusDir + path + ".xml";
62 // If the filename is too long, ignore it.
63 if (Path.GetFileName (name).Length > 255)
64 return null;
66 return File.Exists (name) ? name : null;
69 static public DateTime GetMetaFileTime (string path)
71 path = GetMetaFileName (path);
72 return path != null ? File.GetLastWriteTime (path) : new DateTime ();
75 static private XmlNode GetMetaFileNode (string path)
77 string metaFile = GetMetaFileName (path);
78 if (metaFile == null)
79 return null;
81 DateTime lastWrite = File.GetLastWriteTime (metaFile);
83 string name = Path.GetFileName (path);
85 XmlDocCacheItem cached = (XmlDocCacheItem) cache [metaFile];
86 XmlDocument doc;
87 if (cached == null || lastWrite > cached.timestamp) {
88 doc = new XmlDocument ();
89 doc.Load (new StreamReader (metaFile));
91 cached = new XmlDocCacheItem ();
92 cached.doc = doc;
93 cached.timestamp = lastWrite;
94 cache [metaFile] = cached;
96 } else {
97 doc = cached.doc;
100 string xpath = String.Format ("/directory/file[@name=\"{0}\"]", StringFu.HexEscape (name));
101 return doc.SelectSingleNode (xpath);
104 static public string GetEmblem (string path)
106 XmlNode node = GetMetaFileNode (path);
107 if (node == null)
108 return null;
109 XmlNode subnode = node.SelectSingleNode ("keyword");
110 if (subnode == null)
111 return null;
113 XmlNode attr = subnode.Attributes.GetNamedItem ("name");
114 return attr != null ? attr.Value : null;
117 static public string GetNotes (string path)
119 XmlNode node = GetMetaFileNode (path);
120 if (node == null)
121 return null;
122 XmlNode attr = node.Attributes.GetNamedItem ("annotation");
123 return attr != null ? attr.Value : null;