2 * KFontInst - KDE Font Installer
4 * Copyright 2003-2008 Craig Drummond <craig@kde.org>
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>
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
);
51 class PolicyKitAuthenticatorPrivate
55 PolicyKitAuthenticatorPrivate()
56 : dbus("org.freedesktop.PolicyKit.AuthenticationAgent",
58 "org.freedesktop.PolicyKit.AuthenticationAgent",
59 QDBusConnection::sessionBus())
63 virtual ~PolicyKitAuthenticatorPrivate() { }
65 std::set
<PolicyKitAuthenticatorKey
> sucesses
;
70 PolicyKitAuthenticator::PolicyKitAuthenticator()
71 : d_ptr(new PolicyKitAuthenticatorPrivate
)
75 PolicyKitAuthenticator::~PolicyKitAuthenticator()
80 PolicyKitAuthenticator
* PolicyKitAuthenticator::instance()
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
))
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
);
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
);
118 d
->sucesses
.insert(key
);
122 else if(isAuthenticated(d
->dbus
.connection().call(message
, QDBus::Block
, constDBusTimeout
)))
124 d
->sucesses
.insert(key
);
131 void PolicyKitAuthenticator::reply(const QDBusMessage
&msg
)
133 if(isAuthenticated(msg
))
135 Q_D(PolicyKitAuthenticator
);
141 void PolicyKitAuthenticator::error(const QDBusError
&, const QDBusMessage
&)
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"