2 * SPDX-FileCopyrightText: 2007 Peter Penz <peter.penz19@gmail.com>
4 * SPDX-License-Identifier: GPL-2.0-or-later
7 #include "dolphinviewcontainer.h"
9 #include "dolphin_generalsettings.h"
10 #include "dolphindebug.h"
11 #include "dolphinplacesmodelsingleton.h"
12 #include "filterbar/filterbar.h"
14 #include "search/dolphinsearchbox.h"
15 #include "selectionmode/topbar.h"
16 #include "statusbar/dolphinstatusbar.h"
17 #include "views/viewmodecontroller.h"
18 #include "views/viewproperties.h"
19 #include "dolphin_detailsmodesettings.h"
21 #include <KActionCollection>
23 #include <KActivities/ResourceInstance>
25 #include <KFileItemActions>
26 #include <KFilePlacesModel>
27 #include <KIO/JobUiDelegate>
28 #include <KIO/OpenUrlJob>
29 #include <KLocalizedString>
30 #include <KMessageWidget>
31 #include <KProtocolManager>
34 #include <QDesktopServices>
36 #include <QGridLayout>
37 #include <QGuiApplication>
40 #include <QRegularExpression>
42 // An overview of the widgets contained by this ViewContainer
43 struct LayoutStructure
{
45 int messageWidget
= 1;
46 int selectionModeTopBar
= 2;
48 int selectionModeBottomBar
= 4;
52 constexpr LayoutStructure positionFor
;
54 DolphinViewContainer::DolphinViewContainer(const QUrl
& url
, QWidget
* parent
) :
57 m_urlNavigator
{new DolphinUrlNavigator(url
)},
58 m_urlNavigatorConnected
{nullptr},
60 m_searchModeEnabled(false),
61 m_messageWidget(nullptr),
62 m_selectionModeTopBar
{nullptr},
65 m_selectionModeBottomBar
{nullptr},
67 m_statusBarTimer(nullptr),
68 m_statusBarTimestamp(),
71 , m_activityResourceInstance(nullptr)
76 m_topLayout
= new QGridLayout(this);
77 m_topLayout
->setSpacing(0);
78 m_topLayout
->setContentsMargins(0, 0, 0, 0);
80 m_searchBox
= new DolphinSearchBox(this);
82 connect(m_searchBox
, &DolphinSearchBox::activated
, this, &DolphinViewContainer::activate
);
83 connect(m_searchBox
, &DolphinSearchBox::closeRequest
, this, &DolphinViewContainer::closeSearchBox
);
84 connect(m_searchBox
, &DolphinSearchBox::searchRequest
, this, &DolphinViewContainer::startSearching
);
85 connect(m_searchBox
, &DolphinSearchBox::focusViewRequest
, this, &DolphinViewContainer::requestFocus
);
86 m_searchBox
->setWhatsThis(xi18nc("@info:whatsthis findbar",
87 "<para>This helps you find files and folders. Enter a <emphasis>"
88 "search term</emphasis> and specify search settings with the "
89 "buttons at the bottom:<list><item>Filename/Content: "
90 "Does the item you are looking for contain the search terms "
91 "within its filename or its contents?<nl/>The contents of images, "
92 "audio files and videos will not be searched.</item><item>"
93 "From Here/Everywhere: Do you want to search in this "
94 "folder and its sub-folders or everywhere?</item><item>"
95 "More Options: Click this to search by media type, access "
96 "time or rating.</item><item>More Search Tools: Install other "
97 "means to find an item.</item></list></para>"));
99 m_messageWidget
= new KMessageWidget(this);
100 m_messageWidget
->setCloseButtonVisible(true);
101 m_messageWidget
->hide();
106 // We must be logged in as the root user; show a big scary warning
107 showMessage(i18n("Running Dolphin as root can be dangerous. Please be careful."), Warning
);
111 // Initialize filter bar
112 m_filterBar
= new FilterBar(this);
113 m_filterBar
->setVisible(GeneralSettings::filterBar());
115 connect(m_filterBar
, &FilterBar::filterChanged
,
116 this, &DolphinViewContainer::setNameFilter
);
117 connect(m_filterBar
, &FilterBar::closeRequest
,
118 this, &DolphinViewContainer::closeFilterBar
);
119 connect(m_filterBar
, &FilterBar::focusViewRequest
,
120 this, &DolphinViewContainer::requestFocus
);
122 // Initialize the main view
123 m_view
= new DolphinView(url
, this);
124 connect(m_view
, &DolphinView::urlChanged
,
125 m_filterBar
, &FilterBar::slotUrlChanged
);
126 connect(m_view
, &DolphinView::urlChanged
,
127 m_messageWidget
, &KMessageWidget::hide
);
128 // m_urlNavigator stays in sync with m_view's location changes and
129 // keeps track of them so going back and forth in the history works.
130 connect(m_view
, &DolphinView::urlChanged
,
131 m_urlNavigator
.get(), &DolphinUrlNavigator::setLocationUrl
);
132 connect(m_urlNavigator
.get(), &DolphinUrlNavigator::urlChanged
,
133 this, &DolphinViewContainer::slotUrlNavigatorLocationChanged
);
134 connect(m_urlNavigator
.get(), &DolphinUrlNavigator::urlAboutToBeChanged
,
135 this, &DolphinViewContainer::slotUrlNavigatorLocationAboutToBeChanged
);
136 connect(m_urlNavigator
.get(), &DolphinUrlNavigator::urlSelectionRequested
,
137 this, &DolphinViewContainer::slotUrlSelectionRequested
);
138 connect(m_view
, &DolphinView::writeStateChanged
,
139 this, &DolphinViewContainer::writeStateChanged
);
140 connect(m_view
, &DolphinView::requestItemInfo
,
141 this, &DolphinViewContainer::showItemInfo
);
142 connect(m_view
, &DolphinView::itemActivated
,
143 this, &DolphinViewContainer::slotItemActivated
);
144 connect(m_view
, &DolphinView::itemsActivated
,
145 this, &DolphinViewContainer::slotItemsActivated
);
146 connect(m_view
, &DolphinView::redirection
,
147 this, &DolphinViewContainer::redirect
);
148 connect(m_view
, &DolphinView::directoryLoadingStarted
,
149 this, &DolphinViewContainer::slotDirectoryLoadingStarted
);
150 connect(m_view
, &DolphinView::directoryLoadingCompleted
,
151 this, &DolphinViewContainer::slotDirectoryLoadingCompleted
);
152 connect(m_view
, &DolphinView::directoryLoadingCanceled
,
153 this, &DolphinViewContainer::slotDirectoryLoadingCanceled
);
154 connect(m_view
, &DolphinView::itemCountChanged
,
155 this, &DolphinViewContainer::delayedStatusBarUpdate
);
156 connect(m_view
, &DolphinView::directoryLoadingProgress
,
157 this, &DolphinViewContainer::updateDirectoryLoadingProgress
);
158 connect(m_view
, &DolphinView::directorySortingProgress
,
159 this, &DolphinViewContainer::updateDirectorySortingProgress
);
160 connect(m_view
, &DolphinView::selectionChanged
,
161 this, &DolphinViewContainer::delayedStatusBarUpdate
);
162 connect(m_view
, &DolphinView::errorMessage
,
163 this, &DolphinViewContainer::showErrorMessage
);
164 connect(m_view
, &DolphinView::urlIsFileError
,
165 this, &DolphinViewContainer::slotUrlIsFileError
);
166 connect(m_view
, &DolphinView::activated
,
167 this, &DolphinViewContainer::activate
);
168 connect(m_view
, &DolphinView::hiddenFilesShownChanged
,
169 this, &DolphinViewContainer::slotHiddenFilesShownChanged
);
170 connect(m_view
, &DolphinView::sortHiddenLastChanged
,
171 this, &DolphinViewContainer::slotSortHiddenLastChanged
);
173 // Initialize status bar
174 m_statusBar
= new DolphinStatusBar(this);
175 m_statusBar
->setUrl(m_view
->url());
176 m_statusBar
->setZoomLevel(m_view
->zoomLevel());
177 connect(m_view
, &DolphinView::urlChanged
,
178 m_statusBar
, &DolphinStatusBar::setUrl
);
179 connect(m_view
, &DolphinView::zoomLevelChanged
,
180 m_statusBar
, &DolphinStatusBar::setZoomLevel
);
181 connect(m_view
, &DolphinView::infoMessage
,
182 m_statusBar
, &DolphinStatusBar::setText
);
183 connect(m_view
, &DolphinView::operationCompletedMessage
,
184 m_statusBar
, &DolphinStatusBar::setText
);
185 connect(m_view
, &DolphinView::statusBarTextChanged
,
186 m_statusBar
, &DolphinStatusBar::setDefaultText
);
187 connect(m_view
, &DolphinView::statusBarTextChanged
,
188 m_statusBar
, &DolphinStatusBar::resetToDefaultText
);
189 connect(m_statusBar
, &DolphinStatusBar::stopPressed
,
190 this, &DolphinViewContainer::stopDirectoryLoading
);
191 connect(m_statusBar
, &DolphinStatusBar::zoomLevelChanged
,
192 this, &DolphinViewContainer::slotStatusBarZoomLevelChanged
);
194 m_statusBarTimer
= new QTimer(this);
195 m_statusBarTimer
->setSingleShot(true);
196 m_statusBarTimer
->setInterval(300);
197 connect(m_statusBarTimer
, &QTimer::timeout
, this, &DolphinViewContainer::updateStatusBar
);
199 KIO::FileUndoManager
* undoManager
= KIO::FileUndoManager::self();
200 connect(undoManager
, &KIO::FileUndoManager::jobRecordingFinished
,
201 this, &DolphinViewContainer::delayedStatusBarUpdate
);
203 m_topLayout
->addWidget(m_searchBox
, positionFor
.searchBox
, 0);
204 m_topLayout
->addWidget(m_messageWidget
, positionFor
.messageWidget
, 0);
205 m_topLayout
->addWidget(m_view
, positionFor
.view
, 0);
206 m_topLayout
->addWidget(m_filterBar
, positionFor
.filterBar
, 0);
207 m_topLayout
->addWidget(m_statusBar
, positionFor
.statusBar
, 0);
209 setSearchModeEnabled(isSearchUrl(url
));
211 connect(DetailsModeSettings::self(), &KCoreConfigSkeleton::configChanged
, this, [=]() {
212 if (view()->viewMode() == DolphinView::Mode::DetailsView
) {
217 KFilePlacesModel
*placesModel
= DolphinPlacesModelSingleton::instance().placesModel();
218 connect(placesModel
, &KFilePlacesModel::dataChanged
,
219 this, &DolphinViewContainer::slotPlacesModelChanged
);
220 connect(placesModel
, &KFilePlacesModel::rowsInserted
,
221 this, &DolphinViewContainer::slotPlacesModelChanged
);
222 connect(placesModel
, &KFilePlacesModel::rowsRemoved
,
223 this, &DolphinViewContainer::slotPlacesModelChanged
);
225 connect(this, &DolphinViewContainer::searchModeEnabledChanged
,
226 this, &DolphinViewContainer::captionChanged
);
228 // Initialize kactivities resource instance
231 m_activityResourceInstance
= new KActivities::ResourceInstance(window()->winId(), url
);
232 m_activityResourceInstance
->setParent(this);
236 DolphinViewContainer::~DolphinViewContainer()
240 QUrl
DolphinViewContainer::url() const
242 return m_view
->url();
245 void DolphinViewContainer::setActive(bool active
)
247 m_searchBox
->setActive(active
);
248 if (m_urlNavigatorConnected
) {
249 m_urlNavigatorConnected
->setActive(active
);
251 m_view
->setActive(active
);
255 m_activityResourceInstance
->notifyFocusedIn();
257 m_activityResourceInstance
->notifyFocusedOut();
262 bool DolphinViewContainer::isActive() const
264 return m_view
->isActive();
267 void DolphinViewContainer::setAutoGrabFocus(bool grab
)
269 m_autoGrabFocus
= grab
;
272 bool DolphinViewContainer::autoGrabFocus() const
274 return m_autoGrabFocus
;
277 QString
DolphinViewContainer::currentSearchText() const
279 return m_searchBox
->text();
282 const DolphinStatusBar
* DolphinViewContainer::statusBar() const
287 DolphinStatusBar
* DolphinViewContainer::statusBar()
292 const DolphinUrlNavigator
* DolphinViewContainer::urlNavigator() const
294 return m_urlNavigatorConnected
;
297 DolphinUrlNavigator
* DolphinViewContainer::urlNavigator()
299 return m_urlNavigatorConnected
;
302 const DolphinUrlNavigator
*DolphinViewContainer::urlNavigatorInternalWithHistory() const
304 return m_urlNavigator
.get();
307 DolphinUrlNavigator
*DolphinViewContainer::urlNavigatorInternalWithHistory()
309 return m_urlNavigator
.get();
312 const DolphinView
* DolphinViewContainer::view() const
317 DolphinView
* DolphinViewContainer::view()
322 void DolphinViewContainer::connectUrlNavigator(DolphinUrlNavigator
*urlNavigator
)
324 Q_CHECK_PTR(urlNavigator
);
325 Q_ASSERT(!m_urlNavigatorConnected
);
326 Q_ASSERT(m_urlNavigator
.get() != urlNavigator
);
329 urlNavigator
->setLocationUrl(m_view
->url());
330 urlNavigator
->setShowHiddenFolders(m_view
->hiddenFilesShown());
331 urlNavigator
->setSortHiddenFoldersLast(m_view
->sortHiddenLast());
332 if (m_urlNavigatorVisualState
) {
333 urlNavigator
->setVisualState(*m_urlNavigatorVisualState
.get());
334 m_urlNavigatorVisualState
.reset();
336 urlNavigator
->setActive(isActive());
338 // Url changes are still done via m_urlNavigator.
339 connect(urlNavigator
, &DolphinUrlNavigator::urlChanged
,
340 m_urlNavigator
.get(), &DolphinUrlNavigator::setLocationUrl
);
341 connect(urlNavigator
, &DolphinUrlNavigator::urlsDropped
,
342 this, [=](const QUrl
&destination
, QDropEvent
*event
) {
343 m_view
->dropUrls(destination
, event
, urlNavigator
->dropWidget());
345 // Aside from these, only visual things need to be connected.
346 connect(m_view
, &DolphinView::urlChanged
,
347 urlNavigator
, &DolphinUrlNavigator::setLocationUrl
);
348 connect(urlNavigator
, &DolphinUrlNavigator::activated
,
349 this, &DolphinViewContainer::activate
);
351 m_urlNavigatorConnected
= urlNavigator
;
354 void DolphinViewContainer::disconnectUrlNavigator()
356 if (!m_urlNavigatorConnected
) {
360 disconnect(m_urlNavigatorConnected
, &DolphinUrlNavigator::urlChanged
,
361 m_urlNavigator
.get(), &DolphinUrlNavigator::setLocationUrl
);
362 disconnect(m_urlNavigatorConnected
, &DolphinUrlNavigator::urlsDropped
,
364 disconnect(m_view
, &DolphinView::urlChanged
,
365 m_urlNavigatorConnected
, &DolphinUrlNavigator::setLocationUrl
);
366 disconnect(m_urlNavigatorConnected
, &DolphinUrlNavigator::activated
,
367 this, &DolphinViewContainer::activate
);
369 m_urlNavigatorVisualState
= m_urlNavigatorConnected
->visualState();
370 m_urlNavigatorConnected
= nullptr;
373 void DolphinViewContainer::setSelectionModeEnabled(bool enabled
, KActionCollection
*actionCollection
, SelectionMode::BottomBar::Contents bottomBarContents
)
375 const bool wasEnabled
= m_view
->selectionMode();
376 m_view
->setSelectionModeEnabled(enabled
);
380 return; // nothing to do here
382 Q_CHECK_PTR(m_selectionModeTopBar
); // there is no point in disabling selectionMode when it wasn't even enabled once.
383 Q_CHECK_PTR(m_selectionModeBottomBar
);
384 m_selectionModeTopBar
->setVisible(false, WithAnimation
);
385 m_selectionModeBottomBar
->setVisible(false, WithAnimation
);
386 Q_EMIT
selectionModeChanged(false);
390 if (!m_selectionModeTopBar
) {
391 // Changing the location will disable selection mode.
392 connect(m_urlNavigator
.get(), &DolphinUrlNavigator::urlChanged
, this, [this]() {
393 setSelectionModeEnabled(false);
396 m_selectionModeTopBar
= new SelectionMode::TopBar(this); // will be created hidden
397 connect(m_selectionModeTopBar
, &SelectionMode::TopBar::selectionModeLeavingRequested
, this, [this]() {
398 setSelectionModeEnabled(false);
400 m_topLayout
->addWidget(m_selectionModeTopBar
, positionFor
.selectionModeTopBar
, 0);
403 if (!m_selectionModeBottomBar
) {
404 m_selectionModeBottomBar
= new SelectionMode::BottomBar(actionCollection
, this);
405 connect(m_view
, &DolphinView::selectionChanged
, this, [this](const KFileItemList
&selection
) {
406 m_selectionModeBottomBar
->slotSelectionChanged(selection
, m_view
->url());
408 connect(m_selectionModeBottomBar
, &SelectionMode::BottomBar::error
, this, [this](const QString
&errorMessage
) {
409 showErrorMessage(errorMessage
);
411 connect(m_selectionModeBottomBar
, &SelectionMode::BottomBar::selectionModeLeavingRequested
, this, [this]() {
412 setSelectionModeEnabled(false);
414 m_topLayout
->addWidget(m_selectionModeBottomBar
, positionFor
.selectionModeBottomBar
, 0);
416 m_selectionModeBottomBar
->resetContents(bottomBarContents
);
417 if (bottomBarContents
== SelectionMode::BottomBar::GeneralContents
) {
418 m_selectionModeBottomBar
->slotSelectionChanged(m_view
->selectedItems(), m_view
->url());
422 m_selectionModeTopBar
->setVisible(true, WithAnimation
);
423 m_selectionModeBottomBar
->setVisible(true, WithAnimation
);
424 Q_EMIT
selectionModeChanged(true);
428 bool DolphinViewContainer::isSelectionModeEnabled() const
430 const bool isEnabled
= m_view
->selectionMode();
432 // We can't assert that the bars are invisible only because the selection mode is disabled because the hide animation might still be playing.
433 && (!m_selectionModeBottomBar
|| !m_selectionModeBottomBar
->isEnabled() ||
434 !m_selectionModeBottomBar
->isVisible() || m_selectionModeBottomBar
->contents() == SelectionMode::BottomBar::PasteContents
))
436 && m_selectionModeTopBar
&& m_selectionModeTopBar
->isVisible()
437 // 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.
438 && m_selectionModeBottomBar
439 && (m_selectionModeBottomBar
->isVisible() || m_selectionModeBottomBar
->contents() == SelectionMode::BottomBar::GeneralContents
)));
443 void DolphinViewContainer::slotSplitTabDisabled()
445 if (m_selectionModeBottomBar
) {
446 m_selectionModeBottomBar
->slotSplitTabDisabled();
451 void DolphinViewContainer::showMessage(const QString
& msg
, MessageType type
)
457 m_messageWidget
->setText(msg
);
459 // TODO: wrap at arbitrary character positions once QLabel can do this
460 // https://bugreports.qt.io/browse/QTBUG-1276
461 m_messageWidget
->setWordWrap(true);
464 case Information
: m_messageWidget
->setMessageType(KMessageWidget::Information
); break;
465 case Warning
: m_messageWidget
->setMessageType(KMessageWidget::Warning
); break;
466 case Error
: m_messageWidget
->setMessageType(KMessageWidget::Error
); break;
472 m_messageWidget
->setWordWrap(false);
473 const int unwrappedWidth
= m_messageWidget
->sizeHint().width();
474 m_messageWidget
->setWordWrap(unwrappedWidth
> size().width());
476 if (m_messageWidget
->isVisible()) {
477 m_messageWidget
->hide();
479 m_messageWidget
->animatedShow();
482 void DolphinViewContainer::readSettings()
484 // The startup settings should (only) get applied if they have been
485 // modified by the user. Otherwise keep the (possibly) different current
486 // setting of the filterbar.
487 if (GeneralSettings::modifiedStartupSettings()) {
488 setFilterBarVisible(GeneralSettings::filterBar());
491 m_view
->readSettings();
492 m_statusBar
->readSettings();
495 bool DolphinViewContainer::isFilterBarVisible() const
497 return m_filterBar
->isVisible();
500 void DolphinViewContainer::setSearchModeEnabled(bool enabled
)
502 m_searchBox
->setVisible(enabled
);
505 const QUrl
& locationUrl
= m_urlNavigator
->locationUrl();
506 m_searchBox
->fromSearchUrl(locationUrl
);
509 if (enabled
== isSearchModeEnabled()) {
510 if (enabled
&& !m_searchBox
->hasFocus()) {
511 m_searchBox
->setFocus();
512 m_searchBox
->selectAll();
518 m_view
->setViewPropertiesContext(QString());
520 // Restore the URL for the URL navigator. If Dolphin has been
521 // started with a search-URL, the home URL is used as fallback.
522 QUrl url
= m_searchBox
->searchPath();
523 if (url
.isEmpty() || !url
.isValid() || isSearchUrl(url
)) {
524 url
= Dolphin::homeUrl();
526 m_urlNavigatorConnected
->setLocationUrl(url
);
529 m_searchModeEnabled
= enabled
;
531 Q_EMIT
searchModeEnabledChanged(enabled
);
534 bool DolphinViewContainer::isSearchModeEnabled() const
536 return m_searchModeEnabled
;
539 QString
DolphinViewContainer::placesText() const
543 if (isSearchModeEnabled()) {
544 text
= i18n("Search for %1 in %2", m_searchBox
->text(), m_searchBox
->searchPath().fileName());
546 text
= url().adjusted(QUrl::StripTrailingSlash
).fileName();
547 if (text
.isEmpty()) {
550 if (text
.isEmpty()) {
551 text
= url().scheme();
558 void DolphinViewContainer::reload()
561 m_messageWidget
->hide();
564 QString
DolphinViewContainer::captionWindowTitle() const
566 if (GeneralSettings::showFullPathInTitlebar() && !isSearchModeEnabled()) {
567 if (!url().isLocalFile()) {
568 return url().adjusted(QUrl::StripTrailingSlash
).toString();
570 return url().adjusted(QUrl::StripTrailingSlash
).path();
572 return DolphinViewContainer::caption();
576 QString
DolphinViewContainer::caption() const
578 if (isSearchModeEnabled()) {
579 if (currentSearchText().isEmpty()){
580 return i18n("Search");
582 return i18n("Search for %1", currentSearchText());
586 KFilePlacesModel
*placesModel
= DolphinPlacesModelSingleton::instance().placesModel();
587 const QString pattern
= url().adjusted(QUrl::StripTrailingSlash
).toString(QUrl::FullyEncoded
).append("/?");
588 const auto& matchedPlaces
= placesModel
->match(placesModel
->index(0,0), KFilePlacesModel::UrlRole
, QRegularExpression::anchoredPattern(pattern
), 1, Qt::MatchRegularExpression
);
590 if (!matchedPlaces
.isEmpty()) {
591 return placesModel
->text(matchedPlaces
.first());
595 if (!url().isLocalFile()) {
596 QUrl adjustedUrl
= url().adjusted(QUrl::StripTrailingSlash
);
598 if (!adjustedUrl
.fileName().isEmpty()) {
599 caption
= adjustedUrl
.fileName();
600 } else if (!adjustedUrl
.path().isEmpty() && adjustedUrl
.path() != "/") {
601 caption
= adjustedUrl
.path();
602 } else if (!adjustedUrl
.host().isEmpty()) {
603 caption
= adjustedUrl
.host();
605 caption
= adjustedUrl
.toString();
610 QString fileName
= url().adjusted(QUrl::StripTrailingSlash
).fileName();
611 if (fileName
.isEmpty()) {
618 void DolphinViewContainer::setUrl(const QUrl
& newUrl
)
620 if (newUrl
!= m_urlNavigator
->locationUrl()) {
621 m_urlNavigator
->setLocationUrl(newUrl
);
625 m_activityResourceInstance
->setUri(newUrl
);
629 void DolphinViewContainer::setFilterBarVisible(bool visible
)
631 Q_ASSERT(m_filterBar
);
633 m_view
->hideToolTip(ToolTipManager::HideBehavior::Instantly
);
635 m_filterBar
->setFocus();
636 m_filterBar
->selectAll();
642 void DolphinViewContainer::delayedStatusBarUpdate()
644 if (m_statusBarTimer
->isActive() && (m_statusBarTimestamp
.elapsed() > 2000)) {
645 // No update of the statusbar has been done during the last 2 seconds,
646 // although an update has been requested. Trigger an immediate update.
647 m_statusBarTimer
->stop();
650 // Invoke updateStatusBar() with a small delay. This assures that
651 // when a lot of delayedStatusBarUpdates() are done in a short time,
652 // no bottleneck is given.
653 m_statusBarTimer
->start();
657 void DolphinViewContainer::updateStatusBar()
659 m_statusBarTimestamp
.start();
660 m_view
->requestStatusBarText();
663 void DolphinViewContainer::updateDirectoryLoadingProgress(int percent
)
665 if (m_statusBar
->progressText().isEmpty()) {
666 m_statusBar
->setProgressText(i18nc("@info:progress", "Loading folder..."));
668 m_statusBar
->setProgress(percent
);
671 void DolphinViewContainer::updateDirectorySortingProgress(int percent
)
673 if (m_statusBar
->progressText().isEmpty()) {
674 m_statusBar
->setProgressText(i18nc("@info:progress", "Sorting..."));
676 m_statusBar
->setProgress(percent
);
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
->setProgressText(i18nc("@info", "Searching..."));
686 m_statusBar
->setProgress(-1);
688 // Trigger an undetermined progress indication. The progress
689 // information in percent will be triggered by the percent() signal
690 // of the directory lister later.
691 m_statusBar
->setProgressText(QString());
692 updateDirectoryLoadingProgress(-1);
696 void DolphinViewContainer::slotDirectoryLoadingCompleted()
698 if (!m_statusBar
->progressText().isEmpty()) {
699 m_statusBar
->setProgressText(QString());
700 m_statusBar
->setProgress(100);
703 if (isSearchUrl(url()) && m_view
->itemsCount() == 0) {
704 // The dir lister has been completed on a Baloo-URI and no items have been found. Instead
705 // of showing the default status bar information ("0 items") a more helpful information is given:
706 m_statusBar
->setText(i18nc("@info:status", "No items found."));
712 void DolphinViewContainer::slotDirectoryLoadingCanceled()
714 if (!m_statusBar
->progressText().isEmpty()) {
715 m_statusBar
->setProgressText(QString());
716 m_statusBar
->setProgress(100);
719 m_statusBar
->setText(QString());
722 void DolphinViewContainer::slotUrlIsFileError(const QUrl
& url
)
724 const KFileItem
item(url
);
726 // Find out if the file can be opened in the view (for example, this is the
727 // case if the file is an archive). The mime type must be known for that.
728 item
.determineMimeType();
729 const QUrl
& folderUrl
= DolphinView::openItemAsFolderUrl(item
, true);
730 if (!folderUrl
.isEmpty()) {
733 slotItemActivated(item
);
737 void DolphinViewContainer::slotItemActivated(const KFileItem
&item
)
739 // It is possible to activate items on inactive views by
740 // drag & drop operations. Assure that activating an item always
741 // results in an active view.
742 m_view
->setActive(true);
744 const QUrl
& url
= DolphinView::openItemAsFolderUrl(item
, GeneralSettings::browseThroughArchives());
745 if (!url
.isEmpty()) {
746 const auto modifiers
= QGuiApplication::keyboardModifiers();
747 // keep in sync with KUrlNavigator::slotNavigatorButtonClicked
748 if (modifiers
& Qt::ControlModifier
&& modifiers
& Qt::ShiftModifier
) {
749 Q_EMIT
activeTabRequested(url
);
750 } else if (modifiers
& Qt::ControlModifier
) {
751 Q_EMIT
tabRequested(url
);
752 } else if (modifiers
& Qt::ShiftModifier
) {
753 Dolphin::openNewWindow({KFilePlacesModel::convertedUrl(url
)}, this);
760 KIO::OpenUrlJob
*job
= new KIO::OpenUrlJob(item
.targetUrl(), item
.mimetype());
761 job
->setUiDelegate(new KIO::JobUiDelegate(KJobUiDelegate::AutoWarningHandlingEnabled
, this));
762 job
->setShowOpenOrExecuteDialog(true);
763 connect(job
, &KIO::OpenUrlJob::finished
, this, &DolphinViewContainer::slotOpenUrlFinished
);
767 void DolphinViewContainer::slotItemsActivated(const KFileItemList
& items
)
769 Q_ASSERT(items
.count() >= 2);
771 KFileItemActions
fileItemActions(this);
772 fileItemActions
.runPreferredApplications(items
);
775 void DolphinViewContainer::showItemInfo(const KFileItem
& item
)
778 m_statusBar
->resetToDefaultText();
780 m_statusBar
->setText(item
.getStatusBarInfo());
784 void DolphinViewContainer::closeFilterBar()
786 m_filterBar
->closeFilterBar();
788 Q_EMIT
showFilterBarChanged(false);
791 void DolphinViewContainer::setNameFilter(const QString
& nameFilter
)
793 m_view
->hideToolTip(ToolTipManager::HideBehavior::Instantly
);
794 m_view
->setNameFilter(nameFilter
);
795 delayedStatusBarUpdate();
798 void DolphinViewContainer::activate()
803 void DolphinViewContainer::slotUrlNavigatorLocationAboutToBeChanged(const QUrl
&)
808 void DolphinViewContainer::slotUrlNavigatorLocationChanged(const QUrl
& url
)
810 if (m_urlNavigatorConnected
) {
811 m_urlNavigatorConnected
->slotReturnPressed();
814 if (KProtocolManager::supportsListing(url
)) {
815 setSearchModeEnabled(isSearchUrl(url
));
817 tryRestoreViewState();
819 if (m_autoGrabFocus
&& isActive() && !isSearchUrl(url
)) {
820 // When an URL has been entered, the view should get the focus.
821 // The focus must be requested asynchronously, as changing the URL might create
822 // a new view widget.
823 QTimer::singleShot(0, this, &DolphinViewContainer::requestFocus
);
825 } else if (KProtocolManager::isSourceProtocol(url
)) {
826 if (url
.scheme().startsWith(QLatin1String("http"))) {
827 showMessage(i18nc("@info:status", // krazy:exclude=qmethods
828 "Dolphin does not support web pages, the web browser has been launched"),
831 showMessage(i18nc("@info:status",
832 "Protocol not supported by Dolphin, default application has been launched"),
836 QDesktopServices::openUrl(url
);
837 redirect(QUrl(), m_urlNavigator
->locationUrl(1));
839 showMessage(i18nc("@info:status", "Invalid protocol"), Error
);
840 m_urlNavigator
->goBack();
844 void DolphinViewContainer::slotUrlSelectionRequested(const QUrl
& url
)
846 m_view
->markUrlsAsSelected({url
});
847 m_view
->markUrlAsCurrent(url
); // makes the item scroll into view
850 void DolphinViewContainer::disableUrlNavigatorSelectionRequests()
852 disconnect(m_urlNavigator
.get(), &KUrlNavigator::urlSelectionRequested
,
853 this, &DolphinViewContainer::slotUrlSelectionRequested
);
856 void DolphinViewContainer::enableUrlNavigatorSelectionRequests()
858 connect(m_urlNavigator
.get(), &KUrlNavigator::urlSelectionRequested
,
859 this, &DolphinViewContainer::slotUrlSelectionRequested
);
862 void DolphinViewContainer::redirect(const QUrl
& oldUrl
, const QUrl
& newUrl
)
865 const bool block
= m_urlNavigator
->signalsBlocked();
866 m_urlNavigator
->blockSignals(true);
868 // Assure that the location state is reset for redirection URLs. This
869 // allows to skip redirection URLs when going back or forward in the
871 m_urlNavigator
->saveLocationState(QByteArray());
872 m_urlNavigator
->setLocationUrl(newUrl
);
873 setSearchModeEnabled(isSearchUrl(newUrl
));
875 m_urlNavigator
->blockSignals(block
);
878 void DolphinViewContainer::requestFocus()
883 void DolphinViewContainer::startSearching()
885 Q_CHECK_PTR(m_urlNavigatorConnected
);
886 const QUrl url
= m_searchBox
->urlForSearching();
887 if (url
.isValid() && !url
.isEmpty()) {
888 m_view
->setViewPropertiesContext(QStringLiteral("search"));
889 m_urlNavigatorConnected
->setLocationUrl(url
);
893 void DolphinViewContainer::closeSearchBox()
895 setSearchModeEnabled(false);
898 void DolphinViewContainer::stopDirectoryLoading()
900 m_view
->stopLoading();
901 m_statusBar
->setProgress(100);
904 void DolphinViewContainer::slotStatusBarZoomLevelChanged(int zoomLevel
)
906 m_view
->setZoomLevel(zoomLevel
);
909 void DolphinViewContainer::showErrorMessage(const QString
& msg
)
911 showMessage(msg
, Error
);
914 void DolphinViewContainer::slotPlacesModelChanged()
916 if (!GeneralSettings::showFullPathInTitlebar() && !isSearchModeEnabled()) {
917 Q_EMIT
captionChanged();
921 void DolphinViewContainer::slotHiddenFilesShownChanged(bool showHiddenFiles
)
923 if (m_urlNavigatorConnected
) {
924 m_urlNavigatorConnected
->setShowHiddenFolders(showHiddenFiles
);
928 void DolphinViewContainer::slotSortHiddenLastChanged(bool hiddenLast
)
930 if (m_urlNavigatorConnected
) {
931 m_urlNavigatorConnected
->setSortHiddenFoldersLast(hiddenLast
);
935 void DolphinViewContainer::slotOpenUrlFinished(KJob
*job
)
937 if (job
->error() && job
->error() != KIO::ERR_USER_CANCELED
) {
938 showErrorMessage(job
->errorString());
942 bool DolphinViewContainer::isSearchUrl(const QUrl
& url
) const
944 return url
.scheme().contains(QLatin1String("search"));
947 void DolphinViewContainer::saveViewState()
949 QByteArray locationState
;
950 QDataStream
stream(&locationState
, QIODevice::WriteOnly
);
951 m_view
->saveState(stream
);
952 m_urlNavigator
->saveLocationState(locationState
);
955 void DolphinViewContainer::tryRestoreViewState()
957 QByteArray locationState
= m_urlNavigator
->locationState();
958 if (!locationState
.isEmpty()) {
959 QDataStream
stream(&locationState
, QIODevice::ReadOnly
);
960 m_view
->restoreState(stream
);