#!/bin/bash
#JWM Menu manager
#Copyright 2014
#Sigmund Berglund (zigbert)

export TEXTDOMAIN=jwm
export OUTPUT_CHARSET=UTF-8

[ "$1" = "-bookmarks" ] && PAGE_NR=1 || PAGE_NR=0

APPDIR="`dirname $0`"
[ "$APPDIR" = "." ] && APPDIR="`pwd`"
export APPDIR="$APPDIR"
export WORKDIR=$HOME/.jwm/tmp
[ ! -d "$WORKDIR" ] && mkdir -p "$WORKDIR"

MENHEIGHT=16
[ -f $HOME/.jwm/menuheights ] && . $HOME/.jwm/menuheights
$APPDIR/menu_build_bookmarks
ls -1 /usr/share/applications/ | sed 's/.desktop//g' > $WORKDIR/tray_applist

echo 'pixmap_path "/usr/share/pixmaps/puppy/"
style "icon-style"
{
	stock["gtk-www"] = {{ "internet.svg", *, *, *}}
}
class "GtkWidget" style "icon-style"' > $WORKDIR/gtkrc
export GTK2_RC_FILES=$WORKDIR/gtkrc:/root/.gtkrc-2.0

add (){
	case $1 in
	url)
		if [ ! "`grep -F '://' <<< "$ADD_URL" 2> /dev/null`" ]; then
			/usr/lib/gtkdialog/box_ok JWM error "$(gettext 'Not a valid internet adress.')"
			exit
		fi
		echo "$ADD_URL" >> $HOME/.jwm/bookmarks
		echo "gtk-www|$ADD_URL" >> $WORKDIR/bookmark_list
		;;
	dir)
		if [ ! -d "$ADD_DIR" ]; then
			/usr/lib/gtkdialog/box_ok JWM error "$(gettext 'Not a directory.')"
			exit
		fi
		echo "$ADD_DIR" >> $HOME/.jwm/bookmarks
		echo "gtk-directory|$ADD_DIR" >> $WORKDIR/bookmark_list
		;;
	file)
		if [ ! -f "$ADD_FILE" ]; then
			/usr/lib/gtkdialog/box_ok JWM error "$(gettext 'Not a file.')"
			exit
		fi
		echo "$ADD_FILE" >> $HOME/.jwm/bookmarks
		echo "gtk-file|$ADD_FILE" >> $WORKDIR/bookmark_list
		;;
	program)
		echo "$ADD_PROGRAM" >> $HOME/.jwm/bookmarks
		echo "gtk-execute|$ADD_PROGRAM" >> $WORKDIR/bookmark_list
		;;
	esac
}

remove (){
	if [ ! "$BOOKMARK" ]; then
		exit
	elif [ ! "`grep "$BOOKMARK" $HOME/.jwm/bookmarks`" ]; then #not defined by jwm
		. /usr/lib/gtkdialog/box_yesno "JWM $(gettext 'bookmark.')" "$(gettext 'This bookmark is grabbed from the filebrowsing tools. If you remove this bookmark, it will also be removed from the filebrowser.')" "<b>$(gettext 'Do you really want to remove this bookmark?')</b>"
		[ "$EXIT" != "yes" ] && exit 
		grep -v "$BOOKMARK" $HOME/.config/rox.sourceforge.net/ROX-Filer/Bookmarks.xml > $WORKDIR/bookmark_tmp
		mv -f $WORKDIR/bookmark_tmp $HOME/.config/rox.sourceforge.net/ROX-Filer/Bookmarks.xml
		grep -v "$BOOKMARK" $HOME/.gtk-bookmarks > $WORKDIR/bookmark_tmp
		mv -f $WORKDIR/bookmark_tmp $HOME/.gtk-bookmarks

	fi
	grep -v "$BOOKMARK" $HOME/.jwm/bookmarks > $WORKDIR/bookmarks
	mv -f $WORKDIR/bookmarks $HOME/.jwm/bookmarks
	grep -v "$BOOKMARK" $WORKDIR/bookmark_list > $WORKDIR/bookmark_list_
	mv -f $WORKDIR/bookmark_list_ $WORKDIR/bookmark_list
}

set_height (){
	# menu heights
	NEWMENHEIGHT=$1
	[ -f $HOME/.jwm/menuheights ] && . $HOME/.jwm/menuheights
	if [ "$NEWMENHEIGHT" != "$MENHEIGHT" ];then
		if [ "$NEWMENHEIGHT" = 16 ];then
			[ -f $HOME/.jwm/menuheights ] && rm $HOME/.jwm/menuheights
		else
			echo "MENHEIGHT=$NEWMENHEIGHT" > $HOME/.jwm/menuheights
		fi
		/usr/lib/gtkdialog/box_splash -close never -fontsize large -icon_width 60 -icon /usr/share/pixmaps/puppy/menu.svg -text " $(gettext 'Rebuilding menus...')" &
		XPID=$!
		fixmenus
		kill $XPID
	fi
}

export -f add remove set_height


export JWM_menu='
<window title="'$(gettext 'JWM menu manager')'" icon-name="gtk-preferences">
<vbox space-expand="true" space-fill="true">
  '"`/usr/lib/gtkdialog/xml_info fixed menu.svg 60 "<b>$(gettext 'Menu manager')</b>" "$(gettext 'You can drag files and directory from the filebrowser to the bookmark fields.')"`"'
  <notebook tab-pos="2" page="'$PAGE_NR'" labels="'$(gettext 'Main')'|'$(gettext 'Bookmarks')'" space-expand="true" space-fill="true">
    <vbox margin="10" space-expand="false" space-fill="false">
      <hbox space-expand="false" space-fill="false">
        <text space-expand="false" space-fill="false"><label>"'$(gettext "Add/remove apps from the main menu")'"</label></text>
        <button image-position="2" space-expand="false" space-fill="false">
          '"`/usr/lib/gtkdialog/xml_button-icon menu big`"'
          <action>/usr/sbin/menumanager &</action>
        </button>
      </hbox>
      <hbox space-expand="false" space-fill="false">
        <text space-expand="false" space-fill="false"><label>"'$(gettext "Activate menu by mouseclick")'"</label></text>
        <button image-position="0" space-expand="false" space-fill="false">
          '"`/usr/lib/gtkdialog/xml_button-icon mouse big`"'
          <action>'$APPDIR'/shortcuts -mouse &</action>
        </button>
      </hbox>
      <hbox space-expand="false" space-fill="false">
        <text space-expand="false" space-fill="false"><label>"'$(gettext "Menu graphical theming")'"</label></text>
        <button image-position="0" space-expand="false" space-fill="false">
          '"`/usr/lib/gtkdialog/xml_button-icon puppy_theme big`"'
          <action>'$APPDIR'/theme_switcher &</action>
        </button>
      </hbox>
      <hbox space-expand="true" space-fill="true">
        <text space-expand="false" space-fill="false"><label>"'$(gettext "Menu height (each menuitem in pixels)")'"</label></text>
        <spinbutton range-min="16" range-max="48" range-step="8" editable="false" space-expand="false" space-fill="false">
          <variable>NEWMENHEIGHT</variable>
          <default>'$MENHEIGHT'</default>
        </spinbutton> 
      </hbox>
    </vbox>
    <vbox space-expand="true" space-fill="true">
      <frame '$(gettext 'Bookmarks')'>
        <hbox space-expand="true" space-fill="true">
          <vbox space-expand="true" space-fill="true">
            <hbox space-expand="true" space-fill="true">
              '"`/usr/lib/gtkdialog/xml_pixmap internet.svg big`"'
              <vbox space-expand="true" space-fill="true">
                <text yalign="1" space-expand="true" space-fill="true"><label>"'$(gettext "Add internet adress")'"</label></text>
                <hbox space-expand="true" space-fill="true">
                  <entry space-expand="true" space-fill="true">
                    <variable>ADD_URL</variable>
                    <default>http://</default>
                  </entry>
                  <vbox homogeneous="true" space-expand="false" space-fill="false">
                    <button space-expand="false" space-fill="false">
                      '"`/usr/lib/gtkdialog/xml_button-icon add`"'
                      <action>add url</action>
                      <action>refresh:BOOKMARK</action>
                    </button>
                  </vbox>
                </hbox>
              </vbox>
            </hbox>
            <hbox space-expand="true" space-fill="true">
              '"`/usr/lib/gtkdialog/xml_pixmap file.svg big`"'
              <vbox space-expand="true" space-fill="true">
                <text yalign="1" space-expand="true" space-fill="true"><label>"'$(gettext "Add file")'"</label></text>
                <hbox space-expand="true" space-fill="true">
                  <entry fs-action="file" space-expand="true" space-fill="true">
                    <variable>ADD_FILE</variable>
                  </entry>
                  <vbox homogeneous="true" space-expand="false" space-fill="false">
                    <button space-expand="false" space-fill="false">
                      '"`/usr/lib/gtkdialog/xml_button-icon open`"'
                      <action type="fileselect">ADD_FILE</action>
                    </button>
                  </vbox>
                  <vbox homogeneous="true" space-expand="false" space-fill="false">
                    <button space-expand="false" space-fill="false">
                      '"`/usr/lib/gtkdialog/xml_button-icon add`"'
                      <action>add file</action>
                      <action>refresh:BOOKMARK</action>
                    </button>
                  </vbox>
                </hbox>
              </vbox>
            </hbox>
            <hbox space-expand="true" space-fill="true">
              '"`/usr/lib/gtkdialog/xml_pixmap directory.svg big`"'
              <vbox space-expand="true" space-fill="true">
                <text yalign="1" space-expand="true" space-fill="true"><label>"'$(gettext "Add directory")'"</label></text>
                <hbox space-expand="true" space-fill="true">
                  <entry fs-action="folder" space-expand="true" space-fill="true">
                    <variable>ADD_DIR</variable>
                  </entry>
                  <vbox homogeneous="true" space-expand="false" space-fill="false">
                    <button space-expand="false" space-fill="false">
                      '"`/usr/lib/gtkdialog/xml_button-icon open`"'
                      <action type="fileselect">ADD_DIR</action>
                    </button>
                  </vbox>
                  <vbox homogeneous="true" space-expand="false" space-fill="false">
                    <button space-expand="false" space-fill="false">
                      '"`/usr/lib/gtkdialog/xml_button-icon add`"'
                      <action>add dir</action>
                      <action>refresh:BOOKMARK</action>
                    </button>
                  </vbox>
                </hbox>
              </vbox>
            </hbox>
            <hbox space-expand="true" space-fill="true">
              '"`/usr/lib/gtkdialog/xml_pixmap execute.svg big`"'
              <vbox space-expand="true" space-fill="true">
                <text yalign="1" space-expand="true" space-fill="true"><label>"'$(gettext "Add program")'"</label></text>
                <hbox space-expand="true" space-fill="true">
                  <vbox homogeneous="true" space-expand="false" space-fill="false">
                    <comboboxtext width-request="200" space-fill="false" space-expand="false">
                      <variable>ADD_PROGRAM</variable>
                      <input file>'$WORKDIR'/tray_applist</input>
                      <action>refresh:BOOKMARK</action>
                    </comboboxtext>
                  </vbox>
                  <vbox homogeneous="true" space-expand="false" space-fill="false">
                    <button space-expand="false" space-fill="false">
                      '"`/usr/lib/gtkdialog/xml_button-icon add`"'
                      <action>add program</action>
                      <action>refresh:BOOKMARK</action>
                    </button>
                  </vbox>
                </hbox>
              </vbox>
            </hbox>
            <text space-expand="true" space-fill="true"><label>""</label></text>
          </vbox>
          <vseparator width-request="20" space-expand="false" space-fill="false"></vseparator>
          <vbox space-expand="true" space-fill="true">
            <hbox space-expand="true" space-fill="true">
              <vbox space-expand="true" space-fill="true">
                <tree headers_visible="false" selection-mode="1" space-fill="true" space-expand="true">
                  <variable>BOOKMARK</variable>
                  <label>1|2</label>
                  <input file stock-column="0">'$WORKDIR'/bookmark_list</input>
                  <action signal="button-release-event">enable:BOOKMARK_REMOVE</action>
                </tree>
              </vbox>
              <vbox space-expand="false" space-fill="false">
                <button>
                  <variable>BOOKMARK_REMOVE</variable>
                  '"`/usr/lib/gtkdialog/xml_button-icon remove`"'
                  <sensitive>false</sensitive>
                  <action>remove</action>
                  <action>refresh:BOOKMARK</action>
                </button>
              </vbox>
            </hbox>
          </vbox>
        </hbox>
      </frame>
    </vbox>
  </notebook>
  <hbox space-expand="false" space-fill="false">
    <button>
      '"`/usr/lib/gtkdialog/xml_button-icon apply`"'
      <label>'$(gettext 'Apply')'</label>
      <action>set_height $NEWMENHEIGHT</action>
      <action>'$APPDIR'/menu_build_bookmarks</action>
      <action>/usr/local/jwm_config/jwm_restart</action>
    </button>
    <button>
      '"`/usr/lib/gtkdialog/xml_button-icon ok`"'
      <label>'$(gettext 'Ok')'</label>
      <action>set_height $NEWMENHEIGHT</action>
      <action>'$APPDIR'/menu_build_bookmarks</action>
      <action>/usr/local/jwm_config/jwm_restart</action>
      <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 JWM_menu
