~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to khotkeys/libkhotkeysprivate/daemon/daemon.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2008 Michael Jansen <kde@michael-jansen.biz>
 
2
 
 
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.
 
7
 
 
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.
 
12
 
 
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.
 
17
*/
 
18
 
 
19
#include "daemon.h"
 
20
 
 
21
#include <memory>
 
22
 
 
23
#include "khotkeysglobal.h"
 
24
 
 
25
#include <QtDBus/QtDBus>
 
26
 
 
27
#include <KDE/KConfigGroup>
 
28
#include <KDE/KConfig>
 
29
#include "KDE/KDebug"
 
30
#include "KDE/KLocale"
 
31
 
 
32
 
 
33
namespace KHotKeys { namespace Daemon {
 
34
 
 
35
 
 
36
bool isEnabled()
 
37
    {
 
38
    KConfig khotkeysrc( KHOTKEYS_CONFIG_FILE );
 
39
    KConfigGroup main( &khotkeysrc, "Main" );
 
40
    return ! main.readEntry( "Disabled", false );
 
41
    }
 
42
 
 
43
static QDBusInterface* Kded()
 
44
    {
 
45
    QDBusInterface *iface = new QDBusInterface( "org.kde.kded", "/kded","org.kde.kded" );
 
46
    if (!iface->isValid())
 
47
        {
 
48
        QDBusError err = iface->lastError();
 
49
        if (err.isValid())
 
50
            {
 
51
            kError() << "Failed to contact kded [" << err.name() << "]:" << err.message();
 
52
            }
 
53
        }
 
54
    return iface;
 
55
    }
 
56
 
 
57
 
 
58
bool isRunning()
 
59
    {
 
60
    std::auto_ptr<QDBusInterface> kded( Kded() );
 
61
    if (!kded->isValid())
 
62
        {
 
63
        return false;
 
64
        }
 
65
 
 
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");
 
70
    }
 
71
 
 
72
 
 
73
bool reload()
 
74
    {
 
75
    // No kded no reload
 
76
    std::auto_ptr<QDBusInterface> kded( Kded() );
 
77
    if (!kded->isValid())
 
78
        {
 
79
        return false;
 
80
        }
 
81
 
 
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())
 
90
        {
 
91
        QDBusError err = iface.lastError();
 
92
        if (err.isValid())
 
93
            {
 
94
            kError() << err.name() << ":" << err.message();
 
95
            }
 
96
        return start();
 
97
        }
 
98
 
 
99
    QDBusMessage reply = iface.call("reread_configuration");
 
100
    QDBusError err = iface.lastError();
 
101
    if (err.isValid())
 
102
        {
 
103
        kError() << err.name() << ":" << err.message();
 
104
        return false;
 
105
        }
 
106
 
 
107
    return true;
 
108
    }
 
109
 
 
110
 
 
111
bool start()
 
112
    {
 
113
    std::auto_ptr<QDBusInterface> kded( Kded() );
 
114
    if (!kded->isValid())
 
115
        {
 
116
        return false;
 
117
        }
 
118
    QDBusReply<bool> reply = kded->call( "loadModule", "khotkeys"  );
 
119
    QDBusError err = reply.error();
 
120
 
 
121
    if (err.isValid())
 
122
        {
 
123
        kError() << "Unable to start server org.kde.khotkeys (kded module) [" 
 
124
                 << err.name() << "]:" << err.message();
 
125
        return false;
 
126
        }
 
127
 
 
128
    Q_ASSERT( reply.isValid() );
 
129
 
 
130
    if ( reply.value() )
 
131
        {
 
132
        return true;
 
133
        }
 
134
    else
 
135
        {
 
136
        kError() << "Unable to start server org.kde.khotkeys (kded module)";
 
137
        return false;
 
138
        }
 
139
    }
 
140
 
 
141
 
 
142
bool stop()
 
143
    {
 
144
    if (!isRunning())
 
145
        {
 
146
        return true;
 
147
        }
 
148
 
 
149
    std::auto_ptr<QDBusInterface> kded( Kded() );
 
150
    if (!kded->isValid())
 
151
        {
 
152
        return false;
 
153
        }
 
154
 
 
155
    QDBusReply<bool> reply = kded->call( "unloadModule", "khotkeys"  );
 
156
    QDBusError err = reply.error();
 
157
 
 
158
    if (err.isValid())
 
159
        {
 
160
 
 
161
        kError() << "Error when stopping khotkeys kded module [" << err.name() << "]:" << err.message();
 
162
        return false;
 
163
        }
 
164
 
 
165
    Q_ASSERT( reply.isValid() );
 
166
 
 
167
    if ( reply.value() )
 
168
        {
 
169
        return true;
 
170
        }
 
171
    else
 
172
        {
 
173
        kError() << "Failed to stop server org.kde.khotkeys (kded module)";
 
174
        return false;
 
175
        }
 
176
    }
 
177
 
 
178
}} // namespace KHotKeys::Daemon