From d17289dfbd4b5944000d3bf70a72cb05bd6f2a76 Mon Sep 17 00:00:00 2001 From: Yann Leboulanger Date: Fri, 15 Apr 2005 22:02:13 +0000 Subject: [PATCH] we can now view history_windows --- src/common/gajim.py | 2 + src/common/logger.py | 226 ++++++++++++++---------------------------- src/history_window.py | 76 +++++++------- 3 files changed, 116 insertions(+), 188 deletions(-) diff --git a/src/common/gajim.py b/src/common/gajim.py index 018067b0b..76ada3789 100644 --- a/src/common/gajim.py +++ b/src/common/gajim.py @@ -18,8 +18,10 @@ import logging import common.config +import common.logger version = "0.7" config = common.config.Config() connections = {} log = logging.getLogger('Gajim') +logger = common.logger.Logger() diff --git a/src/common/logger.py b/src/common/logger.py index 9ebcb7257..9d29269b2 100644 --- a/src/common/logger.py +++ b/src/common/logger.py @@ -16,169 +16,87 @@ ## GNU General Public License for more details. ## -def usage(): - #TODO: use i18n - print "usage :", sys.argv[0], ' [OPTION]' - print " -p\tport on whitch the sock plugin listen" - print " -h, --help\tdisplay this help and exit" - -if __name__ == "__main__": - import getopt, sys, pickle, socket - try: - opts, args = getopt.getopt(sys.argv[1:], "p:h", ["help"]) - except getopt.GetoptError: - # print help information and exit: - usage() - sys.exit(2) - port = 8255 - for o, a in opts: - if o == '-p': - port = a - if o in ("-h", "--help"): - usage() - sys.exit() - sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - try: - sock.connect(('', 8255)) - except: - #TODO: use i18n - print "unable to connect to localhost on port "+str(port) - else: - evp = pickle.dumps(('EXEC_PLUGIN', '', 'logger')) - sock.send('<'+evp+'>') - sock.close() - sys.exit() - import os import time -import common.optparser +import common.gajim from common import i18n -LOGPATH = os.path.expanduser("~/.gajim/logs/") +LOGPATH = os.path.expanduser('~/.gajim/logs/') _ = i18n._ -class plugin: - def read_queue(self): - while 1: - while self.queueIN.empty() == 0: - if self.config.has_key('lognotsep'): - lognotsep = self.config['lognotsep'] - else: - lognotsep = 1 - if self.config.has_key('lognotusr'): - lognotusr = self.config['lognotusr'] - else: - lognotusr = 1 - tim = time.time() - - ev = self.queueIN.get() - if ev[0] == 'QUIT': - print _("plugin logger stopped") - return - elif ev[0] == 'NOTIFY': - jid = ev[2][0].split('/')[0] - if jid in self.no_log_for[ev[1]]: - break - if ev[1] in self.no_log_for[ev[1]]: - break - status = ev[2][2] - if not status: - status = "" - status = status.replace('\n', '\\n') - if lognotsep == 1: - fic = open(LOGPATH + "notify.log", "a") - fic.write("%s:%s:%s:%s\n" % (tim, ev[2][0] + '/' + ev[2][3], \ - ev[2][1], status)) - fic.close() - if lognotusr == 1: - fic = open(LOGPATH + jid, "a") - fic.write("%s:%s:%s:%s\n" % (tim, ev[2][0] + '/' + ev[2][3], \ - ev[2][1], status)) - fic.close() - elif ev[0] == 'MSG': - jid = ev[2][0].split('/')[0] - if jid in self.no_log_for[ev[1]]: - break - if ev[1] in self.no_log_for[ev[1]]: - break - if not ev[2][1]: - msg = '' - else: - msg = ev[2][1].replace('\n', '\\n') - fic = open(LOGPATH + jid, "a") - t = time.mktime(ev[2][2]) - fic.write("%s:recv:%s\n" % (t, msg)) - fic.close() - elif ev[0] == 'MSGSENT': - jid = ev[2][0].split('/')[0] - if jid in self.no_log_for[ev[1]]: - break - if ev[1] in self.no_log_for[ev[1]]: - break - msg = ev[2][1].replace('\n', '\\n') - fic = open(LOGPATH + jid, "a") - fic.write("%s:sent:%s\n" % (tim, msg)) - fic.close() - elif ev[0] == 'GC_MSG': - msg = ev[2][1].replace('\n', '\\n') - jids = ev[2][0].split('/') - jid = jids[0] - nick = '' - if len(jids) > 1: - nick = ev[2][0].split('/')[1] - fic = open(LOGPATH + jid, "a") - t = time.mktime(ev[2][2]) - fic.write("%s:recv:%s:%s\n" % (t, nick, msg)) - fic.close() - elif ev[0] == 'CONFIG': - if ev[2][0] == 'Logger': - self.config = ev[2][1] - if ev[2][0] == 'accounts': - accounts = ev[2][1] - self.no_log_for = {} - for acct in accounts.keys(): - self.no_log_for[acct] = [] - if accounts[acct].has_key('no_log_for'): - self.no_log_for[acct] = \ - accounts[acct]['no_log_for'].split() - time.sleep(0.1) - - def wait(self, what): - """Wait for a message from Core""" - #TODO: timeout, save messages that don't fit - while 1: - if not self.queueIN.empty(): - ev = self.queueIN.get() - if ev[0] == what and ev[2][0] == 'Logger': - return ev[2][1] - time.sleep(0.1) - - def __init__(self, quIN, quOUT): - self.queueIN = quIN - self.queueOUT = quOUT - quOUT.put(('REG_MESSAGE', 'logger', ['CONFIG', 'NOTIFY', 'MSG', \ - 'MSGSENT', 'GC_MSG', 'QUIT'])) - quOUT.put(('ASK_CONFIG', None, ('Logger', 'Logger', {\ - 'lognotsep':1, 'lognotusr':1}))) - self.config = self.wait('CONFIG') - quOUT.put(('ASK_CONFIG', None, ('Logger', 'accounts'))) - accounts = self.wait('CONFIG') - self.no_log_for = {} - for acct in accounts.keys(): - self.no_log_for[acct] = [] - if accounts[acct].has_key('no_log_for'): - self.no_log_for[acct] = \ - accounts[acct]['no_log_for'].split() +class Logger: + def __init__(self): #create ~/.gajim/logs/ if it doesn't exist try: - os.stat(os.path.expanduser("~/.gajim")) + os.stat(os.path.expanduser('~/.gajim')) except OSError: - os.mkdir(os.path.expanduser("~/.gajim")) - print _("creating ~/.gajim/") + os.mkdir(os.path.expanduser('~/.gajim')) + print _('creating ~/.gajim/') try: os.stat(LOGPATH) except OSError: os.mkdir(LOGPATH) - print _("creating ~/.gajim/logs/") - self.read_queue() - -print _("plugin logger loaded") + print _('creating ~/.gajim/logs/') + + def write(self, kind, msg, jid, show = None, tim = None): + if not tim: + tim = time.time() + else: + tim = time.mktime(tim) + + if not msg: + msg = '' + msg = msg.replace('\n', '\\n') + ji = jid.split('/')[0] + files = [] + if kind == 'status': #we save time:jid:show:msg + if not show: + show = 'online' + if common.gajim.config.get('lognotusr'): + files.append(ji) + if common.gajim.config.get('lognotsep'): + files.append('notify.log') + elif kind == 'incoming': # we save time:recv:message: + files.append(ji) + jid = 'recv' + show = msg + msg = '' + elif kind == 'outgoing': # we save time:sent:message: + files.append(ji) + jid = 'sent' + show = msg + msg = '' + elif kind == 'gc': + files.append(ji) + jid = 'recv' + jids = jid.split('/') + nick = '' + if len(jids) > 1: + nick = jids[1] + show = nick + for f in files: + fic = open(LOGPATH + f, 'a') + fic.write('%s:%s:%s:%s\n' % (tim, jid, show, msg)) + fic.close() + + def get_nb_line(self, jid): + fic = open(LOGPATH + jid.split('/')[0], 'r') + nb = 0 + while (fic.readline()): + nb += 1 + fic.close() + return nb + + def read(self, jid, begin_line, end_line): + fic = open(LOGPATH + jid.split('/')[0], 'r') + nb = 0 + lines = [] + while (nb < begin_line and fic.readline()): + nb += 1 + while nb < end_line: + line = fic.readline() + if line: + lineSplited = line.split(':') + if len(lineSplited) > 2: + lines.append(lineSplited) + nb += 1 + return nb, lines diff --git a/src/history_window.py b/src/history_window.py index 7ef26144c..b5970b5ce 100644 --- a/src/history_window.py +++ b/src/history_window.py @@ -52,9 +52,11 @@ class History_window: end = 50 if end > self.nb_line: end = self.nb_line - #FIXME: -# self.plugin.send('LOG_GET_RANGE', None, (self.jid, 0, end)) - self.num_begin = self.nb_line + nb, lines = gajim.logger.read(self.jid, 0, end) + self.set_buttons_sensitivity(nb) + for line in lines: + self.new_line(line[0], line[1], line[2:]) + self.num_begin = 0 def on_previous_button_clicked(self, widget): start, end = self.history_buffer.get_bounds() @@ -69,9 +71,11 @@ class History_window: end = begin + 50 if end > self.nb_line: end = self.nb_line - #FIXME: -# self.plugin.send('LOG_GET_RANGE', None, (self.jid, begin, end)) - self.num_begin = self.nb_line + nb, lines = gajim.logger.read(self.jid, begin, end) + self.set_buttons_sensitivity(nb) + for line in lines: + self.new_line(line[0], line[1], line[2:]) + self.num_begin = begin def on_forward_button_clicked(self, widget): start, end = self.history_buffer.get_bounds() @@ -86,9 +90,11 @@ class History_window: end = begin + 50 if end > self.nb_line: end = self.nb_line - #FIXME: -# self.plugin.send('LOG_GET_RANGE', None, (self.jid, begin, end)) - self.num_begin = self.nb_line + nb, lines = gajim.logger.read(self.jid, begin, end) + self.set_buttons_sensitivity(nb) + for line in lines: + self.new_line(line[0], line[1], line[2:]) + self.num_begin = begin def on_latest_button_clicked(self, widget): start, end = self.history_buffer.get_bounds() @@ -100,50 +106,46 @@ class History_window: begin = self.nb_line - 50 if begin < 0: begin = 0 - #FIXME: -# self.plugin.send('LOG_GET_RANGE', None, (self.jid, begin, self.nb_line)) - self.num_begin = self.nb_line + nb, lines = gajim.logger.read(self.jid, begin, self.nb_line) + self.set_buttons_sensitivity(nb) + for line in lines: + self.new_line(line[0], line[1], line[2:]) + self.num_begin = begin - def new_line(self, infos): - """write a new line""" - #infos = [num_line, date, type, data] - if infos[0] < self.num_begin: - self.num_begin = infos[0] - if infos[0] == 50: + def set_buttons_sensitivity(self, nb): + if nb == 50: self.earliest_button.set_sensitive(False) self.previous_button.set_sensitive(False) - if infos[0] == self.nb_line: + if nb == self.nb_line: self.forward_button.set_sensitive(False) self.latest_button.set_sensitive(False) + + def new_line(self, date, type, data): + """write a new line""" start_iter = self.history_buffer.get_start_iter() end_iter = self.history_buffer.get_end_iter() - tim = time.strftime("[%x %X] ", time.localtime(float(infos[1]))) + tim = time.strftime('[%x %X] ', time.localtime(float(date))) self.history_buffer.insert(start_iter, tim) - if infos[2] == 'recv': - msg = ':'.join(infos[3][0:]) + if type == 'recv': + msg = ':'.join(data[0:]) msg = msg.replace('\\n', '\n') self.history_buffer.insert_with_tags_by_name(start_iter, msg, \ 'incoming') - elif infos[2] == 'sent': - msg = ':'.join(infos[3][0:]) + elif type == 'sent': + msg = ':'.join(data[0:]) msg = msg.replace('\\n', '\n') self.history_buffer.insert_with_tags_by_name(start_iter, msg, \ 'outgoing') else: - msg = ':'.join(infos[3][1:]) + msg = ':'.join(data[1:]) msg = msg.replace('\\n', '\n') self.history_buffer.insert_with_tags_by_name(start_iter, \ - _('Status is now: ') + infos[3][0]+': ' + msg, 'status') + _('Status is now: ') + data[0]+': ' + msg, 'status') - def set_nb_line(self, nb_line): - self.nb_line = nb_line - self.num_begin = nb_line - def __init__(self, plugin, jid): self.plugin = plugin self.jid = jid - self.nb_line = 0 - self.num_begin = 0 + self.nb_line = gajim.logger.get_nb_line(jid) xml = gtk.glade.XML(GTKGUI_GLADE, 'history_window', APP) self.window = xml.get_widget('history_window') self.history_buffer = xml.get_widget('history_textview').get_buffer() @@ -161,6 +163,12 @@ class History_window: tagStatus = self.history_buffer.create_tag('status') color = gajim.config.get('statusmsgcolor') tagStatus.set_property('foreground', color) + begin = 0 + if self.nb_line > 50: + begin = self.nb_line - 50 + nb, lines = gajim.logger.read(self.jid, begin, self.nb_line) + self.set_buttons_sensitivity(nb) + for line in lines: + self.new_line(line[0], line[1], line[2:]) + self.num_begin = begin self.window.show_all() - #FIXME: -# self.plugin.send('LOG_NB_LINE', None, jid)