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