#!/bin/sh

. `kf5-config --prefix`/share/kim/kim_translation

CANCEL=`kdialog --warningcontinuecancel "$(gettext "This feature is supposed to create a Flash slideshow, but Flash is dead now!  Are you sure you want to do that?")"`

if [ $CANCEL -eq 1 ]; then
	exit
fi

# Create a Flash slideshow
FILE="";
let "nbfiles = $#"
kdeinstdir=`kf5-config --prefix`
KIM_DIR=$kdeinstdir/share/kim/slideshow
TITLE=`kdialog --inputbox "$(gettext "Kim - Title:")" "$(gettext "My title ...")"`;
AUTHOR=`kdialog --inputbox "$(gettext "Kim - Author:")" "$USER"`;
DATE=`date '+%Y%m%d'`;
SCALE=`kdialog --inputbox "$(gettext "Kim - Size:")" "600x450"`;
DIR=`kdialog --getexistingdirectory /home/$USER`;
W=`echo $SCALE | sed 's/[^0-9]/ /g' | awk '{print $1}'`;
H=`echo $SCALE | sed 's/[^0-9]/ /g' | awk '{print $2}'`;

dbusRef=`kdialog --progressbar "$(gettext "Kim - Initialising ...")" $nbfiles`
qdbus $dbusRef showCancelButton true

mkdir $DIR/images;
cp $KIM_DIR/dewslider.swf $DIR;
# Header of $DIR/index.html
cp $KIM_DIR/index.part1 $DIR/index.html;
echo '<h1>'$TITLE'</h1>' >> $DIR/index.html;
echo '<h5>Author: '$AUTHOR'</h5>' >> $DIR/index.html;
echo '<h5>Date: '$DATE'</h5>' >> $DIR/index.html;
let W2="$W+40";
let H2="$H+60";
echo '<object type="application/x-shockwave-flash" data="dewslider.swf?xml=dewslider.xml" width="'$W2'" height="'$H2'">' >> $DIR/index.html;
cat $KIM_DIR/index.part2 >> $DIR/index.html;
# Header of $DIR/dewslider.xml
echo '<?xml version="1.0" ?>' > $DIR/dewslider.xml;
echo '<album' >> $DIR/dewslider.xml;
echo 'width="'$W'" height="'$H'"' >> $DIR/dewslider.xml;
echo 'bgcolor="0xeeeeee" framecolor="0xffffff" padding="10" margin="10"' >> $DIR/dewslider.xml;
echo 'shadow="8" shadowopacity="15%" shadowcolor="0x000000" showbuttons="on"' >> $DIR/dewslider.xml;
echo 'buttonstyle="flat" showtxt="on" txtcolor="0xcccccc" auto="5" fullscreen="off"' >> $DIR/dewslider.xml;
echo '>' >> $DIR/dewslider.xml;

compteur=0
for i in "$@";do
	if [ -f "$i" ];then
		#test if cancel button has been pushed
		if [[ "$(qdbus $dbusRef wasCancelled)" == "true" ]] ; then
			qdbus $dbusRef close
			exit 1
		fi
		let "compteur +=1"
		FILE="$i"
		convert -resize $SCALE "$FILE" $DIR/images/`basename "$FILE"`.jpg;
		echo '<img src="images/'`basename "$FILE"`'.jpg" title="'`basename "$FILE"`'" />' >> $DIR/dewslider.xml;
		qdbus $dbusRef setLabelText "$(gettext "Kim - Treatment of file: ") `basename "$FILE"`"
 		qdbus $dbusRef org.freedesktop.DBus.Properties.Set org.kde.kdialog.ProgressDialog value "$compteur"
	fi;
done

echo '</album>' >> $DIR/dewslider.xml;
qdbus $dbusRef close;
x-www-browser $DIR/index.html;
