4 // Copyright (C) 2004 Joe Gasiorek
5 // Copyright (C) 2004 Novell, Inc.
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.
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)
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
);
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
);
97 DateTime lastWrite
= File
.GetLastWriteTime (metaFile
);
100 XmlDocCacheItem cached
= (XmlDocCacheItem
) cache
[metaFile
];
102 if (cached
== null || lastWrite
> cached
.timestamp
) {
103 doc
= new XmlDocument ();
104 doc
.Load (new StreamReader (metaFile
));
106 cached
= new XmlDocCacheItem ();
108 cached
.timestamp
= lastWrite
;
109 cache
[metaFile
] = cached
;
118 static public string GetEmblem (string path
)
120 XmlNode node
= GetMetaFileNode (path
);
123 XmlNode subnode
= node
.SelectSingleNode ("keyword");
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
);
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
);
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
);