1 /* 2 * gnote 3 * 4 * Copyright (C) 2010-2014,2017,2019 Aurimas Cernius 5 * Copyright (C) 2009 Hubert Figuiere 6 * 7 * This program is free software: you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation, either version 3 of the License, or 10 * (at your option) any later version. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with this program. If not, see <http://www.gnu.org/licenses/>. 19 */ 20 21 22 23 #include <glibmm/i18n.h> 24 25 #include "sharp/string.hpp" 26 #include "notemanager.hpp" 27 #include "notebooks/notebook.hpp" 28 #include "notebooks/notebookmanager.hpp" 29 30 namespace gnote { 31 namespace notebooks { 32 33 34 const char * Notebook::NOTEBOOK_TAG_PREFIX = "notebook:"; 35 Tag::Ptr Notebook::s_template_tag; 36 template_tag() const37 Tag::Ptr Notebook::template_tag() const 38 { 39 if(s_template_tag == NULL) { 40 s_template_tag = m_note_manager.tag_manager().get_or_create_system_tag( 41 ITagManager::TEMPLATE_NOTE_SYSTEM_TAG); 42 } 43 44 return s_template_tag; 45 } 46 is_template_note(const Note::Ptr & note)47 bool Notebook::is_template_note(const Note::Ptr & note) 48 { 49 Tag::Ptr tag = template_tag(); 50 if(tag == NULL) { 51 return false; 52 } 53 return note->contains_tag(tag); 54 } 55 Notebook(NoteManagerBase & manager,const Glib::ustring & name,bool is_special)56 Notebook::Notebook(NoteManagerBase & manager, const Glib::ustring & name, bool is_special) 57 : m_note_manager(manager) 58 { 59 // is special assume the name as is, and we don't want a tag. 60 if(is_special) { 61 m_name = name; 62 } 63 else { 64 set_name(name); 65 m_tag = manager.tag_manager().get_or_create_system_tag( 66 Glib::ustring(NOTEBOOK_TAG_PREFIX) + name); 67 } 68 } 69 Notebook(NoteManagerBase & manager,const Tag::Ptr & notebookTag)70 Notebook::Notebook(NoteManagerBase & manager, const Tag::Ptr & notebookTag) 71 : m_note_manager(manager) 72 { 73 // Parse the notebook name from the tag name 74 Glib::ustring systemNotebookPrefix = Glib::ustring(Tag::SYSTEM_TAG_PREFIX) 75 + NOTEBOOK_TAG_PREFIX; 76 Glib::ustring notebookName = sharp::string_substring(notebookTag->name(), 77 systemNotebookPrefix.length()); 78 set_name(notebookName); 79 m_tag = notebookTag; 80 } 81 set_name(const Glib::ustring & value)82 void Notebook::set_name(const Glib::ustring & value) 83 { 84 Glib::ustring trimmedName = sharp::string_trim(value); 85 if(!trimmedName.empty()) { 86 m_name = trimmedName; 87 m_normalized_name = trimmedName.lowercase(); 88 89 // The templateNoteTite should show the name of the 90 // notebook. For example, if the name of the notebooks 91 // "Meetings", the templateNoteTitle should be "Meetings 92 // Notebook Template". Translators should place the 93 // name of the notebook accordingly using "%1". 94 Glib::ustring format = _("%1 Notebook Template"); 95 m_default_template_note_title = Glib::ustring::compose(format, m_name); 96 } 97 } 98 99 get_normalized_name() const100 Glib::ustring Notebook::get_normalized_name() const 101 { 102 return m_normalized_name; 103 } 104 105 get_tag() const106 Tag::Ptr Notebook::get_tag() const 107 { 108 return m_tag; 109 } 110 111 find_template_note() const112 Note::Ptr Notebook::find_template_note() const 113 { 114 Note::Ptr note; 115 Tag::Ptr templ_tag = template_tag(); 116 Tag::Ptr notebook_tag = m_note_manager.tag_manager().get_system_tag(NOTEBOOK_TAG_PREFIX + get_name()); 117 if(!templ_tag || !notebook_tag) { 118 return note; 119 } 120 auto notes = templ_tag->get_notes(); 121 for(NoteBase *n : notes) { 122 if(n->contains_tag(notebook_tag)) { 123 note = std::static_pointer_cast<Note>(n->shared_from_this()); 124 break; 125 } 126 } 127 128 return note; 129 } 130 get_template_note() const131 Note::Ptr Notebook::get_template_note() const 132 { 133 NoteBase::Ptr note = find_template_note(); 134 135 if (!note) { 136 Glib::ustring title = m_default_template_note_title; 137 if(m_note_manager.find(title)) { 138 auto tag_notes = m_tag->get_notes(); 139 title = m_note_manager.get_unique_name(title); 140 } 141 note = m_note_manager.create(title, NoteManager::get_note_template_content (title)); 142 143 // Select the initial text 144 NoteBuffer::Ptr buffer = std::static_pointer_cast<Note>(note)->get_buffer(); 145 buffer->select_note_body(); 146 147 // Flag this as a template note 148 Tag::Ptr templ_tag = template_tag(); 149 note->add_tag(templ_tag); 150 151 // Add on the notebook system tag so Tomboy 152 // will persist the tag/notebook across sessions 153 // if no other notes are added to the notebook. 154 Tag::Ptr notebook_tag = m_note_manager.tag_manager().get_or_create_system_tag(NOTEBOOK_TAG_PREFIX + get_name()); 155 note->add_tag (notebook_tag); 156 157 note->queue_save (CONTENT_CHANGED); 158 } 159 160 return std::static_pointer_cast<Note>(note); 161 } 162 create_notebook_note()163 Note::Ptr Notebook::create_notebook_note() 164 { 165 Glib::ustring temp_title; 166 Note::Ptr note_template = get_template_note(); 167 168 temp_title = m_note_manager.get_unique_name(_("New Note")); 169 NoteBase::Ptr note = m_note_manager.create_note_from_template(temp_title, note_template); 170 171 // Add the notebook tag 172 note->add_tag(m_tag); 173 174 return std::static_pointer_cast<Note>(note); 175 } 176 177 /// <summary> 178 /// Returns true when the specified note exists in the notebook 179 /// </summary> 180 /// <param name="note"> 181 /// A <see cref="Note"/> 182 /// </param> 183 /// <returns> 184 /// A <see cref="System.Boolean"/> 185 /// </returns> contains_note(const Note::Ptr & note,bool include_system)186 bool Notebook::contains_note(const Note::Ptr & note, bool include_system) 187 { 188 bool contains = note->contains_tag(m_tag); 189 if(!contains || include_system) { 190 return contains; 191 } 192 return !is_template_note(note); 193 } 194 add_note(const Note::Ptr & note)195 bool Notebook::add_note(const Note::Ptr & note) 196 { 197 m_note_manager.notebook_manager().move_note_to_notebook(note, shared_from_this()); 198 return true; 199 } 200 normalize(const Glib::ustring & s)201 Glib::ustring Notebook::normalize(const Glib::ustring & s) 202 { 203 return Glib::ustring(sharp::string_trim(s)).lowercase(); 204 } 205 206 } 207 } 208