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