1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-08-03 20:57:52 +02:00

Also write credentials to QtKeychain through CredentialsManager.

This commit is contained in:
Teo Mrnjavac
2013-05-22 19:36:34 +02:00
parent 29c8e6ed97
commit c86edcd31a
2 changed files with 43 additions and 12 deletions

View File

@@ -24,6 +24,8 @@
#include <QStringList> #include <QStringList>
#define TOMAHAWK_KEYCHAINSVC QLatin1String("tomahawkaccounts")
namespace Tomahawk namespace Tomahawk
{ {
@@ -43,7 +45,7 @@ CredentialsManager::loadCredentials( QStringList keys )
{ {
foreach ( QString key, keys ) foreach ( QString key, keys )
{ {
QKeychain::ReadPasswordJob* j = new QKeychain::ReadPasswordJob( QLatin1String( "tomahawkaccounts" ), this ); QKeychain::ReadPasswordJob* j = new QKeychain::ReadPasswordJob( TOMAHAWK_KEYCHAINSVC, this );
j->setKey( key ); j->setKey( key );
j->setAutoDelete( false ); j->setAutoDelete( false );
#if defined( Q_OS_UNIX ) && !defined( Q_OS_MAC ) #if defined( Q_OS_UNIX ) && !defined( Q_OS_MAC )
@@ -76,18 +78,46 @@ CredentialsManager::credentials( const QString& key ) const
void void
CredentialsManager::setCredentials( const QString& key, const QVariantHash& value ) CredentialsManager::setCredentials( const QString& key, const QVariantHash& value )
{ {
QMutexLocker locker( &m_mutex );
QKeychain::Job* j;
if ( value.isEmpty() ) if ( value.isEmpty() )
{ {
if ( !m_credentials.contains( key ) ) //if we don't have any credentials for this key, we bail
return;
m_credentials.remove( key ); m_credentials.remove( key );
//TODO: delete job QKeychain::DeletePasswordJob* dj = new QKeychain::DeletePasswordJob( TOMAHAWK_KEYCHAINSVC, this );
dj->setKey( key );
j = dj;
} }
else else
{ {
if ( value == m_credentials.value( key ) ) //if the credentials haven't actually changed, we bail
return;
m_credentials.insert( key, value ); m_credentials.insert( key, value );
//TODO: write job QByteArray data;
{
QDataStream ds( &data, QIODevice::WriteOnly );
ds << value;
}
QKeychain::WritePasswordJob* wj = new QKeychain::WritePasswordJob( TOMAHAWK_KEYCHAINSVC, this );
wj->setKey( key );
wj->setBinaryData( data );
j = wj;
} }
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* ) ) );
j->start();
} }
@@ -113,14 +143,14 @@ CredentialsManager::keychainJobFinished( QKeychain::Job* j )
emit ready(); emit ready();
} }
} }
} else if ( QKeychain::WritePasswordJob* writeJob = qobject_cast< QKeychain::WritePasswordJob* >( j ) )
else if ( QKeychain::WritePasswordJob* writeJob = qobject_cast< QKeychain::WritePasswordJob* >( j ) ) {
{ tLog() << Q_FUNC_INFO << "QtKeychain writeJob for" << writeJob->key() << "finished";
tLog() << Q_FUNC_INFO << "QtKeychain writeJob finished"; }
} else if ( QKeychain::DeletePasswordJob* deleteJob = qobject_cast< QKeychain::DeletePasswordJob* >( j ) )
else if ( QKeychain::DeletePasswordJob* deleteJob = qobject_cast< QKeychain::DeletePasswordJob* >( j ) ) {
{ tLog() << Q_FUNC_INFO << "QtKeychain deleteJob for" << deleteJob->key() << "finished";
tLog() << Q_FUNC_INFO << "QtKeychain deleteJob finished"; }
} }
else else
{ {

View File

@@ -21,13 +21,13 @@
#include <QObject> #include <QObject>
#include <QVariantHash> #include <QVariantHash>
#include <QMutex>
namespace QKeychain namespace QKeychain
{ {
class Job; class Job;
class ReadPasswordJob; class ReadPasswordJob;
class WritePasswordJob;
} }
@@ -69,6 +69,7 @@ private slots:
private: private:
QHash< QString, QVariantHash > m_credentials; QHash< QString, QVariantHash > m_credentials;
QList< QKeychain::ReadPasswordJob* > m_readJobs; QList< QKeychain::ReadPasswordJob* > m_readJobs;
QMutex m_mutex;
}; };
} //namespace Accounts } //namespace Accounts