2 # -*- coding: utf-8 -*-
5 This file is part of Multilist.
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.
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.
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/>.
20 Copyright (C) 2008 Christoph Würstle
35 _moduleLogger = logging.getLogger(__name__)
38 class Liststorehandler(object):
40 def __init__(self, db, selection):
43 self.unitsstore = None
44 self.selection = selection
45 self.collist = ("uid", "status", "title", "quantitiy", "unit", "price", "priority", "date", "private", "stores", "note", "custom1", "custom2")
47 #sql = "DROP TABLE items"
48 #self.db.speichereSQL(sql)
50 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)"
51 self.db.speichereSQL(sql)
54 self.selection.connect("changed", self.update_list)
55 #self.selection.connect("changedCategory", self.update_category)
58 sql = "INSERT INTO items (uid, list, category, title) VALUES (?, ?, ?, ?)"
60 self.db.speichereSQL(sql, (str(uuid.uuid4()), "default", "default", "atitel1"))
61 self.db.speichereSQL(sql, (str(uuid.uuid4()), "default", "default", "btitel2"))
62 self.db.speichereSQL(sql, (str(uuid.uuid4()), "default", "default", "ctitel3"))
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()
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", "", "", "", "", "", ""])
86 return self.unitsstore
88 def get_liststore(self, titlesearch = ""):
89 if (self.liststore == None):
90 self.liststore = gtk.ListStore(str, str, str, str, str, str, str, str, str, str, str, str, str)
91 self.liststore.clear()
93 titlesearch = titlesearch+"%"
95 if (self.selection.get_status() == "0"): #only 0 and 1 (active items)
96 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"
97 rows = self.db.ladeSQL(sql, (self.selection.get_list(), self.selection.get_category(True), self.selection.get_status(), titlesearch))
99 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"
100 rows = self.db.ladeSQL(sql, (self.selection.get_list(), self.selection.get_category(True), titlesearch))
103 if ((rows is not None) and (len(rows) > 0)):
105 uid, status, title, quantitiy, unit, price, priority, date, private, stores, note, custom1, custom2 = row
109 self.liststore.append([uid, status, title, quantitiy, unit, price, priority, date, private, stores, note, custom1, custom2])
111 #self.liststore.append(["-1", "-1", "", " ", "", "", "", "", "", "", "", "", ""])
113 #self.liststore.append(["-1", "-1", "", "", "", "", "", "", "", "", "", "", ""])
115 return self.liststore
117 def emptyValueExists(self):
118 for child in self.liststore:
123 def update_row(self, irow, icol, new_text):
125 #for x in self.liststore:
128 if -1 < irow and self.liststore[irow][0] != "-1" and self.liststore[irow][0] is not None:
129 sql = "UPDATE items SET "+self.collist[icol]+" = ? WHERE uid = ?"
130 self.db.speichereSQL(sql, (new_text, self.liststore[irow][0]), rowid = self.liststore[irow][0])
132 _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 self.liststore[irow][icol] = new_text
136 _moduleLogger.warning("update_row: row does not exist")
138 #if (self.emptyValueExists() == True)and(icol<2):
139 # #print "letzter Eintrag ohne inhalt"
143 #for x in self.liststore:
146 def checkout_rows(self):
147 sql = "UPDATE items SET status = ? WHERE list = ? AND category LIKE ? AND status = ?"
148 self.db.speichereSQL(sql, ("-1", self.selection.get_list(), self.selection.get_category(True), "1"))
149 for i in range(len(self.liststore)):
150 if self.liststore[i][1] == "1":
151 self.liststore[i][1] = "-1"
153 def add_row(self, title = ""):
154 #self.update_row(-1, 1, "-1")
155 #for x in self.liststore:
157 status = self.selection.get_status()
159 uid = str(uuid.uuid4())
160 sql = "INSERT INTO items (uid, list, category, status, title) VALUES (?, ?, ?, ?, ?)"
161 self.db.speichereSQL(sql, (uid, self.selection.get_list(), self.selection.get_category(), status, title), rowid = uid)
162 _moduleLogger.info("Insertet row: status = "+status+" with uid "+str(uid))
163 #self.liststore[irow][0] = str(uuid.uuid4())
165 self.liststore.append([uid, status, title, " ", "", "", "", "", "", "", "", "", ""])
166 self.selection.comboLists_check_for_update()
168 # self.liststore[irow][icol] = new_text
169 # self.liststore[irow][0] = uid
171 # self.liststore.append([uid, "-1", "", " ", "", "", "", "", "", "", "", "", ""])
172 #print "xy", self.liststore[len(self.liststore)-1][0]
173 #Check if a new list/category is opened
174 # self.selection.comboLists_check_for_update()
176 def del_row(self, irow, row_iter):
177 uid = self.liststore[irow][0]
178 self.liststore.remove(row_iter)
179 sql = "DELETE FROM items WHERE uid = ?"
180 self.db.speichereSQL(sql, (uid, ))
182 def get_colname(self, i):
183 if i < len(self.collist):
184 return self.collist[i]
188 def get_colcount(self):
189 return len(self.collist)
191 def rename_category(self, new_name):
192 sql = "UPDATE items SET category = ? WHERE list = ? AND category = ?"
193 self.db.speichereSQL(sql, (new_name, self.selection.get_list(), self.selection.get_category()))
194 self.selection.comboList_changed()
195 self.selection.set_category(new_name)
197 def rename_list(self, new_name):
198 sql = "UPDATE items SET list = ? WHERE list = ?"
199 self.db.speichereSQL(sql, (new_name, self.selection.get_list(), ))
200 self.selection.load()
201 self.selection.set_list(new_name)
203 #@gtk_toolbox.log_exception(_moduleLogger)
204 #def update_category(self, widget = None, data = None, data2 = None, data3 = None):
205 # self.get_liststore()
207 @gtk_toolbox.log_exception(_moduleLogger)
208 def update_list(self, widget = None, data = None, data2 = None, data3 = None):