#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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

import os
import dbus
import gobject
from GenericPlayer import GenericPlayer
from common import Track

class ListenPlayer(GenericPlayer):
    __name__ = 'Listen'
    __version__ = '0.0'
    __author__ = 'Kamencesc <kamencesc@gmail.com>, Alexandr Grigorcea <cahr.gr@gmail.com>'
    __desc__ = 'API to the Listen Music Player'
    
    DBUS_NAME   = 'org.gnome.Listen'
    DBUS_OBJECT = '/org/gnome/listen'
    
    dbus_object = None
    timeout_id = None

    def is_active(self):
        if self.DBUS_NAME in self.dbus.ListNames():
            return True
        else:
            return False

    def connect(self):                        #Connect with dbus
        self.session_bus = dbus.SessionBus()
        self.dbus_object = self.session_bus.get_object(self.DBUS_NAME, self.DBUS_OBJECT)
        self.timeout_id = gobject.timeout_add(2000, self.check_new_song)
        return True
        
    def disconnect(self):
        if self.timeout_id:
            gobject.source_remove(self.timeout_id)
            
    def check_new_song(self):
        self.state_handler(track=True)
        return True
    
    def get_title(self):
        try:
            return self.dbus_object.get_title()
        except:
            return ""
    
    def get_album(self):
        try:
            return self.dbus_object.get_album()
        except:
            return ""
        
    def get_artist(self):
        try:
            return self.dbus_object.get_artist()
        except:
            return ""

    def get_cover_path(self):
        try:
            return self.dbus_object.get_cover_path()
        except:
            return None
    
    def is_playing(self):
        try:
            return self.dbus_object.playing()
        except:
            return ""

    def previous(self, widget, event):
        if self.active:
            self.dbus_object.previous(True)    

    def play(self, widget, event):
        if self.active:
            self.dbus_object.play_pause()
        
    def next(self, widget, event):
        if self.active:
            self.dbus_object.next()
    
    def get_track(self):
        data = {}
        data['title'] = self.dbus_object.get_title()
        data['artist'] = self.dbus_object.get_artist()
        data['album'] = self.dbus_object.get_album()
        
        location = self.dbus_object.get_uri()
        
        cover = self.search_cover(location, data)
        if not cover:
            try:
                cover = self.dbus_object.get_cover_path()
            except:
                cover = ''
        
        track = Track(
            title = data['title'],
            album = data['album'],
            artist= data['artist'],
            cover = cover
        )
        return track

