2023-01-12 01:04:47 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# Copyright (C) 2019 Chris Caron <lead2gold@gmail.com>
|
|
|
|
# All rights reserved.
|
|
|
|
#
|
|
|
|
# This code is licensed under the MIT License.
|
|
|
|
#
|
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
# of this software and associated documentation files(the "Software"), to deal
|
|
|
|
# in the Software without restriction, including without limitation the rights
|
|
|
|
# to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
|
|
|
|
# copies of the Software, and to permit persons to whom the Software is
|
|
|
|
# furnished to do so, subject to the following conditions :
|
|
|
|
#
|
|
|
|
# The above copyright notice and this permission notice shall be included in
|
|
|
|
# all copies or substantial portions of the Software.
|
|
|
|
#
|
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
|
|
|
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
# THE SOFTWARE.
|
|
|
|
|
|
|
|
from __future__ import absolute_import
|
|
|
|
from __future__ import print_function
|
|
|
|
|
2023-01-14 20:40:05 +00:00
|
|
|
import sys
|
2023-01-12 01:04:47 +00:00
|
|
|
from .NotifyBase import NotifyBase
|
|
|
|
from ..common import NotifyImageSize
|
|
|
|
from ..common import NotifyType
|
|
|
|
from ..utils import parse_bool
|
|
|
|
from ..AppriseLocale import gettext_lazy as _
|
|
|
|
|
|
|
|
# Default our global support flag
|
|
|
|
NOTIFY_DBUS_SUPPORT_ENABLED = False
|
|
|
|
|
|
|
|
# Image support is dependant on the GdkPixbuf library being available
|
|
|
|
NOTIFY_DBUS_IMAGE_SUPPORT = False
|
|
|
|
|
|
|
|
# Initialize our mainloops
|
|
|
|
LOOP_GLIB = None
|
|
|
|
LOOP_QT = None
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
# dbus essentials
|
|
|
|
from dbus import SessionBus
|
|
|
|
from dbus import Interface
|
|
|
|
from dbus import Byte
|
|
|
|
from dbus import ByteArray
|
|
|
|
from dbus import DBusException
|
|
|
|
|
|
|
|
#
|
|
|
|
# now we try to determine which mainloop(s) we can access
|
|
|
|
#
|
|
|
|
|
|
|
|
# glib
|
|
|
|
try:
|
|
|
|
from dbus.mainloop.glib import DBusGMainLoop
|
|
|
|
LOOP_GLIB = DBusGMainLoop()
|
|
|
|
|
2023-01-14 20:40:05 +00:00
|
|
|
except ImportError: # pragma: no cover
|
2023-01-12 01:04:47 +00:00
|
|
|
# No problem
|
|
|
|
pass
|
|
|
|
|
|
|
|
# qt
|
|
|
|
try:
|
|
|
|
from dbus.mainloop.qt import DBusQtMainLoop
|
|
|
|
LOOP_QT = DBusQtMainLoop(set_as_default=True)
|
|
|
|
|
|
|
|
except ImportError:
|
|
|
|
# No problem
|
|
|
|
pass
|
|
|
|
|
|
|
|
# We're good as long as at least one
|
|
|
|
NOTIFY_DBUS_SUPPORT_ENABLED = (
|
|
|
|
LOOP_GLIB is not None or LOOP_QT is not None)
|
|
|
|
|
2023-01-14 20:40:05 +00:00
|
|
|
# ImportError: When using gi.repository you must not import static modules
|
|
|
|
# like "gobject". Please change all occurrences of "import gobject" to
|
|
|
|
# "from gi.repository import GObject".
|
|
|
|
# See: https://bugzilla.gnome.org/show_bug.cgi?id=709183
|
|
|
|
if "gobject" in sys.modules: # pragma: no cover
|
|
|
|
del sys.modules["gobject"]
|
|
|
|
|
2023-01-12 01:04:47 +00:00
|
|
|
try:
|
|
|
|
# The following is required for Image/Icon loading only
|
|
|
|
import gi
|
|
|
|
gi.require_version('GdkPixbuf', '2.0')
|
|
|
|
# noinspection PyUnresolvedReferences
|
|
|
|
from gi.repository import GdkPixbuf
|
|
|
|
NOTIFY_DBUS_IMAGE_SUPPORT = True
|
|
|
|
|
|
|
|
except (ImportError, ValueError, AttributeError):
|
|
|
|
# No problem; this will get caught in outer try/catch
|
|
|
|
|
|
|
|
# A ValueError will get thrown upon calling gi.require_version() if
|
|
|
|
# GDK/GTK isn't installed on the system but gi is.
|
|
|
|
pass
|
|
|
|
|
|
|
|
except ImportError:
|
|
|
|
# No problem; we just simply can't support this plugin; we could
|
|
|
|
# be in microsoft windows, or we just don't have the python-gobject
|
|
|
|
# library available to us (or maybe one we don't support)?
|
|
|
|
pass
|
|
|
|
|
|
|
|
# Define our supported protocols and the loop to assign them.
|
|
|
|
# The key to value pairs are the actual supported schema's matched
|
|
|
|
# up with the Main Loop they should reference when accessed.
|
|
|
|
MAINLOOP_MAP = {
|
|
|
|
'qt': LOOP_QT,
|
|
|
|
'kde': LOOP_QT,
|
|
|
|
'glib': LOOP_GLIB,
|
|
|
|
'dbus': LOOP_QT if LOOP_QT else LOOP_GLIB,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# Urgencies
|
2023-01-14 20:40:05 +00:00
|
|
|
class DBusUrgency:
|
2023-01-12 01:04:47 +00:00
|
|
|
LOW = 0
|
|
|
|
NORMAL = 1
|
|
|
|
HIGH = 2
|
|
|
|
|
|
|
|
|
2023-01-14 20:40:05 +00:00
|
|
|
DBUS_URGENCIES = {
|
|
|
|
# Note: This also acts as a reverse lookup mapping
|
|
|
|
DBusUrgency.LOW: 'low',
|
|
|
|
DBusUrgency.NORMAL: 'normal',
|
|
|
|
DBusUrgency.HIGH: 'high',
|
|
|
|
}
|
|
|
|
|
|
|
|
DBUS_URGENCY_MAP = {
|
|
|
|
# Maps against string 'low'
|
|
|
|
'l': DBusUrgency.LOW,
|
|
|
|
# Maps against string 'moderate'
|
|
|
|
'm': DBusUrgency.LOW,
|
|
|
|
# Maps against string 'normal'
|
|
|
|
'n': DBusUrgency.NORMAL,
|
|
|
|
# Maps against string 'high'
|
|
|
|
'h': DBusUrgency.HIGH,
|
|
|
|
# Maps against string 'emergency'
|
|
|
|
'e': DBusUrgency.HIGH,
|
|
|
|
|
|
|
|
# Entries to additionally support (so more like DBus's API)
|
|
|
|
'0': DBusUrgency.LOW,
|
|
|
|
'1': DBusUrgency.NORMAL,
|
|
|
|
'2': DBusUrgency.HIGH,
|
|
|
|
}
|
2023-01-12 01:04:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
class NotifyDBus(NotifyBase):
|
|
|
|
"""
|
|
|
|
A wrapper for local DBus/Qt Notifications
|
|
|
|
"""
|
|
|
|
|
2023-01-14 20:40:05 +00:00
|
|
|
# Set our global enabled flag
|
|
|
|
enabled = NOTIFY_DBUS_SUPPORT_ENABLED
|
|
|
|
|
|
|
|
requirements = {
|
|
|
|
# Define our required packaging in order to work
|
|
|
|
'details': _('libdbus-1.so.x must be installed.')
|
|
|
|
}
|
|
|
|
|
2023-01-12 01:04:47 +00:00
|
|
|
# The default descriptive name associated with the Notification
|
2023-01-14 20:40:05 +00:00
|
|
|
service_name = _('DBus Notification')
|
|
|
|
|
|
|
|
# The services URL
|
|
|
|
service_url = 'http://www.freedesktop.org/Software/dbus/'
|
2023-01-12 01:04:47 +00:00
|
|
|
|
|
|
|
# The default protocols
|
2023-01-14 20:40:05 +00:00
|
|
|
# Python 3 keys() does not return a list object, it is its own dict_keys()
|
2023-01-12 01:04:47 +00:00
|
|
|
# object if we were to reference, we wouldn't be backwards compatible with
|
|
|
|
# Python v2. So converting the result set back into a list makes us
|
|
|
|
# compatible
|
2023-01-14 20:40:05 +00:00
|
|
|
# TODO: Review after dropping support for Python 2.
|
2023-01-12 01:04:47 +00:00
|
|
|
protocol = list(MAINLOOP_MAP.keys())
|
|
|
|
|
|
|
|
# A URL that takes you to the setup/help of the specific protocol
|
|
|
|
setup_url = 'https://github.com/caronc/apprise/wiki/Notify_dbus'
|
|
|
|
|
|
|
|
# No throttling required for DBus queries
|
|
|
|
request_rate_per_sec = 0
|
|
|
|
|
|
|
|
# Allows the user to specify the NotifyImageSize object
|
|
|
|
image_size = NotifyImageSize.XY_128
|
|
|
|
|
2023-01-14 20:40:05 +00:00
|
|
|
# The number of milliseconds to keep the message present for
|
2023-01-12 01:04:47 +00:00
|
|
|
message_timeout_ms = 13000
|
|
|
|
|
|
|
|
# Limit results to just the first 10 line otherwise there is just to much
|
|
|
|
# content to display
|
|
|
|
body_max_line_count = 10
|
|
|
|
|
2023-01-14 20:40:05 +00:00
|
|
|
# The following are required to hook into the notifications:
|
|
|
|
dbus_interface = 'org.freedesktop.Notifications'
|
|
|
|
dbus_setting_location = '/org/freedesktop/Notifications'
|
2023-01-12 01:04:47 +00:00
|
|
|
|
|
|
|
# Define object templates
|
|
|
|
templates = (
|
2023-01-14 20:40:05 +00:00
|
|
|
'{schema}://',
|
2023-01-12 01:04:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# Define our template arguments
|
|
|
|
template_args = dict(NotifyBase.template_args, **{
|
|
|
|
'urgency': {
|
|
|
|
'name': _('Urgency'),
|
|
|
|
'type': 'choice:int',
|
|
|
|
'values': DBUS_URGENCIES,
|
|
|
|
'default': DBusUrgency.NORMAL,
|
|
|
|
},
|
2023-01-14 20:40:05 +00:00
|
|
|
'priority': {
|
|
|
|
# Apprise uses 'priority' everywhere; it's just a nice consistent
|
|
|
|
# feel to be able to use it here as well. Just map the
|
|
|
|
# value back to 'priority'
|
|
|
|
'alias_of': 'urgency',
|
|
|
|
},
|
2023-01-12 01:04:47 +00:00
|
|
|
'x': {
|
|
|
|
'name': _('X-Axis'),
|
|
|
|
'type': 'int',
|
|
|
|
'min': 0,
|
|
|
|
'map_to': 'x_axis',
|
|
|
|
},
|
|
|
|
'y': {
|
|
|
|
'name': _('Y-Axis'),
|
|
|
|
'type': 'int',
|
|
|
|
'min': 0,
|
|
|
|
'map_to': 'y_axis',
|
|
|
|
},
|
|
|
|
'image': {
|
|
|
|
'name': _('Include Image'),
|
|
|
|
'type': 'bool',
|
|
|
|
'default': True,
|
|
|
|
'map_to': 'include_image',
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
def __init__(self, urgency=None, x_axis=None, y_axis=None,
|
|
|
|
include_image=True, **kwargs):
|
|
|
|
"""
|
|
|
|
Initialize DBus Object
|
|
|
|
"""
|
|
|
|
|
2023-01-14 20:40:05 +00:00
|
|
|
super().__init__(**kwargs)
|
2023-01-12 01:04:47 +00:00
|
|
|
|
|
|
|
# Track our notifications
|
|
|
|
self.registry = {}
|
|
|
|
|
|
|
|
# Store our schema; default to dbus
|
|
|
|
self.schema = kwargs.get('schema', 'dbus')
|
|
|
|
|
|
|
|
if self.schema not in MAINLOOP_MAP:
|
|
|
|
msg = 'The schema specified ({}) is not supported.' \
|
|
|
|
.format(self.schema)
|
|
|
|
self.logger.warning(msg)
|
|
|
|
raise TypeError(msg)
|
|
|
|
|
|
|
|
# The urgency of the message
|
2023-01-14 20:40:05 +00:00
|
|
|
self.urgency = int(
|
|
|
|
NotifyDBus.template_args['urgency']['default']
|
|
|
|
if urgency is None else
|
|
|
|
next((
|
|
|
|
v for k, v in DBUS_URGENCY_MAP.items()
|
|
|
|
if str(urgency).lower().startswith(k)),
|
|
|
|
NotifyDBus.template_args['urgency']['default']))
|
2023-01-12 01:04:47 +00:00
|
|
|
|
|
|
|
# Our x/y axis settings
|
2023-01-14 20:40:05 +00:00
|
|
|
if x_axis or y_axis:
|
|
|
|
try:
|
|
|
|
self.x_axis = int(x_axis)
|
|
|
|
self.y_axis = int(y_axis)
|
|
|
|
|
|
|
|
except (TypeError, ValueError):
|
|
|
|
# Invalid x/y values specified
|
|
|
|
msg = 'The x,y coordinates specified ({},{}) are invalid.'\
|
|
|
|
.format(x_axis, y_axis)
|
|
|
|
self.logger.warning(msg)
|
|
|
|
raise TypeError(msg)
|
|
|
|
else:
|
|
|
|
self.x_axis = None
|
|
|
|
self.y_axis = None
|
2023-01-12 01:04:47 +00:00
|
|
|
|
2023-01-14 20:40:05 +00:00
|
|
|
# Track whether we want to add an image to the notification.
|
2023-01-12 01:04:47 +00:00
|
|
|
self.include_image = include_image
|
|
|
|
|
|
|
|
def send(self, body, title='', notify_type=NotifyType.INFO, **kwargs):
|
|
|
|
"""
|
|
|
|
Perform DBus Notification
|
|
|
|
"""
|
|
|
|
# Acquire our session
|
|
|
|
try:
|
|
|
|
session = SessionBus(mainloop=MAINLOOP_MAP[self.schema])
|
|
|
|
|
2023-01-14 20:40:05 +00:00
|
|
|
except DBusException as e:
|
2023-01-12 01:04:47 +00:00
|
|
|
# Handle exception
|
|
|
|
self.logger.warning('Failed to send DBus notification.')
|
2023-01-14 20:40:05 +00:00
|
|
|
self.logger.debug(f'DBus Exception: {e}')
|
2023-01-12 01:04:47 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
# If there is no title, but there is a body, swap the two to get rid
|
|
|
|
# of the weird whitespace
|
|
|
|
if not title:
|
|
|
|
title = body
|
|
|
|
body = ''
|
|
|
|
|
|
|
|
# acquire our dbus object
|
|
|
|
dbus_obj = session.get_object(
|
2023-01-14 20:40:05 +00:00
|
|
|
self.dbus_interface,
|
|
|
|
self.dbus_setting_location,
|
2023-01-12 01:04:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# Acquire our dbus interface
|
|
|
|
dbus_iface = Interface(
|
|
|
|
dbus_obj,
|
2023-01-14 20:40:05 +00:00
|
|
|
dbus_interface=self.dbus_interface,
|
2023-01-12 01:04:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# image path
|
|
|
|
icon_path = None if not self.include_image \
|
|
|
|
else self.image_path(notify_type, extension='.ico')
|
|
|
|
|
|
|
|
# Our meta payload
|
|
|
|
meta_payload = {
|
|
|
|
"urgency": Byte(self.urgency)
|
|
|
|
}
|
|
|
|
|
|
|
|
if not (self.x_axis is None and self.y_axis is None):
|
|
|
|
# Set x/y access if these were set
|
|
|
|
meta_payload['x'] = self.x_axis
|
|
|
|
meta_payload['y'] = self.y_axis
|
|
|
|
|
|
|
|
if NOTIFY_DBUS_IMAGE_SUPPORT and icon_path:
|
|
|
|
try:
|
|
|
|
# Use Pixbuf to create the proper image type
|
|
|
|
image = GdkPixbuf.Pixbuf.new_from_file(icon_path)
|
|
|
|
|
|
|
|
# Associate our image to our notification
|
|
|
|
meta_payload['icon_data'] = (
|
|
|
|
image.get_width(),
|
|
|
|
image.get_height(),
|
|
|
|
image.get_rowstride(),
|
|
|
|
image.get_has_alpha(),
|
|
|
|
image.get_bits_per_sample(),
|
|
|
|
image.get_n_channels(),
|
|
|
|
ByteArray(image.get_pixels())
|
|
|
|
)
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
self.logger.warning(
|
2023-01-14 20:40:05 +00:00
|
|
|
"Could not load notification icon (%s).", icon_path)
|
|
|
|
self.logger.debug(f'DBus Exception: {e}')
|
2023-01-12 01:04:47 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
# Always call throttle() before any remote execution is made
|
|
|
|
self.throttle()
|
|
|
|
|
|
|
|
dbus_iface.Notify(
|
|
|
|
# Application Identifier
|
|
|
|
self.app_id,
|
|
|
|
# Message ID (0 = New Message)
|
|
|
|
0,
|
|
|
|
# Icon (str) - not used
|
|
|
|
'',
|
|
|
|
# Title
|
|
|
|
str(title),
|
|
|
|
# Body
|
|
|
|
str(body),
|
|
|
|
# Actions
|
|
|
|
list(),
|
|
|
|
# Meta
|
|
|
|
meta_payload,
|
|
|
|
# Message Timeout
|
|
|
|
self.message_timeout_ms,
|
|
|
|
)
|
|
|
|
|
|
|
|
self.logger.info('Sent DBus notification.')
|
|
|
|
|
2023-01-14 20:40:05 +00:00
|
|
|
except Exception as e:
|
2023-01-12 01:04:47 +00:00
|
|
|
self.logger.warning('Failed to send DBus notification.')
|
2023-01-14 20:40:05 +00:00
|
|
|
self.logger.debug(f'DBus Exception: {e}')
|
2023-01-12 01:04:47 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
def url(self, privacy=False, *args, **kwargs):
|
|
|
|
"""
|
|
|
|
Returns the URL built dynamically based on specified arguments.
|
|
|
|
"""
|
|
|
|
|
2023-01-14 20:40:05 +00:00
|
|
|
# Define any URL parameters
|
|
|
|
params = {
|
2023-01-12 01:04:47 +00:00
|
|
|
'image': 'yes' if self.include_image else 'no',
|
2023-01-14 20:40:05 +00:00
|
|
|
'urgency':
|
|
|
|
DBUS_URGENCIES[self.template_args['urgency']['default']]
|
|
|
|
if self.urgency not in DBUS_URGENCIES
|
|
|
|
else DBUS_URGENCIES[self.urgency],
|
2023-01-12 01:04:47 +00:00
|
|
|
}
|
|
|
|
|
2023-01-14 20:40:05 +00:00
|
|
|
# Extend our parameters
|
|
|
|
params.update(self.url_parameters(privacy=privacy, *args, **kwargs))
|
|
|
|
|
2023-01-12 01:04:47 +00:00
|
|
|
# x in (x,y) screen coordinates
|
|
|
|
if self.x_axis:
|
2023-01-14 20:40:05 +00:00
|
|
|
params['x'] = str(self.x_axis)
|
2023-01-12 01:04:47 +00:00
|
|
|
|
|
|
|
# y in (x,y) screen coordinates
|
|
|
|
if self.y_axis:
|
2023-01-14 20:40:05 +00:00
|
|
|
params['y'] = str(self.y_axis)
|
2023-01-12 01:04:47 +00:00
|
|
|
|
2023-01-14 20:40:05 +00:00
|
|
|
return '{schema}://_/?{params}'.format(
|
2023-01-12 01:04:47 +00:00
|
|
|
schema=self.schema,
|
2023-01-14 20:40:05 +00:00
|
|
|
params=NotifyDBus.urlencode(params),
|
2023-01-12 01:04:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def parse_url(url):
|
|
|
|
"""
|
|
|
|
There are no parameters nessisary for this protocol; simply having
|
|
|
|
gnome:// is all you need. This function just makes sure that
|
|
|
|
is in place.
|
|
|
|
|
|
|
|
"""
|
2023-01-14 20:40:05 +00:00
|
|
|
|
|
|
|
results = NotifyBase.parse_url(url, verify_host=False)
|
2023-01-12 01:04:47 +00:00
|
|
|
|
|
|
|
# Include images with our message
|
|
|
|
results['include_image'] = \
|
|
|
|
parse_bool(results['qsd'].get('image', True))
|
|
|
|
|
|
|
|
# DBus supports urgency, but we we also support the keyword priority
|
|
|
|
# so that it is consistent with some of the other plugins
|
2023-01-14 20:40:05 +00:00
|
|
|
if 'priority' in results['qsd'] and len(results['qsd']['priority']):
|
|
|
|
# We intentionally store the priority in the urgency section
|
|
|
|
results['urgency'] = \
|
|
|
|
NotifyDBus.unquote(results['qsd']['priority'])
|
2023-01-12 01:04:47 +00:00
|
|
|
|
2023-01-14 20:40:05 +00:00
|
|
|
if 'urgency' in results['qsd'] and len(results['qsd']['urgency']):
|
|
|
|
results['urgency'] = \
|
|
|
|
NotifyDBus.unquote(results['qsd']['urgency'])
|
2023-01-12 01:04:47 +00:00
|
|
|
|
|
|
|
# handle x,y coordinates
|
2023-01-14 20:40:05 +00:00
|
|
|
if 'x' in results['qsd'] and len(results['qsd']['x']):
|
|
|
|
results['x_axis'] = NotifyDBus.unquote(results['qsd'].get('x'))
|
2023-01-12 01:04:47 +00:00
|
|
|
|
2023-01-14 20:40:05 +00:00
|
|
|
if 'y' in results['qsd'] and len(results['qsd']['y']):
|
|
|
|
results['y_axis'] = NotifyDBus.unquote(results['qsd'].get('y'))
|
2023-01-12 01:04:47 +00:00
|
|
|
|
|
|
|
return results
|