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