[gnote] Fix template note rename



commit ac25f9ca2bae0af57b4f97403eecccea1249eb2e
Author: Aurimas Ä?ernius <aurisc4 gmail com>
Date:   Thu Dec 30 22:34:13 2010 +0200

    Fix template note rename
    
    Do not lose template note after renaming it.
    Fixes Bug 627539.

 src/note.cpp                              |    4 +
 src/notebooks/notebook.cpp                |   57 +++++++++++++++----
 src/notebooks/notebook.hpp                |    4 +-
 src/notebooks/notebookmanager.cpp         |    4 +-
 src/notebooks/notebooknewnotemenuitem.cpp |   16 +-----
 src/notemanager.cpp                       |   86 +++++++++++++++++------------
 src/notemanager.hpp                       |    4 +-
 src/recentchanges.cpp                     |   14 +----
 8 files changed, 112 insertions(+), 77 deletions(-)
---
diff --git a/src/note.cpp b/src/note.cpp
index 379ec81..92ec871 100644
--- a/src/note.cpp
+++ b/src/note.cpp
@@ -1,6 +1,7 @@
 /*
  * gnote
  *
+ * Copyright (C) 2010 Aurimas Cernius
  * Copyright (C) 2009 Hubert Figuiere
  *
  * This program is free software: you can redistribute it and/or modify
@@ -707,6 +708,9 @@ namespace gnote {
     
   bool Note::contains_tag(const Tag::Ptr & tag) const
   {
+    if(!tag) {
+      return false;
+    }
     const NoteData::TagMap & thetags(m_data.data().tags());
     return (thetags.find(tag->normalized_name()) != thetags.end());
   }
diff --git a/src/notebooks/notebook.cpp b/src/notebooks/notebook.cpp
index 8b70442..494406e 100644
--- a/src/notebooks/notebook.cpp
+++ b/src/notebooks/notebook.cpp
@@ -1,6 +1,7 @@
 /*
  * gnote
  *
+ * Copyright (C) 2010 Aurimas Cernius
  * Copyright (C) 2009 Hubert Figuiere
  *
  * This program is free software: you can redistribute it and/or modify
@@ -84,7 +85,7 @@ namespace notebooks {
       // Notebook Template".  Translators should place the
       // name of the notebook accordingly using "%1%".
       std::string format = _("%1% Notebook Template");
-      m_template_note_title = str(boost::format(format) % m_name);
+      m_default_template_note_title = str(boost::format(format) % m_name);
     }
   }
 
@@ -104,12 +105,28 @@ namespace notebooks {
   Note::Ptr Notebook::get_template_note() const
   {
     NoteManager & noteManager = Gnote::obj().default_note_manager();
-    Note::Ptr note = noteManager.find (m_template_note_title);
+    Note::Ptr note;
+    Tag::Ptr template_tag = TagManager::obj().get_or_create_system_tag (TagManager::TEMPLATE_NOTE_SYSTEM_TAG);
+    Tag::Ptr notebook_tag = TagManager::obj().get_or_create_system_tag (NOTEBOOK_TAG_PREFIX + get_name());
+    std::list<Note*> notes;
+    template_tag->get_notes(notes);
+    for (std::list<Note*>::iterator iter = notes.begin(); iter != notes.end(); ++iter) {
+      if ((*iter)->contains_tag (notebook_tag)) {
+        note = (*iter)->shared_from_this();
+        break;
+      }
+    }
+
     if (!note) {
+      std::string title = m_default_template_note_title;
+      if (noteManager.find(title)) {
+        std::list<Note*> tag_notes;
+        m_tag->get_notes(tag_notes);
+        title = noteManager.get_unique_name (title, tag_notes.size());
+      }
       note =
-        noteManager.create (m_template_note_title,
-                            NoteManager::get_note_template_content (
-                              m_template_note_title));
+        noteManager.create (title,
+                            NoteManager::get_note_template_content (title));
           
       // Select the initial text
       NoteBuffer::Ptr buffer = note->get_buffer();
@@ -118,16 +135,12 @@ namespace notebooks {
       buffer->move_mark (buffer->get_insert(), buffer->end());
 
       // Flag this as a template note
-      Tag::Ptr tag = TagManager::obj()
-        .get_or_create_system_tag (TagManager::TEMPLATE_NOTE_SYSTEM_TAG);
-      note->add_tag (tag);
+      note->add_tag (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::obj()
-        .get_or_create_system_tag (NOTEBOOK_TAG_PREFIX + get_name());
-      note->add_tag (tag);
+      note->add_tag (notebook_tag);
         
       note->queue_save (Note::CONTENT_CHANGED);
     }
@@ -135,6 +148,28 @@ namespace notebooks {
     return note;
   }
 
+  Note::Ptr Notebook::create_notebook_note()
+  {
+    std::string temp_title;
+    Note::Ptr template_note = get_template_note();
+    NoteManager & note_manager = Gnote::obj().default_note_manager();
+
+    temp_title = note_manager.get_unique_name (_("New Note"), note_manager.get_notes().size());
+
+    // Grab the template body
+    std::string xml_content =
+        sharp::string_replace_first(template_note->xml_content(),
+                                    template_note->get_title(),
+                                    utils::XmlEncoder::encode (temp_title));
+
+    Note::Ptr note = note_manager.create (temp_title, xml_content);
+
+    // Add the notebook tag
+    note->add_tag (m_tag);
+
+    return note;
+  }
+
   /// <summary>
   /// Returns true when the specified note exists in the notebook
   /// </summary>
diff --git a/src/notebooks/notebook.hpp b/src/notebooks/notebook.hpp
index e0b6396..620695e 100644
--- a/src/notebooks/notebook.hpp
+++ b/src/notebooks/notebook.hpp
@@ -1,6 +1,7 @@
 /*
  * gnote
  *
+ * Copyright (C) 2010 Aurimas Cernius
  * Copyright (C) 2009 Hubert Figuiere
  *
  * This program is free software: you can redistribute it and/or modify
@@ -48,6 +49,7 @@ public:
   virtual std::string get_normalized_name() const;
   virtual Tag::Ptr    get_tag() const;
   virtual Note::Ptr   get_template_note() const;
+  Note::Ptr create_notebook_note();
   bool contains_note(const Note::Ptr &);
   static std::string normalize(const std::string & s);
 ////
@@ -58,7 +60,7 @@ private:
   Notebook & operator=(const Notebook &);
   std::string m_name;
   std::string m_normalized_name;
-  std::string m_template_note_title;
+  std::string m_default_template_note_title;
   Tag::Ptr    m_tag;
 };
 
diff --git a/src/notebooks/notebookmanager.cpp b/src/notebooks/notebookmanager.cpp
index 28ff689..51f3e7f 100644
--- a/src/notebooks/notebookmanager.cpp
+++ b/src/notebooks/notebookmanager.cpp
@@ -344,11 +344,13 @@ namespace gnote {
       if (response != Gtk::RESPONSE_YES) {
         return;
       }
+
+      // Grab the template note before removing all the notebook tags
+      Note::Ptr templateNote = notebook->get_template_note ();
       
       instance().delete_notebook (notebook);
 
       // Delete the template note
-      Note::Ptr templateNote = notebook->get_template_note ();
       if (templateNote) {
         NoteManager & noteManager(Gnote::obj().default_note_manager());
         noteManager.delete_note (templateNote);
diff --git a/src/notebooks/notebooknewnotemenuitem.cpp b/src/notebooks/notebooknewnotemenuitem.cpp
index 423ce7c..26bed64 100644
--- a/src/notebooks/notebooknewnotemenuitem.cpp
+++ b/src/notebooks/notebooknewnotemenuitem.cpp
@@ -1,6 +1,7 @@
 /*
  * gnote
  *
+ * Copyright (C) 2010 Aurimas Cernius
  * Copyright (C) 2009 Hubert Figuiere
  *
  * This program is free software: you can redistribute it and/or modify
@@ -51,20 +52,7 @@ namespace gnote {
       }
       
       // Look for the template note and create a new note
-      Note::Ptr templateNote = m_notebook->get_template_note ();
-      Note::Ptr note;
-      
-      NoteManager & noteManager(Gnote::obj().default_note_manager());
-      note = noteManager.create ();
-      if (templateNote) {
-        // Use the body from the template note
-        std::string xmlContent = sharp::string_replace_all(templateNote->xml_content(), 
-                                                           templateNote->get_title(), 
-                                                           note->get_title());
-        note->set_xml_content(xmlContent);
-      }
-      
-      note->add_tag (m_notebook->get_tag());
+      Note::Ptr note = m_notebook->create_notebook_note ();
       note->get_window()->show ();
     }
 
diff --git a/src/notemanager.cpp b/src/notemanager.cpp
index 7c9815b..0173d87 100644
--- a/src/notemanager.cpp
+++ b/src/notemanager.cpp
@@ -1,6 +1,7 @@
 /*
  * gnote
  *
+ * Copyright (C) 2010 Aurimas Cernius
  * Copyright (C) 2010 Debarshi Ray
  * Copyright (C) 2009 Hubert Figuiere
  *
@@ -46,6 +47,7 @@
 #include "sharp/uuid.hpp"
 #include "sharp/string.hpp"
 #include "sharp/datetime.hpp"
+#include "notebooks/notebookmanager.hpp"
 
 namespace gnote {
 
@@ -105,7 +107,7 @@ namespace gnote {
     m_start_note_uri = prefs.get<std::string>(Preferences::START_NOTE_URI);
     prefs.signal_setting_changed().connect(
       sigc::mem_fun(*this, &NoteManager::on_setting_changed));
-    m_note_template_title = _("New Note Template");
+    m_default_note_template_title = _("New Note Template");
 
 
     DBG_OUT("NoteManager created with note path \"%s\".", directory.c_str());
@@ -472,18 +474,7 @@ namespace gnote {
 
   Note::Ptr NoteManager::create()
   {
-    int new_num = m_notes.size();
-    std::string temp_title;
-    
-    while (true) {
-      ++new_num;
-      temp_title = str(boost::format(_("New Note %1%")) %  new_num);
-      if (!find(temp_title)) {
-        break;
-      }
-    }
-
-    return create(temp_title);
+    return create("");
   }
 
   std::string NoteManager::split_title_from_content(std::string title, std::string & body)
@@ -553,32 +544,28 @@ namespace gnote {
     return create_new_note(title, guid);
   }
 
-  // Create a new note with the specified title, and a simple
-  // "Describe..." body or the body from the "New Note Template"
-  // note if it exists.  If the "New Note Template" body is found
-  // the text will not automatically be highlighted.
+  // Create a new note with the specified title from the default
+  // template note. Optionally the body can be overridden.
   Note::Ptr NoteManager::create_new_note (std::string title, const std::string & guid)
   {
     std::string body;
-    
+
+    Note::Ptr template_note = get_or_create_template_note();
     title = split_title_from_content (title, body);
-    if (title.empty())
-      return Note::Ptr();
       
-    Note::Ptr note_template = find(m_note_template_title);
-    if (note_template) {
-      // Use the body from the "New Note Template" note
+    if (title.empty()) {
+      title = get_unique_name(_("New Note"), m_notes.size());
+    }
+
+    if (body.empty()) {
+      // Use the body from the template note
       std::string xml_content =
-        sharp::string_replace_first(note_template->xml_content(), 
-                                    m_note_template_title,
+        sharp::string_replace_first(template_note->xml_content(),
+                                    template_note->get_title(),
                                     utils::XmlEncoder::encode (title));
       return create_new_note (title, xml_content, guid);
     }
       
-    // Use a simple "Describe..." body and highlight
-    // it so it can be easily overwritten
-    body = _("Describe your new note here.");
-    
     Glib::ustring header = title + "\n\n";
     std::string content =
       boost::str(boost::format("<note-content>%1%%2%</note-content>") %
@@ -588,8 +575,6 @@ namespace gnote {
     Note::Ptr new_note = create_new_note (title, content, guid);
     
     // Select the inital
-    // "Describe..." text so typing will overwrite the body text,
-    //NoteBuffer 
     Glib::RefPtr<Gtk::TextBuffer> buffer = new_note->get_buffer();
     Gtk::TextIter iter = buffer->get_iter_at_offset(header.size());
     buffer->move_mark (buffer->get_selection_bound(), iter);
@@ -638,11 +623,26 @@ namespace gnote {
   /// </returns>
   Note::Ptr NoteManager::get_or_create_template_note()
   {
-    Note::Ptr template_note = find(m_note_template_title);
+    Note::Ptr template_note;
+    Tag::Ptr template_tag = TagManager::obj().get_or_create_system_tag (TagManager::TEMPLATE_NOTE_SYSTEM_TAG);
+    std::list<Note*> notes;
+    template_tag->get_notes(notes);
+    for (std::list<Note*>::iterator iter = notes.begin(); iter != notes.end(); ++iter) {
+      Note::Ptr note = (*iter)->shared_from_this();
+      if (!notebooks::NotebookManager::instance().get_notebook_from_note (note)) {
+        template_note = note;
+        break;
+      }
+    }
+
     if (!template_note) {
+      std::string title = m_default_note_template_title;
+      if (find(title)) {
+        title = get_unique_name(title, m_notes.size());
+      }
       template_note =
-        create (m_note_template_title,
-                get_note_template_content(m_note_template_title));
+        create (title,
+                get_note_template_content(title));
           
       // Select the initial text
       Glib::RefPtr<NoteBuffer> buffer = template_note->get_buffer();
@@ -651,8 +651,7 @@ namespace gnote {
       buffer->move_mark(buffer->get_insert(), buffer->end());
 
       // Flag this as a template note
-      Tag::Ptr tag = TagManager::obj().get_or_create_system_tag(TagManager::TEMPLATE_NOTE_SYSTEM_TAG);
-      template_note->add_tag(tag);
+      template_note->add_tag(template_tag);
 
       template_note->queue_save(Note::CONTENT_CHANGED);
     }
@@ -704,6 +703,21 @@ namespace gnote {
     return Note::Ptr();
   }
 
+  // Find a title that does not exist using basename and id as
+  // a starting point
+  std::string NoteManager::get_unique_name (std::string basename, int id) const
+  {
+    std::string title;
+    while (true) {
+      title = str(boost::format("%1% %2%") % basename % id++);
+      if (!find (title)) {
+        break;
+      }
+    }
+
+    return title;
+  }
+
 
   TrieController::TrieController (NoteManager & manager)
     : m_manager(manager)
diff --git a/src/notemanager.hpp b/src/notemanager.hpp
index dc8a801..b62fac1 100644
--- a/src/notemanager.hpp
+++ b/src/notemanager.hpp
@@ -1,6 +1,7 @@
 /*
  * gnote
  *
+ * Copyright (C) 2010 Aurimas Cernius
  * Copyright (C) 2009 Hubert Figuiere
  *
  * This program is free software: you can redistribute it and/or modify
@@ -76,6 +77,7 @@ namespace gnote {
       }
     Note::Ptr find(const std::string &) const;
     Note::Ptr find_by_uri(const std::string &) const;
+    std::string get_unique_name (std::string basename, int id) const;
     void delete_note(const Note::Ptr & note);
 
     Note::Ptr create();
@@ -127,7 +129,7 @@ namespace gnote {
     Note::List m_notes;
     AddinManager   *m_addin_mgr;
     TrieController *m_trie_controller;
-    std::string m_note_template_title;
+    std::string m_default_note_template_title;
     std::string m_start_note_uri;
     NoteChangedSlot m_signal_start_note_created;
   };
diff --git a/src/recentchanges.cpp b/src/recentchanges.cpp
index 2162e14..3eca77e 100644
--- a/src/recentchanges.cpp
+++ b/src/recentchanges.cpp
@@ -1416,19 +1416,7 @@ namespace gnote {
     }
       
     // Look for the template note and create a new note
-    Note::Ptr templateNote = notebook->get_template_note ();
-    Note::Ptr note;
-      
-    note = m_manager.create ();
-    if (templateNote) {
-      // Use the body from the template note
-      std::string xmlContent = sharp::string_replace_first(templateNote->xml_content(), 
-                                                           templateNote->get_title(), 
-                                                           note->get_title());
-      note->set_xml_content(xmlContent);
-    }
-      
-    note->add_tag(notebook->get_tag());
+    Note::Ptr note = notebook->create_notebook_note ();
     note->get_window()->show ();
   }
     



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