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