accountstate.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * Copyright (C) by Daniel Molkentin <danimo@owncloud.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. * for more details.
  13. */
  14. #ifndef ACCOUNTINFO_H
  15. #define ACCOUNTINFO_H
  16. #include <QByteArray>
  17. #include <QElapsedTimer>
  18. #include <QPointer>
  19. #include "connectionvalidator.h"
  20. #include "creds/abstractcredentials.h"
  21. #include <memory>
  22. class QSettings;
  23. namespace OCC {
  24. class AccountState;
  25. class Account;
  26. class AccountApp;
  27. class RemoteWipe;
  28. using AccountStatePtr = QExplicitlySharedDataPointer<AccountState>;
  29. using AccountAppList = QList<AccountApp *>;
  30. /**
  31. * @brief Extra info about an ownCloud server account.
  32. * @ingroup gui
  33. */
  34. class AccountState : public QObject, public QSharedData
  35. {
  36. Q_OBJECT
  37. Q_PROPERTY(AccountPtr account MEMBER _account)
  38. public:
  39. enum State {
  40. /// Not even attempting to connect, most likely because the
  41. /// user explicitly signed out or cancelled a credential dialog.
  42. SignedOut,
  43. /// Account would like to be connected but hasn't heard back yet.
  44. Disconnected,
  45. /// The account is successfully talking to the server.
  46. Connected,
  47. /// There's a temporary problem with talking to the server,
  48. /// don't bother the user too much and try again.
  49. ServiceUnavailable,
  50. /// Similar to ServiceUnavailable, but we know the server is down
  51. /// for maintenance
  52. MaintenanceMode,
  53. /// Could not communicate with the server for some reason.
  54. /// We assume this may resolve itself over time and will try
  55. /// again automatically.
  56. NetworkError,
  57. /// Server configuration error. (For example: unsupported version)
  58. ConfigurationError,
  59. /// We are currently asking the user for credentials
  60. AskingCredentials
  61. };
  62. /// The actual current connectivity status.
  63. using ConnectionStatus = ConnectionValidator::Status;
  64. /// Use the account as parent
  65. explicit AccountState(AccountPtr account);
  66. ~AccountState();
  67. /** Creates an account state from settings and an Account object.
  68. *
  69. * Use from AccountManager with a prepared QSettings object only.
  70. */
  71. static AccountState *loadFromSettings(AccountPtr account, QSettings &settings);
  72. /** Writes account state information to settings.
  73. *
  74. * It does not write the Account data.
  75. */
  76. void writeToSettings(QSettings &settings);
  77. AccountPtr account() const;
  78. ConnectionStatus connectionStatus() const;
  79. QStringList connectionErrors() const;
  80. State state() const;
  81. static QString stateString(State state);
  82. bool isSignedOut() const;
  83. AccountAppList appList() const;
  84. AccountApp* findApp(const QString &appId) const;
  85. /** A user-triggered sign out which disconnects, stops syncs
  86. * for the account and forgets the password. */
  87. void signOutByUi();
  88. /** Tries to connect from scratch.
  89. *
  90. * Does nothing for signed out accounts.
  91. * Connected accounts will be disconnected and try anew.
  92. * Disconnected accounts will go to checkConnectivity().
  93. *
  94. * Useful for when network settings (proxy) change.
  95. */
  96. void freshConnectionAttempt();
  97. /// Move from SignedOut state to Disconnected (attempting to connect)
  98. void signIn();
  99. bool isConnected() const;
  100. /** Returns a new settings object for this account, already in the right groups. */
  101. std::unique_ptr<QSettings> settings();
  102. /** Mark the timestamp when the last successful ETag check happened for
  103. * this account.
  104. * The checkConnectivity() method uses the timestamp to save a call to
  105. * the server to validate the connection if the last successful etag job
  106. * was not so long ago.
  107. */
  108. void tagLastSuccessfullETagRequest();
  109. /** Saves the ETag Response header from the last Notifications api
  110. * request with statusCode 200.
  111. */
  112. QByteArray notificationsEtagResponseHeader() const;
  113. /** Returns the ETag Response header from the last Notifications api
  114. * request with statusCode 200.
  115. */
  116. void setNotificationsEtagResponseHeader(const QByteArray &value);
  117. /** Saves the ETag Response header from the last Navigation Apps api
  118. * request with statusCode 200.
  119. */
  120. QByteArray navigationAppsEtagResponseHeader() const;
  121. /** Returns the ETag Response header from the last Navigation Apps api
  122. * request with statusCode 200.
  123. */
  124. void setNavigationAppsEtagResponseHeader(const QByteArray &value);
  125. ///Asks for user credentials
  126. void handleInvalidCredentials();
  127. public slots:
  128. /// Triggers a ping to the server to update state and
  129. /// connection status and errors.
  130. void checkConnectivity();
  131. private:
  132. void setState(State state);
  133. void fetchNavigationApps();
  134. signals:
  135. void stateChanged(int state);
  136. void isConnectedChanged();
  137. void hasFetchedNavigationApps();
  138. protected Q_SLOTS:
  139. void slotConnectionValidatorResult(ConnectionValidator::Status status, const QStringList &errors);
  140. /// When client gets a 401 or 403 checks if server requested remote wipe
  141. /// before asking for user credentials again
  142. void slotHandleRemoteWipeCheck();
  143. void slotCredentialsFetched(AbstractCredentials *creds);
  144. void slotCredentialsAsked(AbstractCredentials *creds);
  145. void slotNavigationAppsFetched(const QJsonDocument &reply, int statusCode);
  146. void slotEtagResponseHeaderReceived(const QByteArray &value, int statusCode);
  147. void slotOcsError(int statusCode, const QString &message);
  148. private:
  149. AccountPtr _account;
  150. State _state;
  151. ConnectionStatus _connectionStatus;
  152. QStringList _connectionErrors;
  153. bool _waitingForNewCredentials;
  154. QElapsedTimer _timeSinceLastETagCheck;
  155. QPointer<ConnectionValidator> _connectionValidator;
  156. QByteArray _notificationsEtagResponseHeader;
  157. QByteArray _navigationAppsEtagResponseHeader;
  158. /**
  159. * Starts counting when the server starts being back up after 503 or
  160. * maintenance mode. The account will only become connected once this
  161. * timer exceeds the _maintenanceToConnectedDelay value.
  162. */
  163. QElapsedTimer _timeSinceMaintenanceOver;
  164. /**
  165. * Milliseconds for which to delay reconnection after 503/maintenance.
  166. */
  167. int _maintenanceToConnectedDelay;
  168. /**
  169. * Connects remote wipe check with the account
  170. * the log out triggers the check (loads app password -> create request)
  171. */
  172. RemoteWipe *_remoteWipe;
  173. /**
  174. * Holds the App names and URLs available on the server
  175. */
  176. AccountAppList _apps;
  177. };
  178. class AccountApp : public QObject
  179. {
  180. Q_OBJECT
  181. public:
  182. AccountApp(const QString &name, const QUrl &url,
  183. const QString &id, const QUrl &iconUrl,
  184. QObject* parent = nullptr);
  185. QString name() const;
  186. QUrl url() const;
  187. QString id() const;
  188. QUrl iconUrl() const;
  189. private:
  190. QString _name;
  191. QUrl _url;
  192. QString _id;
  193. QUrl _iconUrl;
  194. };
  195. }
  196. Q_DECLARE_METATYPE(OCC::AccountState *)
  197. Q_DECLARE_METATYPE(OCC::AccountStatePtr)
  198. #endif //ACCOUNTINFO_H