UserModel.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. #ifndef USERMODEL_H
  2. #define USERMODEL_H
  3. #include <QAbstractListModel>
  4. #include <QImage>
  5. #include <QDateTime>
  6. #include <QStringList>
  7. #include <QQuickImageProvider>
  8. #include "ActivityListModel.h"
  9. #include "accountmanager.h"
  10. #include "folderman.h"
  11. #include <chrono>
  12. namespace OCC {
  13. class User : public QObject
  14. {
  15. Q_OBJECT
  16. Q_PROPERTY(QString name READ name NOTIFY nameChanged)
  17. Q_PROPERTY(QString server READ server CONSTANT)
  18. Q_PROPERTY(QUrl statusIcon READ statusIcon NOTIFY statusChanged)
  19. Q_PROPERTY(QString statusMessage READ statusMessage NOTIFY statusChanged)
  20. Q_PROPERTY(bool hasLocalFolder READ hasLocalFolder NOTIFY hasLocalFolderChanged)
  21. Q_PROPERTY(bool serverHasTalk READ serverHasTalk NOTIFY serverHasTalkChanged)
  22. Q_PROPERTY(QString avatar READ avatarUrl NOTIFY avatarChanged)
  23. public:
  24. User(AccountStatePtr &account, const bool &isCurrent = false, QObject* parent = nullptr);
  25. AccountPtr account() const;
  26. bool isConnected() const;
  27. bool isCurrentUser() const;
  28. void setCurrentUser(const bool &isCurrent);
  29. Folder *getFolder() const;
  30. ActivityListModel *getActivityModel();
  31. void openLocalFolder();
  32. QString name() const;
  33. QString server(bool shortened = true) const;
  34. bool hasLocalFolder() const;
  35. bool serverHasTalk() const;
  36. AccountApp *talkApp() const;
  37. bool hasActivities() const;
  38. AccountAppList appList() const;
  39. QImage avatar() const;
  40. void login() const;
  41. void logout() const;
  42. void removeAccount() const;
  43. QString avatarUrl() const;
  44. bool isDesktopNotificationsAllowed() const;
  45. UserStatus::Status status() const;
  46. QString statusMessage() const;
  47. QUrl statusIcon() const;
  48. signals:
  49. void guiLog(const QString &, const QString &);
  50. void nameChanged();
  51. void hasLocalFolderChanged();
  52. void serverHasTalkChanged();
  53. void avatarChanged();
  54. void accountStateChanged(int state);
  55. void statusChanged();
  56. public slots:
  57. void slotItemCompleted(const QString &folder, const SyncFileItemPtr &item);
  58. void slotProgressInfo(const QString &folder, const ProgressInfo &progress);
  59. void slotAddError(const QString &folderAlias, const QString &message, ErrorCategory category);
  60. void slotNotificationRequestFinished(int statusCode);
  61. void slotNotifyNetworkError(QNetworkReply *reply);
  62. void slotEndNotificationRequest(int replyCode);
  63. void slotNotifyServerFinished(const QString &reply, int replyCode);
  64. void slotSendNotificationRequest(const QString &accountName, const QString &link, const QByteArray &verb, int row);
  65. void slotBuildNotificationDisplay(const ActivityList &list);
  66. void slotRefreshNotifications();
  67. void slotRefreshActivities();
  68. void slotRefresh();
  69. void slotRefreshUserStatus();
  70. void slotRefreshImmediately();
  71. void setNotificationRefreshInterval(std::chrono::milliseconds interval);
  72. void slotRebuildNavigationAppList();
  73. private:
  74. void slotPushNotificationsReady();
  75. void slotDisconnectPushNotifications();
  76. void slotReceivedPushNotification(Account *account);
  77. void slotReceivedPushActivity(Account *account);
  78. void connectPushNotifications() const;
  79. bool checkPushNotificationsAreReady() const;
  80. bool isActivityOfCurrentAccount(const Folder *folder) const;
  81. bool isUnsolvableConflict(const SyncFileItemPtr &item) const;
  82. private:
  83. AccountStatePtr _account;
  84. bool _isCurrentUser;
  85. ActivityListModel *_activityModel;
  86. ActivityList _blacklistedNotifications;
  87. QTimer _notificationCheckTimer;
  88. QHash<AccountState *, QElapsedTimer> _timeSinceLastCheck;
  89. QElapsedTimer _guiLogTimer;
  90. QSet<int> _guiLoggedNotifications;
  91. // number of currently running notification requests. If non zero,
  92. // no query for notifications is started.
  93. int _notificationRequestsRunning;
  94. };
  95. class UserModel : public QAbstractListModel
  96. {
  97. Q_OBJECT
  98. Q_PROPERTY(User* currentUser READ currentUser NOTIFY newUserSelected)
  99. Q_PROPERTY(int currentUserId READ currentUserId NOTIFY newUserSelected)
  100. public:
  101. static UserModel *instance();
  102. virtual ~UserModel() = default;
  103. void addUser(AccountStatePtr &user, const bool &isCurrent = false);
  104. int currentUserIndex();
  105. int rowCount(const QModelIndex &parent = QModelIndex()) const override;
  106. QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
  107. QImage avatarById(const int &id);
  108. User *currentUser() const;
  109. int findUserIdForAccount(AccountState *account) const;
  110. Q_INVOKABLE void fetchCurrentActivityModel();
  111. Q_INVOKABLE void openCurrentAccountLocalFolder();
  112. Q_INVOKABLE void openCurrentAccountTalk();
  113. Q_INVOKABLE void openCurrentAccountServer();
  114. Q_INVOKABLE int numUsers();
  115. Q_INVOKABLE QString currentUserServer();
  116. Q_INVOKABLE bool currentUserHasActivities();
  117. Q_INVOKABLE bool currentUserHasLocalFolder();
  118. int currentUserId() const;
  119. Q_INVOKABLE bool isUserConnected(const int &id);
  120. Q_INVOKABLE QUrl statusIcon(int id);
  121. Q_INVOKABLE void switchCurrentUser(const int &id);
  122. Q_INVOKABLE void login(const int &id);
  123. Q_INVOKABLE void logout(const int &id);
  124. Q_INVOKABLE void removeAccount(const int &id);
  125. ActivityListModel *currentActivityModel();
  126. enum UserRoles {
  127. NameRole = Qt::UserRole + 1,
  128. ServerRole,
  129. StatusIconRole,
  130. StatusMessageRole,
  131. AvatarRole,
  132. IsCurrentUserRole,
  133. IsConnectedRole,
  134. IdRole
  135. };
  136. AccountAppList appList() const;
  137. signals:
  138. Q_INVOKABLE void addAccount();
  139. Q_INVOKABLE void refreshCurrentUserGui();
  140. Q_INVOKABLE void newUserSelected();
  141. protected:
  142. QHash<int, QByteArray> roleNames() const override;
  143. private:
  144. static UserModel *_instance;
  145. UserModel(QObject *parent = nullptr);
  146. QList<User*> _users;
  147. int _currentUserId = 0;
  148. bool _init = true;
  149. void buildUserList();
  150. };
  151. class ImageProvider : public QQuickImageProvider
  152. {
  153. public:
  154. ImageProvider();
  155. QImage requestImage(const QString &id, QSize *size, const QSize &requestedSize) override;
  156. };
  157. class UserAppsModel : public QAbstractListModel
  158. {
  159. Q_OBJECT
  160. public:
  161. static UserAppsModel *instance();
  162. virtual ~UserAppsModel() = default;
  163. int rowCount(const QModelIndex &parent = QModelIndex()) const override;
  164. QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
  165. enum UserAppsRoles {
  166. NameRole = Qt::UserRole + 1,
  167. UrlRole,
  168. IconUrlRole
  169. };
  170. void buildAppList();
  171. public slots:
  172. void openAppUrl(const QUrl &url);
  173. protected:
  174. QHash<int, QByteArray> roleNames() const override;
  175. private:
  176. static UserAppsModel *_instance;
  177. UserAppsModel(QObject *parent = nullptr);
  178. AccountAppList _apps;
  179. };
  180. }
  181. #endif // USERMODEL_H