Pulling in skeleton changes
[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(__name__)
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_name(self):
57                 category = self._categorySelectorButton.get_label()
58                 assert category != ""
59                 return category
60
61         def get_queryable_category(self):
62                 category = self.get_category_name()
63                 if category == self.ALL_CATEGORIES:
64                         category = "%"
65                 assert category != ""
66                 return category
67
68         def get_categories(self):
69                 return iter(self._categories)
70
71         def _get_this_category_index(self):
72                 categoryName = self.get_category_name()
73                 try:
74                         return self._categories.index(categoryName)
75                 except ValueError:
76                         return -1
77
78         def _get_category_index(self, categoryName):
79                 try:
80                         return self._categories.index(categoryName)
81                 except ValueError:
82                         return -1
83
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(
88                         window,
89                         "Categories",
90                         self._categories,
91                         self.get_category_name(),
92                 )
93                 self.set_category(userSelection)
94
95         def set_category(self, categoryName = None):
96                 if categoryName is not None:
97                         if not categoryName:
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")
105
106         def add_category(self, categoryName):
107                 if categoryName in self._categories:
108                         return
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, ))
113
114         def delete_this_category(self):
115                 category = self.get_category_name()
116                 assert category not in (self.ALL_CATEGORIES, self.UNDEFINED_CATEGORY)
117
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, ))
122
123                 self._categories.remove(category)
124                 self.set_category(self.ALL_CATEGORIES)
125
126         def load_categories(self):
127                 sql = "CREATE TABLE categories (id TEXT , liste TEXT)"
128                 self._db.speichereSQL(sql)
129
130                 sql = "SELECT id, liste FROM categories WHERE id = 0 ORDER BY liste"
131                 rows = self._db.ladeSQL(sql)
132                 cats = []
133                 if rows is not None and 0 < len(rows):
134                         for row in rows:
135                                 cats.append(row[1])
136
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)
142
143                 del self._categories[2:] # Leave ALL_CATEGORIES and UNDEFINED_CATEGORY in
144
145                 if cats is not None:
146                         for cat in cats:
147                                 self._categories.append(cat)
148
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])
154                 else:
155                         self._categorySelectorButton.set_label(self.UNDEFINED_CATEGORY)
156
157                 self._lastCategory = self._get_this_category_index()