~ubuntu-branches/ubuntu/vivid/gpodder/vivid

« back to all changes in this revision

Viewing changes to share/gpodder/extensions/notification-win32.py

  • Committer: Package Import Robot
  • Author(s): tony mancill
  • Date: 2013-04-12 22:07:02 UTC
  • mfrom: (5.2.27 sid)
  • Revision ID: package-import@ubuntu.com-20130412220702-mux8v9r8zt2jin0x
Tags: 3.5.1-1
* New upstream release.
* d/control: declare versioned dependency on python-gtk2 (>= 2.16)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
#
 
3
# gPodder - A media aggregator and podcast client
 
4
# Copyright (c) 2005-2013 Thomas Perl and the gPodder Team
 
5
#
 
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.
 
10
#
 
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.
 
15
#
 
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/>.
 
18
#
 
19
 
 
20
# Notification implementation for Windows
 
21
# Sean Munkel; 2012-12-29
 
22
 
 
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
__mandatory_in__ = 'win32'
 
28
__only_for__ = 'win32'
 
29
 
 
30
import functools
 
31
import os
 
32
import os.path
 
33
import gpodder
 
34
import pywintypes
 
35
import win32gui
 
36
 
 
37
import logging
 
38
 
 
39
logger = logging.getLogger(__name__)
 
40
 
 
41
IDI_APPLICATION = 32512
 
42
WM_TASKBARCREATED = win32gui.RegisterWindowMessage('TaskbarCreated')
 
43
WM_TRAYMESSAGE = 1044
 
44
 
 
45
# based on http://code.activestate.com/recipes/334779/
 
46
class NotifyIcon(object):
 
47
    def __init__(self, hwnd):
 
48
        self._hwnd = hwnd
 
49
        self._id = 0
 
50
        self._flags = win32gui.NIF_MESSAGE | win32gui.NIF_ICON
 
51
        self._callbackmessage = WM_TRAYMESSAGE
 
52
        path = os.path.join(os.path.dirname(__file__), '..', '..',
 
53
                'icons', 'hicolor', '16x16', 'apps', 'gpodder.ico')
 
54
        icon_path = os.path.abspath(path)
 
55
 
 
56
        try:
 
57
            self._hicon = win32gui.LoadImage(None, icon_path, 1, 0, 0, 0x50)
 
58
        except pywintypes.error as e:
 
59
            logger.warn("Couldn't load gpodder icon for tray")
 
60
            self._hicon = win32gui.LoadIcon(0, IDI_APPLICATION)
 
61
 
 
62
        self._tip = ''
 
63
        self._info = ''
 
64
        self._timeout = 0
 
65
        self._infotitle = ''
 
66
        self._infoflags = win32gui.NIIF_NONE
 
67
        win32gui.Shell_NotifyIcon(win32gui.NIM_ADD, self.notify_config_data)
 
68
 
 
69
    @property
 
70
    def notify_config_data(self):
 
71
        """ Function to retrieve the NOTIFYICONDATA Structure. """
 
72
        return (self._hwnd, self._id, self._flags, self._callbackmessage,
 
73
                self._hicon, self._tip, self._info, self._timeout,
 
74
                self._infotitle, self._infoflags)
 
75
 
 
76
    def remove(self):
 
77
        """ Removes the tray icon. """
 
78
        win32gui.Shell_NotifyIcon(win32gui.NIM_DELETE,
 
79
                self.notify_config_data)
 
80
 
 
81
    def set_tooltip(self, tooltip):
 
82
        """ Sets the tray icon tooltip. """
 
83
        self._flags = self._flags | win32gui.NIF_TIP
 
84
        self._tip = tooltip.encode("mbcs")
 
85
        win32gui.Shell_NotifyIcon(win32gui.NIM_MODIFY,
 
86
                self.notify_config_data)
 
87
 
 
88
    def show_balloon(self, title, text, timeout=10,
 
89
            icon=win32gui.NIIF_NONE):
 
90
        """ Shows a balloon tooltip from the tray icon. """
 
91
        self._flags = self._flags | win32gui.NIF_INFO
 
92
        self._infotitle = title.encode("mbcs")
 
93
        self._info = text.encode("mbcs")
 
94
        self._timeout = timeout * 1000
 
95
        self._infoflags = icon
 
96
        win32gui.Shell_NotifyIcon(win32gui.NIM_MODIFY,
 
97
                self.notify_config_data)
 
98
 
 
99
 
 
100
class gPodderExtension(object):
 
101
    def __init__(self, *args):
 
102
        self.notifier = None
 
103
 
 
104
    def on_ui_object_available(self, name, ui_object):
 
105
        def callback(self, window, *args):
 
106
            self.notifier = NotifyIcon(window.window.handle)
 
107
 
 
108
        if name == 'gpodder-gtk':
 
109
            ui_object.main_window.connect('realize',
 
110
                    functools.partial(callback, self))
 
111
 
 
112
    def on_notification_show(self, title, message):
 
113
        if self.notifier is not None:
 
114
            self.notifier.show_balloon(title, message)
 
115
 
 
116
    def on_unload(self):
 
117
        if self.notifier is not None:
 
118
            self.notifier.remove()
 
119