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 <KApplicationTrader>
27 #include <KFileItemActions>
28 #include <KFilePlacesModel>
29 #include <KIO/JobUiDelegateFactory>
30 #include <KIO/OpenUrlJob>
31 #include <KLocalizedString>
32 #include <KMessageWidget>
33 #include <KProtocolManager>
35 #include <kio_version.h>
37 #include <QApplication>
38 #include <QDesktopServices>
40 #include <QGridLayout>
41 #include <QGuiApplication>
42 #include <QRegularExpression>
46 // An overview of the widgets contained by this ViewContainer
47 struct LayoutStructure
{
49 int messageWidget
= 1;
50 int selectionModeTopBar
= 2;
52 int selectionModeBottomBar
= 4;
56 constexpr LayoutStructure positionFor
;
58 DolphinViewContainer::DolphinViewContainer(const QUrl
&url
, QWidget
*parent
)
60 , m_topLayout(nullptr)
61 , m_urlNavigator
{new DolphinUrlNavigator(url
)}
62 , m_urlNavigatorConnected
{nullptr}
63 , m_searchBox(nullptr)
64 , m_searchModeEnabled(false)
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);
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
->hide();
107 // We must be logged in as the root user; show a big scary warning
108 showMessage(i18n("Running Dolphin as root can be dangerous. Please be careful."), Warning
);
112 // Initialize filter bar
113 m_filterBar
= new FilterBar(this);
114 m_filterBar
->setVisible(GeneralSettings::filterBar());
116 connect(m_filterBar
, &FilterBar::filterChanged
, this, &DolphinViewContainer::setNameFilter
);
117 connect(m_filterBar
, &FilterBar::closeRequest
, this, &DolphinViewContainer::closeFilterBar
);
118 connect(m_filterBar
, &FilterBar::focusViewRequest
, this, &DolphinViewContainer::requestFocus
);
120 // Initialize the main view
121 m_view
= new DolphinView(url
, this);
122 connect(m_view
, &DolphinView::urlChanged
, m_filterBar
, &FilterBar::clearIfUnlocked
);
123 connect(m_view
, &DolphinView::urlChanged
, m_messageWidget
, &KMessageWidget::hide
);
124 // m_urlNavigator stays in sync with m_view's location changes and
125 // keeps track of them so going back and forth in the history works.
126 connect(m_view
, &DolphinView::urlChanged
, m_urlNavigator
.get(), &DolphinUrlNavigator::setLocationUrl
);
127 connect(m_urlNavigator
.get(), &DolphinUrlNavigator::urlChanged
, this, &DolphinViewContainer::slotUrlNavigatorLocationChanged
);
128 connect(m_urlNavigator
.get(), &DolphinUrlNavigator::urlAboutToBeChanged
, this, &DolphinViewContainer::slotUrlNavigatorLocationAboutToBeChanged
);
129 connect(m_urlNavigator
.get(), &DolphinUrlNavigator::urlSelectionRequested
, this, &DolphinViewContainer::slotUrlSelectionRequested
);
130 connect(m_view
, &DolphinView::writeStateChanged
, this, &DolphinViewContainer::writeStateChanged
);
131 connect(m_view
, &DolphinView::requestItemInfo
, this, &DolphinViewContainer::showItemInfo
);
132 connect(m_view
, &DolphinView::itemActivated
, this, &DolphinViewContainer::slotItemActivated
);
133 connect(m_view
, &DolphinView::fileMiddleClickActivated
, this, &DolphinViewContainer::slotfileMiddleClickActivated
);
134 connect(m_view
, &DolphinView::itemsActivated
, this, &DolphinViewContainer::slotItemsActivated
);
135 connect(m_view
, &DolphinView::redirection
, this, &DolphinViewContainer::redirect
);
136 connect(m_view
, &DolphinView::directoryLoadingStarted
, this, &DolphinViewContainer::slotDirectoryLoadingStarted
);
137 connect(m_view
, &DolphinView::directoryLoadingCompleted
, this, &DolphinViewContainer::slotDirectoryLoadingCompleted
);
138 connect(m_view
, &DolphinView::directoryLoadingCanceled
, this, &DolphinViewContainer::slotDirectoryLoadingCanceled
);
139 connect(m_view
, &DolphinView::itemCountChanged
, this, &DolphinViewContainer::delayedStatusBarUpdate
);
140 connect(m_view
, &DolphinView::directoryLoadingProgress
, this, &DolphinViewContainer::updateDirectoryLoadingProgress
);
141 connect(m_view
, &DolphinView::directorySortingProgress
, this, &DolphinViewContainer::updateDirectorySortingProgress
);
142 connect(m_view
, &DolphinView::selectionChanged
, this, &DolphinViewContainer::delayedStatusBarUpdate
);
143 connect(m_view
, &DolphinView::errorMessage
, this, &DolphinViewContainer::showErrorMessage
);
144 connect(m_view
, &DolphinView::urlIsFileError
, this, &DolphinViewContainer::slotUrlIsFileError
);
145 connect(m_view
, &DolphinView::activated
, this, &DolphinViewContainer::activate
);
146 connect(m_view
, &DolphinView::hiddenFilesShownChanged
, this, &DolphinViewContainer::slotHiddenFilesShownChanged
);
147 connect(m_view
, &DolphinView::sortHiddenLastChanged
, this, &DolphinViewContainer::slotSortHiddenLastChanged
);
148 connect(m_view
, &DolphinView::currentDirectoryRemoved
, this, &DolphinViewContainer::slotCurrentDirectoryRemoved
);
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_statusBar
, &DolphinStatusBar::stopPressed
, this, &DolphinViewContainer::stopDirectoryLoading
);
161 connect(m_statusBar
, &DolphinStatusBar::zoomLevelChanged
, this, &DolphinViewContainer::slotStatusBarZoomLevelChanged
);
163 m_statusBarTimer
= new QTimer(this);
164 m_statusBarTimer
->setSingleShot(true);
165 m_statusBarTimer
->setInterval(300);
166 connect(m_statusBarTimer
, &QTimer::timeout
, this, &DolphinViewContainer::updateStatusBar
);
168 KIO::FileUndoManager
*undoManager
= KIO::FileUndoManager::self();
169 connect(undoManager
, &KIO::FileUndoManager::jobRecordingFinished
, this, &DolphinViewContainer::delayedStatusBarUpdate
);
171 m_topLayout
->addWidget(m_searchBox
, positionFor
.searchBox
, 0);
172 m_topLayout
->addWidget(m_messageWidget
, positionFor
.messageWidget
, 0);
173 m_topLayout
->addWidget(m_view
, positionFor
.view
, 0);
174 m_topLayout
->addWidget(m_filterBar
, positionFor
.filterBar
, 0);
175 m_topLayout
->addWidget(m_statusBar
, positionFor
.statusBar
, 0);
177 setSearchModeEnabled(isSearchUrl(url
));
179 // Update view as the ContentDisplaySettings change
180 // this happens here and not in DolphinView as DolphinviewContainer and DolphinView are not in the same build target ATM
181 connect(ContentDisplaySettings::self(), &KCoreConfigSkeleton::configChanged
, m_view
, &DolphinView::reload
);
183 KFilePlacesModel
*placesModel
= DolphinPlacesModelSingleton::instance().placesModel();
184 connect(placesModel
, &KFilePlacesModel::dataChanged
, this, &DolphinViewContainer::slotPlacesModelChanged
);
185 connect(placesModel
, &KFilePlacesModel::rowsInserted
, this, &DolphinViewContainer::slotPlacesModelChanged
);
186 connect(placesModel
, &KFilePlacesModel::rowsRemoved
, this, &DolphinViewContainer::slotPlacesModelChanged
);
188 connect(this, &DolphinViewContainer::searchModeEnabledChanged
, this, &DolphinViewContainer::captionChanged
);
191 DolphinViewContainer::~DolphinViewContainer()
195 QUrl
DolphinViewContainer::url() const
197 return m_view
->url();
200 KFileItem
DolphinViewContainer::rootItem() const
202 return m_view
->rootItem();
205 void DolphinViewContainer::setActive(bool active
)
207 m_searchBox
->setActive(active
);
208 if (m_urlNavigatorConnected
) {
209 m_urlNavigatorConnected
->setActive(active
);
211 m_view
->setActive(active
);
214 bool DolphinViewContainer::isActive() const
216 return m_view
->isActive();
219 void DolphinViewContainer::setAutoGrabFocus(bool grab
)
221 m_autoGrabFocus
= grab
;
224 bool DolphinViewContainer::autoGrabFocus() const
226 return m_autoGrabFocus
;
229 QString
DolphinViewContainer::currentSearchText() const
231 return m_searchBox
->text();
234 const DolphinStatusBar
*DolphinViewContainer::statusBar() const
239 DolphinStatusBar
*DolphinViewContainer::statusBar()
244 const DolphinUrlNavigator
*DolphinViewContainer::urlNavigator() const
246 return m_urlNavigatorConnected
;
249 DolphinUrlNavigator
*DolphinViewContainer::urlNavigator()
251 return m_urlNavigatorConnected
;
254 const DolphinUrlNavigator
*DolphinViewContainer::urlNavigatorInternalWithHistory() const
256 return m_urlNavigator
.get();
259 DolphinUrlNavigator
*DolphinViewContainer::urlNavigatorInternalWithHistory()
261 return m_urlNavigator
.get();
264 const DolphinView
*DolphinViewContainer::view() const
269 DolphinView
*DolphinViewContainer::view()
274 void DolphinViewContainer::connectUrlNavigator(DolphinUrlNavigator
*urlNavigator
)
276 Q_CHECK_PTR(urlNavigator
);
277 Q_ASSERT(!m_urlNavigatorConnected
);
278 Q_ASSERT(m_urlNavigator
.get() != urlNavigator
);
281 urlNavigator
->setLocationUrl(m_view
->url());
282 urlNavigator
->setShowHiddenFolders(m_view
->hiddenFilesShown());
283 urlNavigator
->setSortHiddenFoldersLast(m_view
->sortHiddenLast());
284 if (m_urlNavigatorVisualState
) {
285 urlNavigator
->setVisualState(*m_urlNavigatorVisualState
.get());
286 m_urlNavigatorVisualState
.reset();
288 urlNavigator
->setActive(isActive());
290 // Url changes are still done via m_urlNavigator.
291 connect(urlNavigator
, &DolphinUrlNavigator::urlChanged
, m_urlNavigator
.get(), &DolphinUrlNavigator::setLocationUrl
);
292 connect(urlNavigator
, &DolphinUrlNavigator::urlsDropped
, this, [=](const QUrl
&destination
, QDropEvent
*event
) {
293 m_view
->dropUrls(destination
, event
, urlNavigator
->dropWidget());
295 // Aside from these, only visual things need to be connected.
296 connect(m_view
, &DolphinView::urlChanged
, urlNavigator
, &DolphinUrlNavigator::setLocationUrl
);
297 connect(urlNavigator
, &DolphinUrlNavigator::activated
, this, &DolphinViewContainer::activate
);
299 m_urlNavigatorConnected
= urlNavigator
;
302 void DolphinViewContainer::disconnectUrlNavigator()
304 if (!m_urlNavigatorConnected
) {
308 disconnect(m_urlNavigatorConnected
, &DolphinUrlNavigator::urlChanged
, m_urlNavigator
.get(), &DolphinUrlNavigator::setLocationUrl
);
309 disconnect(m_urlNavigatorConnected
, &DolphinUrlNavigator::urlsDropped
, this, nullptr);
310 disconnect(m_view
, &DolphinView::urlChanged
, m_urlNavigatorConnected
, &DolphinUrlNavigator::setLocationUrl
);
311 disconnect(m_urlNavigatorConnected
, &DolphinUrlNavigator::activated
, this, &DolphinViewContainer::activate
);
313 m_urlNavigatorVisualState
= m_urlNavigatorConnected
->visualState();
314 m_urlNavigatorConnected
= nullptr;
317 void DolphinViewContainer::setSelectionModeEnabled(bool enabled
, KActionCollection
*actionCollection
, SelectionMode::BottomBar::Contents bottomBarContents
)
319 const bool wasEnabled
= m_view
->selectionMode();
320 m_view
->setSelectionModeEnabled(enabled
);
324 return; // nothing to do here
326 Q_CHECK_PTR(m_selectionModeTopBar
); // there is no point in disabling selectionMode when it wasn't even enabled once.
327 Q_CHECK_PTR(m_selectionModeBottomBar
);
328 if (m_selectionModeTopBar
->isAncestorOf(QApplication::focusWidget()) || m_selectionModeBottomBar
->isAncestorOf(QApplication::focusWidget())) {
331 m_selectionModeTopBar
->setVisible(false, WithAnimation
);
332 m_selectionModeBottomBar
->setVisible(false, WithAnimation
);
333 Q_EMIT
selectionModeChanged(false);
337 if (!m_selectionModeTopBar
) {
338 // Changing the location will disable selection mode.
339 connect(m_urlNavigator
.get(), &DolphinUrlNavigator::urlChanged
, this, [this]() {
340 setSelectionModeEnabled(false);
343 m_selectionModeTopBar
= new SelectionMode::TopBar(this); // will be created hidden
344 connect(m_selectionModeTopBar
, &SelectionMode::TopBar::selectionModeLeavingRequested
, this, [this]() {
345 setSelectionModeEnabled(false);
347 m_topLayout
->addWidget(m_selectionModeTopBar
, positionFor
.selectionModeTopBar
, 0);
350 if (!m_selectionModeBottomBar
) {
351 m_selectionModeBottomBar
= new SelectionMode::BottomBar(actionCollection
, this);
352 connect(m_view
, &DolphinView::selectionChanged
, this, [this](const KFileItemList
&selection
) {
353 m_selectionModeBottomBar
->slotSelectionChanged(selection
, m_view
->url());
355 connect(m_selectionModeBottomBar
, &SelectionMode::BottomBar::error
, this, [this](const QString
&errorMessage
) {
356 showErrorMessage(errorMessage
);
358 connect(m_selectionModeBottomBar
, &SelectionMode::BottomBar::selectionModeLeavingRequested
, this, [this]() {
359 setSelectionModeEnabled(false);
361 m_topLayout
->addWidget(m_selectionModeBottomBar
, positionFor
.selectionModeBottomBar
, 0);
363 m_selectionModeBottomBar
->resetContents(bottomBarContents
);
364 if (bottomBarContents
== SelectionMode::BottomBar::GeneralContents
) {
365 m_selectionModeBottomBar
->slotSelectionChanged(m_view
->selectedItems(), m_view
->url());
369 m_selectionModeTopBar
->setVisible(true, WithAnimation
);
370 m_selectionModeBottomBar
->setVisible(true, WithAnimation
);
371 Q_EMIT
selectionModeChanged(true);
375 bool DolphinViewContainer::isSelectionModeEnabled() const
377 const bool isEnabled
= m_view
->selectionMode();
379 // We can't assert that the bars are invisible only because the selection mode is disabled because the hide animation might still be playing.
380 && (!m_selectionModeBottomBar
|| !m_selectionModeBottomBar
->isEnabled() || !m_selectionModeBottomBar
->isVisible()
381 || m_selectionModeBottomBar
->contents() == SelectionMode::BottomBar::PasteContents
))
382 || (isEnabled
&& m_selectionModeTopBar
383 && m_selectionModeTopBar
->isVisible()
384 // 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.
385 && m_selectionModeBottomBar
386 && (m_selectionModeBottomBar
->isVisible() || m_selectionModeBottomBar
->contents() == SelectionMode::BottomBar::GeneralContents
)));
390 void DolphinViewContainer::slotSplitTabDisabled()
392 if (m_selectionModeBottomBar
) {
393 m_selectionModeBottomBar
->slotSplitTabDisabled();
397 void DolphinViewContainer::showMessage(const QString
&msg
, MessageType type
)
403 m_messageWidget
->setText(msg
);
405 // TODO: wrap at arbitrary character positions once QLabel can do this
406 // https://bugreports.qt.io/browse/QTBUG-1276
407 m_messageWidget
->setWordWrap(true);
411 m_messageWidget
->setMessageType(KMessageWidget::Information
);
414 m_messageWidget
->setMessageType(KMessageWidget::Warning
);
417 m_messageWidget
->setMessageType(KMessageWidget::Error
);
424 m_messageWidget
->setWordWrap(false);
425 const int unwrappedWidth
= m_messageWidget
->sizeHint().width();
426 m_messageWidget
->setWordWrap(unwrappedWidth
> size().width());
428 if (m_messageWidget
->isVisible()) {
429 m_messageWidget
->hide();
431 m_messageWidget
->animatedShow();
434 void DolphinViewContainer::readSettings()
436 // The startup settings should (only) get applied if they have been
437 // modified by the user. Otherwise keep the (possibly) different current
438 // setting of the filterbar.
439 if (GeneralSettings::modifiedStartupSettings()) {
440 setFilterBarVisible(GeneralSettings::filterBar());
443 m_view
->readSettings();
444 m_statusBar
->readSettings();
447 bool DolphinViewContainer::isFilterBarVisible() const
449 return m_filterBar
->isVisible();
452 void DolphinViewContainer::setSearchModeEnabled(bool enabled
)
454 m_searchBox
->setVisible(enabled
);
457 const QUrl
&locationUrl
= m_urlNavigator
->locationUrl();
458 m_searchBox
->fromSearchUrl(locationUrl
);
461 if (enabled
== isSearchModeEnabled()) {
462 if (enabled
&& !m_searchBox
->hasFocus()) {
463 m_searchBox
->setFocus();
464 m_searchBox
->selectAll();
470 m_view
->setViewPropertiesContext(QString());
472 // Restore the URL for the URL navigator. If Dolphin has been
473 // started with a search-URL, the home URL is used as fallback.
474 QUrl url
= m_searchBox
->searchPath();
475 if (url
.isEmpty() || !url
.isValid() || isSearchUrl(url
)) {
476 url
= Dolphin::homeUrl();
478 m_urlNavigatorConnected
->setLocationUrl(url
);
481 m_searchModeEnabled
= enabled
;
483 Q_EMIT
searchModeEnabledChanged(enabled
);
486 bool DolphinViewContainer::isSearchModeEnabled() const
488 return m_searchModeEnabled
;
491 QString
DolphinViewContainer::placesText() const
495 if (isSearchModeEnabled()) {
496 text
= i18n("Search for %1 in %2", m_searchBox
->text(), m_searchBox
->searchPath().fileName());
498 text
= url().adjusted(QUrl::StripTrailingSlash
).fileName();
499 if (text
.isEmpty()) {
502 if (text
.isEmpty()) {
503 text
= url().scheme();
510 void DolphinViewContainer::reload()
513 m_messageWidget
->hide();
516 QString
DolphinViewContainer::captionWindowTitle() const
518 if (GeneralSettings::showFullPathInTitlebar() && !isSearchModeEnabled()) {
519 if (!url().isLocalFile()) {
520 return url().adjusted(QUrl::StripTrailingSlash
).toString();
522 return url().adjusted(QUrl::StripTrailingSlash
).path();
524 return DolphinViewContainer::caption();
528 QString
DolphinViewContainer::caption() const
530 if (isSearchModeEnabled()) {
531 if (currentSearchText().isEmpty()) {
532 return i18n("Search");
534 return i18n("Search for %1", currentSearchText());
538 KFilePlacesModel
*placesModel
= DolphinPlacesModelSingleton::instance().placesModel();
539 const QString pattern
= url().adjusted(QUrl::StripTrailingSlash
).toString(QUrl::FullyEncoded
).append("/?");
540 const auto &matchedPlaces
=
541 placesModel
->match(placesModel
->index(0, 0), KFilePlacesModel::UrlRole
, QRegularExpression::anchoredPattern(pattern
), 1, Qt::MatchRegularExpression
);
543 if (!matchedPlaces
.isEmpty()) {
544 return placesModel
->text(matchedPlaces
.first());
547 if (!url().isLocalFile()) {
548 QUrl adjustedUrl
= url().adjusted(QUrl::StripTrailingSlash
);
550 if (!adjustedUrl
.fileName().isEmpty()) {
551 caption
= adjustedUrl
.fileName();
552 } else if (!adjustedUrl
.path().isEmpty() && adjustedUrl
.path() != "/") {
553 caption
= adjustedUrl
.path();
554 } else if (!adjustedUrl
.host().isEmpty()) {
555 caption
= adjustedUrl
.host();
557 caption
= adjustedUrl
.toString();
562 QString fileName
= url().adjusted(QUrl::StripTrailingSlash
).fileName();
563 if (fileName
.isEmpty()) {
570 void DolphinViewContainer::setUrl(const QUrl
&newUrl
)
572 if (newUrl
!= m_urlNavigator
->locationUrl()) {
573 m_urlNavigator
->setLocationUrl(newUrl
);
577 KActivities::ResourceInstance::notifyAccessed(newUrl
);
581 void DolphinViewContainer::setFilterBarVisible(bool visible
)
583 Q_ASSERT(m_filterBar
);
585 m_view
->hideToolTip(ToolTipManager::HideBehavior::Instantly
);
587 m_filterBar
->setFocus();
588 m_filterBar
->selectAll();
594 void DolphinViewContainer::delayedStatusBarUpdate()
596 if (m_statusBarTimer
->isActive() && (m_statusBarTimestamp
.elapsed() > 2000)) {
597 // No update of the statusbar has been done during the last 2 seconds,
598 // although an update has been requested. Trigger an immediate update.
599 m_statusBarTimer
->stop();
602 // Invoke updateStatusBar() with a small delay. This assures that
603 // when a lot of delayedStatusBarUpdates() are done in a short time,
604 // no bottleneck is given.
605 m_statusBarTimer
->start();
609 void DolphinViewContainer::updateStatusBar()
611 m_statusBarTimestamp
.start();
612 m_view
->requestStatusBarText();
615 void DolphinViewContainer::updateDirectoryLoadingProgress(int percent
)
617 if (m_statusBar
->progressText().isEmpty()) {
618 m_statusBar
->setProgressText(i18nc("@info:progress", "Loading folder…"));
620 m_statusBar
->setProgress(percent
);
623 void DolphinViewContainer::updateDirectorySortingProgress(int percent
)
625 if (m_statusBar
->progressText().isEmpty()) {
626 m_statusBar
->setProgressText(i18nc("@info:progress", "Sorting…"));
628 m_statusBar
->setProgress(percent
);
631 void DolphinViewContainer::slotDirectoryLoadingStarted()
633 if (isSearchUrl(url())) {
634 // Search KIO-slaves usually don't provide any progress information. Give
635 // a hint to the user that a searching is done:
637 m_statusBar
->setProgressText(i18nc("@info", "Searching…"));
638 m_statusBar
->setProgress(-1);
640 // Trigger an undetermined progress indication. The progress
641 // information in percent will be triggered by the percent() signal
642 // of the directory lister later.
643 m_statusBar
->setProgressText(QString());
644 updateDirectoryLoadingProgress(-1);
648 void DolphinViewContainer::slotDirectoryLoadingCompleted()
650 if (!m_statusBar
->progressText().isEmpty()) {
651 m_statusBar
->setProgressText(QString());
652 m_statusBar
->setProgress(100);
655 if (isSearchUrl(url()) && m_view
->itemsCount() == 0) {
656 // The dir lister has been completed on a Baloo-URI and no items have been found. Instead
657 // of showing the default status bar information ("0 items") a more helpful information is given:
658 m_statusBar
->setText(i18nc("@info:status", "No items found."));
664 void DolphinViewContainer::slotDirectoryLoadingCanceled()
666 if (!m_statusBar
->progressText().isEmpty()) {
667 m_statusBar
->setProgressText(QString());
668 m_statusBar
->setProgress(100);
671 m_statusBar
->setText(QString());
674 void DolphinViewContainer::slotUrlIsFileError(const QUrl
&url
)
676 const KFileItem
item(url
);
678 // Find out if the file can be opened in the view (for example, this is the
679 // case if the file is an archive). The mime type must be known for that.
680 item
.determineMimeType();
681 const QUrl
&folderUrl
= DolphinView::openItemAsFolderUrl(item
, true);
682 if (!folderUrl
.isEmpty()) {
685 slotItemActivated(item
);
689 void DolphinViewContainer::slotItemActivated(const KFileItem
&item
)
691 // It is possible to activate items on inactive views by
692 // drag & drop operations. Assure that activating an item always
693 // results in an active view.
694 m_view
->setActive(true);
696 const QUrl
&url
= DolphinView::openItemAsFolderUrl(item
, GeneralSettings::browseThroughArchives());
697 if (!url
.isEmpty()) {
698 const auto modifiers
= QGuiApplication::keyboardModifiers();
699 // keep in sync with KUrlNavigator::slotNavigatorButtonClicked
700 if (modifiers
& Qt::ControlModifier
&& modifiers
& Qt::ShiftModifier
) {
701 Q_EMIT
activeTabRequested(url
);
702 } else if (modifiers
& Qt::ControlModifier
) {
703 Q_EMIT
tabRequested(url
);
704 } else if (modifiers
& Qt::ShiftModifier
) {
705 Dolphin::openNewWindow({KFilePlacesModel::convertedUrl(url
)}, this);
712 KIO::OpenUrlJob
*job
= new KIO::OpenUrlJob(item
.targetUrl(), item
.mimetype());
713 // Auto*Warning*Handling, errors are put in a KMessageWidget by us in slotOpenUrlFinished.
714 job
->setUiDelegate(KIO::createDefaultJobUiDelegate(KJobUiDelegate::AutoWarningHandlingEnabled
, this));
715 job
->setShowOpenOrExecuteDialog(true);
716 connect(job
, &KIO::OpenUrlJob::finished
, this, &DolphinViewContainer::slotOpenUrlFinished
);
720 void DolphinViewContainer::slotfileMiddleClickActivated(const KFileItem
&item
)
722 KService::List services
= KApplicationTrader::queryByMimeType(item
.mimetype());
724 int indexOfAppToOpenFileWith
= 1;
726 // executable scripts
727 auto mimeType
= item
.currentMimeType();
728 if (item
.isLocalFile() && mimeType
.inherits(QStringLiteral("application/x-executable")) && mimeType
.inherits(QStringLiteral("text/plain"))
729 && QFileInfo(item
.localPath()).isExecutable()) {
730 KConfigGroup
cfgGroup(KSharedConfig::openConfig(QStringLiteral("kiorc")), QStringLiteral("Executable scripts"));
731 const QString value
= cfgGroup
.readEntry("behaviourOnLaunch", "alwaysAsk");
733 // in case KIO::WidgetsOpenOrExecuteFileHandler::promptUserOpenOrExecute would not open the file
734 if (value
!= QLatin1String("open")) {
735 indexOfAppToOpenFileWith
= 0;
739 if (services
.length() >= indexOfAppToOpenFileWith
+ 1) {
740 auto service
= services
.at(indexOfAppToOpenFileWith
);
742 KIO::ApplicationLauncherJob
*job
= new KIO::ApplicationLauncherJob(service
, this);
743 job
->setUrls({item
.url()});
745 job
->setUiDelegate(KIO::createDefaultJobUiDelegate(KJobUiDelegate::AutoHandlingEnabled
, this));
746 connect(job
, &KIO::OpenUrlJob::finished
, this, &DolphinViewContainer::slotOpenUrlFinished
);
751 void DolphinViewContainer::slotItemsActivated(const KFileItemList
&items
)
753 Q_ASSERT(items
.count() >= 2);
755 KFileItemActions
fileItemActions(this);
756 fileItemActions
.runPreferredApplications(items
);
759 void DolphinViewContainer::showItemInfo(const KFileItem
&item
)
762 m_statusBar
->resetToDefaultText();
764 m_statusBar
->setText(item
.getStatusBarInfo());
768 void DolphinViewContainer::closeFilterBar()
770 m_filterBar
->closeFilterBar();
772 Q_EMIT
showFilterBarChanged(false);
775 void DolphinViewContainer::clearFilterBar()
777 m_filterBar
->clearIfUnlocked();
780 void DolphinViewContainer::setNameFilter(const QString
&nameFilter
)
782 m_view
->hideToolTip(ToolTipManager::HideBehavior::Instantly
);
783 m_view
->setNameFilter(nameFilter
);
784 delayedStatusBarUpdate();
787 void DolphinViewContainer::activate()
792 void DolphinViewContainer::slotUrlNavigatorLocationAboutToBeChanged(const QUrl
&)
797 void DolphinViewContainer::slotUrlNavigatorLocationChanged(const QUrl
&url
)
799 if (m_urlNavigatorConnected
) {
800 m_urlNavigatorConnected
->slotReturnPressed();
803 if (KProtocolManager::supportsListing(url
)) {
804 const bool searchBoxInitialized
= isSearchModeEnabled() && m_searchBox
->text().isEmpty();
805 setSearchModeEnabled(isSearchUrl(url
) || searchBoxInitialized
);
808 tryRestoreViewState();
810 if (m_autoGrabFocus
&& isActive() && !isSearchModeEnabled()) {
811 // When an URL has been entered, the view should get the focus.
812 // The focus must be requested asynchronously, as changing the URL might create
813 // a new view widget.
814 QTimer::singleShot(0, this, &DolphinViewContainer::requestFocus
);
816 } else if (KProtocolManager::isSourceProtocol(url
)) {
817 if (url
.scheme().startsWith(QLatin1String("http"))) {
818 showMessage(i18nc("@info:status", // krazy:exclude=qmethods
819 "Dolphin does not support web pages, the web browser has been launched"),
822 showMessage(i18nc("@info:status", "Protocol not supported by Dolphin, default application has been launched"), Information
);
825 QDesktopServices::openUrl(url
);
826 redirect(QUrl(), m_urlNavigator
->locationUrl(1));
828 if (!url
.scheme().isEmpty()) {
829 showMessage(i18nc("@info:status", "Invalid protocol '%1'", url
.scheme()), Error
);
831 showMessage(i18nc("@info:status", "Invalid protocol"), Error
);
833 m_urlNavigator
->goBack();
837 void DolphinViewContainer::slotUrlSelectionRequested(const QUrl
&url
)
839 m_view
->markUrlsAsSelected({url
});
840 m_view
->markUrlAsCurrent(url
); // makes the item scroll into view
843 void DolphinViewContainer::disableUrlNavigatorSelectionRequests()
845 disconnect(m_urlNavigator
.get(), &KUrlNavigator::urlSelectionRequested
, this, &DolphinViewContainer::slotUrlSelectionRequested
);
848 void DolphinViewContainer::enableUrlNavigatorSelectionRequests()
850 connect(m_urlNavigator
.get(), &KUrlNavigator::urlSelectionRequested
, this, &DolphinViewContainer::slotUrlSelectionRequested
);
853 void DolphinViewContainer::redirect(const QUrl
&oldUrl
, const QUrl
&newUrl
)
856 const bool block
= m_urlNavigator
->signalsBlocked();
857 m_urlNavigator
->blockSignals(true);
859 // Assure that the location state is reset for redirection URLs. This
860 // allows to skip redirection URLs when going back or forward in the
862 m_urlNavigator
->saveLocationState(QByteArray());
863 m_urlNavigator
->setLocationUrl(newUrl
);
864 setSearchModeEnabled(isSearchUrl(newUrl
));
866 m_urlNavigator
->blockSignals(block
);
869 void DolphinViewContainer::requestFocus()
874 void DolphinViewContainer::startSearching()
876 Q_CHECK_PTR(m_urlNavigatorConnected
);
877 const QUrl url
= m_searchBox
->urlForSearching();
878 if (url
.isValid() && !url
.isEmpty()) {
879 m_view
->setViewPropertiesContext(QStringLiteral("search"));
880 m_urlNavigatorConnected
->setLocationUrl(url
);
884 void DolphinViewContainer::openSearchBox()
886 setSearchModeEnabled(true);
889 void DolphinViewContainer::closeSearchBox()
891 setSearchModeEnabled(false);
894 void DolphinViewContainer::stopDirectoryLoading()
896 m_view
->stopLoading();
897 m_statusBar
->setProgress(100);
900 void DolphinViewContainer::slotStatusBarZoomLevelChanged(int zoomLevel
)
902 m_view
->setZoomLevel(zoomLevel
);
905 void DolphinViewContainer::showErrorMessage(const QString
&msg
)
907 showMessage(msg
, Error
);
910 void DolphinViewContainer::slotPlacesModelChanged()
912 if (!GeneralSettings::showFullPathInTitlebar() && !isSearchModeEnabled()) {
913 Q_EMIT
captionChanged();
917 void DolphinViewContainer::slotHiddenFilesShownChanged(bool showHiddenFiles
)
919 if (m_urlNavigatorConnected
) {
920 m_urlNavigatorConnected
->setShowHiddenFolders(showHiddenFiles
);
924 void DolphinViewContainer::slotSortHiddenLastChanged(bool hiddenLast
)
926 if (m_urlNavigatorConnected
) {
927 m_urlNavigatorConnected
->setSortHiddenFoldersLast(hiddenLast
);
931 void DolphinViewContainer::slotCurrentDirectoryRemoved()
933 const QString
location(url().toDisplayString(QUrl::PreferLocalFile
));
934 if (url().isLocalFile()) {
935 const QString dirPath
= url().toLocalFile();
936 const QString newPath
= getNearestExistingAncestorOfPath(dirPath
);
937 const QUrl newUrl
= QUrl::fromLocalFile(newPath
);
941 showMessage(xi18n("Current location changed, <filename>%1</filename> is no longer accessible.", location
), Warning
);
944 void DolphinViewContainer::slotOpenUrlFinished(KJob
*job
)
946 if (job
->error() && job
->error() != KIO::ERR_USER_CANCELED
) {
947 showErrorMessage(job
->errorString());
951 bool DolphinViewContainer::isSearchUrl(const QUrl
&url
) const
953 return url
.scheme().contains(QLatin1String("search"));
956 void DolphinViewContainer::saveViewState()
958 QByteArray locationState
;
959 QDataStream
stream(&locationState
, QIODevice::WriteOnly
);
960 m_view
->saveState(stream
);
961 m_urlNavigator
->saveLocationState(locationState
);
964 void DolphinViewContainer::tryRestoreViewState()
966 QByteArray locationState
= m_urlNavigator
->locationState();
967 if (!locationState
.isEmpty()) {
968 QDataStream
stream(&locationState
, QIODevice::ReadOnly
);
969 m_view
->restoreState(stream
);
973 QString
DolphinViewContainer::getNearestExistingAncestorOfPath(const QString
&path
) const
977 dir
.setPath(QDir::cleanPath(dir
.filePath(QStringLiteral(".."))));
978 } while (!dir
.exists() && !dir
.isRoot());
980 return dir
.exists() ? dir
.path() : QString
{};
983 #include "moc_dolphinviewcontainer.cpp"