2018-06-01 13:54:04 +02:00
|
|
|
# Copyright (C) 2005 Sebastian Estienne
|
|
|
|
# Copyright (C) 2005-2006 Andrew Sayman <lorien420 AT myrealbox.com>
|
|
|
|
# Copyright (C) 2005-2007 Nikos Kouremenos <kourem AT gmail.com>
|
|
|
|
# Copyright (C) 2005-2014 Yann Leboulanger <asterix AT lagaule.org>
|
|
|
|
# Copyright (C) 2006 Travis Shirk <travis AT pobox.com>
|
|
|
|
# Copyright (C) 2006-2008 Jean-Marie Traissard <jim AT lapin.org>
|
|
|
|
# Copyright (C) 2007 Julien Pivotto <roidelapluie AT gmail.com>
|
|
|
|
# Stephan Erb <steve-e AT h3c.de>
|
|
|
|
# Copyright (C) 2008 Brendan Taylor <whateley AT gmail.com>
|
|
|
|
# Jonathan Schleifer <js-gajim AT webkeks.org>
|
|
|
|
# Copyright (C) 2018 Philipp Hörist <philipp AT hoerist.com>
|
|
|
|
#
|
|
|
|
# This file is part of Gajim.
|
|
|
|
#
|
|
|
|
# Gajim 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 3 only.
|
|
|
|
#
|
|
|
|
# Gajim 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.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Gajim. If not, see <http://www.gnu.org/licenses/>.
|
2005-11-11 20:12:02 +01:00
|
|
|
|
2017-08-30 20:43:50 +02:00
|
|
|
import sys
|
2018-11-18 21:07:17 +01:00
|
|
|
import logging
|
2018-06-01 13:54:04 +02:00
|
|
|
|
2013-07-29 21:41:01 +02:00
|
|
|
from gi.repository import GLib
|
2017-08-30 20:43:50 +02:00
|
|
|
from gi.repository import Gio
|
2018-06-01 13:54:04 +02:00
|
|
|
from gi.repository import Gdk
|
|
|
|
from gi.repository import Gtk
|
2005-11-11 20:12:02 +01:00
|
|
|
|
2018-06-01 13:54:04 +02:00
|
|
|
from gajim import gtkgui_helpers
|
2017-08-13 13:18:56 +02:00
|
|
|
from gajim.common import app
|
2017-06-13 23:58:06 +02:00
|
|
|
from gajim.common import helpers
|
|
|
|
from gajim.common import ged
|
2018-10-04 23:55:35 +02:00
|
|
|
from gajim.common.i18n import _
|
2005-11-11 20:12:02 +01:00
|
|
|
|
2018-11-18 21:07:17 +01:00
|
|
|
from gajim.gtk.util import get_builder
|
2018-10-28 14:32:54 +01:00
|
|
|
from gajim.gtk.util import get_icon_name
|
2018-10-28 14:59:51 +01:00
|
|
|
from gajim.gtk.util import get_monitor_scale_factor
|
|
|
|
from gajim.gtk.util import get_total_screen_geometry
|
2018-11-18 18:03:08 +01:00
|
|
|
from gajim.gtk.util import load_icon
|
2018-10-28 14:32:54 +01:00
|
|
|
|
2018-11-18 21:07:17 +01:00
|
|
|
log = logging.getLogger('gajim.gtk.notification')
|
2018-08-16 22:52:20 +02:00
|
|
|
|
2018-06-01 13:54:04 +02:00
|
|
|
|
2011-05-01 22:09:50 +02:00
|
|
|
class Notification:
|
|
|
|
"""
|
|
|
|
Handle notifications
|
|
|
|
"""
|
|
|
|
def __init__(self):
|
2018-11-18 21:07:17 +01:00
|
|
|
self._daemon_capabilities = ['actions']
|
|
|
|
self._win32_active_popup = None
|
2018-08-16 22:52:20 +02:00
|
|
|
|
|
|
|
# Detect if actions are supported by the notification daemon
|
2018-11-10 19:28:31 +01:00
|
|
|
if sys.platform not in ('win32', 'darwin'):
|
2018-11-19 15:04:04 +01:00
|
|
|
def on_proxy_ready(_source, res, _data=None):
|
2018-08-16 22:52:20 +02:00
|
|
|
try:
|
|
|
|
proxy = Gio.DBusProxy.new_finish(res)
|
2018-11-18 21:07:17 +01:00
|
|
|
self._daemon_capabilities = proxy.GetCapabilities()
|
|
|
|
except GLib.Error as error:
|
|
|
|
if error.domain == 'g-dbus-error-quark':
|
2018-08-16 22:52:20 +02:00
|
|
|
log.info('Notifications D-Bus connection failed: %s',
|
2018-11-18 21:07:17 +01:00
|
|
|
error.message)
|
2018-08-16 22:52:20 +02:00
|
|
|
else:
|
|
|
|
raise
|
|
|
|
else:
|
|
|
|
log.debug('Notifications D-Bus connected')
|
|
|
|
|
|
|
|
log.debug('Connecting to Notifications D-Bus')
|
|
|
|
Gio.DBusProxy.new_for_bus(Gio.BusType.SESSION,
|
|
|
|
Gio.DBusProxyFlags.DO_NOT_CONNECT_SIGNALS,
|
|
|
|
None,
|
|
|
|
'org.freedesktop.Notifications',
|
|
|
|
'/org/freedesktop/Notifications',
|
|
|
|
'org.freedesktop.Notifications',
|
2018-11-18 21:07:17 +01:00
|
|
|
None,
|
|
|
|
on_proxy_ready)
|
2018-08-16 22:52:20 +02:00
|
|
|
|
2018-06-01 13:54:04 +02:00
|
|
|
app.ged.register_event_handler(
|
|
|
|
'notification', ged.GUI2, self._nec_notification)
|
2018-08-16 22:48:40 +02:00
|
|
|
app.ged.register_event_handler(
|
|
|
|
'our-show', ged.GUI2, self._nec_our_status)
|
|
|
|
app.events.event_removed_subscribe(self._on_event_removed)
|
2011-05-01 22:09:50 +02:00
|
|
|
|
|
|
|
def _nec_notification(self, obj):
|
|
|
|
if obj.do_popup:
|
2018-06-01 13:54:04 +02:00
|
|
|
icon_name = self._get_icon_name(obj)
|
|
|
|
self.popup(obj.popup_event_type, obj.jid, obj.conn.name,
|
|
|
|
obj.popup_msg_type, icon_name=icon_name,
|
|
|
|
title=obj.popup_title, text=obj.popup_text,
|
|
|
|
timeout=obj.popup_timeout)
|
2011-05-01 22:09:50 +02:00
|
|
|
|
|
|
|
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:
|
2011-11-08 22:41:07 +01:00
|
|
|
helpers.exec_command(obj.command, use_shell=True)
|
2011-05-01 22:09:50 +02:00
|
|
|
except Exception:
|
|
|
|
pass
|
2018-06-01 13:54:04 +02:00
|
|
|
|
2018-08-16 22:48:40 +02:00
|
|
|
def _on_event_removed(self, event_list):
|
|
|
|
for event in event_list:
|
|
|
|
if event.type_ == 'gc-invitation':
|
2018-12-25 20:30:36 +01:00
|
|
|
self._withdraw('gc-invitation', event.account, event.muc)
|
2018-08-16 22:48:40 +02:00
|
|
|
if event.type_ in ('normal', 'printed_chat', 'chat',
|
2018-12-15 13:22:58 +01:00
|
|
|
'printed_pm', 'pm', 'printed_marked_gc_msg',
|
|
|
|
'printed_gc_msg'):
|
2018-11-18 21:07:17 +01:00
|
|
|
self._withdraw('new-message', event.account, event.jid)
|
2018-08-16 22:48:40 +02:00
|
|
|
|
|
|
|
def _nec_our_status(self, obj):
|
|
|
|
if app.account_is_connected(obj.conn.name):
|
2018-11-18 21:07:17 +01:00
|
|
|
self._withdraw('connection-failed', obj.conn.name)
|
2018-08-16 22:48:40 +02:00
|
|
|
|
2018-11-18 21:07:17 +01:00
|
|
|
@staticmethod
|
|
|
|
def _get_icon_name(obj):
|
2018-06-01 13:54:04 +02:00
|
|
|
if obj.notif_type == 'msg':
|
|
|
|
if obj.base_event.mtype == 'pm':
|
|
|
|
return 'gajim-priv_msg_recv'
|
|
|
|
if obj.base_event.mtype == 'normal':
|
|
|
|
return 'gajim-single_msg_recv'
|
|
|
|
|
|
|
|
elif obj.notif_type == 'pres':
|
|
|
|
if obj.transport_name is not None:
|
|
|
|
return '%s-%s' % (obj.transport_name, obj.show)
|
2018-10-28 14:32:54 +01:00
|
|
|
return get_icon_name(obj.show)
|
2018-06-01 13:54:04 +02:00
|
|
|
|
|
|
|
def popup(self, event_type, jid, account, type_='', icon_name=None,
|
2018-08-24 22:03:33 +02:00
|
|
|
title=None, text=None, timeout=-1, room_jid=None):
|
2018-06-01 13:54:04 +02:00
|
|
|
"""
|
|
|
|
Notify a user of an event using GNotification and GApplication under
|
|
|
|
Linux, Use PopupNotificationWindow under Windows
|
|
|
|
"""
|
|
|
|
|
|
|
|
if icon_name is None:
|
|
|
|
icon_name = 'gajim-chat_msg_recv'
|
|
|
|
|
|
|
|
if timeout < 0:
|
|
|
|
timeout = app.config.get('notification_timeout')
|
|
|
|
|
|
|
|
if sys.platform == 'win32':
|
2018-11-18 21:07:17 +01:00
|
|
|
self._withdraw()
|
|
|
|
self._win32_active_popup = PopupNotification(
|
|
|
|
event_type, jid, account, type_,
|
|
|
|
icon_name, title, text, timeout)
|
|
|
|
self._win32_active_popup.connect('destroy', self._on_popup_destroy)
|
2018-06-01 13:54:04 +02:00
|
|
|
return
|
|
|
|
|
2018-10-28 14:59:51 +01:00
|
|
|
scale = get_monitor_scale_factor()
|
2018-11-18 18:03:08 +01:00
|
|
|
icon_pixbuf = load_icon(icon_name, size=48, pixbuf=True, scale=scale)
|
2018-06-01 13:54:04 +02:00
|
|
|
|
|
|
|
notification = Gio.Notification()
|
|
|
|
if title is not None:
|
|
|
|
notification.set_title(title)
|
|
|
|
if text is not None:
|
|
|
|
notification.set_body(text)
|
|
|
|
notif_id = None
|
2018-11-18 21:07:17 +01:00
|
|
|
if event_type in (
|
|
|
|
_('Contact Signed In'), _('Contact Signed Out'),
|
|
|
|
_('New Message'), _('New Single Message'), _('New Private Message'),
|
2018-12-15 13:22:58 +01:00
|
|
|
_('New Group Chat Message'),
|
2018-11-18 21:07:17 +01:00
|
|
|
_('Contact Changed Status'), _('File Transfer Request'),
|
|
|
|
_('File Transfer Error'), _('File Transfer Completed'),
|
|
|
|
_('File Transfer Stopped'), _('Groupchat Invitation'),
|
|
|
|
_('Connection Failed'), _('Subscription request'),
|
|
|
|
_('Unsubscribed')):
|
|
|
|
if 'actions' in self._daemon_capabilities:
|
2018-08-16 22:52:20 +02:00
|
|
|
# Create Variant Dict
|
|
|
|
dict_ = {'account': GLib.Variant('s', account),
|
|
|
|
'jid': GLib.Variant('s', jid),
|
|
|
|
'type_': GLib.Variant('s', type_)}
|
|
|
|
variant_dict = GLib.Variant('a{sv}', dict_)
|
|
|
|
action = 'app.{}-open-event'.format(account)
|
|
|
|
#Button in notification
|
|
|
|
notification.add_button_with_target(_('Open'), action,
|
|
|
|
variant_dict)
|
|
|
|
notification.set_default_action_and_target(action,
|
|
|
|
variant_dict)
|
2018-08-16 22:48:40 +02:00
|
|
|
|
|
|
|
# Only one notification per JID
|
2018-11-18 21:07:17 +01:00
|
|
|
if event_type in (_('Contact Signed In'),
|
|
|
|
_('Contact Signed Out'),
|
|
|
|
_('Contact Changed Status')):
|
|
|
|
notif_id = self._make_id('contact-status-changed', account, jid)
|
2018-08-16 22:48:40 +02:00
|
|
|
elif event_type == _('Groupchat Invitation'):
|
2018-11-18 21:07:17 +01:00
|
|
|
notif_id = self._make_id('gc-invitation', account, room_jid)
|
2018-08-16 22:48:40 +02:00
|
|
|
elif event_type == _('Connection Failed'):
|
2018-11-18 21:07:17 +01:00
|
|
|
notif_id = self._make_id('connection-failed', account)
|
|
|
|
elif event_type in (_('New Message'),
|
|
|
|
_('New Single Message'),
|
2018-12-15 13:22:58 +01:00
|
|
|
_('New Private Message'),
|
|
|
|
_('New Group Chat Message')):
|
2018-08-23 11:43:33 +02:00
|
|
|
avatar = app.contacts.get_avatar(account, jid)
|
|
|
|
if avatar:
|
|
|
|
icon_pixbuf = avatar
|
2018-11-18 21:07:17 +01:00
|
|
|
notif_id = self._make_id('new-message', account, jid)
|
2018-08-16 22:48:40 +02:00
|
|
|
|
2018-08-23 11:43:33 +02:00
|
|
|
notification.set_icon(icon_pixbuf)
|
2018-06-01 13:54:04 +02:00
|
|
|
notification.set_priority(Gio.NotificationPriority.NORMAL)
|
2018-08-23 11:43:33 +02:00
|
|
|
|
2018-06-01 13:54:04 +02:00
|
|
|
app.app.send_notification(notif_id, notification)
|
|
|
|
|
2018-11-18 21:07:17 +01:00
|
|
|
def _on_popup_destroy(self, *args):
|
|
|
|
self._win32_active_popup = None
|
2018-08-16 22:48:40 +02:00
|
|
|
|
2018-11-18 21:07:17 +01:00
|
|
|
def _withdraw(self, *args):
|
|
|
|
if sys.platform == 'win32':
|
|
|
|
if self._win32_active_popup is not None:
|
|
|
|
self._win32_active_popup.destroy()
|
|
|
|
else:
|
|
|
|
app.app.withdraw_notification(self._make_id(*args))
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _make_id(*args):
|
2018-08-16 22:48:40 +02:00
|
|
|
return ','.join(args)
|
2018-06-01 13:54:04 +02:00
|
|
|
|
2018-11-18 21:07:17 +01:00
|
|
|
|
|
|
|
class PopupNotification(Gtk.Window):
|
2018-06-01 13:54:04 +02:00
|
|
|
def __init__(self, event_type, jid, account, msg_type='',
|
|
|
|
icon_name=None, title=None, text=None, timeout=-1):
|
2018-11-18 21:07:17 +01:00
|
|
|
Gtk.Window.__init__(self)
|
|
|
|
self.set_type_hint(Gdk.WindowTypeHint.NOTIFICATION)
|
|
|
|
self.set_name('NotificationPopup')
|
|
|
|
self.set_skip_taskbar_hint(True)
|
|
|
|
self.set_decorated(False)
|
|
|
|
self.set_size_request(312, 95)
|
|
|
|
|
|
|
|
self._timeout_id = None
|
2018-06-01 13:54:04 +02:00
|
|
|
self.account = account
|
|
|
|
self.jid = jid
|
|
|
|
self.msg_type = msg_type
|
2018-11-18 21:07:17 +01:00
|
|
|
|
|
|
|
self._ui = get_builder('popup_notification_window.ui')
|
|
|
|
self.add(self._ui.eventbox)
|
2018-06-01 13:54:04 +02:00
|
|
|
|
|
|
|
if not text:
|
|
|
|
text = app.get_name_from_jid(account, jid) # default value of text
|
|
|
|
if not title:
|
|
|
|
title = ''
|
|
|
|
|
2018-11-18 21:07:17 +01:00
|
|
|
self._ui.event_type_label.set_markup(
|
2018-06-01 13:54:04 +02:00
|
|
|
'<span foreground="black" weight="bold">%s</span>' %
|
|
|
|
GLib.markup_escape_text(title))
|
|
|
|
|
|
|
|
css = '#NotificationPopup {background-color: black }'
|
2018-11-18 21:07:17 +01:00
|
|
|
gtkgui_helpers.add_css_to_widget(self, css)
|
2018-06-01 13:54:04 +02:00
|
|
|
|
|
|
|
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'),
|
2018-11-18 21:07:17 +01:00
|
|
|
_('New Private Message'), _('New E-mail')):
|
2018-06-01 13:54:04 +02:00
|
|
|
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'),
|
2018-11-18 21:07:17 +01:00
|
|
|
_('File Transfer Stopped')):
|
2018-06-01 13:54:04 +02:00
|
|
|
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')
|
|
|
|
|
|
|
|
background_class = '''
|
|
|
|
.popup-style {
|
|
|
|
border-image: none;
|
|
|
|
background-image: none;
|
|
|
|
background-color: %s }''' % bg_color
|
|
|
|
|
2018-11-18 21:07:17 +01:00
|
|
|
gtkgui_helpers.add_css_to_widget(self._ui.eventbox, background_class)
|
|
|
|
self._ui.eventbox.get_style_context().add_class('popup-style')
|
2018-06-01 13:54:04 +02:00
|
|
|
|
2018-11-18 21:07:17 +01:00
|
|
|
gtkgui_helpers.add_css_to_widget(
|
|
|
|
self._ui.close_button, background_class)
|
|
|
|
self._ui.close_button.get_style_context().add_class('popup-style')
|
2018-06-01 13:54:04 +02:00
|
|
|
|
2018-11-18 21:07:17 +01:00
|
|
|
escaped_text = GLib.markup_escape_text(text)
|
|
|
|
self._ui.event_description_label.set_markup(
|
|
|
|
'<span foreground="black">%s</span>' % escaped_text)
|
2018-06-01 13:54:04 +02:00
|
|
|
|
|
|
|
# set the image
|
2018-11-18 21:07:17 +01:00
|
|
|
self._ui.image.set_from_icon_name(icon_name, Gtk.IconSize.DIALOG)
|
2018-06-01 13:54:04 +02:00
|
|
|
|
2018-11-18 21:07:17 +01:00
|
|
|
self.move(*self._get_window_pos())
|
|
|
|
|
|
|
|
self._ui.connect_signals(self)
|
|
|
|
self.connect('button-press-event', self._on_button_press)
|
|
|
|
self.connect('destroy', self._on_destroy)
|
|
|
|
self.show_all()
|
|
|
|
if timeout > 0:
|
|
|
|
self._timeout_id = GLib.timeout_add_seconds(timeout, self.destroy)
|
|
|
|
|
|
|
|
def _get_window_pos(self):
|
2018-06-01 13:54:04 +02:00
|
|
|
pos_x = app.config.get('notification_position_x')
|
2018-10-28 14:59:51 +01:00
|
|
|
screen_w, screen_h = get_total_screen_geometry()
|
2018-06-01 13:54:04 +02:00
|
|
|
if pos_x < 0:
|
2018-11-18 21:07:17 +01:00
|
|
|
pos_x = screen_w - 312 + pos_x + 1
|
2018-06-01 13:54:04 +02:00
|
|
|
pos_y = app.config.get('notification_position_y')
|
|
|
|
if pos_y < 0:
|
2018-11-18 21:07:17 +01:00
|
|
|
pos_y = screen_h - 95 - 80 + pos_y + 1
|
|
|
|
return pos_x, pos_y
|
2018-06-01 13:54:04 +02:00
|
|
|
|
2018-11-18 21:07:17 +01:00
|
|
|
def _on_close_button_clicked(self, _widget):
|
|
|
|
self.destroy()
|
|
|
|
|
|
|
|
def _on_button_press(self, _widget, event):
|
|
|
|
if event.button == 1:
|
|
|
|
app.interface.handle_event(self.account, self.jid, self.msg_type)
|
|
|
|
self.destroy()
|
|
|
|
|
|
|
|
def _on_destroy(self, *args):
|
|
|
|
if self._timeout_id is not None:
|
|
|
|
GLib.source_remove(self._timeout_id)
|