#! /usr/bin/env wish
# $Id: tkamarok.tcl 99 2008-03-04 19:13:36Z kostix $
# TkAmarok scripts that monitors Amarok playing state and sends
# relevant "now playing" info to all present instances of Tkabber.
# Requires "np_send" plugin installed on Tkabber's part.
# Written by Konstantin Khomoutov <flatworm@users.sourceforge.net>

wm withdraw .

if {![catch {package require Tclx}]} {
	signal trap SIGTERM shutdown
}

variable active true
variable track
variable tid ""
variable laststate unknown

proc laststate {op val} {
	variable laststate

	switch -- $op {
		is  { expr {[string equal $laststate $val]} }
		set { set laststate $val }
		default {
			return -code error "bad operation \"$op\":\
				must be is or set"
		}
	}
}

proc process {} {
	variable active

	if {[eof stdin]} shutdown

	if {!$active} return

	set line [string trim [gets stdin]]
	switch -glob -- $line {
		configure CmdConfigure
	
		engineStateChange:* {
			CmdEngineStateChange [string trim [string range $line 19 end]]
		}

		trackChange CmdTrackChange
	}
}

proc shutdown {} {
	fileevent stdin readable {}
	notify -state inactive
	after 1000 set ::exit 0
}

proc dcop args {
	eval {exec dcop amarok} $args
}

proc amarokShowMessage msg {
	dcop playlist popupMessage $msg
}

proc CmdConfigure {} {
	amarokShowMessage {TkAmarok plugin has no configuration options}
}

proc CmdEngineStateChange state {
	variable tid

	switch -- $state {
		empty   {
			set tid ""
			HandleInactivity
			laststate set empty
		}
		idle    -
		paused  {
			HandleInactivity
			laststate set paused
		}
		playing {
			if {[laststate is paused] && $tid != ""} ReportTrackChange
			laststate set playing
		}
	}
}

proc CmdTrackChange {} {
	set failed [catch {
		variable tid

		set trackid [dcop playlist currentTrackUniqueId]
		if {![string equal $tid $trackid]} {
			set tid $trackid

			variable track
			array unset track *

			set track(artist) [dcop player artist]
			set track(source) [dcop player album]
			set track(title)  [dcop player title]
			set track(track)  [dcop player track]
			set track(length) [dcop player trackTotalTime]
			set track(date)   [dcop player year]
			set uri           [dcop player encodedURL]

			if {[string match file://* $uri]} {
				set track(uri) ""
			} else {
				set track(uri) $uri
			}
		} else {
			amarokShowMessage "Same track..."
		}

		ReportTrackChange
	} err]
	if {$failed} {
		variable active false
		catch {amarokShowMessage $err}
	}
}

proc HandleInactivity {} {
	variable laststate

	switch -- $laststate {
		unknown -
		playing {
			notify -state inactive
		}
	}
}

proc ReportTrackChange {} {
	variable track

	set cmd [list notify -state active]
	foreach {key val} [array get track] {
		lappend cmd -$key $val
	}

	eval $cmd
}

proc notify args {
	foreach app [lsearch -all -inline [winfo interps] tkabber*] {
		eval [list send -async $app ::plugins::now_playing::now_playing $args]
	}
}

fconfigure stdin -encoding utf-8 -buffering line
fileevent stdin readable process

vwait exit
exit $exit

