[Andrew Sayman] Gajim now uses async call to notification-daemon. Fixes #1388
This commit is contained in:
parent
6cec4ba128
commit
c2594d8792
136
src/notify.py
136
src/notify.py
|
@ -29,6 +29,7 @@
|
|||
##
|
||||
|
||||
import os
|
||||
import time
|
||||
import dialogs
|
||||
import gobject
|
||||
|
||||
|
@ -69,6 +70,7 @@ class NotificationResponseManager:
|
|||
a signal from an interface once it's connected.'''
|
||||
def __init__(self):
|
||||
self.pending = {}
|
||||
self.received = []
|
||||
self.interface = None
|
||||
|
||||
def attach_to_interface(self):
|
||||
|
@ -79,19 +81,37 @@ class NotificationResponseManager:
|
|||
self.interface.connect_to_signal('NotificationClosed', self.on_closed)
|
||||
|
||||
def on_action_invoked(self, id, reason):
|
||||
self.received.append((id, time.time(), 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)
|
||||
if len(self.received) > 20:
|
||||
curt = time.time()
|
||||
for rec in self.received:
|
||||
diff = curt - rec[1]
|
||||
if diff > 10:
|
||||
self.received.remove(rec)
|
||||
|
||||
def on_closed(self, id, reason = None):
|
||||
if self.pending.has_key(id):
|
||||
del self.pending[id]
|
||||
|
||||
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.')
|
||||
|
||||
notification_response_manager = NotificationResponseManager()
|
||||
|
||||
class DesktopNotification:
|
||||
|
@ -99,12 +119,16 @@ class DesktopNotification:
|
|||
Notification specification'''
|
||||
def __init__(self, event_type, jid, account, msg_type = '',
|
||||
path_to_image = None, text = None):
|
||||
self.path_to_image = path_to_image
|
||||
self.event_type = event_type
|
||||
self.text = text
|
||||
self.default_version = '0.3.1'
|
||||
self.account = account
|
||||
self.jid = jid
|
||||
self.msg_type = msg_type
|
||||
|
||||
if not text:
|
||||
text = gajim.get_name_from_jid(account, jid) # default value of text
|
||||
self.text = gajim.get_name_from_jid(account, jid) # default value of text
|
||||
|
||||
if event_type == _('Contact Signed In'):
|
||||
ntype = 'presence.online'
|
||||
|
@ -120,12 +144,12 @@ class DesktopNotification:
|
|||
elif event_type in (_('File Transfer Completed'), _('File Transfer Stopped')):
|
||||
ntype = 'transfer.complete'
|
||||
elif event_type == _('New E-mail'):
|
||||
ntype = 'gmail.notify'
|
||||
ntype = 'email.arrived'
|
||||
elif event_type == _('Groupchat Invitation'):
|
||||
ntype = 'im.invitation'
|
||||
else:
|
||||
# default failsafe values
|
||||
path_to_image = os.path.abspath(
|
||||
self.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
|
||||
|
@ -133,42 +157,70 @@ class DesktopNotification:
|
|||
self.notif = dbus_support.get_notifications_interface()
|
||||
if self.notif is None:
|
||||
raise dbus.dbus_bindings.DBusException()
|
||||
self.ntype = ntype
|
||||
|
||||
self.get_version()
|
||||
|
||||
def attempt_notify(self):
|
||||
version = self.version
|
||||
timeout = gajim.config.get('notification_timeout') # in seconds
|
||||
# Determine the version of notifications
|
||||
# FIXME: This code is blocking, as is the next set. That should be fixed
|
||||
# now that we have a class to encapsulate this behavior
|
||||
try:
|
||||
(name, vendor, version) = self.notif.GetServerInfo()
|
||||
except:
|
||||
# No way to determine the version number, set it to the latest
|
||||
# since it doesn't properly support the version number
|
||||
try:
|
||||
(name, version, version, spec_version) = self.notif.GetServerInformation()
|
||||
except:
|
||||
version = '0.3.1'
|
||||
ntype = self.ntype
|
||||
if version.startswith('0.2'):
|
||||
try:
|
||||
self.id = self.notif.Notify(dbus.String(_('Gajim')),
|
||||
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))
|
||||
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)
|
||||
except AttributeError:
|
||||
version = '0.3.1' # we're actually dealing with the newer version
|
||||
if version.startswith('0.3'):
|
||||
if version >= ( 0, 3, 2):
|
||||
self.id = self.notif.Notify(dbus.String(_('Gajim')),
|
||||
dbus.UInt32(0), dbus.String(path_to_image),
|
||||
dbus.String(event_type), dbus.String(text),
|
||||
(dbus.String('default'), dbus.String(event_type)), {},
|
||||
dbus.UInt32(timeout*1000))
|
||||
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),
|
||||
( dbus.String('default'), dbus.String(self.event_type) ),
|
||||
hints,
|
||||
dbus.UInt32(timeout*1000),
|
||||
reply_handler=self.attach_by_id,
|
||||
error_handler=self.notify_another_way)
|
||||
else:
|
||||
self.id = self.notif.Notify(dbus.String(_('Gajim')),
|
||||
dbus.String(path_to_image), dbus.UInt32(0),
|
||||
dbus.String(event_type), dbus.String(text), dbus.String(''),
|
||||
{}, dbus.UInt32(timeout*1000))
|
||||
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
|
||||
notification_response_manager.attach_to_interface()
|
||||
notification_response_manager.pending[self.id] = self
|
||||
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')
|
||||
|
||||
def on_action_invoked(self, id, reason):
|
||||
if self.notif is None:
|
||||
|
@ -178,3 +230,19 @@ class DesktopNotification:
|
|||
if not self.msg_type:
|
||||
self.msg_type = 'chat'
|
||||
gajim.interface.handle_event(self.account, self.jid, self.msg_type)
|
||||
|
||||
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()
|
||||
|
|
Loading…
Reference in New Issue