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