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
|
2006-03-25 01:59:09 +01:00
|
|
|
import time
|
2005-11-11 20:12:02 +01:00
|
|
|
import dialogs
|
2005-12-10 01:56:38 +01:00
|
|
|
import gobject
|
2005-11-11 20:12:02 +01:00
|
|
|
|
|
|
|
from common import gajim
|
|
|
|
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
|
|
|
|
2006-01-20 18:40:45 +01:00
|
|
|
def notify(event_type, jid, account, msg_type = '', path_to_image = None,
|
|
|
|
text = 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:
|
2006-01-20 18:40:45 +01:00
|
|
|
DesktopNotification(event_type, jid, account, msg_type, path_to_image,
|
|
|
|
text)
|
2005-11-11 20:12:02 +01:00
|
|
|
return
|
2005-12-11 17:58:20 +01:00
|
|
|
except dbus.dbus_bindings.DBusException, e:
|
Merged revisions 4987-4989,4991-4996,4999,5003 via svnmerge from
svn://svn.gajim.org/gajim/trunk
........
r4987 | nk | 2006-01-03 04:00:51 -0700 (Tue, 03 Jan 2006) | 1 line
commit 48x48 transport online/offline imgs by Grenshad (I pngcrushed them)
........
r4988 | nk | 2006-01-03 04:32:01 -0700 (Tue, 03 Jan 2006) | 1 line
icon in notification window not always jabber now. MSN if he uses msn etc. thanks stian barmen for helping me test
........
r4989 | nk | 2006-01-03 04:40:44 -0700 (Tue, 03 Jan 2006) | 1 line
all strings I got report about them, are not translatable; pot/po update
........
r4991 | asterix | 2006-01-03 08:08:21 -0700 (Tue, 03 Jan 2006) | 2 lines
don't remove the jid entry in _contacts[account] when we remove a contact
........
r4992 | asterix | 2006-01-03 08:18:30 -0700 (Tue, 03 Jan 2006) | 2 lines
fix logic
........
r4993 | asterix | 2006-01-03 09:04:14 -0700 (Tue, 03 Jan 2006) | 2 lines
a GC_Contact can have a resource if we knoe his real JID
........
r4994 | asterix | 2006-01-03 09:32:58 -0700 (Tue, 03 Jan 2006) | 2 lines
missing argument in create_gc_contact
........
r4995 | asterix | 2006-01-03 10:36:41 -0700 (Tue, 03 Jan 2006) | 2 lines
we save gc_contact vcard instance in instances[self.account]['infos'][Fake_jid]
........
r4996 | asterix | 2006-01-03 11:17:43 -0700 (Tue, 03 Jan 2006) | 2 lines
in DataForm, a field of type 'list-single' can have no <value> element. Create a default one in such a case to prevent TB
........
r4999 | asterix | 2006-01-04 05:52:26 -0700 (Wed, 04 Jan 2006) | 2 lines
prevent TB when we move a contact that was in no group
........
r5003 | asterix | 2006-01-04 09:03:42 -0700 (Wed, 04 Jan 2006) | 2 lines
handle correctly unlabeled option values in DataForms
........
2006-01-05 04:17:36 +01:00
|
|
|
# Connection to D-Bus 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))
|
2006-01-20 18:40:45 +01:00
|
|
|
instance = dialogs.PopupNotificationWindow(event_type, jid, account, msg_type, path_to_image, text)
|
2005-11-11 20:12:02 +01:00
|
|
|
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 = {}
|
2006-03-25 01:59:09 +01:00
|
|
|
self.received = []
|
2005-12-10 01:56:38 +01:00
|
|
|
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):
|
2006-03-25 01:59:09 +01:00
|
|
|
self.received.append((id, time.time(), reason))
|
2005-12-10 01:56:38 +01:00
|
|
|
if self.pending.has_key(id):
|
|
|
|
notification = self.pending[id]
|
|
|
|
notification.on_action_invoked(id, reason)
|
|
|
|
del self.pending[id]
|
2006-03-25 01:59:09 +01:00
|
|
|
if len(self.received) > 20:
|
|
|
|
curt = time.time()
|
|
|
|
for rec in self.received:
|
|
|
|
diff = curt - rec[1]
|
|
|
|
if diff > 10:
|
|
|
|
self.received.remove(rec)
|
2005-12-10 01:56:38 +01:00
|
|
|
|
2006-01-26 16:43:34 +01:00
|
|
|
def on_closed(self, id, reason = None):
|
2005-12-10 01:56:38 +01:00
|
|
|
if self.pending.has_key(id):
|
|
|
|
del self.pending[id]
|
2005-12-10 23:44:47 +01:00
|
|
|
|
2006-03-25 01:59:09 +01:00
|
|
|
def add_pending(self, id, object):
|
|
|
|
# Check to make sure that we handle an event immediately if we're adding
|
|
|
|
# an id that's already been triggered
|
|
|
|
for rec in self.received:
|
|
|
|
if rec[0] == id:
|
|
|
|
object.on_action_invoked(id, rec[2])
|
|
|
|
self.received.remove(rec)
|
|
|
|
return
|
|
|
|
if id not in self.pending:
|
|
|
|
# Add it
|
|
|
|
self.pending[id] = object
|
|
|
|
else:
|
|
|
|
# We've triggered an event that has a duplicate ID!
|
|
|
|
gajim.log.debug('Duplicate ID of notification. Can\'t handle this.')
|
|
|
|
|
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'''
|
2006-01-12 23:48:49 +01:00
|
|
|
def __init__(self, event_type, jid, account, msg_type = '',
|
2006-01-20 18:40:45 +01:00
|
|
|
path_to_image = None, text = None):
|
2006-03-25 01:59:09 +01:00
|
|
|
self.path_to_image = path_to_image
|
|
|
|
self.event_type = event_type
|
|
|
|
self.text = text
|
|
|
|
self.default_version = '0.3.1'
|
2005-12-10 01:56:38 +01:00
|
|
|
self.account = account
|
|
|
|
self.jid = jid
|
|
|
|
self.msg_type = msg_type
|
2005-12-10 23:44:47 +01:00
|
|
|
|
2006-01-20 18:40:45 +01:00
|
|
|
if not text:
|
2006-03-25 01:59:09 +01:00
|
|
|
self.text = gajim.get_name_from_jid(account, jid) # default value of text
|
2006-01-20 18:40:45 +01:00
|
|
|
|
2005-12-10 01:56:38 +01:00
|
|
|
if event_type == _('Contact Signed In'):
|
|
|
|
ntype = 'presence.online'
|
2006-03-30 14:57:38 +02:00
|
|
|
button_text = _('Start Chat with Contact')
|
2005-12-10 01:56:38 +01:00
|
|
|
elif event_type == _('Contact Signed Out'):
|
|
|
|
ntype = 'presence.offline'
|
|
|
|
elif event_type in (_('New Message'), _('New Single Message'),
|
|
|
|
_('New Private Message')):
|
|
|
|
ntype = 'im.received'
|
2006-03-30 14:57:38 +02:00
|
|
|
button_text = _('Focus Received Message')
|
2005-12-10 01:56:38 +01:00
|
|
|
elif event_type == _('File Transfer Request'):
|
|
|
|
ntype = 'transfer'
|
2006-03-30 14:57:38 +02:00
|
|
|
button_text = _('Manage File Transfer')
|
2005-12-10 01:56:38 +01:00
|
|
|
elif event_type == _('File Transfer Error'):
|
|
|
|
ntype = 'transfer.error'
|
2006-03-30 14:57:38 +02:00
|
|
|
button_text = _('Manage File Transfer')
|
2005-12-10 01:56:38 +01:00
|
|
|
elif event_type in (_('File Transfer Completed'), _('File Transfer Stopped')):
|
|
|
|
ntype = 'transfer.complete'
|
2006-03-30 14:57:38 +02:00
|
|
|
button_text = _('Manage File Transfer')
|
2006-01-11 22:30:48 +01:00
|
|
|
elif event_type == _('New E-mail'):
|
2006-03-25 01:59:09 +01:00
|
|
|
ntype = 'email.arrived'
|
2006-03-30 14:57:38 +02:00
|
|
|
button_text = _('View New E-Mail')
|
2006-03-24 21:01:52 +01:00
|
|
|
elif event_type == _('Groupchat Invitation'):
|
|
|
|
ntype = 'im.invitation'
|
2006-03-30 14:57:38 +02:00
|
|
|
button_text = _('Manage Invitation')
|
2005-12-12 16:15:56 +01:00
|
|
|
else:
|
2006-01-20 18:40:45 +01:00
|
|
|
# default failsafe values
|
2006-03-25 01:59:09 +01:00
|
|
|
self.path_to_image = os.path.abspath(
|
2006-01-20 18:40:45 +01:00
|
|
|
os.path.join(gajim.DATA_DIR, 'pixmaps', 'events',
|
|
|
|
'chat_msg_recv.png')) # img to display
|
|
|
|
ntype = 'im' # Notification Type
|
2006-03-30 14:57:38 +02:00
|
|
|
button_text = event_type
|
|
|
|
self.response_description = button_text
|
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()
|
2006-03-25 01:59:09 +01:00
|
|
|
self.ntype = ntype
|
|
|
|
|
|
|
|
self.get_version()
|
|
|
|
|
|
|
|
def attempt_notify(self):
|
|
|
|
version = self.version
|
2005-12-30 22:37:36 +01:00
|
|
|
timeout = gajim.config.get('notification_timeout') # in seconds
|
2006-03-25 01:59:09 +01:00
|
|
|
ntype = self.ntype
|
2006-01-13 21:33:43 +01:00
|
|
|
if version.startswith('0.2'):
|
|
|
|
try:
|
2006-03-25 01:59:09 +01:00
|
|
|
self.notif.Notify(
|
|
|
|
dbus.String(_('Gajim')),
|
|
|
|
dbus.String(self.path_to_image),
|
|
|
|
dbus.UInt32(0),
|
|
|
|
ntype,
|
|
|
|
dbus.Byte(0),
|
|
|
|
dbus.String(self.event_type),
|
|
|
|
dbus.String(self.text),
|
|
|
|
[dbus.String(self.path_to_image)],
|
|
|
|
{'default': 0},
|
|
|
|
[''],
|
|
|
|
True,
|
|
|
|
dbus.UInt32(timeout),
|
|
|
|
reply_handler=self.attach_by_id,
|
|
|
|
error_handler=self.notify_another_way)
|
2006-01-13 21:33:43 +01:00
|
|
|
except AttributeError:
|
|
|
|
version = '0.3.1' # we're actually dealing with the newer version
|
|
|
|
if version.startswith('0.3'):
|
2006-01-26 16:26:20 +01:00
|
|
|
if version >= ( 0, 3, 2):
|
2006-03-25 01:59:09 +01:00
|
|
|
hints = {}
|
|
|
|
hints['urgency'] = dbus.Byte(0)
|
|
|
|
hints['category'] = dbus.String(ntype)
|
|
|
|
self.notif.Notify(
|
|
|
|
dbus.String(_('Gajim')),
|
|
|
|
dbus.UInt32(0),
|
|
|
|
dbus.String(self.path_to_image),
|
|
|
|
dbus.String(self.event_type),
|
|
|
|
dbus.String(self.text),
|
2006-03-30 14:57:38 +02:00
|
|
|
( dbus.String(ntype),
|
|
|
|
dbus.String(self.response_description) ),
|
2006-03-25 01:59:09 +01:00
|
|
|
hints,
|
|
|
|
dbus.UInt32(timeout*1000),
|
|
|
|
reply_handler=self.attach_by_id,
|
|
|
|
error_handler=self.notify_another_way)
|
2006-01-26 16:26:20 +01:00
|
|
|
else:
|
2006-03-25 01:59:09 +01:00
|
|
|
self.notif.Notify(
|
|
|
|
dbus.String(_('Gajim')),
|
|
|
|
dbus.String(self.path_to_image),
|
|
|
|
dbus.UInt32(0),
|
|
|
|
dbus.String(self.event_type),
|
|
|
|
dbus.String(self.text),
|
|
|
|
dbus.String(''),
|
|
|
|
{},
|
|
|
|
dbus.UInt32(timeout*1000),
|
|
|
|
reply_handler=self.attach_by_id,
|
|
|
|
error_handler=self.notify_another_way)
|
|
|
|
|
|
|
|
def attach_by_id(self, id):
|
|
|
|
self.id = id
|
2005-12-10 01:56:38 +01:00
|
|
|
notification_response_manager.attach_to_interface()
|
2006-03-25 01:59:09 +01:00
|
|
|
notification_response_manager.add_pending(self.id, self)
|
|
|
|
|
|
|
|
def notify_another_way(self,e):
|
|
|
|
gajim.log.debug(str(e))
|
|
|
|
gajim.log.debug('Need to implement a new way of falling back')
|
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)
|
2006-03-25 01:59:09 +01:00
|
|
|
|
|
|
|
def version_reply_handler(self, name, vendor, version, spec_version = None):
|
|
|
|
self.version = version
|
|
|
|
self.attempt_notify()
|
|
|
|
|
|
|
|
def get_version(self):
|
|
|
|
self.notif.GetServerInfo(
|
|
|
|
reply_handler=self.version_reply_handler,
|
|
|
|
error_handler=self.version_error_handler_2_x_try)
|
|
|
|
|
|
|
|
def version_error_handler_2_x_try(self, e):
|
|
|
|
self.notif.GetServerInformation(reply_handler=self.version_reply_handler, error_handler=self.version_error_handler_3_x_try)
|
|
|
|
|
|
|
|
def version_error_handler_3_x_try(self, e):
|
|
|
|
self.version = self.default_version
|
|
|
|
self.attempt_notify()
|