2 * SPDX-FileCopyrightText: 2007 Peter Penz <peter.penz19@gmail.com>
4 * SPDX-License-Identifier: GPL-2.0-or-later
7 #include "dolphinviewcontainer.h"
9 #include "dolphin_compactmodesettings.h"
10 #include "dolphin_contentdisplaysettings.h"
11 #include "dolphin_detailsmodesettings.h"
12 #include "dolphin_generalsettings.h"
13 #include "dolphin_iconsmodesettings.h"
14 #include "dolphindebug.h"
15 #include "dolphinplacesmodelsingleton.h"
16 #include "filterbar/filterbar.h"
18 #include "search/dolphinsearchbox.h"
19 #include "selectionmode/topbar.h"
20 #include "statusbar/dolphinstatusbar.h"
22 #include <KActionCollection>
24 #include <KActivities/ResourceInstance>
26 #include <KFileItemActions>
27 #include <KFilePlacesModel>
28 #include <kio_version.h>
29 #if KIO_VERSION >= QT_VERSION_CHECK(5, 98, 0)
30 #include <KIO/JobUiDelegateFactory>
32 #include <KIO/JobUiDelegate>
34 #include <KApplicationTrader>
35 #include <KIO/OpenUrlJob>
36 #include <KLocalizedString>
37 #include <KMessageWidget>
38 #include <KProtocolManager>
41 #include <QApplication>
42 #include <QDesktopServices>
44 #include <QGridLayout>
45 #include <QGuiApplication>
46 #include <QRegularExpression>
50 // An overview of the widgets contained by this ViewContainer
51 struct LayoutStructure
{
53 int messageWidget
= 1;
54 int selectionModeTopBar
= 2;
56 int selectionModeBottomBar
= 4;
60 constexpr LayoutStructure positionFor
;
62 DolphinViewContainer::DolphinViewContainer(const QUrl
&url
, QWidget
*parent
)
64 , m_topLayout(nullptr)
65 , m_urlNavigator
{new DolphinUrlNavigator(url
)}
66 , m_urlNavigatorConnected
{nullptr}
67 , m_searchBox(nullptr)
68 , m_searchModeEnabled(false)
69 , m_messageWidget(nullptr)
70 , m_selectionModeTopBar
{nullptr}
72 , m_filterBar(nullptr)
73 , m_selectionModeBottomBar
{nullptr}
74 , m_statusBar(nullptr)
75 , m_statusBarTimer(nullptr)
76 , m_statusBarTimestamp()
77 , m_autoGrabFocus(true)
79 , m_activityResourceInstance(nullptr)
84 m_topLayout
= new QGridLayout(this);
85 m_topLayout
->setSpacing(0);
86 m_topLayout
->setContentsMargins(0, 0, 0, 0);
88 m_searchBox
= new DolphinSearchBox(this);
90 connect(m_searchBox
, &DolphinSearchBox::activated
, this, &DolphinViewContainer::activate
);
91 connect(m_searchBox
, &DolphinSearchBox::closeRequest
, this, &DolphinViewContainer::closeSearchBox
);
92 connect(m_searchBox
, &DolphinSearchBox::searchRequest
, this, &DolphinViewContainer::startSearching
);
93 connect(m_searchBox
, &DolphinSearchBox::focusViewRequest
, this, &DolphinViewContainer::requestFocus
);
94 m_searchBox
->setWhatsThis(xi18nc("@info:whatsthis findbar",
95 "<para>This helps you find files and folders. Enter a <emphasis>"
96 "search term</emphasis> and specify search settings with the "
97 "buttons at the bottom:<list><item>Filename/Content: "
98 "Does the item you are looking for contain the search terms "
99 "within its filename or its contents?<nl/>The contents of images, "
100 "audio files and videos will not be searched.</item><item>"
101 "From Here/Everywhere: Do you want to search in this "
102 "folder and its sub-folders or everywhere?</item><item>"
103 "More Options: Click this to search by media type, access "
104 "time or rating.</item><item>More Search Tools: Install other "
105 "means to find an item.</item></list></para>"));
107 m_messageWidget
= new KMessageWidget(this);
108 m_messageWidget
->setCloseButtonVisible(true);
109 m_messageWidget
->hide();
113 // We must be logged in as the root user; show a big scary warning
114 showMessage(i18n("Running Dolphin as root can be dangerous. Please be careful."), Warning
);
118 // Initialize filter bar
119 m_filterBar
= new FilterBar(this);
120 m_filterBar
->setVisible(GeneralSettings::filterBar());
122 connect(m_filterBar
, &FilterBar::filterChanged
, this, &DolphinViewContainer::setNameFilter
);
123 connect(m_filterBar
, &FilterBar::closeRequest
, this, &DolphinViewContainer::closeFilterBar
);
124 connect(m_filterBar
, &FilterBar::focusViewRequest
, this, &DolphinViewContainer::requestFocus
);
126 // Initialize the main view
127 m_view
= new DolphinView(url
, this);
128 connect(m_view
, &DolphinView::urlChanged
, m_filterBar
, &FilterBar::clearIfUnlocked
);
129 connect(m_view
, &DolphinView::urlChanged
, m_messageWidget
, &KMessageWidget::hide
);
130 // m_urlNavigator stays in sync with m_view's location changes and
131 // keeps track of them so going back and forth in the history works.
132 connect(m_view
, &DolphinView::urlChanged
, m_urlNavigator
.get(), &DolphinUrlNavigator::setLocationUrl
);
133 connect(m_urlNavigator
.get(), &DolphinUrlNavigator::urlChanged
, this, &DolphinViewContainer::slotUrlNavigatorLocationChanged
);
134 connect(m_urlNavigator
.get(), &DolphinUrlNavigator::urlAboutToBeChanged
, this, &DolphinViewContainer::slotUrlNavigatorLocationAboutToBeChanged
);
135 connect(m_urlNavigator
.get(), &DolphinUrlNavigator::urlSelectionRequested
, this, &DolphinViewContainer::slotUrlSelectionRequested
);
136 connect(m_view
, &DolphinView::writeStateChanged
, this, &DolphinViewContainer::writeStateChanged
);
137 connect(m_view
, &DolphinView::requestItemInfo
, this, &DolphinViewContainer::showItemInfo
);
138 connect(m_view
, &DolphinView::itemActivated
, this, &DolphinViewContainer::slotItemActivated
);
139 connect(m_view
, &DolphinView::fileMiddleClickActivated
, this, &DolphinViewContainer::slotfileMiddleClickActivated
);
140 connect(m_view
, &DolphinView::itemsActivated
, this, &DolphinViewContainer::slotItemsActivated
);
141 connect(m_view
, &DolphinView::redirection
, this, &DolphinViewContainer::redirect
);
142 connect(m_view
, &DolphinView::directoryLoadingStarted
, this, &DolphinViewContainer::slotDirectoryLoadingStarted
);
143 connect(m_view
, &DolphinView::directoryLoadingCompleted
, this, &DolphinViewContainer::slotDirectoryLoadingCompleted
);
144 connect(m_view
, &DolphinView::directoryLoadingCanceled
, this, &DolphinViewContainer::slotDirectoryLoadingCanceled
);
145 connect(m_view
, &DolphinView::itemCountChanged
, this, &DolphinViewContainer::delayedStatusBarUpdate
);
146 connect(m_view
, &DolphinView::directoryLoadingProgress
, this, &DolphinViewContainer::updateDirectoryLoadingProgress
);
147 connect(m_view
, &DolphinView::directorySortingProgress
, this, &DolphinViewContainer::updateDirectorySortingProgress
);
148 connect(m_view
, &DolphinView::selectionChanged
, this, &DolphinViewContainer::delayedStatusBarUpdate
);
149 connect(m_view
, &DolphinView::errorMessage
, this, &DolphinViewContainer::showErrorMessage
);
150 connect(m_view
, &DolphinView::urlIsFileError
, this, &DolphinViewContainer::slotUrlIsFileError
);
151 connect(m_view
, &DolphinView::activated
, this, &DolphinViewContainer::activate
);
152 connect(m_view
, &DolphinView::hiddenFilesShownChanged
, this, &DolphinViewContainer::slotHiddenFilesShownChanged
);
153 connect(m_view
, &DolphinView::sortHiddenLastChanged
, this, &DolphinViewContainer::slotSortHiddenLastChanged
);
154 connect(m_view
, &DolphinView::currentDirectoryRemoved
, this, &DolphinViewContainer::slotCurrentDirectoryRemoved
);
156 // Initialize status bar
157 m_statusBar
= new DolphinStatusBar(this);
158 m_statusBar
->setUrl(m_view
->url());
159 m_statusBar
->setZoomLevel(m_view
->zoomLevel());
160 connect(m_view
, &DolphinView::urlChanged
, m_statusBar
, &DolphinStatusBar::setUrl
);
161 connect(m_view
, &DolphinView::zoomLevelChanged
, m_statusBar
, &DolphinStatusBar::setZoomLevel
);
162 connect(m_view
, &DolphinView::infoMessage
, m_statusBar
, &DolphinStatusBar::setText
);
163 connect(m_view
, &DolphinView::operationCompletedMessage
, m_statusBar
, &DolphinStatusBar::setText
);
164 connect(m_view
, &DolphinView::statusBarTextChanged
, m_statusBar
, &DolphinStatusBar::setDefaultText
);
165 connect(m_view
, &DolphinView::statusBarTextChanged
, m_statusBar
, &DolphinStatusBar::resetToDefaultText
);
166 connect(m_statusBar
, &DolphinStatusBar::stopPressed
, this, &DolphinViewContainer::stopDirectoryLoading
);
167 connect(m_statusBar
, &DolphinStatusBar::zoomLevelChanged
, this, &DolphinViewContainer::slotStatusBarZoomLevelChanged
);
169 m_statusBarTimer
= new QTimer(this);
170 m_statusBarTimer
->setSingleShot(true);
171 m_statusBarTimer
->setInterval(300);
172 connect(m_statusBarTimer
, &QTimer::timeout
, this, &DolphinViewContainer::updateStatusBar
);
174 KIO::FileUndoManager
*undoManager
= KIO::FileUndoManager::self();
175 connect(undoManager
, &KIO::FileUndoManager::jobRecordingFinished
, this, &DolphinViewContainer::delayedStatusBarUpdate
);
177 m_topLayout
->addWidget(m_searchBox
, positionFor
.searchBox
, 0);
178 m_topLayout
->addWidget(m_messageWidget
, positionFor
.messageWidget
, 0);
179 m_topLayout
->addWidget(m_view
, positionFor
.view
, 0);
180 m_topLayout
->addWidget(m_filterBar
, positionFor
.filterBar
, 0);
181 m_topLayout
->addWidget(m_statusBar
, positionFor
.statusBar
, 0);
183 setSearchModeEnabled(isSearchUrl(url
));
185 // Update view as the ContentDisplaySettings change
186 // this happens here and not in DolphinView as DolphinviewContainer and DolphinView are not in the same build target ATM
187 connect(ContentDisplaySettings::self(), &KCoreConfigSkeleton::configChanged
, m_view
, &DolphinView::reload
);
189 KFilePlacesModel
*placesModel
= DolphinPlacesModelSingleton::instance().placesModel();
190 connect(placesModel
, &KFilePlacesModel::dataChanged
, this, &DolphinViewContainer::slotPlacesModelChanged
);
191 connect(placesModel
, &KFilePlacesModel::rowsInserted
, this, &DolphinViewContainer::slotPlacesModelChanged
);
192 connect(placesModel
, &KFilePlacesModel::rowsRemoved
, this, &DolphinViewContainer::slotPlacesModelChanged
);
194 connect(this, &DolphinViewContainer::searchModeEnabledChanged
, this, &DolphinViewContainer::captionChanged
);
196 // Initialize kactivities resource instance
199 m_activityResourceInstance
= new KActivities::ResourceInstance(window()->winId(), url
);
200 m_activityResourceInstance
->setParent(this);
204 DolphinViewContainer::~DolphinViewContainer()
208 QUrl
DolphinViewContainer::url() const
210 return m_view
->url();
213 KFileItem
DolphinViewContainer::rootItem() const
215 return m_view
->rootItem();
218 void DolphinViewContainer::setActive(bool active
)
220 m_searchBox
->setActive(active
);
221 if (m_urlNavigatorConnected
) {
222 m_urlNavigatorConnected
->setActive(active
);
224 m_view
->setActive(active
);
228 m_activityResourceInstance
->notifyFocusedIn();
230 m_activityResourceInstance
->notifyFocusedOut();
235 bool DolphinViewContainer::isActive() const
237 return m_view
->isActive();
240 void DolphinViewContainer::setAutoGrabFocus(bool grab
)
242 m_autoGrabFocus
= grab
;
245 bool DolphinViewContainer::autoGrabFocus() const
247 return m_autoGrabFocus
;
250 QString
DolphinViewContainer::currentSearchText() const
252 return m_searchBox
->text();
255 const DolphinStatusBar
*DolphinViewContainer::statusBar() const
260 DolphinStatusBar
*DolphinViewContainer::statusBar()
265 const DolphinUrlNavigator
*DolphinViewContainer::urlNavigator() const
267 return m_urlNavigatorConnected
;
270 DolphinUrlNavigator
*DolphinViewContainer::urlNavigator()
272 return m_urlNavigatorConnected
;
275 const DolphinUrlNavigator
*DolphinViewContainer::urlNavigatorInternalWithHistory() const
277 return m_urlNavigator
.get();
280 DolphinUrlNavigator
*DolphinViewContainer::urlNavigatorInternalWithHistory()
282 return m_urlNavigator
.get();
285 const DolphinView
*DolphinViewContainer::view() const
290 DolphinView
*DolphinViewContainer::view()
295 void DolphinViewContainer::connectUrlNavigator(DolphinUrlNavigator
*urlNavigator
)
297 Q_CHECK_PTR(urlNavigator
);
298 Q_ASSERT(!m_urlNavigatorConnected
);
299 Q_ASSERT(m_urlNavigator
.get() != urlNavigator
);
302 urlNavigator
->setLocationUrl(m_view
->url());
303 urlNavigator
->setShowHiddenFolders(m_view
->hiddenFilesShown());
304 urlNavigator
->setSortHiddenFoldersLast(m_view
->sortHiddenLast());
305 if (m_urlNavigatorVisualState
) {
306 urlNavigator
->setVisualState(*m_urlNavigatorVisualState
.get());
307 m_urlNavigatorVisualState
.reset();
309 urlNavigator
->setActive(isActive());
311 // Url changes are still done via m_urlNavigator.
312 connect(urlNavigator
, &DolphinUrlNavigator::urlChanged
, m_urlNavigator
.get(), &DolphinUrlNavigator::setLocationUrl
);
313 connect(urlNavigator
, &DolphinUrlNavigator::urlsDropped
, this, [=](const QUrl
&destination
, QDropEvent
*event
) {
314 m_view
->dropUrls(destination
, event
, urlNavigator
->dropWidget());
316 // Aside from these, only visual things need to be connected.
317 connect(m_view
, &DolphinView::urlChanged
, urlNavigator
, &DolphinUrlNavigator::setLocationUrl
);
318 connect(urlNavigator
, &DolphinUrlNavigator::activated
, this, &DolphinViewContainer::activate
);
320 m_urlNavigatorConnected
= urlNavigator
;
323 void DolphinViewContainer::disconnectUrlNavigator()
325 if (!m_urlNavigatorConnected
) {
329 disconnect(m_urlNavigatorConnected
, &DolphinUrlNavigator::urlChanged
, m_urlNavigator
.get(), &DolphinUrlNavigator::setLocationUrl
);
330 disconnect(m_urlNavigatorConnected
, &DolphinUrlNavigator::urlsDropped
, this, nullptr);
331 disconnect(m_view
, &DolphinView::urlChanged
, m_urlNavigatorConnected
, &DolphinUrlNavigator::setLocationUrl
);
332 disconnect(m_urlNavigatorConnected
, &DolphinUrlNavigator::activated
, this, &DolphinViewContainer::activate
);
334 m_urlNavigatorVisualState
= m_urlNavigatorConnected
->visualState();
335 m_urlNavigatorConnected
= nullptr;
338 void DolphinViewContainer::setSelectionModeEnabled(bool enabled
, KActionCollection
*actionCollection
, SelectionMode::BottomBar::Contents bottomBarContents
)
340 const bool wasEnabled
= m_view
->selectionMode();
341 m_view
->setSelectionModeEnabled(enabled
);
345 return; // nothing to do here
347 Q_CHECK_PTR(m_selectionModeTopBar
); // there is no point in disabling selectionMode when it wasn't even enabled once.
348 Q_CHECK_PTR(m_selectionModeBottomBar
);
349 if (m_selectionModeTopBar
->isAncestorOf(QApplication::focusWidget()) || m_selectionModeBottomBar
->isAncestorOf(QApplication::focusWidget())) {
352 m_selectionModeTopBar
->setVisible(false, WithAnimation
);
353 m_selectionModeBottomBar
->setVisible(false, WithAnimation
);
354 Q_EMIT
selectionModeChanged(false);
358 if (!m_selectionModeTopBar
) {
359 // Changing the location will disable selection mode.
360 connect(m_urlNavigator
.get(), &DolphinUrlNavigator::urlChanged
, this, [this]() {
361 setSelectionModeEnabled(false);
364 m_selectionModeTopBar
= new SelectionMode::TopBar(this); // will be created hidden
365 connect(m_selectionModeTopBar
, &SelectionMode::TopBar::selectionModeLeavingRequested
, this, [this]() {
366 setSelectionModeEnabled(false);
368 m_topLayout
->addWidget(m_selectionModeTopBar
, positionFor
.selectionModeTopBar
, 0);
371 if (!m_selectionModeBottomBar
) {
372 m_selectionModeBottomBar
= new SelectionMode::BottomBar(actionCollection
, this);
373 connect(m_view
, &DolphinView::selectionChanged
, this, [this](const KFileItemList
&selection
) {
374 m_selectionModeBottomBar
->slotSelectionChanged(selection
, m_view
->url());
376 connect(m_selectionModeBottomBar
, &SelectionMode::BottomBar::error
, this, [this](const QString
&errorMessage
) {
377 showErrorMessage(errorMessage
);
379 connect(m_selectionModeBottomBar
, &SelectionMode::BottomBar::selectionModeLeavingRequested
, this, [this]() {
380 setSelectionModeEnabled(false);
382 m_topLayout
->addWidget(m_selectionModeBottomBar
, positionFor
.selectionModeBottomBar
, 0);
384 m_selectionModeBottomBar
->resetContents(bottomBarContents
);
385 if (bottomBarContents
== SelectionMode::BottomBar::GeneralContents
) {
386 m_selectionModeBottomBar
->slotSelectionChanged(m_view
->selectedItems(), m_view
->url());
390 m_selectionModeTopBar
->setVisible(true, WithAnimation
);
391 m_selectionModeBottomBar
->setVisible(true, WithAnimation
);
392 Q_EMIT
selectionModeChanged(true);
396 bool DolphinViewContainer::isSelectionModeEnabled() const
398 const bool isEnabled
= m_view
->selectionMode();
400 // We can't assert that the bars are invisible only because the selection mode is disabled because the hide animation might still be playing.
401 && (!m_selectionModeBottomBar
|| !m_selectionModeBottomBar
->isEnabled() || !m_selectionModeBottomBar
->isVisible()
402 || m_selectionModeBottomBar
->contents() == SelectionMode::BottomBar::PasteContents
))
403 || (isEnabled
&& m_selectionModeTopBar
404 && m_selectionModeTopBar
->isVisible()
405 // The bottom bar is either visible or was hidden because it has nothing to show in GeneralContents mode e.g. because no items are selected.
406 && m_selectionModeBottomBar
407 && (m_selectionModeBottomBar
->isVisible() || m_selectionModeBottomBar
->contents() == SelectionMode::BottomBar::GeneralContents
)));
411 void DolphinViewContainer::slotSplitTabDisabled()
413 if (m_selectionModeBottomBar
) {
414 m_selectionModeBottomBar
->slotSplitTabDisabled();
418 void DolphinViewContainer::showMessage(const QString
&msg
, MessageType type
)
424 m_messageWidget
->setText(msg
);
426 // TODO: wrap at arbitrary character positions once QLabel can do this
427 // https://bugreports.qt.io/browse/QTBUG-1276
428 m_messageWidget
->setWordWrap(true);
432 m_messageWidget
->setMessageType(KMessageWidget::Information
);
435 m_messageWidget
->setMessageType(KMessageWidget::Warning
);
438 m_messageWidget
->setMessageType(KMessageWidget::Error
);
445 m_messageWidget
->setWordWrap(false);
446 const int unwrappedWidth
= m_messageWidget
->sizeHint().width();
447 m_messageWidget
->setWordWrap(unwrappedWidth
> size().width());
449 if (m_messageWidget
->isVisible()) {
450 m_messageWidget
->hide();
452 m_messageWidget
->animatedShow();
455 void DolphinViewContainer::readSettings()
457 // The startup settings should (only) get applied if they have been
458 // modified by the user. Otherwise keep the (possibly) different current
459 // setting of the filterbar.
460 if (GeneralSettings::modifiedStartupSettings()) {
461 setFilterBarVisible(GeneralSettings::filterBar());
464 m_view
->readSettings();
465 m_statusBar
->readSettings();
468 bool DolphinViewContainer::isFilterBarVisible() const
470 return m_filterBar
->isVisible();
473 void DolphinViewContainer::setSearchModeEnabled(bool enabled
)
475 m_searchBox
->setVisible(enabled
);
478 const QUrl
&locationUrl
= m_urlNavigator
->locationUrl();
479 m_searchBox
->fromSearchUrl(locationUrl
);
482 if (enabled
== isSearchModeEnabled()) {
483 if (enabled
&& !m_searchBox
->hasFocus()) {
484 m_searchBox
->setFocus();
485 m_searchBox
->selectAll();
491 m_view
->setViewPropertiesContext(QString());
493 // Restore the URL for the URL navigator. If Dolphin has been
494 // started with a search-URL, the home URL is used as fallback.
495 QUrl url
= m_searchBox
->searchPath();
496 if (url
.isEmpty() || !url
.isValid() || isSearchUrl(url
)) {
497 url
= Dolphin::homeUrl();
499 m_urlNavigatorConnected
->setLocationUrl(url
);
502 m_searchModeEnabled
= enabled
;
504 Q_EMIT
searchModeEnabledChanged(enabled
);
507 bool DolphinViewContainer::isSearchModeEnabled() const
509 return m_searchModeEnabled
;
512 QString
DolphinViewContainer::placesText() const
516 if (isSearchModeEnabled()) {
517 text
= i18n("Search for %1 in %2", m_searchBox
->text(), m_searchBox
->searchPath().fileName());
519 text
= url().adjusted(QUrl::StripTrailingSlash
).fileName();
520 if (text
.isEmpty()) {
523 if (text
.isEmpty()) {
524 text
= url().scheme();
531 void DolphinViewContainer::reload()
534 m_messageWidget
->hide();
537 QString
DolphinViewContainer::captionWindowTitle() const
539 if (GeneralSettings::showFullPathInTitlebar() && !isSearchModeEnabled()) {
540 if (!url().isLocalFile()) {
541 return url().adjusted(QUrl::StripTrailingSlash
).toString();
543 return url().adjusted(QUrl::StripTrailingSlash
).path();
545 return DolphinViewContainer::caption();
549 QString
DolphinViewContainer::caption() const
551 if (isSearchModeEnabled()) {
552 if (currentSearchText().isEmpty()) {
553 return i18n("Search");
555 return i18n("Search for %1", currentSearchText());
559 KFilePlacesModel
*placesModel
= DolphinPlacesModelSingleton::instance().placesModel();
560 const QString pattern
= url().adjusted(QUrl::StripTrailingSlash
).toString(QUrl::FullyEncoded
).append("/?");
561 const auto &matchedPlaces
=
562 placesModel
->match(placesModel
->index(0, 0), KFilePlacesModel::UrlRole
, QRegularExpression::anchoredPattern(pattern
), 1, Qt::MatchRegularExpression
);
564 if (!matchedPlaces
.isEmpty()) {
565 return placesModel
->text(matchedPlaces
.first());
568 if (!url().isLocalFile()) {
569 QUrl adjustedUrl
= url().adjusted(QUrl::StripTrailingSlash
);
571 if (!adjustedUrl
.fileName().isEmpty()) {
572 caption
= adjustedUrl
.fileName();
573 } else if (!adjustedUrl
.path().isEmpty() && adjustedUrl
.path() != "/") {
574 caption
= adjustedUrl
.path();
575 } else if (!adjustedUrl
.host().isEmpty()) {
576 caption
= adjustedUrl
.host();
578 caption
= adjustedUrl
.toString();
583 QString fileName
= url().adjusted(QUrl::StripTrailingSlash
).fileName();
584 if (fileName
.isEmpty()) {
591 void DolphinViewContainer::setUrl(const QUrl
&newUrl
)
593 if (newUrl
!= m_urlNavigator
->locationUrl()) {
594 m_urlNavigator
->setLocationUrl(newUrl
);
598 m_activityResourceInstance
->setUri(newUrl
);
602 void DolphinViewContainer::setFilterBarVisible(bool visible
)
604 Q_ASSERT(m_filterBar
);
606 m_view
->hideToolTip(ToolTipManager::HideBehavior::Instantly
);
608 m_filterBar
->setFocus();
609 m_filterBar
->selectAll();
615 void DolphinViewContainer::delayedStatusBarUpdate()
617 if (m_statusBarTimer
->isActive() && (m_statusBarTimestamp
.elapsed() > 2000)) {
618 // No update of the statusbar has been done during the last 2 seconds,
619 // although an update has been requested. Trigger an immediate update.
620 m_statusBarTimer
->stop();
623 // Invoke updateStatusBar() with a small delay. This assures that
624 // when a lot of delayedStatusBarUpdates() are done in a short time,
625 // no bottleneck is given.
626 m_statusBarTimer
->start();
630 void DolphinViewContainer::updateStatusBar()
632 m_statusBarTimestamp
.start();
633 m_view
->requestStatusBarText();
636 void DolphinViewContainer::updateDirectoryLoadingProgress(int percent
)
638 if (m_statusBar
->progressText().isEmpty()) {
639 m_statusBar
->setProgressText(i18nc("@info:progress", "Loading folder…"));
641 m_statusBar
->setProgress(percent
);
644 void DolphinViewContainer::updateDirectorySortingProgress(int percent
)
646 if (m_statusBar
->progressText().isEmpty()) {
647 m_statusBar
->setProgressText(i18nc("@info:progress", "Sorting…"));
649 m_statusBar
->setProgress(percent
);
652 void DolphinViewContainer::slotDirectoryLoadingStarted()
654 if (isSearchUrl(url())) {
655 // Search KIO-slaves usually don't provide any progress information. Give
656 // a hint to the user that a searching is done:
658 m_statusBar
->setProgressText(i18nc("@info", "Searching…"));
659 m_statusBar
->setProgress(-1);
661 // Trigger an undetermined progress indication. The progress
662 // information in percent will be triggered by the percent() signal
663 // of the directory lister later.
664 m_statusBar
->setProgressText(QString());
665 updateDirectoryLoadingProgress(-1);
669 void DolphinViewContainer::slotDirectoryLoadingCompleted()
671 if (!m_statusBar
->progressText().isEmpty()) {
672 m_statusBar
->setProgressText(QString());
673 m_statusBar
->setProgress(100);
676 if (isSearchUrl(url()) && m_view
->itemsCount() == 0) {
677 // The dir lister has been completed on a Baloo-URI and no items have been found. Instead
678 // of showing the default status bar information ("0 items") a more helpful information is given:
679 m_statusBar
->setText(i18nc("@info:status", "No items found."));
685 void DolphinViewContainer::slotDirectoryLoadingCanceled()
687 if (!m_statusBar
->progressText().isEmpty()) {
688 m_statusBar
->setProgressText(QString());
689 m_statusBar
->setProgress(100);
692 m_statusBar
->setText(QString());
695 void DolphinViewContainer::slotUrlIsFileError(const QUrl
&url
)
697 const KFileItem
item(url
);
699 // Find out if the file can be opened in the view (for example, this is the
700 // case if the file is an archive). The mime type must be known for that.
701 item
.determineMimeType();
702 const QUrl
&folderUrl
= DolphinView::openItemAsFolderUrl(item
, true);
703 if (!folderUrl
.isEmpty()) {
706 slotItemActivated(item
);
710 void DolphinViewContainer::slotItemActivated(const KFileItem
&item
)
712 // It is possible to activate items on inactive views by
713 // drag & drop operations. Assure that activating an item always
714 // results in an active view.
715 m_view
->setActive(true);
717 const QUrl
&url
= DolphinView::openItemAsFolderUrl(item
, GeneralSettings::browseThroughArchives());
718 if (!url
.isEmpty()) {
719 const auto modifiers
= QGuiApplication::keyboardModifiers();
720 // keep in sync with KUrlNavigator::slotNavigatorButtonClicked
721 if (modifiers
& Qt::ControlModifier
&& modifiers
& Qt::ShiftModifier
) {
722 Q_EMIT
activeTabRequested(url
);
723 } else if (modifiers
& Qt::ControlModifier
) {
724 Q_EMIT
tabRequested(url
);
725 } else if (modifiers
& Qt::ShiftModifier
) {
726 Dolphin::openNewWindow({KFilePlacesModel::convertedUrl(url
)}, this);
733 KIO::OpenUrlJob
*job
= new KIO::OpenUrlJob(item
.targetUrl(), item
.mimetype());
734 #if KIO_VERSION >= QT_VERSION_CHECK(5, 98, 0)
735 job
->setUiDelegate(KIO::createDefaultJobUiDelegate(KJobUiDelegate::AutoHandlingEnabled
, this));
737 job
->setUiDelegate(new KIO::JobUiDelegate(KJobUiDelegate::AutoHandlingEnabled
, this));
739 job
->setShowOpenOrExecuteDialog(true);
740 connect(job
, &KIO::OpenUrlJob::finished
, this, &DolphinViewContainer::slotOpenUrlFinished
);
744 void DolphinViewContainer::slotfileMiddleClickActivated(const KFileItem
&item
)
746 KService::List services
= KApplicationTrader::queryByMimeType(item
.mimetype());
748 if (services
.length() >= 2) {
749 auto service
= services
.at(1);
751 KIO::ApplicationLauncherJob
*job
= new KIO::ApplicationLauncherJob(service
, this);
752 job
->setUrls({item
.url()});
754 #if KIO_VERSION >= QT_VERSION_CHECK(5, 98, 0)
755 job
->setUiDelegate(KIO::createDefaultJobUiDelegate(KJobUiDelegate::AutoHandlingEnabled
, this));
757 job
->setUiDelegate(new KIO::JobUiDelegate(KJobUiDelegate::AutoHandlingEnabled
, this));
759 connect(job
, &KIO::OpenUrlJob::finished
, this, &DolphinViewContainer::slotOpenUrlFinished
);
764 void DolphinViewContainer::slotItemsActivated(const KFileItemList
&items
)
766 Q_ASSERT(items
.count() >= 2);
768 KFileItemActions
fileItemActions(this);
769 fileItemActions
.runPreferredApplications(items
);
772 void DolphinViewContainer::showItemInfo(const KFileItem
&item
)
775 m_statusBar
->resetToDefaultText();
777 m_statusBar
->setText(item
.getStatusBarInfo());
781 void DolphinViewContainer::closeFilterBar()
783 m_filterBar
->closeFilterBar();
785 Q_EMIT
showFilterBarChanged(false);
788 void DolphinViewContainer::clearFilterBar()
790 m_filterBar
->clearIfUnlocked();
793 void DolphinViewContainer::setNameFilter(const QString
&nameFilter
)
795 m_view
->hideToolTip(ToolTipManager::HideBehavior::Instantly
);
796 m_view
->setNameFilter(nameFilter
);
797 delayedStatusBarUpdate();
800 void DolphinViewContainer::activate()
805 void DolphinViewContainer::slotUrlNavigatorLocationAboutToBeChanged(const QUrl
&)
810 void DolphinViewContainer::slotUrlNavigatorLocationChanged(const QUrl
&url
)
812 if (m_urlNavigatorConnected
) {
813 m_urlNavigatorConnected
->slotReturnPressed();
816 if (KProtocolManager::supportsListing(url
)) {
817 setSearchModeEnabled(isSearchUrl(url
));
819 tryRestoreViewState();
821 if (m_autoGrabFocus
&& isActive() && !isSearchUrl(url
)) {
822 // When an URL has been entered, the view should get the focus.
823 // The focus must be requested asynchronously, as changing the URL might create
824 // a new view widget.
825 QTimer::singleShot(0, this, &DolphinViewContainer::requestFocus
);
827 } else if (KProtocolManager::isSourceProtocol(url
)) {
828 if (url
.scheme().startsWith(QLatin1String("http"))) {
829 showMessage(i18nc("@info:status", // krazy:exclude=qmethods
830 "Dolphin does not support web pages, the web browser has been launched"),
833 showMessage(i18nc("@info:status", "Protocol not supported by Dolphin, default application has been launched"), Information
);
836 QDesktopServices::openUrl(url
);
837 redirect(QUrl(), m_urlNavigator
->locationUrl(1));
839 showMessage(i18nc("@info:status", "Invalid protocol"), Error
);
840 m_urlNavigator
->goBack();
844 void DolphinViewContainer::slotUrlSelectionRequested(const QUrl
&url
)
846 m_view
->markUrlsAsSelected({url
});
847 m_view
->markUrlAsCurrent(url
); // makes the item scroll into view
850 void DolphinViewContainer::disableUrlNavigatorSelectionRequests()
852 disconnect(m_urlNavigator
.get(), &KUrlNavigator::urlSelectionRequested
, this, &DolphinViewContainer::slotUrlSelectionRequested
);
855 void DolphinViewContainer::enableUrlNavigatorSelectionRequests()
857 connect(m_urlNavigator
.get(), &KUrlNavigator::urlSelectionRequested
, this, &DolphinViewContainer::slotUrlSelectionRequested
);
860 void DolphinViewContainer::redirect(const QUrl
&oldUrl
, const QUrl
&newUrl
)
863 const bool block
= m_urlNavigator
->signalsBlocked();
864 m_urlNavigator
->blockSignals(true);
866 // Assure that the location state is reset for redirection URLs. This
867 // allows to skip redirection URLs when going back or forward in the
869 m_urlNavigator
->saveLocationState(QByteArray());
870 m_urlNavigator
->setLocationUrl(newUrl
);
871 setSearchModeEnabled(isSearchUrl(newUrl
));
873 m_urlNavigator
->blockSignals(block
);
876 void DolphinViewContainer::requestFocus()
881 void DolphinViewContainer::startSearching()
883 Q_CHECK_PTR(m_urlNavigatorConnected
);
884 const QUrl url
= m_searchBox
->urlForSearching();
885 if (url
.isValid() && !url
.isEmpty()) {
886 m_view
->setViewPropertiesContext(QStringLiteral("search"));
887 m_urlNavigatorConnected
->setLocationUrl(url
);
891 void DolphinViewContainer::closeSearchBox()
893 setSearchModeEnabled(false);
896 void DolphinViewContainer::stopDirectoryLoading()
898 m_view
->stopLoading();
899 m_statusBar
->setProgress(100);
902 void DolphinViewContainer::slotStatusBarZoomLevelChanged(int zoomLevel
)
904 m_view
->setZoomLevel(zoomLevel
);
907 void DolphinViewContainer::showErrorMessage(const QString
&msg
)
909 showMessage(msg
, Error
);
912 void DolphinViewContainer::slotPlacesModelChanged()
914 if (!GeneralSettings::showFullPathInTitlebar() && !isSearchModeEnabled()) {
915 Q_EMIT
captionChanged();
919 void DolphinViewContainer::slotHiddenFilesShownChanged(bool showHiddenFiles
)
921 if (m_urlNavigatorConnected
) {
922 m_urlNavigatorConnected
->setShowHiddenFolders(showHiddenFiles
);
926 void DolphinViewContainer::slotSortHiddenLastChanged(bool hiddenLast
)
928 if (m_urlNavigatorConnected
) {
929 m_urlNavigatorConnected
->setSortHiddenFoldersLast(hiddenLast
);
933 void DolphinViewContainer::slotCurrentDirectoryRemoved()
935 const QString
location(url().toDisplayString(QUrl::PreferLocalFile
));
936 if (url().isLocalFile()) {
937 const QString dirPath
= url().toLocalFile();
938 const QString newPath
= getNearestExistingAncestorOfPath(dirPath
);
939 const QUrl newUrl
= QUrl::fromLocalFile(newPath
);
943 showMessage(xi18n("Current location changed, <filename>%1</filename> is no longer accessible.", location
), Warning
);
946 void DolphinViewContainer::slotOpenUrlFinished(KJob
*job
)
948 if (job
->error() && job
->error() != KIO::ERR_USER_CANCELED
) {
949 showErrorMessage(job
->errorString());
953 bool DolphinViewContainer::isSearchUrl(const QUrl
&url
) const
955 return url
.scheme().contains(QLatin1String("search"));
958 void DolphinViewContainer::saveViewState()
960 QByteArray locationState
;
961 QDataStream
stream(&locationState
, QIODevice::WriteOnly
);
962 m_view
->saveState(stream
);
963 m_urlNavigator
->saveLocationState(locationState
);
966 void DolphinViewContainer::tryRestoreViewState()
968 QByteArray locationState
= m_urlNavigator
->locationState();
969 if (!locationState
.isEmpty()) {
970 QDataStream
stream(&locationState
, QIODevice::ReadOnly
);
971 m_view
->restoreState(stream
);
975 QString
DolphinViewContainer::getNearestExistingAncestorOfPath(const QString
&path
) const
979 dir
.setPath(QDir::cleanPath(dir
.filePath(QStringLiteral(".."))));
980 } while (!dir
.exists() && !dir
.isRoot());
982 return dir
.exists() ? dir
.path() : QString
{};
985 #include "moc_dolphinviewcontainer.cpp"