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