docs.kde.org
KDE DIY - Building Your Own Tools
Prev
Next

KDE DIY - Building Your Own Tools

DCOP

Desktop COmmunication Protocol, DCOP, is a lightweight mechanism for inter-process communication. DCOP allows the user to interact with programs that are currently running. KDE supplies two programs to utilitize DCOP: dcop, a command-line program, and kdcop, a GUI program.

A few notes about using dcop:

  • dcop [options] [application [object [function [arg1] [arg2] ... ] ] ]

  • Applications that can open more than one window at a time will be listed as <application>-PID

  • All the arguments are case-sensitve. setFullScreen and setfullscreen are two different functions.

  • The regular expression token * can be used in the application and object arguments.

    % dcop kon*
    konqueror-16006
    konsole-8954
    

Some example commands and their output are below:

% dcop konsole*
konsole-8954

One Konsole is running with a PID of 8954.

% dcop konsole-8954
KBookmarkManager-.../share/apps/kfile/bookmarks.xml
KBookmarkManager-.../share/apps/konqueror/bookmarks.xml
KBookmarkNotifier
KDebug
MainApplication-Interface
konsole (default)
konsole-mainwindow#1
ksycoca
session-1
session-2
session-3
session-4

Here you see that there are four sessions running.

% dcop konsole-8954 konsole
QCStringList interfaces()
QCStringList functions()
int sessionCount()
QString currentSession()
QString newSession()
QString newSession(QString type)
QString sessionId(int position)
void activateSession(QString sessionId)
void nextSession()
void prevSession()
void moveSessionLeft()
void moveSessionRight()
bool fullScreen()
void setFullScreen(bool on)
ASYNC reparseConfiguration()

Here are the options for the main Konsole program.

% dcop konsole-8954 session-1
QCStringList interfaces()
QCStringList functions()
bool closeSession()
bool sendSignal(int signal)
void clearHistory()
void renameSession(QString name)
QString sessionName()
int sessionPID()
QString schema()
void setSchema(QString schema)
QString encoding()
void setEncoding(QString encoding)
QString keytab()
void setKeytab(QString keyboard)
QSize size()
void setSize(QSize size)

Here are the options for the first session, session-1.

% dcop konsole-8954 konsole setFullScreen true

This sets Konsole to full screen.

When there is more than one application/object, which one should you use? Got a reference?

% echo $KONSOLE_DCOP
DCOPRef(konsole-7547,konsole)

% dcop $KONSOLE_DCOP newSession
session-6

% dcopstart konsole
konsole-9058


#!/bin/sh
konsole=$(dcopstart konsole-script)
session=$(dcop $konsole konsole currentSession)
dcop $konsole $session renameSession Local

session=$(dcop $konsole konsole newSession)
dcop $konsole $session renameSession Remote

session=$(dcop $konsole konsole newSession)
dcop $konsole $session renameSession Code
dcop $konsole $session sendSession 'cd /my/work/directory'

KDialog

KDE DIY - Building Your Own Tools

You can use KDE dialogs from your own scripts, to combine the power of UNIX® shell scripting with the ease of use of KDE.

kdialog --msgbox 'You have new mail!'
kdialog --title 'New Mail' --msgbox 'You have new mail!'

The KDialog part can be replaced via --caption option

kdialog --title 'New Mail' --msgbox 'You have new mail!' --dontagain myfile:mykey

Saves whether to show again in $KDEHOME/share/config/myfile (by writing into this file the following lines:

[Notification Messages]
mykey=false

Instead of --msgbox you can also use --sorry and --error, as appropriate. For instance, you might use kdialog --sorry 'The network can not be reached' or kdialog --error 'Mail box can not be opened'.

It is also possible to create message boxes that accept a yes or no answer.

kdialog --yesno 'Do you want to connect
to the Internet?' echo $?
Return ValueMeaning
0Yes, OK, Continue
1No
2Cancel

Make sure to store the result in a variable if you do not use it directly, the next command will fill $? with a new value You can use --dontagain here as well, it will remember the users choice and returns it the next times without showing the dialog any more.

Further variations are:

--warningyesno

like --yesno but with a different icon

--warningcontinuecancel

With Continue and Cancel buttons.

--warningyesnocancel

With Yes, No and Cancel button. For example:

kdialog --warningyesnocancel 'Do you want
to save the changes?'
kdialog --inputbox "Enter your name:" "YourName"

The result is printed to stdout, to put it in a variable you can use name=$(kdialog --inputbox "Enter your name:" "YourName"). The last argument is optional, it is used to pre-fill the dialog.

password=$(kdialog --password "Enter your password:")

The --dontagain option does not work with --inputbox or --password

There are two dialogs that let the user make a choice from a list:

--menu

Lets the user select a single item from a list.

--checklist

Lets the user select one or more items from a list.

city=$(kdialog --menu "Select a city" a London b Madrid c Paris d Berlin)

$city will a, b, c or d.

city=$(kdialog --checklist "Select cities" a London off b Madrid on c Paris on d Berlin off)

Madrid and Paris will be pre-selected. The result with Madrid and Paris selected will be "b" "c".

If you add the --separate-output option, it will put b and c each on a line of its own, making the result easier to process.

file=$(kdialog --getopenfilename $HOME)
file=$(kdialog --getopenfilename $HOME "*.png *.jpg|Image Files")
file=$(kdialog --getsavefilename $HOME/SaveMe.png)
file=$(kdialog --getexistingdirectory $HOME)
Prev
Next
Home


docs.kde.org