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 )
April 25, 2008 at 11:36 pm
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…
April 26, 2008 at 3:14 pm
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
May 9, 2008 at 1:21 pm
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
May 10, 2008 at 11:08 am
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
May 10, 2008 at 11:12 am
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
--May 13, 2008 at 2:58 am
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.
May 13, 2008 at 3:24 pm
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