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