#!/bin/bash
#
# xsearch
#
# Version: 0.0.1 
# Author: Neal <nealbrks0 at gmail.com>
# Co Author: D-M-Wilhelm <meisssw01 at gmail.com>
##########################################################
# Copyright (C) 2011 Bill Reynolds <texstar at gmail.com>
#
#    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; either version 2 of the License, or
#    (at your option) any later version.
#
#    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.
#
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#############################################################
#
#define some variables
Encoding=UTF-8
TITLE="Xsearch"
VERSION="0.0.1"
ICON="/usr/share/pixmaps/xsearch.png"
#
# i18n - Internationalization
export TEXTDOMAIN=xsearch
export TEXTDOMAINDIR="/usr/share/locale"


FILES=$(yad --entry --title "$TITLE $VERSION" \
			--window-icon="$ICON" --image="$ICON"\
			--width=300 \
			--text $"<b>Search for files</b>.\n<b>
\nWhat file are you searching for?</b>") 
	if [ "$FILES" = "" ]; then								             # add entry
	yad --button="gtk-ok:0" --title "$TITLE $VERSION" \
				--window-icon="$ICON" --image="$ICON"\
				--width=300 \
				--text $"Aborted by User, exit now!" && exit             # close app
	else                                                                 # choose where to search
	FIND=$(yad --button="gtk-ok:0" --button="$HOME:1" --file-selection \
			--title "$TITLE $VERSION" \
			--window-icon="$ICON" \
			--width=800 --height=600 \
			--text $"<b>Select a directory.</b> \nOr Notice: Click on $HOME to search in <b>$HOME</b>!")
			
ret=$?

		if [[ $ret -eq 1 ]];then
		XDGOPEN=$(notify-send -i info -t 6000 $"$TITLE is searching for $FILES" | find "$HOME" -name "*$FILES*" | sort -f | while read programm; do echo "<item>$programm</item>"; done)
		else
		XDGOPEN=$(notify-send -i info -t 6000 $"$TITLE is searching for $FILES" | find "$FIND" -name "*$FILES*" | sort -f | while read programm; do echo "<item>$programm</item>"; done)
		fi
fi

XSEARCH='
<window window_position="1" title="'$TITLE$VERSION'" icon-name="info">
  <vbox>
   <frame> 
     <vbox  tooltip-text="'$"If no files are displayed below the list, maybe the list is to long, or no files are available!"'">
      <text use-markup="True">
        <label>"'$"Click on the file in the list below that you want to open."'"</label>
      </text>  
     </vbox>
     <vbox> 
     <tree rules_hint="true" exported_column="0">
      <width>500</width><height>200</height>
      <variable>SELECT</variable>
      <label>'$"Finding files"'</label>
      '$XDGOPEN' 
     </tree>  
     </vbox> 
    </frame>
    
    <frame>
          <hbox>
           <vbox tooltip-text="'$"Listed files will be opened with your default application."'">
            <button>
	        <input file icon="gtk-find"></input>
            <label>'$"Open"'</label>
              <action>xdg-open "$SELECT" &</action>
            </button>
           </vbox>
           
           <vbox tooltip-text="'$"Start a new search to find other files."'"> 
            <button>
	        <input file icon="gtk-find"></input>
            <label>'$"Search"'</label>
              <action>exec xsearch &</action>
              <action type="exit">XSEARCH</action>
            </button>
           </vbox> 

           <vbox tooltip-text="'$"Exit $TITLE!"'">
            <button>
            <label>'$"Close"'</label>
	          <input file stock="gtk-close"></input>
              <action type="closewindow">XSEARCH</action>
            </button>
           </vbox> 
          </hbox>
    </frame>
  </vbox>
</window>
'

export GUI="`echo "$XSEARCH" | sed -e 's/##.*//'`" 
gtkdialog -p GUI





