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