[roger]Merge metacontacts logs on the fly, fix #1891

[jim]Clean patch, move duplicate code.
Added condition missing in [5779], can you confirm Yann ?
This commit is contained in:
Jean-Marie Traissard 2006-06-04 09:54:11 +00:00
parent 5a78e29465
commit 001b1c0f20
2 changed files with 53 additions and 22 deletions

View file

@ -30,6 +30,8 @@ import datetime
import exceptions import exceptions
import i18n import i18n
import gajim
_ = i18n._ _ = i18n._
try: try:
@ -374,35 +376,36 @@ class Logger:
start_of_day = int(time.mktime(local_time)) # we have time since epoch baby :) start_of_day = int(time.mktime(local_time)) # we have time since epoch baby :)
return start_of_day return start_of_day
def get_conversation_for_date(self, jid, year, month, day): def get_conversation_for_date(self, jid, year, month, day, account):
'''returns contact_name, time, kind, show, message '''returns contact_name, time, kind, show, message
for each row in a list of tupples, for each row in a list of tupples,
returns list with empty tupple if we found nothing to meet our demands''' returns list with empty tupple if we found nothing to meet our demands'''
jid = jid.lower() jid = jid.lower()
jid_id = self.get_jid_id(jid) jid_id = self.get_jid_id(jid)
where_sql = self._build_contact_where(account, jid)
start_of_day = self.get_unix_time_from_date(year, month, day) start_of_day = self.get_unix_time_from_date(year, month, day)
seconds_in_a_day = 86400 # 60 * 60 * 24 seconds_in_a_day = 86400 # 60 * 60 * 24
last_second_of_day = start_of_day + seconds_in_a_day - 1 last_second_of_day = start_of_day + seconds_in_a_day - 1
self.cur.execute(''' self.cur.execute('''
SELECT contact_name, time, kind, show, message FROM logs SELECT contact_name, time, kind, show, message FROM logs
WHERE jid_id = %d WHERE (%s)
AND time BETWEEN %d AND %d AND time BETWEEN %d AND %d
ORDER BY time ORDER BY time
''' % (jid_id, start_of_day, last_second_of_day)) ''' % (where_sql, start_of_day, last_second_of_day))
results = self.cur.fetchall() results = self.cur.fetchall()
return results return results
def get_search_results_for_query(self, jid, query): def get_search_results_for_query(self, jid, query, account):
'''returns contact_name, time, kind, show, message '''returns contact_name, time, kind, show, message
for each row in a list of tupples, for each row in a list of tupples,
returns list with empty tupple if we found nothing to meet our demands''' returns list with empty tupple if we found nothing to meet our demands'''
jid = jid.lower() jid = jid.lower()
jid_id = self.get_jid_id(jid) jid_id = self.get_jid_id(jid)
if False: #query.startswith('SELECT '): # it's SQL query
if False: #query.startswith('SELECT '): # it's SQL query (FIXME)
try: try:
self.cur.execute(query) self.cur.execute(query)
except sqlite.OperationalError, e: except sqlite.OperationalError, e:
@ -410,22 +413,24 @@ class Logger:
return results return results
else: # user just typed something, we search in message column else: # user just typed something, we search in message column
where_sql = self._build_contact_where(account, jid)
like_sql = '%' + query + '%' like_sql = '%' + query + '%'
self.cur.execute(''' self.cur.execute('''
SELECT contact_name, time, kind, show, message, subject FROM logs SELECT contact_name, time, kind, show, message, subject FROM logs
WHERE jid_id = ? AND message LIKE ? WHERE (%s) AND message LIKE '%s'
ORDER BY time ORDER BY time
''', (jid_id, like_sql)) ''' % (where_sql, like_sql))
results = self.cur.fetchall() results = self.cur.fetchall()
return results return results
def get_days_with_logs(self, jid, year, month, max_day): def get_days_with_logs(self, jid, year, month, max_day, account):
'''returns the list of days that have logs (not status messages)''' '''returns the list of days that have logs (not status messages)'''
jid = jid.lower() jid = jid.lower()
jid_id = self.get_jid_id(jid) jid_id = self.get_jid_id(jid)
days_with_logs = [] days_with_logs = []
where_sql = self._build_contact_where(account, jid)
# First select all date of month whith logs we want # First select all date of month whith logs we want
start_of_month = self.get_unix_time_from_date(year, month, 1) start_of_month = self.get_unix_time_from_date(year, month, 1)
seconds_in_a_day = 86400 # 60 * 60 * 24 seconds_in_a_day = 86400 # 60 * 60 * 24
@ -433,11 +438,11 @@ class Logger:
self.cur.execute(''' self.cur.execute('''
SELECT time FROM logs SELECT time FROM logs
WHERE jid_id = %d WHERE (%s)
AND time BETWEEN %d AND %d AND time BETWEEN %d AND %d
AND kind NOT IN (%d, %d) AND kind NOT IN (%d, %d)
ORDER BY time ORDER BY time
''' % (jid_id, start_of_month, last_second_of_month, ''' % (where_sql, start_of_month, last_second_of_month,
constants.KIND_STATUS, constants.KIND_GCSTATUS)) constants.KIND_STATUS, constants.KIND_GCSTATUS))
result = self.cur.fetchall() result = self.cur.fetchall()
@ -468,17 +473,23 @@ class Logger:
result = self.cur.fetchone() result = self.cur.fetchone()
return days_with_logs return days_with_logs
def get_last_date_that_has_logs(self, jid, is_room = False): def get_last_date_that_has_logs(self, jid, account = None, is_room = False):
'''returns last time (in seconds since EPOCH) for which '''returns last time (in seconds since EPOCH) for which
we had logs (excluding statuses)''' we had logs (excluding statuses)'''
jid = jid.lower() jid = jid.lower()
jid_id = self.get_jid_id(jid, 'ROOM')
where_sql = ''
if not is_room:
where_sql = self._build_contact_where(account, jid)
else:
jid_id = self.get_jid_id(jid, 'ROOM')
where_sql = 'jid_id = %s' % jid_id
self.cur.execute(''' self.cur.execute('''
SELECT time FROM logs SELECT time FROM logs
WHERE jid_id = ? WHERE (%s)
AND kind NOT IN (?, ?) AND kind NOT IN (%d, %d)
ORDER BY time DESC LIMIT 1 ORDER BY time DESC LIMIT 1
''', (jid_id, constants.KIND_STATUS, constants.KIND_GCSTATUS)) ''' % (where_sql, constants.KIND_STATUS, constants.KIND_GCSTATUS))
results = self.cur.fetchone() results = self.cur.fetchone()
if results is not None: if results is not None:
@ -487,3 +498,21 @@ class Logger:
result = None result = None
return result return result
def _build_contact_where(self, account, jid):
'''build the where clause for a jid, including metacontacts
jid(s) if any'''
where_sql = ''
# will return empty list if jid is not associated with
# any metacontacts
family = gajim.contacts.get_metacontacts_family(account, jid)
if family:
for user in family:
jid_id = self.get_jid_id(user['jid'])
where_sql += 'jid_id = %s' % jid_id
if user != family[-1]:
where_sql += ' OR '
else: # if jid was not associated with metacontacts
jid_id = self.get_jid_id(jid)
where_sql = 'jid_id = %s' % jid_id
return where_sql

View file

@ -120,7 +120,7 @@ class HistoryWindow:
# select and show logs for last date we have logs with contact # select and show logs for last date we have logs with contact
# and if we don't have logs at all, default to today # and if we don't have logs at all, default to today
result = gajim.logger.get_last_date_that_has_logs(self.jid) result = gajim.logger.get_last_date_that_has_logs(self.jid, self.account)
if result is None: if result is None:
date = time.localtime() date = time.localtime()
else: else:
@ -157,7 +157,7 @@ class HistoryWindow:
asks for days in this month if they have logs it bolds them (marks them)''' asks for days in this month if they have logs it bolds them (marks them)'''
weekday, days_in_this_month = calendar.monthrange(year, month) weekday, days_in_this_month = calendar.monthrange(year, month)
log_days = gajim.logger.get_days_with_logs(self.jid, year, log_days = gajim.logger.get_days_with_logs(self.jid, year,
month, days_in_this_month) month, days_in_this_month, self.account)
for day in log_days: for day in log_days:
widget.mark_day(day) widget.mark_day(day)
yield True yield True
@ -197,7 +197,8 @@ class HistoryWindow:
'''adds all the lines for given date in textbuffer''' '''adds all the lines for given date in textbuffer'''
self.history_buffer.set_text('') # clear the buffer first self.history_buffer.set_text('') # clear the buffer first
self.last_time_printout = 0 self.last_time_printout = 0
lines = gajim.logger.get_conversation_for_date(self.jid, year, month, day)
lines = gajim.logger.get_conversation_for_date(self.jid, year, month, day, self.account)
# lines holds list with tupples that have: # lines holds list with tupples that have:
# contact_name, time, kind, show, message # contact_name, time, kind, show, message
for line in lines: for line in lines:
@ -325,7 +326,8 @@ class HistoryWindow:
if text == '': if text == '':
return return
# contact_name, time, kind, show, message, subject # contact_name, time, kind, show, message, subject
results = gajim.logger.get_search_results_for_query(self.jid, text) results = gajim.logger.get_search_results_for_query(
self.jid, text, self.account)
#FIXME: #FIXME:
# add "subject: | message: " in message column if kind is single # add "subject: | message: " in message column if kind is single
# also do we need show at all? (we do not search on subject) # also do we need show at all? (we do not search on subject)