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