keychain.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 "keychain.h"
  10. #include "keychain_p.h"
  11. using namespace QKeychain;
  12. Job::Job( JobPrivate *q, QObject *parent )
  13. : QObject( parent )
  14. , d ( q ) {
  15. }
  16. Job::~Job() {
  17. delete d;
  18. }
  19. QString Job::service() const {
  20. return d->service;
  21. }
  22. QSettings* Job::settings() const {
  23. return d->settings;
  24. }
  25. void Job::setSettings( QSettings* settings ) {
  26. d->settings = settings;
  27. }
  28. void Job::start() {
  29. QMetaObject::invokeMethod( this, "doStart", Qt::QueuedConnection );
  30. }
  31. bool Job::autoDelete() const {
  32. return d->autoDelete;
  33. }
  34. void Job::setAutoDelete( bool autoDelete ) {
  35. d->autoDelete = autoDelete;
  36. }
  37. bool Job::insecureFallback() const {
  38. return d->insecureFallback;
  39. }
  40. void Job::setInsecureFallback( bool insecureFallback ) {
  41. d->insecureFallback = insecureFallback;
  42. }
  43. void Job::doStart() {
  44. JobExecutor::instance()->enqueue( this );
  45. }
  46. void Job::emitFinished() {
  47. emit finished( this );
  48. if ( d->autoDelete )
  49. deleteLater();
  50. }
  51. void Job::emitFinishedWithError( Error error, const QString& errorString ) {
  52. d->error = error;
  53. d->errorString = errorString;
  54. emitFinished();
  55. }
  56. void Job::scheduledStart() {
  57. d->scheduledStart();
  58. }
  59. Error Job::error() const {
  60. return d->error;
  61. }
  62. QString Job::errorString() const {
  63. return d->errorString;
  64. }
  65. void Job::setError( Error error ) {
  66. d->error = error;
  67. }
  68. void Job::setErrorString( const QString& errorString ) {
  69. d->errorString = errorString;
  70. }
  71. ReadPasswordJob::ReadPasswordJob( const QString& service, QObject* parent )
  72. : Job( new ReadPasswordJobPrivate( service, this ), parent ) {
  73. }
  74. ReadPasswordJob::~ReadPasswordJob() {
  75. }
  76. QString ReadPasswordJob::textData() const {
  77. return QString::fromUtf8( d->data );
  78. }
  79. QByteArray ReadPasswordJob::binaryData() const {
  80. return d->data;
  81. }
  82. QString Job::key() const {
  83. return d->key;
  84. }
  85. void Job::setKey( const QString& key_ ) {
  86. d->key = key_;
  87. }
  88. WritePasswordJob::WritePasswordJob( const QString& service, QObject* parent )
  89. : Job( new WritePasswordJobPrivate( service, this ), parent ) {
  90. }
  91. WritePasswordJob::~WritePasswordJob() {
  92. }
  93. void WritePasswordJob::setBinaryData( const QByteArray& data ) {
  94. d->data = data;
  95. d->mode = JobPrivate::Binary;
  96. }
  97. void WritePasswordJob::setTextData( const QString& data ) {
  98. d->data = data.toUtf8();
  99. d->mode = JobPrivate::Text;
  100. }
  101. DeletePasswordJob::DeletePasswordJob( const QString& service, QObject* parent )
  102. : Job( new DeletePasswordJobPrivate( service, this ), parent ) {
  103. }
  104. DeletePasswordJob::~DeletePasswordJob() {
  105. }
  106. DeletePasswordJobPrivate::DeletePasswordJobPrivate(const QString &service_, DeletePasswordJob *qq) :
  107. JobPrivate(service_, qq) {
  108. }
  109. JobExecutor::JobExecutor()
  110. : QObject( 0 )
  111. , m_jobRunning( false ) {
  112. }
  113. void JobExecutor::enqueue( Job* job ) {
  114. m_queue.enqueue( job );
  115. startNextIfNoneRunning();
  116. }
  117. void JobExecutor::startNextIfNoneRunning() {
  118. if ( m_queue.isEmpty() || m_jobRunning )
  119. return;
  120. QPointer<Job> next;
  121. while ( !next && !m_queue.isEmpty() ) {
  122. next = m_queue.dequeue();
  123. }
  124. if ( next ) {
  125. connect( next, SIGNAL(finished(QKeychain::Job*)), this, SLOT(jobFinished(QKeychain::Job*)) );
  126. connect( next, SIGNAL(destroyed(QObject*)), this, SLOT(jobDestroyed(QObject*)) );
  127. m_jobRunning = true;
  128. next->scheduledStart();
  129. }
  130. }
  131. void JobExecutor::jobDestroyed( QObject* object ) {
  132. Job* job = static_cast<Job*>(object);
  133. Q_UNUSED( object ) // for release mode
  134. job->disconnect( this );
  135. m_jobRunning = false;
  136. startNextIfNoneRunning();
  137. }
  138. void JobExecutor::jobFinished( Job* job ) {
  139. Q_UNUSED( job ) // for release mode
  140. job->disconnect( this );
  141. m_jobRunning = false;
  142. startNextIfNoneRunning();
  143. }
  144. JobExecutor* JobExecutor::s_instance = 0;
  145. JobExecutor* JobExecutor::instance() {
  146. if ( !s_instance )
  147. s_instance = new JobExecutor;
  148. return s_instance;
  149. }
  150. ReadPasswordJobPrivate::ReadPasswordJobPrivate(const QString &service_, ReadPasswordJob *qq) :
  151. JobPrivate(service_, qq) {
  152. }
  153. JobPrivate::JobPrivate(const QString &service_, Job *qq)
  154. : q(qq)
  155. , mode( Text )
  156. , error( NoError )
  157. , service( service_ )
  158. , autoDelete( true )
  159. , insecureFallback( false )
  160. {
  161. }
  162. QString JobPrivate::modeToString(Mode m)
  163. {
  164. switch (m) {
  165. case Text:
  166. return QLatin1String("Text");
  167. case Binary:
  168. return QLatin1String("Binary");
  169. }
  170. Q_ASSERT_X(false, Q_FUNC_INFO, "Unhandled Mode value");
  171. return QString();
  172. }
  173. JobPrivate::Mode JobPrivate::stringToMode(const QString& s)
  174. {
  175. if (s == QLatin1String("Text") || s == QLatin1String("1"))
  176. return Text;
  177. if (s == QLatin1String("Binary") || s == QLatin1String("2"))
  178. return Binary;
  179. qCritical("Unexpected mode string '%s'", qPrintable(s));
  180. return Text;
  181. }
  182. WritePasswordJobPrivate::WritePasswordJobPrivate(const QString &service_, WritePasswordJob *qq) :
  183. JobPrivate(service_, qq) {
  184. }