]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinviewcontainer.cpp
Move focus from hiding selection mode bars to view
[dolphin.git] / src / dolphinviewcontainer.cpp
1 /*
2 * SPDX-FileCopyrightText: 2007 Peter Penz <peter.penz19@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7 #include "dolphinviewcontainer.h"
8
9 #include "dolphin_compactmodesettings.h"
10 #include "dolphin_contentdisplaysettings.h"
11 #include "dolphin_detailsmodesettings.h"
12 #include "dolphin_generalsettings.h"
13 #include "dolphin_iconsmodesettings.h"
14 #include "dolphindebug.h"
15 #include "dolphinplacesmodelsingleton.h"
16 #include "filterbar/filterbar.h"
17 #include "global.h"
18 #include "search/dolphinsearchbox.h"
19 #include "selectionmode/topbar.h"
20 #include "statusbar/dolphinstatusbar.h"
21
22 #include <KActionCollection>
23 #if HAVE_KACTIVITIES
24 #include <KActivities/ResourceInstance>
25 #endif
26 #include <KFileItemActions>
27 #include <KFilePlacesModel>
28 #include <kio_version.h>
29 #if KIO_VERSION >= QT_VERSION_CHECK(5, 98, 0)
30 #include <KIO/JobUiDelegateFactory>
31 #else
32 #include <KIO/JobUiDelegate>
33 #endif
34 #include <KIO/OpenUrlJob>
35 #include <KLocalizedString>
36 #include <KMessageWidget>
37 #include <KProtocolManager>
38 #include <KShell>
39
40 #include <QApplication>
41 #include <QDesktopServices>
42 #include <QDropEvent>
43 #include <QGridLayout>
44 #include <QGuiApplication>
45 #include <QRegularExpression>
46 #include <QTimer>
47 #include <QUrl>
48
49 // An overview of the widgets contained by this ViewContainer
50 struct LayoutStructure {
51 int searchBox = 0;
52 int messageWidget = 1;
53 int selectionModeTopBar = 2;
54 int view = 3;
55 int selectionModeBottomBar = 4;
56 int filterBar = 5;
57 int statusBar = 6;
58 };
59 constexpr LayoutStructure positionFor;
60
61 DolphinViewContainer::DolphinViewContainer(const QUrl &url, QWidget *parent)
62 : QWidget(parent)
63 , m_topLayout(nullptr)
64 , m_urlNavigator{new DolphinUrlNavigator(url)}
65 , m_urlNavigatorConnected{nullptr}
66 , m_searchBox(nullptr)
67 , m_searchModeEnabled(false)
68 , m_messageWidget(nullptr)
69 , m_selectionModeTopBar{nullptr}
70 , m_view(nullptr)
71 , m_filterBar(nullptr)
72 , m_selectionModeBottomBar{nullptr}
73 , m_statusBar(nullptr)
74 , m_statusBarTimer(nullptr)
75 , m_statusBarTimestamp()
76 , m_autoGrabFocus(true)
77 #if HAVE_KACTIVITIES
78 , m_activityResourceInstance(nullptr)
79 #endif
80 {
81 hide();
82
83 m_topLayout = new QGridLayout(this);
84 m_topLayout->setSpacing(0);
85 m_topLayout->setContentsMargins(0, 0, 0, 0);
86
87 m_searchBox = new DolphinSearchBox(this);
88 m_searchBox->hide();
89 connect(m_searchBox, &DolphinSearchBox::activated, this, &DolphinViewContainer::activate);
90 connect(m_searchBox, &DolphinSearchBox::closeRequest, this, &DolphinViewContainer::closeSearchBox);
91 connect(m_searchBox, &DolphinSearchBox::searchRequest, this, &DolphinViewContainer::startSearching);
92 connect(m_searchBox, &DolphinSearchBox::focusViewRequest, this, &DolphinViewContainer::requestFocus);
93 m_searchBox->setWhatsThis(xi18nc("@info:whatsthis findbar",
94 "<para>This helps you find files and folders. Enter a <emphasis>"
95 "search term</emphasis> and specify search settings with the "
96 "buttons at the bottom:<list><item>Filename/Content: "
97 "Does the item you are looking for contain the search terms "
98 "within its filename or its contents?<nl/>The contents of images, "
99 "audio files and videos will not be searched.</item><item>"
100 "From Here/Everywhere: Do you want to search in this "
101 "folder and its sub-folders or everywhere?</item><item>"
102 "More Options: Click this to search by media type, access "
103 "time or rating.</item><item>More Search Tools: Install other "
104 "means to find an item.</item></list></para>"));
105
106 m_messageWidget = new KMessageWidget(this);
107 m_messageWidget->setCloseButtonVisible(true);
108 m_messageWidget->hide();
109
110 #ifndef Q_OS_WIN
111 if (getuid() == 0) {
112 // We must be logged in as the root user; show a big scary warning
113 showMessage(i18n("Running Dolphin as root can be dangerous. Please be careful."), Warning);
114 }
115 #endif
116
117 // Initialize filter bar
118 m_filterBar = new FilterBar(this);
119 m_filterBar->setVisible(GeneralSettings::filterBar());
120
121 connect(m_filterBar, &FilterBar::filterChanged, this, &DolphinViewContainer::setNameFilter);
122 connect(m_filterBar, &FilterBar::closeRequest, this, &DolphinViewContainer::closeFilterBar);
123 connect(m_filterBar, &FilterBar::focusViewRequest, this, &DolphinViewContainer::requestFocus);
124
125 // Initialize the main view
126 m_view = new DolphinView(url, this);
127 connect(m_view, &DolphinView::urlChanged, m_filterBar, &FilterBar::clearIfUnlocked);
128 connect(m_view, &DolphinView::urlChanged, m_messageWidget, &KMessageWidget::hide);
129 // m_urlNavigator stays in sync with m_view's location changes and
130 // keeps track of them so going back and forth in the history works.
131 connect(m_view, &DolphinView::urlChanged, m_urlNavigator.get(), &DolphinUrlNavigator::setLocationUrl);
132 connect(m_urlNavigator.get(), &DolphinUrlNavigator::urlChanged, this, &DolphinViewContainer::slotUrlNavigatorLocationChanged);
133 connect(m_urlNavigator.get(), &DolphinUrlNavigator::urlAboutToBeChanged, this, &DolphinViewContainer::slotUrlNavigatorLocationAboutToBeChanged);
134 connect(m_urlNavigator.get(), &DolphinUrlNavigator::urlSelectionRequested, this, &DolphinViewContainer::slotUrlSelectionRequested);
135 connect(m_view, &DolphinView::writeStateChanged, this, &DolphinViewContainer::writeStateChanged);
136 connect(m_view, &DolphinView::requestItemInfo, this, &DolphinViewContainer::showItemInfo);
137 connect(m_view, &DolphinView::itemActivated, this, &DolphinViewContainer::slotItemActivated);
138 connect(m_view, &DolphinView::itemsActivated, this, &DolphinViewContainer::slotItemsActivated);
139 connect(m_view, &DolphinView::redirection, this, &DolphinViewContainer::redirect);
140 connect(m_view, &DolphinView::directoryLoadingStarted, this, &DolphinViewContainer::slotDirectoryLoadingStarted);
141 connect(m_view, &DolphinView::directoryLoadingCompleted, this, &DolphinViewContainer::slotDirectoryLoadingCompleted);
142 connect(m_view, &DolphinView::directoryLoadingCanceled, this, &DolphinViewContainer::slotDirectoryLoadingCanceled);
143 connect(m_view, &DolphinView::itemCountChanged, this, &DolphinViewContainer::delayedStatusBarUpdate);
144 connect(m_view, &DolphinView::directoryLoadingProgress, this, &DolphinViewContainer::updateDirectoryLoadingProgress);
145 connect(m_view, &DolphinView::directorySortingProgress, this, &DolphinViewContainer::updateDirectorySortingProgress);
146 connect(m_view, &DolphinView::selectionChanged, this, &DolphinViewContainer::delayedStatusBarUpdate);
147 connect(m_view, &DolphinView::errorMessage, this, &DolphinViewContainer::showErrorMessage);
148 connect(m_view, &DolphinView::urlIsFileError, this, &DolphinViewContainer::slotUrlIsFileError);
149 connect(m_view, &DolphinView::activated, this, &DolphinViewContainer::activate);
150 connect(m_view, &DolphinView::hiddenFilesShownChanged, this, &DolphinViewContainer::slotHiddenFilesShownChanged);
151 connect(m_view, &DolphinView::sortHiddenLastChanged, this, &DolphinViewContainer::slotSortHiddenLastChanged);
152 connect(m_view, &DolphinView::currentDirectoryRemoved, this, &DolphinViewContainer::slotCurrentDirectoryRemoved);
153
154 // Initialize status bar
155 m_statusBar = new DolphinStatusBar(this);
156 m_statusBar->setUrl(m_view->url());
157 m_statusBar->setZoomLevel(m_view->zoomLevel());
158 connect(m_view, &DolphinView::urlChanged, m_statusBar, &DolphinStatusBar::setUrl);
159 connect(m_view, &DolphinView::zoomLevelChanged, m_statusBar, &DolphinStatusBar::setZoomLevel);
160 connect(m_view, &DolphinView::infoMessage, m_statusBar, &DolphinStatusBar::setText);
161 connect(m_view, &DolphinView::operationCompletedMessage, m_statusBar, &DolphinStatusBar::setText);
162 connect(m_view, &DolphinView::statusBarTextChanged, m_statusBar, &DolphinStatusBar::setDefaultText);
163 connect(m_view, &DolphinView::statusBarTextChanged, m_statusBar, &DolphinStatusBar::resetToDefaultText);
164 connect(m_statusBar, &DolphinStatusBar::stopPressed, this, &DolphinViewContainer::stopDirectoryLoading);
165 connect(m_statusBar, &DolphinStatusBar::zoomLevelChanged, this, &DolphinViewContainer::slotStatusBarZoomLevelChanged);
166
167 m_statusBarTimer = new QTimer(this);
168 m_statusBarTimer->setSingleShot(true);
169 m_statusBarTimer->setInterval(300);
170 connect(m_statusBarTimer, &QTimer::timeout, this, &DolphinViewContainer::updateStatusBar);
171
172 KIO::FileUndoManager *undoManager = KIO::FileUndoManager::self();
173 connect(undoManager, &KIO::FileUndoManager::jobRecordingFinished, this, &DolphinViewContainer::delayedStatusBarUpdate);
174
175 m_topLayout->addWidget(m_searchBox, positionFor.searchBox, 0);
176 m_topLayout->addWidget(m_messageWidget, positionFor.messageWidget, 0);
177 m_topLayout->addWidget(m_view, positionFor.view, 0);
178 m_topLayout->addWidget(m_filterBar, positionFor.filterBar, 0);
179 m_topLayout->addWidget(m_statusBar, positionFor.statusBar, 0);
180
181 setSearchModeEnabled(isSearchUrl(url));
182
183 // Update view as the ContentDisplaySettings change
184 // this happens here and not in DolphinView as DolphinviewContainer and DolphinView are not in the same build target ATM
185 connect(ContentDisplaySettings::self(), &KCoreConfigSkeleton::configChanged, m_view, &DolphinView::reload);
186
187 KFilePlacesModel *placesModel = DolphinPlacesModelSingleton::instance().placesModel();
188 connect(placesModel, &KFilePlacesModel::dataChanged, this, &DolphinViewContainer::slotPlacesModelChanged);
189 connect(placesModel, &KFilePlacesModel::rowsInserted, this, &DolphinViewContainer::slotPlacesModelChanged);
190 connect(placesModel, &KFilePlacesModel::rowsRemoved, this, &DolphinViewContainer::slotPlacesModelChanged);
191
192 connect(this, &DolphinViewContainer::searchModeEnabledChanged, this, &DolphinViewContainer::captionChanged);
193
194 // Initialize kactivities resource instance
195
196 #if HAVE_KACTIVITIES
197 m_activityResourceInstance = new KActivities::ResourceInstance(window()->winId(), url);
198 m_activityResourceInstance->setParent(this);
199 #endif
200 }
201
202 DolphinViewContainer::~DolphinViewContainer()
203 {
204 }
205
206 QUrl DolphinViewContainer::url() const
207 {
208 return m_view->url();
209 }
210
211 KFileItem DolphinViewContainer::rootItem() const
212 {
213 return m_view->rootItem();
214 }
215
216 void DolphinViewContainer::setActive(bool active)
217 {
218 m_searchBox->setActive(active);
219 if (m_urlNavigatorConnected) {
220 m_urlNavigatorConnected->setActive(active);
221 }
222 m_view->setActive(active);
223
224 #if HAVE_KACTIVITIES
225 if (active) {
226 m_activityResourceInstance->notifyFocusedIn();
227 } else {
228 m_activityResourceInstance->notifyFocusedOut();
229 }
230 #endif
231 }
232
233 bool DolphinViewContainer::isActive() const
234 {
235 return m_view->isActive();
236 }
237
238 void DolphinViewContainer::setAutoGrabFocus(bool grab)
239 {
240 m_autoGrabFocus = grab;
241 }
242
243 bool DolphinViewContainer::autoGrabFocus() const
244 {
245 return m_autoGrabFocus;
246 }
247
248 QString DolphinViewContainer::currentSearchText() const
249 {
250 return m_searchBox->text();
251 }
252
253 const DolphinStatusBar *DolphinViewContainer::statusBar() const
254 {
255 return m_statusBar;
256 }
257
258 DolphinStatusBar *DolphinViewContainer::statusBar()
259 {
260 return m_statusBar;
261 }
262
263 const DolphinUrlNavigator *DolphinViewContainer::urlNavigator() const
264 {
265 return m_urlNavigatorConnected;
266 }
267
268 DolphinUrlNavigator *DolphinViewContainer::urlNavigator()
269 {
270 return m_urlNavigatorConnected;
271 }
272
273 const DolphinUrlNavigator *DolphinViewContainer::urlNavigatorInternalWithHistory() const
274 {
275 return m_urlNavigator.get();
276 }
277
278 DolphinUrlNavigator *DolphinViewContainer::urlNavigatorInternalWithHistory()
279 {
280 return m_urlNavigator.get();
281 }
282
283 const DolphinView *DolphinViewContainer::view() const
284 {
285 return m_view;
286 }
287
288 DolphinView *DolphinViewContainer::view()
289 {
290 return m_view;
291 }
292
293 void DolphinViewContainer::connectUrlNavigator(DolphinUrlNavigator *urlNavigator)
294 {
295 Q_CHECK_PTR(urlNavigator);
296 Q_ASSERT(!m_urlNavigatorConnected);
297 Q_ASSERT(m_urlNavigator.get() != urlNavigator);
298 Q_CHECK_PTR(m_view);
299
300 urlNavigator->setLocationUrl(m_view->url());
301 urlNavigator->setShowHiddenFolders(m_view->hiddenFilesShown());
302 urlNavigator->setSortHiddenFoldersLast(m_view->sortHiddenLast());
303 if (m_urlNavigatorVisualState) {
304 urlNavigator->setVisualState(*m_urlNavigatorVisualState.get());
305 m_urlNavigatorVisualState.reset();
306 }
307 urlNavigator->setActive(isActive());
308
309 // Url changes are still done via m_urlNavigator.
310 connect(urlNavigator, &DolphinUrlNavigator::urlChanged, m_urlNavigator.get(), &DolphinUrlNavigator::setLocationUrl);
311 connect(urlNavigator, &DolphinUrlNavigator::urlsDropped, this, [=](const QUrl &destination, QDropEvent *event) {
312 m_view->dropUrls(destination, event, urlNavigator->dropWidget());
313 });
314 // Aside from these, only visual things need to be connected.
315 connect(m_view, &DolphinView::urlChanged, urlNavigator, &DolphinUrlNavigator::setLocationUrl);
316 connect(urlNavigator, &DolphinUrlNavigator::activated, this, &DolphinViewContainer::activate);
317
318 m_urlNavigatorConnected = urlNavigator;
319 }
320
321 void DolphinViewContainer::disconnectUrlNavigator()
322 {
323 if (!m_urlNavigatorConnected) {
324 return;
325 }
326
327 disconnect(m_urlNavigatorConnected, &DolphinUrlNavigator::urlChanged, m_urlNavigator.get(), &DolphinUrlNavigator::setLocationUrl);
328 disconnect(m_urlNavigatorConnected, &DolphinUrlNavigator::urlsDropped, this, nullptr);
329 disconnect(m_view, &DolphinView::urlChanged, m_urlNavigatorConnected, &DolphinUrlNavigator::setLocationUrl);
330 disconnect(m_urlNavigatorConnected, &DolphinUrlNavigator::activated, this, &DolphinViewContainer::activate);
331
332 m_urlNavigatorVisualState = m_urlNavigatorConnected->visualState();
333 m_urlNavigatorConnected = nullptr;
334 }
335
336 void DolphinViewContainer::setSelectionModeEnabled(bool enabled, KActionCollection *actionCollection, SelectionMode::BottomBar::Contents bottomBarContents)
337 {
338 const bool wasEnabled = m_view->selectionMode();
339 m_view->setSelectionModeEnabled(enabled);
340
341 if (!enabled) {
342 if (!wasEnabled) {
343 return; // nothing to do here
344 }
345 Q_CHECK_PTR(m_selectionModeTopBar); // there is no point in disabling selectionMode when it wasn't even enabled once.
346 Q_CHECK_PTR(m_selectionModeBottomBar);
347 if (m_selectionModeTopBar->isAncestorOf(QApplication::focusWidget()) || m_selectionModeBottomBar->isAncestorOf(QApplication::focusWidget())) {
348 m_view->setFocus();
349 }
350 m_selectionModeTopBar->setVisible(false, WithAnimation);
351 m_selectionModeBottomBar->setVisible(false, WithAnimation);
352 Q_EMIT selectionModeChanged(false);
353 return;
354 }
355
356 if (!m_selectionModeTopBar) {
357 // Changing the location will disable selection mode.
358 connect(m_urlNavigator.get(), &DolphinUrlNavigator::urlChanged, this, [this]() {
359 setSelectionModeEnabled(false);
360 });
361
362 m_selectionModeTopBar = new SelectionMode::TopBar(this); // will be created hidden
363 connect(m_selectionModeTopBar, &SelectionMode::TopBar::selectionModeLeavingRequested, this, [this]() {
364 setSelectionModeEnabled(false);
365 });
366 m_topLayout->addWidget(m_selectionModeTopBar, positionFor.selectionModeTopBar, 0);
367 }
368
369 if (!m_selectionModeBottomBar) {
370 m_selectionModeBottomBar = new SelectionMode::BottomBar(actionCollection, this);
371 connect(m_view, &DolphinView::selectionChanged, this, [this](const KFileItemList &selection) {
372 m_selectionModeBottomBar->slotSelectionChanged(selection, m_view->url());
373 });
374 connect(m_selectionModeBottomBar, &SelectionMode::BottomBar::error, this, [this](const QString &errorMessage) {
375 showErrorMessage(errorMessage);
376 });
377 connect(m_selectionModeBottomBar, &SelectionMode::BottomBar::selectionModeLeavingRequested, this, [this]() {
378 setSelectionModeEnabled(false);
379 });
380 m_topLayout->addWidget(m_selectionModeBottomBar, positionFor.selectionModeBottomBar, 0);
381 }
382 m_selectionModeBottomBar->resetContents(bottomBarContents);
383 if (bottomBarContents == SelectionMode::BottomBar::GeneralContents) {
384 m_selectionModeBottomBar->slotSelectionChanged(m_view->selectedItems(), m_view->url());
385 }
386
387 if (!wasEnabled) {
388 m_selectionModeTopBar->setVisible(true, WithAnimation);
389 m_selectionModeBottomBar->setVisible(true, WithAnimation);
390 Q_EMIT selectionModeChanged(true);
391 }
392 }
393
394 bool DolphinViewContainer::isSelectionModeEnabled() const
395 {
396 const bool isEnabled = m_view->selectionMode();
397 Q_ASSERT((!isEnabled
398 // We can't assert that the bars are invisible only because the selection mode is disabled because the hide animation might still be playing.
399 && (!m_selectionModeBottomBar || !m_selectionModeBottomBar->isEnabled() || !m_selectionModeBottomBar->isVisible()
400 || m_selectionModeBottomBar->contents() == SelectionMode::BottomBar::PasteContents))
401 || (isEnabled && m_selectionModeTopBar
402 && m_selectionModeTopBar->isVisible()
403 // 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.
404 && m_selectionModeBottomBar
405 && (m_selectionModeBottomBar->isVisible() || m_selectionModeBottomBar->contents() == SelectionMode::BottomBar::GeneralContents)));
406 return isEnabled;
407 }
408
409 void DolphinViewContainer::slotSplitTabDisabled()
410 {
411 if (m_selectionModeBottomBar) {
412 m_selectionModeBottomBar->slotSplitTabDisabled();
413 }
414 }
415
416 void DolphinViewContainer::showMessage(const QString &msg, MessageType type)
417 {
418 if (msg.isEmpty()) {
419 return;
420 }
421
422 m_messageWidget->setText(msg);
423
424 // TODO: wrap at arbitrary character positions once QLabel can do this
425 // https://bugreports.qt.io/browse/QTBUG-1276
426 m_messageWidget->setWordWrap(true);
427
428 switch (type) {
429 case Information:
430 m_messageWidget->setMessageType(KMessageWidget::Information);
431 break;
432 case Warning:
433 m_messageWidget->setMessageType(KMessageWidget::Warning);
434 break;
435 case Error:
436 m_messageWidget->setMessageType(KMessageWidget::Error);
437 break;
438 default:
439 Q_ASSERT(false);
440 break;
441 }
442
443 m_messageWidget->setWordWrap(false);
444 const int unwrappedWidth = m_messageWidget->sizeHint().width();
445 m_messageWidget->setWordWrap(unwrappedWidth > size().width());
446
447 if (m_messageWidget->isVisible()) {
448 m_messageWidget->hide();
449 }
450 m_messageWidget->animatedShow();
451 }
452
453 void DolphinViewContainer::readSettings()
454 {
455 // The startup settings should (only) get applied if they have been
456 // modified by the user. Otherwise keep the (possibly) different current
457 // setting of the filterbar.
458 if (GeneralSettings::modifiedStartupSettings()) {
459 setFilterBarVisible(GeneralSettings::filterBar());
460 }
461
462 m_view->readSettings();
463 m_statusBar->readSettings();
464 }
465
466 bool DolphinViewContainer::isFilterBarVisible() const
467 {
468 return m_filterBar->isVisible();
469 }
470
471 void DolphinViewContainer::setSearchModeEnabled(bool enabled)
472 {
473 m_searchBox->setVisible(enabled);
474
475 if (enabled) {
476 const QUrl &locationUrl = m_urlNavigator->locationUrl();
477 m_searchBox->fromSearchUrl(locationUrl);
478 }
479
480 if (enabled == isSearchModeEnabled()) {
481 if (enabled && !m_searchBox->hasFocus()) {
482 m_searchBox->setFocus();
483 m_searchBox->selectAll();
484 }
485 return;
486 }
487
488 if (!enabled) {
489 m_view->setViewPropertiesContext(QString());
490
491 // Restore the URL for the URL navigator. If Dolphin has been
492 // started with a search-URL, the home URL is used as fallback.
493 QUrl url = m_searchBox->searchPath();
494 if (url.isEmpty() || !url.isValid() || isSearchUrl(url)) {
495 url = Dolphin::homeUrl();
496 }
497 m_urlNavigatorConnected->setLocationUrl(url);
498 }
499
500 m_searchModeEnabled = enabled;
501
502 Q_EMIT searchModeEnabledChanged(enabled);
503 }
504
505 bool DolphinViewContainer::isSearchModeEnabled() const
506 {
507 return m_searchModeEnabled;
508 }
509
510 QString DolphinViewContainer::placesText() const
511 {
512 QString text;
513
514 if (isSearchModeEnabled()) {
515 text = i18n("Search for %1 in %2", m_searchBox->text(), m_searchBox->searchPath().fileName());
516 } else {
517 text = url().adjusted(QUrl::StripTrailingSlash).fileName();
518 if (text.isEmpty()) {
519 text = url().host();
520 }
521 if (text.isEmpty()) {
522 text = url().scheme();
523 }
524 }
525
526 return text;
527 }
528
529 void DolphinViewContainer::reload()
530 {
531 view()->reload();
532 m_messageWidget->hide();
533 }
534
535 QString DolphinViewContainer::captionWindowTitle() const
536 {
537 if (GeneralSettings::showFullPathInTitlebar() && !isSearchModeEnabled()) {
538 if (!url().isLocalFile()) {
539 return url().adjusted(QUrl::StripTrailingSlash).toString();
540 }
541 return url().adjusted(QUrl::StripTrailingSlash).path();
542 } else {
543 return DolphinViewContainer::caption();
544 }
545 }
546
547 QString DolphinViewContainer::caption() const
548 {
549 if (isSearchModeEnabled()) {
550 if (currentSearchText().isEmpty()) {
551 return i18n("Search");
552 } else {
553 return i18n("Search for %1", currentSearchText());
554 }
555 }
556
557 KFilePlacesModel *placesModel = DolphinPlacesModelSingleton::instance().placesModel();
558 const QString pattern = url().adjusted(QUrl::StripTrailingSlash).toString(QUrl::FullyEncoded).append("/?");
559 const auto &matchedPlaces =
560 placesModel->match(placesModel->index(0, 0), KFilePlacesModel::UrlRole, QRegularExpression::anchoredPattern(pattern), 1, Qt::MatchRegularExpression);
561
562 if (!matchedPlaces.isEmpty()) {
563 return placesModel->text(matchedPlaces.first());
564 }
565
566 if (!url().isLocalFile()) {
567 QUrl adjustedUrl = url().adjusted(QUrl::StripTrailingSlash);
568 QString caption;
569 if (!adjustedUrl.fileName().isEmpty()) {
570 caption = adjustedUrl.fileName();
571 } else if (!adjustedUrl.path().isEmpty() && adjustedUrl.path() != "/") {
572 caption = adjustedUrl.path();
573 } else if (!adjustedUrl.host().isEmpty()) {
574 caption = adjustedUrl.host();
575 } else {
576 caption = adjustedUrl.toString();
577 }
578 return caption;
579 }
580
581 QString fileName = url().adjusted(QUrl::StripTrailingSlash).fileName();
582 if (fileName.isEmpty()) {
583 fileName = '/';
584 }
585
586 return fileName;
587 }
588
589 void DolphinViewContainer::setUrl(const QUrl &newUrl)
590 {
591 if (newUrl != m_urlNavigator->locationUrl()) {
592 m_urlNavigator->setLocationUrl(newUrl);
593 }
594
595 #if HAVE_KACTIVITIES
596 m_activityResourceInstance->setUri(newUrl);
597 #endif
598 }
599
600 void DolphinViewContainer::setFilterBarVisible(bool visible)
601 {
602 Q_ASSERT(m_filterBar);
603 if (visible) {
604 m_view->hideToolTip(ToolTipManager::HideBehavior::Instantly);
605 m_filterBar->show();
606 m_filterBar->setFocus();
607 m_filterBar->selectAll();
608 } else {
609 closeFilterBar();
610 }
611 }
612
613 void DolphinViewContainer::delayedStatusBarUpdate()
614 {
615 if (m_statusBarTimer->isActive() && (m_statusBarTimestamp.elapsed() > 2000)) {
616 // No update of the statusbar has been done during the last 2 seconds,
617 // although an update has been requested. Trigger an immediate update.
618 m_statusBarTimer->stop();
619 updateStatusBar();
620 } else {
621 // Invoke updateStatusBar() with a small delay. This assures that
622 // when a lot of delayedStatusBarUpdates() are done in a short time,
623 // no bottleneck is given.
624 m_statusBarTimer->start();
625 }
626 }
627
628 void DolphinViewContainer::updateStatusBar()
629 {
630 m_statusBarTimestamp.start();
631 m_view->requestStatusBarText();
632 }
633
634 void DolphinViewContainer::updateDirectoryLoadingProgress(int percent)
635 {
636 if (m_statusBar->progressText().isEmpty()) {
637 m_statusBar->setProgressText(i18nc("@info:progress", "Loading folder..."));
638 }
639 m_statusBar->setProgress(percent);
640 }
641
642 void DolphinViewContainer::updateDirectorySortingProgress(int percent)
643 {
644 if (m_statusBar->progressText().isEmpty()) {
645 m_statusBar->setProgressText(i18nc("@info:progress", "Sorting..."));
646 }
647 m_statusBar->setProgress(percent);
648 }
649
650 void DolphinViewContainer::slotDirectoryLoadingStarted()
651 {
652 if (isSearchUrl(url())) {
653 // Search KIO-slaves usually don't provide any progress information. Give
654 // a hint to the user that a searching is done:
655 updateStatusBar();
656 m_statusBar->setProgressText(i18nc("@info", "Searching..."));
657 m_statusBar->setProgress(-1);
658 } else {
659 // Trigger an undetermined progress indication. The progress
660 // information in percent will be triggered by the percent() signal
661 // of the directory lister later.
662 m_statusBar->setProgressText(QString());
663 updateDirectoryLoadingProgress(-1);
664 }
665 }
666
667 void DolphinViewContainer::slotDirectoryLoadingCompleted()
668 {
669 if (!m_statusBar->progressText().isEmpty()) {
670 m_statusBar->setProgressText(QString());
671 m_statusBar->setProgress(100);
672 }
673
674 if (isSearchUrl(url()) && m_view->itemsCount() == 0) {
675 // The dir lister has been completed on a Baloo-URI and no items have been found. Instead
676 // of showing the default status bar information ("0 items") a more helpful information is given:
677 m_statusBar->setText(i18nc("@info:status", "No items found."));
678 } else {
679 updateStatusBar();
680 }
681 }
682
683 void DolphinViewContainer::slotDirectoryLoadingCanceled()
684 {
685 if (!m_statusBar->progressText().isEmpty()) {
686 m_statusBar->setProgressText(QString());
687 m_statusBar->setProgress(100);
688 }
689
690 m_statusBar->setText(QString());
691 }
692
693 void DolphinViewContainer::slotUrlIsFileError(const QUrl &url)
694 {
695 const KFileItem item(url);
696
697 // Find out if the file can be opened in the view (for example, this is the
698 // case if the file is an archive). The mime type must be known for that.
699 item.determineMimeType();
700 const QUrl &folderUrl = DolphinView::openItemAsFolderUrl(item, true);
701 if (!folderUrl.isEmpty()) {
702 setUrl(folderUrl);
703 } else {
704 slotItemActivated(item);
705 }
706 }
707
708 void DolphinViewContainer::slotItemActivated(const KFileItem &item)
709 {
710 // It is possible to activate items on inactive views by
711 // drag & drop operations. Assure that activating an item always
712 // results in an active view.
713 m_view->setActive(true);
714
715 const QUrl &url = DolphinView::openItemAsFolderUrl(item, GeneralSettings::browseThroughArchives());
716 if (!url.isEmpty()) {
717 const auto modifiers = QGuiApplication::keyboardModifiers();
718 // keep in sync with KUrlNavigator::slotNavigatorButtonClicked
719 if (modifiers & Qt::ControlModifier && modifiers & Qt::ShiftModifier) {
720 Q_EMIT activeTabRequested(url);
721 } else if (modifiers & Qt::ControlModifier) {
722 Q_EMIT tabRequested(url);
723 } else if (modifiers & Qt::ShiftModifier) {
724 Dolphin::openNewWindow({KFilePlacesModel::convertedUrl(url)}, this);
725 } else {
726 setUrl(url);
727 }
728 return;
729 }
730
731 KIO::OpenUrlJob *job = new KIO::OpenUrlJob(item.targetUrl(), item.mimetype());
732 #if KIO_VERSION >= QT_VERSION_CHECK(5, 98, 0)
733 job->setUiDelegate(KIO::createDefaultJobUiDelegate(KJobUiDelegate::AutoHandlingEnabled, this));
734 #else
735 job->setUiDelegate(new KIO::JobUiDelegate(KJobUiDelegate::AutoHandlingEnabled, this));
736 #endif
737 job->setShowOpenOrExecuteDialog(true);
738 connect(job, &KIO::OpenUrlJob::finished, this, &DolphinViewContainer::slotOpenUrlFinished);
739 job->start();
740 }
741
742 void DolphinViewContainer::slotItemsActivated(const KFileItemList &items)
743 {
744 Q_ASSERT(items.count() >= 2);
745
746 KFileItemActions fileItemActions(this);
747 fileItemActions.runPreferredApplications(items);
748 }
749
750 void DolphinViewContainer::showItemInfo(const KFileItem &item)
751 {
752 if (item.isNull()) {
753 m_statusBar->resetToDefaultText();
754 } else {
755 m_statusBar->setText(item.getStatusBarInfo());
756 }
757 }
758
759 void DolphinViewContainer::closeFilterBar()
760 {
761 m_filterBar->closeFilterBar();
762 m_view->setFocus();
763 Q_EMIT showFilterBarChanged(false);
764 }
765
766 void DolphinViewContainer::clearFilterBar()
767 {
768 m_filterBar->clearIfUnlocked();
769 }
770
771 void DolphinViewContainer::setNameFilter(const QString &nameFilter)
772 {
773 m_view->hideToolTip(ToolTipManager::HideBehavior::Instantly);
774 m_view->setNameFilter(nameFilter);
775 delayedStatusBarUpdate();
776 }
777
778 void DolphinViewContainer::activate()
779 {
780 setActive(true);
781 }
782
783 void DolphinViewContainer::slotUrlNavigatorLocationAboutToBeChanged(const QUrl &)
784 {
785 saveViewState();
786 }
787
788 void DolphinViewContainer::slotUrlNavigatorLocationChanged(const QUrl &url)
789 {
790 if (m_urlNavigatorConnected) {
791 m_urlNavigatorConnected->slotReturnPressed();
792 }
793
794 if (KProtocolManager::supportsListing(url)) {
795 setSearchModeEnabled(isSearchUrl(url));
796 m_view->setUrl(url);
797 tryRestoreViewState();
798
799 if (m_autoGrabFocus && isActive() && !isSearchUrl(url)) {
800 // When an URL has been entered, the view should get the focus.
801 // The focus must be requested asynchronously, as changing the URL might create
802 // a new view widget.
803 QTimer::singleShot(0, this, &DolphinViewContainer::requestFocus);
804 }
805 } else if (KProtocolManager::isSourceProtocol(url)) {
806 if (url.scheme().startsWith(QLatin1String("http"))) {
807 showMessage(i18nc("@info:status", // krazy:exclude=qmethods
808 "Dolphin does not support web pages, the web browser has been launched"),
809 Information);
810 } else {
811 showMessage(i18nc("@info:status", "Protocol not supported by Dolphin, default application has been launched"), Information);
812 }
813
814 QDesktopServices::openUrl(url);
815 redirect(QUrl(), m_urlNavigator->locationUrl(1));
816 } else {
817 showMessage(i18nc("@info:status", "Invalid protocol"), Error);
818 m_urlNavigator->goBack();
819 }
820 }
821
822 void DolphinViewContainer::slotUrlSelectionRequested(const QUrl &url)
823 {
824 m_view->markUrlsAsSelected({url});
825 m_view->markUrlAsCurrent(url); // makes the item scroll into view
826 }
827
828 void DolphinViewContainer::disableUrlNavigatorSelectionRequests()
829 {
830 disconnect(m_urlNavigator.get(), &KUrlNavigator::urlSelectionRequested, this, &DolphinViewContainer::slotUrlSelectionRequested);
831 }
832
833 void DolphinViewContainer::enableUrlNavigatorSelectionRequests()
834 {
835 connect(m_urlNavigator.get(), &KUrlNavigator::urlSelectionRequested, this, &DolphinViewContainer::slotUrlSelectionRequested);
836 }
837
838 void DolphinViewContainer::redirect(const QUrl &oldUrl, const QUrl &newUrl)
839 {
840 Q_UNUSED(oldUrl)
841 const bool block = m_urlNavigator->signalsBlocked();
842 m_urlNavigator->blockSignals(true);
843
844 // Assure that the location state is reset for redirection URLs. This
845 // allows to skip redirection URLs when going back or forward in the
846 // URL history.
847 m_urlNavigator->saveLocationState(QByteArray());
848 m_urlNavigator->setLocationUrl(newUrl);
849 setSearchModeEnabled(isSearchUrl(newUrl));
850
851 m_urlNavigator->blockSignals(block);
852 }
853
854 void DolphinViewContainer::requestFocus()
855 {
856 m_view->setFocus();
857 }
858
859 void DolphinViewContainer::startSearching()
860 {
861 Q_CHECK_PTR(m_urlNavigatorConnected);
862 const QUrl url = m_searchBox->urlForSearching();
863 if (url.isValid() && !url.isEmpty()) {
864 m_view->setViewPropertiesContext(QStringLiteral("search"));
865 m_urlNavigatorConnected->setLocationUrl(url);
866 }
867 }
868
869 void DolphinViewContainer::closeSearchBox()
870 {
871 setSearchModeEnabled(false);
872 }
873
874 void DolphinViewContainer::stopDirectoryLoading()
875 {
876 m_view->stopLoading();
877 m_statusBar->setProgress(100);
878 }
879
880 void DolphinViewContainer::slotStatusBarZoomLevelChanged(int zoomLevel)
881 {
882 m_view->setZoomLevel(zoomLevel);
883 }
884
885 void DolphinViewContainer::showErrorMessage(const QString &msg)
886 {
887 showMessage(msg, Error);
888 }
889
890 void DolphinViewContainer::slotPlacesModelChanged()
891 {
892 if (!GeneralSettings::showFullPathInTitlebar() && !isSearchModeEnabled()) {
893 Q_EMIT captionChanged();
894 }
895 }
896
897 void DolphinViewContainer::slotHiddenFilesShownChanged(bool showHiddenFiles)
898 {
899 if (m_urlNavigatorConnected) {
900 m_urlNavigatorConnected->setShowHiddenFolders(showHiddenFiles);
901 }
902 }
903
904 void DolphinViewContainer::slotSortHiddenLastChanged(bool hiddenLast)
905 {
906 if (m_urlNavigatorConnected) {
907 m_urlNavigatorConnected->setSortHiddenFoldersLast(hiddenLast);
908 }
909 }
910
911 void DolphinViewContainer::slotCurrentDirectoryRemoved()
912 {
913 const QString location(url().toDisplayString(QUrl::PreferLocalFile));
914 if (url().isLocalFile()) {
915 const QString dirPath = url().toLocalFile();
916 const QString newPath = getNearestExistingAncestorOfPath(dirPath);
917 const QUrl newUrl = QUrl::fromLocalFile(newPath);
918 setUrl(newUrl);
919 }
920
921 showMessage(xi18n("Current location changed, <filename>%1</filename> is no longer accessible.", location), Warning);
922 }
923
924 void DolphinViewContainer::slotOpenUrlFinished(KJob *job)
925 {
926 if (job->error() && job->error() != KIO::ERR_USER_CANCELED) {
927 showErrorMessage(job->errorString());
928 }
929 }
930
931 bool DolphinViewContainer::isSearchUrl(const QUrl &url) const
932 {
933 return url.scheme().contains(QLatin1String("search"));
934 }
935
936 void DolphinViewContainer::saveViewState()
937 {
938 QByteArray locationState;
939 QDataStream stream(&locationState, QIODevice::WriteOnly);
940 m_view->saveState(stream);
941 m_urlNavigator->saveLocationState(locationState);
942 }
943
944 void DolphinViewContainer::tryRestoreViewState()
945 {
946 QByteArray locationState = m_urlNavigator->locationState();
947 if (!locationState.isEmpty()) {
948 QDataStream stream(&locationState, QIODevice::ReadOnly);
949 m_view->restoreState(stream);
950 }
951 }
952
953 QString DolphinViewContainer::getNearestExistingAncestorOfPath(const QString &path) const
954 {
955 QDir dir(path);
956 do {
957 dir.setPath(QDir::cleanPath(dir.filePath(QStringLiteral(".."))));
958 } while (!dir.exists() && !dir.isRoot());
959
960 return dir.exists() ? dir.path() : QString{};
961 }