userinfo.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Copyright (C) by Daniel Molkentin <danimo@owncloud.com>
  3. * Copyright (C) by Michael Schuster <michael@nextcloud.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  12. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  13. * for more details.
  14. */
  15. #include "userinfo.h"
  16. #include "account.h"
  17. #include "accountstate.h"
  18. #include "networkjobs.h"
  19. #include "folderman.h"
  20. #include "creds/abstractcredentials.h"
  21. #include <theme.h>
  22. #include <QTimer>
  23. #include <QJsonDocument>
  24. #include <QJsonObject>
  25. namespace OCC {
  26. namespace {
  27. static const int defaultIntervalT = 30 * 1000;
  28. static const int failIntervalT = 5 * 1000;
  29. }
  30. UserInfo::UserInfo(AccountState *accountState, bool allowDisconnectedAccountState, bool fetchAvatarImage, QObject *parent)
  31. : QObject(parent)
  32. , _accountState(accountState)
  33. , _allowDisconnectedAccountState(allowDisconnectedAccountState)
  34. , _fetchAvatarImage(fetchAvatarImage)
  35. , _lastQuotaTotalBytes(0)
  36. , _lastQuotaUsedBytes(0)
  37. , _active(false)
  38. {
  39. connect(accountState, &AccountState::stateChanged,
  40. this, &UserInfo::slotAccountStateChanged);
  41. connect(&_jobRestartTimer, &QTimer::timeout, this, &UserInfo::slotFetchInfo);
  42. _jobRestartTimer.setSingleShot(true);
  43. }
  44. void UserInfo::setActive(bool active)
  45. {
  46. _active = active;
  47. slotAccountStateChanged();
  48. }
  49. void UserInfo::slotAccountStateChanged()
  50. {
  51. if (canGetInfo()) {
  52. // Obviously assumes there will never be more than thousand of hours between last info
  53. // received and now, hence why we static_cast
  54. auto elapsed = static_cast<int>(_lastInfoReceived.msecsTo(QDateTime::currentDateTime()));
  55. if (_lastInfoReceived.isNull() || elapsed >= defaultIntervalT) {
  56. slotFetchInfo();
  57. } else {
  58. _jobRestartTimer.start(defaultIntervalT - elapsed);
  59. }
  60. } else {
  61. _jobRestartTimer.stop();
  62. }
  63. }
  64. void UserInfo::slotRequestFailed()
  65. {
  66. _lastQuotaTotalBytes = 0;
  67. _lastQuotaUsedBytes = 0;
  68. _jobRestartTimer.start(failIntervalT);
  69. }
  70. bool UserInfo::canGetInfo() const
  71. {
  72. if (!_accountState || !_active) {
  73. return false;
  74. }
  75. AccountPtr account = _accountState->account();
  76. return (_accountState->isConnected() || _allowDisconnectedAccountState)
  77. && account->credentials()
  78. && account->credentials()->ready();
  79. }
  80. void UserInfo::slotFetchInfo()
  81. {
  82. if (!canGetInfo()) {
  83. return;
  84. }
  85. if (_job) {
  86. // The previous job was not finished? Then we cancel it!
  87. _job->deleteLater();
  88. }
  89. AccountPtr account = _accountState->account();
  90. _job = new JsonApiJob(account, QLatin1String("ocs/v1.php/cloud/user"), this);
  91. _job->setTimeout(20 * 1000);
  92. connect(_job.data(), &JsonApiJob::jsonReceived, this, &UserInfo::slotUpdateLastInfo);
  93. connect(_job.data(), &AbstractNetworkJob::networkError, this, &UserInfo::slotRequestFailed);
  94. _job->start();
  95. }
  96. void UserInfo::slotUpdateLastInfo(const QJsonDocument &json)
  97. {
  98. auto objData = json.object().value("ocs").toObject().value("data").toObject();
  99. AccountPtr account = _accountState->account();
  100. // User Info
  101. QString user = objData.value("id").toString();
  102. if (!user.isEmpty()) {
  103. account->setDavUser(user);
  104. }
  105. QString displayName = objData.value("display-name").toString();
  106. if (!displayName.isEmpty()) {
  107. account->setDavDisplayName(displayName);
  108. }
  109. // Quota
  110. auto objQuota = objData.value("quota").toObject();
  111. qint64 used = objQuota.value("used").toDouble();
  112. qint64 total = objQuota.value("quota").toDouble();
  113. if(_lastInfoReceived.isNull() || _lastQuotaUsedBytes != used || _lastQuotaTotalBytes != total) {
  114. _lastQuotaUsedBytes = used;
  115. _lastQuotaTotalBytes = total;
  116. emit quotaUpdated(_lastQuotaTotalBytes, _lastQuotaUsedBytes);
  117. }
  118. _jobRestartTimer.start(defaultIntervalT);
  119. _lastInfoReceived = QDateTime::currentDateTime();
  120. // Avatar Image
  121. if(_fetchAvatarImage) {
  122. auto *job = new AvatarJob(account, account->davUser(), 128, this);
  123. job->setTimeout(20 * 1000);
  124. QObject::connect(job, &AvatarJob::avatarPixmap, this, &UserInfo::slotAvatarImage);
  125. job->start();
  126. }
  127. else
  128. emit fetchedLastInfo(this);
  129. }
  130. void UserInfo::slotAvatarImage(const QImage &img)
  131. {
  132. _accountState->account()->setAvatar(img);
  133. emit fetchedLastInfo(this);
  134. }
  135. } // namespace OCC