plaintextstore.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /******************************************************************************
  2. * Copyright (C) 2011-2015 Frank Osterfeld <frank.osterfeld@gmail.com> *
  3. * Copyright (C) 2016 Mathias Hasselmann <mathias.hasselmann@kdab.com> *
  4. * *
  5. * This program is distributed in the hope that it will be useful, but *
  6. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *
  7. * or FITNESS FOR A PARTICULAR PURPOSE. For licensing and distribution *
  8. * details, check the accompanying file 'COPYING'. *
  9. *****************************************************************************/
  10. #include "plaintextstore_p.h"
  11. using namespace QKeychain;
  12. namespace {
  13. #ifdef Q_OS_WIN
  14. inline QString dataKey(const QString &key) { return key; }
  15. #else // Q_OS_WIN
  16. inline QString dataKey(const QString &key) { return key + QLatin1String("/data"); }
  17. inline QString typeKey(const QString &key) { return key + QLatin1String("/type"); }
  18. #endif // Q_OS_WIN
  19. }
  20. PlainTextStore::PlainTextStore(const QString &service, QSettings *settings)
  21. : m_localSettings(settings ? 0 : new QSettings(service))
  22. , m_actualSettings(settings ? settings : m_localSettings.data())
  23. , m_error(NoError)
  24. {
  25. }
  26. bool PlainTextStore::contains(const QString &key) const
  27. {
  28. return m_actualSettings->contains(dataKey(key));
  29. }
  30. QByteArray PlainTextStore::readData(const QString &key)
  31. {
  32. return read(dataKey(key)).toByteArray();
  33. }
  34. #ifndef Q_OS_WIN
  35. JobPrivate::Mode PlainTextStore::readMode(const QString &key)
  36. {
  37. return JobPrivate::stringToMode(read(typeKey(key)).toString());
  38. }
  39. #endif // Q_OS_WIN
  40. void PlainTextStore::write(const QString &key, const QByteArray &data, JobPrivate::Mode mode)
  41. {
  42. if (m_actualSettings->status() != QSettings::NoError)
  43. return;
  44. #ifndef Q_OS_WIN
  45. m_actualSettings->setValue(typeKey(key), JobPrivate::modeToString(mode));
  46. #else // Q_OS_WIN
  47. Q_UNUSED(mode);
  48. #endif // Q_OS_WIN
  49. m_actualSettings->setValue(dataKey(key), data);
  50. m_actualSettings->sync();
  51. if (m_actualSettings->status() == QSettings::AccessError) {
  52. setError(AccessDenied, tr("Could not store data in settings: access error"));
  53. } else if (m_actualSettings->status() != QSettings::NoError) {
  54. setError(OtherError, tr("Could not store data in settings: format error"));
  55. } else {
  56. setError(NoError, QString());
  57. }
  58. }
  59. void PlainTextStore::remove(const QString &key)
  60. {
  61. if (m_actualSettings->status() != QSettings::NoError)
  62. return;
  63. #ifndef Q_OS_WIN
  64. m_actualSettings->remove(typeKey(key));
  65. #endif // Q_OS_WIN
  66. m_actualSettings->remove(dataKey(key));
  67. m_actualSettings->sync();
  68. if (m_actualSettings->status() == QSettings::AccessError) {
  69. setError(AccessDenied, tr("Could not delete data from settings: access error"));
  70. } else if (m_actualSettings->status() != QSettings::NoError) {
  71. setError(OtherError, tr("Could not delete data from settings: format error"));
  72. } else {
  73. setError(NoError, QString());
  74. }
  75. }
  76. void PlainTextStore::setError(Error error, const QString &errorString)
  77. {
  78. m_error = error;
  79. m_errorString = errorString;
  80. }
  81. QVariant PlainTextStore::read(const QString &key)
  82. {
  83. const QVariant value = m_actualSettings->value(key);
  84. if (value.isNull()) {
  85. setError(EntryNotFound, tr("Entry not found"));
  86. } else {
  87. setError(NoError, QString());
  88. }
  89. return value;
  90. }