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_generalsettings.h"
10 #include "dolphindebug.h"
11 #include "dolphinplacesmodelsingleton.h"
12 #include "filterbar/filterbar.h"
14 #include "search/dolphinsearchbox.h"
15 #include "selectionmode/topbar.h"
16 #include "statusbar/dolphinstatusbar.h"
17 #include "dolphin_detailsmodesettings.h"
19 #include <KActionCollection>
21 #include <KActivities/ResourceInstance>
23 #include <KFileItemActions>
24 #include <KFilePlacesModel>
25 #include <kio_version.h>
26 #if KIO_VERSION >= QT_VERSION_CHECK(5, 98, 0)
27 #include <KIO/JobUiDelegateFactory>
29 #include <KIO/JobUiDelegate>
31 #include <KIO/OpenUrlJob>
32 #include <KLocalizedString>
33 #include <KMessageWidget>
34 #include <KProtocolManager>
37 #include <QDesktopServices>
39 #include <QGridLayout>
40 #include <QGuiApplication>
43 #include <QRegularExpression>
45 // An overview of the widgets contained by this ViewContainer
46 struct LayoutStructure
{
48 int messageWidget
= 1;
49 int selectionModeTopBar
= 2;
51 int selectionModeBottomBar
= 4;
55 constexpr LayoutStructure positionFor
;
57 DolphinViewContainer::DolphinViewContainer(const QUrl
& url
, QWidget
* parent
) :
60 m_urlNavigator
{new DolphinUrlNavigator(url
)},
61 m_urlNavigatorConnected
{nullptr},
63 m_searchModeEnabled(false),
64 m_messageWidget(nullptr),
65 m_selectionModeTopBar
{nullptr},
68 m_selectionModeBottomBar
{nullptr},
70 m_statusBarTimer(nullptr),
71 m_statusBarTimestamp(),
74 , m_activityResourceInstance(nullptr)
79 m_topLayout
= new QGridLayout(this);
80 m_topLayout
->setSpacing(0);
81 m_topLayout
->setContentsMargins(0, 0, 0, 0);
83 m_searchBox
= new DolphinSearchBox(this);
85 connect(m_searchBox
, &DolphinSearchBox::activated
, this, &DolphinViewContainer::activate
);
86 connect(m_searchBox
, &DolphinSearchBox::closeRequest
, this, &DolphinViewContainer::closeSearchBox
);
87 connect(m_searchBox
, &DolphinSearchBox::searchRequest
, this, &DolphinViewContainer::startSearching
);
88 connect(m_searchBox
, &DolphinSearchBox::focusViewRequest
, this, &DolphinViewContainer::requestFocus
);
89 m_searchBox
->setWhatsThis(xi18nc("@info:whatsthis findbar",
90 "<para>This helps you find files and folders. Enter a <emphasis>"
91 "search term</emphasis> and specify search settings with the "
92 "buttons at the bottom:<list><item>Filename/Content: "
93 "Does the item you are looking for contain the search terms "
94 "within its filename or its contents?<nl/>The contents of images, "
95 "audio files and videos will not be searched.</item><item>"
96 "From Here/Everywhere: Do you want to search in this "
97 "folder and its sub-folders or everywhere?</item><item>"
98 "More Options: Click this to search by media type, access "
99 "time or rating.</item><item>More Search Tools: Install other "
100 "means to find an item.</item></list></para>"));
102 m_messageWidget
= new KMessageWidget(this);
103 m_messageWidget
->setCloseButtonVisible(true);
104 m_messageWidget
->hide();
109 // We must be logged in as the root user; show a big scary warning
110 showMessage(i18n("Running Dolphin as root can be dangerous. Please be careful."), Warning
);
114 // Initialize filter bar
115 m_filterBar
= new FilterBar(this);
116 m_filterBar
->setVisible(GeneralSettings::filterBar());
118 connect(m_filterBar
, &FilterBar::filterChanged
,
119 this, &DolphinViewContainer::setNameFilter
);
120 connect(m_filterBar
, &FilterBar::closeRequest
,
121 this, &DolphinViewContainer::closeFilterBar
);
122 connect(m_filterBar
, &FilterBar::focusViewRequest
,
123 this, &DolphinViewContainer::requestFocus
);
125 // Initialize the main view
126 m_view
= new DolphinView(url
, this);
127 connect(m_view
, &DolphinView::urlChanged
,
128 m_filterBar
, &FilterBar::clearIfUnlocked
);
129 connect(m_view
, &DolphinView::urlChanged
,
130 m_messageWidget
, &KMessageWidget::hide
);
131 // m_urlNavigator stays in sync with m_view's location changes and
132 // keeps track of them so going back and forth in the history works.
133 connect(m_view
, &DolphinView::urlChanged
,
134 m_urlNavigator
.get(), &DolphinUrlNavigator::setLocationUrl
);
135 connect(m_urlNavigator
.get(), &DolphinUrlNavigator::urlChanged
,
136 this, &DolphinViewContainer::slotUrlNavigatorLocationChanged
);
137 connect(m_urlNavigator
.get(), &DolphinUrlNavigator::urlAboutToBeChanged
,
138 this, &DolphinViewContainer::slotUrlNavigatorLocationAboutToBeChanged
);
139 connect(m_urlNavigator
.get(), &DolphinUrlNavigator::urlSelectionRequested
,
140 this, &DolphinViewContainer::slotUrlSelectionRequested
);
141 connect(m_view
, &DolphinView::writeStateChanged
,
142 this, &DolphinViewContainer::writeStateChanged
);
143 connect(m_view
, &DolphinView::requestItemInfo
,
144 this, &DolphinViewContainer::showItemInfo
);
145 connect(m_view
, &DolphinView::itemActivated
,
146 this, &DolphinViewContainer::slotItemActivated
);
147 connect(m_view
, &DolphinView::itemsActivated
,
148 this, &DolphinViewContainer::slotItemsActivated
);
149 connect(m_view
, &DolphinView::redirection
,
150 this, &DolphinViewContainer::redirect
);
151 connect(m_view
, &DolphinView::directoryLoadingStarted
,
152 this, &DolphinViewContainer::slotDirectoryLoadingStarted
);
153 connect(m_view
, &DolphinView::directoryLoadingCompleted
,
154 this, &DolphinViewContainer::slotDirectoryLoadingCompleted
);
155 connect(m_view
, &DolphinView::directoryLoadingCanceled
,
156 this, &DolphinViewContainer::slotDirectoryLoadingCanceled
);
157 connect(m_view
, &DolphinView::itemCountChanged
,
158 this, &DolphinViewContainer::delayedStatusBarUpdate
);
159 connect(m_view
, &DolphinView::directoryLoadingProgress
,
160 this, &DolphinViewContainer::updateDirectoryLoadingProgress
);
161 connect(m_view
, &DolphinView::directorySortingProgress
,
162 this, &DolphinViewContainer::updateDirectorySortingProgress
);
163 connect(m_view
, &DolphinView::selectionChanged
,
164 this, &DolphinViewContainer::delayedStatusBarUpdate
);
165 connect(m_view
, &DolphinView::errorMessage
,
166 this, &DolphinViewContainer::showErrorMessage
);
167 connect(m_view
, &DolphinView::urlIsFileError
,
168 this, &DolphinViewContainer::slotUrlIsFileError
);
169 connect(m_view
, &DolphinView::activated
,
170 this, &DolphinViewContainer::activate
);
171 connect(m_view
, &DolphinView::hiddenFilesShownChanged
,
172 this, &DolphinViewContainer::slotHiddenFilesShownChanged
);
173 connect(m_view
, &DolphinView::sortHiddenLastChanged
,
174 this, &DolphinViewContainer::slotSortHiddenLastChanged
);
175 connect(m_view
, &DolphinView::currentDirectoryRemoved
,
176 this, &DolphinViewContainer::slotCurrentDirectoryRemoved
);
178 // Initialize status bar
179 m_statusBar
= new DolphinStatusBar(this);
180 m_statusBar
->setUrl(m_view
->url());
181 m_statusBar
->setZoomLevel(m_view
->zoomLevel());
182 connect(m_view
, &DolphinView::urlChanged
,
183 m_statusBar
, &DolphinStatusBar::setUrl
);
184 connect(m_view
, &DolphinView::zoomLevelChanged
,
185 m_statusBar
, &DolphinStatusBar::setZoomLevel
);
186 connect(m_view
, &DolphinView::infoMessage
,
187 m_statusBar
, &DolphinStatusBar::setText
);
188 connect(m_view
, &DolphinView::operationCompletedMessage
,
189 m_statusBar
, &DolphinStatusBar::setText
);
190 connect(m_view
, &DolphinView::statusBarTextChanged
,
191 m_statusBar
, &DolphinStatusBar::setDefaultText
);
192 connect(m_view
, &DolphinView::statusBarTextChanged
,
193 m_statusBar
, &DolphinStatusBar::resetToDefaultText
);
194 connect(m_statusBar
, &DolphinStatusBar::stopPressed
,
195 this, &DolphinViewContainer::stopDirectoryLoading
);
196 connect(m_statusBar
, &DolphinStatusBar::zoomLevelChanged
,
197 this, &DolphinViewContainer::slotStatusBarZoomLevelChanged
);
199 m_statusBarTimer
= new QTimer(this);
200 m_statusBarTimer
->setSingleShot(true);
201 m_statusBarTimer
->setInterval(300);
202 connect(m_statusBarTimer
, &QTimer::timeout
, this, &DolphinViewContainer::updateStatusBar
);
204 KIO::FileUndoManager
* undoManager
= KIO::FileUndoManager::self();
205 connect(undoManager
, &KIO::FileUndoManager::jobRecordingFinished
,
206 this, &DolphinViewContainer::delayedStatusBarUpdate
);
208 m_topLayout
->addWidget(m_searchBox
, positionFor
.searchBox
, 0);
209 m_topLayout
->addWidget(m_messageWidget
, positionFor
.messageWidget
, 0);
210 m_topLayout
->addWidget(m_view
, positionFor
.view
, 0);
211 m_topLayout
->addWidget(m_filterBar
, positionFor
.filterBar
, 0);
212 m_topLayout
->addWidget(m_statusBar
, positionFor
.statusBar
, 0);
214 setSearchModeEnabled(isSearchUrl(url
));
216 connect(DetailsModeSettings::self(), &KCoreConfigSkeleton::configChanged
, this, [=]() {
217 if (view()->viewMode() == DolphinView::Mode::DetailsView
) {
222 KFilePlacesModel
*placesModel
= DolphinPlacesModelSingleton::instance().placesModel();
223 connect(placesModel
, &KFilePlacesModel::dataChanged
,
224 this, &DolphinViewContainer::slotPlacesModelChanged
);
225 connect(placesModel
, &KFilePlacesModel::rowsInserted
,
226 this, &DolphinViewContainer::slotPlacesModelChanged
);
227 connect(placesModel
, &KFilePlacesModel::rowsRemoved
,
228 this, &DolphinViewContainer::slotPlacesModelChanged
);
230 connect(this, &DolphinViewContainer::searchModeEnabledChanged
,
231 this, &DolphinViewContainer::captionChanged
);
233 // Initialize kactivities resource instance
236 m_activityResourceInstance
= new KActivities::ResourceInstance(window()->winId(), url
);
237 m_activityResourceInstance
->setParent(this);
241 DolphinViewContainer::~DolphinViewContainer()
245 QUrl
DolphinViewContainer::url() const
247 return m_view
->url();
250 void DolphinViewContainer::setActive(bool active
)
252 m_searchBox
->setActive(active
);
253 if (m_urlNavigatorConnected
) {
254 m_urlNavigatorConnected
->setActive(active
);
256 m_view
->setActive(active
);
260 m_activityResourceInstance
->notifyFocusedIn();
262 m_activityResourceInstance
->notifyFocusedOut();
267 bool DolphinViewContainer::isActive() const
269 return m_view
->isActive();
272 void DolphinViewContainer::setAutoGrabFocus(bool grab
)
274 m_autoGrabFocus
= grab
;
277 bool DolphinViewContainer::autoGrabFocus() const
279 return m_autoGrabFocus
;
282 QString
DolphinViewContainer::currentSearchText() const
284 return m_searchBox
->text();
287 const DolphinStatusBar
* DolphinViewContainer::statusBar() const
292 DolphinStatusBar
* DolphinViewContainer::statusBar()
297 const DolphinUrlNavigator
* DolphinViewContainer::urlNavigator() const
299 return m_urlNavigatorConnected
;
302 DolphinUrlNavigator
* DolphinViewContainer::urlNavigator()
304 return m_urlNavigatorConnected
;
307 const DolphinUrlNavigator
*DolphinViewContainer::urlNavigatorInternalWithHistory() const
309 return m_urlNavigator
.get();
312 DolphinUrlNavigator
*DolphinViewContainer::urlNavigatorInternalWithHistory()
314 return m_urlNavigator
.get();
317 const DolphinView
* DolphinViewContainer::view() const
322 DolphinView
* DolphinViewContainer::view()
327 void DolphinViewContainer::connectUrlNavigator(DolphinUrlNavigator
*urlNavigator
)
329 Q_CHECK_PTR(urlNavigator
);
330 Q_ASSERT(!m_urlNavigatorConnected
);
331 Q_ASSERT(m_urlNavigator
.get() != urlNavigator
);
334 urlNavigator
->setLocationUrl(m_view
->url());
335 urlNavigator
->setShowHiddenFolders(m_view
->hiddenFilesShown());
336 urlNavigator
->setSortHiddenFoldersLast(m_view
->sortHiddenLast());
337 if (m_urlNavigatorVisualState
) {
338 urlNavigator
->setVisualState(*m_urlNavigatorVisualState
.get());
339 m_urlNavigatorVisualState
.reset();
341 urlNavigator
->setActive(isActive());
343 // Url changes are still done via m_urlNavigator.
344 connect(urlNavigator
, &DolphinUrlNavigator::urlChanged
,
345 m_urlNavigator
.get(), &DolphinUrlNavigator::setLocationUrl
);
346 connect(urlNavigator
, &DolphinUrlNavigator::urlsDropped
,
347 this, [=](const QUrl
&destination
, QDropEvent
*event
) {
348 m_view
->dropUrls(destination
, event
, urlNavigator
->dropWidget());
350 // Aside from these, only visual things need to be connected.
351 connect(m_view
, &DolphinView::urlChanged
,
352 urlNavigator
, &DolphinUrlNavigator::setLocationUrl
);
353 connect(urlNavigator
, &DolphinUrlNavigator::activated
,
354 this, &DolphinViewContainer::activate
);
356 m_urlNavigatorConnected
= urlNavigator
;
359 void DolphinViewContainer::disconnectUrlNavigator()
361 if (!m_urlNavigatorConnected
) {
365 disconnect(m_urlNavigatorConnected
, &DolphinUrlNavigator::urlChanged
,
366 m_urlNavigator
.get(), &DolphinUrlNavigator::setLocationUrl
);
367 disconnect(m_urlNavigatorConnected
, &DolphinUrlNavigator::urlsDropped
,
369 disconnect(m_view
, &DolphinView::urlChanged
,
370 m_urlNavigatorConnected
, &DolphinUrlNavigator::setLocationUrl
);
371 disconnect(m_urlNavigatorConnected
, &DolphinUrlNavigator::activated
,
372 this, &DolphinViewContainer::activate
);
374 m_urlNavigatorVisualState
= m_urlNavigatorConnected
->visualState();
375 m_urlNavigatorConnected
= nullptr;
378 void DolphinViewContainer::setSelectionModeEnabled(bool enabled
, KActionCollection
*actionCollection
, SelectionMode::BottomBar::Contents bottomBarContents
)
380 const bool wasEnabled
= m_view
->selectionMode();
381 m_view
->setSelectionModeEnabled(enabled
);
385 return; // nothing to do here
387 Q_CHECK_PTR(m_selectionModeTopBar
); // there is no point in disabling selectionMode when it wasn't even enabled once.
388 Q_CHECK_PTR(m_selectionModeBottomBar
);
389 m_selectionModeTopBar
->setVisible(false, WithAnimation
);
390 m_selectionModeBottomBar
->setVisible(false, WithAnimation
);
391 Q_EMIT
selectionModeChanged(false);
395 if (!m_selectionModeTopBar
) {
396 // Changing the location will disable selection mode.
397 connect(m_urlNavigator
.get(), &DolphinUrlNavigator::urlChanged
, this, [this]() {
398 setSelectionModeEnabled(false);
401 m_selectionModeTopBar
= new SelectionMode::TopBar(this); // will be created hidden
402 connect(m_selectionModeTopBar
, &SelectionMode::TopBar::selectionModeLeavingRequested
, this, [this]() {
403 setSelectionModeEnabled(false);
405 m_topLayout
->addWidget(m_selectionModeTopBar
, positionFor
.selectionModeTopBar
, 0);
408 if (!m_selectionModeBottomBar
) {
409 m_selectionModeBottomBar
= new SelectionMode::BottomBar(actionCollection
, this);
410 connect(m_view
, &DolphinView::selectionChanged
, this, [this](const KFileItemList
&selection
) {
411 m_selectionModeBottomBar
->slotSelectionChanged(selection
, m_view
->url());
413 connect(m_selectionModeBottomBar
, &SelectionMode::BottomBar::error
, this, [this](const QString
&errorMessage
) {
414 showErrorMessage(errorMessage
);
416 connect(m_selectionModeBottomBar
, &SelectionMode::BottomBar::selectionModeLeavingRequested
, this, [this]() {
417 setSelectionModeEnabled(false);
419 m_topLayout
->addWidget(m_selectionModeBottomBar
, positionFor
.selectionModeBottomBar
, 0);
421 m_selectionModeBottomBar
->resetContents(bottomBarContents
);
422 if (bottomBarContents
== SelectionMode::BottomBar::GeneralContents
) {
423 m_selectionModeBottomBar
->slotSelectionChanged(m_view
->selectedItems(), m_view
->url());
427 m_selectionModeTopBar
->setVisible(true, WithAnimation
);
428 m_selectionModeBottomBar
->setVisible(true, WithAnimation
);
429 Q_EMIT
selectionModeChanged(true);
433 bool DolphinViewContainer::isSelectionModeEnabled() const
435 const bool isEnabled
= m_view
->selectionMode();
437 // We can't assert that the bars are invisible only because the selection mode is disabled because the hide animation might still be playing.
438 && (!m_selectionModeBottomBar
|| !m_selectionModeBottomBar
->isEnabled() ||
439 !m_selectionModeBottomBar
->isVisible() || m_selectionModeBottomBar
->contents() == SelectionMode::BottomBar::PasteContents
))
441 && m_selectionModeTopBar
&& m_selectionModeTopBar
->isVisible()
442 // 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.
443 && m_selectionModeBottomBar
444 && (m_selectionModeBottomBar
->isVisible() || m_selectionModeBottomBar
->contents() == SelectionMode::BottomBar::GeneralContents
)));
448 void DolphinViewContainer::slotSplitTabDisabled()
450 if (m_selectionModeBottomBar
) {
451 m_selectionModeBottomBar
->slotSplitTabDisabled();
456 void DolphinViewContainer::showMessage(const QString
& msg
, MessageType type
)
462 m_messageWidget
->setText(msg
);
464 // TODO: wrap at arbitrary character positions once QLabel can do this
465 // https://bugreports.qt.io/browse/QTBUG-1276
466 m_messageWidget
->setWordWrap(true);
469 case Information
: m_messageWidget
->setMessageType(KMessageWidget::Information
); break;
470 case Warning
: m_messageWidget
->setMessageType(KMessageWidget::Warning
); break;
471 case Error
: m_messageWidget
->setMessageType(KMessageWidget::Error
); break;
477 m_messageWidget
->setWordWrap(false);
478 const int unwrappedWidth
= m_messageWidget
->sizeHint().width();
479 m_messageWidget
->setWordWrap(unwrappedWidth
> size().width());
481 if (m_messageWidget
->isVisible()) {
482 m_messageWidget
->hide();
484 m_messageWidget
->animatedShow();
487 void DolphinViewContainer::readSettings()
489 // The startup settings should (only) get applied if they have been
490 // modified by the user. Otherwise keep the (possibly) different current
491 // setting of the filterbar.
492 if (GeneralSettings::modifiedStartupSettings()) {
493 setFilterBarVisible(GeneralSettings::filterBar());
496 m_view
->readSettings();
497 m_statusBar
->readSettings();
500 bool DolphinViewContainer::isFilterBarVisible() const
502 return m_filterBar
->isVisible();
505 void DolphinViewContainer::setSearchModeEnabled(bool enabled
)
507 m_searchBox
->setVisible(enabled
);
510 const QUrl
& locationUrl
= m_urlNavigator
->locationUrl();
511 m_searchBox
->fromSearchUrl(locationUrl
);
514 if (enabled
== isSearchModeEnabled()) {
515 if (enabled
&& !m_searchBox
->hasFocus()) {
516 m_searchBox
->setFocus();
517 m_searchBox
->selectAll();
523 m_view
->setViewPropertiesContext(QString());
525 // Restore the URL for the URL navigator. If Dolphin has been
526 // started with a search-URL, the home URL is used as fallback.
527 QUrl url
= m_searchBox
->searchPath();
528 if (url
.isEmpty() || !url
.isValid() || isSearchUrl(url
)) {
529 url
= Dolphin::homeUrl();
531 m_urlNavigatorConnected
->setLocationUrl(url
);
534 m_searchModeEnabled
= enabled
;
536 Q_EMIT
searchModeEnabledChanged(enabled
);
539 bool DolphinViewContainer::isSearchModeEnabled() const
541 return m_searchModeEnabled
;
544 QString
DolphinViewContainer::placesText() const
548 if (isSearchModeEnabled()) {
549 text
= i18n("Search for %1 in %2", m_searchBox
->text(), m_searchBox
->searchPath().fileName());
551 text
= url().adjusted(QUrl::StripTrailingSlash
).fileName();
552 if (text
.isEmpty()) {
555 if (text
.isEmpty()) {
556 text
= url().scheme();
563 void DolphinViewContainer::reload()
566 m_messageWidget
->hide();
569 QString
DolphinViewContainer::captionWindowTitle() const
571 if (GeneralSettings::showFullPathInTitlebar() && !isSearchModeEnabled()) {
572 if (!url().isLocalFile()) {
573 return url().adjusted(QUrl::StripTrailingSlash
).toString();
575 return url().adjusted(QUrl::StripTrailingSlash
).path();
577 return DolphinViewContainer::caption();
581 QString
DolphinViewContainer::caption() const
583 if (isSearchModeEnabled()) {
584 if (currentSearchText().isEmpty()){
585 return i18n("Search");
587 return i18n("Search for %1", currentSearchText());
591 KFilePlacesModel
*placesModel
= DolphinPlacesModelSingleton::instance().placesModel();
592 const QString pattern
= url().adjusted(QUrl::StripTrailingSlash
).toString(QUrl::FullyEncoded
).append("/?");
593 const auto& matchedPlaces
= placesModel
->match(placesModel
->index(0,0), KFilePlacesModel::UrlRole
, QRegularExpression::anchoredPattern(pattern
), 1, Qt::MatchRegularExpression
);
595 if (!matchedPlaces
.isEmpty()) {
596 return placesModel
->text(matchedPlaces
.first());
600 if (!url().isLocalFile()) {
601 QUrl adjustedUrl
= url().adjusted(QUrl::StripTrailingSlash
);
603 if (!adjustedUrl
.fileName().isEmpty()) {
604 caption
= adjustedUrl
.fileName();
605 } else if (!adjustedUrl
.path().isEmpty() && adjustedUrl
.path() != "/") {
606 caption
= adjustedUrl
.path();
607 } else if (!adjustedUrl
.host().isEmpty()) {
608 caption
= adjustedUrl
.host();
610 caption
= adjustedUrl
.toString();
615 QString fileName
= url().adjusted(QUrl::StripTrailingSlash
).fileName();
616 if (fileName
.isEmpty()) {
623 void DolphinViewContainer::setUrl(const QUrl
& newUrl
)
625 if (newUrl
!= m_urlNavigator
->locationUrl()) {
626 m_urlNavigator
->setLocationUrl(newUrl
);
630 m_activityResourceInstance
->setUri(newUrl
);
634 void DolphinViewContainer::setFilterBarVisible(bool visible
)
636 Q_ASSERT(m_filterBar
);
638 m_view
->hideToolTip(ToolTipManager::HideBehavior::Instantly
);
640 m_filterBar
->setFocus();
641 m_filterBar
->selectAll();
647 void DolphinViewContainer::delayedStatusBarUpdate()
649 if (m_statusBarTimer
->isActive() && (m_statusBarTimestamp
.elapsed() > 2000)) {
650 // No update of the statusbar has been done during the last 2 seconds,
651 // although an update has been requested. Trigger an immediate update.
652 m_statusBarTimer
->stop();
655 // Invoke updateStatusBar() with a small delay. This assures that
656 // when a lot of delayedStatusBarUpdates() are done in a short time,
657 // no bottleneck is given.
658 m_statusBarTimer
->start();
662 void DolphinViewContainer::updateStatusBar()
664 m_statusBarTimestamp
.start();
665 m_view
->requestStatusBarText();
668 void DolphinViewContainer::updateDirectoryLoadingProgress(int percent
)
670 if (m_statusBar
->progressText().isEmpty()) {
671 m_statusBar
->setProgressText(i18nc("@info:progress", "Loading folder..."));
673 m_statusBar
->setProgress(percent
);
676 void DolphinViewContainer::updateDirectorySortingProgress(int percent
)
678 if (m_statusBar
->progressText().isEmpty()) {
679 m_statusBar
->setProgressText(i18nc("@info:progress", "Sorting..."));
681 m_statusBar
->setProgress(percent
);
684 void DolphinViewContainer::slotDirectoryLoadingStarted()
686 if (isSearchUrl(url())) {
687 // Search KIO-slaves usually don't provide any progress information. Give
688 // a hint to the user that a searching is done:
690 m_statusBar
->setProgressText(i18nc("@info", "Searching..."));
691 m_statusBar
->setProgress(-1);
693 // Trigger an undetermined progress indication. The progress
694 // information in percent will be triggered by the percent() signal
695 // of the directory lister later.
696 m_statusBar
->setProgressText(QString());
697 updateDirectoryLoadingProgress(-1);
701 void DolphinViewContainer::slotDirectoryLoadingCompleted()
703 if (!m_statusBar
->progressText().isEmpty()) {
704 m_statusBar
->setProgressText(QString());
705 m_statusBar
->setProgress(100);
708 if (isSearchUrl(url()) && m_view
->itemsCount() == 0) {
709 // The dir lister has been completed on a Baloo-URI and no items have been found. Instead
710 // of showing the default status bar information ("0 items") a more helpful information is given:
711 m_statusBar
->setText(i18nc("@info:status", "No items found."));
717 void DolphinViewContainer::slotDirectoryLoadingCanceled()
719 if (!m_statusBar
->progressText().isEmpty()) {
720 m_statusBar
->setProgressText(QString());
721 m_statusBar
->setProgress(100);
724 m_statusBar
->setText(QString());
727 void DolphinViewContainer::slotUrlIsFileError(const QUrl
& url
)
729 const KFileItem
item(url
);
731 // Find out if the file can be opened in the view (for example, this is the
732 // case if the file is an archive). The mime type must be known for that.
733 item
.determineMimeType();
734 const QUrl
& folderUrl
= DolphinView::openItemAsFolderUrl(item
, true);
735 if (!folderUrl
.isEmpty()) {
738 slotItemActivated(item
);
742 void DolphinViewContainer::slotItemActivated(const KFileItem
&item
)
744 // It is possible to activate items on inactive views by
745 // drag & drop operations. Assure that activating an item always
746 // results in an active view.
747 m_view
->setActive(true);
749 const QUrl
& url
= DolphinView::openItemAsFolderUrl(item
, GeneralSettings::browseThroughArchives());
750 if (!url
.isEmpty()) {
751 const auto modifiers
= QGuiApplication::keyboardModifiers();
752 // keep in sync with KUrlNavigator::slotNavigatorButtonClicked
753 if (modifiers
& Qt::ControlModifier
&& modifiers
& Qt::ShiftModifier
) {
754 Q_EMIT
activeTabRequested(url
);
755 } else if (modifiers
& Qt::ControlModifier
) {
756 Q_EMIT
tabRequested(url
);
757 } else if (modifiers
& Qt::ShiftModifier
) {
758 Dolphin::openNewWindow({KFilePlacesModel::convertedUrl(url
)}, this);
765 KIO::OpenUrlJob
*job
= new KIO::OpenUrlJob(item
.targetUrl(), item
.mimetype());
766 #if KIO_VERSION >= QT_VERSION_CHECK(5, 98, 0)
767 job
->setUiDelegate(KIO::createDefaultJobUiDelegate(KJobUiDelegate::AutoHandlingEnabled
, this));
769 job
->setUiDelegate(new KIO::JobUiDelegate(KJobUiDelegate::AutoHandlingEnabled
, this));
771 job
->setShowOpenOrExecuteDialog(true);
772 connect(job
, &KIO::OpenUrlJob::finished
, this, &DolphinViewContainer::slotOpenUrlFinished
);
776 void DolphinViewContainer::slotItemsActivated(const KFileItemList
& items
)
778 Q_ASSERT(items
.count() >= 2);
780 KFileItemActions
fileItemActions(this);
781 fileItemActions
.runPreferredApplications(items
);
784 void DolphinViewContainer::showItemInfo(const KFileItem
& item
)
787 m_statusBar
->resetToDefaultText();
789 m_statusBar
->setText(item
.getStatusBarInfo());
793 void DolphinViewContainer::closeFilterBar()
795 m_filterBar
->closeFilterBar();
797 Q_EMIT
showFilterBarChanged(false);
800 void DolphinViewContainer::clearFilterBar()
802 m_filterBar
->clearIfUnlocked();
805 void DolphinViewContainer::setNameFilter(const QString
& nameFilter
)
807 m_view
->hideToolTip(ToolTipManager::HideBehavior::Instantly
);
808 m_view
->setNameFilter(nameFilter
);
809 delayedStatusBarUpdate();
812 void DolphinViewContainer::activate()
817 void DolphinViewContainer::slotUrlNavigatorLocationAboutToBeChanged(const QUrl
&)
822 void DolphinViewContainer::slotUrlNavigatorLocationChanged(const QUrl
& url
)
824 if (m_urlNavigatorConnected
) {
825 m_urlNavigatorConnected
->slotReturnPressed();
828 if (KProtocolManager::supportsListing(url
)) {
829 setSearchModeEnabled(isSearchUrl(url
));
831 tryRestoreViewState();
833 if (m_autoGrabFocus
&& isActive() && !isSearchUrl(url
)) {
834 // When an URL has been entered, the view should get the focus.
835 // The focus must be requested asynchronously, as changing the URL might create
836 // a new view widget.
837 QTimer::singleShot(0, this, &DolphinViewContainer::requestFocus
);
839 } else if (KProtocolManager::isSourceProtocol(url
)) {
840 if (url
.scheme().startsWith(QLatin1String("http"))) {
841 showMessage(i18nc("@info:status", // krazy:exclude=qmethods
842 "Dolphin does not support web pages, the web browser has been launched"),
845 showMessage(i18nc("@info:status",
846 "Protocol not supported by Dolphin, default application has been launched"),
850 QDesktopServices::openUrl(url
);
851 redirect(QUrl(), m_urlNavigator
->locationUrl(1));
853 showMessage(i18nc("@info:status", "Invalid protocol"), Error
);
854 m_urlNavigator
->goBack();
858 void DolphinViewContainer::slotUrlSelectionRequested(const QUrl
& url
)
860 m_view
->markUrlsAsSelected({url
});
861 m_view
->markUrlAsCurrent(url
); // makes the item scroll into view
864 void DolphinViewContainer::disableUrlNavigatorSelectionRequests()
866 disconnect(m_urlNavigator
.get(), &KUrlNavigator::urlSelectionRequested
,
867 this, &DolphinViewContainer::slotUrlSelectionRequested
);
870 void DolphinViewContainer::enableUrlNavigatorSelectionRequests()
872 connect(m_urlNavigator
.get(), &KUrlNavigator::urlSelectionRequested
,
873 this, &DolphinViewContainer::slotUrlSelectionRequested
);
876 void DolphinViewContainer::redirect(const QUrl
& oldUrl
, const QUrl
& newUrl
)
879 const bool block
= m_urlNavigator
->signalsBlocked();
880 m_urlNavigator
->blockSignals(true);
882 // Assure that the location state is reset for redirection URLs. This
883 // allows to skip redirection URLs when going back or forward in the
885 m_urlNavigator
->saveLocationState(QByteArray());
886 m_urlNavigator
->setLocationUrl(newUrl
);
887 setSearchModeEnabled(isSearchUrl(newUrl
));
889 m_urlNavigator
->blockSignals(block
);
892 void DolphinViewContainer::requestFocus()
897 void DolphinViewContainer::startSearching()
899 Q_CHECK_PTR(m_urlNavigatorConnected
);
900 const QUrl url
= m_searchBox
->urlForSearching();
901 if (url
.isValid() && !url
.isEmpty()) {
902 m_view
->setViewPropertiesContext(QStringLiteral("search"));
903 m_urlNavigatorConnected
->setLocationUrl(url
);
907 void DolphinViewContainer::closeSearchBox()
909 setSearchModeEnabled(false);
912 void DolphinViewContainer::stopDirectoryLoading()
914 m_view
->stopLoading();
915 m_statusBar
->setProgress(100);
918 void DolphinViewContainer::slotStatusBarZoomLevelChanged(int zoomLevel
)
920 m_view
->setZoomLevel(zoomLevel
);
923 void DolphinViewContainer::showErrorMessage(const QString
& msg
)
925 showMessage(msg
, Error
);
928 void DolphinViewContainer::slotPlacesModelChanged()
930 if (!GeneralSettings::showFullPathInTitlebar() && !isSearchModeEnabled()) {
931 Q_EMIT
captionChanged();
935 void DolphinViewContainer::slotHiddenFilesShownChanged(bool showHiddenFiles
)
937 if (m_urlNavigatorConnected
) {
938 m_urlNavigatorConnected
->setShowHiddenFolders(showHiddenFiles
);
942 void DolphinViewContainer::slotSortHiddenLastChanged(bool hiddenLast
)
944 if (m_urlNavigatorConnected
) {
945 m_urlNavigatorConnected
->setSortHiddenFoldersLast(hiddenLast
);
949 void DolphinViewContainer::slotCurrentDirectoryRemoved()
951 const QString
location(url().toDisplayString(QUrl::PreferLocalFile
));
952 if (url().isLocalFile()) {
953 const QString dirPath
= url().toLocalFile();
954 const QString newPath
= getNearestExistingAncestorOfPath(dirPath
);
955 const QUrl newUrl
= QUrl::fromLocalFile(newPath
);
959 showMessage(xi18n("Current location changed, <filename>%1</filename> is no longer accessible.", location
), Warning
);
962 void DolphinViewContainer::slotOpenUrlFinished(KJob
*job
)
964 if (job
->error() && job
->error() != KIO::ERR_USER_CANCELED
) {
965 showErrorMessage(job
->errorString());
969 bool DolphinViewContainer::isSearchUrl(const QUrl
& url
) const
971 return url
.scheme().contains(QLatin1String("search"));
974 void DolphinViewContainer::saveViewState()
976 QByteArray locationState
;
977 QDataStream
stream(&locationState
, QIODevice::WriteOnly
);
978 m_view
->saveState(stream
);
979 m_urlNavigator
->saveLocationState(locationState
);
982 void DolphinViewContainer::tryRestoreViewState()
984 QByteArray locationState
= m_urlNavigator
->locationState();
985 if (!locationState
.isEmpty()) {
986 QDataStream
stream(&locationState
, QIODevice::ReadOnly
);
987 m_view
->restoreState(stream
);
991 QString
DolphinViewContainer::getNearestExistingAncestorOfPath(const QString
& path
) const
995 dir
.setPath(QDir::cleanPath(dir
.filePath(QStringLiteral(".."))));
997 while (!dir
.exists() && !dir
.isRoot());
999 return dir
.exists() ? dir
.path() : QString
{};