Adding import/export capability
[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                 self.__listLabel = gtk.Label(_("List:"))
57                 self.pack_start(self.__listLabel, 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                 self.__catLabel = gtk.Label(_("  Category:"))
65                 self.pack_start(self.__catLabel, 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 set_orientation(self, orientation):
73                 if orientation == gtk.ORIENTATION_HORIZONTAL:
74                         self.__listLabel.show()
75                         self.__catLabel.show()
76                 elif orientation == gtk.ORIENTATION_VERTICAL:
77                         self.__listLabel.hide()
78                         self.__catLabel.hide()
79                 else:
80                         raise NotImplementedError(orientation)
81
82         def load(self):
83                 del self.__lists[:]
84
85                 sql = "SELECT DISTINCT list FROM items ORDER BY list"
86                 rows = self.db.ladeSQL(sql)
87                 if rows is not None:
88                         for row in rows:
89                                 self.__lists.append(row[0])
90                 if not self.__lists:
91                         self.__lists.append("default")
92
93                 s = self.db.ladeDirekt("comboListText")
94                 if s != "":
95                         self.__listButton.set_label(s)
96                 else:
97                         self.__listButton.set_label(self.__lists[0])
98
99                 self.update_categories()
100
101         @gtk_toolbox.log_exception(_moduleLogger)
102         def _on_category_selector(self, *args):
103                 window = gtk_toolbox.find_parent_window(self)
104                 userSelection = hildonize.touch_selector_entry(
105                         window,
106                         "Categories",
107                         self.__categories,
108                         self.__categoryButton.get_label(),
109                 )
110                 self.set_category(userSelection)
111                 self.emit("changed", "category", "")
112                 self.db.speichereDirekt("comboCategoryText"+self.__listButton.get_label(), self.__categoryButton.get_label())
113                 self.update_categories()
114
115         @gtk_toolbox.log_exception(_moduleLogger)
116         def _on_list_selector(self, *args):
117                 window = gtk_toolbox.find_parent_window(self)
118                 userSelection = hildonize.touch_selector_entry(
119                         window,
120                         "Lists",
121                         self.__lists,
122                         self.__listButton.get_label(),
123                 )
124                 self.set_list(userSelection)
125
126                 self.update_categories()
127
128                 self.emit("changed", "list", "")
129                 self.db.speichereDirekt("comboListText", self.__listButton.get_label())
130
131         def update_categories(self):
132                 del self.__categories[:]
133
134                 sql = "SELECT DISTINCT category FROM items WHERE list = ? ORDER BY category"
135                 rows = self.db.ladeSQL(sql, (self.get_list(), ))
136
137                 self.__categories.append(_("all"))
138                 if rows is not None:
139                         for row in rows:
140                                 if (row[0] != _("all")):
141                                         self.__categories.append(row[0])
142
143                 s = self.db.ladeDirekt("comboCategoryText"+self.__listButton.get_label())
144                 if len(s)>0:
145                         self.__categoryButton.set_label(s)
146                 else:
147                         self.__categoryButton.set_label(self.__categories[0])
148
149         def comboLists_check_for_update(self):
150                 categoryName = self.__categoryButton.get_label()
151                 if categoryName not in self.__categories:
152                         self.__categories.append(categoryName)
153
154                 listName = self.__listButton.get_label()
155                 if listName not in self.__lists:
156                         self.__lists.append(listName)
157
158         def lade(self):
159                 _moduleLogger.warning("Laden der aktuellen position noch nicht implementiert")
160
161         def speichere(self):
162                 _moduleLogger.warning("Speichern der aktuellen position noch nicht implementiert")
163
164         def getIsHildon(self):
165                 return self.isHildon
166
167         def get_category(self, select = False):
168                 s = self.__categoryButton.get_label()
169                 if s == _("all"):
170                         if not select:
171                                 return "undefined"
172                         else:
173                                 return "%"
174                 else:
175                         return s
176
177         def set_category(self, category):
178                 # @bug the old code might have relied on this firing a combo change event
179                 self.__categoryButton.set_label(category)
180
181         def set_list(self, listname):
182                 # @bug the old code might have relied on this firing a combo change event
183                 self.__listButton.set_label(listname)
184
185         def get_list(self):
186                 return self.__listButton.get_label()