Slight modifications to the sql dialog
[quicknote] / src / libsqldialog.py
1 #!/usr/bin/env python
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 time
14 import logging
15
16 import gtk
17
18
19 try:
20         _
21 except NameError:
22         _ = lambda x: x
23
24
25 class SqlDialog(gtk.Dialog):
26
27         EXPORT_RESPONSE = 444
28
29         def __init__(self, db):
30                 self.db = db
31
32                 logging.info("sqldialog, init")
33
34                 gtk.Dialog.__init__(self, _("SQL History (the past two days):"), None, gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT)
35
36                 self.add_button(_("Export"), self.EXPORT_RESPONSE)
37                 self.add_button(gtk.STOCK_OK, gtk.RESPONSE_ACCEPT)
38                 self.set_position(gtk.WIN_POS_CENTER)
39
40                 self.liststore = gtk.ListStore(str, str, str)
41
42                 # create the TreeView using liststore
43                 self.treeview = gtk.TreeView(self.liststore)
44                 self.treeview.set_rules_hint(True)
45
46                 # create a CellRenderers to render the data
47                 self.cell1 = gtk.CellRendererText()
48                 self.cell2 = gtk.CellRendererText()
49                 self.cell3 = gtk.CellRendererText()
50
51                 # create the TreeViewColumns to display the data
52                 self.tvcolumn1 = gtk.TreeViewColumn(_('Timestamp'))
53                 self.tvcolumn2 = gtk.TreeViewColumn('SQL')
54                 self.tvcolumn3 = gtk.TreeViewColumn(_('Parameter'))
55
56                 # add columns to treeview
57                 self.treeview.append_column(self.tvcolumn1)
58                 self.treeview.append_column(self.tvcolumn2)
59                 self.treeview.append_column(self.tvcolumn3)
60
61
62                 self.tvcolumn1.pack_start(self.cell1, True)
63                 self.tvcolumn2.pack_start(self.cell2, True)
64                 self.tvcolumn3.pack_start(self.cell3, True)
65
66                 self.tvcolumn1.set_attributes(self.cell1, text = 0) #Spalten setzten hier!!!!
67                 self.tvcolumn2.set_attributes(self.cell2, text = 1)
68                 self.tvcolumn3.set_attributes(self.cell3, text = 2)
69
70                 # Allow NOT drag and drop reordering of rows
71                 self.treeview.set_reorderable(False)
72
73                 scrolled_window = gtk.ScrolledWindow()
74                 scrolled_window.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
75                 scrolled_window.add(self.treeview)
76
77                 self.vbox.pack_start(scrolled_window, True, True, 0)
78
79                 self.vbox.show_all()
80
81                 msgstring = ""
82                 sql = "SELECT pcdatum, sql, param FROM logtable WHERE pcdatum>? ORDER BY pcdatum DESC"
83                 rows = db.ladeSQL(sql, (time.time()-3*24*3600, ))
84                 for row in rows:
85                         pcdatum, sql, param = row
86                         datum = str(time.strftime(_("%d.%m.%y %H:%M:%S "), (time.localtime(pcdatum))))
87                         self.liststore.append([datum, sql, param])
88
89                 self.set_size_request(500, 400)
90
91         def exportSQL(self, filename):
92                 f = open(filename, 'w')
93                 try:
94                         msgstring = ""
95                         sql = "SELECT pcdatum, sql, param FROM logtable WHERE pcdatum>? ORDER BY pcdatum DESC"
96                         rows = self.db.ladeSQL(sql, (time.time()-2*24*3600, ))
97                         for row in rows:
98                                 pcdatum, sql, param = row
99                                 datum = str(time.strftime("%d.%m.%y %H:%M:%S ", (time.localtime(pcdatum))))
100                                 f.write( datum +"\t" + sql + "\t\t" + param+ "\n")
101                 finally:
102                         f.close()