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