603
by spe.stani.be at gmail
implemented notifications for linux |
1 |
# Copyright (C) 2009 www.stani.be
|
2 |
#
|
|
3 |
# This program is free software: you can redistribute it and/or modify
|
|
4 |
# it under the terms of the GNU General Public License as published by
|
|
5 |
# the Free Software Foundation, either version 3 of the License, or
|
|
6 |
# (at your option) any later version.
|
|
7 |
#
|
|
8 |
# This program is distributed in the hope that it will be useful,
|
|
9 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
10 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
11 |
# GNU General Public License for more details.
|
|
983
by spe.stani.be at gmail
implemented Growl support |
12 |
#
|
603
by spe.stani.be at gmail
implemented notifications for linux |
13 |
# You should have received a copy of the GNU General Public License
|
14 |
# along with this program. If not, see http://www.gnu.org/licenses/
|
|
15 |
||
983
by spe.stani.be at gmail
implemented Growl support |
16 |
# Follows PEP8
|
17 |
||
18 |
APP_NAME = 'notify.py' |
|
19 |
||
20 |
# pynotify (linux)
|
|
615
by spe.stani.be at gmail
implemented notification for mac and windows |
21 |
try: |
603
by spe.stani.be at gmail
implemented notifications for linux |
22 |
import pynotify |
623
by Stani Michiels
fix for segmentation fault |
23 |
import gobject |
24 |
gobject.threads_init() |
|
615
by spe.stani.be at gmail
implemented notification for mac and windows |
25 |
except ImportError: |
26 |
pynotify = None |
|
983
by spe.stani.be at gmail
implemented Growl support |
27 |
|
28 |
# Growl (Mac Os X)
|
|
29 |
if pynotify: |
|
30 |
Growl = None |
|
31 |
else: |
|
32 |
try: |
|
33 |
import Growl |
|
34 |
except ImportError: |
|
35 |
Growl = None |
|
36 |
||
37 |
# Toasterbox (Windows)
|
|
38 |
if pynotify or Growl: |
|
39 |
TB = None |
|
40 |
else: |
|
615
by spe.stani.be at gmail
implemented notification for mac and windows |
41 |
try: |
42 |
import wx |
|
974
by spe.stani.be at gmail
other.pyWx |
43 |
import other.pyWx.toasterbox as TB |
615
by spe.stani.be at gmail
implemented notification for mac and windows |
44 |
except ImportError: |
983
by spe.stani.be at gmail
implemented Growl support |
45 |
TB = None |
46 |
||
47 |
||
48 |
def register(app_name): |
|
49 |
global APP_NAME |
|
50 |
APP_NAME = app_name |
|
51 |
||
52 |
||
53 |
def init(app_name, icon=None): |
|
54 |
register(app_name) |
|
615
by spe.stani.be at gmail
implemented notification for mac and windows |
55 |
|
56 |
if pynotify: |
|
983
by spe.stani.be at gmail
implemented Growl support |
57 |
|
58 |
def init(app_name, icon=None): |
|
59 |
register(app_name) |
|
603
by spe.stani.be at gmail
implemented notifications for linux |
60 |
pynotify.init(app_name) |
983
by spe.stani.be at gmail
implemented Growl support |
61 |
|
615
by spe.stani.be at gmail
implemented notification for mac and windows |
62 |
def send(title, message, icon='gtk-dialog-info', wxicon=None, |
63 |
urgency=None, timeout=None): |
|
983
by spe.stani.be at gmail
implemented Growl support |
64 |
n = pynotify.Notification(title, message, icon) |
603
by spe.stani.be at gmail
implemented notifications for linux |
65 |
if urgency: |
66 |
n.set_urgency(getattr(pynotify, |
|
1567.1.3
by spe.stani.be at gmail
pep8 |
67 |
'URGENCY_%s' % urgency.upper())) |
603
by spe.stani.be at gmail
implemented notifications for linux |
68 |
if timeout: |
69 |
n.set_timeout(timeout) |
|
70 |
n.show() |
|
983
by spe.stani.be at gmail
implemented Growl support |
71 |
|
72 |
elif Growl: |
|
73 |
||
1545
by spe.stani.be at gmail
fix growl bug |
74 |
def init(app_name, icon=None): |
983
by spe.stani.be at gmail
implemented Growl support |
75 |
"""Create a growl notifier with appropriate icon if specified.
|
76 |
The notification classes default to [APP_NAME]. The user can
|
|
77 |
enable/disable notifications based on this class name."""
|
|
78 |
global growl |
|
79 |
register(app_name) |
|
80 |
if icon is None: |
|
81 |
icon = {} |
|
82 |
else: |
|
83 |
icon = {'applicationIcon': Growl.Image.imageFromPath(icon)} |
|
84 |
growl = Growl.GrowlNotifier(APP_NAME, [APP_NAME], **icon) |
|
85 |
||
86 |
def send(title, message, icon='gtk-dialog-info', wxicon=None, |
|
87 |
urgency=None, timeout=None): |
|
88 |
global growl |
|
89 |
growl.notify(APP_NAME, title, message) |
|
90 |
||
615
by spe.stani.be at gmail
implemented notification for mac and windows |
91 |
elif TB: |
983
by spe.stani.be at gmail
implemented Growl support |
92 |
|
93 |
def send(title, message, icon='gtk-dialog-info', |
|
1567.1.3
by spe.stani.be at gmail
pep8 |
94 |
wxicon=None, urgency=None, timeout=None): |
615
by spe.stani.be at gmail
implemented notification for mac and windows |
95 |
if wxicon == None: |
96 |
wxicon = wx.ArtProvider_GetBitmap(wx.ART_INFORMATION, |
|
983
by spe.stani.be at gmail
implemented Growl support |
97 |
wx.ART_OTHER, (48, 48)) |
98 |
tb = TB.ToasterBox(wx.GetApp().GetTopWindow(), |
|
615
by spe.stani.be at gmail
implemented notification for mac and windows |
99 |
TB.TB_COMPLEX, TB.DEFAULT_TB_STYLE, TB.TB_ONTIME) |
100 |
tb.SetPopupSize((300, 80)) |
|
101 |
tb.SetPopupPauseTime(5000) |
|
102 |
tb.SetPopupScrollSpeed(8) |
|
103 |
tb.SetPopupPositionByInt(3) |
|
983
by spe.stani.be at gmail
implemented Growl support |
104 |
|
615
by spe.stani.be at gmail
implemented notification for mac and windows |
105 |
#wx controls
|
106 |
tbpanel = tb.GetToasterBoxWindow() |
|
983
by spe.stani.be at gmail
implemented Growl support |
107 |
panel = wx.Panel(tbpanel, -1) |
615
by spe.stani.be at gmail
implemented notification for mac and windows |
108 |
panel.SetBackgroundColour(wx.WHITE) |
983
by spe.stani.be at gmail
implemented Growl support |
109 |
wxicon = wx.StaticBitmap(panel, -1, wxicon) |
110 |
title = wx.StaticText(panel, -1, title) |
|
615
by spe.stani.be at gmail
implemented notification for mac and windows |
111 |
message = wx.StaticText(panel, -1, message) |
983
by spe.stani.be at gmail
implemented Growl support |
112 |
|
615
by spe.stani.be at gmail
implemented notification for mac and windows |
113 |
# wx layout controls
|
983
by spe.stani.be at gmail
implemented Growl support |
114 |
ver_sizer = wx.BoxSizer(wx.VERTICAL) |
115 |
ver_sizer.Add(title, 0, wx.ALL, 4) |
|
116 |
ver_sizer.Add(message, 0, wx.ALL, 4) |
|
117 |
||
118 |
hor_sizer = wx.BoxSizer(wx.HORIZONTAL) |
|
1567.1.3
by spe.stani.be at gmail
pep8 |
119 |
hor_sizer.Add(wxicon, 0, wx.EXPAND | wx.ALIGN_CENTER_VERTICAL \ |
120 |
| wx.ALIGN_CENTER_HORIZONTAL | wx.ALL, 4) |
|
983
by spe.stani.be at gmail
implemented Growl support |
121 |
hor_sizer.Add(ver_sizer, 1, wx.EXPAND) |
615
by spe.stani.be at gmail
implemented notification for mac and windows |
122 |
hor_sizer.Layout() |
123 |
panel.SetSizer(hor_sizer) |
|
124 |
||
125 |
tb.AddPanel(panel) |
|
126 |
tb.Play() |
|
983
by spe.stani.be at gmail
implemented Growl support |
127 |
|
603
by spe.stani.be at gmail
implemented notifications for linux |
128 |
else: |
983
by spe.stani.be at gmail
implemented Growl support |
129 |
|
603
by spe.stani.be at gmail
implemented notifications for linux |
130 |
def send(*args, **keyw): |
131 |
pass
|