#!/bin/sh
#ptheme_roxpin - change rox pinboard icons
#Copyright - GPL 2014
#Sigmund Berglund


export TEXTDOMAIN=ptheme
export OUTPUT_CHARSET=UTF-8

export WORKDIR=$HOME/.ptheme/tmp/ptheme
[ ! -d $WORKDIR ] && mkdir -p $WORKDIR


find /usr/share/ptheme/rox_pinboard -mindepth 1 -maxdepth 1 -type f -printf "%f\n" | sort > $WORKDIR/ptheme_roxpins

apply_roxpin (){
	[ "`grep -F 'pwidgets' <<< $(ps -eo pid,command)`" ] && UPDATE_PWIDGETS=true
	TMP_BACKDROP="`grep -F '<backdrop ' $HOME/Choices/ROX-Filer/PuppyPin`"
	TMP_DRIVEICONS="`grep -F '.pup_event' $HOME/Choices/ROX-Filer/PuppyPin`"
	echo -e '<?xml version="1.0"?>\n<pinboard>' > $WORKDIR/tmp_puppypin
	echo "$TMP_BACKDROP" >> $WORKDIR/tmp_puppypin
	cat /usr/share/ptheme/rox_pinboard/"$ROXPIN" >> $WORKDIR/tmp_puppypin
	echo "$TMP_DRIVEICONS" >> $WORKDIR/tmp_puppypin
	echo '</pinboard>' >> $WORKDIR/tmp_puppypin
	mv $WORKDIR/tmp_puppypin $HOME/Choices/ROX-Filer/PuppyPin

	PID=`grep '/bin/ash.*/sbin/pup_event_frontend_d' <<< $(ps -eo pid,command) | awk '{print $1}'`
	kill -9 $PID
	echo 'ICONWIPE' > /tmp/pup_event_icon_change_flag
	/sbin/clean_desk_icons
	rox -p "$HOME/Choices/ROX-Filer/PuppyPin"
	kill $(pidof pup_event_frontend_d)
	pup_event_frontend_d &

	[ "UPDATE_PWIDGETS" ] && fixwidgets
}

save_roxpin (){
	export savebox='
	<window title="pTheme - '$(gettext 'Save')'" icon-name="gtk-save" resizable="false">
	<vbox>
	  <frame Save>
	    <hbox>
	      <text><label>'$(gettext 'Template name')'</label></text>
	      <comboboxentry width-request="300">
	        <variable>SAVEFILE</variable>
	        <input file>'$WORKDIR'/ptheme_roxpins</input>
	      </comboboxentry>
	    </hbox>
	  </frame>
	  <hbox space-expand="false" space-fill="false">
	    <button>
	      '"`/usr/lib/gtkdialog/xml_button-icon cancel`"'
	      <label>'$(gettext 'Cancel')'</label>
	      <action type="exit">cancel</action>
	    </button>
	    <button can-default="true" has-default="true" use-stock="true">
	      '"`/usr/lib/gtkdialog/xml_button-icon save`"'
	      <label>'$(gettext 'Save')'</label>
	      <action type="exit">save</action>
	    </button>
	  </hbox>
	</vbox>
	</window>'
	for STATEMENTS in "$(gtkdialog -p savebox --center)"; do
		eval $STATEMENTS 2> /dev/null
	done
	if [ "$EXIT" = "save" ]; then
		if [ -f "/usr/share/ptheme/rox_pinboard/$SAVEFILE" ]; then
			. /usr/lib/gtkdialog/box_yesno "pTheme" "$(gettext 'Theme already exist.')" "<b>$(gettext 'Do you want to overwrite?')</b>"
			[ $EXIT = "no" ] && exit
		fi
		grep -Fv '<.pup_event ' $HOME/Choices/ROX-Filer/PuppyPin | grep -F '<icon ' > "/usr/share/ptheme/rox_pinboard/$SAVEFILE"
		ls -1A /usr/share/ptheme/rox_pinboard > $WORKDIR/ptheme_roxpins #update list in gui
	fi
}

export -f apply_roxpin save_roxpin

export pThemeRoxPin='
<window title="'$(gettext 'Desktop icons')'" icon-name="gtk-preferences">
<vbox space-expand="true" space-fill="true">
  '"`/usr/lib/gtkdialog/xml_info fixed "puppy_theme.svg" 60 "<b>$(gettext 'Desktop icons')</b>" "$(gettext 'What icons should be placed on the desktop. Choose a template or save existing icons setup to a new one.')"`"' 
  <frame '$(gettext 'Desktop icons templates')'>
      <tree rules_hint="true" space-expand="true" space-fill="true">
        <label>'$(gettext 'Themes')'</label>
        <variable>ROXPIN</variable>
        <height>200</height><width>50</width>
        <input file>'$WORKDIR'/ptheme_roxpins</input>
        <action>apply_roxpin</action>
      </tree>
      <hbox space-expand="true" space-fill="true">
        <button space-expand="false" space-fill="false">
          '"`/usr/lib/gtkdialog/xml_button-icon harddisk`"'
          <action>eventmanager desktop &</action>
        </button>
        <text xalign="0" space-expand="true" space-fill="true"><label>'$(gettext 'Manage drive icons')'</label></text>
      </hbox>
      <hbox space-expand="true" space-fill="true">
        <button space-expand="false" space-fill="false">
          '"`/usr/lib/gtkdialog/xml_button-icon desktop_icons`"'
          <action>icon_switcher &</action>
        </button>
        <text xalign="0" space-expand="true" space-fill="true"><label>'$(gettext 'Icons theme')'</label></text>
      </hbox>
      <hbox space-expand="true" space-fill="true">
        <button space-expand="false" space-fill="false">
          '"`/usr/lib/gtkdialog/xml_button-icon puppy_theme`"'
          <action>ptheme &</action>
        </button>
        <text xalign="0" space-expand="true" space-fill="true"><label>'$(gettext 'Global theming')'</label></text>
      </hbox>
    </frame>
  <hbox space-expand="false" space-fill="false">
    <text space-expand="true" space-fill="true"><label>""</label></text>
    <button>
      '"`/usr/lib/gtkdialog/xml_button-icon save`"'
     <label>'$(gettext 'Save my icon setup')'</label>
     <action>save_roxpin</action>
     <action>refresh:SAVE</action>
   </button>
    <button>
      '"`/usr/lib/gtkdialog/xml_button-icon apply`"'
     <label>'$(gettext 'Apply')'</label>
     <action>apply_roxpin</action>
   </button>
   <button>
     '"`/usr/lib/gtkdialog/xml_button-icon quit`"'
     <label>'$(gettext 'Quit')'</label>
     <action>EXIT:exit</action>
   </button>
   '"`/usr/lib/gtkdialog/xml_scalegrip`"'
  </hbox>
</vbox>
</window>'

. /usr/lib/gtkdialog/xml_info gtk #build bg_pixmap for gtk-theme
gtkdialog -p pThemeRoxPin 2> /dev/null
