#!/bin/bash
#pMusic - external menu
#Copyright 2013
#Sigmund Berglund



template(){
echo '<hbox spacing="0">
 <pixmap height-request="20" icon_size="1" space-expand="false" space-fill="false"><input file stock="gtk-'$3'"></input></pixmap>
 <button height-request="20" xalign="0" can-focus="no" relief="2" space-expand="true" space-fill="true">
  <label>" '$1'"</label>
  <action>echo '$2' > '$WORKDIR'/OUTPUT</action>
  <action>EXIT:exit</action>
 </button>
</hbox>'
}

echo -n > $WORKDIR/OUTPUT

export gtkdialog_menu='
<window decorated="false" skip_taskbar_hint="true" resizable="false" height-request="92" width-request="330"  modal="true">
  <hbox scrollable="true" hscrollbar-policy="2" vscrollbar-policy="2" space-expand="true" space-fill="true">
  <text width-request="1" space-expand="false" space-fill="false"><label>""</label></text>
  <vbox spacing="2">
   <text height-request="2" space-expand="false" space-fill="false"><label>""</label></text>
   '"
  $(template " $(gettext 'Browse downloads (in main gui)')" browse open)
  $(template " $(gettext 'Add download directory to Favorites')" add_favorites favorites_add)
  <hseparator></hseparator>
  $(template " $(gettext 'Delete preset')" delete_preset delete)
 "'</vbox></hbox>
 <action signal="focus-out-event">EXIT:exit</action>
</window>'

$GTKDIALOG -p gtkdialog_menu -G +"$(($PTR_X_ROOT-5))"+"$(($PTR_Y_ROOT-5))" > /dev/null

OUTPUT=($(<"$WORKDIR/OUTPUT"))
case "$OUTPUT" in
	browse)
		TMP="$(<"$WORKDIR/RADIOHITS_PRESS_EVENT")"
		SOURCE="`cut -d'|' -f2 "$STORAGE_DIR/radio-hits preset/$TMP"`"
		PTR_BTN=1 #else browse will exit
		. $APPDIR/func -browse
		echo true > $WORKDIR/UPDATE_SOURCELIST
		;;
	add_favorites)
		gettext 'Add to Favorites' > $WORKDIR/splashtext
		$APPDIR/box_splash &
		TMP="`cut -d'|' -f2 "$STORAGE_DIR/radio-hits preset/$(<"$WORKDIR/RADIOHITS_PRESS_EVENT")"`"
		$APPDIR/func_favorites -add_favorites "$TMP"
		#---
		sleep 0.5
		echo 100 > $WORKDIR/splash
		;;
	delete_preset)
		RADIOHITS_NAME="$(<"$WORKDIR/RADIOHITS_PRESS_EVENT")"
		RADIOHITS_URL="`cut -d'|' -f3 "$STORAGE_DIR/radio-hits preset/$RADIOHITS_NAME"`"
		rm "$STORAGE_DIR/radio-hits preset/$RADIOHITS_NAME"
		#kill process
		TMP="`ps`"
		for I in `echo "$TMP" | grep -F "$RADIOHITS_URL" | grep -F streamripper | awk '{print $1}'`; do kill -9 $I 2> /dev/null; done
		#update list
		ls -1 "$STORAGE_DIR/radio-hits preset" | sort > $WORKDIR/radiohits_presets
		cp $WORKDIR/radiohits_presets $WORKDIR/radiohits_output
		;;
esac
