1 #!/usr/bin/env python2.5
2 # -*- coding: utf-8 -*-
5 Copyright (C) 2007 Christoph Würstle
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.
27 _moduleLogger = logging.getLogger("kopfzeile")
30 class Kopfzeile(gtk.HBox):
36 'category_changed' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, ()),
39 ALL_CATEGORIES = _("all")
40 UNDEFINED_CATEGORY = "undefined"
42 def __init__(self, db):
43 self._lastCategory = ""
46 _moduleLogger.info("libkopfzeile, init")
47 gtk.HBox.__init__(self, homogeneous = False, spacing = 3)
49 categoryHBox = gtk.HBox()
50 self.pack_start(categoryHBox, expand = False, fill = True, padding = 0)
52 label = gtk.Label(_("Category: "))
53 categoryHBox.pack_start(label, expand = False, fill = True, padding = 0)
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)
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)
65 searchHBox = gtk.HBox()
66 self.pack_start(searchHBox, expand = True, fill = True, padding = 0)
68 label = gtk.Label(_("Search: "))
69 searchHBox.pack_start(label, expand = False, fill = True, padding = 0)
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)
75 def get_category(self):
76 entry = self.categoryCombo.get_child()
77 category = entry.get_text()
78 if category == self.ALL_CATEGORIES:
81 category = self.UNDEFINED_CATEGORY
82 self._categorySelectorButton.set_label(category)
83 self.categoryCombo.set_active(1)
84 self.categoryCombo.show()
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(
94 self._categorySelectorButton.get_label(),
96 if userSelection == self._categorySelectorButton.get_label():
99 sql = "UPDATE categories SET liste = ? WHERE id = 1"
100 self._db.speichereSQL(sql, (self.categoryCombo.get_active(), ))
102 self.emit("category_changed")
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(), ))
110 self.emit("category_changed")
112 def search_entry_changed(self, widget = None, data = None):
113 _moduleLogger.debug("search_entry_changed")
114 self.emit("category_changed")
116 def define_this_category(self):
117 category = self.get_category()
119 model = self.categoryCombo.get_model()
120 n = len(self.categoryCombo.get_model())
124 for i, row in enumerate(model):
125 if row[0] == category:
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)
138 def delete_this_category(self):
139 category = self.get_category()
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()
148 self.categoryCombo.remove_text(pos)
149 self.categoryCombo.set_active(0)
150 self._categorySelectorButton.set_label(self.ALL_CATEGORIES)
152 def get_search_pattern(self):
153 return self._searchEntry.get_text()
155 def load_categories(self):
156 sql = "CREATE TABLE categories (id TEXT , liste TEXT)"
157 self._db.speichereSQL(sql)
159 sql = "SELECT id, liste FROM categories WHERE id = 0 ORDER BY liste"
160 rows = self._db.ladeSQL(sql)
162 if rows is not None and 0 < len(rows):
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)
172 #self.categoryCombo.clear()
173 while 0 < len(self.categoryCombo.get_model()):
174 self.categoryCombo.remove_text(0)
175 del self._categories[2:]
177 self.categoryCombo.append_text(self.ALL_CATEGORIES)
178 self.categoryCombo.append_text(self.UNDEFINED_CATEGORY)
182 self.categoryCombo.append_text(cat)
183 self._categories.append(cat)
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])
192 self.categoryCombo.set_active(1)
193 self._categorySelectorButton.set_label(self.UNDEFINED_CATEGORY)
195 self._lastCategory = self.categoryCombo.get_active()