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