7eb7c35240cfefa539e6a5ca054850af204a068f
[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._db = db
44                 self._lastCategory = 1
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                 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)
56
57                 self.load_categories()
58
59                 searchHBox = gtk.HBox()
60                 self.pack_start(searchHBox, expand = True, fill = True, padding = 0)
61
62                 label = gtk.Label(_("Search:  "))
63                 searchHBox.pack_start(label, expand = False, fill = True, padding = 0)
64
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)
68
69         def get_category(self):
70                 category = self._categorySelectorButton.get_label()
71                 if category == self.ALL_CATEGORIES:
72                         category = "%"
73                 if category == "":
74                         category = self.UNDEFINED_CATEGORY
75                         self._categorySelectorButton.set_label(category)
76                 return category
77
78         def _get_category_index(self):
79                 categoryName = self._categorySelectorButton.get_label()
80                 try:
81                         return self._categories.index(categoryName)
82                 except ValueError:
83                         return -1
84
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(
89                         window,
90                         "Categories",
91                         self._categories,
92                         self._categorySelectorButton.get_label(),
93                 )
94                 self.set_category(userSelection)
95
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")
102
103         def search_entry_changed(self, widget = None, data = None):
104                 _moduleLogger.debug("search_entry_changed")
105                 self.emit("category_changed")
106
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
111
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)
117
118         def delete_this_category(self):
119                 category = self.get_category()
120
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, ))
125
126                 pos = self._get_category_index()
127                 if 1 < pos:
128                         del self._categories[pos]
129                         self._categorySelectorButton.set_label(self.ALL_CATEGORIES)
130
131         def get_search_pattern(self):
132                 return self._searchEntry.get_text()
133
134         def load_categories(self):
135                 sql = "CREATE TABLE categories (id TEXT , liste TEXT)"
136                 self._db.speichereSQL(sql)
137
138                 sql = "SELECT id, liste FROM categories WHERE id = 0 ORDER BY liste"
139                 rows = self._db.ladeSQL(sql)
140                 cats = []
141                 if rows is not None and 0 < len(rows):
142                         for row in rows:
143                                 cats.append(row[1])
144
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)
150
151                 del self._categories[2:] # Leave ALL_CATEGORIES and UNDEFINED_CATEGORY in
152
153                 if cats is not None:
154                         for cat in cats:
155                                 self._categories.append(cat)
156
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])
162                 else:
163                         self._categorySelectorButton.set_label(self.UNDEFINED_CATEGORY)
164
165                 self._lastCategory = self._get_category_index()