#! /bin/sh
#
# Multi-call script providing GTK boxes to manage a desktop following
# Freedesktop standards.
#
# (C) GNU gpl v3 - SliTaz GNU/Linux 2008.
#
VERSION=20090504

# Glade XML file path and translated messages path.
GLADE_XML=/usr/share/slitaz/glade
MESSAGES=/usr/share/slitaz/messages

# Export script path and other if needed so we can use them in 'quote'.
export BIN=$0
export AUTOSTART_SCRIPT=$HOME/.config/openbox/autostart.sh

# Standard directories.
mkdir -p $HOME/Desktop $HOME/.local/share/applications

# Get the active locale (default to English).
case $LANG in
	fr*)
		. $MESSAGES/fr/desktopbox.msg ;;
	*)
		. $MESSAGES/en/desktopbox.msg ;;
esac

# Command line usage.
usage()
{
	echo -e "\nSliTaz Freedesktop Box - Version: $VERSION\n
\033[1mUsage: \033[0m `basename $0` command
\033[1mCommands: \033[0m\n
  new-folder   Create a new folder on the desktop with mkdir.
  new-file     Create a new empty file or SHell script on the desktop.
  add-icons    Add a system icon to the desktop.
  notify       Display a notification message (center/no decoration).
               Ex: `basename $0` notify \"Message to display\" 4
  autostart    Manage autostarted applications with Openbox.
  logout       Prompt for X session exit or system halt/reboot.\n"
}

# Openbox autostart functions, first column is used for icon
autostart_list()
{
	# Enabled
	for app in `cat $AUTOSTART_SCRIPT | grep ^[a-z] | awk '{ print $1 }'`
	do
		comment=`grep -B 1 "^$app" $AUTOSTART_SCRIPT | grep '^# ' | sed s/'#'//`
		[ -x /usr/bin/$app ] && echo "go-next | $app | $comment"
	done
	# Disabled
	for app in `cat $AUTOSTART_SCRIPT | grep ^#[a-z] | awk '{ print $1 }'`
	do
		comment=`grep -B 1 "^$app" $AUTOSTART_SCRIPT | grep '^# ' | sed s/'#'//`
		app=${app#\#}
		[ -x /usr/bin/$app ] && echo "stop | $app | $comment"
	done
}

# Enable or disbale autostarted applications.
autostart_actions()
{
	if grep -q "^$APPLICATION" $AUTOSTART_SCRIPT; then
		sed -i s/"^$APPLICATION"/"\#$APPLICATION"/ $AUTOSTART_SCRIPT
	else
		sed -i s/"^\#$APPLICATION"/"$APPLICATION"/ $AUTOSTART_SCRIPT
	fi
}

add_autostarted_app()
{
	if ! grep -q "^$NEW_APP" $AUTOSTART_SCRIPT; then
		NEW_APP=`echo $NEW_APP | sed s/'&'/''/`
		echo "" >> $AUTOSTART_SCRIPT
		echo "# $NEW_COMMENT" >> $AUTOSTART_SCRIPT
		echo "$NEW_APP &" >> $AUTOSTART_SCRIPT
	fi
}

add_autostarted_app_box()
{
	export ADD_AUTO_START_BOX='
<window title="Add auto started applications" icon-name="preferences-system-session">
<vbox>
	<text width-chars="54">
		<label>"
Add a new application starting with your session
		"</label>
	</text>
	<hbox>
		<text>
			<label>"Application:"</label>
		</text>
		<entry>
			<variable>NEW_APP</variable>
		</entry>
	</hbox>
	<hbox>
		<text>
			<label>"Comment:  "</label>
		</text>
		<entry>
			<variable>NEW_COMMENT</variable>
		</entry>
	</hbox>
	<hbox>
		<button ok>
			<action>$BIN add_autostarted_app</action>
			<action type="exit">exit</action>
		</button>
		<button cancel></button>
	</hbox>
</vbox>
</window>'
	gtkdialog --center --program=ADD_AUTO_START_BOX
}

# Box commands.

case $1 in
	new-folder)
		# Create a directory on the ~/Desktop.
		#
		DESKTOP_DIALOG="
<window title=\"Desktopbox - mkdir\" icon-name=\"folder-new\">
<vbox>

	<text use-markup=\"true\" width-chars=\"40\">
		<label>\"
<b>$NEW_FOLDER_LABEL</b>\"
		</label>
	</text>

	<hbox>
		<entry>
			<default>$FOLDER_ENTRY_MSG</default>
			<variable>DIR</variable>
		</entry>
	</hbox>"
		ACTIONS='
	<hbox>
		<button>
			<label>Mkdir</label>
			<input file icon="folder-new"></input>
			<action>mkdir -p "$HOME/Desktop/$DIR"</action>
			<action type="exit">Exit</action>
		</button>
		<button cancel>
			<action type="exit">Exit</action>
		</button>
	</hbox>

</vbox>
</window>'
		export DESKTOP_DIALOG="${DESKTOP_DIALOG}${ACTIONS}" ;;
	new-file)
		# Create a file on the ~/Desktop.
		#
		DESKTOP_DIALOG="
<window title=\"Desktopbox - touch/cat\" icon-name=\"document-new\">
<vbox>
	<text use-markup=\"true\" width-chars=\"40\">
		<label>\"
<b>$NEW_FILE_LABEL</b>\"
		</label>
	</text>

	<hbox>
		<entry>
			<default>$FILE_ENTRY_MSG</default>
			<variable>FILE</variable>
		</entry>
	</hbox>"
		ACTIONS='
	<hbox>
		<button>
			<label>SH script</label>
			<input file icon="document-new"></input>
			<action>echo "#!/bin/sh" > "$HOME/Desktop/$FILE"</action>
			<action>echo "#" >> "$HOME/Desktop/$FILE"</action>
			<action>chmod +x "$HOME/Desktop/$FILE"</action>
			<action type="exit">Exit</action>
		</button>
		<button>
			<label>Empty</label>
			<input file icon="document-new"></input>
			<action>touch "$HOME/Desktop/$FILE"</action>
			<action type="exit">Exit</action>
		</button>
		<button cancel>
			<action type="exit">Exit</action>
		</button>
	</hbox>
</vbox>
</window>'
		export DESKTOP_DIALOG="${DESKTOP_DIALOG}${ACTIONS}" ;;
	add-icons)
		# Add new icons on the ~/Desktop from /usr/share/applications.
		#
		DESKTOP_DIALOG="
<window title=\"$ADD_ICON_LABEL\" icon-name=\"document-new\">
<vbox>
	<text use-markup=\"true\" width-chars=\"40\">
		<label>\"
<b>$ADD_ICON_LABEL</b>
\"
		</label>
	</text>
	<tree headers_visible=\"false\">
		<width>420</width><height>200</height>
		<variable>ICON</variable>
		<label>Filename|Application</label>"
		# Get application name and icon.
		cd /usr/share/applications
		for file in *.desktop
		do
			# Try to get the name in the right locale.
			NAME=`grep ^Name $file | grep $lang || grep ^Name= $file`
			NAME=`echo $NAME | cut -d "=" -f 2`
			ICON=`grep ^Icon= $file | cut -d "=" -f 2`
			ICON=`basename $ICON`
			ICON=${ICON%.*}
			FILE=${file%.desktop}
			ITEM="<item icon=\"$ICON\">$FILE | $NAME</item>"
			DESKTOP_DIALOG="${DESKTOP_DIALOG}${ITEM}"
		done
		ACTIONS='<action>cp /usr/share/applications/$ICON.desktop ~/Desktop</action>
	</tree>
	<hbox>
		<button>
			<label>Add</label>
			<input file icon="gtk-add"></input>
			<action>cp /usr/share/applications/$ICON.desktop ~/Desktop</action>
		</button>
		<button>
			<label>Exit</label>
			<input file icon="exit"></input>
			<action type="exit">Exit</action>
		</button>
	</hbox>
</vbox>
</window>'
		export DESKTOP_DIALOG=${DESKTOP_DIALOG}${ACTIONS} ;;
	logout)
		# X session/system logout.
		#
		DESKTOP_DIALOG="
<window title=\"SliTaz Desktop logout\" icon-name=\"user-desktop\" skip_taskbar_hint=\"true\">
<vbox>
	<pixmap>
		<input file>/usr/share/icons/Tango/32x32/places/user-desktop.png</input>
	</pixmap>
	<hbox>
		<text use-markup=\"true\" width-chars=\"$CHARS_SIZE\">
			<label>
\"<b>$DESKTOP_DIALOG_LABEL</b>
\"
			</label>
		</text>
	</hbox>"
		TAZUSB_DIALOG="
	<hbox>
		<checkbox>
			<label>$DESKTOP_DIALOG_TAZUSB</label>
			<variable>TAZUSB_WRITE</variable>
			<default>false</default>
		</checkbox>
		<radiobutton>
			<label>lzma</label>
			<variable>LZMA</variable>
		</radiobutton>
		<radiobutton active=\"true\">
			<label>gzip</label>
			<variable>GZIP</variable>
		</radiobutton>
		<radiobutton>
			<label>none</label>
			<variable>NONE</variable>
		</radiobutton>
	</hbox>"
		EXTRA="COMP=none; [ \$LZMA = true ] && COMP=lzma; [ \$GZIP = true ] && COMP=gzip; [ \$TAZUSB_WRITE = true ] && { subox \"xterm -e '/usr/bin/tazusb writefs \$COMP'\"; sleep 1; while ps x | grep -v grep | grep -q tazusb; do sleep 1; done; };"
		[ -f /home/boot/rootfs.gz ] || { TAZUSB_DIALOG=""; EXTRA=""; }
		# Logout for Openbox or JWM and system shutdown or reboot.
		ACTIONS="
	<hbox>
		<button>
			<label>$DESKTOP_LOGOUT_BUTTON</label>
			<input file icon=\"video-display\"></input>
			<action>$EXTRA openbox --exit || jwm -exit</action>
			<action type=\"exit\">Exit</action>
		</button>
		<button>
			<label>$DESKTOP_SHUTDOWN_BUTTON</label>
			<input file icon=\"system-shutdown\"></input>
			<action>$EXTRA poweroff</action>
			<action type=\"exit\">Exit</action>
		</button>
		<button>
			<label>$DESKTOP_REBOOT_BUTTON</label>
			<input file icon=\"reload\"></input>
			<action>$EXTRA reboot</action>
			<action type=\"exit\">Exit</action>
		</button>
		<button cancel>
			<action type=\"exit\">Exit</action>
		</button>
	</hbox>
</vbox>
</window>"
		export DESKTOP_DIALOG=${DESKTOP_DIALOG}${TAZUSB_DIALOG}${ACTIONS} ;;
	notify)
		# Nofification message without window decoration.
		MSG="$2"
		SEC=$3
		[ -z $SEC ] && SEC=4
		export NOTIFY_BOX="
<window decorated=\"false\" skip_taskbar_hint=\"true\">
<vbox>
	<text width-chars=\"64\" use-markup=\"true\">
		<label>\"
<b>$MSG</b>
		\"</label>
	</text>
</vbox>
</window>"
		gtkdialog --center --program=NOTIFY_BOX >/dev/null &
		sleep $SEC
		pid=`ps | grep NOTIFY_BOX | awk '{ print $1 }'`
		kill $pid 2>/dev/null
		exit 0 ;;
	autostart)
		# Autostarted apps management. Functions are used for input 
		# and actions
		export DESKTOP_DIALOG='
<window title="Auto start applications with Openbox" icon-name="preferences-system-session">
<vbox>
	<tree>
		<width>540</width><height>200</height>
		<variable>APPLICATION</variable>
		<label>Application|Comment</label>
		<input icon_column="0">$BIN autostart_list</input>
		<action>$BIN autostart_actions</action>
		<action>refresh:APPLICATION</action>
	</tree>
	<hbox>
		<text width-chars="36">
				<label>
"Double click to enable/disable an application"
				</label>
			</text>
		<button>
			<label>Add</label>
			<input file icon="gtk-add"></input>
			<action>$BIN add_autostarted_app_box</action>
			<action>refresh:APPLICATION</action>
		</button>
		<button>
			<label>Configuration</label>
			<input file icon="accessories-text-editor"></input>
			<action>editor $AUTOSTART_SCRIPT</action>
			<action>refresh:APPLICATION</action>
		</button>
		<button>
			<label>Exit</label>
			<input file icon="exit"></input>	
			<action type="exit">exit</action>
		</button>
	</hbox>
</vbox>
</window>'
		;;
	tazapps)
		# Default applications configuration script. System wide config file
		# is /etc/slitaz/applications.conf and each user can have personnal
		# settings. System wide for root and personnal config for user.
		export CONFIG="$HOME/.config/slitaz/applications.conf"
		if [ ! -f $CONFIG ]; then
			mkdir -p $HOME/.config/slitaz
			cp /etc/slitaz/applications.conf $CONFIG
		fi
		export DESKTOP_DIALOG='
<window title="SliTaz default applications" icon-name="preferences-desktop">
<vbox>
	<vbox>
		<text use-markup="true" width-chars="54">
			<label>"
<b>SliTaz default applications configuration</b>
			"</label>
		</text>
	</vbox>
	<hbox>
		<text wrap="false">
			<label>"File manager:       "</label>
		</text>
		<entry>
			<input>. $CONFIG; echo $FILE_MANAGER</input>
			<variable>FILE_MANAGER</variable>
		</entry>
		<button>
			<label>Change</label>
			<input file icon="forward"></input>
			<action>sed -i s/"FILE_MANAGER=.*"/"FILE_MANAGER=\"$FILE_MANAGER\""/ $CONFIG</action>
		</button>
	</hbox>
	<hbox>
		<text wrap="false">
			<label>"Web browser:      "</label>
		</text>
		<entry>
			<input>. $CONFIG; echo $BROWSER</input>
			<variable>BROWSER</variable>
		</entry>
		<button>
			<label>Change</label>
			<input file icon="forward"></input>
			<action>sed -i s/"BROWSER=.*"/"BROWSER=\"$BROWSER\""/ $CONFIG</action>
		</button>
	</hbox>
	<hbox>
		<text wrap="false">
			<label>"Text editor:          "</label>
		</text>
		<entry>
			<input>. $CONFIG; echo $EDITOR</input>
			<variable>EDITOR</variable>
		</entry>
		<button>
			<label>Change</label>
			<input file icon="forward"></input>
			<action>sed -i s/"EDITOR=.*"/"EDITOR=\"$EDITOR\""/ $CONFIG</action>
		</button>
	</hbox>
	<hbox>
		<text wrap="false">
			<label>"Terminal:              "</label>
		</text>
		<entry>
			<input>. $CONFIG; echo $TERMINAL</input>
			<variable>TERMINAL</variable>
		</entry>
		<button>
			<label>Change</label>
			<input file icon="forward"></input>
			<action>sed -i s/"TERMINAL=.*"/"TERMINAL=\"$TERMINAL\""/ $CONFIG</action>
		</button>
	</hbox>
	<hbox>
		<text wrap="false">
			<label>"Window manager:"</label>
		</text>
		<entry>
			<input>. $CONFIG; echo $WINDOW_MANAGER</input>
			<variable>WINDOW_MANAGER</variable>
		</entry>
		<button>
			<label>Change</label>
			<input file icon="forward"></input>
			<action>sed -i s/"WINDOW_MANAGER=.*"/"WINDOW_MANAGER=\"$WINDOW_MANAGER\""/ $CONFIG</action>
		</button>
	</hbox>
	<hbox>
		<button>
			<label>Exit</label>
			<input file icon="exit"></input>	
			<action type="exit">exit</action>
		</button>
	</hbox>
</vbox>
</window>' ;;
	*_*)
		# Exec all function called by args (must have an underscore).
		$1
		exit 0 ;;
	*)
		# Usage if executed from cmdline.
		#
		usage
		exit 0 ;;
esac

gtkdialog --center --program=DESKTOP_DIALOG >/dev/null

exit 0
