]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinviewcontainer.cpp
f3899560329e0bf0b937c3f179166789deee03ea
[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->setSelectionModeEnabled(enabled);
380
381 if (!enabled) {
382 if (!wasEnabled) {
383 return; // nothing to do here
384 }
385 Q_CHECK_PTR(m_selectionModeTopBar); // there is no point in disabling selectionMode when it wasn't even enabled once.
386 Q_CHECK_PTR(m_selectionModeBottomBar);
387 m_selectionModeTopBar->setVisible(false, WithAnimation);
388 m_selectionModeBottomBar->setVisible(false, WithAnimation);
389 Q_EMIT selectionModeChanged(false);
390 return;
391 }
392
393 if (!m_selectionModeTopBar) {
394 // Changing the location will disable selection mode.
395 connect(m_urlNavigator.get(), &DolphinUrlNavigator::urlChanged, this, [this]() {
396 setSelectionModeEnabled(false);
397 });
398
399 m_selectionModeTopBar = new SelectionMode::TopBar(this); // will be created hidden
400 connect(m_selectionModeTopBar, &SelectionMode::TopBar::selectionModeLeavingRequested, this, [this]() {
401 setSelectionModeEnabled(false);
402 });
403 m_topLayout->addWidget(m_selectionModeTopBar, positionFor.selectionModeTopBar, 0);
404 }
405
406 if (!m_selectionModeBottomBar) {
407 m_selectionModeBottomBar = new SelectionMode::BottomBar(actionCollection, this);
408 connect(m_view, &DolphinView::selectionChanged, this, [this](const KFileItemList &selection) {
409 m_selectionModeBottomBar->slotSelectionChanged(selection, m_view->url());
410 });
411 connect(m_selectionModeBottomBar, &SelectionMode::BottomBar::error, this, [this](const QString &errorMessage) {
412 showErrorMessage(errorMessage);
413 });
414 connect(m_selectionModeBottomBar, &SelectionMode::BottomBar::selectionModeLeavingRequested, this, [this]() {
415 setSelectionModeEnabled(false);
416 });
417 m_topLayout->addWidget(m_selectionModeBottomBar, positionFor.selectionModeBottomBar, 0);
418 }
419 m_selectionModeBottomBar->resetContents(bottomBarContents);
420 if (bottomBarContents == SelectionMode::BottomBar::GeneralContents) {
421 m_selectionModeBottomBar->slotSelectionChanged(m_view->selectedItems(), m_view->url());
422 }
423
424 if (!wasEnabled) {
425 m_selectionModeTopBar->setVisible(true, WithAnimation);
426 m_selectionModeBottomBar->setVisible(true, WithAnimation);
427 Q_EMIT selectionModeChanged(true);
428 }
429 }
430
431 bool DolphinViewContainer::isSelectionModeEnabled() const
432 {
433 const bool isEnabled = m_view->selectionMode();
434 Q_ASSERT((!isEnabled
435 // We can't assert that the bars are invisible only because the selection mode is disabled because the hide animation might still be playing.
436 && (!m_selectionModeBottomBar || !m_selectionModeBottomBar->isEnabled() ||
437 !m_selectionModeBottomBar->isVisible() || m_selectionModeBottomBar->contents() == SelectionMode::BottomBar::PasteContents))
438 || ( isEnabled
439 && m_selectionModeTopBar && m_selectionModeTopBar->isVisible()
440 // 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.
441 && m_selectionModeBottomBar
442 && (m_selectionModeBottomBar->isVisible() || m_selectionModeBottomBar->contents() == SelectionMode::BottomBar::GeneralContents)));
443 return isEnabled;
444 }
445
446 void DolphinViewContainer::slotSplitTabDisabled()
447 {
448 if (m_selectionModeBottomBar) {
449 m_selectionModeBottomBar->slotSplitTabDisabled();
450 }
451 }
452
453
454 void DolphinViewContainer::showMessage(const QString& msg, MessageType type)
455 {
456 if (msg.isEmpty()) {
457 return;
458 }
459
460 m_messageWidget->setText(msg);
461
462 // TODO: wrap at arbitrary character positions once QLabel can do this
463 // https://bugreports.qt.io/browse/QTBUG-1276
464 m_messageWidget->setWordWrap(true);
465
466 switch (type) {
467 case Information: m_messageWidget->setMessageType(KMessageWidget::Information); break;
468 case Warning: m_messageWidget->setMessageType(KMessageWidget::Warning); break;
469 case Error: m_messageWidget->setMessageType(KMessageWidget::Error); break;
470 default:
471 Q_ASSERT(false);
472 break;
473 }
474
475 m_messageWidget->setWordWrap(false);
476 const int unwrappedWidth = m_messageWidget->sizeHint().width();
477 m_messageWidget->setWordWrap(unwrappedWidth > size().width());
478
479 if (m_messageWidget->isVisible()) {
480 m_messageWidget->hide();
481 }
482 m_messageWidget->animatedShow();
483 }
484
485 void DolphinViewContainer::readSettings()
486 {
487 // The startup settings should (only) get applied if they have been
488 // modified by the user. Otherwise keep the (possibly) different current
489 // setting of the filterbar.
490 if (GeneralSettings::modifiedStartupSettings()) {
491 setFilterBarVisible(GeneralSettings::filterBar());
492 }
493
494 m_view->readSettings();
495 m_statusBar->readSettings();
496 }
497
498 bool DolphinViewContainer::isFilterBarVisible() const
499 {
500 return m_filterBar->isVisible();
501 }
502
503 void DolphinViewContainer::setSearchModeEnabled(bool enabled)
504 {
505 m_searchBox->setVisible(enabled);
506
507 if (enabled) {
508 const QUrl& locationUrl = m_urlNavigator->locationUrl();
509 m_searchBox->fromSearchUrl(locationUrl);
510 }
511
512 if (enabled == isSearchModeEnabled()) {
513 if (enabled && !m_searchBox->hasFocus()) {
514 m_searchBox->setFocus();
515 m_searchBox->selectAll();
516 }
517 return;
518 }
519
520 if (!enabled) {
521 m_view->setViewPropertiesContext(QString());
522
523 // Restore the URL for the URL navigator. If Dolphin has been
524 // started with a search-URL, the home URL is used as fallback.
525 QUrl url = m_searchBox->searchPath();
526 if (url.isEmpty() || !url.isValid() || isSearchUrl(url)) {
527 url = Dolphin::homeUrl();
528 }
529 m_urlNavigatorConnected->setLocationUrl(url);
530 }
531
532 m_searchModeEnabled = enabled;
533
534 Q_EMIT searchModeEnabledChanged(enabled);
535 }
536
537 bool DolphinViewContainer::isSearchModeEnabled() const
538 {
539 return m_searchModeEnabled;
540 }
541
542 QString DolphinViewContainer::placesText() const
543 {
544 QString text;
545
546 if (isSearchModeEnabled()) {
547 text = i18n("Search for %1 in %2", m_searchBox->text(), m_searchBox->searchPath().fileName());
548 } else {
549 text = url().adjusted(QUrl::StripTrailingSlash).fileName();
550 if (text.isEmpty()) {
551 text = url().host();
552 }
553 if (text.isEmpty()) {
554 text = url().scheme();
555 }
556 }
557
558 return text;
559 }
560
561 void DolphinViewContainer::reload()
562 {
563 view()->reload();
564 m_messageWidget->hide();
565 }
566
567 QString DolphinViewContainer::captionWindowTitle() const
568 {
569 if (GeneralSettings::showFullPathInTitlebar() && !isSearchModeEnabled()) {
570 if (!url().isLocalFile()) {
571 return url().adjusted(QUrl::StripTrailingSlash).toString();
572 }
573 return url().adjusted(QUrl::StripTrailingSlash).path();
574 } else {
575 return DolphinViewContainer::caption();
576 }
577 }
578
579 QString DolphinViewContainer::caption() const
580 {
581 if (isSearchModeEnabled()) {
582 if (currentSearchText().isEmpty()){
583 return i18n("Search");
584 } else {
585 return i18n("Search for %1", currentSearchText());
586 }
587 }
588
589 KFilePlacesModel *placesModel = DolphinPlacesModelSingleton::instance().placesModel();
590 const QString pattern = url().adjusted(QUrl::StripTrailingSlash).toString(QUrl::FullyEncoded).append("/?");
591 const auto& matchedPlaces = placesModel->match(placesModel->index(0,0), KFilePlacesModel::UrlRole, QRegularExpression::anchoredPattern(pattern), 1, Qt::MatchRegularExpression);
592
593 if (!matchedPlaces.isEmpty()) {
594 return placesModel->text(matchedPlaces.first());
595 }
596
597
598 if (!url().isLocalFile()) {
599 QUrl adjustedUrl = url().adjusted(QUrl::StripTrailingSlash);
600 QString caption;
601 if (!adjustedUrl.fileName().isEmpty()) {
602 caption = adjustedUrl.fileName();
603 } else if (!adjustedUrl.path().isEmpty() && adjustedUrl.path() != "/") {
604 caption = adjustedUrl.path();
605 } else if (!adjustedUrl.host().isEmpty()) {
606 caption = adjustedUrl.host();
607 } else {
608 caption = adjustedUrl.toString();
609 }
610 return caption;
611 }
612
613 QString fileName = url().adjusted(QUrl::StripTrailingSlash).fileName();
614 if (fileName.isEmpty()) {
615 fileName = '/';
616 }
617
618 return fileName;
619 }
620
621 void DolphinViewContainer::setUrl(const QUrl& newUrl)
622 {
623 if (newUrl != m_urlNavigator->locationUrl()) {
624 m_urlNavigator->setLocationUrl(newUrl);
625 }
626
627 #if HAVE_KACTIVITIES
628 m_activityResourceInstance->setUri(newUrl);
629 #endif
630 }
631
632 void DolphinViewContainer::setFilterBarVisible(bool visible)
633 {
634 Q_ASSERT(m_filterBar);
635 if (visible) {
636 m_view->hideToolTip(ToolTipManager::HideBehavior::Instantly);
637 m_filterBar->show();
638 m_filterBar->setFocus();
639 m_filterBar->selectAll();
640 } else {
641 closeFilterBar();
642 }
643 }
644
645 void DolphinViewContainer::delayedStatusBarUpdate()
646 {
647 if (m_statusBarTimer->isActive() && (m_statusBarTimestamp.elapsed() > 2000)) {
648 // No update of the statusbar has been done during the last 2 seconds,
649 // although an update has been requested. Trigger an immediate update.
650 m_statusBarTimer->stop();
651 updateStatusBar();
652 } else {
653 // Invoke updateStatusBar() with a small delay. This assures that
654 // when a lot of delayedStatusBarUpdates() are done in a short time,
655 // no bottleneck is given.
656 m_statusBarTimer->start();
657 }
658 }
659
660 void DolphinViewContainer::updateStatusBar()
661 {
662 m_statusBarTimestamp.start();
663 m_view->requestStatusBarText();
664 }
665
666 void DolphinViewContainer::updateDirectoryLoadingProgress(int percent)
667 {
668 if (m_statusBar->progressText().isEmpty()) {
669 m_statusBar->setProgressText(i18nc("@info:progress", "Loading folder..."));
670 }
671 m_statusBar->setProgress(percent);
672 }
673
674 void DolphinViewContainer::updateDirectorySortingProgress(int percent)
675 {
676 if (m_statusBar->progressText().isEmpty()) {
677 m_statusBar->setProgressText(i18nc("@info:progress", "Sorting..."));
678 }
679 m_statusBar->setProgress(percent);
680 }
681
682 void DolphinViewContainer::slotDirectoryLoadingStarted()
683 {
684 if (isSearchUrl(url())) {
685 // Search KIO-slaves usually don't provide any progress information. Give
686 // a hint to the user that a searching is done:
687 updateStatusBar();
688 m_statusBar->setProgressText(i18nc("@info", "Searching..."));
689 m_statusBar->setProgress(-1);
690 } else {
691 // Trigger an undetermined progress indication. The progress
692 // information in percent will be triggered by the percent() signal
693 // of the directory lister later.
694 m_statusBar->setProgressText(QString());
695 updateDirectoryLoadingProgress(-1);
696 }
697 }
698
699 void DolphinViewContainer::slotDirectoryLoadingCompleted()
700 {
701 if (!m_statusBar->progressText().isEmpty()) {
702 m_statusBar->setProgressText(QString());
703 m_statusBar->setProgress(100);
704 }
705
706 if (isSearchUrl(url()) && m_view->itemsCount() == 0) {
707 // The dir lister has been completed on a Baloo-URI and no items have been found. Instead
708 // of showing the default status bar information ("0 items") a more helpful information is given:
709 m_statusBar->setText(i18nc("@info:status", "No items found."));
710 } else {
711 updateStatusBar();
712 }
713 }
714
715 void DolphinViewContainer::slotDirectoryLoadingCanceled()
716 {
717 if (!m_statusBar->progressText().isEmpty()) {
718 m_statusBar->setProgressText(QString());
719 m_statusBar->setProgress(100);
720 }
721
722 m_statusBar->setText(QString());
723 }
724
725 void DolphinViewContainer::slotUrlIsFileError(const QUrl& url)
726 {
727 const KFileItem item(url);
728
729 // Find out if the file can be opened in the view (for example, this is the
730 // case if the file is an archive). The mime type must be known for that.
731 item.determineMimeType();
732 const QUrl& folderUrl = DolphinView::openItemAsFolderUrl(item, true);
733 if (!folderUrl.isEmpty()) {
734 setUrl(folderUrl);
735 } else {
736 slotItemActivated(item);
737 }
738 }
739
740 void DolphinViewContainer::slotItemActivated(const KFileItem &item)
741 {
742 // It is possible to activate items on inactive views by
743 // drag & drop operations. Assure that activating an item always
744 // results in an active view.
745 m_view->setActive(true);
746
747 const QUrl& url = DolphinView::openItemAsFolderUrl(item, GeneralSettings::browseThroughArchives());
748 if (!url.isEmpty()) {
749 const auto modifiers = QGuiApplication::keyboardModifiers();
750 // keep in sync with KUrlNavigator::slotNavigatorButtonClicked
751 if (modifiers & Qt::ControlModifier && modifiers & Qt::ShiftModifier) {
752 Q_EMIT activeTabRequested(url);
753 } else if (modifiers & Qt::ControlModifier) {
754 Q_EMIT tabRequested(url);
755 } else if (modifiers & Qt::ShiftModifier) {
756 Dolphin::openNewWindow({KFilePlacesModel::convertedUrl(url)}, this);
757 } else {
758 setUrl(url);
759 }
760 return;
761 }
762
763 KIO::OpenUrlJob *job = new KIO::OpenUrlJob(item.targetUrl(), item.mimetype());
764 job->setUiDelegate(new KIO::JobUiDelegate(KJobUiDelegate::AutoWarningHandlingEnabled, this));
765 job->setShowOpenOrExecuteDialog(true);
766 connect(job, &KIO::OpenUrlJob::finished, this, &DolphinViewContainer::slotOpenUrlFinished);
767 job->start();
768 }
769
770 void DolphinViewContainer::slotItemsActivated(const KFileItemList& items)
771 {
772 Q_ASSERT(items.count() >= 2);
773
774 KFileItemActions fileItemActions(this);
775 fileItemActions.runPreferredApplications(items);
776 }
777
778 void DolphinViewContainer::showItemInfo(const KFileItem& item)
779 {
780 if (item.isNull()) {
781 m_statusBar->resetToDefaultText();
782 } else {
783 m_statusBar->setText(item.getStatusBarInfo());
784 }
785 }
786
787 void DolphinViewContainer::closeFilterBar()
788 {
789 m_filterBar->closeFilterBar();
790 m_view->setFocus();
791 Q_EMIT showFilterBarChanged(false);
792 }
793
794 void DolphinViewContainer::setNameFilter(const QString& nameFilter)
795 {
796 m_view->hideToolTip(ToolTipManager::HideBehavior::Instantly);
797 m_view->setNameFilter(nameFilter);
798 delayedStatusBarUpdate();
799 }
800
801 void DolphinViewContainer::activate()
802 {
803 setActive(true);
804 }
805
806 void DolphinViewContainer::slotUrlNavigatorLocationAboutToBeChanged(const QUrl&)
807 {
808 saveViewState();
809 }
810
811 void DolphinViewContainer::slotUrlNavigatorLocationChanged(const QUrl& url)
812 {
813 if (m_urlNavigatorConnected) {
814 m_urlNavigatorConnected->slotReturnPressed();
815 }
816
817 if (KProtocolManager::supportsListing(url)) {
818 setSearchModeEnabled(isSearchUrl(url));
819 m_view->setUrl(url);
820 tryRestoreViewState();
821
822 if (m_autoGrabFocus && isActive() && !isSearchUrl(url)) {
823 // When an URL has been entered, the view should get the focus.
824 // The focus must be requested asynchronously, as changing the URL might create
825 // a new view widget.
826 QTimer::singleShot(0, this, &DolphinViewContainer::requestFocus);
827 }
828 } else if (KProtocolManager::isSourceProtocol(url)) {
829 if (url.scheme().startsWith(QLatin1String("http"))) {
830 showMessage(i18nc("@info:status", // krazy:exclude=qmethods
831 "Dolphin does not support web pages, the web browser has been launched"),
832 Information);
833 } else {
834 showMessage(i18nc("@info:status",
835 "Protocol not supported by Dolphin, default application has been launched"),
836 Information);
837 }
838
839 QDesktopServices::openUrl(url);
840 redirect(QUrl(), m_urlNavigator->locationUrl(1));
841 } else {
842 showMessage(i18nc("@info:status", "Invalid protocol"), Error);
843 m_urlNavigator->goBack();
844 }
845 }
846
847 void DolphinViewContainer::slotUrlSelectionRequested(const QUrl& url)
848 {
849 m_view->markUrlsAsSelected({url});
850 m_view->markUrlAsCurrent(url); // makes the item scroll into view
851 }
852
853 void DolphinViewContainer::disableUrlNavigatorSelectionRequests()
854 {
855 disconnect(m_urlNavigator.get(), &KUrlNavigator::urlSelectionRequested,
856 this, &DolphinViewContainer::slotUrlSelectionRequested);
857 }
858
859 void DolphinViewContainer::enableUrlNavigatorSelectionRequests()
860 {
861 connect(m_urlNavigator.get(), &KUrlNavigator::urlSelectionRequested,
862 this, &DolphinViewContainer::slotUrlSelectionRequested);
863 }
864
865 void DolphinViewContainer::redirect(const QUrl& oldUrl, const QUrl& newUrl)
866 {
867 Q_UNUSED(oldUrl)
868 const bool block = m_urlNavigator->signalsBlocked();
869 m_urlNavigator->blockSignals(true);
870
871 // Assure that the location state is reset for redirection URLs. This
872 // allows to skip redirection URLs when going back or forward in the
873 // URL history.
874 m_urlNavigator->saveLocationState(QByteArray());
875 m_urlNavigator->setLocationUrl(newUrl);
876 setSearchModeEnabled(isSearchUrl(newUrl));
877
878 m_urlNavigator->blockSignals(block);
879 }
880
881 void DolphinViewContainer::requestFocus()
882 {
883 m_view->setFocus();
884 }
885
886 void DolphinViewContainer::startSearching()
887 {
888 Q_CHECK_PTR(m_urlNavigatorConnected);
889 const QUrl url = m_searchBox->urlForSearching();
890 if (url.isValid() && !url.isEmpty()) {
891 m_view->setViewPropertiesContext(QStringLiteral("search"));
892 m_urlNavigatorConnected->setLocationUrl(url);
893 }
894 }
895
896 void DolphinViewContainer::closeSearchBox()
897 {
898 setSearchModeEnabled(false);
899 }
900
901 void DolphinViewContainer::stopDirectoryLoading()
902 {
903 m_view->stopLoading();
904 m_statusBar->setProgress(100);
905 }
906
907 void DolphinViewContainer::slotStatusBarZoomLevelChanged(int zoomLevel)
908 {
909 m_view->setZoomLevel(zoomLevel);
910 }
911
912 void DolphinViewContainer::showErrorMessage(const QString& msg)
913 {
914 showMessage(msg, Error);
915 }
916
917 void DolphinViewContainer::slotPlacesModelChanged()
918 {
919 if (!GeneralSettings::showFullPathInTitlebar() && !isSearchModeEnabled()) {
920 Q_EMIT captionChanged();
921 }
922 }
923
924 void DolphinViewContainer::slotHiddenFilesShownChanged(bool showHiddenFiles)
925 {
926 if (m_urlNavigatorConnected) {
927 m_urlNavigatorConnected->setShowHiddenFolders(showHiddenFiles);
928 }
929 }
930
931 void DolphinViewContainer::slotSortHiddenLastChanged(bool hiddenLast)
932 {
933 if (m_urlNavigatorConnected) {
934 m_urlNavigatorConnected->setSortHiddenFoldersLast(hiddenLast);
935 }
936 }
937
938 void DolphinViewContainer::slotOpenUrlFinished(KJob *job)
939 {
940 if (job->error() && job->error() != KIO::ERR_USER_CANCELED) {
941 showErrorMessage(job->errorString());
942 }
943 }
944
945 bool DolphinViewContainer::isSearchUrl(const QUrl& url) const
946 {
947 return url.scheme().contains(QLatin1String("search"));
948 }
949
950 void DolphinViewContainer::saveViewState()
951 {
952 QByteArray locationState;
953 QDataStream stream(&locationState, QIODevice::WriteOnly);
954 m_view->saveState(stream);
955 m_urlNavigator->saveLocationState(locationState);
956 }
957
958 void DolphinViewContainer::tryRestoreViewState()
959 {
960 QByteArray locationState = m_urlNavigator->locationState();
961 if (!locationState.isEmpty()) {
962 QDataStream stream(&locationState, QIODevice::ReadOnly);
963 m_view->restoreState(stream);
964 }
965 }