#!/bin/bash

# demo-standard, for Konch 0.90+
# Author: Alex Ibrado <alex@kdex.org>
# Part of the Konch distribution, http://konch.kdex.org/

NAME="Konch Standard Demo"
ICON="favorites"
TIP_STATIC="Click to show static icon"
TIP_BLINK="Double-click to blink"

ID=$1
CMD=$2
shift 2
PARM="$@"

############################################################
# Function to query Konch states: Blinking, Changed, Movie
############################################################
# Note: For Standard mode, you can also set your own state
#       variables via "set", e.g. BLINKING, CHANGED, MOVIE

function is_konch {
  # $KONCH is the DCOP ID of the instance running the script
  X=`dcop $KONCH Konch is$1`
  if [ "$X" == "true" ]; then
    echo 1
  fi
}


##########################################################
# Handle events from Konch....
##########################################################

if [ "$CMD" == "quit" ]; then
  # Perform whatever cleanups you need (within 5 seconds)
  # Better handle this one first...
  exit

elif [ "$CMD" == "init" ]; then
  cat <<END

    # This is a comment
    ; So is this...
    // And this...
    /* And even this. */

    /* 
       Or this multiple line one.

       Note that the delimiters must be the first or last
       non-whitespace characters for them to be recognized. 

       That means no comments alongside commands (for now?)
       although your script can certainly ignore extra parameters...
    */

    name $NAME
    icon $ICON
    tooltip $TIP_BLINK

    # HTML markup support built into KPassivePopup
    # Using \ as the last character combines multiple lines into one 

    popup "<i>Hello $USER!</i><br/> \
          How is this demo on <b>$HOSTNAME</b> doing?"

    menu Konsole,konsole,run konsole
    menu KCalc,kcalc,run kcalc

    submenu editmenu,Editors,package_editors
    editmenu KEdit,kedit,run kedit
    editmenu KWrite,kwrite,run kwrite
    editmenu Kate,kate,run kate
    editmenu -
    editmenu OpenOffice Writer,openofficeorg-writer,run oowriter

    menu -

    submenu demomenu,Demo Menu
    demomenu Show Kopete animation,kopete,animated
    demomenu Show standard icon,$ICON,stdicon
    demomenu Select icon...,,changeicon
    demomenu Force icon reset,,force

    /* Color menu is an exclusive menu because there is a 5th parameter to "submenu"
       "Default" is the item checked at startup
       If you don't want any checked, just use something like "1" */

    submenu colormenu,Colorize,kcoloredit,demomenu,Default

    /* The items you want to be checkable must also have extra parameter.
       You can use whatever you want, e.g. CHECKABLE, 1, true, etc. as long
       as it's not "false" or "0" */

    colormenu Default,,color default,1
    colormenu -
    colormenu Red,,color red,1
    colormenu Green,,color green,1
    colormenu Blue,,color blue,1
    colormenu Aquamarine,,color aqua,1

    /* You can't directly specify which ones are checked at startup.
       Use the "check" command in "init" instead, after creating the
       menus. See below... */

    demomenu -
    demomenu Use faster blink,,fastblink,1
    demomenu Blinking,,blink,1
    demomenu -

    /* A checkable item with an icon
       You can use single or double quotes if you want...
       Spaces between parameters are ok, too */

    demomenu 'Only show KCalc', kcalc, simplify, 1
    demomenu "Quit via DCOP", exit, 'run dcop $KONCH Konch quit'

    ## Let's turn on faster blinking by default...
    check demomenu "Use faster blink"

    # Make it phase shift every 0.5 seconds
    # Semi-persistent variable, only for Standard mode
    set BLINK_SPEED 250
END

elif [ "$CMD" == "click" ]; then
  if [ `is_konch Movie` ]; then
    # Also run the commands to use a standard icon
    CMD=stdicon
  fi

  echo blink 0
  echo tooltip $TIP_BLINK

  # We need to sync the menu item checkmark
  echo uncheck demomenu Blinking

elif [ "$CMD" == "rclick" ]; then
  echo menu show

elif [ "$CMD" == "mclick" ]; then
  echo popup "That tickles!"
  
elif [ "$CMD" == "dclick" ]; then
  echo blink $BLINK_SPEED
  echo tooltip $TIP_STATIC

  # We need to sync the menu item checkmark
  echo check demomenu Blinking
fi


##########################################################
# Now handle the script's own commands...
##########################################################

if [ "$CMD" == "stdicon" -o "$CMD" == "force" ]; then
  if [ "$CMD" == "stdicon" ]; then
    # This restores the icon to the state it was before the movie
    echo restore
    if [ `is_konch Blinking` ]; then
      echo tooltip $TIP_STATIC
    else
      echo tooltip $TIP_BLINK
    fi
    if [ ! `is_konch Changed` ]; then
      echo check colormenu "Default"
    fi
  else
    # This resets the icon totally... no blinking, colorization, etc.
    echo reset
    # We need to sync the menu item checkmarks
    echo check colormenu "Default"
    echo uncheck demomenu "Blinking"
    echo tooltip $TIP_BLINK
  fi

  # Sync the colorize options
  #  -- this unchecks the others since colormenu is exclusive
  cat <<END
    # Enable Kopete animation
    enable demomenu "Show Kopete animation"

    # Turn on the pixmap effects
    enable demomenu "Use faster blink"
    enable demomenu "Blinking"

    # Enable the whole submenu
    enable colormenu
END

elif [ "$CMD" == "animated" ]; then
  cat <<END
    setMovie newmessage
    tooltip $TIP_STATIC

    # Disable this item
    disable demomenu "Show Kopete animation"

    # Turn off the pixmap effects
    disable demomenu "Use faster blink"
    disable demomenu "Blinking"

    # Disable the whole submenu
    disable colormenu
END

elif [ "$CMD" == "blink" ]; then
  # demomenu is not exclusive, a 0/1 check indicator is sent
  if [ `is_konch Blinking` ]; then
    echo blink 0
    echo tooltip $TIP_BLINK
  else
    echo blink $BLINK_SPEED
    echo tooltip $TIP_STATIC
  fi

elif [ "$CMD" == "fastblink" ]; then
  # demomenu is not exclusive, a 0/1 check indicator is sent

  if [ "$PARM" == "1" ]; then
    # This item is checked
    echo set BLINK_SPEED 250
    # We have to set a temp variable too, since "set" will only take
    # effect in the next run
    BS=250
  else
    echo set BLINK_SPEED 500
    BS=500
  fi

  if [ `is_konch Blinking` ]; then
    echo blink $BS
  fi

elif [ "$CMD" == "color" ]; then
  # colormenu is exclusive, no check indicators are needed or sent

  # Restore to default -- we don't want to accumulate the colorize effect
  # Not "reset", because we still want to keep blinking, if ever
  echo restore

  if [ "$PARM" == "red" ]; then
    echo colorize 255 0 0 100
  elif [ "$PARM" == "green" ]; then
    echo colorize 0 255 0 100
  elif [ "$PARM" == "blue" ]; then
    echo colorize 0 0 255 100
  elif [ "$PARM" == "aqua" ]; then
    echo colorize 78 120 160 100
  fi

elif [ "$CMD" == "simplify" ]; then
  if [ "$PARM" == "1" ]; then
    # Hide favorites except for KCalc
    echo hideitem menu Konsole
    echo hideitem editmenu
  else
    # Show all favorites
    echo showitem menu Konsole
    echo showitem editmenu
  fi

elif [ "$CMD" == "changeicon" ]; then
  ICON=`kdialog --geticon toolbar actions`
  if [ "$ICON" != "" ]; then
    echo icon "$ICON"
  fi

elif [ "$CMD" == "run" ]; then
  $PARM &

fi

