#! /bin/sh
#pBurn - Burning gui
#Sigmund Berglund, copyright 2007-2013
export TEXTDOMAIN=pburn

#------------------------------
#This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation version 2.

#This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. <http://www.gnu.org/licenses/>.
#------------------------------

#Update infotext
. $APPDIR/func -info_burn
#GTK-theme
if [ -f $HOME/.gtkrc-2.0 ]; then export GTK2_RC_FILES="$HOME/.gtkrc-2.0" #get active theme
	elif [ -f /etc/gtk-2.0/gtkrc ]; then GTK_OLD_PUPPY=':/etc/gtk-2.0/gtkrc' #/etc/gtk-2.0/gtkrc is for < Puppy4
fi
export GTK2_RC_FILES="$GTK2_RC_FILES$GTK_OLD_PUPPY:$APPDIR/themes/$THEME/gtk/gtkrc"
#make items for <combobox> (devices)
echo -n > $WORKDIR/tmp
for I in `echo "$CD_DRIVES" | cut -d " " -f 1`; do
	echo "<item>""$I""</item>" >> $WORKDIR/tmp
done
export ITEMS_DEVICES_COMBO=($(<$WORKDIR/tmp))
OPEN_FILE=("$(<"$WORKDIR/open_file")")
#what's visible
if [ "$BURN_DATA" = "true" ]; then
	echo data > $WORKDIR/BURNLIST_MODE #normal startup. Others are handled in -open
	VISIBLE_MOVE_UPDOWN=disabled
	VISIBLE_NEWDIR=enabled
else
	VISIBLE_MOVE_UPDOWN=enabled
	VISIBLE_NEWDIR=disabled
fi
#generate gtk-bookmarks
if [ -s $HOME/.gtk-bookmarks ]; then
	BOOKMARKS=""
	cut -d/ -f3- $HOME/.gtk-bookmarks | cut -d' ' -f1 | sed -e 's/%20/ /g' | grep -v "^$" > $WORKDIR/tmp
	while read I; do
		BOOKMARKS="$BOOKMARKS
		<menuitem stock=\"gtk-directory\" label=\"$I\">
		 <action>DIR=\"$I\"; . $APPDIR/func -browser_go_to_dir</action>
		 <action type=\"refresh\">DIR</action>
		 <action>refresh:CHOOSER</action>
		</menuitem>"
	done < $WORKDIR/tmp
fi
#check pfileaserch
if [ "$BOOLEAN_PFILESEARCH" = "true" ]; then
   MENU_PFILESEARCH="<menuitem stock=\"gtk-find\" label=\"$(gettext 'Search')\">
    <action>$APPDIR/func -pfilesearch</action>
    <action>refresh:CHOOSER</action>
   </menuitem>
   <separator></separator>"
fi

#define theme-picture
[ -f $APPDIR/themes/$THEME/heading-main.png ] && PIC="<hbox><pixmap><input file>$APPDIR/themes/$THEME/heading-main_left.png</input></pixmap><pixmap><input file>$APPDIR/themes/$THEME/heading-main.png</input></pixmap></hbox>"

echo $BURN_DATA > $WORKDIR/BURN_DATA
echo $BURN_AUDIO > $WORKDIR/BURN_AUDIO
echo $BURN_VIDEO > $WORKDIR/BURN_VIDEO


S="
<window title=\"pBurn $VERSION - $OPEN_FILE\" icon-name=\"media-optical\" default_height=\"550\" default_width=\"790\">
<vbox>
 <menubar>
  <menu>
   <menuitem stock=\"gtk-new\"><action>. $APPDIR/func -new</action><action>refresh:BURNLIST</action></menuitem>
   <menuitem stock=\"gtk-open\"><action>EXIT:open</action></menuitem>
   <menuitem stock=\"gtk-save\"><action>. $APPDIR/func -save</action></menuitem>
   <menuitem stock=\"gtk-save-as\"><action>SAVEAS=true; . $APPDIR/func -save</action></menuitem>
   <menuitemseparator></menuitemseparator>
   <menuitem stock=\"gtk-cdrom\" label=\"$(gettext 'Import session(s) from disc')\">
    <action>. $APPDIR/func -import_session</action>
    <action>refresh:BURNLIST</action>
   </menuitem>
   <menuitem stock=\"gtk-open\" label=\"$(gettext 'Import (play)list')...\">
    <action>GO_CHOOSER=true; . $APPDIR/func -add_list</action>
    <action>refresh:BURNLIST</action>
    <action>refresh:BURN_DATA</action>
    <action>refresh:BURN_AUDIO</action>
    <action>refresh:BURN_VIDEO</action>
   </menuitem>
   <menuitem stock=\"gtk-open\"  label=\"$(gettext 'Import ISO-image')...\">
    <action>. $APPDIR/func -import_iso</action>
    <action>refresh:BURNLIST</action>
   </menuitem>
   <menuitem stock=\"gtk-save-as\" label=\"$(gettext 'Export to ISO-image')...\">
    <action>. $APPDIR/func -export_iso</action>
    <action>refresh:BURNLIST</action>
   </menuitem>
   <menuitemseparator></menuitemseparator>
   <menuitem stock=\"gtk-preferences\" label=\"$(gettext 'Preferences')...\">
    <action>. $APPDIR/func -config</action>
    <action>refresh:BURNLIST</action>
   </menuitem>
   <menuitemseparator></menuitemseparator>
   <menuitem stock=\"gtk-quit\"><action>EXIT:abort</action></menuitem>
   <label>$(gettext 'File')</label>
  </menu>
  <menu>
   $MENU_PFILESEARCH
   <menuitem stock=\"gtk-add\" label=\"$(gettext 'Add selection')\">
    <action>ADD=\"$(gettext 'Add selection') [$(gettext 'right click')]\"; BUTTON=3; . $APPDIR/func -add</action>
    <action>refresh:BURNLIST</action>
   </menuitem>
   <menuitem stock=\"gtk-open\" label=\"$(gettext 'New directory')...\">
    <action>. $APPDIR/func -burnlist_create_new_dir</action>
    <action>refresh:BURNLIST</action>
   </menuitem>
   <menuitem stock=\"gtk-convert\" label=\"$(gettext 'Rename')...\">
    <action>. $APPDIR/func -burnlist_rename</action>
    <action>refresh:BURNLIST</action>
   </menuitem>
   <menuitem stock=\"gtk-remove\" label=\"$(gettext 'Remove')\">
    <action>. $APPDIR/func -burnlist_remove</action>
    <action>refresh:BURNLIST</action>
   </menuitem>
   <menuitemseparator></menuitemseparator>
   <menuitem stock=\"gtk-add\" label=\"$(gettext 'Add audio files')\">
    <action>ADD=\"$(gettext 'Add audio files')\"; BUTTON=3; . $APPDIR/func -add</action>
    <action>refresh:BURNLIST</action>
   </menuitem>
   <menuitem stock=\"gtk-add\" label=\"$(gettext 'Add videos')\">
    <action>ADD=\"$(gettext 'Add videos')\"; BUTTON=3; . $APPDIR/func -add</action>
    <action>refresh:BURNLIST</action>
   </menuitem>
   <menuitem stock=\"gtk-add\" label=\"$(gettext 'Add images')\">
    <action>ADD=\"$(gettext 'Add images')\"; BUTTON=3; . $APPDIR/func -add</action>
    <action>refresh:BURNLIST</action>
   </menuitem>
   <menuitem stock=\"gtk-add\" label=\"$(gettext 'Add all')\">
    <action>ADD=\"$(gettext 'Add all')\"; BUTTON=3; . $APPDIR/func -add</action>
    <action>refresh:BURNLIST</action>
   </menuitem>

   <menuitemseparator></menuitemseparator>
   
   <menuitem stock=\"gtk-add\" label=\"$(gettext 'Add content of (play)list')\">
    <action>. $APPDIR/func -add_list</action>
    <action>refresh:BURNLIST</action>
    <action>refresh:BURN_DATA</action>
    <action>refresh:BURN_AUDIO</action>
    <action>refresh:BURN_VIDEO</action>
   </menuitem>
   <label>$(gettext 'Edit')</label>
  </menu>
  <menu>
   <menuitem stock=\"gtk-cdrom\" label=\"$(gettext 'Data CD/DVD/Blu-ray')...\"><action>BURN=data; . $APPDIR/func_burn_options</action></menuitem>
   <menuitem stock=\"gtk-cdrom\" label=\"$(gettext 'Audio CD')...\"><action>BURN=audio; . $APPDIR/func_burn_options</action></menuitem>
   <menuitem stock=\"gtk-cdrom\" label=\"$(gettext 'Video DVD/CD')...\"><action>BURN=video; . $APPDIR/func_burn_options</action></menuitem>"'
   
   <menuitemseparator></menuitemseparator>

   <menuitem stock="gtk-cdrom" label="ISO-'$(gettext 'image')'...">
    <action>BURN=iso; . '$APPDIR'/func_burn_options</action>
    <action>. '$APPDIR'/func -calculate_needed_space</action>
    <action>refresh:BURNLIST</action>
   </menuitem>
   <menuitem stock="gtk-cdrom" label="NRG-'$(gettext 'image')' (Nero)...">
    <action>BURN=iso; . '$APPDIR'/func_burn_options</action>
    <action>. '$APPDIR'/func -calculate_needed_space</action>
    <action>refresh:BURNLIST</action>'
    ! type nrgiso > /dev/null 2>&1 && S=$S'<sensitive>false</sensitive>'
   S=$S"</menuitem>
   
   <menuitemseparator></menuitemseparator>
   
   <menuitem stock=\"gtk-copy\" label=\"$(gettext 'Copy data disc')...\"><action>BURN=copy; . $APPDIR/func_burn_options</action></menuitem>
   <menuitem stock=\"gtk-copy\" label=\"$(gettext 'Copy audio CD')...\"><action>BURN=copy_audio; . $APPDIR/func_burn_options</action></menuitem>
   <menuitem stock=\"gtk-copy\" label=\"$(gettext 'Copy/shrink video DVD')...\"><action>BURN=copy_video; . $APPDIR/func_burn_options</action></menuitem>
   <menuitem stock=\"gtk-clear\" label=\"$(gettext 'Blank (Erase) disc')...\"><action>BURN=blank; . $APPDIR/func_burn_options</action><action>refresh:BURNLIST</action></menuitem>

   <menuitemseparator></menuitemseparator>
   <menuitem stock=\"gtk-cdrom\" label=\"$(gettext 'Audio from TOC file')...\"><action>TOC=true; . $APPDIR/func_toc_cue</action></menuitem>
   <menuitem stock=\"gtk-cdrom\" label=\"$(gettext 'Audio from CUE file')...\"><action>CUE=true; . $APPDIR/func_toc_cue</action></menuitem>
   <label>$(gettext 'Burn')</label>
  </menu>
  <menu>
   <menuitem stock=\"gtk-info\" label=\"$(gettext 'Disc info')...\"><action>. $APPDIR/func -disc_info</action></menuitem>
   <menuitemseparator></menuitemseparator>
   <menuitem stock=\"gtk-cdrom\" label=\"$(gettext 'Fixate') CD...\"><action>. $APPDIR/func -fixate_CD</action></menuitem>
   <menuitem stock=\"gtk-cdrom\" label=\"$(gettext 'Fixate') DVD...\"><action>. $APPDIR/func -fixate_DVD</action></menuitem>
   <menuitem stock=\"gtk-cdrom\" label=\"$(gettext 'Verify CD quality')...\"><action>. $APPDIR/func -c2scan</action></menuitem>
   <menuitem stock=\"gtk-convert\" label=\"$(gettext 'Convert NRG-image (Nero) to ISO-image')...\"><action>. $APPDIR/func -nrg2iso</action>"
   ! type nrgiso > /dev/null 2>&1 && S=$S'<sensitive>false</sensitive>'
   S=$S'</menuitem>
   <menuitemseparator></menuitemseparator>
   <menuitem label="'$(gettext 'Author files in list to Video-DVD filesystem')'..."><action>export BURN=export_video_ts; . '$APPDIR'/func_burn_options</action><action>refresh:BURNLIST</action></menuitem>
   <menuitem label="'$(gettext 'Author files in list to Video-DVD ISO-image')'..."><action>export BURN=export_iso_video; . '$APPDIR'/func_burn_options</action><action>refresh:BURNLIST</action></menuitem>
   <menuitemseparator></menuitemseparator>
   <menuitem stock="gtk-cdrom" label="'$(gettext 'Eject disc')'"><action>. '$APPDIR'/func -eject</action></menuitem>
   <menuitem stock="gtk-cdrom" label="'$(gettext 'Load tray')'"><action>. '$APPDIR'/func -load_tray</action></menuitem>
   <label>'$(gettext 'Tools')'</label>
  </menu>
  <menu>
   <menuitem stock="gtk-about"><action>. '$APPDIR'/func -about &</action></menuitem>
   <menuitem stock="gtk-help"><action>. '$APPDIR'/func -help &</action></menuitem>
   <menuitemseparator></menuitemseparator>
   <menuitem label="'$(gettext 'Dependency check')'"><action>'$APPDIR'/func -dependency_check</action></menuitem>
   <label>'$(gettext 'Help')'</label>
  </menu>
 </menubar>
 <hbox homogeneous="true">
  <hbox>
   <button>'"
    <label>\"   $(gettext 'Burn ISO-image')   \"</label>
    <action>BURN=iso; . $APPDIR/func_burn_options</action>
    <action>. $APPDIR/func -calculate_needed_space</action>
    <action>refresh:BURNLIST</action>
   </button>
   <button label=\"   $(gettext 'Blank disc')   \"><action>BURN=blank; . $APPDIR/func_burn_options</action></button>
   <button label=\"   $(gettext 'Copy data disc')   \"><action>BURN=copy; . $APPDIR/func_burn_options</action></button>
   <button label=\"   $(gettext 'Copy audio CD')   \"><action>BURN=copy_audio; . $APPDIR/func_burn_options</action></button>
   <button label=\"   $(gettext 'Copy/shrink video DVD')   \"><action>BURN=copy_video; . $APPDIR/func_burn_options</action></button>
  </hbox>
 </hbox>
  <hbox>
  <frame $(gettext 'Burn mode')>
   <hbox>
     <vbox>
      <radiobutton label=\"$(gettext 'Burn Data')\">
       <variable>BURN_DATA</variable>
       <input file>$WORKDIR/BURN_DATA</input>
       <action>if true . $APPDIR/func -calculate_needed_space; . $APPDIR/func -info_burn; echo data > $WORKDIR/BURNLIST_MODE</action>
       <action>if true refresh:INFO</action>
       <action>if true refresh:BURNLIST</action>
       <action>if true disable:MOVE_UP</action>
       <action>if true disable:MOVE_DOWN</action>
       <action>if true enable:BUTTON_NEWDIR</action>
      </radiobutton>
      <radiobutton label=\"$(gettext 'Burn Audio')\">
       <variable>BURN_AUDIO</variable>
       <input file>$WORKDIR/BURN_AUDIO</input>
       <action>if true . $APPDIR/func -calculate_needed_space; . $APPDIR/func -info_burn; echo audio > $WORKDIR/BURNLIST_MODE</action>
       <action>if true refresh:INFO</action>
       <action>if true refresh:BURNLIST</action>
       <action>if true enable:MOVE_UP</action>
       <action>if true enable:MOVE_DOWN</action>
       <action>if true disable:BUTTON_NEWDIR</action>
      </radiobutton>
      <radiobutton label=\"$(gettext 'Burn Video')\">
       <variable>BURN_VIDEO</variable>
       <input file>$WORKDIR/BURN_VIDEO</input>
       <action>if true . $APPDIR/func -calculate_needed_space; . $APPDIR/func -info_burn; echo video > $WORKDIR/BURNLIST_MODE</action>
       <action>if true refresh:INFO</action>
       <action>if true refresh:BURNLIST</action>
       <action>if true enable:MOVE_UP</action>
       <action>if true enable:MOVE_DOWN</action>
       <action>if true disable:BUTTON_NEWDIR</action>
      </radiobutton>
     </vbox>
     <text><label>\"      \"</label></text>
     <edit editable=\"false\" left_margin=\"10\">
      <variable>INFO</variable>
      <input file>$WORKDIR/info</input>
      <visible>disabled</visible>
      <width>100</width><height>70</height>
     </edit>
     <text><label>\"      \"</label></text>
     <button tooltip-text=\" $(gettext 'Go to burn options') \" height-request=\"72\" width-request=\"72\">
      $LABEL_BUTTON_BURN
      <action>. $APPDIR/func_burn_options</action>
      <action>refresh:BURNLIST</action>
     </button>
    </hbox>
   </frame>
  </hbox>
  $PIC
  <hbox space-expand=\"true\" space-fill=\"true\">
   <frame $(gettext 'Add data from here... (mouse-click-right)')>
    <hbox>
     <menubar>
      <menu>
       <menuitem stock=\"gtk-home\" label=\"$(gettext 'Home')\">
        <action>DIR=$HOME; . $APPDIR/func -browser_go_to_dir</action>
        <action>refresh:DIR</action>
        <action>refresh:CHOOSER</action>
       </menuitem>
       <menuitem stock=\"gtk-directory\" label=\"$(gettext 'Desktop')\">
        <action>DIR='$HOME/Desktop/'; . $APPDIR/func -browser_go_to_dir</action>
        <action>refresh:DIR</action>
        <action>refresh:CHOOSER</action>
       </menuitem>
       <menuitem stock=\"gtk-harddisk\" label=\"$(gettext 'Filesystem')\">
        <action>DIR='/'; . $APPDIR/func -browser_go_to_dir</action>
        <action>refresh:DIR</action>
        <action>refresh:CHOOSER</action>
       </menuitem>
       <separator></separator>
       $BOOKMARKS
       <label>..</label>
      </menu>
     </menubar>
     <entry activates-default=\"true\"><variable>DIR</variable><input>cat $WORKDIR/dir</input></entry>
     <button tooltip-text=\" $(gettext 'Search for filename') \" can-default=\"true\" has-default=\"true\" use-stock=\"true\">
      <input file stock=\"gtk-find\"></input>
      <action>. $APPDIR/func -browser_go_to_dir</action>"'
      <action>refresh:CHOOSER</action>
      <action>refresh:DIR</action>
     </button>
    </hbox>
    <tree headers_visible="false" exported_column="2" selection-mode="3" rubber-banding="true" column-visible="true|false|false">
     <label>"a|b|c"</label>
     <variable>CHOOSER</variable>
     <input icon_column="0">cat '$WORKDIR'/ls</input>
     <height>100</height><width>200</width>
     <action signal="button-release-event">ADD="'$(gettext 'Add selection')' ['$(gettext 'right click')']"; . '$APPDIR'/func -add</action>
     <action signal="button-release-event">refresh:BURNLIST</action>
     <action>. '$APPDIR'/func -browser_change_dir</action>
     <action>refresh:CHOOSER</action>
     <action>refresh:DIR</action>
    </tree>
    
    <progressbar>
     <label>" "</label>'"
     <input>"'while [ ! -f $WORKDIR/end_while ]; do if [ "`cddetect_quick -d$BURNDEV`" !=  "`cat $WORKDIR/cddetect 2> /dev/null`" ] || [ ! -s $WORKDIR/cddetect ] || [ "`cat $WORKDIR/burn`" != "`cat $WORKDIR/burn_old`" ]; then nice -n -18 $APPDIR/func -update_sizebar; fi; sleep 0.3; done'"</input>
    </progressbar>
   </frame>
   <frame $(gettext '...burn content of this list')>
    <tree rules_hint=\"true\"> ## hover-selection=\"true\"
     <label>\"$(gettext 'name')                                                               |$(gettext 'size')\"</label>
     <variable>BURNLIST</variable>
     <height>1</height><width>400</width>
     <input icon_column=\"0\">cat $WORKDIR/burn</input>
     ##<action signal=\"button-press-event\">. $APPDIR/func -burnlist_right-menu</action>
     ##<action signal=\"button-press-event\">refresh:BURNLIST</action>
     <action>. $APPDIR/func -burnlist_change_dir</action>
     <action>refresh:BURNLIST</action>
    </tree>
    <hbox>
     <button tooltip-text=\" $(gettext 'Add item to burn-list. (mouse-click-right)') \">
      <input file stock=\"gtk-add\"></input>
      <action>BUTTON=3; ADD=\"$(gettext 'Add selection') [$(gettext 'right click')]\"; . $APPDIR/func -add</action>
      <action>refresh:BURNLIST</action>
     </button>
     <button tooltip-text=\" $(gettext 'Remove item from burn list.
This will NOT delete source file.') \">
      <variable>BUTTON_REMOVE</variable>
      <input file stock=\"gtk-remove\"></input>
      <action>. $APPDIR/func -burnlist_remove</action>
      <action>refresh:BURNLIST</action>
     </button>
     <text><label>\"  \"</label></text>
     <button tooltip-text=\" $(gettext 'Create new directory on the burn disc.') \">
      <variable>BUTTON_NEWDIR</variable>
      <input file stock=\"gtk-open\"></input>
      <visible>$VISIBLE_NEWDIR</visible>
      <action>. $APPDIR/func -burnlist_create_new_dir</action>
      <action>refresh:BURNLIST</action>
     </button>
     <button tooltip-text=\" $(gettext 'Rename item on the burn disc. 
This will NOT change source file.') \">
      <variable>BUTTON_RENAME</variable>
      <input file stock=\"gtk-convert\"></input>
      <action>. $APPDIR/func -burnlist_rename</action>
      <action>refresh:BURNLIST</action>
     </button>
     <text><label>\"  \"</label></text>
     <button tooltip-text=\" $(gettext 'Move item UP') \">
      <variable>MOVE_UP</variable>
      <input file stock=\"gtk-go-up\"></input>
      <visible>$VISIBLE_MOVE_UPDOWN</visible>
      <action>DIRECTION=up; . $APPDIR/func -move_up_down; . $APPDIR/func -info_burn</action>
      <action>refresh:BURNLIST</action>
     </button>
     <button tooltip-text=\" $(gettext 'Move item DOWN') \">
      <variable>MOVE_DOWN</variable>
      <input file stock=\"gtk-go-down\"></input>
      <visible>$VISIBLE_MOVE_UPDOWN</visible>
      <action>DIRECTION=down;. $APPDIR/func -move_up_down; . $APPDIR/func -info_burn</action>
      <action>refresh:BURNLIST</action>
     </button>
    </hbox>
   </frame>
  </hbox>
  </vbox>
 <action signal=\"show\">echo 100 > $WORKDIR/splash</action>
 <action signal=\"hide\">exit:Exit</action>
</window>"

[ $SHOW_TOOLTIPS = false ] && S="`echo "$S" | sed 's%tooltip-text%tooltipXXXtext%g'`" #deactivate tooltips
echo "$S" | sed 's/##.*//' > $WORKDIR/xml_pburn #I use double hash (##) for comments. --> as #FF0000
#show gui
I=$IFS; IFS=""
for STATEMENTS in  $($GTKDIALOG -f $WORKDIR/xml_pburn 2> /dev/null); do
	eval $STATEMENTS 2> /dev/null
done
IFS=$I
case $EXIT in
	open)	   
		for I in `ps -eo pid,command | grep -Fi burn | grep -F end_while | awk '{print $1}'`; do kill -9 $I 2> /dev/null; done
		OPEN_DIALOG=true; . $APPDIR/func -open
		GTK2_RC_FILES=""
		. $APPDIR/func_gui
		;;
esac
