Maemo might not have gtkspell, so making it optional
[quicknote] / src / libnotizen.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
12 import time
13 import logging
14 import uuid
15
16 import gobject
17 import gtk
18 try:
19         import gtkspell
20 except ImportError:
21         gtkspell = None
22
23 import simple_list
24
25
26 try:
27         _
28 except NameError:
29         _ = lambda x: x
30
31
32 class Notizen(gtk.HBox):
33
34         def __init__(self, db, topBox):
35                 self._db = db
36                 self._topBox = topBox
37                 self.noteId = -1
38                 self._pos = -1
39                 self._noteBody = None #Last notetext
40                 self._categoryName = ""
41
42                 gtk.HBox.__init__(self, homogeneous = False, spacing = 0)
43                 logging.info("libnotizen, init")
44
45                 self._noteslist = simple_list.SimpleList()
46                 self._noteslist.set_eventfunction_cursor_changed(self._update_noteslist)
47
48                 self._noteslist.set_size_request(250, -1)
49
50                 vbox = gtk.VBox(homogeneous = False, spacing = 0)
51
52                 frame = gtk.Frame(_("Titles"))
53                 frame.add(self._noteslist)
54                 vbox.pack_start(frame, expand = True, fill = True, padding = 3)
55
56                 buttonHBox = gtk.HBox()
57                 vbox.pack_start(buttonHBox, expand = False, fill = True, padding = 3)
58
59                 button = gtk.Button(stock = gtk.STOCK_ADD)
60                 button.connect("clicked", self._on_add_note, None)
61                 buttonHBox.pack_start(button, expand = True, fill = True, padding = 3)
62
63                 button = gtk.Button(stock = gtk.STOCK_DELETE)
64                 button.connect("clicked", self._on_delete_note, None)
65                 buttonHBox.pack_start(button, expand = True, fill = True, padding = 3)
66
67                 self.pack_start(vbox, expand = False, fill = True, padding = 3)
68
69                 self._noteBodyView = gtk.TextView()
70                 self._noteBodyView.connect("focus-out-event", self.save_note, "focus-out-event")
71                 buf = self._noteBodyView.get_buffer()
72                 buf.set_text("")
73                 buf.connect("changed", self._on_note_changed, None)
74                 if gtkspell is not None:
75                         self._noteBodySpellChecker = gtkspell.Spell(self._noteBodyView)
76                 else:
77                         self._noteBodySpellChecker = None
78
79                 #self.textviewNotiz.set_size_request(-1, 50)
80                 self._noteScrollWindow = gtk.ScrolledWindow()
81                 self._noteScrollWindow.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
82                 self._noteScrollWindow.add(self._noteBodyView)
83
84                 frame = gtk.Frame(_("Note"))
85                 frame.add(self._noteScrollWindow)
86
87                 vbox = gtk.VBox(homogeneous = False, spacing = 0)
88                 vbox.pack_start(frame, expand = True, fill = True, padding = 3)
89
90                 self._historyBox = gtk.HBox(homogeneous = False, spacing = 0)
91
92                 self._historyStatusLabel = gtk.Label(_("No History"))
93                 self._historyStatusLabel.set_alignment(0.0, 0.5)
94                 self._historyBox.pack_start(self._historyStatusLabel, expand = True, fill = True, padding = 3)
95
96                 button = gtk.Button(_("History"))
97                 button.connect("clicked", self._on_show_history, None)
98                 self._historyBox.pack_start(button, expand = True, fill = True, padding = 3)
99
100                 vbox.pack_start(self._historyBox, expand = False, fill = True, padding = 3)
101
102                 self.pack_start(vbox, expand = True, fill = True, padding = 3)
103
104                 self.load_notes()
105                 self._topBox.connect("reload_notes", self.load_notes)
106
107         def set_wordwrap(self, enableWordWrap):
108                 if enableWordWrap:
109                         self._noteScrollWindow.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
110                         self._noteBodyView.set_wrap_mode(gtk.WRAP_WORD)
111                 else:
112                         self._noteScrollWindow.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
113                         self._noteBodyView.set_wrap_mode(gtk.WRAP_NONE)
114
115         def show_history_area(self, visible):
116                 if visible:
117                         self._historyBox.show()
118                 else:
119                         self._historyBox.hide()
120
121         def load_notes(self, data = None):
122                 logging.info("load_notes params: pos:"+str(self._pos)+" noteid:"+str(self.noteId))
123                 self._noteslist.clear_items()
124                 self._noteslist.append_item(_("New Note..."), "new")
125
126                 self._categoryName = self._topBox.get_category()
127                 search = self._topBox.get_search_pattern()
128                 notes = self._db.searchNotes(search, self._categoryName)
129
130                 if notes is not None:
131                         for note in notes:
132                                 noteid, category, noteText = note
133                                 title = self._get_title(noteText)
134                                 self._noteslist.append_item(title, noteid)
135
136                 self.noteId = -1
137                 self._pos = -1
138                 self._noteBodyView.get_buffer().set_text("")
139
140         def save_note(self, widget = None, data = None, data2 = None):
141                 logging.info("save_note params: pos:"+str(self._pos)+" noteid:"+str(self.noteId))
142                 #print "params:", data, data2
143                 buf = self._noteBodyView.get_buffer().get_text(self._noteBodyView.get_buffer().get_start_iter(), self._noteBodyView.get_buffer().get_end_iter())
144                 if buf is None or len(buf) == 0:
145                         return
146
147                 if buf == self._noteBody:
148                         return
149
150                 logging.info("Saving note: "+buf)
151                 if self._pos == -1 or self.noteId == -1:
152                         self._pos = -1
153                         self.noteId = str(uuid.uuid4())
154                         self._db.saveNote(self.noteId, buf, self._categoryName)
155                         self._noteslist.append_item(self._get_title(buf), self.noteId)
156                         self._pos = self._noteslist.select_last_item()
157                 else:
158                         self._db.saveNote(self.noteId, buf, self._categoryName)
159
160                 self._topBox.define_this_category()
161
162         def _get_title(self, buf):
163                 """
164                 @returns the title of the current note
165                 """
166                 eol = buf.find("\n")
167                 if -1 == eol:
168                         eol = len(buf)
169                 title = buf[:eol]
170                 return title
171
172         def _set_focus(self):
173                 self._noteBodyView.grab_focus()
174                 return False
175
176         def _update_noteslist(self, data = None, data2 = None):
177                 if self._pos != -1:
178                         self.save_note()
179
180                 try:
181                         (pos, key, value) = self._noteslist.get_selection_data()
182                         if (pos == -1):
183                                 return
184                 except StandardError:
185                         if data != "new":
186                                 return
187                         key = None
188
189                 if key == "new" or data == "new":
190                         #both methods supported click add note or new note (first one disabled)
191                         self.noteId = str(uuid.uuid4())
192                         self._db.saveNote(self.noteId, "", self._categoryName)
193                         self._pos = -1
194                         self._noteslist.append_item("", self.noteId)
195                         self._noteBodyView.get_buffer().set_text("")
196                         self._pos = self._noteslist.select_last_item()
197                 else:
198                         self._pos = pos
199                         self.noteId, pcdatum, self._categoryName, self._noteBody = self._db.loadNote(key)
200                         self._historyStatusLabel.set_text(time.strftime(_("Last change: %d.%m.%y %H:%M"), time.localtime(pcdatum)))
201                         buf = self._noteBodyView.get_buffer()
202                         buf.set_text(self._noteBody)
203
204                 gobject.timeout_add(200, self._set_focus)
205
206         def _on_note_changed(self, widget = None, data = None):
207                 if self._pos == -1 or self.noteId == -1:
208                         return
209
210                 buf = self._noteBodyView.get_buffer().get_text(self._noteBodyView.get_buffer().get_start_iter(), self._noteBodyView.get_buffer().get_end_iter())
211
212                 title = self._get_title(buf)
213                 value, key = self._noteslist.get_item(self._pos)
214
215                 if value != title:
216                         self._noteslist.change_item(self._pos, title, self.noteId)
217
218         def _on_add_note(self, widget = None, data = None):
219                 self._update_noteslist("new")
220
221         def _on_delete_note(self, widget = None, data = None):
222                 if (self.noteId == -1):
223                         return
224                 mbox = gtk.MessageDialog(None, gtk.DIALOG_MODAL, gtk.MESSAGE_WARNING, gtk.BUTTONS_YES_NO, _("Really delete?"))
225                 response = mbox.run()
226                 mbox.hide()
227                 mbox.destroy()
228                 if response == gtk.RESPONSE_YES:
229                         self._db.delNote(self.noteId)
230                         self.noteId = -1
231                         self._noteslist.remove_item(self._pos)
232                         self._pos = -1
233                         self._noteBodyView.get_buffer().set_text("")
234
235         def _on_show_history(self, widget = None, data = None, label = None):
236                 if self.noteId == -1:
237                         mbox =  gtk.MessageDialog(None, gtk.DIALOG_MODAL, gtk.MESSAGE_INFO, gtk.BUTTONS_OK, _("No note selected."))
238                         response = mbox.run()
239                         mbox.hide()
240                         mbox.destroy()
241                         return
242
243                 rows = self._db.getNoteHistory(self.noteId)
244
245                 import libhistory
246                 dialog = libhistory.Dialog()
247
248                 lastNoteStr = ""
249                 for row in rows:
250                         daten = row[4][1]
251                         if daten != "" and lastNoteStr != daten:
252                                 lastNoteStr = daten
253                                 dialog.noteHistory.append([row[0], row[1], row[2], row[3], daten+"\n"])
254
255                 dialog.vbox.show_all()
256                 dialog.set_size_request(600, 380)
257
258                 if dialog.run() == gtk.RESPONSE_ACCEPT:
259                         print "saving"
260                         self.save_note()
261                         data = dialog.get_selected_row()
262                         if data is not None:
263                                 self._db.speichereSQL(data[2], data[3].split(" <<Tren-ner>> "), rowid = self.noteId)
264                                 logging.info("loading History")
265                                 self._update_noteslist()
266
267                 dialog.destroy()