4bc6d5727d7b6d56a0cbeedb84f947cb31c5398a
[public/library.git] / browser.py
1 import sys
2 import curses
3 import dbLayer as db
4 from form import bookForm,categoryForm
5
6 class browserWindow:
7     hl=0
8     topline = 0
9     entries = []
10     selected = []
11     commands = [(' /', 'search'), (' n', 'find next'), (' N', 'find previous'), (' q', 'quit')]
12     cs = []
13     # column definitions are in (label, weight, specified width) triples
14     columnDefs = [('something',1,None)]
15     mx = my = 0
16     cx = cy = 0
17     # for searches
18     last_search = ""
19     found_index = 0
20
21     def __init__(self,window,helpbar):
22         self.w = window
23         self.hb = helpbar
24         self.updateGeometry()
25         self.commands = self.cs+self.commands
26
27     def sortByColumn(self, col):
28         self.entries.sort(key=lambda k: k.get(col)) # key=dict.get(col))
29         self.selected = map(lambda x: False, self.selected)
30
31     def updateGeometry(self):
32         (self.my,self.mx)=self.w.getmaxyx()
33         (y,x) = self.w.getbegyx()
34         self.cx = x + self.mx/2
35         self.cy = y + self.my/2
36         self.pageSize = self.my-4
37         self.calcColWidths()
38
39     def calcColWidths(self):
40         total_weights = 0
41         available_space = self.mx - len(self.columnDefs) -2
42         cols = []
43         for label,weight,value in self.columnDefs:
44             if value!=None:
45                 available_space -= value
46             else:
47                 total_weights+=weight
48
49         for label,weight,value in self.columnDefs:
50             if value!=None:
51                 cols.append((label,value))
52             else:
53                 cols.append((label,available_space*weight/total_weights))
54         self.columns=cols
55
56     def refresh(self):
57         self.hb.commands = self.commands
58         self.hb.refresh()
59         self.w.box()
60         self.displayHeader()
61         for r in range(0,self.pageSize):
62             self.displayRow(r)
63         self.w.refresh()
64         self.highlight()
65
66     def clear(self):
67         self.w.erase()
68         self.w.refresh()
69
70     def centreChild(self,child):
71         (y,x)=child.getmaxyx()
72         child.mvwin(self.cy-y/2,self.cx-x/2)
73
74
75     def displayHeader(self):
76         cursor = 2
77         for header,width in self.columns:
78             self.w.addnstr(1,cursor,header+" "*width,width)
79             self.w.addstr(2,cursor,"-"*width)
80             cursor += width+1
81
82     def displayRow(self,row):
83         if self.topline+row < len(self.entries):
84             entry = self.entries[self.topline+row]
85             cursor = 2
86             if self.selected[self.topline+row]:
87                 self.w.addstr(row+3, 1, "*")
88             else:
89                 self.w.addstr(row+3, 1, " ")
90             for k,width in self.columns:
91                 if k.lower() in entry:
92                     self.w.addnstr(row+3,cursor,str(entry[k.lower()])+" "*width,width)
93                 cursor += width+1
94         else:
95             self.w.addstr(row+3,1," "*(self.mx-2))
96
97     def highlight(self):
98         row = self.hl-self.topline+3
99         if row > 1 and row < self.my:
100             self.w.chgat(row,1,self.mx-2,curses.A_REVERSE)
101
102     def unHighlight(self):
103         row = self.hl-self.topline+3
104         if row > 1 and row < self.my:
105             self.w.chgat(row,1,self.mx-2,curses.A_NORMAL)
106
107     def mvHighlight(self,delta):
108         new = self.hl+delta
109         new = max(new,0)
110         new = min(new,len(self.entries)-1)
111         self.unHighlight()
112         self.hl = new
113         self.highlight()
114     
115     def scroll(self,delta):
116         self.unHighlight()
117         self.topline += delta
118         self.topline = min(self.topline,len(self.entries)-1)
119         self.topline = max(self.topline,0)
120         self.refresh()
121
122     def search(self, string):
123         i = 0
124         found = False
125         for e in self.entries:
126             for k,v in e.items():
127                 if str(v).find(string) != -1:
128                     found = True
129             if found:
130                 break
131             i += 1;
132         if found:
133             self.last_search = string
134             self.search_index = i
135             return i
136         else:
137             self.search_index = -1
138             return -1
139
140     def findNext(self):
141         if self.last_search == "" or self.search_index == -1:
142             return -1
143         found = False
144         for i in range(self.hl+1,len(self.entries)-1):
145             for k,v in self.entries[i].items():
146                 if str(v).find(self.last_search) != -1:
147                     found = True
148             if found:
149                 break
150         if found:
151             self.search_index = i
152             return i
153         else:
154             return -1
155
156     def findPrevious(self):
157         if self.last_search == "" or self.search_index == -1:
158             return -1
159         found = False
160         for i in range(self.hl-1, 0, -1):
161             for k,v in self.entries[i].items():
162                 if str(v).find(self.last_search) != -1:
163                     found = True
164             if found:
165                 break
166         if found:
167             self.search_index = i
168             return i
169         else:
170             return -1
171
172
173     def eventLoop(self):
174         self.w.keypad(1)
175         self.refresh()
176
177         ch = self.w.getch()
178         while ch != 27 and ch != 113:
179             ch = self.handleInput(ch)
180             if ch==113:
181                 return {}
182             self.w.refresh()
183             ch = self.w.getch()
184             self.hb.refresh()
185
186     def handleInput(self,ch):
187         if ch == curses.KEY_UP or ch == 107 or ch == 16:
188             if self.hl == self.topline:
189                 self.scroll(-self.pageSize/2-1)
190             self.mvHighlight(-1)
191         elif ch == curses.KEY_DOWN or ch == 106 or ch == 14:
192             if self.hl == self.topline+self.pageSize-1:
193                 self.scroll(+self.pageSize/2+1)
194             self.mvHighlight(+1)
195         elif ch == curses.KEY_PPAGE:
196             self.scroll(-self.pageSize)
197             self.mvHighlight(-self.pageSize)
198         elif ch == curses.KEY_NPAGE:
199             self.scroll(+self.pageSize)
200             self.mvHighlight(+self.pageSize)
201         elif ch == 47: # forward slash
202             string = self.hb.getSearch()
203             hl = self.search(string)
204             if hl != -1:
205                 delta = hl - self.hl
206                 self.scroll(delta)
207                 self.mvHighlight(delta)
208             else:
209                 self.hb.display(string+' not found')
210         elif ch == 110: # n
211             hl = self.findNext()
212             if hl != -1:
213                 delta = hl - self.hl
214                 self.scroll(delta)
215                 self.mvHighlight(delta)
216             else:
217                 self.hb.display(self.last_search+' not found')
218         elif ch == 78: # N
219             hl = self.findPrevious()
220             if hl != -1:
221                 delta = hl - self.hl
222                 self.scroll(delta)
223                 self.mvHighlight(delta)
224             else:
225                 self.hb.display(self.last_search+' not found')
226         elif ch == 32:
227             if len(self.selected)>0:
228                 self.selected[self.hl] = not self.selected[self.hl]
229             self.displayRow(self.hl-self.topline)
230             self.highlight()
231
232
233
234 class trashBrowser(browserWindow):
235     columnDefs = [('ID',0,3),
236                   ('ISBN',0,13),
237                   ('Authors',30,None),
238                   ('Title',60,None)]
239     
240     cs = [(' r', 'restore selected'), (' d', 'delete selected')]
241     
242     # redefinable functions
243     def viewSelection(self,book):
244         bookid = book['id']
245         w=curses.newwin(1,1,20,20)
246         bf = bookForm(w,self.hb,book)
247         self.centreChild(w)
248         bf.caption='Viewing Book '+str(bookid)
249         bf.blabel='done'
250         bf.eventLoop()
251         bf.clear()
252
253     def restoreSelected(self):
254         books = []
255         for sel,book in zip(self.selected, self.entries):
256             if sel:
257                 books.append(book)
258         db.restoreBooks(books)
259
260     def delSelected(self):
261         books = []
262         for sel,book in zip(self.selected, self.entries):
263             if sel:
264                 books.append(book)
265         db.deleteBooks(books)
266
267     def refreshBooks(self):
268         self.entries = db.getRemovedBooks()
269         self.selected = map(lambda x:False, self.entries)
270
271     def handleInput(self,ch):
272         browserWindow.handleInput(self,ch)
273         if ch == 10:
274             book = self.entries[self.hl]
275             self.viewSelection(book)
276             self.refresh()
277         if ch==114: #restore books
278             count=0
279             for s in self.selected[0:self.hl-1]:
280                 if s:
281                     count+=1
282             self.restoreSelected()
283             self.refreshBooks()
284             self.refresh()
285             self.scroll(-count)
286             self.mvHighlight(-count)
287         if ch==100: # delete books
288             count=0
289             for s in self.selected[0:self.hl-1]:
290                 if s:
291                     count+=1
292             self.delSelected()
293             self.refreshBooks()
294             self.refresh()
295             self.scroll(-count)
296             self.mvHighlight(-count)
297         return ch
298
299 class bookBrowser(browserWindow):
300     columnDefs = [('ID',0,3),
301                   ('ISBN',0,13),
302                   ('Authors',30,None),
303                   ('Title',60,None)]
304     
305     cs = [(' u', 'update'), (' d', 'delete selected')]
306     
307     # redefinable functions
308     def updateSelection(self,book):
309         bookid = book['id']
310         
311         w=curses.newwin(1,1)
312         bf=bookForm(w,self.hb,book)
313         self.centreChild(w)
314         bf.caption='Update Book '+str(bookid)
315         bf.blabel='update'
316         newbook = bf.eventLoop()
317         if len(newbook)!=0:
318             db.updateBook(newbook,bookid)
319         bf.clear()
320
321     def viewSelection(self,book):
322         bookid = book['id']
323         w=curses.newwin(1,1,20,20)
324         bf = bookForm(w,self.hb,book)
325         self.centreChild(w)
326         bf.caption='Viewing Book '+str(bookid)
327         bf.blabel='done'
328         bf.eventLoop()
329         bf.clear()
330
331     def categorizeSelection(self,book):
332         w = curses.newwin(40,20,20,20)
333         cs = categorySelector(w,self.hb)
334         self.centreChild(w)
335         cs.book = book
336         cs.refreshCategories()
337         cs.eventLoop()
338         cs.clear()
339     
340     def delSelected(self):
341         books = []
342         for sel,book in zip(self.selected, self.entries):
343             if sel:
344                 books.append(book)
345         db.removeBooks(books)
346
347     def refreshBooks(self):
348         self.entries = db.getBooks()
349         self.selected = map(lambda x:False, self.entries)
350
351     def refreshBooksInCategory(self,cat):
352         self.entries = db.getBooksByCategory(cat)
353         self.selected = map(lambda x:False, self.entries)
354
355     def handleInput(self,ch):
356         browserWindow.handleInput(self,ch)
357         if ch == 117: #update on 'u'
358             book = self.entries[self.hl]
359             self.updateSelection(book)
360             self.entries[self.hl]=db.getBookByID(book['id'])
361             self.refresh()
362         elif ch == 10:
363             book = self.entries[self.hl]
364             self.viewSelection(book)
365             self.refresh()
366         elif ch == 99:
367             book = self.entries[self.hl]
368             self.categorizeSelection(book)
369             self.refresh()
370         if ch==100:
371             count=0
372             for s in self.selected[0:self.hl-1]:
373                 if s:
374                     count+=1
375             self.delSelected()
376             self.refreshBooks()
377             self.refresh()
378             self.scroll(-count)
379             self.mvHighlight(-count)
380         return ch
381
382 class categoryBrowser(browserWindow):
383     columnDefs = [('Category',100,None)]
384     cs = [(' a', 'add category'), (' d', 'delete selected')]
385
386
387     def refreshCategories(self):
388         self.entries = db.getCategories()
389         self.sortByColumn('category')
390         self.selected = map(lambda x:False, self.entries)
391
392     def addCategory(self):
393         w = curses.newwin(1,1,10,10)
394         cf = categoryForm(w,self.hb)
395         self.centreChild(w)
396         cats = cf.eventLoop()
397         for c in cats:
398             db.addCategory(c)
399         cf.clear()
400
401     def viewCategory(self):
402         w = curses.newwin(20,80,20,20)
403         b = bookBrowser(w,self.hb)
404         self.centreChild(w)
405         b.refreshBooksInCategory(self.entries[self.hl])
406         b.eventLoop()
407         b.clear()
408
409     def delSelected(self):
410         categories = []
411         for sel,cat in zip(self.selected, self.entries):
412             if sel:
413                 categories.append(cat)
414         db.deleteCategories(categories)
415
416     def handleInput(self,ch):
417         browserWindow.handleInput(self,ch)
418         if ch==97:
419             self.addCategory()
420             self.refreshCategories()
421             self.refresh()
422         if ch ==10:
423             self.viewCategory()
424             self.refresh()
425         if ch==100:
426             count=0
427             for s in self.selected[0:self.hl-1]:
428                 if s:
429                     count+=1
430             self.delSelected()
431             self.refreshCategories()
432             self.refresh()
433             self.scroll(-count)
434             self.mvHighlight(-count)
435         return ch
436
437 class categorySelector(browserWindow):
438     columnDefs = [('Category',100,None)]
439     cs = [(' a', 'add category'), (' c', 'commit')]
440     book = {'id':''}
441     original=[]
442
443
444     def refreshCategories(self):
445         self.entries = db.getCategories()
446         self.sortByColumn('category')
447         self.refreshSelected()
448
449     def refreshSelected(self):
450         self.original = map(lambda x:False, self.entries)
451         cats = db.getBookCategories(self.book)
452         cats.sort()
453         cats.sort(key=lambda k: k.get('category')) # key=dict.get(col))
454         i = 0
455         j = 0
456         for cat in self.entries:
457             if i == len(cats):
458                 break
459             if cat['id']==cats[i]['cat_id']:
460                 self.original[j] = True;
461                 i+=1
462             j+=1
463         self.selected = self.original[:]
464
465
466     def addCategory(self):
467         w = curses.newwin(1,1,10,10)
468         cf = categoryForm(w,self.hb)
469         self.centreChild(w)
470         cats = cf.eventLoop()
471         for c in cats:
472             db.addCategory(c)
473         cf.clear()
474
475     def updateCategories(self):
476         # first removed the deselected ones
477         uncats = []
478         cats = []
479         for old, new, category in zip(self.original, self.selected, self.entries):
480             if old and (not new):
481                 uncats.append(category)
482             if (not old) and new:
483                 cats.append(category)
484         db.uncategorizeBook(self.book, uncats)
485         # add the newly selected categories
486         db.categorizeBook(self.book, cats)
487
488
489     def handleInput(self,ch):
490         browserWindow.handleInput(self,ch)
491         if ch==97:
492             self.addCategory()
493             self.refreshCategories()
494             self.refresh()
495         if ch==99:
496             self.updateCategories()
497             return 113
498