Making search go for pattern in any part of text rather than just beginning
[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 ConfigParser
24 import logging
25
26 import gtk
27
28 import gtk_toolbox
29
30 try:
31         _
32 except NameError:
33         _ = lambda x: x
34
35
36 _moduleLogger = logging.getLogger(__name__)
37
38
39 class Liststorehandler(object):
40
41         SHOW_ALL = "all"
42         SHOW_NEW = "-1"
43         SHOW_ACTIVE = "0"
44         SHOW_COMPLETE = "1"
45         ALL_FILTERS = (SHOW_ALL, SHOW_NEW, SHOW_ACTIVE, SHOW_COMPLETE)
46
47         def __init__(self, db, selection):
48                 self.db = db
49                 self.__filter = self.SHOW_ALL
50                 self.liststore = None
51                 self.unitsstore = None
52                 self.selection = selection
53                 self.collist = ("uid", "status", "title", "quantity", "unit", "price", "priority", "date", "private", "stores", "note", "custom1", "custom2")
54
55                 sql = "CREATE TABLE items (uid TEXT, list TEXT, category TEXT, status TEXT, title TEXT, quantity 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         def save_settings(self, config, sectionName):
63                 config.set(sectionName, "filter", self.__filter)
64
65         def load_settings(self, config, sectionName):
66                 try:
67                         selectedFilter = config.get(sectionName, "filter")
68                         self.set_filter(selectedFilter)
69                 except ConfigParser.NoSectionError:
70                         pass
71                 except ConfigParser.NoOptionError:
72                         pass
73
74         def set_filter(self, filter):
75                 assert filter in self.ALL_FILTERS
76                 self.__filter = filter
77                 self.update_list()
78
79         def get_filter(self):
80                 return self.__filter
81
82         def get_unitsstore(self):
83                 if self.unitsstore is None:
84                         self.unitsstore = gtk.ListStore(str, str, str, str, str,  str, str, str, str, str, str, str, str)
85                 self.unitsstore.clear()
86                 #row(3) quantities
87                 #row 4 units
88                 #row 6 priority
89                 self.unitsstore.append(["-1", "-1", "", "", "", "", "", "", "", "", "", "", ""])
90                 self.unitsstore.append(["-1", "-1", "", "1", "g", "", "0", "", "", "", "", "", ""])
91                 self.unitsstore.append(["-1", "-1", "", "2", "kg", "", "1", "", "", "", "", "", ""])
92                 self.unitsstore.append(["-1", "-1", "", "3", "liter", "", "2", "", "", "", "", "", ""])
93                 self.unitsstore.append(["-1", "-1", "", "4", "packs", "", "3", "", "", "", "", "", ""])
94                 self.unitsstore.append(["-1", "-1", "", "5", "", "", "4", "", "", "", "", "", ""])
95                 self.unitsstore.append(["-1", "-1", "", "6", "", "", "5", "", "", "", "", "", ""])
96                 self.unitsstore.append(["-1", "-1", "", "7", "", "", "6", "", "", "", "", "", ""])
97                 self.unitsstore.append(["-1", "-1", "", "8", "", "", "7", "", "", "", "", "", ""])
98                 self.unitsstore.append(["-1", "-1", "", "9", "", "", "8", "", "", "", "", "", ""])
99                 self.unitsstore.append(["-1", "-1", "", "", "", "", "9", "", "", "", "", "", ""])
100
101                 return self.unitsstore
102
103         def __calculate_status(self):
104                 if self.__filter == self.SHOW_ALL:
105                         status = self.SHOW_NEW
106                 else:
107                         status = self.__filter
108                 return status
109
110         def get_liststore(self, titlesearch = ""):
111                 if self.liststore is None:
112                         self.liststore = gtk.ListStore(str, str, str, str, str, str, str, str, str, str, str, str, str)
113                 self.liststore.clear()
114
115                 titlesearch = "%"+titlesearch+"%"
116
117                 if self.__filter != self.SHOW_ALL:
118                         status = self.__calculate_status()
119                         sql = "SELECT uid, status, title, quantity, 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"
120                         rows = self.db.ladeSQL(sql, (self.selection.get_list(), self.selection.get_category(True), status, titlesearch))
121                 else:
122                         sql = "SELECT uid, status, title, quantity, unit, price, priority, date, private, stores, note, custom1, custom2 FROM items WHERE list = ? AND category LIKE ? AND title LIKE ? ORDER BY category, title ASC"
123                         rows = self.db.ladeSQL(sql, (self.selection.get_list(), self.selection.get_category(True), titlesearch))
124
125                 if rows is not None:
126                         for row in rows:
127                                 uid, status, title, quantity, unit, price, priority, date, private, stores, note, custom1, custom2 = row
128                                 if unit == None:
129                                         pass
130                                         #unit = ""
131                                 self.liststore.append([uid, status, title, quantity, unit, price, priority, date, private, stores, note, custom1, custom2])
132
133                 return self.liststore
134
135         def emptyValueExists(self):
136                 for child in self.liststore:
137                         if child[2] == "":
138                                 return True
139                 return False
140
141         def update_row(self, irow, icol, new_text):
142                 if -1 < irow and self.liststore[irow][0] != "-1" and self.liststore[irow][0] is not None:
143                         sql = "UPDATE items SET "+self.collist[icol]+" = ? WHERE uid = ?"
144                         self.db.speichereSQL(sql, (new_text, self.liststore[irow][0]), rowid = self.liststore[irow][0])
145
146                         _moduleLogger.info("Updated row: "+self.collist[icol]+" new text "+new_text+" Titel: "+str(self.liststore[irow][2])+" with uid "+str(self.liststore[irow][0]))
147
148                         self.liststore[irow][icol] = new_text
149                 else:
150                         _moduleLogger.warning("update_row: row does not exist")
151                         return
152
153         def checkout_rows(self):
154                 sql = "UPDATE items SET status = ? WHERE list = ? AND category LIKE ? AND status = ?"
155                 self.db.speichereSQL(sql, (self.SHOW_NEW, self.selection.get_list(), self.selection.get_category(True), self.SHOW_COMPLETE))
156                 for i in range(len(self.liststore)):
157                         if self.liststore[i][1] == self.SHOW_COMPLETE:
158                                 self.liststore[i][1] = self.SHOW_NEW
159
160         def add_row(self, title = ""):
161                 status = self.__calculate_status()
162                 import uuid
163                 uid = str(uuid.uuid4())
164                 sql = "INSERT INTO items (uid, list, category, status, title) VALUES (?, ?, ?, ?, ?)"
165                 self.db.speichereSQL(sql, (uid, self.selection.get_list(), self.selection.get_category(), status, title), rowid = uid)
166                 _moduleLogger.info("Insertet row: status = "+status+" with uid "+str(uid))
167
168                 self.liststore.append([uid, status, title, " ", "", "", "", "", "", "", "", "", ""])
169                 self.selection.comboLists_check_for_update()
170
171         def del_row(self, irow, row_iter):
172                 uid = self.liststore[irow][0]
173                 self.liststore.remove(row_iter)
174                 sql = "DELETE FROM items WHERE uid = ?"
175                 self.db.speichereSQL(sql, (uid, ))
176
177         def get_colname(self, i):
178                 if i < len(self.collist):
179                         return self.collist[i]
180                 else:
181                         return None
182
183         def get_colcount(self):
184                 return len(self.collist)
185
186         def rename_category(self, new_name):
187                 sql = "UPDATE items SET category = ? WHERE list = ? AND category = ?"
188                 self.db.speichereSQL(sql, (new_name, self.selection.get_list(), self.selection.get_category()))
189                 self.selection.update_categories()
190                 self.selection.set_category(new_name)
191
192         def rename_list(self, new_name):
193                 sql = "UPDATE items SET list = ? WHERE list = ?"
194                 self.db.speichereSQL(sql, (new_name, self.selection.get_list(), ))
195                 self.selection.load()
196                 self.selection.set_list(new_name)
197
198         #@gtk_toolbox.log_exception(_moduleLogger)
199         #def update_category(self, widget = None, data = None, data2 = None, data3 = None):
200         #       self.get_liststore()
201
202         @gtk_toolbox.log_exception(_moduleLogger)
203         def update_list(self, widget = None, data = None, data2 = None, data3 = None):
204                 self.get_liststore()