screenlets.plugins.Listen

Package screenlets :: Package plugins :: Module Listen |
[hide private]
[frames] | no frames]

# Source Code for Module screenlets.plugins.Listen

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