KIO
kpasswdserver.cpp
Go to the documentation of this file.
00001 /* 00002 * This file is part of the KDE libraries 00003 * Copyright (c) 2009 Michael Leupold <lemma@confuego.org> 00004 * 00005 * This library is free software; you can redistribute it and/or 00006 * modify it under the terms of the GNU Lesser General Public 00007 * License as published by the Free Software Foundation; either 00008 * version 2.1 of the License, or (at your option) version 3, or any 00009 * later version accepted by the membership of KDE e.V. (or its 00010 * successor approved by the membership of KDE e.V.), which shall 00011 * act as a proxy defined in Section 6 of version 3 of the license. 00012 * 00013 * This library is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 * Lesser General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU Lesser General Public 00019 * License along with this library. If not, see <http://www.gnu.org/licenses/>. 00020 */ 00021 00022 #include "kpasswdserver_p.h" 00023 00024 #include <kio/authinfo.h> 00025 #include <QtCore/QByteArray> 00026 #include <QtCore/QEventLoop> 00027 #include <kdebug.h> 00028 00029 #include "kpasswdserverloop_p.h" 00030 #include "kpasswdserver_interface.h" 00031 00032 namespace KIO 00033 { 00034 00035 KPasswdServer::KPasswdServer() 00036 : m_interface(new OrgKdeKPasswdServerInterface("org.kde.kded", 00037 "/modules/kpasswdserver", 00038 QDBusConnection::sessionBus())) 00039 { 00040 } 00041 00042 KPasswdServer::~KPasswdServer() 00043 { 00044 delete m_interface; 00045 } 00046 00047 bool KPasswdServer::checkAuthInfo(KIO::AuthInfo &info, qlonglong windowId, 00048 qlonglong usertime) 00049 { 00050 kDebug(7019) << "window-id=" << windowId << "url=" << info.url; 00051 00052 // special handling for kioslaves which aren't QCoreApplications 00053 if (!QCoreApplication::instance()) { 00054 kWarning(7019) << "kioslave is not a QCoreApplication!"; 00055 return legacyCheckAuthInfo(info, windowId, usertime); 00056 } 00057 00058 // create the loop for waiting for a result before sending the request 00059 KPasswdServerLoop loop; 00060 QObject::connect(m_interface, SIGNAL(checkAuthInfoAsyncResult(qlonglong, qlonglong, const KIO::AuthInfo &)), 00061 &loop, SLOT(slotQueryResult(qlonglong, qlonglong, const KIO::AuthInfo &))); 00062 00063 QDBusReply<qlonglong> reply = m_interface->checkAuthInfoAsync(info, windowId, 00064 usertime); 00065 if (!reply.isValid()) { 00066 if (reply.error().type() == QDBusError::UnknownMethod) { 00067 if (legacyCheckAuthInfo(info, windowId, usertime)) { 00068 return true; 00069 } 00070 } 00071 00072 kWarning(7019) << "Can't communicate with kded_kpasswdserver (for checkAuthInfo)!"; 00073 kDebug(7019) << reply.error().name() << reply.error().message(); 00074 return false; 00075 } 00076 00077 if (!loop.waitForResult(reply.value())) { 00078 kWarning(7019) << "kded_kpasswdserver died while waiting for reply!"; 00079 return false; 00080 } 00081 00082 if (loop.authInfo().isModified()) { 00083 kDebug(7019) << "username=" << info.username << "password=[hidden]"; 00084 info = loop.authInfo(); 00085 return true; 00086 } 00087 00088 return false; 00089 } 00090 00091 bool KPasswdServer::legacyCheckAuthInfo(KIO::AuthInfo &info, qlonglong windowId, 00092 qlonglong usertime) 00093 { 00094 kWarning(7019) << "Querying old kded_kpasswdserver."; 00095 00096 QByteArray params; 00097 QDataStream stream(¶ms, QIODevice::WriteOnly); 00098 stream << info; 00099 QDBusReply<QByteArray> reply = m_interface->checkAuthInfo(params, windowId, 00100 usertime); 00101 if (reply.isValid()) { 00102 AuthInfo authResult; 00103 QDataStream stream2(reply.value()); 00104 stream2 >> authResult; 00105 if (authResult.isModified()) { 00106 info = authResult; 00107 return true; 00108 } 00109 } 00110 return false; 00111 } 00112 00113 qlonglong KPasswdServer::queryAuthInfo(KIO::AuthInfo &info, const QString &errorMsg, 00114 qlonglong windowId, qlonglong seqNr, 00115 qlonglong usertime) 00116 { 00117 kDebug(7019) << "window-id=" << windowId; 00118 00119 // special handling for kioslaves which aren't QCoreApplications 00120 if (!QCoreApplication::instance()) { 00121 kWarning(7019) << "kioslave is not a QCoreApplication!"; 00122 return legacyQueryAuthInfo(info, errorMsg, windowId, seqNr, usertime); 00123 } 00124 00125 // create the loop for waiting for a result before sending the request 00126 KPasswdServerLoop loop; 00127 QObject::connect(m_interface, SIGNAL(queryAuthInfoAsyncResult(qlonglong, qlonglong, const KIO::AuthInfo &)), 00128 &loop, SLOT(slotQueryResult(qlonglong, qlonglong, const KIO::AuthInfo &))); 00129 00130 QDBusReply<qlonglong> reply = m_interface->queryAuthInfoAsync(info, errorMsg, 00131 windowId, seqNr, 00132 usertime); 00133 if (!reply.isValid()) { 00134 // backwards compatibility for old kpasswdserver 00135 if (reply.error().type() == QDBusError::UnknownMethod) { 00136 qlonglong res = legacyQueryAuthInfo(info, errorMsg, windowId, seqNr, 00137 usertime); 00138 if (res > 0) { 00139 return res; 00140 } 00141 } 00142 00143 kWarning(7019) << "Can't communicate with kded_kpasswdserver (for queryAuthInfo)!"; 00144 kDebug(7019) << reply.error().name() << reply.error().message(); 00145 return -1; 00146 } 00147 00148 if (!loop.waitForResult(reply.value())) { 00149 kWarning(7019) << "kded_kpasswdserver died while waiting for reply!"; 00150 return -1; 00151 } 00152 00153 info = loop.authInfo(); 00154 00155 kDebug(7019) << "username=" << info.username << "password=[hidden]"; 00156 00157 return loop.seqNr(); 00158 } 00159 00160 qlonglong KPasswdServer::legacyQueryAuthInfo(KIO::AuthInfo &info, const QString &errorMsg, 00161 qlonglong windowId, qlonglong seqNr, 00162 qlonglong usertime) 00163 { 00164 kWarning(7019) << "Querying old kded_kpasswdserver."; 00165 00166 QByteArray params; 00167 QDataStream stream(¶ms, QIODevice::WriteOnly); 00168 stream << info; 00169 QDBusPendingReply<QByteArray, qlonglong> reply = m_interface->queryAuthInfo(params, errorMsg, 00170 windowId, seqNr, 00171 usertime); 00172 reply.waitForFinished(); 00173 if (reply.isValid()) { 00174 AuthInfo authResult; 00175 QDataStream stream2(reply.argumentAt<0>()); 00176 stream2 >> authResult; 00177 if (authResult.isModified()) { 00178 info = authResult; 00179 } 00180 return reply.argumentAt<1>(); 00181 } 00182 return -1; 00183 } 00184 00185 void KPasswdServer::addAuthInfo(const KIO::AuthInfo &info, qlonglong windowId) 00186 { 00187 QDBusReply<void> reply = m_interface->addAuthInfo(info, windowId); 00188 if (!reply.isValid() && reply.error().type() == QDBusError::UnknownMethod) { 00189 legacyAddAuthInfo(info, windowId); 00190 } 00191 } 00192 00193 void KPasswdServer::legacyAddAuthInfo(const KIO::AuthInfo &info, qlonglong windowId) 00194 { 00195 kWarning(7019) << "Querying old kded_kpasswdserver."; 00196 00197 QByteArray params; 00198 QDataStream stream(¶ms, QIODevice::WriteOnly); 00199 stream << info; 00200 m_interface->addAuthInfo(params, windowId); 00201 } 00202 00203 void KPasswdServer::removeAuthInfo(const QString &host, const QString &protocol, 00204 const QString &user) 00205 { 00206 m_interface->removeAuthInfo(host, protocol, user); 00207 } 00208 00209 }
KDE 4.6 API Reference