]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinviewcontainer.cpp
Remove unused includes
[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/JobUiDelegate>
28 #include <KIO/OpenUrlJob>
29 #include <KLocalizedString>
30 #include <KMessageWidget>
31 #include <KProtocolManager>
32 #include <KShell>
33
34 #include <QDesktopServices>
35 #include <QDropEvent>
36 #include <QGridLayout>
37 #include <QGuiApplication>
38 #include <QTimer>
39 #include <QUrl>
40
41 // An overview of the widgets contained by this ViewContainer
42 struct LayoutStructure {
43 int searchBox = 0;
44 int messageWidget = 1;
45 int selectionModeTopBar = 2;
46 int view = 3;
47 int selectionModeBottomBar = 4;
48 int filterBar = 5;
49 int statusBar = 6;
50 };
51 constexpr LayoutStructure positionFor;
52
53 DolphinViewContainer::DolphinViewContainer(const QUrl& url, QWidget* parent) :
54 QWidget(parent),
55 m_topLayout(nullptr),
56 m_urlNavigator{new DolphinUrlNavigator(url)},
57 m_urlNavigatorConnected{nullptr},
58 m_searchBox(nullptr),
59 m_searchModeEnabled(false),
60 m_messageWidget(nullptr),
61 m_selectionModeTopBar{nullptr},
62 m_view(nullptr),
63 m_filterBar(nullptr),
64 m_selectionModeBottomBar{nullptr},
65 m_statusBar(nullptr),
66 m_statusBarTimer(nullptr),
67 m_statusBarTimestamp(),
68 m_autoGrabFocus(true)
69 #if HAVE_KACTIVITIES
70 , m_activityResourceInstance(nullptr)
71 #endif
72 {
73 hide();
74
75 m_topLayout = new QGridLayout(this);
76 m_topLayout->setSpacing(0);
77 m_topLayout->setContentsMargins(0, 0, 0, 0);
78
79 m_searchBox = new DolphinSearchBox(this);
80 m_searchBox->hide();
81 connect(m_searchBox, &DolphinSearchBox::activated, this, &DolphinViewContainer::activate);
82 connect(m_searchBox, &DolphinSearchBox::closeRequest, this, &DolphinViewContainer::closeSearchBox);
83 connect(m_searchBox, &DolphinSearchBox::searchRequest, this, &DolphinViewContainer::startSearching);
84 connect(m_searchBox, &DolphinSearchBox::focusViewRequest, this, &DolphinViewContainer::requestFocus);
85 m_searchBox->setWhatsThis(xi18nc("@info:whatsthis findbar",
86 "<para>This helps you find files and folders. Enter a <emphasis>"
87 "search term</emphasis> and specify search settings with the "
88 "buttons at the bottom:<list><item>Filename/Content: "
89 "Does the item you are looking for contain the search terms "
90 "within its filename or its contents?<nl/>The contents of images, "
91 "audio files and videos will not be searched.</item><item>"
92 "From Here/Everywhere: Do you want to search in this "
93 "folder and its sub-folders or everywhere?</item><item>"
94 "More Options: Click this to search by media type, access "
95 "time or rating.</item><item>More Search Tools: Install other "
96 "means to find an item.</item></list></para>"));
97
98 m_messageWidget = new KMessageWidget(this);
99 m_messageWidget->setCloseButtonVisible(true);
100 m_messageWidget->hide();
101
102 #ifndef Q_OS_WIN
103 if (getuid() == 0) {
104
105 // We must be logged in as the root user; show a big scary warning
106 showMessage(i18n("Running Dolphin as root can be dangerous. Please be careful."), Warning);
107 }
108 #endif
109
110 // Initialize filter bar
111 m_filterBar = new FilterBar(this);
112 m_filterBar->setVisible(GeneralSettings::filterBar());
113
114 connect(m_filterBar, &FilterBar::filterChanged,
115 this, &DolphinViewContainer::setNameFilter);
116 connect(m_filterBar, &FilterBar::closeRequest,
117 this, &DolphinViewContainer::closeFilterBar);
118 connect(m_filterBar, &FilterBar::focusViewRequest,
119 this, &DolphinViewContainer::requestFocus);
120
121 // Initialize the main view
122 m_view = new DolphinView(url, this);
123 connect(m_view, &DolphinView::urlChanged,
124 m_filterBar, &FilterBar::slotUrlChanged);
125 connect(m_view, &DolphinView::urlChanged,
126 m_messageWidget, &KMessageWidget::hide);
127 // m_urlNavigator stays in sync with m_view's location changes and
128 // keeps track of them so going back and forth in the history works.
129 connect(m_view, &DolphinView::urlChanged,
130 m_urlNavigator.get(), &DolphinUrlNavigator::setLocationUrl);
131 connect(m_urlNavigator.get(), &DolphinUrlNavigator::urlChanged,
132 this, &DolphinViewContainer::slotUrlNavigatorLocationChanged);
133 connect(m_urlNavigator.get(), &DolphinUrlNavigator::urlAboutToBeChanged,
134 this, &DolphinViewContainer::slotUrlNavigatorLocationAboutToBeChanged);
135 connect(m_urlNavigator.get(), &DolphinUrlNavigator::urlSelectionRequested,
136 this, &DolphinViewContainer::slotUrlSelectionRequested);
137 connect(m_view, &DolphinView::writeStateChanged,
138 this, &DolphinViewContainer::writeStateChanged);
139 connect(m_view, &DolphinView::requestItemInfo,
140 this, &DolphinViewContainer::showItemInfo);
141 connect(m_view, &DolphinView::itemActivated,
142 this, &DolphinViewContainer::slotItemActivated);
143 connect(m_view, &DolphinView::itemsActivated,
144 this, &DolphinViewContainer::slotItemsActivated);
145 connect(m_view, &DolphinView::redirection,
146 this, &DolphinViewContainer::redirect);
147 connect(m_view, &DolphinView::directoryLoadingStarted,
148 this, &DolphinViewContainer::slotDirectoryLoadingStarted);
149 connect(m_view, &DolphinView::directoryLoadingCompleted,
150 this, &DolphinViewContainer::slotDirectoryLoadingCompleted);
151 connect(m_view, &DolphinView::directoryLoadingCanceled,
152 this, &DolphinViewContainer::slotDirectoryLoadingCanceled);
153 connect(m_view, &DolphinView::itemCountChanged,
154 this, &DolphinViewContainer::delayedStatusBarUpdate);
155 connect(m_view, &DolphinView::directoryLoadingProgress,
156 this, &DolphinViewContainer::updateDirectoryLoadingProgress);
157 connect(m_view, &DolphinView::directorySortingProgress,
158 this, &DolphinViewContainer::updateDirectorySortingProgress);
159 connect(m_view, &DolphinView::selectionChanged,
160 this, &DolphinViewContainer::delayedStatusBarUpdate);
161 connect(m_view, &DolphinView::errorMessage,
162 this, &DolphinViewContainer::showErrorMessage);
163 connect(m_view, &DolphinView::urlIsFileError,
164 this, &DolphinViewContainer::slotUrlIsFileError);
165 connect(m_view, &DolphinView::activated,
166 this, &DolphinViewContainer::activate);
167 connect(m_view, &DolphinView::hiddenFilesShownChanged,
168 this, &DolphinViewContainer::slotHiddenFilesShownChanged);
169 connect(m_view, &DolphinView::sortHiddenLastChanged,
170 this, &DolphinViewContainer::slotSortHiddenLastChanged);
171
172 // Initialize status bar
173 m_statusBar = new DolphinStatusBar(this);
174 m_statusBar->setUrl(m_view->url());
175 m_statusBar->setZoomLevel(m_view->zoomLevel());
176 connect(m_view, &DolphinView::urlChanged,
177 m_statusBar, &DolphinStatusBar::setUrl);
178 connect(m_view, &DolphinView::zoomLevelChanged,
179 m_statusBar, &DolphinStatusBar::setZoomLevel);
180 connect(m_view, &DolphinView::infoMessage,
181 m_statusBar, &DolphinStatusBar::setText);
182 connect(m_view, &DolphinView::operationCompletedMessage,
183 m_statusBar, &DolphinStatusBar::setText);
184 connect(m_view, &DolphinView::statusBarTextChanged,
185 m_statusBar, &DolphinStatusBar::setDefaultText);
186 connect(m_view, &DolphinView::statusBarTextChanged,
187 m_statusBar, &DolphinStatusBar::resetToDefaultText);
188 connect(m_statusBar, &DolphinStatusBar::stopPressed,
189 this, &DolphinViewContainer::stopDirectoryLoading);
190 connect(m_statusBar, &DolphinStatusBar::zoomLevelChanged,
191 this, &DolphinViewContainer::slotStatusBarZoomLevelChanged);
192
193 m_statusBarTimer = new QTimer(this);
194 m_statusBarTimer->setSingleShot(true);
195 m_statusBarTimer->setInterval(300);
196 connect(m_statusBarTimer, &QTimer::timeout, this, &DolphinViewContainer::updateStatusBar);
197
198 KIO::FileUndoManager* undoManager = KIO::FileUndoManager::self();
199 connect(undoManager, &KIO::FileUndoManager::jobRecordingFinished,
200 this, &DolphinViewContainer::delayedStatusBarUpdate);
201
202 m_topLayout->addWidget(m_searchBox, positionFor.searchBox, 0);
203 m_topLayout->addWidget(m_messageWidget, positionFor.messageWidget, 0);
204 m_topLayout->addWidget(m_view, positionFor.view, 0);
205 m_topLayout->addWidget(m_filterBar, positionFor.filterBar, 0);
206 m_topLayout->addWidget(m_statusBar, positionFor.statusBar, 0);
207
208 setSearchModeEnabled(isSearchUrl(url));
209
210 connect(DetailsModeSettings::self(), &KCoreConfigSkeleton::configChanged, this, [=]() {
211 if (view()->viewMode() == DolphinView::Mode::DetailsView) {
212 view()->reload();
213 }
214 });
215
216 KFilePlacesModel *placesModel = DolphinPlacesModelSingleton::instance().placesModel();
217 connect(placesModel, &KFilePlacesModel::dataChanged,
218 this, &DolphinViewContainer::slotPlacesModelChanged);
219 connect(placesModel, &KFilePlacesModel::rowsInserted,
220 this, &DolphinViewContainer::slotPlacesModelChanged);
221 connect(placesModel, &KFilePlacesModel::rowsRemoved,
222 this, &DolphinViewContainer::slotPlacesModelChanged);
223
224 connect(this, &DolphinViewContainer::searchModeEnabledChanged,
225 this, &DolphinViewContainer::captionChanged);
226
227 // Initialize kactivities resource instance
228
229 #if HAVE_KACTIVITIES
230 m_activityResourceInstance = new KActivities::ResourceInstance(window()->winId(), url);
231 m_activityResourceInstance->setParent(this);
232 #endif
233 }
234
235 DolphinViewContainer::~DolphinViewContainer()
236 {
237 }
238
239 QUrl DolphinViewContainer::url() const
240 {
241 return m_view->url();
242 }
243
244 void DolphinViewContainer::setActive(bool active)
245 {
246 m_searchBox->setActive(active);
247 if (m_urlNavigatorConnected) {
248 m_urlNavigatorConnected->setActive(active);
249 }
250 m_view->setActive(active);
251
252 #if HAVE_KACTIVITIES
253 if (active) {
254 m_activityResourceInstance->notifyFocusedIn();
255 } else {
256 m_activityResourceInstance->notifyFocusedOut();
257 }
258 #endif
259 }
260
261 bool DolphinViewContainer::isActive() const
262 {
263 return m_view->isActive();
264 }
265
266 void DolphinViewContainer::setAutoGrabFocus(bool grab)
267 {
268 m_autoGrabFocus = grab;
269 }
270
271 bool DolphinViewContainer::autoGrabFocus() const
272 {
273 return m_autoGrabFocus;
274 }
275
276 QString DolphinViewContainer::currentSearchText() const
277 {
278 return m_searchBox->text();
279 }
280
281 const DolphinStatusBar* DolphinViewContainer::statusBar() const
282 {
283 return m_statusBar;
284 }
285
286 DolphinStatusBar* DolphinViewContainer::statusBar()
287 {
288 return m_statusBar;
289 }
290
291 const DolphinUrlNavigator* DolphinViewContainer::urlNavigator() const
292 {
293 return m_urlNavigatorConnected;
294 }
295
296 DolphinUrlNavigator* DolphinViewContainer::urlNavigator()
297 {
298 return m_urlNavigatorConnected;
299 }
300
301 const DolphinUrlNavigator *DolphinViewContainer::urlNavigatorInternalWithHistory() const
302 {
303 return m_urlNavigator.get();
304 }
305
306 DolphinUrlNavigator *DolphinViewContainer::urlNavigatorInternalWithHistory()
307 {
308 return m_urlNavigator.get();
309 }
310
311 const DolphinView* DolphinViewContainer::view() const
312 {
313 return m_view;
314 }
315
316 DolphinView* DolphinViewContainer::view()
317 {
318 return m_view;
319 }
320
321 void DolphinViewContainer::connectUrlNavigator(DolphinUrlNavigator *urlNavigator)
322 {
323 Q_CHECK_PTR(urlNavigator);
324 Q_ASSERT(!m_urlNavigatorConnected);
325 Q_ASSERT(m_urlNavigator.get() != urlNavigator);
326 Q_CHECK_PTR(m_view);
327
328 urlNavigator->setLocationUrl(m_view->url());
329 urlNavigator->setShowHiddenFolders(m_view->hiddenFilesShown());
330 urlNavigator->setSortHiddenFoldersLast(m_view->sortHiddenLast());
331 if (m_urlNavigatorVisualState) {
332 urlNavigator->setVisualState(*m_urlNavigatorVisualState.get());
333 m_urlNavigatorVisualState.reset();
334 }
335 urlNavigator->setActive(isActive());
336
337 // Url changes are still done via m_urlNavigator.
338 connect(urlNavigator, &DolphinUrlNavigator::urlChanged,
339 m_urlNavigator.get(), &DolphinUrlNavigator::setLocationUrl);
340 connect(urlNavigator, &DolphinUrlNavigator::urlsDropped,
341 this, [=](const QUrl &destination, QDropEvent *event) {
342 m_view->dropUrls(destination, event, urlNavigator->dropWidget());
343 });
344 // Aside from these, only visual things need to be connected.
345 connect(m_view, &DolphinView::urlChanged,
346 urlNavigator, &DolphinUrlNavigator::setLocationUrl);
347 connect(urlNavigator, &DolphinUrlNavigator::activated,
348 this, &DolphinViewContainer::activate);
349
350 m_urlNavigatorConnected = urlNavigator;
351 }
352
353 void DolphinViewContainer::disconnectUrlNavigator()
354 {
355 if (!m_urlNavigatorConnected) {
356 return;
357 }
358
359 disconnect(m_urlNavigatorConnected, &DolphinUrlNavigator::urlChanged,
360 m_urlNavigator.get(), &DolphinUrlNavigator::setLocationUrl);
361 disconnect(m_urlNavigatorConnected, &DolphinUrlNavigator::urlsDropped,
362 this, nullptr);
363 disconnect(m_view, &DolphinView::urlChanged,
364 m_urlNavigatorConnected, &DolphinUrlNavigator::setLocationUrl);
365 disconnect(m_urlNavigatorConnected, &DolphinUrlNavigator::activated,
366 this, &DolphinViewContainer::activate);
367
368 m_urlNavigatorVisualState = m_urlNavigatorConnected->visualState();
369 m_urlNavigatorConnected = nullptr;
370 }
371
372 void DolphinViewContainer::setSelectionModeEnabled(bool enabled, KActionCollection *actionCollection, SelectionMode::BottomBar::Contents bottomBarContents)
373 {
374 const bool wasEnabled = m_view->selectionMode();
375 m_view->setSelectionModeEnabled(enabled);
376
377 if (!enabled) {
378 if (!wasEnabled) {
379 return; // nothing to do here
380 }
381 Q_CHECK_PTR(m_selectionModeTopBar); // there is no point in disabling selectionMode when it wasn't even enabled once.
382 Q_CHECK_PTR(m_selectionModeBottomBar);
383 m_selectionModeTopBar->setVisible(false, WithAnimation);
384 m_selectionModeBottomBar->setVisible(false, WithAnimation);
385 Q_EMIT selectionModeChanged(false);
386 return;
387 }
388
389 if (!m_selectionModeTopBar) {
390 // Changing the location will disable selection mode.
391 connect(m_urlNavigator.get(), &DolphinUrlNavigator::urlChanged, this, [this]() {
392 setSelectionModeEnabled(false);
393 });
394
395 m_selectionModeTopBar = new SelectionMode::TopBar(this); // will be created hidden
396 connect(m_selectionModeTopBar, &SelectionMode::TopBar::selectionModeLeavingRequested, this, [this]() {
397 setSelectionModeEnabled(false);
398 });
399 m_topLayout->addWidget(m_selectionModeTopBar, positionFor.selectionModeTopBar, 0);
400 }
401
402 if (!m_selectionModeBottomBar) {
403 m_selectionModeBottomBar = new SelectionMode::BottomBar(actionCollection, this);
404 connect(m_view, &DolphinView::selectionChanged, this, [this](const KFileItemList &selection) {
405 m_selectionModeBottomBar->slotSelectionChanged(selection, m_view->url());
406 });
407 connect(m_selectionModeBottomBar, &SelectionMode::BottomBar::error, this, [this](const QString &errorMessage) {
408 showErrorMessage(errorMessage);
409 });
410 connect(m_selectionModeBottomBar, &SelectionMode::BottomBar::selectionModeLeavingRequested, this, [this]() {
411 setSelectionModeEnabled(false);
412 });
413 m_topLayout->addWidget(m_selectionModeBottomBar, positionFor.selectionModeBottomBar, 0);
414 }
415 m_selectionModeBottomBar->resetContents(bottomBarContents);
416 if (bottomBarContents == SelectionMode::BottomBar::GeneralContents) {
417 m_selectionModeBottomBar->slotSelectionChanged(m_view->selectedItems(), m_view->url());
418 }
419
420 if (!wasEnabled) {
421 m_selectionModeTopBar->setVisible(true, WithAnimation);
422 m_selectionModeBottomBar->setVisible(true, WithAnimation);
423 Q_EMIT selectionModeChanged(true);
424 }
425 }
426
427 bool DolphinViewContainer::isSelectionModeEnabled() const
428 {
429 const bool isEnabled = m_view->selectionMode();
430 Q_ASSERT((!isEnabled
431 // We can't assert that the bars are invisible only because the selection mode is disabled because the hide animation might still be playing.
432 && (!m_selectionModeBottomBar || !m_selectionModeBottomBar->isEnabled() ||
433 !m_selectionModeBottomBar->isVisible() || m_selectionModeBottomBar->contents() == SelectionMode::BottomBar::PasteContents))
434 || ( isEnabled
435 && m_selectionModeTopBar && m_selectionModeTopBar->isVisible()
436 // 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.
437 && m_selectionModeBottomBar
438 && (m_selectionModeBottomBar->isVisible() || m_selectionModeBottomBar->contents() == SelectionMode::BottomBar::GeneralContents)));
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 }