]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinviewcontainer.cpp
Notify users if authorization is required to proceed
[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
45 // An overview of the widgets contained by this ViewContainer
46 struct LayoutStructure {
47 int searchBox = 0;
48 int adminBar = 1;
49 int messageWidget = 2;
50 int selectionModeTopBar = 3;
51 int view = 4;
52 int selectionModeBottomBar = 5;
53 int filterBar = 6;
54 int statusBar = 7;
55 };
56 constexpr LayoutStructure positionFor;
57
58 DolphinViewContainer::DolphinViewContainer(const QUrl &url, QWidget *parent)
59 : QWidget(parent)
60 , m_topLayout(nullptr)
61 , m_urlNavigator{new DolphinUrlNavigator(url)}
62 , m_urlNavigatorConnected{nullptr}
63 , m_searchBox(nullptr)
64 , m_searchModeEnabled(false)
65 , m_adminBar{nullptr}
66 , m_authorizeToEnterFolderAction{nullptr}
67 , m_messageWidget(nullptr)
68 , m_selectionModeTopBar{nullptr}
69 , m_view(nullptr)
70 , m_filterBar(nullptr)
71 , m_selectionModeBottomBar{nullptr}
72 , m_statusBar(nullptr)
73 , m_statusBarTimer(nullptr)
74 , m_statusBarTimestamp()
75 , m_autoGrabFocus(true)
76 {
77 hide();
78
79 m_topLayout = new QGridLayout(this);
80 m_topLayout->setSpacing(0);
81 m_topLayout->setContentsMargins(0, 0, 0, 0);
82
83 m_searchBox = new DolphinSearchBox(this);
84 m_searchBox->setVisible(false, WithoutAnimation);
85 connect(m_searchBox, &DolphinSearchBox::activated, this, &DolphinViewContainer::activate);
86 connect(m_searchBox, &DolphinSearchBox::openRequest, this, &DolphinViewContainer::openSearchBox);
87 connect(m_searchBox, &DolphinSearchBox::closeRequest, this, &DolphinViewContainer::closeSearchBox);
88 connect(m_searchBox, &DolphinSearchBox::searchRequest, this, &DolphinViewContainer::startSearching);
89 connect(m_searchBox, &DolphinSearchBox::focusViewRequest, this, &DolphinViewContainer::requestFocus);
90 m_searchBox->setWhatsThis(xi18nc("@info:whatsthis findbar",
91 "<para>This helps you find files and folders. Enter a <emphasis>"
92 "search term</emphasis> and specify search settings with the "
93 "buttons at the bottom:<list><item>Filename/Content: "
94 "Does the item you are looking for contain the search terms "
95 "within its filename or its contents?<nl/>The contents of images, "
96 "audio files and videos will not be searched.</item><item>"
97 "From Here/Everywhere: Do you want to search in this "
98 "folder and its sub-folders or everywhere?</item><item>"
99 "More Options: Click this to search by media type, access "
100 "time or rating.</item><item>More Search Tools: Install other "
101 "means to find an item.</item></list></para>"));
102
103 m_messageWidget = new KMessageWidget(this);
104 m_messageWidget->setCloseButtonVisible(true);
105 m_messageWidget->setPosition(KMessageWidget::Header);
106 m_messageWidget->hide();
107
108 #if !defined(Q_OS_WIN) && !defined(Q_OS_HAIKU)
109 if (getuid() == 0) {
110 // We must be logged in as the root user; show a big scary warning
111 showMessage(i18n("Running Dolphin as root can be dangerous. Please be careful."), KMessageWidget::Warning);
112 }
113 #endif
114
115 // Initialize filter bar
116 m_filterBar = new FilterBar(this);
117 m_filterBar->setVisible(GeneralSettings::filterBar(), WithoutAnimation);
118
119 connect(m_filterBar, &FilterBar::filterChanged, this, &DolphinViewContainer::setNameFilter);
120 connect(m_filterBar, &FilterBar::closeRequest, this, &DolphinViewContainer::closeFilterBar);
121 connect(m_filterBar, &FilterBar::focusViewRequest, this, &DolphinViewContainer::requestFocus);
122
123 // Initialize the main view
124 m_view = new DolphinView(url, this);
125 connect(m_view, &DolphinView::urlChanged, m_filterBar, &FilterBar::clearIfUnlocked);
126 connect(m_view, &DolphinView::urlChanged, m_messageWidget, &KMessageWidget::hide);
127 // m_urlNavigator stays in sync with m_view's location changes and
128 // keeps track of them so going back and forth in the history works.
129 connect(m_view, &DolphinView::urlChanged, m_urlNavigator.get(), &DolphinUrlNavigator::setLocationUrl);
130 connect(m_urlNavigator.get(), &DolphinUrlNavigator::urlChanged, this, &DolphinViewContainer::slotUrlNavigatorLocationChanged);
131 connect(m_urlNavigator.get(), &DolphinUrlNavigator::urlAboutToBeChanged, this, &DolphinViewContainer::slotUrlNavigatorLocationAboutToBeChanged);
132 connect(m_urlNavigator.get(), &DolphinUrlNavigator::urlSelectionRequested, this, &DolphinViewContainer::slotUrlSelectionRequested);
133 connect(m_view, &DolphinView::writeStateChanged, this, &DolphinViewContainer::writeStateChanged);
134 connect(m_view, &DolphinView::requestItemInfo, this, &DolphinViewContainer::showItemInfo);
135 connect(m_view, &DolphinView::itemActivated, this, &DolphinViewContainer::slotItemActivated);
136 connect(m_view, &DolphinView::fileMiddleClickActivated, this, &DolphinViewContainer::slotfileMiddleClickActivated);
137 connect(m_view, &DolphinView::itemsActivated, this, &DolphinViewContainer::slotItemsActivated);
138 connect(m_view, &DolphinView::redirection, this, &DolphinViewContainer::redirect);
139 connect(m_view, &DolphinView::directoryLoadingStarted, this, &DolphinViewContainer::slotDirectoryLoadingStarted);
140 connect(m_view, &DolphinView::directoryLoadingCompleted, this, &DolphinViewContainer::slotDirectoryLoadingCompleted);
141 connect(m_view, &DolphinView::directoryLoadingCanceled, this, &DolphinViewContainer::slotDirectoryLoadingCanceled);
142 connect(m_view, &DolphinView::itemCountChanged, this, &DolphinViewContainer::delayedStatusBarUpdate);
143 connect(m_view, &DolphinView::selectionChanged, this, &DolphinViewContainer::delayedStatusBarUpdate);
144 connect(m_view, &DolphinView::errorMessage, this, &DolphinViewContainer::slotErrorMessageFromView);
145 connect(m_view, &DolphinView::urlIsFileError, this, &DolphinViewContainer::slotUrlIsFileError);
146 connect(m_view, &DolphinView::activated, this, &DolphinViewContainer::activate);
147 connect(m_view, &DolphinView::hiddenFilesShownChanged, this, &DolphinViewContainer::slotHiddenFilesShownChanged);
148 connect(m_view, &DolphinView::sortHiddenLastChanged, this, &DolphinViewContainer::slotSortHiddenLastChanged);
149 connect(m_view, &DolphinView::currentDirectoryRemoved, this, &DolphinViewContainer::slotCurrentDirectoryRemoved);
150
151 // Initialize status bar
152 m_statusBar = new DolphinStatusBar(this);
153 m_statusBar->setUrl(m_view->url());
154 m_statusBar->setZoomLevel(m_view->zoomLevel());
155 connect(m_view, &DolphinView::urlChanged, m_statusBar, &DolphinStatusBar::setUrl);
156 connect(m_view, &DolphinView::zoomLevelChanged, m_statusBar, &DolphinStatusBar::setZoomLevel);
157 connect(m_view, &DolphinView::infoMessage, m_statusBar, &DolphinStatusBar::setText);
158 connect(m_view, &DolphinView::operationCompletedMessage, m_statusBar, &DolphinStatusBar::setText);
159 connect(m_view, &DolphinView::statusBarTextChanged, m_statusBar, &DolphinStatusBar::setDefaultText);
160 connect(m_view, &DolphinView::statusBarTextChanged, m_statusBar, &DolphinStatusBar::resetToDefaultText);
161 connect(m_view, &DolphinView::directoryLoadingProgress, m_statusBar, [this](int percent) {
162 m_statusBar->showProgress(i18nc("@info:progress", "Loading folder…"), percent);
163 });
164 connect(m_view, &DolphinView::directorySortingProgress, m_statusBar, [this](int percent) {
165 m_statusBar->showProgress(i18nc("@info:progress", "Sorting…"), percent);
166 });
167 connect(m_statusBar, &DolphinStatusBar::stopPressed, this, &DolphinViewContainer::stopDirectoryLoading);
168 connect(m_statusBar, &DolphinStatusBar::zoomLevelChanged, this, &DolphinViewContainer::slotStatusBarZoomLevelChanged);
169 connect(m_statusBar, &DolphinStatusBar::showMessage, this, [this](const QString &message, KMessageWidget::MessageType messageType) {
170 showMessage(message, messageType);
171 });
172
173 m_statusBarTimer = new QTimer(this);
174 m_statusBarTimer->setSingleShot(true);
175 m_statusBarTimer->setInterval(300);
176 connect(m_statusBarTimer, &QTimer::timeout, this, &DolphinViewContainer::updateStatusBar);
177
178 KIO::FileUndoManager *undoManager = KIO::FileUndoManager::self();
179 connect(undoManager, &KIO::FileUndoManager::jobRecordingFinished, this, &DolphinViewContainer::delayedStatusBarUpdate);
180
181 m_topLayout->addWidget(m_searchBox, positionFor.searchBox, 0);
182 m_topLayout->addWidget(m_messageWidget, positionFor.messageWidget, 0);
183 m_topLayout->addWidget(m_view, positionFor.view, 0);
184 m_topLayout->addWidget(m_filterBar, positionFor.filterBar, 0);
185 m_topLayout->addWidget(m_statusBar, positionFor.statusBar, 0);
186
187 setSearchModeEnabled(isSearchUrl(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, &DolphinViewContainer::showErrorMessage);
370 connect(m_selectionModeBottomBar, &SelectionMode::BottomBar::selectionModeLeavingRequested, this, [this]() {
371 setSelectionModeEnabled(false);
372 });
373 m_topLayout->addWidget(m_selectionModeBottomBar, positionFor.selectionModeBottomBar, 0);
374 }
375 m_selectionModeBottomBar->resetContents(bottomBarContents);
376 if (bottomBarContents == SelectionMode::BottomBar::GeneralContents) {
377 m_selectionModeBottomBar->slotSelectionChanged(m_view->selectedItems(), m_view->url());
378 }
379
380 if (!wasEnabled) {
381 m_selectionModeTopBar->setVisible(true, WithAnimation);
382 m_selectionModeBottomBar->setVisible(true, WithAnimation);
383 Q_EMIT selectionModeChanged(true);
384 }
385 }
386
387 bool DolphinViewContainer::isSelectionModeEnabled() const
388 {
389 const bool isEnabled = m_view->selectionMode();
390 Q_ASSERT((!isEnabled
391 // We can't assert that the bars are invisible only because the selection mode is disabled because the hide animation might still be playing.
392 && (!m_selectionModeBottomBar || !m_selectionModeBottomBar->isEnabled() || !m_selectionModeBottomBar->isVisible()
393 || m_selectionModeBottomBar->contents() == SelectionMode::BottomBar::PasteContents))
394 || (isEnabled && m_selectionModeTopBar
395 && m_selectionModeTopBar->isVisible()
396 // 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.
397 && m_selectionModeBottomBar
398 && (m_selectionModeBottomBar->isVisible() || m_selectionModeBottomBar->contents() == SelectionMode::BottomBar::GeneralContents)));
399 return isEnabled;
400 }
401
402 void DolphinViewContainer::slotSplitTabDisabled()
403 {
404 if (m_selectionModeBottomBar) {
405 m_selectionModeBottomBar->slotSplitTabDisabled();
406 }
407 }
408
409 void DolphinViewContainer::showMessage(const QString &message, KMessageWidget::MessageType messageType, std::initializer_list<QAction *> buttonActions)
410 {
411 if (message.isEmpty()) {
412 return;
413 }
414
415 m_messageWidget->setText(message);
416
417 // TODO: wrap at arbitrary character positions once QLabel can do this
418 // https://bugreports.qt.io/browse/QTBUG-1276
419 m_messageWidget->setWordWrap(true);
420 m_messageWidget->setMessageType(messageType);
421
422 const QList<QAction *> previousMessageWidgetActions = m_messageWidget->actions();
423 for (auto action : previousMessageWidgetActions) {
424 m_messageWidget->removeAction(action);
425 }
426 for (QAction *action : buttonActions) {
427 m_messageWidget->addAction(action);
428 }
429
430 m_messageWidget->setWordWrap(false);
431 const int unwrappedWidth = m_messageWidget->sizeHint().width();
432 m_messageWidget->setWordWrap(unwrappedWidth > size().width());
433
434 if (m_messageWidget->isVisible()) {
435 m_messageWidget->hide();
436 }
437 m_messageWidget->animatedShow();
438 }
439
440 void DolphinViewContainer::readSettings()
441 {
442 // The startup settings should (only) get applied if they have been
443 // modified by the user. Otherwise keep the (possibly) different current
444 // setting of the filterbar.
445 if (GeneralSettings::modifiedStartupSettings()) {
446 setFilterBarVisible(GeneralSettings::filterBar());
447 }
448
449 m_view->readSettings();
450 m_statusBar->readSettings();
451 }
452
453 bool DolphinViewContainer::isFilterBarVisible() const
454 {
455 return m_filterBar->isEnabled(); // Gets disabled in AnimatedHeightWidget while animating towards a hidden state.
456 }
457
458 void DolphinViewContainer::setSearchModeEnabled(bool enabled)
459 {
460 m_searchBox->setVisible(enabled, WithAnimation);
461
462 if (enabled) {
463 const QUrl &locationUrl = m_urlNavigator->locationUrl();
464 m_searchBox->fromSearchUrl(locationUrl);
465 }
466
467 if (enabled == isSearchModeEnabled()) {
468 if (enabled && !m_searchBox->hasFocus()) {
469 m_searchBox->setFocus();
470 m_searchBox->selectAll();
471 }
472 return;
473 }
474
475 if (!enabled) {
476 m_view->setViewPropertiesContext(QString());
477
478 // Restore the URL for the URL navigator. If Dolphin has been
479 // started with a search-URL, the home URL is used as fallback.
480 QUrl url = m_searchBox->searchPath();
481 if (url.isEmpty() || !url.isValid() || isSearchUrl(url)) {
482 url = Dolphin::homeUrl();
483 }
484 m_urlNavigatorConnected->setLocationUrl(url);
485 }
486
487 m_searchModeEnabled = enabled;
488
489 Q_EMIT searchModeEnabledChanged(enabled);
490 }
491
492 bool DolphinViewContainer::isSearchModeEnabled() const
493 {
494 return m_searchModeEnabled;
495 }
496
497 QString DolphinViewContainer::placesText() const
498 {
499 QString text;
500
501 if (isSearchModeEnabled()) {
502 text = i18n("Search for %1 in %2", m_searchBox->text(), m_searchBox->searchPath().fileName());
503 } else {
504 text = url().adjusted(QUrl::StripTrailingSlash).fileName();
505 if (text.isEmpty()) {
506 text = url().host();
507 }
508 if (text.isEmpty()) {
509 text = url().scheme();
510 }
511 }
512
513 return text;
514 }
515
516 void DolphinViewContainer::reload()
517 {
518 view()->reload();
519 m_messageWidget->hide();
520 }
521
522 QString DolphinViewContainer::captionWindowTitle() const
523 {
524 if (GeneralSettings::showFullPathInTitlebar() && !isSearchModeEnabled()) {
525 if (!url().isLocalFile()) {
526 return url().adjusted(QUrl::StripTrailingSlash).toString();
527 }
528 return url().adjusted(QUrl::StripTrailingSlash).path();
529 } else {
530 return DolphinViewContainer::caption();
531 }
532 }
533
534 QString DolphinViewContainer::caption() const
535 {
536 if (isSearchModeEnabled()) {
537 if (currentSearchText().isEmpty()) {
538 return i18n("Search");
539 } else {
540 return i18n("Search for %1", currentSearchText());
541 }
542 }
543
544 KFilePlacesModel *placesModel = DolphinPlacesModelSingleton::instance().placesModel();
545 const QString pattern = url().adjusted(QUrl::StripTrailingSlash).toString(QUrl::FullyEncoded).append("/?");
546 const auto &matchedPlaces =
547 placesModel->match(placesModel->index(0, 0), KFilePlacesModel::UrlRole, QRegularExpression::anchoredPattern(pattern), 1, Qt::MatchRegularExpression);
548
549 if (!matchedPlaces.isEmpty()) {
550 return placesModel->text(matchedPlaces.first());
551 }
552
553 if (!url().isLocalFile()) {
554 QUrl adjustedUrl = url().adjusted(QUrl::StripTrailingSlash);
555 QString caption;
556 if (!adjustedUrl.fileName().isEmpty()) {
557 caption = adjustedUrl.fileName();
558 } else if (!adjustedUrl.path().isEmpty() && adjustedUrl.path() != "/") {
559 caption = adjustedUrl.path();
560 } else if (!adjustedUrl.host().isEmpty()) {
561 caption = adjustedUrl.host();
562 } else {
563 caption = adjustedUrl.toString();
564 }
565 return caption;
566 }
567
568 QString fileName = url().adjusted(QUrl::StripTrailingSlash).fileName();
569 if (fileName.isEmpty()) {
570 fileName = '/';
571 }
572
573 return fileName;
574 }
575
576 void DolphinViewContainer::setUrl(const QUrl &newUrl)
577 {
578 if (newUrl != m_urlNavigator->locationUrl()) {
579 m_urlNavigator->setLocationUrl(newUrl);
580 }
581 }
582
583 void DolphinViewContainer::setFilterBarVisible(bool visible)
584 {
585 Q_ASSERT(m_filterBar);
586 if (visible) {
587 m_view->hideToolTip(ToolTipManager::HideBehavior::Instantly);
588 m_filterBar->setVisible(true, WithAnimation);
589 m_filterBar->setFocus();
590 m_filterBar->selectAll();
591 } else {
592 closeFilterBar();
593 }
594 }
595
596 void DolphinViewContainer::delayedStatusBarUpdate()
597 {
598 if (m_statusBarTimer->isActive() && (m_statusBarTimestamp.elapsed() > 2000)) {
599 // No update of the statusbar has been done during the last 2 seconds,
600 // although an update has been requested. Trigger an immediate update.
601 m_statusBarTimer->stop();
602 updateStatusBar();
603 } else {
604 // Invoke updateStatusBar() with a small delay. This assures that
605 // when a lot of delayedStatusBarUpdates() are done in a short time,
606 // no bottleneck is given.
607 m_statusBarTimer->start();
608 }
609 }
610
611 void DolphinViewContainer::updateStatusBar()
612 {
613 m_statusBarTimestamp.start();
614 m_view->requestStatusBarText();
615 }
616
617 void DolphinViewContainer::slotDirectoryLoadingStarted()
618 {
619 if (isSearchUrl(url())) {
620 // Search KIO-slaves usually don't provide any progress information. Give
621 // a hint to the user that a searching is done:
622 updateStatusBar();
623 m_statusBar->showProgress(i18nc("@info", "Searching…"), -1);
624 } else {
625 // Trigger an undetermined progress indication. The progress
626 // information in percent will be triggered by the percent() signal
627 // of the directory lister later.
628 m_statusBar->showProgress(QString(), -1);
629 }
630
631 if (m_urlNavigatorConnected) {
632 m_urlNavigatorConnected->setReadOnlyBadgeVisible(false);
633 }
634 }
635
636 void DolphinViewContainer::slotDirectoryLoadingCompleted()
637 {
638 m_statusBar->showProgress(QString(), 100);
639
640 if (isSearchUrl(url()) && m_view->itemsCount() == 0) {
641 // The dir lister has been completed on a Baloo-URI and no items have been found. Instead
642 // of showing the default status bar information ("0 items") a more helpful information is given:
643 m_statusBar->setText(i18nc("@info:status", "No items found."));
644 } else {
645 updateStatusBar();
646 }
647
648 if (m_urlNavigatorConnected) {
649 m_urlNavigatorConnected->setReadOnlyBadgeVisible(rootItem().isLocalFile() && !rootItem().isWritable());
650 }
651
652 // Update admin bar visibility
653 if (m_view->url().scheme() == QStringLiteral("admin")) {
654 if (!m_adminBar) {
655 m_adminBar = new Admin::Bar(this);
656 m_topLayout->addWidget(m_adminBar, positionFor.adminBar, 0);
657 }
658 m_adminBar->setVisible(true, WithAnimation);
659 } else if (m_adminBar) {
660 m_adminBar->setVisible(false, WithAnimation);
661 }
662 }
663
664 void DolphinViewContainer::slotDirectoryLoadingCanceled()
665 {
666 m_statusBar->showProgress(QString(), 100);
667 m_statusBar->setText(QString());
668 }
669
670 void DolphinViewContainer::slotUrlIsFileError(const QUrl &url)
671 {
672 const KFileItem item(url);
673
674 // Find out if the file can be opened in the view (for example, this is the
675 // case if the file is an archive). The mime type must be known for that.
676 item.determineMimeType();
677 const QUrl &folderUrl = DolphinView::openItemAsFolderUrl(item, true);
678 if (!folderUrl.isEmpty()) {
679 setUrl(folderUrl);
680 } else {
681 slotItemActivated(item);
682 }
683 }
684
685 void DolphinViewContainer::slotItemActivated(const KFileItem &item)
686 {
687 // It is possible to activate items on inactive views by
688 // drag & drop operations. Assure that activating an item always
689 // results in an active view.
690 m_view->setActive(true);
691
692 const QUrl &url = DolphinView::openItemAsFolderUrl(item, GeneralSettings::browseThroughArchives());
693 if (!url.isEmpty()) {
694 const auto modifiers = QGuiApplication::keyboardModifiers();
695 // keep in sync with KUrlNavigator::slotNavigatorButtonClicked
696 if (modifiers & Qt::ControlModifier && modifiers & Qt::ShiftModifier) {
697 Q_EMIT activeTabRequested(url);
698 } else if (modifiers & Qt::ControlModifier) {
699 Q_EMIT tabRequested(url);
700 } else if (modifiers & Qt::ShiftModifier) {
701 Dolphin::openNewWindow({KFilePlacesModel::convertedUrl(url)}, this);
702 } else {
703 setUrl(url);
704 }
705 return;
706 }
707
708 KIO::OpenUrlJob *job = new KIO::OpenUrlJob(item.targetUrl(), item.mimetype());
709 // Auto*Warning*Handling, errors are put in a KMessageWidget by us in slotOpenUrlFinished.
710 job->setUiDelegate(KIO::createDefaultJobUiDelegate(KJobUiDelegate::AutoWarningHandlingEnabled, this));
711 job->setShowOpenOrExecuteDialog(true);
712 connect(job, &KIO::OpenUrlJob::finished, this, &DolphinViewContainer::slotOpenUrlFinished);
713 job->start();
714 }
715
716 void DolphinViewContainer::slotfileMiddleClickActivated(const KFileItem &item)
717 {
718 KService::List services = KApplicationTrader::queryByMimeType(item.mimetype());
719
720 int indexOfAppToOpenFileWith = 1;
721
722 // executable scripts
723 auto mimeType = item.currentMimeType();
724 if (item.isLocalFile() && mimeType.inherits(QStringLiteral("application/x-executable")) && mimeType.inherits(QStringLiteral("text/plain"))
725 && QFileInfo(item.localPath()).isExecutable()) {
726 KConfigGroup cfgGroup(KSharedConfig::openConfig(QStringLiteral("kiorc")), QStringLiteral("Executable scripts"));
727 const QString value = cfgGroup.readEntry("behaviourOnLaunch", "alwaysAsk");
728
729 // in case KIO::WidgetsOpenOrExecuteFileHandler::promptUserOpenOrExecute would not open the file
730 if (value != QLatin1String("open")) {
731 indexOfAppToOpenFileWith = 0;
732 }
733 }
734
735 if (services.length() >= indexOfAppToOpenFileWith + 1) {
736 auto service = services.at(indexOfAppToOpenFileWith);
737
738 KIO::ApplicationLauncherJob *job = new KIO::ApplicationLauncherJob(service, this);
739 job->setUrls({item.url()});
740
741 job->setUiDelegate(KIO::createDefaultJobUiDelegate(KJobUiDelegate::AutoHandlingEnabled, this));
742 connect(job, &KIO::OpenUrlJob::finished, this, &DolphinViewContainer::slotOpenUrlFinished);
743 job->start();
744 }
745 }
746
747 void DolphinViewContainer::slotItemsActivated(const KFileItemList &items)
748 {
749 Q_ASSERT(items.count() >= 2);
750
751 KFileItemActions fileItemActions(this);
752 fileItemActions.runPreferredApplications(items);
753 }
754
755 void DolphinViewContainer::showItemInfo(const KFileItem &item)
756 {
757 if (item.isNull()) {
758 m_statusBar->resetToDefaultText();
759 } else {
760 m_statusBar->setText(item.getStatusBarInfo());
761 }
762 }
763
764 void DolphinViewContainer::closeFilterBar()
765 {
766 m_filterBar->closeFilterBar();
767 m_view->setFocus();
768 Q_EMIT showFilterBarChanged(false);
769 }
770
771 void DolphinViewContainer::clearFilterBar()
772 {
773 m_filterBar->clearIfUnlocked();
774 }
775
776 void DolphinViewContainer::setNameFilter(const QString &nameFilter)
777 {
778 m_view->hideToolTip(ToolTipManager::HideBehavior::Instantly);
779 m_view->setNameFilter(nameFilter);
780 delayedStatusBarUpdate();
781 }
782
783 void DolphinViewContainer::activate()
784 {
785 setActive(true);
786 }
787
788 void DolphinViewContainer::slotUrlNavigatorLocationAboutToBeChanged(const QUrl &)
789 {
790 saveViewState();
791 }
792
793 void DolphinViewContainer::slotUrlNavigatorLocationChanged(const QUrl &url)
794 {
795 if (m_urlNavigatorConnected) {
796 m_urlNavigatorConnected->slotReturnPressed();
797 }
798
799 if (KProtocolManager::supportsListing(url)) {
800 const bool searchBoxInitialized = isSearchModeEnabled() && m_searchBox->text().isEmpty();
801 setSearchModeEnabled(isSearchUrl(url) || searchBoxInitialized);
802
803 m_view->setUrl(url);
804 tryRestoreViewState();
805
806 if (m_autoGrabFocus && isActive() && !isSearchModeEnabled()) {
807 // When an URL has been entered, the view should get the focus.
808 // The focus must be requested asynchronously, as changing the URL might create
809 // a new view widget.
810 QTimer::singleShot(0, this, &DolphinViewContainer::requestFocus);
811 }
812 } else if (KProtocolManager::isSourceProtocol(url)) {
813 if (url.scheme().startsWith(QLatin1String("http"))) {
814 showMessage(i18nc("@info:status", // krazy:exclude=qmethods
815 "Dolphin does not support web pages, the web browser has been launched"),
816 KMessageWidget::Information);
817 } else {
818 showMessage(i18nc("@info:status", "Protocol not supported by Dolphin, default application has been launched"), KMessageWidget::Information);
819 }
820
821 QDesktopServices::openUrl(url);
822 redirect(QUrl(), m_urlNavigator->locationUrl(1));
823 } else {
824 if (!url.scheme().isEmpty()) {
825 showMessage(i18nc("@info:status", "Invalid protocol '%1'", url.scheme()), KMessageWidget::Error);
826 } else {
827 showMessage(i18nc("@info:status", "Invalid protocol"), KMessageWidget::Error);
828 }
829 m_urlNavigator->goBack();
830 }
831 }
832
833 void DolphinViewContainer::slotUrlSelectionRequested(const QUrl &url)
834 {
835 // 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
836
837 m_view->markUrlAsCurrent(url); // makes the item scroll into view
838 }
839
840 void DolphinViewContainer::disableUrlNavigatorSelectionRequests()
841 {
842 disconnect(m_urlNavigator.get(), &KUrlNavigator::urlSelectionRequested, this, &DolphinViewContainer::slotUrlSelectionRequested);
843 }
844
845 void DolphinViewContainer::enableUrlNavigatorSelectionRequests()
846 {
847 connect(m_urlNavigator.get(), &KUrlNavigator::urlSelectionRequested, this, &DolphinViewContainer::slotUrlSelectionRequested);
848 }
849
850 void DolphinViewContainer::redirect(const QUrl &oldUrl, const QUrl &newUrl)
851 {
852 Q_UNUSED(oldUrl)
853 const bool block = m_urlNavigator->signalsBlocked();
854 m_urlNavigator->blockSignals(true);
855
856 // Assure that the location state is reset for redirection URLs. This
857 // allows to skip redirection URLs when going back or forward in the
858 // URL history.
859 m_urlNavigator->saveLocationState(QByteArray());
860 m_urlNavigator->setLocationUrl(newUrl);
861 setSearchModeEnabled(isSearchUrl(newUrl));
862
863 m_urlNavigator->blockSignals(block);
864 }
865
866 void DolphinViewContainer::requestFocus()
867 {
868 m_view->setFocus();
869 }
870
871 void DolphinViewContainer::startSearching()
872 {
873 Q_CHECK_PTR(m_urlNavigatorConnected);
874 const QUrl url = m_searchBox->urlForSearching();
875 if (url.isValid() && !url.isEmpty()) {
876 m_view->setViewPropertiesContext(QStringLiteral("search"));
877 m_urlNavigatorConnected->setLocationUrl(url);
878 }
879 }
880
881 void DolphinViewContainer::openSearchBox()
882 {
883 setSearchModeEnabled(true);
884 }
885
886 void DolphinViewContainer::closeSearchBox()
887 {
888 setSearchModeEnabled(false);
889 }
890
891 void DolphinViewContainer::stopDirectoryLoading()
892 {
893 m_view->stopLoading();
894 m_statusBar->showProgress(QString(), 100);
895 }
896
897 void DolphinViewContainer::slotStatusBarZoomLevelChanged(int zoomLevel)
898 {
899 m_view->setZoomLevel(zoomLevel);
900 }
901
902 void DolphinViewContainer::slotErrorMessageFromView(const QString &message, const int kioErrorCode)
903 {
904 if (kioErrorCode == KIO::ERR_CANNOT_ENTER_DIRECTORY && m_view->url().scheme() == QStringLiteral("file")
905 && KProtocolInfo::isKnownProtocol(QStringLiteral("admin")) && !rootItem().isReadable()) {
906 // Explain to users that they need authentication to see the folder contents.
907 if (!m_authorizeToEnterFolderAction) { // This code is similar to parts of Admin::Bar::hideTheNextTimeAuthorizationExpires().
908 // We should not simply use the actAsAdminAction() itself here because that one always refers to the active view instead of this->m_view.
909 auto actAsAdminAction = Admin::WorkerIntegration::FriendAccess::actAsAdminAction();
910 m_authorizeToEnterFolderAction = new QAction{actAsAdminAction->icon(), actAsAdminAction->text(), this};
911 m_authorizeToEnterFolderAction->setToolTip(actAsAdminAction->toolTip());
912 m_authorizeToEnterFolderAction->setWhatsThis(actAsAdminAction->whatsThis());
913 connect(m_authorizeToEnterFolderAction, &QAction::triggered, this, [this, actAsAdminAction]() {
914 setActive(true);
915 actAsAdminAction->trigger();
916 });
917 }
918 showMessage(i18nc("@info", "Authorization required to enter this folder."), KMessageWidget::Error, {m_authorizeToEnterFolderAction});
919 return;
920 }
921 Q_EMIT showErrorMessage(message);
922 }
923
924 void DolphinViewContainer::showErrorMessage(const QString &message)
925 {
926 showMessage(message, KMessageWidget::Error);
927 }
928
929 void DolphinViewContainer::slotPlacesModelChanged()
930 {
931 if (!GeneralSettings::showFullPathInTitlebar() && !isSearchModeEnabled()) {
932 Q_EMIT captionChanged();
933 }
934 }
935
936 void DolphinViewContainer::slotHiddenFilesShownChanged(bool showHiddenFiles)
937 {
938 if (m_urlNavigatorConnected) {
939 m_urlNavigatorConnected->setShowHiddenFolders(showHiddenFiles);
940 }
941 }
942
943 void DolphinViewContainer::slotSortHiddenLastChanged(bool hiddenLast)
944 {
945 if (m_urlNavigatorConnected) {
946 m_urlNavigatorConnected->setSortHiddenFoldersLast(hiddenLast);
947 }
948 }
949
950 void DolphinViewContainer::slotCurrentDirectoryRemoved()
951 {
952 const QString location(url().toDisplayString(QUrl::PreferLocalFile));
953 if (url().isLocalFile()) {
954 const QString dirPath = url().toLocalFile();
955 const QString newPath = getNearestExistingAncestorOfPath(dirPath);
956 const QUrl newUrl = QUrl::fromLocalFile(newPath);
957 setUrl(newUrl);
958 }
959
960 showMessage(xi18n("Current location changed, <filename>%1</filename> is no longer accessible.", location), KMessageWidget::Warning);
961 }
962
963 void DolphinViewContainer::slotOpenUrlFinished(KJob *job)
964 {
965 if (job->error() && job->error() != KIO::ERR_USER_CANCELED) {
966 showErrorMessage(job->errorString());
967 }
968 }
969
970 bool DolphinViewContainer::isSearchUrl(const QUrl &url) const
971 {
972 return url.scheme().contains(QLatin1String("search"));
973 }
974
975 void DolphinViewContainer::saveViewState()
976 {
977 QByteArray locationState;
978 QDataStream stream(&locationState, QIODevice::WriteOnly);
979 m_view->saveState(stream);
980 m_urlNavigator->saveLocationState(locationState);
981 }
982
983 void DolphinViewContainer::tryRestoreViewState()
984 {
985 QByteArray locationState = m_urlNavigator->locationState();
986 if (!locationState.isEmpty()) {
987 QDataStream stream(&locationState, QIODevice::ReadOnly);
988 m_view->restoreState(stream);
989 }
990 }
991
992 QString DolphinViewContainer::getNearestExistingAncestorOfPath(const QString &path) const
993 {
994 QDir dir(path);
995 do {
996 dir.setPath(QDir::cleanPath(dir.filePath(QStringLiteral(".."))));
997 } while (!dir.exists() && !dir.isRoot());
998
999 return dir.exists() ? dir.path() : QString{};
1000 }
1001
1002 #include "moc_dolphinviewcontainer.cpp"