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

# Glade XML file path.
GLADE_XML=/usr/share/slitaz-tools/glade

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

# Get the active locale (default to English).
case $LANG in
	es*)
		lang="es"
		NEW_FOLDER_LABEL="Creat a new folder on the desktop:"
		FOLDER_ENTRY_MSG="dirname"
		NEW_FILE_LABEL="Creat a new file on the desktop:"
		FILE_ENTRY_MSG="filename"
		ADD_ICON_LABEL="Add some desktop icons"
		DESKTOP_DIALOG_LABEL="Session logout, system shutdown or reboot"  ;;
	fr*)
		lang="fr"
		NEW_FOLDER_LABEL="Créer un nouveau dossier sur le bureau:"
		FOLDER_ENTRY_MSG="dossier"
		NEW_FILE_LABEL="Créer un nouveau fichier sur le bureau:"
		FILE_ENTRY_MSG="fichier"
		ADD_ICON_LABEL="Ajouter des icons de bureau"
		DESKTOP_DIALOG_LABEL="Déconnexion, arrêt ou redémarrage du système" ;;
	*)
		lang=""
		NEW_FOLDER_LABEL="Creat a new folder on the desktop:"
		FOLDER_ENTRY_MSG="dirname"
		NEW_FILE_LABEL="Creat a new file on the desktop:"
		FILE_ENTRY_MSG="filename"
		ADD_ICON_LABEL="Add some desktop icons"
		DESKTOP_DIALOG_LABEL="Session logout, system shutdown or reboot" ;;
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   Creat a new folder on the desktop with mkdir.
  new-file     Creat a new empty file or SHell script on the desktop.
  add-icons    Add a system icon on the desktop.
  calendar     Display a calendard under mouse pointer.
  logout       Prompt for X session exit or system halt/reboot.\n"
}

# Box commands.
case $1 in
	new-folder)
		# Creat 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)
		# Creat 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 a 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} ;;
	calendar)
		# Calendar using glade file.
		#
		gtkdialog --glade-xml=$GLADE_XML/calendar.glade \
        	--program=MAIN_WINDOW ;;
	logout)
		# X session/system logout.
		#
		DESKTOP_DIALOG="
<window title=\"SliTaz Desktop logout\" icon-name=\"user-desktop\">
<vbox>
	<pixmap>
		<input file>/usr/share/icons/Tango/32x32/places/user-desktop.png</input>
	</pixmap>
	<hbox>
		<text use-markup=\"true\" width-chars=\"64\">
			<label>
\"<b>$DESKTOP_DIALOG_LABEL</b>
\"
			</label>
		</text>
	</hbox>"
		# Logout for Openbox or JWM and system shutdown or reboot.
		ACTIONS='
	<hbox>
		<button>
			<label>Logout X session</label>
			<input file icon="video-display"></input>
			<action>openbox --exit || jwm -exit</action>
			<action type="exit">Exit</action>
		</button>
		<button>
			<label>Shutdown computer</label>
			<input file icon="system-shutdown"></input>
			<action>poweroff</action>
			<action type="exit">Exit</action>
		</button>
		<button>
			<label>Reboot system</label>
			<input file icon="reload"></input>
			<action>reboot</action>
			<action type="exit">Exit</action>
		</button>
		<button cancel>
			<action type="exit">Exit</action>
		</button>
	</hbox>

</vbox>
</window>'
		export DESKTOP_DIALOG=${DESKTOP_DIALOG}${ACTIONS} ;;
	*)
		# Usage if executet from cmdline.
		#
		usage
		exit 0 ;;
esac

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

exit 0
