- for (int i = 0; i < count(); i++) {
- // Conversion to display string is necessary to deal with the '~' alias.
- // i.e. to acknowledge that ~/ is equivalent to /home/user/
- const QUrl tabUrl = tabPageAt(i)->activeViewContainer()->url();
- if (url == tabUrl ||
- url.toDisplayString(QUrl::StripTrailingSlash) == tabUrl.toDisplayString(QUrl::StripTrailingSlash)) {
- return i;
+ // The item might not be loaded yet even though it exists. So instead
+ // we check if the folder containing the item is showing its contents.
+ const QUrl dirContainingItem(item.adjusted(QUrl::RemoveFilename | QUrl::StripTrailingSlash));
+
+ // The dirContainingItem is either open directly or expanded in a tree-style view mode.
+ // Is dirContainingitem the base url of a view?
+ auto viewOpenAtContainingDirectory = viewOpenAtDirectory(dirContainingItem);
+ if (viewOpenAtContainingDirectory.has_value()) {
+ return viewOpenAtContainingDirectory;
+ }
+
+ // Is dirContainingItem expanded in some tree-style view?
+ // The rest of this method is about figuring this out.
+
+ int i = currentIndex();
+ if (i < 0) {
+ return std::nullopt;
+ }
+ // loop over the tabs starting from the current one
+ do {
+ const auto tabPage = tabPageAt(i);
+ if (tabPage->primaryViewContainer()->url().isParentOf(item)) {
+ const KFileItem fileItemContainingItem = tabPage->primaryViewContainer()->view()->items().findByUrl(dirContainingItem);
+ if (!fileItemContainingItem.isNull() && tabPage->primaryViewContainer()->view()->isExpanded(fileItemContainingItem)) {
+ return std::optional(ViewIndex{i, true});
+ }
+ }
+
+ if (tabPage->splitViewEnabled() && tabPage->secondaryViewContainer()->url().isParentOf(item)) {
+ const KFileItem fileItemContainingItem = tabPage->secondaryViewContainer()->view()->items().findByUrl(dirContainingItem);
+ if (!fileItemContainingItem.isNull() && tabPage->secondaryViewContainer()->view()->isExpanded(fileItemContainingItem)) {
+ return std::optional(ViewIndex{i, false});
+ }