screenlets.plugins.Banshee

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

# Source Code for Module screenlets.plugins.Banshee

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