Refactor Windows Notifications

- Only show one notification at a time
- Fix pylint errors
This commit is contained in:
Philipp Hörist 2018-11-18 21:07:17 +01:00
parent fedc00eb83
commit dffa54f7d3
3 changed files with 166 additions and 184 deletions

View File

@ -1,32 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.20.1 --> <!-- Generated with glade 3.22.1 -->
<interface> <interface>
<requires lib="gtk+" version="3.12"/> <requires lib="gtk+" version="3.12"/>
<object class="GtkWindow" id="popup_notification_window">
<property name="width_request">312</property>
<property name="height_request">95</property>
<property name="can_focus">False</property>
<property name="border_width">1</property>
<property name="type">popup</property>
<property name="resizable">False</property>
<property name="skip_taskbar_hint">True</property>
<property name="skip_pager_hint">True</property>
<property name="decorated">False</property>
<property name="gravity">south-east</property>
<signal name="button-press-event" handler="on_popup_notification_window_button_press_event" swapped="no"/>
<child>
<object class="GtkEventBox" id="eventbox"> <object class="GtkEventBox" id="eventbox">
<property name="name">PopupNotificationEventBox</property> <property name="name">PopupNotificationEventBox</property>
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">False</property> <property name="can_focus">False</property>
<child> <child>
<object class="GtkBox" id="hbox3019"> <object class="GtkBox">
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">False</property> <property name="can_focus">False</property>
<property name="border_width">4</property> <property name="border_width">4</property>
<property name="spacing">6</property> <property name="spacing">6</property>
<child> <child>
<object class="GtkImage" id="notification_image"> <object class="GtkImage" id="image">
<property name="width_request">68</property> <property name="width_request">68</property>
<property name="height_request">86</property> <property name="height_request">86</property>
<property name="visible">True</property> <property name="visible">True</property>
@ -39,12 +26,12 @@
</packing> </packing>
</child> </child>
<child> <child>
<object class="GtkBox" id="vbox111"> <object class="GtkBox">
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">False</property> <property name="can_focus">False</property>
<property name="orientation">vertical</property> <property name="orientation">vertical</property>
<child> <child>
<object class="GtkBox" id="hbox3020"> <object class="GtkBox">
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">False</property> <property name="can_focus">False</property>
<child> <child>
@ -55,6 +42,7 @@
<property name="label" translatable="yes">Event Type</property> <property name="label" translatable="yes">Event Type</property>
<property name="use_markup">True</property> <property name="use_markup">True</property>
<property name="wrap">True</property> <property name="wrap">True</property>
<property name="xalign">0</property>
</object> </object>
<packing> <packing>
<property name="expand">False</property> <property name="expand">False</property>
@ -68,9 +56,9 @@
<property name="can_focus">False</property> <property name="can_focus">False</property>
<property name="receives_default">False</property> <property name="receives_default">False</property>
<property name="relief">none</property> <property name="relief">none</property>
<signal name="clicked" handler="on_close_button_clicked" swapped="no"/> <signal name="clicked" handler="_on_close_button_clicked" swapped="no"/>
<child> <child>
<object class="GtkImage" id="image496"> <object class="GtkImage">
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">False</property> <property name="can_focus">False</property>
<property name="stock">gtk-close</property> <property name="stock">gtk-close</property>
@ -99,6 +87,9 @@
<property name="can_focus">False</property> <property name="can_focus">False</property>
<property name="label" translatable="yes">Event desc</property> <property name="label" translatable="yes">Event desc</property>
<property name="wrap">True</property> <property name="wrap">True</property>
<property name="ellipsize">end</property>
<property name="max_width_chars">30</property>
<property name="xalign">0</property>
</object> </object>
<packing> <packing>
<property name="expand">False</property> <property name="expand">False</property>
@ -116,9 +107,4 @@
</object> </object>
</child> </child>
</object> </object>
</child>
<child type="titlebar">
<placeholder/>
</child>
</object>
</interface> </interface>

View File

@ -24,8 +24,8 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with Gajim. If not, see <http://www.gnu.org/licenses/>. # along with Gajim. If not, see <http://www.gnu.org/licenses/>.
import logging
import sys import sys
import logging
from gi.repository import GLib from gi.repository import GLib
from gi.repository import Gio from gi.repository import Gio
@ -38,12 +38,13 @@ from gajim.common import helpers
from gajim.common import ged from gajim.common import ged
from gajim.common.i18n import _ from gajim.common.i18n import _
from gajim.gtk.util import get_builder
from gajim.gtk.util import get_icon_name from gajim.gtk.util import get_icon_name
from gajim.gtk.util import get_monitor_scale_factor from gajim.gtk.util import get_monitor_scale_factor
from gajim.gtk.util import get_total_screen_geometry from gajim.gtk.util import get_total_screen_geometry
from gajim.gtk.util import load_icon from gajim.gtk.util import load_icon
log = logging.getLogger('gajim.notify') log = logging.getLogger('gajim.gtk.notification')
class Notification: class Notification:
@ -51,18 +52,19 @@ class Notification:
Handle notifications Handle notifications
""" """
def __init__(self): def __init__(self):
self.daemon_capabilities = ['actions'] self._daemon_capabilities = ['actions']
self._win32_active_popup = None
# Detect if actions are supported by the notification daemon # Detect if actions are supported by the notification daemon
if sys.platform not in ('win32', 'darwin'): if sys.platform not in ('win32', 'darwin'):
def on_proxy_ready(source, res, data=None): def on_proxy_ready(_source, res, _data):
try: try:
proxy = Gio.DBusProxy.new_finish(res) proxy = Gio.DBusProxy.new_finish(res)
self.daemon_capabilities = proxy.GetCapabilities() self._daemon_capabilities = proxy.GetCapabilities()
except GLib.Error as e: except GLib.Error as error:
if e.domain == 'g-dbus-error-quark': if error.domain == 'g-dbus-error-quark':
log.info('Notifications D-Bus connection failed: %s', log.info('Notifications D-Bus connection failed: %s',
e.message) error.message)
else: else:
raise raise
else: else:
@ -75,7 +77,8 @@ class Notification:
'org.freedesktop.Notifications', 'org.freedesktop.Notifications',
'/org/freedesktop/Notifications', '/org/freedesktop/Notifications',
'org.freedesktop.Notifications', 'org.freedesktop.Notifications',
None, on_proxy_ready) None,
on_proxy_ready)
app.ged.register_event_handler( app.ged.register_event_handler(
'notification', ged.GUI2, self._nec_notification) 'notification', ged.GUI2, self._nec_notification)
@ -106,16 +109,17 @@ class Notification:
def _on_event_removed(self, event_list): def _on_event_removed(self, event_list):
for event in event_list: for event in event_list:
if event.type_ == 'gc-invitation': if event.type_ == 'gc-invitation':
self.withdraw('gc-invitation', event.account, event.room_jid) self._withdraw('gc-invitation', event.account, event.room_jid)
if event.type_ in ('normal', 'printed_chat', 'chat', if event.type_ in ('normal', 'printed_chat', 'chat',
'printed_pm', 'pm'): 'printed_pm', 'pm'):
self.withdraw('new-message', event.account, event.jid) self._withdraw('new-message', event.account, event.jid)
def _nec_our_status(self, obj): def _nec_our_status(self, obj):
if app.account_is_connected(obj.conn.name): if app.account_is_connected(obj.conn.name):
self.withdraw('connection-failed', obj.conn.name) self._withdraw('connection-failed', obj.conn.name)
def _get_icon_name(self, obj): @staticmethod
def _get_icon_name(obj):
if obj.notif_type == 'msg': if obj.notif_type == 'msg':
if obj.base_event.mtype == 'pm': if obj.base_event.mtype == 'pm':
return 'gajim-priv_msg_recv' return 'gajim-priv_msg_recv'
@ -141,9 +145,11 @@ class Notification:
timeout = app.config.get('notification_timeout') timeout = app.config.get('notification_timeout')
if sys.platform == 'win32': if sys.platform == 'win32':
instance = PopupNotificationWindow(event_type, jid, account, type_, self._withdraw()
self._win32_active_popup = PopupNotification(
event_type, jid, account, type_,
icon_name, title, text, timeout) icon_name, title, text, timeout)
app.interface.roster.popup_notification_windows.append(instance) self._win32_active_popup.connect('destroy', self._on_popup_destroy)
return return
scale = get_monitor_scale_factor() scale = get_monitor_scale_factor()
@ -155,13 +161,15 @@ class Notification:
if text is not None: if text is not None:
notification.set_body(text) notification.set_body(text)
notif_id = None notif_id = None
if event_type in (_('Contact Signed In'), _('Contact Signed Out'), if event_type in (
_('Contact Signed In'), _('Contact Signed Out'),
_('New Message'), _('New Single Message'), _('New Private Message'), _('New Message'), _('New Single Message'), _('New Private Message'),
_('Contact Changed Status'), _('File Transfer Request'), _('Contact Changed Status'), _('File Transfer Request'),
_('File Transfer Error'), _('File Transfer Completed'), _('File Transfer Error'), _('File Transfer Completed'),
_('File Transfer Stopped'), _('Groupchat Invitation'), _('File Transfer Stopped'), _('Groupchat Invitation'),
_('Connection Failed'), _('Subscription request'), _('Unsubscribed')): _('Connection Failed'), _('Subscription request'),
if 'actions' in self.daemon_capabilities: _('Unsubscribed')):
if 'actions' in self._daemon_capabilities:
# Create Variant Dict # Create Variant Dict
dict_ = {'account': GLib.Variant('s', account), dict_ = {'account': GLib.Variant('s', account),
'jid': GLib.Variant('s', jid), 'jid': GLib.Variant('s', jid),
@ -175,60 +183,71 @@ class Notification:
variant_dict) variant_dict)
# Only one notification per JID # Only one notification per JID
if event_type in (_('Contact Signed In'), _('Contact Signed Out'), if event_type in (_('Contact Signed In'),
_('Contact Signed Out'),
_('Contact Changed Status')): _('Contact Changed Status')):
notif_id = self._id('contact-status-changed', account, jid) notif_id = self._make_id('contact-status-changed', account, jid)
elif event_type == _('Groupchat Invitation'): elif event_type == _('Groupchat Invitation'):
notif_id = self._id('gc-invitation', account, room_jid) notif_id = self._make_id('gc-invitation', account, room_jid)
elif event_type == _('Connection Failed'): elif event_type == _('Connection Failed'):
notif_id = self._id('connection-failed', account) notif_id = self._make_id('connection-failed', account)
elif event_type in (_('New Message'), _('New Single Message'), elif event_type in (_('New Message'),
_('New Single Message'),
_('New Private Message')): _('New Private Message')):
avatar = app.contacts.get_avatar(account, jid) avatar = app.contacts.get_avatar(account, jid)
if avatar: if avatar:
icon_pixbuf = avatar icon_pixbuf = avatar
notif_id = self._id('new-message', account, jid) notif_id = self._make_id('new-message', account, jid)
notification.set_icon(icon_pixbuf) notification.set_icon(icon_pixbuf)
notification.set_priority(Gio.NotificationPriority.NORMAL) notification.set_priority(Gio.NotificationPriority.NORMAL)
app.app.send_notification(notif_id, notification) app.app.send_notification(notif_id, notification)
def withdraw(self, *args): def _on_popup_destroy(self, *args):
if sys.platform != 'win32': self._win32_active_popup = None
app.app.withdraw_notification(self._id(*args))
def _id(self, *args): 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):
return ','.join(args) return ','.join(args)
class PopupNotificationWindow:
class PopupNotification(Gtk.Window):
def __init__(self, event_type, jid, account, msg_type='', def __init__(self, event_type, jid, account, msg_type='',
icon_name=None, title=None, text=None, timeout=-1): icon_name=None, title=None, text=None, timeout=-1):
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
self.account = account self.account = account
self.jid = jid self.jid = jid
self.msg_type = msg_type 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._ui = get_builder('popup_notification_window.ui')
self.window = xml.get_object('popup_notification_window') self.add(self._ui.eventbox)
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: if not text:
text = app.get_name_from_jid(account, jid) # default value of text text = app.get_name_from_jid(account, jid) # default value of text
if not title: if not title:
title = '' title = ''
event_type_label.set_markup( self._ui.event_type_label.set_markup(
'<span foreground="black" weight="bold">%s</span>' % '<span foreground="black" weight="bold">%s</span>' %
GLib.markup_escape_text(title)) GLib.markup_escape_text(title))
css = '#NotificationPopup {background-color: black }' css = '#NotificationPopup {background-color: black }'
gtkgui_helpers.add_css_to_widget(self.window, css) gtkgui_helpers.add_css_to_widget(self, css)
if event_type == _('Contact Signed In'): if event_type == _('Contact Signed In'):
bg_color = app.config.get('notif_signin_color') bg_color = app.config.get('notif_signin_color')
@ -257,67 +276,47 @@ class PopupNotificationWindow:
background-image: none; background-image: none;
background-color: %s }''' % bg_color background-color: %s }''' % bg_color
gtkgui_helpers.add_css_to_widget(eventbox, background_class) gtkgui_helpers.add_css_to_widget(self._ui.eventbox, background_class)
eventbox.get_style_context().add_class('popup-style') self._ui.eventbox.get_style_context().add_class('popup-style')
gtkgui_helpers.add_css_to_widget(close_button, background_class) gtkgui_helpers.add_css_to_widget(
eventbox.get_style_context().add_class('popup-style') self._ui.close_button, background_class)
self._ui.close_button.get_style_context().add_class('popup-style')
event_description_label.set_markup('<span foreground="black">%s</span>' % escaped_text = GLib.markup_escape_text(text)
GLib.markup_escape_text(text)) self._ui.event_description_label.set_markup(
'<span foreground="black">%s</span>' % escaped_text)
# set the image # set the image
image.set_from_icon_name(icon_name, Gtk.IconSize.DIALOG) self._ui.image.set_from_icon_name(icon_name, Gtk.IconSize.DIALOG)
# position the window to bottom-right of screen self.move(*self._get_window_pos())
window_width, self.window_height = self.window.get_size()
app.interface.roster.popups_notification_height += self.window_height 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):
pos_x = app.config.get('notification_position_x') pos_x = app.config.get('notification_position_x')
screen_w, screen_h = get_total_screen_geometry() screen_w, screen_h = get_total_screen_geometry()
if pos_x < 0: if pos_x < 0:
pos_x = screen_w - window_width + pos_x + 1 pos_x = screen_w - 312 + pos_x + 1
pos_y = app.config.get('notification_position_y') pos_y = app.config.get('notification_position_y')
if pos_y < 0: if pos_y < 0:
pos_y = screen_h - \ pos_y = screen_h - 95 - 80 + pos_y + 1
app.interface.roster.popups_notification_height + pos_y + 1 return pos_x, pos_y
self.window.move(pos_x, pos_y)
xml.connect_signals(self) def _on_close_button_clicked(self, _widget):
self.window.show_all() self.destroy()
if timeout > 0:
GLib.timeout_add_seconds(timeout, self.on_timeout)
def on_close_button_clicked(self, widget): def _on_button_press(self, _widget, event):
self.adjust_height_and_move_popup_notification_windows() if event.button == 1:
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 = 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) app.interface.handle_event(self.account, self.jid, self.msg_type)
self.adjust_height_and_move_popup_notification_windows() self.destroy()
def _on_destroy(self, *args):
if self._timeout_id is not None:
GLib.source_remove(self._timeout_id)

View File

@ -5668,9 +5668,6 @@ class RosterWindow:
app.config.get('roster_x-position'), app.config.get('roster_x-position'),
app.config.get('roster_y-position')) app.config.get('roster_y-position'))
self.popups_notification_height = 0
self.popup_notification_windows = []
# Remove contact from roster when last event opened # Remove contact from roster when last event opened
# { (contact, account): { backend: boolean } # { (contact, account): { backend: boolean }
self.contacts_to_be_removed = {} self.contacts_to_be_removed = {}