screenlets.plugins.Sonata

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

# Source Code for Module screenlets.plugins.Sonata

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