-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathNote.cs
1768 lines (1524 loc) · 44.6 KB
/
Note.cs
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
using System;
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);
public enum ChangeType
{
NoChange,
ContentChanged,
OtherDataChanged
}
// 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;
DateTime metadata_change_date;
int cursor_pos, selection_bound_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;
selection_bound_pos = noPosition;
tags = new Dictionary<string, Tag> ();
create_date = DateTime.MinValue;
change_date = DateTime.MinValue;
metadata_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;
}
}
/// <summary>
/// Indicates the last time note content data changed.
/// Does not include tag/notebook changes (see MetadataChangeDate).
/// </summary>
public DateTime ChangeDate
{
get {
return change_date;
}
set {
change_date = value;
metadata_change_date = value;
}
}
/// <summary>
/// Indicates the last time non-content note data changed.
/// This currently only applies to tags/notebooks.
/// </summary>
public DateTime MetadataChangeDate
{
get {
return metadata_change_date;
}
set {
metadata_change_date = value;
}
}
// FIXME: the next six 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 SelectionBoundPosition
{
get {
return selection_bound_pos;
}
set {
selection_bound_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)
{
if (x < 0 || y < 0)
return;
if (width <= 0 || height <= 0)
return;
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 += OnBufferChanged;
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);
if (data.SelectionBoundPosition >= 0) {
// Move selection bound to last-saved position
Gtk.TextIter selection_bound;
selection_bound = buffer.GetIterAtOffset (data.SelectionBoundPosition);
buffer.MoveMark (buffer.SelectionBound.Name, selection_bound);
}
// New events should create Undo actions
buffer.Undoer.ThawUndo ();
}
}
// Callbacks
void OnBufferChanged (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;
bool enabled = true;
bool save_errordlg_active;
NoteManager manager;
NoteWindow window;
NoteBuffer buffer;
NoteTagTable tag_table;
InterruptableTimeout save_timeout;
// By default we'll be saving our notes every 4 seconds
const uint SAVE_TIMEOUT_MS = 4000;
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;
save_errordlg_active = false;
}
/// <summary>
/// Returns a Tomboy URL from the given path.
/// </summary>
/// <param name="filepath">
/// A <see cref="System.String"/>
/// </param>
/// <returns>
/// A <see cref="System.String"/>
/// </returns>
static string UrlFromPath (string filepath)
{
return "note://tomboy/" +
Path.GetFileNameWithoutExtension (filepath);
}
public override int GetHashCode ()
{
return this.Title.GetHashCode();
}
/// <summary>
/// Creates a New Note with the given values.
/// </summary>
/// <param name="title">
/// A <see cref="System.String"/>
/// </param>
/// <param name="filepath">
/// A <see cref="System.String"/>
/// </param>
/// <param name="manager">
/// A <see cref="NoteManager"/>
/// </param>
/// <returns>
/// A <see cref="Note"/>
/// </returns>
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;
// Do nothing if an error happened and we are presenting an error dialog
if (save_errordlg_active) {
Logger.Debug("Error dialog is active - skipping saving");
return;
}
string new_note_pattern = String.Format (Catalog.GetString ("New Note {0}"), @"\d+");
Note template_note = manager.GetOrCreateTemplateNote ();
string template_content = template_note.TextContent.Replace (template_note.Title, Title);
// Do nothing if this note contains the unchanged template content
// and if the title matches the new note title template to prevent
// lots of unwanted "New Note NNN" notes: Bug #545252
if (Regex.IsMatch (Title, new_note_pattern) && TextContent.Equals (template_content))
return;
Logger.Debug ("Saving '{0}'...", data.Data.Title);
try {
NoteArchiver.Write (filepath, data.GetDataSynchronized ());
} catch (Exception e) {
// Probably IOException or UnauthorizedAccessException?
Logger.Error ("Exception while saving note: " + e.ToString ());
// This will disable note saving until the user takes an action
// and closes the error dialog.
save_errordlg_active = true;
save_timeout.Cancel ();
NoteUtils.ShowIOErrorDialog (window);
save_errordlg_active = false;
save_timeout.Reset(SAVE_TIMEOUT_MS);
}
if (Saved != null)
Saved (this);
}
//
// Buffer change signals. These queue saves and invalidate the serialized text
// depending on the change...
//
void OnBufferChanged (object sender, EventArgs args)
{
DebugSave ("OnBufferChanged queueing save");
QueueSave (ChangeType.ContentChanged);
if (BufferChanged != null)
BufferChanged (this);
}
void BufferTagApplied (object sender, Gtk.TagAppliedArgs args)
{
if (NoteTagTable.TagIsSerializable (args.Tag)) {
DebugSave ("BufferTagApplied queueing save: {0}", args.Tag.Name);
QueueSave (TagTable.GetChangeType (args.Tag));
}
}
void BufferTagRemoved (object sender, Gtk.TagRemovedArgs args)
{
if (NoteTagTable.TagIsSerializable (args.Tag)) {
DebugSave ("BufferTagRemoved queueing save: {0}", args.Tag.Name);
QueueSave (TagTable.GetChangeType (args.Tag));
}
}
void OnBufferMarkSet (object sender, Gtk.MarkSetArgs args)
{
if (args.Mark == buffer.InsertMark)
data.Data.CursorPosition = args.Location.Offset;
else if (args.Mark == buffer.SelectionBound)
data.Data.SelectionBoundPosition = args.Location.Offset;
else
return;
DebugSave ("OnBufferSetMark queueing save");
QueueSave (ChangeType.NoChange);
}
//
// 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 (ChangeType.NoChange);
}
[GLib.ConnectBefore]
void WindowDestroyed (object sender, EventArgs args)
{
window = null;
}
/// <summary>
/// Set a 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 (ChangeType changeType)
{
DebugSave ("Got QueueSave");
// Replace the existing save timeout. Wait SAVE_TIMEOUT_MS milliseconds
// before saving...
save_timeout.Reset (SAVE_TIMEOUT_MS);
if (!is_deleting)
save_needed = true;
switch (changeType)
{
case ChangeType.ContentChanged:
// NOTE: Updating ChangeDate automatically updates MetdataChangeDate to match.
data.Data.ChangeDate = DateTime.Now;
break;
case ChangeType.OtherDataChanged:
// Only update MetadataChangeDate. Used by sync/etc
// to know when non-content note data has changed,
// but order of notes in menu and search UI is
// unaffected.
data.Data.MetadataChangeDate = DateTime.Now;
break;
default:
break;
}
}
// Save timeout to avoid constanly resaving. Called every SAVE_TIMEOUT_MS milliseconds.
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.Error ("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 (ChangeType.OtherDataChanged);
}
}
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 (ChangeType.OtherDataChanged);
}
public bool ContainsTag (Tag tag)
{
if (tag != null && data.Data.Tags.ContainsKey (tag.NormalizedName))
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 {
SetTitle (value, false);
}
}
public void SetTitle (string new_title, bool from_user_action)
{
if (data.Data.Title != new_title) {
if (window != null)
window.Title = new_title;
string old_title = data.Data.Title;
data.Data.Title = new_title;
if (from_user_action)
ProcessRenameLinkUpdate (old_title);
if (Renamed != null)
Renamed (this, old_title);
QueueSave (ChangeType.ContentChanged); // TODO: Right place for this?
}
}
private void ProcessRenameLinkUpdate (string old_title)
{
List<Note> linkingNotes = new List<Note> ();
foreach (Note note in manager.Notes) {
// Technically, containing text does not imply linking,
// but this is less work
if (note != this && note.ContainsText (old_title))
linkingNotes.Add (note);
}
if (linkingNotes.Count > 0) {
NoteRenameBehavior behavior = (NoteRenameBehavior)
Preferences.Get (Preferences.NOTE_RENAME_BEHAVIOR);
if (behavior == NoteRenameBehavior.AlwaysShowDialog) {
var dlg = new NoteRenameDialog (linkingNotes, old_title, this);
Gtk.ResponseType response = (Gtk.ResponseType) dlg.Run ();
if (response != Gtk.ResponseType.Cancel &&
dlg.SelectedBehavior != NoteRenameBehavior.AlwaysShowDialog)
Preferences.Set (Preferences.NOTE_RENAME_BEHAVIOR, (int) dlg.SelectedBehavior);
foreach (var pair in dlg.Notes) {
if (pair.Value && response == Gtk.ResponseType.Yes) // Rename
pair.Key.RenameLinks (old_title, this);
else
pair.Key.RemoveLinks (old_title, this);
}
dlg.Destroy ();
} else if (behavior == NoteRenameBehavior.AlwaysRemoveLinks)
foreach (var note in linkingNotes)
note.RemoveLinks (old_title, this);
else if (behavior == NoteRenameBehavior.AlwaysRenameLinks)
foreach (var note in linkingNotes)
note.RenameLinks (old_title, this);
}
}
private bool ContainsText (string text)
{
return TextContent.IndexOf (text, StringComparison.InvariantCultureIgnoreCase) > -1;
}
private void RenameLinks (string old_title, Note renamed)
{
HandleLinkRename (old_title, renamed, true);
}
private void RemoveLinks (string old_title, Note renamed)
{
HandleLinkRename (old_title, renamed, false);
}
private void HandleLinkRename (string old_title, Note renamed, bool rename_links)
{
// Check again, things may have changed
if (!ContainsText (old_title))
return;
string old_title_lower = old_title.ToLower ();
NoteTag link_tag = TagTable.LinkTag;
// Replace existing links with the new title.
TextTagEnumerator enumerator = new TextTagEnumerator (Buffer, link_tag);
foreach (TextRange range in enumerator) {
if (range.Text.ToLower () != old_title_lower)
continue;
if (!rename_links) {
Logger.Debug ("Removing link tag from text '{0}'",
range.Text);
Buffer.RemoveTag (link_tag, range.Start, range.End);
} else {
Logger.Debug ("Replacing '{0}' with '{1}'",
range.Text,
renamed.Title);
Gtk.TextIter start_iter = range.Start;
Gtk.TextIter end_iter = range.End;
Buffer.Delete (ref start_iter, ref end_iter);
start_iter = range.Start;
Buffer.InsertWithTags (ref start_iter, renamed.Title, link_tag);
}
}
}
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 (ChangeType.ContentChanged); // TODO: Right place for this?
}
}
public string XmlContent
{
get {
return data.Text;
}
set {
if (buffer != null) {
buffer.Text = string.Empty;
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 ()
{
return NoteArchiver.WriteString (data.GetDataSynchronized ());
}
// Reload note data from a complete note XML string
// Should referesh note window, too
public void LoadForeignNoteXml (string foreignNoteXml, ChangeType changeType)
{
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;
// Remove tags now, since a note with no tags has
// no "tags" element in the XML
List<Tag> newTags = new List<Tag> ();
using (var xml = new XmlTextReader (new StringReader (foreignNoteXml)) {Namespaces = false}) {
DateTime date;
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":
if (DateTime.TryParse (xml.ReadString (), out date))
data.Data.ChangeDate = date;
else
data.Data.ChangeDate = DateTime.Now;
break;
case "last-metadata-change-date":
if (DateTime.TryParse (xml.ReadString (), out date))
data.Data.MetadataChangeDate = date;
else
data.Data.MetadataChangeDate = DateTime.Now;
break;
case "create-date":
if (DateTime.TryParse (xml.ReadString (), out date))
data.Data.CreateDate = date;
else
data.Data.CreateDate = DateTime.Now;
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);
newTags.Add (tag);
}
break;
case "open-on-startup":
bool isStartup;
if (bool.TryParse (xml.ReadString (), out isStartup))
IsOpenOnStartup = isStartup;
break;
}
break;
}
}
}
foreach (Tag oldTag in Tags)
if (!newTags.Contains (oldTag))