Added logging of exceptions
[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
31 try:
32         _
33 except NameError:
34         _ = lambda x: x
35
36
37 _moduleLogger = logging.getLogger(__name__)
38
39
40 class Selection(gtk.HBox):
41
42         __gsignals__ = {
43                 'changed' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_STRING, gobject.TYPE_STRING)),
44                 #'changedCategory': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_STRING, gobject.TYPE_STRING))
45         }
46
47         def __init__(self, db, isHildon):
48                 gtk.HBox.__init__(self, homogeneous = False, spacing = 3)
49
50                 self.db = db
51                 self.isHildon = isHildon
52
53                 _moduleLogger.info("libSelection, init")
54
55                 label = gtk.Label(_("List:"))
56                 self.pack_start(label, expand = False, fill = True, padding = 0)
57
58                 self.comboList = gtk.combo_box_entry_new_text()
59                 self.comboList.set_size_request(180, -1)
60                 self.pack_start(self.comboList, expand = False, fill = True, padding = 0)
61
62                 label = gtk.Label(_("  Category:"))
63                 self.pack_start(label, expand = False, fill = True, padding = 0)
64
65                 self.comboCategory = gtk.combo_box_entry_new_text()
66                 self.comboCategory.set_size_request(180, -1)
67                 self.pack_start(self.comboCategory, expand = False, fill = True, padding = 0)
68
69                 self.comboList.connect("changed", self.comboList_changed, None)
70                 self.comboCategory.connect("changed", self.comboCategory_changed, None)
71
72                 label = gtk.Label(_("  View:"))
73                 self.pack_start(label, expand = False, fill = True, padding = 0)
74
75                 self.radio_all = gtk.RadioButton(group = None, label = _("All"), use_underline = True)
76                 self.pack_start(self.radio_all, expand = False, fill = True, padding = 0)
77                 self.radio_active = gtk.RadioButton(group = self.radio_all, label = _("Active"), use_underline = True)
78                 self.pack_start(self.radio_active, expand = False, fill = True, padding = 0)
79                 self.radio_all.connect("toggled", self.radioActive_changed, None)
80
81         def load(self):
82                 model = self.comboList.get_model()
83                 model.clear()
84                 #self.comboList.remove(0)
85
86                 sql = "SELECT DISTINCT list FROM items ORDER BY list"
87                 rows = self.db.ladeSQL(sql)
88                 if ((rows is not None)and(len(rows)>0)):
89                         for row in rows:
90                                 self.comboList.append_text(row[0])
91                 else:
92                         self.comboList.append_text("default")
93
94                 s = self.db.ladeDirekt("comboListText")
95                 if s != "":
96                         self.comboList.get_child().set_text(s)
97                 else:
98                         self.comboList.set_active(0)
99
100         @gtk_toolbox.log_exception(_moduleLogger)
101         def comboList_changed(self, widget = None, data = None):
102                 #self.comboCategory.set_model(None)
103                 #print "reload categories"
104                 while len(self.comboCategory.get_model())>0:
105                         self.comboCategory.remove_text(0)
106
107                 sql = "SELECT DISTINCT category FROM items WHERE list = ? ORDER BY category"
108                 rows = self.db.ladeSQL(sql, (self.get_list(), ))
109
110                 self.comboCategory.append_text(_("all"))
111                 if ((rows is not None)and(len(rows)>0)):
112                         for row in rows:
113                                 if (row[0] != _("all")):
114                                         self.comboCategory.append_text(row[0])
115
116                 s = self.db.ladeDirekt("comboCategoryText"+self.comboList.get_child().get_text())
117                 if len(s)>0:
118                         self.comboCategory.get_child().set_text(s)
119                 else:
120                         self.comboCategory.set_active(0)
121
122                 self.emit("changed", "list", "")
123                 self.db.speichereDirekt("comboListText", self.comboList.get_child().get_text())
124
125         @gtk_toolbox.log_exception(_moduleLogger)
126         def comboCategory_changed(self, widget = None, data = None):
127                 #_moduleLogger.info("Klasse geaendert zu ")
128                 #self.hauptRegister.set_current_page(0)
129                 self.emit("changed", "category", "")
130                 if self.comboCategory.get_active()>-1:
131                         self.db.speichereDirekt("comboCategoryText"+self.comboList.get_child().get_text(), self.comboCategory.get_child().get_text())
132
133         @gtk_toolbox.log_exception(_moduleLogger)
134         def radioActive_changed(self, widget, data = None):
135                 self.emit("changed", "radio", "")
136
137         def comboLists_check_for_update(self):
138                 if self.comboCategory.get_active() == -1:
139                         model = self.comboCategory.get_model()
140                         found = False
141                         cat = self.get_category()
142                         for x in model:
143                                 if x[0] == cat:
144                                         found = True
145                         if found == False:
146                                 self.comboCategory.append_text(self.get_category())
147                                 self.comboCategory.set_active(len(self.comboCategory.get_model())-1)
148                 if self.comboList.get_active() == -1:
149                         model = self.comboList.get_model()
150                         found = False
151                         list = self.get_list()
152                         for x in model:
153                                 if x[0] == list:
154                                         found = True
155                         if found == False:
156                                 self.comboList.append_text(self.get_list())
157                                 self.comboList.set_active(len(self.comboList.get_model())-1)
158
159         def lade(self):
160                 _moduleLogger.warning("Laden der aktuellen position noch nicht implementiert")
161
162         def speichere(self):
163                 _moduleLogger.warning("Speichern der aktuellen position noch nicht implementiert")
164
165         def getIsHildon(self):
166                 return self.isHildon
167
168         def get_category(self, select = False):
169                 s = self.comboCategory.get_child().get_text()
170                 if s == _("all"):
171                         if (select == False):
172                                 return "undefined"
173                         else:
174                                 return "%"
175                 else:
176                         return s
177         def set_category(self, category):
178                 self.comboCategory.get_child().set_text(category)
179
180         def set_list(self, listname):
181                 self.comboList.get_child().set_text(listname)
182
183         def get_list(self):
184                 return self.comboList.get_child().get_text()
185
186         def get_status(self):
187                 #return self.comboCategory.get_child().get_text()
188                 if self.radio_all.get_active() == True:
189                         return "-1"
190                 else:
191                         return "0"