#############################
# Update: main loop of code #
#############################
def update(self):
global count
count += 1
try:
self.update_conn()
if self.conn():
self.status = self.conn().do.status()
self.songinfo = self.conn().do.currentsong()
self.update_state()
self.prevc = self.conn()
self.prevstatus = self.status
self.prevsonginfo = self.songinfo
except (mpdclient.socket.error, EOFError, AttributeError, IndexError):
global connections
connections = {}
self.prevstatus = None
except KeyboardInterrupt, e:
raise e
return True
def update_conn(self):
if self.conn() != self.prevc:
if self.conn() is None:
self.w.ppbutton.set_property('sensitive', False)
self.w.stopbutton.set_property('sensitive', False)
self.w.prevbutton.set_property('sensitive', False)
self.w.nextbutton.set_property('sensitive', False)
self.w.volumebutton.set_property('sensitive', False)
self.w.playlistbutton.set_property('sensitive', False)
self.w.rmbutton.set_property('sensitive', False)
self.w.clbutton.set_property('sensitive', False)
self.w.repeatbutton.set_property('sensitive', False)
self.w.shufflebutton.set_property('sensitive', False)
self.w.statusimage.set_from_stock(gtk.STOCK_NO, gtk.ICON_SIZE_SMALL_TOOLBAR)
self.w.updateimage.set_from_stock(gtk.STOCK_NO, gtk.ICON_SIZE_MENU)
try:
self.trayimage.set_from_stock('pygmy', gtk.ICON_SIZE_BUTTON)
except:
pass
self.update_progressbar(False)
self.update_songtitle()
self.update_windowtitle()
self.playlistdata.clear()
self.tooltips.set_tip(self.w.statusbox, "Not connected")
self.w.prefsbutton.grab_focus()
else:
self.w.ppbutton.set_property('sensitive', True)
self.w.stopbutton.set_property('sensitive', True)
self.w.prevbutton.set_property('sensitive', True)
self.w.nextbutton.set_property('sensitive', True)
self.w.volumebutton.set_property('sensitive', True)
self.w.playlistbutton.set_property('sensitive', True)
self.w.rmbutton.set_property('sensitive', True)
self.w.clbutton.set_property('sensitive', True)
self.w.repeatbutton.set_property('sensitive', True)
self.w.shufflebutton.set_property('sensitive', True)
self.w.statusimage.set_from_stock(gtk.STOCK_YES, gtk.ICON_SIZE_SMALL_TOOLBAR)
#self.w.updateimage.set_from_stock(gtk.STOCK_YES, gtk.ICON_SIZE_MENU)
self.tooltips.set_tip(self.w.statusbox, "Connected")
self.w.ppbutton.grab_focus()
self.prevc = self.conn()
def update_state(self):
if not hasattr(self.songinfo, 'artist'):
self.songinfo.artist = None
if not hasattr(self.songinfo, 'title'):
self.songinfo.title = None
if not hasattr(self.status, 'song'):
self.status.song = None
# Display current playlist
if self.prevstatus is None or self.prevstatus.playlist != self.status.playlist:
self.update_playlist()
# Update progress frequently if we're playing
if self.status.state in ['play', 'pause']:
self.update_progressbar(True)
# If state changes
if self.prevstatus is None or self.prevstatus.state != self.status.state:
# Update progressbar if the state changes too
self.update_progressbar(True)
self.update_windowtitle()
self.update_songtitle()
if self.status.state == 'stop':
self.w.ppimage.set_from_stock(gtk.STOCK_MEDIA_PLAY, gtk.ICON_SIZE_BUTTON)
try:
self.trayimage.set_from_stock('pygmy', gtk.ICON_SIZE_BUTTON)
except:
pass
self.tooltips.set_tip(self.w.ppbutton, "Play")
# Unbold playing song (if we were playing)
if self.prevstatus and self.prevstatus.state == 'play':
oldrow = int(self.prevsonginfo.pos)
snip = self.playlistdata[oldrow][1]
if snip[0:3] == '' and snip[-4:] == '':
self.playlistdata[oldrow][1] = snip[3:-4]
elif self.status.state == 'pause':
self.w.ppimage.set_from_stock(gtk.STOCK_MEDIA_PLAY, gtk.ICON_SIZE_BUTTON)
try:
self.trayimage.set_from_stock(gtk.STOCK_MEDIA_PAUSE, gtk.ICON_SIZE_BUTTON)
except:
pass
self.tooltips.set_tip(self.w.ppbutton, "Play")
elif self.status.state == 'play':
self.w.ppimage.set_from_stock(gtk.STOCK_MEDIA_PAUSE, gtk.ICON_SIZE_BUTTON)
try:
self.trayimage.set_from_stock(gtk.STOCK_MEDIA_PLAY, gtk.ICON_SIZE_BUTTON)
except:
pass
self.tooltips.set_tip(self.w.ppbutton, "Pause")
if self.status.state in ['play', 'pause']:
row = int(self.songinfo.pos)
self.playlistdata[row][1] = make_bold( self.playlistdata[row][1])
# Only update song info if song has changed
if self.prevstatus is None or self.prevstatus.song != self.status.song:
if self.stopaftercurrent == True:
self.conn().do.stop()
self.stopaftercurrent = False
return
self.update_windowtitle()
self.update_songtitle()
# Unbold all songs. If you have a better idea, let me know.
for song in self.playlistdata:
if song[1][0:3] == '' and song[1][-4:] == '':
self.playlistdata[song.path[0]][1] = song[1][3:-4]
if self.status.state in ['play', 'pause']:
row = int(self.songinfo.pos)
self.playlistdata[row][1] = make_bold(self.playlistdata[row][1])
self.tooltips.set_tip(self.trayeventbox, self.songinfo.artist + ':' + '\n' + self.songinfo.title)
self.after_expander_activate(self.w.expander)
# If shuffle/repeat have changed, update the button state
if self.prevstatus is None or self.status.repeat != self.prevstatus.repeat:
self.w.repeatbutton.handler_block(self.repeatid)
self.w.repeatbutton.set_active('1' == self.status.repeat)
self.w.repeatbutton.handler_unblock(self.repeatid)
if self.prevstatus is None or self.status.random != self.prevstatus.random:
self.w.shufflebutton.handler_block(self.shuffleid)
self.w.shufflebutton.set_active('1' == self.status.random)
self.w.shufflebutton.handler_unblock(self.shuffleid)
# Check if MPD is updating the library
if self.prevstatus and self.prevstatus.get('updating_db', 0) != self.status.get('updating_db', 0):
if self.status.get('updating_db', 0):
self.w.statusimage.set_from_stock(gtk.STOCK_REFRESH, gtk.ICON_SIZE_SMALL_TOOLBAR)
self.tooltips.set_tip(self.w.statusbox, "Updating...")
else:
self.w.statusimage.set_from_stock(gtk.STOCK_YES, gtk.ICON_SIZE_SMALL_TOOLBAR)
self.tooltips.set_tip(self.w.statusbox, "Connected")
if self.prevstatus and self.status.volume != self.prevstatus.volume:
self.w.volumescale.get_adjustment().set_value(int(self.status.volume))