2018-09-17 00:27:00 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2023-06-07 18:05:42 +00:00
|
|
|
# BSD 3-Clause License
|
2018-09-17 00:27:00 +00:00
|
|
|
#
|
2023-06-07 18:05:42 +00:00
|
|
|
# Apprise - Push Notification Library.
|
|
|
|
# Copyright (c) 2023, Chris Caron <lead2gold@gmail.com>
|
2018-09-17 00:27:00 +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:
|
2018-09-17 00:27:00 +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.
|
2018-09-17 00:27:00 +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.
|
2018-09-17 00:27:00 +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
|
|
|
|
2018-09-17 00:27:00 +00:00
|
|
|
from __future__ import absolute_import
|
|
|
|
from __future__ import print_function
|
|
|
|
|
|
|
|
from time import sleep
|
|
|
|
|
|
|
|
from .NotifyBase import NotifyBase
|
|
|
|
from ..common import NotifyImageSize
|
2019-06-07 11:16:07 +00:00
|
|
|
from ..common import NotifyType
|
|
|
|
from ..utils import parse_bool
|
|
|
|
from ..AppriseLocale import gettext_lazy as _
|
2018-09-17 00:27:00 +00:00
|
|
|
|
|
|
|
# Default our global support flag
|
|
|
|
NOTIFY_WINDOWS_SUPPORT_ENABLED = False
|
|
|
|
|
|
|
|
try:
|
|
|
|
# 3rd party modules (Windows Only)
|
|
|
|
import win32api
|
|
|
|
import win32con
|
|
|
|
import win32gui
|
|
|
|
|
|
|
|
# We're good to go!
|
|
|
|
NOTIFY_WINDOWS_SUPPORT_ENABLED = True
|
|
|
|
|
|
|
|
except ImportError:
|
|
|
|
# No problem; we just simply can't support this plugin because we're
|
2020-09-14 12:24:26 +00:00
|
|
|
# either using Linux, or simply do not have pywin32 installed.
|
2018-09-17 00:27:00 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class NotifyWindows(NotifyBase):
|
|
|
|
"""
|
|
|
|
A wrapper for local Windows Notifications
|
|
|
|
"""
|
2021-12-02 02:19:18 +00:00
|
|
|
# Set our global enabled flag
|
|
|
|
enabled = NOTIFY_WINDOWS_SUPPORT_ENABLED
|
|
|
|
|
|
|
|
requirements = {
|
|
|
|
# Define our required packaging in order to work
|
|
|
|
'details': _('A local Microsoft Windows environment is required.')
|
|
|
|
}
|
2018-09-17 00:27:00 +00:00
|
|
|
|
2018-10-22 16:39:11 +00:00
|
|
|
# The default descriptive name associated with the Notification
|
|
|
|
service_name = 'Windows Notification'
|
|
|
|
|
2018-09-17 00:27:00 +00:00
|
|
|
# The default protocol
|
|
|
|
protocol = 'windows'
|
|
|
|
|
2018-10-22 16:39:11 +00:00
|
|
|
# A URL that takes you to the setup/help of the specific protocol
|
|
|
|
setup_url = 'https://github.com/caronc/apprise/wiki/Notify_windows'
|
|
|
|
|
2019-06-07 11:16:07 +00:00
|
|
|
# Disable throttle rate for Windows requests since they are normally
|
|
|
|
# local anyway
|
|
|
|
request_rate_per_sec = 0
|
|
|
|
|
2018-09-17 00:27:00 +00:00
|
|
|
# Allows the user to specify the NotifyImageSize object
|
|
|
|
image_size = NotifyImageSize.XY_128
|
|
|
|
|
2019-06-07 11:16:07 +00:00
|
|
|
# Limit results to just the first 2 line otherwise there is just to much
|
|
|
|
# content to display
|
|
|
|
body_max_line_count = 2
|
|
|
|
|
|
|
|
# The number of seconds to display the popup for
|
|
|
|
default_popup_duration_sec = 12
|
|
|
|
|
|
|
|
# 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, **{
|
|
|
|
'duration': {
|
|
|
|
'name': _('Duration'),
|
|
|
|
'type': 'int',
|
|
|
|
'min': 1,
|
|
|
|
'default': 12,
|
|
|
|
},
|
|
|
|
'image': {
|
|
|
|
'name': _('Include Image'),
|
|
|
|
'type': 'bool',
|
|
|
|
'default': True,
|
|
|
|
'map_to': 'include_image',
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
def __init__(self, include_image=True, duration=None, **kwargs):
|
2018-09-17 00:27:00 +00:00
|
|
|
"""
|
|
|
|
Initialize Windows Object
|
|
|
|
"""
|
|
|
|
|
2023-06-07 18:05:42 +00:00
|
|
|
super().__init__(**kwargs)
|
2019-06-07 11:16:07 +00:00
|
|
|
|
2018-09-17 00:27:00 +00:00
|
|
|
# Number of seconds to display notification for
|
2019-06-07 11:16:07 +00:00
|
|
|
self.duration = self.default_popup_duration_sec \
|
|
|
|
if not (isinstance(duration, int) and duration > 0) else duration
|
2018-09-17 00:27:00 +00:00
|
|
|
|
|
|
|
# Define our handler
|
|
|
|
self.hwnd = None
|
|
|
|
|
2019-06-07 11:16:07 +00:00
|
|
|
# Track whether or not we want to send an image with our notification
|
|
|
|
# or not.
|
|
|
|
self.include_image = include_image
|
2018-09-17 00:27:00 +00:00
|
|
|
|
|
|
|
def _on_destroy(self, hwnd, msg, wparam, lparam):
|
|
|
|
"""
|
|
|
|
Destroy callback function
|
|
|
|
"""
|
|
|
|
|
|
|
|
nid = (self.hwnd, 0)
|
|
|
|
win32gui.Shell_NotifyIcon(win32gui.NIM_DELETE, nid)
|
|
|
|
win32api.PostQuitMessage(0)
|
|
|
|
|
2023-06-07 18:05:42 +00:00
|
|
|
return 0
|
2018-09-17 00:27:00 +00:00
|
|
|
|
2019-06-07 11:16:07 +00:00
|
|
|
def send(self, body, title='', notify_type=NotifyType.INFO, **kwargs):
|
2018-09-17 00:27:00 +00:00
|
|
|
"""
|
|
|
|
Perform Windows Notification
|
|
|
|
"""
|
|
|
|
|
2019-06-07 11:16:07 +00:00
|
|
|
# Always call throttle before any remote server i/o is made
|
|
|
|
self.throttle()
|
2018-09-17 00:27:00 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
# Register destruction callback
|
|
|
|
message_map = {win32con.WM_DESTROY: self._on_destroy, }
|
|
|
|
|
|
|
|
# Register the window class.
|
|
|
|
self.wc = win32gui.WNDCLASS()
|
|
|
|
self.hinst = self.wc.hInstance = win32api.GetModuleHandle(None)
|
|
|
|
self.wc.lpszClassName = str("PythonTaskbar")
|
|
|
|
self.wc.lpfnWndProc = message_map
|
|
|
|
self.classAtom = win32gui.RegisterClass(self.wc)
|
|
|
|
|
|
|
|
# Styling and window type
|
|
|
|
style = win32con.WS_OVERLAPPED | win32con.WS_SYSMENU
|
|
|
|
self.hwnd = win32gui.CreateWindow(
|
|
|
|
self.classAtom, "Taskbar", style, 0, 0,
|
|
|
|
win32con.CW_USEDEFAULT, win32con.CW_USEDEFAULT, 0, 0,
|
|
|
|
self.hinst, None)
|
|
|
|
win32gui.UpdateWindow(self.hwnd)
|
|
|
|
|
2019-06-07 11:16:07 +00:00
|
|
|
# image path (if configured to acquire)
|
|
|
|
icon_path = None if not self.include_image \
|
|
|
|
else self.image_path(notify_type, extension='.ico')
|
2018-09-17 00:27:00 +00:00
|
|
|
|
2019-06-07 11:16:07 +00:00
|
|
|
if icon_path:
|
|
|
|
icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE
|
2018-09-17 00:27:00 +00:00
|
|
|
|
2019-06-07 11:16:07 +00:00
|
|
|
try:
|
|
|
|
hicon = win32gui.LoadImage(
|
|
|
|
self.hinst, icon_path, win32con.IMAGE_ICON, 0, 0,
|
|
|
|
icon_flags)
|
2018-09-17 00:27:00 +00:00
|
|
|
|
2019-06-07 11:16:07 +00:00
|
|
|
except Exception as e:
|
|
|
|
self.logger.warning(
|
|
|
|
"Could not load windows notification icon ({}): {}"
|
|
|
|
.format(icon_path, e))
|
|
|
|
|
|
|
|
# disable icon
|
|
|
|
hicon = win32gui.LoadIcon(0, win32con.IDI_APPLICATION)
|
|
|
|
else:
|
2018-09-17 00:27:00 +00:00
|
|
|
# disable icon
|
|
|
|
hicon = win32gui.LoadIcon(0, win32con.IDI_APPLICATION)
|
|
|
|
|
|
|
|
# Taskbar icon
|
|
|
|
flags = win32gui.NIF_ICON | win32gui.NIF_MESSAGE | win32gui.NIF_TIP
|
|
|
|
nid = (self.hwnd, 0, flags, win32con.WM_USER + 20, hicon,
|
|
|
|
"Tooltip")
|
|
|
|
win32gui.Shell_NotifyIcon(win32gui.NIM_ADD, nid)
|
|
|
|
win32gui.Shell_NotifyIcon(win32gui.NIM_MODIFY, (
|
|
|
|
self.hwnd, 0, win32gui.NIF_INFO, win32con.WM_USER + 20, hicon,
|
|
|
|
"Balloon Tooltip", body, 200, title))
|
|
|
|
|
|
|
|
# take a rest then destroy
|
|
|
|
sleep(self.duration)
|
|
|
|
win32gui.DestroyWindow(self.hwnd)
|
|
|
|
win32gui.UnregisterClass(self.wc.lpszClassName, None)
|
|
|
|
|
|
|
|
self.logger.info('Sent Windows notification.')
|
|
|
|
|
2022-10-11 01:19:24 +00:00
|
|
|
except Exception as e:
|
2018-09-17 00:27:00 +00:00
|
|
|
self.logger.warning('Failed to send Windows notification.')
|
2022-10-11 01:19:24 +00:00
|
|
|
self.logger.debug('Windows Exception: {}', str(e))
|
2018-09-17 00:27:00 +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',
|
|
|
|
'duration': str(self.duration),
|
|
|
|
}
|
|
|
|
|
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=NotifyWindows.urlencode(params),
|
2019-06-07 11:16:07 +00:00
|
|
|
)
|
|
|
|
|
2018-09-17 00:27:00 +00:00
|
|
|
@staticmethod
|
|
|
|
def parse_url(url):
|
|
|
|
"""
|
|
|
|
There are no parameters nessisary for this protocol; simply having
|
|
|
|
windows:// 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))
|
|
|
|
|
|
|
|
# Set duration
|
|
|
|
try:
|
|
|
|
results['duration'] = int(results['qsd'].get('duration'))
|
|
|
|
|
|
|
|
except (TypeError, ValueError):
|
|
|
|
# Not a valid integer; ignore entry
|
|
|
|
pass
|
|
|
|
|
|
|
|
# return results
|
|
|
|
return results
|