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