signon  8.40
signonauthsession.cpp
Go to the documentation of this file.
00001 /*
00002  * This file is part of signon
00003  *
00004  * Copyright (C) 2009-2010 Nokia Corporation.
00005  *
00006  * Conta Alberto Mardegan <alberto.mardegan@canonical.com>
00007  *
00008  * This library is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Lesser General Public License
00010  * version 2.1 as published by the Free Software Foundation.
00011  *
00012  * This library is distributed in the hope that it will be useful, but
00013  * WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
00020  * 02110-1301 USA
00021  */
00022 
00023 #include "signond-common.h"
00024 #include "signonauthsession.h"
00025 #include "signonauthsessionadaptor.h"
00026 
00027 using namespace SignonDaemonNS;
00028 
00029 SignonAuthSession::SignonAuthSession(quint32 id,
00030                                      const QString &method,
00031                                      pid_t ownerPid):
00032     m_id(id),
00033     m_method(method),
00034     m_registered(false),
00035     m_ownerPid(ownerPid)
00036 {
00037     TRACE();
00038 
00039     static quint32 incr = 0;
00040     QString objectName = SIGNOND_DAEMON_OBJECTPATH +
00041         QLatin1String("/AuthSession_") + QString::number(incr++, 16);
00042     TRACE() << objectName;
00043 
00044     setObjectName(objectName);
00045 }
00046 
00047 SignonAuthSession::~SignonAuthSession()
00048 {
00049     TRACE();
00050 
00051     if (m_registered)
00052     {
00053         emit unregistered();
00054         QDBusConnection connection(SIGNOND_BUS);
00055         connection.unregisterObject(objectName());
00056     }
00057 }
00058 
00059 QString SignonAuthSession::getAuthSessionObjectPath(const quint32 id,
00060                                                     const QString &method,
00061                                                     SignonDaemon *parent,
00062                                                     bool &supportsAuthMethod,
00063                                                     pid_t ownerPid)
00064 {
00065     TRACE();
00066     supportsAuthMethod = true;
00067     SignonAuthSession* sas = new SignonAuthSession(id, method, ownerPid);
00068 
00069     QDBusConnection connection(SIGNOND_BUS);
00070     if (!connection.isConnected()) {
00071         TRACE() << "Cannot get DBUS object connected";
00072         delete sas;
00073         return QString();
00074     }
00075 
00076     (void)new SignonAuthSessionAdaptor(sas);
00077     QString objectName = sas->objectName();
00078     if (!connection.registerObject(sas->objectName(), sas,
00079                                    QDBusConnection::ExportAdaptors)) {
00080         TRACE() << "Object cannot be registered: " << objectName;
00081         delete sas;
00082         return QString();
00083     }
00084 
00085     SignonSessionCore *core = SignonSessionCore::sessionCore(id, method, parent);
00086     if (!core) {
00087         TRACE() << "Cannot retrieve proper tasks queue";
00088         supportsAuthMethod = false;
00089         delete sas;
00090         return QString();
00091     }
00092 
00093     sas->objectRegistered();
00094     sas->setParent(core);
00095 
00096     connect(core, SIGNAL(stateChanged(const QString&, int, const QString&)),
00097             sas, SLOT(stateChangedSlot(const QString&, int, const QString&)));
00098 
00099     TRACE() << "SignonAuthSession is created successfully: " << objectName;
00100     return objectName;
00101 }
00102 
00103 void SignonAuthSession::stopAllAuthSessions()
00104 {
00105     SignonSessionCore::stopAllAuthSessions();
00106 }
00107 
00108 quint32 SignonAuthSession::id() const
00109 {
00110     return m_id;
00111 }
00112 
00113 QString SignonAuthSession::method() const
00114 {
00115     return m_method;
00116 }
00117 
00118 pid_t SignonAuthSession::ownerPid() const
00119 {
00120     return m_ownerPid;
00121 }
00122 
00123 QStringList
00124 SignonAuthSession::queryAvailableMechanisms(const QStringList &wantedMechanisms)
00125 {
00126     return parent()->queryAvailableMechanisms(wantedMechanisms);
00127 }
00128 
00129 QVariantMap SignonAuthSession::process(const QVariantMap &sessionDataVa,
00130                                        const QString &mechanism)
00131 {
00132     setDelayedReply(true);
00133     parent()->process(connection(),
00134                       message(),
00135                       sessionDataVa,
00136                       mechanism,
00137                       objectName());
00138     return QVariantMap();
00139 }
00140 
00141 void SignonAuthSession::cancel()
00142 {
00143     TRACE();
00144     parent()->cancel(objectName());
00145 }
00146 
00147 void SignonAuthSession::setId(quint32 id)
00148 {
00149     m_id = id;
00150     parent()->setId(id);
00151 }
00152 
00153 void SignonAuthSession::objectUnref()
00154 {
00155     //TODO - remove the `objectUnref` functionality from the DBus API
00156     TRACE();
00157     cancel();
00158 
00159     if (m_registered) {
00160         QDBusConnection connection(SIGNOND_BUS);
00161         connection.unregisterObject(objectName());
00162         m_registered = false;
00163     }
00164 
00165     deleteLater();
00166 }
00167 
00168 void SignonAuthSession::stateChangedSlot(const QString &sessionKey,
00169                                          int state,
00170                                          const QString &message)
00171 {
00172     TRACE();
00173 
00174     if (sessionKey == objectName())
00175         emit stateChanged(state, message);
00176 }
00177 
00178 void SignonAuthSession::objectRegistered()
00179 {
00180     m_registered = true;
00181 }