Setting up support for branches and tags
[quicknote] / src / libkopfzeile.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
13 import logging
14
15 import gobject
16 import gtk
17
18
19 try:
20         _
21 except NameError:
22         _ = lambda x: x
23
24
25 class Kopfzeile(gtk.HBox):
26
27         __gsignals__ = {
28                 'reload_notes' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, ()),
29         }
30
31         def __init__(self, db):
32                 self.lastCategory = ""
33                 self.db = db
34
35                 gtk.HBox.__init__(self, homogeneous = False, spacing = 3)
36                 logging.info("libkopfzeile, init")
37
38                 categoryHBox = gtk.HBox()
39                 self.pack_start(categoryHBox, expand = False, fill = True, padding = 0)
40
41                 label = gtk.Label(_("Category:  "))
42                 categoryHBox.pack_start(label, expand = False, fill = True, padding = 0)
43
44                 self.comboCategory = gtk.combo_box_entry_new_text()
45                 categoryHBox.pack_start(self.comboCategory, expand = True, fill = True, padding = 0)
46                 self.loadCategories()
47                 self.comboCategory.connect("changed", self.comboCategoryChanged, None)
48
49                 searchHBox = gtk.HBox()
50                 self.pack_start(searchHBox, expand = True, fill = True, padding = 0)
51
52                 label = gtk.Label(_("Search:  "))
53                 searchHBox.pack_start(label, expand = False, fill = True, padding = 0)
54
55                 self.searchEntry = gtk.Entry()
56                 searchHBox.pack_start(self.searchEntry, expand = True, fill = True, padding = 0)
57                 self.searchEntry.connect("changed", self.searchEntryChanged, None)
58
59         def comboCategoryChanged(self, widget = None, data = None):
60                 logging.debug("comboCategoryChanged")
61                 if self.lastCategory != self.comboCategory.get_active():
62                         sql = "UPDATE categories SET liste = ? WHERE id = 1"
63                         self.db.speichereSQL(sql, (self.comboCategory.get_active(), ))
64
65                 self.emit("reload_notes")
66
67         def searchEntryChanged(self, widget = None, data = None):
68                 logging.debug("searchEntryChanged")
69                 self.emit("reload_notes")
70
71         def getCategory(self):
72                 entry = self.comboCategory.get_child()
73                 category = entry.get_text()
74                 if category == _("all"):
75                         category = "%"
76                 if category == "":
77                         category = "undefined"
78                         self.comboCategory.set_active(1)
79                         self.comboCategory.show()
80                 return category
81
82         def defineThisCategory(self):
83                 category = self.getCategory()
84
85                 model = self.comboCategory.get_model()
86                 n = len(self.comboCategory.get_model())
87                 i = 0
88                 active = -1
89                 cats = []
90                 while i < n:
91                         if (model[i][0] == category):
92                                 #self.comboCategory.set_active(i)
93                                 active = i
94                         if (model[i][0]!= "%"):
95                                 cats.append(model[i][0])
96                         i += 1
97
98                 if (active == -1) and (category!= "%"):
99                         self.comboCategory.append_text(category)
100                         sql = "INSERT INTO categories  (id, liste) VALUES (0, ?)"
101                         self.db.speichereSQL(sql, (category, ))
102                         self.comboCategory.set_active(i)
103
104         def getSearchPattern(self):
105                 return self.searchEntry.get_text()
106
107         def loadCategories(self):
108                 sql = "CREATE TABLE categories (id TEXT , liste TEXT)"
109                 self.db.speichereSQL(sql)
110
111                 sql = "SELECT id, liste FROM categories WHERE id = 0 ORDER BY liste"
112                 rows = self.db.ladeSQL(sql)
113                 cats = []
114                 if rows is not None and 0 < len(rows):
115                         for row in rows:
116                                 cats.append(row[1])
117
118                 sql = "SELECT * FROM categories WHERE id = 1"
119                 rows = self.db.ladeSQL(sql)
120                 if rows is None or len(rows) == 0:
121                         sql = "INSERT INTO categories (id, liste) VALUES (1, 1)"
122                         self.db.speichereSQL(sql)
123
124                 #self.comboCategory.clear()
125                 while 0 < len(self.comboCategory.get_model()):
126                         self.comboCategory.remove_text(0)
127
128                 self.comboCategory.append_text(_('all'))
129                 self.comboCategory.append_text('undefined')
130
131                 if cats is not None and 0 < len(cats):
132                         for cat in cats:
133                                 self.comboCategory.append_text(cat)
134
135                 sql = "SELECT * FROM categories WHERE id = 1"
136                 rows = self.db.ladeSQL(sql)
137                 if rows is not None and 0 < len(rows):
138                         self.comboCategory.set_active(int(rows[0][1]))
139                 else:
140                         self.comboCategory.set_active(1)
141
142                 self.lastCategory = self.comboCategory.get_active()