Merging from head
[beagle.git] / Util / Note.cs
blob0a4eda4820b9585fa0578498dfc0cd623d374e60
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.Text;
31 using System.Xml;
33 namespace Beagle.Util {
35 public abstract class Note {
37 private string type;
38 private string filename;
39 public string subject;
40 public string text;
41 public DateTime timestamp;
43 //////////////////////////
45 protected Note (string _type, string _filename) {
46 this.type = _type;
47 this.filename = _filename;
50 // FIXME: this is a mess
51 public Uri Uri {
52 get {
53 return BuildNoteUri (filename, type);
57 public static Uri BuildNoteUri (string _filename, string _type)
59 string name = Path.GetFileNameWithoutExtension (_filename);
60 string uri = String.Format ("note://{0}/{1}", _type, name);
61 return new Uri (uri, true);
65 ///////////////////////////////////////////////////////////////////////////////
68 // Parse Tomboy notes
71 public class TomboyNote : Note {
73 private TomboyNote (string type, string filename) : base ("tomboy", filename)
74 { }
76 private TomboyNote (string filename) : base ("tomboy", filename)
77 { }
79 public static Note ParseNote (FileInfo file)
81 // Tomboy uses .note files.
82 // Don't index the backup folder.
83 if (file.Extension != ".note"
84 || file.DirectoryName.EndsWith ("/Backup")) {
85 return null;
88 // Create new note instance
89 Note note = new TomboyNote(file.FullName);
90 note.timestamp = file.LastWriteTimeUtc;
92 // Parse XML info from file
93 StreamReader reader = new StreamReader (file.FullName, System.Text.Encoding.UTF8);
94 XmlTextReader doc = new XmlTextReader (reader);
95 doc.Namespaces = false;
97 bool read_text = false;
98 StringBuilder sb = new StringBuilder ();
100 while (doc.Read ()) {
101 // FIXME: Extract more information than mere text from the tags in note-content
102 // for hottext, linking notes etc.
103 if (doc.NodeType == XmlNodeType.Element && doc.Name == "note-content") {
104 read_text = true;
105 continue;
108 if (doc.NodeType == XmlNodeType.EndElement && doc.Name == "note-content") {
109 read_text = false;
110 continue;
113 if (doc.NodeType == XmlNodeType.Element && doc.Name == "title") {
114 note.subject = doc.ReadString ();
115 continue;
118 if (doc.NodeType == XmlNodeType.Text) {
119 if (read_text)
120 sb.Append (doc.Value);
121 continue;
125 doc.Close ();
126 note.text = sb.ToString ();
128 return note;