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