2005-08-16 Gabor Kelemen <kelemeng@gnome.hu>
[beagle.git] / Util / Note.cs
blobe859f07623530bede9392a53c180a87fb887c968
1 //
2 // Note.cs
3 //
4 // Copyright (C) 2004 Christopher Orr
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.Xml;
32 namespace Beagle.Util {
34 public abstract class Note {
36 private string type;
37 private string filename;
38 public string subject;
39 public string text;
40 public DateTime timestamp;
42 //////////////////////////
44 protected Note (string _type, string _filename) {
45 this.type = _type;
46 this.filename = _filename;
49 // FIXME: this is a mess
50 public Uri Uri {
51 get {
52 return BuildNoteUri (filename, type);
56 public static Uri BuildNoteUri (string _filename, string _type)
58 string name = Path.GetFileNameWithoutExtension (_filename);
59 string uri = String.Format ("note://{0}/{1}", _type, name);
60 return new Uri (uri, true);
64 ///////////////////////////////////////////////////////////////////////////////
67 // Parse Tomboy notes
70 public class TomboyNote : Note {
72 private TomboyNote (string type, string filename) : base ("tomboy", filename)
73 { }
75 private TomboyNote (string filename) : base ("tomboy", filename)
76 { }
78 public static Note ParseNote (FileInfo file)
80 // Tomboy uses .note files.
81 // Don't index the backup folder.
82 if (file.Extension != ".note"
83 || file.DirectoryName.EndsWith ("/Backup")) {
84 return null;
87 // Create new note instance
88 Note note = new TomboyNote(file.FullName);
89 note.timestamp = file.LastWriteTime;
91 // Parse XML info from file
92 StreamReader reader = new StreamReader (file.FullName, System.Text.Encoding.UTF8);
93 XmlTextReader doc = new XmlTextReader (reader);
94 doc.Namespaces = false;
96 while (doc.Read ()) {
97 if (doc.NodeType != XmlNodeType.Element)
98 continue;
100 switch (doc.Name) {
101 case "title":
102 note.subject = doc.ReadString ();
103 break;
104 case "note-content":
105 // FIXME: don't discard XML style info
106 note.text = StripTags (doc.ReadInnerXml ());
107 break;
110 doc.Close ();
112 return note;
115 private static string StripTags (string line)
117 int i, j;
118 while (true) {
119 i = line.IndexOf ('<');
120 if (i == -1)
121 break;
122 j = line.IndexOf ('>', i);
123 line = line.Substring (0, i) + line.Substring (j+1);
125 return line;