#!/bin/dash
# rox-functions library for Fatdog drive icons
# Copyright (C) James Budiono 2013
# License: GNU GPL Version 3 or later
#
# dependency: xml2, awk, sed, rox
#
# This file should be sourced, not executed.
#
# Except for rox_set_icon_image, the functions assumes they are executed in the context of current user
#

### configurations - default values
ICON_IMAGE_ROOT=/usr/share/midi-icons
ICON_ALIGNMENT=bottom SHOW_VOLUME_LABEL=yes	
MARGIN_TOP=32 MARGIN_BOTTOM=64 MARGIN_LEFT=32 MARGIN_RIGHT=64 	# margins in pixels
GRID_X=4	# check every $GRID pixels - old rox default is 64
GRID_Y=16	# check every $GRID pixels - old rox default is 64
ICON_DX=48	# assumed icon width in pixels - for old rox, should not be multiple of GRID_X
ICON_DY=52	# assumed icon height in pixels - for old rox, should not be multiple of GRID_Y
MAX_VOLABEL=6 # truncate volume labels to this length
EVENTMANAGER_CONFIG=/etc/eventmanager
. $EVENTMANAGER_CONFIG # UNPATCHED_ROX

# prints out icon label based on $1 and $2
# $1-drivename $2-volume label
get_icon_label() {
	if [ -z $MAX_VOLABEL ] || [ $MAX_VOLABEL -eq 0 ]; then
		printf "%s" "$1"
	else
		printf "%s\r\n%.${MAX_VOLABEL}s" "$1" "$2"
	fi
}

### remove icon from pinboard
# $1-path of icon to remove
rox_remove_icon() {
		rox -R << EOF
<?xml version="1.0"?>
<env:Envelope xmlns:env="http://www.w3.org/2001/12/soap-envelope">
	<env:Body xmlns="http://rox.sourceforge.net/SOAP/ROX-Filer">
		<PinboardRemove>
			<Path>$1</Path>
		</PinboardRemove>
	</env:Body>
</env:Envelope>
EOF
}

### add icon from pinboard - for patched Rox
#$1-path $2-COORD_X*UNUSED* $3-COORD_Y*UNUSED* $4-label $5-args
#also settings parameters from $EVENTMANAGER_CONFIG
rox_add_icon() {
		rox -R << EOF
<?xml version="1.0"?>
<env:Envelope xmlns:env="http://www.w3.org/2001/12/soap-envelope">
	<env:Body xmlns="http://rox.sourceforge.net/SOAP/ROX-Filer">
		<PinboardAddDriveIcon>
			<Path>$1</Path>
			<Align>$ICON_ALIGNMENT</Align>			
			<Label>$4</Label>
			<Args>$5</Args>
			<Left>$MARGIN_LEFT</Left>
			<Top>$MARGIN_TOP</Top>
			<Right>$MARGIN_RIGHT</Right>
			<Bottom>$MARGIN_BOTTOM</Bottom>
			<GridX>$GRID_X</GridX>
			<GridY>$GRID_Y</GridY>
		</PinboardAddDriveIcon>
	</env:Body>
</env:Envelope>
EOF
}
# this function is no longer used now, it's done inside Rox itself
get_free_coord() {
	return 0
}

### override these functions with unpatched-ROX version when asked
if [ "$UNPATCHED_ROX" ]; then
### add icon from pinboard
#$1-path $2-COORD_X $3-COORD_Y $4-label $5-args
rox_add_icon() {
		rox -R << EOF
<?xml version="1.0"?>
<env:Envelope xmlns:env="http://www.w3.org/2001/12/soap-envelope">
	<env:Body xmlns="http://rox.sourceforge.net/SOAP/ROX-Filer">
		<PinboardAdd>
			<Path>$1</Path>
			<X>$2</X>
			<Y>$3</Y>
			<Label>$4</Label>
			<Args>$5</Args>
		</PinboardAdd>
	</env:Body>
</env:Envelope>
EOF
}
### get coordinates of a free location on pinboard, for adding icons
# $1-pinboard file
# $2-aligment = top, bottom, left, right (default is bottom)
# (top & bottom the same, only starting position is different)
# (left & right the same, only starting position is different)
# returns COORD_X and COORD_Y for historical reasons
GRID_X=64 GRID_Y=64 # old default
get_free_coord() {
	out=$(/usr/sbin/fatdog-drive-icon-get_free_coord.awk -v pinboard="$1" \
		-v xgrid=$GRID_X -v ygrid=$GRID_Y -v xicon=$ICON_DX -v yicon=$ICON_DY \
		-v top=$MARGIN_TOP -v bottom=$MARGIN_BOTTOM -v left=$MARGIN_LEFT -v right=$MARGIN_RIGHT \
		-v align="$2")
	COORD_X=${out% *}
	COORD_Y=${out#* }
}
fi


### create a rox icon file
# svg icon inspired by technosaurus (Brad Conroy), see http://www.murga-linux.com/puppy/viewtopic.php?t=76431
#$1-path $2-type
create_rox_icon_file() {
	local DRVICON=drive48.png USBICON=""
	
	# primary image
	case $2 in
		card*) DRVICON=card48.png ;;
		optical*) DRVICON=optical48.png ;;		
		floppy*) DRVICON=floppy48.png ;;
	esac
		
	cat > $1/.DirIcon << EOF
<svg width="48" height="48" id="svg1" xmlns:xlink="http://www.w3.org/1999/xlink">
<image xlink:href="$ICON_IMAGE_ROOT/$DRVICON" width="48" height="48"/>
EOF

	# secondary image
	case $2 in
		*usb*) USBICON=usb48.png
	esac

	[ $USBICON ] && cat >> $1/.DirIcon << EOF
<image xlink:href="$ICON_IMAGE_ROOT/$USBICON" width="48" height="48"/>	
EOF
	# closing tag
	cat >> $1/.DirIcon << EOF
</svg>
EOF
}


### create drive-icon rox application 
# $1-path, $2-type, $3-devname $4-summary
create_rox_app() {
	{
	
	# standard menus
	cat << EOF
<?xml version="1.0"?>
<AppInfo>
	<Summary>$4</Summary>
	<About>
		<Purpose>Fatdog desktop drive icons</Purpose>
		<Authors>Re-written for Fatdog64-600, James Budiono 2012</Authors>
		<License>GNU GPL Version 3 or later</License>
	</About>
	<AppMenu>
		<Item option="umount $3" icon="gtk-cancel"><Label>Un-mount ${3##*/}</Label></Item>
		<Item option="pmount" icon="gtk-execute"><Label>Drive Mounter</Label></Item>
		<Item option="usage" icon="gtk-execute"><Label>Disk Usage</Label></Item>
EOF
	
	# extra menus for optical drives & non-optical drives
	case $2 in
		optical*) cat << EOF
		<Item option="explore $3" icon="gtk-cdrom"><Label>Explore disc</Label></Item>
		<Item option="eject $3" icon="gtk-disconnect"><Label>Eject disc</Label></Item>			
EOF
		;;
		
		*) cat << EOF
		<Item option="fsck $3" icon="gtk-execute"><Label>Check integrity</Label></Item>		
EOF
		;;
	esac
	
	# extra menus for usb/mmc drives
	case $2 in
		*usb*|*card*) grep -qE "^$3" /proc/mounts > /dev/null || cat << EOF
		<Item option="safe-remove $3" icon="gtk-disconnect"><Label>Safely remove ${3##*/}</Label></Item>
EOF
		;;
	esac
	
	# closing tags
cat << EOF
		<Item option="redraw" icon="gtk-refresh"><Label>Redraw all icons</Label></Item>
</AppMenu></AppInfo>
EOF
	} > $1/AppInfo.xml
	
	create_rox_icon_file $1 $2
	ln -s /usr/sbin/fatdog-drive-icon-action-handler.sh $1/AppRun
}


### set image for the icon 
#$1-path $2-icon $USER=user to use for contacting Rox, $DISPLAY=the correct display to connect to
rox_set_icon_image() {
	pidof ROX-Filer > /dev/null || return
	ROX='rox -R'
	[ $(id -u) -eq 0 ] && [ $USER != "root" ] && ROX="su $USER -c '$ROX'"
	eval $ROX << EOF
<?xml version="1.0"?>
<env:Envelope xmlns:env="http://www.w3.org/2001/12/soap-envelope">
	<env:Body xmlns="http://rox.sourceforge.net/SOAP/ROX-Filer">
		<SetIcon>
			<Path>$1</Path>
			<Icon>$2</Icon>
		</SetIcon>
	</env:Body>
</env:Envelope>
EOF
}
