~ubuntu-branches/ubuntu/intrepid/tomboy/intrepid

« back to all changes in this revision

Viewing changes to Tomboy/Plugins/StickyNoteImport.cs

  • Committer: Bazaar Package Importer
  • Author(s): Sebastian Dröge
  • Date: 2008-05-20 11:05:10 UTC
  • mfrom: (1.1.38 upstream) (2.1.56 hardy)
  • Revision ID: james.westby@ubuntu.com-20080520110510-y075w3uklbdb0ei3
Tags: 0.10.2-1
New upstream bugfix release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// Import notes from StickyNote applet to Tomboy
2
 
// (C) 2006 Sandy Armstrong <sanfordarmstrong@gmail.com>
3
 
 
4
 
using System;
5
 
using System.IO;
6
 
using System.Xml;
7
 
using Mono.Unix;
8
 
 
9
 
using Tomboy;
10
 
 
11
 
public class StickyNoteImporter : NotePlugin
12
 
{
13
 
        private const string sticky_xml_rel_path = "/.gnome2/stickynotes_applet";
14
 
        private const string sticky_note_query = "//note";
15
 
 
16
 
        private const string base_note_xml = 
17
 
                "<note-content><note-title>{0}</note-title>\n\n{1}</note-content>";
18
 
        private const string base_duplicate_note_title = "{0} (#{1})";
19
 
 
20
 
        private const string debug_no_sticky_file =
21
 
                "StickyNoteImporter: Sticky Notes XML file does not exist or is invalid!";
22
 
        private const string debug_create_error_base =
23
 
                "StickyNoteImporter: Error while trying to create note \"{0}\": {1}";
24
 
        private const string debug_first_run_detected =
25
 
                "StickyNoteImporter: Detecting that importer has never been run...";
26
 
        private const string debug_gconf_set_error_base =
27
 
                "StickyNoteImporter: Error setting initial GConf first run key value: {0}";
28
 
 
29
 
        private string sticky_xml_path;
30
 
        
31
 
        protected override void Initialize ()
32
 
        {
33
 
                Gtk.ImageMenuItem item = 
34
 
                        new Gtk.ImageMenuItem (Catalog.GetString ("Import from Sticky Notes"));
35
 
                item.Image = new Gtk.Image (Gtk.Stock.Convert, Gtk.IconSize.Menu);
36
 
                item.Activated += ImportButtonClicked;
37
 
                item.Show ();
38
 
                AddPluginMenuItem (item);
39
 
                
40
 
                sticky_xml_path =
41
 
                        Environment.GetFolderPath (System.Environment.SpecialFolder.Personal)
42
 
                        + sticky_xml_rel_path;
43
 
                
44
 
                CheckForFirstRun ();
45
 
        }
46
 
        
47
 
        protected override void Shutdown ()
48
 
        {
49
 
                // Do nothing.
50
 
        }
51
 
 
52
 
        protected override void OnNoteOpened () 
53
 
        {
54
 
                // Do nothing.
55
 
        }
56
 
 
57
 
        void CheckForFirstRun ()
58
 
        {
59
 
                bool firstRun = (bool) Preferences.Get (Preferences.STICKYNOTEIMPORTER_FIRST_RUN);
60
 
                
61
 
                if (firstRun) {
62
 
                        try {
63
 
                                Preferences.Set (Preferences.STICKYNOTEIMPORTER_FIRST_RUN, false);
64
 
                        } catch (Exception e) {
65
 
                                Logger.Log (debug_gconf_set_error_base, e);
66
 
                        }
67
 
 
68
 
                        Logger.Log (debug_first_run_detected);
69
 
 
70
 
                        XmlDocument xmlDoc = GetStickyXmlDoc ();                
71
 
                        if (xmlDoc != null)
72
 
                                ImportNotes (xmlDoc);
73
 
                }
74
 
        }
75
 
 
76
 
        XmlDocument GetStickyXmlDoc ()
77
 
        {
78
 
                if (File.Exists (sticky_xml_path)) {
79
 
                        try {
80
 
                                XmlDocument xmlDoc = new XmlDocument ();
81
 
                                xmlDoc.Load (sticky_xml_path);
82
 
                                return xmlDoc;
83
 
                        } catch {
84
 
                                Logger.Log (debug_no_sticky_file);
85
 
                                return null;
86
 
                        }
87
 
                }
88
 
                else {
89
 
                        Logger.Log (debug_no_sticky_file);
90
 
                        return null;
91
 
                }
92
 
                
93
 
        }
94
 
        
95
 
        void ImportButtonClicked (object sender, EventArgs args)
96
 
        {
97
 
                XmlDocument xmlDoc = GetStickyXmlDoc ();
98
 
                
99
 
                if (xmlDoc != null)
100
 
                        ImportNotes (xmlDoc);
101
 
                else
102
 
                        ShowNoStickyXMLDialog (sticky_xml_path);
103
 
        }
104
 
        
105
 
        void ShowNoStickyXMLDialog (string xmlPath)
106
 
        {
107
 
                // TODO: Why does "stickynotes_applet" show up as
108
 
                //       "stickynotesapplet" with the "a" underlined???
109
 
                ShowMessageDialog (
110
 
                        Catalog.GetString ("No Sticky Notes found"),
111
 
                        string.Format (Catalog.GetString ("No suitable Sticky Notes file was " + 
112
 
                                                          "found at \"{0}\"."),
113
 
                                       xmlPath),
114
 
                        Gtk.MessageType.Error);
115
 
        }
116
 
        
117
 
        void ShowResultsDialog (int numNotesImported, int numNotesTotal)
118
 
        {
119
 
                ShowMessageDialog (
120
 
                        Catalog.GetString ("Sticky Notes import completed"),
121
 
                        string.Format (Catalog.GetString ("<b>{0}</b> of <b>{1}</b> Sticky Notes " +
122
 
                                                          "were successfully imported."),
123
 
                                       numNotesImported,
124
 
                                       numNotesTotal),
125
 
                        Gtk.MessageType.Info);
126
 
        }
127
 
        
128
 
        void ImportNotes (XmlDocument xmlDoc)
129
 
        {
130
 
                XmlNodeList nodes = xmlDoc.SelectNodes (sticky_note_query);
131
 
                
132
 
                int numSuccessful = 0;
133
 
                string defaultTitle = Catalog.GetString ("Untitled");
134
 
 
135
 
                foreach (XmlNode node in nodes) {
136
 
                        XmlAttribute titleAttr = node.Attributes["title"];
137
 
                        string stickyTitle = defaultTitle;
138
 
                        if (titleAttr != null && titleAttr.InnerXml.Length > 0)
139
 
                                stickyTitle = titleAttr.InnerXml;
140
 
                        string stickyContent = node.InnerXml;
141
 
 
142
 
                        if (CreateNoteFromSticky (stickyTitle, stickyContent))
143
 
                                numSuccessful++;
144
 
                }
145
 
                
146
 
                ShowResultsDialog (numSuccessful, nodes.Count);
147
 
        }
148
 
        
149
 
        bool CreateNoteFromSticky (string stickyTitle, string content)
150
 
        {
151
 
                // There should be no XML in the content
152
 
                // TODO: Report the error in the results dialog
153
 
                //       (this error should only happen if somebody has messed with the XML file)
154
 
                if (content.IndexOf ('>') != -1 || content.IndexOf ('<') != -1) {
155
 
                        Logger.Log (string.Format (debug_create_error_base, 
156
 
                                                   stickyTitle, 
157
 
                                                   "Invalid characters in note XML"));
158
 
                        return false;
159
 
                }
160
 
 
161
 
                string preferredTitle = Catalog.GetString ("Sticky Note: ") + stickyTitle;
162
 
                string title = preferredTitle;
163
 
 
164
 
                int i = 2; // Append numbers to create unique title, starting with 2
165
 
                while (Manager.Find (title) != null)
166
 
                        title = string.Format (base_duplicate_note_title, preferredTitle, i++);
167
 
                
168
 
                string noteXml = string.Format (base_note_xml, title, content);
169
 
                
170
 
                try {
171
 
                        Note newNote = Manager.Create (title, noteXml);
172
 
                        newNote.QueueSave (false);
173
 
                        newNote.Save ();
174
 
                        return true;
175
 
                } catch (Exception e) {
176
 
                        Logger.Log (string.Format (debug_create_error_base, title, e.Message));
177
 
                        return false;
178
 
                }
179
 
        }
180
 
        
181
 
        void ShowMessageDialog (string title, string message, Gtk.MessageType messageType)
182
 
        {
183
 
                HIGMessageDialog dialog =
184
 
                        new HIGMessageDialog (
185
 
                                Note.Window,
186
 
                                Gtk.DialogFlags.DestroyWithParent,
187
 
                                messageType,
188
 
                                Gtk.ButtonsType.Ok,
189
 
                                title,
190
 
                                message);
191
 
                dialog.Run ();
192
 
                dialog.Destroy ();              
193
 
        }
194
 
}