Thursday, January 24, 2013

The sick and twisted world of KDE/konsole 4.8.4 from dbus

YUCK

For some reason, you have to find yourself when running from a konsole window now.

So the code for opening tabs now has to:

  • Grab a list of all the fricking windows (getWindowId()/getWindows() below) to find my own id.
  • Grab a list of all the fricking sessions (getSessions() below)
  • Open a new tab
  • Grab the session list AGAIN and compare it to find the one we just opened.
  • Only then are you able to send a command to that tab.
Hope you can read some shell script, because this is almost too painful to explain.

#!/usr/bin/env bash

# konsole dbus functions

getWindows() {
    windows=`qdbus org.kde.konsole /konsole org.freedesktop.DBus.Introspectable.Introspect |grep MainWindow | cut -d \" -f 2`
    echo ${windows}
}

getSessions() {
    sessions=`qdbus org.kde.konsole /Sessions org.freedesktop.DBus.Introspectable.Introspect |grep "node name" | cut -d \" -f 2`
    echo ${sessions}
}

getWindowId() {
    windows=`getWindows`
    for i in $windows
    do
        id=`qdbus org.kde.konsole /konsole/$i org.kde.KMainWindow.winId`
        if [ "${WINDOWID}" -eq "$id" ]; then
            echo $i
        fi    
    done
}    
findNewSession() {
    old="$@"
    new=`getSessions`
    for t in $new; do 
        grep -q $t <(echo $old) 
        if [ "$?" -eq "1" ]; then 
            echo $t
        fi
    done
}
sshopentabs () {
    boxlist=$1
    box_name=$2
    comm='ssh -o StrictHostKeyChecking=no -o CheckHostIP=no -o UserKnownHostsFile=/dev/null'
    
    slot=1
    for i in $boxlist
    do
        sessions=`getSessions`
        echo ${KONSOLE_DBUS_SERVICE}
        newtab
        j=`findNewSession $sessions`
        if [ "${box_name}" -eq "" ]; then
            renametab ${j} $i
        else
            renametab ${j} "${box_name} slot ${slot}"
        fi
        sendtab ${j} "${comm} root@${i}"
        let slot=$slot+1
    done

    return ${j}
}
newtab () {
    id=`getWindowId`
    dbus-send --session --dest=${KONSOLE_DBUS_SERVICE} --type=method_call --print-reply /konsole/$id org.kde.KMainWindow.activateAction string:"new-tab"
}

renametab () {
    sessionno=$1
    tabname=$2
    session="/Sessions/${sessionno}"
    dbus-send --session --dest=${KONSOLE_DBUS_SERVICE} --type=method_call --print-reply ${session} org.kde.konsole.Session.setTabTitleFormat int32:0 string:"$tabname"
    dbus-send --session --dest=${KONSOLE_DBUS_SERVICE} --type=method_call --print-reply ${session} org.kde.konsole.Session.setTabTitleFormat int32:1 string:"$tabname"
    dbus-send --session --dest=${KONSOLE_DBUS_SERVICE} --type=method_call --print-reply ${session} org.kde.konsole.Session.setTitle int32:1 string:"$tabname"
} 

sendtab () {
    sessionno=$1
    cmd=$2
    session="/Sessions/${sessionno}"
    dbus-send --session --dest=${KONSOLE_DBUS_SERVICE} --type=method_call --print-reply ${session} org.kde.konsole.Session.sendText string:"$cmd^M"
}

closetab () {
    dbus-send --session --dest=${KONSOLE_DBUS_SERVICE} --type=method_call --print-reply ${KONSOLE_DBUS_SESSION} org.kde.konsole.Session.sendText string:"exit^M"
}

No comments:

Post a Comment