Updating changelist
[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 = "DROP TABLE items"
53                 #self.db.speichereSQL(sql)
54
55                 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)"
56                 self.db.speichereSQL(sql)
57
58                 self.selection.load()
59                 self.selection.connect("changed", self.update_list)
60                 #self.selection.connect("changedCategory", self.update_category)
61
62                 """
63                 sql = "INSERT INTO items (uid, list, category, title) VALUES (?, ?, ?, ?)"
64                 import uuid
65                 self.db.speichereSQL(sql, (str(uuid.uuid4()), "default", "default", "atitel1"))
66                 self.db.speichereSQL(sql, (str(uuid.uuid4()), "default", "default", "btitel2"))
67                 self.db.speichereSQL(sql, (str(uuid.uuid4()), "default", "default", "ctitel3"))
68                 
69                 print "inserted"
70                 """
71
72         def set_filter(self, filter):
73                 assert filter in self.ALL_FILTERS
74                 self.__filter = filter
75                 self.update_list()
76
77         def get_filter(self):
78                 return self.__filter
79
80         def get_unitsstore(self):
81                 if (self.unitsstore == None):
82                         self.unitsstore = gtk.ListStore(str, str, str, str, str,  str, str, str, str, str, str, str, str)
83                 self.unitsstore.clear()
84                 #row(3) quantities
85                 #row 4 units
86                 #row 6 priority
87                 self.unitsstore.append(["-1", "-1", "", "", "", "", "", "", "", "", "", "", ""])
88                 self.unitsstore.append(["-1", "-1", "", "1", "g", "", "0", "", "", "", "", "", ""])
89                 self.unitsstore.append(["-1", "-1", "", "2", "kg", "", "1", "", "", "", "", "", ""])
90                 self.unitsstore.append(["-1", "-1", "", "3", "liter", "", "2", "", "", "", "", "", ""])
91                 self.unitsstore.append(["-1", "-1", "", "4", "packs", "", "3", "", "", "", "", "", ""])
92                 self.unitsstore.append(["-1", "-1", "", "5", "", "", "4", "", "", "", "", "", ""])
93                 self.unitsstore.append(["-1", "-1", "", "6", "", "", "5", "", "", "", "", "", ""])
94                 self.unitsstore.append(["-1", "-1", "", "7", "", "", "6", "", "", "", "", "", ""])
95                 self.unitsstore.append(["-1", "-1", "", "8", "", "", "7", "", "", "", "", "", ""])
96                 self.unitsstore.append(["-1", "-1", "", "9", "", "", "8", "", "", "", "", "", ""])
97                 self.unitsstore.append(["-1", "-1", "", "", "", "", "9", "", "", "", "", "", ""])
98
99                 return self.unitsstore
100
101         def __calculate_status(self):
102                 if self.__filter == self.SHOW_ACTIVE:
103                         status = "0"
104                 else:
105                         status = "-1"
106                 return status
107
108         def get_liststore(self, titlesearch = ""):
109                 if (self.liststore == None):
110                         self.liststore = gtk.ListStore(str, str, str, str, str,  str, str, str, str, str, str, str, str)
111                 self.liststore.clear()
112
113                 titlesearch = titlesearch+"%"
114
115                 if self.__filter == self.SHOW_ACTIVE:
116                         status = self.__calculate_status()
117                         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"
118                         rows = self.db.ladeSQL(sql, (self.selection.get_list(), self.selection.get_category(True), status, titlesearch))
119                 else:
120                         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"
121                         rows = self.db.ladeSQL(sql, (self.selection.get_list(), self.selection.get_category(True), titlesearch))
122
123                 if rows is not None:
124                         for row in rows:
125                                 uid, status, title, quantitiy, unit, price, priority, date, private, stores, note, custom1, custom2 = row
126                                 if unit == None:
127                                         pass
128                                         #unit = ""
129                                 self.liststore.append([uid, status, title, quantitiy, unit, price, priority, date, private, stores, note, custom1, custom2])
130                         #else:
131                         #self.liststore.append(["-1", "-1", "", " ", "", "", "", "", "", "", "", "", ""])       
132                 #import uuid
133                 #self.liststore.append(["-1", "-1", "", "", "", "", "", "", "", "", "", "", ""])
134
135                 return self.liststore
136
137         def emptyValueExists(self):
138                 for child in self.liststore:
139                         if child[2] == "":
140                                 return True
141                 return False
142
143         def update_row(self, irow, icol, new_text):
144                 #print "liststore 1"
145                 #for x in self.liststore:
146                 #       print x[0], x[2]
147
148                 if -1 < irow and self.liststore[irow][0] != "-1" and self.liststore[irow][0] is not None:
149                         sql = "UPDATE items SET "+self.collist[icol]+" = ? WHERE uid = ?"
150                         self.db.speichereSQL(sql, (new_text, self.liststore[irow][0]), rowid = self.liststore[irow][0])
151
152                         _moduleLogger.info("Updated row: "+self.collist[icol]+" new text "+new_text+" Titel: "+str(self.liststore[irow][2])+" with uid "+str(self.liststore[irow][0]))
153
154                         self.liststore[irow][icol] = new_text
155                 else:
156                         _moduleLogger.warning("update_row: row does not exist")
157                         return
158                         #if (self.emptyValueExists() == True)and(icol<2):
159                         #       #print "letzter Eintrag ohne inhalt"
160                         #       return
161
162                 #print "liststore 2"
163                 #for x in self.liststore:
164                 #       print x[0], x[2]
165
166         def checkout_rows(self):
167                 sql = "UPDATE items SET status = ? WHERE list = ? AND category LIKE ? AND status = ?"
168                 self.db.speichereSQL(sql, ("-1", self.selection.get_list(), self.selection.get_category(True), "1"))
169                 for i in range(len(self.liststore)):
170                         if self.liststore[i][1] == "1":
171                                 self.liststore[i][1] = "-1"
172
173         def add_row(self, title = ""):
174                 #self.update_row(-1, 1, "-1")
175                 #for x in self.liststore:
176                 #       print x[0], x[2]
177                 status = self.__calculate_status()
178                 import uuid
179                 uid = str(uuid.uuid4())
180                 sql = "INSERT INTO items (uid, list, category, status, title) VALUES (?, ?, ?, ?, ?)"
181                 self.db.speichereSQL(sql, (uid, self.selection.get_list(), self.selection.get_category(), status, title), rowid = uid)
182                 _moduleLogger.info("Insertet row: status = "+status+" with uid "+str(uid))
183                         #self.liststore[irow][0] = str(uuid.uuid4())
184
185                 self.liststore.append([uid, status, title, " ", "", "", "", "", "", "", "", "", ""])
186                 self.selection.comboLists_check_for_update()
187                 #       if (irow>-1):
188                 #               self.liststore[irow][icol] = new_text
189                 #               self.liststore[irow][0] = uid
190                 #       else:
191                 #               self.liststore.append([uid, "-1", "", " ", "", "", "", "", "", "", "", "", ""])
192                                 #print "xy", self.liststore[len(self.liststore)-1][0]
193                         #Check if a new list/category is opened
194                 #       self.selection.comboLists_check_for_update()
195
196         def del_row(self, irow, row_iter):
197                 uid = self.liststore[irow][0]
198                 self.liststore.remove(row_iter)
199                 sql = "DELETE FROM items WHERE uid = ?"
200                 self.db.speichereSQL(sql, (uid, ))
201
202         def get_colname(self, i):
203                 if i < len(self.collist):
204                         return self.collist[i]
205                 else:
206                         return None
207
208         def get_colcount(self):
209                 return len(self.collist)
210
211         def rename_category(self, new_name):
212                 sql = "UPDATE items SET category = ? WHERE list = ? AND category = ?"
213                 self.db.speichereSQL(sql, (new_name, self.selection.get_list(), self.selection.get_category()))
214                 self.selection.comboList_changed()
215                 self.selection.set_category(new_name)
216
217         def rename_list(self, new_name):
218                 sql = "UPDATE items SET list = ? WHERE list = ?"
219                 self.db.speichereSQL(sql, (new_name, self.selection.get_list(), ))
220                 self.selection.load()
221                 self.selection.set_list(new_name)
222
223         #@gtk_toolbox.log_exception(_moduleLogger)
224         #def update_category(self, widget = None, data = None, data2 = None, data3 = None):
225         #       self.get_liststore()
226
227         @gtk_toolbox.log_exception(_moduleLogger)
228         def update_list(self, widget = None, data = None, data2 = None, data3 = None):
229                 self.get_liststore()