[lukas and I] improve tooltip to show info about pending events. fixes #1971

This commit is contained in:
Yann Leboulanger 2007-07-03 09:31:43 +00:00
parent 1c0aa7ad10
commit 20965b9ba5
2 changed files with 120 additions and 59 deletions

View File

@ -5,6 +5,7 @@
## Copyright (C) 2005 ## Copyright (C) 2005
## Dimitur Kirov <dkirov@gmail.com> ## Dimitur Kirov <dkirov@gmail.com>
## Travis Shirk <travis@pobox.com> ## Travis Shirk <travis@pobox.com>
## Copyright (C) 2007 Lukas Petrovicky <lukas@petrovicky.net>
## ##
## This program is free software; you can redistribute it and/or modify ## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published ## it under the terms of the GNU General Public License as published
@ -874,62 +875,121 @@ def reduce_chars_newlines(text, max_chars = 0, max_lines = 0):
reduced_text = '' reduced_text = ''
return reduced_text return reduced_text
def get_notification_icon_tooltip_text(): def get_account_status(account):
text = None status = reduce_chars_newlines(account['status_line'], 100, 1)
unread_chat = gajim.events.get_nb_events(types = ['printed_chat', return status
'chat'])
unread_single_chat = gajim.events.get_nb_events(types = ['normal']) def get_notification_icon_tooltip_dict():
unread_gc = gajim.events.get_nb_events(types = ['printed_gc_msg', '''returns a dict of the form {acct: {'show': show, 'message': message,
'printed_marked_gc_msg', 'gc_msg']) 'event_lines': [list of text lines to show in tooltip]}'''
unread_pm = gajim.events.get_nb_events(types = ['printed_pm', 'pm']) # How many events must there be before they're shown summarized, not per-user
max_ungrouped_events = 10
accounts = get_accounts_info() accounts = get_accounts_info()
if unread_chat or unread_single_chat or unread_gc or unread_pm: # Gather events. (With accounts, when there are more.)
text = 'Gajim ' for account in accounts:
awaiting_events = unread_chat + unread_single_chat + unread_gc + unread_pm account_name = account['name']
if awaiting_events == unread_chat or awaiting_events == unread_single_chat \ account['event_lines'] = []
or awaiting_events == unread_gc or awaiting_events == unread_pm: # Gather events per-account
# This condition is like previous if but with xor... pending_events = gajim.events.get_events(account = account_name)
# Print in one line messages, non_messages, total_messages, total_non_messages = {}, {}, 0, 0
text += '-' for jid in pending_events:
else: for event in pending_events[jid]:
# Print in multiple lines if event.type_.count('file') > 0:
text += '\n ' # This is a non-messagee event.
if unread_chat: messages[jid] = non_messages.get(jid, 0) + 1
text += ngettext( total_non_messages = total_non_messages + 1
' %d unread message', else:
' %d unread messages', # This is a message.
unread_chat, unread_chat, unread_chat) messages[jid] = messages.get(jid, 0) + 1
text += '\n ' total_messages = total_messages + 1
if unread_single_chat: # Display unread messages numbers, if any
text += ngettext( if total_messages > 0:
' %d unread single message', if total_messages > max_ungrouped_events:
' %d unread single messages', text = ngettext(
unread_single_chat, unread_single_chat, unread_single_chat) '%d message pending',
text += '\n ' '%d messages pending',
if unread_gc: total_messages, total_messages, total_messages)
text += ngettext( account['event_lines'].append(text)
' %d unread group chat message', else:
' %d unread group chat messages', for jid in messages.keys():
unread_gc, unread_gc, unread_gc) text = ngettext(
text += '\n ' '%d message pending',
if unread_pm: '%d messages pending',
text += ngettext( messages[jid], messages[jid], messages[jid])
' %d unread private message', contact = gajim.contacts.get_first_contact_from_jid(
' %d unread private messages', account['name'], jid)
unread_pm, unread_pm, unread_pm) if jid in gajim.gc_connected[account['name']]:
text += '\n ' text += _(' from room %s') % (jid)
text = text[:-4] # remove latest '\n ' elif contact:
elif len(accounts) > 1: name = contact.get_shown_name()
text = _('Gajim') text += _(' from user %s') % (name)
elif len(accounts) == 1: else:
message = accounts[0]['status_line'] text += _(' from %s') % (jid)
message = reduce_chars_newlines(message, 100, 1) account['event_lines'].append(text)
text = _('Gajim - %s') % message
else:
text = _('Gajim - %s') % get_uf_show('offline')
# Display unseen events numbers, if any
if total_non_messages > 0:
if total_non_messages > max_ungrouped_events:
text = ngettext(
'%d event pending',
'%d events pending',
total_non_messages, total_non_messages, total_non_messages)
accounts[account]['event_lines'].append(text)
else:
for jid in non_messages.keys():
text = ngettext(
'%d event pending',
'%d events pending',
non_messages[jid], non_messages[jid], non_messages[jid])
text += _(' from user %s') % (jid)
accounts[account]['event_lines'].append(text)
return accounts
def get_notification_icon_tooltip_text():
text = None
# How many events must there be before they're shown summarized, not per-user
max_ungrouped_events = 10
# Character which should be used to indent in the tooltip.
indent_with = ' '
accounts = get_notification_icon_tooltip_dict()
if len(accounts) == 0:
# No configured account
return _('Gajim')
# at least one account present
# Is there more that one account?
if len(accounts) == 1:
show_more_accounts = False
else:
show_more_accounts = True
# If there is only one account, its status is shown on the first line.
if show_more_accounts:
text = _('Gajim')
else:
text = _('Gajim - %s') % (get_account_status(accounts[0]))
# Gather and display events. (With accounts, when there are more.)
for account in accounts:
account_name = account['name']
# Set account status, if not set above
if (show_more_accounts):
message = '\n' + indent_with + ' %s - %s'
text += message % (account_name, get_account_status(account))
# Account list shown, messages need to be indented more
indent_how = 2
else:
# If no account list is shown, messages could have default indenting.
indent_how = 1
for line in account['event_lines']:
text += '\n' + indent_with * indent_how + ' '
text += line
return text return text
def get_accounts_info(): def get_accounts_info():

View File

@ -160,13 +160,15 @@ class StatusTable:
self.table = gtk.Table(4, 1) self.table = gtk.Table(4, 1)
self.table.set_property('column-spacing', 2) self.table.set_property('column-spacing', 2)
def add_text_row(self, text): def add_text_row(self, text, col_inc = 0):
self.current_row += 1
self.text_label = gtk.Label() self.text_label = gtk.Label()
self.text_label.set_line_wrap(True) self.text_label.set_line_wrap(True)
self.text_label.set_alignment(0, 0) self.text_label.set_alignment(0, 0)
self.text_label.set_selectable(False) self.text_label.set_selectable(False)
self.text_label.set_markup(text) self.text_label.set_markup(text)
self.table.attach(self.text_label, 1, 4, 1, 2) self.table.attach(self.text_label, 1 + col_inc, 4, self.current_row,
self.current_row + 1)
def get_status_info(self, resource, priority, show, status): def get_status_info(self, resource, priority, show, status):
str_status = resource + ' (' + unicode(priority) + ')' str_status = resource + ' (' + unicode(priority) + ')'
@ -247,21 +249,20 @@ class NotificationAreaTooltip(BaseTooltip, StatusTable):
self.add_status_row(file_path, acct['show'], self.add_status_row(file_path, acct['show'],
gobject.markup_escape_text(acct['name']) gobject.markup_escape_text(acct['name'])
, show_lock=show_lock) , show_lock=show_lock)
for line in acct['event_lines']:
self.add_text_row(' ' + line, 2)
def populate(self, data): def populate(self, data):
self.create_window() self.create_window()
self.create_table() self.create_table()
accounts = helpers.get_accounts_info()
accounts = helpers.get_notification_icon_tooltip_dict()
if len(accounts) > 1: if len(accounts) > 1:
self.table.resize(2, 1) self.table.resize(2, 1)
self.fill_table_with_accounts(accounts) self.fill_table_with_accounts(accounts)
self.hbox = gtk.HBox() self.hbox = gtk.HBox()
self.table.set_property('column-spacing', 1) self.table.set_property('column-spacing', 1)
text = helpers.get_notification_icon_tooltip_text()
text = gobject.markup_escape_text(text)
self.add_text_row(text)
self.hbox.add(self.table) self.hbox.add(self.table)
self.win.add(self.hbox) self.win.add(self.hbox)