From 9172a5fc4ce89893e3ade16912cbad4907951394 Mon Sep 17 00:00:00 2001 From: Olivier Goffart Date: Mon, 31 Aug 2015 10:12:45 +0200 Subject: [PATCH] FolderStatusModel: add a function to get a QModelIndex from the path Will be usefull to solve #3704 --- src/gui/folderstatusmodel.cpp | 39 +++++++++++++++++++++++++++++++++++ src/gui/folderstatusmodel.h | 7 ++++++- 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/src/gui/folderstatusmodel.cpp b/src/gui/folderstatusmodel.cpp index 5435d61e9..d21d25e3b 100644 --- a/src/gui/folderstatusmodel.cpp +++ b/src/gui/folderstatusmodel.cpp @@ -330,6 +330,45 @@ FolderStatusModel::SubFolderInfo* FolderStatusModel::infoForIndex(const QModelIn } } +QModelIndex FolderStatusModel::indexForPath(Folder *f, const QString& path) const +{ + int slashPos = path.lastIndexOf('/'); + if (slashPos == -1) { + // first level folder + for (int i = 0; i < _folders.size(); ++i) { + if (_folders.at(i)._folder == f) { + for (int j = 0; j < _folders.at(i)._subs.size(); ++j) { + if (_folders.at(i)._subs.at(j)._name == path) { + return index(j, 0, index(i)); + } + } + return QModelIndex(); + } + } + return QModelIndex(); + } + + auto parent = indexForPath(f, path.left(slashPos)); + if (!parent.isValid()) + return parent; + + if (slashPos == path.size() - 1) { + // The slash is the last part, we found our index + return parent; + } + + auto parentInfo = infoForIndex(parent); + if (!parentInfo) { + return QModelIndex(); + } + for (int i = 0; i < parentInfo->_subs.size(); ++i) { + if (parentInfo->_subs.at(i)._name == path.mid(slashPos + 1)) { + return index(i, 0, parent); + } + } + + return QModelIndex(); +} QModelIndex FolderStatusModel::index(int row, int column, const QModelIndex& parent) const { diff --git a/src/gui/folderstatusmodel.h b/src/gui/folderstatusmodel.h index d849ee2ab..c19f0fe08 100644 --- a/src/gui/folderstatusmodel.h +++ b/src/gui/folderstatusmodel.h @@ -49,7 +49,6 @@ public: void fetchMore(const QModelIndex& parent) Q_DECL_OVERRIDE; bool hasChildren(const QModelIndex& parent = QModelIndex()) const Q_DECL_OVERRIDE; - struct SubFolderInfo { SubFolderInfo() : _folder(0), _size(0), _fetched(false), _fetching(false), _isUndecided(false), @@ -87,6 +86,12 @@ public: // If the selective sync check boxes were changed bool isDirty() { return _dirty; } + /** + * return a QModelIndex for the given path within the given folder. + * Note: this method returns an invalid index if the path was not fetch from the server before + */ + QModelIndex indexForPath(Folder *f, const QString &path) const; + public slots: void slotUpdateFolderState(Folder *); void slotApplySelectiveSync();