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(__name__)
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):
44 self._lastCategory = 1
46 _moduleLogger.info("libkopfzeile, init")
47 gtk.HBox.__init__(self, homogeneous = False, spacing = 3)
49 self._categories = [self.ALL_CATEGORIES, self.UNDEFINED_CATEGORY]
50 self._categorySelectorButton = gtk.Button(self.UNDEFINED_CATEGORY)
51 self._categorySelectorButton.connect("clicked", self._on_category_selector)
52 self.pack_start(self._categorySelectorButton, expand = True, fill = True)
54 self.load_categories()
56 def get_category_name(self):
57 category = self._categorySelectorButton.get_label()
61 def get_queryable_category(self):
62 category = self.get_category_name()
63 if category == self.ALL_CATEGORIES:
68 def get_categories(self):
69 return iter(self._categories)
71 def _get_this_category_index(self):
72 categoryName = self.get_category_name()
74 return self._categories.index(categoryName)
78 def _get_category_index(self, categoryName):
80 return self._categories.index(categoryName)
84 @gtk_toolbox.log_exception(_moduleLogger)
85 def _on_category_selector(self, *args):
86 window = gtk_toolbox.find_parent_window(self)
87 userSelection = hildonize.touch_selector_entry(
91 self.get_category_name(),
93 self.set_category(userSelection)
95 def set_category(self, categoryName = None):
96 if categoryName is not None:
98 categoryName = self.UNDEFINED_CATEGORY
99 if categoryName != self.get_category_name():
100 self.add_category(categoryName)
101 self._categorySelectorButton.set_label(categoryName)
102 sql = "UPDATE categories SET liste = ? WHERE id = 1"
103 self._db.speichereSQL(sql, (self._get_this_category_index(), ))
104 self.emit("category_changed")
106 def add_category(self, categoryName):
107 if categoryName in self._categories:
109 assert "%" not in categoryName, "Not sure, but maybe %s can't be in names"
110 self._categories.append(categoryName)
111 sql = "INSERT INTO categories (id, liste) VALUES (0, ?)"
112 self._db.speichereSQL(sql, (categoryName, ))
114 def delete_this_category(self):
115 category = self.get_category_name()
116 assert category not in (self.ALL_CATEGORIES, self.UNDEFINED_CATEGORY)
118 sql = "UPDATE notes SET category = ? WHERE category = ?"
119 self._db.speichereSQL(sql, (self.UNDEFINED_CATEGORY, category))
120 sql = "DELETE FROM categories WHERE liste = ?"
121 self._db.speichereSQL(sql, (category, ))
123 self._categories.remove(category)
124 self.set_category(self.ALL_CATEGORIES)
126 def load_categories(self):
127 sql = "CREATE TABLE categories (id TEXT , liste TEXT)"
128 self._db.speichereSQL(sql)
130 sql = "SELECT id, liste FROM categories WHERE id = 0 ORDER BY liste"
131 rows = self._db.ladeSQL(sql)
133 if rows is not None and 0 < len(rows):
137 sql = "SELECT * FROM categories WHERE id = 1"
138 rows = self._db.ladeSQL(sql)
139 if rows is None or len(rows) == 0:
140 sql = "INSERT INTO categories (id, liste) VALUES (1, 1)"
141 self._db.speichereSQL(sql)
143 del self._categories[2:] # Leave ALL_CATEGORIES and UNDEFINED_CATEGORY in
147 self._categories.append(cat)
149 sql = "SELECT * FROM categories WHERE id = 1"
150 rows = self._db.ladeSQL(sql)
151 if rows is not None and 0 < len(rows):
152 index = int(rows[0][1])
153 self._categorySelectorButton.set_label(self._categories[index])
155 self._categorySelectorButton.set_label(self.UNDEFINED_CATEGORY)
157 self._lastCategory = self._get_this_category_index()