notify.py 12.8 KB
Newer Older
Philipp Hörist's avatar
Philipp Hörist committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
#
# 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/>.
nkour's avatar
nkour committed
27

28
import sys
Philipp Hörist's avatar
Philipp Hörist committed
29

Yann Leboulanger's avatar
Yann Leboulanger committed
30
from gi.repository import GLib
31
from gi.repository import Gio
Philipp Hörist's avatar
Philipp Hörist committed
32 33
from gi.repository import Gdk
from gi.repository import Gtk
nkour's avatar
nkour committed
34

Philipp Hörist's avatar
Philipp Hörist committed
35
from gajim import gtkgui_helpers
36
from gajim.common import app
André's avatar
André committed
37 38
from gajim.common import helpers
from gajim.common import ged
nkour's avatar
nkour committed
39

Philipp Hörist's avatar
Philipp Hörist committed
40

41
def get_show_in_roster(event, account, jid, session=None):
42 43 44 45 46 47
    """
    Return True if this event must be shown in roster, else False
    """
    if event == 'gc_message_received':
        return True
    if event == 'message_received':
48 49
        if app.config.get('autopopup_chat_opened'):
            return True
50 51 52
        if session and session.control:
            return False
    return True
53

Philipp Hörist's avatar
Philipp Hörist committed
54

55
def get_show_in_systray(event, account, jid, type_=None):
56 57 58
    """
    Return True if this event must be shown in systray, else False
    """
Philipp Hörist's avatar
Philipp Hörist committed
59 60 61

    notify = app.config.get('notify_on_all_muc_messages')
    notify_for_jid = app.config.get_per(
62
        'rooms', jid, 'notify_on_all_messages')
Philipp Hörist's avatar
Philipp Hörist committed
63 64

    if type_ == 'printed_gc_msg' and not notify and not notify_for_jid:
65 66
        # it's not an highlighted message, don't show in systray
        return False
67
    return app.config.get('trayicon_notification_on_events')
68

nkour's avatar
nkour committed
69

70 71 72 73 74
class Notification:
    """
    Handle notifications
    """
    def __init__(self):
Philipp Hörist's avatar
Philipp Hörist committed
75 76
        app.ged.register_event_handler(
            'notification', ged.GUI2, self._nec_notification)
77 78 79
        app.ged.register_event_handler(
            'our-show', ged.GUI2, self._nec_our_status)
        app.events.event_removed_subscribe(self._on_event_removed)
80 81 82

    def _nec_notification(self, obj):
        if obj.do_popup:
Philipp Hörist's avatar
Philipp Hörist committed
83 84 85 86 87
            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)
88 89 90 91 92 93 94 95 96

        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:
97
                helpers.exec_command(obj.command, use_shell=True)
98 99
            except Exception:
                pass
Philipp Hörist's avatar
Philipp Hörist committed
100

101 102 103 104 105 106 107 108 109 110 111 112
    def _on_event_removed(self, event_list):
        for event in event_list:
            if event.type_ == 'gc-invitation':
                self.withdraw('gc-invitation', event.account, event.room_jid)
            if event.type_ in ('normal', 'printed_chat', 'chat',
            'printed_pm', 'pm'):
                self.withdraw('new-message', event.account, event.jid)

    def _nec_our_status(self, obj):
        if app.account_is_connected(obj.conn.name):
            self.withdraw('connection-failed', obj.conn.name)

Philipp Hörist's avatar
Philipp Hörist committed
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
    def _get_icon_name(self, obj):
        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)
            else:
                return gtkgui_helpers.get_iconset_name_for(obj.show)

    def popup(self, event_type, jid, account, type_='', icon_name=None,
              title=None, text=None, timeout=-1):
        """
        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':
            instance = PopupNotificationWindow(event_type, jid, account, type_,
                                               icon_name, title, text, timeout)
            app.interface.roster.popup_notification_windows.append(instance)
            return

145
        scale = gtkgui_helpers.get_monitor_scale_factor()
Philipp Hörist's avatar
Philipp Hörist committed
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
        icon_pixbuf = gtkgui_helpers.gtk_icon_theme.load_icon_for_scale(
            icon_name, 48, scale, 0)

        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_pixbuf)
        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)
            #Button in notification
            notification.add_button_with_target(_('Open'), action, variant_dict)
            notification.set_default_action_and_target(action, variant_dict)
171 172 173 174 175 176 177 178 179 180

            # Only one notification per JID
            if event_type in (_('Contact Signed In'), _('Contact Signed Out'),
            _('Contact Changed Status')):
                notif_id = self._id('contact-status-changed', account, jid)
            elif event_type == _('Groupchat Invitation'):
                notif_id = self._id('gc-invitation', account, jid)
            elif event_type == _('Connection Failed'):
                notif_id = self._id('connection-failed', account)
            elif event_type in (_('New Message'), _('New Single Message'),
Philipp Hörist's avatar
Philipp Hörist committed
181
            _('New Private Message')):
182 183
                notif_id = self._id('new-message', account, jid)

Philipp Hörist's avatar
Philipp Hörist committed
184 185 186
        notification.set_priority(Gio.NotificationPriority.NORMAL)
        app.app.send_notification(notif_id, notification)

187 188 189 190 191 192
    def withdraw(self, *args):
        if sys.platform != 'win32':
            app.app.withdraw_notification(self._id(*args))

    def _id(self, *args):
        return ','.join(args)
Philipp Hörist's avatar
Philipp Hörist committed
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313

class PopupNotificationWindow:
    def __init__(self, event_type, jid, account, msg_type='',
                 icon_name=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)
        self.window.set_name('NotificationPopup')
        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))

        css = '#NotificationPopup {background-color: black }'
        gtkgui_helpers.add_css_to_widget(self.window, css)

        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')

        background_class = '''
            .popup-style {
                border-image: none;
                background-image: none;
                background-color: %s }''' % bg_color

        gtkgui_helpers.add_css_to_widget(eventbox, background_class)
        eventbox.get_style_context().add_class('popup-style')

        gtkgui_helpers.add_css_to_widget(close_button, background_class)
        eventbox.get_style_context().add_class('popup-style')

        event_description_label.set_markup('<span foreground="black">%s</span>' %
            GLib.markup_escape_text(text))

        # set the image
        image.set_from_icon_name(icon_name, Gtk.IconSize.DIALOG)

        # 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')
        screen_w, screen_h = gtkgui_helpers.get_total_screen_geometry()
        if pos_x < 0:
            pos_x = screen_w - window_width + pos_x + 1
        pos_y = app.config.get('notification_position_y')
        if pos_y < 0:
            pos_y = screen_h - \
                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
            screen_w, screen_h = gtkgui_helpers.get_total_screen_geometry()
            window_instance.window.move(screen_w - window_width,
                screen_h - \
                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()