1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-07-31 19:30:21 +02:00

Added CredentialsManager as a QtKeychain credentials cache.

This commit is contained in:
Teo Mrnjavac
2013-05-22 16:15:25 +02:00
parent 7e947cc135
commit f47d8ddf74
5 changed files with 211 additions and 3 deletions

View File

@@ -204,6 +204,7 @@ list(APPEND libSources
accounts/Account.cpp
accounts/AccountModel.cpp
accounts/AccountModelFilterProxy.cpp
accounts/CredentialsManager.cpp
accounts/ResolverAccount.cpp
accounts/AccountDelegate.cpp
accounts/DelegateConfigWrapper.cpp

View File

@@ -18,6 +18,8 @@
*/
#include "AccountManager.h"
#include "CredentialsManager.h"
#include "config.h"
#include "SourceList.h"
#include "TomahawkSettings.h"
@@ -33,6 +35,7 @@
#include <QtCore/QCoreApplication>
#include <QTimer>
namespace Tomahawk
{
@@ -86,7 +89,7 @@ AccountManager::init()
m_accountFactories[ f->factoryId() ] = f;
registerAccountFactoryForFilesystem( f );
emit ready();
emit ready(); //Notifies TomahawkApp to load the remaining AccountFactories, then Accounts from config
}
@@ -276,7 +279,22 @@ void
AccountManager::loadFromConfig()
{
QStringList accountIds = TomahawkSettings::instance()->accounts();
qDebug() << "LOADING ALL CREDENTIALS" << accountIds;
m_creds = new CredentialsManager( this );
connect( m_creds, SIGNAL( ready() ),
this, SLOT( finishLoadingFromConfig() ) );
m_creds->loadCredentials( accountIds );
}
void
AccountManager::finishLoadingFromConfig()
{
QStringList accountIds = m_creds->keys();
qDebug() << "LOADING ALL ACCOUNTS" << accountIds;
foreach ( const QString& accountId, accountIds )
{
QString pluginFactory = factoryFromId( accountId );
@@ -498,6 +516,6 @@ AccountManager::onStateChanged( Account::ConnectionState state )
}
};
}
};
}

View File

@@ -34,6 +34,8 @@ namespace Tomahawk
namespace Accounts
{
class CredentialsManager;
class DLLEXPORT AccountManager : public QObject
{
Q_OBJECT
@@ -105,6 +107,7 @@ private slots:
void init();
void onStateChanged( Tomahawk::Accounts::Account::ConnectionState state );
void onError( int code, const QString& msg );
void finishLoadingFromConfig();
void onSettingsChanged();
@@ -117,6 +120,8 @@ private:
Account* loadPlugin( const QString &accountId );
void hookupAccount( Account* ) const;
CredentialsManager* m_creds;
QList< Account* > m_accounts;
QList< Account* > m_enabledAccounts;
QList< Account* > m_connectedAccounts;

View File

@@ -0,0 +1,110 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright 2013, Teo Mrnjavac <teo@kde.org>
*
* Tomahawk is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Tomahawk is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
*/
#include "CredentialsManager.h"
#include "utils/Logger.h"
#include <qtkeychain/keychain.h>
#include <QStringList>
namespace Tomahawk
{
namespace Accounts
{
CredentialsManager::CredentialsManager( QObject* parent )
: QObject( parent )
{
}
void
CredentialsManager::loadCredentials( QStringList keys )
{
foreach ( QString key, keys )
{
QKeychain::ReadPasswordJob* j = new QKeychain::ReadPasswordJob( QLatin1String( "tomahawkaccounts" ), this );
j->setKey( key );
j->setAutoDelete( false );
#if defined( Q_OS_UNIX ) && !defined( Q_OS_MAC )
j->setInsecureFallback( true );
#endif
connect( j, SIGNAL( finished( QKeychain::Job* ) ),
SLOT( keychainJobFinished( QKeychain::Job* ) ) );
m_readJobs << j;
j->start();
}
}
QStringList
CredentialsManager::keys() const
{
QStringList keys = m_credentials.keys();
return keys;
}
void
CredentialsManager::keychainJobFinished( QKeychain::Job* j )
{
if ( j->error() == QKeychain::NoError )
{
if ( QKeychain::ReadPasswordJob* readJob = qobject_cast< QKeychain::ReadPasswordJob* >( j ) )
{
tDebug() << "QtKeychain readJob for" << readJob->key() << "finished without errors";
QVariantHash creds;
QDataStream dataStream( readJob->binaryData() );
dataStream >> creds;
m_credentials.insert( readJob->key(), creds );
m_readJobs.removeAll( readJob );
if ( m_readJobs.isEmpty() )
{
emit ready();
}
}
}
else if ( QKeychain::WritePasswordJob* writeJob = qobject_cast< QKeychain::WritePasswordJob* >( j ) )
{
tLog() << Q_FUNC_INFO << "QtKeychain writeJob finished";
}
else if ( QKeychain::DeletePasswordJob* deleteJob = qobject_cast< QKeychain::DeletePasswordJob* >( j ) )
{
tLog() << Q_FUNC_INFO << "QtKeychain deleteJob finished";
}
else
{
tDebug() << "QtKeychain job finished with error:" << j->error() << j->errorString();
}
j->deleteLater();
}
} //namespace Accounts
} //namespace Tomahawk

View File

@@ -0,0 +1,74 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright 2013, Teo Mrnjavac <teo@kde.org>
*
* Tomahawk is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Tomahawk is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef CREDENTIALSMANAGER_H
#define CREDENTIALSMANAGER_H
#include <QObject>
#include <QVariantHash>
namespace QKeychain
{
class Job;
class ReadPasswordJob;
class WritePasswordJob;
}
namespace Tomahawk
{
namespace Accounts
{
/**
* @brief The CredentialsManager class holds an in-memory cache of whatever credentials are stored
* in the system's QtKeychain-accessible credentials storage.
* After instantiating the class, loadCredentials should be called, and this is the only time a read
* operation from QtKeychain is performed. When CredentialsManager emits ready(), it can be used for
* all other operations. The only QtKeychain operations performed at any time after startup are
* write and delete.
* This ensures an illusion of synchronous operations for Tomahawk's Account classes, even though all
* QtKeychain jobs are async.
*/
class CredentialsManager : public QObject
{
Q_OBJECT
public:
explicit CredentialsManager( QObject* parent = 0 );
void loadCredentials( QStringList keys );
QStringList keys() const;
signals:
void ready();
private slots:
void keychainJobFinished( QKeychain::Job* );
private:
QHash< QString, QVariantHash > m_credentials;
QList< QKeychain::ReadPasswordJob* > m_readJobs;
};
} //namespace Accounts
} //namespace Tomahawk
#endif // CREDENTIALSMANAGER_H