893e481e9c0da7e7b15faef989ad595c27a3281a
[multilist] / src / libselection.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 """
5 This file is part of Multilist.
6
7 Multilist is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 Multilist is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Multilist.  If not, see <http://www.gnu.org/licenses/>.
19
20 Copyright (C) 2008 Christoph Würstle
21 """
22
23
24 import logging
25
26 import gobject
27 import gtk
28
29 import gtk_toolbox
30 import hildonize
31
32 try:
33         _
34 except NameError:
35         _ = lambda x: x
36
37
38 _moduleLogger = logging.getLogger(__name__)
39
40
41 class Selection(gtk.HBox):
42
43         __gsignals__ = {
44                 'changed' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_STRING, gobject.TYPE_STRING)),
45                 #'changedCategory': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_STRING, gobject.TYPE_STRING))
46         }
47
48         def __init__(self, db, isHildon):
49                 gtk.HBox.__init__(self, homogeneous = False, spacing = 3)
50
51                 self.db = db
52                 self.isHildon = isHildon
53
54                 _moduleLogger.info("libSelection, init")
55
56                 label = gtk.Label(_("List:"))
57                 self.pack_start(label, expand = False, fill = True, padding = 0)
58
59                 self.__lists = []
60                 self.__listButton = gtk.Button("")
61                 self.__listButton.connect("clicked", self._on_list_selector)
62                 self.pack_start(self.__listButton, expand = True, fill = True, padding = 0)
63
64                 label = gtk.Label(_("  Category:"))
65                 self.pack_start(label, expand = False, fill = True, padding = 0)
66
67                 self.__categories = []
68                 self.__categoryButton = gtk.Button("")
69                 self.__categoryButton.connect("clicked", self._on_category_selector)
70                 self.pack_start(self.__categoryButton, expand = True, fill = True, padding = 0)
71
72         def load(self):
73                 del self.__lists[:]
74
75                 sql = "SELECT DISTINCT list FROM items ORDER BY list"
76                 rows = self.db.ladeSQL(sql)
77                 if rows is not None:
78                         for row in rows:
79                                 self.__lists.append(row[0])
80                 else:
81                         self.__lists.append("default")
82
83                 s = self.db.ladeDirekt("comboListText")
84                 if s != "":
85                         self.__listButton.set_label(s)
86                 else:
87                         self.__listButton.set_label(self.__lists[0])
88
89                 self.update_categories()
90
91         @gtk_toolbox.log_exception(_moduleLogger)
92         def _on_category_selector(self, *args):
93                 window = gtk_toolbox.find_parent_window(self)
94                 userSelection = hildonize.touch_selector_entry(
95                         window,
96                         "Categories",
97                         self.__categories,
98                         self.__categoryButton.get_label(),
99                 )
100                 self.set_category(userSelection)
101                 self.emit("changed", "category", "")
102                 self.db.speichereDirekt("comboCategoryText"+self.__listButton.get_label(), self.__categoryButton.get_label())
103                 self.update_categories()
104
105         @gtk_toolbox.log_exception(_moduleLogger)
106         def _on_list_selector(self, *args):
107                 window = gtk_toolbox.find_parent_window(self)
108                 userSelection = hildonize.touch_selector_entry(
109                         window,
110                         "Lists",
111                         self.__lists,
112                         self.__listButton.get_label(),
113                 )
114                 self.set_list(userSelection)
115
116                 self.update_categories()
117
118                 self.emit("changed", "list", "")
119                 self.db.speichereDirekt("comboListText", self.__listButton.get_label())
120
121         def update_categories(self):
122                 del self.__categories[:]
123
124                 sql = "SELECT DISTINCT category FROM items WHERE list = ? ORDER BY category"
125                 rows = self.db.ladeSQL(sql, (self.get_list(), ))
126
127                 self.__categories.append(_("all"))
128                 if rows is not None:
129                         for row in rows:
130                                 if (row[0] != _("all")):
131                                         self.__categories.append(row[0])
132
133                 s = self.db.ladeDirekt("comboCategoryText"+self.__listButton.get_label())
134                 if len(s)>0:
135                         self.__categoryButton.set_label(s)
136                 else:
137                         self.__categoryButton.set_label(self.__categories[0])
138
139         def comboLists_check_for_update(self):
140                 categoryName = self.__categoryButton.get_label()
141                 if categoryName not in self.__categories:
142                         self.__categories.append(categoryName)
143
144                 listName = self.__listButton.get_label()
145                 if listName not in self.__lists:
146                         self.__lists.append(listName)
147
148         def lade(self):
149                 _moduleLogger.warning("Laden der aktuellen position noch nicht implementiert")
150
151         def speichere(self):
152                 _moduleLogger.warning("Speichern der aktuellen position noch nicht implementiert")
153
154         def getIsHildon(self):
155                 return self.isHildon
156
157         def get_category(self, select = False):
158                 s = self.__categoryButton.get_label()
159                 if s == _("all"):
160                         if not select:
161                                 return "undefined"
162                         else:
163                                 return "%"
164                 else:
165                         return s
166
167         def set_category(self, category):
168                 # @bug the old code might have relied on this firing a combo change event
169                 self.__categoryButton.set_label(category)
170
171         def set_list(self, listname):
172                 # @bug the old code might have relied on this firing a combo change event
173                 self.__listButton.set_label(listname)
174
175         def get_list(self):
176                 return self.__listButton.get_label()