2008-08-15 19:31:51 +02:00
|
|
|
# -*- coding:utf-8 -*-
|
2008-08-15 05:20:23 +02:00
|
|
|
## src/notify.py
|
2005-11-11 20:12:02 +01:00
|
|
|
##
|
2008-08-15 05:20:23 +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>
|
2014-01-02 09:33:54 +01:00
|
|
|
## Copyright (C) 2005-2014 Yann Leboulanger <asterix AT lagaule.org>
|
2008-08-15 05:20:23 +02:00
|
|
|
## 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>
|
2005-11-11 20:12:02 +01:00
|
|
|
##
|
2007-10-22 13:13:13 +02:00
|
|
|
## This file is part of Gajim.
|
|
|
|
##
|
|
|
|
## Gajim is free software; you can redistribute it and/or modify
|
2005-11-11 20:12:02 +01:00
|
|
|
## it under the terms of the GNU General Public License as published
|
2007-10-22 13:13:13 +02:00
|
|
|
## by the Free Software Foundation; version 3 only.
|
2005-11-11 20:12:02 +01:00
|
|
|
##
|
2007-10-22 13:13:13 +02:00
|
|
|
## Gajim is distributed in the hope that it will be useful,
|
2005-11-11 20:12:02 +01:00
|
|
|
## but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2008-08-15 05:20:23 +02:00
|
|
|
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2005-11-11 20:12:02 +01:00
|
|
|
## GNU General Public License for more details.
|
|
|
|
##
|
2007-10-22 13:13:13 +02:00
|
|
|
## You should have received a copy of the GNU General Public License
|
2008-08-15 05:20:23 +02:00
|
|
|
## along with Gajim. If not, see <http://www.gnu.org/licenses/>.
|
2007-10-22 13:13:13 +02:00
|
|
|
##
|
2005-11-11 20:12:02 +01:00
|
|
|
|
2017-08-30 20:43:50 +02:00
|
|
|
import sys
|
2017-06-13 23:58:06 +02:00
|
|
|
from gajim.dialogs import PopupNotificationWindow
|
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
|
2017-06-13 23:58:06 +02:00
|
|
|
from gajim import gtkgui_helpers
|
2005-11-11 20:12:02 +01:00
|
|
|
|
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
|
2005-11-11 20:12:02 +01:00
|
|
|
|
2008-09-22 11:10:18 +02:00
|
|
|
def get_show_in_roster(event, account, contact, session=None):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Return True if this event must be shown in roster, else False
|
|
|
|
"""
|
|
|
|
if event == 'gc_message_received':
|
|
|
|
return True
|
|
|
|
if event == 'message_received':
|
|
|
|
if session and session.control:
|
|
|
|
return False
|
|
|
|
return True
|
2006-09-02 23:01:11 +02:00
|
|
|
|
2008-02-27 22:33:50 +01:00
|
|
|
def get_show_in_systray(event, account, contact, type_=None):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Return True if this event must be shown in systray, else False
|
|
|
|
"""
|
2017-08-13 13:18:56 +02:00
|
|
|
if type_ == 'printed_gc_msg' and not app.config.get(
|
2017-08-20 23:07:58 +02:00
|
|
|
'notify_on_all_muc_messages') and not app.config.get_per('rooms',
|
|
|
|
contact.jid, 'notify_on_all_messages'):
|
2010-02-08 15:08:40 +01:00
|
|
|
# it's not an highlighted message, don't show in systray
|
|
|
|
return False
|
2017-08-13 13:18:56 +02:00
|
|
|
return app.config.get('trayicon_notification_on_events')
|
2006-09-02 23:01:11 +02:00
|
|
|
|
2017-08-30 20:43:50 +02:00
|
|
|
def popup(event_type, jid, account, type_='', path_to_image=None, title=None,
|
2012-04-09 13:38:28 +02:00
|
|
|
text=None, timeout=-1):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
2017-08-30 20:43:50 +02:00
|
|
|
Notify a user of an event using GNotification and GApplication under linux,
|
|
|
|
the older style PopupNotificationWindow method under windows
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
# default image
|
|
|
|
if not path_to_image:
|
|
|
|
path_to_image = gtkgui_helpers.get_icon_path('gajim-chat_msg_recv', 48)
|
|
|
|
|
2012-04-09 13:38:28 +02:00
|
|
|
if timeout < 0:
|
2017-08-13 13:18:56 +02:00
|
|
|
timeout = app.config.get('notification_timeout')
|
2012-04-09 13:38:28 +02:00
|
|
|
|
2017-08-30 20:43:50 +02:00
|
|
|
if sys.platform == 'win32':
|
|
|
|
instance = PopupNotificationWindow(event_type, jid, account, type_,
|
|
|
|
path_to_image, title, text, timeout)
|
|
|
|
app.interface.roster.popup_notification_windows.append(instance)
|
|
|
|
return
|
|
|
|
|
|
|
|
# use GNotification
|
|
|
|
# TODO: Move to standard GTK+ icons here.
|
|
|
|
icon = Gio.FileIcon.new(Gio.File.new_for_path(path_to_image))
|
|
|
|
notification = Gio.Notification()
|
|
|
|
if title is not None:
|
|
|
|
notification.set_title(title)
|
|
|
|
if text is not None:
|
|
|
|
notification.set_body(text)
|
|
|
|
notification.set_icon(icon)
|
|
|
|
notif_id = None
|
|
|
|
if event_type in (_('Contact Signed In'), _('Contact Signed Out'),
|
|
|
|
_('New Message'), _('New Single Message'), _('New Private Message'),
|
|
|
|
_('Contact Changed Status'), _('File Transfer Request'),
|
|
|
|
_('File Transfer Error'), _('File Transfer Completed'),
|
|
|
|
_('File Transfer Stopped'), _('Groupchat Invitation'),
|
|
|
|
_('Connection Failed'), _('Subscription request'), _('Unsubscribed')):
|
|
|
|
# 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)
|
2018-01-11 15:12:07 +01:00
|
|
|
notification.add_button_with_target(_('Open'), action, variant_dict)
|
2017-08-30 20:43:50 +02:00
|
|
|
notification.set_default_action_and_target(action, variant_dict)
|
|
|
|
if event_type in (_('New Message'), _('New Single Message'),
|
|
|
|
_('New Private Message')):
|
|
|
|
# Only one notification per JID
|
|
|
|
notif_id = jid
|
|
|
|
notification.set_priority(Gio.NotificationPriority.NORMAL)
|
|
|
|
notification.set_urgent(False)
|
|
|
|
app.app.send_notification(notif_id, notification)
|
2006-09-28 02:31:59 +02:00
|
|
|
|
2005-11-11 20:12:02 +01:00
|
|
|
|
2011-05-01 22:09:50 +02:00
|
|
|
class Notification:
|
|
|
|
"""
|
|
|
|
Handle notifications
|
|
|
|
"""
|
|
|
|
def __init__(self):
|
2017-08-13 13:18:56 +02:00
|
|
|
app.ged.register_event_handler('notification', ged.GUI2,
|
2011-05-01 22:09:50 +02:00
|
|
|
self._nec_notification)
|
|
|
|
|
|
|
|
def _nec_notification(self, obj):
|
|
|
|
if obj.do_popup:
|
2013-02-18 20:52:04 +01:00
|
|
|
if obj.popup_image:
|
|
|
|
icon_path = gtkgui_helpers.get_icon_path(obj.popup_image, 48)
|
2013-01-05 17:08:07 +01:00
|
|
|
if icon_path:
|
2013-02-18 20:52:04 +01:00
|
|
|
image_path = icon_path
|
|
|
|
elif obj.popup_image_path:
|
|
|
|
image_path = obj.popup_image_path
|
|
|
|
else:
|
|
|
|
image_path = ''
|
2011-05-01 22:09:50 +02:00
|
|
|
popup(obj.popup_event_type, obj.jid, obj.conn.name,
|
2013-02-18 20:52:04 +01:00
|
|
|
obj.popup_msg_type, path_to_image=image_path,
|
2012-04-09 13:38:28 +02:00
|
|
|
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
|