2 * SPDX-FileCopyrightText: 2007 Peter Penz <peter.penz19@gmail.com>
4 * SPDX-License-Identifier: GPL-2.0-or-later
7 #include "dolphinviewcontainer.h"
10 #include "admin/workerintegration.h"
11 #include "dolphin_compactmodesettings.h"
12 #include "dolphin_contentdisplaysettings.h"
13 #include "dolphin_detailsmodesettings.h"
14 #include "dolphin_generalsettings.h"
15 #include "dolphin_iconsmodesettings.h"
16 #include "dolphindebug.h"
17 #include "dolphinplacesmodelsingleton.h"
18 #include "filterbar/filterbar.h"
20 #include "kitemviews/kitemlistcontainer.h"
21 #include "search/dolphinsearchbox.h"
22 #include "selectionmode/topbar.h"
23 #include "statusbar/dolphinstatusbar.h"
25 #include <KActionCollection>
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 #ifndef QT_NO_ACCESSIBILITY
38 #include <QAccessible>
40 #include <QApplication>
41 #include <QDesktopServices>
43 #include <QGridLayout>
44 #include <QGuiApplication>
45 #include <QRegularExpression>
52 // An overview of the widgets contained by this ViewContainer
53 struct LayoutStructure
{
56 int messageWidget
= 2;
57 int selectionModeTopBar
= 3;
59 int selectionModeBottomBar
= 5;
63 constexpr LayoutStructure positionFor
;
65 DolphinViewContainer::DolphinViewContainer(const QUrl
&url
, QWidget
*parent
)
67 , m_topLayout(nullptr)
68 , m_urlNavigator
{new DolphinUrlNavigator(url
)}
69 , m_urlNavigatorConnected
{nullptr}
70 , m_searchBox(nullptr)
71 , m_searchModeEnabled(false)
73 , m_authorizeToEnterFolderAction
{nullptr}
74 , m_messageWidget(nullptr)
75 , m_selectionModeTopBar
{nullptr}
77 , m_filterBar(nullptr)
78 , m_selectionModeBottomBar
{nullptr}
79 , m_statusBar(nullptr)
80 , m_statusBarTimer(nullptr)
81 , m_statusBarTimestamp()
82 , m_autoGrabFocus(true)
86 m_topLayout
= new QGridLayout(this);
87 m_topLayout
->setSpacing(0);
88 m_topLayout
->setContentsMargins(0, 0, 0, 0);
90 m_searchBox
= new DolphinSearchBox(this);
91 m_searchBox
->setVisible(false, WithoutAnimation
);
92 connect(m_searchBox
, &DolphinSearchBox::activated
, this, &DolphinViewContainer::activate
);
93 connect(m_searchBox
, &DolphinSearchBox::openRequest
, this, &DolphinViewContainer::openSearchBox
);
94 connect(m_searchBox
, &DolphinSearchBox::closeRequest
, this, &DolphinViewContainer::closeSearchBox
);
95 connect(m_searchBox
, &DolphinSearchBox::searchRequest
, this, &DolphinViewContainer::startSearching
);
96 connect(m_searchBox
, &DolphinSearchBox::focusViewRequest
, this, &DolphinViewContainer::requestFocus
);
97 m_searchBox
->setWhatsThis(xi18nc("@info:whatsthis findbar",
98 "<para>This helps you find files and folders. Enter a <emphasis>"
99 "search term</emphasis> and specify search settings with the "
100 "buttons at the bottom:<list><item>Filename/Content: "
101 "Does the item you are looking for contain the search terms "
102 "within its filename or its contents?<nl/>The contents of images, "
103 "audio files and videos will not be searched.</item><item>"
104 "From Here/Everywhere: Do you want to search in this "
105 "folder and its sub-folders or everywhere?</item><item>"
106 "More Options: Click this to search by media type, access "
107 "time or rating.</item><item>More Search Tools: Install other "
108 "means to find an item.</item></list></para>"));
110 m_messageWidget
= new KMessageWidget(this);
111 m_messageWidget
->setCloseButtonVisible(true);
112 m_messageWidget
->setPosition(KMessageWidget::Header
);
113 m_messageWidget
->hide();
115 #if !defined(Q_OS_WIN) && !defined(Q_OS_HAIKU)
117 // We must be logged in as the root user; show a big scary warning
118 showMessage(i18n("Running Dolphin as root can be dangerous. Please be careful."), KMessageWidget::Warning
);
122 // Initialize filter bar
123 m_filterBar
= new FilterBar(this);
124 m_filterBar
->setVisible(GeneralSettings::filterBar(), WithoutAnimation
);
126 connect(m_filterBar
, &FilterBar::filterChanged
, this, &DolphinViewContainer::setNameFilter
);
127 connect(m_filterBar
, &FilterBar::closeRequest
, this, &DolphinViewContainer::closeFilterBar
);
128 connect(m_filterBar
, &FilterBar::focusViewRequest
, this, &DolphinViewContainer::requestFocus
);
130 // Initialize the main view
131 m_view
= new DolphinView(url
, this);
132 connect(m_view
, &DolphinView::urlChanged
, m_filterBar
, &FilterBar::clearIfUnlocked
);
133 connect(m_view
, &DolphinView::urlChanged
, m_messageWidget
, &KMessageWidget::hide
);
134 // m_urlNavigator stays in sync with m_view's location changes and
135 // keeps track of them so going back and forth in the history works.
136 connect(m_view
, &DolphinView::urlChanged
, m_urlNavigator
.get(), &DolphinUrlNavigator::setLocationUrl
);
137 connect(m_urlNavigator
.get(), &DolphinUrlNavigator::urlChanged
, this, &DolphinViewContainer::slotUrlNavigatorLocationChanged
);
138 connect(m_urlNavigator
.get(), &DolphinUrlNavigator::urlAboutToBeChanged
, this, &DolphinViewContainer::slotUrlNavigatorLocationAboutToBeChanged
);
139 connect(m_urlNavigator
.get(), &DolphinUrlNavigator::urlSelectionRequested
, this, &DolphinViewContainer::slotUrlSelectionRequested
);
140 connect(m_view
, &DolphinView::writeStateChanged
, this, &DolphinViewContainer::writeStateChanged
);
141 connect(m_view
, &DolphinView::requestItemInfo
, this, &DolphinViewContainer::showItemInfo
);
142 connect(m_view
, &DolphinView::itemActivated
, this, &DolphinViewContainer::slotItemActivated
);
143 connect(m_view
, &DolphinView::fileMiddleClickActivated
, this, &DolphinViewContainer::slotfileMiddleClickActivated
);
144 connect(m_view
, &DolphinView::itemsActivated
, this, &DolphinViewContainer::slotItemsActivated
);
145 connect(m_view
, &DolphinView::redirection
, this, &DolphinViewContainer::redirect
);
146 connect(m_view
, &DolphinView::directoryLoadingStarted
, this, &DolphinViewContainer::slotDirectoryLoadingStarted
);
147 connect(m_view
, &DolphinView::directoryLoadingCompleted
, this, &DolphinViewContainer::slotDirectoryLoadingCompleted
);
148 connect(m_view
, &DolphinView::directoryLoadingCanceled
, this, &DolphinViewContainer::slotDirectoryLoadingCanceled
);
149 connect(m_view
, &DolphinView::itemCountChanged
, this, &DolphinViewContainer::delayedStatusBarUpdate
);
150 connect(m_view
, &DolphinView::selectionChanged
, this, &DolphinViewContainer::delayedStatusBarUpdate
);
151 connect(m_view
, &DolphinView::errorMessage
, this, &DolphinViewContainer::slotErrorMessageFromView
);
152 connect(m_view
, &DolphinView::urlIsFileError
, this, &DolphinViewContainer::slotUrlIsFileError
);
153 connect(m_view
, &DolphinView::activated
, this, &DolphinViewContainer::activate
);
154 connect(m_view
, &DolphinView::hiddenFilesShownChanged
, this, &DolphinViewContainer::slotHiddenFilesShownChanged
);
155 connect(m_view
, &DolphinView::sortHiddenLastChanged
, this, &DolphinViewContainer::slotSortHiddenLastChanged
);
156 connect(m_view
, &DolphinView::currentDirectoryRemoved
, this, &DolphinViewContainer::slotCurrentDirectoryRemoved
);
158 // Initialize status bar
159 m_statusBar
= new DolphinStatusBar(this);
160 m_statusBar
->setUrl(m_view
->url());
161 m_statusBar
->setZoomLevel(m_view
->zoomLevel());
162 connect(m_view
, &DolphinView::urlChanged
, m_statusBar
, &DolphinStatusBar::setUrl
);
163 connect(m_view
, &DolphinView::zoomLevelChanged
, m_statusBar
, &DolphinStatusBar::setZoomLevel
);
164 connect(m_view
, &DolphinView::infoMessage
, m_statusBar
, &DolphinStatusBar::setText
);
165 connect(m_view
, &DolphinView::operationCompletedMessage
, m_statusBar
, &DolphinStatusBar::setText
);
166 connect(m_view
, &DolphinView::statusBarTextChanged
, m_statusBar
, &DolphinStatusBar::setDefaultText
);
167 connect(m_view
, &DolphinView::statusBarTextChanged
, m_statusBar
, &DolphinStatusBar::resetToDefaultText
);
168 connect(m_view
, &DolphinView::directoryLoadingProgress
, m_statusBar
, [this](int percent
) {
169 m_statusBar
->showProgress(i18nc("@info:progress", "Loading folder…"), percent
);
171 connect(m_view
, &DolphinView::directorySortingProgress
, m_statusBar
, [this](int percent
) {
172 m_statusBar
->showProgress(i18nc("@info:progress", "Sorting…"), percent
);
174 connect(m_statusBar
, &DolphinStatusBar::stopPressed
, this, &DolphinViewContainer::stopDirectoryLoading
);
175 connect(m_statusBar
, &DolphinStatusBar::zoomLevelChanged
, this, &DolphinViewContainer::slotStatusBarZoomLevelChanged
);
176 connect(m_statusBar
, &DolphinStatusBar::showMessage
, this, [this](const QString
&message
, KMessageWidget::MessageType messageType
) {
177 showMessage(message
, messageType
);
179 connect(m_statusBar
, &DolphinStatusBar::widthUpdated
, this, &DolphinViewContainer::updateStatusBarGeometry
);
180 connect(m_statusBar
, &DolphinStatusBar::urlChanged
, this, &DolphinViewContainer::updateStatusBar
);
181 connect(this, &DolphinViewContainer::showFilterBarChanged
, this, &DolphinViewContainer::updateStatusBar
);
183 m_statusBarTimer
= new QTimer(this);
184 m_statusBarTimer
->setSingleShot(true);
185 m_statusBarTimer
->setInterval(300);
186 connect(m_statusBarTimer
, &QTimer::timeout
, this, &DolphinViewContainer::updateStatusBar
);
188 KIO::FileUndoManager
*undoManager
= KIO::FileUndoManager::self();
189 connect(undoManager
, &KIO::FileUndoManager::jobRecordingFinished
, this, &DolphinViewContainer::delayedStatusBarUpdate
);
191 m_topLayout
->addWidget(m_searchBox
, positionFor
.searchBox
, 0);
192 m_topLayout
->addWidget(m_messageWidget
, positionFor
.messageWidget
, 0);
193 m_topLayout
->addWidget(m_view
, positionFor
.view
, 0);
194 m_topLayout
->addWidget(m_filterBar
, positionFor
.filterBar
, 0);
195 if (GeneralSettings::showStatusBar() == GeneralSettings::EnumShowStatusBar::FullWidth
) {
196 m_topLayout
->addWidget(m_statusBar
, positionFor
.statusBar
, 0);
198 connect(m_statusBar
, &DolphinStatusBar::modeUpdated
, this, [this]() {
199 const bool statusBarInLayout
= m_topLayout
->itemAtPosition(positionFor
.statusBar
, 0);
200 if (GeneralSettings::showStatusBar() == GeneralSettings::EnumShowStatusBar::FullWidth
) {
201 if (!statusBarInLayout
) {
202 m_topLayout
->addWidget(m_statusBar
, positionFor
.statusBar
, 0);
203 m_statusBar
->setUrl(m_view
->url());
206 if (statusBarInLayout
) {
207 m_topLayout
->removeWidget(m_statusBar
);
210 updateStatusBarGeometry();
212 m_statusBar
->setHidden(false);
214 setSearchModeEnabled(isSearchUrl(url
));
216 // Update view as the ContentDisplaySettings change
217 // this happens here and not in DolphinView as DolphinviewContainer and DolphinView are not in the same build target ATM
218 connect(ContentDisplaySettings::self(), &KCoreConfigSkeleton::configChanged
, m_view
, &DolphinView::reload
);
220 KFilePlacesModel
*placesModel
= DolphinPlacesModelSingleton::instance().placesModel();
221 connect(placesModel
, &KFilePlacesModel::dataChanged
, this, &DolphinViewContainer::slotPlacesModelChanged
);
222 connect(placesModel
, &KFilePlacesModel::rowsInserted
, this, &DolphinViewContainer::slotPlacesModelChanged
);
223 connect(placesModel
, &KFilePlacesModel::rowsRemoved
, this, &DolphinViewContainer::slotPlacesModelChanged
);
225 connect(this, &DolphinViewContainer::searchModeEnabledChanged
, this, &DolphinViewContainer::captionChanged
);
227 QApplication::instance()->installEventFilter(this);
230 DolphinViewContainer::~DolphinViewContainer()
234 QUrl
DolphinViewContainer::url() const
236 return m_view
->url();
239 KFileItem
DolphinViewContainer::rootItem() const
241 return m_view
->rootItem();
244 void DolphinViewContainer::setActive(bool active
)
246 m_searchBox
->setActive(active
);
247 if (m_urlNavigatorConnected
) {
248 m_urlNavigatorConnected
->setActive(active
);
250 m_view
->setActive(active
);
253 bool DolphinViewContainer::isActive() const
255 return m_view
->isActive();
258 void DolphinViewContainer::setAutoGrabFocus(bool grab
)
260 m_autoGrabFocus
= grab
;
263 bool DolphinViewContainer::autoGrabFocus() const
265 return m_autoGrabFocus
;
268 QString
DolphinViewContainer::currentSearchText() const
270 return m_searchBox
->text();
273 const DolphinStatusBar
*DolphinViewContainer::statusBar() const
278 DolphinStatusBar
*DolphinViewContainer::statusBar()
283 const DolphinUrlNavigator
*DolphinViewContainer::urlNavigator() const
285 return m_urlNavigatorConnected
;
288 DolphinUrlNavigator
*DolphinViewContainer::urlNavigator()
290 return m_urlNavigatorConnected
;
293 const DolphinUrlNavigator
*DolphinViewContainer::urlNavigatorInternalWithHistory() const
295 return m_urlNavigator
.get();
298 DolphinUrlNavigator
*DolphinViewContainer::urlNavigatorInternalWithHistory()
300 return m_urlNavigator
.get();
303 const DolphinView
*DolphinViewContainer::view() const
308 DolphinView
*DolphinViewContainer::view()
313 void DolphinViewContainer::connectUrlNavigator(DolphinUrlNavigator
*urlNavigator
)
315 Q_CHECK_PTR(urlNavigator
);
316 Q_ASSERT(!m_urlNavigatorConnected
);
317 Q_ASSERT(m_urlNavigator
.get() != urlNavigator
);
320 urlNavigator
->setLocationUrl(m_view
->url());
321 urlNavigator
->setShowHiddenFolders(m_view
->hiddenFilesShown());
322 urlNavigator
->setSortHiddenFoldersLast(m_view
->sortHiddenLast());
323 if (m_urlNavigatorVisualState
) {
324 urlNavigator
->setVisualState(*m_urlNavigatorVisualState
.get());
325 m_urlNavigatorVisualState
.reset();
327 urlNavigator
->setActive(isActive());
329 // Url changes are still done via m_urlNavigator.
330 connect(urlNavigator
, &DolphinUrlNavigator::urlChanged
, m_urlNavigator
.get(), &DolphinUrlNavigator::setLocationUrl
);
331 connect(urlNavigator
, &DolphinUrlNavigator::urlsDropped
, this, [=, this](const QUrl
&destination
, QDropEvent
*event
) {
332 m_view
->dropUrls(destination
, event
, urlNavigator
->dropWidget());
334 // Aside from these, only visual things need to be connected.
335 connect(m_view
, &DolphinView::urlChanged
, urlNavigator
, &DolphinUrlNavigator::setLocationUrl
);
336 connect(urlNavigator
, &DolphinUrlNavigator::activated
, this, &DolphinViewContainer::activate
);
337 connect(urlNavigator
, &DolphinUrlNavigator::requestToLoseFocus
, m_view
, [this]() {
341 urlNavigator
->setReadOnlyBadgeVisible(rootItem().isLocalFile() && !rootItem().isWritable());
343 m_urlNavigatorConnected
= urlNavigator
;
346 void DolphinViewContainer::disconnectUrlNavigator()
348 if (!m_urlNavigatorConnected
) {
352 disconnect(m_urlNavigatorConnected
, &DolphinUrlNavigator::urlChanged
, m_urlNavigator
.get(), &DolphinUrlNavigator::setLocationUrl
);
353 disconnect(m_urlNavigatorConnected
, &DolphinUrlNavigator::urlsDropped
, this, nullptr);
354 disconnect(m_view
, &DolphinView::urlChanged
, m_urlNavigatorConnected
, &DolphinUrlNavigator::setLocationUrl
);
355 disconnect(m_urlNavigatorConnected
, &DolphinUrlNavigator::activated
, this, &DolphinViewContainer::activate
);
356 disconnect(m_urlNavigatorConnected
, &DolphinUrlNavigator::requestToLoseFocus
, m_view
, nullptr);
358 m_urlNavigatorVisualState
= m_urlNavigatorConnected
->visualState();
359 m_urlNavigatorConnected
= nullptr;
362 void DolphinViewContainer::setSelectionModeEnabled(bool enabled
, KActionCollection
*actionCollection
, SelectionMode::BottomBar::Contents bottomBarContents
)
364 const bool wasEnabled
= m_view
->selectionMode();
365 m_view
->setSelectionModeEnabled(enabled
);
369 return; // nothing to do here
371 Q_CHECK_PTR(m_selectionModeTopBar
); // there is no point in disabling selectionMode when it wasn't even enabled once.
372 Q_CHECK_PTR(m_selectionModeBottomBar
);
373 m_selectionModeTopBar
->setVisible(false, WithAnimation
);
374 m_selectionModeBottomBar
->setVisible(false, WithAnimation
);
375 Q_EMIT
selectionModeChanged(false);
377 if (!QApplication::focusWidget() || m_selectionModeTopBar
->isAncestorOf(QApplication::focusWidget())
378 || m_selectionModeBottomBar
->isAncestorOf(QApplication::focusWidget())) {
384 if (!m_selectionModeTopBar
) {
385 // Changing the location will disable selection mode.
386 connect(m_urlNavigator
.get(), &DolphinUrlNavigator::urlChanged
, this, [this]() {
387 setSelectionModeEnabled(false);
390 m_selectionModeTopBar
= new SelectionMode::TopBar(this); // will be created hidden
391 connect(m_selectionModeTopBar
, &SelectionMode::TopBar::selectionModeLeavingRequested
, this, [this]() {
392 setSelectionModeEnabled(false);
394 m_topLayout
->addWidget(m_selectionModeTopBar
, positionFor
.selectionModeTopBar
, 0);
397 if (!m_selectionModeBottomBar
) {
398 m_selectionModeBottomBar
= new SelectionMode::BottomBar(actionCollection
, this);
399 connect(m_view
, &DolphinView::selectionChanged
, this, [this](const KFileItemList
&selection
) {
400 m_selectionModeBottomBar
->slotSelectionChanged(selection
, m_view
->url());
402 connect(m_selectionModeBottomBar
, &SelectionMode::BottomBar::error
, this, &DolphinViewContainer::showErrorMessage
);
403 connect(m_selectionModeBottomBar
, &SelectionMode::BottomBar::selectionModeLeavingRequested
, this, [this]() {
404 setSelectionModeEnabled(false);
406 m_topLayout
->addWidget(m_selectionModeBottomBar
, positionFor
.selectionModeBottomBar
, 0);
408 m_selectionModeBottomBar
->resetContents(bottomBarContents
);
409 if (bottomBarContents
== SelectionMode::BottomBar::GeneralContents
) {
410 m_selectionModeBottomBar
->slotSelectionChanged(m_view
->selectedItems(), m_view
->url());
414 m_selectionModeTopBar
->setVisible(true, WithAnimation
);
415 m_selectionModeBottomBar
->setVisible(true, WithAnimation
);
416 Q_EMIT
selectionModeChanged(true);
420 bool DolphinViewContainer::isSelectionModeEnabled() const
422 const bool isEnabled
= m_view
->selectionMode();
424 // We can't assert that the bars are invisible only because the selection mode is disabled because the hide animation might still be playing.
425 && (!m_selectionModeBottomBar
|| !m_selectionModeBottomBar
->isEnabled() || !m_selectionModeBottomBar
->isVisible()
426 || m_selectionModeBottomBar
->contents() == SelectionMode::BottomBar::PasteContents
))
427 || (isEnabled
&& m_selectionModeTopBar
428 && m_selectionModeTopBar
->isVisible()
429 // 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.
430 && m_selectionModeBottomBar
431 && (m_selectionModeBottomBar
->isVisible() || m_selectionModeBottomBar
->contents() == SelectionMode::BottomBar::GeneralContents
)));
435 void DolphinViewContainer::slotSplitTabDisabled()
437 if (m_selectionModeBottomBar
) {
438 m_selectionModeBottomBar
->slotSplitTabDisabled();
442 void DolphinViewContainer::showMessage(const QString
&message
, KMessageWidget::MessageType messageType
, std::initializer_list
<QAction
*> buttonActions
)
444 if (message
.isEmpty()) {
448 m_messageWidget
->setText(message
);
450 // TODO: wrap at arbitrary character positions once QLabel can do this
451 // https://bugreports.qt.io/browse/QTBUG-1276
452 m_messageWidget
->setWordWrap(true);
453 m_messageWidget
->setMessageType(messageType
);
455 const QList
<QAction
*> previousMessageWidgetActions
= m_messageWidget
->actions();
456 for (auto action
: previousMessageWidgetActions
) {
457 m_messageWidget
->removeAction(action
);
459 for (QAction
*action
: buttonActions
) {
460 m_messageWidget
->addAction(action
);
463 m_messageWidget
->setWordWrap(false);
464 const int unwrappedWidth
= m_messageWidget
->sizeHint().width();
465 m_messageWidget
->setWordWrap(unwrappedWidth
> size().width());
467 if (m_messageWidget
->isVisible()) {
468 m_messageWidget
->hide();
470 m_messageWidget
->animatedShow();
472 #ifndef QT_NO_ACCESSIBILITY
473 if (QAccessible::isActive() && isActive()) {
474 // To announce the new message keyboard focus must be moved to the message label. However, we do not have direct access to the label that is internal
475 // to the KMessageWidget. Instead we setFocus() on the KMessageWidget and trust that it has set correct focus handling.
476 m_messageWidget
->setFocus();
481 void DolphinViewContainer::readSettings()
483 // The startup settings should (only) get applied if they have been
484 // modified by the user. Otherwise keep the (possibly) different current
485 // setting of the filterbar.
486 if (GeneralSettings::modifiedStartupSettings()) {
487 setFilterBarVisible(GeneralSettings::filterBar());
490 m_view
->readSettings();
491 m_statusBar
->readSettings();
494 bool DolphinViewContainer::isFilterBarVisible() const
496 return m_filterBar
->isEnabled(); // Gets disabled in AnimatedHeightWidget while animating towards a hidden state.
499 void DolphinViewContainer::setSearchModeEnabled(bool enabled
)
501 m_searchBox
->setVisible(enabled
, WithAnimation
);
504 const QUrl
&locationUrl
= m_urlNavigator
->locationUrl();
505 m_searchBox
->fromSearchUrl(locationUrl
);
508 if (enabled
== isSearchModeEnabled()) {
509 if (enabled
&& !m_searchBox
->hasFocus()) {
510 m_searchBox
->setFocus();
511 m_searchBox
->selectAll();
517 m_view
->setViewPropertiesContext(QString());
519 // Restore the URL for the URL navigator. If Dolphin has been
520 // started with a search-URL, the home URL is used as fallback.
521 QUrl url
= m_searchBox
->searchPath();
522 if (url
.isEmpty() || !url
.isValid() || isSearchUrl(url
)) {
523 url
= Dolphin::homeUrl();
525 if (m_urlNavigatorConnected
) {
526 m_urlNavigatorConnected
->setLocationUrl(url
);
530 m_searchModeEnabled
= enabled
;
532 Q_EMIT
searchModeEnabledChanged(enabled
);
535 bool DolphinViewContainer::isSearchModeEnabled() const
537 return m_searchModeEnabled
;
540 QString
DolphinViewContainer::placesText() const
544 if (isSearchModeEnabled()) {
545 text
= i18n("Search for %1 in %2", m_searchBox
->text(), m_searchBox
->searchPath().fileName());
547 text
= url().adjusted(QUrl::StripTrailingSlash
).fileName();
548 if (text
.isEmpty()) {
551 if (text
.isEmpty()) {
552 text
= url().scheme();
559 void DolphinViewContainer::reload()
562 m_messageWidget
->hide();
565 QString
DolphinViewContainer::captionWindowTitle() const
567 if (GeneralSettings::showFullPathInTitlebar() && !isSearchModeEnabled()) {
568 if (!url().isLocalFile()) {
569 return url().adjusted(QUrl::StripTrailingSlash
).toString();
571 return url().adjusted(QUrl::StripTrailingSlash
).path();
573 return DolphinViewContainer::caption();
577 QString
DolphinViewContainer::caption() const
579 // see KUrlNavigatorPrivate::firstButtonText().
580 if (url().path().isEmpty() || url().path() == QLatin1Char('/')) {
581 QUrlQuery
query(url());
582 const QString title
= query
.queryItemValue(QStringLiteral("title"));
583 if (!title
.isEmpty()) {
588 if (isSearchModeEnabled()) {
589 if (currentSearchText().isEmpty()) {
590 return i18n("Search");
592 return i18n("Search for %1", currentSearchText());
596 KFilePlacesModel
*placesModel
= DolphinPlacesModelSingleton::instance().placesModel();
598 QModelIndex url_index
= placesModel
->closestItem(url());
600 if (url_index
.isValid() && placesModel
->url(url_index
).matches(url(), QUrl::StripTrailingSlash
)) {
601 return placesModel
->text(url_index
);
604 if (!url().isLocalFile()) {
605 QUrl adjustedUrl
= url().adjusted(QUrl::StripTrailingSlash
);
607 if (!adjustedUrl
.fileName().isEmpty()) {
608 caption
= adjustedUrl
.fileName();
609 } else if (!adjustedUrl
.path().isEmpty() && adjustedUrl
.path() != "/") {
610 caption
= adjustedUrl
.path();
611 } else if (!adjustedUrl
.host().isEmpty()) {
612 caption
= adjustedUrl
.host();
614 caption
= adjustedUrl
.toString();
619 QString fileName
= url().adjusted(QUrl::StripTrailingSlash
).fileName();
620 if (fileName
.isEmpty()) {
627 void DolphinViewContainer::setUrl(const QUrl
&newUrl
)
629 if (newUrl
!= m_urlNavigator
->locationUrl()) {
630 m_urlNavigator
->setLocationUrl(newUrl
);
634 void DolphinViewContainer::setFilterBarVisible(bool visible
)
636 Q_ASSERT(m_filterBar
);
638 m_view
->hideToolTip(ToolTipManager::HideBehavior::Instantly
);
639 m_filterBar
->setVisible(true, WithAnimation
);
640 m_filterBar
->setFocus();
641 m_filterBar
->selectAll();
642 Q_EMIT
showFilterBarChanged(true);
648 void DolphinViewContainer::delayedStatusBarUpdate()
650 if (m_statusBarTimer
->isActive() && (m_statusBarTimestamp
.elapsed() > 2000)) {
651 // No update of the statusbar has been done during the last 2 seconds,
652 // although an update has been requested. Trigger an immediate update.
653 m_statusBarTimer
->stop();
656 // Invoke updateStatusBar() with a small delay. This assures that
657 // when a lot of delayedStatusBarUpdates() are done in a short time,
658 // no bottleneck is given.
659 m_statusBarTimer
->start();
663 void DolphinViewContainer::updateStatusBar()
665 m_statusBarTimestamp
.start();
666 m_view
->requestStatusBarText();
667 updateStatusBarGeometry();
670 void DolphinViewContainer::slotDirectoryLoadingStarted()
672 if (isSearchUrl(url())) {
673 // Search KIO-slaves usually don't provide any progress information. Give
674 // a hint to the user that a searching is done:
676 m_statusBar
->showProgress(i18nc("@info", "Searching…"), -1);
678 // Trigger an undetermined progress indication. The progress
679 // information in percent will be triggered by the percent() signal
680 // of the directory lister later.
681 m_statusBar
->showProgress(QString(), -1);
684 if (m_urlNavigatorConnected
) {
685 m_urlNavigatorConnected
->setReadOnlyBadgeVisible(false);
689 void DolphinViewContainer::slotDirectoryLoadingCompleted()
691 m_statusBar
->showProgress(QString(), 100);
693 if (isSearchUrl(url()) && m_view
->itemsCount() == 0) {
694 // The dir lister has been completed on a Baloo-URI and no items have been found. Instead
695 // of showing the default status bar information ("0 items") a more helpful information is given:
696 m_statusBar
->setText(i18nc("@info:status", "No items found."));
701 if (m_urlNavigatorConnected
) {
702 m_urlNavigatorConnected
->setReadOnlyBadgeVisible(rootItem().isLocalFile() && !rootItem().isWritable());
705 // Update admin bar visibility
706 if (m_view
->url().scheme() == QStringLiteral("admin")) {
708 m_adminBar
= new Admin::Bar(this);
709 m_topLayout
->addWidget(m_adminBar
, positionFor
.adminBar
, 0);
711 m_adminBar
->setVisible(true, WithAnimation
);
712 } else if (m_adminBar
) {
713 m_adminBar
->setVisible(false, WithAnimation
);
717 void DolphinViewContainer::slotDirectoryLoadingCanceled()
719 m_statusBar
->showProgress(QString(), 100);
720 m_statusBar
->setText(QString());
723 void DolphinViewContainer::slotUrlIsFileError(const QUrl
&url
)
725 const KFileItem
item(url
);
727 // Find out if the file can be opened in the view (for example, this is the
728 // case if the file is an archive). The mime type must be known for that.
729 item
.determineMimeType();
730 const QUrl
&folderUrl
= DolphinView::openItemAsFolderUrl(item
, true);
731 if (!folderUrl
.isEmpty()) {
734 slotItemActivated(item
);
738 void DolphinViewContainer::slotItemActivated(const KFileItem
&item
)
740 // It is possible to activate items on inactive views by
741 // drag & drop operations. Assure that activating an item always
742 // results in an active view.
743 m_view
->setActive(true);
745 const QUrl
&url
= DolphinView::openItemAsFolderUrl(item
, GeneralSettings::browseThroughArchives());
746 if (!url
.isEmpty()) {
747 const auto modifiers
= QGuiApplication::keyboardModifiers();
748 // keep in sync with KUrlNavigator::slotNavigatorButtonClicked
749 if (modifiers
& Qt::ControlModifier
&& modifiers
& Qt::ShiftModifier
) {
750 Q_EMIT
activeTabRequested(url
);
751 } else if (modifiers
& Qt::ControlModifier
) {
752 Q_EMIT
tabRequested(url
);
753 } else if (modifiers
& Qt::ShiftModifier
) {
754 Dolphin::openNewWindow({KFilePlacesModel::convertedUrl(url
)}, this);
761 KIO::OpenUrlJob
*job
= new KIO::OpenUrlJob(item
.targetUrl(), item
.mimetype());
762 // Auto*Warning*Handling, errors are put in a KMessageWidget by us in slotOpenUrlFinished.
763 job
->setUiDelegate(KIO::createDefaultJobUiDelegate(KJobUiDelegate::AutoWarningHandlingEnabled
, this));
764 job
->setShowOpenOrExecuteDialog(true);
765 connect(job
, &KIO::OpenUrlJob::finished
, this, &DolphinViewContainer::slotOpenUrlFinished
);
769 void DolphinViewContainer::slotfileMiddleClickActivated(const KFileItem
&item
)
771 KService::List services
= KApplicationTrader::queryByMimeType(item
.mimetype());
773 int indexOfAppToOpenFileWith
= 1;
775 // executable scripts
776 auto mimeType
= item
.currentMimeType();
777 if (item
.isLocalFile() && mimeType
.inherits(QStringLiteral("application/x-executable")) && mimeType
.inherits(QStringLiteral("text/plain"))
778 && QFileInfo(item
.localPath()).isExecutable()) {
779 KConfigGroup
cfgGroup(KSharedConfig::openConfig(QStringLiteral("kiorc")), QStringLiteral("Executable scripts"));
780 const QString value
= cfgGroup
.readEntry("behaviourOnLaunch", "alwaysAsk");
782 // in case KIO::WidgetsOpenOrExecuteFileHandler::promptUserOpenOrExecute would not open the file
783 if (value
!= QLatin1String("open")) {
784 indexOfAppToOpenFileWith
= 0;
788 if (services
.length() >= indexOfAppToOpenFileWith
+ 1) {
789 auto service
= services
.at(indexOfAppToOpenFileWith
);
791 KIO::ApplicationLauncherJob
*job
= new KIO::ApplicationLauncherJob(service
, this);
792 job
->setUrls({item
.url()});
794 job
->setUiDelegate(KIO::createDefaultJobUiDelegate(KJobUiDelegate::AutoHandlingEnabled
, this));
795 connect(job
, &KIO::OpenUrlJob::finished
, this, &DolphinViewContainer::slotOpenUrlFinished
);
798 // If no 2nd service available, try to open archives in new tabs, regardless of the "Open archives as folder" setting.
799 const QUrl
&url
= DolphinView::openItemAsFolderUrl(item
);
800 const auto modifiers
= QGuiApplication::keyboardModifiers();
801 if (!url
.isEmpty()) {
802 // keep in sync with KUrlNavigator::slotNavigatorButtonClicked
803 if (modifiers
& Qt::ShiftModifier
) {
804 Q_EMIT
activeTabRequested(url
);
806 Q_EMIT
tabRequested(url
);
812 void DolphinViewContainer::slotItemsActivated(const KFileItemList
&items
)
814 Q_ASSERT(items
.count() >= 2);
816 KFileItemActions
fileItemActions(this);
817 fileItemActions
.runPreferredApplications(items
);
820 void DolphinViewContainer::showItemInfo(const KFileItem
&item
)
823 m_statusBar
->resetToDefaultText();
825 m_statusBar
->setText(item
.getStatusBarInfo());
829 void DolphinViewContainer::closeFilterBar()
831 m_filterBar
->closeFilterBar();
833 Q_EMIT
showFilterBarChanged(false);
836 void DolphinViewContainer::clearFilterBar()
838 m_filterBar
->clearIfUnlocked();
841 void DolphinViewContainer::setNameFilter(const QString
&nameFilter
)
843 m_view
->hideToolTip(ToolTipManager::HideBehavior::Instantly
);
844 m_view
->setNameFilter(nameFilter
);
845 delayedStatusBarUpdate();
848 void DolphinViewContainer::activate()
853 void DolphinViewContainer::slotUrlNavigatorLocationAboutToBeChanged(const QUrl
&)
858 void DolphinViewContainer::slotUrlNavigatorLocationChanged(const QUrl
&url
)
860 if (m_urlNavigatorConnected
) {
861 m_urlNavigatorConnected
->slotReturnPressed();
864 if (KProtocolManager::supportsListing(url
)) {
865 const bool searchBoxInitialized
= isSearchModeEnabled() && m_searchBox
->text().isEmpty();
866 setSearchModeEnabled(isSearchUrl(url
) || searchBoxInitialized
);
869 tryRestoreViewState();
871 if (m_autoGrabFocus
&& isActive() && !isSearchModeEnabled()) {
872 // When an URL has been entered, the view should get the focus.
873 // The focus must be requested asynchronously, as changing the URL might create
874 // a new view widget.
875 QTimer::singleShot(0, this, &DolphinViewContainer::requestFocus
);
877 } else if (KProtocolManager::isSourceProtocol(url
)) {
878 if (url
.scheme().startsWith(QLatin1String("http"))) {
879 showMessage(i18nc("@info:status", // krazy:exclude=qmethods
880 "Dolphin does not support web pages, the web browser has been launched"),
881 KMessageWidget::Information
);
883 showMessage(i18nc("@info:status", "Protocol not supported by Dolphin, default application has been launched"), KMessageWidget::Information
);
886 QDesktopServices::openUrl(url
);
887 redirect(QUrl(), m_urlNavigator
->locationUrl(1));
889 if (!url
.scheme().isEmpty()) {
890 showMessage(i18nc("@info:status", "Invalid protocol '%1'", url
.scheme()), KMessageWidget::Error
);
892 showMessage(i18nc("@info:status", "Invalid protocol"), KMessageWidget::Error
);
894 m_urlNavigator
->goBack();
898 void DolphinViewContainer::slotUrlSelectionRequested(const QUrl
&url
)
900 // We do not want to select any item here because there is no reason to assume that the user wants to edit the folder we are emerging from. BUG: 424723
902 m_view
->markUrlAsCurrent(url
); // makes the item scroll into view
905 void DolphinViewContainer::disableUrlNavigatorSelectionRequests()
907 disconnect(m_urlNavigator
.get(), &KUrlNavigator::urlSelectionRequested
, this, &DolphinViewContainer::slotUrlSelectionRequested
);
910 void DolphinViewContainer::enableUrlNavigatorSelectionRequests()
912 connect(m_urlNavigator
.get(), &KUrlNavigator::urlSelectionRequested
, this, &DolphinViewContainer::slotUrlSelectionRequested
);
915 void DolphinViewContainer::redirect(const QUrl
&oldUrl
, const QUrl
&newUrl
)
918 const bool block
= m_urlNavigator
->signalsBlocked();
919 m_urlNavigator
->blockSignals(true);
921 // Assure that the location state is reset for redirection URLs. This
922 // allows to skip redirection URLs when going back or forward in the
924 m_urlNavigator
->saveLocationState(QByteArray());
925 m_urlNavigator
->setLocationUrl(newUrl
);
926 if (m_searchBox
->isActive()) {
927 m_searchBox
->setSearchPath(newUrl
);
929 setSearchModeEnabled(isSearchUrl(newUrl
));
931 m_urlNavigator
->blockSignals(block
);
934 void DolphinViewContainer::requestFocus()
939 void DolphinViewContainer::startSearching()
941 Q_CHECK_PTR(m_urlNavigatorConnected
);
942 const QUrl url
= m_searchBox
->urlForSearching();
943 if (url
.isValid() && !url
.isEmpty()) {
944 m_view
->setViewPropertiesContext(QStringLiteral("search"));
945 // If we open a new tab that has a search assigned to it, we can't
946 // update the urlNavigator, since there is none connected to that tab.
948 if (m_urlNavigatorConnected
) {
949 m_urlNavigatorConnected
->setLocationUrl(url
);
954 void DolphinViewContainer::openSearchBox()
956 setSearchModeEnabled(true);
959 void DolphinViewContainer::closeSearchBox()
961 setSearchModeEnabled(false);
964 void DolphinViewContainer::stopDirectoryLoading()
966 m_view
->stopLoading();
967 m_statusBar
->showProgress(QString(), 100);
970 void DolphinViewContainer::slotStatusBarZoomLevelChanged(int zoomLevel
)
972 m_view
->setZoomLevel(zoomLevel
);
975 void DolphinViewContainer::slotErrorMessageFromView(const QString
&message
, const int kioErrorCode
)
977 if (kioErrorCode
== KIO::ERR_CANNOT_ENTER_DIRECTORY
&& m_view
->url().scheme() == QStringLiteral("file")
978 && KProtocolInfo::isKnownProtocol(QStringLiteral("admin")) && !rootItem().isReadable()) {
979 // Explain to users that they need authentication to see the folder contents.
980 if (!m_authorizeToEnterFolderAction
) { // This code is similar to parts of Admin::Bar::hideTheNextTimeAuthorizationExpires().
981 // We should not simply use the actAsAdminAction() itself here because that one always refers to the active view instead of this->m_view.
982 auto actAsAdminAction
= Admin::WorkerIntegration::FriendAccess::actAsAdminAction();
983 m_authorizeToEnterFolderAction
= new QAction
{actAsAdminAction
->icon(), actAsAdminAction
->text(), this};
984 m_authorizeToEnterFolderAction
->setToolTip(actAsAdminAction
->toolTip());
985 m_authorizeToEnterFolderAction
->setWhatsThis(actAsAdminAction
->whatsThis());
986 connect(m_authorizeToEnterFolderAction
, &QAction::triggered
, this, [this, actAsAdminAction
]() {
988 actAsAdminAction
->trigger();
991 showMessage(i18nc("@info", "Authorization required to enter this folder."), KMessageWidget::Error
, {m_authorizeToEnterFolderAction
});
994 Q_EMIT
showErrorMessage(message
);
997 void DolphinViewContainer::showErrorMessage(const QString
&message
)
999 showMessage(message
, KMessageWidget::Error
);
1002 void DolphinViewContainer::slotPlacesModelChanged()
1004 if (!GeneralSettings::showFullPathInTitlebar() && !isSearchModeEnabled()) {
1005 Q_EMIT
captionChanged();
1009 void DolphinViewContainer::slotHiddenFilesShownChanged(bool showHiddenFiles
)
1011 if (m_urlNavigatorConnected
) {
1012 m_urlNavigatorConnected
->setShowHiddenFolders(showHiddenFiles
);
1016 void DolphinViewContainer::slotSortHiddenLastChanged(bool hiddenLast
)
1018 if (m_urlNavigatorConnected
) {
1019 m_urlNavigatorConnected
->setSortHiddenFoldersLast(hiddenLast
);
1023 void DolphinViewContainer::slotCurrentDirectoryRemoved()
1025 const QString
location(url().toDisplayString(QUrl::PreferLocalFile
));
1026 if (url().isLocalFile()) {
1027 const QString dirPath
= url().toLocalFile();
1028 const QString newPath
= getNearestExistingAncestorOfPath(dirPath
);
1029 const QUrl newUrl
= QUrl::fromLocalFile(newPath
);
1030 // #473377: Delay changing the url to avoid modifying KCoreDirLister before KCoreDirListerCache::deleteDir() returns.
1031 QTimer::singleShot(0, this, [&, newUrl
, location
] {
1033 showMessage(xi18n("Current location changed, <filename>%1</filename> is no longer accessible.", location
), KMessageWidget::Warning
);
1036 showMessage(xi18n("Current location changed, <filename>%1</filename> is no longer accessible.", location
), KMessageWidget::Warning
);
1039 void DolphinViewContainer::slotOpenUrlFinished(KJob
*job
)
1041 if (job
->error() && job
->error() != KIO::ERR_USER_CANCELED
) {
1042 showErrorMessage(job
->errorString());
1046 bool DolphinViewContainer::isSearchUrl(const QUrl
&url
) const
1048 return url
.scheme().contains(QLatin1String("search"));
1051 void DolphinViewContainer::saveViewState()
1053 QByteArray locationState
;
1054 QDataStream
stream(&locationState
, QIODevice::WriteOnly
);
1055 m_view
->saveState(stream
);
1056 m_urlNavigator
->saveLocationState(locationState
);
1059 void DolphinViewContainer::tryRestoreViewState()
1061 QByteArray locationState
= m_urlNavigator
->locationState();
1062 if (!locationState
.isEmpty()) {
1063 QDataStream
stream(&locationState
, QIODevice::ReadOnly
);
1064 m_view
->restoreState(stream
);
1068 QString
DolphinViewContainer::getNearestExistingAncestorOfPath(const QString
&path
) const
1072 dir
.setPath(QDir::cleanPath(dir
.filePath(QStringLiteral(".."))));
1073 } while (!dir
.exists() && !dir
.isRoot());
1075 return dir
.exists() ? dir
.path() : QString
{};
1078 void DolphinViewContainer::updateStatusBarGeometry()
1083 if (GeneralSettings::showStatusBar() == GeneralSettings::EnumShowStatusBar::Small
) {
1084 QRect
statusBarRect(preferredSmallStatusBarGeometry());
1085 if (view()->layoutDirection() == Qt::RightToLeft
) {
1086 const int splitterWidth
= m_statusBar
->clippingAmount();
1087 statusBarRect
.setLeft(rect().width() - m_statusBar
->width() + splitterWidth
); // Add clipping amount.
1089 // Move statusbar to bottomLeft, or bottomRight with right-to-left-layout.
1090 m_statusBar
->setGeometry(statusBarRect
);
1091 // Add 1 due to how qrect coordinates work.
1092 m_view
->setStatusBarOffset(m_statusBar
->geometry().height() - m_statusBar
->clippingAmount() + 1);
1094 m_view
->setStatusBarOffset(0);
1098 bool DolphinViewContainer::eventFilter(QObject
*object
, QEvent
*event
)
1100 if (GeneralSettings::showStatusBar() == GeneralSettings::EnumShowStatusBar::Small
&& object
== m_view
) {
1101 switch (event
->type()) {
1102 case QEvent::Resize
: {
1103 m_statusBar
->updateWidthToContent();
1106 case QEvent::LayoutRequest
: {
1107 m_statusBar
->updateWidthToContent();
1117 QRect
DolphinViewContainer::preferredSmallStatusBarGeometry()
1119 // Adjust to clipping, we need to add 1 due to how QRects coordinates work.
1120 int clipAdjustment
= m_statusBar
->clippingAmount() + 1;
1121 // Add offset depending if horizontal scrollbar or filterbar is visible.
1122 const int yPos
= m_view
->geometry().bottom() - m_view
->horizontalScrollBarHeight() - m_statusBar
->minimumHeight() + clipAdjustment
;
1123 QRect statusBarRect
= rect().adjusted(-clipAdjustment
, yPos
, 0, 0);
1124 return statusBarRect
;
1127 #include "moc_dolphinviewcontainer.cpp"