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):
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(self):
57 category = self._categorySelectorButton.get_label()
58 if category == self.ALL_CATEGORIES:
61 category = self.UNDEFINED_CATEGORY
62 self._categorySelectorButton.set_label(category)
65 def _get_category_index(self):
66 categoryName = self._categorySelectorButton.get_label()
68 return self._categories.index(categoryName)
72 @gtk_toolbox.log_exception(_moduleLogger)
73 def _on_category_selector(self, *args):
74 window = gtk_toolbox.find_parent_window(self)
75 userSelection = hildonize.touch_selector_entry(
79 self._categorySelectorButton.get_label(),
81 self.set_category(userSelection)
83 def set_category(self, categoryName = None):
84 if categoryName is not None and categoryName != self._categorySelectorButton.get_label():
85 self._categorySelectorButton.set_label(categoryName)
86 sql = "UPDATE categories SET liste = ? WHERE id = 1"
87 self._db.speichereSQL(sql, (self._get_category_index(), ))
88 self.emit("category_changed")
90 def define_this_category(self):
91 category = self.get_category()
92 catIndex = self._get_category_index()
93 cats = self._categories[1:] # Skip ALL_CATEGORIES
95 if catIndex == -1 and category != "%":
96 self._categories.append(category)
97 sql = "INSERT INTO categories (id, liste) VALUES (0, ?)"
98 self._db.speichereSQL(sql, (category, ))
99 self._categorySelectorButton.set_label(category)
101 def delete_this_category(self):
102 category = self.get_category()
104 sql = "UPDATE notes SET category = ? WHERE category = ?"
105 self._db.speichereSQL(sql, (self.UNDEFINED_CATEGORY, category))
106 sql = "DELETE FROM categories WHERE liste = ?"
107 self._db.speichereSQL(sql, (category, ))
109 pos = self._get_category_index()
111 del self._categories[pos]
112 self._categorySelectorButton.set_label(self.ALL_CATEGORIES)
114 def load_categories(self):
115 sql = "CREATE TABLE categories (id TEXT , liste TEXT)"
116 self._db.speichereSQL(sql)
118 sql = "SELECT id, liste FROM categories WHERE id = 0 ORDER BY liste"
119 rows = self._db.ladeSQL(sql)
121 if rows is not None and 0 < len(rows):
125 sql = "SELECT * FROM categories WHERE id = 1"
126 rows = self._db.ladeSQL(sql)
127 if rows is None or len(rows) == 0:
128 sql = "INSERT INTO categories (id, liste) VALUES (1, 1)"
129 self._db.speichereSQL(sql)
131 del self._categories[2:] # Leave ALL_CATEGORIES and UNDEFINED_CATEGORY in
135 self._categories.append(cat)
137 sql = "SELECT * FROM categories WHERE id = 1"
138 rows = self._db.ladeSQL(sql)
139 if rows is not None and 0 < len(rows):
140 index = int(rows[0][1])
141 self._categorySelectorButton.set_label(self._categories[index])
143 self._categorySelectorButton.set_label(self.UNDEFINED_CATEGORY)
145 self._lastCategory = self._get_category_index()