From 7465902216242489bc8c7f7d811fddc65bb1126b Mon Sep 17 00:00:00 2001 From: Frank Osterfeld Date: Mon, 7 May 2012 16:56:52 +0200 Subject: [PATCH] Introduce DeletePasswordJob to delete passwords. Creating a WritePasswordJob with no password data does the same, but that makes rather cryptic API. --- keychain.cpp | 32 ++++++++++++++++++++++++++++++++ keychain.h | 19 ++++++++++++++++++- keychain_p.h | 11 +++++++++++ testclient.cpp | 2 +- 4 files changed, 62 insertions(+), 2 deletions(-) diff --git a/keychain.cpp b/keychain.cpp index 8ed4dab..3f275ee 100644 --- a/keychain.cpp +++ b/keychain.cpp @@ -131,3 +131,35 @@ void WritePasswordJob::setTextData( const QString& data ) { void WritePasswordJob::doStart() { d->doStart(); } + +DeletePasswordJob::DeletePasswordJob( const QString& service, QObject* parent ) + : Job( service, parent ) + , d( new Private( this ) ) { +} + +DeletePasswordJob::~DeletePasswordJob() { + delete d; +} + +void DeletePasswordJob::doStart() { + //Internally, to delete a password we just execute a write job with no data set (null byte array). + //In all current implementations, this deletes the entry so this is sufficient + WritePasswordJob* job = new WritePasswordJob( service(), this ); + connect( job, SIGNAL(finished(QKeychain::Job*)), d, SLOT(jobFinished(QKeychain::Job*)) ); + job->setKey( d->key ); + job->start(); +} + +QString DeletePasswordJob::key() const { + return d->key; +} + +void DeletePasswordJob::setKey( const QString& key ) { + d->key = key; +} + +void DeletePasswordJob::Private::jobFinished( Job* job ) { + q->setError( job->error() ); + q->setErrorString( job->errorString() ); + q->emitFinished(); +} diff --git a/keychain.h b/keychain.h index 5ddb0bb..82d0bd2 100644 --- a/keychain.h +++ b/keychain.h @@ -107,6 +107,23 @@ private: Private* const d; }; -} +class QKEYCHAIN_EXPORT DeletePasswordJob : public Job { + Q_OBJECT +public: + explicit DeletePasswordJob( const QString& service, QObject* parent=0 ); + ~DeletePasswordJob(); + + QString key() const; + void setKey( const QString& key ); + +protected: + void doStart(); + +private: + class Private; + Private* const d; +}; + +} // namespace QtKeychain #endif diff --git a/keychain_p.h b/keychain_p.h index bac2b99..a7ae1cb 100644 --- a/keychain_p.h +++ b/keychain_p.h @@ -60,6 +60,17 @@ public: QString textData; }; +class DeletePasswordJob::Private : public QObject { + Q_OBJECT +public: + explicit Private( DeletePasswordJob* qq ) : q( qq ) {} + void doStart(); + DeletePasswordJob* const q; + QString key; +private Q_SLOTS: + void jobFinished( QKeychain::Job* ); +}; + } #endif // KEYCHAIN_P_H diff --git a/testclient.cpp b/testclient.cpp index 70e923a..b381130 100644 --- a/testclient.cpp +++ b/testclient.cpp @@ -78,7 +78,7 @@ int main( int argc, char** argv ) { const QString acc = *it; if ( ++it != args.constEnd() ) return printUsage(); - WritePasswordJob job( QLatin1String("qtkeychain-testclient") ); + DeletePasswordJob job( QLatin1String("qtkeychain-testclient") ); job.setAutoDelete( false ); job.setKey( acc ); QEventLoop loop;