Page 1 of 1

How to improve fluxbox shutdown? solved

Posted: Tue 16 Oct 2012, 03:53
by oldyeller
Hello everyone,

I would like to improve on my fluxbox shutdown gui.

I would like to have the option for each of my chooses to have a yes to continue or cancel if they forgot something and start all over.

How would I be able to do this?
#!/bin/sh
#MODES are poweroff reboot exit
## FUNCTIONS
set -a #export all functions
fluxexit () {
echo -n "$1" > /tmp/wmexitmode.txt
KCNT=0
PSPEFD="`ps -C pup_event_frontend_d | grep 'pup_event_front' | grep -v 'defunct'`"
while [ "$PSPEFD" != "" ];do
sleep 0.5
KCNT=`expr $KCNT + 1`
[ $KCNT -gt 60 ] && break #30 secs.
PSPEFD="`ps -C pup_event_frontend_d | grep 'pup_event_front' | grep -v 'defunct'`"
done
sleep 0.2
sync
#CURRENTWM="`cat /etc/windowmanager`"
#if [ "$CURRENTWM" = "startxfce4" ];then
#CURRENTWM="xfce4-session"
#fi
#kill -9 `pidof $CURRENTWM`
kill -9 `pidof fluxbox`
}

export MAIN_DIALOG='
<window title="Shutdown" icon-name="shutdown24" resizable="false" decorated="true" height-request="0" width-request="250">
<vbox>

<hseparator></hseparator>
<text wrap="false" xalign="0.5" selectable="false">

<label>Options</label>

</text>
<hseparator></hseparator>

<hbox>
<frame>
<vbox>
<button height-request="0" width-request="125">
<label>Power-Off</label>
<input file stock="gtk-disconnect"></input>
<action>fluxexit poweroff &</action>
<action type="exit">exiting completed</action>
</button>

<button height-request="0" width-request="125">
<label>Reboot</label>
<input file stock="gtk-connect"></input>
<action>fluxexit reboot &</action>
<action type="exit">exiting completed</action>
</button>
</vbox>
</frame>
<frame>
<vbox>
<button height-request="0" width-request="125">
<label>Restart X</label>
<input file stock="gtk-refresh"></input>
<action>fluxexit restartwm &</action>
<action type="exit">exiting completed</action>
</button>

<button height-request="0" width-request="125">
<label>Log-Out</label>
<input file stock="gtk-ok"></input>
<action>fluxexit exit &</action>
<action type="exit">exiting completed</action>
</button>

</vbox>
</frame>
</hbox>

<vbox>
<hseparator></hseparator>
<button cancel></button>
<hseparator></hseparator>
</vbox>
</vbox>
</window>
'

gtkdialog --center --program=MAIN_DIALOG

Posted: Tue 16 Oct 2012, 15:05
by SFR
Hey Oldyeller
[...]yes to continue or cancel[...]
If you don't mind to use "No" instead of "Cancel" :wink: , Xdialog would the simpliest way I can think about at the moment.
Add these two lines in the beginning of the "fluxexit" function:

Code: Select all

fluxexit () {
Xdialog --title "Confirmation" --yesno "Are you sure you want to $1?" 5 40
[ "$?" -ne 0 ] && exit
$? return codes are 0=yes, 1=no, 255=window_closed

But, if you'd like to return to the main window when user presses "No", try this:

Code: Select all

set -a #export all functions
MYNAME="$0"

fluxexit () {
Xdialog --title "Confirmation" --yesno "Are you sure you want to $1?" 5 40
[ "$?" -ne 0 ] && exec "$MYNAME"
exec "$MYNAME" will simply restart the script.

Greetings!

Posted: Tue 16 Oct 2012, 17:26
by Karl Godt
MYNAME="$0"
This is common practice but i think it should be

Code: Select all

MYNAME=`readlink -e "$0"`
in case you have the script in a folder something like /usr/local/shutdown and link to it from somewhere in the PATH .
This is for the case you have several scripts in that directory and forget to cd into it .

Posted: Tue 16 Oct 2012, 20:03
by oldyeller
SFR wrote:Hey Oldyeller
[...]yes to continue or cancel[...]
If you don't mind to use "No" instead of "Cancel" :wink: , Xdialog would the simpliest way I can think about at the moment.
Add these two lines in the beginning of the "fluxexit" function:

Code: Select all

fluxexit () {
Xdialog --title "Confirmation" --yesno "Are you sure you want to $1?" 5 40
[ "$?" -ne 0 ] && exit
$? return codes are 0=yes, 1=no, 255=window_closed

But, if you'd like to return to the main window when user presses "No", try this:

Code: Select all

set -a #export all functions
MYNAME="$0"

fluxexit () {
Xdialog --title "Confirmation" --yesno "Are you sure you want to $1?" 5 40
[ "$?" -ne 0 ] && exec "$MYNAME"
exec "$MYNAME" will simply restart the script.

Greetings!
Edit=I have done both they work just fine. will see which when I like the best and use it. Thanks again

Thanks this does the job, but could you give me an explanation of the script so I can better understand what it does.

Like
$1?" 5 40
[ "$?" -ne 0 ] && exec "$MYNAME"[/code]

Posted: Tue 16 Oct 2012, 20:29
by SFR
oldyeller wrote:Thanks this does the job, but could you give me an explanation of the script so I can better understand what it does.

Like
$1?" 5 40
[ "$?" -ne 0 ] && exec "$MYNAME"[/code]
Sure!

$1 is the parameter passed from, eg. <action>fluxexit restartwm &</action> to fluxexit function (in this case $1=restartwm).

5 40 are dimensions of the window (height and width respectively).
More info: Xdialog --help

[ "$?" -ne 0 ] && exec "$MYNAME"
Simply: if the return code from Xdialog window is not equal 0 then restart the script, else - continue.

MYNAME="$0" - $0 is the variable that holds the filename of the script.

Additonaly you can also "humanize" the confirmation questions by adding something like this:

Code: Select all

set -a #export all functions
MYNAME=`readlink -e "$0"`

fluxexit () {
case "$1" in
  "poweroff" ) MESSAGE="turn off computer" ;;	
  "reboot" ) MESSAGE="reboot computer" ;;
  "restartwm" ) MESSAGE="restart window manager" ;;
  "exit" ) MESSAGE="exit to prompt";;
esac	

Xdialog --title "Confirmation" --yesno "Are you sure you want to $MESSAGE?" 5 50
[ "$?" -ne 0 ] && exec "$MYNAME"
BTW, Karl's suggestion makes that MYNAME variable will contain the full path to the actual script, not a path to a symlink (in case it's launched through a symlink, of course).

Greetings!

Posted: Tue 16 Oct 2012, 20:40
by oldyeller
Thanks,

this helps to learn when things get explained.

Now I off for to study bash


thanks again

Cheers.

Posted: Tue 16 Oct 2012, 21:24
by dejan555
Hey oldyeller, here I attach shutdown script/dialog from my fluxbox package for lupu with icons included if you want to inspect

My full fluxbox lupu package is here: fluxbox-1.1.1-i486-lupu.pet

Beside the compiled files included in it are menu generation binary and scripts/files, shutdown dialog and other menu tweaks.

Posted: Wed 17 Oct 2012, 01:33
by oldyeller
Hi dejan555,

Thanks, this is were I got some of the things I needed to start with. will look again at your work and take what I might still need.

Cheers