#!/bin/sh
#(c) Copyright Barry Kauler, Jan 2013. bkhome.org
#Licence: GPL3 (/usr/share/doc/legal).
#140101 new wallpaper setting application. see also /usr/sbin/set_bg, background_reshape, /root/.xinitrc
#160727 set wallpaper on commandline, bypass gui. also handle svg.
#220303 adjust compatibility with Woof-CE and JWMdesk (radky)

export TEXTDOMAIN=qwallpaper
export OUTPUT_CHARSET=UTF-8
export APPDIR=/usr/local/jwmdesk

[ "`which gtkdialog4 2>/dev/null`" ] && export GTKDIALOG=gtkdialog4 || export GTKDIALOG=gtkdialog

mkdir -p $HOME/.config/wallpaper

#define header
XML_INFO_COLOR='#EDEBD7' # background color
XML_INFO_OPACITY=0.5 # background opacity
. $APPDIR/xml_info_jwmdesk gtk > /dev/null # build bg_pixmap for gtk-theme

BOX_HEIGHT=90 # HEADER
ICON=$APPDIR/icons/wallpaper.svg
ICON_HEIGHT=70
MSG_1="<b><span size='"'x-large'"'>$(gettext "Qwallpaper")</span></b>"
MSG_2="<b>$(gettext "Set desktop background")</b>"
ALIGN=center # center or left
HEADER="
   <hbox height-request="'"${BOX_HEIGHT}"'">
   $(. $APPDIR/xml_info_jwmdesk "$ICON" "$ICON_HEIGHT" "$MSG_1" "$MSG_2" "$ALIGN")
   </hbox>"

#define icon-name
[ ! -f /usr/share/icons/hicolor/48x48/apps/wallpaper.svg ] && \
ln -sf $APPDIR/icons/wallpaper.svg /usr/share/icons/hicolor/48x48/apps && gtk-update-icon-cache -f -i /usr/share/icons/hicolor 2>/dev/null

#define filemanager and imageviewer
for f in defaultfilemanager roxfiler rox pcmanfm Thunar; do [ "`which $f 2>/dev/null`" ] && { FILEMANAGER="$f"; break; } done
for i in defaultimageviewer gpicview viewnior qview geeqie gwenview ristretto ; do [ "`which $i 2>/dev/null`" ] && { IMAGEVIEWER="$i"; break; } done
export FILEMANAGER IMAGEVIEWER

#define background mode
bg_mode=$(cat $HOME/.config/wallpaper/backgroundmode 2>/dev/null)
if [[ ! $(echo $bg_mode | grep -E 'Centre$|Scale$|Stretch$|Tile$') ]]; then
 bg_mode="Stretch"
 echo "$bg_mode" > $HOME/.config/wallpaper/backgroundmode
fi

bg_mode_options="
     <hseparator space-expand=\"false\" space-fill=\"false\"></hseparator>
     <comboboxtext width-request=\"100\" space-expand=\"false\" space-fill=\"false\" tooltip-text=\" $(gettext 'Image mode') \">
     <variable>bg_mode</variable>
     <default>$bg_mode</default>
     <item>Centre</item>
     <item>Scale</item>
     <item>Stretch</item>
     <item>Tile</item>
     <action>echo \$bg_mode > $HOME/.config/wallpaper/backgroundmode</action>
    </comboboxtext>"

ps="`busybox ps`"
echo "$ps" | grep -q -m1 'pcmanfm --desktop' && PCMANFM=yes
[ "$PCMANFM" ] && bg_mode_options=""

#160727
if [ $1 ];then
 [ ! -f "$@" ] && exit 1
 commFILE="$@"
 commBASE="$(basename "$commFILE")"
 case $commBASE in
  *.svg|*.SVG)
   xcommBASE="$(echo -n $commBASE | rev | cut -f 2- -d '.' | rev)"
   rsvg-convert -f png -o /usr/share/backgrounds/${xcommBASE}.png "$commFILE"
   commBASE=${xcommBASE}.png
  ;;
  *.jpg|*.jpeg|*.JPG|*.JPEG)
   true
  ;;
  *.png|*.PNG)
   true
  ;;
  *)
   exit 1
  ;;
 esac
 [ ! -f /usr/share/backgrounds/${commBASE} ] && cp -a "$commFILE" /usr/share/backgrounds/
 echo "/usr/share/backgrounds/${commBASE}" > $HOME/.config/wallpaper/bg_img
 set_bg /usr/share/backgrounds/${commBASE}
 exit 0
fi

#rox desktop (Puppy and EasyOS)
curIMAGEFILE=""
if [ -f $HOME/Choices/ROX-Filer/PuppyPin ];then
 if [ "`grep '<backdrop' $HOME/Choices/ROX-Filer/PuppyPin 2>/dev/null`" ]; then
  curIMAGEFILE="`grep '<backdrop' $HOME/Choices/ROX-Filer/PuppyPin | cut -f 2 -d '>' | cut -f 1 -d '<'`"
  curIMAGEBASE=${curIMAGEFILE##*/}
  #rox has it's own drag-n-drop thing for the wallpaper, image can be anywhere...
  [ ! -f /usr/share/backgrounds/${curIMAGEBASE} ] && cp -f "$curIMAGEFILE" /usr/share/backgrounds/
 fi
fi

#non-rox desktop (Puppy)
ps="`busybox ps`"
echo "$ps" | grep -q -m1 "ROX-Filer -p" && ROX_PINBOARD="yes"
if [ ! "$ROX_PINBOARD" ]; then
 curIMAGEFILE=""
 if [ "`grep 'Background type=' $HOME/.jwm/jwmrc-wallpaper 2>/dev/null`" ]; then
  curIMAGEFILE="`grep 'Background type=' $HOME/.jwm/jwmrc-wallpaper | cut -f 2 -d '>' | cut -f 1 -d '<'`"
  curIMAGEBASE=${curIMAGEFILE##*/}
 fi
fi

#non-rox desktop (EasyOS)
if [ -d /usr/local/easy_version ]; then
 bg_mode_options=""
 if [ -f /etc/uimanager ]; then
  . /etc/uimanager
  if [ "$UI_DESK_MANAGER" == "jwm" ]; then
   if [ "`grep 'Background type=' $HOME/.jwm/jwmrc-personal 2>/dev/null`" ]; then
    curIMAGEFILE="`grep 'Background type=' $HOME/.jwm/jwmrc-personal | cut -f 2 -d '>' | cut -f 1 -d '<'`"
    curIMAGEBASE=${curIMAGEFILE##*/}
   fi
  fi
 fi
fi

if [ "$curIMAGEFILE" ];then
 #qwallpaper reads this...
 echo -n "/usr/share/backgrounds/${curIMAGEBASE}" > $HOME/.config/wallpaper/bg_img
else
 if [ ! -s $HOME/.config/wallpaper/bg_img ];then
  curIMAGEFILE="`ls -1 /usr/share/backgrounds/default.* | head -n 1`"
  [ ! "$curIMAGEFILE" ] && curIMAGEFILE="`ls -1 /usr/share/backgrounds/*.jpg | head -n 1`"
  [ ! "$curIMAGEFILE" ] && curIMAGEFILE="`ls -1 /usr/share/backgrounds/*.svg | head -n 1`" #ex: standard Woof-CE wallpaper
  echo -n "$curIMAGEFILE" > $HOME/.config/wallpaper/bg_img #ex: /usr/share/backgrounds/default.png
 else
  curIMAGEFILE="`cat $HOME/.config/wallpaper/bg_img`"
 fi
 curIMAGEBASE=${curIMAGEFILE##*/}
fi

#note, /usr/share/backgrounds/${curIMAGEBASE} is not the actual image that gets displayed, that will be in an "aspect ratio" subdir.
ln -snf /usr/share/backgrounds/"${curIMAGEBASE}" /tmp/qwallpaper-preview-image

#create list of available backgrounds ; move current background to top of list and remove trailing duplicate
echo -e "$curIMAGEBASE\n$(ls -1 /usr/share/backgrounds | grep -E 'png$|PNG$|jpeg$|JPEG$|jpg$|JPG$|svg$|SVG$')" | awk '! seen[$0]++' > /tmp/qwallpaper-images

export QW_DLG="
<window title=\"$(gettext 'Qwallpaper')\" resizable=\"true\" icon-name=\"wallpaper\">
 <vbox>

  <vbox>
   ${HEADER}
  </vbox>

  <vbox space-expand=\"true\" space-fill=\"true\">
   <hbox space-expand=\"true\" space-fill=\"true\">
    <vbox space-expand=\"true\" space-fill=\"true\">
     <tree has-focus=\"true\" enable-search=\"false\" headers-visible=\"false\">
      <variable>IMAGE</variable>
      <input>cat /tmp/qwallpaper-images</input>
      <action signal=\"button-release-event\">ln -snf /usr/share/backgrounds/\"\$IMAGE\" /tmp/qwallpaper-preview-image</action>
      <action signal=\"button-release-event\" type=\"refresh\">PIXMAP</action>
      <action signal=\"key-release-event\">ln -snf /usr/share/backgrounds/\"\$IMAGE\" /tmp/qwallpaper-preview-image</action>
      <action signal=\"key-release-event\" type=\"refresh\">PIXMAP</action>
     </tree>
     $bg_mode_options
    </vbox>
     <vseparator space-expand=\"false\" space-fill=\"false\"></vseparator>
    <vbox height-request=\"345\" homogeneous=\"true\" space-expand=\"false\" space-fill=\"false\">
     <pixmap>
      <variable>PIXMAP</variable>
      <input file>/tmp/qwallpaper-preview-image</input><width>330</width>
     </pixmap>
    </vbox>
   </hbox>

   <hseparator space-expand=\"false\" space-fill=\"false\"></hseparator>

   <hbox space-expand=\"false\" space-fill=\"false\">
    <button space-expand=\"true\" space-fill=\"true\">
     <label>$(gettext 'Apply')</label>
     <input file>$APPDIR/icons/apply.svg</input><height>18</height><width>18</width>
     <action>echo /usr/share/backgrounds/\$IMAGE > $HOME/.config/wallpaper/bg_img</action>
     <action>set_bg /usr/share/backgrounds/\"\$IMAGE\"</action>
    </button>
    <button space-expand=\"true\" space-fill=\"true\">
     <label>$(gettext 'Files')</label>
     <input file>$APPDIR/icons/manage.svg</input><height>20</height><width>20</width>
     <action>$FILEMANAGER /usr/share/backgrounds &</action>
    </button>
    <button space-expand=\"true\" space-fill=\"true\">
     <label>$(gettext 'View')</label>
     <input file>$APPDIR/icons/view.svg</input><height>20</height><width>20</width>
     <action>$IMAGEVIEWER \"\$(readlink /tmp/qwallpaper-preview-image)\" &</action>
    </button>
    <button space-expand=\"true\" space-fill=\"true\">
     <label>$(gettext 'Quit')</label>
     <input file>$APPDIR/icons/quit.svg</input><height>18</height><width>18</width>
     <action type=\"exit\">QWEXIT</action>
    </button>
   </hbox>
  </vbox>

 </vbox>
</window>
"
$GTKDIALOG -p QW_DLG -G +50+50 --styles=/tmp/jwmdesk/gtkrc_xml_info.css

###END###
