You are not logged in.

#1 2015-10-26 22:37:57

damo
....moderator....
Registered: 2015-08-20
Posts: 5,418

Is there any desire for an aerosnap script, or OB Config Manager?

I posted a couple of scripts in "Scripts, Tutorials & Tips", but as yet there has been little feedback so I'll try some publicity here.

Openbox Configuration Manager

Save or Restore a group of configs with a couple of clicks.
There is a preview dialog to remind you what a saved collection looks like - a screenshot is taken of the desktop, with the menu showing, and an lxappearance window, to show the OB and GTK themes in use.
It works with single or dual monitor setups, but I haven't been able to test with different-sized monitors (except for changing the resolution on one of them).

winsnap - an aerosnapping script for Openbox

# Based on ideas in a script found at
# http://icculus.org/pipermail/openbox/2013-January/007772.html
#
# Written by damo <damo@bunsenlabs.org> October 2015
#
# The script snaps a window to left or right halves of screen, or top and bottom,
# using X window properties for getting and storing values.
#
# Left, right, top, or bottom screen margins can be specified (negative values allowed);
# Works with dual monitors - windows will snap to edges of monitor they are on;
# Honours user-defined Openbox left and right screen margins;
# Works with decorated and undecorated windows, and windows with no borders;
# Doesn't cover panels at top,bottom, desktop left or desktop right.
#
# REQUIRES: xdotool, wmctrl
#
########################################################################
#
# TODO: Account for _NET_WORKAREA with top/bottom snapping if panel only
# appears on the other monitor.
#
########################################################################
########################################################################
#
# TODO: Account for _NET_WORKAREA with top/bottom snapping if panel only
# appears on the other monitor.
#
########################################################################

Last edited by damo (2015-10-27 20:52:16)


Be Excellent to Each Other...

FORUM RULES and posting guidelines «» Help page for forum post formatting
Artwork on DeviantArt  «» BunsenLabs on DeviantArt

Offline

#2 2015-10-26 22:47:42

altman
Member
From: Canada
Registered: 2015-10-24
Posts: 130

Re: Is there any desire for an aerosnap script, or OB Config Manager?

OBConf would be a good addition to BL on my end .


HP Pavilion dv7 Notebook/Intel Core i7-2670QM/ BunsenLab Helium
Dell Latitude XFR D630 /Intel Core2 Duo T7500/ BunsenLab Helium
Dell Inspiron 1720/Intel Core2 Duo T5450/ ArchLabs 2018.05
My craptop trio on Linux installs only.

Offline

#3 2015-10-26 22:51:43

damo
....moderator....
Registered: 2015-08-20
Posts: 5,418

Re: Is there any desire for an aerosnap script, or OB Config Manager?

altman wrote:

OBConf would be a good addition to BL on my end .

`obconf` is already in the iso. Or did you mean the script I posted (I call it BLOB!)


Be Excellent to Each Other...

FORUM RULES and posting guidelines «» Help page for forum post formatting
Artwork on DeviantArt  «» BunsenLabs on DeviantArt

Offline

#4 2015-10-26 22:55:40

altman
Member
From: Canada
Registered: 2015-10-24
Posts: 130

Re: Is there any desire for an aerosnap script, or OB Config Manager?

Ah , sorry , that s it ,I m mixed up here mate ! lol

Yep , (blob )

Last edited by altman (2015-10-26 22:56:18)


HP Pavilion dv7 Notebook/Intel Core i7-2670QM/ BunsenLab Helium
Dell Latitude XFR D630 /Intel Core2 Duo T7500/ BunsenLab Helium
Dell Inspiron 1720/Intel Core2 Duo T5450/ ArchLabs 2018.05
My craptop trio on Linux installs only.

Offline

#5 2015-10-26 23:01:56

gako
Member
Registered: 2015-10-02
Posts: 241

Re: Is there any desire for an aerosnap script, or OB Config Manager?

I think BLOB is great. I'm not using Openbox recently, but I always end up coming back to it.
BLOB and winsnap are already in my arsenal, and will have fun with them when I come home to OB.

(B)unsen (L)abs (O)pen (B)ox I suspect the name derived from - nifty.

Offline

#6 2015-10-27 00:17:28

vasa1
Member
Registered: 2015-09-29
Posts: 178

Re: Is there any desire for an aerosnap script, or OB Config Manager?

damo wrote:

I posted a couple of scripts in "Scripts, Tutorials & Tips", but as yet there has been little feedback so I'll try some publicity here.
...

I saw the winsnap post a few days ago but the copy.com link didn't work for me. maybe I need a copy.com account for it to work? I've just got it from your github page and the "restore" function is working just fine on Lubuntu 14.04.3 which has older versions of xdotool and wmctrl. This is on a laptop so just the one screen.

    <keybind key="C-W-Left">
      <action name="Execute">
        <command>winsnap --left</command>
      </action>
    </keybind>
    <keybind key="C-W-Right">
      <action name="Execute">
        <command>winsnap --right</command>
      </action>
    </keybind>
    <keybind key="C-W-Up">
      <action name="Execute">
        <command>winsnap --top</command>
      </action>
    </keybind>
    <keybind key="C-W-Down">
      <action name="Execute">
        <command>winsnap --bottom</command>
      </action>
    </keybind>

Using the Openbox (3.5.2) session of Lubuntu 14.04 LTS but very interested in BL :)

Offline

#7 2015-10-27 04:17:13

nore
>2⁹
From: squirrels' nest
Registered: 2015-09-29
Posts: 483

Re: Is there any desire for an aerosnap script, or OB Config Manager?

damo wrote:

I posted a couple of scripts in "Scripts, Tutorials & Tips", but as yet there has been little feedback so I'll try some publicity here.

You did a good job to start with, that's why there was so little feedback. I suggest these should be added in default setup in final Hydrogen release. They would make a lot of users happy and wouldn't hurt those who don't care about them.

Offline

#8 2015-10-27 08:23:27

ohnonot
...again
Registered: 2015-09-29
Posts: 4,227
Website

Re: Is there any desire for an aerosnap script, or OB Config Manager?

will both scripts work with any openbox-based setup?
esp. "blob" suggests that it only works with bunsenlabs...

about hosting, i haven't tried the copy.com link, but i know from previous experiences that some of these "free" cloud drives won't play with NoScript, even when I temporarily allow all requests. (*)
Isn't it enough to just point to the zip on github?
if not, i'm willing to host files on my kitchenserver (plus a blog entry if so desired).

(*) i can now confirm that this is exactly what happens with copy.com.

Last edited by ohnonot (2015-10-27 08:34:41)

Offline

#9 2015-10-27 09:10:31

damo
....moderator....
Registered: 2015-08-20
Posts: 5,418

Re: Is there any desire for an aerosnap script, or OB Config Manager?

^ It was specifically written for BL - ie a default OB + OB/gtk themes + tint2 + conky + nitrogen (or feh), so YMMV with another setup.

The copy.com link is temporary - I'll make a github release for winsnap later today.


Be Excellent to Each Other...

FORUM RULES and posting guidelines «» Help page for forum post formatting
Artwork on DeviantArt  «» BunsenLabs on DeviantArt

Offline

#10 2015-10-27 09:14:27

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 6,184
Website

Re: Is there any desire for an aerosnap script, or OB Config Manager?

@ohnonot I agree of course with your general point, but just for the record I was able to download that file from copy.com without having to disable noscript.


John

...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), idle Twitterings and GitStuff )

Offline

#11 2015-10-27 09:40:45

Snap
Member
Registered: 2015-10-02
Posts: 465

Re: Is there any desire for an aerosnap script, or OB Config Manager?

Great job, damo.

Offline

#12 2015-10-27 17:11:52

ohnonot
...again
Registered: 2015-09-29
Posts: 4,227
Website

Re: Is there any desire for an aerosnap script, or OB Config Manager?

johnraff wrote:

@ohnonot I agree of course with your general point, but just for the record I was able to download that file from copy.com without having to disable noscript.

you don't get this
abe-deny.png
???

Offline

#13 2015-10-27 17:33:39

nobody0
Disabled account
Registered: 2015-09-29
Posts: 664

Re: Is there any desire for an aerosnap script, or OB Config Manager?

Damo's winsnap

#!/bin/bash
#
# Based on ideas in a script found at
# http://icculus.org/pipermail/openbox/2013-January/007772.html
#
# Written by damo <damo@bunsenlabs.org> October 2015
#
# The script snaps a window to left or right sides of screen, using X window
# properties for getting and storing values.
#
# Left and/or right screen margins can be specified;
# Works with dual monitors - windows will snap to edges of monitor they are on;
# Honours user-defined Openbox left and right screen margins;
# Works with decorated and undecorated windows, and windows with no borders;
# Doesn't cover panels at top,bottom, desktop left or desktop right.
#
# REQUIRES: xdotool, wmctrl
#
########################################################################
#
# TODO:
# Enable top/bottom splitting
########################################################################

USAGE=$(echo -e "\vUSAGE:\twinsnap [--help|--left|--right] <margin>"
        echo -e "\v\t--help \t\tUsage"
        echo -e "\t--left \t\tsnap to left screen edge"
        echo -e "\t--right \tsnap to right screen edge"
        echo
        echo -e "\tIf no margin is specified, the left and right values set\n\tfor Openbox in rc.xml are used."
        echo
        echo -e "\tThe active window will snap to the edge of the screen on\n\twhich it placed"
        echo
        echo -e "\tOriginal window position and dimensions are restored if\n\tthe --left or --right command is repeated,"
)

####  FUNCTIONS  ####################################################

function set_prop_int() {    # Add var values to X window properties
  PROPNAME="$1"
  VAL="$2"
  xprop -id "$WINDOW" -f "$PROPNAME" 32i -set "$PROPNAME" "$VAL"
}

function set_prop_str() {
    PROPNAME="$1"
    VAL="$2"
    xprop -id "$WINDOW" -f "$PROPNAME" 8s -set "$PROPNAME" "$VAL"
}

function get_prop() {    # Retrieve var values from X window properties
  PROPNAME="$1"
  VARNAME="$2"
  eval "$VARNAME"=$(xprop -id $WINDOW $PROPNAME | awk '{print $3}')
}

function count_monitors() { #test for more than 2 monitors connected.
    MON=$(xrandr -q | grep -c " connected")
    case $MON in
        1 | 2 ) MONITORS=$MON
                ;;
        3 | * ) echo "Script cannot deal with more than 2 monitors yet" >&2
                exit
                ;;
    esac
}

function get_screen_dimensions() {   # get net workarea, if panels are present
    # X pos, Y pos, usable width, usable height
    vals=$(echo $(xprop -root | grep _NET_WORKAREA) | awk '{gsub(",","");print $3,$4,$5,$6}')
    read valX valY valW valH <<< "$vals"

    desktopW=$(xrandr -q | awk '/Screen/ {print $8}')  # total desktop width

    # Get monitors geometry and position ( w x h x offset_x offset_y )
    MONS=$(echo $(xrandr -q | awk '/ connected/ {if ($3=="primary") print $1,$4; else print $1,$3}'))
    read monA monAgeom monB monBgeom <<< "$MONS"

    monApos=$(echo $monAgeom | awk -F "+" '{print $2}') # get offset_x
    monBpos=$(echo $monBgeom | awk -F "+" '{print $2}')

    if (( $monApos -lt $monBpos )); then
        screenW1=${monAgeom%'x'*}   # width of left screen
        screenW2=${monBgeom%'x'*}   # width of right screen
    else
        screenW2=${monAgeom%'x'*}
        screenW1=${monBgeom%'x'*}
    fi

    # X position of active window:
    WINPOS=$(xwininfo -id $WINDOW | grep "Absolute upper-left X")

    if (( ${WINPOS##*' '} -gt $screenW1 ));then # window is on R monitor
        X_zero=$(( $desktopW - $screenW2 ))
        panelR=$(( $desktopW - $valW - $valX ))
        screenW=$(( $desktopW - $screenW1 - $panelR ))
    else
        X_zero=$valX                            # window is on L monitor
        screenW=$(( $screenW1 - $valX ))
    fi
}

get_WM_FRAME() {         # get borders set by WM
    # WM sets window frame and border sizes
    # Titlebar height depends on fontsize of Active titlebar
    winEXTENTS=$(xprop -id $WINDOW | awk '/_NET_FRAME_EXTENTS/ {gsub(",","");print $3,$4,$5,$6}')
    read BORDER_L BORDER_R BORDER_T BORDER_B <<< "$winEXTENTS"

    Xoffset=$(( $BORDER_L + $BORDER_R ))    # Need corrections for wmctrl
    Yoffset=$(( $BORDER_T + $BORDER_B ))
}

function get_OB_margins() {
    RC="$HOME/.config/openbox/rc.xml"
    if [[ -f "$RC" ]]&>/dev/null;then
        TAG="margins"
        RCXML=$(sed -n "/<$TAG>/,/<\/$TAG>/p" "$RC")
        OB_LEFT=$(grep -oPm1 "(?<=<left>)[^<]+" <<< "$RCXML")
        OB_RIGHT=$(grep -oPm1 "(?<=<right>)[^<]+" <<< "$RCXML")
    else
        echo "$RC not found"
        exit 1
    fi
}

function store_geometry() {  # store values in X window properties
    # Store number of monitors
    set_prop_int "_MONITORS" "$MONITORS"

    eval $(xdotool getactivewindow getwindowgeometry --shell)
    # Set initial geometry and position
    set_prop_int "_INITIAL_DIMENSION_X" "$X"
    set_prop_int "_INITIAL_DIMENSION_Y" "$Y"
    set_prop_int "_INITIAL_DIMENSION_WIDTH" "$WIDTH"
    set_prop_int "_INITIAL_DIMENSION_HEIGHT" "$HEIGHT"

    get_WM_FRAME  # Get frame and border sizes
    set_prop_int "_OB_BORDER_L" "$BORDER_L"
    set_prop_int "_OB_BORDER_R" "$BORDER_R"
    set_prop_int "_OB_BORDER_T" "$BORDER_T"
    set_prop_int "_OB_BORDER_B" "$BORDER_B"
    set_prop_int "_OFFSET_X" "$Xoffset"

    # Use different corrections if window is decorated/undecorated
    if xprop -id $WINDOW | grep -q _OB_WM_STATE_UNDECORATED ;then
        OFFSET_Y="$Yoffset"
    else
        OFFSET_Y=$(( $BORDER_T * 2 ))
    fi
    set_prop_int "_OFFSET_Y" "$OFFSET_Y"

    get_OB_margins
    set_prop_int "_OB_MARGIN_L" "$OB_LEFT"
    set_prop_int "_OB_MARGIN_R" "$OB_RIGHT"
}

function load_stored_geometry() {
    get_prop "_MONITORS" "num_monitors"
    get_prop "_INITIAL_DIMENSION_X" "initial_x"
    get_prop "_INITIAL_DIMENSION_Y" "initial_y"
    get_prop "_INITIAL_DIMENSION_WIDTH" "initial_width"
    get_prop "_INITIAL_DIMENSION_HEIGHT" "initial_height"
    get_prop "_OFFSET_X" "adjust_X"
    get_prop "_OFFSET_Y" "adjust_Y"
    get_prop "_OB_BORDER_L" "OB_border_left"
    get_prop "_OB_BORDER_R" "OB_border_right"
    get_prop "_OB_BORDER_T" "OB_border_top"
    get_prop "_OB_BORDER_B" "OB_border_bottom"
    get_prop "_OB_MARGIN_L" "OB_margin_left"
    get_prop "_OB_MARGIN_R" "OB_margin_right"
}

restore_dimension_geometry() {
    Xpos=$(( initial_x - adjust_X ))    # Correct for frame and border values
    Ypos=$(( initial_y - adjust_Y ))

    wmctrl -r :ACTIVE: -b remove,maximized_vert && \
    wmctrl -r :ACTIVE: -e 0,"$Xpos","$Ypos","$initial_width","$initial_height"

    xprop -id $WINDOW -remove _MONITORS
    xprop -id $WINDOW -remove _SNAPPED
    xprop -id $WINDOW -remove _INITIAL_DIMENSION_X  # Clear X window properties
    xprop -id $WINDOW -remove _INITIAL_DIMENSION_Y
    xprop -id $WINDOW -remove _INITIAL_DIMENSION_WIDTH
    xprop -id $WINDOW -remove _INITIAL_DIMENSION_HEIGHT
    xprop -id $WINDOW -remove _OFFSET_X
    xprop -id $WINDOW -remove _OFFSET_Y
    xprop -id $WINDOW -remove _OB_BORDER_L
    xprop -id $WINDOW -remove _OB_BORDER_R
    xprop -id $WINDOW -remove _OB_BORDER_T
    xprop -id $WINDOW -remove _OB_BORDER_B
    xprop -id $WINDOW -remove _OB_MARGIN_L
    xprop -id $WINDOW -remove _OB_MARGIN_R
}

function snap() {
    case "$1" in
        "--left"    )   snap_left $MARGIN
                        ;;
        "--right"   )   snap_right $MARGIN
                        ;;
        #"--top"     )   snap_top "$MARGIN"
                        #;;
        #"--bottom"  )   snap_bottom "$MARGIN"
                        #;;
    esac
}

function snap_left() {
    if (( $1 != 0 ));then
        if (( $1 <= $OB_border_left ));then
            XPOS=$(( $OB_border_left + $X_zero ))    # don't need OB margin
        else
            XPOS=$(( $1 + $OB_border_left + $X_zero ))
        fi
    else
        XPOS=$(( $OB_margin_left + $X_zero ))  # add OB margin
    fi

    WIN_WIDTH_L=$((( $screenW / 2 ) - $XPOS - $adjust_X + $X_zero ))
    # Move window
    wmctrl -r :ACTIVE: -b add,maximized_vert && \
    wmctrl -r :ACTIVE: -b remove,maximized_horz && \
    wmctrl -r :ACTIVE: -e 0,"$XPOS",0,"$WIN_WIDTH_L",-1
}

function snap_right() {
    if (( $1 != 0 ));then
        if (( $1 <= $OB_border_right ));then
            MARGIN_R="$OB_border_right"    # don't need OB margin
        else
            MARGIN_R=$(( $1 + $OB_border_right ))
        fi
    else
        MARGIN_R="$OB_margin_right"  # add OB margin to right edge
    fi

    XPOS=$((( $screenW / 2 ) + $X_zero ))
    # Move window
    WIN_WIDTH_R=$((( $screenW / 2 ) - $MARGIN_R - $adjust_X ))
    wmctrl -r :ACTIVE: -b add,maximized_vert && \
    wmctrl -r :ACTIVE: -b remove,maximized_horz && \
    wmctrl -r :ACTIVE: -e 0,"$XPOS",0,"$WIN_WIDTH_R",-1
}

####    END FUNCTIONS   ################################################

if [[ $1 = "--help" ]] || [[ ! $@ ]];then
    echo "$USAGE"
    echo
    exit
fi
if [[ $2 ]];then
    MARGIN=$2
else
    MARGIN=0
fi
WINDOW=$(xdotool getactivewindow)

#load_stored_geometry
get_prop "_SNAPPED" "SNAP"      #"Flag" for left/right snapped
                                #      1: Window is snapped to LEFT
                                #      2: Window is snapped to RIGHT
# xprop returns 'such' or 'found.' upon error
case $SNAP in
    "such" | "found." )
                count_monitors
                store_geometry
                get_screen_dimensions
                get_prop "_OFFSET_X" "adjust_X"
                get_prop "_OFFSET_Y" "adjust_Y"
                get_prop "_OB_BORDER_L" "OB_border_left"
                get_prop "_OB_BORDER_R" "OB_border_right"
                get_prop "_OB_BORDER_T" "OB_border_top"
                get_prop "_OB_BORDER_B" "OB_border_bottom"
                get_prop "_OB_MARGIN_L" "OB_margin_left"
                get_prop "_OB_MARGIN_R" "OB_margin_right"

                case "$1" in
                    "--left"    )   snap_left $MARGIN
                                    ;;
                    "--right"   )   snap_right $MARGIN
                                    ;;
                    #"--top"     )   snap_top "$MARGIN"
                                    #;;
                    #"--bottom"  )   snap_bottom "$MARGIN"
                                    #;;
                esac
                set_prop_str "_SNAPPED" "$1"
                ;;

    #"--left" | "--right" | "--top" | "--bottom" )
    "--left" | "--right" )
                load_stored_geometry
                get_screen_dimensions

                if [[ $1 = "$SNAP" ]];then
                    restore_dimension_geometry
                else
                    snap "$1"
                    set_prop_str "_SNAPPED" "$1"
                fi
                ;;
esac

Offline

#14 2015-10-27 17:35:26

nobody0
Disabled account
Registered: 2015-09-29
Posts: 664

Re: Is there any desire for an aerosnap script, or OB Config Manager?

Damo's BLOB

#!/bin/bash
#
# blobthemes: script to save or restore openbox gui configurations
#
# (Requires yad, wmctrl, xdotool, xrandr, nitrogen|feh)
#
# Written for BunsenLabs by damo <damo@bunsenlabs.org> October 2015
#
# Save options are for  Conky(s)
#                       Tint2(s)
#                       Openbox theme
#                       GTK theme
#                       Background (uses Nitrogen or feh, depending which
#                                   has the newer saved bg config file)
#                       Alt Menu (dmenu)
#                       Lightdm login gtk greeter
#
# Paths are assumed to be BunsenLabs defaults
#
# WARNING: It is assumed that the conkys are not being run via a script, so
# they will appear on all desktops. If you usually use a script, then you
# may have to make your own arrangements! Conkys in non-default directories
# can be saved, but will not be seen when running bl-conkyzen.
#
# A Screenshot is saved (Windows are hidden briefly so the image
#                        is the bare desktop, with any Tint2s
#                        or Conkys which are running, and a representive
#                        menu open)
#
# A time-stamped backup of rc.xml is saved "just in case".
#
# The dialog icon is "Bunsenlabs-flame-256.svg": put it in /usr/share/pixmaps.
#
# xdotool is used to move the mouse pointer, and assumes the root-menu is shown.
# with "Super + Space" keys, and the right mouse button is set for r-click.
# If you have set these to something else, then edit the "MENUMODIFIER" and
# MOUSECMD variables. Set the variable "KEYDOWN" to where "Preferences" is
# in the root-menu (or anything else you might want displayed).
#
########################################################################
#
# KNOWN ISSUES:
# Virtualbox does not allow the guest to move the host mouse cursor, using xdotool,
# so the screenshot may produce an incorrect theme displayed and/or poor positioning.
# A workaround is to move the cursor to some free space, and use the "Enter" key in the
# "Configurations to be saved:" dialog.
#
# TODO:
# Deal with per-desktop conky situations;
#
#### VARIABLES #########################################################
CONFIGPATH="$HOME/.config/blob"
SETTINGS=""
TEMPFILE="$HOME/blob.tmp"
OBPATH="$HOME/.config/openbox"
CURSESSION="$CONFIGPATH/current"
CONFIGDIR=""
LISTMSG=""
CZEN=0
TZEN=0
NUMDIRS=0
CONKYCMD=""
### CONFIGS VARIABLES
FEHFILE="$HOME/.fehbg"
NITRODIR="$HOME/.config/nitrogen"
NITROFILE="$NITRODIR/bg-saved.cfg"
CONKYPATH="$HOME/.config/conky"
CONKYSESSION="$CONKYPATH/conky-sessionfile"
TINTSESSION="$HOME/.config/tint2/tint2-sessionfile"
GTK2=".gtkrc-2.0"
GTK2MINE=".gtkrc-2.0.mine"
GTK3="$HOME/.config/gtk-3.0"
GTK3x="$HOME/.config/xfce4/xfconf/xfce-perchannel-xml/xfce4-notifyd.xml"
LDMDIR="/etc/lightdm"
LDM="lightdm-gtk-greeter.conf"
DMENUDIR="$HOME/.config/dmenu"
DMENU="dmenu-bind.sh"
PICKOBCFGS="OB menu.xml rc.xml autostart"
PICKOB="OB theme"
PICKGTK="GTK theme"
PICKCONKY="Conky"
PICKTINT="Tint2"
PICKBG="Background Wallpaper"
PICKDMENU="dmenu (Alt menu)"
PICKLDM="Lightdm Login theme"
PICKSCROT="Save screenshot"
### END CONFIGS VARIABLES

### DIALOG VARIABLES
DIALOG="yad --center --undecorated --borders=20 --window-icon=/usr/share/pixmaps/Bunsenlabs-flame-256.svg"
DIALOGDEC="yad --center  --borders=20 --window-icon=/usr/share/pixmaps/Bunsenlabs-flame-256.svg"
TITLE="BLOB Config Manager"
IMG="--image="
IMGPATH="/usr/share/pixmaps/Bunsenlabs-flame-256.svg"
T="--text="
OK="--button=OK:0"
CANCEL="--button=gtk-cancel:1"
EXIT="--button=gtk-quit:1"
CLOSE="--button=gtk-close:1"
TXTINTRO="<big><b>BunsenLabs\n\nOpenbox Configuration Manager</b></big>\n\n\n \
<b>Save or Restore settings for:</b>\n\n \
\t\tOpenbox theme\n \
\t\tGTK theme\n \
\t\tConkys\n \
\t\tTint2\n \
\t\tDesktop Background\n \
\t\tAlt Menu (dmenu)\n \
\t\tLightDM theme\n \
\t\t+ Screenshot\n\n"
### END DIALOG VARIABLES

### USER OB ACTIONS
MENUMODIFIER="super+space"  # keybind for show root-menu
MOUSECMD="click 3"          # mouse r-click
KEYDOWN=13           # adjust this for where "Preferences" is in the menu
#### END VARIABLES #####################################################

### FUNCTIONS ##########################################################

function Intro(){
    getSet
    #  If blob/ is empty, then don't display "Save Settings" option in Intro dialog
    if [[ $NUMDIRS -gt 0 ]];then
        INTRO=$($DIALOGDEC $IMG$IMGPATH "$T$TXTINTRO" \
        --title="$TITLE" \
        --button="Save Settings:0" --button="View Saved:2" $CLOSE \
        --borders=30 \
        --width=520 --height=500 \
        2>/dev/null )
    else
        INTRO=$($DIALOGDEC $IMG$IMGPATH \
        --title="$TITLE" \
        "$T$TXTINTRO" \
        --button="Save Settings:0" --button="gtk-close:1" \
        --borders=30 \
        --width=520 --height=500 \
        2>/dev/null )
    fi
    RET=$?
    case $RET in
        0)  setName
            saveSettings
            getScrot
            if [[ -d $CONFIGDIR-BKP ]];then
                rm -rf "$CONFIGDIR-BKP"
                echo -e "\n  Temporary $CONFIGDIR deleted\n"
            fi
            if [[ $? ]];then
                restoreSettings
            fi
            ;;
        1)  echo "CANCELLED..."
            exit 0
            ;;
        2)  getSet
            restoreSettings
            ;;
        *)  echo "RETURN ERROR" 2>&1
            ;;
    esac
}

function getSet(){      # get name of currently set BLOB config
    NUMDIRS=0
    NUMDIRS=$(ls -l "$CONFIGPATH" | grep -c ^d) # check if any dirs present
    if (( $NUMDIRS == 0 ));then
        if [[ -f $CURSESSION ]];then
            rm $CURSESSION
        fi
    elif [[ $NUMDIRS -gt 0 ]] && [[ -f $CURSESSION ]];then
        SET=$(cat $CURSESSION)
    fi
}

function countMonitors(){ #test for more than 2 monitors connected.
    MON=$(xrandr -q | grep -c " connected")
    PRIMARY=$(xrandr -q | awk '/ connected/ {if ($3=="primary") print $4}')
    PSIZE=${PRIMARY%%x*}    # Primary monitor width
    case $MON in
        1|2|3)  getScreenDims $MON $PSIZE
                ;;
        * )     W=600
                H=600
                echo -e "\n  Script cannot deal with these monitors.\
                \n  Setting dialog width height = 600 x 600" 2>&1
    esac
}

function getScreenDims(){   # Set width and height for View dialog
    # param $1 = num screens; param $2 = primary screen width
    desktopW=$(xrandr -q | awk '/Screen/ {print $8}')  # total desktop width
    desktopH=$(xrandr -q | awk '/Screen/ {print $10}') # desktop height
    desktopH=${desktopH%%,*}    # remove trailing comma
    if (( $1 == 1 ));then
        W=$(( $desktopW / 3 ))  # 1/3 width
    else
        W=$(( $2 / 3 ))  # 1/3 width primary monitor
    fi
    H=$(( ($desktopH / 3)*2 ))  # 2/3 height
}

function setName(){  # set name of collection, make dir and session settings file
    MSG="  Configurations will be saved to a new directory in  \
    \n  $CONFIGPATH\n\n  Enter name of new collection...  "
    MSG2="  No file specified for new saved session.\n\n  Try again?"
    LOOP=1
    DEL=0
    while [[ $LOOP ]];do  # loop dialog if nothing is selected
        ANS=$($DIALOG --entry \
            $CANCEL --button="OK:0" \
            "$T$MSG" \
            2>/dev/null )
        if (( $? == 1 )) ;then # Cancel was selected
            Intro
        elif [[ ! $ANS ]] ;then     # entry was empty
            $DIALOG --image="dialog-question" \
            "$CANCEL" "$OK" \
            "$T$MSG2" \
            2>/dev/null
            RET=$?
            echo -e "\n  No name entered for new collection!" 2>&1
            if (( $RET == 0 ));then
                continue
            else
                Intro
            fi
        else
            ANS=$(echo $ANS | sed -e 's/ /_/g')    # replace any spaces in dir name
            CONFIGDIR="$CONFIGPATH/$ANS"
            if [[ -d $CONFIGDIR ]];then
                echo -e "\n  $CONFIGDIR already exists"

                $DIALOG --form --image="dialog-question" \
                --text="Overwrite existing saved session?" \
                "$CANCEL" "$OK" \
                --width=300 \
                2>/dev/null

                if (( $? == 0 ));then
                    mv {"$CONFIGDIR","$CONFIGDIR-BKP"} && mkdir -p "$CONFIGDIR"
                    DEL=1
                    SETTINGS="$CONFIGDIR/settings.cfg"
                    > "$SETTINGS"
                    LISTMSG="$CONFIGDIR/LISTMSG.txt"
                    > "$LISTMSG"
                    break
                else
                    continue
                fi
            fi
            mkdir -p "$CONFIGDIR"
            SETTINGS="$CONFIGDIR/settings.cfg"
            LISTMSG="$CONFIGDIR/LISTMSG.txt"
            > "$SETTINGS"
            > "$LISTMSG"
            break
        fi
    done
    setText
}

function setText(){
    TIME=$(date)
    if (( $DEL == 1 ));then
        TXT="  (Existing session overwritten)"
    else
        TXT=""
    fi
    echo -e "\n  Configuration will be saved as: $ANS$TXT"
    if [[ $1 = repeat ]];then
        > "$LISTMSG"    # reset LISTNSG.txt
    fi
    # Store list in listfile
    echo -e "<b>Configuration name: $ANS</b>\t$TXT\nSaved at $TIME\n" >> "$LISTMSG"
}

function getBg(){    # find if feh or nitrogen was used. Save config file(s)
    if [[ -e $NITROFILE ]] && [[ -e $FEHFILE ]];then  # see which was last used to set background
        if [[ $NITROFILE -nt $FEHFILE ]];then # use most recent method
            BGSET="NITROGEN"
        else
            BGSET="FEH"
        fi
    elif [[ -e $FEHFILE ]] && [[ ! -e $NITROFILE ]];then  # use feh for background
        BGSET="FEH" 
    elif [[ -e $NITROFILE ]] &&  [[ ! -e $FEHFILE ]];then    # use nitrogen for background
        BGSET="NITROGEN"
    else
        echo -e "\n  No background-setting application found" 2>&1
        BGSET="None"
    fi
    echo -e "\n  Background to be set with $BGSET"
    case "$BGSET" in
        FEH     )   echo "[BACKGROUND] $BGSET" >> "$SETTINGS"
                    cp "$FEHFILE" "$CONFIGDIR"
                    echo -e "\n  $FEHFILE copied"
                    bgFeh
                    ;;
        NITROGEN)   echo "[BACKGROUND] $BGSET" >> "$SETTINGS"
                    cp "$NITRODIR/nitrogen.cfg" "$CONFIGDIR"
                    cp "$NITROFILE" "$CONFIGDIR"
                    echo -e "\n  Saved Nitrogen file: $NITROFILE"
                    getNitrogen
                    ;;
        None    )   echo -e "\n  No background-setting application found" 2>&1
                    echo "[BACKGROUND] $BGSET" >> "$SETTINGS"
                    ;;
        *       )   echo -e "\n  ERROR: No background config found" 2>&1
                    exit 1
                    ;;
    esac
}

function bgFeh(){    # get Feh saved backgrounds
    FFILE="$CONFIGDIR/.fehbg"
    if [[ -f $FFILE ]];then
        BG=""
        # get fields between single quotes
        for F in $(grep -o "'.*'" $FFILE | sed "s/'//g" 2>/dev/null);do
            BG="$BG$F;\n"
        done
        echo -e "\n  Backgrounds set with Feh, using:\n\
        \t$FEHFILE\n\t$BG"
        echo "Background set with feh:\n\t$BG$F" >> "$LISTMSG"
    else
        echo "\n  $FFILE not found" 2>&1
    fi
}

function getNitrogen(){  # get Nitrogen saved backgrounds
    NFILE="$CONFIGDIR/bg-saved.cfg"
    if [[ -f $NFILE ]];then
        BG=""
        while read line;do
            BGLINE=$(echo $line | awk -F"file=" '{print $2}')
            if [[ $BGLINE ]];then
                BG="$BG$BGLINE;\n"
            fi
        done < "$NFILE"
        echo -e "\n  Backgrounds set with Nitrogen, using:\t$NITROFILE"
        echo -e "$BG"
        echo "Background set with nitrogen:\n$BG" >> "$LISTMSG"
    else
        echo "\n  $NFILE not found" 2>&1
    fi
}

function getConky(){
    CONKYCMD=""
    LISTMSGTXT=""
    TXT=""
    if [[ $(pidof conky) ]];then    # get full command of running conkys
        > "$TEMPFILE"
        pgrep -a conky | while read pid CMD;do
            echo "$CMD;" >> "$TEMPFILE"
        done
        TXT="Conkys:\n\n"
        DEF="Default"
        MSG="Running Conkys:"
        while read line;do
            LINE=$(echo "$line" | sed 's/;//')
            CONKYCMD="$CONKYCMD $LINE & sleep 1s;"
            if [[ $LINE = "conky" ]] || [[ $LINE = "conky -q" ]];then
                line="$DEF"
            else
                line=${line##*" "}      # get full filepath
            fi
            TXT="$TXT$line"
            LISTMSGTXT="$LISTMSGTXT\n\t$line"
        done < "$TEMPFILE"
        echo "[CONKY] $CONKYCMD" >> "$SETTINGS"
        rm "$TEMPFILE"
        echo "$MSG\t$LISTMSGTXT\n" >> "$LISTMSG"
    else
        $DIALOG --form --image="dialog-warning" \
        --text="You selected to save Conkys\nbut none are running" \
        "$OK" \
        --width=300 \
        2>/dev/null
        cleanup save
    fi
}

function checkConkyzen(){    # see if bl-conkyzen and session file present
    if type bl-conkyzen &>/dev/null;then
        if [[ -f $CONKYSESSION ]]; then
            CZEN=1  # set flag for Restore choice
        fi
    fi
}

function getTint(){
    > "$TEMPFILE" # make blank tempfile, to save running tint2 paths
    pgrep -a tint2 | while read pid cmd;do
        if [[ ${cmd%% *} = tint2 ]];then
            TPATH=$(echo "$cmd" | awk '{print $NF}')
            echo "$TPATH;" >> "$TEMPFILE"
        fi
    done
    TXT="Tint2s:\n\n"
    MSG="Running Tint2s:"
    TINTCMD=""
    LISTMSGTXT=""
    while read line;do
        TXT="$TXT$line"
        LINE=$(echo "$line" | sed 's/;//')
        if [[ $LINE = tint2 ]];then         # default command was used
            TINTCMD="$TINTCMD$LINE;"
            LISTMSGTXT="$LISTMSGTXT\n\tDefault Tint2"
        else
            TINTCMD="$TINTCMD$LINE;"
            LISTMSGTXT="$LISTMSGTXT\n\t$LINE"
        fi
    done < "$TEMPFILE"
    echo "[TINT2] $TINTCMD" >> "$SETTINGS"
    rm "$TEMPFILE"
    echo "$MSG\t$LISTMSGTXT\n" >> "$LISTMSG"
}

function killTints(){
    pgrep -a tint2 | while read pid cmd; do
        if [[ ${cmd%% *} = tint2 ]]; then
            kill "$pid"
        fi
    done
}

function checkTint2zen(){    # see if bl-tint2zen and session file present
    if type bl-tint2zen &>/dev/null;then
        if [[ -f $TINTSESSION ]]; then
            TZEN=1          # set flag for Restore choice
        fi
    fi
}

function getOBtheme(){   # copy <theme> section from rc.xml to obtheme.txt
    RCFILE="$OBPATH/rc.xml"
    tag="theme"
    sed -n "/<$tag>/,/<\/$tag>/p" "$RCFILE" > "$CONFIGDIR/obtheme.txt"
    echo "[OBTHEME]" >> "$SETTINGS"
    getOBname
}

function getOBname(){    # get OB theme name
    OBFILE="$CONFIGDIR/obtheme.txt"
    OBTHEME=$(awk 'NR==2 {print;exit}' $OBFILE | awk -F'[>|<]' '{print $3}')
    TXT="Openbox theme:  $OBTHEME\n"
    echo -e "\n  Saved $TXT"
    echo "$TXT" >> "$LISTMSG"
}

function getGTKtheme(){
    GTKTHEMES=( "$HOME/$GTK2" "$HOME/$GTK2MINE" "$GTK3" "$GTK3x" )
    for f in "${GTKTHEMES[@]}";do
        if [[ -f $f ]];then
            cp "$f" "$CONFIGDIR"
        elif [[ -d $f ]];then
            cp -r "$f" "$CONFIGDIR"
        fi
    done
    echo "[GTK]" >> "$SETTINGS"
    getGTKname
}

function getGTKname(){   # get GTK theme name
    GTKFILE="$CONFIGDIR/.gtkrc-2.0"
    while read line;do
        if [[ $(echo $line | grep "gtk-theme-name" ) ]] &>/dev/null;then
            GTKTHEME=$(echo $line | awk -F '"' '{print $2}')
        fi
    done < "$GTKFILE"
    TXT="GTK theme:  $GTKTHEME\n"
    echo -e "\n  Saved $TXT"
    echo "$TXT" >> "$LISTMSG"
}

function getDmenu(){
    if [[ -d $DMENUDIR ]];then
        cp "$DMENUDIR/$DMENU" "$CONFIGDIR"
        echo "[DMENU]" >> "$SETTINGS"
        TXT="dmenu config:  $DMENUDIR/$DMENU\n"
        echo -e "\n  Saved $TXT"
        echo "$TXT" >> "$LISTMSG"
    else
        echo "  $DMENUDIR/$DMENU not found"
    fi
}

function getLightdm(){
    if [[ -f $LDMDIR/$LDM ]];then
        cp "$LDMDIR/$LDM" "$CONFIGDIR"
        TXT="Lightdm config:  $LDMDIR/$LDM\n"
        echo -e "\n  Saved $TXT"
        echo "[LIGHTDM]" >> "$SETTINGS"
        echo "$TXT" >> "$LISTMSG"
    fi
}

function getScrot(){
    pid=""
    TMPSCROT="$CONFIGPATH/tmpscrot.tmp"
    SCROT="$CONFIGPATH/$ANS"
    CURRDTOP=$(xprop -root _NET_CURRENT_DESKTOP | tail -c -2) # desktop number
    MONS=$(xrandr -q | grep -c " connected")    # number of monitors
    # Set mouse position, for appearance of root-menu
    screenW=$(xrandr -q | awk '/Screen/ {print $8}')
    screenH=$(xrandr -q | awk '/Screen/ {print $10}')
    screenH=${screenH%,}
    
    if (( $MONS == 1 ));then # single monitor
        appX=$(( (screenW/20)*8 ))
        menuX=$(( (screenW/20)*2 ))
    else
        appX=$(( (screenW/20)*4 ))
        menuX=$(( (screenW/20)*1 ))
    fi
    appY=$(( (screenH/20)*4 ))
    menuY=$(( (screenH/20)*4 ))

    wmctrl -l -x > "$TMPSCROT"      # store window list in tempfile
    wmctrl -k on                    # hide windows, show desktop
    # show any conkys which have been hidden by wmctrl, (ie not own_window_type desktop)
    for CONK in $(xdotool search --classname "Conky");do
        xdotool windowactivate "$CONK"
    done
    # start lxappearance
    lxappearance &
    pid=$!
    sleep 0.5
    # get lx window, make sure it loses focus, then move mouse and start root-menu
    LX=$(xdotool getwindowfocus)
    xdotool windowmove --sync $LX $appX $appY && sleep 0.1 && \
    xdotool mousemove --sync --window $LX -- -50 0 && xdotool click 1
    xdotool mousemove --sync $menuX $menuY && xdotool key --clearmodifiers "super+space"
    i=1
    while [ $i -le $KEYDOWN ];do 
        xdotool key --delay 30 Down 
        i=$(( $i+1 ))
    done
    xdotool key --delay 20 Right && sleep 0.3
    # take scrot
    scrot -t 9 "$SCROT.jpg"    # scrot thumbnail @9% fullsize
    THUMB=$(find $CONFIGPATH -maxdepth 1 -type f -name "*-thumb.jpg" 2>/dev/null)
    mv $THUMB $CONFIGDIR
    # close root menu
    xdotool mousemove_relative --sync --polar 0 10 click 3
    # kill lxappearance
    if [[ $(pgrep lxappearance | grep $pid) ]];then
        kill -9 $pid &>/dev/null
    fi
    # restore hidden windows
    while read line; do
        WINDOW=$(echo $line | awk '{print $1}') # Window_ID is first field
        DTOP=$(echo $line | awk '{print $2}' )  # Desktop number is second field
        if [[ $DTOP = $CURRDTOP ]] 2>/dev/null;then
            xdotool windowactivate "$WINDOW"
        fi
    done < "$TMPSCROT"
    rm "$TMPSCROT"
}

function restoreGTK(){   # $1 is chosen saved config dir
    GTKPATHS=( "$HOME" "$HOME" "$GTK3" "$GTK3x" )
    GTKSAVED=( "$1/$GTK2" "$1/$GTK2MINE" "$1/gtk-3.0" "$1/xfce4-notifyd.xml" )
    i=0
    for f in ${GTKSAVED[@]};do
        if [[ -e $f ]];then     # if destination exists
            if [[ -e ${GTKPATHS[i]} ]];then
                if [[ ${GTKSAVED[i]} = $1/gtk-3.0 ]];then
                    cp -r ${GTKSAVED[i]}/* ${GTKPATHS[i]} # restore contents of gtk-3.0
                fi
                cp ${GTKSAVED[i]} ${GTKPATHS[i]}
                echo -e "\n  Restored: ${GTKSAVED[i]}"
            else
                echo -e "\n  Restore path ${GTKPATHS[i]} not found" 2>&1
            fi
        else
            echo -e "\n  ${GTKSAVED[i]} not found" 2>&1
        fi
        i=$(($i+1))
    done
}

function restoreOBrc(){
    RCFILE="$OBPATH/rc.xml"
    THEMEFILE="$1/obtheme.txt"
    FTEMP="$1/temp.txt"
    if [[ $(grep "[OBTHEME]" "$1/settings.cfg" ) ]] &>/dev/null;then
        # backup rc.xml first
        NOW=$(date +"%Y%m%d-%H%M%S")
        RCBKP="$RCFILE.blob.$NOW"
        cp "$RCFILE" "$RCBKP"
        # remove older backups
        for f in $(find $OBPATH -type f|grep .blob. );do
            if [[ $f -ot $RCBKP ]];then
                rm "$f"
            fi
        done
        ## put placeholder in place of <theme> section, write to tempfile
        sed -n "/<theme>/{:a;N;/<\/theme>/!ba;N;s/.*\n/THEMESECTION\n/};p" "$RCFILE" > $FTEMP
        # replace placeholder from theme file
        sed -i "/THEMESECTION/{
            s/THEMESECTION//g
            r $THEMEFILE
        }" $FTEMP
        sed -i '/^$/d' $FTEMP   # remove empty lines
        cp $FTEMP "$RCFILE"     # overwrite rc.xml
        echo -e "\n  rc.xml backed up and edited for OB theme"
        rm $FTEMP
    fi
}

function restoreTint2(){      # write to tint2 session file
    echo "$1" | awk -F';' '{for(i=1; i<=NF; i++) print $i}' >> "$TINTSESSION"
    killTints
    while read tintline;do
        (tint2 -c "$tintline" &) &>/dev/null
        sleep 1s
    done < "$TINTSESSION"
}

function restoreDmenu(){
    DFILE="$1"
    if [[ -e $DMENUDIR ]];then        # backup DMENU first
        DMENUBKP="$DMENUDIR/$DMENU.bkp"
        cp "$DMENUDIR/$DMENU" "$DMENUBKP"
        cp "$DFILE" "$DMENUDIR"
        echo -e "\n  $DMENUDIR/$DMENU backed up and restored"
    else
        cp "$DFILE" "$DMENUDIR/$DMENU"
        echo -e "\n  $DMENUDIR/$DMENU restored"
    fi
}

function restoreLightdm(){   # need sudo to restore lightdm-gtk-greeter.conf
    CMD="cp $1 $LDMDIR/$LDM"
    TXT="Authenticate restore of lightdm-gtk-greeter.conf"
    LOOP=1
    while [[ $LOOP ]];do
        gksudo -m "$TXT" "$CMD"
        DLG=$?
        case "$DLG" in
            0   )   MSG="  Restored: lightdm-gtk-greeter.conf"
                    echo -e "\n  $MSG"
                    break
                    ;;
            255 )   MSG="  Authentication cancelled\n\nlightdm-gtk-greeter.conf was not restored"
                    echo -e "\n  $MSG" 2>&1
                    break
                    ;;
            *   )   MSG="  Password input failed\n\nlightdm-gtk-greeter.conf was not restored"
                    echo -e "\n  $MSG" 2>&1
                    "$DIALOG" --image="dialog-warning" "$T$MSG" "$OK" 2>/dev/null
                    ;;
        esac
    done
}

function cleanup(){
    if [[ $1 = all ]];then
        if [[ -d $CONFIGDIR ]];then
            rm -rf "$CONFIGDIR"
            echo -e "\n  Deleted $CONFIGDIR"
        fi
        if [[ -d $CONFIGDIR-BKP ]];then
            mv {"$CONFIGDIR-BKP","$CONFIGDIR"}
            echo -e "\n  Cancelled\n..Temporary $CONFIGDIR restored\n"
        fi
    fi
    if [[ $1 = save ]];then
        saveSettings
    else
        Intro
    fi
}

function saveSettings(){
    COLUMNS=(\
        "FALSE" "$PICKOB" \
        "FALSE" "$PICKGTK" \
        "FALSE" "$PICKCONKY" \
        "FALSE" "$PICKTINT" \
        "FALSE" "$PICKBG" \
        "FALSE" "$PICKDMENU" \
        "FALSE" "$PICKLDM" \
        )
    SLOOP=1
    while (( $SLOOP == 1 ));do
        CHOICE=$($DIALOG --list --checklist --title \
            --text="Choose the configs you want to save..." \
            $CANCEL $OK \
            --width=340 --height=340 \
            --column="Select" --column="Config" "${COLUMNS[@]}" \
            2>/dev/null )
        if (( $? == 1 )); then # cancel button pressed
            cleanup all
        else
            if [[ ! $CHOICE ]] ; then # entry field is empty, so try again
                RET=$($DIALOG  --image="dialog-question" \
                --text="\n  Make a selection...." \
                $EXIT $OK \
                --width=300 \
                2>/dev/null )
                if (( $? == 1 ));then
                    cleanup all
                fi
            else
                i=0
                retChoice=()
                OIFS=$IFS # save Internal Field Separator
                IFS=$"|" # separator is "|" in returned choices
                # dialog return is in format 'TRUE|value|\nTRUE|value'
                for ret in $CHOICE; do
                    if [[ $(echo $ret | grep TRUE) ]] &>/dev/null;then
                        continue
                    fi
                    retChoice[$i]="$ret"
                    i=$(($i+1))
                done
                IFS=$OIFS # reset IFS back to default
                len=${#retChoice[@]}
                for (( i=0; i<${len}; i++ ));do
                    item="${retChoice[$i-1]}"
                    case "$item" in
                        "$PICKOB"   )   getOBtheme
                                        ;;
                        "$PICKGTK"  )   getGTKtheme
                                        ;;
                        "$PICKCONKY")   getConky
                                        ;;
                        "$PICKTINT" )   getTint
                                        ;;
                        "$PICKBG"   )   getBg
                                        ;;
                        "$PICKDMENU")   getDmenu
                                        ;;
                        "$PICKLDM"  )   getLightdm
                                        ;;
                        *           )   echo -e "\n  Unknown value!" 2>&1
                                        exit 1
                                        ;;
                    esac
                done
                SLOOP=0
            fi
        fi
    done
    TXT="<big><b><u>Openbox Configurations to be saved:</u></b></big>\n\n"
    TXT2=$(cat "$LISTMSG")
    NEW=$($DIALOG --text="$TXT$TXT2")
    if (( $? == 1 ));then
        cleanup all
        restoreSettings
    else
        echo "$ANS" > "$CURSESSION"     # replace entry in saved config flag file
    fi
}

function ViewRestoreDialog(){  #   Display saved theme collections
    getSet          # get name of latest set BLOB config
    if (( $NUMDIRS == 0 ));then   # no saved configs; go back to Intro dialog
        Intro
    fi
    DLGLIST=""
    setARR=()
    namesARR=()
    thumbsARR=()
    i=0
    for dir in "$CONFIGPATH"/* ;do
        if [[ -d $dir ]];then
            if [[ -e $dir/settings.cfg ]];then
                d=$(echo $dir | sed -e 's/ /_/g')    # replace any spaces in dir name
                namesARR[$i]="<big>$(basename $d)</big>" # add theme collection name to array
                d=${d##*/}  # get directory name
                # Get name of current session, set icon for TRUE/FALSE
                if [[ $SET = $d ]];then
                    setARR[$i]="gtk-yes"
                else
                    setARR[$i]="gtk-no"
                fi
                IMGTHUMB="None"     # Placeholder if no thumbnail found
                f="$dir"/*"-thumb.jpg"
                if [[ $f ]];then
                    IMGTHUMB="$f"
                fi
                thumbsARR[$i]="$IMGTHUMB"
                CONFIGS="\""
                while read line;do
                    CONFIGS="$CONFIGS $line\n"
                done < "$dir/LISTMSG.txt"
                CONFIGS="$CONFIGS\""
                configARR[$i]="$CONFIGS" # add saved configs to array
            fi
            i=$(($i+1))
        fi
    done
    for ((j=0; j<${#namesARR[*]}; j++));do
        DLGLIST="$DLGLIST ${setARR[j]} ${namesARR[j]} ${thumbsARR[j]}"
    done
}

function restoreSettings(){
    ViewRestoreDialog
    FPATH=""
    LOOP=1
    while [[ $LOOP ]] ;do
        CONFIG=$($DIALOGDEC --list --title="$TITLE" \
            --text="Double-click selection, or \"Enter\", or \"Restore\" to restore settings\n" \
            --separator=" " \
            --button="Add New:3" \
            --button="Delete:2" --button="Restore:0" --button="gtk-quit:1" \
            --always-print-result \
            --width=$W --height=$H --center --image-preview \
            --expand-column=3 \
            --column="Last Set:IMG" \
            --column="Config Name":TEXT \
            --column="Screenshot":IMG \
            $DLGLIST \
            2>/dev/null )
        RET=$?
        if [[ $CONFIG ]];then
            DIR=$(echo "$CONFIG" | awk -F'[>|<]' '{print $3}' )
            FPATH="$CONFIGPATH/$DIR"
        fi
        case $RET in
            0)  SAVED="$FPATH/LISTMSG.txt"
                TEXT="Restoring Configs saved as <big>$DIR</big>\n\n"
                TEXT="$TEXT$(cat $SAVED)"
                $DIALOG "$T$TEXT" "$CANCEL" "$OK" 2>/dev/null
                if (( $? == 0 ));then
                    echo $(basename $FPATH) > "$CURSESSION"
                    break
                fi
                ;;
            1)  exit 0
                ;;
            2)   if ! [[ $CONFIG ]];then
                    $DIALOG --text="Make a selection before trying to delete" "$EXIT" "$OK"
                    if (( $? == 0 ));then
                        continue
                    else
                        exit 0
                    fi
                fi
                TEXT="Delete saved configurations <b>$FPATH</b>\nand screenshot?"
                $DIALOG "$T$TEXT" "$CANCEL" "$OK" 2>/dev/null

                if (( $? == 0 ));then
                    rm -rf "$FPATH"
                    rm "$FPATH.jpg"
                    echo -e "\n$DIR DELETED"
                    currentsession=$(cat $CURSESSION)
                    fpath=$(basename $FPATH)
                    if [[ $currentsession = $fpath ]];then
                        echo "" > "$CURSESSION"
                    fi
                    restoreSettings
                else
                    continue
                fi
                ;;
            3)  setName
                saveSettings
                getScrot
                $DIALOG --text="Settings were saved as <b>$ANS</b>" --image="gtk-save" "$OK"
                if [[ -d $CONFIGDIR-BKP ]];then
                    rm -rf "$CONFIGDIR-BKP"
                    echo -e "\n  Temporary $CONFIGDIR deleted\n"
                fi
                restoreSettings
                ;;
            *)  if (( $RET == 252 ));then
                    echo -e "\n  Exited with \"ESC\" key"
                else
                    echo -e "\n  Error= $RET" 2>&1
                fi
                exit 1
                ;;
        esac
    done
    SETCFG="$FPATH/settings.cfg"
    # Get chosen saved configs and restore them
    while read line;do
        TAG=$(echo $line | awk '{print $1}')
        VAL=$(echo $line | awk '{print $2}')
        case "$TAG" in
            "[TINT2]"       )   checkTint2zen # see if bl-tint2zen installed
                                if (( $TZEN == 1 ));then
                                    > "$TINTSESSION" # overwrite tint2 session file
                                fi
                                TINTCMD=$(echo $line | cut -d ' ' -f2-)
                                restoreTint2 "$TINTCMD"
                                ;;
            "[CONKY]"       )   checkConkyzen # see if bl-conkyzen installed
                                if (( $CZEN == 1 ));then
                                    > "$CONKYSESSION" # overwrite conky session file
                                fi
                                CONKYCMD=$(echo $line | cut -d ' ' -f2-)
                                # write to conky session file...
                                echo $CONKYCMD | awk -F';' '{for(i=1; i<=NF; i++) print $i}' >> "$CONKYSESSION"
                                killall conky
                                source "$CONKYSESSION"
                                ;;
            "[GTK]"         )   restoreGTK "$FPATH"
                                ;;
            "[OBTHEME]"     )   restoreOBrc "$FPATH"
                                ;;
            "[BACKGROUND]"  )   if [[ $VAL = FEH ]];then
                                    cp "$FPATH/.fehbg" "$HOME"
                                    eval $(cat ~/.fehbg)
                                else
                                    cp "$FPATH/bg-saved.cfg" "$NITRODIR"
                                    nitrogen --restore
                                fi
                                ;;
            "[DMENU]"       )   restoreDmenu "$FPATH/$DMENU"
                                ;;
            "[LIGHTDM]"     )   restoreLightdm "$FPATH/$LDM"
                                ;;
            *               )   echo -e "  Unknown value!" 2>&1
                                restoreSettings
                                ;;
        esac
    done < "$SETCFG"
    openbox --reconfigure
    restoreSettings
}

function reloadGTK(){ # reload gtk theme after restoring saved config
python - <<END
import gtk

events=gtk.gdk.Event(gtk.gdk.CLIENT_EVENT)
data=gtk.gdk.atom_intern("_GTK_READ_RCFILES", False)
events.data_format=8
events.send_event=True
events.message_type=data
events.send_clientmessage_toall()

END
}

### END FUNCTIONS ######################################################

# test for blobthemes config directory, create if it doesn't exist
mkdir -p "$CONFIGPATH"
# create file to hold most recent saved configuration name
if [[ ! -f $CURSESSION ]] 2>/dev/null;then
    touch "$CURSESSION"
fi
countMonitors
getSet
if (( $NUMDIRS == 0 ));then
    Intro
else
    restoreSettings
fi
exit 0

Offline

#15 2015-10-27 18:20:54

damo
....moderator....
Registered: 2015-08-20
Posts: 5,418

Re: Is there any desire for an aerosnap script, or OB Config Manager?

@ostrolek

I am quite capable of posting hundreds of lines of my code, in a thread I started. The reason I didn't is so people can get the latest code from the links, and I don't have to respond to help questions because someone C&P's it from your posts.

How about asking first if it was OK to do it? mad


Be Excellent to Each Other...

FORUM RULES and posting guidelines «» Help page for forum post formatting
Artwork on DeviantArt  «» BunsenLabs on DeviantArt

Offline

#16 2015-10-27 18:27:35

nobody0
Disabled account
Registered: 2015-09-29
Posts: 664

Re: Is there any desire for an aerosnap script, or OB Config Manager?

^ Sorry, damo. I thought the guys are saying they can't download them.

Offline

#17 2015-10-27 18:39:15

damo
....moderator....
Registered: 2015-08-20
Posts: 5,418

Re: Is there any desire for an aerosnap script, or OB Config Manager?

ostrolek wrote:

^ Sorry, damo. I thought the guys are saying they can't download them.

One guy said he couldn't, so he got it from github. Then I said I would make a github release today.

EDIT:  Github release: winsnap v1.0.0-alpha

Last edited by damo (2015-10-27 20:49:50)


Be Excellent to Each Other...

FORUM RULES and posting guidelines «» Help page for forum post formatting
Artwork on DeviantArt  «» BunsenLabs on DeviantArt

Offline

#18 2015-10-28 09:31:39

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 6,184
Website

Re: Is there any desire for an aerosnap script, or OB Config Manager?

ohnonot wrote:
johnraff wrote:

@ohnonot I agree of course with your general point, but just for the record I was able to download that file from copy.com without having to disable noscript.

you don't get this
http://iki.fi/dt/stuff/abe-deny.png
???

No, I just clicked download and down it came.


John

...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), idle Twitterings and GitStuff )

Offline

#19 2015-10-28 19:28:34

ohnonot
...again
Registered: 2015-09-29
Posts: 4,227
Website

Re: Is there any desire for an aerosnap script, or OB Config Manager?

^ 'K, thanks. weird. maybe it's to do with IPs, countries, even ISPs...
but i don't want to hijack the thread, so move along folks.

Offline

#20 2015-10-29 02:50:37

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 6,184
Website

Re: Is there any desire for an aerosnap script, or OB Config Manager?

(OT @ohnonot or maybe I had allowed more sites. /OT)


John

...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), idle Twitterings and GitStuff )

Offline

Board footer

Powered by FluxBB