4 # TabNote - Note Taking Application
5 # Copyright 2011 Paul Hänsch
6 # Contact: haensch.paul@gmail.com
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
22 from pysqlite2 import dbapi2
28 Database storage layer
33 Open a database, create and initialize the database if needed
35 self.dbcon = dbapi2.connect(os.getenv('HOME') + os.sep + '.tabnote.sqlite')
36 self.dbcur = self.dbcon.cursor()
37 initnote = ('Welcome to TabNote!\n\n' +
38 'Click the "+"-Tab to open a new note.\n' +
39 'Double Click on the selected tab to relabel or delete it.\n' +
40 'Notes are stored when you exit the program.\n\n' +
42 'Ctrl Left - switch to previous tab\n' +
43 'Ctrl Right - switch to next tab\n' +
44 'Ctrl Return - bring up relabel/delete dialog\n' +
45 'Ctrl t - create new tab\n' +
46 'Ctrl q - save and quit')
48 self.dbcur.execute('CREATE TABLE settings (key TEXT, value TEXT, UNIQUE (key))')
49 self.dbcur.execute('INSERT INTO settings (key, value) VALUES ("version", "1.0");')
50 self.dbcur.execute('INSERT INTO settings (key, value) VALUES ("geometry", "420x300");')
51 self.dbcur.execute('CREATE TABLE notes (id INTEGER PRIMARY KEY, label TEXT, content TEXT);')
52 self.dbcur.execute('INSERT INTO notes (label, content) VALUES ("Introduction", "' +
53 self.escape(initnote) + '");')
56 def getSetting(self, key):
57 self.dbcur.execute('SELECT value FROM settings WHERE key = "' + self.escape(key) + '";')
58 try: return self.dbcur.fetchall()[0][0]
61 def setSetting(self, key, value):
63 self.dbcur.execute('INSERT INTO settings (key, value) VALUES ("' + self.escape(key) +
64 '", "' + self.escape(value) + '");')
66 self.dbcur.execute('UPDATE settings SET value = "' + self.escape(value) +
67 '" WHERE key = "' + self.escape(key) + '";')
69 def escape(self, string):
71 Return sanitized string for use in SQL-Statements
75 if c == '"': ret += '""'
81 Return list of all recorded notes
82 Each item is an array of the format [id, label, textcontent]
84 self.dbcur.execute('SELECT id, label, content FROM notes ORDER BY id;')
85 return self.dbcur.fetchall()
87 def setRecord(self, rid = None, label = None, content = None):
89 Create or alter a record
92 if label: label = self.escape(label)
94 if content: content = self.escape(content)
96 self.dbcur.execute('INSERT INTO notes (label, content) VALUES ("' + self.escape(label) +
97 '", "' + self.escape(content) + '");')
100 self.dbcur.execute('UPDATE notes SET content = "' + self.escape(content) +
101 '" WHERE id = ' + str(rid) + ';')
103 self.dbcur.execute('UPDATE notes SET label = "' + self.escape(label) +
104 '" WHERE id = ' + str(rid) + ';')
105 return self.dbcur.lastrowid
107 def delRecord(self, rid):
111 self.dbcur.execute('DELETE FROM notes WHERE id = ' + str(rid) + ';')
115 Flush and close databese
123 Main application class
127 Open Database, create Main application window, and retrieve stored data
130 self.storage = Persistence()
131 self.protocol('WM_DELETE_WINDOW', self.quit)
132 self.title('TabNote')
134 self.relabelWin = False
137 self.tablist = Pmw.NoteBook(self, raisecommand = self.tabselect, lowercommand = self.tabunselect)
139 self.tablist.pack(expand = True, fill = BOTH)
140 for tab in self.storage.allRecords():
141 self.addTab(tab[0], tab[1], tab[2])
142 self.tablist.delete(Pmw.END)
143 self.tablist.add('+')
145 self.bind_all('<Control-Left>', self.previouspage)
146 self.bind_all('<Control-Right>', self.nextpage)
147 self.bind_all('<Control-t>', self.newTab)
148 self.bind_all('<Control-q>', self.quit)
149 self.bind_all('<Control-Return>', self.tabedit)
152 self.geometry(self.storage.getSetting('geometry'))
153 self.tablist.selectpage(self.storage.getSetting('activeTab'))
156 def newTab(self, junk):
158 Event wrapper for new tab
160 self.tablist.selectpage('+')
162 def previouspage(self, junk):
164 Event wrapper for tab switching
166 if self.tablist.getcurselection() == self.tablist.pagenames()[0]:
167 self.tablist.selectpage(self.tablist.pagenames()[-2])
169 self.tablist.previouspage()
171 def nextpage(self, junk):
173 Event wrapper for tab switching
175 if self.tablist.getcurselection() == self.tablist.pagenames()[-2]:
176 self.tablist.selectpage(0)
178 self.tablist.nextpage()
180 def closeRelabel(self, junk = None):
182 Close relabeling dialog window
184 self.relabelWin.destroy()
185 self.relabelWin = False
187 def delTab(self, junk = None):
189 Delete a tab (and its record)
190 Called by relabeling dialog
192 for tab in self.contents:
193 if tab[1] == self.tablist.page(self.relabel):
194 self.storage.delRecord(tab[0])
195 self.contents.remove(tab)
196 self.tablist.previouspage()
197 self.tablist.delete(self.relabel)
201 def relabelTab(self, junk = None):
203 Change the name of a tab
204 Called by relabeling dialog
206 for tab in self.contents:
207 if tab[1] == self.tablist.page(self.relabel) and self.relabel != self.relabelText.get():
208 label = self.newName(self.relabelText.get())
209 content = tab[2].getvalue()[:-1]
211 tab[1] = self.tablist.insert(label, before = self.tablist.index(self.relabel))
212 self.tablist.tab(label).bind(sequence = '<Double-Button-1>', func = self.tabedit)
213 tab[2] = Pmw.ScrolledText(tab[1])
214 tab[2].setvalue(content)
215 tab[2].pack(expand = True, fill = BOTH)
217 self.tablist.previouspage()
218 self.tablist.delete(self.relabel)
219 self.storage.setRecord(tab[0], label = label)
223 def tabedit(self, junk):
225 Bring up relabeling dialog
227 if not self.relabelWin:
228 self.relabel = self.tablist.getcurselection()
229 self.relabelWin = Tk()
230 self.relabelWin.bind('<Escape>', self.closeRelabel)
231 self.relabelWin.protocol('WM_DELETE_WINDOW', self.closeRelabel)
232 self.relabelText = Entry(self.relabelWin)
233 self.relabelText.insert(0, self.relabel)
234 self.relabelText.focus()
235 self.relabelText.bind('<Return>', self.relabelTab)
236 self.relabelText.pack(side = TOP, fill = X, expand = True)
237 relabelButton = Button(self.relabelWin, text='Relabel', command=self.relabelTab)
238 relabelButton.bind('<Return>', self.relabelTab)
239 relabelButton.pack(side = LEFT, fill = BOTH, expand = True)
240 deleteButton = Button(self.relabelWin, text='Delete', command=self.delTab)
241 deleteButton.bind('<Return>', self.delTab)
242 deleteButton.pack(side = LEFT, fill = BOTH, expand = True)
244 def newName(self, basename = 'New'):
246 Return a unique name for a tab by appending a number to the given basename
248 if not basename in self.tablist.pagenames(): return basename
250 while (basename + str(incrementor)) in self.tablist.pagenames(): incrementor += 1
251 return (basename + str(incrementor))
253 def tabunselect(self, name):
255 Called when a tab becomes unselected, write the DB record for this tab
257 for tab in self.contents:
258 if tab[1] == self.tablist.page(name):
259 self.storage.setRecord(tab[0], content = tab[2].getvalue()[:-1])
261 def addTab(self, rid, label = 'New', content = ""):
265 label = self.newName(label)
266 self.contents.append([rid, self.tablist.insert(label, before = self.tablist.index(Pmw.END)), None])
267 self.tablist.tab(label).bind(sequence = '<Double-Button-1>', func = self.tabedit)
268 self.contents[-1][2] = Pmw.ScrolledText(self.contents[-1][1])
269 self.contents[-1][2].setvalue(content)
270 self.contents[-1][2].pack(expand = True, fill = BOTH)
272 def tabselect(self, name):
274 Called when a tab is selected
275 Normally sets focus to the text editor widget
276 If the + tab is selected, this function creates a new record and tab
279 self.addTab(self.storage.setRecord(None, 'New', ''), 'New', '');
280 self.tablist.previouspage();
282 for tab in self.contents:
283 if tab[1] == self.tablist.page(name):
284 tab[2].component('text').focus()
286 def quit(self, junk = None):
288 Store all data and terminate program
290 self.storage.setSetting('activeTab', self.tablist.getcurselection())
291 self.storage.setSetting('geometry', str(self.geometry()))
292 self.tabunselect(self.tablist.getcurselection())