Package screenlets :: Package plugins :: Module Banshee

[hide private]
[frames] | no frames]

# Source Code for Module screenlets.plugins.Banshee

      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  # Banshee API by Whise and vrunner
     10
     11  import os
     12  import dbus
     13  import string
     14  import gobject
     15  import urllib
     16  from GenericPlayer import GenericAPI
     17
     18 -class BansheeAPI(GenericAPI):
     19          __name__ = 'Banshee API'
     20          __version__ = '0.0'
     21          __author__ = 'Whise and vrunner'
     22          __desc__ = 'API to the Banshee Music Player'
     23
     24          ns = "org.gnome.Banshee"
     25          iroot = "/org/gnome/Banshee/Player"
     26          iface = "org.gnome.Banshee.Core"
     27          playerAPI = None
     28
     29          __timeout = None
     30          __interval = 2
     31
     32          callbackFn = None
     33          __curplaying = None
     34
     35          # Extended Functions from the GenericAPI
     36
     37 -        def __init__(self, session_bus):
     38                  GenericAPI.__init__(self, session_bus)
     39
     40 -        def is_active(self, dbus_iface):
     41                  if self.ns in dbus_iface.ListNames(): return True
     42                  else:
     43                          self.ns = "org.bansheeproject.Banshee"
     44                          self.iroot = "/org/bansheeproject/Banshee/PlayerEngine"
     45                          self.iface = "org.bansheeproject.Banshee.PlayerEngine"
     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                  try:
     55                          return self.playerAPI.GetPlayingTitle()
     56
     57                  except:
     58                          return self.playerAPI.GetCurrentTrack()['name']
     59
     60 -        def get_album(self):
     61                  try:
     62                          return self.playerAPI.GetPlayingAlbum()
     63
     64                  except:
     65                          return self.playerAPI.GetCurrentTrack()['album']
     66
     67 -        def get_artist(self):
     68                  try:
     69                          return self.playerAPI.GetPlayingArtist()
     70
     71                  except:
     72                          return self.playerAPI.GetCurrentTrack()['artist']
     73
     74 -        def get_cover_path(self):
     75                  try:
     76                          return self.playerAPI.GetPlayingCoverUri()
     77                  except:
     78
     79                          t = self.playerAPI.GetCurrentUri().replace('file://','')
     80                          t = urllib.unquote(unicode.encode(t, 'utf-8'))
     81                          t = t.split('/')
     82                          basePath = ''
     83                          for l in t:
     84                                  if l.find('.') == -1:
     85                                          basePath = basePath + l +'/'
     86
     87                          names = ['Album', 'Cover', 'Folde']
     88                          for x in os.listdir(basePath):
     89                                  if os.path.splitext(x)[1] in [".jpg", ".png"] and (x.capitalize()[:5] in names):
     90                                          coverFile = basePath + x
     91                                          return coverFile
     92
     93 -        def is_playing(self):
     94                  try:
     95                          if self.playerAPI.GetPlayingStatus() == 1: return True
     96                          else: return False
     97                  except:
     98                          if self.playerAPI.GetCurrentState() == 'playing':return True
     99                          else: return False
    100
    101
    102 -        def play_pause(self):
    103                  self.playerAPI.TogglePlaying()
    104
    105 -        def next(self):
    106                  try:
    107                          self.playerAPI.Next()
    108                  except: os.system(' banshee-1 --next &')
    109
    110 -        def previous(self):
    111                  try:
    112                          self.playerAPI.Previous()
    113                  except: os.system(' banshee-1 --previous &')
    114
    115 -        def register_change_callback(self, fn):
    116                  self.callback_fn = fn
    117                  # Could not find a callback signal for Banshee, so just calling after some time interval
    118                  if self.__timeout:
    119                          gobject.source_remove(self.__timeout)
    120                  self.__timeout = gobject.timeout_add(self.__interval * 1000, self.info_changed)
    121
    122 -        def info_changed(self, signal=None):
    123                  # Only call the callback function if Data has changed
    124                  if self.__timeout:
    125                          gobject.source_remove(self.__timeout)
    126                  try:
    127                          if self.__curplaying != None and not self.is_playing():
    128                                  self.__curplaying = None
    129                                  self.callback_fn()
    130                          try:
    131                                  playinguri = self.playerAPI.GetPlayingUri()
    132                          except:
    133                                  playinguri = self.playerAPI.GetCurrentUri()
    134                          if self.is_playing() and self.__curplaying != playinguri:
    135                                  self.__curplaying = playinguri
    136                                  self.callback_fn()
    137                          self.__timeout = gobject.timeout_add(self.__interval * 1000, self.info_changed)
    138                  except:
    139                          # The player exited ? call callback function
    140                          self.callback_fn()
    141