folderstatusmodel.h 5.6 KB

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