#!/bin/bash
###########################################################################
#                                                                         #
#                         Powersave Daemon                                #
#                                                                         #
#          Copyright (C) 2004,2005 SUSE Linux Products GmbH               #
#                                                                         #
# 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 you   #
# 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., #
# 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA                  #
#                                                                         #
###########################################################################

# first get helper functions (e.g. DEBUG, load_scheme, ...)
#. "/usr/lib/powersave/scripts/helper_functions"

# program locations:
KDIALOG_BIN=$KDE_BINDIR/kdialog
ZENITY_BIN=$GNOME_BINDIR/zenity
XMESSAGE_BIN=/usr/X11R6/bin/xmessage

# this fixes an obscure bug if you restart powersaved from an root terminal
# that has access to the X server. 
unset XAUTHORITY

get_x_user(){
    [ -n "$X_USER" -a -n "$DISP" ] && return 0
    local DUMMY
    read DUMMY X_USER DISP DUMMY < <($WTTYHX -v)
    return 0
}

get_x_users(){
    local DUMMY i
    i=0
    KDE_RUNNING[$i]=false
    while read X_USERS[$i] DISPS[$i] DUMMY; do
      su - ${X_USERS[$i]} -c "DISPLAY=${DISPS[$i]} $KDE_BINDIR/dcopfind kdesktop >/dev/null 2>&1" && \
            KDE_RUNNING[$i]=true
      let i++
    done < <($WTTYHX -a)
    return 0
}

# chosses the appropriate popup which is available
# it is one of kdialog, zenity or simply xmessage
# this should be handled by graphical clients like kpowersave. We leave
# this in here for those who want to do homemade notification, a stock
# installation will soon only use the xmessage popup as a fallback.
choose_popup(){
    local MESSAGE="$1"
    local LEVEL="$2"
    local RET="1"

    # x_helper_functions
    if $KDE_RUNNING; then
	popup_kdialog "$MESSAGE" "$LEVEL"
	RET=$?
    elif $ZENITY; then
	popup_zenity "$MESSAGE" "$LEVEL"
	RET=$?
    else
	popup_xmessage "$MESSAGE" "$LEVEL"
	RET=$?
    fi
    return $RET
}

check_x_access(){
    local RET
    [ -n "$X_USER" -a -n "$DISP" ] || return 1
    su - $X_USER -c "DISPLAY=$DISP xset q > /dev/null"
    RET=$?
    return $RET
}
    
# the popup_* functions pop up a window containing $MESSAGE, a ok or a yes and a no button.
#
# no KDE, no GNOME: just xmessage. Not too nice.
popup_xmessage(){
    local ARG RET
    local MESSSAGE="$1"
    local LEVEL="$2"
    local WAIT="&"
    case "$LEVEL" in
        QUESTION)
            ARG="-buttons Yes,No" 
            WAIT="" ;;
        ERROR|WARN|INFO)
            ARG="" ;;
        *)  ARG=""
            DEBUG "wrong LEVEL in popup_xmessage: '$LEVEL', assuming 'empty'" WARN ;;
    esac
    get_x_user
    if check_x_access; then
        su - $X_USER -c "DISPLAY=$DISP $XMESSAGE_BIN -center -fn \
            '-misc-fixed-bold-r-*-*-18-*-*-*-*-*-iso10646-1' $ARG\
            '`echo $MESSAGE|fmt -w 60 `' $WAIT"  # we re-format it to 60 chars
        RET=$?
    else
        RET=255
    fi
    [ "$RET" = "101" ] && RET=0 # Yes
    [ "$RET" = "102" ] && RET=1 # No
    return $RET
}

# you want this actually only if you are really running KDE...
popup_kdialog(){
    local ARG
    local MSG="`echo $MESSAGE`" # kdialog does word-wrapping
    local WAIT="&"
    case "$LEVEL" in
        ERROR)    ARG="--error" ;;
        WARN)     ARG="--sorry" ;;
        INFO)     ARG="--msgbox" ;;
	QUESTION) ARG="--yesno"  
	          WAIT=""
		  ;;	          
        *)        ARG="--msgbox"
                DEBUG "wrong LEVEL in popup_kdialog: '$LEVEL', assuming 'INFO'" WARN ;;
    esac
    get_x_user
    [ "$X_USER" -a -n "$DISP" ] && \
        su - $X_USER -c "DISPLAY=$DISP $KDIALOG_BIN $ARG \"$MSG\" $WAIT" 
    return $?
}


# zenity is more lightweight than kdialog, but it's not in the default install
popup_zenity(){
    local ARG="--question"
    local MSG="`echo $MESSAGE`" # zenity does word-wrapping
    local WAIT="&"
    case "$LEVEL" in
        ERROR)    ARG="--error" ;;
        WARN)     ARG="--info" ;;
        INFO)     ARG="--info" ;;
	QUESTION) ARG="--question"
	          WAIT=""
		  ;;	
        *)        ARG="--info"
                  DEBUG "wrong LEVEL in popup_zenity: '$LEVEL', assuming 'INFO'" WARN ;;
    esac
    get_x_user
    if check_x_access; then
        su - $X_USER -c "DISPLAY=$DISP $ZENITY_BIN $ARG --text=\"$MSG\" $WAIT" 
        return $?
    else
	return 255
    fi
}

