Commit 5dc9ad67 authored by Emmanuel Gil Peyrot's avatar Emmanuel Gil Peyrot
Browse files

notify: Replace the custom implementation with GNotification.

parent ec1d75f1
Pipeline #149 passed with stages
in 2 minutes
......@@ -74,7 +74,6 @@ class Config:
'autopopupaway': [ opt_bool, False ],
'autopopup_chat_opened': [ opt_bool, False, _('Show desktop notification even when a chat window is opened for this contact and does not have focus') ],
'sounddnd': [ opt_bool, False, _('Play sound when user is busy')],
'use_notif_daemon': [ opt_bool, True, _('Use D-Bus and Notification-Daemon to show notifications') ],
'showoffline': [ opt_bool, False ],
'show_only_chat_and_online': [ opt_bool, False, _('Show only online and free for chat contacts in roster.')],
'show_transports_group': [ opt_bool, True ],
......@@ -99,15 +98,6 @@ class Config:
'statusmsgcolor': [ opt_color, '#4e9a06', _('Status message text color.'), True ],
'markedmsgcolor': [ opt_color, '#ff8080', '', True ],
'urlmsgcolor': [ opt_color, '#204a87', '', True ],
'notif_signin_color': [ opt_color, '#32CD32', _('Contact signed in notification color.') ], # limegreen
'notif_signout_color': [ opt_color, '#FF0000', _('Contact signout notification color') ], # red
'notif_message_color': [ opt_color, '#1E90FF', _('New message/email notification color.') ], # dodgerblue
'notif_ftrequest_color': [ opt_color, '#F0E68C', _('File transfer request notification color.') ], # khaki
'notif_fterror_color': [ opt_color, '#B22222', _('File transfer error notification color.') ], # firebrick
'notif_ftcomplete_color': [ opt_color, '#9ACD32', _('File transfer complete or stopped notification color.') ], # yellowgreen
'notif_invite_color': [ opt_color, '#D2B48C', _('Groupchat invitation notification color') ], # tan1
'notif_status_color': [ opt_color, '#D8BFD8', _('Background color of status changed notification') ], # thistle2
'notif_other_color': [ opt_color, '#FFFFFF', _('Other dialogs color.') ], # white
'inmsgfont': [ opt_str, '', _('Incoming nickname font.'), True ],
'outmsgfont': [ opt_str, '', _('Outgoing nickname font.'), True ],
'inmsgtxtfont': [ opt_str, '', _('Incoming text font.'), True ],
......@@ -243,10 +233,6 @@ class Config:
'vcard_avatar_width': [opt_int, 200],
'vcard_avatar_height': [opt_int, 200],
'notification_preview_message': [opt_bool, True, _('Preview new messages in notification popup?')],
'notification_position_x': [opt_int, -1],
'notification_position_y': [opt_int, -1],
'notification_avatar_width': [opt_int, 48],
'notification_avatar_height': [opt_int, 48],
'muc_highlight_words': [opt_str, '', _('A semicolon-separated list of words that will be highlighted in group chats.')],
'quit_on_roster_x_button': [opt_bool, False, _('If True, quits Gajim when X button of Window Manager is clicked. This setting is taken into account only if notification icon is used.')],
'show_unread_tab_icon': [opt_bool, False, _('If True, Gajim will display an icon on each tab containing unread messages. Depending on the theme, this icon may be animated.')],
......@@ -269,7 +255,6 @@ class Config:
'hide_avatar_of_transport': [opt_bool, False, _('Don\'t show avatar for the transport itself.')],
'roster_window_skip_taskbar': [opt_bool, False, _('Don\'t show roster in the system taskbar.')],
'use_urgency_hint': [opt_bool, True, _('If True and installed GTK+ and PyGTK versions are at least 2.8, make the window flash (the default behaviour in most Window Managers) when holding pending events.')],
'notification_timeout': [opt_int, 5],
'send_sha_in_gc_presence': [opt_bool, True, _('Jabberd1.4 does not like sha info when one join a password protected group chat. Turn this option to False to stop sending sha info in group chat presences.')],
'one_message_window': [opt_str, 'always',
#always, never, peracct, pertype should not be translated
......
......@@ -159,27 +159,6 @@ def get_interface(interface, path, start_service=True):
return None
def get_notifications_interface(notif=None):
"""
Get the notifications interface
:param notif: DesktopNotification instance
"""
# try to see if KDE notifications are available
iface = get_interface('org.kde.VisualNotifications', '/VisualNotifications',
start_service=False)
if iface != None:
if notif != None:
notif.kde_notifications = True
return iface
# KDE notifications don't seem to be available, falling back to
# notification-daemon
else:
if notif != None:
notif.kde_notifications = False
return get_interface('org.freedesktop.Notifications',
'/org/freedesktop/Notifications')
if supported:
class MissingArgument(dbus.DBusException):
_dbus_error_name = _GAJIM_ERROR_IFACE + '.MissingArgument'
......
......@@ -2937,123 +2937,6 @@ class ChangePasswordDialog:
dialog.destroy()
self.on_response(password1)
class PopupNotificationWindow:
def __init__(self, event_type, jid, account, msg_type='',
path_to_image=None, title=None, text=None, timeout=-1):
self.account = account
self.jid = jid
self.msg_type = msg_type
self.index = len(app.interface.roster.popup_notification_windows)
xml = gtkgui_helpers.get_gtk_builder('popup_notification_window.ui')
self.window = xml.get_object('popup_notification_window')
self.window.set_type_hint(Gdk.WindowTypeHint.TOOLTIP)
close_button = xml.get_object('close_button')
event_type_label = xml.get_object('event_type_label')
event_description_label = xml.get_object('event_description_label')
eventbox = xml.get_object('eventbox')
image = xml.get_object('notification_image')
if not text:
text = app.get_name_from_jid(account, jid) # default value of text
if not title:
title = ''
event_type_label.set_markup(
'<span foreground="black" weight="bold">%s</span>' %
GLib.markup_escape_text(title))
# set colors [ http://www.pitt.edu/~nisg/cis/web/cgi/rgb.html ]
color = Gdk.RGBA()
Gdk.RGBA.parse(color, 'black')
self.window.override_background_color(Gtk.StateType.NORMAL, color)
# default image
if not path_to_image:
path_to_image = gtkgui_helpers.get_icon_path('gajim-chat_msg_recv', 48)
if event_type == _('Contact Signed In'):
bg_color = app.config.get('notif_signin_color')
elif event_type == _('Contact Signed Out'):
bg_color = app.config.get('notif_signout_color')
elif event_type in (_('New Message'), _('New Single Message'),
_('New Private Message'), _('New E-mail')):
bg_color = app.config.get('notif_message_color')
elif event_type == _('File Transfer Request'):
bg_color = app.config.get('notif_ftrequest_color')
elif event_type == _('File Transfer Error'):
bg_color = app.config.get('notif_fterror_color')
elif event_type in (_('File Transfer Completed'),
_('File Transfer Stopped')):
bg_color = app.config.get('notif_ftcomplete_color')
elif event_type == _('Groupchat Invitation'):
bg_color = app.config.get('notif_invite_color')
elif event_type == _('Contact Changed Status'):
bg_color = app.config.get('notif_status_color')
else: # Unknown event! Shouldn't happen but deal with it
bg_color = app.config.get('notif_other_color')
popup_bg_color = Gdk.RGBA()
Gdk.RGBA.parse(popup_bg_color, bg_color)
close_button.override_background_color(Gtk.StateType.NORMAL,
popup_bg_color)
eventbox.override_background_color(Gtk.StateType.NORMAL, popup_bg_color)
event_description_label.set_markup('<span foreground="black">%s</span>' %
GLib.markup_escape_text(text))
# set the image
image.set_from_file(path_to_image)
# position the window to bottom-right of screen
window_width, self.window_height = self.window.get_size()
app.interface.roster.popups_notification_height += self.window_height
pos_x = app.config.get('notification_position_x')
if pos_x < 0:
pos_x = Gdk.Screen.width() - window_width + pos_x + 1
pos_y = app.config.get('notification_position_y')
if pos_y < 0:
pos_y = Gdk.Screen.height() - \
app.interface.roster.popups_notification_height + pos_y + 1
self.window.move(pos_x, pos_y)
xml.connect_signals(self)
self.window.show_all()
if timeout > 0:
GLib.timeout_add_seconds(timeout, self.on_timeout)
def on_close_button_clicked(self, widget):
self.adjust_height_and_move_popup_notification_windows()
def on_timeout(self):
self.adjust_height_and_move_popup_notification_windows()
def adjust_height_and_move_popup_notification_windows(self):
#remove
app.interface.roster.popups_notification_height -= self.window_height
self.window.destroy()
if len(app.interface.roster.popup_notification_windows) > self.index:
# we want to remove the destroyed window from the list
app.interface.roster.popup_notification_windows.pop(self.index)
# move the rest of popup windows
app.interface.roster.popups_notification_height = 0
current_index = 0
for window_instance in app.interface.roster.popup_notification_windows:
window_instance.index = current_index
current_index += 1
window_width, window_height = window_instance.window.get_size()
app.interface.roster.popups_notification_height += window_height
window_instance.window.move(Gdk.Screen.width() - window_width,
Gdk.Screen.height() - \
app.interface.roster.popups_notification_height)
def on_popup_notification_window_button_press_event(self, widget, event):
if event.button != 1:
self.window.destroy()
return
app.interface.handle_event(self.account, self.jid, self.msg_type)
self.adjust_height_and_move_popup_notification_windows()
class SingleMessageWindow:
"""
SingleMessageWindow can send or show a received singled message depending on
......
......@@ -73,10 +73,6 @@ class FeaturesWindow:
_('Spellchecking of composed messages.'),
_('Requires libgtkspell.'),
_('Requires libgtkspell and libenchant.')),
_('Notification'): (self.notification_available,
_('Passive popups notifying for new events.'),
_('Requires python-notify or instead python-dbus in conjunction with notification-daemon.'),
_('Feature not available under Windows.')),
_('Automatic status'): (self.idle_available,
_('Ability to measure idle time, in order to set auto status.'),
_('Requires libxss library.'),
......@@ -199,18 +195,6 @@ class FeaturesWindow:
return False
return True
def notification_available(self):
if os.name == 'nt':
return False
from gajim.common import dbus_support
if self.dbus_available() and dbus_support.get_notifications_interface():
return True
try:
__import__('pynotify')
except Exception:
return False
return True
def idle_available(self):
from gajim.common import sleepy
return sleepy.SUPPORTED
......
......@@ -2747,7 +2747,6 @@ class Interface:
# Creating Network Events Controller
from gajim.common import nec
app.nec = nec.NetworkEventsController()
app.notification = notify.Notification()
self.create_core_handlers_list()
self.register_core_handlers()
......
......@@ -27,30 +27,10 @@
## along with Gajim. If not, see <http://www.gnu.org/licenses/>.
##
import os
import time
from gajim.dialogs import PopupNotificationWindow
from gi.repository import GObject
from gi.repository import GLib
from gi.repository import Gio
from gajim import gtkgui_helpers
from gajim.common import app
from gajim.common import helpers
from gajim.common import ged
from gajim.common import dbus_support
if dbus_support.supported:
import dbus
USER_HAS_PYNOTIFY = True # user has pynotify module
try:
import gi
gi.require_version('Notify', '0.7')
from gi.repository import Notify
Notify.init('Gajim Notification')
except ValueError:
USER_HAS_PYNOTIFY = False
def get_show_in_roster(event, account, contact, session=None):
"""
......@@ -75,406 +55,28 @@ def get_show_in_systray(event, account, contact, type_=None):
return app.config.get('trayicon_notification_on_events')
def popup(event_type, jid, account, msg_type='', path_to_image=None, title=None,
text=None, timeout=-1):
text=None):
"""
Notify 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
Notify the user of an event using GNotification and GApplication.
"""
# TODO: Remove unused arguments.
# default image
if not path_to_image:
path_to_image = gtkgui_helpers.get_icon_path('gajim-chat_msg_recv', 48)
if timeout < 0:
timeout = app.config.get('notification_timeout')
# Try to show our popup via D-Bus and notification daemon
if app.config.get('use_notif_daemon') and dbus_support.supported:
try:
DesktopNotification(event_type, jid, account, msg_type,
path_to_image, title, GLib.markup_escape_text(text), timeout)
return # sucessfully did D-Bus Notification procedure!
except dbus.DBusException as e:
# Connection to D-Bus failed
app.log.debug(str(e))
except TypeError as e:
# This means that we sent the message incorrectly
app.log.debug(str(e))
# Ok, that failed. Let's try pynotify, which also uses notification daemon
if app.config.get('use_notif_daemon') and USER_HAS_PYNOTIFY:
if not text and event_type == 'new_message':
# empty text for new_message means do_preview = False
# -> default value for text
_text = GLib.markup_escape_text(app.get_name_from_jid(account,
jid))
else:
_text = GLib.markup_escape_text(text)
if not title:
_title = ''
else:
_title = title
notification = Notify.Notification.new(_title, _text)
notification.set_timeout(timeout*1000)
notification.set_category(event_type)
notification._data = {}
notification._data["event_type"] = event_type
notification._data["jid"] = jid
notification._data["account"] = account
notification._data["msg_type"] = msg_type
notification.set_property('icon-name', path_to_image)
if 'actions' in Notify.get_server_caps():
notification.add_action('default', 'Default Action',
on_pynotify_notification_clicked)
try:
notification.show()
return
except GObject.GError as e:
# Connection to notification-daemon failed, see #2893
app.log.debug(str(e))
# Either nothing succeeded or the user wants old-style notifications
instance = PopupNotificationWindow(event_type, jid, account, msg_type,
path_to_image, title, text, timeout)
app.interface.roster.popup_notification_windows.append(instance)
def on_pynotify_notification_clicked(notification, action):
jid = notification._data.jid
account = notification._data.account
msg_type = notification._data.msg_type
notification.close()
app.interface.handle_event(account, jid, msg_type)
class Notification:
"""
Handle notifications
"""
def __init__(self):
app.ged.register_event_handler('notification', ged.GUI2,
self._nec_notification)
def _nec_notification(self, obj):
if obj.do_popup:
if obj.popup_image:
icon_path = gtkgui_helpers.get_icon_path(obj.popup_image, 48)
if icon_path:
image_path = icon_path
elif obj.popup_image_path:
image_path = obj.popup_image_path
else:
image_path = ''
popup(obj.popup_event_type, obj.jid, obj.conn.name,
obj.popup_msg_type, path_to_image=image_path,
title=obj.popup_title, text=obj.popup_text,
timeout=obj.popup_timeout)
if obj.do_sound:
if obj.sound_file:
helpers.play_sound_file(obj.sound_file)
elif obj.sound_event:
helpers.play_sound(obj.sound_event)
if obj.do_command:
try:
helpers.exec_command(obj.command, use_shell=True)
except Exception:
pass
class NotificationResponseManager:
"""
Collect 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.received = []
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)
def on_action_invoked(self, id_, reason):
if id_ in self.pending:
notification = self.pending[id_]
notification.on_action_invoked(id_, reason)
del self.pending[id_]
return
# got an action on popup that isn't handled yet? Maybe user clicked too
# fast. Remember it.
self.received.append((id_, time.time(), 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 id_ in self.pending:
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!
app.log.debug('Duplicate ID of notification. Can\'t handle this.')
notification_response_manager = NotificationResponseManager()
class DesktopNotification:
"""
A DesktopNotification that interfaces with D-Bus via the Desktop
Notification Specification
"""
def __init__(self, event_type, jid, account, msg_type='',
path_to_image=None, title=None, text=None, timeout=-1):
self.path_to_image = os.path.abspath(path_to_image)
self.event_type = event_type
self.title = title
self.text = text
self.timeout = timeout
# 0.3.1 is the only version of notification daemon that has no way
# to determine which version it is. If no method exists, it means
# they're using that one.
self.default_version = [0, 3, 1]
self.account = account
self.jid = jid
self.msg_type = msg_type
# default value of text
if not text and event_type == 'new_message':
# empty text for new_message means do_preview = False
self.text = app.get_name_from_jid(account, jid)
if not title:
self.title = event_type # default value
if event_type == _('Contact Signed In'):
ntype = 'presence.online'
elif event_type == _('Contact Signed Out'):
ntype = 'presence.offline'
elif event_type in (_('New Message'), _('New Single Message'),
_('New Private Message')):
ntype = 'im.received'
elif event_type == _('File Transfer Request'):
ntype = 'transfer'
elif event_type == _('File Transfer Error'):
ntype = 'transfer.error'
elif event_type in (_('File Transfer Completed'),
_('File Transfer Stopped')):
ntype = 'transfer.complete'
elif event_type == _('New E-mail'):
ntype = 'email.arrived'
elif event_type == _('Groupchat Invitation'):
ntype = 'im.invitation'
elif event_type == _('Contact Changed Status'):
ntype = 'presence.status'
elif event_type == _('Connection Failed'):
ntype = 'connection.failed'
elif event_type == _('Subscription request'):
ntype = 'subscription.request'
elif event_type == _('Unsubscribed'):
ntype = 'unsubscribed'
else:
# default failsafe values
self.path_to_image = gtkgui_helpers.get_icon_path(
'gajim-chat_msg_recv', 48)
ntype = 'im' # Notification Type
self.notif = dbus_support.get_notifications_interface(self)
if self.notif is None:
raise dbus.DBusException('unable to get notifications interface')
self.ntype = ntype
if self.kde_notifications:
self.attempt_notify()
else:
self.capabilities = self.notif.GetCapabilities()
if self.capabilities is None:
self.capabilities = ['actions']
self.get_version()
def attempt_notify(self):
ntype = self.ntype
if self.kde_notifications:
notification_text = ('<html><img src="%(image)s" align=left />' \
'%(title)s<br/>%(text)s</html>') % {'title': self.title,
'text': self.text, 'image': self.path_to_image}
gajim_icon = gtkgui_helpers.get_icon_path('org.gajim.Gajim', 48)
try:
self.notif.Notify(
dbus.String(_('Gajim')), # app_name (string)
dbus.UInt32(0), # replaces_id (uint)
ntype, # event_id (string)
dbus.String(gajim_icon), # app_icon (string)
dbus.String(''), # summary (string)
dbus.String(notification_text), # body (string)
# actions (stringlist)
(dbus.String('default'), dbus.String(self.event_type),
dbus.String('ignore'), dbus.String(_('Ignore'))),
[], # hints (not used in KDE yet)
dbus.UInt32(self.timeout*1000), # timeout (int), in ms
reply_handler=self.attach_by_id,
error_handler=self.notify_another_way)
return
except Exception:
pass
version = self.version
if version[:2] == [0, 2]:
actions = {}
if 'actions' in self.capabilities and self.msg_type:
actions = {'default': 0}
try:
self.notif.Notify(
dbus.String(_('Gajim')),
dbus.String(self.path_to_image),
dbus.UInt32(0),
ntype,
dbus.Byte(0),
dbus.String(self.title),
dbus.String(self.text),
[dbus.String(self.path_to_image)],
actions,
[''],
True,
dbus.UInt32(self.timeout),
reply_handler=self.attach_by_id,
error_handler=self.notify_another_way)
except AttributeError:
# we're actually dealing with the newer version
version = [0, 3, 1]
if version > [0, 3]:
if app.interface.systray_enabled and \
app.config.get('attach_notifications_to_systray'):
status_icon = app.interface.systray.status_icon
rect = status_icon.get_geometry()[2]
x, y, width, height = rect.x, rect.y, rect.width, rect.height
pos_x = x + (width / 2)
pos_y = y + (height / 2)
hints = {'x': pos_x, 'y': pos_y}
else:
hints = {}
if version >= [0, 3, 2]:
hints['urgency'] = dbus.Byte(0) # Low Urgency
hints['category'] = dbus.String(ntype)
# it seems notification-daemon doesn't like empty text
if self.text:
text = self.text
if len(self.text) > 200:
text = '%s\n…' % self.text[:200]
else:
text = ' '
if os.environ.get('KDE_FULL_SESSION') == 'true':
text = '<table style=\'padding: 3px\'><tr><td>' \
'<img src=\"%s\"></td><td width=20> </td>' \
'<td>%s</td></tr></table>' % (self.path_to_image,
text)
self.path_to_image = os.path.abspath(
gtkgui_helpers.get_icon_path('org.gajim.Gajim', 48))
actions = ()
if 'actions' in self.capabilities and self.msg_type:
actions = (dbus.String('default'), dbus.String(
self.event_type))
try:
self.notif.Notify(
dbus.String(_('Gajim')),
# this notification does not replace other
dbus.UInt32(0),
dbus.String(self.path_to_image),