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 categoryHBox = gtk.HBox()
50 self.pack_start(categoryHBox, expand = False, fill = True, padding = 0)
52 self._categories = [self.ALL_CATEGORIES, self.UNDEFINED_CATEGORY]
53 self._categorySelectorButton = gtk.Button(self.UNDEFINED_CATEGORY)
54 self._categorySelectorButton.connect("clicked", self._on_category_selector)
55 categoryHBox.pack_start(self._categorySelectorButton)
57 self.load_categories()
59 searchHBox = gtk.HBox()
60 self.pack_start(searchHBox, expand = True, fill = True, padding = 0)
62 label = gtk.Label(_("Search: "))
63 searchHBox.pack_start(label, expand = False, fill = True, padding = 0)
65 self._searchEntry = gtk.Entry()
66 searchHBox.pack_start(self._searchEntry, expand = True, fill = True, padding = 0)
67 self._searchEntry.connect("changed", self.search_entry_changed, None)
69 def get_category(self):
70 category = self._categorySelectorButton.get_label()
71 if category == self.ALL_CATEGORIES:
74 category = self.UNDEFINED_CATEGORY
75 self._categorySelectorButton.set_label(category)
78 def _get_category_index(self):
79 categoryName = self._categorySelectorButton.get_label()
81 return self._categories.index(categoryName)
85 @gtk_toolbox.log_exception(_moduleLogger)
86 def _on_category_selector(self, *args):
87 window = gtk_toolbox.find_parent_window(self)
88 userSelection = hildonize.touch_selector_entry(
92 self._categorySelectorButton.get_label(),
94 self.set_category(userSelection)
96 def set_category(self, categoryName = None):
97 if categoryName is not None and categoryName != self._categorySelectorButton.get_label():
98 self._categorySelectorButton.set_label(categoryName)
99 sql = "UPDATE categories SET liste = ? WHERE id = 1"
100 self._db.speichereSQL(sql, (self._get_category_index(), ))
101 self.emit("category_changed")
103 def search_entry_changed(self, widget = None, data = None):
104 _moduleLogger.debug("search_entry_changed")
105 self.emit("category_changed")
107 def define_this_category(self):
108 category = self.get_category()
109 catIndex = self._get_category_index()
110 cats = self._categories[1:] # Skip ALL_CATEGORIES
112 if catIndex == -1 and category != "%":
113 self._categories.append(category)
114 sql = "INSERT INTO categories (id, liste) VALUES (0, ?)"
115 self._db.speichereSQL(sql, (category, ))
116 self._categorySelectorButton.set_label(category)
118 def delete_this_category(self):
119 category = self.get_category()
121 sql = "UPDATE notes SET category = ? WHERE category = ?"
122 self._db.speichereSQL(sql, (self.UNDEFINED_CATEGORY, category))
123 sql = "DELETE FROM categories WHERE liste = ?"
124 self._db.speichereSQL(sql, (category, ))
126 pos = self._get_category_index()
128 del self._categories[pos]
129 self._categorySelectorButton.set_label(self.ALL_CATEGORIES)
131 def get_search_pattern(self):
132 return self._searchEntry.get_text()
134 def load_categories(self):
135 sql = "CREATE TABLE categories (id TEXT , liste TEXT)"
136 self._db.speichereSQL(sql)
138 sql = "SELECT id, liste FROM categories WHERE id = 0 ORDER BY liste"
139 rows = self._db.ladeSQL(sql)
141 if rows is not None and 0 < len(rows):
145 sql = "SELECT * FROM categories WHERE id = 1"
146 rows = self._db.ladeSQL(sql)
147 if rows is None or len(rows) == 0:
148 sql = "INSERT INTO categories (id, liste) VALUES (1, 1)"
149 self._db.speichereSQL(sql)
151 del self._categories[2:] # Leave ALL_CATEGORIES and UNDEFINED_CATEGORY in
155 self._categories.append(cat)
157 sql = "SELECT * FROM categories WHERE id = 1"
158 rows = self._db.ladeSQL(sql)
159 if rows is not None and 0 < len(rows):
160 index = int(rows[0][1])
161 self._categorySelectorButton.set_label(self._categories[index])
163 self._categorySelectorButton.set_label(self.UNDEFINED_CATEGORY)
165 self._lastCategory = self._get_category_index()