Harsh but fair

Open source chicanery and the battle with my inner geek

KDE4 Save Current Session - DBUS to the Rescue

Posted by raetsel on April 25, 2008

Executive Summary

To cut to the chase, if you want to save your current sessions in KDE4 run the following command from a command line ( or put it in a script that you can call from a desktop shortcut ):

dbus-send --dest=org.kde.ksmserver /KSMServer org.kde.KSMServerInterface.saveCurrentSession

The Details

Now for how I got here in the first place:

I’ve just upgraded to Kubuntu Hardy Heron and decided to give KDE4 a go. I’ll be posting about the upgrade process and KDE4 in general over the coming days but one thing that has kept me “amused” today is the lack of a “Save Current Session” button in KDE4

Rather than have the desktop returned to how it was when I last logged out ( “Restore Previous Session” under KControl ) I like to return to a standard set of applications so I use the “Restore Manually Saved Session” option under KDE 3.5.

In KDE4 KControl there are the same options for the session manager. The snag is that there is no button anywhere to actually save the session when you want to. Thus choosing this option effectively logs you back in to a blank session. This has been reported as a bug in KDE but does not seem to be making any progress.

Flushed with my success with dcop recently I was hoping I could use it with the ksmserver object ( the session manager ) but in KDE4 dcop is not used and the dbus interface is used instead which is a bit more complicated to use.

There is a tool you can install called kdbus that does a similar job to kdcop but I found a much better tool called qdbusviewer along with a useful overview of KDE and dbus here.

To use qdbusviewer you need to install the package qt4-dev-tools then run qdbusviewer from a command line:-

sudo apt-get install qt4-dev-tools

qdbusviewer

You can use qdbusviewer to browser what objects are available and execute methods on them just like kdcop and kdbus ( but kdbus is rather slower )

7 Responses to “KDE4 Save Current Session - DBUS to the Rescue”

  1. Peter Lewis Says:

    Thanks Simon. I’d noticed that “save current session” had either dissapeared or moved to somewhere beyond my knowledge in recent KDE 3s in Kubuntu. Thanks for the tip on doing this in 4! (Building today’s KDE 4 SVN as we speak… ;)

  2. raetsel Says:

    Hi Pete,

    I haven’t seen that issue with the 3.5 builds, certainly Hardy Heron 3.5.9 has a save session button on the kicker as long as you have “Restore Manually Saved” session switched on in the Session Manager.

    However in KDE 3.5 the dcop equivalent is:-

    dcop ksmserver default saveCurrentSession

  3. resu Says:

    Hi, Reatsel!

    Thank you for your useful suggestion on using dbus-send. In my case (Kubuntu 8.04, KDE4) however it didn’t work. I checked with kdbus and found the command line that worked:

    dbus-send –session dest=org.kde.ksmserver /KSMServer org.kde.KSMServerInterface.saveCurrentSession

    This allowed me to save the session I needed, though this session included terminal I used to run the commanу itself. When I tried to use ‘run command’ on KDE desktop it didn’t work.

    Is there a workaround?

    Thanks and regards

  4. raetsel Says:

    Hi Resu,

    I managed to get a desktop shortcut to work. I created it with the following executable command:-

    /usr/bin/dbus-send –print-reply –session –dest=org.kde.ksmserver /KSMServer org.kde.KSMServerInterface.saveCurrentSession >/tmp/session.out 2>&1

    Not that when this is clicked it creates a file call /tmp/session.out that you can look at to see the output of the command. Also I have added the –print-reply option which will show the response from the dbus server. Which should be something like:-

    method return sender=:1.6 -> dest=:1.49 reply_serial=2

  5. raetsel Says:

    Oh and please note that the command line options all need double dashes before them i.e. - - , wordpress is mangling these to show a single long dash --

  6. Edney Says:

    Hi!

    Nice article! I’m trying to grow from it to custom my acpi power button event to ask user to shutdown when button pressed. But I can’t handle the fact that root can’t send message to other users.I’m using

    #usr/bin/dbus-send –session –type=method_call –dest=org.kde.ksmserver /KSMServer org.kde.KSMServerInterface.logout int32:1 int32:2 int32:0

    this works fine when called from the user logged in, but not from root.

    Do you know how to handle this situation?

    Thanks.

  7. raetsel Says:

    Hi Edney,

    I haven’t really done anything with passing parameters to dbus-send etc. but a couple of things spring to mind.

    Firstly would using the system bus work rather than the session bus :-

    dbus-send --system --type etc etc.

    Or failing that can you find out the name of the user logged in and use the su -c command as root to send the info as that user? (Not ideal but might work):-

    LOGUSER=[ some code to work out the user ]
    su - $LOGUSER -c dbus-send –system –type etc etc

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>