sharemodel.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * Copyright (C) 2022 by Claudio Cambra <claudio.cambra@nextcloud.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. #pragma once
  15. #include <QAbstractListModel>
  16. #include "accountstate.h"
  17. #include "folder.h"
  18. #include "sharemanager.h"
  19. #include "sharepermissions.h"
  20. namespace OCC {
  21. class ShareModel : public QAbstractListModel
  22. {
  23. Q_OBJECT
  24. Q_PROPERTY(AccountState* accountState READ accountState WRITE setAccountState NOTIFY accountStateChanged)
  25. Q_PROPERTY(QString localPath READ localPath WRITE setLocalPath NOTIFY localPathChanged)
  26. Q_PROPERTY(bool accountConnected READ accountConnected NOTIFY accountConnectedChanged)
  27. Q_PROPERTY(bool sharingEnabled READ sharingEnabled NOTIFY sharingEnabledChanged)
  28. Q_PROPERTY(bool publicLinkSharesEnabled READ publicLinkSharesEnabled NOTIFY publicLinkSharesEnabledChanged)
  29. Q_PROPERTY(bool userGroupSharingEnabled READ userGroupSharingEnabled NOTIFY userGroupSharingEnabledChanged)
  30. Q_PROPERTY(bool canShare READ canShare NOTIFY sharePermissionsChanged)
  31. Q_PROPERTY(bool fetchOngoing READ fetchOngoing NOTIFY fetchOngoingChanged)
  32. Q_PROPERTY(bool hasInitialShareFetchCompleted READ hasInitialShareFetchCompleted NOTIFY hasInitialShareFetchCompletedChanged)
  33. Q_PROPERTY(QVariantList sharees READ sharees NOTIFY shareesChanged)
  34. public:
  35. enum Roles {
  36. ShareRole = Qt::UserRole + 1,
  37. ShareTypeRole,
  38. ShareIdRole,
  39. IconUrlRole,
  40. AvatarUrlRole,
  41. LinkRole,
  42. LinkShareNameRole,
  43. LinkShareLabelRole,
  44. NoteEnabledRole,
  45. NoteRole,
  46. ExpireDateEnabledRole,
  47. ExpireDateEnforcedRole,
  48. ExpireDateRole,
  49. EnforcedMaximumExpireDateRole,
  50. PasswordProtectEnabledRole,
  51. PasswordRole,
  52. PasswordEnforcedRole,
  53. EditingAllowedRole,
  54. IsSecureFileDropLinkRole,
  55. };
  56. Q_ENUM(Roles)
  57. /**
  58. * Possible share types
  59. * Need to be in sync with Share::ShareType.
  60. * We use this in QML.
  61. */
  62. enum ShareType {
  63. ShareTypeUser = Share::TypeUser,
  64. ShareTypeGroup = Share::TypeGroup,
  65. ShareTypeLink = Share::TypeLink,
  66. ShareTypeEmail = Share::TypeEmail,
  67. ShareTypeRemote = Share::TypeRemote,
  68. ShareTypeCircle = Share::TypeCircle,
  69. ShareTypeRoom = Share::TypeRoom,
  70. ShareTypePlaceholderLink = Share::TypePlaceholderLink,
  71. ShareTypeInternalLink = Share::TypeInternalLink,
  72. ShareTypeSecureFileDropPlaceholderLink = Share::TypeSecureFileDropPlaceholderLink,
  73. };
  74. Q_ENUM(ShareType);
  75. explicit ShareModel(QObject *parent = nullptr);
  76. [[nodiscard]] QVariant data(const QModelIndex &index, const int role) const override;
  77. [[nodiscard]] int rowCount(const QModelIndex &parent = QModelIndex()) const override;
  78. [[nodiscard]] QHash<int, QByteArray> roleNames() const override;
  79. [[nodiscard]] AccountState *accountState() const;
  80. [[nodiscard]] QString localPath() const;
  81. [[nodiscard]] bool accountConnected() const;
  82. [[nodiscard]] bool sharingEnabled() const;
  83. [[nodiscard]] bool publicLinkSharesEnabled() const;
  84. [[nodiscard]] bool userGroupSharingEnabled() const;
  85. [[nodiscard]] bool canShare() const;
  86. [[nodiscard]] bool fetchOngoing() const;
  87. [[nodiscard]] bool hasInitialShareFetchCompleted() const;
  88. [[nodiscard]] QVariantList sharees() const;
  89. signals:
  90. void localPathChanged();
  91. void accountStateChanged();
  92. void accountConnectedChanged();
  93. void sharingEnabledChanged();
  94. void publicLinkSharesEnabledChanged();
  95. void userGroupSharingEnabledChanged();
  96. void sharePermissionsChanged();
  97. void lockExpireStringChanged();
  98. void fetchOngoingChanged();
  99. void hasInitialShareFetchCompletedChanged();
  100. void shareesChanged();
  101. void internalLinkReady();
  102. void serverError(const int code, const QString &message);
  103. void passwordSetError(const QString &shareId, const int code, const QString &message);
  104. void requestPasswordForLinkShare();
  105. void requestPasswordForEmailSharee(const OCC::ShareePtr &sharee);
  106. void sharesChanged();
  107. public slots:
  108. void setAccountState(OCC::AccountState *accountState);
  109. void setLocalPath(const QString &localPath);
  110. void createNewLinkShare() const;
  111. void createNewLinkShareWithPassword(const QString &password) const;
  112. void createNewUserGroupShare(const OCC::ShareePtr &sharee);
  113. void createNewUserGroupShareFromQml(const QVariant &sharee);
  114. void createNewUserGroupShareWithPassword(const OCC::ShareePtr &sharee, const QString &password) const;
  115. void createNewUserGroupShareWithPasswordFromQml(const QVariant &sharee, const QString &password) const;
  116. void deleteShare(const OCC::SharePtr &share) const;
  117. void deleteShareFromQml(const QVariant &share) const;
  118. void toggleShareAllowEditing(const OCC::SharePtr &share, const bool enable) const;
  119. void toggleShareAllowEditingFromQml(const QVariant &share, const bool enable) const;
  120. void toggleShareAllowResharing(const OCC::SharePtr &share, const bool enable) const;
  121. void toggleShareAllowResharingFromQml(const QVariant &share, const bool enable) const;
  122. void toggleSharePasswordProtect(const OCC::SharePtr &share, const bool enable);
  123. void toggleSharePasswordProtectFromQml(const QVariant &share, const bool enable);
  124. void toggleShareExpirationDate(const OCC::SharePtr &share, const bool enable) const;
  125. void toggleShareExpirationDateFromQml(const QVariant &share, const bool enable) const;
  126. void toggleShareNoteToRecipient(const OCC::SharePtr &share, const bool enable) const;
  127. void toggleShareNoteToRecipientFromQml(const QVariant &share, const bool enable) const;
  128. void setLinkShareLabel(const QSharedPointer<OCC::LinkShare> &linkShare, const QString &label) const;
  129. void setLinkShareLabelFromQml(const QVariant &linkShare, const QString &label) const;
  130. void setShareExpireDate(const OCC::SharePtr &share, const qint64 milliseconds) const;
  131. // Needed as ints in QML are 32 bits so we need to use a QVariant
  132. void setShareExpireDateFromQml(const QVariant &share, const QVariant milliseconds) const;
  133. void setSharePassword(const OCC::SharePtr &share, const QString &password);
  134. void setSharePasswordFromQml(const QVariant &share, const QString &password);
  135. void setShareNote(const OCC::SharePtr &share, const QString &note) const;
  136. void setShareNoteFromQml(const QVariant &share, const QString &note) const;
  137. private slots:
  138. void resetData();
  139. void updateData();
  140. void initShareManager();
  141. void handlePlaceholderLinkShare();
  142. void handleSecureFileDropLinkShare();
  143. void handleLinkShare();
  144. void setupInternalLinkShare();
  145. void slotPropfindReceived(const QVariantMap &result);
  146. void slotServerError(const int code, const QString &message);
  147. void slotAddShare(const OCC::SharePtr &share);
  148. void slotRemoveShareWithId(const QString &shareId);
  149. void slotSharesFetched(const QList<OCC::SharePtr> &shares);
  150. void slotAddSharee(const OCC::ShareePtr &sharee);
  151. void slotRemoveSharee(const OCC::ShareePtr &sharee);
  152. void slotSharePermissionsSet(const QString &shareId);
  153. void slotSharePasswordSet(const QString &shareId);
  154. void slotShareNoteSet(const QString &shareId);
  155. void slotShareNameSet(const QString &shareId);
  156. void slotShareLabelSet(const QString &shareId);
  157. void slotShareExpireDateSet(const QString &shareId);
  158. private:
  159. [[nodiscard]] QString displayStringForShare(const SharePtr &share) const;
  160. [[nodiscard]] QString iconUrlForShare(const SharePtr &share) const;
  161. [[nodiscard]] QString avatarUrlForShare(const SharePtr &share) const;
  162. [[nodiscard]] long long enforcedMaxExpireDateForShare(const SharePtr &share) const;
  163. [[nodiscard]] bool expireDateEnforcedForShare(const SharePtr &share) const;
  164. [[nodiscard]] bool validCapabilities() const;
  165. bool _fetchOngoing = false;
  166. bool _hasInitialShareFetchCompleted = false;
  167. SharePtr _placeholderLinkShare;
  168. SharePtr _internalLinkShare;
  169. SharePtr _secureFileDropPlaceholderLinkShare;
  170. QPointer<AccountState> _accountState;
  171. QPointer<Folder> _folder;
  172. QString _localPath;
  173. QString _sharePath;
  174. SharePermissions _maxSharingPermissions;
  175. QByteArray _numericFileId;
  176. bool _isEncryptedItem = false;
  177. bool _isSecureFileDropSupportedFolder = false;
  178. SyncJournalFileLockInfo _filelockState;
  179. QString _privateLinkUrl;
  180. QSharedPointer<ShareManager> _manager;
  181. QVector<SharePtr> _shares;
  182. QHash<QString, QPersistentModelIndex> _shareIdIndexHash;
  183. QHash<QString, QString> _shareIdRecentlySetPasswords;
  184. QVector<ShareePtr> _sharees;
  185. };
  186. } // namespace OCC