2 * SPDX-FileCopyrightText: 2007 Peter Penz <peter.penz19@gmail.com>
4 * SPDX-License-Identifier: GPL-2.0-or-later
7 #include "dolphinviewcontainer.h"
9 #include "dolphin_compactmodesettings.h"
10 #include "dolphin_contentdisplaysettings.h"
11 #include "dolphin_detailsmodesettings.h"
12 #include "dolphin_generalsettings.h"
13 #include "dolphin_iconsmodesettings.h"
14 #include "dolphindebug.h"
15 #include "dolphinplacesmodelsingleton.h"
16 #include "filterbar/filterbar.h"
18 #include "search/dolphinsearchbox.h"
19 #include "selectionmode/topbar.h"
20 #include "statusbar/dolphinstatusbar.h"
22 #include <KActionCollection>
24 #include <KActivities/ResourceInstance>
26 #include <KFileItemActions>
27 #include <KFilePlacesModel>
28 #include <kio_version.h>
29 #if KIO_VERSION >= QT_VERSION_CHECK(5, 98, 0)
30 #include <KIO/JobUiDelegateFactory>
32 #include <KIO/JobUiDelegate>
34 #include <KIO/OpenUrlJob>
35 #include <KLocalizedString>
36 #include <KMessageWidget>
37 #include <KProtocolManager>
40 #include <QApplication>
41 #include <QDesktopServices>
43 #include <QGridLayout>
44 #include <QGuiApplication>
45 #include <QRegularExpression>
49 // An overview of the widgets contained by this ViewContainer
50 struct LayoutStructure
{
52 int messageWidget
= 1;
53 int selectionModeTopBar
= 2;
55 int selectionModeBottomBar
= 4;
59 constexpr LayoutStructure positionFor
;
61 DolphinViewContainer::DolphinViewContainer(const QUrl
&url
, QWidget
*parent
)
63 , m_topLayout(nullptr)
64 , m_urlNavigator
{new DolphinUrlNavigator(url
)}
65 , m_urlNavigatorConnected
{nullptr}
66 , m_searchBox(nullptr)
67 , m_searchModeEnabled(false)
68 , m_messageWidget(nullptr)
69 , m_selectionModeTopBar
{nullptr}
71 , m_filterBar(nullptr)
72 , m_selectionModeBottomBar
{nullptr}
73 , m_statusBar(nullptr)
74 , m_statusBarTimer(nullptr)
75 , m_statusBarTimestamp()
76 , m_autoGrabFocus(true)
78 , m_activityResourceInstance(nullptr)
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);
89 connect(m_searchBox
, &DolphinSearchBox::activated
, this, &DolphinViewContainer::activate
);
90 connect(m_searchBox
, &DolphinSearchBox::closeRequest
, this, &DolphinViewContainer::closeSearchBox
);
91 connect(m_searchBox
, &DolphinSearchBox::searchRequest
, this, &DolphinViewContainer::startSearching
);
92 connect(m_searchBox
, &DolphinSearchBox::focusViewRequest
, this, &DolphinViewContainer::requestFocus
);
93 m_searchBox
->setWhatsThis(xi18nc("@info:whatsthis findbar",
94 "<para>This helps you find files and folders. Enter a <emphasis>"
95 "search term</emphasis> and specify search settings with the "
96 "buttons at the bottom:<list><item>Filename/Content: "
97 "Does the item you are looking for contain the search terms "
98 "within its filename or its contents?<nl/>The contents of images, "
99 "audio files and videos will not be searched.</item><item>"
100 "From Here/Everywhere: Do you want to search in this "
101 "folder and its sub-folders or everywhere?</item><item>"
102 "More Options: Click this to search by media type, access "
103 "time or rating.</item><item>More Search Tools: Install other "
104 "means to find an item.</item></list></para>"));
106 m_messageWidget
= new KMessageWidget(this);
107 m_messageWidget
->setCloseButtonVisible(true);
108 m_messageWidget
->hide();
112 // We must be logged in as the root user; show a big scary warning
113 showMessage(i18n("Running Dolphin as root can be dangerous. Please be careful."), Warning
);
117 // Initialize filter bar
118 m_filterBar
= new FilterBar(this);
119 m_filterBar
->setVisible(GeneralSettings::filterBar());
121 connect(m_filterBar
, &FilterBar::filterChanged
, this, &DolphinViewContainer::setNameFilter
);
122 connect(m_filterBar
, &FilterBar::closeRequest
, this, &DolphinViewContainer::closeFilterBar
);
123 connect(m_filterBar
, &FilterBar::focusViewRequest
, this, &DolphinViewContainer::requestFocus
);
125 // Initialize the main view
126 m_view
= new DolphinView(url
, this);
127 connect(m_view
, &DolphinView::urlChanged
, m_filterBar
, &FilterBar::clearIfUnlocked
);
128 connect(m_view
, &DolphinView::urlChanged
, m_messageWidget
, &KMessageWidget::hide
);
129 // m_urlNavigator stays in sync with m_view's location changes and
130 // keeps track of them so going back and forth in the history works.
131 connect(m_view
, &DolphinView::urlChanged
, m_urlNavigator
.get(), &DolphinUrlNavigator::setLocationUrl
);
132 connect(m_urlNavigator
.get(), &DolphinUrlNavigator::urlChanged
, this, &DolphinViewContainer::slotUrlNavigatorLocationChanged
);
133 connect(m_urlNavigator
.get(), &DolphinUrlNavigator::urlAboutToBeChanged
, this, &DolphinViewContainer::slotUrlNavigatorLocationAboutToBeChanged
);
134 connect(m_urlNavigator
.get(), &DolphinUrlNavigator::urlSelectionRequested
, this, &DolphinViewContainer::slotUrlSelectionRequested
);
135 connect(m_view
, &DolphinView::writeStateChanged
, this, &DolphinViewContainer::writeStateChanged
);
136 connect(m_view
, &DolphinView::requestItemInfo
, this, &DolphinViewContainer::showItemInfo
);
137 connect(m_view
, &DolphinView::itemActivated
, this, &DolphinViewContainer::slotItemActivated
);
138 connect(m_view
, &DolphinView::itemsActivated
, this, &DolphinViewContainer::slotItemsActivated
);
139 connect(m_view
, &DolphinView::redirection
, this, &DolphinViewContainer::redirect
);
140 connect(m_view
, &DolphinView::directoryLoadingStarted
, this, &DolphinViewContainer::slotDirectoryLoadingStarted
);
141 connect(m_view
, &DolphinView::directoryLoadingCompleted
, this, &DolphinViewContainer::slotDirectoryLoadingCompleted
);
142 connect(m_view
, &DolphinView::directoryLoadingCanceled
, this, &DolphinViewContainer::slotDirectoryLoadingCanceled
);
143 connect(m_view
, &DolphinView::itemCountChanged
, this, &DolphinViewContainer::delayedStatusBarUpdate
);
144 connect(m_view
, &DolphinView::directoryLoadingProgress
, this, &DolphinViewContainer::updateDirectoryLoadingProgress
);
145 connect(m_view
, &DolphinView::directorySortingProgress
, this, &DolphinViewContainer::updateDirectorySortingProgress
);
146 connect(m_view
, &DolphinView::selectionChanged
, this, &DolphinViewContainer::delayedStatusBarUpdate
);
147 connect(m_view
, &DolphinView::errorMessage
, this, &DolphinViewContainer::showErrorMessage
);
148 connect(m_view
, &DolphinView::urlIsFileError
, this, &DolphinViewContainer::slotUrlIsFileError
);
149 connect(m_view
, &DolphinView::activated
, this, &DolphinViewContainer::activate
);
150 connect(m_view
, &DolphinView::hiddenFilesShownChanged
, this, &DolphinViewContainer::slotHiddenFilesShownChanged
);
151 connect(m_view
, &DolphinView::sortHiddenLastChanged
, this, &DolphinViewContainer::slotSortHiddenLastChanged
);
152 connect(m_view
, &DolphinView::currentDirectoryRemoved
, this, &DolphinViewContainer::slotCurrentDirectoryRemoved
);
154 // Initialize status bar
155 m_statusBar
= new DolphinStatusBar(this);
156 m_statusBar
->setUrl(m_view
->url());
157 m_statusBar
->setZoomLevel(m_view
->zoomLevel());
158 connect(m_view
, &DolphinView::urlChanged
, m_statusBar
, &DolphinStatusBar::setUrl
);
159 connect(m_view
, &DolphinView::zoomLevelChanged
, m_statusBar
, &DolphinStatusBar::setZoomLevel
);
160 connect(m_view
, &DolphinView::infoMessage
, m_statusBar
, &DolphinStatusBar::setText
);
161 connect(m_view
, &DolphinView::operationCompletedMessage
, m_statusBar
, &DolphinStatusBar::setText
);
162 connect(m_view
, &DolphinView::statusBarTextChanged
, m_statusBar
, &DolphinStatusBar::setDefaultText
);
163 connect(m_view
, &DolphinView::statusBarTextChanged
, m_statusBar
, &DolphinStatusBar::resetToDefaultText
);
164 connect(m_statusBar
, &DolphinStatusBar::stopPressed
, this, &DolphinViewContainer::stopDirectoryLoading
);
165 connect(m_statusBar
, &DolphinStatusBar::zoomLevelChanged
, this, &DolphinViewContainer::slotStatusBarZoomLevelChanged
);
167 m_statusBarTimer
= new QTimer(this);
168 m_statusBarTimer
->setSingleShot(true);
169 m_statusBarTimer
->setInterval(300);
170 connect(m_statusBarTimer
, &QTimer::timeout
, this, &DolphinViewContainer::updateStatusBar
);
172 KIO::FileUndoManager
*undoManager
= KIO::FileUndoManager::self();
173 connect(undoManager
, &KIO::FileUndoManager::jobRecordingFinished
, this, &DolphinViewContainer::delayedStatusBarUpdate
);
175 m_topLayout
->addWidget(m_searchBox
, positionFor
.searchBox
, 0);
176 m_topLayout
->addWidget(m_messageWidget
, positionFor
.messageWidget
, 0);
177 m_topLayout
->addWidget(m_view
, positionFor
.view
, 0);
178 m_topLayout
->addWidget(m_filterBar
, positionFor
.filterBar
, 0);
179 m_topLayout
->addWidget(m_statusBar
, positionFor
.statusBar
, 0);
181 setSearchModeEnabled(isSearchUrl(url
));
183 // Update view as the ContentDisplaySettings change
184 // this happens here and not in DolphinView as DolphinviewContainer and DolphinView are not in the same build target ATM
185 connect(ContentDisplaySettings::self(), &KCoreConfigSkeleton::configChanged
, m_view
, &DolphinView::reload
);
187 KFilePlacesModel
*placesModel
= DolphinPlacesModelSingleton::instance().placesModel();
188 connect(placesModel
, &KFilePlacesModel::dataChanged
, this, &DolphinViewContainer::slotPlacesModelChanged
);
189 connect(placesModel
, &KFilePlacesModel::rowsInserted
, this, &DolphinViewContainer::slotPlacesModelChanged
);
190 connect(placesModel
, &KFilePlacesModel::rowsRemoved
, this, &DolphinViewContainer::slotPlacesModelChanged
);
192 connect(this, &DolphinViewContainer::searchModeEnabledChanged
, this, &DolphinViewContainer::captionChanged
);
194 // Initialize kactivities resource instance
197 m_activityResourceInstance
= new KActivities::ResourceInstance(window()->winId(), url
);
198 m_activityResourceInstance
->setParent(this);
202 DolphinViewContainer::~DolphinViewContainer()
206 QUrl
DolphinViewContainer::url() const
208 return m_view
->url();
211 KFileItem
DolphinViewContainer::rootItem() const
213 return m_view
->rootItem();
216 void DolphinViewContainer::setActive(bool active
)
218 m_searchBox
->setActive(active
);
219 if (m_urlNavigatorConnected
) {
220 m_urlNavigatorConnected
->setActive(active
);
222 m_view
->setActive(active
);
226 m_activityResourceInstance
->notifyFocusedIn();
228 m_activityResourceInstance
->notifyFocusedOut();
233 bool DolphinViewContainer::isActive() const
235 return m_view
->isActive();
238 void DolphinViewContainer::setAutoGrabFocus(bool grab
)
240 m_autoGrabFocus
= grab
;
243 bool DolphinViewContainer::autoGrabFocus() const
245 return m_autoGrabFocus
;
248 QString
DolphinViewContainer::currentSearchText() const
250 return m_searchBox
->text();
253 const DolphinStatusBar
*DolphinViewContainer::statusBar() const
258 DolphinStatusBar
*DolphinViewContainer::statusBar()
263 const DolphinUrlNavigator
*DolphinViewContainer::urlNavigator() const
265 return m_urlNavigatorConnected
;
268 DolphinUrlNavigator
*DolphinViewContainer::urlNavigator()
270 return m_urlNavigatorConnected
;
273 const DolphinUrlNavigator
*DolphinViewContainer::urlNavigatorInternalWithHistory() const
275 return m_urlNavigator
.get();
278 DolphinUrlNavigator
*DolphinViewContainer::urlNavigatorInternalWithHistory()
280 return m_urlNavigator
.get();
283 const DolphinView
*DolphinViewContainer::view() const
288 DolphinView
*DolphinViewContainer::view()
293 void DolphinViewContainer::connectUrlNavigator(DolphinUrlNavigator
*urlNavigator
)
295 Q_CHECK_PTR(urlNavigator
);
296 Q_ASSERT(!m_urlNavigatorConnected
);
297 Q_ASSERT(m_urlNavigator
.get() != urlNavigator
);
300 urlNavigator
->setLocationUrl(m_view
->url());
301 urlNavigator
->setShowHiddenFolders(m_view
->hiddenFilesShown());
302 urlNavigator
->setSortHiddenFoldersLast(m_view
->sortHiddenLast());
303 if (m_urlNavigatorVisualState
) {
304 urlNavigator
->setVisualState(*m_urlNavigatorVisualState
.get());
305 m_urlNavigatorVisualState
.reset();
307 urlNavigator
->setActive(isActive());
309 // Url changes are still done via m_urlNavigator.
310 connect(urlNavigator
, &DolphinUrlNavigator::urlChanged
, m_urlNavigator
.get(), &DolphinUrlNavigator::setLocationUrl
);
311 connect(urlNavigator
, &DolphinUrlNavigator::urlsDropped
, this, [=](const QUrl
&destination
, QDropEvent
*event
) {
312 m_view
->dropUrls(destination
, event
, urlNavigator
->dropWidget());
314 // Aside from these, only visual things need to be connected.
315 connect(m_view
, &DolphinView::urlChanged
, urlNavigator
, &DolphinUrlNavigator::setLocationUrl
);
316 connect(urlNavigator
, &DolphinUrlNavigator::activated
, this, &DolphinViewContainer::activate
);
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
);
332 m_urlNavigatorVisualState
= m_urlNavigatorConnected
->visualState();
333 m_urlNavigatorConnected
= nullptr;
336 void DolphinViewContainer::setSelectionModeEnabled(bool enabled
, KActionCollection
*actionCollection
, SelectionMode::BottomBar::Contents bottomBarContents
)
338 const bool wasEnabled
= m_view
->selectionMode();
339 m_view
->setSelectionModeEnabled(enabled
);
343 return; // nothing to do here
345 Q_CHECK_PTR(m_selectionModeTopBar
); // there is no point in disabling selectionMode when it wasn't even enabled once.
346 Q_CHECK_PTR(m_selectionModeBottomBar
);
347 if (m_selectionModeTopBar
->isAncestorOf(QApplication::focusWidget()) || m_selectionModeBottomBar
->isAncestorOf(QApplication::focusWidget())) {
350 m_selectionModeTopBar
->setVisible(false, WithAnimation
);
351 m_selectionModeBottomBar
->setVisible(false, WithAnimation
);
352 Q_EMIT
selectionModeChanged(false);
356 if (!m_selectionModeTopBar
) {
357 // Changing the location will disable selection mode.
358 connect(m_urlNavigator
.get(), &DolphinUrlNavigator::urlChanged
, this, [this]() {
359 setSelectionModeEnabled(false);
362 m_selectionModeTopBar
= new SelectionMode::TopBar(this); // will be created hidden
363 connect(m_selectionModeTopBar
, &SelectionMode::TopBar::selectionModeLeavingRequested
, this, [this]() {
364 setSelectionModeEnabled(false);
366 m_topLayout
->addWidget(m_selectionModeTopBar
, positionFor
.selectionModeTopBar
, 0);
369 if (!m_selectionModeBottomBar
) {
370 m_selectionModeBottomBar
= new SelectionMode::BottomBar(actionCollection
, this);
371 connect(m_view
, &DolphinView::selectionChanged
, this, [this](const KFileItemList
&selection
) {
372 m_selectionModeBottomBar
->slotSelectionChanged(selection
, m_view
->url());
374 connect(m_selectionModeBottomBar
, &SelectionMode::BottomBar::error
, this, [this](const QString
&errorMessage
) {
375 showErrorMessage(errorMessage
);
377 connect(m_selectionModeBottomBar
, &SelectionMode::BottomBar::selectionModeLeavingRequested
, this, [this]() {
378 setSelectionModeEnabled(false);
380 m_topLayout
->addWidget(m_selectionModeBottomBar
, positionFor
.selectionModeBottomBar
, 0);
382 m_selectionModeBottomBar
->resetContents(bottomBarContents
);
383 if (bottomBarContents
== SelectionMode::BottomBar::GeneralContents
) {
384 m_selectionModeBottomBar
->slotSelectionChanged(m_view
->selectedItems(), m_view
->url());
388 m_selectionModeTopBar
->setVisible(true, WithAnimation
);
389 m_selectionModeBottomBar
->setVisible(true, WithAnimation
);
390 Q_EMIT
selectionModeChanged(true);
394 bool DolphinViewContainer::isSelectionModeEnabled() const
396 const bool isEnabled
= m_view
->selectionMode();
398 // We can't assert that the bars are invisible only because the selection mode is disabled because the hide animation might still be playing.
399 && (!m_selectionModeBottomBar
|| !m_selectionModeBottomBar
->isEnabled() || !m_selectionModeBottomBar
->isVisible()
400 || m_selectionModeBottomBar
->contents() == SelectionMode::BottomBar::PasteContents
))
401 || (isEnabled
&& m_selectionModeTopBar
402 && m_selectionModeTopBar
->isVisible()
403 // 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.
404 && m_selectionModeBottomBar
405 && (m_selectionModeBottomBar
->isVisible() || m_selectionModeBottomBar
->contents() == SelectionMode::BottomBar::GeneralContents
)));
409 void DolphinViewContainer::slotSplitTabDisabled()
411 if (m_selectionModeBottomBar
) {
412 m_selectionModeBottomBar
->slotSplitTabDisabled();
416 void DolphinViewContainer::showMessage(const QString
&msg
, MessageType type
)
422 m_messageWidget
->setText(msg
);
424 // TODO: wrap at arbitrary character positions once QLabel can do this
425 // https://bugreports.qt.io/browse/QTBUG-1276
426 m_messageWidget
->setWordWrap(true);
430 m_messageWidget
->setMessageType(KMessageWidget::Information
);
433 m_messageWidget
->setMessageType(KMessageWidget::Warning
);
436 m_messageWidget
->setMessageType(KMessageWidget::Error
);
443 m_messageWidget
->setWordWrap(false);
444 const int unwrappedWidth
= m_messageWidget
->sizeHint().width();
445 m_messageWidget
->setWordWrap(unwrappedWidth
> size().width());
447 if (m_messageWidget
->isVisible()) {
448 m_messageWidget
->hide();
450 m_messageWidget
->animatedShow();
453 void DolphinViewContainer::readSettings()
455 // The startup settings should (only) get applied if they have been
456 // modified by the user. Otherwise keep the (possibly) different current
457 // setting of the filterbar.
458 if (GeneralSettings::modifiedStartupSettings()) {
459 setFilterBarVisible(GeneralSettings::filterBar());
462 m_view
->readSettings();
463 m_statusBar
->readSettings();
466 bool DolphinViewContainer::isFilterBarVisible() const
468 return m_filterBar
->isVisible();
471 void DolphinViewContainer::setSearchModeEnabled(bool enabled
)
473 m_searchBox
->setVisible(enabled
);
476 const QUrl
&locationUrl
= m_urlNavigator
->locationUrl();
477 m_searchBox
->fromSearchUrl(locationUrl
);
480 if (enabled
== isSearchModeEnabled()) {
481 if (enabled
&& !m_searchBox
->hasFocus()) {
482 m_searchBox
->setFocus();
483 m_searchBox
->selectAll();
489 m_view
->setViewPropertiesContext(QString());
491 // Restore the URL for the URL navigator. If Dolphin has been
492 // started with a search-URL, the home URL is used as fallback.
493 QUrl url
= m_searchBox
->searchPath();
494 if (url
.isEmpty() || !url
.isValid() || isSearchUrl(url
)) {
495 url
= Dolphin::homeUrl();
497 m_urlNavigatorConnected
->setLocationUrl(url
);
500 m_searchModeEnabled
= enabled
;
502 Q_EMIT
searchModeEnabledChanged(enabled
);
505 bool DolphinViewContainer::isSearchModeEnabled() const
507 return m_searchModeEnabled
;
510 QString
DolphinViewContainer::placesText() const
514 if (isSearchModeEnabled()) {
515 text
= i18n("Search for %1 in %2", m_searchBox
->text(), m_searchBox
->searchPath().fileName());
517 text
= url().adjusted(QUrl::StripTrailingSlash
).fileName();
518 if (text
.isEmpty()) {
521 if (text
.isEmpty()) {
522 text
= url().scheme();
529 void DolphinViewContainer::reload()
532 m_messageWidget
->hide();
535 QString
DolphinViewContainer::captionWindowTitle() const
537 if (GeneralSettings::showFullPathInTitlebar() && !isSearchModeEnabled()) {
538 if (!url().isLocalFile()) {
539 return url().adjusted(QUrl::StripTrailingSlash
).toString();
541 return url().adjusted(QUrl::StripTrailingSlash
).path();
543 return DolphinViewContainer::caption();
547 QString
DolphinViewContainer::caption() const
549 if (isSearchModeEnabled()) {
550 if (currentSearchText().isEmpty()) {
551 return i18n("Search");
553 return i18n("Search for %1", currentSearchText());
557 KFilePlacesModel
*placesModel
= DolphinPlacesModelSingleton::instance().placesModel();
558 const QString pattern
= url().adjusted(QUrl::StripTrailingSlash
).toString(QUrl::FullyEncoded
).append("/?");
559 const auto &matchedPlaces
=
560 placesModel
->match(placesModel
->index(0, 0), KFilePlacesModel::UrlRole
, QRegularExpression::anchoredPattern(pattern
), 1, Qt::MatchRegularExpression
);
562 if (!matchedPlaces
.isEmpty()) {
563 return placesModel
->text(matchedPlaces
.first());
566 if (!url().isLocalFile()) {
567 QUrl adjustedUrl
= url().adjusted(QUrl::StripTrailingSlash
);
569 if (!adjustedUrl
.fileName().isEmpty()) {
570 caption
= adjustedUrl
.fileName();
571 } else if (!adjustedUrl
.path().isEmpty() && adjustedUrl
.path() != "/") {
572 caption
= adjustedUrl
.path();
573 } else if (!adjustedUrl
.host().isEmpty()) {
574 caption
= adjustedUrl
.host();
576 caption
= adjustedUrl
.toString();
581 QString fileName
= url().adjusted(QUrl::StripTrailingSlash
).fileName();
582 if (fileName
.isEmpty()) {
589 void DolphinViewContainer::setUrl(const QUrl
&newUrl
)
591 if (newUrl
!= m_urlNavigator
->locationUrl()) {
592 m_urlNavigator
->setLocationUrl(newUrl
);
596 m_activityResourceInstance
->setUri(newUrl
);
600 void DolphinViewContainer::setFilterBarVisible(bool visible
)
602 Q_ASSERT(m_filterBar
);
604 m_view
->hideToolTip(ToolTipManager::HideBehavior::Instantly
);
606 m_filterBar
->setFocus();
607 m_filterBar
->selectAll();
613 void DolphinViewContainer::delayedStatusBarUpdate()
615 if (m_statusBarTimer
->isActive() && (m_statusBarTimestamp
.elapsed() > 2000)) {
616 // No update of the statusbar has been done during the last 2 seconds,
617 // although an update has been requested. Trigger an immediate update.
618 m_statusBarTimer
->stop();
621 // Invoke updateStatusBar() with a small delay. This assures that
622 // when a lot of delayedStatusBarUpdates() are done in a short time,
623 // no bottleneck is given.
624 m_statusBarTimer
->start();
628 void DolphinViewContainer::updateStatusBar()
630 m_statusBarTimestamp
.start();
631 m_view
->requestStatusBarText();
634 void DolphinViewContainer::updateDirectoryLoadingProgress(int percent
)
636 if (m_statusBar
->progressText().isEmpty()) {
637 m_statusBar
->setProgressText(i18nc("@info:progress", "Loading folder..."));
639 m_statusBar
->setProgress(percent
);
642 void DolphinViewContainer::updateDirectorySortingProgress(int percent
)
644 if (m_statusBar
->progressText().isEmpty()) {
645 m_statusBar
->setProgressText(i18nc("@info:progress", "Sorting..."));
647 m_statusBar
->setProgress(percent
);
650 void DolphinViewContainer::slotDirectoryLoadingStarted()
652 if (isSearchUrl(url())) {
653 // Search KIO-slaves usually don't provide any progress information. Give
654 // a hint to the user that a searching is done:
656 m_statusBar
->setProgressText(i18nc("@info", "Searching..."));
657 m_statusBar
->setProgress(-1);
659 // Trigger an undetermined progress indication. The progress
660 // information in percent will be triggered by the percent() signal
661 // of the directory lister later.
662 m_statusBar
->setProgressText(QString());
663 updateDirectoryLoadingProgress(-1);
667 void DolphinViewContainer::slotDirectoryLoadingCompleted()
669 if (!m_statusBar
->progressText().isEmpty()) {
670 m_statusBar
->setProgressText(QString());
671 m_statusBar
->setProgress(100);
674 if (isSearchUrl(url()) && m_view
->itemsCount() == 0) {
675 // The dir lister has been completed on a Baloo-URI and no items have been found. Instead
676 // of showing the default status bar information ("0 items") a more helpful information is given:
677 m_statusBar
->setText(i18nc("@info:status", "No items found."));
683 void DolphinViewContainer::slotDirectoryLoadingCanceled()
685 if (!m_statusBar
->progressText().isEmpty()) {
686 m_statusBar
->setProgressText(QString());
687 m_statusBar
->setProgress(100);
690 m_statusBar
->setText(QString());
693 void DolphinViewContainer::slotUrlIsFileError(const QUrl
&url
)
695 const KFileItem
item(url
);
697 // Find out if the file can be opened in the view (for example, this is the
698 // case if the file is an archive). The mime type must be known for that.
699 item
.determineMimeType();
700 const QUrl
&folderUrl
= DolphinView::openItemAsFolderUrl(item
, true);
701 if (!folderUrl
.isEmpty()) {
704 slotItemActivated(item
);
708 void DolphinViewContainer::slotItemActivated(const KFileItem
&item
)
710 // It is possible to activate items on inactive views by
711 // drag & drop operations. Assure that activating an item always
712 // results in an active view.
713 m_view
->setActive(true);
715 const QUrl
&url
= DolphinView::openItemAsFolderUrl(item
, GeneralSettings::browseThroughArchives());
716 if (!url
.isEmpty()) {
717 const auto modifiers
= QGuiApplication::keyboardModifiers();
718 // keep in sync with KUrlNavigator::slotNavigatorButtonClicked
719 if (modifiers
& Qt::ControlModifier
&& modifiers
& Qt::ShiftModifier
) {
720 Q_EMIT
activeTabRequested(url
);
721 } else if (modifiers
& Qt::ControlModifier
) {
722 Q_EMIT
tabRequested(url
);
723 } else if (modifiers
& Qt::ShiftModifier
) {
724 Dolphin::openNewWindow({KFilePlacesModel::convertedUrl(url
)}, this);
731 KIO::OpenUrlJob
*job
= new KIO::OpenUrlJob(item
.targetUrl(), item
.mimetype());
732 #if KIO_VERSION >= QT_VERSION_CHECK(5, 98, 0)
733 job
->setUiDelegate(KIO::createDefaultJobUiDelegate(KJobUiDelegate::AutoHandlingEnabled
, this));
735 job
->setUiDelegate(new KIO::JobUiDelegate(KJobUiDelegate::AutoHandlingEnabled
, this));
737 job
->setShowOpenOrExecuteDialog(true);
738 connect(job
, &KIO::OpenUrlJob::finished
, this, &DolphinViewContainer::slotOpenUrlFinished
);
742 void DolphinViewContainer::slotItemsActivated(const KFileItemList
&items
)
744 Q_ASSERT(items
.count() >= 2);
746 KFileItemActions
fileItemActions(this);
747 fileItemActions
.runPreferredApplications(items
);
750 void DolphinViewContainer::showItemInfo(const KFileItem
&item
)
753 m_statusBar
->resetToDefaultText();
755 m_statusBar
->setText(item
.getStatusBarInfo());
759 void DolphinViewContainer::closeFilterBar()
761 m_filterBar
->closeFilterBar();
763 Q_EMIT
showFilterBarChanged(false);
766 void DolphinViewContainer::clearFilterBar()
768 m_filterBar
->clearIfUnlocked();
771 void DolphinViewContainer::setNameFilter(const QString
&nameFilter
)
773 m_view
->hideToolTip(ToolTipManager::HideBehavior::Instantly
);
774 m_view
->setNameFilter(nameFilter
);
775 delayedStatusBarUpdate();
778 void DolphinViewContainer::activate()
783 void DolphinViewContainer::slotUrlNavigatorLocationAboutToBeChanged(const QUrl
&)
788 void DolphinViewContainer::slotUrlNavigatorLocationChanged(const QUrl
&url
)
790 if (m_urlNavigatorConnected
) {
791 m_urlNavigatorConnected
->slotReturnPressed();
794 if (KProtocolManager::supportsListing(url
)) {
795 setSearchModeEnabled(isSearchUrl(url
));
797 tryRestoreViewState();
799 if (m_autoGrabFocus
&& isActive() && !isSearchUrl(url
)) {
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"),
811 showMessage(i18nc("@info:status", "Protocol not supported by Dolphin, default application has been launched"), Information
);
814 QDesktopServices::openUrl(url
);
815 redirect(QUrl(), m_urlNavigator
->locationUrl(1));
817 showMessage(i18nc("@info:status", "Invalid protocol"), Error
);
818 m_urlNavigator
->goBack();
822 void DolphinViewContainer::slotUrlSelectionRequested(const QUrl
&url
)
824 m_view
->markUrlsAsSelected({url
});
825 m_view
->markUrlAsCurrent(url
); // makes the item scroll into view
828 void DolphinViewContainer::disableUrlNavigatorSelectionRequests()
830 disconnect(m_urlNavigator
.get(), &KUrlNavigator::urlSelectionRequested
, this, &DolphinViewContainer::slotUrlSelectionRequested
);
833 void DolphinViewContainer::enableUrlNavigatorSelectionRequests()
835 connect(m_urlNavigator
.get(), &KUrlNavigator::urlSelectionRequested
, this, &DolphinViewContainer::slotUrlSelectionRequested
);
838 void DolphinViewContainer::redirect(const QUrl
&oldUrl
, const QUrl
&newUrl
)
841 const bool block
= m_urlNavigator
->signalsBlocked();
842 m_urlNavigator
->blockSignals(true);
844 // Assure that the location state is reset for redirection URLs. This
845 // allows to skip redirection URLs when going back or forward in the
847 m_urlNavigator
->saveLocationState(QByteArray());
848 m_urlNavigator
->setLocationUrl(newUrl
);
849 setSearchModeEnabled(isSearchUrl(newUrl
));
851 m_urlNavigator
->blockSignals(block
);
854 void DolphinViewContainer::requestFocus()
859 void DolphinViewContainer::startSearching()
861 Q_CHECK_PTR(m_urlNavigatorConnected
);
862 const QUrl url
= m_searchBox
->urlForSearching();
863 if (url
.isValid() && !url
.isEmpty()) {
864 m_view
->setViewPropertiesContext(QStringLiteral("search"));
865 m_urlNavigatorConnected
->setLocationUrl(url
);
869 void DolphinViewContainer::closeSearchBox()
871 setSearchModeEnabled(false);
874 void DolphinViewContainer::stopDirectoryLoading()
876 m_view
->stopLoading();
877 m_statusBar
->setProgress(100);
880 void DolphinViewContainer::slotStatusBarZoomLevelChanged(int zoomLevel
)
882 m_view
->setZoomLevel(zoomLevel
);
885 void DolphinViewContainer::showErrorMessage(const QString
&msg
)
887 showMessage(msg
, Error
);
890 void DolphinViewContainer::slotPlacesModelChanged()
892 if (!GeneralSettings::showFullPathInTitlebar() && !isSearchModeEnabled()) {
893 Q_EMIT
captionChanged();
897 void DolphinViewContainer::slotHiddenFilesShownChanged(bool showHiddenFiles
)
899 if (m_urlNavigatorConnected
) {
900 m_urlNavigatorConnected
->setShowHiddenFolders(showHiddenFiles
);
904 void DolphinViewContainer::slotSortHiddenLastChanged(bool hiddenLast
)
906 if (m_urlNavigatorConnected
) {
907 m_urlNavigatorConnected
->setSortHiddenFoldersLast(hiddenLast
);
911 void DolphinViewContainer::slotCurrentDirectoryRemoved()
913 const QString
location(url().toDisplayString(QUrl::PreferLocalFile
));
914 if (url().isLocalFile()) {
915 const QString dirPath
= url().toLocalFile();
916 const QString newPath
= getNearestExistingAncestorOfPath(dirPath
);
917 const QUrl newUrl
= QUrl::fromLocalFile(newPath
);
921 showMessage(xi18n("Current location changed, <filename>%1</filename> is no longer accessible.", location
), Warning
);
924 void DolphinViewContainer::slotOpenUrlFinished(KJob
*job
)
926 if (job
->error() && job
->error() != KIO::ERR_USER_CANCELED
) {
927 showErrorMessage(job
->errorString());
931 bool DolphinViewContainer::isSearchUrl(const QUrl
&url
) const
933 return url
.scheme().contains(QLatin1String("search"));
936 void DolphinViewContainer::saveViewState()
938 QByteArray locationState
;
939 QDataStream
stream(&locationState
, QIODevice::WriteOnly
);
940 m_view
->saveState(stream
);
941 m_urlNavigator
->saveLocationState(locationState
);
944 void DolphinViewContainer::tryRestoreViewState()
946 QByteArray locationState
= m_urlNavigator
->locationState();
947 if (!locationState
.isEmpty()) {
948 QDataStream
stream(&locationState
, QIODevice::ReadOnly
);
949 m_view
->restoreState(stream
);
953 QString
DolphinViewContainer::getNearestExistingAncestorOfPath(const QString
&path
) const
957 dir
.setPath(QDir::cleanPath(dir
.filePath(QStringLiteral(".."))));
958 } while (!dir
.exists() && !dir
.isRoot());
960 return dir
.exists() ? dir
.path() : QString
{};