From 067c3c65777e1869a0ee1fc5c5820a38fb87ad2e Mon Sep 17 00:00:00 2001 From: Frank Osterfeld Date: Fri, 28 Oct 2011 09:38:02 +0200 Subject: [PATCH] add entryExists() --- keychain.cpp | 9 +++++++++ keychain.h | 12 ++++++++++-- keychain_mac.cpp | 22 ++++++++++++++++++++++ keychain_p.h | 4 +++- keychain_win.cpp | 39 +++++++++++++++++++++++++++++++++++---- lib.pro | 8 ++++++-- testclient.pro | 3 ++- 7 files changed, 87 insertions(+), 10 deletions(-) diff --git a/keychain.cpp b/keychain.cpp index caf3e37..2026470 100644 --- a/keychain.cpp +++ b/keychain.cpp @@ -60,6 +60,15 @@ QByteArray Keychain::readEntry( const QString& key ) { return pw; } +bool Keychain::entryExists( const QString& key ) { + QString err; + bool exists = false; + const Error ret = d->entryExistsImpl( &exists, key, &err ); + d->error = ret; + d->errorString = err; + return exists; +} + void Keychain::deleteEntry( const QString& key ) { QString err; const Error ret = d->deleteEntryImpl( key, &err ); diff --git a/keychain.h b/keychain.h index 70d071c..7d368c9 100644 --- a/keychain.h +++ b/keychain.h @@ -91,7 +91,7 @@ public: * Reads the password for a given @p key from the keychain. * error() and errorString() hold the result of the read operation. * - * @param key the key ot read the password for + * @param key the key to read the password for */ QString readPassword( const QString& key ); @@ -99,10 +99,18 @@ public: * Reads data for a given @p key from the keychain. * error() and errorString() hold the result of the read operation. * - * @param key the key ot read the password for + * @param key the key to read the password for */ QByteArray readEntry( const QString& key ); + /** + * Returns whether the keychain has an entry with key @p key + * error() and errorString() hold the result of the read operation. + * + * @param key the key to check for + */ + bool entryExists( const QString& key ); + /** * Deletes the data for a @p key from the keychain. * error() and errorString() hold the result of the delete operation. diff --git a/keychain_mac.cpp b/keychain_mac.cpp index e1c1fc5..b835f13 100644 --- a/keychain_mac.cpp +++ b/keychain_mac.cpp @@ -138,3 +138,25 @@ Keychain::Error Keychain::Private::deleteEntryImpl( const QString& account, return CouldNotDeleteEntry; } + +Keychain::Error Keychain::Private::entryExistsImpl( bool* exists, + const QString& account, + QString* err ) { + Q_ASSERT( exists ); + *exists = false; + SecKeychainItemRef ref; + QByteArray pw; + const OSStatus ret1 = readPw( &pw, service, account, &ref ); + if ( ret1 == errSecItemNotFound ) { + return NoError; + } + if ( ret1 != noErr ) { + *err = strForStatus( ret1 ); + //TODO map error code, set errstr + return OtherError; + } + + CFRelease( ref ); + *exists = true; + return NoError; +} diff --git a/keychain_p.h b/keychain_p.h index 4abb0a1..fe4c8bb 100644 --- a/keychain_p.h +++ b/keychain_p.h @@ -27,7 +27,9 @@ public: Keychain::Error readEntryImpl( QByteArray* password, const QString& account, QString* errorString ); - + Keychain::Error entryExistsImpl( bool* exists, + const QString& key, + QString* errorString ); const QString service; Keychain::Error error; QString errorString; diff --git a/keychain_win.cpp b/keychain_win.cpp index fc781d3..1f8fd5b 100644 --- a/keychain_win.cpp +++ b/keychain_win.cpp @@ -8,11 +8,42 @@ *****************************************************************************/ #include "keychain_p.h" -QString Keychain::Private::readPasswordImpl( const QString& account ) const { - throw KeychainException( QLatin1String("not implemented") ); +using namespace QKeychain; + +Keychain::Error Keychain::Private::readEntryImpl( QByteArray* pw, + const QString& account, + QString* err ) { + Q_ASSERT( pw ); + Q_ASSERT( err ); + err->clear(); + *err = tr("Not implemented"); + return OtherError; } -void Keychain::Private::writePasswordImpl( const QString& account, const QString& password ) { - throw KeychainException( QLatin1String("not implemented") ); +Keychain::Error Keychain::Private::writeEntryImpl( const QString& account, + const QByteArray& data, + QString* err ) { + Q_ASSERT( err ); + err->clear(); + *err = tr("Not implemented"); + return OtherError; } +Keychain::Error Keychain::Private::deleteEntryImpl( const QString& account, + QString* err ) { + Q_ASSERT( err ); + err->clear(); + *err = tr("Not implemented"); + return OtherError; +} + + +Keychain::Error Keychain::Private::entryExistsImpl( bool* exists, + const QString& account, + QString* err ) { + Q_ASSERT( exists ); + Q_ASSERT( err ); + err->clear(); + *err = tr("Not implemented"); + return OtherError; +} diff --git a/lib.pro b/lib.pro index 3a6ffdf..b631a7b 100644 --- a/lib.pro +++ b/lib.pro @@ -7,9 +7,13 @@ HEADERS += keychain.h \ keychain_p.h SOURCES += keychain.cpp + macx { LIBS += -framework Security -framework CoreFoundation SOURCES += keychain_mac.cpp } -win32:SOURCES += keychain_win.cpp - +win32 { + DESTDIR = lib + DLLDESTDIR = lib + SOURCES += keychain_win.cpp +} diff --git a/testclient.pro b/testclient.pro index 912fdb1..ce8cebf 100644 --- a/testclient.pro +++ b/testclient.pro @@ -7,5 +7,6 @@ QT -= gui CONFIG += console macx:CONFIG -= app_bundle -LIBS += -L$$OUT_PWD -lqtkeychain +win32:LIBS += -Llib -lqtkeychain +unix:LIBS += -L$$OUT_PWD -lqtkeychain