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

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

# Standard 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="Create a new folder on the desktop:"
		FOLDER_ENTRY_MSG="dirname"
		NEW_FILE_LABEL="Create a new file on the desktop:"
		FILE_ENTRY_MSG="filename"
		ADD_ICON_LABEL="Add some desktop icons"
		DESKTOP_DIALOG_TAZUSB="Save filesystem using compression"
		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_TAZUSB="Enregistrer le système avec la compression"
		DESKTOP_DIALOG_LABEL="Déconnexion, arrêt ou redémarrage du système" ;;
	*)
		lang=""
		NEW_FOLDER_LABEL="Create a new folder on the desktop:"
		FOLDER_ENTRY_MSG="dirname"
		NEW_FILE_LABEL="Create a new file on the desktop:"
		FILE_ENTRY_MSG="filename"
		ADD_ICON_LABEL="Add some desktop icons"
		DESKTOP_DIALOG_TAZUSB="Save filesystem using compression"
		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   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 on the desktop.
  calendar     Display a calendar under mouse pointer.
  logout       Prompt for X session exit or system halt/reboot.\n"
}

# 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} ;;
	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>"
		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>Logout X session</label>
			<input file icon=\"video-display\"></input>
			<action>$EXTRA openbox --exit || jwm -exit</action>
			<action type=\"exit\">Exit</action>
		</button>
		<button>
			<label>Shutdown computer</label>
			<input file icon=\"system-shutdown\"></input>
			<action>$EXTRA poweroff</action>
			<action type=\"exit\">Exit</action>
		</button>
		<button>
			<label>Reboot system</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} ;;
	*)
		# Usage if executed from cmdline.
		#
		usage
		exit 0 ;;
esac

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

exit 0
