Package screenlets :: Package plugins :: Module Listen

[hide private]
[frames] | no frames]

# Source Code for Module screenlets.plugins.Listen

      1  # This application is released under the GNU General Public License
      2  # v3 (or, at your option, any later version). You can find the full
      3  # text of the license under http://www.gnu.org/licenses/gpl.txt.
      4  # By using, editing and/or distributing this software you agree to
      5  # the terms and conditions of this license.
      6  # Thank you for using free software!
      7
      8
      9  # Listen API by vrunner
     10
     11  import os
     12  import dbus
     13  import string
     14  import gobject
     15  from GenericPlayer import GenericAPI
     16
     17  #LISTEN = {'DBUS_NAME':'org.gnome.Listen','DBUS_OBJECT':'/org/gnome/listen', \
     18  #                       'DBUS_TITLE':'get_title()','DBUS_ALBUM':'get_album()', \
     19  #                       'DBUS_ARTIST':'get_artist()','DBUS_ART':'get_cover_path()',\
     20  #                       'DBUS_PLAYING':'playing()','PLAY_WORD':False}
     21
     22 -class ListenAPI(GenericAPI):
     23          __name__ = 'Listen API'
     24          __version__ = '0.0'
     25          __author__ = 'vrunner'
     26          __desc__ = 'API to the Listen Music Player'
     27
     28          ns = "org.gnome.Listen"
     29          iroot = "/org/gnome/listen"
     30          iface = "org.gnome.Listen"
     31
     32          playerAPI = None
     33
     34          __timeout = None
     35          __interval = 2
     36
     37          callbackFn = None
     38          __curplaying = None
     39
     40          # Extended Functions from the GenericAPI
     41
     42 -        def __init__(self, session_bus):
     43                  GenericAPI.__init__(self, session_bus)
     44
     45 -        def is_active(self, dbus_iface):
     46                  if self.ns in dbus_iface.ListNames(): return True
     47                  else: return False
     48
     49 -        def connect(self):
     50                  proxy_obj = self.session_bus.get_object(self.ns, self.iroot)
     51                  self.playerAPI = dbus.Interface(proxy_obj, self.iface)
     52
     53 -        def get_title(self):
     54                  return self.playerAPI.current_playing().split(" - ",3)[0]
     55                  #return self.playerAPI.get_title()
     56
     57 -        def get_album(self):
     58                  return self.playerAPI.current_playing().split(" - ",3)[1][1:]
     59                  #return self.playerAPI.get_album()
     60
     61 -        def get_artist(self):
     62                  artist = self.playerAPI.current_playing().split(" - ",3)[2]
     63                  return artist[0:len(artist)-1]
     64                  #return self.playerAPI.get_artist()
     65
     66 -        def get_cover_path(self):
     67                  return os.environ['HOME']+"/.listen/cover/"+\
     68                          string.lower(self.get_artist()+"+"+self.get_album()+".jpg")
     69                  #return self.playerAPI.get_cover_path()
     70
     71 -        def is_playing(self):
     72                  #if self.playerAPI.playing() == "False": return False
     73                  if self.playerAPI.current_playing() == "": return False
     74                  else: return True
     75
     76 -        def play_pause(self):
     77                  self.playerAPI.play_pause()
     78
     79 -        def next(self):
     80                  self.playerAPI.next()
     81
     82 -        def previous(self):
     83                  self.playerAPI.previous()
     84
     85 -        def register_change_callback(self, fn):
     86                  self.callback_fn = fn
     87                  # Could not find a callback signal for Listen, so just calling after some time interval
     88                  if self.__timeout:
     89                          gobject.source_remove(self.__timeout)
     90                  self.__timeout = gobject.timeout_add(self.__interval * 1000, self.info_changed)
     91                  #self.playerAPI.connect_to_signal("playingUriChanged", self.info_changed)
     92
     93 -        def info_changed(self, signal=None):
     94                  # Only call the callback function if Data has changed
     95                  if self.__timeout:
     96                          gobject.source_remove(self.__timeout)
     97                  try:
     98                          if self.__curplaying != self.playerAPI.current_playing():
     99                                  self.__curplaying = self.playerAPI.current_playing()
    100                                  self.callback_fn()
    101
    102                          self.__timeout = gobject.timeout_add(self.__interval * 1000, self.info_changed)
    103                  except:
    104                          # The player exited ? call callback function
    105                          self.callback_fn()
    106