forked from albertz/music-player
-
Notifications
You must be signed in to change notification settings - Fork 0
/
notifications.py
79 lines (65 loc) · 2.11 KB
/
notifications.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# MusicPlayer, https://github.com/albertz/music-player
# Copyright (c) 2012, Albert Zeyer, www.az2000.de
# All rights reserved.
# This code is under the 2-clause BSD license, see License.txt in the root directory of this project.
def macNotificationsMain():
from State import state
from player import PlayerEventCallbacks
import AppKit
import utils
if not hasattr(AppKit, "NSUserNotificationCenter"):
print "macNotificationsMain: NSUserNotificationCenter not available. this is only available since MacOSX 10.8"
return
pool = AppKit.NSAutoreleasePool.alloc().init()
appDelegate = AppKit.NSApplication.sharedApplication().delegate()
notifCenter = AppKit.NSUserNotificationCenter.defaultUserNotificationCenter()
notifCenter.setDelegate_(appDelegate)
def notifyCurSong():
notif = AppKit.NSUserNotification.alloc().init()
notif.setTitle_("MusicPlayer")
song = state.curSong
s = None
try:
s = utils.convertToUnicode(song.userString)
except: pass
notif.setInformativeText_(s)
notifCenter.deliverNotification_(notif)
#print "notification:", notif
for ev,args,kwargs in state.updates.read():
if ev is PlayerEventCallbacks.onSongChange:
notifyCurSong()
elif ev is PlayerEventCallbacks.onPlayingStateChange and kwargs["newState"] == True:
notifyCurSong()
del pool
def linuxNotificationsMain():
from State import state
from player import PlayerEventCallbacks
from utils import convertToUnicode
try:
import pynotify
except ImportError:
return
def notifyCurSong():
pynotify.init("MusicPlayer")
song = state.curSong
s = None
try:
s = convertToUnicode(song.userString)
except: pass
notif = pynotify.Notification(s)
notif.show()
#print "notification:", notif
for ev, args, kwargs in state.updates.read():
if ev is PlayerEventCallbacks.onSongChange:
notifyCurSong()
elif ev is PlayerEventCallbacks.onPlayingStateChange and kwargs["newState"] == True:
notifyCurSong()
del pool
def notificationsMain():
import sys
if sys.platform == "darwin":
macNotificationsMain()
elif sys.platform == "linux2":
linuxNotificationsMain()
else:
print "no notification implementation"