- DolphinView* view = mainWindow()->activeView();
- m_dirLister->stop();
- m_dirLister->openUrl(view->url());
- connect(view, SIGNAL(urlChanged(const KUrl&)),
- this, SLOT(updatePosition(const KUrl&)));
+ disconnect(m_dirLister, SIGNAL(completed()),
+ this, SLOT(loadSubTree()));
+
+ QItemSelectionModel* selModel = m_treeView->selectionModel();
+ selModel->clearSelection();
+
+ if (m_leafDir.isParentOf(m_dirLister->url())) {
+ // The leaf directory is not a child of the base URL, hence
+ // no sub directory must be loaded or selected.
+ return;
+ }
+
+ const QModelIndex index = m_dirModel->indexForUrl(m_leafDir);
+ if (index.isValid()) {
+ // the item with the given URL is already part of the model
+ const QModelIndex proxyIndex = m_proxyModel->mapFromSource(index);
+ m_treeView->scrollTo(proxyIndex);
+ selModel->setCurrentIndex(proxyIndex, QItemSelectionModel::Select);
+ } else {
+ // Load all sub directories that need to get expanded for making
+ // the leaf directory visible. The slot triggerExpanding() will
+ // get invoked if the expanding has been finished.
+ m_dirModel->expandToUrl(m_leafDir);
+ }
+}
+
+void TreeViewSidebarPage::loadTree(const KUrl& url)
+{
+ Q_ASSERT(m_dirLister != 0);
+ m_leafDir = url;
+
+ // adjust the root of the tree to the base place
+ KFilePlacesModel* placesModel = DolphinSettings::instance().placesModel();
+ KUrl baseUrl = placesModel->url(placesModel->closestItem(url));
+ if (!baseUrl.isValid()) {
+ // it's possible that no closest item is available and hence an
+ // empty URL is returned
+ baseUrl = url;
+ }
+
+ connect(m_dirLister, SIGNAL(completed()),
+ this, SLOT(loadSubTree()));
+ if (m_dirLister->url() != baseUrl) {
+ m_dirLister->stop();
+ m_dirLister->openUrl(baseUrl);
+ } else {
+ loadSubTree();
+ }