/ testclient.cpp
testclient.cpp
  1  /******************************************************************************
  2   *   Copyright (C) 2011-2015 Frank Osterfeld <frank.osterfeld@gmail.com>      *
  3   *                                                                            *
  4   * This program is distributed in the hope that it will be useful, but        *
  5   * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *
  6   * or FITNESS FOR A PARTICULAR PURPOSE. For licensing and distribution        *
  7   * details, check the accompanying file 'COPYING'.                            *
  8   *****************************************************************************/
  9  #include <QCoreApplication>
 10  #include <QStringList>
 11  
 12  #include "keychain.h"
 13  #include <iostream>
 14  
 15  using namespace QKeychain;
 16  
 17  static int printUsage() {
 18      std::cerr << "testclient store <account> <password>" << std::endl;
 19      std::cerr << "testclient restore <account>" << std::endl;
 20      std::cerr << "testclient delete <account>" << std::endl;
 21      return 1;
 22  }
 23  
 24  int main( int argc, char** argv ) {
 25      QCoreApplication app( argc, argv );
 26      const QStringList args = app.arguments();
 27      if ( args.count() < 2 )
 28          return printUsage();
 29  
 30      QStringList::ConstIterator it = args.constBegin();
 31      ++it;
 32  
 33      if ( *it == QLatin1String("store") ) {
 34          if ( ++it == args.constEnd() )
 35              return printUsage();
 36          const QString acc = *it;
 37          if ( ++it == args.constEnd() )
 38              return printUsage();
 39          const QString pass = *it;
 40          if ( ++it != args.constEnd() )
 41              return printUsage();
 42          WritePasswordJob job( QLatin1String("qtkeychain-testclient") );
 43          job.setAutoDelete( false );
 44          job.setKey( acc );
 45          job.setTextData( pass );
 46          QEventLoop loop;
 47          job.connect( &job, SIGNAL(finished(QKeychain::Job*)), &loop, SLOT(quit()) );
 48          job.start();
 49          loop.exec();
 50       if ( job.error() ) {
 51              std::cerr << "Storing password failed: " << qPrintable(job.errorString()) << std::endl;
 52              return 1;
 53          }
 54          std::cout << "Password stored successfully" << std::endl;
 55      } else if ( *it == QLatin1String("bstore") ) {
 56          if ( ++it == args.constEnd() )
 57              return printUsage();
 58          const QString acc = *it;
 59          if ( ++it == args.constEnd() )
 60              return printUsage();
 61          const QString pass = *it;
 62          if ( ++it != args.constEnd() )
 63              return printUsage();
 64          WritePasswordJob job( QLatin1String("qtkeychain-testclient") );
 65          job.setAutoDelete( false );
 66          job.setKey( acc );
 67          job.setBinaryData( pass.toUtf8() );
 68          QEventLoop loop;
 69          job.connect( &job, SIGNAL(finished(QKeychain::Job*)), &loop, SLOT(quit()) );
 70          job.start();
 71          loop.exec();
 72       if ( job.error() ) {
 73              std::cerr << "Storing binary password failed: "
 74                        << qPrintable(job.errorString()) << std::endl;
 75              return 1;
 76          }
 77          std::cout << "Password stored successfully" << std::endl;
 78      } else if ( *it == QLatin1String("restore") ) {
 79          if ( ++it == args.constEnd() )
 80              return printUsage();
 81          const QString acc = *it;
 82          if ( ++it != args.constEnd() )
 83              return printUsage();
 84          ReadPasswordJob job( QLatin1String("qtkeychain-testclient") );
 85          job.setAutoDelete( false );
 86          job.setKey( acc );
 87          QEventLoop loop;
 88          job.connect( &job, SIGNAL(finished(QKeychain::Job*)), &loop, SLOT(quit()) );
 89          job.start();
 90          loop.exec();
 91  
 92          const QString pw = job.textData();
 93          if ( job.error() ) {
 94              std::cerr << "Restoring password failed: " << qPrintable(job.errorString()) << std::endl;
 95              return 1;
 96          }
 97          std::cout << qPrintable(pw) << std::endl;
 98      } else if ( *it == QLatin1String("delete") ) {
 99          if ( ++it == args.constEnd() )
100              return printUsage();
101          const QString acc = *it;
102          if ( ++it != args.constEnd() )
103              return printUsage();
104          DeletePasswordJob job( QLatin1String("qtkeychain-testclient") );
105          job.setAutoDelete( false );
106          job.setKey( acc );
107          QEventLoop loop;
108          job.connect( &job, SIGNAL(finished(QKeychain::Job*)), &loop, SLOT(quit()) );
109          job.start();
110          loop.exec();
111  
112          if ( job.error() ) {
113              std::cerr << "Deleting password failed: " << qPrintable(job.errorString()) << std::endl;
114              return 1;
115          }
116          std::cout << "Password deleted successfully" << std::endl;
117      } else {
118          return printUsage();
119      }
120  }
121