require "cgi"

class AMAROK
	# current track information
	def AMAROK::getTitle()
		return `dcop amarok player title`
	end
	def AMAROK::getArtist()
		return `dcop amarok player artist`
	end
	def AMAROK::getAlbum()
		return `dcop amarok player album`
	end
	def AMAROK::getCoverArt()
		return `dcop amarok player coverImage`
	end
	def AMAROK::getPlayCount()
		return `dcop amarok player trackPlayCounter`
	end
	def AMAROK::getComment()
		return `dcop amarok player comment`
	end
	
	# collection functions
	def AMAROK::query( sql )	
		`dcop amarok collection query "#{sql}"`
	end
	
	# custom section manipulation
	def AMAROK::addCustomSection( name )
		`dcop amarok contextbrowser addCustomSection "#{name}"`
	end
	def AMAROK::clearCustomSection( name )
		`dcop amarok contextbrowser setCustomSection "#{name}" ""`
	end
	def AMAROK::setCustomSection( name, contents )
		#contents.gsub!('"','\\"')
		#contents.gsub!('`','\`')
		#contents.gsub!('<','\<')
		#contents.gsub!(';','\;')
		#contents.gsub!('>','\>')
		contents = CGI.escapeHTML( contents )
		
		puts "dcop amarok contextbrowser setCustomSection \"#{name}\" \"#{contents}\""
		
		`dcop amarok contextbrowser setCustomSection "#{name}" "#{contents}"`
	end
	def AMAROK::setCustomSectionTitle( name, title )
		title.gsub!('"','\"')
		title.gsub!('`','\`')
		
		`dcop amarok contextbrowser setCustomSectionTitle "#{name}" "#{title}"`
	end
	
	# results caching to speed up results
	def AMAROK::setCache( key, contents )
		key.gsub!(" ","_")
		key.gsub!("'","#squot#")
		key.gsub!("(","#opara#")
		key.gsub!(")","#cpara#")
		puts "caching in #{key}"
		home=`cd;pwd`.strip()
		f = File.open("#{home}/.kde/share/apps/amarok/scripts-data/cache/#{key}.cachefile","w")
		f.puts(contents)
		f.close()
	end
	def AMAROK::getCache( key )
		key.gsub!(" ","_")
		key.gsub!("'","#squot#")
		key.gsub!("(","#opara#")
		key.gsub!(")","#cpara#")
		home=`cd;pwd`.strip()
		res = `cat "#{home}/.kde/share/apps/amarok/scripts-data/cache/#{key}.cachefile" 2>/dev/null`
		puts res
		if res != nil and res != ""
			return res.strip()
		else
			return ""
		end
	end
end