Separated Category and Search in the code/ui cleaning up the UI quite a bit
[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.Dialog()
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                 if dialog.run() == gtk.RESPONSE_ACCEPT:
173                         print "saving"
174                         self.save_note()
175                         data = dialog.get_selected_row()
176                         if data is not None:
177                                 self._db.speichereSQL(data[2], data[3].split(" <<Tren-ner>> "), rowid = self.noteId)
178                                 _moduleLogger.info("loading History")
179                                 self._update_noteslist()
180
181                 dialog.destroy()
182
183         def _get_title(self, buf):
184                 """
185                 @returns the title of the current note
186                 """
187                 eol = buf.find("\n")
188                 if -1 == eol:
189                         eol = len(buf)
190                 title = buf[:eol]
191                 return title
192
193         def _set_focus(self):
194                 self._noteBodyView.grab_focus()
195                 return False
196
197         def _update_noteslist(self, *args):
198                 if self._pos != -1:
199                         self.save_note()
200
201                 if args:
202                         data = args[0]
203                 else:
204                         data = None
205
206                 try:
207                         (pos, key, value) = self._noteslist.get_selection_data()
208                         if pos == -1:
209                                 return
210                 except StandardError:
211                         if data != "new":
212                                 return
213                         key = None
214
215                 if key == "new" or data == "new":
216                         #both methods supported click add note or new note (first one disabled)
217                         self.noteId = str(uuid.uuid4())
218                         self._db.saveNote(self.noteId, "", self._categoryName)
219                         self._pos = -1
220                         self._noteslist.append_item("", self.noteId)
221                         self._noteBodyView.get_buffer().set_text("")
222                         self._pos = self._noteslist.select_last_item()
223                 else:
224                         self._pos = pos
225                         self.noteId, pcdatum, self._categoryName, self._noteBody = self._db.loadNote(key)
226                         buf = self._noteBodyView.get_buffer()
227                         buf.set_text(self._noteBody)
228
229                 gobject.timeout_add(200, self._set_focus)
230
231         @gtk_toolbox.log_exception(_moduleLogger)
232         def _on_note_changed(self, widget = None, data = None):
233                 if self._pos == -1 or self.noteId == -1:
234                         return
235
236                 buf = self._noteBodyView.get_buffer().get_text(self._noteBodyView.get_buffer().get_start_iter(), self._noteBodyView.get_buffer().get_end_iter())
237
238                 title = self._get_title(buf)
239                 value, key = self._noteslist.get_item(self._pos)
240
241                 if value != title:
242                         self._noteslist.change_item(self._pos, title, self.noteId)
243
244         @gtk_toolbox.log_exception(_moduleLogger)
245         def _on_add_note(self, widget = None, data = None):
246                 self._update_noteslist("new")
247
248         @gtk_toolbox.log_exception(_moduleLogger)
249         def _on_delete_note(self, widget = None, data = None):
250                 if (self.noteId == -1):
251                         return
252                 mbox = gtk.MessageDialog(None, gtk.DIALOG_MODAL, gtk.MESSAGE_WARNING, gtk.BUTTONS_YES_NO, _("Really delete?"))
253                 response = mbox.run()
254                 mbox.hide()
255                 mbox.destroy()
256                 if response == gtk.RESPONSE_YES:
257                         self._db.delNote(self.noteId)
258                         self.noteId = -1
259                         self._noteslist.remove_item(self._pos)
260                         self._pos = -1
261                         self._noteBodyView.get_buffer().set_text("")