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/bar.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/MkpathJob>
31 #include <KIO/OpenUrlJob>
32 #include <KLocalizedString>
33 #include <KMessageWidget>
34 #include <KProtocolManager>
36 #include <kio_version.h>
38 #ifndef QT_NO_ACCESSIBILITY
39 #include <QAccessible>
41 #include <QApplication>
42 #include <QDesktopServices>
44 #include <QGridLayout>
45 #include <QGuiApplication>
46 #include <QRegularExpression>
53 bool isSearchUrl(const QUrl
&url
)
55 return url
.scheme().contains(QLatin1String("search"));
58 // An overview of the widgets contained by this ViewContainer
59 struct LayoutStructure
{
62 int messageWidget
= 2;
63 int selectionModeTopBar
= 3;
65 int selectionModeBottomBar
= 5;
69 constexpr LayoutStructure positionFor
;
71 DolphinViewContainer::DolphinViewContainer(const QUrl
&url
, QWidget
*parent
)
73 , m_topLayout(nullptr)
74 , m_urlNavigator
{new DolphinUrlNavigator(url
)}
75 , m_urlNavigatorConnected
{nullptr}
76 , m_searchBar(nullptr)
77 , m_searchModeEnabled(false)
79 , m_authorizeToEnterFolderAction
{nullptr}
80 , m_createFolderAction(nullptr)
81 , m_messageWidget(nullptr)
82 , m_selectionModeTopBar
{nullptr}
84 , m_filterBar(nullptr)
85 , m_selectionModeBottomBar
{nullptr}
86 , m_statusBar(nullptr)
87 , m_statusBarTimer(nullptr)
88 , m_statusBarTimestamp()
89 , m_grabFocusOnUrlChange
{true}
93 m_topLayout
= new QGridLayout(this);
94 m_topLayout
->setSpacing(0);
95 m_topLayout
->setContentsMargins(0, 0, 0, 0);
97 m_messageWidget
= new KMessageWidget(this);
98 m_messageWidget
->setCloseButtonVisible(true);
99 m_messageWidget
->setPosition(KMessageWidget::Header
);
100 m_messageWidget
->hide();
102 #if !defined(Q_OS_WIN) && !defined(Q_OS_HAIKU)
104 // We must be logged in as the root user; show a big scary warning
105 showMessage(i18n("Running Dolphin as root can be dangerous. Please be careful."), KMessageWidget::Warning
);
109 // Initialize filter bar
110 m_filterBar
= new FilterBar(this);
111 m_filterBar
->setVisible(GeneralSettings::filterBar(), WithoutAnimation
);
113 connect(m_filterBar
, &FilterBar::filterChanged
, this, &DolphinViewContainer::setNameFilter
);
114 connect(m_filterBar
, &FilterBar::closeRequest
, this, &DolphinViewContainer::closeFilterBar
);
115 connect(m_filterBar
, &FilterBar::focusViewRequest
, this, &DolphinViewContainer::requestFocus
);
117 // Initialize the main view
118 m_view
= new DolphinView(url
, this);
119 connect(m_view
, &DolphinView::urlChanged
, m_filterBar
, &FilterBar::clearIfUnlocked
);
120 connect(m_view
, &DolphinView::urlChanged
, m_messageWidget
, &KMessageWidget::hide
);
121 // m_urlNavigator stays in sync with m_view's location changes and
122 // keeps track of them so going back and forth in the history works.
123 connect(m_view
, &DolphinView::urlChanged
, m_urlNavigator
.get(), &DolphinUrlNavigator::setLocationUrl
);
124 connect(m_urlNavigator
.get(), &DolphinUrlNavigator::urlChanged
, this, &DolphinViewContainer::slotUrlNavigatorLocationChanged
);
125 connect(m_urlNavigator
.get(), &DolphinUrlNavigator::urlAboutToBeChanged
, this, &DolphinViewContainer::slotUrlNavigatorLocationAboutToBeChanged
);
126 connect(m_urlNavigator
.get(), &DolphinUrlNavigator::urlSelectionRequested
, this, &DolphinViewContainer::slotUrlSelectionRequested
);
127 connect(m_view
, &DolphinView::writeStateChanged
, this, &DolphinViewContainer::writeStateChanged
);
128 connect(m_view
, &DolphinView::requestItemInfo
, this, &DolphinViewContainer::showItemInfo
);
129 connect(m_view
, &DolphinView::itemActivated
, this, &DolphinViewContainer::slotItemActivated
);
130 connect(m_view
, &DolphinView::fileMiddleClickActivated
, this, &DolphinViewContainer::slotfileMiddleClickActivated
);
131 connect(m_view
, &DolphinView::itemsActivated
, this, &DolphinViewContainer::slotItemsActivated
);
132 connect(m_view
, &DolphinView::redirection
, this, &DolphinViewContainer::redirect
);
133 connect(m_view
, &DolphinView::directoryLoadingStarted
, this, &DolphinViewContainer::slotDirectoryLoadingStarted
);
134 connect(m_view
, &DolphinView::directoryLoadingCompleted
, this, &DolphinViewContainer::slotDirectoryLoadingCompleted
);
135 connect(m_view
, &DolphinView::directoryLoadingCanceled
, this, &DolphinViewContainer::slotDirectoryLoadingCanceled
);
136 connect(m_view
, &DolphinView::itemCountChanged
, this, &DolphinViewContainer::delayedStatusBarUpdate
);
137 connect(m_view
, &DolphinView::selectionChanged
, this, &DolphinViewContainer::delayedStatusBarUpdate
);
138 connect(m_view
, &DolphinView::errorMessage
, this, &DolphinViewContainer::slotErrorMessageFromView
);
139 connect(m_view
, &DolphinView::urlIsFileError
, this, &DolphinViewContainer::slotUrlIsFileError
);
140 connect(m_view
, &DolphinView::activated
, this, &DolphinViewContainer::activate
);
141 connect(m_view
, &DolphinView::hiddenFilesShownChanged
, this, &DolphinViewContainer::slotHiddenFilesShownChanged
);
142 connect(m_view
, &DolphinView::sortHiddenLastChanged
, this, &DolphinViewContainer::slotSortHiddenLastChanged
);
143 connect(m_view
, &DolphinView::currentDirectoryRemoved
, this, &DolphinViewContainer::slotCurrentDirectoryRemoved
);
145 // Initialize status bar
146 m_statusBar
= new DolphinStatusBar(this);
147 m_statusBar
->setUrl(m_view
->url());
148 m_statusBar
->setZoomLevel(m_view
->zoomLevel());
149 connect(m_view
, &DolphinView::urlChanged
, m_statusBar
, &DolphinStatusBar::setUrl
);
150 connect(m_view
, &DolphinView::zoomLevelChanged
, m_statusBar
, &DolphinStatusBar::setZoomLevel
);
151 connect(m_view
, &DolphinView::infoMessage
, m_statusBar
, &DolphinStatusBar::setText
);
152 connect(m_view
, &DolphinView::operationCompletedMessage
, m_statusBar
, &DolphinStatusBar::setText
);
153 connect(m_view
, &DolphinView::statusBarTextChanged
, m_statusBar
, &DolphinStatusBar::setDefaultText
);
154 connect(m_view
, &DolphinView::statusBarTextChanged
, m_statusBar
, &DolphinStatusBar::resetToDefaultText
);
155 connect(m_view
, &DolphinView::directoryLoadingProgress
, m_statusBar
, [this](int percent
) {
156 m_statusBar
->showProgress(i18nc("@info:progress", "Loading folder…"), percent
);
158 connect(m_view
, &DolphinView::directorySortingProgress
, m_statusBar
, [this](int percent
) {
159 m_statusBar
->showProgress(i18nc("@info:progress", "Sorting…"), percent
);
161 connect(m_statusBar
, &DolphinStatusBar::stopPressed
, this, &DolphinViewContainer::stopDirectoryLoading
);
162 connect(m_statusBar
, &DolphinStatusBar::zoomLevelChanged
, this, &DolphinViewContainer::slotStatusBarZoomLevelChanged
);
163 connect(m_statusBar
, &DolphinStatusBar::showMessage
, this, [this](const QString
&message
, KMessageWidget::MessageType messageType
) {
164 showMessage(message
, messageType
);
166 connect(m_statusBar
, &DolphinStatusBar::widthUpdated
, this, &DolphinViewContainer::updateStatusBarGeometry
);
167 connect(m_statusBar
, &DolphinStatusBar::urlChanged
, this, &DolphinViewContainer::updateStatusBar
);
168 connect(this, &DolphinViewContainer::showFilterBarChanged
, this, &DolphinViewContainer::updateStatusBar
);
170 m_statusBarTimer
= new QTimer(this);
171 m_statusBarTimer
->setSingleShot(true);
172 m_statusBarTimer
->setInterval(300);
173 connect(m_statusBarTimer
, &QTimer::timeout
, this, &DolphinViewContainer::updateStatusBar
);
175 KIO::FileUndoManager
*undoManager
= KIO::FileUndoManager::self();
176 connect(undoManager
, &KIO::FileUndoManager::jobRecordingFinished
, this, &DolphinViewContainer::delayedStatusBarUpdate
);
178 m_topLayout
->addWidget(m_messageWidget
, positionFor
.messageWidget
, 0);
179 m_topLayout
->addWidget(m_view
, positionFor
.view
, 0);
180 m_topLayout
->addWidget(m_filterBar
, positionFor
.filterBar
, 0);
181 if (GeneralSettings::showStatusBar() == GeneralSettings::EnumShowStatusBar::FullWidth
) {
182 m_topLayout
->addWidget(m_statusBar
, positionFor
.statusBar
, 0);
184 connect(m_statusBar
, &DolphinStatusBar::modeUpdated
, this, [this]() {
185 const bool statusBarInLayout
= m_topLayout
->itemAtPosition(positionFor
.statusBar
, 0);
186 if (GeneralSettings::showStatusBar() == GeneralSettings::EnumShowStatusBar::FullWidth
) {
187 if (!statusBarInLayout
) {
188 m_topLayout
->addWidget(m_statusBar
, positionFor
.statusBar
, 0);
189 m_statusBar
->setUrl(m_view
->url());
192 if (statusBarInLayout
) {
193 m_topLayout
->removeWidget(m_statusBar
);
196 updateStatusBarGeometry();
198 m_statusBar
->setHidden(false);
200 setSearchBarVisible(isSearchUrl(url
));
202 // Update view as the ContentDisplaySettings change
203 // this happens here and not in DolphinView as DolphinviewContainer and DolphinView are not in the same build target ATM
204 connect(ContentDisplaySettings::self(), &KCoreConfigSkeleton::configChanged
, m_view
, &DolphinView::reload
);
206 KFilePlacesModel
*placesModel
= DolphinPlacesModelSingleton::instance().placesModel();
207 connect(placesModel
, &KFilePlacesModel::dataChanged
, this, &DolphinViewContainer::slotPlacesModelChanged
);
208 connect(placesModel
, &KFilePlacesModel::rowsInserted
, this, &DolphinViewContainer::slotPlacesModelChanged
);
209 connect(placesModel
, &KFilePlacesModel::rowsRemoved
, this, &DolphinViewContainer::slotPlacesModelChanged
);
211 QApplication::instance()->installEventFilter(this);
214 DolphinViewContainer::~DolphinViewContainer()
218 QUrl
DolphinViewContainer::url() const
220 return m_view
->url();
223 KFileItem
DolphinViewContainer::rootItem() const
225 return m_view
->rootItem();
228 void DolphinViewContainer::setActive(bool active
)
230 if (m_urlNavigatorConnected
) {
231 m_urlNavigatorConnected
->setActive(active
);
233 m_view
->setActive(active
);
236 bool DolphinViewContainer::isActive() const
238 return m_view
->isActive();
241 void DolphinViewContainer::setGrabFocusOnUrlChange(bool grabFocus
)
243 m_grabFocusOnUrlChange
= grabFocus
;
246 const DolphinStatusBar
*DolphinViewContainer::statusBar() const
251 DolphinStatusBar
*DolphinViewContainer::statusBar()
256 const DolphinUrlNavigator
*DolphinViewContainer::urlNavigator() const
258 return m_urlNavigatorConnected
;
261 DolphinUrlNavigator
*DolphinViewContainer::urlNavigator()
263 return m_urlNavigatorConnected
;
266 const DolphinUrlNavigator
*DolphinViewContainer::urlNavigatorInternalWithHistory() const
268 return m_urlNavigator
.get();
271 DolphinUrlNavigator
*DolphinViewContainer::urlNavigatorInternalWithHistory()
273 return m_urlNavigator
.get();
276 const DolphinView
*DolphinViewContainer::view() const
281 DolphinView
*DolphinViewContainer::view()
286 void DolphinViewContainer::connectUrlNavigator(DolphinUrlNavigator
*urlNavigator
)
288 Q_CHECK_PTR(urlNavigator
);
289 Q_ASSERT(!m_urlNavigatorConnected
);
290 Q_ASSERT(m_urlNavigator
.get() != urlNavigator
);
293 urlNavigator
->setLocationUrl(m_view
->url());
294 urlNavigator
->setShowHiddenFolders(m_view
->hiddenFilesShown());
295 urlNavigator
->setSortHiddenFoldersLast(m_view
->sortHiddenLast());
296 if (m_urlNavigatorVisualState
) {
297 urlNavigator
->setVisualState(*m_urlNavigatorVisualState
.get());
298 m_urlNavigatorVisualState
.reset();
300 urlNavigator
->setActive(isActive());
302 // Url changes are still done via m_urlNavigator.
303 connect(urlNavigator
, &DolphinUrlNavigator::urlChanged
, m_urlNavigator
.get(), &DolphinUrlNavigator::setLocationUrl
);
304 connect(urlNavigator
, &DolphinUrlNavigator::urlsDropped
, this, [=, this](const QUrl
&destination
, QDropEvent
*event
) {
305 m_view
->dropUrls(destination
, event
, urlNavigator
->dropWidget());
307 // Aside from these, only visual things need to be connected.
308 connect(m_view
, &DolphinView::urlChanged
, urlNavigator
, &DolphinUrlNavigator::setLocationUrl
);
309 connect(urlNavigator
, &DolphinUrlNavigator::activated
, this, &DolphinViewContainer::activate
);
310 connect(urlNavigator
, &DolphinUrlNavigator::requestToLoseFocus
, m_view
, [this]() {
314 urlNavigator
->setReadOnlyBadgeVisible(rootItem().isLocalFile() && !rootItem().isWritable());
316 m_urlNavigatorConnected
= urlNavigator
;
319 void DolphinViewContainer::disconnectUrlNavigator()
321 if (!m_urlNavigatorConnected
) {
325 disconnect(m_urlNavigatorConnected
, &DolphinUrlNavigator::urlChanged
, m_urlNavigator
.get(), &DolphinUrlNavigator::setLocationUrl
);
326 disconnect(m_urlNavigatorConnected
, &DolphinUrlNavigator::urlsDropped
, this, nullptr);
327 disconnect(m_view
, &DolphinView::urlChanged
, m_urlNavigatorConnected
, &DolphinUrlNavigator::setLocationUrl
);
328 disconnect(m_urlNavigatorConnected
, &DolphinUrlNavigator::activated
, this, &DolphinViewContainer::activate
);
329 disconnect(m_urlNavigatorConnected
, &DolphinUrlNavigator::requestToLoseFocus
, m_view
, nullptr);
331 m_urlNavigatorVisualState
= m_urlNavigatorConnected
->visualState();
332 m_urlNavigatorConnected
= nullptr;
335 void DolphinViewContainer::setSearchBarVisible(bool visible
)
338 if (isSearchBarVisible()) {
339 m_searchBar
->setVisible(false, WithAnimation
);
345 m_searchBar
= new Search::Bar(std::make_shared
<const Search::DolphinQuery
>(m_urlNavigator
->locationUrl(), QUrl
{} /** will be set below. */), this);
346 connect(m_searchBar
, &Search::Bar::urlChangeRequested
, this, [this](const QUrl
&url
) {
347 m_view
->setViewPropertiesContext(isSearchUrl(url
) ? QStringLiteral("search") : QString());
348 setGrabFocusOnUrlChange(false); // Prevent loss of focus while typing or refining a search.
350 setGrabFocusOnUrlChange(true);
352 connect(m_searchBar
, &Search::Bar::focusViewRequest
, this, &DolphinViewContainer::requestFocus
);
353 connect(m_searchBar
, &Search::Bar::showMessage
, this, [this](const QString
&message
, KMessageWidget::MessageType messageType
) {
354 showMessage(message
, messageType
);
357 &Search::Bar::showInstallationProgress
,
359 [this](const QString
¤tlyRunningTaskTitle
, int installationProgressPercent
) {
360 m_statusBar
->showProgress(currentlyRunningTaskTitle
, installationProgressPercent
, DolphinStatusBar::CancelLoading::Disallowed
);
362 connect(m_searchBar
, &Search::Bar::visibilityChanged
, this, &DolphinViewContainer::searchBarVisibilityChanged
);
363 m_topLayout
->addWidget(m_searchBar
, positionFor
.searchBar
, 0);
366 m_searchBar
->setVisible(true, WithAnimation
);
368 // The Search::Bar has been set visible but its state does not yet match with this view container or view.
369 // The view might for example already be searching because it was opened with a search URL. The Search::Bar needs to be updated to show the parameters of
370 // that search. And even if there is no search URL loaded in the view currently, we still need to figure out where the Search::Bar should be searching if
371 // the user starts a search from there. Let's figure out the search location in this method and let the DolphinQuery constructor figure out the rest from
372 // the current m_urlNavigator->locationUrl().
373 for (int i
= m_urlNavigator
->historyIndex(); i
< m_urlNavigator
->historySize(); i
++) {
374 QUrl url
= m_urlNavigator
->locationUrl(i
);
375 if (isSearchUrl(url
)) {
376 // The previous location was a search URL. Try to see if that search URL has a valid search path so we keep searching in the same location.
377 const auto searchPath
= Search::DolphinQuery(url
, QUrl
{}).searchPath(); // DolphinQuery is great at extracting the search path from a search URL.
378 if (searchPath
.isValid()) {
379 m_searchBar
->updateStateToMatch(std::make_shared
<const Search::DolphinQuery
>(m_urlNavigator
->locationUrl(), searchPath
));
382 } else if (url
.scheme() == QLatin1String("tags")) {
383 continue; // We avoid setting a tags url as the backup search path because a DolphinQuery constructed from a tags url will already search tagged
386 m_searchBar
->updateStateToMatch(std::make_shared
<const Search::DolphinQuery
>(m_urlNavigator
->locationUrl(), url
));
390 // We could not find any URL fit for searching in the history. This might happen because this view only ever loaded a search which searches "Everywhere"
391 // and therefore there is no specific search path to choose from. But the Search::Bar *needs* to know a search path because the user might switch from
392 // searching "Everywhere" to "Here" and it is everybody's guess what "Here" is supposed to mean in that context… We'll simply fall back to the user's home
393 // path for lack of a better option.
394 m_searchBar
->updateStateToMatch(std::make_shared
<const Search::DolphinQuery
>(m_urlNavigator
->locationUrl(), QUrl::fromUserInput(QDir::homePath())));
397 bool DolphinViewContainer::isSearchBarVisible() const
399 return m_searchBar
&& m_searchBar
->isVisible() && m_searchBar
->isEnabled();
402 void DolphinViewContainer::setFocusToSearchBar()
404 Q_ASSERT(isSearchBarVisible());
405 m_searchBar
->selectAll();
408 void DolphinViewContainer::setSelectionModeEnabled(bool enabled
, KActionCollection
*actionCollection
, SelectionMode::BottomBar::Contents bottomBarContents
)
410 const bool wasEnabled
= m_view
->selectionMode();
411 m_view
->setSelectionModeEnabled(enabled
);
415 return; // nothing to do here
417 Q_CHECK_PTR(m_selectionModeTopBar
); // there is no point in disabling selectionMode when it wasn't even enabled once.
418 Q_CHECK_PTR(m_selectionModeBottomBar
);
419 m_selectionModeTopBar
->setVisible(false, WithAnimation
);
420 m_selectionModeBottomBar
->setVisible(false, WithAnimation
);
421 Q_EMIT
selectionModeChanged(false);
423 if (!QApplication::focusWidget() || m_selectionModeTopBar
->isAncestorOf(QApplication::focusWidget())
424 || m_selectionModeBottomBar
->isAncestorOf(QApplication::focusWidget())) {
430 if (!m_selectionModeTopBar
) {
431 // Changing the location will disable selection mode.
432 connect(m_urlNavigator
.get(), &DolphinUrlNavigator::urlChanged
, this, [this]() {
433 setSelectionModeEnabled(false);
436 m_selectionModeTopBar
= new SelectionMode::TopBar(this); // will be created hidden
437 connect(m_selectionModeTopBar
, &SelectionMode::TopBar::selectionModeLeavingRequested
, this, [this]() {
438 setSelectionModeEnabled(false);
440 m_topLayout
->addWidget(m_selectionModeTopBar
, positionFor
.selectionModeTopBar
, 0);
443 if (!m_selectionModeBottomBar
) {
444 m_selectionModeBottomBar
= new SelectionMode::BottomBar(actionCollection
, this);
445 connect(m_view
, &DolphinView::selectionChanged
, this, [this](const KFileItemList
&selection
) {
446 m_selectionModeBottomBar
->slotSelectionChanged(selection
, m_view
->url());
448 connect(m_selectionModeBottomBar
, &SelectionMode::BottomBar::error
, this, &DolphinViewContainer::showErrorMessage
);
449 connect(m_selectionModeBottomBar
, &SelectionMode::BottomBar::selectionModeLeavingRequested
, this, [this]() {
450 setSelectionModeEnabled(false);
452 m_topLayout
->addWidget(m_selectionModeBottomBar
, positionFor
.selectionModeBottomBar
, 0);
454 m_selectionModeBottomBar
->resetContents(bottomBarContents
);
455 if (bottomBarContents
== SelectionMode::BottomBar::GeneralContents
) {
456 m_selectionModeBottomBar
->slotSelectionChanged(m_view
->selectedItems(), m_view
->url());
460 m_selectionModeTopBar
->setVisible(true, WithAnimation
);
461 m_selectionModeBottomBar
->setVisible(true, WithAnimation
);
462 Q_EMIT
selectionModeChanged(true);
466 bool DolphinViewContainer::isSelectionModeEnabled() const
468 const bool isEnabled
= m_view
->selectionMode();
470 // We can't assert that the bars are invisible only because the selection mode is disabled because the hide animation might still be playing.
471 && (!m_selectionModeBottomBar
|| !m_selectionModeBottomBar
->isEnabled() || !m_selectionModeBottomBar
->isVisible()
472 || m_selectionModeBottomBar
->contents() == SelectionMode::BottomBar::PasteContents
))
473 || (isEnabled
&& m_selectionModeTopBar
474 && m_selectionModeTopBar
->isVisible()
475 // 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.
476 && m_selectionModeBottomBar
477 && (m_selectionModeBottomBar
->isVisible() || m_selectionModeBottomBar
->contents() == SelectionMode::BottomBar::GeneralContents
)));
481 void DolphinViewContainer::slotSplitTabDisabled()
483 if (m_selectionModeBottomBar
) {
484 m_selectionModeBottomBar
->slotSplitTabDisabled();
488 void DolphinViewContainer::showMessage(const QString
&message
, KMessageWidget::MessageType messageType
, std::initializer_list
<QAction
*> buttonActions
)
490 if (message
.isEmpty()) {
494 m_messageWidget
->setText(message
);
496 // TODO: wrap at arbitrary character positions once QLabel can do this
497 // https://bugreports.qt.io/browse/QTBUG-1276
498 m_messageWidget
->setWordWrap(true);
499 m_messageWidget
->setMessageType(messageType
);
501 const QList
<QAction
*> previousMessageWidgetActions
= m_messageWidget
->actions();
502 for (auto action
: previousMessageWidgetActions
) {
503 m_messageWidget
->removeAction(action
);
505 for (QAction
*action
: buttonActions
) {
506 m_messageWidget
->addAction(action
);
509 m_messageWidget
->setWordWrap(false);
510 const int unwrappedWidth
= m_messageWidget
->sizeHint().width();
511 m_messageWidget
->setWordWrap(unwrappedWidth
> size().width());
513 if (m_messageWidget
->isVisible()) {
514 m_messageWidget
->hide();
516 m_messageWidget
->animatedShow();
518 #ifndef QT_NO_ACCESSIBILITY
519 if (QAccessible::isActive() && isActive()) {
520 // 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
521 // to the KMessageWidget. Instead we setFocus() on the KMessageWidget and trust that it has set correct focus handling.
522 m_messageWidget
->setFocus();
527 void DolphinViewContainer::showProgress(const QString
¤tlyRunningTaskTitle
, int progressPercent
)
529 m_statusBar
->showProgress(currentlyRunningTaskTitle
, progressPercent
, DolphinStatusBar::CancelLoading::Disallowed
);
532 void DolphinViewContainer::readSettings()
534 // The startup settings should (only) get applied if they have been
535 // modified by the user. Otherwise keep the (possibly) different current
536 // setting of the filterbar.
537 if (GeneralSettings::modifiedStartupSettings()) {
538 setFilterBarVisible(GeneralSettings::filterBar());
541 m_view
->readSettings();
542 m_statusBar
->readSettings();
545 bool DolphinViewContainer::isFilterBarVisible() const
547 return m_filterBar
->isEnabled(); // Gets disabled in AnimatedHeightWidget while animating towards a hidden state.
550 QString
DolphinViewContainer::placesText() const
554 if (isSearchBarVisible() && m_searchBar
->isSearchConfigured()) {
555 text
= m_searchBar
->queryTitle();
557 text
= url().adjusted(QUrl::StripTrailingSlash
).fileName();
558 if (text
.isEmpty()) {
561 if (text
.isEmpty()) {
562 text
= url().scheme();
569 void DolphinViewContainer::reload()
572 m_messageWidget
->hide();
575 QString
DolphinViewContainer::captionWindowTitle() const
577 if (GeneralSettings::showFullPathInTitlebar() && (!isSearchBarVisible() || !m_searchBar
->isSearchConfigured())) {
578 if (!url().isLocalFile()) {
579 return url().adjusted(QUrl::StripTrailingSlash
).toString();
581 return url().adjusted(QUrl::StripTrailingSlash
).path();
583 return DolphinViewContainer::caption();
587 QString
DolphinViewContainer::caption() const
589 // see KUrlNavigatorPrivate::firstButtonText().
590 if (url().path().isEmpty() || url().path() == QLatin1Char('/')) {
591 QUrlQuery
query(url());
592 const QString title
= query
.queryItemValue(QStringLiteral("title"), QUrl::FullyDecoded
);
593 if (!title
.isEmpty()) {
598 if (isSearchBarVisible() && m_searchBar
->isSearchConfigured()) {
599 return m_searchBar
->queryTitle();
602 KFilePlacesModel
*placesModel
= DolphinPlacesModelSingleton::instance().placesModel();
604 QModelIndex url_index
= placesModel
->closestItem(url());
606 if (url_index
.isValid() && placesModel
->url(url_index
).matches(url(), QUrl::StripTrailingSlash
)) {
607 return placesModel
->text(url_index
);
610 if (!url().isLocalFile()) {
611 QUrl adjustedUrl
= url().adjusted(QUrl::StripTrailingSlash
);
613 if (!adjustedUrl
.fileName().isEmpty()) {
614 caption
= adjustedUrl
.fileName();
615 } else if (!adjustedUrl
.path().isEmpty() && adjustedUrl
.path() != "/") {
616 caption
= adjustedUrl
.path();
617 } else if (!adjustedUrl
.host().isEmpty()) {
618 caption
= adjustedUrl
.host();
620 caption
= adjustedUrl
.toString();
625 QString fileName
= url().adjusted(QUrl::StripTrailingSlash
).fileName();
626 if (fileName
.isEmpty()) {
633 void DolphinViewContainer::setUrl(const QUrl
&newUrl
)
635 if (newUrl
!= m_urlNavigator
->locationUrl()) {
636 m_urlNavigator
->setLocationUrl(newUrl
);
637 if (m_searchBar
&& !Search::isSupportedSearchScheme(newUrl
.scheme())) {
638 m_searchBar
->setSearchPath(newUrl
);
643 void DolphinViewContainer::setFilterBarVisible(bool visible
)
645 Q_ASSERT(m_filterBar
);
647 m_view
->hideToolTip(ToolTipManager::HideBehavior::Instantly
);
648 m_filterBar
->setVisible(true, WithAnimation
);
649 m_filterBar
->setFocus();
650 m_filterBar
->selectAll();
651 Q_EMIT
showFilterBarChanged(true);
657 void DolphinViewContainer::delayedStatusBarUpdate()
659 if (m_statusBarTimer
->isActive() && (m_statusBarTimestamp
.elapsed() > 2000)) {
660 // No update of the statusbar has been done during the last 2 seconds,
661 // although an update has been requested. Trigger an immediate update.
662 m_statusBarTimer
->stop();
665 // Invoke updateStatusBar() with a small delay. This assures that
666 // when a lot of delayedStatusBarUpdates() are done in a short time,
667 // no bottleneck is given.
668 m_statusBarTimer
->start();
672 void DolphinViewContainer::updateStatusBar()
674 m_statusBarTimestamp
.start();
675 m_view
->requestStatusBarText();
676 updateStatusBarGeometry();
679 void DolphinViewContainer::slotDirectoryLoadingStarted()
681 if (isSearchUrl(url())) {
682 // Search KIO-slaves usually don't provide any progress information. Give
683 // a hint to the user that a searching is done:
685 m_statusBar
->showProgress(i18nc("@info", "Searching…"), -1);
687 // Trigger an undetermined progress indication. The progress
688 // information in percent will be triggered by the percent() signal
689 // of the directory lister later.
690 m_statusBar
->showProgress(QString(), -1);
693 if (m_urlNavigatorConnected
) {
694 m_urlNavigatorConnected
->setReadOnlyBadgeVisible(false);
698 void DolphinViewContainer::slotDirectoryLoadingCompleted()
700 m_statusBar
->showProgress(QString(), 100);
702 if (isSearchUrl(url()) && m_view
->itemsCount() == 0) {
703 // The dir lister has been completed on a Baloo-URI and no items have been found. Instead
704 // of showing the default status bar information ("0 items") a more helpful information is given:
705 m_statusBar
->setText(i18nc("@info:status", "No items found."));
710 if (m_urlNavigatorConnected
) {
711 m_urlNavigatorConnected
->setReadOnlyBadgeVisible(rootItem().isLocalFile() && !rootItem().isWritable());
714 // Update admin bar visibility
715 if (m_view
->url().scheme() == QStringLiteral("admin")) {
717 m_adminBar
= new Admin::Bar(this);
718 m_topLayout
->addWidget(m_adminBar
, positionFor
.adminBar
, 0);
720 m_adminBar
->setVisible(true, WithAnimation
);
721 } else if (m_adminBar
) {
722 m_adminBar
->setVisible(false, WithAnimation
);
726 void DolphinViewContainer::slotDirectoryLoadingCanceled()
728 m_statusBar
->showProgress(QString(), 100);
729 m_statusBar
->setText(QString());
732 void DolphinViewContainer::slotUrlIsFileError(const QUrl
&url
)
734 const KFileItem
item(url
);
736 // Find out if the file can be opened in the view (for example, this is the
737 // case if the file is an archive). The mime type must be known for that.
738 item
.determineMimeType();
739 const QUrl
&folderUrl
= DolphinView::openItemAsFolderUrl(item
, true);
740 if (!folderUrl
.isEmpty()) {
743 slotItemActivated(item
);
747 void DolphinViewContainer::slotItemActivated(const KFileItem
&item
)
749 // It is possible to activate items on inactive views by
750 // drag & drop operations. Assure that activating an item always
751 // results in an active view.
752 m_view
->setActive(true);
754 const QUrl
&url
= DolphinView::openItemAsFolderUrl(item
, GeneralSettings::browseThroughArchives());
755 if (!url
.isEmpty()) {
756 const auto modifiers
= QGuiApplication::keyboardModifiers();
757 // keep in sync with KUrlNavigator::slotNavigatorButtonClicked
758 if (modifiers
& Qt::ControlModifier
&& modifiers
& Qt::ShiftModifier
) {
759 Q_EMIT
activeTabRequested(url
);
760 } else if (modifiers
& Qt::ControlModifier
) {
761 Q_EMIT
tabRequested(url
);
762 } else if (modifiers
& Qt::ShiftModifier
) {
763 Dolphin::openNewWindow({KFilePlacesModel::convertedUrl(url
)}, this);
770 KIO::OpenUrlJob
*job
= new KIO::OpenUrlJob(item
.targetUrl(), item
.mimetype());
771 // Auto*Warning*Handling, errors are put in a KMessageWidget by us in slotOpenUrlFinished.
772 job
->setUiDelegate(KIO::createDefaultJobUiDelegate(KJobUiDelegate::AutoWarningHandlingEnabled
, this));
773 job
->setShowOpenOrExecuteDialog(true);
774 connect(job
, &KIO::OpenUrlJob::finished
, this, &DolphinViewContainer::slotOpenUrlFinished
);
778 void DolphinViewContainer::slotfileMiddleClickActivated(const KFileItem
&item
)
780 KService::List services
= KApplicationTrader::queryByMimeType(item
.mimetype());
782 int indexOfAppToOpenFileWith
= 1;
784 // executable scripts
785 auto mimeType
= item
.currentMimeType();
786 if (item
.isLocalFile() && mimeType
.inherits(QStringLiteral("application/x-executable")) && mimeType
.inherits(QStringLiteral("text/plain"))
787 && QFileInfo(item
.localPath()).isExecutable()) {
788 KConfigGroup
cfgGroup(KSharedConfig::openConfig(QStringLiteral("kiorc")), QStringLiteral("Executable scripts"));
789 const QString value
= cfgGroup
.readEntry("behaviourOnLaunch", "alwaysAsk");
791 // in case KIO::WidgetsOpenOrExecuteFileHandler::promptUserOpenOrExecute would not open the file
792 if (value
!= QLatin1String("open")) {
793 indexOfAppToOpenFileWith
= 0;
797 if (services
.length() >= indexOfAppToOpenFileWith
+ 1) {
798 auto service
= services
.at(indexOfAppToOpenFileWith
);
800 KIO::ApplicationLauncherJob
*job
= new KIO::ApplicationLauncherJob(service
, this);
801 job
->setUrls({item
.url()});
803 job
->setUiDelegate(KIO::createDefaultJobUiDelegate(KJobUiDelegate::AutoHandlingEnabled
, this));
804 connect(job
, &KIO::OpenUrlJob::finished
, this, &DolphinViewContainer::slotOpenUrlFinished
);
807 // If no 2nd service available, try to open archives in new tabs, regardless of the "Open archives as folder" setting.
808 const QUrl
&url
= DolphinView::openItemAsFolderUrl(item
);
809 const auto modifiers
= QGuiApplication::keyboardModifiers();
810 if (!url
.isEmpty()) {
811 // keep in sync with KUrlNavigator::slotNavigatorButtonClicked
812 if (modifiers
& Qt::ShiftModifier
) {
813 Q_EMIT
activeTabRequested(url
);
815 Q_EMIT
tabRequested(url
);
821 void DolphinViewContainer::slotItemsActivated(const KFileItemList
&items
)
823 Q_ASSERT(items
.count() >= 2);
825 KFileItemActions
fileItemActions(this);
826 fileItemActions
.runPreferredApplications(items
);
829 void DolphinViewContainer::showItemInfo(const KFileItem
&item
)
832 m_statusBar
->resetToDefaultText();
834 m_statusBar
->setText(item
.getStatusBarInfo());
838 void DolphinViewContainer::closeFilterBar()
840 m_filterBar
->closeFilterBar();
842 Q_EMIT
showFilterBarChanged(false);
845 void DolphinViewContainer::clearFilterBar()
847 m_filterBar
->clearIfUnlocked();
850 void DolphinViewContainer::setNameFilter(const QString
&nameFilter
)
852 m_view
->hideToolTip(ToolTipManager::HideBehavior::Instantly
);
853 m_view
->setNameFilter(nameFilter
);
854 delayedStatusBarUpdate();
857 void DolphinViewContainer::activate()
862 void DolphinViewContainer::slotUrlNavigatorLocationAboutToBeChanged(const QUrl
&)
867 void DolphinViewContainer::slotUrlNavigatorLocationChanged(const QUrl
&url
)
869 if (m_urlNavigatorConnected
) {
870 m_urlNavigatorConnected
->slotReturnPressed();
873 if (KProtocolManager::supportsListing(url
)) {
874 if (isSearchUrl(url
)) {
875 setSearchBarVisible(true);
876 } else if (m_searchBar
&& m_searchBar
->isSearchConfigured()) {
877 // Hide the search bar because it shows an outdated search which the user does not care about anymore.
878 setSearchBarVisible(false);
882 tryRestoreViewState();
884 if (m_grabFocusOnUrlChange
&& isActive()) {
885 // When an URL has been entered, the view should get the focus.
886 // The focus must be requested asynchronously, as changing the URL might create
887 // a new view widget.
888 QTimer::singleShot(0, this, &DolphinViewContainer::requestFocus
);
890 } else if (KProtocolManager::isSourceProtocol(url
)) {
891 if (url
.scheme().startsWith(QLatin1String("http"))) {
892 showMessage(i18nc("@info:status", // krazy:exclude=qmethods
893 "Dolphin does not support web pages, the web browser has been launched"),
894 KMessageWidget::Information
);
896 showMessage(i18nc("@info:status", "Protocol not supported by Dolphin, default application has been launched"), KMessageWidget::Information
);
899 QDesktopServices::openUrl(url
);
900 redirect(QUrl(), m_urlNavigator
->locationUrl(1));
902 if (!url
.scheme().isEmpty()) {
903 showMessage(i18nc("@info:status", "Invalid protocol '%1'", url
.scheme()), KMessageWidget::Error
);
905 showMessage(i18nc("@info:status", "Invalid protocol"), KMessageWidget::Error
);
907 m_urlNavigator
->goBack();
911 void DolphinViewContainer::slotUrlSelectionRequested(const QUrl
&url
)
913 // 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
915 m_view
->markUrlAsCurrent(url
); // makes the item scroll into view
918 void DolphinViewContainer::disableUrlNavigatorSelectionRequests()
920 disconnect(m_urlNavigator
.get(), &KUrlNavigator::urlSelectionRequested
, this, &DolphinViewContainer::slotUrlSelectionRequested
);
923 void DolphinViewContainer::enableUrlNavigatorSelectionRequests()
925 connect(m_urlNavigator
.get(), &KUrlNavigator::urlSelectionRequested
, this, &DolphinViewContainer::slotUrlSelectionRequested
);
928 void DolphinViewContainer::redirect(const QUrl
&oldUrl
, const QUrl
&newUrl
)
931 const bool block
= m_urlNavigator
->signalsBlocked();
932 m_urlNavigator
->blockSignals(true);
934 // Assure that the location state is reset for redirection URLs. This
935 // allows to skip redirection URLs when going back or forward in the
937 m_urlNavigator
->saveLocationState(QByteArray());
938 m_urlNavigator
->setLocationUrl(newUrl
);
939 setSearchBarVisible(isSearchUrl(newUrl
));
941 m_urlNavigator
->blockSignals(block
);
944 void DolphinViewContainer::requestFocus()
949 void DolphinViewContainer::stopDirectoryLoading()
951 m_view
->stopLoading();
952 m_statusBar
->showProgress(QString(), 100);
955 void DolphinViewContainer::slotStatusBarZoomLevelChanged(int zoomLevel
)
957 m_view
->setZoomLevel(zoomLevel
);
960 void DolphinViewContainer::slotErrorMessageFromView(const QString
&message
, const int kioErrorCode
)
962 if (kioErrorCode
== KIO::ERR_CANNOT_ENTER_DIRECTORY
&& m_view
->url().scheme() == QStringLiteral("file")
963 && KProtocolInfo::isKnownProtocol(QStringLiteral("admin")) && !rootItem().isReadable()) {
964 // Explain to users that they need authentication to see the folder contents.
965 if (!m_authorizeToEnterFolderAction
) { // This code is similar to parts of Admin::Bar::hideTheNextTimeAuthorizationExpires().
966 // We should not simply use the actAsAdminAction() itself here because that one always refers to the active view instead of this->m_view.
967 auto actAsAdminAction
= Admin::WorkerIntegration::FriendAccess::actAsAdminAction();
968 m_authorizeToEnterFolderAction
= new QAction
{actAsAdminAction
->icon(), actAsAdminAction
->text(), this};
969 m_authorizeToEnterFolderAction
->setToolTip(actAsAdminAction
->toolTip());
970 m_authorizeToEnterFolderAction
->setWhatsThis(actAsAdminAction
->whatsThis());
971 connect(m_authorizeToEnterFolderAction
, &QAction::triggered
, this, [this, actAsAdminAction
]() {
973 actAsAdminAction
->trigger();
976 showMessage(i18nc("@info", "Authorization required to enter this folder."), KMessageWidget::Error
, {m_authorizeToEnterFolderAction
});
978 } else if (kioErrorCode
== KIO::ERR_DOES_NOT_EXIST
&& m_view
->url().isLocalFile()) {
979 if (!m_createFolderAction
) {
980 m_createFolderAction
= new QAction(this);
981 m_createFolderAction
->setText(i18nc("@action", "Create missing folder"));
982 m_createFolderAction
->setIcon(QIcon::fromTheme(QStringLiteral("folder-new")));
983 m_createFolderAction
->setToolTip(i18nc("@info:tooltip", "Create the folder at this path and open it"));
984 connect(m_createFolderAction
, &QAction::triggered
, this, [this](bool) {
985 KIO::MkpathJob
*job
= KIO::mkpath(m_view
->url());
986 KIO::FileUndoManager::self()->recordJob(KIO::FileUndoManager::Mkpath
, {}, m_view
->url(), job
);
987 connect(job
, &KJob::result
, this, [this](KJob
*job
) {
989 showErrorMessage(job
->errorString());
992 m_messageWidget
->animatedHide();
997 showMessage(message
, KMessageWidget::Error
, {m_createFolderAction
});
1000 Q_EMIT
showErrorMessage(message
);
1003 void DolphinViewContainer::showErrorMessage(const QString
&message
)
1005 showMessage(message
, KMessageWidget::Error
);
1008 void DolphinViewContainer::slotPlacesModelChanged()
1010 if (!GeneralSettings::showFullPathInTitlebar()) {
1011 Q_EMIT
captionChanged();
1015 void DolphinViewContainer::slotHiddenFilesShownChanged(bool showHiddenFiles
)
1017 if (m_urlNavigatorConnected
) {
1018 m_urlNavigatorConnected
->setShowHiddenFolders(showHiddenFiles
);
1022 void DolphinViewContainer::slotSortHiddenLastChanged(bool hiddenLast
)
1024 if (m_urlNavigatorConnected
) {
1025 m_urlNavigatorConnected
->setSortHiddenFoldersLast(hiddenLast
);
1029 void DolphinViewContainer::slotCurrentDirectoryRemoved()
1031 const QString
location(url().toDisplayString(QUrl::PreferLocalFile
));
1032 if (url().isLocalFile()) {
1033 const QString dirPath
= url().toLocalFile();
1034 const QString newPath
= getNearestExistingAncestorOfPath(dirPath
);
1035 const QUrl newUrl
= QUrl::fromLocalFile(newPath
);
1036 // #473377: Delay changing the url to avoid modifying KCoreDirLister before KCoreDirListerCache::deleteDir() returns.
1037 QTimer::singleShot(0, this, [&, newUrl
, location
] {
1039 showMessage(xi18n("Current location changed, <filename>%1</filename> is no longer accessible.", location
), KMessageWidget::Warning
);
1042 showMessage(xi18n("Current location changed, <filename>%1</filename> is no longer accessible.", location
), KMessageWidget::Warning
);
1045 void DolphinViewContainer::slotOpenUrlFinished(KJob
*job
)
1047 if (job
->error() && job
->error() != KIO::ERR_USER_CANCELED
) {
1048 showErrorMessage(job
->errorString());
1052 void DolphinViewContainer::saveViewState()
1054 QByteArray locationState
;
1055 QDataStream
stream(&locationState
, QIODevice::WriteOnly
);
1056 m_view
->saveState(stream
);
1057 m_urlNavigator
->saveLocationState(locationState
);
1060 void DolphinViewContainer::tryRestoreViewState()
1062 QByteArray locationState
= m_urlNavigator
->locationState();
1063 if (!locationState
.isEmpty()) {
1064 QDataStream
stream(&locationState
, QIODevice::ReadOnly
);
1065 m_view
->restoreState(stream
);
1069 QString
DolphinViewContainer::getNearestExistingAncestorOfPath(const QString
&path
) const
1073 dir
.setPath(QDir::cleanPath(dir
.filePath(QStringLiteral(".."))));
1074 } while (!dir
.exists() && !dir
.isRoot());
1076 return dir
.exists() ? dir
.path() : QString
{};
1079 void DolphinViewContainer::updateStatusBarGeometry()
1084 if (GeneralSettings::showStatusBar() == GeneralSettings::EnumShowStatusBar::Small
) {
1085 QRect
statusBarRect(preferredSmallStatusBarGeometry());
1086 if (view()->layoutDirection() == Qt::RightToLeft
) {
1087 const int splitterWidth
= m_statusBar
->clippingAmount();
1088 statusBarRect
.setLeft(rect().width() - m_statusBar
->width() + splitterWidth
); // Add clipping amount.
1090 // Move statusbar to bottomLeft, or bottomRight with right-to-left-layout.
1091 m_statusBar
->setGeometry(statusBarRect
);
1092 // Add 1 due to how qrect coordinates work.
1093 m_view
->setStatusBarOffset(m_statusBar
->geometry().height() - m_statusBar
->clippingAmount() + 1);
1095 m_view
->setStatusBarOffset(0);
1099 bool DolphinViewContainer::eventFilter(QObject
*object
, QEvent
*event
)
1101 if (GeneralSettings::showStatusBar() == GeneralSettings::EnumShowStatusBar::Small
&& object
== m_view
) {
1102 switch (event
->type()) {
1103 case QEvent::Resize
: {
1104 m_statusBar
->updateWidthToContent();
1107 case QEvent::LayoutRequest
: {
1108 m_statusBar
->updateWidthToContent();
1118 QRect
DolphinViewContainer::preferredSmallStatusBarGeometry()
1120 // Add offset depending if horizontal scrollbar or filterbar is visible, we need to add 1 due to how QRect coordinates work.
1121 const int yPos
= m_view
->geometry().bottom() - m_view
->horizontalScrollBarHeight() - m_statusBar
->minimumHeight() + 1;
1122 QRect statusBarRect
= rect().adjusted(0, yPos
, 0, 0);
1123 return statusBarRect
;
1126 #include "moc_dolphinviewcontainer.cpp"