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>
23 #if HAVE_PLASMA_ACTIVITIES
24 #include <PlasmaActivities/ResourceInstance>
26 #include <KApplicationTrader>
27 #include <KFileItemActions>
28 #include <KFilePlacesModel>
29 #include <KIO/JobUiDelegateFactory>
30 #include <KIO/OpenUrlJob>
31 #include <KLocalizedString>
32 #include <KMessageWidget>
33 #include <KProtocolManager>
35 #include <kio_version.h>
37 #include <QApplication>
38 #include <QDesktopServices>
40 #include <QGridLayout>
41 #include <QGuiApplication>
42 #include <QRegularExpression>
46 // An overview of the widgets contained by this ViewContainer
47 struct LayoutStructure
{
49 int messageWidget
= 1;
50 int selectionModeTopBar
= 2;
52 int selectionModeBottomBar
= 4;
56 constexpr LayoutStructure positionFor
;
58 DolphinViewContainer::DolphinViewContainer(const QUrl
&url
, QWidget
*parent
)
60 , m_topLayout(nullptr)
61 , m_urlNavigator
{new DolphinUrlNavigator(url
)}
62 , m_urlNavigatorConnected
{nullptr}
63 , m_searchBox(nullptr)
64 , m_searchModeEnabled(false)
65 , m_messageWidget(nullptr)
66 , m_selectionModeTopBar
{nullptr}
68 , m_filterBar(nullptr)
69 , m_selectionModeBottomBar
{nullptr}
70 , m_statusBar(nullptr)
71 , m_statusBarTimer(nullptr)
72 , m_statusBarTimestamp()
73 , m_autoGrabFocus(true)
77 m_topLayout
= new QGridLayout(this);
78 m_topLayout
->setSpacing(0);
79 m_topLayout
->setContentsMargins(0, 0, 0, 0);
81 m_searchBox
= new DolphinSearchBox(this);
83 connect(m_searchBox
, &DolphinSearchBox::activated
, this, &DolphinViewContainer::activate
);
84 connect(m_searchBox
, &DolphinSearchBox::openRequest
, this, &DolphinViewContainer::openSearchBox
);
85 connect(m_searchBox
, &DolphinSearchBox::closeRequest
, this, &DolphinViewContainer::closeSearchBox
);
86 connect(m_searchBox
, &DolphinSearchBox::searchRequest
, this, &DolphinViewContainer::startSearching
);
87 connect(m_searchBox
, &DolphinSearchBox::focusViewRequest
, this, &DolphinViewContainer::requestFocus
);
88 m_searchBox
->setWhatsThis(xi18nc("@info:whatsthis findbar",
89 "<para>This helps you find files and folders. Enter a <emphasis>"
90 "search term</emphasis> and specify search settings with the "
91 "buttons at the bottom:<list><item>Filename/Content: "
92 "Does the item you are looking for contain the search terms "
93 "within its filename or its contents?<nl/>The contents of images, "
94 "audio files and videos will not be searched.</item><item>"
95 "From Here/Everywhere: Do you want to search in this "
96 "folder and its sub-folders or everywhere?</item><item>"
97 "More Options: Click this to search by media type, access "
98 "time or rating.</item><item>More Search Tools: Install other "
99 "means to find an item.</item></list></para>"));
101 m_messageWidget
= new KMessageWidget(this);
102 m_messageWidget
->setCloseButtonVisible(true);
103 m_messageWidget
->setPosition(KMessageWidget::Header
);
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::fileMiddleClickActivated
, this, &DolphinViewContainer::slotfileMiddleClickActivated
);
135 connect(m_view
, &DolphinView::itemsActivated
, this, &DolphinViewContainer::slotItemsActivated
);
136 connect(m_view
, &DolphinView::redirection
, this, &DolphinViewContainer::redirect
);
137 connect(m_view
, &DolphinView::directoryLoadingStarted
, this, &DolphinViewContainer::slotDirectoryLoadingStarted
);
138 connect(m_view
, &DolphinView::directoryLoadingCompleted
, this, &DolphinViewContainer::slotDirectoryLoadingCompleted
);
139 connect(m_view
, &DolphinView::directoryLoadingCanceled
, this, &DolphinViewContainer::slotDirectoryLoadingCanceled
);
140 connect(m_view
, &DolphinView::itemCountChanged
, this, &DolphinViewContainer::delayedStatusBarUpdate
);
141 connect(m_view
, &DolphinView::directoryLoadingProgress
, this, &DolphinViewContainer::updateDirectoryLoadingProgress
);
142 connect(m_view
, &DolphinView::directorySortingProgress
, this, &DolphinViewContainer::updateDirectorySortingProgress
);
143 connect(m_view
, &DolphinView::selectionChanged
, this, &DolphinViewContainer::delayedStatusBarUpdate
);
144 connect(m_view
, &DolphinView::errorMessage
, this, &DolphinViewContainer::showErrorMessage
);
145 connect(m_view
, &DolphinView::urlIsFileError
, this, &DolphinViewContainer::slotUrlIsFileError
);
146 connect(m_view
, &DolphinView::activated
, this, &DolphinViewContainer::activate
);
147 connect(m_view
, &DolphinView::hiddenFilesShownChanged
, this, &DolphinViewContainer::slotHiddenFilesShownChanged
);
148 connect(m_view
, &DolphinView::sortHiddenLastChanged
, this, &DolphinViewContainer::slotSortHiddenLastChanged
);
149 connect(m_view
, &DolphinView::currentDirectoryRemoved
, this, &DolphinViewContainer::slotCurrentDirectoryRemoved
);
151 // Initialize status bar
152 m_statusBar
= new DolphinStatusBar(this);
153 m_statusBar
->setUrl(m_view
->url());
154 m_statusBar
->setZoomLevel(m_view
->zoomLevel());
155 connect(m_view
, &DolphinView::urlChanged
, m_statusBar
, &DolphinStatusBar::setUrl
);
156 connect(m_view
, &DolphinView::zoomLevelChanged
, m_statusBar
, &DolphinStatusBar::setZoomLevel
);
157 connect(m_view
, &DolphinView::infoMessage
, m_statusBar
, &DolphinStatusBar::setText
);
158 connect(m_view
, &DolphinView::operationCompletedMessage
, m_statusBar
, &DolphinStatusBar::setText
);
159 connect(m_view
, &DolphinView::statusBarTextChanged
, m_statusBar
, &DolphinStatusBar::setDefaultText
);
160 connect(m_view
, &DolphinView::statusBarTextChanged
, m_statusBar
, &DolphinStatusBar::resetToDefaultText
);
161 connect(m_statusBar
, &DolphinStatusBar::stopPressed
, this, &DolphinViewContainer::stopDirectoryLoading
);
162 connect(m_statusBar
, &DolphinStatusBar::zoomLevelChanged
, this, &DolphinViewContainer::slotStatusBarZoomLevelChanged
);
164 m_statusBarTimer
= new QTimer(this);
165 m_statusBarTimer
->setSingleShot(true);
166 m_statusBarTimer
->setInterval(300);
167 connect(m_statusBarTimer
, &QTimer::timeout
, this, &DolphinViewContainer::updateStatusBar
);
169 KIO::FileUndoManager
*undoManager
= KIO::FileUndoManager::self();
170 connect(undoManager
, &KIO::FileUndoManager::jobRecordingFinished
, this, &DolphinViewContainer::delayedStatusBarUpdate
);
172 m_topLayout
->addWidget(m_searchBox
, positionFor
.searchBox
, 0);
173 m_topLayout
->addWidget(m_messageWidget
, positionFor
.messageWidget
, 0);
174 m_topLayout
->addWidget(m_view
, positionFor
.view
, 0);
175 m_topLayout
->addWidget(m_filterBar
, positionFor
.filterBar
, 0);
176 m_topLayout
->addWidget(m_statusBar
, positionFor
.statusBar
, 0);
178 setSearchModeEnabled(isSearchUrl(url
));
180 // Update view as the ContentDisplaySettings change
181 // this happens here and not in DolphinView as DolphinviewContainer and DolphinView are not in the same build target ATM
182 connect(ContentDisplaySettings::self(), &KCoreConfigSkeleton::configChanged
, m_view
, &DolphinView::reload
);
184 KFilePlacesModel
*placesModel
= DolphinPlacesModelSingleton::instance().placesModel();
185 connect(placesModel
, &KFilePlacesModel::dataChanged
, this, &DolphinViewContainer::slotPlacesModelChanged
);
186 connect(placesModel
, &KFilePlacesModel::rowsInserted
, this, &DolphinViewContainer::slotPlacesModelChanged
);
187 connect(placesModel
, &KFilePlacesModel::rowsRemoved
, this, &DolphinViewContainer::slotPlacesModelChanged
);
189 connect(this, &DolphinViewContainer::searchModeEnabledChanged
, this, &DolphinViewContainer::captionChanged
);
192 DolphinViewContainer::~DolphinViewContainer()
196 QUrl
DolphinViewContainer::url() const
198 return m_view
->url();
201 KFileItem
DolphinViewContainer::rootItem() const
203 return m_view
->rootItem();
206 void DolphinViewContainer::setActive(bool active
)
208 m_searchBox
->setActive(active
);
209 if (m_urlNavigatorConnected
) {
210 m_urlNavigatorConnected
->setActive(active
);
212 m_view
->setActive(active
);
215 bool DolphinViewContainer::isActive() const
217 return m_view
->isActive();
220 void DolphinViewContainer::setAutoGrabFocus(bool grab
)
222 m_autoGrabFocus
= grab
;
225 bool DolphinViewContainer::autoGrabFocus() const
227 return m_autoGrabFocus
;
230 QString
DolphinViewContainer::currentSearchText() const
232 return m_searchBox
->text();
235 const DolphinStatusBar
*DolphinViewContainer::statusBar() const
240 DolphinStatusBar
*DolphinViewContainer::statusBar()
245 const DolphinUrlNavigator
*DolphinViewContainer::urlNavigator() const
247 return m_urlNavigatorConnected
;
250 DolphinUrlNavigator
*DolphinViewContainer::urlNavigator()
252 return m_urlNavigatorConnected
;
255 const DolphinUrlNavigator
*DolphinViewContainer::urlNavigatorInternalWithHistory() const
257 return m_urlNavigator
.get();
260 DolphinUrlNavigator
*DolphinViewContainer::urlNavigatorInternalWithHistory()
262 return m_urlNavigator
.get();
265 const DolphinView
*DolphinViewContainer::view() const
270 DolphinView
*DolphinViewContainer::view()
275 void DolphinViewContainer::connectUrlNavigator(DolphinUrlNavigator
*urlNavigator
)
277 Q_CHECK_PTR(urlNavigator
);
278 Q_ASSERT(!m_urlNavigatorConnected
);
279 Q_ASSERT(m_urlNavigator
.get() != urlNavigator
);
282 urlNavigator
->setLocationUrl(m_view
->url());
283 urlNavigator
->setShowHiddenFolders(m_view
->hiddenFilesShown());
284 urlNavigator
->setSortHiddenFoldersLast(m_view
->sortHiddenLast());
285 if (m_urlNavigatorVisualState
) {
286 urlNavigator
->setVisualState(*m_urlNavigatorVisualState
.get());
287 m_urlNavigatorVisualState
.reset();
289 urlNavigator
->setActive(isActive());
291 // Url changes are still done via m_urlNavigator.
292 connect(urlNavigator
, &DolphinUrlNavigator::urlChanged
, m_urlNavigator
.get(), &DolphinUrlNavigator::setLocationUrl
);
293 connect(urlNavigator
, &DolphinUrlNavigator::urlsDropped
, this, [=](const QUrl
&destination
, QDropEvent
*event
) {
294 m_view
->dropUrls(destination
, event
, urlNavigator
->dropWidget());
296 // Aside from these, only visual things need to be connected.
297 connect(m_view
, &DolphinView::urlChanged
, urlNavigator
, &DolphinUrlNavigator::setLocationUrl
);
298 connect(urlNavigator
, &DolphinUrlNavigator::activated
, this, &DolphinViewContainer::activate
);
300 m_urlNavigatorConnected
= urlNavigator
;
303 void DolphinViewContainer::disconnectUrlNavigator()
305 if (!m_urlNavigatorConnected
) {
309 disconnect(m_urlNavigatorConnected
, &DolphinUrlNavigator::urlChanged
, m_urlNavigator
.get(), &DolphinUrlNavigator::setLocationUrl
);
310 disconnect(m_urlNavigatorConnected
, &DolphinUrlNavigator::urlsDropped
, this, nullptr);
311 disconnect(m_view
, &DolphinView::urlChanged
, m_urlNavigatorConnected
, &DolphinUrlNavigator::setLocationUrl
);
312 disconnect(m_urlNavigatorConnected
, &DolphinUrlNavigator::activated
, this, &DolphinViewContainer::activate
);
314 m_urlNavigatorVisualState
= m_urlNavigatorConnected
->visualState();
315 m_urlNavigatorConnected
= nullptr;
318 void DolphinViewContainer::setSelectionModeEnabled(bool enabled
, KActionCollection
*actionCollection
, SelectionMode::BottomBar::Contents bottomBarContents
)
320 const bool wasEnabled
= m_view
->selectionMode();
321 m_view
->setSelectionModeEnabled(enabled
);
325 return; // nothing to do here
327 Q_CHECK_PTR(m_selectionModeTopBar
); // there is no point in disabling selectionMode when it wasn't even enabled once.
328 Q_CHECK_PTR(m_selectionModeBottomBar
);
329 m_selectionModeTopBar
->setVisible(false, WithAnimation
);
330 m_selectionModeBottomBar
->setVisible(false, WithAnimation
);
331 Q_EMIT
selectionModeChanged(false);
333 if (m_selectionModeTopBar
->isAncestorOf(QApplication::focusWidget()) || m_selectionModeBottomBar
->isAncestorOf(QApplication::focusWidget())) {
339 if (!m_selectionModeTopBar
) {
340 // Changing the location will disable selection mode.
341 connect(m_urlNavigator
.get(), &DolphinUrlNavigator::urlChanged
, this, [this]() {
342 setSelectionModeEnabled(false);
345 m_selectionModeTopBar
= new SelectionMode::TopBar(this); // will be created hidden
346 connect(m_selectionModeTopBar
, &SelectionMode::TopBar::selectionModeLeavingRequested
, this, [this]() {
347 setSelectionModeEnabled(false);
349 m_topLayout
->addWidget(m_selectionModeTopBar
, positionFor
.selectionModeTopBar
, 0);
352 if (!m_selectionModeBottomBar
) {
353 m_selectionModeBottomBar
= new SelectionMode::BottomBar(actionCollection
, this);
354 connect(m_view
, &DolphinView::selectionChanged
, this, [this](const KFileItemList
&selection
) {
355 m_selectionModeBottomBar
->slotSelectionChanged(selection
, m_view
->url());
357 connect(m_selectionModeBottomBar
, &SelectionMode::BottomBar::error
, this, [this](const QString
&errorMessage
) {
358 showErrorMessage(errorMessage
);
360 connect(m_selectionModeBottomBar
, &SelectionMode::BottomBar::selectionModeLeavingRequested
, this, [this]() {
361 setSelectionModeEnabled(false);
363 m_topLayout
->addWidget(m_selectionModeBottomBar
, positionFor
.selectionModeBottomBar
, 0);
365 m_selectionModeBottomBar
->resetContents(bottomBarContents
);
366 if (bottomBarContents
== SelectionMode::BottomBar::GeneralContents
) {
367 m_selectionModeBottomBar
->slotSelectionChanged(m_view
->selectedItems(), m_view
->url());
371 m_selectionModeTopBar
->setVisible(true, WithAnimation
);
372 m_selectionModeBottomBar
->setVisible(true, WithAnimation
);
373 Q_EMIT
selectionModeChanged(true);
377 bool DolphinViewContainer::isSelectionModeEnabled() const
379 const bool isEnabled
= m_view
->selectionMode();
381 // We can't assert that the bars are invisible only because the selection mode is disabled because the hide animation might still be playing.
382 && (!m_selectionModeBottomBar
|| !m_selectionModeBottomBar
->isEnabled() || !m_selectionModeBottomBar
->isVisible()
383 || m_selectionModeBottomBar
->contents() == SelectionMode::BottomBar::PasteContents
))
384 || (isEnabled
&& m_selectionModeTopBar
385 && m_selectionModeTopBar
->isVisible()
386 // 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.
387 && m_selectionModeBottomBar
388 && (m_selectionModeBottomBar
->isVisible() || m_selectionModeBottomBar
->contents() == SelectionMode::BottomBar::GeneralContents
)));
392 void DolphinViewContainer::slotSplitTabDisabled()
394 if (m_selectionModeBottomBar
) {
395 m_selectionModeBottomBar
->slotSplitTabDisabled();
399 void DolphinViewContainer::showMessage(const QString
&msg
, MessageType type
)
405 m_messageWidget
->setText(msg
);
407 // TODO: wrap at arbitrary character positions once QLabel can do this
408 // https://bugreports.qt.io/browse/QTBUG-1276
409 m_messageWidget
->setWordWrap(true);
413 m_messageWidget
->setMessageType(KMessageWidget::Information
);
416 m_messageWidget
->setMessageType(KMessageWidget::Warning
);
419 m_messageWidget
->setMessageType(KMessageWidget::Error
);
426 m_messageWidget
->setWordWrap(false);
427 const int unwrappedWidth
= m_messageWidget
->sizeHint().width();
428 m_messageWidget
->setWordWrap(unwrappedWidth
> size().width());
430 if (m_messageWidget
->isVisible()) {
431 m_messageWidget
->hide();
433 m_messageWidget
->animatedShow();
436 void DolphinViewContainer::readSettings()
438 // The startup settings should (only) get applied if they have been
439 // modified by the user. Otherwise keep the (possibly) different current
440 // setting of the filterbar.
441 if (GeneralSettings::modifiedStartupSettings()) {
442 setFilterBarVisible(GeneralSettings::filterBar());
445 m_view
->readSettings();
446 m_statusBar
->readSettings();
449 bool DolphinViewContainer::isFilterBarVisible() const
451 return m_filterBar
->isVisible();
454 void DolphinViewContainer::setSearchModeEnabled(bool enabled
)
456 m_searchBox
->setVisible(enabled
);
459 const QUrl
&locationUrl
= m_urlNavigator
->locationUrl();
460 m_searchBox
->fromSearchUrl(locationUrl
);
463 if (enabled
== isSearchModeEnabled()) {
464 if (enabled
&& !m_searchBox
->hasFocus()) {
465 m_searchBox
->setFocus();
466 m_searchBox
->selectAll();
472 m_view
->setViewPropertiesContext(QString());
474 // Restore the URL for the URL navigator. If Dolphin has been
475 // started with a search-URL, the home URL is used as fallback.
476 QUrl url
= m_searchBox
->searchPath();
477 if (url
.isEmpty() || !url
.isValid() || isSearchUrl(url
)) {
478 url
= Dolphin::homeUrl();
480 m_urlNavigatorConnected
->setLocationUrl(url
);
483 m_searchModeEnabled
= enabled
;
485 Q_EMIT
searchModeEnabledChanged(enabled
);
488 bool DolphinViewContainer::isSearchModeEnabled() const
490 return m_searchModeEnabled
;
493 QString
DolphinViewContainer::placesText() const
497 if (isSearchModeEnabled()) {
498 text
= i18n("Search for %1 in %2", m_searchBox
->text(), m_searchBox
->searchPath().fileName());
500 text
= url().adjusted(QUrl::StripTrailingSlash
).fileName();
501 if (text
.isEmpty()) {
504 if (text
.isEmpty()) {
505 text
= url().scheme();
512 void DolphinViewContainer::reload()
515 m_messageWidget
->hide();
518 QString
DolphinViewContainer::captionWindowTitle() const
520 if (GeneralSettings::showFullPathInTitlebar() && !isSearchModeEnabled()) {
521 if (!url().isLocalFile()) {
522 return url().adjusted(QUrl::StripTrailingSlash
).toString();
524 return url().adjusted(QUrl::StripTrailingSlash
).path();
526 return DolphinViewContainer::caption();
530 QString
DolphinViewContainer::caption() const
532 if (isSearchModeEnabled()) {
533 if (currentSearchText().isEmpty()) {
534 return i18n("Search");
536 return i18n("Search for %1", currentSearchText());
540 KFilePlacesModel
*placesModel
= DolphinPlacesModelSingleton::instance().placesModel();
541 const QString pattern
= url().adjusted(QUrl::StripTrailingSlash
).toString(QUrl::FullyEncoded
).append("/?");
542 const auto &matchedPlaces
=
543 placesModel
->match(placesModel
->index(0, 0), KFilePlacesModel::UrlRole
, QRegularExpression::anchoredPattern(pattern
), 1, Qt::MatchRegularExpression
);
545 if (!matchedPlaces
.isEmpty()) {
546 return placesModel
->text(matchedPlaces
.first());
549 if (!url().isLocalFile()) {
550 QUrl adjustedUrl
= url().adjusted(QUrl::StripTrailingSlash
);
552 if (!adjustedUrl
.fileName().isEmpty()) {
553 caption
= adjustedUrl
.fileName();
554 } else if (!adjustedUrl
.path().isEmpty() && adjustedUrl
.path() != "/") {
555 caption
= adjustedUrl
.path();
556 } else if (!adjustedUrl
.host().isEmpty()) {
557 caption
= adjustedUrl
.host();
559 caption
= adjustedUrl
.toString();
564 QString fileName
= url().adjusted(QUrl::StripTrailingSlash
).fileName();
565 if (fileName
.isEmpty()) {
572 void DolphinViewContainer::setUrl(const QUrl
&newUrl
)
574 if (newUrl
!= m_urlNavigator
->locationUrl()) {
575 m_urlNavigator
->setLocationUrl(newUrl
);
578 #if HAVE_PLASMA_ACTIVITIES
579 KActivities::ResourceInstance::notifyAccessed(newUrl
);
583 void DolphinViewContainer::setFilterBarVisible(bool visible
)
585 Q_ASSERT(m_filterBar
);
587 m_view
->hideToolTip(ToolTipManager::HideBehavior::Instantly
);
589 m_filterBar
->setFocus();
590 m_filterBar
->selectAll();
596 void DolphinViewContainer::delayedStatusBarUpdate()
598 if (m_statusBarTimer
->isActive() && (m_statusBarTimestamp
.elapsed() > 2000)) {
599 // No update of the statusbar has been done during the last 2 seconds,
600 // although an update has been requested. Trigger an immediate update.
601 m_statusBarTimer
->stop();
604 // Invoke updateStatusBar() with a small delay. This assures that
605 // when a lot of delayedStatusBarUpdates() are done in a short time,
606 // no bottleneck is given.
607 m_statusBarTimer
->start();
611 void DolphinViewContainer::updateStatusBar()
613 m_statusBarTimestamp
.start();
614 m_view
->requestStatusBarText();
617 void DolphinViewContainer::updateDirectoryLoadingProgress(int percent
)
619 if (m_statusBar
->progressText().isEmpty()) {
620 m_statusBar
->setProgressText(i18nc("@info:progress", "Loading folder…"));
622 m_statusBar
->setProgress(percent
);
625 void DolphinViewContainer::updateDirectorySortingProgress(int percent
)
627 if (m_statusBar
->progressText().isEmpty()) {
628 m_statusBar
->setProgressText(i18nc("@info:progress", "Sorting…"));
630 m_statusBar
->setProgress(percent
);
633 void DolphinViewContainer::slotDirectoryLoadingStarted()
635 if (isSearchUrl(url())) {
636 // Search KIO-slaves usually don't provide any progress information. Give
637 // a hint to the user that a searching is done:
639 m_statusBar
->setProgressText(i18nc("@info", "Searching…"));
640 m_statusBar
->setProgress(-1);
642 // Trigger an undetermined progress indication. The progress
643 // information in percent will be triggered by the percent() signal
644 // of the directory lister later.
645 m_statusBar
->setProgressText(QString());
646 updateDirectoryLoadingProgress(-1);
650 void DolphinViewContainer::slotDirectoryLoadingCompleted()
652 if (!m_statusBar
->progressText().isEmpty()) {
653 m_statusBar
->setProgressText(QString());
654 m_statusBar
->setProgress(100);
657 if (isSearchUrl(url()) && m_view
->itemsCount() == 0) {
658 // The dir lister has been completed on a Baloo-URI and no items have been found. Instead
659 // of showing the default status bar information ("0 items") a more helpful information is given:
660 m_statusBar
->setText(i18nc("@info:status", "No items found."));
666 void DolphinViewContainer::slotDirectoryLoadingCanceled()
668 if (!m_statusBar
->progressText().isEmpty()) {
669 m_statusBar
->setProgressText(QString());
670 m_statusBar
->setProgress(100);
673 m_statusBar
->setText(QString());
676 void DolphinViewContainer::slotUrlIsFileError(const QUrl
&url
)
678 const KFileItem
item(url
);
680 // Find out if the file can be opened in the view (for example, this is the
681 // case if the file is an archive). The mime type must be known for that.
682 item
.determineMimeType();
683 const QUrl
&folderUrl
= DolphinView::openItemAsFolderUrl(item
, true);
684 if (!folderUrl
.isEmpty()) {
687 slotItemActivated(item
);
691 void DolphinViewContainer::slotItemActivated(const KFileItem
&item
)
693 // It is possible to activate items on inactive views by
694 // drag & drop operations. Assure that activating an item always
695 // results in an active view.
696 m_view
->setActive(true);
698 const QUrl
&url
= DolphinView::openItemAsFolderUrl(item
, GeneralSettings::browseThroughArchives());
699 if (!url
.isEmpty()) {
700 const auto modifiers
= QGuiApplication::keyboardModifiers();
701 // keep in sync with KUrlNavigator::slotNavigatorButtonClicked
702 if (modifiers
& Qt::ControlModifier
&& modifiers
& Qt::ShiftModifier
) {
703 Q_EMIT
activeTabRequested(url
);
704 } else if (modifiers
& Qt::ControlModifier
) {
705 Q_EMIT
tabRequested(url
);
706 } else if (modifiers
& Qt::ShiftModifier
) {
707 Dolphin::openNewWindow({KFilePlacesModel::convertedUrl(url
)}, this);
714 KIO::OpenUrlJob
*job
= new KIO::OpenUrlJob(item
.targetUrl(), item
.mimetype());
715 // Auto*Warning*Handling, errors are put in a KMessageWidget by us in slotOpenUrlFinished.
716 job
->setUiDelegate(KIO::createDefaultJobUiDelegate(KJobUiDelegate::AutoWarningHandlingEnabled
, this));
717 job
->setShowOpenOrExecuteDialog(true);
718 connect(job
, &KIO::OpenUrlJob::finished
, this, &DolphinViewContainer::slotOpenUrlFinished
);
722 void DolphinViewContainer::slotfileMiddleClickActivated(const KFileItem
&item
)
724 KService::List services
= KApplicationTrader::queryByMimeType(item
.mimetype());
726 int indexOfAppToOpenFileWith
= 1;
728 // executable scripts
729 auto mimeType
= item
.currentMimeType();
730 if (item
.isLocalFile() && mimeType
.inherits(QStringLiteral("application/x-executable")) && mimeType
.inherits(QStringLiteral("text/plain"))
731 && QFileInfo(item
.localPath()).isExecutable()) {
732 KConfigGroup
cfgGroup(KSharedConfig::openConfig(QStringLiteral("kiorc")), QStringLiteral("Executable scripts"));
733 const QString value
= cfgGroup
.readEntry("behaviourOnLaunch", "alwaysAsk");
735 // in case KIO::WidgetsOpenOrExecuteFileHandler::promptUserOpenOrExecute would not open the file
736 if (value
!= QLatin1String("open")) {
737 indexOfAppToOpenFileWith
= 0;
741 if (services
.length() >= indexOfAppToOpenFileWith
+ 1) {
742 auto service
= services
.at(indexOfAppToOpenFileWith
);
744 KIO::ApplicationLauncherJob
*job
= new KIO::ApplicationLauncherJob(service
, this);
745 job
->setUrls({item
.url()});
747 job
->setUiDelegate(KIO::createDefaultJobUiDelegate(KJobUiDelegate::AutoHandlingEnabled
, this));
748 connect(job
, &KIO::OpenUrlJob::finished
, this, &DolphinViewContainer::slotOpenUrlFinished
);
753 void DolphinViewContainer::slotItemsActivated(const KFileItemList
&items
)
755 Q_ASSERT(items
.count() >= 2);
757 KFileItemActions
fileItemActions(this);
758 fileItemActions
.runPreferredApplications(items
);
761 void DolphinViewContainer::showItemInfo(const KFileItem
&item
)
764 m_statusBar
->resetToDefaultText();
766 m_statusBar
->setText(item
.getStatusBarInfo());
770 void DolphinViewContainer::closeFilterBar()
772 m_filterBar
->closeFilterBar();
774 Q_EMIT
showFilterBarChanged(false);
777 void DolphinViewContainer::clearFilterBar()
779 m_filterBar
->clearIfUnlocked();
782 void DolphinViewContainer::setNameFilter(const QString
&nameFilter
)
784 m_view
->hideToolTip(ToolTipManager::HideBehavior::Instantly
);
785 m_view
->setNameFilter(nameFilter
);
786 delayedStatusBarUpdate();
789 void DolphinViewContainer::activate()
794 void DolphinViewContainer::slotUrlNavigatorLocationAboutToBeChanged(const QUrl
&)
799 void DolphinViewContainer::slotUrlNavigatorLocationChanged(const QUrl
&url
)
801 if (m_urlNavigatorConnected
) {
802 m_urlNavigatorConnected
->slotReturnPressed();
805 if (KProtocolManager::supportsListing(url
)) {
806 const bool searchBoxInitialized
= isSearchModeEnabled() && m_searchBox
->text().isEmpty();
807 setSearchModeEnabled(isSearchUrl(url
) || searchBoxInitialized
);
810 tryRestoreViewState();
812 if (m_autoGrabFocus
&& isActive() && !isSearchModeEnabled()) {
813 // When an URL has been entered, the view should get the focus.
814 // The focus must be requested asynchronously, as changing the URL might create
815 // a new view widget.
816 QTimer::singleShot(0, this, &DolphinViewContainer::requestFocus
);
818 } else if (KProtocolManager::isSourceProtocol(url
)) {
819 if (url
.scheme().startsWith(QLatin1String("http"))) {
820 showMessage(i18nc("@info:status", // krazy:exclude=qmethods
821 "Dolphin does not support web pages, the web browser has been launched"),
824 showMessage(i18nc("@info:status", "Protocol not supported by Dolphin, default application has been launched"), Information
);
827 QDesktopServices::openUrl(url
);
828 redirect(QUrl(), m_urlNavigator
->locationUrl(1));
830 if (!url
.scheme().isEmpty()) {
831 showMessage(i18nc("@info:status", "Invalid protocol '%1'", url
.scheme()), Error
);
833 showMessage(i18nc("@info:status", "Invalid protocol"), Error
);
835 m_urlNavigator
->goBack();
839 void DolphinViewContainer::slotUrlSelectionRequested(const QUrl
&url
)
841 m_view
->markUrlsAsSelected({url
});
842 m_view
->markUrlAsCurrent(url
); // makes the item scroll into view
845 void DolphinViewContainer::disableUrlNavigatorSelectionRequests()
847 disconnect(m_urlNavigator
.get(), &KUrlNavigator::urlSelectionRequested
, this, &DolphinViewContainer::slotUrlSelectionRequested
);
850 void DolphinViewContainer::enableUrlNavigatorSelectionRequests()
852 connect(m_urlNavigator
.get(), &KUrlNavigator::urlSelectionRequested
, this, &DolphinViewContainer::slotUrlSelectionRequested
);
855 void DolphinViewContainer::redirect(const QUrl
&oldUrl
, const QUrl
&newUrl
)
858 const bool block
= m_urlNavigator
->signalsBlocked();
859 m_urlNavigator
->blockSignals(true);
861 // Assure that the location state is reset for redirection URLs. This
862 // allows to skip redirection URLs when going back or forward in the
864 m_urlNavigator
->saveLocationState(QByteArray());
865 m_urlNavigator
->setLocationUrl(newUrl
);
866 setSearchModeEnabled(isSearchUrl(newUrl
));
868 m_urlNavigator
->blockSignals(block
);
871 void DolphinViewContainer::requestFocus()
876 void DolphinViewContainer::startSearching()
878 Q_CHECK_PTR(m_urlNavigatorConnected
);
879 const QUrl url
= m_searchBox
->urlForSearching();
880 if (url
.isValid() && !url
.isEmpty()) {
881 m_view
->setViewPropertiesContext(QStringLiteral("search"));
882 m_urlNavigatorConnected
->setLocationUrl(url
);
886 void DolphinViewContainer::openSearchBox()
888 setSearchModeEnabled(true);
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"