Fixing a typo
[multilist] / src / libliststorehandler.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 import logging
24
25 import gtk
26
27 import gtk_toolbox
28
29 try:
30         _
31 except NameError:
32         _ = lambda x: x
33
34
35 _moduleLogger = logging.getLogger(__name__)
36
37
38 class Liststorehandler(object):
39
40         SHOW_ALL = "all"
41         SHOW_NEW = "-1"
42         SHOW_ACTIVE = "0"
43         SHOW_COMPLETE = "1"
44         ALL_FILTERS = (SHOW_ALL, SHOW_NEW, SHOW_ACTIVE, SHOW_COMPLETE)
45
46         def __init__(self, db, selection):
47                 self.db = db
48                 self.__filter = self.SHOW_ALL
49                 self.liststore = None
50                 self.unitsstore = None
51                 self.selection = selection
52                 self.collist = ("uid", "status", "title", "quantity", "unit", "price", "priority", "date", "private", "stores", "note", "custom1", "custom2")
53
54                 sql = "CREATE TABLE items (uid TEXT, list TEXT, category TEXT, status TEXT, title TEXT, quantity TEXT, unit TEXT, price TEXT, priority TEXT, date TEXT, pcdate TEXT, private TEXT, stores TEXT, note TEXT, custom1 TEXT, custom2 TEXT)"
55                 self.db.speichereSQL(sql)
56
57                 self.selection.load()
58                 self.selection.connect("changed", self.update_list)
59                 #self.selection.connect("changedCategory", self.update_category)
60
61         def set_filter(self, filter):
62                 assert filter in self.ALL_FILTERS
63                 self.__filter = filter
64                 self.update_list()
65
66         def get_filter(self):
67                 return self.__filter
68
69         def get_unitsstore(self):
70                 if self.unitsstore is None:
71                         self.unitsstore = gtk.ListStore(str, str, str, str, str,  str, str, str, str, str, str, str, str)
72                 self.unitsstore.clear()
73                 #row(3) quantities
74                 #row 4 units
75                 #row 6 priority
76                 self.unitsstore.append(["-1", "-1", "", "", "", "", "", "", "", "", "", "", ""])
77                 self.unitsstore.append(["-1", "-1", "", "1", "g", "", "0", "", "", "", "", "", ""])
78                 self.unitsstore.append(["-1", "-1", "", "2", "kg", "", "1", "", "", "", "", "", ""])
79                 self.unitsstore.append(["-1", "-1", "", "3", "liter", "", "2", "", "", "", "", "", ""])
80                 self.unitsstore.append(["-1", "-1", "", "4", "packs", "", "3", "", "", "", "", "", ""])
81                 self.unitsstore.append(["-1", "-1", "", "5", "", "", "4", "", "", "", "", "", ""])
82                 self.unitsstore.append(["-1", "-1", "", "6", "", "", "5", "", "", "", "", "", ""])
83                 self.unitsstore.append(["-1", "-1", "", "7", "", "", "6", "", "", "", "", "", ""])
84                 self.unitsstore.append(["-1", "-1", "", "8", "", "", "7", "", "", "", "", "", ""])
85                 self.unitsstore.append(["-1", "-1", "", "9", "", "", "8", "", "", "", "", "", ""])
86                 self.unitsstore.append(["-1", "-1", "", "", "", "", "9", "", "", "", "", "", ""])
87
88                 return self.unitsstore
89
90         def __calculate_status(self):
91                 if self.__filter == self.SHOW_ALL:
92                         status = self.SHOW_NEW
93                 else:
94                         status = self.__filter
95                 return status
96
97         def get_liststore(self, titlesearch = ""):
98                 if (self.liststore == None):
99                         self.liststore = gtk.ListStore(str, str, str, str, str,  str, str, str, str, str, str, str, str)
100                 self.liststore.clear()
101
102                 titlesearch = titlesearch+"%"
103
104                 if self.__filter != self.SHOW_ALL:
105                         status = self.__calculate_status()
106                         sql = "SELECT uid, status, title, quantity, unit, price, priority, date, private, stores, note, custom1, custom2 FROM items WHERE list = ? AND category LIKE ? AND status = ? AND title like ? ORDER BY category, status, title"
107                         rows = self.db.ladeSQL(sql, (self.selection.get_list(), self.selection.get_category(True), status, titlesearch))
108                 else:
109                         sql = "SELECT uid, status, title, quantity, unit, price, priority, date, private, stores, note, custom1, custom2 FROM items WHERE list = ? AND category LIKE ? AND title LIKE ? ORDER BY category, title ASC"
110                         rows = self.db.ladeSQL(sql, (self.selection.get_list(), self.selection.get_category(True), titlesearch))
111
112                 if rows is not None:
113                         for row in rows:
114                                 uid, status, title, quantity, unit, price, priority, date, private, stores, note, custom1, custom2 = row
115                                 if unit == None:
116                                         pass
117                                         #unit = ""
118                                 self.liststore.append([uid, status, title, quantity, unit, price, priority, date, private, stores, note, custom1, custom2])
119
120                 return self.liststore
121
122         def emptyValueExists(self):
123                 for child in self.liststore:
124                         if child[2] == "":
125                                 return True
126                 return False
127
128         def update_row(self, irow, icol, new_text):
129                 if -1 < irow and self.liststore[irow][0] != "-1" and self.liststore[irow][0] is not None:
130                         sql = "UPDATE items SET "+self.collist[icol]+" = ? WHERE uid = ?"
131                         self.db.speichereSQL(sql, (new_text, self.liststore[irow][0]), rowid = self.liststore[irow][0])
132
133                         _moduleLogger.info("Updated row: "+self.collist[icol]+" new text "+new_text+" Titel: "+str(self.liststore[irow][2])+" with uid "+str(self.liststore[irow][0]))
134
135                         self.liststore[irow][icol] = new_text
136                 else:
137                         _moduleLogger.warning("update_row: row does not exist")
138                         return
139
140         def checkout_rows(self):
141                 sql = "UPDATE items SET status = ? WHERE list = ? AND category LIKE ? AND status = ?"
142                 self.db.speichereSQL(sql, (self.SHOW_NEW, self.selection.get_list(), self.selection.get_category(True), self.SHOW_COMPLETE))
143                 for i in range(len(self.liststore)):
144                         if self.liststore[i][1] == self.SHOW_COMPLETE:
145                                 self.liststore[i][1] = self.SHOW_NEW
146
147         def add_row(self, title = ""):
148                 status = self.__calculate_status()
149                 import uuid
150                 uid = str(uuid.uuid4())
151                 sql = "INSERT INTO items (uid, list, category, status, title) VALUES (?, ?, ?, ?, ?)"
152                 self.db.speichereSQL(sql, (uid, self.selection.get_list(), self.selection.get_category(), status, title), rowid = uid)
153                 _moduleLogger.info("Insertet row: status = "+status+" with uid "+str(uid))
154
155                 self.liststore.append([uid, status, title, " ", "", "", "", "", "", "", "", "", ""])
156                 self.selection.comboLists_check_for_update()
157
158         def del_row(self, irow, row_iter):
159                 uid = self.liststore[irow][0]
160                 self.liststore.remove(row_iter)
161                 sql = "DELETE FROM items WHERE uid = ?"
162                 self.db.speichereSQL(sql, (uid, ))
163
164         def get_colname(self, i):
165                 if i < len(self.collist):
166                         return self.collist[i]
167                 else:
168                         return None
169
170         def get_colcount(self):
171                 return len(self.collist)
172
173         def rename_category(self, new_name):
174                 sql = "UPDATE items SET category = ? WHERE list = ? AND category = ?"
175                 self.db.speichereSQL(sql, (new_name, self.selection.get_list(), self.selection.get_category()))
176                 self.selection.update_categories()
177                 self.selection.set_category(new_name)
178
179         def rename_list(self, new_name):
180                 sql = "UPDATE items SET list = ? WHERE list = ?"
181                 self.db.speichereSQL(sql, (new_name, self.selection.get_list(), ))
182                 self.selection.load()
183                 self.selection.set_list(new_name)
184
185         #@gtk_toolbox.log_exception(_moduleLogger)
186         #def update_category(self, widget = None, data = None, data2 = None, data3 = None):
187         #       self.get_liststore()
188
189         @gtk_toolbox.log_exception(_moduleLogger)
190         def update_list(self, widget = None, data = None, data2 = None, data3 = None):
191                 self.get_liststore()