[knuckles] refactor API about notifications so we do not dup code allover

This commit is contained in:
Nikos Kouremenos 2006-01-20 17:40:45 +00:00
parent 818d2471fc
commit bbc42cc115
4 changed files with 137 additions and 188 deletions

View File

@ -269,6 +269,23 @@ def get_first_event(account, jid, typ = None):
if ev[0] == typ:
return ev
return None
def get_notification_image_prefix(jid):
'''returns the prefix for the notification images'''
transport_name = get_transport_name_from_jid(jid)
if transport_name in ('aim', 'icq', 'msn', 'yahoo'):
prefix = transport_name
else:
prefix = 'jabber'
return prefix
def get_actor(account, jid):
contact = contacts.get_first_contact_from_jid(account, jid)
if contact:
actor = contact.get_shown_name()
else:
actor = jid
return actor
def popup_window(account):
autopopup = config.get('autopopup')

View File

@ -867,11 +867,10 @@ _('Without a connection, you can not change your password.')).get_response()
class PopupNotificationWindow:
def __init__(self, event_type, jid, account, msg_type = '', file_props = None, gmail_new_messages = None):
def __init__(self, event_type, jid, account, msg_type = '', path_to_image = None, text = None):
self.account = account
self.jid = jid
self.msg_type = msg_type
self.file_props = file_props
xml = gtk.glade.XML(GTKGUI_GLADE, 'popup_notification_window', APP)
self.window = xml.get_widget('popup_notification_window')
@ -884,121 +883,64 @@ class PopupNotificationWindow:
event_type_label.set_markup(
'<span foreground="black" weight="bold">%s</span>' % event_type)
if self.jid in gajim.contacts.get_jid_list(account):
txt = gajim.contacts.get_first_contact_from_jid(account,
self.jid).get_shown_name()
else:
txt = self.jid
if not text:
text = gajim.get_actor(account, jid) # default value of text
# set colors [ http://www.pitt.edu/~nisg/cis/web/cgi/rgb.html ]
self.window.modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse('black'))
# prefix for images
transport_name = gajim.get_transport_name_from_jid(jid)
if transport_name in ('aim', 'icq', 'msn', 'yahoo'):
prefix = transport_name
else:
prefix = 'jabber'
# default image
img = 'chat_msg_recv.png'
if not path_to_image:
path_to_image = os.path.abspath(
os.path.join(gajim.DATA_DIR, 'pixmaps', 'events', 'chat_msg_recv.png')) # img to display
if event_type == _('Contact Signed In'):
limegreen = gtk.gdk.color_parse('limegreen')
close_button.modify_bg(gtk.STATE_NORMAL, limegreen)
eventbox.modify_bg(gtk.STATE_NORMAL, limegreen)
event_description_label.set_markup(
'<span foreground="black">%s</span>' % txt)
path_to_file = os.path.join(gajim.AVATAR_PATH, jid) + '_notif_size_colored.png'
if not os.path.exists(path_to_file):
img = prefix + '_online.png'
else:
img = path_to_file
'<span foreground="black">%s</span>' % text)
elif event_type == _('Contact Signed Out'):
red = gtk.gdk.color_parse('red')
close_button.modify_bg(gtk.STATE_NORMAL, red)
eventbox.modify_bg(gtk.STATE_NORMAL, red)
event_description_label.set_markup(
'<span foreground="black">%s</span>' % txt)
path_to_file = os.path.join(gajim.AVATAR_PATH, jid) + '_notif_size_bw.png'
if not os.path.exists(path_to_file):
img = prefix + '_offline.png'
else:
img = path_to_file
'<span foreground="black">%s</span>' % text)
elif event_type in (_('New Message'), _('New Single Message'),
_('New Private Message')):
dodgerblue = gtk.gdk.color_parse('dodgerblue')
close_button.modify_bg(gtk.STATE_NORMAL, dodgerblue)
eventbox.modify_bg(gtk.STATE_NORMAL, dodgerblue)
if event_type == _('New Private Message'):
room_jid, nick = gajim.get_room_and_nick_from_fjid(jid)
room_name,t = gajim.get_room_name_and_server_from_room_jid(room_jid)
txt = _('From %s in room %s') % (nick, room_name)
img = 'priv_msg_recv.png'
else:
txt = _('From %s') % txt
if event_type == _('New Message'):
img = 'chat_msg_recv.png'
else: # New Single Message
img = 'single_msg_recv.png'
event_description_label.set_markup('<span foreground="black">%s</span>' % txt)
event_description_label.set_markup(
'<span foreground="black">%s</span>' % text)
elif event_type == _('File Transfer Request'):
bg_color = gtk.gdk.color_parse('khaki')
close_button.modify_bg(gtk.STATE_NORMAL, bg_color)
eventbox.modify_bg(gtk.STATE_NORMAL, bg_color)
txt = _('From %s') % txt
event_description_label.set_markup('<span foreground="black">%s</span>' % txt)
img = 'ft_request.png'
event_description_label.set_markup(
'<span foreground="black">%s</span>' % text)
elif event_type == _('File Transfer Error'):
bg_color = gtk.gdk.color_parse('firebrick')
close_button.modify_bg(gtk.STATE_NORMAL, bg_color)
eventbox.modify_bg(gtk.STATE_NORMAL, bg_color)
event_description_label.set_markup('<span foreground="black">%s</span>' % txt)
img = 'ft_stopped.png'
event_description_label.set_markup(
'<span foreground="black">%s</span>' % text)
elif event_type in (_('File Transfer Completed'),
_('File Transfer Stopped')):
bg_color = gtk.gdk.color_parse('yellowgreen')
close_button.modify_bg(gtk.STATE_NORMAL, bg_color)
eventbox.modify_bg(gtk.STATE_NORMAL, bg_color)
if file_props is not None:
if file_props['type'] == 'r':
# get the name of the sender, as it is in the roster
sender = unicode(file_props['sender']).split('/')[0]
name = gajim.contacts.get_first_contact_from_jid(account,
sender).get_shown_name()
txt = _('From %s') % name
if event_type == _('File Transfer Completed'):
img = 'ft_done.png'
else: # ft stopped
img = 'ft_stopped.png'
else:
receiver = file_props['receiver']
if hasattr(receiver, 'jid'):
receiver = receiver.jid
receiver = receiver.split('/')[0]
# get the name of the contact, as it is in the roster
name = gajim.contacts.get_first_contact_from_jid(account,
receiver).get_shown_name()
txt = _('To %s') % name
if event_type == _('File Transfer Completed'):
img = 'ft_done.png'
else: # ft stopped
img = 'ft_stopped.png'
else:
txt = ''
event_description_label.set_markup('<span foreground="black">%s</span>' % txt)
event_description_label.set_markup(
'<span foreground="black">%s</span>' % text)
elif event_type == _('New E-mail'):
dodgerblue = gtk.gdk.color_parse('dodgerblue')
close_button.modify_bg(gtk.STATE_NORMAL, dodgerblue)
eventbox.modify_bg(gtk.STATE_NORMAL, dodgerblue)
text = i18n.ngettext('You have %d new E-mail message', 'You have %d new E-mail messages', gmail_new_messages, gmail_new_messages, gmail_new_messages)
txt = _('%(new_mail_gajim_ui_msg)s on %(gmail_mail_address)s') % {'new_mail_gajim_ui_msg': text, 'gmail_mail_address': jid}
event_description_label.set_markup('<span foreground="black">%s</span>' % txt)
img = 'single_msg_recv.png' #FIXME: find a better image
event_description_label.set_markup(
'<span foreground="black">%s</span>' % text)
# set the image
path = os.path.join(gajim.DATA_DIR, 'pixmaps', 'events', img)
path = os.path.abspath(path)
image.set_from_file(path)
image.set_from_file(path_to_image)
# position the window to bottom-right of screen
window_width, self.window_height = self.window.get_size()

View File

@ -360,8 +360,13 @@ class Interface:
# we're online or chat
show_notification = True
if show_notification:
notify.notify(_('Contact Signed In'), jid, account,
path_to_image = None)
path_to_file = os.path.join(gajim.AVATAR_PATH, jid) + '_notif_size_colored.png'
if not os.path.exists(path_to_file):
img = gajim.get_notification_image_prefix(jid) + '_online.png'
else:
img = path_to_file
path = os.path.abspath(os.path.join(gajim.DATA_DIR, 'pixmaps', 'events', img))
notify.notify(_('Contact Signed In'), jid, account, path_to_image = path)
if self.remote_ctrl:
self.remote_ctrl.raise_signal('ContactPresence',
@ -381,8 +386,14 @@ class Interface:
elif gajim.connections[account].connected in (2, 3): # we're online or chat
show_notification = True
if show_notification:
notify.notify(_('Contact Signed Out'), jid, account,
path_to_image = None)
path_to_file = os.path.join(gajim.AVATAR_PATH, jid) + '_notif_size_bw.png'
if not os.path.exists(path_to_file):
img = gajim.get_notification_image_prefix(jid) + '_offline.png'
else:
img = path_to_file
path = os.path.abspath(os.path.join(gajim.DATA_DIR, 'pixmaps', 'events', img))
notify.notify(_('Contact Signed Out'), jid, account, path_to_image = path)
if self.remote_ctrl:
self.remote_ctrl.raise_signal('ContactAbsence', (account, array))
# FIXME: stop non active file transfers
@ -419,7 +430,13 @@ class Interface:
if not gajim.interface.msg_win_mgr.has_window(fjid) and \
not gajim.awaiting_events[account].has_key(fjid):
if show_notification:
notify.notify(_('New Private Message'), fjid, account, 'pm')
room_jid, nick = gajim.get_room_and_nick_from_fjid(jid)
room_name,t = gajim.get_room_name_and_server_from_room_jid(room_jid)
txt = _('%(nickname)s in room %(room_name)s has sent you a new message.')\
% {'nickname': nick, 'room_name': room_name}
img = 'priv_msg_recv.png'
path = os.path.abspath(os.path.join(gajim.DATA_DIR, 'pixmaps', 'events', img))
notify.notify(_('New Private Message'), fjid, account, 'pm', path_to_image = path, text = txt)
chat_control.on_private_message(nick, array[1], array[2])
return
@ -461,10 +478,17 @@ class Interface:
elif gajim.connections[account].connected in (2, 3): # we're online or chat
show_notification = True
if show_notification:
txt = _('%s has sent you a new message.') % gajim.get_actor(account, jid)
if msg_type == 'normal': # single message
notify.notify(_('New Single Message'), jid, account, msg_type)
img = 'single_msg_recv.png'
path = os.path.abspath(os.path.join(gajim.DATA_DIR, 'pixmaps', 'events', img))
notify.notify(_('New Single Message'), jid, account, msg_type,
path_to_image = path, text = txt)
else: # chat message
notify.notify(_('New Message'), jid, account, msg_type)
img = 'chat_msg_recv.png'
path = os.path.abspath(os.path.join(gajim.DATA_DIR, 'pixmaps', 'events', img))
notify.notify(_('New Message'), jid, account, msg_type,
path_to_image = path, text = txt)
# array : (contact, msg, time, encrypted, msg_type, subject)
self.roster.on_message(jid, array[1], array[2], account, array[3],
@ -831,14 +855,20 @@ class Interface:
self.add_event(account, jid, 'file-send-error', file_props)
if gajim.show_notification(account):
img = 'ft_stopped.png'
path = os.path.abspath(os.path.join(gajim.DATA_DIR, 'pixmaps', 'events', img))
notify.notify(_('File Transfer Error'),
jid, account, 'file-send-error', file_props)
jid, account, 'file-send-error', file_props, path_to_image = path)
def handle_event_gmail_notify(self, account, array):
jid = array[0]
newmsgs = array[1]
gmail_new_messages = int(array[1])
if gajim.config.get('notify_on_new_gmail_email'):
notify.notify(_('New E-mail'), jid, account, 'gmail', gmail_new_messages = int(newmsgs))
img = 'single_msg_recv.png' #FIXME: find a better image
path = os.path.abspath(os.path.join(gajim.DATA_DIR, 'pixmaps', 'events', img))
txt = i18n.ngettext('You have %d new E-mail message', 'You have %d new E-mail messages', gmail_new_messages, gmail_new_messages, gmail_new_messages)
txt = _('%(new_mail_gajim_ui_msg)s on %(gmail_mail_address)s') % {'new_mail_gajim_ui_msg': txt, 'gmail_mail_address': jid}
notify.notify(_('New E-mail'), jid, account, 'gmail', path_to_image = path, text = txt)
def save_avatar_files(self, jid, photo_decoded):
'''Save the decoded avatar to a separate file, and generate files for dbus notifications'''
@ -933,8 +963,10 @@ class Interface:
self.add_event(account, jid, 'file-request', file_props)
if gajim.show_notification(account):
notify.notify(_('File Transfer Request'),
jid, account, 'file-request')
img = 'ft_request.png'
path = os.path.abspath(os.path.join(gajim.DATA_DIR, 'pixmaps', 'events', img))
txt = _('%s wants to send you a file.') % gajim.get_actor(account, jid)
notify.notify(_('File Transfer Request'), jid, account, 'file-request', path_to_image = path, text = txt)
def handle_event_file_progress(self, account, file_props):
self.instances['file_transfers'].set_progress(file_props['type'],
@ -978,13 +1010,49 @@ class Interface:
if msg_type:
self.add_event(account, jid, msg_type, file_props)
if file_props is not None:
if file_props['type'] == 'r':
# get the name of the sender, as it is in the roster
sender = unicode(file_props['sender']).split('/')[0]
name = gajim.contacts.get_first_contact_from_jid(account,
sender).get_shown_name()
filename = os.path.basename(file_props['file-name'])
if event_type == _('File Transfer Completed'):
txt = _('You successfully received %(filename)s from %(name)s.')\
% {'filename': filename, 'name': name}
img = 'ft_done.png'
else: # ft stopped
txt = _('File transfer of %(filename)s from %(name)s stopped.')\
% {'filename': filename, 'name': name}
img = 'ft_stopped.png'
else:
receiver = file_props['receiver']
if hasattr(receiver, 'jid'):
receiver = receiver.jid
receiver = receiver.split('/')[0]
# get the name of the contact, as it is in the roster
name = gajim.contacts.get_first_contact_from_jid(account,
receiver).get_shown_name()
filename = os.path.basename(file_props['file-name'])
if event_type == _('File Transfer Completed'):
txt = _('You successfully sent %(filename)s to %(name)s.')\
% {'filename': filename, 'name': name}
img = 'ft_done.png'
else: # ft stopped
txt = _('File transfer of %(filename)s to %(name)s stopped.')\
% {'filename': filename, 'name': name}
img = 'ft_stopped.png'
path = os.path.abspath(os.path.join(gajim.DATA_DIR, 'pixmaps', 'events', img))
else:
txt = ''
if gajim.config.get('notify_on_file_complete') and \
(gajim.config.get('autopopupaway') or \
gajim.connections[account].connected in (2, 3)):
# we want to be notified and we are online/chat or we don't mind
# bugged when away/na/busy
notify.notify(event_type, jid, account, msg_type, file_props)
notify.notify(event_type, jid, account, msg_type, path_to_image = path, text = txt)
def handle_event_stanza_arrived(self, account, stanza):
if not self.instances.has_key(account):

View File

@ -47,15 +47,15 @@ if dbus_support.supported:
import dbus.glib
import dbus.service
def notify(event_type, jid, account, msg_type = '', file_props = None,
path_to_image = None, gmail_new_messages = None):
def notify(event_type, jid, account, msg_type = '', path_to_image = None,
text = None):
'''Notifies a user of an event. It first tries to a valid implementation of
the Desktop Notification Specification. If that fails, then we fall back to
the older style PopupNotificationWindow method.'''
if gajim.config.get('use_notif_daemon') and dbus_support.supported:
try:
DesktopNotification(event_type, jid, account, msg_type, file_props,
path_to_image, gmail_new_messages)
DesktopNotification(event_type, jid, account, msg_type, path_to_image,
text)
return
except dbus.dbus_bindings.DBusException, e:
# Connection to D-Bus failed, try popup
@ -63,8 +63,7 @@ def notify(event_type, jid, account, msg_type = '', file_props = None,
except TypeError, e:
# This means that we sent the message incorrectly
gajim.log.debug(str(e))
instance = dialogs.PopupNotificationWindow(event_type, jid, account,
msg_type, file_props, gmail_new_messages)
instance = dialogs.PopupNotificationWindow(event_type, jid, account, msg_type, path_to_image, text)
gajim.interface.roster.popup_notification_windows.append(instance)
class NotificationResponseManager:
@ -102,112 +101,35 @@ class DesktopNotification:
'''A DesktopNotification that interfaces with DBus via the Desktop
Notification specification'''
def __init__(self, event_type, jid, account, msg_type = '',
file_props = None, path_to_image = None, gmail_new_messages = None):
path_to_image = None, text = None):
self.account = account
self.jid = jid
self.msg_type = msg_type
self.file_props = file_props
contact = gajim.contacts.get_first_contact_from_jid(account, jid)
if contact:
actor = contact.get_shown_name()
else:
actor = jid
txt = actor # default value of txt
transport_name = gajim.get_transport_name_from_jid(jid)
if transport_name in ('aim', 'icq', 'msn', 'yahoo'):
prefix = transport_name
else:
prefix = 'jabber'
if not text:
text = gajim.get_actor(account, jid) # default value of text
if event_type == _('Contact Signed In'):
path_to_file = os.path.join(gajim.AVATAR_PATH, jid) + '_notif_size_colored.png'
if not os.path.exists(path_to_file):
img = prefix + '_online.png'
else:
img = path_to_file
ntype = 'presence.online'
elif event_type == _('Contact Signed Out'):
path_to_file = os.path.join(gajim.AVATAR_PATH, jid) + '_notif_size_bw.png'
if not os.path.exists(path_to_file):
img = prefix + '_offline.png'
else:
img = path_to_file
ntype = 'presence.offline'
elif event_type in (_('New Message'), _('New Single Message'),
_('New Private Message')):
ntype = 'im.received'
if event_type == _('New Private Message'):
room_jid, nick = gajim.get_room_and_nick_from_fjid(jid)
room_name,t = gajim.get_room_name_and_server_from_room_jid(room_jid)
txt = _('%(nickname)s in room %(room_name)s has sent you a new message.')\
% {'nickname': nick, 'room_name': room_name}
img = 'priv_msg_recv.png'
else:
#we talk about a name here
txt = _('%s has sent you a new message.') % actor
if event_type == _('New Message'):
img = 'chat_msg_recv.png'
else: # New Single Message
img = 'single_msg_recv.png'
elif event_type == _('File Transfer Request'):
img = 'ft_request.png'
ntype = 'transfer'
#we talk about a name here
txt = _('%s wants to send you a file.') % actor
elif event_type == _('File Transfer Error'):
img = 'ft_stopped.png'
ntype = 'transfer.error'
elif event_type in (_('File Transfer Completed'), _('File Transfer Stopped')):
ntype = 'transfer.complete'
if file_props is not None:
if file_props['type'] == 'r':
# get the name of the sender, as it is in the roster
sender = unicode(file_props['sender']).split('/')[0]
name = gajim.contacts.get_first_contact_from_jid(account,
sender).get_shown_name()
filename = os.path.basename(file_props['file-name'])
if event_type == _('File Transfer Completed'):
txt = _('You successfully received %(filename)s from %(name)s.')\
% {'filename': filename, 'name': name}
img = 'ft_done.png'
else: # ft stopped
txt = _('File transfer of %(filename)s from %(name)s stopped.')\
% {'filename': filename, 'name': name}
img = 'ft_stopped.png'
else:
receiver = file_props['receiver']
if hasattr(receiver, 'jid'):
receiver = receiver.jid
receiver = receiver.split('/')[0]
# get the name of the contact, as it is in the roster
name = gajim.contacts.get_first_contact_from_jid(account,
receiver).get_shown_name()
filename = os.path.basename(file_props['file-name'])
if event_type == _('File Transfer Completed'):
txt = _('You successfully sent %(filename)s to %(name)s.')\
% {'filename': filename, 'name': name}
img = 'ft_done.png'
else: # ft stopped
txt = _('File transfer of %(filename)s to %(name)s stopped.')\
% {'filename': filename, 'name': name}
img = 'ft_stopped.png'
else:
txt = ''
elif event_type == _('New E-mail'):
text = i18n.ngettext('You have %d new E-mail message', 'You have %d new E-mail messages', gmail_new_messages, gmail_new_messages, gmail_new_messages)
txt = _('%(new_mail_gajim_ui_msg)s on %(gmail_mail_address)s') % {'new_mail_gajim_ui_msg': text, 'gmail_mail_address': jid}
ntype = 'gmail.notify'
img = 'single_msg_recv.png' #FIXME: find a better image
else:
# defaul failsafe values
img = 'chat_msg_recv.png' # img to display
ntype = 'im' # Notification Type
path = os.path.join(gajim.DATA_DIR, 'pixmaps', 'events', img)
path = os.path.abspath(path)
# default failsafe values
path_to_image = os.path.abspath(
os.path.join(gajim.DATA_DIR, 'pixmaps', 'events',
'chat_msg_recv.png')) # img to display
ntype = 'im' # Notification Type
self.notif = dbus_support.get_notifications_interface()
if self.notif is None:
@ -225,16 +147,16 @@ class DesktopNotification:
if version.startswith('0.2'):
try:
self.id = self.notif.Notify(dbus.String(_('Gajim')),
dbus.String(path), dbus.UInt32(0), ntype, dbus.Byte(0),
dbus.String(event_type), dbus.String(txt),
[dbus.String(path)], {'default': 0}, [''], True, dbus.UInt32(
timeout))
dbus.String(path_to_image), dbus.UInt32(0), ntype, dbus.Byte(0),
dbus.String(event_type), dbus.String(text),
[dbus.String(path_to_image)], {'default': 0}, [''], True,
dbus.UInt32(timeout))
except AttributeError:
version = '0.3.1' # we're actually dealing with the newer version
if version.startswith('0.3'):
self.id = self.notif.Notify(dbus.String(_('Gajim')),
dbus.String(path), dbus.UInt32(0), dbus.String(event_type),
dbus.String(txt), dbus.String(''), {}, dbus.UInt32(timeout*1000))
dbus.String(path_to_image), dbus.UInt32(0), dbus.String(event_type),
dbus.String(text), dbus.String(''), {}, dbus.UInt32(timeout*1000))
notification_response_manager.attach_to_interface()
notification_response_manager.pending[self.id] = self