# -*- Mode: python; coding: utf-8; tab-width: 8; indent-tabs-mode: t; -*- 
#
# Copyright (C) 2006 - Beat Bolli <bbolli@ewanet.ch>
#
# 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, 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 St, Fifth Floor, Boston, MA 02110-1301  USA.

import rhythmdb, rb
import gtk, gobject
import urllib

BASE_URL = 'http://www.example.com/current_song.php?song='


class PostTunePlugin (rb.Plugin):
	def __init__ (self):
		rb.Plugin.__init__ (self)
                self.current_entry = None

	def activate (self, shell):
		self.shell = shell
		sp = shell.get_player()
		self.pec_id = sp.connect ('playing-song-changed', self.playing_entry_changed)
		self.pc_id = sp.connect ('playing-changed', self.playing_changed)
		entry = sp.get_playing_entry ()
		self.playing_entry_changed (sp, entry)

	def deactivate (self, shell):
		self.shell = None
		sp = shell.get_player ()
		sp.disconnect (self.pec_id)
		sp.disconnect (self.pc_id)

	def playing_changed (self, sp, playing):
		self.post_entry(sp.get_playing_entry ())

	def playing_entry_changed (self, sp, entry):
		self.post_entry(entry)

	def post_entry (self, entry):
		if entry == self.current_entry:
                        return
		self.current_entry = entry

		db = self.shell.get_property ("db")
		st_artist = db.entry_get (entry, rhythmdb.PROP_ARTIST) or _("Unknown")
		st_title = db.entry_get (entry, rhythmdb.PROP_TITLE) or _("Unknown")

                urllib.urlopen(BASE_URL + urllib.quote('%s - %s' % (st_artist, st_title)))