history manager. pep8 pylint

This commit is contained in:
Denis Fomin 2011-03-20 12:15:57 +03:00
parent 2d72cf8bf9
commit bc4b3d0437
1 changed files with 123 additions and 108 deletions

View File

@ -24,7 +24,8 @@
##
## NOTE: some method names may match those of logger.py but that's it
## someday (TM) should have common class that abstracts db connections and helpers on it
## someday (TM) should have common class
## that abstracts db connections and helpers on it
## the same can be said for history_window.py
import os
@ -108,7 +109,8 @@ import sqlite3 as sqlite
class HistoryManager:
def __init__(self):
pix = gtkgui_helpers.get_icon_pixmap('gajim')
gtk.window_set_default_icon(pix) # set the icon to all newly opened windows
# set the icon to all newly opened windows
gtk.window_set_default_icon(pix)
if not os.path.exists(LOG_DB_PATH):
dialogs.ErrorDialog(_('Cannot find history logs database'),
@ -126,11 +128,11 @@ class HistoryManager:
'search_results_scrolledwindow')
self.welcome_vbox = xml.get_object('welcome_vbox')
self.jids_already_in = [] # holds jids that we already have in DB
self.jids_already_in = [] # holds jids that we already have in DB
self.AT_LEAST_ONE_DELETION_DONE = False
self.con = sqlite.connect(LOG_DB_PATH, timeout = 20.0,
isolation_level = 'IMMEDIATE')
self.con = sqlite.connect(LOG_DB_PATH, timeout=20.0,
isolation_level='IMMEDIATE')
self.cur = self.con.cursor()
self._init_jids_listview()
@ -146,47 +148,50 @@ class HistoryManager:
xml.connect_signals(self)
def _init_jids_listview(self):
self.jids_liststore = gtk.ListStore(str, str) # jid, jid_id
self.jids_liststore = gtk.ListStore(str, str) # jid, jid_id
self.jids_listview.set_model(self.jids_liststore)
self.jids_listview.get_selection().set_mode(gtk.SELECTION_MULTIPLE)
renderer_text = gtk.CellRendererText() # holds jid
col = gtk.TreeViewColumn(_('Contacts'), renderer_text, text = 0)
renderer_text = gtk.CellRendererText() # holds jid
col = gtk.TreeViewColumn(_('Contacts'), renderer_text, text=0)
self.jids_listview.append_column(col)
self.jids_listview.get_selection().connect('changed',
self.on_jids_listview_selection_changed)
def _init_logs_listview(self):
# log_line_id (HIDDEN), jid_id (HIDDEN), time, message, subject, nickname
# log_line_id(HIDDEN), jid_id(HIDDEN), time, message, subject, nickname
self.logs_liststore = gtk.ListStore(str, str, str, str, str, str)
self.logs_listview.set_model(self.logs_liststore)
self.logs_listview.get_selection().set_mode(gtk.SELECTION_MULTIPLE)
renderer_text = gtk.CellRendererText() # holds time
col = gtk.TreeViewColumn(_('Date'), renderer_text, text = C_UNIXTIME)
col.set_sort_column_id(C_UNIXTIME) # user can click this header and sort
renderer_text = gtk.CellRendererText() # holds time
col = gtk.TreeViewColumn(_('Date'), renderer_text, text=C_UNIXTIME)
# user can click this header and sort
col.set_sort_column_id(C_UNIXTIME)
col.set_resizable(True)
self.logs_listview.append_column(col)
renderer_text = gtk.CellRendererText() # holds nickname
col = gtk.TreeViewColumn(_('Nickname'), renderer_text, text = C_NICKNAME)
col.set_sort_column_id(C_NICKNAME) # user can click this header and sort
renderer_text = gtk.CellRendererText() # holds nickname
col = gtk.TreeViewColumn(_('Nickname'), renderer_text, text=C_NICKNAME)
# user can click this header and sort
col.set_sort_column_id(C_NICKNAME)
col.set_resizable(True)
col.set_visible(False)
self.nickname_col_for_logs = col
self.logs_listview.append_column(col)
renderer_text = gtk.CellRendererText() # holds message
col = gtk.TreeViewColumn(_('Message'), renderer_text, markup = C_MESSAGE)
col.set_sort_column_id(C_MESSAGE) # user can click this header and sort
renderer_text = gtk.CellRendererText() # holds message
col = gtk.TreeViewColumn(_('Message'), renderer_text, markup=C_MESSAGE)
# user can click this header and sort
col.set_sort_column_id(C_MESSAGE)
col.set_resizable(True)
self.message_col_for_logs = col
self.logs_listview.append_column(col)
renderer_text = gtk.CellRendererText() # holds subject
col = gtk.TreeViewColumn(_('Subject'), renderer_text, text = C_SUBJECT)
col.set_sort_column_id(C_SUBJECT) # user can click this header and sort
renderer_text = gtk.CellRendererText() # holds subject
col = gtk.TreeViewColumn(_('Subject'), renderer_text, text=C_SUBJECT)
col.set_sort_column_id(C_SUBJECT) # user can click this header and sort
col.set_resizable(True)
col.set_visible(False)
self.subject_col_for_logs = col
@ -194,92 +199,98 @@ class HistoryManager:
def _init_search_results_listview(self):
# log_line_id (HIDDEN), jid, time, message, subject, nickname
self.search_results_liststore = gtk.ListStore(str, str, str, str, str, str)
self.search_results_liststore = gtk.ListStore(str, str, str, str, str,
str)
self.search_results_listview.set_model(self.search_results_liststore)
renderer_text = gtk.CellRendererText() # holds JID (who said this)
col = gtk.TreeViewColumn(_('JID'), renderer_text, text = 1)
col.set_sort_column_id(1) # user can click this header and sort
renderer_text = gtk.CellRendererText() # holds JID (who said this)
col = gtk.TreeViewColumn(_('JID'), renderer_text, text=1)
col.set_sort_column_id(1) # user can click this header and sort
col.set_resizable(True)
self.search_results_listview.append_column(col)
renderer_text = gtk.CellRendererText() # holds time
col = gtk.TreeViewColumn(_('Date'), renderer_text, text = C_UNIXTIME)
col.set_sort_column_id(C_UNIXTIME) # user can click this header and sort
renderer_text = gtk.CellRendererText() # holds time
col = gtk.TreeViewColumn(_('Date'), renderer_text, text=C_UNIXTIME)
# user can click this header and sort
col.set_sort_column_id(C_UNIXTIME)
col.set_resizable(True)
self.search_results_listview.append_column(col)
renderer_text = gtk.CellRendererText() # holds message
col = gtk.TreeViewColumn(_('Message'), renderer_text, text = C_MESSAGE)
col.set_sort_column_id(C_MESSAGE) # user can click this header and sort
renderer_text = gtk.CellRendererText() # holds message
col = gtk.TreeViewColumn(_('Message'), renderer_text, text=C_MESSAGE)
col.set_sort_column_id(C_MESSAGE) # user can click this header and sort
col.set_resizable(True)
self.search_results_listview.append_column(col)
renderer_text = gtk.CellRendererText() # holds subject
col = gtk.TreeViewColumn(_('Subject'), renderer_text, text = C_SUBJECT)
col.set_sort_column_id(C_SUBJECT) # user can click this header and sort
renderer_text = gtk.CellRendererText() # holds subject
col = gtk.TreeViewColumn(_('Subject'), renderer_text, text=C_SUBJECT)
col.set_sort_column_id(C_SUBJECT) # user can click this header and sort
col.set_resizable(True)
self.search_results_listview.append_column(col)
renderer_text = gtk.CellRendererText() # holds nickname
col = gtk.TreeViewColumn(_('Nickname'), renderer_text, text = C_NICKNAME)
col.set_sort_column_id(C_NICKNAME) # user can click this header and sort
renderer_text = gtk.CellRendererText() # holds nickname
col = gtk.TreeViewColumn(_('Nickname'), renderer_text, text=C_NICKNAME)
# user can click this header and sort
col.set_sort_column_id(C_NICKNAME)
col.set_resizable(True)
self.search_results_listview.append_column(col)
def on_history_manager_window_delete_event(self, widget, event):
if self.AT_LEAST_ONE_DELETION_DONE:
def on_yes(clicked):
self.cur.execute('VACUUM')
self.con.commit()
gtk.main_quit()
def on_no():
gtk.main_quit()
dialogs.YesNoDialog(
_('Do you want to clean up the database? '
'(STRONGLY NOT RECOMMENDED IF GAJIM IS RUNNING)'),
_('Normally allocated database size will not be freed, '
'it will just become reusable. If you really want to reduce '
'database filesize, click YES, else click NO.'
'\n\nIn case you click YES, please wait...'),
on_response_yes=on_yes, on_response_no=on_no)
if not self.AT_LEAST_ONE_DELETION_DONE:
gtk.main_quit()
return
gtk.main_quit()
def on_yes(clicked):
self.cur.execute('VACUUM')
self.con.commit()
gtk.main_quit()
def on_no():
gtk.main_quit()
dialog = dialogs.YesNoDialog(
_('Do you want to clean up the database? '
'(STRONGLY NOT RECOMMENDED IF GAJIM IS RUNNING)'),
_('Normally allocated database size will not be freed, '
'it will just become reusable. If you really want to reduce '
'database filesize, click YES, else click NO.'
'\n\nIn case you click YES, please wait...'),
on_response_yes=on_yes, on_response_no=on_no)
button_box = dialog.get_children()[0].get_children()[1]
button_box.get_children()[0].grab_focus()
def _fill_jids_listview(self):
# get those jids that have at least one entry in logs
self.cur.execute('SELECT jid, jid_id FROM jids WHERE jid_id IN (SELECT '
'distinct logs.jid_id FROM logs) ORDER BY jid')
rows = self.cur.fetchall() # list of tupples: [(u'aaa@bbb',), (u'cc@dd',)]
self.cur.execute('SELECT jid, jid_id FROM jids WHERE jid_id IN ('
'SELECT distinct logs.jid_id FROM logs) ORDER BY jid')
# list of tupples: [(u'aaa@bbb',), (u'cc@dd',)]
rows = self.cur.fetchall()
for row in rows:
self.jids_already_in.append(row[0]) # jid
self.jids_liststore.append(row) # jid, jid_id
self.jids_already_in.append(row[0]) # jid
self.jids_liststore.append(row) # jid, jid_id
def on_jids_listview_selection_changed(self, widget, data = None):
def on_jids_listview_selection_changed(self, widget, data=None):
liststore, list_of_paths = self.jids_listview.get_selection()\
.get_selected_rows()
paths_len = len(list_of_paths)
if paths_len == 0: # nothing is selected
if paths_len == 0: # nothing is selected
return
self.logs_liststore.clear() # clear the store
self.logs_liststore.clear() # clear the store
self.welcome_vbox.hide()
self.search_results_scrolledwindow.hide()
self.logs_scrolledwindow.show()
list_of_rowrefs = []
for path in list_of_paths: # make them treerowrefs (it's needed)
for path in list_of_paths: # make them treerowrefs (it's needed)
list_of_rowrefs.append(gtk.TreeRowReference(liststore, path))
for rowref in list_of_rowrefs: # FILL THE STORE, for all rows selected
for rowref in list_of_rowrefs: # FILL THE STORE, for all rows selected
path = rowref.get_path()
if path is None:
continue
jid = liststore[path][0].decode('utf-8') # jid
jid = liststore[path][0].decode('utf-8') # jid
self._fill_logs_listview(jid)
def _get_jid_id(self, jid):
@ -290,10 +301,10 @@ class HistoryManager:
So to ask logs we need jid_id that matches our jid in jids table this
method wants jid and returns the jid_id for later sql-ing on logs
"""
if jid.find('/') != -1: # if it has a /
if jid.find('/') != -1: # if it has a /
jid_is_from_pm = self._jid_is_from_pm(jid)
if not jid_is_from_pm: # it's normal jid with resource
jid = jid.split('/', 1)[0] # remove the resource
if not jid_is_from_pm: # it's normal jid with resource
jid = jid.split('/', 1)[0] # remove the resource
self.cur.execute('SELECT jid_id FROM jids WHERE jid = ?', (jid,))
jid_id = self.cur.fetchone()[0]
return str(jid_id)
@ -337,7 +348,7 @@ class HistoryManager:
raise
elif row[0] == constants.JID_ROOM_TYPE:
return True
else: # normal type
else: # normal type
return False
def _fill_logs_listview(self, jid):
@ -357,7 +368,7 @@ class HistoryManager:
results = self.cur.fetchall()
if self._jid_is_room_type(jid): # is it room?
if self._jid_is_room_type(jid): # is it room?
self.nickname_col_for_logs.set_visible(True)
self.subject_col_for_logs.set_visible(False)
else:
@ -369,10 +380,11 @@ class HistoryManager:
# time, message, subject, nickname
# but store in liststore
# log_line_id, jid_id, time, message, subject, nickname
log_line_id, jid_id, time_, kind, message, subject, nickname, show = row
log_line_id, jid_id, time_, kind, message, subject, nickname, \
show = row
try:
time_ = time.strftime('%x', time.localtime(float(time_))).decode(
locale.getpreferredencoding())
time_ = time.strftime('%x', time.localtime(float(time_))
).decode(locale.getpreferredencoding())
except ValueError:
pass
else:
@ -380,27 +392,29 @@ class HistoryManager:
if kind in (constants.KIND_SINGLE_MSG_RECV,
constants.KIND_CHAT_MSG_RECV, constants.KIND_GC_MSG):
# it is the other side
color = gajim.config.get('inmsgcolor') # so incoming color
color = gajim.config.get('inmsgcolor') # so incoming color
elif kind in (constants.KIND_SINGLE_MSG_SENT,
constants.KIND_CHAT_MSG_SENT): # it is us
color = gajim.config.get('outmsgcolor') # so outgoing color
constants.KIND_CHAT_MSG_SENT): # it is us
color = gajim.config.get('outmsgcolor') # so outgoing color
elif kind in (constants.KIND_STATUS,
constants.KIND_GCSTATUS): # is is statuses
color = gajim.config.get('statusmsgcolor') # so status color
constants.KIND_GCSTATUS): # is is statuses
# so status color
color = gajim.config.get('statusmsgcolor')
# include status into (status) message
if message is None:
message = ''
else:
message = ' : ' + message
message = helpers.get_uf_show(gajim.SHOW_LIST[show]) + message
message = helpers.get_uf_show(gajim.SHOW_LIST[show]) + \
message
message_ = '<span'
if color:
message_ += ' foreground="%s"' % color
message_ += '>%s</span>' % \
gobject.markup_escape_text(message)
self.logs_liststore.append((log_line_id, jid_id, time_, message_,
subject, nickname))
self.logs_liststore.append((log_line_id, jid_id, time_,
message_, subject, nickname))
def _fill_search_results_listview(self, text):
"""
@ -423,8 +437,8 @@ class HistoryManager:
# log_line_id, jid (from jid_id), time, message, subject, nickname
log_line_id, jid_id, time_, message, subject, nickname = row
try:
time_ = time.strftime('%x', time.localtime(float(time_))).decode(
locale.getpreferredencoding())
time_ = time.strftime('%x', time.localtime(float(time_))
).decode(locale.getpreferredencoding())
except ValueError:
pass
else:
@ -440,8 +454,9 @@ class HistoryManager:
self._delete_logs(liststore, list_of_paths)
def on_listview_button_press_event(self, widget, event):
if event.button == 3: # right click
xml = gtkgui_helpers.get_gtk_builder('history_manager.ui', 'context_menu')
if event.button == 3: # right click
xml = gtkgui_helpers.get_gtk_builder('history_manager.ui',
'context_menu')
if widget.name != 'jids_listview':
xml.get_object('export_menuitem').hide()
xml.get_object('delete_menuitem').connect('activate',
@ -453,7 +468,8 @@ class HistoryManager:
return True
def on_export_menuitem_activate(self, widget):
xml = gtkgui_helpers.get_gtk_builder('history_manager.ui', 'filechooserdialog')
xml = gtkgui_helpers.get_gtk_builder('history_manager.ui',
'filechooserdialog')
xml.connect_signals(self)
dlg = xml.get_object('filechooserdialog')
@ -462,11 +478,12 @@ class HistoryManager:
dlg.props.do_overwrite_confirmation = True
response = dlg.run()
if response == gtk.RESPONSE_OK: # user want us to export ;)
if response == gtk.RESPONSE_OK: # user want us to export ;)
liststore, list_of_paths = self.jids_listview.get_selection()\
.get_selected_rows()
path_to_file = dlg.get_filename()
self._export_jids_logs_to_file(liststore, list_of_paths, path_to_file)
self._export_jids_logs_to_file(liststore, list_of_paths,
path_to_file)
dlg.destroy()
@ -477,7 +494,7 @@ class HistoryManager:
self._delete_jid_logs(liststore, list_of_paths)
elif widget_name in ('logs_listview', 'search_results_listview'):
self._delete_logs(liststore, list_of_paths)
else: # Huh ? We don't know this widget
else: # Huh ? We don't know this widget
return
def on_jids_listview_key_press_event(self, widget, event):
@ -488,11 +505,11 @@ class HistoryManager:
def _export_jids_logs_to_file(self, liststore, list_of_paths, path_to_file):
paths_len = len(list_of_paths)
if paths_len == 0: # nothing is selected
if paths_len == 0: # nothing is selected
return
list_of_rowrefs = []
for path in list_of_paths: # make them treerowrefs (it's needed)
for path in list_of_paths: # make them treerowrefs (it's needed)
list_of_rowrefs.append(gtk.TreeRowReference(liststore, path))
for rowref in list_of_rowrefs:
@ -523,28 +540,28 @@ class HistoryManager:
who = _('You')
elif kind == constants.KIND_GC_MSG:
who = nickname
else: # status or gc_status. do not save
else: # status or gc_status. do not save
#print kind
continue
try:
time_ = time.strftime('%c', time.localtime(float(time_))).decode(
locale.getpreferredencoding())
time_ = time.strftime('%c', time.localtime(float(time_))
).decode(locale.getpreferredencoding())
except ValueError:
pass
file_.write(_('%(who)s on %(time)s said: %(message)s\n') % {'who': who,
'time': time_, 'message': message})
file_.write(_('%(who)s on %(time)s said: %(message)s\n') % {
'who': who, 'time': time_, 'message': message})
def _delete_jid_logs(self, liststore, list_of_paths):
paths_len = len(list_of_paths)
if paths_len == 0: # nothing is selected
if paths_len == 0: # nothing is selected
return
def on_ok(liststore, list_of_paths):
# delete all rows from db that match jid_id
list_of_rowrefs = []
for path in list_of_paths: # make them treerowrefs (it's needed)
for path in list_of_paths: # make them treerowrefs (it's needed)
list_of_rowrefs.append(gtk.TreeRowReference(liststore, path))
for rowref in list_of_rowrefs:
@ -552,7 +569,7 @@ class HistoryManager:
if path is None:
continue
jid_id = liststore[path][1]
del liststore[path] # remove from UI
del liststore[path] # remove from UI
# remove from db
self.cur.execute('''
DELETE FROM logs
@ -574,7 +591,7 @@ class HistoryManager:
'Do you really want to delete logs of the selected contacts?',
paths_len)
dialog = dialogs.ConfirmationDialog(pri_text,
_('This is an irreversible operation.'), on_response_ok = (on_ok,
_('This is an irreversible operation.'), on_response_ok=(on_ok,
liststore, list_of_paths))
ok_button = dialog.get_children()[0].get_children()[1].get_children()[0]
ok_button.grab_focus()
@ -582,13 +599,13 @@ class HistoryManager:
def _delete_logs(self, liststore, list_of_paths):
paths_len = len(list_of_paths)
if paths_len == 0: # nothing is selected
if paths_len == 0: # nothing is selected
return
def on_ok(liststore, list_of_paths):
# delete rows from db that match log_line_id
list_of_rowrefs = []
for path in list_of_paths: # make them treerowrefs (it's needed)
for path in list_of_paths: # make them treerowrefs (it's needed)
list_of_rowrefs.append(gtk.TreeRowReference(liststore, path))
for rowref in list_of_rowrefs:
@ -596,7 +613,7 @@ class HistoryManager:
if path is None:
continue
log_line_id = liststore[path][0]
del liststore[path] # remove from UI
del liststore[path] # remove from UI
# remove from db
self.cur.execute('''
DELETE FROM logs
@ -607,12 +624,11 @@ class HistoryManager:
self.AT_LEAST_ONE_DELETION_DONE = True
pri_text = i18n.ngettext(
'Do you really want to delete the selected message?',
'Do you really want to delete the selected messages?', paths_len)
dialog = dialogs.ConfirmationDialog(pri_text,
_('This is an irreversible operation.'), on_response_ok = (on_ok,
_('This is an irreversible operation.'), on_response_ok=(on_ok,
liststore, list_of_paths))
ok_button = dialog.get_children()[0].get_children()[1].get_children()[0]
ok_button.grab_focus()
@ -637,7 +653,6 @@ class HistoryManager:
# as this is what db returns so I don't have to fight with types
jid_id = self._get_jid_id(jid)
iter_ = self.jids_liststore.get_iter_root()
while iter_:
# self.jids_liststore[iter_][1] holds jid_ids
@ -662,6 +677,6 @@ class HistoryManager:
self.logs_listview.scroll_to_cell(path)
if __name__ == '__main__':
signal.signal(signal.SIGINT, signal.SIG_DFL) # ^C exits the application
signal.signal(signal.SIGINT, signal.SIG_DFL) # ^C exits the application
HistoryManager()
gtk.main()