2019-06-07 11:16:07 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2023-06-07 18:05:42 +00:00
|
|
|
# BSD 3-Clause License
|
2019-06-07 11:16:07 +00:00
|
|
|
#
|
2023-06-07 18:05:42 +00:00
|
|
|
# Apprise - Push Notification Library.
|
|
|
|
# Copyright (c) 2023, Chris Caron <lead2gold@gmail.com>
|
2019-06-07 11:16:07 +00:00
|
|
|
#
|
2023-06-07 18:05:42 +00:00
|
|
|
# Redistribution and use in source and binary forms, with or without
|
|
|
|
# modification, are permitted provided that the following conditions are met:
|
2019-06-07 11:16:07 +00:00
|
|
|
#
|
2023-06-07 18:05:42 +00:00
|
|
|
# 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
# this list of conditions and the following disclaimer.
|
2019-06-07 11:16:07 +00:00
|
|
|
#
|
2023-06-07 18:05:42 +00:00
|
|
|
# 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
# this list of conditions and the following disclaimer in the documentation
|
|
|
|
# and/or other materials provided with the distribution.
|
2019-06-07 11:16:07 +00:00
|
|
|
#
|
2023-06-07 18:05:42 +00:00
|
|
|
# 3. Neither the name of the copyright holder nor the names of its
|
|
|
|
# contributors may be used to endorse or promote products derived from
|
|
|
|
# this software without specific prior written permission.
|
|
|
|
#
|
|
|
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
|
|
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
# POSSIBILITY OF SUCH DAMAGE.
|
2019-06-07 11:16:07 +00:00
|
|
|
|
|
|
|
from __future__ import absolute_import
|
|
|
|
from __future__ import print_function
|
|
|
|
|
|
|
|
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_GNOME_SUPPORT_ENABLED = False
|
|
|
|
|
|
|
|
try:
|
|
|
|
# 3rd party modules (Gnome Only)
|
|
|
|
import gi
|
|
|
|
|
|
|
|
# require_version() call is required otherwise we generate a warning
|
|
|
|
gi.require_version("Notify", "0.7")
|
|
|
|
|
|
|
|
# We can import the actual libraries we care about now:
|
|
|
|
from gi.repository import Notify
|
|
|
|
from gi.repository import GdkPixbuf
|
|
|
|
|
|
|
|
# We're good to go!
|
|
|
|
NOTIFY_GNOME_SUPPORT_ENABLED = True
|
|
|
|
|
2019-11-25 01:15:30 +00:00
|
|
|
except (ImportError, ValueError, AttributeError):
|
2019-06-07 11:16:07 +00:00
|
|
|
# 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)?
|
|
|
|
|
2023-06-07 18:05:42 +00:00
|
|
|
# Alternatively, a `ValueError` will get raised upon calling
|
|
|
|
# gi.require_version() if the requested Notify namespace isn't available.
|
2019-06-07 11:16:07 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
# Urgencies
|
2022-10-11 01:19:24 +00:00
|
|
|
class GnomeUrgency:
|
2019-06-07 11:16:07 +00:00
|
|
|
LOW = 0
|
|
|
|
NORMAL = 1
|
|
|
|
HIGH = 2
|
|
|
|
|
|
|
|
|
2022-10-11 01:19:24 +00:00
|
|
|
GNOME_URGENCIES = {
|
|
|
|
GnomeUrgency.LOW: 'low',
|
|
|
|
GnomeUrgency.NORMAL: 'normal',
|
|
|
|
GnomeUrgency.HIGH: 'high',
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
GNOME_URGENCY_MAP = {
|
|
|
|
# Maps against string 'low'
|
|
|
|
'l': GnomeUrgency.LOW,
|
|
|
|
# Maps against string 'moderate'
|
|
|
|
'm': GnomeUrgency.LOW,
|
|
|
|
# Maps against string 'normal'
|
|
|
|
'n': GnomeUrgency.NORMAL,
|
|
|
|
# Maps against string 'high'
|
|
|
|
'h': GnomeUrgency.HIGH,
|
|
|
|
# Maps against string 'emergency'
|
|
|
|
'e': GnomeUrgency.HIGH,
|
|
|
|
|
|
|
|
# Entries to additionally support (so more like Gnome's API)
|
|
|
|
'0': GnomeUrgency.LOW,
|
|
|
|
'1': GnomeUrgency.NORMAL,
|
|
|
|
'2': GnomeUrgency.HIGH,
|
|
|
|
}
|
2019-06-07 11:16:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
class NotifyGnome(NotifyBase):
|
|
|
|
"""
|
|
|
|
A wrapper for local Gnome Notifications
|
|
|
|
"""
|
|
|
|
|
2021-12-02 02:19:18 +00:00
|
|
|
# Set our global enabled flag
|
|
|
|
enabled = NOTIFY_GNOME_SUPPORT_ENABLED
|
|
|
|
|
|
|
|
requirements = {
|
|
|
|
# Define our required packaging in order to work
|
|
|
|
'details': _('A local Gnome environment is required.')
|
|
|
|
}
|
|
|
|
|
2019-06-07 11:16:07 +00:00
|
|
|
# The default descriptive name associated with the Notification
|
2021-12-02 02:19:18 +00:00
|
|
|
service_name = _('Gnome Notification')
|
|
|
|
|
|
|
|
# The service URL
|
|
|
|
service_url = 'https://www.gnome.org/'
|
2019-06-07 11:16:07 +00:00
|
|
|
|
|
|
|
# The default protocol
|
|
|
|
protocol = 'gnome'
|
|
|
|
|
|
|
|
# A URL that takes you to the setup/help of the specific protocol
|
|
|
|
setup_url = 'https://github.com/caronc/apprise/wiki/Notify_gnome'
|
|
|
|
|
|
|
|
# Allows the user to specify the NotifyImageSize object
|
|
|
|
image_size = NotifyImageSize.XY_128
|
|
|
|
|
|
|
|
# Disable throttle rate for Gnome requests since they are normally
|
|
|
|
# local anyway
|
|
|
|
request_rate_per_sec = 0
|
|
|
|
|
|
|
|
# Limit results to just the first 10 line otherwise there is just to much
|
|
|
|
# content to display
|
|
|
|
body_max_line_count = 10
|
|
|
|
|
|
|
|
# A title can not be used for Gnome Messages. Setting this to zero will
|
|
|
|
# cause any title (if defined) to get placed into the message body.
|
|
|
|
title_maxlen = 0
|
|
|
|
|
|
|
|
# Define object templates
|
|
|
|
templates = (
|
2020-09-14 12:24:26 +00:00
|
|
|
'{schema}://',
|
2019-06-07 11:16:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# Define our template arguments
|
|
|
|
template_args = dict(NotifyBase.template_args, **{
|
|
|
|
'urgency': {
|
|
|
|
'name': _('Urgency'),
|
|
|
|
'type': 'choice:int',
|
|
|
|
'values': GNOME_URGENCIES,
|
|
|
|
'default': GnomeUrgency.NORMAL,
|
|
|
|
},
|
2022-10-11 01:19:24 +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',
|
|
|
|
},
|
2019-06-07 11:16:07 +00:00
|
|
|
'image': {
|
|
|
|
'name': _('Include Image'),
|
|
|
|
'type': 'bool',
|
|
|
|
'default': True,
|
|
|
|
'map_to': 'include_image',
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
def __init__(self, urgency=None, include_image=True, **kwargs):
|
|
|
|
"""
|
|
|
|
Initialize Gnome Object
|
|
|
|
"""
|
|
|
|
|
2023-06-07 18:05:42 +00:00
|
|
|
super().__init__(**kwargs)
|
2019-06-07 11:16:07 +00:00
|
|
|
|
|
|
|
# The urgency of the message
|
2022-10-11 01:19:24 +00:00
|
|
|
self.urgency = int(
|
|
|
|
NotifyGnome.template_args['urgency']['default']
|
|
|
|
if urgency is None else
|
|
|
|
next((
|
|
|
|
v for k, v in GNOME_URGENCY_MAP.items()
|
|
|
|
if str(urgency).lower().startswith(k)),
|
|
|
|
NotifyGnome.template_args['urgency']['default']))
|
2019-06-07 11:16:07 +00:00
|
|
|
|
2023-06-07 18:05:42 +00:00
|
|
|
# Track whether we want to add an image to the notification.
|
2019-06-07 11:16:07 +00:00
|
|
|
self.include_image = include_image
|
|
|
|
|
|
|
|
def send(self, body, title='', notify_type=NotifyType.INFO, **kwargs):
|
|
|
|
"""
|
|
|
|
Perform Gnome Notification
|
|
|
|
"""
|
|
|
|
|
|
|
|
try:
|
|
|
|
# App initialization
|
|
|
|
Notify.init(self.app_id)
|
|
|
|
|
|
|
|
# image path
|
|
|
|
icon_path = None if not self.include_image \
|
|
|
|
else self.image_path(notify_type, extension='.ico')
|
|
|
|
|
|
|
|
# Build message body
|
|
|
|
notification = Notify.Notification.new(body)
|
|
|
|
|
|
|
|
# Assign urgency
|
|
|
|
notification.set_urgency(self.urgency)
|
|
|
|
|
|
|
|
# Always call throttle before any remote server i/o is made
|
|
|
|
self.throttle()
|
|
|
|
|
|
|
|
if 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
|
|
|
|
notification.set_icon_from_pixbuf(image)
|
|
|
|
notification.set_image_from_pixbuf(image)
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
self.logger.warning(
|
2023-06-07 18:05:42 +00:00
|
|
|
"Could not load notification icon (%s).", icon_path)
|
|
|
|
self.logger.debug(f'Gnome Exception: {e}')
|
2019-06-07 11:16:07 +00:00
|
|
|
|
|
|
|
notification.show()
|
|
|
|
self.logger.info('Sent Gnome notification.')
|
|
|
|
|
2023-06-07 18:05:42 +00:00
|
|
|
except Exception as e:
|
2019-06-07 11:16:07 +00:00
|
|
|
self.logger.warning('Failed to send Gnome notification.')
|
2023-06-07 18:05:42 +00:00
|
|
|
self.logger.debug(f'Gnome Exception: {e}')
|
2019-06-07 11:16:07 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
2019-11-25 01:15:30 +00:00
|
|
|
def url(self, privacy=False, *args, **kwargs):
|
2019-06-07 11:16:07 +00:00
|
|
|
"""
|
|
|
|
Returns the URL built dynamically based on specified arguments.
|
|
|
|
"""
|
|
|
|
|
2020-09-14 12:24:26 +00:00
|
|
|
# Define any URL parameters
|
|
|
|
params = {
|
2019-06-07 11:16:07 +00:00
|
|
|
'image': 'yes' if self.include_image else 'no',
|
2022-10-11 01:19:24 +00:00
|
|
|
'urgency':
|
|
|
|
GNOME_URGENCIES[self.template_args['urgency']['default']]
|
|
|
|
if self.urgency not in GNOME_URGENCIES
|
|
|
|
else GNOME_URGENCIES[self.urgency],
|
2019-06-07 11:16:07 +00:00
|
|
|
}
|
|
|
|
|
2020-09-14 12:24:26 +00:00
|
|
|
# Extend our parameters
|
|
|
|
params.update(self.url_parameters(privacy=privacy, *args, **kwargs))
|
|
|
|
|
|
|
|
return '{schema}://?{params}'.format(
|
2019-06-07 11:16:07 +00:00
|
|
|
schema=self.protocol,
|
2020-09-14 12:24:26 +00:00
|
|
|
params=NotifyGnome.urlencode(params),
|
2019-06-07 11:16:07 +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.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2020-09-14 12:24:26 +00:00
|
|
|
results = NotifyBase.parse_url(url, verify_host=False)
|
2019-06-07 11:16:07 +00:00
|
|
|
|
|
|
|
# Include images with our message
|
|
|
|
results['include_image'] = \
|
|
|
|
parse_bool(results['qsd'].get('image', True))
|
|
|
|
|
|
|
|
# Gnome supports urgency, but we we also support the keyword priority
|
|
|
|
# so that it is consistent with some of the other plugins
|
2022-10-11 01:19:24 +00:00
|
|
|
if 'priority' in results['qsd'] and len(results['qsd']['priority']):
|
|
|
|
# We intentionally store the priority in the urgency section
|
|
|
|
results['urgency'] = \
|
|
|
|
NotifyGnome.unquote(results['qsd']['priority'])
|
|
|
|
|
|
|
|
if 'urgency' in results['qsd'] and len(results['qsd']['urgency']):
|
|
|
|
results['urgency'] = \
|
|
|
|
NotifyGnome.unquote(results['qsd']['urgency'])
|
2019-06-07 11:16:07 +00:00
|
|
|
|
|
|
|
return results
|