Renames and making Speichern a new style object
[quicknote] / src / kopfzeile.py
1 #!/usr/bin/env python2.5
2 # -*- coding: utf-8 -*-
3
4 """
5  Copyright (C) 2007 Christoph W├╝rstle
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License version 2 as
9 published by the Free Software Foundation.
10 """
11
12
13 import logging
14
15 import gobject
16 import gtk
17
18
19 try:
20         _
21 except NameError:
22         _ = lambda x: x
23
24
25 _moduleLogger = logging.getLogger("kopfzeile")
26
27
28 class Kopfzeile(gtk.HBox):
29         """
30         Category/Search box
31         """
32
33         __gsignals__ = {
34                 'reload_notes' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, ()),
35         }
36
37         def __init__(self, db):
38                 self._lastCategory = ""
39                 self._db = db
40
41                 gtk.HBox.__init__(self, homogeneous = False, spacing = 3)
42                 _moduleLogger.info("libkopfzeile, init")
43
44                 categoryHBox = gtk.HBox()
45                 self.pack_start(categoryHBox, expand = False, fill = True, padding = 0)
46
47                 label = gtk.Label(_("Category:  "))
48                 categoryHBox.pack_start(label, expand = False, fill = True, padding = 0)
49
50                 self.categoryCombo = gtk.combo_box_entry_new_text()
51                 categoryHBox.pack_start(self.categoryCombo, expand = True, fill = True, padding = 0)
52                 self.load_categories()
53                 self.categoryCombo.connect("changed", self.category_combo_changed, None)
54
55                 searchHBox = gtk.HBox()
56                 self.pack_start(searchHBox, expand = True, fill = True, padding = 0)
57
58                 label = gtk.Label(_("Search:  "))
59                 searchHBox.pack_start(label, expand = False, fill = True, padding = 0)
60
61                 self._searchEntry = gtk.Entry()
62                 searchHBox.pack_start(self._searchEntry, expand = True, fill = True, padding = 0)
63                 self._searchEntry.connect("changed", self.search_entry_changed, None)
64
65         def category_combo_changed(self, widget = None, data = None):
66                 _moduleLogger.debug("comboCategoryChanged")
67                 if self._lastCategory != self.categoryCombo.get_active():
68                         sql = "UPDATE categories SET liste = ? WHERE id = 1"
69                         self._db.speichereSQL(sql, (self.categoryCombo.get_active(), ))
70
71                 self.emit("reload_notes")
72
73         def search_entry_changed(self, widget = None, data = None):
74                 _moduleLogger.debug("search_entry_changed")
75                 self.emit("reload_notes")
76
77         def get_category(self):
78                 entry = self.categoryCombo.get_child()
79                 category = entry.get_text()
80                 if category == _("all"):
81                         category = "%"
82                 if category == "":
83                         category = "undefined"
84                         self.categoryCombo.set_active(1)
85                         self.categoryCombo.show()
86                 return category
87
88         def define_this_category(self):
89                 category = self.get_category()
90
91                 model = self.categoryCombo.get_model()
92                 n = len(self.categoryCombo.get_model())
93                 i = 0
94                 active = -1
95                 cats = []
96                 for i, row in enumerate(model):
97                         if row[0] == category:
98                                 active = i
99                         if row[0] != "%":
100                                 cats.append(row[0])
101
102                 if active == -1 and category != "%":
103                         self.categoryCombo.append_text(category)
104                         sql = "INSERT INTO categories  (id, liste) VALUES (0, ?)"
105                         self._db.speichereSQL(sql, (category, ))
106                         self.categoryCombo.set_active(i)
107
108         def get_search_pattern(self):
109                 return self._searchEntry.get_text()
110
111         def load_categories(self):
112                 sql = "CREATE TABLE categories (id TEXT , liste TEXT)"
113                 self._db.speichereSQL(sql)
114
115                 sql = "SELECT id, liste FROM categories WHERE id = 0 ORDER BY liste"
116                 rows = self._db.ladeSQL(sql)
117                 cats = []
118                 if rows is not None and 0 < len(rows):
119                         for row in rows:
120                                 cats.append(row[1])
121
122                 sql = "SELECT * FROM categories WHERE id = 1"
123                 rows = self._db.ladeSQL(sql)
124                 if rows is None or len(rows) == 0:
125                         sql = "INSERT INTO categories (id, liste) VALUES (1, 1)"
126                         self._db.speichereSQL(sql)
127
128                 #self.categoryCombo.clear()
129                 while 0 < len(self.categoryCombo.get_model()):
130                         self.categoryCombo.remove_text(0)
131
132                 self.categoryCombo.append_text(_('all'))
133                 self.categoryCombo.append_text('undefined')
134
135                 if cats is not None and 0 < len(cats):
136                         for cat in cats:
137                                 self.categoryCombo.append_text(cat)
138
139                 sql = "SELECT * FROM categories WHERE id = 1"
140                 rows = self._db.ladeSQL(sql)
141                 if rows is not None and 0 < len(rows):
142                         self.categoryCombo.set_active(int(rows[0][1]))
143                 else:
144                         self.categoryCombo.set_active(1)
145
146                 self._lastCategory = self.categoryCombo.get_active()