1 /*
2  * gnote
3  *
4  * Copyright (C) 2011-2014,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 <gtkmm/targetentry.h>
24 
25 #include "debug.hpp"
26 #include "notebooks/notebook.hpp"
27 #include "notebooks/notebookmanager.hpp"
28 #include "notebooks/notebookstreeview.hpp"
29 #include "notebooks/specialnotebooks.hpp"
30 #include "notemanagerbase.hpp"
31 
32 namespace gnote {
33   namespace notebooks {
34 
NotebooksTreeView(NoteManagerBase & manager,const Glib::RefPtr<Gtk::TreeModel> & model)35     NotebooksTreeView::NotebooksTreeView(NoteManagerBase & manager, const Glib::RefPtr<Gtk::TreeModel> & model)
36       : Gtk::TreeView(model)
37       , m_note_manager(manager)
38     {
39       // Set up the notebooksTree as a drag target so that notes
40       // can be dragged into the notebook.
41       std::vector<Gtk::TargetEntry> targets;
42       targets.push_back(Gtk::TargetEntry ("text/uri-list",
43                                           Gtk::TARGET_SAME_APP,
44                                           1));
45       drag_dest_set(targets, Gtk::DEST_DEFAULT_ALL, Gdk::ACTION_MOVE);
46 
47     }
48 
on_drag_data_received(const Glib::RefPtr<Gdk::DragContext> & context,int x,int y,const Gtk::SelectionData & selectionData,guint,guint time_)49     void NotebooksTreeView::on_drag_data_received( const Glib::RefPtr<Gdk::DragContext> & context,
50                                                    int x, int y,
51                                                    const Gtk::SelectionData & selectionData,
52                                                    guint , guint time_)
53     {
54       utils::UriList uriList(selectionData);
55       if (uriList.size() == 0) {
56         context->drag_finish (false, false, time_);
57         return;
58       }
59 
60       Gtk::TreePath treepath;
61       Gtk::TreeViewDropPosition pos;
62       if (get_dest_row_at_pos (x, y, treepath, pos) == false) {
63         context->drag_finish (false, false, time_);
64         return;
65       }
66 
67       Gtk::TreeIter iter = get_model()->get_iter(treepath);
68       if (!iter) {
69         context->drag_finish (false, false, time_);
70         return;
71       }
72 
73       Notebook::Ptr destNotebook;
74       iter->get_value(0, destNotebook);
75       if(std::dynamic_pointer_cast<AllNotesNotebook>(destNotebook)) {
76         context->drag_finish (false, false, time_);
77         return;
78       }
79 
80       for(utils::UriList::const_iterator uri_iter = uriList.begin();
81           uri_iter != uriList.end(); ++uri_iter) {
82         const sharp::Uri & uri(*uri_iter);
83         NoteBase::Ptr note = m_note_manager.find_by_uri(uri.to_string());
84         if (!note)
85           continue;
86 
87         DBG_OUT ("Dropped into notebook: %s", note->get_title().c_str());
88 
89         destNotebook->add_note(std::static_pointer_cast<Note>(note));
90       }
91 
92       context->drag_finish (true, false, time_);
93     }
94 
on_drag_motion(const Glib::RefPtr<Gdk::DragContext> &,int x,int y,guint)95     bool NotebooksTreeView::on_drag_motion(const Glib::RefPtr<Gdk::DragContext> &,
96                                 int x, int y, guint )
97     {
98       Gtk::TreePath treepath;
99       Gtk::TreeViewDropPosition pos;
100       if (get_dest_row_at_pos (x, y, treepath,pos) == false) {
101         gtk_tree_view_set_drag_dest_row (gobj(), NULL, GTK_TREE_VIEW_DROP_INTO_OR_AFTER);
102         return false;
103       }
104 
105       Gtk::TreeIter iter = get_model()->get_iter (treepath);
106       if (!iter) {
107         gtk_tree_view_set_drag_dest_row (gobj(), NULL, GTK_TREE_VIEW_DROP_INTO_OR_AFTER);
108         return false;
109       }
110 
111       Notebook::Ptr destNotebook;
112       iter->get_value(0, destNotebook);
113       if(std::dynamic_pointer_cast<AllNotesNotebook>(destNotebook)) {
114         gtk_tree_view_set_drag_dest_row (gobj(), NULL, GTK_TREE_VIEW_DROP_INTO_OR_AFTER);
115         return true;
116       }
117 
118       set_drag_dest_row (treepath , Gtk::TREE_VIEW_DROP_INTO_OR_AFTER);
119 
120       return true;
121     }
122 
on_drag_leave(const Glib::RefPtr<Gdk::DragContext> &,guint)123     void NotebooksTreeView::on_drag_leave(const Glib::RefPtr<Gdk::DragContext> & , guint )
124     {
125       gtk_tree_view_set_drag_dest_row (gobj(), NULL, GTK_TREE_VIEW_DROP_INTO_OR_AFTER);
126     }
127 
128   }
129 }
130