1# Gramps - a GTK+/GNOME based genealogy program
2#
3# Copyright (C) 2007-2009  Douglas S. Blank <doug.blank@gmail.com>
4# Copyright (C) 2013       Nick Hall
5#
6# This program is free software; you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation; either version 2 of the License, or
9# (at your option) any later version.
10#
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with this program; if not, write to the Free Software
18# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19#
20
21#-------------------------------------------------------------------------
22#
23# Gtk modules
24#
25#-------------------------------------------------------------------------
26from gi.repository import Gtk
27
28#-------------------------------------------------------------------------
29#
30# Gramps modules
31#
32#-------------------------------------------------------------------------
33from gramps.gen.plug import Gramplet
34from gramps.gui.widgets.styledtexteditor import StyledTextEditor
35from gramps.gui.widgets import SimpleButton
36from gramps.gen.lib import StyledText, Note, NoteType
37from gramps.gen.filters import GenericFilterFactory, rules
38from gramps.gen.utils.db import navigation_label
39from gramps.gen.const import GRAMPS_LOCALE as glocale
40_ = glocale.translation.gettext
41
42class ToDoGramplet(Gramplet):
43    """
44    Displays all the To Do notes in the database.
45    """
46    def init(self):
47        self.gui.WIDGET = self.build_gui()
48        self.gui.get_container_widget().remove(self.gui.textview)
49        self.gui.get_container_widget().add(self.gui.WIDGET)
50        self.gui.WIDGET.show()
51
52    def build_gui(self):
53        """
54        Build the GUI interface.
55        """
56        top = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
57
58        hbox = Gtk.Box()
59        self.left = SimpleButton('go-previous', self.left_clicked)
60        self.left.set_tooltip_text(_('Previous To Do note'))
61        self.left.set_sensitive(False)
62        hbox.pack_start(self.left, False, False, 0)
63        self.right = SimpleButton('go-next', self.right_clicked)
64        self.right.set_tooltip_text(_('Next To Do note'))
65        self.right.set_sensitive(False)
66        hbox.pack_start(self.right, False, False, 0)
67        self.edit = SimpleButton('gtk-edit', self.edit_clicked)
68        self.edit.set_tooltip_text(_('Edit the selected To Do note'))
69        self.edit.set_sensitive(False)
70        hbox.pack_start(self.edit, False, False, 0)
71        self.new = SimpleButton('document-new', self.new_clicked)
72        self.new.set_tooltip_text(_('Add a new To Do note'))
73        hbox.pack_start(self.new, False, False, 0)
74        self.page = Gtk.Label()
75        hbox.pack_end(self.page, False, False, 10)
76
77        self.title = Gtk.Label(halign=Gtk.Align.START)
78        self.title.set_line_wrap(True)
79
80        scrolledwindow = Gtk.ScrolledWindow()
81        scrolledwindow.set_policy(Gtk.PolicyType.AUTOMATIC,
82                                  Gtk.PolicyType.AUTOMATIC)
83        self.texteditor = StyledTextEditor()
84        self.texteditor.set_editable(False)
85        self.texteditor.set_wrap_mode(Gtk.WrapMode.WORD)
86        scrolledwindow.add(self.texteditor)
87
88        top.pack_start(hbox, False, False, 0)
89        top.pack_start(self.title, False, False, 4)
90        top.pack_start(scrolledwindow, True, True, 0)
91        top.show_all()
92        return top
93
94    def main(self):
95        self.get_notes()
96
97    def get_note_list(self):
98        """
99        Get a list of all To Do notes.
100        """
101        all_notes = self.dbstate.db.get_note_handles()
102        FilterClass = GenericFilterFactory('Note')
103        filter = FilterClass()
104        filter.add_rule(rules.note.HasType(["To Do"]))
105        note_list = filter.apply(self.dbstate.db, all_notes)
106        return note_list
107
108    def get_notes(self):
109        """
110        Display all the To Do notes.
111        """
112        self.left.set_sensitive(False)
113        self.right.set_sensitive(False)
114        self.edit.set_sensitive(False)
115        self.texteditor.set_text(StyledText())
116        self.note_list = self.get_note_list()
117        self.page.set_text('')
118        self.title.set_text('')
119        if len(self.note_list) > 0:
120            self.set_has_data(True)
121            self.edit.set_sensitive(True)
122            if len(self.note_list) > 1:
123                self.right.set_sensitive(True)
124            self.current = 0
125            self.display_note()
126        else:
127            self.set_has_data(False)
128
129    def clear_text(self):
130        self.left.set_sensitive(False)
131        self.right.set_sensitive(False)
132        self.edit.set_sensitive(False)
133        self.texteditor.set_text(StyledText())
134        self.page.set_text('')
135        self.title.set_text('')
136        self.current = 0
137
138    def display_note(self):
139        """
140        Display the current note.
141        """
142        note_handle = self.note_list[self.current]
143        note = self.dbstate.db.get_note_from_handle(note_handle)
144        obj = [x for x in self.dbstate.db.find_backlink_handles(note_handle)]
145        if obj:
146            name, obj = navigation_label(self.dbstate.db, obj[0][0], obj[0][1])
147            self.title.set_text(name)
148        else:
149            self.title.set_text(_("Unattached"))
150        self.texteditor.set_text(note.get_styledtext())
151        self.page.set_text(_('%(current)d of %(total)d') %
152                           {'current': self.current + 1,
153                            'total': len(self.note_list)})
154
155    def left_clicked(self, button):
156        """
157        Display the previous note.
158        """
159        if self.current > 0:
160            self.current -= 1
161            self.right.set_sensitive(True)
162            if self.current == 0:
163                self.left.set_sensitive(False)
164            self.display_note()
165
166    def right_clicked(self, button):
167        """
168        Display the next note.
169        """
170        if self.current < len(self.note_list) - 1:
171            self.current += 1
172            self.left.set_sensitive(True)
173            if self.current == len(self.note_list) - 1:
174                self.right.set_sensitive(False)
175            self.display_note()
176
177    def get_has_data(self):
178        """
179        Return True if the gramplet has data, else return False.
180        """
181        if self.get_note_list():
182            return True
183        return False
184
185    def edit_clicked(self, obj):
186        """
187        Edit current To Do note.
188        """
189        from gramps.gui.editors import EditNote
190        note_handle = self.note_list[self.current]
191        note = self.dbstate.db.get_note_from_handle(note_handle)
192        try:
193            EditNote(self.gui.dbstate, self.gui.uistate, [], note)
194        except AttributeError:
195            pass
196
197    def new_clicked(self, obj):
198        """
199        Create a new To Do note.
200        """
201        from gramps.gui.editors import EditNote
202        note = Note()
203        note.set_type(NoteType.TODO)
204        try:
205            EditNote(self.gui.dbstate, self.gui.uistate, [], note)
206        except AttributeError:
207            pass
208
209    def update_has_data(self):
210        self.set_has_data(self.get_has_data())
211
212    def db_changed(self):
213        self.connect(self.dbstate.db, 'note-add', self.update)
214        self.connect(self.dbstate.db, 'note-delete', self.update)
215        self.connect(self.dbstate.db, 'note-update', self.update)
216