[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 os
|
||||||
|
import time
|
||||||
import dialogs
|
import dialogs
|
||||||
import gobject
|
import gobject
|
||||||
|
|
||||||
|
@ -69,6 +70,7 @@ class NotificationResponseManager:
|
||||||
a signal from an interface once it's connected.'''
|
a signal from an interface once it's connected.'''
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.pending = {}
|
self.pending = {}
|
||||||
|
self.received = []
|
||||||
self.interface = None
|
self.interface = None
|
||||||
|
|
||||||
def attach_to_interface(self):
|
def attach_to_interface(self):
|
||||||
|
@ -79,19 +81,37 @@ class NotificationResponseManager:
|
||||||
self.interface.connect_to_signal('NotificationClosed', self.on_closed)
|
self.interface.connect_to_signal('NotificationClosed', self.on_closed)
|
||||||
|
|
||||||
def on_action_invoked(self, id, reason):
|
def on_action_invoked(self, id, reason):
|
||||||
|
self.received.append((id, time.time(), reason))
|
||||||
if self.pending.has_key(id):
|
if self.pending.has_key(id):
|
||||||
notification = self.pending[id]
|
notification = self.pending[id]
|
||||||
notification.on_action_invoked(id, reason)
|
notification.on_action_invoked(id, reason)
|
||||||
del self.pending[id]
|
del self.pending[id]
|
||||||
else:
|
if len(self.received) > 20:
|
||||||
# This happens in the case of a race condition where the user clicks
|
curt = time.time()
|
||||||
# on a popup before the program finishes registering this callback
|
for rec in self.received:
|
||||||
gobject.timeout_add(1000, self.on_action_invoked, id, reason)
|
diff = curt - rec[1]
|
||||||
|
if diff > 10:
|
||||||
|
self.received.remove(rec)
|
||||||
|
|
||||||
def on_closed(self, id, reason = None):
|
def on_closed(self, id, reason = None):
|
||||||
if self.pending.has_key(id):
|
if self.pending.has_key(id):
|
||||||
del self.pending[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()
|
notification_response_manager = NotificationResponseManager()
|
||||||
|
|
||||||
class DesktopNotification:
|
class DesktopNotification:
|
||||||
|
@ -99,12 +119,16 @@ class DesktopNotification:
|
||||||
Notification specification'''
|
Notification specification'''
|
||||||
def __init__(self, event_type, jid, account, msg_type = '',
|
def __init__(self, event_type, jid, account, msg_type = '',
|
||||||
path_to_image = None, text = None):
|
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.account = account
|
||||||
self.jid = jid
|
self.jid = jid
|
||||||
self.msg_type = msg_type
|
self.msg_type = msg_type
|
||||||
|
|
||||||
if not text:
|
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'):
|
if event_type == _('Contact Signed In'):
|
||||||
ntype = 'presence.online'
|
ntype = 'presence.online'
|
||||||
|
@ -120,12 +144,12 @@ class DesktopNotification:
|
||||||
elif event_type in (_('File Transfer Completed'), _('File Transfer Stopped')):
|
elif event_type in (_('File Transfer Completed'), _('File Transfer Stopped')):
|
||||||
ntype = 'transfer.complete'
|
ntype = 'transfer.complete'
|
||||||
elif event_type == _('New E-mail'):
|
elif event_type == _('New E-mail'):
|
||||||
ntype = 'gmail.notify'
|
ntype = 'email.arrived'
|
||||||
elif event_type == _('Groupchat Invitation'):
|
elif event_type == _('Groupchat Invitation'):
|
||||||
ntype = 'im.invitation'
|
ntype = 'im.invitation'
|
||||||
else:
|
else:
|
||||||
# default failsafe values
|
# default failsafe values
|
||||||
path_to_image = os.path.abspath(
|
self.path_to_image = os.path.abspath(
|
||||||
os.path.join(gajim.DATA_DIR, 'pixmaps', 'events',
|
os.path.join(gajim.DATA_DIR, 'pixmaps', 'events',
|
||||||
'chat_msg_recv.png')) # img to display
|
'chat_msg_recv.png')) # img to display
|
||||||
ntype = 'im' # Notification Type
|
ntype = 'im' # Notification Type
|
||||||
|
@ -133,42 +157,70 @@ class DesktopNotification:
|
||||||
self.notif = dbus_support.get_notifications_interface()
|
self.notif = dbus_support.get_notifications_interface()
|
||||||
if self.notif is None:
|
if self.notif is None:
|
||||||
raise dbus.dbus_bindings.DBusException()
|
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
|
timeout = gajim.config.get('notification_timeout') # in seconds
|
||||||
# Determine the version of notifications
|
ntype = self.ntype
|
||||||
# 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'
|
|
||||||
if version.startswith('0.2'):
|
if version.startswith('0.2'):
|
||||||
try:
|
try:
|
||||||
self.id = self.notif.Notify(dbus.String(_('Gajim')),
|
self.notif.Notify(
|
||||||
dbus.String(path_to_image), dbus.UInt32(0), ntype, dbus.Byte(0),
|
dbus.String(_('Gajim')),
|
||||||
dbus.String(event_type), dbus.String(text),
|
dbus.String(self.path_to_image),
|
||||||
[dbus.String(path_to_image)], {'default': 0}, [''], True,
|
dbus.UInt32(0),
|
||||||
dbus.UInt32(timeout))
|
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:
|
except AttributeError:
|
||||||
version = '0.3.1' # we're actually dealing with the newer version
|
version = '0.3.1' # we're actually dealing with the newer version
|
||||||
if version.startswith('0.3'):
|
if version.startswith('0.3'):
|
||||||
if version >= ( 0, 3, 2):
|
if version >= ( 0, 3, 2):
|
||||||
self.id = self.notif.Notify(dbus.String(_('Gajim')),
|
hints = {}
|
||||||
dbus.UInt32(0), dbus.String(path_to_image),
|
hints['urgency'] = dbus.Byte(0)
|
||||||
dbus.String(event_type), dbus.String(text),
|
hints['category'] = dbus.String(ntype)
|
||||||
(dbus.String('default'), dbus.String(event_type)), {},
|
self.notif.Notify(
|
||||||
dbus.UInt32(timeout*1000))
|
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:
|
else:
|
||||||
self.id = self.notif.Notify(dbus.String(_('Gajim')),
|
self.notif.Notify(
|
||||||
dbus.String(path_to_image), dbus.UInt32(0),
|
dbus.String(_('Gajim')),
|
||||||
dbus.String(event_type), dbus.String(text), dbus.String(''),
|
dbus.String(self.path_to_image),
|
||||||
{}, dbus.UInt32(timeout*1000))
|
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.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):
|
def on_action_invoked(self, id, reason):
|
||||||
if self.notif is None:
|
if self.notif is None:
|
||||||
|
@ -178,3 +230,19 @@ class DesktopNotification:
|
||||||
if not self.msg_type:
|
if not self.msg_type:
|
||||||
self.msg_type = 'chat'
|
self.msg_type = 'chat'
|
||||||
gajim.interface.handle_event(self.account, self.jid, self.msg_type)
|
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