1 /*
2  * gnote
3  *
4  * Copyright (C) 2011-2015,2017,2019,2021 Aurimas Cernius
5  * Copyright (C) 2010 Debarshi Ray
6  * Copyright (C) 2009 Hubert Figuiere
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 
23 
24 #include <glibmm/i18n.h>
25 #include <glibmm/stringutils.h>
26 #include <gtkmm/stock.h>
27 #include <gtkmm/separatormenuitem.h>
28 #include <gtkmm/treeiter.h>
29 #include <gtkmm/treemodel.h>
30 #include <gtkmm/uimanager.h>
31 
32 #include "sharp/string.hpp"
33 #include "notebooks/notebookapplicationaddin.hpp"
34 #include "notebooks/notebookmanager.hpp"
35 #include "notebooks/notebook.hpp"
36 #include "iactionmanager.hpp"
37 #include "ignote.hpp"
38 #include "debug.hpp"
39 #include "notemanager.hpp"
40 
41 namespace gnote {
42   namespace notebooks {
43 
create()44     ApplicationAddin * NotebookApplicationAddin::create()
45     {
46       return new NotebookApplicationAddin();
47     }
48 
49 
NotebookApplicationAddin()50     NotebookApplicationAddin::NotebookApplicationAddin()
51       : m_initialized(false)
52     {
53     }
54 
55 
56 
initialize()57     void NotebookApplicationAddin::initialize ()
58     {
59       IActionManager & am(ignote().action_manager());
60 
61       NoteManager & nm(note_manager());
62 
63       for(const NoteBase::Ptr & note : nm.get_notes()) {
64         note->signal_tag_added.connect(
65           sigc::mem_fun(*this, &NotebookApplicationAddin::on_tag_added));
66         note->signal_tag_removed.connect(
67           sigc::mem_fun(*this, &NotebookApplicationAddin::on_tag_removed));
68       }
69 
70       nm.signal_note_added.connect(
71         sigc::mem_fun(*this, &NotebookApplicationAddin::on_note_added));
72       nm.signal_note_deleted.connect(
73         sigc::mem_fun(*this, &NotebookApplicationAddin::on_note_deleted));
74 
75       am.add_app_action("new-notebook");
76       am.get_app_action("new-notebook")->signal_activate().connect(
77         sigc::mem_fun(*this, &NotebookApplicationAddin::on_new_notebook_action));
78       am.add_app_menu_item(APP_SECTION_NEW, 300, _("New Note_book..."), "app.new-notebook");
79 
80       m_initialized = true;
81     }
82 
83 
shutdown()84     void NotebookApplicationAddin::shutdown ()
85     {
86       m_initialized = false;
87     }
88 
initialized()89     bool NotebookApplicationAddin::initialized ()
90     {
91       return m_initialized;
92     }
93 
on_new_notebook_action(const Glib::VariantBase &)94     void NotebookApplicationAddin::on_new_notebook_action(const Glib::VariantBase&)
95     {
96       NotebookManager::prompt_create_new_notebook(ignote(), NULL);
97     }
98 
99 
on_tag_added(const NoteBase & note,const Tag::Ptr & tag)100     void NotebookApplicationAddin::on_tag_added(const NoteBase & note, const Tag::Ptr& tag)
101     {
102       NotebookManager & manager = ignote().notebook_manager();
103       if (manager.is_adding_notebook()) {
104         return;
105       }
106 
107       Glib::ustring megaPrefix(Tag::SYSTEM_TAG_PREFIX);
108       megaPrefix += Notebook::NOTEBOOK_TAG_PREFIX;
109       if (!tag->is_system() || !Glib::str_has_prefix(tag->name(), megaPrefix)) {
110         return;
111       }
112 
113       Glib::ustring notebookName = sharp::string_substring(tag->name(), megaPrefix.size());
114 
115       Notebook::Ptr notebook = manager.get_or_create_notebook(notebookName);
116 
117       manager.signal_note_added_to_notebook() (static_cast<const Note&>(note), notebook);
118     }
119 
120 
121 
on_tag_removed(const NoteBase::Ptr & note,const Glib::ustring & normalizedTagName)122     void NotebookApplicationAddin::on_tag_removed(const NoteBase::Ptr & note,
123                                                   const Glib::ustring & normalizedTagName)
124     {
125       Glib::ustring megaPrefix(Tag::SYSTEM_TAG_PREFIX);
126       megaPrefix += Notebook::NOTEBOOK_TAG_PREFIX;
127 
128       if (!Glib::str_has_prefix(normalizedTagName, megaPrefix)) {
129         return;
130       }
131 
132       Glib::ustring normalizedNotebookName =
133         sharp::string_substring(normalizedTagName, megaPrefix.size());
134 
135       NotebookManager & manager = ignote().notebook_manager();
136       Notebook::Ptr notebook = manager.get_notebook(normalizedNotebookName);
137       if (!notebook) {
138         return;
139       }
140 
141       manager.signal_note_removed_from_notebook() (*std::static_pointer_cast<Note>(note), notebook);
142     }
143 
on_note_added(const NoteBase::Ptr & note)144     void NotebookApplicationAddin::on_note_added(const NoteBase::Ptr & note)
145     {
146         note->signal_tag_added.connect(
147           sigc::mem_fun(*this, &NotebookApplicationAddin::on_tag_added));
148         note->signal_tag_removed.connect(
149           sigc::mem_fun(*this, &NotebookApplicationAddin::on_tag_removed));
150     }
151 
152 
on_note_deleted(const NoteBase::Ptr &)153     void NotebookApplicationAddin::on_note_deleted(const NoteBase::Ptr &)
154     {
155       // remove the signal to the note...
156     }
157 
158 
159   }
160 }
161