-
Notifications
You must be signed in to change notification settings - Fork 1
/
__init__.py
268 lines (212 loc) · 8.24 KB
/
__init__.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# Rhythmbox Skype Mood Notifier - updates skype mood text from playlist
# Copyright (C) 2010 Christopher Gabijan
#
# This program 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 3 of the License, or
# (at your option) any later version.
#
# This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
import cgi
import gtk, gtk.glade, gconf
import rhythmdb, rb
from DBusSkype import SkypeRhythmboxMediator
from string import Template
STRM_SONG_ARTIST = 'rb:stream-song-artist'
STRM_SONG_TITLE = 'rb:stream-song-title'
STRM_SONG_ALBUM = 'rb:stream-song-album'
CONF_KEY_MOOD = '/apps/rhythmbox/plugins/rbskypemoodnotify/MoodFormat'
CONF_KEY_PAUSE = '/apps/rhythmbox/plugins/rbskypemoodnotify/PauseMessage'
CONF_VAL_DEFAULT_MOOD = '<SS type="music">d(^_^)b</SS> $TITLE ($ALBUM) - $ARTIST'
CONF_VAL_DEFAULT_PAUSE = '<SS type="talking">\(^_^)_</SS> ...'
class RhythmboxSkypeMoodNotifier(rb.Plugin):
def __init__(self):
rb.Plugin.__init__(self)
def activate(self, shell):
self.shell = shell
self.player = shell.get_player()
self.skype = SkypeRhythmboxMediator(self.got_ringHi,self.got_ringLo)
self.skype.sk_hooking()
self.pc_id = self.player.connect('playing-changed', self.playing_changed)
self.psc_id = self.player.connect('playing-song-changed', self.song_changed)
self.pspc_id = self.player.connect('playing-song-property-changed', self.song_property_changed)
self.skypeEvent = False
self.hist_mood = None
#gui
self.dialog = None
self.txFormat = None
self.txPause = None
#for configuration
self.conf_client = gconf.client_get_default()
self.mood_msg = None
self.pause_msg = None
self.loadConfig()
def deactivate(self,shell):
self.skype.setMood(self.skype.getOldMood())
if(self.skype): self.skype.sk_unHooking()
self.skype.deref()
if(self.player): self.player = None
self.shell.get_player().disconnect (self.pspc_id)
self.shell.get_player().disconnect (self.psc_id)
self.shell.get_player().disconnect (self.pc_id)
self.hist_mood = None
del self.skypeEvent
del self.pspc_id
del self.psc_id
del self.pc_id
del self.shell
del self.player
del self.skype
#gui
del self.dialog
del self.txFormat
del self.txPause
#for configuration
del self.conf_client
del self.mood_msg
del self.pause_msg
# routine returns:
# 1 : play
# 2 : stop
# 3 : paused
def playerStatus(self):
retval = 0
if self.player.props.playing :
retval = 1
else :
entry = self.player.get_playing_entry()
if entry is None :
retval = 2
else :
retval = 3
return retval
def got_ringHi(self) :
# toggle pause here so that
# it won't garbage in gaat inspection
self.player.pause()
self.skypeEvent = True
def got_ringLo(self) :
self.skypeEvent = False
self.player.play()
def playing_changed(self, player, playing) :
db = self.shell.get_property('db')
entry = player.get_playing_entry()
self.gaat(db,entry)
def song_changed(self, player, entry):
db = self.shell.get_property('db')
self.gaat(db,entry)
def song_property_changed(self, player, uri, prop, old_val, new_val):
db = self.shell.get_property('db')
entry = player.get_playing_entry()
self.gaat(db,entry)
# Get Attributes And Transform
# a single point of entry where we manipulate
# everything to simplify modifications in the
# future
def gaat(self,db, entry):
if db is None :
self.skype.setMood(self.skype.getOldMood())
return
if entry is None:
self.skype.setMood(self.skype.getOldMood())
return
artist = None
album = None
title = None
#experimental from :: Library - Radio
station = None
artist = db.entry_get(entry, rhythmdb.PROP_ARTIST)
album = db.entry_get(entry, rhythmdb.PROP_ALBUM)
title = db.entry_get(entry, rhythmdb.PROP_TITLE)
#TODO:find a nice solution to this one
# quick and dirty hack for ubuntu 10.10 netbook
if hasattr(entry.get_entry_type(), 'props') :
if (entry.get_entry_type().props.category == rhythmdb.ENTRY_STREAM) :
station = title
artist = db.entry_request_extra_metadata(entry,STRM_SONG_ARTIST)
album = db.entry_request_extra_metadata(entry,STRM_SONG_ALBUM)
title = db.entry_request_extra_metadata(entry,STRM_SONG_TITLE)
else :
if (entry.get_entry_type().category == rhythmdb.ENTRY_STREAM) :
station = title
artist = db.entry_request_extra_metadata(entry,STRM_SONG_ARTIST)
album = db.entry_request_extra_metadata(entry,STRM_SONG_ALBUM)
title = db.entry_request_extra_metadata(entry,STRM_SONG_TITLE)
#filter input by using cgi.escape
#proposed fix for issue 01 @ github by koze
artist = cgi.escape(artist)
album = cgi.escape(album)
title = cgi.escape(title)
formatted_mood = self.format_resp(artist, title, album)
if (station is not None) : formatted_mood = "%s :: %s" % (station,formatted_mood)
if (self.skypeEvent and self.playerStatus() == 1 ) :
self.skype.fSetMood(formatted_mood)
self.hist_mood = formatted_mood
self.skypeEvent = False
return
if self.playerStatus() == 1 :
pass
if self.playerStatus() == 2 :
formatted_mood = self.skype.getOldMood()
elif self.playerStatus() == 3 :
formatted_mood = self.pause_msg
if not (self.hist_mood == formatted_mood) :
self.skype.setMood(formatted_mood)
self.hist_mood = formatted_mood
self.skypeEvent = False
return
def format_resp(self, artist, title, album):
retval = Template(self.mood_msg)
if artist is None : artist = ''
if title is None : title = ''
if album is None : album = ''
return retval.substitute(TITLE=title, ARTIST=artist, ALBUM=album)
def create_configure_dialog(self, dialog=None):
axdic = {
'on_btnCancel_clicked' : self.hideDialog,
'on_btnSave_clicked' : self.saveConfigFromDialog
}
if not dialog:
glade_file = self.find_file("pref.dialog")
gladexml = gtk.glade.XML(glade_file)
gladexml.signal_autoconnect(axdic)
self.dialog = gladexml.get_widget('dialog1')
self.txFormat = gladexml.get_widget('txFormat')
self.txPause = gladexml.get_widget('txPause')
if self.mood_msg is None :
self.mood_msg = CONF_VAL_DEFAULT_MOOD
if self.pause_msg is None :
self.mood_msg = CONF_VAL_DEFAULT_PAUSE
self.txFormat.set_text(self.mood_msg)
self.txPause.set_text(self.pause_msg)
return self.dialog
def loadConfig(self):
self.mood_msg = self.conf_client.get_string(CONF_KEY_MOOD)
self.pause_msg = self.conf_client.get_string(CONF_KEY_PAUSE)
if self.mood_msg is None:
self.conf_client.add_dir(CONF_KEY_MOOD,gconf.CLIENT_PRELOAD_NONE)
self.conf_client.set_string(CONF_KEY_MOOD,CONF_VAL_DEFAULT_MOOD)
self.mood_msg = self.conf_client.get_string(CONF_KEY_MOOD)
if self.pause_msg is None:
self.conf_client.add_dir(CONF_KEY_PAUSE,gconf.CLIENT_PRELOAD_NONE)
self.conf_client.set_string(CONF_KEY_PAUSE,CONF_VAL_DEFAULT_PAUSE)
self.pause_msg = self.conf_client.get_string(CONF_KEY_PAUSE)
#load listeners
self.conf_client.notify_add(CONF_KEY_MOOD,self.newMoodSetup)
self.conf_client.notify_add(CONF_KEY_PAUSE,self.newPauseSetup)
def newMoodSetup(self, client, *args, **kwargs):
self.mood_msg = client.get_string(CONF_KEY_MOOD)
def newPauseSetup(self, client, *args, **kwargs):
self.pause_msg = client.get_string(CONF_KEY_PAUSE)
def saveConfigFromDialog(self,widget):
self.conf_client.set_string(CONF_KEY_MOOD,self.txFormat.get_text())
self.conf_client.set_string(CONF_KEY_PAUSE,self.txPause.get_text())
self.dialog.hide()
def hideDialog(self,widget):
self.dialog.hide()