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