PARTIAL COMMIT: Got most of the stuff in place to switch category selection over...
[quicknote] / src / kopfzeile.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 import gtk_toolbox
19 import hildonize
20
21 try:
22         _
23 except NameError:
24         _ = lambda x: x
25
26
27 _moduleLogger = logging.getLogger("kopfzeile")
28
29
30 class Kopfzeile(gtk.HBox):
31         """
32         Category/Search box
33         """
34
35         __gsignals__ = {
36                 'category_changed' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, ()),
37         }
38
39         ALL_CATEGORIES = _("all")
40         UNDEFINED_CATEGORY = "undefined"
41
42         def __init__(self, db):
43                 self._lastCategory = ""
44                 self._db = db
45
46                 _moduleLogger.info("libkopfzeile, init")
47                 gtk.HBox.__init__(self, homogeneous = False, spacing = 3)
48
49                 categoryHBox = gtk.HBox()
50                 self.pack_start(categoryHBox, expand = False, fill = True, padding = 0)
51
52                 label = gtk.Label(_("Category:  "))
53                 categoryHBox.pack_start(label, expand = False, fill = True, padding = 0)
54
55                 self._categories = [self.ALL_CATEGORIES, self.UNDEFINED_CATEGORY]
56                 self._categorySelectorButton = gtk.Button(self.UNDEFINED_CATEGORY)
57                 self._categorySelectorButton.connect("clicked", self._on_category_selector)
58                 #categoryHBox.pack_start(self._categorySelectorButton)
59
60                 self.categoryCombo = gtk.combo_box_entry_new_text()
61                 categoryHBox.pack_start(self.categoryCombo, expand = True, fill = True, padding = 0)
62                 self.load_categories()
63                 self.categoryCombo.connect("changed", self.category_combo_changed, None)
64
65                 searchHBox = gtk.HBox()
66                 self.pack_start(searchHBox, expand = True, fill = True, padding = 0)
67
68                 label = gtk.Label(_("Search:  "))
69                 searchHBox.pack_start(label, expand = False, fill = True, padding = 0)
70
71                 self._searchEntry = gtk.Entry()
72                 searchHBox.pack_start(self._searchEntry, expand = True, fill = True, padding = 0)
73                 self._searchEntry.connect("changed", self.search_entry_changed, None)
74
75         def get_category(self):
76                 entry = self.categoryCombo.get_child()
77                 category = entry.get_text()
78                 if category == self.ALL_CATEGORIES:
79                         category = "%"
80                 if category == "":
81                         category = self.UNDEFINED_CATEGORY
82                         self._categorySelectorButton.set_label(category)
83                         self.categoryCombo.set_active(1)
84                         self.categoryCombo.show()
85                 return category
86
87         @gtk_toolbox.log_exception(_moduleLogger)
88         def _on_category_selector(self, *args):
89                 window = gtk_toolbox.find_parent_window(self)
90                 userSelection = hildonize.touch_selector_entry(
91                         window,
92                         "Categories",
93                         self._categories,
94                         self._categorySelectorButton.get_label(),
95                 )
96                 if userSelection == self._categorySelectorButton.get_label():
97                         return
98
99                 sql = "UPDATE categories SET liste = ? WHERE id = 1"
100                 self._db.speichereSQL(sql, (self.categoryCombo.get_active(), ))
101
102                 self.emit("category_changed")
103
104         def category_combo_changed(self, widget = None, data = None):
105                 _moduleLogger.debug("comboCategoryChanged")
106                 if self._lastCategory != self.categoryCombo.get_active():
107                         sql = "UPDATE categories SET liste = ? WHERE id = 1"
108                         self._db.speichereSQL(sql, (self.categoryCombo.get_active(), ))
109
110                 self.emit("category_changed")
111
112         def search_entry_changed(self, widget = None, data = None):
113                 _moduleLogger.debug("search_entry_changed")
114                 self.emit("category_changed")
115
116         def define_this_category(self):
117                 category = self.get_category()
118
119                 model = self.categoryCombo.get_model()
120                 n = len(self.categoryCombo.get_model())
121                 i = 0
122                 active = -1
123                 cats = []
124                 for i, row in enumerate(model):
125                         if row[0] == category:
126                                 active = i
127                         if row[0] != "%":
128                                 cats.append(row[0])
129
130                 if active == -1 and category != "%":
131                         self.categoryCombo.append_text(category)
132                         self._categories.append(category)
133                         sql = "INSERT INTO categories  (id, liste) VALUES (0, ?)"
134                         self._db.speichereSQL(sql, (category, ))
135                         self.categoryCombo.set_active(i)
136                         self._categorySelectorButton.set_label(category)
137
138         def delete_this_category(self):
139                 category = self.get_category()
140
141                 sql = "UPDATE notes SET category = ? WHERE category = ?"
142                 self._db.speichereSQL(sql, (self.UNDEFINED_CATEGORY, category))
143                 sql = "DELETE FROM categories WHERE liste = ?"
144                 self._db.speichereSQL(sql, (category, ))
145                 model = self.categoryCombo.get_model()
146                 pos = self.categoryCombo.get_active()
147                 if 1 < pos:
148                         self.categoryCombo.remove_text(pos)
149                         self.categoryCombo.set_active(0)
150                         self._categorySelectorButton.set_label(self.ALL_CATEGORIES)
151
152         def get_search_pattern(self):
153                 return self._searchEntry.get_text()
154
155         def load_categories(self):
156                 sql = "CREATE TABLE categories (id TEXT , liste TEXT)"
157                 self._db.speichereSQL(sql)
158
159                 sql = "SELECT id, liste FROM categories WHERE id = 0 ORDER BY liste"
160                 rows = self._db.ladeSQL(sql)
161                 cats = []
162                 if rows is not None and 0 < len(rows):
163                         for row in rows:
164                                 cats.append(row[1])
165
166                 sql = "SELECT * FROM categories WHERE id = 1"
167                 rows = self._db.ladeSQL(sql)
168                 if rows is None or len(rows) == 0:
169                         sql = "INSERT INTO categories (id, liste) VALUES (1, 1)"
170                         self._db.speichereSQL(sql)
171
172                 #self.categoryCombo.clear()
173                 while 0 < len(self.categoryCombo.get_model()):
174                         self.categoryCombo.remove_text(0)
175                 del self._categories[2:]
176
177                 self.categoryCombo.append_text(self.ALL_CATEGORIES)
178                 self.categoryCombo.append_text(self.UNDEFINED_CATEGORY)
179
180                 if cats is not None:
181                         for cat in cats:
182                                 self.categoryCombo.append_text(cat)
183                                 self._categories.append(cat)
184
185                 sql = "SELECT * FROM categories WHERE id = 1"
186                 rows = self._db.ladeSQL(sql)
187                 if rows is not None and 0 < len(rows):
188                         index = int(rows[0][1])
189                         self.categoryCombo.set_active(index)
190                         self._categorySelectorButton.set_label(self._categories[index])
191                 else:
192                         self.categoryCombo.set_active(1)
193                         self._categorySelectorButton.set_label(self.UNDEFINED_CATEGORY)
194
195                 self._lastCategory = self.categoryCombo.get_active()