userinfo.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright (C) by Daniel Molkentin <danimo@owncloud.com>
  3. * Copyright (C) by Michael Schuster <michael@schuster.ms>
  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. #ifndef USERINFO_H
  16. #define USERINFO_H
  17. #include <QObject>
  18. #include <QPointer>
  19. #include <QVariant>
  20. #include <QTimer>
  21. #include <QDateTime>
  22. namespace OCC {
  23. class AccountState;
  24. class JsonApiJob;
  25. /**
  26. * @brief handles getting the user info and quota to display in the UI
  27. *
  28. * It is typically owned by the AccountSetting page.
  29. *
  30. * The user info and quota is requested if these 3 conditions are met:
  31. * - This object is active via setActive() (typically if the settings page is visible.)
  32. * - The account is connected.
  33. * - Every 30 seconds (defaultIntervalT) or 5 seconds in case of failure (failIntervalT)
  34. *
  35. * We only request the info when the UI is visible otherwise this might slow down the server with
  36. * too many requests. But we still need to do it every 30 seconds otherwise user complains that the
  37. * quota is not updated fast enough when changed on the server.
  38. *
  39. * If the fetch job is not finished within 30 seconds, it is cancelled and another one is started
  40. *
  41. * Constructor notes:
  42. * - allowDisconnectedAccountState: set to true if you want to ignore AccountState's isConnected() state,
  43. * this is used by ConnectionValidator (prior having a valid AccountState).
  44. * - fetchAvatarImage: set to false if you don't want to fetch the avatar image
  45. *
  46. * @ingroup gui
  47. *
  48. * Here follows the state machine
  49. \code{.unparsed}
  50. *---> slotFetchInfo
  51. JsonApiJob (ocs/v1.php/cloud/user)
  52. |
  53. +-> slotUpdateLastInfo
  54. AvatarJob (if _fetchAvatarImage is true)
  55. |
  56. +-> slotAvatarImage -->
  57. +-----------------------------------+
  58. |
  59. +-> Client Side Encryption Checks --+ --reportResult()
  60. \endcode
  61. */
  62. class UserInfo : public QObject
  63. {
  64. Q_OBJECT
  65. public:
  66. explicit UserInfo(OCC::AccountState *accountState, bool allowDisconnectedAccountState, bool fetchAvatarImage, QObject *parent = nullptr);
  67. [[nodiscard]] qint64 lastQuotaTotalBytes() const { return _lastQuotaTotalBytes; }
  68. [[nodiscard]] qint64 lastQuotaUsedBytes() const { return _lastQuotaUsedBytes; }
  69. /**
  70. * When the quotainfo is active, it requests the quota at regular interval.
  71. * When setting it to active it will request the quota immediately if the last time
  72. * the quota was requested was more than the interval
  73. */
  74. void setActive(bool active);
  75. public Q_SLOTS:
  76. void slotFetchInfo();
  77. private Q_SLOTS:
  78. void slotUpdateLastInfo(const QJsonDocument &json);
  79. void slotAccountStateChanged();
  80. void slotRequestFailed();
  81. void slotAvatarImage(const QImage &img);
  82. Q_SIGNALS:
  83. void quotaUpdated(qint64 total, qint64 used);
  84. void fetchedLastInfo(OCC::UserInfo *userInfo);
  85. private:
  86. [[nodiscard]] bool canGetInfo() const;
  87. QPointer<AccountState> _accountState;
  88. bool _allowDisconnectedAccountState;
  89. bool _fetchAvatarImage;
  90. qint64 _lastQuotaTotalBytes = 0;
  91. qint64 _lastQuotaUsedBytes = 0;
  92. QTimer _jobRestartTimer;
  93. QDateTime _lastInfoReceived; // the time at which the user info and quota was received last
  94. bool _active = false; // if we should check at regular interval (when the UI is visible)
  95. QPointer<JsonApiJob> _job; // the currently running job
  96. };
  97. } // namespace OCC
  98. #endif //USERINFO_H