keychain_p.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. #ifndef KEYCHAIN_P_H
  10. #define KEYCHAIN_P_H
  11. #include <QCoreApplication>
  12. #include <QObject>
  13. #include <QPointer>
  14. #include <QSettings>
  15. #include <QQueue>
  16. #if defined(KEYCHAIN_DBUS)
  17. #include <QDBusPendingCallWatcher>
  18. #include "kwallet_interface.h"
  19. #else
  20. class QDBusPendingCallWatcher;
  21. #endif
  22. #include "keychain.h"
  23. namespace QKeychain {
  24. class JobExecutor;
  25. class JobPrivate : public QObject {
  26. Q_OBJECT
  27. public:
  28. enum Mode {
  29. Text,
  30. Binary
  31. };
  32. virtual void scheduledStart() = 0;
  33. static QString modeToString(Mode m);
  34. static Mode stringToMode(const QString& s);
  35. Job* const q;
  36. Mode mode;
  37. QByteArray data;
  38. #if defined(KEYCHAIN_DBUS)
  39. org::kde::KWallet* iface;
  40. int walletHandle;
  41. static void gnomeKeyring_readCb( int result, const char* string, JobPrivate* data );
  42. static void gnomeKeyring_writeCb( int result, JobPrivate* self );
  43. virtual void fallbackOnError(const QDBusError& err) = 0;
  44. protected Q_SLOTS:
  45. void kwalletWalletFound( QDBusPendingCallWatcher* watcher );
  46. virtual void kwalletFinished( QDBusPendingCallWatcher* watcher );
  47. virtual void kwalletOpenFinished( QDBusPendingCallWatcher* watcher );
  48. #else
  49. void kwalletWalletFound( QDBusPendingCallWatcher* ) {}
  50. virtual void kwalletFinished( QDBusPendingCallWatcher* ) {}
  51. virtual void kwalletOpenFinished( QDBusPendingCallWatcher* ) {}
  52. #endif
  53. protected:
  54. JobPrivate( const QString& service_, Job *q );
  55. protected:
  56. QKeychain::Error error;
  57. QString errorString;
  58. QString service;
  59. bool autoDelete;
  60. bool insecureFallback;
  61. QPointer<QSettings> settings;
  62. QString key;
  63. friend class Job;
  64. friend class JobExecutor;
  65. friend class ReadPasswordJob;
  66. friend class WritePasswordJob;
  67. friend class PlainTextStore;
  68. };
  69. class ReadPasswordJobPrivate : public JobPrivate {
  70. Q_OBJECT
  71. public:
  72. explicit ReadPasswordJobPrivate( const QString &service_, ReadPasswordJob* qq );
  73. void scheduledStart() override;
  74. #if defined(KEYCHAIN_DBUS)
  75. void fallbackOnError(const QDBusError& err) override;
  76. private Q_SLOTS:
  77. void kwalletOpenFinished( QDBusPendingCallWatcher* watcher ) override;
  78. void kwalletEntryTypeFinished( QDBusPendingCallWatcher* watcher );
  79. void kwalletFinished( QDBusPendingCallWatcher* watcher ) override;
  80. #else //moc's too dumb to respect above macros, so just define empty slot implementations
  81. private Q_SLOTS:
  82. void kwalletOpenFinished( QDBusPendingCallWatcher* ) {}
  83. void kwalletEntryTypeFinished( QDBusPendingCallWatcher* ) {}
  84. void kwalletFinished( QDBusPendingCallWatcher* ) {}
  85. #endif
  86. friend class ReadPasswordJob;
  87. };
  88. class WritePasswordJobPrivate : public JobPrivate {
  89. Q_OBJECT
  90. public:
  91. explicit WritePasswordJobPrivate( const QString &service_, WritePasswordJob* qq );
  92. void scheduledStart() override;
  93. #if defined(KEYCHAIN_DBUS)
  94. void fallbackOnError(const QDBusError& err) override;
  95. #endif
  96. friend class WritePasswordJob;
  97. };
  98. class DeletePasswordJobPrivate : public JobPrivate {
  99. Q_OBJECT
  100. public:
  101. explicit DeletePasswordJobPrivate( const QString &service_, DeletePasswordJob* qq );
  102. void scheduledStart() override;
  103. #if defined(KEYCHAIN_DBUS)
  104. void fallbackOnError(const QDBusError& err) override;
  105. #endif
  106. protected:
  107. void doStart();
  108. friend class DeletePasswordJob;
  109. };
  110. class JobExecutor : public QObject {
  111. Q_OBJECT
  112. public:
  113. static JobExecutor* instance();
  114. void enqueue( Job* job );
  115. private:
  116. explicit JobExecutor();
  117. void startNextIfNoneRunning();
  118. private Q_SLOTS:
  119. void jobFinished( QKeychain::Job* );
  120. void jobDestroyed( QObject* object );
  121. private:
  122. static JobExecutor* s_instance;
  123. QQueue<QPointer<Job> > m_queue;
  124. bool m_jobRunning;
  125. };
  126. }
  127. #endif // KEYCHAIN_P_H