1
// Import notes from StickyNote applet to Tomboy
2
// (C) 2006 Sandy Armstrong <sanfordarmstrong@gmail.com>
11
public class StickyNoteImporter : NotePlugin
13
private const string sticky_xml_rel_path = "/.gnome2/stickynotes_applet";
14
private const string sticky_note_query = "//note";
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})";
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}";
29
private string sticky_xml_path;
31
protected override void Initialize ()
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;
38
AddPluginMenuItem (item);
41
Environment.GetFolderPath (System.Environment.SpecialFolder.Personal)
42
+ sticky_xml_rel_path;
47
protected override void Shutdown ()
52
protected override void OnNoteOpened ()
57
void CheckForFirstRun ()
59
bool firstRun = (bool) Preferences.Get (Preferences.STICKYNOTEIMPORTER_FIRST_RUN);
63
Preferences.Set (Preferences.STICKYNOTEIMPORTER_FIRST_RUN, false);
64
} catch (Exception e) {
65
Logger.Log (debug_gconf_set_error_base, e);
68
Logger.Log (debug_first_run_detected);
70
XmlDocument xmlDoc = GetStickyXmlDoc ();
76
XmlDocument GetStickyXmlDoc ()
78
if (File.Exists (sticky_xml_path)) {
80
XmlDocument xmlDoc = new XmlDocument ();
81
xmlDoc.Load (sticky_xml_path);
84
Logger.Log (debug_no_sticky_file);
89
Logger.Log (debug_no_sticky_file);
95
void ImportButtonClicked (object sender, EventArgs args)
97
XmlDocument xmlDoc = GetStickyXmlDoc ();
100
ImportNotes (xmlDoc);
102
ShowNoStickyXMLDialog (sticky_xml_path);
105
void ShowNoStickyXMLDialog (string xmlPath)
107
// TODO: Why does "stickynotes_applet" show up as
108
// "stickynotesapplet" with the "a" underlined???
110
Catalog.GetString ("No Sticky Notes found"),
111
string.Format (Catalog.GetString ("No suitable Sticky Notes file was " +
112
"found at \"{0}\"."),
114
Gtk.MessageType.Error);
117
void ShowResultsDialog (int numNotesImported, int numNotesTotal)
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."),
125
Gtk.MessageType.Info);
128
void ImportNotes (XmlDocument xmlDoc)
130
XmlNodeList nodes = xmlDoc.SelectNodes (sticky_note_query);
132
int numSuccessful = 0;
133
string defaultTitle = Catalog.GetString ("Untitled");
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;
142
if (CreateNoteFromSticky (stickyTitle, stickyContent))
146
ShowResultsDialog (numSuccessful, nodes.Count);
149
bool CreateNoteFromSticky (string stickyTitle, string content)
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,
157
"Invalid characters in note XML"));
161
string preferredTitle = Catalog.GetString ("Sticky Note: ") + stickyTitle;
162
string title = preferredTitle;
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++);
168
string noteXml = string.Format (base_note_xml, title, content);
171
Note newNote = Manager.Create (title, noteXml);
172
newNote.QueueSave (false);
175
} catch (Exception e) {
176
Logger.Log (string.Format (debug_create_error_base, title, e.Message));
181
void ShowMessageDialog (string title, string message, Gtk.MessageType messageType)
183
HIGMessageDialog dialog =
184
new HIGMessageDialog (
186
Gtk.DialogFlags.DestroyWithParent,