Package screenlets :: Package plugins :: Module Sonata

[hide private]
[frames] | no frames]

# Source Code for Module screenlets.plugins.Sonata

      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  #  Sonata API (c) Whise (Helder Fraga) 2008 <helder.fraga@hotmail.com>
      9
     10
     11  import os
     12  import dbus
     13  import gobject
     14  import mpdclient2
     15  from GenericPlayer import GenericAPI
     16
     17 -class SonataAPI(GenericAPI):
     18      __name__ = 'Sonata API'
     19      __version__ = '0.0'
     20      __author__ = ''
     21      __desc__ = ''
     22
     23
     24      playerAPI = None
     25
     26      __timeout = None
     27      __interval = 2
     28
     29      callbackFn = None
     30      __curplaying = None
     31
     32
     33      ns = "org.MPD.Sonata"
     34      iroot = "/org/MPD/Sonata"
     35      iface = "org.MPD.SonataInterface"
     36
     37      host = 'localhost'
     38      port = 6600
     39      musicdir = '/media/MULTIMEDIA/music/'
     40
     41 -    def __init__(self, session_bus):
     42          GenericAPI.__init__(self, session_bus)
     43
     44      # Check if the player is active : Returns Boolean
     45      # A handle to the dbus interface is passed in : doesn't need to be used
     46      # if there are other ways of checking this (like dcop in amarok)
     47 -    def is_active(self, dbus_iface):
     48          if self.ns in dbus_iface.ListNames(): return True
     49          else: return False
     50
     51      # Make a connection to the Player
     52 -    def connect(self):
     53          proxy_obj = self.session_bus.get_object(self.ns, self.iroot)
     54          self.playerAPI = dbus.Interface(proxy_obj, self.iface)
     55
     56      # The following return Strings
     57 -    def get_title(self):
     58          song = mpdclient2.connect().currentsong()
     59          return song.title
     60
     61 -    def get_album(self):
     62          song = mpdclient2.connect().currentsong()
     63          return song.album
     64
     65 -    def get_artist(self):
     66          song = mpdclient2.connect().currentsong()
     67          return song.artist
     68
     69 -    def get_cover_path(self):
     70                  artist = self.get_artist()
     71                  album = self.get_album()
     72                  filename = os.path.expanduser("~/.covers/" + artist + "-" + album + ".jpg")
     73                  if os.path.isfile(filename):
     74                          return filename
     75
     76                  try:
     77                          t = mpdclient2.connect().currentsong().file
     78                          t = t.replace('file://','')
     79                          t = t.split('/')
     80                          basePath = ''
     81                          for l in t:
     82                                  if l.find('.') == -1:
     83                                          basePath = basePath + l +'/'
     84
     85                          names = ['Album', 'Cover', 'Folde']
     86                          for x in os.listdir(basePath):
     87                                  if os.path.splitext(x)[1] in [".jpg", ".png"] and (x.capitalize()[:5] in names):
     88                                          coverFile = basePath + x
     89                                          return coverFile
     90                  except: return ''
     91                  return ''
     92
     93
     94      # Returns Boolean
     95 -    def is_playing(self):
     96                  status = mpdclient2.connect().status()
     97                  return (status.state != 'stop')
     98
     99      # The following do not return any values
    100 -    def play_pause(self):
    101                  status = mpdclient2.connect().status()
    102                  if status.state == 'play':
    103                          mpdclient2.connect().pause(1)
    104                  elif status.state == 'pause':
    105                          mpdclient2.connect().pause(0)
    106                  else:
    107                          mpdclient2.connect().play()
    108
    109 -    def next(self):
    110          mpdclient2.connect().next()
    111
    112 -    def previous(self):
    113          mpdclient2.connect().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
    131              playinguri = self.get_title()
    132              if self.is_playing() and self.__curplaying != playinguri:
    133                  self.__curplaying = playinguri
    134                  self.callback_fn()
    135              self.__timeout = gobject.timeout_add(self.__interval * 1000, self.info_changed)
    136          except:
    137              # The player exited ? call callback function
    138              self.callback_fn()
    139