#  methlab - A music library application
#  Copyright (C) 2007 Ingmar K. Steen (iksteen@gmail.com)
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

DRIVERS = ['AmarokDBusDriver']

try:
  from dbus import Bus, DBusException
except ImportError:
  DRIVERS = []

from gettext import gettext as _
import urllib

class AmarokDBusDriver:
  name = 'amarok-dbus'
  name_tr = _('Amarok (using DBus)')
  def __init__(self, methlab):
    self.bus = Bus(Bus.TYPE_SESSION)
    self.amarok = self.bus.get_object('org.kde.amarok', '/TrackList')

  def play_files(self, files):
    try:
      for x in range(1,self.amarok.GetLength()+1): self.amarok.DelTrack(x)
      uris = ['file://' + file for file in files]
      for file in uris:
        self.amarok.AddTrack(file,True)
    except DBusException:
      self.bus = Bus(Bus.TYPE_SESSION)
      self.amarok = self.bus.get_object('org.kde.amarok', '/TrackList')
      try:
        for x in range(1,self.amarok.GetLength()+1): self.amarok.DelTrack(x)
        uris = ['file://' + file for file in files]
        for file in uris:
          self.amarok.AddTrack(file,True)
      except DBusException:
        # FIXME: Should provide feedback on how to handle the problem
        pass


  def enqueue_files(self, files):
    try:
      uris = ['file://' + file for file in files]
      for file in uris:
        self.amarok.AddTrack(file,False)
    except DBusException:
      self.bus = Bus(Bus.TYPE_SESSION)
      self.amarok = self.bus.get_object('org.kde.amarok', '/TrackList')
      try:
        uris = ['file://' + file for file in files]
        for file in uris:
          self.amarok.AddTrack(file,False)
      except DBusException:
        # FIXME: Should provide feedback on how to handle the problem
        pass

