2005-11-11 20:12:02 +01:00
|
|
|
## notify.py
|
|
|
|
##
|
2005-12-09 18:15:30 +01:00
|
|
|
## Contributors for this file:
|
2005-11-11 20:12:02 +01:00
|
|
|
## - Yann Le Boulanger <asterix@lagaule.org>
|
|
|
|
## - Nikos Kouremenos <kourem@gmail.com>
|
|
|
|
## - Dimitur Kirov <dkirov@gmail.com>
|
|
|
|
## - Andrew Sayman <lorien420@myrealbox.com>
|
|
|
|
##
|
2005-12-10 00:30:28 +01:00
|
|
|
## Copyright (C) 2003-2004 Yann Le Boulanger <asterix@lagaule.org>
|
|
|
|
## Vincent Hanquez <tab@snarc.org>
|
|
|
|
## Copyright (C) 2005 Yann Le Boulanger <asterix@lagaule.org>
|
|
|
|
## Vincent Hanquez <tab@snarc.org>
|
|
|
|
## Nikos Kouremenos <nkour@jabber.org>
|
|
|
|
## Dimitur Kirov <dkirov@gmail.com>
|
|
|
|
## Travis Shirk <travis@pobox.com>
|
|
|
|
## Norman Rasmussen <norman@rasmussen.co.za>
|
2005-11-11 20:12:02 +01:00
|
|
|
##
|
|
|
|
## DBUS/libnotify connection code:
|
|
|
|
## Copyright (C) 2005 by Sebastian Estienne
|
|
|
|
##
|
|
|
|
## This program is free software; you can redistribute it and/or modify
|
|
|
|
## it under the terms of the GNU General Public License as published
|
|
|
|
## by the Free Software Foundation; version 2 only.
|
|
|
|
##
|
|
|
|
## This program is distributed in the hope that it will be useful,
|
|
|
|
## but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
## GNU General Public License for more details.
|
|
|
|
##
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import gajim
|
|
|
|
import dialogs
|
2005-12-10 01:56:38 +01:00
|
|
|
import gobject
|
2005-11-11 20:12:02 +01:00
|
|
|
|
|
|
|
from common import gajim
|
2005-12-10 01:56:38 +01:00
|
|
|
from common import exceptions
|
2005-11-11 20:12:02 +01:00
|
|
|
from common import i18n
|
|
|
|
i18n.init()
|
|
|
|
_ = i18n._
|
|
|
|
|
2005-12-10 01:56:38 +01:00
|
|
|
import dbus_support
|
|
|
|
if dbus_support.supported:
|
|
|
|
import dbus
|
|
|
|
if dbus_support.version >= (0, 41, 0):
|
|
|
|
import dbus.glib
|
|
|
|
import dbus.service
|
2005-11-11 20:12:02 +01:00
|
|
|
|
|
|
|
def notify(event_type, jid, account, msg_type = '', file_props = None):
|
2005-12-10 01:56:38 +01:00
|
|
|
'''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:
|
2005-11-11 20:12:02 +01:00
|
|
|
try:
|
2005-12-10 01:56:38 +01:00
|
|
|
DesktopNotification(event_type, jid, account, msg_type, file_props)
|
2005-11-11 20:12:02 +01:00
|
|
|
return
|
2005-12-11 17:58:20 +01:00
|
|
|
except dbus.dbus_bindings.DBusException, e:
|
2005-11-11 20:12:02 +01:00
|
|
|
# Connection to DBus failed, try popup
|
2005-12-26 17:41:22 +01:00
|
|
|
gajim.log.debug(str(e))
|
2005-11-11 20:12:02 +01:00
|
|
|
except TypeError, e:
|
|
|
|
# This means that we sent the message incorrectly
|
2005-12-26 17:41:22 +01:00
|
|
|
gajim.log.debug(str(e))
|
2005-11-11 20:12:02 +01:00
|
|
|
instance = dialogs.PopupNotificationWindow(event_type, jid, account,
|
|
|
|
msg_type, file_props)
|
|
|
|
gajim.interface.roster.popup_notification_windows.append(instance)
|
|
|
|
|
2005-12-10 01:56:38 +01:00
|
|
|
class NotificationResponseManager:
|
|
|
|
'''Collects references to pending DesktopNotifications and manages there
|
|
|
|
signalling. This is necessary due to a bug in DBus where you can't remove
|
|
|
|
a signal from an interface once it's connected.'''
|
|
|
|
def __init__(self):
|
|
|
|
self.pending = {}
|
|
|
|
self.interface = None
|
|
|
|
|
|
|
|
def attach_to_interface(self):
|
|
|
|
if self.interface is not None:
|
|
|
|
return
|
|
|
|
self.interface = dbus_support.get_notifications_interface()
|
|
|
|
self.interface.connect_to_signal('ActionInvoked', self.on_action_invoked)
|
|
|
|
self.interface.connect_to_signal('NotificationClosed', self.on_closed)
|
2005-12-10 23:44:47 +01:00
|
|
|
|
2005-12-10 01:56:38 +01:00
|
|
|
def on_action_invoked(self, id, reason):
|
|
|
|
if self.pending.has_key(id):
|
|
|
|
notification = self.pending[id]
|
|
|
|
notification.on_action_invoked(id, reason)
|
|
|
|
del self.pending[id]
|
|
|
|
else:
|
|
|
|
# This happens in the case of a race condition where the user clicks
|
|
|
|
# on a popup before the program finishes registering this callback
|
|
|
|
gobject.timeout_add(1000, self.on_action_invoked, id, reason)
|
|
|
|
|
|
|
|
def on_closed(self, id, reason):
|
|
|
|
if self.pending.has_key(id):
|
|
|
|
del self.pending[id]
|
2005-12-10 23:44:47 +01:00
|
|
|
|
2005-12-10 01:56:38 +01:00
|
|
|
notification_response_manager = NotificationResponseManager()
|
|
|
|
|
|
|
|
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):
|
|
|
|
self.account = account
|
|
|
|
self.jid = jid
|
|
|
|
self.msg_type = msg_type
|
|
|
|
self.file_props = file_props
|
|
|
|
|
|
|
|
if jid in gajim.contacts[account]:
|
|
|
|
actor = gajim.get_first_contact_instance_from_jid(account, jid).name
|
|
|
|
else:
|
|
|
|
actor = jid
|
|
|
|
|
2005-12-10 23:44:47 +01:00
|
|
|
txt = actor # default value of txt
|
|
|
|
|
2005-12-10 01:56:38 +01:00
|
|
|
if event_type == _('Contact Signed In'):
|
2005-12-10 23:41:16 +01:00
|
|
|
img = 'contact_online.png'
|
2005-12-10 01:56:38 +01:00
|
|
|
ntype = 'presence.online'
|
|
|
|
elif event_type == _('Contact Signed Out'):
|
2005-12-10 23:41:16 +01:00
|
|
|
img = 'contact_offline.png'
|
2005-12-10 01:56:38 +01:00
|
|
|
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}
|
2005-12-10 23:27:41 +01:00
|
|
|
img = 'priv_msg_recv.png'
|
2005-12-10 01:56:38 +01:00
|
|
|
else:
|
|
|
|
#we talk about a name here
|
|
|
|
txt = _('%s has sent you a new message.') % actor
|
2005-12-10 23:27:41 +01:00
|
|
|
if event_type == _('New Message'):
|
|
|
|
img = 'chat_msg_recv.png'
|
|
|
|
else: # New Single Message
|
|
|
|
img = 'single_msg_recv.png'
|
2005-12-10 01:56:38 +01:00
|
|
|
elif event_type == _('File Transfer Request'):
|
2005-12-11 19:40:25 +01:00
|
|
|
img = 'ft_request.png'
|
2005-12-10 01:56:38 +01:00
|
|
|
ntype = 'transfer'
|
|
|
|
#we talk about a name here
|
|
|
|
txt = _('%s wants to send you a file.') % actor
|
|
|
|
elif event_type == _('File Transfer Error'):
|
2005-12-10 23:00:06 +01:00
|
|
|
img = 'ft_stopped.png'
|
2005-12-10 01:56:38 +01:00
|
|
|
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]
|
2005-12-10 23:44:47 +01:00
|
|
|
name = gajim.get_first_contact_instance_from_jid(
|
2005-12-10 01:56:38 +01:00
|
|
|
account, sender).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}
|
2005-12-10 23:00:06 +01:00
|
|
|
img = 'ft_done.png'
|
2005-12-10 01:56:38 +01:00
|
|
|
else: # ft stopped
|
|
|
|
txt = _('File transfer of %(filename)s from %(name)s stopped.')\
|
|
|
|
% {'filename': filename, 'name': name}
|
2005-12-10 23:00:06 +01:00
|
|
|
img = 'ft_stopped.png'
|
2005-12-10 01:56:38 +01:00
|
|
|
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
|
2005-12-10 23:44:47 +01:00
|
|
|
name = gajim.get_first_contact_instance_from_jid(
|
2005-12-10 01:56:38 +01:00
|
|
|
account, receiver).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}
|
2005-12-10 23:00:06 +01:00
|
|
|
img = 'ft_done.png'
|
2005-12-10 01:56:38 +01:00
|
|
|
else: # ft stopped
|
|
|
|
txt = _('File transfer of %(filename)s to %(name)s stopped.')\
|
|
|
|
% {'filename': filename, 'name': name}
|
2005-12-10 23:00:06 +01:00
|
|
|
img = 'ft_stopped.png'
|
2005-12-10 01:56:38 +01:00
|
|
|
else:
|
|
|
|
txt = ''
|
2005-12-12 16:15:56 +01:00
|
|
|
else:
|
|
|
|
# defaul failsafe values
|
|
|
|
img = 'chat_msg_recv.png' # img to display
|
|
|
|
ntype = 'im' # Notification Type
|
2005-12-10 01:56:38 +01:00
|
|
|
|
2005-12-10 23:00:06 +01:00
|
|
|
path = os.path.join(gajim.DATA_DIR, 'pixmaps', 'events', img)
|
2005-12-10 01:56:38 +01:00
|
|
|
path = os.path.abspath(path)
|
2005-12-10 23:44:47 +01:00
|
|
|
|
2005-12-10 01:56:38 +01:00
|
|
|
self.notif = dbus_support.get_notifications_interface()
|
|
|
|
if self.notif is None:
|
2005-12-11 17:58:20 +01:00
|
|
|
raise dbus.dbus_bindings.DBusException()
|
2005-12-10 23:44:47 +01:00
|
|
|
self.id = self.notif.Notify(dbus.String(_('Gajim')),
|
2005-12-10 01:56:38 +01:00
|
|
|
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(5))
|
|
|
|
notification_response_manager.attach_to_interface()
|
|
|
|
notification_response_manager.pending[self.id] = self
|
2005-12-10 23:44:47 +01:00
|
|
|
|
2005-12-10 01:56:38 +01:00
|
|
|
def on_action_invoked(self, id, reason):
|
|
|
|
if self.notif is None:
|
|
|
|
return
|
|
|
|
self.notif.CloseNotification(dbus.UInt32(id))
|
|
|
|
self.notif = None
|
2005-12-12 10:28:36 +01:00
|
|
|
if not self.msg_type:
|
|
|
|
self.msg_type = 'chat'
|
2005-12-11 11:32:11 +01:00
|
|
|
gajim.interface.handle_event(self.account, self.jid, self.msg_type)
|