2 * SPDX-FileCopyrightText: 2007 Peter Penz <peter.penz19@gmail.com>
4 * SPDX-License-Identifier: GPL-2.0-or-later
7 #include "dolphinviewcontainer.h"
9 #include "dolphin_compactmodesettings.h"
10 #include "dolphin_contentdisplaysettings.h"
11 #include "dolphin_detailsmodesettings.h"
12 #include "dolphin_generalsettings.h"
13 #include "dolphin_iconsmodesettings.h"
14 #include "dolphindebug.h"
15 #include "dolphinplacesmodelsingleton.h"
16 #include "filterbar/filterbar.h"
18 #include "search/dolphinsearchbox.h"
19 #include "selectionmode/topbar.h"
20 #include "statusbar/dolphinstatusbar.h"
22 #include <KActionCollection>
24 #include <KActivities/ResourceInstance>
26 #include <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)
75 , m_activityResourceInstance(nullptr)
80 m_topLayout
= new QGridLayout(this);
81 m_topLayout
->setSpacing(0);
82 m_topLayout
->setContentsMargins(0, 0, 0, 0);
84 m_searchBox
= new DolphinSearchBox(this);
86 connect(m_searchBox
, &DolphinSearchBox::activated
, this, &DolphinViewContainer::activate
);
87 connect(m_searchBox
, &DolphinSearchBox::openRequest
, this, &DolphinViewContainer::openSearchBox
);
88 connect(m_searchBox
, &DolphinSearchBox::closeRequest
, this, &DolphinViewContainer::closeSearchBox
);
89 connect(m_searchBox
, &DolphinSearchBox::searchRequest
, this, &DolphinViewContainer::startSearching
);
90 connect(m_searchBox
, &DolphinSearchBox::focusViewRequest
, this, &DolphinViewContainer::requestFocus
);
91 m_searchBox
->setWhatsThis(xi18nc("@info:whatsthis findbar",
92 "<para>This helps you find files and folders. Enter a <emphasis>"
93 "search term</emphasis> and specify search settings with the "
94 "buttons at the bottom:<list><item>Filename/Content: "
95 "Does the item you are looking for contain the search terms "
96 "within its filename or its contents?<nl/>The contents of images, "
97 "audio files and videos will not be searched.</item><item>"
98 "From Here/Everywhere: Do you want to search in this "
99 "folder and its sub-folders or everywhere?</item><item>"
100 "More Options: Click this to search by media type, access "
101 "time or rating.</item><item>More Search Tools: Install other "
102 "means to find an item.</item></list></para>"));
104 m_messageWidget
= new KMessageWidget(this);
105 m_messageWidget
->setCloseButtonVisible(true);
106 m_messageWidget
->hide();
110 // We must be logged in as the root user; show a big scary warning
111 showMessage(i18n("Running Dolphin as root can be dangerous. Please be careful."), Warning
);
115 // Initialize filter bar
116 m_filterBar
= new FilterBar(this);
117 m_filterBar
->setVisible(GeneralSettings::filterBar());
119 connect(m_filterBar
, &FilterBar::filterChanged
, this, &DolphinViewContainer::setNameFilter
);
120 connect(m_filterBar
, &FilterBar::closeRequest
, this, &DolphinViewContainer::closeFilterBar
);
121 connect(m_filterBar
, &FilterBar::focusViewRequest
, this, &DolphinViewContainer::requestFocus
);
123 // Initialize the main view
124 m_view
= new DolphinView(url
, this);
125 connect(m_view
, &DolphinView::urlChanged
, m_filterBar
, &FilterBar::clearIfUnlocked
);
126 connect(m_view
, &DolphinView::urlChanged
, m_messageWidget
, &KMessageWidget::hide
);
127 // m_urlNavigator stays in sync with m_view's location changes and
128 // keeps track of them so going back and forth in the history works.
129 connect(m_view
, &DolphinView::urlChanged
, m_urlNavigator
.get(), &DolphinUrlNavigator::setLocationUrl
);
130 connect(m_urlNavigator
.get(), &DolphinUrlNavigator::urlChanged
, this, &DolphinViewContainer::slotUrlNavigatorLocationChanged
);
131 connect(m_urlNavigator
.get(), &DolphinUrlNavigator::urlAboutToBeChanged
, this, &DolphinViewContainer::slotUrlNavigatorLocationAboutToBeChanged
);
132 connect(m_urlNavigator
.get(), &DolphinUrlNavigator::urlSelectionRequested
, this, &DolphinViewContainer::slotUrlSelectionRequested
);
133 connect(m_view
, &DolphinView::writeStateChanged
, this, &DolphinViewContainer::writeStateChanged
);
134 connect(m_view
, &DolphinView::requestItemInfo
, this, &DolphinViewContainer::showItemInfo
);
135 connect(m_view
, &DolphinView::itemActivated
, this, &DolphinViewContainer::slotItemActivated
);
136 connect(m_view
, &DolphinView::fileMiddleClickActivated
, this, &DolphinViewContainer::slotfileMiddleClickActivated
);
137 connect(m_view
, &DolphinView::itemsActivated
, this, &DolphinViewContainer::slotItemsActivated
);
138 connect(m_view
, &DolphinView::redirection
, this, &DolphinViewContainer::redirect
);
139 connect(m_view
, &DolphinView::directoryLoadingStarted
, this, &DolphinViewContainer::slotDirectoryLoadingStarted
);
140 connect(m_view
, &DolphinView::directoryLoadingCompleted
, this, &DolphinViewContainer::slotDirectoryLoadingCompleted
);
141 connect(m_view
, &DolphinView::directoryLoadingCanceled
, this, &DolphinViewContainer::slotDirectoryLoadingCanceled
);
142 connect(m_view
, &DolphinView::itemCountChanged
, this, &DolphinViewContainer::delayedStatusBarUpdate
);
143 connect(m_view
, &DolphinView::directoryLoadingProgress
, this, &DolphinViewContainer::updateDirectoryLoadingProgress
);
144 connect(m_view
, &DolphinView::directorySortingProgress
, this, &DolphinViewContainer::updateDirectorySortingProgress
);
145 connect(m_view
, &DolphinView::selectionChanged
, this, &DolphinViewContainer::delayedStatusBarUpdate
);
146 connect(m_view
, &DolphinView::errorMessage
, this, &DolphinViewContainer::showErrorMessage
);
147 connect(m_view
, &DolphinView::urlIsFileError
, this, &DolphinViewContainer::slotUrlIsFileError
);
148 connect(m_view
, &DolphinView::activated
, this, &DolphinViewContainer::activate
);
149 connect(m_view
, &DolphinView::hiddenFilesShownChanged
, this, &DolphinViewContainer::slotHiddenFilesShownChanged
);
150 connect(m_view
, &DolphinView::sortHiddenLastChanged
, this, &DolphinViewContainer::slotSortHiddenLastChanged
);
151 connect(m_view
, &DolphinView::currentDirectoryRemoved
, this, &DolphinViewContainer::slotCurrentDirectoryRemoved
);
153 // Initialize status bar
154 m_statusBar
= new DolphinStatusBar(this);
155 m_statusBar
->setUrl(m_view
->url());
156 m_statusBar
->setZoomLevel(m_view
->zoomLevel());
157 connect(m_view
, &DolphinView::urlChanged
, m_statusBar
, &DolphinStatusBar::setUrl
);
158 connect(m_view
, &DolphinView::zoomLevelChanged
, m_statusBar
, &DolphinStatusBar::setZoomLevel
);
159 connect(m_view
, &DolphinView::infoMessage
, m_statusBar
, &DolphinStatusBar::setText
);
160 connect(m_view
, &DolphinView::operationCompletedMessage
, m_statusBar
, &DolphinStatusBar::setText
);
161 connect(m_view
, &DolphinView::statusBarTextChanged
, m_statusBar
, &DolphinStatusBar::setDefaultText
);
162 connect(m_view
, &DolphinView::statusBarTextChanged
, m_statusBar
, &DolphinStatusBar::resetToDefaultText
);
163 connect(m_statusBar
, &DolphinStatusBar::stopPressed
, this, &DolphinViewContainer::stopDirectoryLoading
);
164 connect(m_statusBar
, &DolphinStatusBar::zoomLevelChanged
, this, &DolphinViewContainer::slotStatusBarZoomLevelChanged
);
166 m_statusBarTimer
= new QTimer(this);
167 m_statusBarTimer
->setSingleShot(true);
168 m_statusBarTimer
->setInterval(300);
169 connect(m_statusBarTimer
, &QTimer::timeout
, this, &DolphinViewContainer::updateStatusBar
);
171 KIO::FileUndoManager
*undoManager
= KIO::FileUndoManager::self();
172 connect(undoManager
, &KIO::FileUndoManager::jobRecordingFinished
, this, &DolphinViewContainer::delayedStatusBarUpdate
);
174 m_topLayout
->addWidget(m_searchBox
, positionFor
.searchBox
, 0);
175 m_topLayout
->addWidget(m_messageWidget
, positionFor
.messageWidget
, 0);
176 m_topLayout
->addWidget(m_view
, positionFor
.view
, 0);
177 m_topLayout
->addWidget(m_filterBar
, positionFor
.filterBar
, 0);
178 m_topLayout
->addWidget(m_statusBar
, positionFor
.statusBar
, 0);
180 setSearchModeEnabled(isSearchUrl(url
));
182 // Update view as the ContentDisplaySettings change
183 // this happens here and not in DolphinView as DolphinviewContainer and DolphinView are not in the same build target ATM
184 connect(ContentDisplaySettings::self(), &KCoreConfigSkeleton::configChanged
, m_view
, &DolphinView::reload
);
186 KFilePlacesModel
*placesModel
= DolphinPlacesModelSingleton::instance().placesModel();
187 connect(placesModel
, &KFilePlacesModel::dataChanged
, this, &DolphinViewContainer::slotPlacesModelChanged
);
188 connect(placesModel
, &KFilePlacesModel::rowsInserted
, this, &DolphinViewContainer::slotPlacesModelChanged
);
189 connect(placesModel
, &KFilePlacesModel::rowsRemoved
, this, &DolphinViewContainer::slotPlacesModelChanged
);
191 connect(this, &DolphinViewContainer::searchModeEnabledChanged
, this, &DolphinViewContainer::captionChanged
);
193 // Initialize kactivities resource instance
196 m_activityResourceInstance
= new KActivities::ResourceInstance(window()->winId(), url
);
197 m_activityResourceInstance
->setParent(this);
201 DolphinViewContainer::~DolphinViewContainer()
205 QUrl
DolphinViewContainer::url() const
207 return m_view
->url();
210 KFileItem
DolphinViewContainer::rootItem() const
212 return m_view
->rootItem();
215 void DolphinViewContainer::setActive(bool active
)
217 m_searchBox
->setActive(active
);
218 if (m_urlNavigatorConnected
) {
219 m_urlNavigatorConnected
->setActive(active
);
221 m_view
->setActive(active
);
225 m_activityResourceInstance
->notifyFocusedIn();
227 m_activityResourceInstance
->notifyFocusedOut();
232 bool DolphinViewContainer::isActive() const
234 return m_view
->isActive();
237 void DolphinViewContainer::setAutoGrabFocus(bool grab
)
239 m_autoGrabFocus
= grab
;
242 bool DolphinViewContainer::autoGrabFocus() const
244 return m_autoGrabFocus
;
247 QString
DolphinViewContainer::currentSearchText() const
249 return m_searchBox
->text();
252 const DolphinStatusBar
*DolphinViewContainer::statusBar() const
257 DolphinStatusBar
*DolphinViewContainer::statusBar()
262 const DolphinUrlNavigator
*DolphinViewContainer::urlNavigator() const
264 return m_urlNavigatorConnected
;
267 DolphinUrlNavigator
*DolphinViewContainer::urlNavigator()
269 return m_urlNavigatorConnected
;
272 const DolphinUrlNavigator
*DolphinViewContainer::urlNavigatorInternalWithHistory() const
274 return m_urlNavigator
.get();
277 DolphinUrlNavigator
*DolphinViewContainer::urlNavigatorInternalWithHistory()
279 return m_urlNavigator
.get();
282 const DolphinView
*DolphinViewContainer::view() const
287 DolphinView
*DolphinViewContainer::view()
292 void DolphinViewContainer::connectUrlNavigator(DolphinUrlNavigator
*urlNavigator
)
294 Q_CHECK_PTR(urlNavigator
);
295 Q_ASSERT(!m_urlNavigatorConnected
);
296 Q_ASSERT(m_urlNavigator
.get() != urlNavigator
);
299 urlNavigator
->setLocationUrl(m_view
->url());
300 urlNavigator
->setShowHiddenFolders(m_view
->hiddenFilesShown());
301 urlNavigator
->setSortHiddenFoldersLast(m_view
->sortHiddenLast());
302 if (m_urlNavigatorVisualState
) {
303 urlNavigator
->setVisualState(*m_urlNavigatorVisualState
.get());
304 m_urlNavigatorVisualState
.reset();
306 urlNavigator
->setActive(isActive());
308 // Url changes are still done via m_urlNavigator.
309 connect(urlNavigator
, &DolphinUrlNavigator::urlChanged
, m_urlNavigator
.get(), &DolphinUrlNavigator::setLocationUrl
);
310 connect(urlNavigator
, &DolphinUrlNavigator::urlsDropped
, this, [=](const QUrl
&destination
, QDropEvent
*event
) {
311 m_view
->dropUrls(destination
, event
, urlNavigator
->dropWidget());
313 // Aside from these, only visual things need to be connected.
314 connect(m_view
, &DolphinView::urlChanged
, urlNavigator
, &DolphinUrlNavigator::setLocationUrl
);
315 connect(urlNavigator
, &DolphinUrlNavigator::activated
, this, &DolphinViewContainer::activate
);
317 m_urlNavigatorConnected
= urlNavigator
;
320 void DolphinViewContainer::disconnectUrlNavigator()
322 if (!m_urlNavigatorConnected
) {
326 disconnect(m_urlNavigatorConnected
, &DolphinUrlNavigator::urlChanged
, m_urlNavigator
.get(), &DolphinUrlNavigator::setLocationUrl
);
327 disconnect(m_urlNavigatorConnected
, &DolphinUrlNavigator::urlsDropped
, this, nullptr);
328 disconnect(m_view
, &DolphinView::urlChanged
, m_urlNavigatorConnected
, &DolphinUrlNavigator::setLocationUrl
);
329 disconnect(m_urlNavigatorConnected
, &DolphinUrlNavigator::activated
, this, &DolphinViewContainer::activate
);
331 m_urlNavigatorVisualState
= m_urlNavigatorConnected
->visualState();
332 m_urlNavigatorConnected
= nullptr;
335 void DolphinViewContainer::setSelectionModeEnabled(bool enabled
, KActionCollection
*actionCollection
, SelectionMode::BottomBar::Contents bottomBarContents
)
337 const bool wasEnabled
= m_view
->selectionMode();
338 m_view
->setSelectionModeEnabled(enabled
);
342 return; // nothing to do here
344 Q_CHECK_PTR(m_selectionModeTopBar
); // there is no point in disabling selectionMode when it wasn't even enabled once.
345 Q_CHECK_PTR(m_selectionModeBottomBar
);
346 if (m_selectionModeTopBar
->isAncestorOf(QApplication::focusWidget()) || m_selectionModeBottomBar
->isAncestorOf(QApplication::focusWidget())) {
349 m_selectionModeTopBar
->setVisible(false, WithAnimation
);
350 m_selectionModeBottomBar
->setVisible(false, WithAnimation
);
351 Q_EMIT
selectionModeChanged(false);
355 if (!m_selectionModeTopBar
) {
356 // Changing the location will disable selection mode.
357 connect(m_urlNavigator
.get(), &DolphinUrlNavigator::urlChanged
, this, [this]() {
358 setSelectionModeEnabled(false);
361 m_selectionModeTopBar
= new SelectionMode::TopBar(this); // will be created hidden
362 connect(m_selectionModeTopBar
, &SelectionMode::TopBar::selectionModeLeavingRequested
, this, [this]() {
363 setSelectionModeEnabled(false);
365 m_topLayout
->addWidget(m_selectionModeTopBar
, positionFor
.selectionModeTopBar
, 0);
368 if (!m_selectionModeBottomBar
) {
369 m_selectionModeBottomBar
= new SelectionMode::BottomBar(actionCollection
, this);
370 connect(m_view
, &DolphinView::selectionChanged
, this, [this](const KFileItemList
&selection
) {
371 m_selectionModeBottomBar
->slotSelectionChanged(selection
, m_view
->url());
373 connect(m_selectionModeBottomBar
, &SelectionMode::BottomBar::error
, this, [this](const QString
&errorMessage
) {
374 showErrorMessage(errorMessage
);
376 connect(m_selectionModeBottomBar
, &SelectionMode::BottomBar::selectionModeLeavingRequested
, this, [this]() {
377 setSelectionModeEnabled(false);
379 m_topLayout
->addWidget(m_selectionModeBottomBar
, positionFor
.selectionModeBottomBar
, 0);
381 m_selectionModeBottomBar
->resetContents(bottomBarContents
);
382 if (bottomBarContents
== SelectionMode::BottomBar::GeneralContents
) {
383 m_selectionModeBottomBar
->slotSelectionChanged(m_view
->selectedItems(), m_view
->url());
387 m_selectionModeTopBar
->setVisible(true, WithAnimation
);
388 m_selectionModeBottomBar
->setVisible(true, WithAnimation
);
389 Q_EMIT
selectionModeChanged(true);
393 bool DolphinViewContainer::isSelectionModeEnabled() const
395 const bool isEnabled
= m_view
->selectionMode();
397 // We can't assert that the bars are invisible only because the selection mode is disabled because the hide animation might still be playing.
398 && (!m_selectionModeBottomBar
|| !m_selectionModeBottomBar
->isEnabled() || !m_selectionModeBottomBar
->isVisible()
399 || m_selectionModeBottomBar
->contents() == SelectionMode::BottomBar::PasteContents
))
400 || (isEnabled
&& m_selectionModeTopBar
401 && m_selectionModeTopBar
->isVisible()
402 // 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.
403 && m_selectionModeBottomBar
404 && (m_selectionModeBottomBar
->isVisible() || m_selectionModeBottomBar
->contents() == SelectionMode::BottomBar::GeneralContents
)));
408 void DolphinViewContainer::slotSplitTabDisabled()
410 if (m_selectionModeBottomBar
) {
411 m_selectionModeBottomBar
->slotSplitTabDisabled();
415 void DolphinViewContainer::showMessage(const QString
&msg
, MessageType type
)
421 m_messageWidget
->setText(msg
);
423 // TODO: wrap at arbitrary character positions once QLabel can do this
424 // https://bugreports.qt.io/browse/QTBUG-1276
425 m_messageWidget
->setWordWrap(true);
429 m_messageWidget
->setMessageType(KMessageWidget::Information
);
432 m_messageWidget
->setMessageType(KMessageWidget::Warning
);
435 m_messageWidget
->setMessageType(KMessageWidget::Error
);
442 m_messageWidget
->setWordWrap(false);
443 const int unwrappedWidth
= m_messageWidget
->sizeHint().width();
444 m_messageWidget
->setWordWrap(unwrappedWidth
> size().width());
446 if (m_messageWidget
->isVisible()) {
447 m_messageWidget
->hide();
449 m_messageWidget
->animatedShow();
452 void DolphinViewContainer::readSettings()
454 // The startup settings should (only) get applied if they have been
455 // modified by the user. Otherwise keep the (possibly) different current
456 // setting of the filterbar.
457 if (GeneralSettings::modifiedStartupSettings()) {
458 setFilterBarVisible(GeneralSettings::filterBar());
461 m_view
->readSettings();
462 m_statusBar
->readSettings();
465 bool DolphinViewContainer::isFilterBarVisible() const
467 return m_filterBar
->isVisible();
470 void DolphinViewContainer::setSearchModeEnabled(bool enabled
)
472 m_searchBox
->setVisible(enabled
);
475 const QUrl
&locationUrl
= m_urlNavigator
->locationUrl();
476 m_searchBox
->fromSearchUrl(locationUrl
);
479 if (enabled
== isSearchModeEnabled()) {
480 if (enabled
&& !m_searchBox
->hasFocus()) {
481 m_searchBox
->setFocus();
482 m_searchBox
->selectAll();
488 m_view
->setViewPropertiesContext(QString());
490 // Restore the URL for the URL navigator. If Dolphin has been
491 // started with a search-URL, the home URL is used as fallback.
492 QUrl url
= m_searchBox
->searchPath();
493 if (url
.isEmpty() || !url
.isValid() || isSearchUrl(url
)) {
494 url
= Dolphin::homeUrl();
496 m_urlNavigatorConnected
->setLocationUrl(url
);
499 m_searchModeEnabled
= enabled
;
501 Q_EMIT
searchModeEnabledChanged(enabled
);
504 bool DolphinViewContainer::isSearchModeEnabled() const
506 return m_searchModeEnabled
;
509 QString
DolphinViewContainer::placesText() const
513 if (isSearchModeEnabled()) {
514 text
= i18n("Search for %1 in %2", m_searchBox
->text(), m_searchBox
->searchPath().fileName());
516 text
= url().adjusted(QUrl::StripTrailingSlash
).fileName();
517 if (text
.isEmpty()) {
520 if (text
.isEmpty()) {
521 text
= url().scheme();
528 void DolphinViewContainer::reload()
531 m_messageWidget
->hide();
534 QString
DolphinViewContainer::captionWindowTitle() const
536 if (GeneralSettings::showFullPathInTitlebar() && !isSearchModeEnabled()) {
537 if (!url().isLocalFile()) {
538 return url().adjusted(QUrl::StripTrailingSlash
).toString();
540 return url().adjusted(QUrl::StripTrailingSlash
).path();
542 return DolphinViewContainer::caption();
546 QString
DolphinViewContainer::caption() const
548 if (isSearchModeEnabled()) {
549 if (currentSearchText().isEmpty()) {
550 return i18n("Search");
552 return i18n("Search for %1", currentSearchText());
556 KFilePlacesModel
*placesModel
= DolphinPlacesModelSingleton::instance().placesModel();
557 const QString pattern
= url().adjusted(QUrl::StripTrailingSlash
).toString(QUrl::FullyEncoded
).append("/?");
558 const auto &matchedPlaces
=
559 placesModel
->match(placesModel
->index(0, 0), KFilePlacesModel::UrlRole
, QRegularExpression::anchoredPattern(pattern
), 1, Qt::MatchRegularExpression
);
561 if (!matchedPlaces
.isEmpty()) {
562 return placesModel
->text(matchedPlaces
.first());
565 if (!url().isLocalFile()) {
566 QUrl adjustedUrl
= url().adjusted(QUrl::StripTrailingSlash
);
568 if (!adjustedUrl
.fileName().isEmpty()) {
569 caption
= adjustedUrl
.fileName();
570 } else if (!adjustedUrl
.path().isEmpty() && adjustedUrl
.path() != "/") {
571 caption
= adjustedUrl
.path();
572 } else if (!adjustedUrl
.host().isEmpty()) {
573 caption
= adjustedUrl
.host();
575 caption
= adjustedUrl
.toString();
580 QString fileName
= url().adjusted(QUrl::StripTrailingSlash
).fileName();
581 if (fileName
.isEmpty()) {
588 void DolphinViewContainer::setUrl(const QUrl
&newUrl
)
590 if (newUrl
!= m_urlNavigator
->locationUrl()) {
591 m_urlNavigator
->setLocationUrl(newUrl
);
595 m_activityResourceInstance
->setUri(newUrl
);
599 void DolphinViewContainer::setFilterBarVisible(bool visible
)
601 Q_ASSERT(m_filterBar
);
603 m_view
->hideToolTip(ToolTipManager::HideBehavior::Instantly
);
605 m_filterBar
->setFocus();
606 m_filterBar
->selectAll();
612 void DolphinViewContainer::delayedStatusBarUpdate()
614 if (m_statusBarTimer
->isActive() && (m_statusBarTimestamp
.elapsed() > 2000)) {
615 // No update of the statusbar has been done during the last 2 seconds,
616 // although an update has been requested. Trigger an immediate update.
617 m_statusBarTimer
->stop();
620 // Invoke updateStatusBar() with a small delay. This assures that
621 // when a lot of delayedStatusBarUpdates() are done in a short time,
622 // no bottleneck is given.
623 m_statusBarTimer
->start();
627 void DolphinViewContainer::updateStatusBar()
629 m_statusBarTimestamp
.start();
630 m_view
->requestStatusBarText();
633 void DolphinViewContainer::updateDirectoryLoadingProgress(int percent
)
635 if (m_statusBar
->progressText().isEmpty()) {
636 m_statusBar
->setProgressText(i18nc("@info:progress", "Loading folder…"));
638 m_statusBar
->setProgress(percent
);
641 void DolphinViewContainer::updateDirectorySortingProgress(int percent
)
643 if (m_statusBar
->progressText().isEmpty()) {
644 m_statusBar
->setProgressText(i18nc("@info:progress", "Sorting…"));
646 m_statusBar
->setProgress(percent
);
649 void DolphinViewContainer::slotDirectoryLoadingStarted()
651 if (isSearchUrl(url())) {
652 // Search KIO-slaves usually don't provide any progress information. Give
653 // a hint to the user that a searching is done:
655 m_statusBar
->setProgressText(i18nc("@info", "Searching…"));
656 m_statusBar
->setProgress(-1);
658 // Trigger an undetermined progress indication. The progress
659 // information in percent will be triggered by the percent() signal
660 // of the directory lister later.
661 m_statusBar
->setProgressText(QString());
662 updateDirectoryLoadingProgress(-1);
666 void DolphinViewContainer::slotDirectoryLoadingCompleted()
668 if (!m_statusBar
->progressText().isEmpty()) {
669 m_statusBar
->setProgressText(QString());
670 m_statusBar
->setProgress(100);
673 if (isSearchUrl(url()) && m_view
->itemsCount() == 0) {
674 // The dir lister has been completed on a Baloo-URI and no items have been found. Instead
675 // of showing the default status bar information ("0 items") a more helpful information is given:
676 m_statusBar
->setText(i18nc("@info:status", "No items found."));
682 void DolphinViewContainer::slotDirectoryLoadingCanceled()
684 if (!m_statusBar
->progressText().isEmpty()) {
685 m_statusBar
->setProgressText(QString());
686 m_statusBar
->setProgress(100);
689 m_statusBar
->setText(QString());
692 void DolphinViewContainer::slotUrlIsFileError(const QUrl
&url
)
694 const KFileItem
item(url
);
696 // Find out if the file can be opened in the view (for example, this is the
697 // case if the file is an archive). The mime type must be known for that.
698 item
.determineMimeType();
699 const QUrl
&folderUrl
= DolphinView::openItemAsFolderUrl(item
, true);
700 if (!folderUrl
.isEmpty()) {
703 slotItemActivated(item
);
707 void DolphinViewContainer::slotItemActivated(const KFileItem
&item
)
709 // It is possible to activate items on inactive views by
710 // drag & drop operations. Assure that activating an item always
711 // results in an active view.
712 m_view
->setActive(true);
714 const QUrl
&url
= DolphinView::openItemAsFolderUrl(item
, GeneralSettings::browseThroughArchives());
715 if (!url
.isEmpty()) {
716 const auto modifiers
= QGuiApplication::keyboardModifiers();
717 // keep in sync with KUrlNavigator::slotNavigatorButtonClicked
718 if (modifiers
& Qt::ControlModifier
&& modifiers
& Qt::ShiftModifier
) {
719 Q_EMIT
activeTabRequested(url
);
720 } else if (modifiers
& Qt::ControlModifier
) {
721 Q_EMIT
tabRequested(url
);
722 } else if (modifiers
& Qt::ShiftModifier
) {
723 Dolphin::openNewWindow({KFilePlacesModel::convertedUrl(url
)}, this);
730 KIO::OpenUrlJob
*job
= new KIO::OpenUrlJob(item
.targetUrl(), item
.mimetype());
731 // Auto*Warning*Handling, errors are put in a KMessageWidget by us in slotOpenUrlFinished.
732 job
->setUiDelegate(KIO::createDefaultJobUiDelegate(KJobUiDelegate::AutoWarningHandlingEnabled
, this));
733 job
->setShowOpenOrExecuteDialog(true);
734 connect(job
, &KIO::OpenUrlJob::finished
, this, &DolphinViewContainer::slotOpenUrlFinished
);
738 void DolphinViewContainer::slotfileMiddleClickActivated(const KFileItem
&item
)
740 KService::List services
= KApplicationTrader::queryByMimeType(item
.mimetype());
742 int indexOfAppToOpenFileWith
= 1;
744 // executable scripts
745 auto mimeType
= item
.currentMimeType();
746 if (item
.isLocalFile() && mimeType
.inherits(QStringLiteral("application/x-executable")) && mimeType
.inherits(QStringLiteral("text/plain"))
747 && QFileInfo(item
.localPath()).isExecutable()) {
748 KConfigGroup
cfgGroup(KSharedConfig::openConfig(QStringLiteral("kiorc")), "Executable scripts");
749 const QString value
= cfgGroup
.readEntry("behaviourOnLaunch", "alwaysAsk");
751 // in case KIO::WidgetsOpenOrExecuteFileHandler::promptUserOpenOrExecute would not open the file
752 if (value
!= QLatin1String("open")) {
753 indexOfAppToOpenFileWith
= 0;
757 if (services
.length() >= indexOfAppToOpenFileWith
+ 1) {
758 auto service
= services
.at(indexOfAppToOpenFileWith
);
760 KIO::ApplicationLauncherJob
*job
= new KIO::ApplicationLauncherJob(service
, this);
761 job
->setUrls({item
.url()});
763 job
->setUiDelegate(KIO::createDefaultJobUiDelegate(KJobUiDelegate::AutoHandlingEnabled
, this));
764 connect(job
, &KIO::OpenUrlJob::finished
, this, &DolphinViewContainer::slotOpenUrlFinished
);
769 void DolphinViewContainer::slotItemsActivated(const KFileItemList
&items
)
771 Q_ASSERT(items
.count() >= 2);
773 KFileItemActions
fileItemActions(this);
774 fileItemActions
.runPreferredApplications(items
);
777 void DolphinViewContainer::showItemInfo(const KFileItem
&item
)
780 m_statusBar
->resetToDefaultText();
782 m_statusBar
->setText(item
.getStatusBarInfo());
786 void DolphinViewContainer::closeFilterBar()
788 m_filterBar
->closeFilterBar();
790 Q_EMIT
showFilterBarChanged(false);
793 void DolphinViewContainer::clearFilterBar()
795 m_filterBar
->clearIfUnlocked();
798 void DolphinViewContainer::setNameFilter(const QString
&nameFilter
)
800 m_view
->hideToolTip(ToolTipManager::HideBehavior::Instantly
);
801 m_view
->setNameFilter(nameFilter
);
802 delayedStatusBarUpdate();
805 void DolphinViewContainer::activate()
810 void DolphinViewContainer::slotUrlNavigatorLocationAboutToBeChanged(const QUrl
&)
815 void DolphinViewContainer::slotUrlNavigatorLocationChanged(const QUrl
&url
)
817 if (m_urlNavigatorConnected
) {
818 m_urlNavigatorConnected
->slotReturnPressed();
821 if (KProtocolManager::supportsListing(url
)) {
822 const bool searchBoxInitialized
= isSearchModeEnabled() && m_searchBox
->text().isEmpty();
823 setSearchModeEnabled(isSearchUrl(url
) || searchBoxInitialized
);
826 tryRestoreViewState();
828 if (m_autoGrabFocus
&& isActive() && !isSearchModeEnabled()) {
829 // When an URL has been entered, the view should get the focus.
830 // The focus must be requested asynchronously, as changing the URL might create
831 // a new view widget.
832 QTimer::singleShot(0, this, &DolphinViewContainer::requestFocus
);
834 } else if (KProtocolManager::isSourceProtocol(url
)) {
835 if (url
.scheme().startsWith(QLatin1String("http"))) {
836 showMessage(i18nc("@info:status", // krazy:exclude=qmethods
837 "Dolphin does not support web pages, the web browser has been launched"),
840 showMessage(i18nc("@info:status", "Protocol not supported by Dolphin, default application has been launched"), Information
);
843 QDesktopServices::openUrl(url
);
844 redirect(QUrl(), m_urlNavigator
->locationUrl(1));
846 if (!url
.scheme().isEmpty()) {
847 showMessage(i18nc("@info:status", "Invalid protocol '%1'", url
.scheme()), Error
);
849 showMessage(i18nc("@info:status", "Invalid protocol"), Error
);
851 m_urlNavigator
->goBack();
855 void DolphinViewContainer::slotUrlSelectionRequested(const QUrl
&url
)
857 m_view
->markUrlsAsSelected({url
});
858 m_view
->markUrlAsCurrent(url
); // makes the item scroll into view
861 void DolphinViewContainer::disableUrlNavigatorSelectionRequests()
863 disconnect(m_urlNavigator
.get(), &KUrlNavigator::urlSelectionRequested
, this, &DolphinViewContainer::slotUrlSelectionRequested
);
866 void DolphinViewContainer::enableUrlNavigatorSelectionRequests()
868 connect(m_urlNavigator
.get(), &KUrlNavigator::urlSelectionRequested
, this, &DolphinViewContainer::slotUrlSelectionRequested
);
871 void DolphinViewContainer::redirect(const QUrl
&oldUrl
, const QUrl
&newUrl
)
874 const bool block
= m_urlNavigator
->signalsBlocked();
875 m_urlNavigator
->blockSignals(true);
877 // Assure that the location state is reset for redirection URLs. This
878 // allows to skip redirection URLs when going back or forward in the
880 m_urlNavigator
->saveLocationState(QByteArray());
881 m_urlNavigator
->setLocationUrl(newUrl
);
882 setSearchModeEnabled(isSearchUrl(newUrl
));
884 m_urlNavigator
->blockSignals(block
);
887 void DolphinViewContainer::requestFocus()
892 void DolphinViewContainer::startSearching()
894 Q_CHECK_PTR(m_urlNavigatorConnected
);
895 const QUrl url
= m_searchBox
->urlForSearching();
896 if (url
.isValid() && !url
.isEmpty()) {
897 m_view
->setViewPropertiesContext(QStringLiteral("search"));
898 m_urlNavigatorConnected
->setLocationUrl(url
);
902 void DolphinViewContainer::openSearchBox()
904 setSearchModeEnabled(true);
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(".."))));
996 } while (!dir
.exists() && !dir
.isRoot());
998 return dir
.exists() ? dir
.path() : QString
{};
1001 #include "moc_dolphinviewcontainer.cpp"