def onSeek(self, elapsed):
		print "## seek to:", elapsed
		if self.lyrics:
			self.lyricsTimer.pause()
			self.lyricsTimer = LyricsTimer(self.lyrics, self.update)
			self.lyricsTimer.start(elapsed)
			self.label.set_text(self.lyrics[self.lyricsTimer.actualLine].text[0].strip())
	def onLyricsFound(self, lyrics):
		if not self.lyrics and lyrics:
			print "onLyricsFound"
			try:
				parsed = lyricsparser.parseLyrics(lyrics)
				if isinstance(parsed[0], LyricEntity):
					self.lyrics = parsed
					self.lyricsTimer = LyricsTimer(self.lyrics ,self.update)
					elapsed = self.player.getElapsed()
					#print "elapsed:", elapsed
					self.lyricsTimer.start(elapsed)
					self.label.set_text(self.lyrics[self.lyricsTimer.actualLine].text[0].strip())
					self.lyricsEngine.stop()
			except:
				traceback.print_exc()
class Lyrics(object):

	color = gtk.gdk.Color()
	font = "Sans 10"
	applet = None
	label = None
	lyrics_directory = "/home/dencer/Lyrics"

	def __init__(self):
		self.lyrics = None
		self.lyricsTimer = None
		self.player = Player()
		self.player.registerOnPlayerConnected(self.onPlayerConnected)
		self.player.registerOnSongChange(self.onSongChanged)
		self.player.registerOnPlay(self.onPlay)
		self.player.registerOnStop(self.onStop)
		self.player.registerOnElapsedChanged(self.onSeek)
		self.lyricsEngine = LyricsEngine(self.onLyricsFound, self.onEngineFinish)
		self.lyricsEngine.setLyricsSources(['alsong', 'minilyrics', 'lrcdb', 'lyricsscreenlet'])
		
		self.label = gtk.Label("Lyrics Applet")
		self.gconf_client = gconf.client_get_default()
		self.gconf_client.add_dir("/apps/lyrics_applet", gconf.CLIENT_PRELOAD_NONE)
		self.gconf_client.notify_add('/apps/lyrics_applet/color', self.color_changed)
		self.gconf_client.notify_add('/apps/lyrics_applet/font', self.font_changed)
		
		self.color_changed(None)
		self.font_changed(None)


	def color_changed(self, client, *args):
		color = self.gconf_client.get_string("/apps/lyrics_applet/color")
		if color:
			self.color = gtk.gdk.Color(color)
			self.label.modify_fg(gtk.STATE_NORMAL, self.color)

	def font_changed(self, client, *args):
		font = self.gconf_client.get_string("/apps/lyrics_applet/font")
		if font:
			self.font = font
			font_desc = pango.FontDescription(font)
			self.label.modify_font(font_desc)

	def onPlayerConnected(self):
		print "onPlayerConnected"

	def onStop(self):
		if self.lyricsTimer:
			self.lyricsTimer.pause()

	def onPlay(self):
		if self.lyricsTimer:
			self.lyricsTimer.resume()

	def onSeek(self, elapsed):
		print "## seek to:", elapsed
		if self.lyrics:
			self.lyricsTimer.pause()
			self.lyricsTimer = LyricsTimer(self.lyrics, self.update)
			self.lyricsTimer.start(elapsed)
			self.label.set_text(self.lyrics[self.lyricsTimer.actualLine].text[0].strip())

	def onSongChanged(self, songFile):
		try:
			self.lyrics = None
			if self.lyricsTimer:
				self.lyricsTimer.pause()
				self.lyricsTimer = None
		
			print "onSongChanged"
			metadata = getSongMetadata(self.player)
			metadata['file'] = songFile
		
			self.label.set_text("")
			print metadata
		
			lyrics = self.getLyricsFromDisk(metadata)
			if lyrics is None:
				self.lyricsEngine.search(metadata)
				print "searching started"
			else:
				self.onLyricsFound(lyrics)
		except:
			traceback.print_exc()


	def onLyricsFound(self, lyrics):
		if not self.lyrics and lyrics:
			print "onLyricsFound"
			try:
				parsed = lyricsparser.parseLyrics(lyrics)
				if isinstance(parsed[0], LyricEntity):
					self.lyrics = parsed
					self.lyricsTimer = LyricsTimer(self.lyrics ,self.update)
					elapsed = self.player.getElapsed()
					#print "elapsed:", elapsed
					self.lyricsTimer.start(elapsed)
					self.label.set_text(self.lyrics[self.lyricsTimer.actualLine].text[0].strip())
					self.lyricsEngine.stop()
			except:
				traceback.print_exc()

	def update(self, lyricsLine):
		lyricLine = self.lyrics[lyricsLine].text[0].strip()
		if lyricLine:
			self.label.set_text(self.lyrics[lyricsLine].text[0].strip())

	def onEngineFinish(self):
		print "onEngineFinish"

	def getLyricsFromDisk(self, songInfo):
		logger.debug("searching lyrics on disk")
		# check for .lrc file in song file directory
		lrc_file = None
		if songInfo.has_key('file'):
			lrc_file = songInfo['file'].rstrip("mp3")+"lrc" #TODO: not only mp3 expecting
		if lrc_file == None or not os.path.exists(lrc_file):
			lrc_path = lyricsFile(songInfo)
			lrc_file = os.path.join(self.lyrics_directory, lrc_path['folder'], lrc_path['file'])
			logger.debug("lyrics should be here: %s" % lrc_file)
			
		print lrc_file
		if lrc_file != None and os.path.exists(lrc_file):
			f = open(lrc_file, 'r')
			print "lyrics from file: %s" % lrc_file
			lrc = f.read()
			f.close()
			return lrc
		print "Nothing on disk"
		return None

	def show_about(self, *args):
		print args
		pass

	def show_preferences(self, *args):
		dialog = OptionsDialog(self)
		response = dialog.run()
		if response == gtk.RESPONSE_OK:
			dialog.save_preferences()
		dialog.destroy()