[gnote] New note body selection when default



commit 07f72fb4b8c115fc9cae29a88b4af896110c99ea
Author: Aurimas Ä?ernius <aurisc4 gmail com>
Date:   Sun Feb 27 19:05:21 2011 +0200

    New note body selection when default
    
    Select new note body if template does not exist or is the same as default.
    Fixes Bug 627073.

 src/notemanager.cpp |   42 ++++++++++++++++++++++++------------------
 src/notemanager.hpp |    1 +
 2 files changed, 25 insertions(+), 18 deletions(-)
---
diff --git a/src/notemanager.cpp b/src/notemanager.cpp
index d396bf0..a148c36 100644
--- a/src/notemanager.cpp
+++ b/src/notemanager.cpp
@@ -549,8 +549,6 @@ namespace gnote {
   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()) {
@@ -558,29 +556,37 @@ namespace gnote {
     }
 
     if (body.empty()) {
+      std::string content = get_note_template_content(title);
+      Note::Ptr new_note = create_new_note (title, content, guid);
+      new_note->get_buffer()->select_note_body();
       // Use the body from the template note
-      std::string xml_content =
-        sharp::string_replace_first(template_note->xml_content(),
-                                    template_note->get_title(),
-                                    utils::XmlEncoder::encode (title));
-      return create_new_note (title, xml_content, guid);
+      Note::Ptr template_note = find_template_note();
+      if(template_note)
+        replace_body_if_differ(new_note, template_note);
+      return new_note;
     }
-      
+
     Glib::ustring header = title + "\n\n";
     std::string content =
       boost::str(boost::format("<note-content>%1%%2%</note-content>") %
                  utils::XmlEncoder::encode (header) 
                  % utils::XmlEncoder::encode (body));
-    
-    Note::Ptr new_note = create_new_note (title, content, guid);
-    
-    // Select the inital
-    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);
-    buffer->move_mark (buffer->get_insert(), buffer->end());
-    
-    return new_note;
+    return create_new_note (title, content, guid);
+  }
+
+  //replace dest body by src one, if the text is different
+  void NoteManager::replace_body_if_differ(Note::Ptr dest, const Note::Ptr src)
+  {
+    std::string dest_body = sharp::string_trim(sharp::string_replace_first(dest->text_content(),
+                                                                           dest->get_title(), ""));
+    std::string src_body = sharp::string_trim(sharp::string_replace_first(src->text_content(),
+                                                                          src->get_title(), ""));
+    if(dest_body != src_body) {
+      std::string xml_content = sharp::string_replace_first(src->xml_content(),
+        sharp::string_trim(utils::XmlEncoder::encode(src->get_title())),
+        sharp::string_trim(utils::XmlEncoder::encode(dest->get_title())));
+      dest->set_xml_content(xml_content);
+    }
   }
 
   // Create a new note with the specified Xml content
diff --git a/src/notemanager.hpp b/src/notemanager.hpp
index 110c323..69c6d5f 100644
--- a/src/notemanager.hpp
+++ b/src/notemanager.hpp
@@ -83,6 +83,7 @@ namespace gnote {
     Note::Ptr create();
     Note::Ptr create(const std::string & title);
     Note::Ptr create(const std::string & title, const std::string & xml_content);
+    static void replace_body_if_differ(Note::Ptr dest, const Note::Ptr src);
     // Import a note read from file_path
     // Will ensure the sanity including the unique title.
     Note::Ptr import_note(const std::string & file_path);



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