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