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