c3eb0f63d1b5db25122edf478136205407fd73dd
[quicknote] / src / notizen.py
1 #!/usr/bin/env python2.5
2 # -*- coding: utf-8 -*-
3
4 """
5  Copyright (C) 2007 Christoph W├╝rstle
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 version 2 as
9 published by the Free Software Foundation.
10
11 @todo It would be nice to move the category selector to the category list's column and search below everything.
12 @todo Search would be activated by menu or CTRL+F rather than zoom
13 """
14
15 import time
16 import logging
17 import uuid
18
19 import gobject
20 import gtk
21 try:
22         import gtkspell
23 except ImportError:
24         gtkspell = None
25
26 import hildonize
27 import gtk_toolbox
28 import simple_list
29
30
31 try:
32         _
33 except NameError:
34         _ = lambda x: x
35
36
37 _moduleLogger = logging.getLogger("notizen")
38
39
40 class Notizen(gtk.HBox):
41
42         def __init__(self, db, category, search):
43                 self._db = db
44                 self._category = category
45                 self._search = search
46                 self.noteId = -1
47                 self._pos = -1
48                 self._noteBody = None #Last notetext
49                 self._categoryName = ""
50
51                 gtk.HBox.__init__(self, homogeneous = False, spacing = 0)
52                 _moduleLogger.info("libnotizen, init")
53
54                 # Note list
55                 self._noteslist = simple_list.SimpleList()
56                 self._noteslist.set_eventfunction_cursor_changed(self._update_noteslist)
57                 self._noteslist.widget.set_size_request(250, -1)
58
59                 buttonHBox = gtk.HBox()
60
61                 button = gtk.Button(stock = gtk.STOCK_ADD)
62                 button.connect("clicked", self._on_add_note, None)
63                 buttonHBox.pack_start(button, expand = True, fill = True, padding = 3)
64
65                 button = gtk.Button(stock = gtk.STOCK_DELETE)
66                 button.connect("clicked", self._on_delete_note, None)
67                 buttonHBox.pack_start(button, expand = True, fill = True, padding = 3)
68
69                 listVbox = gtk.VBox(homogeneous = False, spacing = 0)
70                 listVbox.pack_start(self._category, expand = False, fill = True, padding = 3)
71                 listVbox.pack_start(self._noteslist.widget, expand = True, fill = True, padding = 3)
72                 listVbox.pack_start(buttonHBox, expand = False, fill = True, padding = 3)
73                 self.pack_start(listVbox, expand = False, fill = True, padding = 3)
74
75                 # Note view
76                 self._noteBodyView = gtk.TextView()
77                 self._noteBodyView.connect("focus-out-event", self.save_note, "focus-out-event")
78                 buf = self._noteBodyView.get_buffer()
79                 buf.set_text("")
80                 buf.connect("changed", self._on_note_changed, None)
81                 if gtkspell is not None:
82                         self._noteBodySpellChecker = gtkspell.Spell(self._noteBodyView)
83                 else:
84                         self._noteBodySpellChecker = None
85
86                 self._noteScrollWindow = gtk.ScrolledWindow()
87                 self._noteScrollWindow.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
88                 self._noteScrollWindow.add(self._noteBodyView)
89                 hildonize.hildonize_scrollwindow_with_viewport(self._noteScrollWindow)
90
91                 # Note and history stuff in same column
92                 noteVbox = gtk.VBox(homogeneous = False, spacing = 0)
93                 noteVbox.pack_start(self._noteScrollWindow, expand = True, fill = True, padding = 3)
94                 self.pack_start(noteVbox, expand = True, fill = True, padding = 3)
95
96                 self.load_notes()
97                 self._category.connect("category_changed", self.load_notes)
98                 self._search.connect("search_changed", self.load_notes)
99
100         def set_wordwrap(self, enableWordWrap):
101                 if enableWordWrap:
102                         self._noteScrollWindow.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
103                         self._noteBodyView.set_wrap_mode(gtk.WRAP_WORD)
104                 else:
105                         self._noteScrollWindow.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
106                         self._noteBodyView.set_wrap_mode(gtk.WRAP_NONE)
107
108         def load_notes(self, data = None):
109                 _moduleLogger.info("load_notes params: pos:"+str(self._pos)+" noteid:"+str(self.noteId))
110                 self._noteslist.clear_items()
111
112                 self._categoryName = self._category.get_category()
113                 search = self._search.get_search_pattern()
114                 notes = self._db.searchNotes(search, self._categoryName)
115
116                 if notes is not None:
117                         for note in notes:
118                                 noteid, category, noteText = note
119                                 title = self._get_title(noteText)
120                                 self._noteslist.append_item(title, noteid)
121
122                 self.noteId = -1
123                 self._pos = -1
124                 self._noteBodyView.get_buffer().set_text("")
125
126         def save_note(self, widget = None, data = None, data2 = None):
127                 _moduleLogger.info("save_note params: pos:"+str(self._pos)+" noteid:"+str(self.noteId))
128                 #print "params:", data, data2
129                 buf = self._noteBodyView.get_buffer().get_text(self._noteBodyView.get_buffer().get_start_iter(), self._noteBodyView.get_buffer().get_end_iter())
130                 if buf is None or len(buf) == 0:
131                         return
132
133                 if buf == self._noteBody:
134                         return
135
136                 title = self._get_title(buf)
137                 _moduleLogger.info("Saving note: " + title)
138                 if self._pos == -1 or self.noteId == -1:
139                         self._pos = -1
140                         self.noteId = str(uuid.uuid4())
141                         self._db.saveNote(self.noteId, buf, self._categoryName)
142                         self._noteslist.append_item(title, self.noteId)
143                         self._pos = self._noteslist.select_last_item()
144                 else:
145                         self._db.saveNote(self.noteId, buf, self._categoryName)
146
147                 self._category.define_this_category()
148
149         def show_history(self, *args):
150                 if self.noteId == -1:
151                         mbox =  gtk.MessageDialog(None, gtk.DIALOG_MODAL, gtk.MESSAGE_INFO, gtk.BUTTONS_OK, _("No note selected."))
152                         response = mbox.run()
153                         mbox.hide()
154                         mbox.destroy()
155                         return
156
157                 rows = self._db.getNoteHistory(self.noteId)
158
159                 import history
160                 dialog = history.HistorySelectionDialog()
161
162                 lastNoteStr = ""
163                 for row in rows:
164                         daten = row[4][1]
165                         if daten != "" and lastNoteStr != daten:
166                                 lastNoteStr = daten
167                                 dialog.noteHistory.append([row[0], row[1], row[2], row[3], daten+"\n"])
168
169                 dialog.vbox.show_all()
170                 dialog.set_size_request(600, 380)
171
172                 try:
173                         userResponse = dialog.run()
174
175                         if userResponse == gtk.RESPONSE_ACCEPT:
176                                 self.save_note()
177                                 data = dialog.get_selected_row()
178                                 if data is not None:
179                                         self._db.speichereSQL(data[2], data[3].split(" <<Tren-ner>> "), rowid = self.noteId)
180                                         _moduleLogger.info("loading History")
181                                         self._update_noteslist()
182
183                 finally:
184                         dialog.destroy()
185
186         def _get_title(self, buf):
187                 """
188                 @returns the title of the current note
189                 """
190                 eol = buf.find("\n")
191                 if -1 == eol:
192                         eol = len(buf)
193                 title = buf[:eol]
194                 return title
195
196         def _set_focus(self):
197                 self._noteBodyView.grab_focus()
198                 return False
199
200         def _update_noteslist(self, *args):
201                 if self._pos != -1:
202                         self.save_note()
203
204                 if args:
205                         data = args[0]
206                 else:
207                         data = None
208
209                 try:
210                         (pos, key, value) = self._noteslist.get_selection_data()
211                         if pos == -1:
212                                 return
213                 except StandardError:
214                         if data != "new":
215                                 return
216                         key = None
217
218                 if key == "new" or data == "new":
219                         #both methods supported click add note or new note (first one disabled)
220                         self.noteId = str(uuid.uuid4())
221                         self._db.saveNote(self.noteId, "", self._categoryName)
222                         self._pos = -1
223                         self._noteslist.append_item("", self.noteId)
224                         self._noteBodyView.get_buffer().set_text("")
225                         self._pos = self._noteslist.select_last_item()
226                 else:
227                         self._pos = pos
228                         self.noteId, pcdatum, self._categoryName, self._noteBody = self._db.loadNote(key)
229                         buf = self._noteBodyView.get_buffer()
230                         buf.set_text(self._noteBody)
231
232                 gobject.timeout_add(200, self._set_focus)
233
234         @gtk_toolbox.log_exception(_moduleLogger)
235         def _on_note_changed(self, widget = None, data = None):
236                 if self._pos == -1 or self.noteId == -1:
237                         return
238
239                 buf = self._noteBodyView.get_buffer().get_text(self._noteBodyView.get_buffer().get_start_iter(), self._noteBodyView.get_buffer().get_end_iter())
240
241                 title = self._get_title(buf)
242                 value, key = self._noteslist.get_item(self._pos)
243
244                 if value != title:
245                         self._noteslist.change_item(self._pos, title, self.noteId)
246
247         @gtk_toolbox.log_exception(_moduleLogger)
248         def _on_add_note(self, widget = None, data = None):
249                 self._update_noteslist("new")
250
251         @gtk_toolbox.log_exception(_moduleLogger)
252         def _on_delete_note(self, widget = None, data = None):
253                 if self.noteId == -1:
254                         return
255
256                 doDeletion = False
257                 if self._noteBodyView.get_buffer().get_char_count() == 0:
258                         doDeletion = True
259                 else:
260                         mbox = gtk.MessageDialog(None, gtk.DIALOG_MODAL, gtk.MESSAGE_WARNING, gtk.BUTTONS_YES_NO, _("Really delete?"))
261                         try:
262                                 response = mbox.run()
263                         finally:
264                                 mbox.hide()
265                                 mbox.destroy()
266
267                         doDeletion = response == gtk.RESPONSE_YES
268
269                 if doDeletion:
270                         self._db.delNote(self.noteId)
271                         self.noteId = -1
272                         self._noteslist.remove_item(self._pos)
273                         self._pos = -1
274                         self._noteBodyView.get_buffer().set_text("")