2018-09-17 08:27:00 +08:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2019-06-07 19:16:07 +08:00
|
|
|
# Copyright (C) 2019 Chris Caron <lead2gold@gmail.com>
|
|
|
|
# All rights reserved.
|
2018-09-17 08:27:00 +08:00
|
|
|
#
|
2019-06-07 19:16:07 +08:00
|
|
|
# This code is licensed under the MIT License.
|
2018-09-17 08:27:00 +08:00
|
|
|
#
|
2019-06-07 19:16:07 +08:00
|
|
|
# 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 :
|
2018-09-17 08:27:00 +08:00
|
|
|
#
|
2019-06-07 19:16:07 +08:00
|
|
|
# The above copyright notice and this permission notice shall be included in
|
|
|
|
# all copies or substantial portions of the Software.
|
2018-09-17 08:27:00 +08:00
|
|
|
#
|
2019-06-07 19:16:07 +08:00
|
|
|
# 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.
|
|
|
|
|
2018-09-17 08:27:00 +08: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 19:16:07 +08:00
|
|
|
from ..common import NotifyType
|
|
|
|
from ..utils import parse_bool
|
|
|
|
from ..AppriseLocale import gettext_lazy as _
|
2018-09-17 08:27:00 +08: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
|
|
|
|
# either using Linux, or simply do not have pypiwin32 installed.
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class NotifyWindows(NotifyBase):
|
|
|
|
"""
|
|
|
|
A wrapper for local Windows Notifications
|
|
|
|
"""
|
|
|
|
|
2018-10-23 00:39:11 +08:00
|
|
|
# The default descriptive name associated with the Notification
|
|
|
|
service_name = 'Windows Notification'
|
|
|
|
|
2018-09-17 08:27:00 +08:00
|
|
|
# The default protocol
|
|
|
|
protocol = 'windows'
|
|
|
|
|
2018-10-23 00:39:11 +08: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 19:16:07 +08:00
|
|
|
# Disable throttle rate for Windows requests since they are normally
|
|
|
|
# local anyway
|
|
|
|
request_rate_per_sec = 0
|
|
|
|
|
2018-09-17 08:27:00 +08:00
|
|
|
# Allows the user to specify the NotifyImageSize object
|
|
|
|
image_size = NotifyImageSize.XY_128
|
|
|
|
|
2019-06-07 19:16:07 +08: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
|
|
|
|
|
2018-09-17 08:27:00 +08:00
|
|
|
# This entry is a bit hacky, but it allows us to unit-test this library
|
|
|
|
# in an environment that simply doesn't have the windows packages
|
|
|
|
# available to us. It also allows us to handle situations where the
|
|
|
|
# packages actually are present but we need to test that they aren't.
|
|
|
|
# If anyone is seeing this had knows a better way of testing this
|
|
|
|
# outside of what is defined in test/test_windows_plugin.py, please
|
|
|
|
# let me know! :)
|
|
|
|
_enabled = NOTIFY_WINDOWS_SUPPORT_ENABLED
|
|
|
|
|
2019-06-07 19:16:07 +08:00
|
|
|
# Define object templates
|
|
|
|
templates = (
|
|
|
|
'{schema}://_/',
|
|
|
|
)
|
|
|
|
|
|
|
|
# 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 08:27:00 +08:00
|
|
|
"""
|
|
|
|
Initialize Windows Object
|
|
|
|
"""
|
|
|
|
|
2019-06-07 19:16:07 +08:00
|
|
|
super(NotifyWindows, self).__init__(**kwargs)
|
|
|
|
|
2018-09-17 08:27:00 +08:00
|
|
|
# Number of seconds to display notification for
|
2019-06-07 19:16:07 +08:00
|
|
|
self.duration = self.default_popup_duration_sec \
|
|
|
|
if not (isinstance(duration, int) and duration > 0) else duration
|
2018-09-17 08:27:00 +08:00
|
|
|
|
|
|
|
# Define our handler
|
|
|
|
self.hwnd = None
|
|
|
|
|
2019-06-07 19:16:07 +08:00
|
|
|
# Track whether or not we want to send an image with our notification
|
|
|
|
# or not.
|
|
|
|
self.include_image = include_image
|
2018-09-17 08:27:00 +08: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)
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
2019-06-07 19:16:07 +08:00
|
|
|
def send(self, body, title='', notify_type=NotifyType.INFO, **kwargs):
|
2018-09-17 08:27:00 +08:00
|
|
|
"""
|
|
|
|
Perform Windows Notification
|
|
|
|
"""
|
|
|
|
|
|
|
|
if not self._enabled:
|
|
|
|
self.logger.warning(
|
|
|
|
"Windows Notifications are not supported by this system.")
|
|
|
|
return False
|
|
|
|
|
2019-06-07 19:16:07 +08:00
|
|
|
# Always call throttle before any remote server i/o is made
|
|
|
|
self.throttle()
|
2018-09-17 08:27:00 +08: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 19:16:07 +08: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 08:27:00 +08:00
|
|
|
|
2019-06-07 19:16:07 +08:00
|
|
|
if icon_path:
|
|
|
|
icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE
|
2018-09-17 08:27:00 +08:00
|
|
|
|
2019-06-07 19:16:07 +08:00
|
|
|
try:
|
|
|
|
hicon = win32gui.LoadImage(
|
|
|
|
self.hinst, icon_path, win32con.IMAGE_ICON, 0, 0,
|
|
|
|
icon_flags)
|
2018-09-17 08:27:00 +08:00
|
|
|
|
2019-06-07 19:16:07 +08: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 08:27:00 +08: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.')
|
|
|
|
|
2019-06-07 19:16:07 +08:00
|
|
|
except Exception:
|
2018-09-17 08:27:00 +08:00
|
|
|
self.logger.warning('Failed to send Windows notification.')
|
|
|
|
self.logger.exception('Windows Exception')
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
2019-06-07 19:16:07 +08:00
|
|
|
def url(self):
|
|
|
|
"""
|
|
|
|
Returns the URL built dynamically based on specified arguments.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Define any arguments set
|
|
|
|
args = {
|
|
|
|
'format': self.notify_format,
|
|
|
|
'overflow': self.overflow_mode,
|
|
|
|
'image': 'yes' if self.include_image else 'no',
|
|
|
|
'duration': str(self.duration),
|
|
|
|
'verify': 'yes' if self.verify_certificate else 'no',
|
|
|
|
}
|
|
|
|
|
|
|
|
return '{schema}://_/?{args}'.format(
|
|
|
|
schema=self.protocol,
|
|
|
|
args=NotifyWindows.urlencode(args),
|
|
|
|
)
|
|
|
|
|
2018-09-17 08:27:00 +08: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.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2019-06-07 19:16:07 +08:00
|
|
|
results = NotifyBase.parse_url(url)
|
|
|
|
if not results:
|
|
|
|
results = {
|
|
|
|
'schema': NotifyWindows.protocol,
|
|
|
|
'user': None,
|
|
|
|
'password': None,
|
|
|
|
'port': None,
|
|
|
|
'host': '_',
|
|
|
|
'fullpath': None,
|
|
|
|
'path': None,
|
|
|
|
'url': url,
|
|
|
|
'qsd': {},
|
|
|
|
}
|
|
|
|
|
|
|
|
# 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
|