Update copyright years for 2013
[gpodder.git] / share / gpodder / extensions / notification-win32.py
blob20808f4ac84329af882fd1f893592a09199e1357
1 # -*- coding: utf-8 -*-
3 # gPodder - A media aggregator and podcast client
4 # Copyright (c) 2005-2013 Thomas Perl and the gPodder Team
6 # gPodder is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
11 # gPodder is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 # Notification implementation for Windows
21 # Sean Munkel; 2012-12-29
23 __title__ = 'Notification Bubbles for Windows'
24 __description__ = 'Display notification bubbles for different events.'
25 __authors__ = 'Sean Munkel <SeanMunkel@gmail.com>'
26 __category__ = 'desktop-integration'
27 __only_for__ = 'win32'
29 import functools
30 import os
31 import os.path
32 import gpodder
33 import pywintypes
34 import win32gui
36 import logging
38 logger = logging.getLogger(__name__)
40 IDI_APPLICATION = 32512
41 WM_TASKBARCREATED = win32gui.RegisterWindowMessage('TaskbarCreated')
42 WM_TRAYMESSAGE = 1044
44 # based on http://code.activestate.com/recipes/334779/
45 class NotifyIcon(object):
46 def __init__(self, hwnd):
47 self._hwnd = hwnd
48 self._id = 0
49 self._flags = win32gui.NIF_MESSAGE | win32gui.NIF_ICON
50 self._callbackmessage = WM_TRAYMESSAGE
51 path = os.path.join(os.path.dirname(__file__), '..', '..',
52 'icons', 'hicolor', '16x16', 'apps', 'gpodder.ico')
53 icon_path = os.path.abspath(path)
55 try:
56 self._hicon = win32gui.LoadImage(None, icon_path, 1, 0, 0, 0x50)
57 except pywintypes.error as e:
58 logger.warn("Couldn't load gpodder icon for tray")
59 self._hicon = win32gui.LoadIcon(0, IDI_APPLICATION)
61 self._tip = ''
62 self._info = ''
63 self._timeout = 0
64 self._infotitle = ''
65 self._infoflags = win32gui.NIIF_NONE
66 win32gui.Shell_NotifyIcon(win32gui.NIM_ADD, self.notify_config_data)
68 @property
69 def notify_config_data(self):
70 """ Function to retrieve the NOTIFYICONDATA Structure. """
71 return (self._hwnd, self._id, self._flags, self._callbackmessage,
72 self._hicon, self._tip, self._info, self._timeout,
73 self._infotitle, self._infoflags)
75 def remove(self):
76 """ Removes the tray icon. """
77 win32gui.Shell_NotifyIcon(win32gui.NIM_DELETE,
78 self.notify_config_data)
80 def set_tooltip(self, tooltip):
81 """ Sets the tray icon tooltip. """
82 self._flags = self._flags | win32gui.NIF_TIP
83 self._tip = tooltip.encode("mbcs")
84 win32gui.Shell_NotifyIcon(win32gui.NIM_MODIFY,
85 self.notify_config_data)
87 def show_balloon(self, title, text, timeout=10,
88 icon=win32gui.NIIF_NONE):
89 """ Shows a balloon tooltip from the tray icon. """
90 self._flags = self._flags | win32gui.NIF_INFO
91 self._infotitle = title.encode("mbcs")
92 self._info = text.encode("mbcs")
93 self._timeout = timeout * 1000
94 self._infoflags = icon
95 win32gui.Shell_NotifyIcon(win32gui.NIM_MODIFY,
96 self.notify_config_data)
99 class gPodderExtension(object):
100 def __init__(self, *args):
101 self.notifier = None
103 def on_ui_object_available(self, name, ui_object):
104 def callback(self, window, *args):
105 self.notifier = NotifyIcon(window.window.handle)
107 if name == 'gpodder-gtk':
108 ui_object.main_window.connect('realize',
109 functools.partial(callback, self))
111 def on_notification_show(self, title, message):
112 if self.notifier is not None:
113 self.notifier.show_balloon(title, message)
115 def on_unload(self):
116 if self.notifier is not None:
117 self.notifier.remove()