Forgot a file
[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         def load(self):
73                 model = self.comboList.get_model()
74                 model.clear()
75                 #self.comboList.remove(0)
76
77                 sql = "SELECT DISTINCT list FROM items ORDER BY list"
78                 rows = self.db.ladeSQL(sql)
79                 if ((rows is not None)and(len(rows)>0)):
80                         for row in rows:
81                                 self.comboList.append_text(row[0])
82                 else:
83                         self.comboList.append_text("default")
84
85                 s = self.db.ladeDirekt("comboListText")
86                 if s != "":
87                         self.comboList.get_child().set_text(s)
88                 else:
89                         self.comboList.set_active(0)
90
91         @gtk_toolbox.log_exception(_moduleLogger)
92         def comboList_changed(self, widget = None, data = None):
93                 #self.comboCategory.set_model(None)
94                 #print "reload categories"
95                 while len(self.comboCategory.get_model())>0:
96                         self.comboCategory.remove_text(0)
97
98                 sql = "SELECT DISTINCT category FROM items WHERE list = ? ORDER BY category"
99                 rows = self.db.ladeSQL(sql, (self.get_list(), ))
100
101                 self.comboCategory.append_text(_("all"))
102                 if ((rows is not None)and(len(rows)>0)):
103                         for row in rows:
104                                 if (row[0] != _("all")):
105                                         self.comboCategory.append_text(row[0])
106
107                 s = self.db.ladeDirekt("comboCategoryText"+self.comboList.get_child().get_text())
108                 if len(s)>0:
109                         self.comboCategory.get_child().set_text(s)
110                 else:
111                         self.comboCategory.set_active(0)
112
113                 self.emit("changed", "list", "")
114                 self.db.speichereDirekt("comboListText", self.comboList.get_child().get_text())
115
116         @gtk_toolbox.log_exception(_moduleLogger)
117         def comboCategory_changed(self, widget = None, data = None):
118                 #_moduleLogger.info("Klasse geaendert zu ")
119                 #self.hauptRegister.set_current_page(0)
120                 self.emit("changed", "category", "")
121                 if self.comboCategory.get_active()>-1:
122                         self.db.speichereDirekt("comboCategoryText"+self.comboList.get_child().get_text(), self.comboCategory.get_child().get_text())
123
124         def comboLists_check_for_update(self):
125                 if self.comboCategory.get_active() == -1:
126                         model = self.comboCategory.get_model()
127                         found = False
128                         cat = self.get_category()
129                         for x in model:
130                                 if x[0] == cat:
131                                         found = True
132                         if found == False:
133                                 self.comboCategory.append_text(self.get_category())
134                                 self.comboCategory.set_active(len(self.comboCategory.get_model())-1)
135                 if self.comboList.get_active() == -1:
136                         model = self.comboList.get_model()
137                         found = False
138                         list = self.get_list()
139                         for x in model:
140                                 if x[0] == list:
141                                         found = True
142                         if found == False:
143                                 self.comboList.append_text(self.get_list())
144                                 self.comboList.set_active(len(self.comboList.get_model())-1)
145
146         def lade(self):
147                 _moduleLogger.warning("Laden der aktuellen position noch nicht implementiert")
148
149         def speichere(self):
150                 _moduleLogger.warning("Speichern der aktuellen position noch nicht implementiert")
151
152         def getIsHildon(self):
153                 return self.isHildon
154
155         def get_category(self, select = False):
156                 s = self.comboCategory.get_child().get_text()
157                 if s == _("all"):
158                         if not select:
159                                 return "undefined"
160                         else:
161                                 return "%"
162                 else:
163                         return s
164
165         def set_category(self, category):
166                 self.comboCategory.get_child().set_text(category)
167
168         def set_list(self, listname):
169                 self.comboList.get_child().set_text(listname)
170
171         def get_list(self):
172                 return self.comboList.get_child().get_text()