add more spacing
[personal-kdebase.git] / workspace / khotkeys / libkhotkeysprivate / daemon / daemon.cpp
blobe1dc6a6a7a6e5422aac25f1103d29e725bcb092f
1 /* Copyright (C) 2008 Michael Jansen <kde@michael-jansen.biz>
3 This library is free software; you can redistribute it and/or
4 modify it under the terms of the GNU Library General Public
5 License as published by the Free Software Foundation; either
6 version 2 of the License, or (at your option) any later version.
8 This library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 Library General Public License for more details.
13 You should have received a copy of the GNU Library General Public License
14 along with this library; see the file COPYING.LIB. If not, write to
15 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 Boston, MA 02110-1301, USA.
19 #include "daemon.h"
21 #include <memory>
23 #include "khotkeysglobal.h"
25 #include <QtDBus/QtDBus>
27 #include <KDE/KConfigGroup>
28 #include <KDE/KConfig>
29 #include "KDE/KDebug"
30 #include "KDE/KLocale"
33 namespace KHotKeys { namespace Daemon {
36 bool isEnabled()
38 KConfig khotkeysrc( KHOTKEYS_CONFIG_FILE );
39 KConfigGroup main( &khotkeysrc, "Main" );
40 return ! main.readEntry( "Disabled", false );
43 static QDBusInterface* Kded()
45 QDBusInterface *iface = new QDBusInterface( "org.kde.kded", "/kded","org.kde.kded" );
46 if (!iface->isValid())
48 QDBusError err = iface->lastError();
49 if (err.isValid())
51 kError() << "Failed to contact kded [" << err.name() << "]:" << err.message();
54 return iface;
58 bool isRunning()
60 std::auto_ptr<QDBusInterface> kded( Kded() );
61 if (!kded->isValid())
63 return false;
66 // I started with checking if i could get a valid /KHotKeys Interface. But
67 // it resisted to work. So lets do the other thing.
68 QDBusReply<QStringList> modules = kded->call( "loadedModules" );
69 return modules.value().contains("khotkeys");
73 bool reload()
75 // No kded no reload
76 std::auto_ptr<QDBusInterface> kded( Kded() );
77 if (!kded->isValid())
79 return false;
82 // Inform kdedkhotkeys demon to reload settings
83 QDBusConnection bus = QDBusConnection::sessionBus();
84 QDBusInterface iface(
85 "org.kde.kded",
86 "/modules/khotkeys",
87 "org.kde.khotkeys",
88 bus );
89 if(!iface.isValid())
91 QDBusError err = iface.lastError();
92 if (err.isValid())
94 kError() << err.name() << ":" << err.message();
96 return start();
99 QDBusMessage reply = iface.call("reread_configuration");
100 QDBusError err = iface.lastError();
101 if (err.isValid())
103 kError() << err.name() << ":" << err.message();
104 return false;
107 return true;
111 bool start()
113 std::auto_ptr<QDBusInterface> kded( Kded() );
114 if (!kded->isValid())
116 return false;
118 QDBusReply<bool> reply = kded->call( "loadModule", "khotkeys" );
119 QDBusError err = reply.error();
121 if (err.isValid())
123 kError() << "Unable to start server org.kde.khotkeys (kded module) ["
124 << err.name() << "]:" << err.message();
125 return false;
128 Q_ASSERT( reply.isValid() );
130 if ( reply.value() )
132 return true;
134 else
136 kError() << "Unable to start server org.kde.khotkeys (kded module)";
137 return false;
142 bool stop()
144 if (!isRunning())
146 return true;
149 std::auto_ptr<QDBusInterface> kded( Kded() );
150 if (!kded->isValid())
152 return false;
155 QDBusReply<bool> reply = kded->call( "unloadModule", "khotkeys" );
156 QDBusError err = reply.error();
158 if (err.isValid())
161 kError() << "Error when stopping khotkeys kded module [" << err.name() << "]:" << err.message();
162 return false;
165 Q_ASSERT( reply.isValid() );
167 if ( reply.value() )
169 return true;
171 else
173 kError() << "Failed to stop server org.kde.khotkeys (kded module)";
174 return false;
178 }} // namespace KHotKeys::Daemon