folderstatusmodel.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Copyright (C) by Klaas Freitag <freitag@kde.org>
  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 FOLDERSTATUSMODEL_H
  15. #define FOLDERSTATUSMODEL_H
  16. #include <accountfwd.h>
  17. #include <QAbstractItemModel>
  18. #include <QLoggingCategory>
  19. #include <QVector>
  20. #include <QElapsedTimer>
  21. #include <QPointer>
  22. class QNetworkReply;
  23. namespace OCC {
  24. Q_DECLARE_LOGGING_CATEGORY(lcFolderStatus)
  25. class Folder;
  26. class ProgressInfo;
  27. class LsColJob;
  28. /**
  29. * @brief The FolderStatusModel class
  30. * @ingroup gui
  31. */
  32. class FolderStatusModel : public QAbstractItemModel
  33. {
  34. Q_OBJECT
  35. public:
  36. enum {FileIdRole = Qt::UserRole+1};
  37. FolderStatusModel(QObject *parent = nullptr);
  38. ~FolderStatusModel() override;
  39. void setAccountState(const AccountState *accountState);
  40. Qt::ItemFlags flags(const QModelIndex &) const override;
  41. QVariant data(const QModelIndex &index, int role) const override;
  42. bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
  43. int columnCount(const QModelIndex &parent = QModelIndex()) const override;
  44. int rowCount(const QModelIndex &parent = QModelIndex()) const override;
  45. QModelIndex index(int row, int column = 0, const QModelIndex &parent = QModelIndex()) const override;
  46. QModelIndex parent(const QModelIndex &child) const override;
  47. bool canFetchMore(const QModelIndex &parent) const override;
  48. void fetchMore(const QModelIndex &parent) override;
  49. void resetAndFetch(const QModelIndex &parent);
  50. bool hasChildren(const QModelIndex &parent = QModelIndex()) const override;
  51. struct SubFolderInfo
  52. {
  53. Folder *_folder = nullptr;
  54. QString _name; // Folder name to be displayed in the UI
  55. QString _path; // Sub-folder path that should always point to a local filesystem's folder
  56. QString _e2eMangledName; // Mangled name that needs to be used when making fetch requests and should not be used for displaying in the UI
  57. QVector<int> _pathIdx;
  58. QVector<SubFolderInfo> _subs;
  59. qint64 _size = 0;
  60. bool _isExternal = false;
  61. bool _isEncrypted = false;
  62. bool _fetched = false; // If we did the LSCOL for this folder already
  63. QPointer<LsColJob> _fetchingJob; // Currently running LsColJob
  64. bool _hasError = false; // If the last fetching job ended in an error
  65. QString _lastErrorString;
  66. bool _fetchingLabel = false; // Whether a 'fetching in progress' label is shown.
  67. // undecided folders are the big folders that the user has not accepted yet
  68. bool _isUndecided = false;
  69. QByteArray _fileId; // the file id for this folder on the server.
  70. Qt::CheckState _checked = Qt::Checked;
  71. // Whether this has a FetchLabel subrow
  72. bool hasLabel() const;
  73. // Reset all subfolders and fetch status
  74. void resetSubs(FolderStatusModel *model, QModelIndex index);
  75. struct Progress
  76. {
  77. bool isNull() const
  78. {
  79. return _progressString.isEmpty() && _warningCount == 0 && _overallSyncString.isEmpty();
  80. }
  81. QString _progressString;
  82. QString _overallSyncString;
  83. int _warningCount = 0;
  84. int _overallPercent = 0;
  85. };
  86. Progress _progress;
  87. };
  88. QVector<SubFolderInfo> _folders;
  89. enum ItemType { RootFolder,
  90. SubFolder,
  91. AddButton,
  92. FetchLabel };
  93. ItemType classify(const QModelIndex &index) const;
  94. SubFolderInfo *infoForIndex(const QModelIndex &index) const;
  95. bool isAnyAncestorEncrypted(const QModelIndex &index) const;
  96. // If the selective sync check boxes were changed
  97. bool isDirty() { return _dirty; }
  98. /**
  99. * return a QModelIndex for the given path within the given folder.
  100. * Note: this method returns an invalid index if the path was not fetched from the server before
  101. */
  102. QModelIndex indexForPath(Folder *f, const QString &path) const;
  103. public slots:
  104. void slotUpdateFolderState(Folder *);
  105. void slotApplySelectiveSync();
  106. void resetFolders();
  107. void slotSyncAllPendingBigFolders();
  108. void slotSyncNoPendingBigFolders();
  109. void slotSetProgress(const ProgressInfo &progress);
  110. private slots:
  111. void slotUpdateDirectories(const QStringList &);
  112. void slotGatherPermissions(const QString &name, const QMap<QString, QString> &properties);
  113. void slotGatherEncryptionStatus(const QString &href, const QMap<QString, QString> &properties);
  114. void slotLscolFinishedWithError(QNetworkReply *r);
  115. void slotFolderSyncStateChange(Folder *f);
  116. void slotFolderScheduleQueueChanged();
  117. void slotNewBigFolder();
  118. /**
  119. * "In progress" labels for fetching data from the server are only
  120. * added after some time to avoid popping.
  121. */
  122. void slotShowFetchProgress();
  123. private:
  124. QStringList createBlackList(const OCC::FolderStatusModel::SubFolderInfo &root,
  125. const QStringList &oldBlackList) const;
  126. const AccountState *_accountState = nullptr;
  127. bool _dirty = false; // If the selective sync checkboxes were changed
  128. /**
  129. * Keeps track of items that are fetching data from the server.
  130. *
  131. * See slotShowPendingFetchProgress()
  132. */
  133. QMap<QPersistentModelIndex, QElapsedTimer> _fetchingItems;
  134. signals:
  135. void dirtyChanged();
  136. // Tell the view that this item should be expanded because it has an undecided item
  137. void suggestExpand(const QModelIndex &);
  138. friend struct SubFolderInfo;
  139. };
  140. } // namespace OCC
  141. Q_DECLARE_METATYPE(OCC::FolderStatusModel::SubFolderInfo*)
  142. #endif // FOLDERSTATUSMODEL_H