not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / kcontrol / kfontinst / kcmfontinst / PolicyKitAuthenticator.cpp
blob2d6424577bd4f63bd481d40639e3f7b610c95367
1 /*
2 * KFontInst - KDE Font Installer
4 * Copyright 2003-2008 Craig Drummond <craig@kde.org>
6 * ----
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; see the file COPYING. If not, write to
20 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
24 #include "PolicyKitAuthenticator.h"
25 #include <QtGui/QWidget>
26 #include <QtGui/QApplication>
27 #include <QtCore/QEventLoop>
28 #include <QtDBus/QDBusMessage>
29 #include <QtDBus/QDBusInterface>
30 #include <KDE/KGlobal>
31 #include <sys/types.h>
32 #include <unistd.h>
33 #include <set>
35 K_GLOBAL_STATIC(PolicyKitAuthenticator, theInstance)
37 struct PolicyKitAuthenticatorKey
39 PolicyKitAuthenticatorKey(const QString &a=QString(), uint p=0)
40 : action(a), pid(p) { }
42 bool operator<(const PolicyKitAuthenticatorKey &o) const
44 return pid<o.pid || (pid==o.pid && action<o.action);
47 QString action;
48 uint pid;
51 class PolicyKitAuthenticatorPrivate
53 public:
55 PolicyKitAuthenticatorPrivate()
56 : dbus("org.freedesktop.PolicyKit.AuthenticationAgent",
57 "/",
58 "org.freedesktop.PolicyKit.AuthenticationAgent",
59 QDBusConnection::sessionBus())
63 virtual ~PolicyKitAuthenticatorPrivate() { }
65 std::set<PolicyKitAuthenticatorKey> sucesses;
66 QDBusInterface dbus;
67 bool replyStatus;
70 PolicyKitAuthenticator::PolicyKitAuthenticator()
71 : d_ptr(new PolicyKitAuthenticatorPrivate)
75 PolicyKitAuthenticator::~PolicyKitAuthenticator()
77 delete d_ptr;
80 PolicyKitAuthenticator * PolicyKitAuthenticator::instance()
82 return theInstance;
85 bool PolicyKitAuthenticator::authenticate(const QString &action, QWidget *widet, bool gui)
87 return authenticate(action, widet ? widet->window()->winId() : 0, (uint)getpid(), gui);
90 bool PolicyKitAuthenticator::authenticate(const QString &action, uint winId, uint pid, bool gui)
92 static const int constDBusTimeout=20*60*1000; // 20mins
94 Q_D(PolicyKitAuthenticator);
95 PolicyKitAuthenticatorKey key(action, pid);
97 if(d->sucesses.end()!=d->sucesses.find(key))
98 return true;
100 QList<QVariant> args;
102 args << action << winId << pid;
104 QDBusMessage message=QDBusMessage::createMethodCall(d->dbus.service(), d->dbus.path(), d->dbus.interface(), "ObtainAuthorization");
105 message.setArguments(args);
107 if(gui)
109 QEventLoop eventLoop;
111 d->dbus.connection().callWithCallback(message, this, SLOT(reply(QDBusMessage)), SLOT(error(QDBusError, QDBusMessage)), constDBusTimeout);
112 connect(this, SIGNAL(quitLoop()), &eventLoop, SLOT(quit()));
113 d->replyStatus=false;
114 eventLoop.exec(QEventLoop::ExcludeUserInputEvents);
116 if(d->replyStatus)
118 d->sucesses.insert(key);
119 return true;
122 else if(isAuthenticated(d->dbus.connection().call(message, QDBus::Block, constDBusTimeout)))
124 d->sucesses.insert(key);
125 return true;
128 return false;
131 void PolicyKitAuthenticator::reply(const QDBusMessage &msg)
133 if(isAuthenticated(msg))
135 Q_D(PolicyKitAuthenticator);
136 d->replyStatus=true;
138 emit quitLoop();
141 void PolicyKitAuthenticator::error(const QDBusError &, const QDBusMessage &)
143 emit quitLoop();
146 bool PolicyKitAuthenticator::isAuthenticated(const QDBusMessage &msg)
148 QList<QVariant> args=msg.arguments();
150 return QDBusMessage::ReplyMessage==msg.type() && 1==args.count() &&
151 QVariant::Bool==args[0].type() && args[0].toBool();
154 #include "PolicyKitAuthenticator.moc"