1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
|
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Xml;
using System.Text.RegularExpressions;
using Mono.Unix;
namespace Tomboy
{
public delegate void NoteRenameHandler (Note sender, string old_title);
public delegate void NoteSavedHandler (Note note);
public delegate void TagAddedHandler (Note note, Tag tag);
public delegate void TagRemovingHandler (Note note, Tag tag);
public delegate void TagRemovedHandler (Note note, string tag_name);
// Contains all pure note data, like the note title and note text.
public class NoteData
{
readonly string uri;
string title;
string text;
DateTime create_date;
DateTime change_date;
int cursor_pos;
int width, height;
int x, y;
bool open_on_startup;
Dictionary<string, Tag> tags;
const int noPosition = -1;
public NoteData (string uri)
{
this.uri = uri;
this.text = "";
x = noPosition;
y = noPosition;
tags = new Dictionary<string, Tag> ();
create_date = DateTime.MinValue;
change_date = DateTime.MinValue;
}
public string Uri
{
get {
return uri;
}
}
public string Title
{
get {
return title;
}
set {
title = value;
}
}
public string Text
{
get {
return text;
}
set {
text = value;
}
}
public DateTime CreateDate
{
get {
return create_date;
}
set {
create_date = value;
}
}
public DateTime ChangeDate
{
get {
return change_date;
}
set {
change_date = value;
}
}
// FIXME: the next five attributes don't belong here (the data
// model), but belong into the view; for now they are kept here
// for backwards compatibility
public int CursorPosition
{
get {
return cursor_pos;
}
set {
cursor_pos = value;
}
}
public int Width
{
get {
return width;
}
set {
width = value;
}
}
public int Height
{
get {
return height;
}
set {
height = value;
}
}
public int X
{
get {
return x;
}
set {
x = value;
}
}
public int Y
{
get {
return y;
}
set {
y = value;
}
}
public Dictionary<string, Tag> Tags
{
get {
return tags;
}
}
public bool IsOpenOnStartup
{
get {
return open_on_startup;
}
set {
open_on_startup = value;
}
}
public void SetPositionExtent (int x, int y, int width, int height)
{
Debug.Assert (x >= 0 && y >= 0);
Debug.Assert (width > 0 && height > 0);
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
public bool HasPosition ()
{
return x != noPosition && y != noPosition;
}
public bool HasExtent ()
{
return width != 0 && height != 0;
}
}
// This class wraps a NoteData instance. Most method calls are
// forwarded to the wrapped instance, but there is special behaviour
// for the Text attribute. This class takes care that this attribute
// is synchronized with the contents of a NoteBuffer instance.
public class NoteDataBufferSynchronizer
{
readonly NoteData data;
NoteBuffer buffer;
public NoteDataBufferSynchronizer (NoteData data)
{
this.data = data;
}
public NoteData GetDataSynchronized ()
{
// Assert that Data.Text returns the current
// text from the text buffer.
SynchronizeText ();
return data;
}
public NoteData Data
{
get {
return data;
}
}
public NoteBuffer Buffer
{
get {
return buffer;
}
set {
buffer = value;
buffer.Changed += BufferChanged;
buffer.TagApplied += BufferTagApplied;
buffer.TagRemoved += BufferTagRemoved;
SynchronizeBuffer ();
InvalidateText ();
}
}
//Text is actually an Xml formatted string
public string Text
{
get {
SynchronizeText ();
return data.Text;
}
set {
data.Text = value;
SynchronizeBuffer ();
}
}
// Custom Methods
void InvalidateText ()
{
data.Text = "";
}
bool TextInvalid ()
{
return data.Text == "";
}
void SynchronizeText ()
{
if (TextInvalid () && buffer != null) {
data.Text = NoteBufferArchiver.Serialize (buffer);
}
}
void SynchronizeBuffer ()
{
if (!TextInvalid () && buffer != null) {
// Don't create Undo actions during load
buffer.Undoer.FreezeUndo ();
buffer.Clear ();
// Load the stored xml text
NoteBufferArchiver.Deserialize (buffer,
buffer.StartIter,
data.Text);
buffer.Modified = false;
Gtk.TextIter cursor;
if (data.CursorPosition != 0) {
// Move cursor to last-saved position
cursor = buffer.GetIterAtOffset (data.CursorPosition);
} else {
// Avoid title line
cursor = buffer.GetIterAtLine (2);
}
buffer.PlaceCursor (cursor);
// New events should create Undo actions
buffer.Undoer.ThawUndo ();
}
}
// Callbacks
void BufferChanged (object sender, EventArgs args)
{
InvalidateText ();
}
void BufferTagApplied (object sender, Gtk.TagAppliedArgs args)
{
if (NoteTagTable.TagIsSerializable (args.Tag)) {
InvalidateText ();
}
}
void BufferTagRemoved (object sender, Gtk.TagRemovedArgs args)
{
if (NoteTagTable.TagIsSerializable (args.Tag)) {
InvalidateText ();
}
}
}
public class Note
{
readonly NoteDataBufferSynchronizer data;
string filepath;
bool save_needed;
bool is_deleting;
NoteManager manager;
NoteWindow window;
NoteBuffer buffer;
NoteTagTable tag_table;
InterruptableTimeout save_timeout;
struct ChildWidgetData
{
public Gtk.TextChildAnchor anchor;
public Gtk.Widget widget;
};
Queue <ChildWidgetData> childWidgetQueue;
[System.Diagnostics.Conditional ("DEBUG_SAVE")]
static void DebugSave (string format, params object[] args)
{
Console.WriteLine (format, args);
}
Note (NoteData data, string filepath, NoteManager manager)
{
this.data = new NoteDataBufferSynchronizer (data);
this.filepath = filepath;
this.manager = manager;
// Make sure each of the tags that NoteData found point to the
// instance of this note.
foreach (Tag tag in data.Tags.Values) {
AddTag (tag);
}
save_timeout = new InterruptableTimeout ();
save_timeout.Timeout += SaveTimeout;
childWidgetQueue = new Queue <ChildWidgetData> ();
is_deleting = false;
}
static string UrlFromPath (string filepath)
{
return "note://tomboy/" +
Path.GetFileNameWithoutExtension (filepath);
}
public static Note CreateNewNote (string title,
string filepath,
NoteManager manager)
{
NoteData data = new NoteData (UrlFromPath (filepath));
data.Title = title;
data.CreateDate = DateTime.Now;
data.ChangeDate = data.CreateDate;
return new Note (data, filepath, manager);
}
public static Note CreateExistingNote (NoteData data,
string filepath,
NoteManager manager)
{
if (data.CreateDate == DateTime.MinValue)
data.CreateDate = File.GetCreationTime (filepath);
if (data.ChangeDate == DateTime.MinValue)
data.ChangeDate = File.GetLastWriteTime (filepath);
return new Note (data, filepath, manager);
}
public void Delete ()
{
is_deleting = true;
save_timeout.Cancel ();
// Remove the note from all the tags
foreach (Tag tag in Tags) {
RemoveTag (tag);
}
if (window != null) {
window.Hide ();
window.Destroy ();
}
// Remove note URI from GConf entry menu_pinned_notes
IsPinned = false;
}
// Load from an existing Note...
public static Note Load (string read_file, NoteManager manager)
{
NoteData data = NoteArchiver.Read (read_file, UrlFromPath (read_file));
Note note = CreateExistingNote (data, read_file, manager);
return note;
}
public void Save ()
{
// Prevent any other condition forcing a save on the note
// if Delete has been called.
if (is_deleting)
return;
// Do nothing if we don't need to save. Avoids unneccessary saves
// e.g on forced quit when we call save for every note.
if (!save_needed)
return;
Logger.Log ("Saving '{0}'...", data.Data.Title);
NoteArchiver.Write (filepath, data.GetDataSynchronized ());
if (Saved != null)
Saved (this);
}
//
// Buffer change signals. These queue saves and invalidate the serialized text
// depending on the change...
//
void BufferChanged (object sender, EventArgs args)
{
DebugSave ("BufferChanged queueing save");
QueueSave (true);
}
void BufferTagApplied (object sender, Gtk.TagAppliedArgs args)
{
if (NoteTagTable.TagIsSerializable (args.Tag)) {
DebugSave ("BufferTagApplied queueing save: {0}", args.Tag.Name);
QueueSave (true);
}
}
void BufferTagRemoved (object sender, Gtk.TagRemovedArgs args)
{
if (NoteTagTable.TagIsSerializable (args.Tag)) {
DebugSave ("BufferTagRemoved queueing save: {0}", args.Tag.Name);
QueueSave (true);
}
}
void BufferInsertMarkSet (object sender, Gtk.MarkSetArgs args)
{
if (args.Mark != buffer.InsertMark)
return;
data.Data.CursorPosition = args.Location.Offset;
DebugSave ("BufferInsertSetMark queueing save");
QueueSave (false);
}
//
// Window events. Queue a save when the window location/size has changed, and set
// our window to null on delete, and fire the Opened event on window realize...
//
[GLib.ConnectBefore]
void WindowConfigureEvent (object sender, Gtk.ConfigureEventArgs args)
{
int cur_x, cur_y, cur_width, cur_height;
// Ignore events when maximized. We don't want notes
// popping up maximized the next run.
if ((window.GdkWindow.State & Gdk.WindowState.Maximized) > 0)
return;
window.GetPosition (out cur_x, out cur_y);
window.GetSize (out cur_width, out cur_height);
if (data.Data.X == cur_x &&
data.Data.Y == cur_y &&
data.Data.Width == cur_width &&
data.Data.Height == cur_height)
return;
data.Data.SetPositionExtent (cur_x, cur_y, cur_width, cur_height);
DebugSave ("WindowConfigureEvent queueing save");
QueueSave (false);
}
[GLib.ConnectBefore]
void WindowDestroyed (object sender, EventArgs args)
{
window = null;
}
/// <summary>
/// Set a 4 second timeout to execute the save. Possibly
/// invalidate the text, which causes a re-serialize when the
/// timeout is called...
/// </summary>
/// <param name="content_changed">Indicates whether or not
/// to update the note's last change date</param>
public void QueueSave (bool content_changed)
{
DebugSave ("Got QueueSave");
// Replace the existing save timeout. Wait 4 seconds
// before saving...
save_timeout.Reset (4000);
save_needed = true;
if (content_changed) {
data.Data.ChangeDate = DateTime.Now;
}
}
// Save timeout to avoid constanly resaving. Called every 4 seconds.
void SaveTimeout (object sender, EventArgs args)
{
try {
Save ();
save_needed = false;
} catch (Exception e) {
// FIXME: Present a nice dialog here that interprets the
// error message correctly.
Logger.Log ("Error while saving: {0}", e);
}
}
public void AddTag (Tag tag)
{
if (tag == null)
throw new ArgumentNullException ("Note.AddTag () called with a null tag.");
tag.AddNote (this);
if (!data.Data.Tags.ContainsKey (tag.NormalizedName)) {
data.Data.Tags [tag.NormalizedName] = tag;
if (TagAdded != null)
TagAdded (this, tag);
DebugSave ("Tag added, queueing save");
QueueSave (true);
}
}
public void RemoveTag (Tag tag)
{
if (tag == null)
throw new ArgumentException ("Note.RemoveTag () called with a null tag.");
if (!data.Data.Tags.ContainsKey (tag.NormalizedName))
return;
if (TagRemoving != null)
TagRemoving (this, tag);
data.Data.Tags.Remove (tag.NormalizedName);
tag.RemoveNote (this);
if (TagRemoved != null)
TagRemoved (this, tag.NormalizedName);
DebugSave ("Tag removed, queueing save");
QueueSave (true);
}
public bool ContainsTag (Tag tag)
{
if (data.Data.Tags.ContainsKey (tag.NormalizedName) == true)
return true;
return false;
}
public void AddChildWidget (Gtk.TextChildAnchor childAnchor, Gtk.Widget widget)
{
ChildWidgetData data = new ChildWidgetData ();
data.anchor = childAnchor;
data.widget = widget;
childWidgetQueue.Enqueue (data);
if (HasWindow)
ProcessChildWidgetQueue ();
}
private void ProcessChildWidgetQueue ()
{
// Insert widgets in the childWidgetQueue into the NoteEditor
if (!HasWindow)
return; // can't do anything without a window
foreach (ChildWidgetData data in childWidgetQueue) {
data.widget.Show();
Window.Editor.AddChildAtAnchor (data.widget, data.anchor);
}
childWidgetQueue.Clear ();
}
public string Uri
{
get {
return data.Data.Uri;
}
}
public string Id
{
get {
return data.Data.Uri.Replace ("note://tomboy/",""); // TODO: Store on Note instantiation
}
}
public string FilePath
{
get {
return filepath;
}
set {
filepath = value;
}
}
public string Title
{
get {
return data.Data.Title;
}
set {
if (data.Data.Title != value) {
if (window != null)
window.Title = value;
string old_title = data.Data.Title;
data.Data.Title = value;
if (Renamed != null)
Renamed (this, old_title);
QueueSave (true); // TODO: Right place for this?
}
}
}
public void RenameWithoutLinkUpdate (string newTitle)
{
if (data.Data.Title != newTitle) {
if (window != null)
window.Title = newTitle;
data.Data.Title = newTitle;
// HACK:
if (Renamed != null)
Renamed (this, newTitle);
QueueSave (true); // TODO: Right place for this?
}
}
public string XmlContent
{
get {
return data.Text;
}
set {
if (buffer != null) {
buffer.SetText("");
NoteBufferArchiver.Deserialize (buffer, value);
} else
data.Text = value;
}
}
/// <summary>
/// Return the complete contents of this note's .note XML file
/// In case of any error, null is returned.
/// </summary>
public string GetCompleteNoteXml ()
{
if (!File.Exists (filepath))
return null;
// Make sure file contents are up to date
save_needed = true; // HACK: Catches newly created notes
Save ();
StreamReader reader = null;
try {
reader = new StreamReader (filepath);
return reader.ReadToEnd ();
} catch (Exception e) {
Logger.Error ("Error received while attempting to read " +
filepath + ": " + e.Message);
return null;
} finally {
if (reader != null)
reader.Close ();
}
}
// Reload note data from a complete note XML string
// Should referesh note window, too
public void LoadForeignNoteXml (string foreignNoteXml)
{
if (foreignNoteXml == null)
throw new ArgumentNullException ("foreignNoteXml");
// Arguments to this method cannot be trusted. If this method
// were to throw an XmlException in the middle of processing,
// a note could be damaged. Therefore, we check for parseability
// ahead of time, and throw early.
XmlDocument xmlDoc = new XmlDocument ();
// This will throw an XmlException if foreignNoteXml is not parseable
xmlDoc.LoadXml (foreignNoteXml);
xmlDoc = null;
StringReader reader = new StringReader (foreignNoteXml);
XmlTextReader xml = new XmlTextReader (reader);
xml.Namespaces = false;
// Remove tags now, since a note with no tags has
// no "tags" element in the XML
foreach (Tag tag in Tags)
RemoveTag (tag);
while (xml.Read ()) {
switch (xml.NodeType) {
case XmlNodeType.Element:
switch (xml.Name) {
case "title":
Title = xml.ReadString ();
break;
case "text":
XmlContent = xml.ReadInnerXml ();
break;
case "last-change-date":
data.Data.ChangeDate =
XmlConvert.ToDateTime (xml.ReadString (), NoteArchiver.DATE_TIME_FORMAT);
break;
case "create-date":
data.Data.CreateDate =
XmlConvert.ToDateTime (xml.ReadString (), NoteArchiver.DATE_TIME_FORMAT);
break;
case "tags":
XmlDocument doc = new XmlDocument ();
List<string> tag_strings = ParseTags (doc.ReadNode (xml.ReadSubtree ()));
foreach (string tag_str in tag_strings) {
Tag tag = TagManager.GetOrCreateTag (tag_str);
AddTag (tag);
}
break;
case "open-on-startup":
IsOpenOnStartup = bool.Parse (xml.ReadString ());
break;
}
break;
}
}
xml.Close ();
// TODO: Any reason to queue a save here? Maybe not for sync but for others?
}
// TODO: CODE DUPLICATION SUCKS
List<string> ParseTags (XmlNode tagNodes)
{
List<string> tags = new List<string> ();
foreach (XmlNode node in tagNodes.SelectNodes ("//tag")) {
string tag = node.InnerText;
tags.Add (tag);
}
return tags;
}
public string TextContent
{
get {
if (buffer != null)
return buffer.GetSlice (buffer.StartIter,
buffer.EndIter,
false /* hidden_chars */);
else
return XmlDecoder.Decode (XmlContent);
}
set {
if (buffer != null)
buffer.SetText (value);
else
Logger.Log ("Setting text content for closed notes not supported");
}
}
public NoteData Data
{
get {
return data.GetDataSynchronized ();
}
}
public DateTime CreateDate
{
get {
return data.Data.CreateDate;
}
}
public DateTime ChangeDate
{
get {
return data.Data.ChangeDate;
}
}
public NoteManager Manager
{
get {
return manager;
}
set {
manager = value;
}
}
public NoteTagTable TagTable
{
get {
if (tag_table == null) {
#if FIXED_GTKSPELL
// NOTE: Sharing the same TagTable means
// that formatting is duplicated between
// buffers.
tag_table = NoteTagTable.Instance;
#else
// NOTE: GtkSpell chokes on shared
// TagTables because it blindly tries to
// create a new "gtkspell-misspelling"
// tag, which fails if one already
// exists in the table.
tag_table = new NoteTagTable ();
#endif
}
return tag_table;
}
}
public bool HasBuffer
{
get {
return null != buffer;
}
}
public NoteBuffer Buffer
{
get {
if (buffer == null) {
Logger.Log ("Creating Buffer for '{0}'...",
data.Data.Title);
buffer = new NoteBuffer (TagTable, this);
data.Buffer = buffer;
// Listen for further changed signals
buffer.Changed += BufferChanged;
buffer.TagApplied += BufferTagApplied;
buffer.TagRemoved += BufferTagRemoved;
buffer.MarkSet += BufferInsertMarkSet;
}
return buffer;
}
}
public bool HasWindow
{
get {
return null != window;
}
}
public NoteWindow Window
{
get {
if (window == null) {
window = new NoteWindow (this);
window.Destroyed += WindowDestroyed;
window.ConfigureEvent += WindowConfigureEvent;
if (data.Data.HasExtent ())
window.SetDefaultSize (data.Data.Width,
data.Data.Height);
if (data.Data.HasPosition ())
window.Move (data.Data.X, data.Data.Y);
// This is here because emiting inside
// OnRealized causes segfaults.
if (Opened != null)
Opened (this, new EventArgs ());
// Add any child widgets if any exist now that
// the window is showing.
ProcessChildWidgetQueue ();
}
return window;
}
}
public bool IsSpecial
{
get {
return NoteManager.StartNoteUri == data.Data.Uri;
}
}
public bool IsNew
{
get {
// Note is new if created in the last 24 hours.
return data.Data.CreateDate > DateTime.Now.AddHours (-24);
}
}
public bool IsLoaded
{
get {
return buffer != null;
}
}
public bool IsOpened
{
get {
return window != null;
}
}
public bool IsPinned
{
get {
string pinned_uris = (string)
Preferences.Get (Preferences.MENU_PINNED_NOTES);
return pinned_uris.IndexOf (Uri) > -1;
}
set {
string new_pinned = "";
string old_pinned = (string)
Preferences.Get (Preferences.MENU_PINNED_NOTES);
bool pinned = old_pinned.IndexOf (Uri) > -1;
if (value == pinned)
return;
if (value) {
new_pinned = Uri + " " + old_pinned;
} else {
string [] pinned_split = old_pinned.Split (' ', '\t', '\n');
foreach (string pin in pinned_split) {
if (pin != "" && pin != Uri) {
new_pinned += pin + " ";
}
}
}
Preferences.Set (Preferences.MENU_PINNED_NOTES, new_pinned);
}
}
public bool IsOpenOnStartup
{
get {
return Data.IsOpenOnStartup;
}
set {
if (Data.IsOpenOnStartup != value) {
Data.IsOpenOnStartup = value;
save_needed = true;
}
}
}
public List<Tag> Tags
{
get {
return new List<Tag> (data.Data.Tags.Values);
}
}
public event EventHandler Opened;
public event NoteRenameHandler Renamed;
public event NoteSavedHandler Saved;
public event TagAddedHandler TagAdded;
public event TagRemovingHandler TagRemoving;
public event TagRemovedHandler TagRemoved;
}
// Singleton - allow overriding the instance for easy sensing in
// test classes - we're not bothering with double-check locking,
// since this class is only seldomly used
public class NoteArchiver
{
public const string CURRENT_VERSION = "0.2";
public const string DATE_TIME_FORMAT = "yyyy-MM-ddTHH:mm:ss.fffffffzzz";
static NoteArchiver instance = null;
static readonly object lock_ = new object();
protected NoteArchiver ()
{
}
public static NoteArchiver Instance
{
get
{
lock (lock_)
{
if (instance == null)
instance = new NoteArchiver ();
return instance;
}
}
set {
lock (lock_)
{
instance = value;
}
}
}
public static NoteData Read (string read_file, string uri)
{
return Instance.ReadFile (read_file, uri);
}
public virtual NoteData ReadFile (string read_file, string uri)
{
NoteData note = new NoteData (uri);
string version = "";
StreamReader reader = new StreamReader (read_file,
System.Text.Encoding.UTF8);
XmlTextReader xml = new XmlTextReader (reader);
xml.Namespaces = false;
while (xml.Read ()) {
switch (xml.NodeType) {
case XmlNodeType.Element:
switch (xml.Name) {
case "note":
version = xml.GetAttribute ("version");
break;
case "title":
note.Title = xml.ReadString ();
break;
case "text":
// <text> is just a wrapper around <note-content>
// NOTE: Use .text here to avoid triggering a save.
note.Text = xml.ReadInnerXml ();
break;
case "last-change-date":
note.ChangeDate =
XmlConvert.ToDateTime (xml.ReadString (), DATE_TIME_FORMAT);
break;
case "create-date":
note.CreateDate =
XmlConvert.ToDateTime (xml.ReadString (), DATE_TIME_FORMAT);
break;
case "cursor-position":
note.CursorPosition = int.Parse (xml.ReadString ());
break;
case "width":
note.Width = int.Parse (xml.ReadString ());
break;
case "height":
note.Height = int.Parse (xml.ReadString ());
break;
case "x":
note.X = int.Parse (xml.ReadString ());
break;
case "y":
note.Y = int.Parse (xml.ReadString ());
break;
case "tags":
XmlDocument doc = new XmlDocument ();
List<string> tag_strings = ParseTags (doc.ReadNode (xml.ReadSubtree ()));
foreach (string tag_str in tag_strings) {
Tag tag = TagManager.GetOrCreateTag (tag_str);
note.Tags [tag.NormalizedName] = tag;
}
break;
case "open-on-startup":
note.IsOpenOnStartup = bool.Parse (xml.ReadString ());
break;
}
break;
}
}
xml.Close ();
if (version != NoteArchiver.CURRENT_VERSION) {
// Note has old format, so rewrite it. No need
// to reread, since we are not adding anything.
Logger.Log ("Updating note XML to newest format...");
NoteArchiver.Write (read_file, note);
}
return note;
}
public static void Write (string write_file, NoteData note)
{
Instance.WriteFile (write_file, note);
}
public virtual void WriteFile (string write_file, NoteData note)
{
string tmp_file = write_file + ".tmp";
XmlTextWriter xml = new XmlTextWriter (tmp_file, System.Text.Encoding.UTF8);
Write (xml, note);
xml.Close ();
if (File.Exists (write_file)) {
string backup_path = write_file + "~";
if (File.Exists (backup_path))
File.Delete (backup_path);
// Backup the to a ~ file, just in case
File.Move (write_file, backup_path);
// Move the temp file to write_file
File.Move (tmp_file, write_file);
// Delete the ~ file
File.Delete (backup_path);
} else {
// Move the temp file to write_file
File.Move (tmp_file, write_file);
}
}
public static void Write (TextWriter writer, NoteData note)
{
Instance.WriteFile (writer, note);
}
public void WriteFile (TextWriter writer, NoteData note)
{
XmlTextWriter xml = new XmlTextWriter (writer);
Write (xml, note);
xml.Close ();
}
void Write (XmlTextWriter xml, NoteData note)
{
xml.Formatting = Formatting.Indented;
xml.WriteStartDocument ();
xml.WriteStartElement (null, "note", "http://beatniksoftware.com/tomboy");
xml.WriteAttributeString(null,
"version",
null,
CURRENT_VERSION);
xml.WriteAttributeString("xmlns",
"link",
null,
"http://beatniksoftware.com/tomboy/link");
xml.WriteAttributeString("xmlns",
"size",
null,
"http://beatniksoftware.com/tomboy/size");
xml.WriteStartElement (null, "title", null);
xml.WriteString (note.Title);
xml.WriteEndElement ();
xml.WriteStartElement (null, "text", null);
xml.WriteAttributeString ("xml", "space", null, "preserve");
// Insert <note-content> blob...
xml.WriteRaw (note.Text);
xml.WriteEndElement ();
xml.WriteStartElement (null, "last-change-date", null);
xml.WriteString (
XmlConvert.ToString (note.ChangeDate, DATE_TIME_FORMAT));
xml.WriteEndElement ();
if (note.CreateDate != DateTime.MinValue) {
xml.WriteStartElement (null, "create-date", null);
xml.WriteString (
XmlConvert.ToString (note.CreateDate, DATE_TIME_FORMAT));
xml.WriteEndElement ();
}
xml.WriteStartElement (null, "cursor-position", null);
xml.WriteString (note.CursorPosition.ToString ());
xml.WriteEndElement ();
xml.WriteStartElement (null, "width", null);
xml.WriteString (note.Width.ToString ());
xml.WriteEndElement ();
xml.WriteStartElement (null, "height", null);
xml.WriteString (note.Height.ToString ());
xml.WriteEndElement ();
xml.WriteStartElement (null, "x", null);
xml.WriteString (note.X.ToString ());
xml.WriteEndElement ();
xml.WriteStartElement (null, "y", null);
xml.WriteString (note.Y.ToString ());
xml.WriteEndElement ();
if (note.Tags.Count > 0) {
xml.WriteStartElement (null, "tags", null);
foreach (Tag tag in note.Tags.Values) {
xml.WriteStartElement (null, "tag", null);
xml.WriteString (tag.Name);
xml.WriteEndElement ();
}
xml.WriteEndElement ();
}
xml.WriteStartElement (null, "open-on-startup", null);
xml.WriteString (note.IsOpenOnStartup.ToString ());
xml.WriteEndElement ();
xml.WriteEndElement (); // Note
xml.WriteEndDocument ();
}
// <summary>
// Parse the tags from the <tags> element
// </summary>
List<string> ParseTags (XmlNode tagNodes)
{
List<string> tags = new List<string> ();
foreach (XmlNode node in tagNodes.SelectNodes ("//tag")) {
string tag = node.InnerText;
tags.Add (tag);
}
return tags;
}
public virtual string GetRenamedNoteXml (string noteXml, string oldTitle, string newTitle)
{
string updatedXml;
// Replace occurences of oldTitle with newTitle in noteXml
string titleTagPattern =
string.Format ("<title>{0}</title>", oldTitle);
string titleTagReplacement =
string.Format ("<title>{0}</title>", newTitle);
updatedXml = Regex.Replace (noteXml, titleTagPattern, titleTagReplacement);
string titleContentPattern =
string.Format ("<note-content([^>]*)>\\s*{0}", oldTitle);
string titleContentReplacement =
string.Format ("<note-content$1>{0}", newTitle);
updatedXml = Regex.Replace (updatedXml, titleContentPattern, titleContentReplacement);
return updatedXml;
}
public virtual string GetTitleFromNoteXml (string noteXml)
{
if (noteXml != null && noteXml.Length > 0) {
XmlTextReader xml = new XmlTextReader (new StringReader (noteXml));
xml.Namespaces = false;
while (xml.Read ()) {
switch (xml.NodeType) {
case XmlNodeType.Element:
switch (xml.Name) {
case "title":
return xml.ReadString ();
break;
}
break;
}
}
}
return null;
}
}
public class NoteUtils
{
public static void ShowDeletionDialog (List<Note> notes, Gtk.Window parent)
{
string message;
if (notes.Count == 1)
message = Catalog.GetString ("Really delete this note?");
else
message = Catalog.GetString ("Really delete these notes?");
HIGMessageDialog dialog =
new HIGMessageDialog (
parent,
Gtk.DialogFlags.DestroyWithParent,
Gtk.MessageType.Question,
Gtk.ButtonsType.None,
message,
Catalog.GetString ("If you delete a note it is " +
"permanently lost."));
Gtk.Button button;
button = new Gtk.Button (Gtk.Stock.Cancel);
button.CanDefault = true;
button.Show ();
dialog.AddActionWidget (button, Gtk.ResponseType.Cancel);
dialog.DefaultResponse = Gtk.ResponseType.Cancel;
button = new Gtk.Button (Gtk.Stock.Delete);
button.CanDefault = true;
button.Show ();
dialog.AddActionWidget (button, 666);
int result = dialog.Run ();
if (result == 666) {
foreach (Note note in notes) {
note.Manager.Delete (note);
}
}
dialog.Destroy();
}
}
}
|