• Skip to content
  • Skip to link menu
KDE 4.3 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

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 <kio/authinfo.h>
00023 #include <QtCore/QByteArray>
00024 #include <QtCore/QEventLoop>
00025 #include <kdebug.h>
00026 
00027 #include "kpasswdserver_p.h"
00028 #include "kpasswdserverloop_p.h"
00029 #include "kpasswdserver_interface.h"
00030 
00031 namespace KIO
00032 {
00033 
00034 KPasswdServer::KPasswdServer()
00035     : m_interface(new OrgKdeKPasswdServerInterface("org.kde.kded",
00036                                                    "/modules/kpasswdserver",
00037                                                    QDBusConnection::sessionBus()))
00038 {
00039 }
00040 
00041 KPasswdServer::~KPasswdServer()
00042 {
00043     delete m_interface;
00044 }
00045 
00046 bool KPasswdServer::checkAuthInfo(KIO::AuthInfo &info, qlonglong windowId,
00047                                   qlonglong usertime)
00048 {
00049     kDebug(7019) << "window-id=" << windowId << "url=" << info.url;
00050 
00051     // special handling for kioslaves which aren't QCoreApplications
00052     if (!QCoreApplication::instance()) {
00053         kWarning(7019) << "kioslave is not a QCoreApplication!";
00054         return legacyCheckAuthInfo(info, windowId, usertime);
00055     }
00056     
00057     // create the loop for waiting for a result before sending the request
00058     KPasswdServerLoop loop;
00059     QObject::connect(m_interface, SIGNAL(checkAuthInfoAsyncResult(qlonglong, qlonglong, const KIO::AuthInfo &)),
00060                      &loop, SLOT(slotQueryResult(qlonglong, qlonglong, const KIO::AuthInfo &)));
00061             
00062     QDBusReply<qlonglong> reply = m_interface->checkAuthInfoAsync(info, windowId,
00063                                                                   usertime);
00064     if (!reply.isValid()) {
00065         if (reply.error().type() == QDBusError::UnknownMethod) {
00066             if (legacyCheckAuthInfo(info, windowId, usertime)) {
00067                 return true;
00068             }
00069         }
00070 
00071         kWarning(7019) << "Can't communicate with kded_kpasswdserver (for checkAuthInfo)!";
00072         kDebug(7019) << reply.error().name() << reply.error().message();
00073         return false;
00074     }
00075 
00076     if (!loop.waitForResult(reply.value())) {
00077         kWarning(7019) << "kded_kpasswdserver died while waiting for reply!";
00078         return false;
00079     }
00080 
00081     if (loop.authInfo().isModified()) {
00082         kDebug(7019) << "username=" << info.username << "password=[hidden]";
00083         info = loop.authInfo();
00084         return true;
00085     }
00086 
00087     return false;
00088 }
00089 
00090 bool KPasswdServer::legacyCheckAuthInfo(KIO::AuthInfo &info, qlonglong windowId,
00091                                              qlonglong usertime)
00092 {
00093     kWarning(7019) << "Querying old kded_kpasswdserver.";
00094     
00095     QByteArray params;
00096     QDataStream stream(&params, QIODevice::WriteOnly);
00097     stream << info;
00098     QDBusReply<QByteArray> reply = m_interface->checkAuthInfo(params, windowId,
00099                                                               usertime);
00100     if (reply.isValid()) {
00101         AuthInfo authResult;
00102         QDataStream stream2(reply.value());
00103         stream2 >> authResult;
00104         if (authResult.isModified()) {
00105             info = authResult;
00106             return true;
00107         }
00108     }
00109     return false;
00110 }
00111 
00112 qlonglong KPasswdServer::queryAuthInfo(KIO::AuthInfo &info, const QString &errorMsg,
00113                                        qlonglong windowId, qlonglong seqNr,
00114                                        qlonglong usertime)
00115 {
00116     kDebug(7019) << "window-id=" << windowId;
00117 
00118     // special handling for kioslaves which aren't QCoreApplications
00119     if (!QCoreApplication::instance()) {
00120         kWarning(7019) << "kioslave is not a QCoreApplication!";
00121         return legacyQueryAuthInfo(info, errorMsg, windowId, seqNr, usertime);
00122     }
00123     
00124     // create the loop for waiting for a result before sending the request
00125     KPasswdServerLoop loop;
00126     QObject::connect(m_interface, SIGNAL(queryAuthInfoAsyncResult(qlonglong, qlonglong, const KIO::AuthInfo &)),
00127                      &loop, SLOT(slotQueryResult(qlonglong, qlonglong, const KIO::AuthInfo &)));
00128 
00129     QDBusReply<qlonglong> reply = m_interface->queryAuthInfoAsync(info, errorMsg,
00130                                                                   windowId, seqNr,
00131                                                                   usertime);
00132     if (!reply.isValid()) {
00133         // backwards compatibility for old kpasswdserver
00134         if (reply.error().type() == QDBusError::UnknownMethod) {
00135             qlonglong res = legacyQueryAuthInfo(info, errorMsg, windowId, seqNr,
00136                                                 usertime);
00137             if (res > 0) {
00138                 return res;
00139             }
00140         }
00141 
00142         kWarning(7019) << "Can't communicate with kded_kpasswdserver (for queryAuthInfo)!";
00143         kDebug(7019) << reply.error().name() << reply.error().message();
00144         return -1;
00145     }
00146 
00147     if (!loop.waitForResult(reply.value())) {
00148         kWarning(7019) << "kded_kpasswdserver died while waiting for reply!";
00149         return -1;
00150     }
00151 
00152     info = loop.authInfo();
00153 
00154     kDebug(7019) << "username=" << info.username << "password=[hidden]";
00155 
00156     return loop.seqNr();
00157 }
00158 
00159 qlonglong KPasswdServer::legacyQueryAuthInfo(KIO::AuthInfo &info, const QString &errorMsg,
00160                                              qlonglong windowId, qlonglong seqNr,
00161                                              qlonglong usertime)
00162 {
00163     kWarning(7019) << "Querying old kded_kpasswdserver.";
00164     
00165     QByteArray params;
00166     QDataStream stream(&params, QIODevice::WriteOnly);
00167     stream << info;
00168     QDBusPendingReply<QByteArray, qlonglong> reply = m_interface->queryAuthInfo(params, errorMsg,
00169                                                                                 windowId, seqNr,
00170                                                                                 usertime);
00171     reply.waitForFinished();
00172     if (reply.isValid()) {
00173         AuthInfo authResult;
00174         QDataStream stream2(reply.argumentAt<0>());
00175         stream2 >> authResult;
00176         if (authResult.isModified()) {
00177             info = authResult;
00178         }
00179         return reply.argumentAt<1>();
00180     }
00181     return -1;
00182 }
00183 
00184 void KPasswdServer::addAuthInfo(const KIO::AuthInfo &info, qlonglong windowId)
00185 {
00186     QDBusReply<void> reply = m_interface->addAuthInfo(info, windowId);
00187     if (!reply.isValid() && reply.error().type() == QDBusError::UnknownMethod) {
00188         legacyAddAuthInfo(info, windowId);
00189     }
00190 }
00191 
00192 void KPasswdServer::legacyAddAuthInfo(const KIO::AuthInfo &info, qlonglong windowId)
00193 {
00194     kWarning(7019) << "Querying old kded_kpasswdserver.";
00195     
00196     QByteArray params;
00197     QDataStream stream(&params, QIODevice::WriteOnly);
00198     stream << info;
00199     m_interface->addAuthInfo(params, windowId);
00200 }
00201 
00202 void KPasswdServer::removeAuthInfo(const QString &host, const QString &protocol,
00203                                    const QString &user)
00204 {
00205     m_interface->removeAuthInfo(host, protocol, user);
00206 }
00207 
00208 }

KIO

Skip menu "KIO"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.6.1
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal