Separated Category and Search in the code/ui cleaning up the UI quite a bit
[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 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                 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)
53
54                 self.load_categories()
55
56         def get_category(self):
57                 category = self._categorySelectorButton.get_label()
58                 if category == self.ALL_CATEGORIES:
59                         category = "%"
60                 if category == "":
61                         category = self.UNDEFINED_CATEGORY
62                         self._categorySelectorButton.set_label(category)
63                 return category
64
65         def _get_category_index(self):
66                 categoryName = self._categorySelectorButton.get_label()
67                 try:
68                         return self._categories.index(categoryName)
69                 except ValueError:
70                         return -1
71
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(
76                         window,
77                         "Categories",
78                         self._categories,
79                         self._categorySelectorButton.get_label(),
80                 )
81                 self.set_category(userSelection)
82
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")
89
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
94
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)
100
101         def delete_this_category(self):
102                 category = self.get_category()
103
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, ))
108
109                 pos = self._get_category_index()
110                 if 1 < pos:
111                         del self._categories[pos]
112                         self._categorySelectorButton.set_label(self.ALL_CATEGORIES)
113
114         def load_categories(self):
115                 sql = "CREATE TABLE categories (id TEXT , liste TEXT)"
116                 self._db.speichereSQL(sql)
117
118                 sql = "SELECT id, liste FROM categories WHERE id = 0 ORDER BY liste"
119                 rows = self._db.ladeSQL(sql)
120                 cats = []
121                 if rows is not None and 0 < len(rows):
122                         for row in rows:
123                                 cats.append(row[1])
124
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)
130
131                 del self._categories[2:] # Leave ALL_CATEGORIES and UNDEFINED_CATEGORY in
132
133                 if cats is not None:
134                         for cat in cats:
135                                 self._categories.append(cat)
136
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])
142                 else:
143                         self._categorySelectorButton.set_label(self.UNDEFINED_CATEGORY)
144
145                 self._lastCategory = self._get_category_index()