[tomboy] Allow renaming of template notes



commit faa29b032abb4b8ffd330dfcf76d42678812078c
Author: Aaron Borden <adborden live com>
Date:   Sat Apr 9 00:06:39 2011 -0700

    Allow renaming of template notes
    
    Find temlpate notes by tag instead of by name. This allows the user to modify
    the template's name without losing it.
    
    http://bugzilla.gnome.org/show_bug.cgi?id=509147

 Tomboy/Note.cs                              |    2 +-
 Tomboy/NoteManager.cs                       |   48 ++++++++++++++++++++++-----
 Tomboy/Notebooks/Notebook.cs                |   27 +++++++++-----
 Tomboy/Notebooks/NotebookManager.cs         |    4 ++-
 Tomboy/Notebooks/NotebookNewNoteMenuItem.cs |    4 ++-
 Tomboy/RecentChanges.cs                     |    4 ++-
 6 files changed, 66 insertions(+), 23 deletions(-)
---
diff --git a/Tomboy/Note.cs b/Tomboy/Note.cs
index 1fc3838..724ba96 100644
--- a/Tomboy/Note.cs
+++ b/Tomboy/Note.cs
@@ -683,7 +683,7 @@ namespace Tomboy
 		
 		public bool ContainsTag (Tag tag)
 		{
-			if (data.Data.Tags.ContainsKey (tag.NormalizedName) == true)
+			if (tag != null && data.Data.Tags.ContainsKey (tag.NormalizedName))
 				return true;
 			
 			return false;
diff --git a/Tomboy/NoteManager.cs b/Tomboy/NoteManager.cs
index 000841e..b80674b 100644
--- a/Tomboy/NoteManager.cs
+++ b/Tomboy/NoteManager.cs
@@ -534,12 +534,14 @@ Ciao!");
 			if (title == null)
 				return null;
 			
-			Note note_template = Find (NoteTemplateTitle);
-			if (note_template != null) {
+			Note note_template = GetOrCreateTemplateNote ();
+
+			if (String.IsNullOrEmpty (body)) {
 				// Use the body from the "New Note Template" note
 				string xml_content =
-					note_template.XmlContent.Replace (NoteTemplateTitle,
-					                                  XmlEncoder.Encode (title));
+					note_template.XmlContent.Replace (XmlEncoder.Encode (note_template.Title),
+						XmlEncoder.Encode (title));
+				xml_content = SanitizeXmlContent (xml_content);
 				return CreateNewNote (title, xml_content, guid);
 			}
 			
@@ -555,8 +557,7 @@ Ciao!");
 
 			Note new_note = CreateNewNote (title, content, guid);
 
-			// Select the inital
-			// "Describe..." text so typing will overwrite the body text,
+			// Select the inital text so typing will overwrite the body text
 			NoteBuffer buffer = new_note.Buffer;
 			Gtk.TextIter iter = buffer.GetIterAtOffset (header.Length);
 			buffer.MoveMark (buffer.SelectionBound, iter);
@@ -606,7 +607,19 @@ Ciao!");
 		/// </returns>
 		public Note GetOrCreateTemplateNote ()
 		{
-			Note template_note = Find (NoteTemplateTitle);
+			// The default template note will have the system template tag and
+			// will belong to zero notebooks. We find this by searching all
+			// notes with the TemplateNoteSystemTag and check that it's
+			// notebook == null
+			Note template_note = null;
+			Tag template_tag = TagManager.GetOrCreateSystemTag (TagManager.TemplateNoteSystemTag);
+			foreach (Note note in template_tag.Notes) {
+				if (Notebooks.NotebookManager.GetNotebookFromNote (note) == null) {
+					template_note = note;
+					break;
+				}
+			}
+			
 			if (template_note == null) {
 				template_note =
 					Create (NoteTemplateTitle,
@@ -619,8 +632,7 @@ Ciao!");
 				buffer.MoveMark (buffer.InsertMark, buffer.EndIter);
 
 				// Flag this as a template note
-				Tag tag = TagManager.GetOrCreateSystemTag (TagManager.TemplateNoteSystemTag);
-				template_note.AddTag (tag);
+				template_note.AddTag (template_tag);
 
 				template_note.QueueSave (ChangeType.ContentChanged);
 			}
@@ -658,6 +670,24 @@ Ciao!");
 			}
 			return null;
 		}
+		
+		// Removes any trailing whitespace on the title line
+		public static string SanitizeXmlContent (string xml_content)
+		{
+			int i = String.IsNullOrEmpty (xml_content) ? -1 : xml_content.IndexOf ('\n');
+			while (--i >= 0) {
+				if (xml_content [i].Equals ('\r'))
+					continue;
+			
+				if (Char.IsWhiteSpace (xml_content [i]))
+					xml_content = xml_content.Remove (i,1);
+				else
+					break;
+			}
+			
+			return xml_content;
+		}
+
 
 		class CompareDates : IComparer<Note>
 		{
diff --git a/Tomboy/Notebooks/Notebook.cs b/Tomboy/Notebooks/Notebook.cs
index 7e2daf3..8780a25 100644
--- a/Tomboy/Notebooks/Notebook.cs
+++ b/Tomboy/Notebooks/Notebook.cs
@@ -109,32 +109,39 @@ namespace Tomboy.Notebooks
 		public virtual Note GetTemplateNote ()
 		{
 			NoteManager noteManager = Tomboy.DefaultNoteManager;
-			Note note = noteManager.Find (templateNoteTitle);
-			if (note == null) {
-				note =
+			Note template_note = null;
+			Tag template_tag = TagManager.GetOrCreateSystemTag (TagManager.TemplateNoteSystemTag);
+			Tag notebook_tag = TagManager.GetOrCreateSystemTag (NotebookTagPrefix + Name);
+			foreach (Note note in template_tag.Notes) {
+				if (note.ContainsTag (notebook_tag)) {
+					template_note = note;
+					break;
+				}
+			}
+			
+			if (template_note == null) {
+				template_note =
 					noteManager.Create (templateNoteTitle,
 							NoteManager.GetNoteTemplateContent (templateNoteTitle));
 					
 				// Select the initial text
-				NoteBuffer buffer = note.Buffer;
+				NoteBuffer buffer = template_note.Buffer;
 				Gtk.TextIter iter = buffer.GetIterAtLineOffset (2, 0);
 				buffer.MoveMark (buffer.SelectionBound, iter);
 				buffer.MoveMark (buffer.InsertMark, buffer.EndIter);
 
 				// Flag this as a template note
-				Tag tag = TagManager.GetOrCreateSystemTag (TagManager.TemplateNoteSystemTag);
-				note.AddTag (tag);
+				template_note.AddTag (template_tag);
 
 				// Add on the notebook system tag so Tomboy
 				// will persist the tag/notebook across sessions
 				// if no other notes are added to the notebook.
-				tag = TagManager.GetOrCreateSystemTag (NotebookTagPrefix + Name);
-				note.AddTag (tag);
+				template_note.AddTag (notebook_tag);
 				
-				note.QueueSave (ChangeType.ContentChanged);
+				template_note.QueueSave (ChangeType.ContentChanged);
 			}
 			
-			return note;
+			return template_note;
 		}
 		
 		/// <summary>
diff --git a/Tomboy/Notebooks/NotebookManager.cs b/Tomboy/Notebooks/NotebookManager.cs
index 8027c8c..2a7d4da 100644
--- a/Tomboy/Notebooks/NotebookManager.cs
+++ b/Tomboy/Notebooks/NotebookManager.cs
@@ -375,10 +375,12 @@ namespace Tomboy.Notebooks
 			if (response != (int) Gtk.ResponseType.Yes)
 				return;
 			
+			// Grab the template note before removing all the notebook tags
+			Note templateNote = notebook.GetTemplateNote ();
+			
 			DeleteNotebook (notebook);
 
 			// Delete the template note
-			Note templateNote = notebook.GetTemplateNote ();
 			if (templateNote != null) {
 				NoteManager noteManager = Tomboy.DefaultNoteManager;
 				noteManager.Delete (templateNote);
diff --git a/Tomboy/Notebooks/NotebookNewNoteMenuItem.cs b/Tomboy/Notebooks/NotebookNewNoteMenuItem.cs
index 04e7f72..a28fd8a 100644
--- a/Tomboy/Notebooks/NotebookNewNoteMenuItem.cs
+++ b/Tomboy/Notebooks/NotebookNewNoteMenuItem.cs
@@ -47,7 +47,9 @@ namespace Tomboy.Notebooks
 			note = noteManager.Create ();
 			if (templateNote != null) {
 				// Use the body from the template note
-				string xmlContent = templateNote.XmlContent.Replace (templateNote.Title, note.Title);
+				string xmlContent = templateNote.XmlContent.Replace (XmlEncoder.Encode (templateNote.Title),
+					XmlEncoder.Encode (note.Title));
+				xmlContent = NoteManager.SanitizeXmlContent (xmlContent);
 				note.XmlContent = xmlContent;
 			}
 			
diff --git a/Tomboy/RecentChanges.cs b/Tomboy/RecentChanges.cs
index 9f6547c..8d90a06 100644
--- a/Tomboy/RecentChanges.cs
+++ b/Tomboy/RecentChanges.cs
@@ -1420,7 +1420,9 @@ namespace Tomboy
 			note = manager.Create ();
 			if (templateNote != null) {
 				// Use the body from the template note
-				string xmlContent = templateNote.XmlContent.Replace (templateNote.Title, note.Title);
+				string xmlContent = templateNote.XmlContent.Replace (XmlEncoder.Encode (templateNote.Title),
+					XmlEncoder.Encode (note.Title));
+				xmlContent = NoteManager.SanitizeXmlContent (xmlContent);
 				note.XmlContent = xmlContent;
 			}
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]