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_generalsettings.h"
10 #include "dolphindebug.h"
11 #include "dolphinplacesmodelsingleton.h"
12 #include "filterbar/filterbar.h"
14 #include "search/dolphinsearchbox.h"
15 #include "selectionmode/topbar.h"
16 #include "statusbar/dolphinstatusbar.h"
17 #include "views/viewmodecontroller.h"
18 #include "views/viewproperties.h"
19 #include "dolphin_detailsmodesettings.h"
21 #include <KActionCollection>
23 #include <KActivities/ResourceInstance>
25 #include <KFileItemActions>
26 #include <KFilePlacesModel>
27 #include <KIO/PreviewJob>
28 #include <KIO/OpenUrlJob>
29 #include <KIO/JobUiDelegate>
30 #include <KLocalizedString>
31 #include <KMessageWidget>
32 #include <KProtocolManager>
34 #include <KUrlComboBox>
37 #include <QGridLayout>
38 #include <QGuiApplication>
39 #include <QLoggingCategory>
43 #include <QDesktopServices>
45 // An overview of the widgets contained by this ViewContainer
46 struct LayoutStructure
{
48 int messageWidget
= 1;
49 int selectionModeTopBar
= 2;
51 int selectionModeBottomBar
= 4;
55 constexpr LayoutStructure positionFor
;
57 DolphinViewContainer::DolphinViewContainer(const QUrl
& url
, QWidget
* parent
) :
60 m_urlNavigator
{new DolphinUrlNavigator(url
)},
61 m_urlNavigatorConnected
{nullptr},
63 m_searchModeEnabled(false),
64 m_messageWidget(nullptr),
65 m_selectionModeTopBar
{nullptr},
68 m_selectionModeBottomBar
{nullptr},
70 m_statusBarTimer(nullptr),
71 m_statusBarTimestamp(),
74 , m_activityResourceInstance(nullptr)
79 m_topLayout
= new QGridLayout(this);
80 m_topLayout
->setSpacing(0);
81 m_topLayout
->setContentsMargins(0, 0, 0, 0);
83 m_searchBox
= new DolphinSearchBox(this);
85 connect(m_searchBox
, &DolphinSearchBox::activated
, this, &DolphinViewContainer::activate
);
86 connect(m_searchBox
, &DolphinSearchBox::closeRequest
, this, &DolphinViewContainer::closeSearchBox
);
87 connect(m_searchBox
, &DolphinSearchBox::searchRequest
, this, &DolphinViewContainer::startSearching
);
88 connect(m_searchBox
, &DolphinSearchBox::focusViewRequest
, this, &DolphinViewContainer::requestFocus
);
89 m_searchBox
->setWhatsThis(xi18nc("@info:whatsthis findbar",
90 "<para>This helps you find files and folders. Enter a <emphasis>"
91 "search term</emphasis> and specify search settings with the "
92 "buttons at the bottom:<list><item>Filename/Content: "
93 "Does the item you are looking for contain the search terms "
94 "within its filename or its contents?<nl/>The contents of images, "
95 "audio files and videos will not be searched.</item><item>"
96 "From Here/Everywhere: Do you want to search in this "
97 "folder and its sub-folders or everywhere?</item><item>"
98 "More Options: Click this to search by media type, access "
99 "time or rating.</item><item>More Search Tools: Install other "
100 "means to find an item.</item></list></para>"));
102 m_messageWidget
= new KMessageWidget(this);
103 m_messageWidget
->setCloseButtonVisible(true);
104 m_messageWidget
->hide();
109 // We must be logged in as the root user; show a big scary warning
110 showMessage(i18n("Running Dolphin as root can be dangerous. Please be careful."), Warning
);
114 // Initialize filter bar
115 m_filterBar
= new FilterBar(this);
116 m_filterBar
->setVisible(GeneralSettings::filterBar());
118 connect(m_filterBar
, &FilterBar::filterChanged
,
119 this, &DolphinViewContainer::setNameFilter
);
120 connect(m_filterBar
, &FilterBar::closeRequest
,
121 this, &DolphinViewContainer::closeFilterBar
);
122 connect(m_filterBar
, &FilterBar::focusViewRequest
,
123 this, &DolphinViewContainer::requestFocus
);
125 // Initialize the main view
126 m_view
= new DolphinView(url
, this);
127 connect(m_view
, &DolphinView::urlChanged
,
128 m_filterBar
, &FilterBar::slotUrlChanged
);
129 connect(m_view
, &DolphinView::urlChanged
,
130 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
,
134 m_urlNavigator
.get(), &DolphinUrlNavigator::setLocationUrl
);
135 connect(m_urlNavigator
.get(), &DolphinUrlNavigator::urlChanged
,
136 this, &DolphinViewContainer::slotUrlNavigatorLocationChanged
);
137 connect(m_urlNavigator
.get(), &DolphinUrlNavigator::urlAboutToBeChanged
,
138 this, &DolphinViewContainer::slotUrlNavigatorLocationAboutToBeChanged
);
139 connect(m_urlNavigator
.get(), &DolphinUrlNavigator::urlSelectionRequested
,
140 this, &DolphinViewContainer::slotUrlSelectionRequested
);
141 connect(m_view
, &DolphinView::writeStateChanged
,
142 this, &DolphinViewContainer::writeStateChanged
);
143 connect(m_view
, &DolphinView::requestItemInfo
,
144 this, &DolphinViewContainer::showItemInfo
);
145 connect(m_view
, &DolphinView::itemActivated
,
146 this, &DolphinViewContainer::slotItemActivated
);
147 connect(m_view
, &DolphinView::itemsActivated
,
148 this, &DolphinViewContainer::slotItemsActivated
);
149 connect(m_view
, &DolphinView::redirection
,
150 this, &DolphinViewContainer::redirect
);
151 connect(m_view
, &DolphinView::directoryLoadingStarted
,
152 this, &DolphinViewContainer::slotDirectoryLoadingStarted
);
153 connect(m_view
, &DolphinView::directoryLoadingCompleted
,
154 this, &DolphinViewContainer::slotDirectoryLoadingCompleted
);
155 connect(m_view
, &DolphinView::directoryLoadingCanceled
,
156 this, &DolphinViewContainer::slotDirectoryLoadingCanceled
);
157 connect(m_view
, &DolphinView::itemCountChanged
,
158 this, &DolphinViewContainer::delayedStatusBarUpdate
);
159 connect(m_view
, &DolphinView::directoryLoadingProgress
,
160 this, &DolphinViewContainer::updateDirectoryLoadingProgress
);
161 connect(m_view
, &DolphinView::directorySortingProgress
,
162 this, &DolphinViewContainer::updateDirectorySortingProgress
);
163 connect(m_view
, &DolphinView::selectionChanged
,
164 this, &DolphinViewContainer::delayedStatusBarUpdate
);
165 connect(m_view
, &DolphinView::errorMessage
,
166 this, &DolphinViewContainer::showErrorMessage
);
167 connect(m_view
, &DolphinView::urlIsFileError
,
168 this, &DolphinViewContainer::slotUrlIsFileError
);
169 connect(m_view
, &DolphinView::activated
,
170 this, &DolphinViewContainer::activate
);
171 connect(m_view
, &DolphinView::hiddenFilesShownChanged
,
172 this, &DolphinViewContainer::slotHiddenFilesShownChanged
);
173 connect(m_view
, &DolphinView::sortHiddenLastChanged
,
174 this, &DolphinViewContainer::slotSortHiddenLastChanged
);
176 // Initialize status bar
177 m_statusBar
= new DolphinStatusBar(this);
178 m_statusBar
->setUrl(m_view
->url());
179 m_statusBar
->setZoomLevel(m_view
->zoomLevel());
180 connect(m_view
, &DolphinView::urlChanged
,
181 m_statusBar
, &DolphinStatusBar::setUrl
);
182 connect(m_view
, &DolphinView::zoomLevelChanged
,
183 m_statusBar
, &DolphinStatusBar::setZoomLevel
);
184 connect(m_view
, &DolphinView::infoMessage
,
185 m_statusBar
, &DolphinStatusBar::setText
);
186 connect(m_view
, &DolphinView::operationCompletedMessage
,
187 m_statusBar
, &DolphinStatusBar::setText
);
188 connect(m_view
, &DolphinView::statusBarTextChanged
,
189 m_statusBar
, &DolphinStatusBar::setDefaultText
);
190 connect(m_view
, &DolphinView::statusBarTextChanged
,
191 m_statusBar
, &DolphinStatusBar::resetToDefaultText
);
192 connect(m_statusBar
, &DolphinStatusBar::stopPressed
,
193 this, &DolphinViewContainer::stopDirectoryLoading
);
194 connect(m_statusBar
, &DolphinStatusBar::zoomLevelChanged
,
195 this, &DolphinViewContainer::slotStatusBarZoomLevelChanged
);
197 m_statusBarTimer
= new QTimer(this);
198 m_statusBarTimer
->setSingleShot(true);
199 m_statusBarTimer
->setInterval(300);
200 connect(m_statusBarTimer
, &QTimer::timeout
, this, &DolphinViewContainer::updateStatusBar
);
202 KIO::FileUndoManager
* undoManager
= KIO::FileUndoManager::self();
203 connect(undoManager
, &KIO::FileUndoManager::jobRecordingFinished
,
204 this, &DolphinViewContainer::delayedStatusBarUpdate
);
206 m_topLayout
->addWidget(m_searchBox
, positionFor
.searchBox
, 0);
207 m_topLayout
->addWidget(m_messageWidget
, positionFor
.messageWidget
, 0);
208 m_topLayout
->addWidget(m_view
, positionFor
.view
, 0);
209 m_topLayout
->addWidget(m_filterBar
, positionFor
.filterBar
, 0);
210 m_topLayout
->addWidget(m_statusBar
, positionFor
.statusBar
, 0);
212 setSearchModeEnabled(isSearchUrl(url
));
214 connect(DetailsModeSettings::self(), &KCoreConfigSkeleton::configChanged
, this, [=]() {
215 if (view()->viewMode() == DolphinView::Mode::DetailsView
) {
220 KFilePlacesModel
*placesModel
= DolphinPlacesModelSingleton::instance().placesModel();
221 connect(placesModel
, &KFilePlacesModel::dataChanged
,
222 this, &DolphinViewContainer::slotPlacesModelChanged
);
223 connect(placesModel
, &KFilePlacesModel::rowsInserted
,
224 this, &DolphinViewContainer::slotPlacesModelChanged
);
225 connect(placesModel
, &KFilePlacesModel::rowsRemoved
,
226 this, &DolphinViewContainer::slotPlacesModelChanged
);
228 connect(this, &DolphinViewContainer::searchModeEnabledChanged
,
229 this, &DolphinViewContainer::captionChanged
);
231 // Initialize kactivities resource instance
234 m_activityResourceInstance
= new KActivities::ResourceInstance(window()->winId(), url
);
235 m_activityResourceInstance
->setParent(this);
239 DolphinViewContainer::~DolphinViewContainer()
243 QUrl
DolphinViewContainer::url() const
245 return m_view
->url();
248 void DolphinViewContainer::setActive(bool active
)
250 m_searchBox
->setActive(active
);
251 if (m_urlNavigatorConnected
) {
252 m_urlNavigatorConnected
->setActive(active
);
254 m_view
->setActive(active
);
258 m_activityResourceInstance
->notifyFocusedIn();
260 m_activityResourceInstance
->notifyFocusedOut();
265 bool DolphinViewContainer::isActive() const
267 return m_view
->isActive();
270 void DolphinViewContainer::setAutoGrabFocus(bool grab
)
272 m_autoGrabFocus
= grab
;
275 bool DolphinViewContainer::autoGrabFocus() const
277 return m_autoGrabFocus
;
280 QString
DolphinViewContainer::currentSearchText() const
282 return m_searchBox
->text();
285 const DolphinStatusBar
* DolphinViewContainer::statusBar() const
290 DolphinStatusBar
* DolphinViewContainer::statusBar()
295 const DolphinUrlNavigator
* DolphinViewContainer::urlNavigator() const
297 return m_urlNavigatorConnected
;
300 DolphinUrlNavigator
* DolphinViewContainer::urlNavigator()
302 return m_urlNavigatorConnected
;
305 const DolphinUrlNavigator
*DolphinViewContainer::urlNavigatorInternalWithHistory() const
307 return m_urlNavigator
.get();
310 DolphinUrlNavigator
*DolphinViewContainer::urlNavigatorInternalWithHistory()
312 return m_urlNavigator
.get();
315 const DolphinView
* DolphinViewContainer::view() const
320 DolphinView
* DolphinViewContainer::view()
325 void DolphinViewContainer::connectUrlNavigator(DolphinUrlNavigator
*urlNavigator
)
327 Q_CHECK_PTR(urlNavigator
);
328 Q_ASSERT(!m_urlNavigatorConnected
);
329 Q_ASSERT(m_urlNavigator
.get() != urlNavigator
);
332 urlNavigator
->setLocationUrl(m_view
->url());
333 urlNavigator
->setShowHiddenFolders(m_view
->hiddenFilesShown());
334 urlNavigator
->setSortHiddenFoldersLast(m_view
->sortHiddenLast());
335 if (m_urlNavigatorVisualState
) {
336 urlNavigator
->setVisualState(*m_urlNavigatorVisualState
.get());
337 m_urlNavigatorVisualState
.reset();
339 urlNavigator
->setActive(isActive());
341 // Url changes are still done via m_urlNavigator.
342 connect(urlNavigator
, &DolphinUrlNavigator::urlChanged
,
343 m_urlNavigator
.get(), &DolphinUrlNavigator::setLocationUrl
);
344 connect(urlNavigator
, &DolphinUrlNavigator::urlsDropped
,
345 this, [=](const QUrl
&destination
, QDropEvent
*event
) {
346 m_view
->dropUrls(destination
, event
, urlNavigator
->dropWidget());
348 // Aside from these, only visual things need to be connected.
349 connect(m_view
, &DolphinView::urlChanged
,
350 urlNavigator
, &DolphinUrlNavigator::setLocationUrl
);
351 connect(urlNavigator
, &DolphinUrlNavigator::activated
,
352 this, &DolphinViewContainer::activate
);
354 m_urlNavigatorConnected
= urlNavigator
;
357 void DolphinViewContainer::disconnectUrlNavigator()
359 if (!m_urlNavigatorConnected
) {
363 disconnect(m_urlNavigatorConnected
, &DolphinUrlNavigator::urlChanged
,
364 m_urlNavigator
.get(), &DolphinUrlNavigator::setLocationUrl
);
365 disconnect(m_urlNavigatorConnected
, &DolphinUrlNavigator::urlsDropped
,
367 disconnect(m_view
, &DolphinView::urlChanged
,
368 m_urlNavigatorConnected
, &DolphinUrlNavigator::setLocationUrl
);
369 disconnect(m_urlNavigatorConnected
, &DolphinUrlNavigator::activated
,
370 this, &DolphinViewContainer::activate
);
372 m_urlNavigatorVisualState
= m_urlNavigatorConnected
->visualState();
373 m_urlNavigatorConnected
= nullptr;
376 void DolphinViewContainer::setSelectionModeEnabled(bool enabled
, KActionCollection
*actionCollection
, SelectionMode::BottomBar::Contents bottomBarContents
)
378 const bool wasEnabled
= m_view
->selectionMode();
379 m_view
->setSelectionMode(enabled
);
382 Q_CHECK_PTR(m_selectionModeTopBar
); // there is no point in disabling selectionMode when it wasn't even enabled once.
383 Q_CHECK_PTR(m_selectionModeBottomBar
);
384 m_selectionModeTopBar
->setVisible(false, WithAnimation
);
385 m_selectionModeBottomBar
->setVisible(false, WithAnimation
);
387 Q_EMIT
selectionModeChanged(false);
392 if (!m_selectionModeTopBar
) {
393 // Changing the location will disable selection mode.
394 connect(m_urlNavigator
.get(), &DolphinUrlNavigator::urlChanged
, this, [this]() {
395 setSelectionModeEnabled(false);
398 m_selectionModeTopBar
= new SelectionMode::TopBar(this); // will be created hidden
399 connect(m_selectionModeTopBar
, &SelectionMode::TopBar::leaveSelectionModeRequested
, this, [this]() {
400 setSelectionModeEnabled(false);
402 m_topLayout
->addWidget(m_selectionModeTopBar
, positionFor
.selectionModeTopBar
, 0);
405 if (!m_selectionModeBottomBar
) {
406 m_selectionModeBottomBar
= new SelectionMode::BottomBar(actionCollection
, this);
407 connect(m_view
, &DolphinView::selectionChanged
, this, [this](const KFileItemList
&selection
) {
408 m_selectionModeBottomBar
->slotSelectionChanged(selection
, m_view
->url());
410 connect(m_selectionModeBottomBar
, &SelectionMode::BottomBar::error
, this, [this](const QString
&errorMessage
) {
411 showErrorMessage(errorMessage
);
413 connect(m_selectionModeBottomBar
, &SelectionMode::BottomBar::leaveSelectionModeRequested
, this, [this]() {
414 setSelectionModeEnabled(false);
416 m_topLayout
->addWidget(m_selectionModeBottomBar
, positionFor
.selectionModeBottomBar
, 0);
418 m_selectionModeBottomBar
->resetContents(bottomBarContents
);
419 if (bottomBarContents
== SelectionMode::BottomBar::GeneralContents
) {
420 m_selectionModeBottomBar
->slotSelectionChanged(m_view
->selectedItems(), m_view
->url());
424 m_selectionModeTopBar
->setVisible(true, WithAnimation
);
425 m_selectionModeBottomBar
->setVisible(true, WithAnimation
);
426 Q_EMIT
selectionModeChanged(true);
430 bool DolphinViewContainer::isSelectionModeEnabled() const
432 const bool isEnabled
= m_view
->selectionMode();
434 // We can't assert that the bars are invisible only because the selection mode is disabled because the hide animation might still be playing.
435 && (!m_selectionModeBottomBar
|| !m_selectionModeBottomBar
->isEnabled() ||
436 !m_selectionModeBottomBar
->isVisible() || m_selectionModeBottomBar
->contents() == SelectionMode::BottomBar::PasteContents
))
438 && m_selectionModeTopBar
&& m_selectionModeTopBar
->isVisible()
439 // 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.
440 && m_selectionModeBottomBar
441 && (m_selectionModeBottomBar
->isVisible() || m_selectionModeBottomBar
->contents() == SelectionMode::BottomBar::GeneralContents
)));
445 void DolphinViewContainer::slotSplitTabDisabled()
447 if (m_selectionModeBottomBar
) {
448 m_selectionModeBottomBar
->slotSplitTabDisabled();
453 void DolphinViewContainer::showMessage(const QString
& msg
, MessageType type
)
459 m_messageWidget
->setText(msg
);
461 // TODO: wrap at arbitrary character positions once QLabel can do this
462 // https://bugreports.qt.io/browse/QTBUG-1276
463 m_messageWidget
->setWordWrap(true);
466 case Information
: m_messageWidget
->setMessageType(KMessageWidget::Information
); break;
467 case Warning
: m_messageWidget
->setMessageType(KMessageWidget::Warning
); break;
468 case Error
: m_messageWidget
->setMessageType(KMessageWidget::Error
); break;
474 m_messageWidget
->setWordWrap(false);
475 const int unwrappedWidth
= m_messageWidget
->sizeHint().width();
476 m_messageWidget
->setWordWrap(unwrappedWidth
> size().width());
478 if (m_messageWidget
->isVisible()) {
479 m_messageWidget
->hide();
481 m_messageWidget
->animatedShow();
484 void DolphinViewContainer::readSettings()
486 // The startup settings should (only) get applied if they have been
487 // modified by the user. Otherwise keep the (possibly) different current
488 // setting of the filterbar.
489 if (GeneralSettings::modifiedStartupSettings()) {
490 setFilterBarVisible(GeneralSettings::filterBar());
493 m_view
->readSettings();
494 m_statusBar
->readSettings();
497 bool DolphinViewContainer::isFilterBarVisible() const
499 return m_filterBar
->isVisible();
502 void DolphinViewContainer::setSearchModeEnabled(bool enabled
)
504 m_searchBox
->setVisible(enabled
);
507 const QUrl
& locationUrl
= m_urlNavigator
->locationUrl();
508 m_searchBox
->fromSearchUrl(locationUrl
);
511 if (enabled
== isSearchModeEnabled()) {
512 if (enabled
&& !m_searchBox
->hasFocus()) {
513 m_searchBox
->setFocus();
514 m_searchBox
->selectAll();
520 m_view
->setViewPropertiesContext(QString());
522 // Restore the URL for the URL navigator. If Dolphin has been
523 // started with a search-URL, the home URL is used as fallback.
524 QUrl url
= m_searchBox
->searchPath();
525 if (url
.isEmpty() || !url
.isValid() || isSearchUrl(url
)) {
526 url
= Dolphin::homeUrl();
528 m_urlNavigatorConnected
->setLocationUrl(url
);
531 m_searchModeEnabled
= enabled
;
533 Q_EMIT
searchModeEnabledChanged(enabled
);
536 bool DolphinViewContainer::isSearchModeEnabled() const
538 return m_searchModeEnabled
;
541 QString
DolphinViewContainer::placesText() const
545 if (isSearchModeEnabled()) {
546 text
= i18n("Search for %1 in %2", m_searchBox
->text(), m_searchBox
->searchPath().fileName());
548 text
= url().adjusted(QUrl::StripTrailingSlash
).fileName();
549 if (text
.isEmpty()) {
552 if (text
.isEmpty()) {
553 text
= url().scheme();
560 void DolphinViewContainer::reload()
563 m_messageWidget
->hide();
566 QString
DolphinViewContainer::captionWindowTitle() const
568 if (GeneralSettings::showFullPathInTitlebar() && !isSearchModeEnabled()) {
569 if (!url().isLocalFile()) {
570 return url().adjusted(QUrl::StripTrailingSlash
).toString();
572 return url().adjusted(QUrl::StripTrailingSlash
).path();
574 return DolphinViewContainer::caption();
578 QString
DolphinViewContainer::caption() const
580 if (isSearchModeEnabled()) {
581 if (currentSearchText().isEmpty()){
582 return i18n("Search");
584 return i18n("Search for %1", currentSearchText());
588 KFilePlacesModel
*placesModel
= DolphinPlacesModelSingleton::instance().placesModel();
589 const QString pattern
= url().adjusted(QUrl::StripTrailingSlash
).toString(QUrl::FullyEncoded
).append("/?");
590 const auto& matchedPlaces
= placesModel
->match(placesModel
->index(0,0), KFilePlacesModel::UrlRole
, QRegularExpression::anchoredPattern(pattern
), 1, Qt::MatchRegularExpression
);
592 if (!matchedPlaces
.isEmpty()) {
593 return placesModel
->text(matchedPlaces
.first());
597 if (!url().isLocalFile()) {
598 QUrl adjustedUrl
= url().adjusted(QUrl::StripTrailingSlash
);
600 if (!adjustedUrl
.fileName().isEmpty()) {
601 caption
= adjustedUrl
.fileName();
602 } else if (!adjustedUrl
.path().isEmpty() && adjustedUrl
.path() != "/") {
603 caption
= adjustedUrl
.path();
604 } else if (!adjustedUrl
.host().isEmpty()) {
605 caption
= adjustedUrl
.host();
607 caption
= adjustedUrl
.toString();
612 QString fileName
= url().adjusted(QUrl::StripTrailingSlash
).fileName();
613 if (fileName
.isEmpty()) {
620 void DolphinViewContainer::setUrl(const QUrl
& newUrl
)
622 if (newUrl
!= m_urlNavigator
->locationUrl()) {
623 m_urlNavigator
->setLocationUrl(newUrl
);
627 m_activityResourceInstance
->setUri(newUrl
);
631 void DolphinViewContainer::setFilterBarVisible(bool visible
)
633 Q_ASSERT(m_filterBar
);
635 m_view
->hideToolTip(ToolTipManager::HideBehavior::Instantly
);
637 m_filterBar
->setFocus();
638 m_filterBar
->selectAll();
644 void DolphinViewContainer::delayedStatusBarUpdate()
646 if (m_statusBarTimer
->isActive() && (m_statusBarTimestamp
.elapsed() > 2000)) {
647 // No update of the statusbar has been done during the last 2 seconds,
648 // although an update has been requested. Trigger an immediate update.
649 m_statusBarTimer
->stop();
652 // Invoke updateStatusBar() with a small delay. This assures that
653 // when a lot of delayedStatusBarUpdates() are done in a short time,
654 // no bottleneck is given.
655 m_statusBarTimer
->start();
659 void DolphinViewContainer::updateStatusBar()
661 m_statusBarTimestamp
.start();
662 m_view
->requestStatusBarText();
665 void DolphinViewContainer::updateDirectoryLoadingProgress(int percent
)
667 if (m_statusBar
->progressText().isEmpty()) {
668 m_statusBar
->setProgressText(i18nc("@info:progress", "Loading folder..."));
670 m_statusBar
->setProgress(percent
);
673 void DolphinViewContainer::updateDirectorySortingProgress(int percent
)
675 if (m_statusBar
->progressText().isEmpty()) {
676 m_statusBar
->setProgressText(i18nc("@info:progress", "Sorting..."));
678 m_statusBar
->setProgress(percent
);
681 void DolphinViewContainer::slotDirectoryLoadingStarted()
683 if (isSearchUrl(url())) {
684 // Search KIO-slaves usually don't provide any progress information. Give
685 // a hint to the user that a searching is done:
687 m_statusBar
->setProgressText(i18nc("@info", "Searching..."));
688 m_statusBar
->setProgress(-1);
690 // Trigger an undetermined progress indication. The progress
691 // information in percent will be triggered by the percent() signal
692 // of the directory lister later.
693 m_statusBar
->setProgressText(QString());
694 updateDirectoryLoadingProgress(-1);
698 void DolphinViewContainer::slotDirectoryLoadingCompleted()
700 if (!m_statusBar
->progressText().isEmpty()) {
701 m_statusBar
->setProgressText(QString());
702 m_statusBar
->setProgress(100);
705 if (isSearchUrl(url()) && m_view
->itemsCount() == 0) {
706 // The dir lister has been completed on a Baloo-URI and no items have been found. Instead
707 // of showing the default status bar information ("0 items") a more helpful information is given:
708 m_statusBar
->setText(i18nc("@info:status", "No items found."));
714 void DolphinViewContainer::slotDirectoryLoadingCanceled()
716 if (!m_statusBar
->progressText().isEmpty()) {
717 m_statusBar
->setProgressText(QString());
718 m_statusBar
->setProgress(100);
721 m_statusBar
->setText(QString());
724 void DolphinViewContainer::slotUrlIsFileError(const QUrl
& url
)
726 const KFileItem
item(url
);
728 // Find out if the file can be opened in the view (for example, this is the
729 // case if the file is an archive). The mime type must be known for that.
730 item
.determineMimeType();
731 const QUrl
& folderUrl
= DolphinView::openItemAsFolderUrl(item
, true);
732 if (!folderUrl
.isEmpty()) {
735 slotItemActivated(item
);
739 void DolphinViewContainer::slotItemActivated(const KFileItem
&item
)
741 // It is possible to activate items on inactive views by
742 // drag & drop operations. Assure that activating an item always
743 // results in an active view.
744 m_view
->setActive(true);
746 const QUrl
& url
= DolphinView::openItemAsFolderUrl(item
, GeneralSettings::browseThroughArchives());
747 if (!url
.isEmpty()) {
748 const auto modifiers
= QGuiApplication::keyboardModifiers();
749 // keep in sync with KUrlNavigator::slotNavigatorButtonClicked
750 if (modifiers
& Qt::ControlModifier
&& modifiers
& Qt::ShiftModifier
) {
751 Q_EMIT
activeTabRequested(url
);
752 } else if (modifiers
& Qt::ControlModifier
) {
753 Q_EMIT
tabRequested(url
);
754 } else if (modifiers
& Qt::ShiftModifier
) {
755 Dolphin::openNewWindow({KFilePlacesModel::convertedUrl(url
)}, this);
762 KIO::OpenUrlJob
*job
= new KIO::OpenUrlJob(item
.targetUrl(), item
.mimetype());
763 job
->setUiDelegate(new KIO::JobUiDelegate(KJobUiDelegate::AutoWarningHandlingEnabled
, this));
764 job
->setShowOpenOrExecuteDialog(true);
765 connect(job
, &KIO::OpenUrlJob::finished
, this, &DolphinViewContainer::slotOpenUrlFinished
);
769 void DolphinViewContainer::slotItemsActivated(const KFileItemList
& items
)
771 Q_ASSERT(items
.count() >= 2);
773 KFileItemActions
fileItemActions(this);
774 fileItemActions
.runPreferredApplications(items
);
777 void DolphinViewContainer::showItemInfo(const KFileItem
& item
)
780 m_statusBar
->resetToDefaultText();
782 m_statusBar
->setText(item
.getStatusBarInfo());
786 void DolphinViewContainer::closeFilterBar()
788 m_filterBar
->closeFilterBar();
790 Q_EMIT
showFilterBarChanged(false);
793 void DolphinViewContainer::setNameFilter(const QString
& nameFilter
)
795 m_view
->hideToolTip(ToolTipManager::HideBehavior::Instantly
);
796 m_view
->setNameFilter(nameFilter
);
797 delayedStatusBarUpdate();
800 void DolphinViewContainer::activate()
805 void DolphinViewContainer::slotUrlNavigatorLocationAboutToBeChanged(const QUrl
&)
810 void DolphinViewContainer::slotUrlNavigatorLocationChanged(const QUrl
& url
)
812 if (m_urlNavigatorConnected
) {
813 m_urlNavigatorConnected
->slotReturnPressed();
816 if (KProtocolManager::supportsListing(url
)) {
817 setSearchModeEnabled(isSearchUrl(url
));
819 tryRestoreViewState();
821 if (m_autoGrabFocus
&& isActive() && !isSearchUrl(url
)) {
822 // When an URL has been entered, the view should get the focus.
823 // The focus must be requested asynchronously, as changing the URL might create
824 // a new view widget.
825 QTimer::singleShot(0, this, &DolphinViewContainer::requestFocus
);
827 } else if (KProtocolManager::isSourceProtocol(url
)) {
828 if (url
.scheme().startsWith(QLatin1String("http"))) {
829 showMessage(i18nc("@info:status", // krazy:exclude=qmethods
830 "Dolphin does not support web pages, the web browser has been launched"),
833 showMessage(i18nc("@info:status",
834 "Protocol not supported by Dolphin, default application has been launched"),
838 QDesktopServices::openUrl(url
);
839 redirect(QUrl(), m_urlNavigator
->locationUrl(1));
841 showMessage(i18nc("@info:status", "Invalid protocol"), Error
);
842 m_urlNavigator
->goBack();
846 void DolphinViewContainer::slotUrlSelectionRequested(const QUrl
& url
)
848 m_view
->markUrlsAsSelected({url
});
849 m_view
->markUrlAsCurrent(url
); // makes the item scroll into view
852 void DolphinViewContainer::disableUrlNavigatorSelectionRequests()
854 disconnect(m_urlNavigator
.get(), &KUrlNavigator::urlSelectionRequested
,
855 this, &DolphinViewContainer::slotUrlSelectionRequested
);
858 void DolphinViewContainer::enableUrlNavigatorSelectionRequests()
860 connect(m_urlNavigator
.get(), &KUrlNavigator::urlSelectionRequested
,
861 this, &DolphinViewContainer::slotUrlSelectionRequested
);
864 void DolphinViewContainer::redirect(const QUrl
& oldUrl
, const QUrl
& newUrl
)
867 const bool block
= m_urlNavigator
->signalsBlocked();
868 m_urlNavigator
->blockSignals(true);
870 // Assure that the location state is reset for redirection URLs. This
871 // allows to skip redirection URLs when going back or forward in the
873 m_urlNavigator
->saveLocationState(QByteArray());
874 m_urlNavigator
->setLocationUrl(newUrl
);
875 setSearchModeEnabled(isSearchUrl(newUrl
));
877 m_urlNavigator
->blockSignals(block
);
880 void DolphinViewContainer::requestFocus()
885 void DolphinViewContainer::startSearching()
887 Q_CHECK_PTR(m_urlNavigatorConnected
);
888 const QUrl url
= m_searchBox
->urlForSearching();
889 if (url
.isValid() && !url
.isEmpty()) {
890 m_view
->setViewPropertiesContext(QStringLiteral("search"));
891 m_urlNavigatorConnected
->setLocationUrl(url
);
895 void DolphinViewContainer::closeSearchBox()
897 setSearchModeEnabled(false);
900 void DolphinViewContainer::stopDirectoryLoading()
902 m_view
->stopLoading();
903 m_statusBar
->setProgress(100);
906 void DolphinViewContainer::slotStatusBarZoomLevelChanged(int zoomLevel
)
908 m_view
->setZoomLevel(zoomLevel
);
911 void DolphinViewContainer::showErrorMessage(const QString
& msg
)
913 showMessage(msg
, Error
);
916 void DolphinViewContainer::slotPlacesModelChanged()
918 if (!GeneralSettings::showFullPathInTitlebar() && !isSearchModeEnabled()) {
919 Q_EMIT
captionChanged();
923 void DolphinViewContainer::slotHiddenFilesShownChanged(bool showHiddenFiles
)
925 if (m_urlNavigatorConnected
) {
926 m_urlNavigatorConnected
->setShowHiddenFolders(showHiddenFiles
);
930 void DolphinViewContainer::slotSortHiddenLastChanged(bool hiddenLast
)
932 if (m_urlNavigatorConnected
) {
933 m_urlNavigatorConnected
->setSortHiddenFoldersLast(hiddenLast
);
937 void DolphinViewContainer::slotOpenUrlFinished(KJob
*job
)
939 if (job
->error() && job
->error() != KIO::ERR_USER_CANCELED
) {
940 showErrorMessage(job
->errorString());
944 bool DolphinViewContainer::isSearchUrl(const QUrl
& url
) const
946 return url
.scheme().contains(QLatin1String("search"));
949 void DolphinViewContainer::saveViewState()
951 QByteArray locationState
;
952 QDataStream
stream(&locationState
, QIODevice::WriteOnly
);
953 m_view
->saveState(stream
);
954 m_urlNavigator
->saveLocationState(locationState
);
957 void DolphinViewContainer::tryRestoreViewState()
959 QByteArray locationState
= m_urlNavigator
->locationState();
960 if (!locationState
.isEmpty()) {
961 QDataStream
stream(&locationState
, QIODevice::ReadOnly
);
962 m_view
->restoreState(stream
);