2 * SPDX-FileCopyrightText: 2007 Peter Penz <peter.penz19@gmail.com>
4 * SPDX-License-Identifier: GPL-2.0-or-later
7 #include "dolphinviewcontainer.h"
10 #include "admin/workerintegration.h"
11 #include "dolphin_compactmodesettings.h"
12 #include "dolphin_contentdisplaysettings.h"
13 #include "dolphin_detailsmodesettings.h"
14 #include "dolphin_generalsettings.h"
15 #include "dolphin_iconsmodesettings.h"
16 #include "dolphindebug.h"
17 #include "dolphinplacesmodelsingleton.h"
18 #include "filterbar/filterbar.h"
20 #include "search/dolphinsearchbox.h"
21 #include "selectionmode/topbar.h"
22 #include "statusbar/dolphinstatusbar.h"
24 #include <KActionCollection>
25 #include <KApplicationTrader>
26 #include <KFileItemActions>
27 #include <KFilePlacesModel>
28 #include <KIO/JobUiDelegateFactory>
29 #include <KIO/OpenUrlJob>
30 #include <KLocalizedString>
31 #include <KMessageWidget>
32 #include <KProtocolManager>
34 #include <kio_version.h>
36 #include <QApplication>
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
{
49 int messageWidget
= 2;
50 int selectionModeTopBar
= 3;
52 int selectionModeBottomBar
= 5;
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)
66 , m_authorizeToEnterFolderAction
{nullptr}
67 , m_messageWidget(nullptr)
68 , m_selectionModeTopBar
{nullptr}
70 , m_filterBar(nullptr)
71 , m_selectionModeBottomBar
{nullptr}
72 , m_statusBar(nullptr)
73 , m_statusBarTimer(nullptr)
74 , m_statusBarTimestamp()
75 , m_autoGrabFocus(true)
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);
84 m_searchBox
->setVisible(false, WithoutAnimation
);
85 connect(m_searchBox
, &DolphinSearchBox::activated
, this, &DolphinViewContainer::activate
);
86 connect(m_searchBox
, &DolphinSearchBox::openRequest
, this, &DolphinViewContainer::openSearchBox
);
87 connect(m_searchBox
, &DolphinSearchBox::closeRequest
, this, &DolphinViewContainer::closeSearchBox
);
88 connect(m_searchBox
, &DolphinSearchBox::searchRequest
, this, &DolphinViewContainer::startSearching
);
89 connect(m_searchBox
, &DolphinSearchBox::focusViewRequest
, this, &DolphinViewContainer::requestFocus
);
90 m_searchBox
->setWhatsThis(xi18nc("@info:whatsthis findbar",
91 "<para>This helps you find files and folders. Enter a <emphasis>"
92 "search term</emphasis> and specify search settings with the "
93 "buttons at the bottom:<list><item>Filename/Content: "
94 "Does the item you are looking for contain the search terms "
95 "within its filename or its contents?<nl/>The contents of images, "
96 "audio files and videos will not be searched.</item><item>"
97 "From Here/Everywhere: Do you want to search in this "
98 "folder and its sub-folders or everywhere?</item><item>"
99 "More Options: Click this to search by media type, access "
100 "time or rating.</item><item>More Search Tools: Install other "
101 "means to find an item.</item></list></para>"));
103 m_messageWidget
= new KMessageWidget(this);
104 m_messageWidget
->setCloseButtonVisible(true);
105 m_messageWidget
->setPosition(KMessageWidget::Header
);
106 m_messageWidget
->hide();
108 #if !defined(Q_OS_WIN) && !defined(Q_OS_HAIKU)
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."), KMessageWidget::Warning
);
115 // Initialize filter bar
116 m_filterBar
= new FilterBar(this);
117 m_filterBar
->setVisible(GeneralSettings::filterBar(), WithoutAnimation
);
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::selectionChanged
, this, &DolphinViewContainer::delayedStatusBarUpdate
);
144 connect(m_view
, &DolphinView::errorMessage
, this, &DolphinViewContainer::slotErrorMessageFromView
);
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_view
, &DolphinView::directoryLoadingProgress
, m_statusBar
, [this](int percent
) {
162 m_statusBar
->showProgress(i18nc("@info:progress", "Loading folder…"), percent
);
164 connect(m_view
, &DolphinView::directorySortingProgress
, m_statusBar
, [this](int percent
) {
165 m_statusBar
->showProgress(i18nc("@info:progress", "Sorting…"), percent
);
167 connect(m_statusBar
, &DolphinStatusBar::stopPressed
, this, &DolphinViewContainer::stopDirectoryLoading
);
168 connect(m_statusBar
, &DolphinStatusBar::zoomLevelChanged
, this, &DolphinViewContainer::slotStatusBarZoomLevelChanged
);
169 connect(m_statusBar
, &DolphinStatusBar::showMessage
, this, [this](const QString
&message
, KMessageWidget::MessageType messageType
) {
170 showMessage(message
, messageType
);
173 m_statusBarTimer
= new QTimer(this);
174 m_statusBarTimer
->setSingleShot(true);
175 m_statusBarTimer
->setInterval(300);
176 connect(m_statusBarTimer
, &QTimer::timeout
, this, &DolphinViewContainer::updateStatusBar
);
178 KIO::FileUndoManager
*undoManager
= KIO::FileUndoManager::self();
179 connect(undoManager
, &KIO::FileUndoManager::jobRecordingFinished
, this, &DolphinViewContainer::delayedStatusBarUpdate
);
181 m_topLayout
->addWidget(m_searchBox
, positionFor
.searchBox
, 0);
182 m_topLayout
->addWidget(m_messageWidget
, positionFor
.messageWidget
, 0);
183 m_topLayout
->addWidget(m_view
, positionFor
.view
, 0);
184 m_topLayout
->addWidget(m_filterBar
, positionFor
.filterBar
, 0);
185 m_topLayout
->addWidget(m_statusBar
, positionFor
.statusBar
, 0);
187 setSearchModeEnabled(isSearchUrl(url
));
189 // Update view as the ContentDisplaySettings change
190 // this happens here and not in DolphinView as DolphinviewContainer and DolphinView are not in the same build target ATM
191 connect(ContentDisplaySettings::self(), &KCoreConfigSkeleton::configChanged
, m_view
, &DolphinView::reload
);
193 KFilePlacesModel
*placesModel
= DolphinPlacesModelSingleton::instance().placesModel();
194 connect(placesModel
, &KFilePlacesModel::dataChanged
, this, &DolphinViewContainer::slotPlacesModelChanged
);
195 connect(placesModel
, &KFilePlacesModel::rowsInserted
, this, &DolphinViewContainer::slotPlacesModelChanged
);
196 connect(placesModel
, &KFilePlacesModel::rowsRemoved
, this, &DolphinViewContainer::slotPlacesModelChanged
);
198 connect(this, &DolphinViewContainer::searchModeEnabledChanged
, this, &DolphinViewContainer::captionChanged
);
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
);
224 bool DolphinViewContainer::isActive() const
226 return m_view
->isActive();
229 void DolphinViewContainer::setAutoGrabFocus(bool grab
)
231 m_autoGrabFocus
= grab
;
234 bool DolphinViewContainer::autoGrabFocus() const
236 return m_autoGrabFocus
;
239 QString
DolphinViewContainer::currentSearchText() const
241 return m_searchBox
->text();
244 const DolphinStatusBar
*DolphinViewContainer::statusBar() const
249 DolphinStatusBar
*DolphinViewContainer::statusBar()
254 const DolphinUrlNavigator
*DolphinViewContainer::urlNavigator() const
256 return m_urlNavigatorConnected
;
259 DolphinUrlNavigator
*DolphinViewContainer::urlNavigator()
261 return m_urlNavigatorConnected
;
264 const DolphinUrlNavigator
*DolphinViewContainer::urlNavigatorInternalWithHistory() const
266 return m_urlNavigator
.get();
269 DolphinUrlNavigator
*DolphinViewContainer::urlNavigatorInternalWithHistory()
271 return m_urlNavigator
.get();
274 const DolphinView
*DolphinViewContainer::view() const
279 DolphinView
*DolphinViewContainer::view()
284 void DolphinViewContainer::connectUrlNavigator(DolphinUrlNavigator
*urlNavigator
)
286 Q_CHECK_PTR(urlNavigator
);
287 Q_ASSERT(!m_urlNavigatorConnected
);
288 Q_ASSERT(m_urlNavigator
.get() != urlNavigator
);
291 urlNavigator
->setLocationUrl(m_view
->url());
292 urlNavigator
->setShowHiddenFolders(m_view
->hiddenFilesShown());
293 urlNavigator
->setSortHiddenFoldersLast(m_view
->sortHiddenLast());
294 if (m_urlNavigatorVisualState
) {
295 urlNavigator
->setVisualState(*m_urlNavigatorVisualState
.get());
296 m_urlNavigatorVisualState
.reset();
298 urlNavigator
->setActive(isActive());
300 // Url changes are still done via m_urlNavigator.
301 connect(urlNavigator
, &DolphinUrlNavigator::urlChanged
, m_urlNavigator
.get(), &DolphinUrlNavigator::setLocationUrl
);
302 connect(urlNavigator
, &DolphinUrlNavigator::urlsDropped
, this, [=](const QUrl
&destination
, QDropEvent
*event
) {
303 m_view
->dropUrls(destination
, event
, urlNavigator
->dropWidget());
305 // Aside from these, only visual things need to be connected.
306 connect(m_view
, &DolphinView::urlChanged
, urlNavigator
, &DolphinUrlNavigator::setLocationUrl
);
307 connect(urlNavigator
, &DolphinUrlNavigator::activated
, this, &DolphinViewContainer::activate
);
309 urlNavigator
->setReadOnlyBadgeVisible(rootItem().isLocalFile() && !rootItem().isWritable());
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);
344 if (!QApplication::focusWidget() || m_selectionModeTopBar
->isAncestorOf(QApplication::focusWidget())
345 || m_selectionModeBottomBar
->isAncestorOf(QApplication::focusWidget())) {
351 if (!m_selectionModeTopBar
) {
352 // Changing the location will disable selection mode.
353 connect(m_urlNavigator
.get(), &DolphinUrlNavigator::urlChanged
, this, [this]() {
354 setSelectionModeEnabled(false);
357 m_selectionModeTopBar
= new SelectionMode::TopBar(this); // will be created hidden
358 connect(m_selectionModeTopBar
, &SelectionMode::TopBar::selectionModeLeavingRequested
, this, [this]() {
359 setSelectionModeEnabled(false);
361 m_topLayout
->addWidget(m_selectionModeTopBar
, positionFor
.selectionModeTopBar
, 0);
364 if (!m_selectionModeBottomBar
) {
365 m_selectionModeBottomBar
= new SelectionMode::BottomBar(actionCollection
, this);
366 connect(m_view
, &DolphinView::selectionChanged
, this, [this](const KFileItemList
&selection
) {
367 m_selectionModeBottomBar
->slotSelectionChanged(selection
, m_view
->url());
369 connect(m_selectionModeBottomBar
, &SelectionMode::BottomBar::error
, this, &DolphinViewContainer::showErrorMessage
);
370 connect(m_selectionModeBottomBar
, &SelectionMode::BottomBar::selectionModeLeavingRequested
, this, [this]() {
371 setSelectionModeEnabled(false);
373 m_topLayout
->addWidget(m_selectionModeBottomBar
, positionFor
.selectionModeBottomBar
, 0);
375 m_selectionModeBottomBar
->resetContents(bottomBarContents
);
376 if (bottomBarContents
== SelectionMode::BottomBar::GeneralContents
) {
377 m_selectionModeBottomBar
->slotSelectionChanged(m_view
->selectedItems(), m_view
->url());
381 m_selectionModeTopBar
->setVisible(true, WithAnimation
);
382 m_selectionModeBottomBar
->setVisible(true, WithAnimation
);
383 Q_EMIT
selectionModeChanged(true);
387 bool DolphinViewContainer::isSelectionModeEnabled() const
389 const bool isEnabled
= m_view
->selectionMode();
391 // We can't assert that the bars are invisible only because the selection mode is disabled because the hide animation might still be playing.
392 && (!m_selectionModeBottomBar
|| !m_selectionModeBottomBar
->isEnabled() || !m_selectionModeBottomBar
->isVisible()
393 || m_selectionModeBottomBar
->contents() == SelectionMode::BottomBar::PasteContents
))
394 || (isEnabled
&& m_selectionModeTopBar
395 && m_selectionModeTopBar
->isVisible()
396 // 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.
397 && m_selectionModeBottomBar
398 && (m_selectionModeBottomBar
->isVisible() || m_selectionModeBottomBar
->contents() == SelectionMode::BottomBar::GeneralContents
)));
402 void DolphinViewContainer::slotSplitTabDisabled()
404 if (m_selectionModeBottomBar
) {
405 m_selectionModeBottomBar
->slotSplitTabDisabled();
409 void DolphinViewContainer::showMessage(const QString
&message
, KMessageWidget::MessageType messageType
, std::initializer_list
<QAction
*> buttonActions
)
411 if (message
.isEmpty()) {
415 m_messageWidget
->setText(message
);
417 // TODO: wrap at arbitrary character positions once QLabel can do this
418 // https://bugreports.qt.io/browse/QTBUG-1276
419 m_messageWidget
->setWordWrap(true);
420 m_messageWidget
->setMessageType(messageType
);
422 const QList
<QAction
*> previousMessageWidgetActions
= m_messageWidget
->actions();
423 for (auto action
: previousMessageWidgetActions
) {
424 m_messageWidget
->removeAction(action
);
426 for (QAction
*action
: buttonActions
) {
427 m_messageWidget
->addAction(action
);
430 m_messageWidget
->setWordWrap(false);
431 const int unwrappedWidth
= m_messageWidget
->sizeHint().width();
432 m_messageWidget
->setWordWrap(unwrappedWidth
> size().width());
434 if (m_messageWidget
->isVisible()) {
435 m_messageWidget
->hide();
437 m_messageWidget
->animatedShow();
440 void DolphinViewContainer::readSettings()
442 // The startup settings should (only) get applied if they have been
443 // modified by the user. Otherwise keep the (possibly) different current
444 // setting of the filterbar.
445 if (GeneralSettings::modifiedStartupSettings()) {
446 setFilterBarVisible(GeneralSettings::filterBar());
449 m_view
->readSettings();
450 m_statusBar
->readSettings();
453 bool DolphinViewContainer::isFilterBarVisible() const
455 return m_filterBar
->isEnabled(); // Gets disabled in AnimatedHeightWidget while animating towards a hidden state.
458 void DolphinViewContainer::setSearchModeEnabled(bool enabled
)
460 m_searchBox
->setVisible(enabled
, WithAnimation
);
463 const QUrl
&locationUrl
= m_urlNavigator
->locationUrl();
464 m_searchBox
->fromSearchUrl(locationUrl
);
467 if (enabled
== isSearchModeEnabled()) {
468 if (enabled
&& !m_searchBox
->hasFocus()) {
469 m_searchBox
->setFocus();
470 m_searchBox
->selectAll();
476 m_view
->setViewPropertiesContext(QString());
478 // Restore the URL for the URL navigator. If Dolphin has been
479 // started with a search-URL, the home URL is used as fallback.
480 QUrl url
= m_searchBox
->searchPath();
481 if (url
.isEmpty() || !url
.isValid() || isSearchUrl(url
)) {
482 url
= Dolphin::homeUrl();
484 m_urlNavigatorConnected
->setLocationUrl(url
);
487 m_searchModeEnabled
= enabled
;
489 Q_EMIT
searchModeEnabledChanged(enabled
);
492 bool DolphinViewContainer::isSearchModeEnabled() const
494 return m_searchModeEnabled
;
497 QString
DolphinViewContainer::placesText() const
501 if (isSearchModeEnabled()) {
502 text
= i18n("Search for %1 in %2", m_searchBox
->text(), m_searchBox
->searchPath().fileName());
504 text
= url().adjusted(QUrl::StripTrailingSlash
).fileName();
505 if (text
.isEmpty()) {
508 if (text
.isEmpty()) {
509 text
= url().scheme();
516 void DolphinViewContainer::reload()
519 m_messageWidget
->hide();
522 QString
DolphinViewContainer::captionWindowTitle() const
524 if (GeneralSettings::showFullPathInTitlebar() && !isSearchModeEnabled()) {
525 if (!url().isLocalFile()) {
526 return url().adjusted(QUrl::StripTrailingSlash
).toString();
528 return url().adjusted(QUrl::StripTrailingSlash
).path();
530 return DolphinViewContainer::caption();
534 QString
DolphinViewContainer::caption() const
536 if (isSearchModeEnabled()) {
537 if (currentSearchText().isEmpty()) {
538 return i18n("Search");
540 return i18n("Search for %1", currentSearchText());
544 KFilePlacesModel
*placesModel
= DolphinPlacesModelSingleton::instance().placesModel();
545 const QString pattern
= url().adjusted(QUrl::StripTrailingSlash
).toString(QUrl::FullyEncoded
).append("/?");
546 const auto &matchedPlaces
=
547 placesModel
->match(placesModel
->index(0, 0), KFilePlacesModel::UrlRole
, QRegularExpression::anchoredPattern(pattern
), 1, Qt::MatchRegularExpression
);
549 if (!matchedPlaces
.isEmpty()) {
550 return placesModel
->text(matchedPlaces
.first());
553 if (!url().isLocalFile()) {
554 QUrl adjustedUrl
= url().adjusted(QUrl::StripTrailingSlash
);
556 if (!adjustedUrl
.fileName().isEmpty()) {
557 caption
= adjustedUrl
.fileName();
558 } else if (!adjustedUrl
.path().isEmpty() && adjustedUrl
.path() != "/") {
559 caption
= adjustedUrl
.path();
560 } else if (!adjustedUrl
.host().isEmpty()) {
561 caption
= adjustedUrl
.host();
563 caption
= adjustedUrl
.toString();
568 QString fileName
= url().adjusted(QUrl::StripTrailingSlash
).fileName();
569 if (fileName
.isEmpty()) {
576 void DolphinViewContainer::setUrl(const QUrl
&newUrl
)
578 if (newUrl
!= m_urlNavigator
->locationUrl()) {
579 m_urlNavigator
->setLocationUrl(newUrl
);
583 void DolphinViewContainer::setFilterBarVisible(bool visible
)
585 Q_ASSERT(m_filterBar
);
587 m_view
->hideToolTip(ToolTipManager::HideBehavior::Instantly
);
588 m_filterBar
->setVisible(true, WithAnimation
);
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::slotDirectoryLoadingStarted()
619 if (isSearchUrl(url())) {
620 // Search KIO-slaves usually don't provide any progress information. Give
621 // a hint to the user that a searching is done:
623 m_statusBar
->showProgress(i18nc("@info", "Searching…"), -1);
625 // Trigger an undetermined progress indication. The progress
626 // information in percent will be triggered by the percent() signal
627 // of the directory lister later.
628 m_statusBar
->showProgress(QString(), -1);
631 if (m_urlNavigatorConnected
) {
632 m_urlNavigatorConnected
->setReadOnlyBadgeVisible(false);
636 void DolphinViewContainer::slotDirectoryLoadingCompleted()
638 m_statusBar
->showProgress(QString(), 100);
640 if (isSearchUrl(url()) && m_view
->itemsCount() == 0) {
641 // The dir lister has been completed on a Baloo-URI and no items have been found. Instead
642 // of showing the default status bar information ("0 items") a more helpful information is given:
643 m_statusBar
->setText(i18nc("@info:status", "No items found."));
648 if (m_urlNavigatorConnected
) {
649 m_urlNavigatorConnected
->setReadOnlyBadgeVisible(rootItem().isLocalFile() && !rootItem().isWritable());
652 // Update admin bar visibility
653 if (m_view
->url().scheme() == QStringLiteral("admin")) {
655 m_adminBar
= new Admin::Bar(this);
656 m_topLayout
->addWidget(m_adminBar
, positionFor
.adminBar
, 0);
658 m_adminBar
->setVisible(true, WithAnimation
);
659 } else if (m_adminBar
) {
660 m_adminBar
->setVisible(false, WithAnimation
);
664 void DolphinViewContainer::slotDirectoryLoadingCanceled()
666 m_statusBar
->showProgress(QString(), 100);
667 m_statusBar
->setText(QString());
670 void DolphinViewContainer::slotUrlIsFileError(const QUrl
&url
)
672 const KFileItem
item(url
);
674 // Find out if the file can be opened in the view (for example, this is the
675 // case if the file is an archive). The mime type must be known for that.
676 item
.determineMimeType();
677 const QUrl
&folderUrl
= DolphinView::openItemAsFolderUrl(item
, true);
678 if (!folderUrl
.isEmpty()) {
681 slotItemActivated(item
);
685 void DolphinViewContainer::slotItemActivated(const KFileItem
&item
)
687 // It is possible to activate items on inactive views by
688 // drag & drop operations. Assure that activating an item always
689 // results in an active view.
690 m_view
->setActive(true);
692 const QUrl
&url
= DolphinView::openItemAsFolderUrl(item
, GeneralSettings::browseThroughArchives());
693 if (!url
.isEmpty()) {
694 const auto modifiers
= QGuiApplication::keyboardModifiers();
695 // keep in sync with KUrlNavigator::slotNavigatorButtonClicked
696 if (modifiers
& Qt::ControlModifier
&& modifiers
& Qt::ShiftModifier
) {
697 Q_EMIT
activeTabRequested(url
);
698 } else if (modifiers
& Qt::ControlModifier
) {
699 Q_EMIT
tabRequested(url
);
700 } else if (modifiers
& Qt::ShiftModifier
) {
701 Dolphin::openNewWindow({KFilePlacesModel::convertedUrl(url
)}, this);
708 KIO::OpenUrlJob
*job
= new KIO::OpenUrlJob(item
.targetUrl(), item
.mimetype());
709 // Auto*Warning*Handling, errors are put in a KMessageWidget by us in slotOpenUrlFinished.
710 job
->setUiDelegate(KIO::createDefaultJobUiDelegate(KJobUiDelegate::AutoWarningHandlingEnabled
, this));
711 job
->setShowOpenOrExecuteDialog(true);
712 connect(job
, &KIO::OpenUrlJob::finished
, this, &DolphinViewContainer::slotOpenUrlFinished
);
716 void DolphinViewContainer::slotfileMiddleClickActivated(const KFileItem
&item
)
718 KService::List services
= KApplicationTrader::queryByMimeType(item
.mimetype());
720 int indexOfAppToOpenFileWith
= 1;
722 // executable scripts
723 auto mimeType
= item
.currentMimeType();
724 if (item
.isLocalFile() && mimeType
.inherits(QStringLiteral("application/x-executable")) && mimeType
.inherits(QStringLiteral("text/plain"))
725 && QFileInfo(item
.localPath()).isExecutable()) {
726 KConfigGroup
cfgGroup(KSharedConfig::openConfig(QStringLiteral("kiorc")), QStringLiteral("Executable scripts"));
727 const QString value
= cfgGroup
.readEntry("behaviourOnLaunch", "alwaysAsk");
729 // in case KIO::WidgetsOpenOrExecuteFileHandler::promptUserOpenOrExecute would not open the file
730 if (value
!= QLatin1String("open")) {
731 indexOfAppToOpenFileWith
= 0;
735 if (services
.length() >= indexOfAppToOpenFileWith
+ 1) {
736 auto service
= services
.at(indexOfAppToOpenFileWith
);
738 KIO::ApplicationLauncherJob
*job
= new KIO::ApplicationLauncherJob(service
, this);
739 job
->setUrls({item
.url()});
741 job
->setUiDelegate(KIO::createDefaultJobUiDelegate(KJobUiDelegate::AutoHandlingEnabled
, this));
742 connect(job
, &KIO::OpenUrlJob::finished
, this, &DolphinViewContainer::slotOpenUrlFinished
);
747 void DolphinViewContainer::slotItemsActivated(const KFileItemList
&items
)
749 Q_ASSERT(items
.count() >= 2);
751 KFileItemActions
fileItemActions(this);
752 fileItemActions
.runPreferredApplications(items
);
755 void DolphinViewContainer::showItemInfo(const KFileItem
&item
)
758 m_statusBar
->resetToDefaultText();
760 m_statusBar
->setText(item
.getStatusBarInfo());
764 void DolphinViewContainer::closeFilterBar()
766 m_filterBar
->closeFilterBar();
768 Q_EMIT
showFilterBarChanged(false);
771 void DolphinViewContainer::clearFilterBar()
773 m_filterBar
->clearIfUnlocked();
776 void DolphinViewContainer::setNameFilter(const QString
&nameFilter
)
778 m_view
->hideToolTip(ToolTipManager::HideBehavior::Instantly
);
779 m_view
->setNameFilter(nameFilter
);
780 delayedStatusBarUpdate();
783 void DolphinViewContainer::activate()
788 void DolphinViewContainer::slotUrlNavigatorLocationAboutToBeChanged(const QUrl
&)
793 void DolphinViewContainer::slotUrlNavigatorLocationChanged(const QUrl
&url
)
795 if (m_urlNavigatorConnected
) {
796 m_urlNavigatorConnected
->slotReturnPressed();
799 if (KProtocolManager::supportsListing(url
)) {
800 const bool searchBoxInitialized
= isSearchModeEnabled() && m_searchBox
->text().isEmpty();
801 setSearchModeEnabled(isSearchUrl(url
) || searchBoxInitialized
);
804 tryRestoreViewState();
806 if (m_autoGrabFocus
&& isActive() && !isSearchModeEnabled()) {
807 // When an URL has been entered, the view should get the focus.
808 // The focus must be requested asynchronously, as changing the URL might create
809 // a new view widget.
810 QTimer::singleShot(0, this, &DolphinViewContainer::requestFocus
);
812 } else if (KProtocolManager::isSourceProtocol(url
)) {
813 if (url
.scheme().startsWith(QLatin1String("http"))) {
814 showMessage(i18nc("@info:status", // krazy:exclude=qmethods
815 "Dolphin does not support web pages, the web browser has been launched"),
816 KMessageWidget::Information
);
818 showMessage(i18nc("@info:status", "Protocol not supported by Dolphin, default application has been launched"), KMessageWidget::Information
);
821 QDesktopServices::openUrl(url
);
822 redirect(QUrl(), m_urlNavigator
->locationUrl(1));
824 if (!url
.scheme().isEmpty()) {
825 showMessage(i18nc("@info:status", "Invalid protocol '%1'", url
.scheme()), KMessageWidget::Error
);
827 showMessage(i18nc("@info:status", "Invalid protocol"), KMessageWidget::Error
);
829 m_urlNavigator
->goBack();
833 void DolphinViewContainer::slotUrlSelectionRequested(const QUrl
&url
)
835 // We do not want to select any item here because there is no reason to assume that the user wants to edit the folder we are emerging from. BUG: 424723
837 m_view
->markUrlAsCurrent(url
); // makes the item scroll into view
840 void DolphinViewContainer::disableUrlNavigatorSelectionRequests()
842 disconnect(m_urlNavigator
.get(), &KUrlNavigator::urlSelectionRequested
, this, &DolphinViewContainer::slotUrlSelectionRequested
);
845 void DolphinViewContainer::enableUrlNavigatorSelectionRequests()
847 connect(m_urlNavigator
.get(), &KUrlNavigator::urlSelectionRequested
, this, &DolphinViewContainer::slotUrlSelectionRequested
);
850 void DolphinViewContainer::redirect(const QUrl
&oldUrl
, const QUrl
&newUrl
)
853 const bool block
= m_urlNavigator
->signalsBlocked();
854 m_urlNavigator
->blockSignals(true);
856 // Assure that the location state is reset for redirection URLs. This
857 // allows to skip redirection URLs when going back or forward in the
859 m_urlNavigator
->saveLocationState(QByteArray());
860 m_urlNavigator
->setLocationUrl(newUrl
);
861 setSearchModeEnabled(isSearchUrl(newUrl
));
863 m_urlNavigator
->blockSignals(block
);
866 void DolphinViewContainer::requestFocus()
871 void DolphinViewContainer::startSearching()
873 Q_CHECK_PTR(m_urlNavigatorConnected
);
874 const QUrl url
= m_searchBox
->urlForSearching();
875 if (url
.isValid() && !url
.isEmpty()) {
876 m_view
->setViewPropertiesContext(QStringLiteral("search"));
877 m_urlNavigatorConnected
->setLocationUrl(url
);
881 void DolphinViewContainer::openSearchBox()
883 setSearchModeEnabled(true);
886 void DolphinViewContainer::closeSearchBox()
888 setSearchModeEnabled(false);
891 void DolphinViewContainer::stopDirectoryLoading()
893 m_view
->stopLoading();
894 m_statusBar
->showProgress(QString(), 100);
897 void DolphinViewContainer::slotStatusBarZoomLevelChanged(int zoomLevel
)
899 m_view
->setZoomLevel(zoomLevel
);
902 void DolphinViewContainer::slotErrorMessageFromView(const QString
&message
, const int kioErrorCode
)
904 if (kioErrorCode
== KIO::ERR_CANNOT_ENTER_DIRECTORY
&& m_view
->url().scheme() == QStringLiteral("file")
905 && KProtocolInfo::isKnownProtocol(QStringLiteral("admin")) && !rootItem().isReadable()) {
906 // Explain to users that they need authentication to see the folder contents.
907 if (!m_authorizeToEnterFolderAction
) { // This code is similar to parts of Admin::Bar::hideTheNextTimeAuthorizationExpires().
908 // We should not simply use the actAsAdminAction() itself here because that one always refers to the active view instead of this->m_view.
909 auto actAsAdminAction
= Admin::WorkerIntegration::FriendAccess::actAsAdminAction();
910 m_authorizeToEnterFolderAction
= new QAction
{actAsAdminAction
->icon(), actAsAdminAction
->text(), this};
911 m_authorizeToEnterFolderAction
->setToolTip(actAsAdminAction
->toolTip());
912 m_authorizeToEnterFolderAction
->setWhatsThis(actAsAdminAction
->whatsThis());
913 connect(m_authorizeToEnterFolderAction
, &QAction::triggered
, this, [this, actAsAdminAction
]() {
915 actAsAdminAction
->trigger();
918 showMessage(i18nc("@info", "Authorization required to enter this folder."), KMessageWidget::Error
, {m_authorizeToEnterFolderAction
});
921 Q_EMIT
showErrorMessage(message
);
924 void DolphinViewContainer::showErrorMessage(const QString
&message
)
926 showMessage(message
, KMessageWidget::Error
);
929 void DolphinViewContainer::slotPlacesModelChanged()
931 if (!GeneralSettings::showFullPathInTitlebar() && !isSearchModeEnabled()) {
932 Q_EMIT
captionChanged();
936 void DolphinViewContainer::slotHiddenFilesShownChanged(bool showHiddenFiles
)
938 if (m_urlNavigatorConnected
) {
939 m_urlNavigatorConnected
->setShowHiddenFolders(showHiddenFiles
);
943 void DolphinViewContainer::slotSortHiddenLastChanged(bool hiddenLast
)
945 if (m_urlNavigatorConnected
) {
946 m_urlNavigatorConnected
->setSortHiddenFoldersLast(hiddenLast
);
950 void DolphinViewContainer::slotCurrentDirectoryRemoved()
952 const QString
location(url().toDisplayString(QUrl::PreferLocalFile
));
953 if (url().isLocalFile()) {
954 const QString dirPath
= url().toLocalFile();
955 const QString newPath
= getNearestExistingAncestorOfPath(dirPath
);
956 const QUrl newUrl
= QUrl::fromLocalFile(newPath
);
960 showMessage(xi18n("Current location changed, <filename>%1</filename> is no longer accessible.", location
), KMessageWidget::Warning
);
963 void DolphinViewContainer::slotOpenUrlFinished(KJob
*job
)
965 if (job
->error() && job
->error() != KIO::ERR_USER_CANCELED
) {
966 showErrorMessage(job
->errorString());
970 bool DolphinViewContainer::isSearchUrl(const QUrl
&url
) const
972 return url
.scheme().contains(QLatin1String("search"));
975 void DolphinViewContainer::saveViewState()
977 QByteArray locationState
;
978 QDataStream
stream(&locationState
, QIODevice::WriteOnly
);
979 m_view
->saveState(stream
);
980 m_urlNavigator
->saveLocationState(locationState
);
983 void DolphinViewContainer::tryRestoreViewState()
985 QByteArray locationState
= m_urlNavigator
->locationState();
986 if (!locationState
.isEmpty()) {
987 QDataStream
stream(&locationState
, QIODevice::ReadOnly
);
988 m_view
->restoreState(stream
);
992 QString
DolphinViewContainer::getNearestExistingAncestorOfPath(const QString
&path
) const
996 dir
.setPath(QDir::cleanPath(dir
.filePath(QStringLiteral(".."))));
997 } while (!dir
.exists() && !dir
.isRoot());
999 return dir
.exists() ? dir
.path() : QString
{};
1002 #include "moc_dolphinviewcontainer.cpp"