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 #ifndef QT_NO_ACCESSIBILITY
37 #include <QAccessible>
39 #include <QApplication>
40 #include <QDesktopServices>
42 #include <QGridLayout>
43 #include <QGuiApplication>
44 #include <QRegularExpression>
49 // An overview of the widgets contained by this ViewContainer
50 struct LayoutStructure
{
53 int messageWidget
= 2;
54 int selectionModeTopBar
= 3;
56 int selectionModeBottomBar
= 5;
60 constexpr LayoutStructure positionFor
;
62 DolphinViewContainer::DolphinViewContainer(const QUrl
&url
, QWidget
*parent
)
64 , m_topLayout(nullptr)
65 , m_urlNavigator
{new DolphinUrlNavigator(url
)}
66 , m_urlNavigatorConnected
{nullptr}
67 , m_searchBox(nullptr)
68 , m_searchModeEnabled(false)
70 , m_authorizeToEnterFolderAction
{nullptr}
71 , m_messageWidget(nullptr)
72 , m_selectionModeTopBar
{nullptr}
74 , m_filterBar(nullptr)
75 , m_selectionModeBottomBar
{nullptr}
76 , m_statusBar(nullptr)
77 , m_statusBarTimer(nullptr)
78 , m_statusBarTimestamp()
79 , m_autoGrabFocus(true)
83 m_topLayout
= new QGridLayout(this);
84 m_topLayout
->setSpacing(0);
85 m_topLayout
->setContentsMargins(0, 0, 0, 0);
87 m_searchBox
= new DolphinSearchBox(this);
88 m_searchBox
->setVisible(false, WithoutAnimation
);
89 connect(m_searchBox
, &DolphinSearchBox::activated
, this, &DolphinViewContainer::activate
);
90 connect(m_searchBox
, &DolphinSearchBox::openRequest
, this, &DolphinViewContainer::openSearchBox
);
91 connect(m_searchBox
, &DolphinSearchBox::closeRequest
, this, &DolphinViewContainer::closeSearchBox
);
92 connect(m_searchBox
, &DolphinSearchBox::searchRequest
, this, &DolphinViewContainer::startSearching
);
93 connect(m_searchBox
, &DolphinSearchBox::focusViewRequest
, this, &DolphinViewContainer::requestFocus
);
94 m_searchBox
->setWhatsThis(xi18nc("@info:whatsthis findbar",
95 "<para>This helps you find files and folders. Enter a <emphasis>"
96 "search term</emphasis> and specify search settings with the "
97 "buttons at the bottom:<list><item>Filename/Content: "
98 "Does the item you are looking for contain the search terms "
99 "within its filename or its contents?<nl/>The contents of images, "
100 "audio files and videos will not be searched.</item><item>"
101 "From Here/Everywhere: Do you want to search in this "
102 "folder and its sub-folders or everywhere?</item><item>"
103 "More Options: Click this to search by media type, access "
104 "time or rating.</item><item>More Search Tools: Install other "
105 "means to find an item.</item></list></para>"));
107 m_messageWidget
= new KMessageWidget(this);
108 m_messageWidget
->setCloseButtonVisible(true);
109 m_messageWidget
->setPosition(KMessageWidget::Header
);
110 m_messageWidget
->hide();
112 #if !defined(Q_OS_WIN) && !defined(Q_OS_HAIKU)
114 // We must be logged in as the root user; show a big scary warning
115 showMessage(i18n("Running Dolphin as root can be dangerous. Please be careful."), KMessageWidget::Warning
);
119 // Initialize filter bar
120 m_filterBar
= new FilterBar(this);
121 m_filterBar
->setVisible(GeneralSettings::filterBar(), WithoutAnimation
);
123 connect(m_filterBar
, &FilterBar::filterChanged
, this, &DolphinViewContainer::setNameFilter
);
124 connect(m_filterBar
, &FilterBar::closeRequest
, this, &DolphinViewContainer::closeFilterBar
);
125 connect(m_filterBar
, &FilterBar::focusViewRequest
, this, &DolphinViewContainer::requestFocus
);
127 // Initialize the main view
128 m_view
= new DolphinView(url
, this);
129 connect(m_view
, &DolphinView::urlChanged
, m_filterBar
, &FilterBar::clearIfUnlocked
);
130 connect(m_view
, &DolphinView::urlChanged
, m_messageWidget
, &KMessageWidget::hide
);
131 // m_urlNavigator stays in sync with m_view's location changes and
132 // keeps track of them so going back and forth in the history works.
133 connect(m_view
, &DolphinView::urlChanged
, m_urlNavigator
.get(), &DolphinUrlNavigator::setLocationUrl
);
134 connect(m_urlNavigator
.get(), &DolphinUrlNavigator::urlChanged
, this, &DolphinViewContainer::slotUrlNavigatorLocationChanged
);
135 connect(m_urlNavigator
.get(), &DolphinUrlNavigator::urlAboutToBeChanged
, this, &DolphinViewContainer::slotUrlNavigatorLocationAboutToBeChanged
);
136 connect(m_urlNavigator
.get(), &DolphinUrlNavigator::urlSelectionRequested
, this, &DolphinViewContainer::slotUrlSelectionRequested
);
137 connect(m_view
, &DolphinView::writeStateChanged
, this, &DolphinViewContainer::writeStateChanged
);
138 connect(m_view
, &DolphinView::requestItemInfo
, this, &DolphinViewContainer::showItemInfo
);
139 connect(m_view
, &DolphinView::itemActivated
, this, &DolphinViewContainer::slotItemActivated
);
140 connect(m_view
, &DolphinView::fileMiddleClickActivated
, this, &DolphinViewContainer::slotfileMiddleClickActivated
);
141 connect(m_view
, &DolphinView::itemsActivated
, this, &DolphinViewContainer::slotItemsActivated
);
142 connect(m_view
, &DolphinView::redirection
, this, &DolphinViewContainer::redirect
);
143 connect(m_view
, &DolphinView::directoryLoadingStarted
, this, &DolphinViewContainer::slotDirectoryLoadingStarted
);
144 connect(m_view
, &DolphinView::directoryLoadingCompleted
, this, &DolphinViewContainer::slotDirectoryLoadingCompleted
);
145 connect(m_view
, &DolphinView::directoryLoadingCanceled
, this, &DolphinViewContainer::slotDirectoryLoadingCanceled
);
146 connect(m_view
, &DolphinView::itemCountChanged
, this, &DolphinViewContainer::delayedStatusBarUpdate
);
147 connect(m_view
, &DolphinView::selectionChanged
, this, &DolphinViewContainer::delayedStatusBarUpdate
);
148 connect(m_view
, &DolphinView::errorMessage
, this, &DolphinViewContainer::slotErrorMessageFromView
);
149 connect(m_view
, &DolphinView::urlIsFileError
, this, &DolphinViewContainer::slotUrlIsFileError
);
150 connect(m_view
, &DolphinView::activated
, this, &DolphinViewContainer::activate
);
151 connect(m_view
, &DolphinView::hiddenFilesShownChanged
, this, &DolphinViewContainer::slotHiddenFilesShownChanged
);
152 connect(m_view
, &DolphinView::sortHiddenLastChanged
, this, &DolphinViewContainer::slotSortHiddenLastChanged
);
153 connect(m_view
, &DolphinView::currentDirectoryRemoved
, this, &DolphinViewContainer::slotCurrentDirectoryRemoved
);
155 // Initialize status bar
156 m_statusBar
= new DolphinStatusBar(this);
157 m_statusBar
->setUrl(m_view
->url());
158 m_statusBar
->setZoomLevel(m_view
->zoomLevel());
159 connect(m_view
, &DolphinView::urlChanged
, m_statusBar
, &DolphinStatusBar::setUrl
);
160 connect(m_view
, &DolphinView::zoomLevelChanged
, m_statusBar
, &DolphinStatusBar::setZoomLevel
);
161 connect(m_view
, &DolphinView::infoMessage
, m_statusBar
, &DolphinStatusBar::setText
);
162 connect(m_view
, &DolphinView::operationCompletedMessage
, m_statusBar
, &DolphinStatusBar::setText
);
163 connect(m_view
, &DolphinView::statusBarTextChanged
, m_statusBar
, &DolphinStatusBar::setDefaultText
);
164 connect(m_view
, &DolphinView::statusBarTextChanged
, m_statusBar
, &DolphinStatusBar::resetToDefaultText
);
165 connect(m_view
, &DolphinView::directoryLoadingProgress
, m_statusBar
, [this](int percent
) {
166 m_statusBar
->showProgress(i18nc("@info:progress", "Loading folder…"), percent
);
168 connect(m_view
, &DolphinView::directorySortingProgress
, m_statusBar
, [this](int percent
) {
169 m_statusBar
->showProgress(i18nc("@info:progress", "Sorting…"), percent
);
171 connect(m_statusBar
, &DolphinStatusBar::stopPressed
, this, &DolphinViewContainer::stopDirectoryLoading
);
172 connect(m_statusBar
, &DolphinStatusBar::zoomLevelChanged
, this, &DolphinViewContainer::slotStatusBarZoomLevelChanged
);
173 connect(m_statusBar
, &DolphinStatusBar::showMessage
, this, [this](const QString
&message
, KMessageWidget::MessageType messageType
) {
174 showMessage(message
, messageType
);
177 m_statusBarTimer
= new QTimer(this);
178 m_statusBarTimer
->setSingleShot(true);
179 m_statusBarTimer
->setInterval(300);
180 connect(m_statusBarTimer
, &QTimer::timeout
, this, &DolphinViewContainer::updateStatusBar
);
182 KIO::FileUndoManager
*undoManager
= KIO::FileUndoManager::self();
183 connect(undoManager
, &KIO::FileUndoManager::jobRecordingFinished
, this, &DolphinViewContainer::delayedStatusBarUpdate
);
185 m_topLayout
->addWidget(m_searchBox
, positionFor
.searchBox
, 0);
186 m_topLayout
->addWidget(m_messageWidget
, positionFor
.messageWidget
, 0);
187 m_topLayout
->addWidget(m_view
, positionFor
.view
, 0);
188 m_topLayout
->addWidget(m_filterBar
, positionFor
.filterBar
, 0);
189 m_topLayout
->addWidget(m_statusBar
, positionFor
.statusBar
, 0);
191 setSearchModeEnabled(isSearchUrl(url
));
193 // Update view as the ContentDisplaySettings change
194 // this happens here and not in DolphinView as DolphinviewContainer and DolphinView are not in the same build target ATM
195 connect(ContentDisplaySettings::self(), &KCoreConfigSkeleton::configChanged
, m_view
, &DolphinView::reload
);
197 KFilePlacesModel
*placesModel
= DolphinPlacesModelSingleton::instance().placesModel();
198 connect(placesModel
, &KFilePlacesModel::dataChanged
, this, &DolphinViewContainer::slotPlacesModelChanged
);
199 connect(placesModel
, &KFilePlacesModel::rowsInserted
, this, &DolphinViewContainer::slotPlacesModelChanged
);
200 connect(placesModel
, &KFilePlacesModel::rowsRemoved
, this, &DolphinViewContainer::slotPlacesModelChanged
);
202 connect(this, &DolphinViewContainer::searchModeEnabledChanged
, this, &DolphinViewContainer::captionChanged
);
205 DolphinViewContainer::~DolphinViewContainer()
209 QUrl
DolphinViewContainer::url() const
211 return m_view
->url();
214 KFileItem
DolphinViewContainer::rootItem() const
216 return m_view
->rootItem();
219 void DolphinViewContainer::setActive(bool active
)
221 m_searchBox
->setActive(active
);
222 if (m_urlNavigatorConnected
) {
223 m_urlNavigatorConnected
->setActive(active
);
225 m_view
->setActive(active
);
228 bool DolphinViewContainer::isActive() const
230 return m_view
->isActive();
233 void DolphinViewContainer::setAutoGrabFocus(bool grab
)
235 m_autoGrabFocus
= grab
;
238 bool DolphinViewContainer::autoGrabFocus() const
240 return m_autoGrabFocus
;
243 QString
DolphinViewContainer::currentSearchText() const
245 return m_searchBox
->text();
248 const DolphinStatusBar
*DolphinViewContainer::statusBar() const
253 DolphinStatusBar
*DolphinViewContainer::statusBar()
258 const DolphinUrlNavigator
*DolphinViewContainer::urlNavigator() const
260 return m_urlNavigatorConnected
;
263 DolphinUrlNavigator
*DolphinViewContainer::urlNavigator()
265 return m_urlNavigatorConnected
;
268 const DolphinUrlNavigator
*DolphinViewContainer::urlNavigatorInternalWithHistory() const
270 return m_urlNavigator
.get();
273 DolphinUrlNavigator
*DolphinViewContainer::urlNavigatorInternalWithHistory()
275 return m_urlNavigator
.get();
278 const DolphinView
*DolphinViewContainer::view() const
283 DolphinView
*DolphinViewContainer::view()
288 void DolphinViewContainer::connectUrlNavigator(DolphinUrlNavigator
*urlNavigator
)
290 Q_CHECK_PTR(urlNavigator
);
291 Q_ASSERT(!m_urlNavigatorConnected
);
292 Q_ASSERT(m_urlNavigator
.get() != urlNavigator
);
295 urlNavigator
->setLocationUrl(m_view
->url());
296 urlNavigator
->setShowHiddenFolders(m_view
->hiddenFilesShown());
297 urlNavigator
->setSortHiddenFoldersLast(m_view
->sortHiddenLast());
298 if (m_urlNavigatorVisualState
) {
299 urlNavigator
->setVisualState(*m_urlNavigatorVisualState
.get());
300 m_urlNavigatorVisualState
.reset();
302 urlNavigator
->setActive(isActive());
304 // Url changes are still done via m_urlNavigator.
305 connect(urlNavigator
, &DolphinUrlNavigator::urlChanged
, m_urlNavigator
.get(), &DolphinUrlNavigator::setLocationUrl
);
306 connect(urlNavigator
, &DolphinUrlNavigator::urlsDropped
, this, [=, this](const QUrl
&destination
, QDropEvent
*event
) {
307 m_view
->dropUrls(destination
, event
, urlNavigator
->dropWidget());
309 // Aside from these, only visual things need to be connected.
310 connect(m_view
, &DolphinView::urlChanged
, urlNavigator
, &DolphinUrlNavigator::setLocationUrl
);
311 connect(urlNavigator
, &DolphinUrlNavigator::activated
, this, &DolphinViewContainer::activate
);
312 connect(urlNavigator
, &DolphinUrlNavigator::requestToLoseFocus
, m_view
, [this]() {
316 urlNavigator
->setReadOnlyBadgeVisible(rootItem().isLocalFile() && !rootItem().isWritable());
318 m_urlNavigatorConnected
= urlNavigator
;
321 void DolphinViewContainer::disconnectUrlNavigator()
323 if (!m_urlNavigatorConnected
) {
327 disconnect(m_urlNavigatorConnected
, &DolphinUrlNavigator::urlChanged
, m_urlNavigator
.get(), &DolphinUrlNavigator::setLocationUrl
);
328 disconnect(m_urlNavigatorConnected
, &DolphinUrlNavigator::urlsDropped
, this, nullptr);
329 disconnect(m_view
, &DolphinView::urlChanged
, m_urlNavigatorConnected
, &DolphinUrlNavigator::setLocationUrl
);
330 disconnect(m_urlNavigatorConnected
, &DolphinUrlNavigator::activated
, this, &DolphinViewContainer::activate
);
331 disconnect(m_urlNavigatorConnected
, &DolphinUrlNavigator::requestToLoseFocus
, m_view
, nullptr);
333 m_urlNavigatorVisualState
= m_urlNavigatorConnected
->visualState();
334 m_urlNavigatorConnected
= nullptr;
337 void DolphinViewContainer::setSelectionModeEnabled(bool enabled
, KActionCollection
*actionCollection
, SelectionMode::BottomBar::Contents bottomBarContents
)
339 const bool wasEnabled
= m_view
->selectionMode();
340 m_view
->setSelectionModeEnabled(enabled
);
344 return; // nothing to do here
346 Q_CHECK_PTR(m_selectionModeTopBar
); // there is no point in disabling selectionMode when it wasn't even enabled once.
347 Q_CHECK_PTR(m_selectionModeBottomBar
);
348 m_selectionModeTopBar
->setVisible(false, WithAnimation
);
349 m_selectionModeBottomBar
->setVisible(false, WithAnimation
);
350 Q_EMIT
selectionModeChanged(false);
352 if (!QApplication::focusWidget() || m_selectionModeTopBar
->isAncestorOf(QApplication::focusWidget())
353 || m_selectionModeBottomBar
->isAncestorOf(QApplication::focusWidget())) {
359 if (!m_selectionModeTopBar
) {
360 // Changing the location will disable selection mode.
361 connect(m_urlNavigator
.get(), &DolphinUrlNavigator::urlChanged
, this, [this]() {
362 setSelectionModeEnabled(false);
365 m_selectionModeTopBar
= new SelectionMode::TopBar(this); // will be created hidden
366 connect(m_selectionModeTopBar
, &SelectionMode::TopBar::selectionModeLeavingRequested
, this, [this]() {
367 setSelectionModeEnabled(false);
369 m_topLayout
->addWidget(m_selectionModeTopBar
, positionFor
.selectionModeTopBar
, 0);
372 if (!m_selectionModeBottomBar
) {
373 m_selectionModeBottomBar
= new SelectionMode::BottomBar(actionCollection
, this);
374 connect(m_view
, &DolphinView::selectionChanged
, this, [this](const KFileItemList
&selection
) {
375 m_selectionModeBottomBar
->slotSelectionChanged(selection
, m_view
->url());
377 connect(m_selectionModeBottomBar
, &SelectionMode::BottomBar::error
, this, &DolphinViewContainer::showErrorMessage
);
378 connect(m_selectionModeBottomBar
, &SelectionMode::BottomBar::selectionModeLeavingRequested
, this, [this]() {
379 setSelectionModeEnabled(false);
381 m_topLayout
->addWidget(m_selectionModeBottomBar
, positionFor
.selectionModeBottomBar
, 0);
383 m_selectionModeBottomBar
->resetContents(bottomBarContents
);
384 if (bottomBarContents
== SelectionMode::BottomBar::GeneralContents
) {
385 m_selectionModeBottomBar
->slotSelectionChanged(m_view
->selectedItems(), m_view
->url());
389 m_selectionModeTopBar
->setVisible(true, WithAnimation
);
390 m_selectionModeBottomBar
->setVisible(true, WithAnimation
);
391 Q_EMIT
selectionModeChanged(true);
395 bool DolphinViewContainer::isSelectionModeEnabled() const
397 const bool isEnabled
= m_view
->selectionMode();
399 // We can't assert that the bars are invisible only because the selection mode is disabled because the hide animation might still be playing.
400 && (!m_selectionModeBottomBar
|| !m_selectionModeBottomBar
->isEnabled() || !m_selectionModeBottomBar
->isVisible()
401 || m_selectionModeBottomBar
->contents() == SelectionMode::BottomBar::PasteContents
))
402 || (isEnabled
&& m_selectionModeTopBar
403 && m_selectionModeTopBar
->isVisible()
404 // 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.
405 && m_selectionModeBottomBar
406 && (m_selectionModeBottomBar
->isVisible() || m_selectionModeBottomBar
->contents() == SelectionMode::BottomBar::GeneralContents
)));
410 void DolphinViewContainer::slotSplitTabDisabled()
412 if (m_selectionModeBottomBar
) {
413 m_selectionModeBottomBar
->slotSplitTabDisabled();
417 void DolphinViewContainer::showMessage(const QString
&message
, KMessageWidget::MessageType messageType
, std::initializer_list
<QAction
*> buttonActions
)
419 if (message
.isEmpty()) {
423 m_messageWidget
->setText(message
);
425 // TODO: wrap at arbitrary character positions once QLabel can do this
426 // https://bugreports.qt.io/browse/QTBUG-1276
427 m_messageWidget
->setWordWrap(true);
428 m_messageWidget
->setMessageType(messageType
);
430 const QList
<QAction
*> previousMessageWidgetActions
= m_messageWidget
->actions();
431 for (auto action
: previousMessageWidgetActions
) {
432 m_messageWidget
->removeAction(action
);
434 for (QAction
*action
: buttonActions
) {
435 m_messageWidget
->addAction(action
);
438 m_messageWidget
->setWordWrap(false);
439 const int unwrappedWidth
= m_messageWidget
->sizeHint().width();
440 m_messageWidget
->setWordWrap(unwrappedWidth
> size().width());
442 if (m_messageWidget
->isVisible()) {
443 m_messageWidget
->hide();
445 m_messageWidget
->animatedShow();
447 #ifndef QT_NO_ACCESSIBILITY
448 if (QAccessible::isActive() && isActive()) {
449 // To announce the new message keyboard focus must be moved to the message label. However, we do not have direct access to the label that is internal
450 // to the KMessageWidget. Instead we setFocus() on the KMessageWidget and trust that it has set correct focus handling.
451 m_messageWidget
->setFocus();
456 void DolphinViewContainer::readSettings()
458 // The startup settings should (only) get applied if they have been
459 // modified by the user. Otherwise keep the (possibly) different current
460 // setting of the filterbar.
461 if (GeneralSettings::modifiedStartupSettings()) {
462 setFilterBarVisible(GeneralSettings::filterBar());
465 m_view
->readSettings();
466 m_statusBar
->readSettings();
469 bool DolphinViewContainer::isFilterBarVisible() const
471 return m_filterBar
->isEnabled(); // Gets disabled in AnimatedHeightWidget while animating towards a hidden state.
474 void DolphinViewContainer::setSearchModeEnabled(bool enabled
)
476 m_searchBox
->setVisible(enabled
, WithAnimation
);
479 const QUrl
&locationUrl
= m_urlNavigator
->locationUrl();
480 m_searchBox
->fromSearchUrl(locationUrl
);
483 if (enabled
== isSearchModeEnabled()) {
484 if (enabled
&& !m_searchBox
->hasFocus()) {
485 m_searchBox
->setFocus();
486 m_searchBox
->selectAll();
492 m_view
->setViewPropertiesContext(QString());
494 // Restore the URL for the URL navigator. If Dolphin has been
495 // started with a search-URL, the home URL is used as fallback.
496 QUrl url
= m_searchBox
->searchPath();
497 if (url
.isEmpty() || !url
.isValid() || isSearchUrl(url
)) {
498 url
= Dolphin::homeUrl();
500 if (m_urlNavigatorConnected
) {
501 m_urlNavigatorConnected
->setLocationUrl(url
);
505 m_searchModeEnabled
= enabled
;
507 Q_EMIT
searchModeEnabledChanged(enabled
);
510 bool DolphinViewContainer::isSearchModeEnabled() const
512 return m_searchModeEnabled
;
515 QString
DolphinViewContainer::placesText() const
519 if (isSearchModeEnabled()) {
520 text
= i18n("Search for %1 in %2", m_searchBox
->text(), m_searchBox
->searchPath().fileName());
522 text
= url().adjusted(QUrl::StripTrailingSlash
).fileName();
523 if (text
.isEmpty()) {
526 if (text
.isEmpty()) {
527 text
= url().scheme();
534 void DolphinViewContainer::reload()
537 m_messageWidget
->hide();
540 QString
DolphinViewContainer::captionWindowTitle() const
542 if (GeneralSettings::showFullPathInTitlebar() && !isSearchModeEnabled()) {
543 if (!url().isLocalFile()) {
544 return url().adjusted(QUrl::StripTrailingSlash
).toString();
546 return url().adjusted(QUrl::StripTrailingSlash
).path();
548 return DolphinViewContainer::caption();
552 QString
DolphinViewContainer::caption() const
554 // see KUrlNavigatorPrivate::firstButtonText().
555 if (url().path().isEmpty() || url().path() == QLatin1Char('/')) {
556 QUrlQuery
query(url());
557 const QString title
= query
.queryItemValue(QStringLiteral("title"));
558 if (!title
.isEmpty()) {
563 if (isSearchModeEnabled()) {
564 if (currentSearchText().isEmpty()) {
565 return i18n("Search");
567 return i18n("Search for %1", currentSearchText());
571 KFilePlacesModel
*placesModel
= DolphinPlacesModelSingleton::instance().placesModel();
573 QModelIndex url_index
= placesModel
->closestItem(url());
575 if (url_index
.isValid() && placesModel
->url(url_index
).matches(url(), QUrl::StripTrailingSlash
)) {
576 return placesModel
->text(url_index
);
579 if (!url().isLocalFile()) {
580 QUrl adjustedUrl
= url().adjusted(QUrl::StripTrailingSlash
);
582 if (!adjustedUrl
.fileName().isEmpty()) {
583 caption
= adjustedUrl
.fileName();
584 } else if (!adjustedUrl
.path().isEmpty() && adjustedUrl
.path() != "/") {
585 caption
= adjustedUrl
.path();
586 } else if (!adjustedUrl
.host().isEmpty()) {
587 caption
= adjustedUrl
.host();
589 caption
= adjustedUrl
.toString();
594 QString fileName
= url().adjusted(QUrl::StripTrailingSlash
).fileName();
595 if (fileName
.isEmpty()) {
602 void DolphinViewContainer::setUrl(const QUrl
&newUrl
)
604 if (newUrl
!= m_urlNavigator
->locationUrl()) {
605 m_urlNavigator
->setLocationUrl(newUrl
);
609 void DolphinViewContainer::setFilterBarVisible(bool visible
)
611 Q_ASSERT(m_filterBar
);
613 m_view
->hideToolTip(ToolTipManager::HideBehavior::Instantly
);
614 m_filterBar
->setVisible(true, WithAnimation
);
615 m_filterBar
->setFocus();
616 m_filterBar
->selectAll();
622 void DolphinViewContainer::delayedStatusBarUpdate()
624 if (m_statusBarTimer
->isActive() && (m_statusBarTimestamp
.elapsed() > 2000)) {
625 // No update of the statusbar has been done during the last 2 seconds,
626 // although an update has been requested. Trigger an immediate update.
627 m_statusBarTimer
->stop();
630 // Invoke updateStatusBar() with a small delay. This assures that
631 // when a lot of delayedStatusBarUpdates() are done in a short time,
632 // no bottleneck is given.
633 m_statusBarTimer
->start();
637 void DolphinViewContainer::updateStatusBar()
639 m_statusBarTimestamp
.start();
640 m_view
->requestStatusBarText();
643 void DolphinViewContainer::slotDirectoryLoadingStarted()
645 if (isSearchUrl(url())) {
646 // Search KIO-slaves usually don't provide any progress information. Give
647 // a hint to the user that a searching is done:
649 m_statusBar
->showProgress(i18nc("@info", "Searching…"), -1);
651 // Trigger an undetermined progress indication. The progress
652 // information in percent will be triggered by the percent() signal
653 // of the directory lister later.
654 m_statusBar
->showProgress(QString(), -1);
657 if (m_urlNavigatorConnected
) {
658 m_urlNavigatorConnected
->setReadOnlyBadgeVisible(false);
662 void DolphinViewContainer::slotDirectoryLoadingCompleted()
664 m_statusBar
->showProgress(QString(), 100);
666 if (isSearchUrl(url()) && m_view
->itemsCount() == 0) {
667 // The dir lister has been completed on a Baloo-URI and no items have been found. Instead
668 // of showing the default status bar information ("0 items") a more helpful information is given:
669 m_statusBar
->setText(i18nc("@info:status", "No items found."));
674 if (m_urlNavigatorConnected
) {
675 m_urlNavigatorConnected
->setReadOnlyBadgeVisible(rootItem().isLocalFile() && !rootItem().isWritable());
678 // Update admin bar visibility
679 if (m_view
->url().scheme() == QStringLiteral("admin")) {
681 m_adminBar
= new Admin::Bar(this);
682 m_topLayout
->addWidget(m_adminBar
, positionFor
.adminBar
, 0);
684 m_adminBar
->setVisible(true, WithAnimation
);
685 } else if (m_adminBar
) {
686 m_adminBar
->setVisible(false, WithAnimation
);
690 void DolphinViewContainer::slotDirectoryLoadingCanceled()
692 m_statusBar
->showProgress(QString(), 100);
693 m_statusBar
->setText(QString());
696 void DolphinViewContainer::slotUrlIsFileError(const QUrl
&url
)
698 const KFileItem
item(url
);
700 // Find out if the file can be opened in the view (for example, this is the
701 // case if the file is an archive). The mime type must be known for that.
702 item
.determineMimeType();
703 const QUrl
&folderUrl
= DolphinView::openItemAsFolderUrl(item
, true);
704 if (!folderUrl
.isEmpty()) {
707 slotItemActivated(item
);
711 void DolphinViewContainer::slotItemActivated(const KFileItem
&item
)
713 // It is possible to activate items on inactive views by
714 // drag & drop operations. Assure that activating an item always
715 // results in an active view.
716 m_view
->setActive(true);
718 const QUrl
&url
= DolphinView::openItemAsFolderUrl(item
, GeneralSettings::browseThroughArchives());
719 if (!url
.isEmpty()) {
720 const auto modifiers
= QGuiApplication::keyboardModifiers();
721 // keep in sync with KUrlNavigator::slotNavigatorButtonClicked
722 if (modifiers
& Qt::ControlModifier
&& modifiers
& Qt::ShiftModifier
) {
723 Q_EMIT
activeTabRequested(url
);
724 } else if (modifiers
& Qt::ControlModifier
) {
725 Q_EMIT
tabRequested(url
);
726 } else if (modifiers
& Qt::ShiftModifier
) {
727 Dolphin::openNewWindow({KFilePlacesModel::convertedUrl(url
)}, this);
734 KIO::OpenUrlJob
*job
= new KIO::OpenUrlJob(item
.targetUrl(), item
.mimetype());
735 // Auto*Warning*Handling, errors are put in a KMessageWidget by us in slotOpenUrlFinished.
736 job
->setUiDelegate(KIO::createDefaultJobUiDelegate(KJobUiDelegate::AutoWarningHandlingEnabled
, this));
737 job
->setShowOpenOrExecuteDialog(true);
738 connect(job
, &KIO::OpenUrlJob::finished
, this, &DolphinViewContainer::slotOpenUrlFinished
);
742 void DolphinViewContainer::slotfileMiddleClickActivated(const KFileItem
&item
)
744 KService::List services
= KApplicationTrader::queryByMimeType(item
.mimetype());
746 int indexOfAppToOpenFileWith
= 1;
748 // executable scripts
749 auto mimeType
= item
.currentMimeType();
750 if (item
.isLocalFile() && mimeType
.inherits(QStringLiteral("application/x-executable")) && mimeType
.inherits(QStringLiteral("text/plain"))
751 && QFileInfo(item
.localPath()).isExecutable()) {
752 KConfigGroup
cfgGroup(KSharedConfig::openConfig(QStringLiteral("kiorc")), QStringLiteral("Executable scripts"));
753 const QString value
= cfgGroup
.readEntry("behaviourOnLaunch", "alwaysAsk");
755 // in case KIO::WidgetsOpenOrExecuteFileHandler::promptUserOpenOrExecute would not open the file
756 if (value
!= QLatin1String("open")) {
757 indexOfAppToOpenFileWith
= 0;
761 if (services
.length() >= indexOfAppToOpenFileWith
+ 1) {
762 auto service
= services
.at(indexOfAppToOpenFileWith
);
764 KIO::ApplicationLauncherJob
*job
= new KIO::ApplicationLauncherJob(service
, this);
765 job
->setUrls({item
.url()});
767 job
->setUiDelegate(KIO::createDefaultJobUiDelegate(KJobUiDelegate::AutoHandlingEnabled
, this));
768 connect(job
, &KIO::OpenUrlJob::finished
, this, &DolphinViewContainer::slotOpenUrlFinished
);
771 // If no 2nd service available, try to open archives in new tabs, regardless of the "Open archives as folder" setting.
772 const QUrl
&url
= DolphinView::openItemAsFolderUrl(item
);
773 const auto modifiers
= QGuiApplication::keyboardModifiers();
774 if (!url
.isEmpty()) {
775 // keep in sync with KUrlNavigator::slotNavigatorButtonClicked
776 if (modifiers
& Qt::ShiftModifier
) {
777 Q_EMIT
activeTabRequested(url
);
779 Q_EMIT
tabRequested(url
);
785 void DolphinViewContainer::slotItemsActivated(const KFileItemList
&items
)
787 Q_ASSERT(items
.count() >= 2);
789 KFileItemActions
fileItemActions(this);
790 fileItemActions
.runPreferredApplications(items
);
793 void DolphinViewContainer::showItemInfo(const KFileItem
&item
)
796 m_statusBar
->resetToDefaultText();
798 m_statusBar
->setText(item
.getStatusBarInfo());
802 void DolphinViewContainer::closeFilterBar()
804 m_filterBar
->closeFilterBar();
806 Q_EMIT
showFilterBarChanged(false);
809 void DolphinViewContainer::clearFilterBar()
811 m_filterBar
->clearIfUnlocked();
814 void DolphinViewContainer::setNameFilter(const QString
&nameFilter
)
816 m_view
->hideToolTip(ToolTipManager::HideBehavior::Instantly
);
817 m_view
->setNameFilter(nameFilter
);
818 delayedStatusBarUpdate();
821 void DolphinViewContainer::activate()
826 void DolphinViewContainer::slotUrlNavigatorLocationAboutToBeChanged(const QUrl
&)
831 void DolphinViewContainer::slotUrlNavigatorLocationChanged(const QUrl
&url
)
833 if (m_urlNavigatorConnected
) {
834 m_urlNavigatorConnected
->slotReturnPressed();
837 if (KProtocolManager::supportsListing(url
)) {
838 const bool searchBoxInitialized
= isSearchModeEnabled() && m_searchBox
->text().isEmpty();
839 setSearchModeEnabled(isSearchUrl(url
) || searchBoxInitialized
);
842 tryRestoreViewState();
844 if (m_autoGrabFocus
&& isActive() && !isSearchModeEnabled()) {
845 // When an URL has been entered, the view should get the focus.
846 // The focus must be requested asynchronously, as changing the URL might create
847 // a new view widget.
848 QTimer::singleShot(0, this, &DolphinViewContainer::requestFocus
);
850 } else if (KProtocolManager::isSourceProtocol(url
)) {
851 if (url
.scheme().startsWith(QLatin1String("http"))) {
852 showMessage(i18nc("@info:status", // krazy:exclude=qmethods
853 "Dolphin does not support web pages, the web browser has been launched"),
854 KMessageWidget::Information
);
856 showMessage(i18nc("@info:status", "Protocol not supported by Dolphin, default application has been launched"), KMessageWidget::Information
);
859 QDesktopServices::openUrl(url
);
860 redirect(QUrl(), m_urlNavigator
->locationUrl(1));
862 if (!url
.scheme().isEmpty()) {
863 showMessage(i18nc("@info:status", "Invalid protocol '%1'", url
.scheme()), KMessageWidget::Error
);
865 showMessage(i18nc("@info:status", "Invalid protocol"), KMessageWidget::Error
);
867 m_urlNavigator
->goBack();
871 void DolphinViewContainer::slotUrlSelectionRequested(const QUrl
&url
)
873 // 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
875 m_view
->markUrlAsCurrent(url
); // makes the item scroll into view
878 void DolphinViewContainer::disableUrlNavigatorSelectionRequests()
880 disconnect(m_urlNavigator
.get(), &KUrlNavigator::urlSelectionRequested
, this, &DolphinViewContainer::slotUrlSelectionRequested
);
883 void DolphinViewContainer::enableUrlNavigatorSelectionRequests()
885 connect(m_urlNavigator
.get(), &KUrlNavigator::urlSelectionRequested
, this, &DolphinViewContainer::slotUrlSelectionRequested
);
888 void DolphinViewContainer::redirect(const QUrl
&oldUrl
, const QUrl
&newUrl
)
891 const bool block
= m_urlNavigator
->signalsBlocked();
892 m_urlNavigator
->blockSignals(true);
894 // Assure that the location state is reset for redirection URLs. This
895 // allows to skip redirection URLs when going back or forward in the
897 m_urlNavigator
->saveLocationState(QByteArray());
898 m_urlNavigator
->setLocationUrl(newUrl
);
899 if (m_searchBox
->isActive()) {
900 m_searchBox
->setSearchPath(newUrl
);
902 setSearchModeEnabled(isSearchUrl(newUrl
));
904 m_urlNavigator
->blockSignals(block
);
907 void DolphinViewContainer::requestFocus()
912 void DolphinViewContainer::startSearching()
914 Q_CHECK_PTR(m_urlNavigatorConnected
);
915 const QUrl url
= m_searchBox
->urlForSearching();
916 if (url
.isValid() && !url
.isEmpty()) {
917 m_view
->setViewPropertiesContext(QStringLiteral("search"));
918 m_urlNavigatorConnected
->setLocationUrl(url
);
922 void DolphinViewContainer::openSearchBox()
924 setSearchModeEnabled(true);
927 void DolphinViewContainer::closeSearchBox()
929 setSearchModeEnabled(false);
932 void DolphinViewContainer::stopDirectoryLoading()
934 m_view
->stopLoading();
935 m_statusBar
->showProgress(QString(), 100);
938 void DolphinViewContainer::slotStatusBarZoomLevelChanged(int zoomLevel
)
940 m_view
->setZoomLevel(zoomLevel
);
943 void DolphinViewContainer::slotErrorMessageFromView(const QString
&message
, const int kioErrorCode
)
945 if (kioErrorCode
== KIO::ERR_CANNOT_ENTER_DIRECTORY
&& m_view
->url().scheme() == QStringLiteral("file")
946 && KProtocolInfo::isKnownProtocol(QStringLiteral("admin")) && !rootItem().isReadable()) {
947 // Explain to users that they need authentication to see the folder contents.
948 if (!m_authorizeToEnterFolderAction
) { // This code is similar to parts of Admin::Bar::hideTheNextTimeAuthorizationExpires().
949 // We should not simply use the actAsAdminAction() itself here because that one always refers to the active view instead of this->m_view.
950 auto actAsAdminAction
= Admin::WorkerIntegration::FriendAccess::actAsAdminAction();
951 m_authorizeToEnterFolderAction
= new QAction
{actAsAdminAction
->icon(), actAsAdminAction
->text(), this};
952 m_authorizeToEnterFolderAction
->setToolTip(actAsAdminAction
->toolTip());
953 m_authorizeToEnterFolderAction
->setWhatsThis(actAsAdminAction
->whatsThis());
954 connect(m_authorizeToEnterFolderAction
, &QAction::triggered
, this, [this, actAsAdminAction
]() {
956 actAsAdminAction
->trigger();
959 showMessage(i18nc("@info", "Authorization required to enter this folder."), KMessageWidget::Error
, {m_authorizeToEnterFolderAction
});
962 Q_EMIT
showErrorMessage(message
);
965 void DolphinViewContainer::showErrorMessage(const QString
&message
)
967 showMessage(message
, KMessageWidget::Error
);
970 void DolphinViewContainer::slotPlacesModelChanged()
972 if (!GeneralSettings::showFullPathInTitlebar() && !isSearchModeEnabled()) {
973 Q_EMIT
captionChanged();
977 void DolphinViewContainer::slotHiddenFilesShownChanged(bool showHiddenFiles
)
979 if (m_urlNavigatorConnected
) {
980 m_urlNavigatorConnected
->setShowHiddenFolders(showHiddenFiles
);
984 void DolphinViewContainer::slotSortHiddenLastChanged(bool hiddenLast
)
986 if (m_urlNavigatorConnected
) {
987 m_urlNavigatorConnected
->setSortHiddenFoldersLast(hiddenLast
);
991 void DolphinViewContainer::slotCurrentDirectoryRemoved()
993 const QString
location(url().toDisplayString(QUrl::PreferLocalFile
));
994 if (url().isLocalFile()) {
995 const QString dirPath
= url().toLocalFile();
996 const QString newPath
= getNearestExistingAncestorOfPath(dirPath
);
997 const QUrl newUrl
= QUrl::fromLocalFile(newPath
);
998 // #473377: Delay changing the url to avoid modifying KCoreDirLister before KCoreDirListerCache::deleteDir() returns.
999 QTimer::singleShot(0, this, [&, newUrl
, location
] {
1001 showMessage(xi18n("Current location changed, <filename>%1</filename> is no longer accessible.", location
), KMessageWidget::Warning
);
1004 showMessage(xi18n("Current location changed, <filename>%1</filename> is no longer accessible.", location
), KMessageWidget::Warning
);
1007 void DolphinViewContainer::slotOpenUrlFinished(KJob
*job
)
1009 if (job
->error() && job
->error() != KIO::ERR_USER_CANCELED
) {
1010 showErrorMessage(job
->errorString());
1014 bool DolphinViewContainer::isSearchUrl(const QUrl
&url
) const
1016 return url
.scheme().contains(QLatin1String("search"));
1019 void DolphinViewContainer::saveViewState()
1021 QByteArray locationState
;
1022 QDataStream
stream(&locationState
, QIODevice::WriteOnly
);
1023 m_view
->saveState(stream
);
1024 m_urlNavigator
->saveLocationState(locationState
);
1027 void DolphinViewContainer::tryRestoreViewState()
1029 QByteArray locationState
= m_urlNavigator
->locationState();
1030 if (!locationState
.isEmpty()) {
1031 QDataStream
stream(&locationState
, QIODevice::ReadOnly
);
1032 m_view
->restoreState(stream
);
1036 QString
DolphinViewContainer::getNearestExistingAncestorOfPath(const QString
&path
) const
1040 dir
.setPath(QDir::cleanPath(dir
.filePath(QStringLiteral(".."))));
1041 } while (!dir
.exists() && !dir
.isRoot());
1043 return dir
.exists() ? dir
.path() : QString
{};
1046 #include "moc_dolphinviewcontainer.cpp"