%
% anyremote configuration file for clementine (http://www.clementine-player.org/) management. (Server-mode)
% 
% Thanks Kristjan Ugrin for corrections.
%
% TODO: support DBUS 
%       http://code.google.com/p/clementine-player/wiki/MPRIS
%

% STATUS stable
% XTEST no
% ENV no
% SOUND mixer
% DEF_PLAYER   
% ALL_IN_ONE 
% MEDIACENTER audio

GuiAppName=Clementine
GuiAppBinary=clementine
GuiAppRun=echo 'A=`ps -ef|grep clementine|grep -v grep|grep -v edit`; if [ "x$A" = "x" ]; then echo NOK; else echo OK; fi'|sh -s
GuiAppIcon=clementine.png
GuiAppType=Application
GuiAppProtocols=Server
GuiAppDesc=A music player.

[Protocol]=Server

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

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

(Connect)=\
	Make(mode,clementine_$(clementine_layout));

[Mode]=clementine_general

PLAY=\
	Exec(clementine -t);\
	Macro(CLEMENTINE_TITLE,sleep 1;echo 0);\
	Timer(CLEMENTINE_TITLE,$(UpdateTimeout),0);

RWD=\
	Exec(clementine --seek-by -10);

FF=\
	Exec(clementine --seek-by +10);

PREV=\
	Exec(clementine -r);\
	Macro(CLEMENTINE_TITLE);

STOP=\
	Timer(CLEMENTINE_TITLE,cancel);\
	Exec(clementine -s);\
	Set(title,);\
	Set(status,);

NEXT=\
	Exec(clementine -f);\
	Macro(CLEMENTINE_TITLE);

PAUSE=\
	Exec(clementine -u);

RUN_IF_NEED=\
	Exec($(CfgDir)/Utils/run-or-switch-to.sh clementine);
	
RUN_OR_QUIT=\
	Exec(P=`ps auxc|grep clementine`;\
	     if [ "x$P" = "x" ]; then clementine & else killall clementine; fi);
	
APP_QUIT=\
	Exec(killall clementine);
	
CLEMENTINE_TITLE_GENERAL=\
	Make(var,clementine_index,qdbus org.mpris.clementine /TrackList org.freedesktop.MediaPlayer.GetCurrentTrack);\
	Make(var,clementine_artist,qdbus org.mpris.clementine /TrackList org.freedesktop.MediaPlayer.GetMetadata $(clementine_index)|grep artist|sed "s/artist: //");\
	Make(var,clementine_album,qdbus org.mpris.clementine /TrackList org.freedesktop.MediaPlayer.GetMetadata $(clementine_index)|grep album|sed "s/album: //");\
	Make(var,clementine_song,qdbus org.mpris.clementine /TrackList org.freedesktop.MediaPlayer.GetMetadata $(clementine_index)|grep title|sed "s/title: //");\
	Set(title,$(clementine_artist) - $(clementine_album));\
	Set(status,$(clementine_song));
	
Shuffle=\
	Exec(P=`qdbus org.mpris.clementine /org/mpris/MediaPlayer2 Shuffle`; \
		if [ "x$P" = "xtrue" ]; \
		then \
		    qdbus org.mpris.clementine /org/mpris/MediaPlayer2 Shuffle false & \
		else \
		    qdbus org.mpris.clementine /org/mpris/MediaPlayer2 Shuffle true & \
		fi);

Repeat=\
	Exec(P=`qdbus org.mpris.clementine /org/mpris/MediaPlayer2 LoopStatus`; \
		if [ "x$P" = "xNone" ]; \
		then \
		    qdbus org.mpris.clementine /org/mpris/MediaPlayer2 LoopStatus Track & \
		else \
		    qdbus org.mpris.clementine /org/mpris/MediaPlayer2 LoopStatus None & \
		fi);
Playlist=\
	Make(mode,clementine_playlist);

APP_PLAY_STATUS=\
	Make(var,clementine_status,qdbus org.mpris.clementine /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.PlaybackStatus);\
	Make(var,app_status,if [ "x$(clementine_status)" = "xPlaying" ]; then echo playing; else echo paused; fi);

Layout 3x4=\
	Make(mode,clementine_3x4);

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

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

[ModeEnd]

[Mode]=clementine_3x4 : clementine_general,default_player

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

APP_MENU=\
	Set(menu,replace,Playlist,Shuffle,Repeat,Layout 5x1,Layout 7x1,PowerMgmt);\
	Macro(ALLIN1MENU);\
	Macro(MEDIACENTER_MENU);

APP_POST_SETUP=\
	Make(var,clementine_layout,by_value,3x4);

% PowerMgmt by Utils/aliases-server.cfg
	
% Volume control and 0-9,*,# assignments by Utils/aliases-server-player.cfg
%1=Exec(clementine --volume-down)
%2=Macro(DEFAULT_TOGGLE_MUTE)
%3=Exec(clementine --volume-up)

CLEMENTINE_TITLE=\
    Make(var,clementine_status,qdbus org.mpris.clementine /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.PlaybackStatus);\
	Macro(DO_CLEMENTINE_TITLE,if [ "x$(clementine_status)" = "xPlaying" ]; then echo 0; else echo 1; fi);

DO_CLEMENTINE_TITLE=\
	Macro(CLEMENTINE_TITLE_GENERAL);

[ModeEnd]

[Mode]=clementine_cover

CLEMENTINE_TITLE=\
    Make(var,clementine_status,qdbus org.mpris.clementine /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.PlaybackStatus);\
	Macro(DO_CLEMENTINE_TITLE,if [ "x$(clementine_status)" = "xPlaying" ]; then echo 0; else echo 1; fi);

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

APP_COVER=\
	Make(var,media_file,\
	     qdbus org.mpris.clementine /TrackList org.freedesktop.MediaPlayer.GetMetadata $(clementine_index)|grep location|\
	     sed "s/^[^/]*\//\//;s/\/\/\//\//;s/'/'\\\''/g;s/\//\'\/\'/g;s/$/\'/g;s/^'\//\//");\
	Macro(DEFAULT_COVER);

[ModeEnd]

[Mode]=clementine_7x1 : clementine_general,default_player_7x1,clementine_cover

APP_MENU=\
	Set(menu,replace,Playlist,Mute,Shuffle,Repeat,Next Cover,Previous Cover,Update Cover,Layout 3x4,Layout 5x1,Help,PowerMgmt);\
	Macro(ALLIN1MENU);\
	Macro(MEDIACENTER_MENU);

APP_POST_SETUP=\
	Make(var,clementine_layout,by_value,7x1);

[ModeEnd]

[Mode]=clementine_5x1 : clementine_general,default_player_5x1,clementine_cover

APP_MENU=\
	Set(menu,replace,Playlist,Mute,Shuffle,Repeat,Next Cover,Previous Cover,Update Cover,Layout 3x4,Layout 7x1,Help,PowerMgmt);\
	Macro(ALLIN1MENU);\
	Macro(MEDIACENTER_MENU);

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

APP_TITLE=\
	Macro(CLEMENTINE_TITLE);

% no timer
PLAY=\
	Exec(clementine -t);\
	Macro(CLEMENTINE_TITLE,sleep 1;echo 0);

[ModeEnd]

[Mode]=clementine_nobutton

% bug-prone :)
1=Make(mode,clementine_$(clementine_layout));	
2=Make(mode,clementine_$(clementine_layout));	
3=Make(mode,clementine_$(clementine_layout));	
4=Make(mode,clementine_$(clementine_layout));	
5=Make(mode,clementine_$(clementine_layout));	
6=Make(mode,clementine_$(clementine_layout));	
7=Make(mode,clementine_$(clementine_layout));	
8=Make(mode,clementine_$(clementine_layout));	
9=Make(mode,clementine_$(clementine_layout));	
*=Make(mode,clementine_$(clementine_layout));	
0=Make(mode,clementine_$(clementine_layout));	
#=Make(mode,clementine_$(clementine_layout));	

[ModeEnd]

[Mode]=clementine_playlist : clementine_nobutton

(EnterMode)=\
	ExecAndSet(list,replace,Playlist,$(CfgDir)/Utils/getPlayList.sh org.mpris.clementine);\
  	ExecAndSet(list,select,expr 1 + `qdbus org.mpris.clementine  /TrackList GetCurrentTrack`);\
	Set(menu,replace,Choose,Delete);
	
Push($$)=\
	Macro(Choose($$))
	
Choose($$)=\
	Exec(qdbus org.mpris.clementine /TrackList PlayTrack `expr $(Index) - 1`);

Delete($$)=\
	Exec(qdbus org.mpris.clementine /TrackList DelTrack `expr $(Index) - 1`);

Back($$)=\
	Set(list,close);\
	Make(mode,clementine_$(clementine_layout));

[ModeEnd]

%qdbus org.mpris.clementine /TrackList org.freedesktop.MediaPlayer.GetCurrentTrack
%qdbus org.mpris.clementine /TrackList org.freedesktop.MediaPlayer.GetMetadata
%qdbus org.mpris.MediaPlayer2.clementine  /TrackList org.freedesktop.MediaPlayer.AddTrack
%qdbus org.mpris.MediaPlayer2.clementine  /TrackList  org.freedesktop.MediaPlayer.DelTrack

[End]
