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_detailsmodesettings.h"
10 #include "dolphin_generalsettings.h"
11 #include "dolphindebug.h"
12 #include "dolphinplacesmodelsingleton.h"
13 #include "filterbar/filterbar.h"
15 #include "search/dolphinsearchbox.h"
16 #include "selectionmode/topbar.h"
17 #include "statusbar/dolphinstatusbar.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>
41 #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
)
59 , m_topLayout(nullptr)
60 , m_urlNavigator
{new DolphinUrlNavigator(url
)}
61 , m_urlNavigatorConnected
{nullptr}
62 , m_searchBox(nullptr)
63 , m_searchModeEnabled(false)
64 , m_messageWidget(nullptr)
65 , m_selectionModeTopBar
{nullptr}
67 , m_filterBar(nullptr)
68 , m_selectionModeBottomBar
{nullptr}
69 , m_statusBar(nullptr)
70 , m_statusBarTimer(nullptr)
71 , m_statusBarTimestamp()
72 , m_autoGrabFocus(true)
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();
108 // We must be logged in as the root user; show a big scary warning
109 showMessage(i18n("Running Dolphin as root can be dangerous. Please be careful."), Warning
);
113 // Initialize filter bar
114 m_filterBar
= new FilterBar(this);
115 m_filterBar
->setVisible(GeneralSettings::filterBar());
117 connect(m_filterBar
, &FilterBar::filterChanged
, this, &DolphinViewContainer::setNameFilter
);
118 connect(m_filterBar
, &FilterBar::closeRequest
, this, &DolphinViewContainer::closeFilterBar
);
119 connect(m_filterBar
, &FilterBar::focusViewRequest
, this, &DolphinViewContainer::requestFocus
);
121 // Initialize the main view
122 m_view
= new DolphinView(url
, this);
123 connect(m_view
, &DolphinView::urlChanged
, m_filterBar
, &FilterBar::clearIfUnlocked
);
124 connect(m_view
, &DolphinView::urlChanged
, m_messageWidget
, &KMessageWidget::hide
);
125 // m_urlNavigator stays in sync with m_view's location changes and
126 // keeps track of them so going back and forth in the history works.
127 connect(m_view
, &DolphinView::urlChanged
, m_urlNavigator
.get(), &DolphinUrlNavigator::setLocationUrl
);
128 connect(m_urlNavigator
.get(), &DolphinUrlNavigator::urlChanged
, this, &DolphinViewContainer::slotUrlNavigatorLocationChanged
);
129 connect(m_urlNavigator
.get(), &DolphinUrlNavigator::urlAboutToBeChanged
, this, &DolphinViewContainer::slotUrlNavigatorLocationAboutToBeChanged
);
130 connect(m_urlNavigator
.get(), &DolphinUrlNavigator::urlSelectionRequested
, this, &DolphinViewContainer::slotUrlSelectionRequested
);
131 connect(m_view
, &DolphinView::writeStateChanged
, this, &DolphinViewContainer::writeStateChanged
);
132 connect(m_view
, &DolphinView::requestItemInfo
, this, &DolphinViewContainer::showItemInfo
);
133 connect(m_view
, &DolphinView::itemActivated
, this, &DolphinViewContainer::slotItemActivated
);
134 connect(m_view
, &DolphinView::itemsActivated
, this, &DolphinViewContainer::slotItemsActivated
);
135 connect(m_view
, &DolphinView::redirection
, this, &DolphinViewContainer::redirect
);
136 connect(m_view
, &DolphinView::directoryLoadingStarted
, this, &DolphinViewContainer::slotDirectoryLoadingStarted
);
137 connect(m_view
, &DolphinView::directoryLoadingCompleted
, this, &DolphinViewContainer::slotDirectoryLoadingCompleted
);
138 connect(m_view
, &DolphinView::directoryLoadingCanceled
, this, &DolphinViewContainer::slotDirectoryLoadingCanceled
);
139 connect(m_view
, &DolphinView::itemCountChanged
, this, &DolphinViewContainer::delayedStatusBarUpdate
);
140 connect(m_view
, &DolphinView::directoryLoadingProgress
, this, &DolphinViewContainer::updateDirectoryLoadingProgress
);
141 connect(m_view
, &DolphinView::directorySortingProgress
, this, &DolphinViewContainer::updateDirectorySortingProgress
);
142 connect(m_view
, &DolphinView::selectionChanged
, this, &DolphinViewContainer::delayedStatusBarUpdate
);
143 connect(m_view
, &DolphinView::errorMessage
, this, &DolphinViewContainer::showErrorMessage
);
144 connect(m_view
, &DolphinView::urlIsFileError
, this, &DolphinViewContainer::slotUrlIsFileError
);
145 connect(m_view
, &DolphinView::activated
, this, &DolphinViewContainer::activate
);
146 connect(m_view
, &DolphinView::hiddenFilesShownChanged
, this, &DolphinViewContainer::slotHiddenFilesShownChanged
);
147 connect(m_view
, &DolphinView::sortHiddenLastChanged
, this, &DolphinViewContainer::slotSortHiddenLastChanged
);
148 connect(m_view
, &DolphinView::currentDirectoryRemoved
, this, &DolphinViewContainer::slotCurrentDirectoryRemoved
);
150 // Initialize status bar
151 m_statusBar
= new DolphinStatusBar(this);
152 m_statusBar
->setUrl(m_view
->url());
153 m_statusBar
->setZoomLevel(m_view
->zoomLevel());
154 connect(m_view
, &DolphinView::urlChanged
, m_statusBar
, &DolphinStatusBar::setUrl
);
155 connect(m_view
, &DolphinView::zoomLevelChanged
, m_statusBar
, &DolphinStatusBar::setZoomLevel
);
156 connect(m_view
, &DolphinView::infoMessage
, m_statusBar
, &DolphinStatusBar::setText
);
157 connect(m_view
, &DolphinView::operationCompletedMessage
, m_statusBar
, &DolphinStatusBar::setText
);
158 connect(m_view
, &DolphinView::statusBarTextChanged
, m_statusBar
, &DolphinStatusBar::setDefaultText
);
159 connect(m_view
, &DolphinView::statusBarTextChanged
, m_statusBar
, &DolphinStatusBar::resetToDefaultText
);
160 connect(m_statusBar
, &DolphinStatusBar::stopPressed
, this, &DolphinViewContainer::stopDirectoryLoading
);
161 connect(m_statusBar
, &DolphinStatusBar::zoomLevelChanged
, this, &DolphinViewContainer::slotStatusBarZoomLevelChanged
);
163 m_statusBarTimer
= new QTimer(this);
164 m_statusBarTimer
->setSingleShot(true);
165 m_statusBarTimer
->setInterval(300);
166 connect(m_statusBarTimer
, &QTimer::timeout
, this, &DolphinViewContainer::updateStatusBar
);
168 KIO::FileUndoManager
*undoManager
= KIO::FileUndoManager::self();
169 connect(undoManager
, &KIO::FileUndoManager::jobRecordingFinished
, this, &DolphinViewContainer::delayedStatusBarUpdate
);
171 m_topLayout
->addWidget(m_searchBox
, positionFor
.searchBox
, 0);
172 m_topLayout
->addWidget(m_messageWidget
, positionFor
.messageWidget
, 0);
173 m_topLayout
->addWidget(m_view
, positionFor
.view
, 0);
174 m_topLayout
->addWidget(m_filterBar
, positionFor
.filterBar
, 0);
175 m_topLayout
->addWidget(m_statusBar
, positionFor
.statusBar
, 0);
177 setSearchModeEnabled(isSearchUrl(url
));
179 connect(DetailsModeSettings::self(), &KCoreConfigSkeleton::configChanged
, this, [=]() {
180 if (view()->viewMode() == DolphinView::Mode::DetailsView
) {
185 KFilePlacesModel
*placesModel
= DolphinPlacesModelSingleton::instance().placesModel();
186 connect(placesModel
, &KFilePlacesModel::dataChanged
, this, &DolphinViewContainer::slotPlacesModelChanged
);
187 connect(placesModel
, &KFilePlacesModel::rowsInserted
, this, &DolphinViewContainer::slotPlacesModelChanged
);
188 connect(placesModel
, &KFilePlacesModel::rowsRemoved
, this, &DolphinViewContainer::slotPlacesModelChanged
);
190 connect(this, &DolphinViewContainer::searchModeEnabledChanged
, this, &DolphinViewContainer::captionChanged
);
192 // Initialize kactivities resource instance
195 m_activityResourceInstance
= new KActivities::ResourceInstance(window()->winId(), url
);
196 m_activityResourceInstance
->setParent(this);
200 DolphinViewContainer::~DolphinViewContainer()
204 QUrl
DolphinViewContainer::url() const
206 return m_view
->url();
209 void DolphinViewContainer::setActive(bool active
)
211 m_searchBox
->setActive(active
);
212 if (m_urlNavigatorConnected
) {
213 m_urlNavigatorConnected
->setActive(active
);
215 m_view
->setActive(active
);
219 m_activityResourceInstance
->notifyFocusedIn();
221 m_activityResourceInstance
->notifyFocusedOut();
226 bool DolphinViewContainer::isActive() const
228 return m_view
->isActive();
231 void DolphinViewContainer::setAutoGrabFocus(bool grab
)
233 m_autoGrabFocus
= grab
;
236 bool DolphinViewContainer::autoGrabFocus() const
238 return m_autoGrabFocus
;
241 QString
DolphinViewContainer::currentSearchText() const
243 return m_searchBox
->text();
246 const DolphinStatusBar
*DolphinViewContainer::statusBar() const
251 DolphinStatusBar
*DolphinViewContainer::statusBar()
256 const DolphinUrlNavigator
*DolphinViewContainer::urlNavigator() const
258 return m_urlNavigatorConnected
;
261 DolphinUrlNavigator
*DolphinViewContainer::urlNavigator()
263 return m_urlNavigatorConnected
;
266 const DolphinUrlNavigator
*DolphinViewContainer::urlNavigatorInternalWithHistory() const
268 return m_urlNavigator
.get();
271 DolphinUrlNavigator
*DolphinViewContainer::urlNavigatorInternalWithHistory()
273 return m_urlNavigator
.get();
276 const DolphinView
*DolphinViewContainer::view() const
281 DolphinView
*DolphinViewContainer::view()
286 void DolphinViewContainer::connectUrlNavigator(DolphinUrlNavigator
*urlNavigator
)
288 Q_CHECK_PTR(urlNavigator
);
289 Q_ASSERT(!m_urlNavigatorConnected
);
290 Q_ASSERT(m_urlNavigator
.get() != urlNavigator
);
293 urlNavigator
->setLocationUrl(m_view
->url());
294 urlNavigator
->setShowHiddenFolders(m_view
->hiddenFilesShown());
295 urlNavigator
->setSortHiddenFoldersLast(m_view
->sortHiddenLast());
296 if (m_urlNavigatorVisualState
) {
297 urlNavigator
->setVisualState(*m_urlNavigatorVisualState
.get());
298 m_urlNavigatorVisualState
.reset();
300 urlNavigator
->setActive(isActive());
302 // Url changes are still done via m_urlNavigator.
303 connect(urlNavigator
, &DolphinUrlNavigator::urlChanged
, m_urlNavigator
.get(), &DolphinUrlNavigator::setLocationUrl
);
304 connect(urlNavigator
, &DolphinUrlNavigator::urlsDropped
, this, [=](const QUrl
&destination
, QDropEvent
*event
) {
305 m_view
->dropUrls(destination
, event
, urlNavigator
->dropWidget());
307 // Aside from these, only visual things need to be connected.
308 connect(m_view
, &DolphinView::urlChanged
, urlNavigator
, &DolphinUrlNavigator::setLocationUrl
);
309 connect(urlNavigator
, &DolphinUrlNavigator::activated
, this, &DolphinViewContainer::activate
);
311 m_urlNavigatorConnected
= urlNavigator
;
314 void DolphinViewContainer::disconnectUrlNavigator()
316 if (!m_urlNavigatorConnected
) {
320 disconnect(m_urlNavigatorConnected
, &DolphinUrlNavigator::urlChanged
, m_urlNavigator
.get(), &DolphinUrlNavigator::setLocationUrl
);
321 disconnect(m_urlNavigatorConnected
, &DolphinUrlNavigator::urlsDropped
, this, nullptr);
322 disconnect(m_view
, &DolphinView::urlChanged
, m_urlNavigatorConnected
, &DolphinUrlNavigator::setLocationUrl
);
323 disconnect(m_urlNavigatorConnected
, &DolphinUrlNavigator::activated
, this, &DolphinViewContainer::activate
);
325 m_urlNavigatorVisualState
= m_urlNavigatorConnected
->visualState();
326 m_urlNavigatorConnected
= nullptr;
329 void DolphinViewContainer::setSelectionModeEnabled(bool enabled
, KActionCollection
*actionCollection
, SelectionMode::BottomBar::Contents bottomBarContents
)
331 const bool wasEnabled
= m_view
->selectionMode();
332 m_view
->setSelectionModeEnabled(enabled
);
336 return; // nothing to do here
338 Q_CHECK_PTR(m_selectionModeTopBar
); // there is no point in disabling selectionMode when it wasn't even enabled once.
339 Q_CHECK_PTR(m_selectionModeBottomBar
);
340 m_selectionModeTopBar
->setVisible(false, WithAnimation
);
341 m_selectionModeBottomBar
->setVisible(false, WithAnimation
);
342 Q_EMIT
selectionModeChanged(false);
346 if (!m_selectionModeTopBar
) {
347 // Changing the location will disable selection mode.
348 connect(m_urlNavigator
.get(), &DolphinUrlNavigator::urlChanged
, this, [this]() {
349 setSelectionModeEnabled(false);
352 m_selectionModeTopBar
= new SelectionMode::TopBar(this); // will be created hidden
353 connect(m_selectionModeTopBar
, &SelectionMode::TopBar::selectionModeLeavingRequested
, this, [this]() {
354 setSelectionModeEnabled(false);
356 m_topLayout
->addWidget(m_selectionModeTopBar
, positionFor
.selectionModeTopBar
, 0);
359 if (!m_selectionModeBottomBar
) {
360 m_selectionModeBottomBar
= new SelectionMode::BottomBar(actionCollection
, this);
361 connect(m_view
, &DolphinView::selectionChanged
, this, [this](const KFileItemList
&selection
) {
362 m_selectionModeBottomBar
->slotSelectionChanged(selection
, m_view
->url());
364 connect(m_selectionModeBottomBar
, &SelectionMode::BottomBar::error
, this, [this](const QString
&errorMessage
) {
365 showErrorMessage(errorMessage
);
367 connect(m_selectionModeBottomBar
, &SelectionMode::BottomBar::selectionModeLeavingRequested
, this, [this]() {
368 setSelectionModeEnabled(false);
370 m_topLayout
->addWidget(m_selectionModeBottomBar
, positionFor
.selectionModeBottomBar
, 0);
372 m_selectionModeBottomBar
->resetContents(bottomBarContents
);
373 if (bottomBarContents
== SelectionMode::BottomBar::GeneralContents
) {
374 m_selectionModeBottomBar
->slotSelectionChanged(m_view
->selectedItems(), m_view
->url());
378 m_selectionModeTopBar
->setVisible(true, WithAnimation
);
379 m_selectionModeBottomBar
->setVisible(true, WithAnimation
);
380 Q_EMIT
selectionModeChanged(true);
384 bool DolphinViewContainer::isSelectionModeEnabled() const
386 const bool isEnabled
= m_view
->selectionMode();
388 // We can't assert that the bars are invisible only because the selection mode is disabled because the hide animation might still be playing.
389 && (!m_selectionModeBottomBar
|| !m_selectionModeBottomBar
->isEnabled() || !m_selectionModeBottomBar
->isVisible()
390 || m_selectionModeBottomBar
->contents() == SelectionMode::BottomBar::PasteContents
))
391 || (isEnabled
&& m_selectionModeTopBar
392 && m_selectionModeTopBar
->isVisible()
393 // 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.
394 && m_selectionModeBottomBar
395 && (m_selectionModeBottomBar
->isVisible() || m_selectionModeBottomBar
->contents() == SelectionMode::BottomBar::GeneralContents
)));
399 void DolphinViewContainer::slotSplitTabDisabled()
401 if (m_selectionModeBottomBar
) {
402 m_selectionModeBottomBar
->slotSplitTabDisabled();
406 void DolphinViewContainer::showMessage(const QString
&msg
, MessageType type
)
412 m_messageWidget
->setText(msg
);
414 // TODO: wrap at arbitrary character positions once QLabel can do this
415 // https://bugreports.qt.io/browse/QTBUG-1276
416 m_messageWidget
->setWordWrap(true);
420 m_messageWidget
->setMessageType(KMessageWidget::Information
);
423 m_messageWidget
->setMessageType(KMessageWidget::Warning
);
426 m_messageWidget
->setMessageType(KMessageWidget::Error
);
433 m_messageWidget
->setWordWrap(false);
434 const int unwrappedWidth
= m_messageWidget
->sizeHint().width();
435 m_messageWidget
->setWordWrap(unwrappedWidth
> size().width());
437 if (m_messageWidget
->isVisible()) {
438 m_messageWidget
->hide();
440 m_messageWidget
->animatedShow();
443 void DolphinViewContainer::readSettings()
445 // The startup settings should (only) get applied if they have been
446 // modified by the user. Otherwise keep the (possibly) different current
447 // setting of the filterbar.
448 if (GeneralSettings::modifiedStartupSettings()) {
449 setFilterBarVisible(GeneralSettings::filterBar());
452 m_view
->readSettings();
453 m_statusBar
->readSettings();
456 bool DolphinViewContainer::isFilterBarVisible() const
458 return m_filterBar
->isVisible();
461 void DolphinViewContainer::setSearchModeEnabled(bool enabled
)
463 m_searchBox
->setVisible(enabled
);
466 const QUrl
&locationUrl
= m_urlNavigator
->locationUrl();
467 m_searchBox
->fromSearchUrl(locationUrl
);
470 if (enabled
== isSearchModeEnabled()) {
471 if (enabled
&& !m_searchBox
->hasFocus()) {
472 m_searchBox
->setFocus();
473 m_searchBox
->selectAll();
479 m_view
->setViewPropertiesContext(QString());
481 // Restore the URL for the URL navigator. If Dolphin has been
482 // started with a search-URL, the home URL is used as fallback.
483 QUrl url
= m_searchBox
->searchPath();
484 if (url
.isEmpty() || !url
.isValid() || isSearchUrl(url
)) {
485 url
= Dolphin::homeUrl();
487 m_urlNavigatorConnected
->setLocationUrl(url
);
490 m_searchModeEnabled
= enabled
;
492 Q_EMIT
searchModeEnabledChanged(enabled
);
495 bool DolphinViewContainer::isSearchModeEnabled() const
497 return m_searchModeEnabled
;
500 QString
DolphinViewContainer::placesText() const
504 if (isSearchModeEnabled()) {
505 text
= i18n("Search for %1 in %2", m_searchBox
->text(), m_searchBox
->searchPath().fileName());
507 text
= url().adjusted(QUrl::StripTrailingSlash
).fileName();
508 if (text
.isEmpty()) {
511 if (text
.isEmpty()) {
512 text
= url().scheme();
519 void DolphinViewContainer::reload()
522 m_messageWidget
->hide();
525 QString
DolphinViewContainer::captionWindowTitle() const
527 if (GeneralSettings::showFullPathInTitlebar() && !isSearchModeEnabled()) {
528 if (!url().isLocalFile()) {
529 return url().adjusted(QUrl::StripTrailingSlash
).toString();
531 return url().adjusted(QUrl::StripTrailingSlash
).path();
533 return DolphinViewContainer::caption();
537 QString
DolphinViewContainer::caption() const
539 if (isSearchModeEnabled()) {
540 if (currentSearchText().isEmpty()) {
541 return i18n("Search");
543 return i18n("Search for %1", currentSearchText());
547 KFilePlacesModel
*placesModel
= DolphinPlacesModelSingleton::instance().placesModel();
548 const QString pattern
= url().adjusted(QUrl::StripTrailingSlash
).toString(QUrl::FullyEncoded
).append("/?");
549 const auto &matchedPlaces
=
550 placesModel
->match(placesModel
->index(0, 0), KFilePlacesModel::UrlRole
, QRegularExpression::anchoredPattern(pattern
), 1, Qt::MatchRegularExpression
);
552 if (!matchedPlaces
.isEmpty()) {
553 return placesModel
->text(matchedPlaces
.first());
556 if (!url().isLocalFile()) {
557 QUrl adjustedUrl
= url().adjusted(QUrl::StripTrailingSlash
);
559 if (!adjustedUrl
.fileName().isEmpty()) {
560 caption
= adjustedUrl
.fileName();
561 } else if (!adjustedUrl
.path().isEmpty() && adjustedUrl
.path() != "/") {
562 caption
= adjustedUrl
.path();
563 } else if (!adjustedUrl
.host().isEmpty()) {
564 caption
= adjustedUrl
.host();
566 caption
= adjustedUrl
.toString();
571 QString fileName
= url().adjusted(QUrl::StripTrailingSlash
).fileName();
572 if (fileName
.isEmpty()) {
579 void DolphinViewContainer::setUrl(const QUrl
&newUrl
)
581 if (newUrl
!= m_urlNavigator
->locationUrl()) {
582 m_urlNavigator
->setLocationUrl(newUrl
);
586 m_activityResourceInstance
->setUri(newUrl
);
590 void DolphinViewContainer::setFilterBarVisible(bool visible
)
592 Q_ASSERT(m_filterBar
);
594 m_view
->hideToolTip(ToolTipManager::HideBehavior::Instantly
);
596 m_filterBar
->setFocus();
597 m_filterBar
->selectAll();
603 void DolphinViewContainer::delayedStatusBarUpdate()
605 if (m_statusBarTimer
->isActive() && (m_statusBarTimestamp
.elapsed() > 2000)) {
606 // No update of the statusbar has been done during the last 2 seconds,
607 // although an update has been requested. Trigger an immediate update.
608 m_statusBarTimer
->stop();
611 // Invoke updateStatusBar() with a small delay. This assures that
612 // when a lot of delayedStatusBarUpdates() are done in a short time,
613 // no bottleneck is given.
614 m_statusBarTimer
->start();
618 void DolphinViewContainer::updateStatusBar()
620 m_statusBarTimestamp
.start();
621 m_view
->requestStatusBarText();
624 void DolphinViewContainer::updateDirectoryLoadingProgress(int percent
)
626 if (m_statusBar
->progressText().isEmpty()) {
627 m_statusBar
->setProgressText(i18nc("@info:progress", "Loading folder..."));
629 m_statusBar
->setProgress(percent
);
632 void DolphinViewContainer::updateDirectorySortingProgress(int percent
)
634 if (m_statusBar
->progressText().isEmpty()) {
635 m_statusBar
->setProgressText(i18nc("@info:progress", "Sorting..."));
637 m_statusBar
->setProgress(percent
);
640 void DolphinViewContainer::slotDirectoryLoadingStarted()
642 if (isSearchUrl(url())) {
643 // Search KIO-slaves usually don't provide any progress information. Give
644 // a hint to the user that a searching is done:
646 m_statusBar
->setProgressText(i18nc("@info", "Searching..."));
647 m_statusBar
->setProgress(-1);
649 // Trigger an undetermined progress indication. The progress
650 // information in percent will be triggered by the percent() signal
651 // of the directory lister later.
652 m_statusBar
->setProgressText(QString());
653 updateDirectoryLoadingProgress(-1);
657 void DolphinViewContainer::slotDirectoryLoadingCompleted()
659 if (!m_statusBar
->progressText().isEmpty()) {
660 m_statusBar
->setProgressText(QString());
661 m_statusBar
->setProgress(100);
664 if (isSearchUrl(url()) && m_view
->itemsCount() == 0) {
665 // The dir lister has been completed on a Baloo-URI and no items have been found. Instead
666 // of showing the default status bar information ("0 items") a more helpful information is given:
667 m_statusBar
->setText(i18nc("@info:status", "No items found."));
673 void DolphinViewContainer::slotDirectoryLoadingCanceled()
675 if (!m_statusBar
->progressText().isEmpty()) {
676 m_statusBar
->setProgressText(QString());
677 m_statusBar
->setProgress(100);
680 m_statusBar
->setText(QString());
683 void DolphinViewContainer::slotUrlIsFileError(const QUrl
&url
)
685 const KFileItem
item(url
);
687 // Find out if the file can be opened in the view (for example, this is the
688 // case if the file is an archive). The mime type must be known for that.
689 item
.determineMimeType();
690 const QUrl
&folderUrl
= DolphinView::openItemAsFolderUrl(item
, true);
691 if (!folderUrl
.isEmpty()) {
694 slotItemActivated(item
);
698 void DolphinViewContainer::slotItemActivated(const KFileItem
&item
)
700 // It is possible to activate items on inactive views by
701 // drag & drop operations. Assure that activating an item always
702 // results in an active view.
703 m_view
->setActive(true);
705 const QUrl
&url
= DolphinView::openItemAsFolderUrl(item
, GeneralSettings::browseThroughArchives());
706 if (!url
.isEmpty()) {
707 const auto modifiers
= QGuiApplication::keyboardModifiers();
708 // keep in sync with KUrlNavigator::slotNavigatorButtonClicked
709 if (modifiers
& Qt::ControlModifier
&& modifiers
& Qt::ShiftModifier
) {
710 Q_EMIT
activeTabRequested(url
);
711 } else if (modifiers
& Qt::ControlModifier
) {
712 Q_EMIT
tabRequested(url
);
713 } else if (modifiers
& Qt::ShiftModifier
) {
714 Dolphin::openNewWindow({KFilePlacesModel::convertedUrl(url
)}, this);
721 KIO::OpenUrlJob
*job
= new KIO::OpenUrlJob(item
.targetUrl(), item
.mimetype());
722 #if KIO_VERSION >= QT_VERSION_CHECK(5, 98, 0)
723 job
->setUiDelegate(KIO::createDefaultJobUiDelegate(KJobUiDelegate::AutoHandlingEnabled
, this));
725 job
->setUiDelegate(new KIO::JobUiDelegate(KJobUiDelegate::AutoHandlingEnabled
, this));
727 job
->setShowOpenOrExecuteDialog(true);
728 connect(job
, &KIO::OpenUrlJob::finished
, this, &DolphinViewContainer::slotOpenUrlFinished
);
732 void DolphinViewContainer::slotItemsActivated(const KFileItemList
&items
)
734 Q_ASSERT(items
.count() >= 2);
736 KFileItemActions
fileItemActions(this);
737 fileItemActions
.runPreferredApplications(items
);
740 void DolphinViewContainer::showItemInfo(const KFileItem
&item
)
743 m_statusBar
->resetToDefaultText();
745 m_statusBar
->setText(item
.getStatusBarInfo());
749 void DolphinViewContainer::closeFilterBar()
751 m_filterBar
->closeFilterBar();
753 Q_EMIT
showFilterBarChanged(false);
756 void DolphinViewContainer::clearFilterBar()
758 m_filterBar
->clearIfUnlocked();
761 void DolphinViewContainer::setNameFilter(const QString
&nameFilter
)
763 m_view
->hideToolTip(ToolTipManager::HideBehavior::Instantly
);
764 m_view
->setNameFilter(nameFilter
);
765 delayedStatusBarUpdate();
768 void DolphinViewContainer::activate()
773 void DolphinViewContainer::slotUrlNavigatorLocationAboutToBeChanged(const QUrl
&)
778 void DolphinViewContainer::slotUrlNavigatorLocationChanged(const QUrl
&url
)
780 if (m_urlNavigatorConnected
) {
781 m_urlNavigatorConnected
->slotReturnPressed();
784 if (KProtocolManager::supportsListing(url
)) {
785 setSearchModeEnabled(isSearchUrl(url
));
787 tryRestoreViewState();
789 if (m_autoGrabFocus
&& isActive() && !isSearchUrl(url
)) {
790 // When an URL has been entered, the view should get the focus.
791 // The focus must be requested asynchronously, as changing the URL might create
792 // a new view widget.
793 QTimer::singleShot(0, this, &DolphinViewContainer::requestFocus
);
795 } else if (KProtocolManager::isSourceProtocol(url
)) {
796 if (url
.scheme().startsWith(QLatin1String("http"))) {
797 showMessage(i18nc("@info:status", // krazy:exclude=qmethods
798 "Dolphin does not support web pages, the web browser has been launched"),
801 showMessage(i18nc("@info:status", "Protocol not supported by Dolphin, default application has been launched"), Information
);
804 QDesktopServices::openUrl(url
);
805 redirect(QUrl(), m_urlNavigator
->locationUrl(1));
807 showMessage(i18nc("@info:status", "Invalid protocol"), Error
);
808 m_urlNavigator
->goBack();
812 void DolphinViewContainer::slotUrlSelectionRequested(const QUrl
&url
)
814 m_view
->markUrlsAsSelected({url
});
815 m_view
->markUrlAsCurrent(url
); // makes the item scroll into view
818 void DolphinViewContainer::disableUrlNavigatorSelectionRequests()
820 disconnect(m_urlNavigator
.get(), &KUrlNavigator::urlSelectionRequested
, this, &DolphinViewContainer::slotUrlSelectionRequested
);
823 void DolphinViewContainer::enableUrlNavigatorSelectionRequests()
825 connect(m_urlNavigator
.get(), &KUrlNavigator::urlSelectionRequested
, this, &DolphinViewContainer::slotUrlSelectionRequested
);
828 void DolphinViewContainer::redirect(const QUrl
&oldUrl
, const QUrl
&newUrl
)
831 const bool block
= m_urlNavigator
->signalsBlocked();
832 m_urlNavigator
->blockSignals(true);
834 // Assure that the location state is reset for redirection URLs. This
835 // allows to skip redirection URLs when going back or forward in the
837 m_urlNavigator
->saveLocationState(QByteArray());
838 m_urlNavigator
->setLocationUrl(newUrl
);
839 setSearchModeEnabled(isSearchUrl(newUrl
));
841 m_urlNavigator
->blockSignals(block
);
844 void DolphinViewContainer::requestFocus()
849 void DolphinViewContainer::startSearching()
851 Q_CHECK_PTR(m_urlNavigatorConnected
);
852 const QUrl url
= m_searchBox
->urlForSearching();
853 if (url
.isValid() && !url
.isEmpty()) {
854 m_view
->setViewPropertiesContext(QStringLiteral("search"));
855 m_urlNavigatorConnected
->setLocationUrl(url
);
859 void DolphinViewContainer::closeSearchBox()
861 setSearchModeEnabled(false);
864 void DolphinViewContainer::stopDirectoryLoading()
866 m_view
->stopLoading();
867 m_statusBar
->setProgress(100);
870 void DolphinViewContainer::slotStatusBarZoomLevelChanged(int zoomLevel
)
872 m_view
->setZoomLevel(zoomLevel
);
875 void DolphinViewContainer::showErrorMessage(const QString
&msg
)
877 showMessage(msg
, Error
);
880 void DolphinViewContainer::slotPlacesModelChanged()
882 if (!GeneralSettings::showFullPathInTitlebar() && !isSearchModeEnabled()) {
883 Q_EMIT
captionChanged();
887 void DolphinViewContainer::slotHiddenFilesShownChanged(bool showHiddenFiles
)
889 if (m_urlNavigatorConnected
) {
890 m_urlNavigatorConnected
->setShowHiddenFolders(showHiddenFiles
);
894 void DolphinViewContainer::slotSortHiddenLastChanged(bool hiddenLast
)
896 if (m_urlNavigatorConnected
) {
897 m_urlNavigatorConnected
->setSortHiddenFoldersLast(hiddenLast
);
901 void DolphinViewContainer::slotCurrentDirectoryRemoved()
903 const QString
location(url().toDisplayString(QUrl::PreferLocalFile
));
904 if (url().isLocalFile()) {
905 const QString dirPath
= url().toLocalFile();
906 const QString newPath
= getNearestExistingAncestorOfPath(dirPath
);
907 const QUrl newUrl
= QUrl::fromLocalFile(newPath
);
911 showMessage(xi18n("Current location changed, <filename>%1</filename> is no longer accessible.", location
), Warning
);
914 void DolphinViewContainer::slotOpenUrlFinished(KJob
*job
)
916 if (job
->error() && job
->error() != KIO::ERR_USER_CANCELED
) {
917 showErrorMessage(job
->errorString());
921 bool DolphinViewContainer::isSearchUrl(const QUrl
&url
) const
923 return url
.scheme().contains(QLatin1String("search"));
926 void DolphinViewContainer::saveViewState()
928 QByteArray locationState
;
929 QDataStream
stream(&locationState
, QIODevice::WriteOnly
);
930 m_view
->saveState(stream
);
931 m_urlNavigator
->saveLocationState(locationState
);
934 void DolphinViewContainer::tryRestoreViewState()
936 QByteArray locationState
= m_urlNavigator
->locationState();
937 if (!locationState
.isEmpty()) {
938 QDataStream
stream(&locationState
, QIODevice::ReadOnly
);
939 m_view
->restoreState(stream
);
943 QString
DolphinViewContainer::getNearestExistingAncestorOfPath(const QString
&path
) const
947 dir
.setPath(QDir::cleanPath(dir
.filePath(QStringLiteral(".."))));
948 } while (!dir
.exists() && !dir
.isRoot());
950 return dir
.exists() ? dir
.path() : QString
{};