File: NotificationManager.py

package info (click to toggle)
radiotray 0.7.3-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 872 kB
  • ctags: 464
  • sloc: python: 2,946; xml: 83; makefile: 8
file content (93 lines) | stat: -rw-r--r-- 3,284 bytes parent folder | download | duplicates (2)
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
##########################################################################
# Copyright 2009 Carlos Ribeiro
#
# This file is part of Radio Tray
#
# Radio Tray is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 1 of the License, or
# (at your option) any later version.
#
# Radio Tray is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Radio Tray.  If not, see <http://www.gnu.org/licenses/>.
#
##########################################################################
from lib.common import APPNAME
from events.EventMngNotificationWrapper import EventMngNotificationWrapper
import urllib2
from lib.common import USER_AGENT, ICON_FILE
import logging
import traceback

class NotificationManager(object):

    def __init__(self, eventManagerWrapper):
        self.eventManagerWrapper = eventManagerWrapper
        self.log = logging.getLogger('radiotray')
        self.lastState = None
        
    def on_state_changed(self, data):
    
        state = data['state']
        
        if(state == 'playing' and state != self.lastState):
            station = data['station']
            self.lastState = state
            self.eventManagerWrapper.notify(_('Radio Tray Playing'), station)

            

    def on_song_changed(self, data):
    
        self.log.debug(data)
        
        station = data['station']
        msgTitle = "%s - %s" % (APPNAME , station)
        msg = None

        if('artist' in data.keys() and 'title' in data.keys()):
            artist = data['artist']
            title = data['title']
            msg = "%s - %s" % (artist, title)            
        elif('artist' in data.keys()):
            msg = data['artist']
        elif('title' in data.keys()):
            msg = data['title']

        if('homepage' in data.keys() and (data['homepage'].endswith('png') or data['homepage'].endswith('jpg'))):
            #download image
            try:
                req = urllib2.Request(data['homepage'])
                req.add_header('User-Agent', USER_AGENT)
                response = urllib2.urlopen(req)
                pix = response.read()
                f = open(ICON_FILE,'wb')
                try:
                    f.write(pix)
                except Exception, e:
                    log.warn('Error saving icon')
                finally:
                    f.close()

                self.eventManagerWrapper.notify_icon(msgTitle, msg, ICON_FILE)
                
            except Exception, e:
                traceback.print_exc()
                self.eventManagerWrapper.notify(msgTitle, msg)
        else:
            self.eventManagerWrapper.notify(msgTitle, msg)
        
    def on_station_error(self, data):
    
        self.eventManagerWrapper.notify(_('Radio Error'), str(data['error']))

    def on_bookmarks_reloaded(self, data):

        self.eventManagerWrapper.notify(_("Bookmarks Reloaded"), _("Bookmarks Reloaded"))