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