%
% anyremote configuration file for Juk/KDE4 management (Server-mode)
%
% Thanks to Calve (calvinh34 at gmail.com) for enhancing this configuration file
%

% STATUS stable
% XTEST no
% SOUND app
% ENV no
% IPC DBUS
% DEF_PLAYER
% ALL_IN_ONE
% MEDIACENTER audio
% TODO Volume control by Juk is a bit buggy: no min/max values check inside Juk

GuiAppName=Juk/KDE4
GuiAppBinary=juk
GuiAppVersion=echo 'V=`juk -v|grep KDE|tr -d " "|cut -f 2 -d ":"|cut -f 1 -d "."`; if [ "x$V" = "x4" ]; then echo OK; else echo NOK; fi'|sh -s
GuiAppRun=echo 'A=`pgrep juk`; if [ "x$A" = "x" ]; then echo NOK; else echo OK; fi'|sh -s
GuiAppIcon=juk.png
GuiAppType=Application
GuiAppProtocols=Server
GuiAppDesc=JuK is a free software audio player for KDE, part of the kdemultimedia package. JuK supports collections of MP3, Ogg Vorbis, and FLAC audio files. This configuration file is for Juk/KDE4.

[Protocol]=Server

(Init)=\
	Include($(CfgDir)/Utils/aliases-server.cfg);\
	Make(var,player_name,by_value,Juk);\
	Include($(CfgDir)/Utils/aliases-server-player.cfg);\
	Make(var,kde4_app,by_value,juk);\
	Macro(TEST_KDE4_APP);\
	Make(var,cover_size,by_value,);\
	Make(var,juk_layout,\
	     V=`$(CfgDir)/Utils/registry.sh $(TmpDir) get juk_layout`;\
	     if [ "x$V" = "x" ]; then echo "5x1"; else echo $V; fi);

(Exit)=\
	Exec($(CfgDir)/Utils/registry.sh $(TmpDir) set juk_layout $(juk_layout));

(Connect)=\
	Make(mode,juk_$(juk_layout));

[Mode]=juk_general

% Custom volume control
JUK_VOL-=\
	Exec(dbus-send --type=method_call --dest=org.kde.juk /Player org.kde.juk.player.volumeDown);\
	Macro(JUK_VOLUME);
	
JUK_MUTE=\
	Exec(dbus-send --type=method_call --dest=org.kde.juk /Player org.kde.juk.player.mute);

JUK_VOL+=\
	Exec(dbus-send --type=method_call --dest=org.kde.juk /Player org.kde.juk.player.volumeUp);\
	Macro(JUK_VOLUME);

SlideDown=\
	Macro(JUK_VOL-);
	
SlideUp=\
	Macro(JUK_VOL+);

VOL-=\
	Macro(JUK_VOL-);

VOL+=\
	Macro(JUK_VOL+);

% JUK_TITLE should be defined in child modes

PLAY_ONLY=\
	Exec(qdbus org.kde.juk /Player org.kde.juk.player.playPause);

PLAY=\
	Macro(PLAY_ONLY);\
	Macro(JUK_START);
	
RWD=\
	Exec(qdbus org.kde.juk /Player org.kde.juk.player.seekBack);

FF=\
	Exec(qdbus org.kde.juk /Player org.kde.juk.player.seekForward);

PREV=\
	Exec(qdbus org.kde.juk /Player org.kde.juk.player.back);\
	Macro(JUK_TITLE);

STOP=\
	Timer(JUK_TITLE,cancel);\
	Exec(qdbus org.kde.juk /Player org.kde.juk.player.stop);

NEXT=\
	Exec(qdbus org.kde.juk /Player org.kde.juk.player.forward);\
	Macro(JUK_TITLE);

PAUSE=\
	Exec(qdbus org.kde.juk /Player org.kde.juk.player.pause);

RUN_IF_NEED=\
	Exec($(CfgDir)/Utils/run-or-switch-to.sh juk);
	
RUN_OR_QUIT=\
	Exec(P=`pgrep juk`; \
	     if [ "x$P" = "x" ]; then \
	     	juk; \
	     else \
	     	dbus-send --type=method_call --dest=org.kde.juk /MainApplication org.kde.KApplication.quit; \
	     fi);

APP_QUIT=\
	Timer(JUK_TITLE,cancel);\
	Exec(dbus-send --type=method_call --dest=org.kde.juk /MainApplication org.kde.KApplication.quit);

JUK_TITLE_GENERAL=\
	ExecAndSet(title,qdbus org.kde.juk /Player playingString|sed 's/<i>//;s/<\/i>//')

Random=\
	Exec(P=`qdbus org.kde.juk /Player org.kde.juk.player.randomPlayMode`;\
	     if [ "x$P" = "xRandom" ]; then \
	         qdbus org.kde.juk /Player org.kde.juk.player.setRandomPlayMode NoRandom; \
	     else \
	         qdbus org.kde.juk /Player org.kde.juk.player.setRandomPlayMode Random;\
	     fi);
	     
JUK_CHECK_TIMER=\
	Make(var,juk_status,qdbus  org.kde.juk /Player org.kde.juk.player.status);\
	Make(var,timer_action,if [ "x$(juk_status)" = "x2" ]; \
	                      then echo JUK_START; else echo JUK_CANCEL; fi);\
	Macro($(timer_action));

JUK_CANCEL=\
	Timer(JUK_TITLE,cancel);

JUK_START=\
	Macro(JUK_TITLE);\
	Timer(JUK_TITLE,$(UpdateTimeout),0);
	
Layout 3x4=\
	Make(mode,juk_3x4);

Layout 7x1=\
	Make(mode,juk_7x1);

Layout 5x1=\
	Make(mode,juk_5x1);

[ModeEnd]
	
[Mode]= juk_3x4: juk_general, default_player

% (EnterMode) by Utils/aliases-server-player.cfg

% Custom volume control
1=Macro(JUK_VOL-);
2=Macro(JUK_MUTE);
3=Macro(JUK_VOL+);

% PowerMgmt by Utils/aliases-server.cfg
APP_MENU=\
	Set(menu,replace,Search,Random,Layout 7x1,Layout 5x1,PowerMgmt);\
	Macro(ALLIN1MENU);\
	Macro(MEDIACENTER_MENU);

APP_POST_SETUP=\
	Make(var,juk_layout,by_value,3x4);\
	Macro(JUK_CHECK_TIMER);

JUK_TITLE=\
	Macro(JUK_TITLE_GENERAL);

JUK_VOLUME=\
	ExecAndSet(status,echo `qdbus org.kde.juk /Player volume`|awk '{min=int($0*100);print min;}');
		
Search=\
	Make(mode,juk_search);

[ModeEnd]

[Mode]= juk_cover

APP_COVER=\
	Make(var,media_file,qdbus org.kde.juk /Player org.kde.juk.player.trackProperty Path|\
	                     sed "s/'/'\\\''/g;s/\//\'\/\'/g;s/$/\'/g;s/^'\//\//");\
	Make(var,juk_cover_file,qdbus org.kde.juk /Collection  org.kde.juk.collection.trackCover $(media_file));\
	Make(var,coverfile,echo "$(juk_cover_file)"|md5sum|cut -f 1 -d ' ');\
	Make(var,dummy,\
	     if  [ "x$(juk_cover_file)" != "x" ]; then \
	         convert -resize $(cover_size)x$(cover_size) -depth -background transparent \
		         "$(juk_cover_file)" $(TmpDir)/album_covers/$(coverfile)-$(cover_size).png;\
		 echo "$(TmpDir)/album_covers/$(coverfile)-$(cover_size).png";\
	     fi);\
	Set(cover,noname,$(TmpDir)/album_covers/$(coverfile)-$(cover_size).png);

JUK_TITLE=\
	Macro(JUK_TITLE_GENERAL);\
	Macro(APP_COVER, if [ "x$(cover_size)" = "x" ]; then echo 1; else echo 0; fi);

% Custom volume control
Mute=\
	Macro(JUK_MUTE);

JUK_VOLUME=\
	Make(none);

[ModeEnd]

[Mode]= juk_7x1: juk_general, juk_cover, default_player_7x1

% Help, PowerMgmt by Utils/aliases-server-player.cfg
APP_MENU=\
	Set(menu,replace,Mute,Random,Layout 3x4,Layout 5x1,Help,PowerMgmt);\
	Macro(ALLIN1MENU);\
	Macro(MEDIACENTER_MENU);

APP_POST_SETUP=\
	Make(var,juk_layout,by_value,7x1);\
	Macro(JUK_CHECK_TIMER);

[ModeEnd]

[Mode]= juk_5x1: juk_general, juk_cover, default_player_5x1

% Help, PowerMgmt by Utils/aliases-server-player.cfg
APP_MENU=\
	Set(menu,replace,Mute,Random,Layout 3x4,Layout 7x1,Help,PowerMgmt);\
	Macro(ALLIN1MENU);\
	Macro(MEDIACENTER_MENU);

APP_POST_SETUP=\
	Make(var,juk_layout,by_value,5x1);

PLAY=\
	Macro(PLAY_ONLY);

APP_PLAY_STATUS=\
	Make(var,juk_status,qdbus org.kde.juk /Player org.kde.juk.player.status);\
	Make(var,app_status,if [ "x$(juk_status)" = "x2" ]; then echo playing; else echo paused; fi);

APP_TITLE=\
	Macro(JUK_TITLE);

[ModeEnd]

[Mode]=juk_search

(EnterMode)=\
	Make(var,searchval,qdbus org.kde.juk /Search org.kde.juk.search.searchText);\
	ExecAndSet(editfield,echo "Search...,Search,$(searchval)");

Ok($$)=\
	Set(text,close);\
	Exec(qdbus org.kde.juk /Search org.kde.juk.search.setSearchText "$(Param)" );\
	Set(vibrate,1);\
	Make(mode,juk_$(juk_layout));
	
Back($$)=\
	Make(mode,juk_$(juk_layout));

Back=\
	Make(mode,juk_$(juk_layout));

[ModeEnd]

%
% NOT USED
%
[Mode]=juk_playlist

% Found no way to choose track
Playlist=\
       Make(var,juk_playlist,qdbus org.kde.juk /Collection org.kde.juk.collection.playingPlaylist);\
       ExecAndSet(list,replace,Playlist,qdbus org.kde.juk /Collection org.kde.juk.collection.playlistTracks $(juk_playlist)|sed "s/'//"|xargs -i basename {}|sed "s/$/,/");\
       ExecAndSet(list,select,expr 1 + `......`);\
       Set(menu,replace,Choose);
       
Push($$)=\
       Macro(Choose($$))
       
%Choose($$)=\
%       Exec(.... `expr $(Index) - 1`);

Back($$)=\
	Set(list,close);\
	Make(mode,juk_$(juk_layout));

[ModeEnd]

[End]
