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