File indexing completed on 2025-01-19 03:55:38
0001 // 0002 // Created by michaelpollind on 3/13/17. 0003 // 0004 #include "o0keychainstore.h" 0005 0006 #include <QDebug> 0007 #include <keychain.h> 0008 #include <QtCore/QDataStream> 0009 #include <QtCore/QBuffer> 0010 #include <QtCore/QEventLoop> 0011 0012 using namespace QKeychain; 0013 0014 o0keyChainStore::o0keyChainStore(const QString& app,const QString& name,QObject *parent): 0015 O0AbstractStore(parent), app_(app),name_(name),pairs_() 0016 { 0017 } 0018 0019 QString o0keyChainStore::value(const QString &key, const QString &defaultValue) { 0020 return pairs_.value(key, defaultValue); 0021 } 0022 0023 void o0keyChainStore::setValue(const QString &key, const QString &value) { 0024 pairs_.insert(key,value); 0025 } 0026 0027 int o0keyChainStore::persist() { 0028 WritePasswordJob job(app_); 0029 initJob(job); 0030 0031 QByteArray data; 0032 QDataStream ds(&data,QIODevice::ReadWrite); 0033 ds << pairs_; 0034 job.setBinaryData(data); 0035 0036 return executeJob(job, "persist"); 0037 } 0038 0039 int o0keyChainStore::fetchFromKeychain() { 0040 ReadPasswordJob job(app_); 0041 initJob(job); 0042 const int result = executeJob(job, "fetch"); 0043 if (result == 0) { // success 0044 QByteArray data; 0045 data.append(job.binaryData()); 0046 QDataStream ds(&data, QIODevice::ReadOnly); 0047 ds >> pairs_; 0048 } 0049 return result; 0050 } 0051 0052 int o0keyChainStore::clearFromKeychain() { 0053 DeletePasswordJob job(app_); 0054 initJob(job); 0055 return executeJob(job, "clear"); 0056 } 0057 0058 bool o0keyChainStore::isEntryNotFoundError(int errorCode) { 0059 return errorCode == QKeychain::EntryNotFound; 0060 } 0061 0062 void o0keyChainStore::initJob(QKeychain::Job &job) const { 0063 job.setAutoDelete(false); 0064 job.setKey(name_); 0065 } 0066 0067 int o0keyChainStore::executeJob(QKeychain::Job &job, const char *actionName) const { 0068 QEventLoop loop; 0069 job.connect( &job, SIGNAL(finished(QKeychain::Job*)), &loop, SLOT(quit()) ); 0070 job.start(); 0071 loop.exec(); 0072 0073 const QKeychain::Error errorCode = job.error(); 0074 if (errorCode != QKeychain::NoError) { 0075 qWarning() << "keychain store could not" << actionName << name_ << ":" 0076 << job.errorString() << "(" << errorCode << ")."; 0077 } 0078 return errorCode; 0079 } 0080 0081 #include "moc_o0keychainstore.cpp"