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 "dolphin_compactmodesettings.h"
11 #include "dolphin_contentdisplaysettings.h"
12 #include "dolphin_detailsmodesettings.h"
13 #include "dolphin_generalsettings.h"
14 #include "dolphin_iconsmodesettings.h"
15 #include "dolphindebug.h"
16 #include "dolphinplacesmodelsingleton.h"
17 #include "filterbar/filterbar.h"
19 #include "search/dolphinsearchbox.h"
20 #include "selectionmode/topbar.h"
21 #include "statusbar/dolphinstatusbar.h"
23 #include <KActionCollection>
24 #include <KApplicationTrader>
25 #include <KFileItemActions>
26 #include <KFilePlacesModel>
27 #include <KIO/JobUiDelegateFactory>
28 #include <KIO/OpenUrlJob>
29 #include <KLocalizedString>
30 #include <KMessageWidget>
31 #include <KProtocolManager>
33 #include <kio_version.h>
35 #include <QApplication>
36 #include <QDesktopServices>
38 #include <QGridLayout>
39 #include <QGuiApplication>
40 #include <QRegularExpression>
44 // An overview of the widgets contained by this ViewContainer
45 struct LayoutStructure
{
48 int messageWidget
= 2;
49 int selectionModeTopBar
= 3;
51 int selectionModeBottomBar
= 5;
55 constexpr LayoutStructure positionFor
;
57 DolphinViewContainer::DolphinViewContainer(const QUrl
&url
, QWidget
*parent
)
59 , m_topLayout(nullptr)
60 , m_urlNavigator
{new DolphinUrlNavigator(url
)}
61 , m_urlNavigatorConnected
{nullptr}
62 , m_searchBox(nullptr)
63 , m_searchModeEnabled(false)
65 , m_messageWidget(nullptr)
66 , m_selectionModeTopBar
{nullptr}
68 , m_filterBar(nullptr)
69 , m_selectionModeBottomBar
{nullptr}
70 , m_statusBar(nullptr)
71 , m_statusBarTimer(nullptr)
72 , m_statusBarTimestamp()
73 , m_autoGrabFocus(true)
77 m_topLayout
= new QGridLayout(this);
78 m_topLayout
->setSpacing(0);
79 m_topLayout
->setContentsMargins(0, 0, 0, 0);
81 m_searchBox
= new DolphinSearchBox(this);
82 m_searchBox
->setVisible(false, WithoutAnimation
);
83 connect(m_searchBox
, &DolphinSearchBox::activated
, this, &DolphinViewContainer::activate
);
84 connect(m_searchBox
, &DolphinSearchBox::openRequest
, this, &DolphinViewContainer::openSearchBox
);
85 connect(m_searchBox
, &DolphinSearchBox::closeRequest
, this, &DolphinViewContainer::closeSearchBox
);
86 connect(m_searchBox
, &DolphinSearchBox::searchRequest
, this, &DolphinViewContainer::startSearching
);
87 connect(m_searchBox
, &DolphinSearchBox::focusViewRequest
, this, &DolphinViewContainer::requestFocus
);
88 m_searchBox
->setWhatsThis(xi18nc("@info:whatsthis findbar",
89 "<para>This helps you find files and folders. Enter a <emphasis>"
90 "search term</emphasis> and specify search settings with the "
91 "buttons at the bottom:<list><item>Filename/Content: "
92 "Does the item you are looking for contain the search terms "
93 "within its filename or its contents?<nl/>The contents of images, "
94 "audio files and videos will not be searched.</item><item>"
95 "From Here/Everywhere: Do you want to search in this "
96 "folder and its sub-folders or everywhere?</item><item>"
97 "More Options: Click this to search by media type, access "
98 "time or rating.</item><item>More Search Tools: Install other "
99 "means to find an item.</item></list></para>"));
101 m_messageWidget
= new KMessageWidget(this);
102 m_messageWidget
->setCloseButtonVisible(true);
103 m_messageWidget
->setPosition(KMessageWidget::Header
);
104 m_messageWidget
->hide();
106 #if !defined(Q_OS_WIN) && !defined(Q_OS_HAIKU)
108 // We must be logged in as the root user; show a big scary warning
109 showMessage(i18n("Running Dolphin as root can be dangerous. Please be careful."), KMessageWidget::Warning
);
113 // Initialize filter bar
114 m_filterBar
= new FilterBar(this);
115 m_filterBar
->setVisible(GeneralSettings::filterBar(), WithoutAnimation
);
117 connect(m_filterBar
, &FilterBar::filterChanged
, this, &DolphinViewContainer::setNameFilter
);
118 connect(m_filterBar
, &FilterBar::closeRequest
, this, &DolphinViewContainer::closeFilterBar
);
119 connect(m_filterBar
, &FilterBar::focusViewRequest
, this, &DolphinViewContainer::requestFocus
);
121 // Initialize the main view
122 m_view
= new DolphinView(url
, this);
123 connect(m_view
, &DolphinView::urlChanged
, m_filterBar
, &FilterBar::clearIfUnlocked
);
124 connect(m_view
, &DolphinView::urlChanged
, m_messageWidget
, &KMessageWidget::hide
);
125 // m_urlNavigator stays in sync with m_view's location changes and
126 // keeps track of them so going back and forth in the history works.
127 connect(m_view
, &DolphinView::urlChanged
, m_urlNavigator
.get(), &DolphinUrlNavigator::setLocationUrl
);
128 connect(m_urlNavigator
.get(), &DolphinUrlNavigator::urlChanged
, this, &DolphinViewContainer::slotUrlNavigatorLocationChanged
);
129 connect(m_urlNavigator
.get(), &DolphinUrlNavigator::urlAboutToBeChanged
, this, &DolphinViewContainer::slotUrlNavigatorLocationAboutToBeChanged
);
130 connect(m_urlNavigator
.get(), &DolphinUrlNavigator::urlSelectionRequested
, this, &DolphinViewContainer::slotUrlSelectionRequested
);
131 connect(m_view
, &DolphinView::writeStateChanged
, this, &DolphinViewContainer::writeStateChanged
);
132 connect(m_view
, &DolphinView::requestItemInfo
, this, &DolphinViewContainer::showItemInfo
);
133 connect(m_view
, &DolphinView::itemActivated
, this, &DolphinViewContainer::slotItemActivated
);
134 connect(m_view
, &DolphinView::fileMiddleClickActivated
, this, &DolphinViewContainer::slotfileMiddleClickActivated
);
135 connect(m_view
, &DolphinView::itemsActivated
, this, &DolphinViewContainer::slotItemsActivated
);
136 connect(m_view
, &DolphinView::redirection
, this, &DolphinViewContainer::redirect
);
137 connect(m_view
, &DolphinView::directoryLoadingStarted
, this, &DolphinViewContainer::slotDirectoryLoadingStarted
);
138 connect(m_view
, &DolphinView::directoryLoadingCompleted
, this, &DolphinViewContainer::slotDirectoryLoadingCompleted
);
139 connect(m_view
, &DolphinView::directoryLoadingCanceled
, this, &DolphinViewContainer::slotDirectoryLoadingCanceled
);
140 connect(m_view
, &DolphinView::itemCountChanged
, this, &DolphinViewContainer::delayedStatusBarUpdate
);
141 connect(m_view
, &DolphinView::selectionChanged
, this, &DolphinViewContainer::delayedStatusBarUpdate
);
142 connect(m_view
, &DolphinView::errorMessage
, this, &DolphinViewContainer::showErrorMessage
);
143 connect(m_view
, &DolphinView::urlIsFileError
, this, &DolphinViewContainer::slotUrlIsFileError
);
144 connect(m_view
, &DolphinView::activated
, this, &DolphinViewContainer::activate
);
145 connect(m_view
, &DolphinView::hiddenFilesShownChanged
, this, &DolphinViewContainer::slotHiddenFilesShownChanged
);
146 connect(m_view
, &DolphinView::sortHiddenLastChanged
, this, &DolphinViewContainer::slotSortHiddenLastChanged
);
147 connect(m_view
, &DolphinView::currentDirectoryRemoved
, this, &DolphinViewContainer::slotCurrentDirectoryRemoved
);
148 connect(m_view
, &DolphinView::urlChanged
, this, &DolphinViewContainer::updateAdminBarVisibility
);
150 // Initialize status bar
151 m_statusBar
= new DolphinStatusBar(this);
152 m_statusBar
->setUrl(m_view
->url());
153 m_statusBar
->setZoomLevel(m_view
->zoomLevel());
154 connect(m_view
, &DolphinView::urlChanged
, m_statusBar
, &DolphinStatusBar::setUrl
);
155 connect(m_view
, &DolphinView::zoomLevelChanged
, m_statusBar
, &DolphinStatusBar::setZoomLevel
);
156 connect(m_view
, &DolphinView::infoMessage
, m_statusBar
, &DolphinStatusBar::setText
);
157 connect(m_view
, &DolphinView::operationCompletedMessage
, m_statusBar
, &DolphinStatusBar::setText
);
158 connect(m_view
, &DolphinView::statusBarTextChanged
, m_statusBar
, &DolphinStatusBar::setDefaultText
);
159 connect(m_view
, &DolphinView::statusBarTextChanged
, m_statusBar
, &DolphinStatusBar::resetToDefaultText
);
160 connect(m_view
, &DolphinView::directoryLoadingProgress
, m_statusBar
, [this](int percent
) {
161 m_statusBar
->showProgress(i18nc("@info:progress", "Loading folder…"), percent
);
163 connect(m_view
, &DolphinView::directorySortingProgress
, m_statusBar
, [this](int percent
) {
164 m_statusBar
->showProgress(i18nc("@info:progress", "Sorting…"), percent
);
166 connect(m_statusBar
, &DolphinStatusBar::stopPressed
, this, &DolphinViewContainer::stopDirectoryLoading
);
167 connect(m_statusBar
, &DolphinStatusBar::zoomLevelChanged
, this, &DolphinViewContainer::slotStatusBarZoomLevelChanged
);
168 connect(m_statusBar
, &DolphinStatusBar::showMessage
, this, &DolphinViewContainer::showMessage
);
170 m_statusBarTimer
= new QTimer(this);
171 m_statusBarTimer
->setSingleShot(true);
172 m_statusBarTimer
->setInterval(300);
173 connect(m_statusBarTimer
, &QTimer::timeout
, this, &DolphinViewContainer::updateStatusBar
);
175 KIO::FileUndoManager
*undoManager
= KIO::FileUndoManager::self();
176 connect(undoManager
, &KIO::FileUndoManager::jobRecordingFinished
, this, &DolphinViewContainer::delayedStatusBarUpdate
);
178 m_topLayout
->addWidget(m_searchBox
, positionFor
.searchBox
, 0);
179 m_topLayout
->addWidget(m_messageWidget
, positionFor
.messageWidget
, 0);
180 m_topLayout
->addWidget(m_view
, positionFor
.view
, 0);
181 m_topLayout
->addWidget(m_filterBar
, positionFor
.filterBar
, 0);
182 m_topLayout
->addWidget(m_statusBar
, positionFor
.statusBar
, 0);
184 setSearchModeEnabled(isSearchUrl(url
));
185 updateAdminBarVisibility(url
);
187 // Update view as the ContentDisplaySettings change
188 // this happens here and not in DolphinView as DolphinviewContainer and DolphinView are not in the same build target ATM
189 connect(ContentDisplaySettings::self(), &KCoreConfigSkeleton::configChanged
, m_view
, &DolphinView::reload
);
191 KFilePlacesModel
*placesModel
= DolphinPlacesModelSingleton::instance().placesModel();
192 connect(placesModel
, &KFilePlacesModel::dataChanged
, this, &DolphinViewContainer::slotPlacesModelChanged
);
193 connect(placesModel
, &KFilePlacesModel::rowsInserted
, this, &DolphinViewContainer::slotPlacesModelChanged
);
194 connect(placesModel
, &KFilePlacesModel::rowsRemoved
, this, &DolphinViewContainer::slotPlacesModelChanged
);
196 connect(this, &DolphinViewContainer::searchModeEnabledChanged
, this, &DolphinViewContainer::captionChanged
);
199 DolphinViewContainer::~DolphinViewContainer()
203 QUrl
DolphinViewContainer::url() const
205 return m_view
->url();
208 KFileItem
DolphinViewContainer::rootItem() const
210 return m_view
->rootItem();
213 void DolphinViewContainer::setActive(bool active
)
215 m_searchBox
->setActive(active
);
216 if (m_urlNavigatorConnected
) {
217 m_urlNavigatorConnected
->setActive(active
);
219 m_view
->setActive(active
);
222 bool DolphinViewContainer::isActive() const
224 return m_view
->isActive();
227 void DolphinViewContainer::setAutoGrabFocus(bool grab
)
229 m_autoGrabFocus
= grab
;
232 bool DolphinViewContainer::autoGrabFocus() const
234 return m_autoGrabFocus
;
237 QString
DolphinViewContainer::currentSearchText() const
239 return m_searchBox
->text();
242 const DolphinStatusBar
*DolphinViewContainer::statusBar() const
247 DolphinStatusBar
*DolphinViewContainer::statusBar()
252 const DolphinUrlNavigator
*DolphinViewContainer::urlNavigator() const
254 return m_urlNavigatorConnected
;
257 DolphinUrlNavigator
*DolphinViewContainer::urlNavigator()
259 return m_urlNavigatorConnected
;
262 const DolphinUrlNavigator
*DolphinViewContainer::urlNavigatorInternalWithHistory() const
264 return m_urlNavigator
.get();
267 DolphinUrlNavigator
*DolphinViewContainer::urlNavigatorInternalWithHistory()
269 return m_urlNavigator
.get();
272 const DolphinView
*DolphinViewContainer::view() const
277 DolphinView
*DolphinViewContainer::view()
282 void DolphinViewContainer::connectUrlNavigator(DolphinUrlNavigator
*urlNavigator
)
284 Q_CHECK_PTR(urlNavigator
);
285 Q_ASSERT(!m_urlNavigatorConnected
);
286 Q_ASSERT(m_urlNavigator
.get() != urlNavigator
);
289 urlNavigator
->setLocationUrl(m_view
->url());
290 urlNavigator
->setShowHiddenFolders(m_view
->hiddenFilesShown());
291 urlNavigator
->setSortHiddenFoldersLast(m_view
->sortHiddenLast());
292 if (m_urlNavigatorVisualState
) {
293 urlNavigator
->setVisualState(*m_urlNavigatorVisualState
.get());
294 m_urlNavigatorVisualState
.reset();
296 urlNavigator
->setActive(isActive());
298 // Url changes are still done via m_urlNavigator.
299 connect(urlNavigator
, &DolphinUrlNavigator::urlChanged
, m_urlNavigator
.get(), &DolphinUrlNavigator::setLocationUrl
);
300 connect(urlNavigator
, &DolphinUrlNavigator::urlsDropped
, this, [=](const QUrl
&destination
, QDropEvent
*event
) {
301 m_view
->dropUrls(destination
, event
, urlNavigator
->dropWidget());
303 // Aside from these, only visual things need to be connected.
304 connect(m_view
, &DolphinView::urlChanged
, urlNavigator
, &DolphinUrlNavigator::setLocationUrl
);
305 connect(urlNavigator
, &DolphinUrlNavigator::activated
, this, &DolphinViewContainer::activate
);
307 urlNavigator
->setReadOnlyBadgeVisible(rootItem().isLocalFile() && !rootItem().isWritable());
309 m_urlNavigatorConnected
= urlNavigator
;
312 void DolphinViewContainer::disconnectUrlNavigator()
314 if (!m_urlNavigatorConnected
) {
318 disconnect(m_urlNavigatorConnected
, &DolphinUrlNavigator::urlChanged
, m_urlNavigator
.get(), &DolphinUrlNavigator::setLocationUrl
);
319 disconnect(m_urlNavigatorConnected
, &DolphinUrlNavigator::urlsDropped
, this, nullptr);
320 disconnect(m_view
, &DolphinView::urlChanged
, m_urlNavigatorConnected
, &DolphinUrlNavigator::setLocationUrl
);
321 disconnect(m_urlNavigatorConnected
, &DolphinUrlNavigator::activated
, this, &DolphinViewContainer::activate
);
323 m_urlNavigatorVisualState
= m_urlNavigatorConnected
->visualState();
324 m_urlNavigatorConnected
= nullptr;
327 void DolphinViewContainer::setSelectionModeEnabled(bool enabled
, KActionCollection
*actionCollection
, SelectionMode::BottomBar::Contents bottomBarContents
)
329 const bool wasEnabled
= m_view
->selectionMode();
330 m_view
->setSelectionModeEnabled(enabled
);
334 return; // nothing to do here
336 Q_CHECK_PTR(m_selectionModeTopBar
); // there is no point in disabling selectionMode when it wasn't even enabled once.
337 Q_CHECK_PTR(m_selectionModeBottomBar
);
338 m_selectionModeTopBar
->setVisible(false, WithAnimation
);
339 m_selectionModeBottomBar
->setVisible(false, WithAnimation
);
340 Q_EMIT
selectionModeChanged(false);
342 if (!QApplication::focusWidget() || m_selectionModeTopBar
->isAncestorOf(QApplication::focusWidget())
343 || m_selectionModeBottomBar
->isAncestorOf(QApplication::focusWidget())) {
349 if (!m_selectionModeTopBar
) {
350 // Changing the location will disable selection mode.
351 connect(m_urlNavigator
.get(), &DolphinUrlNavigator::urlChanged
, this, [this]() {
352 setSelectionModeEnabled(false);
355 m_selectionModeTopBar
= new SelectionMode::TopBar(this); // will be created hidden
356 connect(m_selectionModeTopBar
, &SelectionMode::TopBar::selectionModeLeavingRequested
, this, [this]() {
357 setSelectionModeEnabled(false);
359 m_topLayout
->addWidget(m_selectionModeTopBar
, positionFor
.selectionModeTopBar
, 0);
362 if (!m_selectionModeBottomBar
) {
363 m_selectionModeBottomBar
= new SelectionMode::BottomBar(actionCollection
, this);
364 connect(m_view
, &DolphinView::selectionChanged
, this, [this](const KFileItemList
&selection
) {
365 m_selectionModeBottomBar
->slotSelectionChanged(selection
, m_view
->url());
367 connect(m_selectionModeBottomBar
, &SelectionMode::BottomBar::error
, this, &DolphinViewContainer::showErrorMessage
);
368 connect(m_selectionModeBottomBar
, &SelectionMode::BottomBar::selectionModeLeavingRequested
, this, [this]() {
369 setSelectionModeEnabled(false);
371 m_topLayout
->addWidget(m_selectionModeBottomBar
, positionFor
.selectionModeBottomBar
, 0);
373 m_selectionModeBottomBar
->resetContents(bottomBarContents
);
374 if (bottomBarContents
== SelectionMode::BottomBar::GeneralContents
) {
375 m_selectionModeBottomBar
->slotSelectionChanged(m_view
->selectedItems(), m_view
->url());
379 m_selectionModeTopBar
->setVisible(true, WithAnimation
);
380 m_selectionModeBottomBar
->setVisible(true, WithAnimation
);
381 Q_EMIT
selectionModeChanged(true);
385 bool DolphinViewContainer::isSelectionModeEnabled() const
387 const bool isEnabled
= m_view
->selectionMode();
389 // We can't assert that the bars are invisible only because the selection mode is disabled because the hide animation might still be playing.
390 && (!m_selectionModeBottomBar
|| !m_selectionModeBottomBar
->isEnabled() || !m_selectionModeBottomBar
->isVisible()
391 || m_selectionModeBottomBar
->contents() == SelectionMode::BottomBar::PasteContents
))
392 || (isEnabled
&& m_selectionModeTopBar
393 && m_selectionModeTopBar
->isVisible()
394 // 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.
395 && m_selectionModeBottomBar
396 && (m_selectionModeBottomBar
->isVisible() || m_selectionModeBottomBar
->contents() == SelectionMode::BottomBar::GeneralContents
)));
400 void DolphinViewContainer::slotSplitTabDisabled()
402 if (m_selectionModeBottomBar
) {
403 m_selectionModeBottomBar
->slotSplitTabDisabled();
407 void DolphinViewContainer::showMessage(const QString
&message
, KMessageWidget::MessageType messageType
)
409 if (message
.isEmpty()) {
413 m_messageWidget
->setText(message
);
415 // TODO: wrap at arbitrary character positions once QLabel can do this
416 // https://bugreports.qt.io/browse/QTBUG-1276
417 m_messageWidget
->setWordWrap(true);
418 m_messageWidget
->setMessageType(messageType
);
420 m_messageWidget
->setWordWrap(false);
421 const int unwrappedWidth
= m_messageWidget
->sizeHint().width();
422 m_messageWidget
->setWordWrap(unwrappedWidth
> size().width());
424 if (m_messageWidget
->isVisible()) {
425 m_messageWidget
->hide();
427 m_messageWidget
->animatedShow();
430 void DolphinViewContainer::readSettings()
432 // The startup settings should (only) get applied if they have been
433 // modified by the user. Otherwise keep the (possibly) different current
434 // setting of the filterbar.
435 if (GeneralSettings::modifiedStartupSettings()) {
436 setFilterBarVisible(GeneralSettings::filterBar());
439 m_view
->readSettings();
440 m_statusBar
->readSettings();
443 bool DolphinViewContainer::isFilterBarVisible() const
445 return m_filterBar
->isEnabled(); // Gets disabled in AnimatedHeightWidget while animating towards a hidden state.
448 void DolphinViewContainer::setSearchModeEnabled(bool enabled
)
450 m_searchBox
->setVisible(enabled
, WithAnimation
);
453 const QUrl
&locationUrl
= m_urlNavigator
->locationUrl();
454 m_searchBox
->fromSearchUrl(locationUrl
);
457 if (enabled
== isSearchModeEnabled()) {
458 if (enabled
&& !m_searchBox
->hasFocus()) {
459 m_searchBox
->setFocus();
460 m_searchBox
->selectAll();
466 m_view
->setViewPropertiesContext(QString());
468 // Restore the URL for the URL navigator. If Dolphin has been
469 // started with a search-URL, the home URL is used as fallback.
470 QUrl url
= m_searchBox
->searchPath();
471 if (url
.isEmpty() || !url
.isValid() || isSearchUrl(url
)) {
472 url
= Dolphin::homeUrl();
474 m_urlNavigatorConnected
->setLocationUrl(url
);
477 m_searchModeEnabled
= enabled
;
479 Q_EMIT
searchModeEnabledChanged(enabled
);
482 bool DolphinViewContainer::isSearchModeEnabled() const
484 return m_searchModeEnabled
;
487 QString
DolphinViewContainer::placesText() const
491 if (isSearchModeEnabled()) {
492 text
= i18n("Search for %1 in %2", m_searchBox
->text(), m_searchBox
->searchPath().fileName());
494 text
= url().adjusted(QUrl::StripTrailingSlash
).fileName();
495 if (text
.isEmpty()) {
498 if (text
.isEmpty()) {
499 text
= url().scheme();
506 void DolphinViewContainer::reload()
509 m_messageWidget
->hide();
512 QString
DolphinViewContainer::captionWindowTitle() const
514 if (GeneralSettings::showFullPathInTitlebar() && !isSearchModeEnabled()) {
515 if (!url().isLocalFile()) {
516 return url().adjusted(QUrl::StripTrailingSlash
).toString();
518 return url().adjusted(QUrl::StripTrailingSlash
).path();
520 return DolphinViewContainer::caption();
524 QString
DolphinViewContainer::caption() const
526 if (isSearchModeEnabled()) {
527 if (currentSearchText().isEmpty()) {
528 return i18n("Search");
530 return i18n("Search for %1", currentSearchText());
534 KFilePlacesModel
*placesModel
= DolphinPlacesModelSingleton::instance().placesModel();
535 const QString pattern
= url().adjusted(QUrl::StripTrailingSlash
).toString(QUrl::FullyEncoded
).append("/?");
536 const auto &matchedPlaces
=
537 placesModel
->match(placesModel
->index(0, 0), KFilePlacesModel::UrlRole
, QRegularExpression::anchoredPattern(pattern
), 1, Qt::MatchRegularExpression
);
539 if (!matchedPlaces
.isEmpty()) {
540 return placesModel
->text(matchedPlaces
.first());
543 if (!url().isLocalFile()) {
544 QUrl adjustedUrl
= url().adjusted(QUrl::StripTrailingSlash
);
546 if (!adjustedUrl
.fileName().isEmpty()) {
547 caption
= adjustedUrl
.fileName();
548 } else if (!adjustedUrl
.path().isEmpty() && adjustedUrl
.path() != "/") {
549 caption
= adjustedUrl
.path();
550 } else if (!adjustedUrl
.host().isEmpty()) {
551 caption
= adjustedUrl
.host();
553 caption
= adjustedUrl
.toString();
558 QString fileName
= url().adjusted(QUrl::StripTrailingSlash
).fileName();
559 if (fileName
.isEmpty()) {
566 void DolphinViewContainer::setUrl(const QUrl
&newUrl
)
568 if (newUrl
!= m_urlNavigator
->locationUrl()) {
569 m_urlNavigator
->setLocationUrl(newUrl
);
573 void DolphinViewContainer::setFilterBarVisible(bool visible
)
575 Q_ASSERT(m_filterBar
);
577 m_view
->hideToolTip(ToolTipManager::HideBehavior::Instantly
);
578 m_filterBar
->setVisible(true, WithAnimation
);
579 m_filterBar
->setFocus();
580 m_filterBar
->selectAll();
586 void DolphinViewContainer::delayedStatusBarUpdate()
588 if (m_statusBarTimer
->isActive() && (m_statusBarTimestamp
.elapsed() > 2000)) {
589 // No update of the statusbar has been done during the last 2 seconds,
590 // although an update has been requested. Trigger an immediate update.
591 m_statusBarTimer
->stop();
594 // Invoke updateStatusBar() with a small delay. This assures that
595 // when a lot of delayedStatusBarUpdates() are done in a short time,
596 // no bottleneck is given.
597 m_statusBarTimer
->start();
601 void DolphinViewContainer::updateStatusBar()
603 m_statusBarTimestamp
.start();
604 m_view
->requestStatusBarText();
607 void DolphinViewContainer::slotDirectoryLoadingStarted()
609 if (isSearchUrl(url())) {
610 // Search KIO-slaves usually don't provide any progress information. Give
611 // a hint to the user that a searching is done:
613 m_statusBar
->showProgress(i18nc("@info", "Searching…"), -1);
615 // Trigger an undetermined progress indication. The progress
616 // information in percent will be triggered by the percent() signal
617 // of the directory lister later.
618 m_statusBar
->showProgress(QString(), -1);
621 if (m_urlNavigatorConnected
) {
622 m_urlNavigatorConnected
->setReadOnlyBadgeVisible(false);
626 void DolphinViewContainer::slotDirectoryLoadingCompleted()
628 m_statusBar
->showProgress(QString(), 100);
630 if (isSearchUrl(url()) && m_view
->itemsCount() == 0) {
631 // The dir lister has been completed on a Baloo-URI and no items have been found. Instead
632 // of showing the default status bar information ("0 items") a more helpful information is given:
633 m_statusBar
->setText(i18nc("@info:status", "No items found."));
638 if (m_urlNavigatorConnected
) {
639 m_urlNavigatorConnected
->setReadOnlyBadgeVisible(rootItem().isLocalFile() && !rootItem().isWritable());
643 void DolphinViewContainer::slotDirectoryLoadingCanceled()
645 m_statusBar
->showProgress(QString(), 100);
646 m_statusBar
->setText(QString());
649 void DolphinViewContainer::slotUrlIsFileError(const QUrl
&url
)
651 const KFileItem
item(url
);
653 // Find out if the file can be opened in the view (for example, this is the
654 // case if the file is an archive). The mime type must be known for that.
655 item
.determineMimeType();
656 const QUrl
&folderUrl
= DolphinView::openItemAsFolderUrl(item
, true);
657 if (!folderUrl
.isEmpty()) {
660 slotItemActivated(item
);
664 void DolphinViewContainer::slotItemActivated(const KFileItem
&item
)
666 // It is possible to activate items on inactive views by
667 // drag & drop operations. Assure that activating an item always
668 // results in an active view.
669 m_view
->setActive(true);
671 const QUrl
&url
= DolphinView::openItemAsFolderUrl(item
, GeneralSettings::browseThroughArchives());
672 if (!url
.isEmpty()) {
673 const auto modifiers
= QGuiApplication::keyboardModifiers();
674 // keep in sync with KUrlNavigator::slotNavigatorButtonClicked
675 if (modifiers
& Qt::ControlModifier
&& modifiers
& Qt::ShiftModifier
) {
676 Q_EMIT
activeTabRequested(url
);
677 } else if (modifiers
& Qt::ControlModifier
) {
678 Q_EMIT
tabRequested(url
);
679 } else if (modifiers
& Qt::ShiftModifier
) {
680 Dolphin::openNewWindow({KFilePlacesModel::convertedUrl(url
)}, this);
687 KIO::OpenUrlJob
*job
= new KIO::OpenUrlJob(item
.targetUrl(), item
.mimetype());
688 // Auto*Warning*Handling, errors are put in a KMessageWidget by us in slotOpenUrlFinished.
689 job
->setUiDelegate(KIO::createDefaultJobUiDelegate(KJobUiDelegate::AutoWarningHandlingEnabled
, this));
690 job
->setShowOpenOrExecuteDialog(true);
691 connect(job
, &KIO::OpenUrlJob::finished
, this, &DolphinViewContainer::slotOpenUrlFinished
);
695 void DolphinViewContainer::slotfileMiddleClickActivated(const KFileItem
&item
)
697 KService::List services
= KApplicationTrader::queryByMimeType(item
.mimetype());
699 int indexOfAppToOpenFileWith
= 1;
701 // executable scripts
702 auto mimeType
= item
.currentMimeType();
703 if (item
.isLocalFile() && mimeType
.inherits(QStringLiteral("application/x-executable")) && mimeType
.inherits(QStringLiteral("text/plain"))
704 && QFileInfo(item
.localPath()).isExecutable()) {
705 KConfigGroup
cfgGroup(KSharedConfig::openConfig(QStringLiteral("kiorc")), QStringLiteral("Executable scripts"));
706 const QString value
= cfgGroup
.readEntry("behaviourOnLaunch", "alwaysAsk");
708 // in case KIO::WidgetsOpenOrExecuteFileHandler::promptUserOpenOrExecute would not open the file
709 if (value
!= QLatin1String("open")) {
710 indexOfAppToOpenFileWith
= 0;
714 if (services
.length() >= indexOfAppToOpenFileWith
+ 1) {
715 auto service
= services
.at(indexOfAppToOpenFileWith
);
717 KIO::ApplicationLauncherJob
*job
= new KIO::ApplicationLauncherJob(service
, this);
718 job
->setUrls({item
.url()});
720 job
->setUiDelegate(KIO::createDefaultJobUiDelegate(KJobUiDelegate::AutoHandlingEnabled
, this));
721 connect(job
, &KIO::OpenUrlJob::finished
, this, &DolphinViewContainer::slotOpenUrlFinished
);
726 void DolphinViewContainer::slotItemsActivated(const KFileItemList
&items
)
728 Q_ASSERT(items
.count() >= 2);
730 KFileItemActions
fileItemActions(this);
731 fileItemActions
.runPreferredApplications(items
);
734 void DolphinViewContainer::showItemInfo(const KFileItem
&item
)
737 m_statusBar
->resetToDefaultText();
739 m_statusBar
->setText(item
.getStatusBarInfo());
743 void DolphinViewContainer::updateAdminBarVisibility(const QUrl
&url
)
745 if (url
.scheme() == QStringLiteral("admin")) {
747 m_adminBar
= new Admin::Bar(this);
748 m_topLayout
->addWidget(m_adminBar
, positionFor
.adminBar
, 0);
749 connect(m_adminBar
, &Admin::Bar::activated
, this, &DolphinViewContainer::activate
);
751 m_adminBar
->setVisible(true, WithAnimation
);
752 } else if (m_adminBar
) {
753 m_adminBar
->setVisible(false, WithAnimation
);
757 void DolphinViewContainer::closeFilterBar()
759 m_filterBar
->closeFilterBar();
761 Q_EMIT
showFilterBarChanged(false);
764 void DolphinViewContainer::clearFilterBar()
766 m_filterBar
->clearIfUnlocked();
769 void DolphinViewContainer::setNameFilter(const QString
&nameFilter
)
771 m_view
->hideToolTip(ToolTipManager::HideBehavior::Instantly
);
772 m_view
->setNameFilter(nameFilter
);
773 delayedStatusBarUpdate();
776 void DolphinViewContainer::activate()
781 void DolphinViewContainer::slotUrlNavigatorLocationAboutToBeChanged(const QUrl
&)
786 void DolphinViewContainer::slotUrlNavigatorLocationChanged(const QUrl
&url
)
788 if (m_urlNavigatorConnected
) {
789 m_urlNavigatorConnected
->slotReturnPressed();
792 if (KProtocolManager::supportsListing(url
)) {
793 const bool searchBoxInitialized
= isSearchModeEnabled() && m_searchBox
->text().isEmpty();
794 setSearchModeEnabled(isSearchUrl(url
) || searchBoxInitialized
);
797 tryRestoreViewState();
799 if (m_autoGrabFocus
&& isActive() && !isSearchModeEnabled()) {
800 // When an URL has been entered, the view should get the focus.
801 // The focus must be requested asynchronously, as changing the URL might create
802 // a new view widget.
803 QTimer::singleShot(0, this, &DolphinViewContainer::requestFocus
);
805 } else if (KProtocolManager::isSourceProtocol(url
)) {
806 if (url
.scheme().startsWith(QLatin1String("http"))) {
807 showMessage(i18nc("@info:status", // krazy:exclude=qmethods
808 "Dolphin does not support web pages, the web browser has been launched"),
809 KMessageWidget::Information
);
811 showMessage(i18nc("@info:status", "Protocol not supported by Dolphin, default application has been launched"), KMessageWidget::Information
);
814 QDesktopServices::openUrl(url
);
815 redirect(QUrl(), m_urlNavigator
->locationUrl(1));
817 if (!url
.scheme().isEmpty()) {
818 showMessage(i18nc("@info:status", "Invalid protocol '%1'", url
.scheme()), KMessageWidget::Error
);
820 showMessage(i18nc("@info:status", "Invalid protocol"), KMessageWidget::Error
);
822 m_urlNavigator
->goBack();
826 void DolphinViewContainer::slotUrlSelectionRequested(const QUrl
&url
)
828 // 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
830 m_view
->markUrlAsCurrent(url
); // makes the item scroll into view
833 void DolphinViewContainer::disableUrlNavigatorSelectionRequests()
835 disconnect(m_urlNavigator
.get(), &KUrlNavigator::urlSelectionRequested
, this, &DolphinViewContainer::slotUrlSelectionRequested
);
838 void DolphinViewContainer::enableUrlNavigatorSelectionRequests()
840 connect(m_urlNavigator
.get(), &KUrlNavigator::urlSelectionRequested
, this, &DolphinViewContainer::slotUrlSelectionRequested
);
843 void DolphinViewContainer::redirect(const QUrl
&oldUrl
, const QUrl
&newUrl
)
846 const bool block
= m_urlNavigator
->signalsBlocked();
847 m_urlNavigator
->blockSignals(true);
849 // Assure that the location state is reset for redirection URLs. This
850 // allows to skip redirection URLs when going back or forward in the
852 m_urlNavigator
->saveLocationState(QByteArray());
853 m_urlNavigator
->setLocationUrl(newUrl
);
854 setSearchModeEnabled(isSearchUrl(newUrl
));
856 m_urlNavigator
->blockSignals(block
);
859 void DolphinViewContainer::requestFocus()
864 void DolphinViewContainer::startSearching()
866 Q_CHECK_PTR(m_urlNavigatorConnected
);
867 const QUrl url
= m_searchBox
->urlForSearching();
868 if (url
.isValid() && !url
.isEmpty()) {
869 m_view
->setViewPropertiesContext(QStringLiteral("search"));
870 m_urlNavigatorConnected
->setLocationUrl(url
);
874 void DolphinViewContainer::openSearchBox()
876 setSearchModeEnabled(true);
879 void DolphinViewContainer::closeSearchBox()
881 setSearchModeEnabled(false);
884 void DolphinViewContainer::stopDirectoryLoading()
886 m_view
->stopLoading();
887 m_statusBar
->showProgress(QString(), 100);
890 void DolphinViewContainer::slotStatusBarZoomLevelChanged(int zoomLevel
)
892 m_view
->setZoomLevel(zoomLevel
);
895 void DolphinViewContainer::showErrorMessage(const QString
&message
)
897 showMessage(message
, KMessageWidget::Error
);
900 void DolphinViewContainer::slotPlacesModelChanged()
902 if (!GeneralSettings::showFullPathInTitlebar() && !isSearchModeEnabled()) {
903 Q_EMIT
captionChanged();
907 void DolphinViewContainer::slotHiddenFilesShownChanged(bool showHiddenFiles
)
909 if (m_urlNavigatorConnected
) {
910 m_urlNavigatorConnected
->setShowHiddenFolders(showHiddenFiles
);
914 void DolphinViewContainer::slotSortHiddenLastChanged(bool hiddenLast
)
916 if (m_urlNavigatorConnected
) {
917 m_urlNavigatorConnected
->setSortHiddenFoldersLast(hiddenLast
);
921 void DolphinViewContainer::slotCurrentDirectoryRemoved()
923 const QString
location(url().toDisplayString(QUrl::PreferLocalFile
));
924 if (url().isLocalFile()) {
925 const QString dirPath
= url().toLocalFile();
926 const QString newPath
= getNearestExistingAncestorOfPath(dirPath
);
927 const QUrl newUrl
= QUrl::fromLocalFile(newPath
);
931 showMessage(xi18n("Current location changed, <filename>%1</filename> is no longer accessible.", location
), KMessageWidget::Warning
);
934 void DolphinViewContainer::slotOpenUrlFinished(KJob
*job
)
936 if (job
->error() && job
->error() != KIO::ERR_USER_CANCELED
) {
937 showErrorMessage(job
->errorString());
941 bool DolphinViewContainer::isSearchUrl(const QUrl
&url
) const
943 return url
.scheme().contains(QLatin1String("search"));
946 void DolphinViewContainer::saveViewState()
948 QByteArray locationState
;
949 QDataStream
stream(&locationState
, QIODevice::WriteOnly
);
950 m_view
->saveState(stream
);
951 m_urlNavigator
->saveLocationState(locationState
);
954 void DolphinViewContainer::tryRestoreViewState()
956 QByteArray locationState
= m_urlNavigator
->locationState();
957 if (!locationState
.isEmpty()) {
958 QDataStream
stream(&locationState
, QIODevice::ReadOnly
);
959 m_view
->restoreState(stream
);
963 QString
DolphinViewContainer::getNearestExistingAncestorOfPath(const QString
&path
) const
967 dir
.setPath(QDir::cleanPath(dir
.filePath(QStringLiteral(".."))));
968 } while (!dir
.exists() && !dir
.isRoot());
970 return dir
.exists() ? dir
.path() : QString
{};
973 #include "moc_dolphinviewcontainer.cpp"