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