]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinviewcontainer.cpp
Output of licensedigger + manual cleanup afterwards.
[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 "dolphin_generalsettings.h"
10 #include "dolphinplacesmodelsingleton.h"
11 #include "dolphindebug.h"
12 #include "filterbar/filterbar.h"
13 #include "global.h"
14 #include "search/dolphinsearchbox.h"
15 #include "statusbar/dolphinstatusbar.h"
16 #include "trash/dolphintrash.h"
17 #include "views/viewmodecontroller.h"
18 #include "views/viewproperties.h"
19 #include "dolphin_detailsmodesettings.h"
20 #include "views/dolphinview.h"
21
22 #ifdef HAVE_KACTIVITIES
23 #include <KActivities/ResourceInstance>
24 #endif
25 #include <KFileItemActions>
26 #include <KFilePlacesModel>
27 #include <KIO/PreviewJob>
28 #include <KIO/OpenUrlJob>
29 #include <KIO/JobUiDelegate>
30 #include <KLocalizedString>
31 #include <KMessageWidget>
32 #include <KProtocolManager>
33 #include <KShell>
34 #include <KUrlComboBox>
35 #include <KUrlNavigator>
36
37 #include <QDropEvent>
38 #include <QLoggingCategory>
39 #include <QMimeData>
40 #include <QTimer>
41 #include <QUrl>
42 #include <QVBoxLayout>
43 #include <QDesktopServices>
44
45 DolphinViewContainer::DolphinViewContainer(const QUrl& url, QWidget* parent) :
46 QWidget(parent),
47 m_topLayout(nullptr),
48 m_navigatorWidget(nullptr),
49 m_urlNavigator(nullptr),
50 m_emptyTrashButton(nullptr),
51 m_searchBox(nullptr),
52 m_searchModeEnabled(false),
53 m_messageWidget(nullptr),
54 m_view(nullptr),
55 m_filterBar(nullptr),
56 m_statusBar(nullptr),
57 m_statusBarTimer(nullptr),
58 m_statusBarTimestamp(),
59 m_autoGrabFocus(true)
60 #ifdef HAVE_KACTIVITIES
61 , m_activityResourceInstance(nullptr)
62 #endif
63 {
64 hide();
65
66 m_topLayout = new QVBoxLayout(this);
67 m_topLayout->setSpacing(0);
68 m_topLayout->setContentsMargins(0, 0, 0, 0);
69
70 m_navigatorWidget = new QWidget(this);
71 QHBoxLayout* navigatorLayout = new QHBoxLayout(m_navigatorWidget);
72 navigatorLayout->setSpacing(0);
73 navigatorLayout->setContentsMargins(0, 0, 0, 0);
74 m_navigatorWidget->setWhatsThis(xi18nc("@info:whatsthis location bar",
75 "<para>This line describes the location of the files and folders "
76 "displayed below.</para><para>The name of the currently viewed "
77 "folder can be read at the very right. To the left of it is the "
78 "name of the folder that contains it. The whole line is called "
79 "the <emphasis>path</emphasis> to the current location because "
80 "following these folders from left to right leads here.</para>"
81 "<para>The path is displayed on the <emphasis>location bar</emphasis> "
82 "which is more powerful than one would expect. To learn more "
83 "about the basic and advanced features of the location bar "
84 "<link url='help:/dolphin/location-bar.html'>click here</link>. "
85 "This will open the dedicated page in the Handbook.</para>"));
86
87 m_urlNavigator = new KUrlNavigator(DolphinPlacesModelSingleton::instance().placesModel(), url, this);
88 connect(m_urlNavigator, &KUrlNavigator::activated,
89 this, &DolphinViewContainer::activate);
90 connect(m_urlNavigator->editor(), &KUrlComboBox::completionModeChanged,
91 this, &DolphinViewContainer::saveUrlCompletionMode);
92
93 const GeneralSettings* settings = GeneralSettings::self();
94 m_urlNavigator->setUrlEditable(settings->editableUrl());
95 m_urlNavigator->setShowFullPath(settings->showFullPath());
96 m_urlNavigator->setHomeUrl(Dolphin::homeUrl());
97 KUrlComboBox* editor = m_urlNavigator->editor();
98 editor->setCompletionMode(KCompletion::CompletionMode(settings->urlCompletionMode()));
99
100 m_emptyTrashButton = new QPushButton(QIcon::fromTheme(QStringLiteral("user-trash")), i18nc("@action:button", "Empty Trash"), this);
101 m_emptyTrashButton->setFlat(true);
102 connect(m_emptyTrashButton, &QPushButton::clicked, this, [this]() { Trash::empty(this); });
103 connect(&Trash::instance(), &Trash::emptinessChanged, m_emptyTrashButton, &QPushButton::setDisabled);
104 m_emptyTrashButton->setDisabled(Trash::isEmpty());
105 m_emptyTrashButton->hide();
106
107 m_searchBox = new DolphinSearchBox(this);
108 m_searchBox->hide();
109 connect(m_searchBox, &DolphinSearchBox::activated, this, &DolphinViewContainer::activate);
110 connect(m_searchBox, &DolphinSearchBox::closeRequest, this, &DolphinViewContainer::closeSearchBox);
111 connect(m_searchBox, &DolphinSearchBox::searchRequest, this, &DolphinViewContainer::startSearching);
112 connect(m_searchBox, &DolphinSearchBox::focusViewRequest, this, &DolphinViewContainer::requestFocus);
113 m_searchBox->setWhatsThis(xi18nc("@info:whatsthis findbar",
114 "<para>This helps you find files and folders. Enter a <emphasis>"
115 "search term</emphasis> and specify search settings with the "
116 "buttons at the bottom:<list><item>Filename/Content: "
117 "Does the item you are looking for contain the search terms "
118 "within its filename or its contents?<nl/>The contents of images, "
119 "audio files and videos will not be searched.</item><item>"
120 "From Here/Everywhere: Do you want to search in this "
121 "folder and its sub-folders or everywhere?</item><item>"
122 "More Options: Click this to search by media type, access "
123 "time or rating.</item><item>More Search Tools: Install other "
124 "means to find an item.</item></list></para>"));
125
126 m_messageWidget = new KMessageWidget(this);
127 m_messageWidget->setCloseButtonVisible(true);
128 m_messageWidget->hide();
129
130 #ifndef Q_OS_WIN
131 if (getuid() == 0) {
132
133 // We must be logged in as the root user; show a big scary warning
134 showMessage(i18n("Running Dolphin as root can be dangerous. Please be careful."), Warning);
135 }
136 #endif
137
138 // Initialize filter bar
139 m_filterBar = new FilterBar(this);
140 m_filterBar->setVisible(settings->filterBar());
141
142 connect(m_filterBar, &FilterBar::filterChanged,
143 this, &DolphinViewContainer::setNameFilter);
144 connect(m_filterBar, &FilterBar::closeRequest,
145 this, &DolphinViewContainer::closeFilterBar);
146 connect(m_filterBar, &FilterBar::focusViewRequest,
147 this, &DolphinViewContainer::requestFocus);
148
149 // Initialize the main view
150 m_view = new DolphinView(url, this);
151 connect(m_view, &DolphinView::urlChanged,
152 m_filterBar, &FilterBar::slotUrlChanged);
153 connect(m_view, &DolphinView::urlChanged,
154 m_urlNavigator, &KUrlNavigator::setLocationUrl);
155 connect(m_view, &DolphinView::urlChanged,
156 m_messageWidget, &KMessageWidget::hide);
157 connect(m_view, &DolphinView::writeStateChanged,
158 this, &DolphinViewContainer::writeStateChanged);
159 connect(m_view, &DolphinView::requestItemInfo,
160 this, &DolphinViewContainer::showItemInfo);
161 connect(m_view, &DolphinView::itemActivated,
162 this, &DolphinViewContainer::slotItemActivated);
163 connect(m_view, &DolphinView::itemsActivated,
164 this, &DolphinViewContainer::slotItemsActivated);
165 connect(m_view, &DolphinView::redirection,
166 this, &DolphinViewContainer::redirect);
167 connect(m_view, &DolphinView::directoryLoadingStarted,
168 this, &DolphinViewContainer::slotDirectoryLoadingStarted);
169 connect(m_view, &DolphinView::directoryLoadingCompleted,
170 this, &DolphinViewContainer::slotDirectoryLoadingCompleted);
171 connect(m_view, &DolphinView::directoryLoadingCanceled,
172 this, &DolphinViewContainer::slotDirectoryLoadingCanceled);
173 connect(m_view, &DolphinView::itemCountChanged,
174 this, &DolphinViewContainer::delayedStatusBarUpdate);
175 connect(m_view, &DolphinView::directoryLoadingProgress,
176 this, &DolphinViewContainer::updateDirectoryLoadingProgress);
177 connect(m_view, &DolphinView::directorySortingProgress,
178 this, &DolphinViewContainer::updateDirectorySortingProgress);
179 connect(m_view, &DolphinView::selectionChanged,
180 this, &DolphinViewContainer::delayedStatusBarUpdate);
181 connect(m_view, &DolphinView::errorMessage,
182 this, &DolphinViewContainer::showErrorMessage);
183 connect(m_view, &DolphinView::urlIsFileError,
184 this, &DolphinViewContainer::slotUrlIsFileError);
185 connect(m_view, &DolphinView::activated,
186 this, &DolphinViewContainer::activate);
187
188 connect(m_urlNavigator, &KUrlNavigator::urlAboutToBeChanged,
189 this, &DolphinViewContainer::slotUrlNavigatorLocationAboutToBeChanged);
190 connect(m_urlNavigator, &KUrlNavigator::urlChanged,
191 this, &DolphinViewContainer::slotUrlNavigatorLocationChanged);
192 connect(m_urlNavigator, &KUrlNavigator::urlSelectionRequested,
193 this, &DolphinViewContainer::slotUrlSelectionRequested);
194 connect(m_urlNavigator, &KUrlNavigator::returnPressed,
195 this, &DolphinViewContainer::slotReturnPressed);
196 connect(m_urlNavigator, &KUrlNavigator::urlsDropped, this, [=](const QUrl &destination, QDropEvent *event) {
197 m_view->dropUrls(destination, event, m_urlNavigator->dropWidget());
198 });
199
200 connect(m_view, &DolphinView::directoryLoadingCompleted, this, [this]() {
201 m_emptyTrashButton->setVisible(m_view->url().scheme() == QLatin1String("trash"));
202 });
203
204 // Initialize status bar
205 m_statusBar = new DolphinStatusBar(this);
206 m_statusBar->setUrl(m_view->url());
207 m_statusBar->setZoomLevel(m_view->zoomLevel());
208 connect(m_view, &DolphinView::urlChanged,
209 m_statusBar, &DolphinStatusBar::setUrl);
210 connect(m_view, &DolphinView::zoomLevelChanged,
211 m_statusBar, &DolphinStatusBar::setZoomLevel);
212 connect(m_view, &DolphinView::infoMessage,
213 m_statusBar, &DolphinStatusBar::setText);
214 connect(m_view, &DolphinView::operationCompletedMessage,
215 m_statusBar, &DolphinStatusBar::setText);
216 connect(m_statusBar, &DolphinStatusBar::stopPressed,
217 this, &DolphinViewContainer::stopDirectoryLoading);
218 connect(m_statusBar, &DolphinStatusBar::zoomLevelChanged,
219 this, &DolphinViewContainer::slotStatusBarZoomLevelChanged);
220
221 m_statusBarTimer = new QTimer(this);
222 m_statusBarTimer->setSingleShot(true);
223 m_statusBarTimer->setInterval(300);
224 connect(m_statusBarTimer, &QTimer::timeout, this, &DolphinViewContainer::updateStatusBar);
225
226 KIO::FileUndoManager* undoManager = KIO::FileUndoManager::self();
227 connect(undoManager, &KIO::FileUndoManager::jobRecordingFinished,
228 this, &DolphinViewContainer::delayedStatusBarUpdate);
229
230 navigatorLayout->addWidget(m_urlNavigator);
231 navigatorLayout->addWidget(m_emptyTrashButton);
232
233 m_topLayout->addWidget(m_navigatorWidget);
234 m_topLayout->addWidget(m_searchBox);
235 m_topLayout->addWidget(m_messageWidget);
236 m_topLayout->addWidget(m_view);
237 m_topLayout->addWidget(m_filterBar);
238 m_topLayout->addWidget(m_statusBar);
239
240 setSearchModeEnabled(isSearchUrl(url));
241
242 connect(DetailsModeSettings::self(), &KCoreConfigSkeleton::configChanged, this, [=]() {
243 if (view()->mode() == DolphinView::Mode::DetailsView) {
244 view()->reload();
245 }
246 });
247
248 // Initialize kactivities resource instance
249
250 #ifdef HAVE_KACTIVITIES
251 m_activityResourceInstance = new KActivities::ResourceInstance(window()->winId(), url);
252 m_activityResourceInstance->setParent(this);
253 #endif
254 }
255
256 DolphinViewContainer::~DolphinViewContainer()
257 {
258 }
259
260 QUrl DolphinViewContainer::url() const
261 {
262 return m_view->url();
263 }
264
265 void DolphinViewContainer::setActive(bool active)
266 {
267 m_searchBox->setActive(active);
268 m_urlNavigator->setActive(active);
269 m_view->setActive(active);
270
271 #ifdef HAVE_KACTIVITIES
272 if (active) {
273 m_activityResourceInstance->notifyFocusedIn();
274 } else {
275 m_activityResourceInstance->notifyFocusedOut();
276 }
277 #endif
278 }
279
280 bool DolphinViewContainer::isActive() const
281 {
282 Q_ASSERT(m_view->isActive() == m_urlNavigator->isActive());
283 return m_view->isActive();
284 }
285
286 void DolphinViewContainer::setAutoGrabFocus(bool grab)
287 {
288 m_autoGrabFocus = grab;
289 }
290
291 bool DolphinViewContainer::autoGrabFocus() const
292 {
293 return m_autoGrabFocus;
294 }
295
296 QString DolphinViewContainer::currentSearchText() const
297 {
298 return m_searchBox->text();
299 }
300
301 const DolphinStatusBar* DolphinViewContainer::statusBar() const
302 {
303 return m_statusBar;
304 }
305
306 DolphinStatusBar* DolphinViewContainer::statusBar()
307 {
308 return m_statusBar;
309 }
310
311 const KUrlNavigator* DolphinViewContainer::urlNavigator() const
312 {
313 return m_urlNavigator;
314 }
315
316 KUrlNavigator* DolphinViewContainer::urlNavigator()
317 {
318 return m_urlNavigator;
319 }
320
321 const DolphinView* DolphinViewContainer::view() const
322 {
323 return m_view;
324 }
325
326 DolphinView* DolphinViewContainer::view()
327 {
328 return m_view;
329 }
330
331 void DolphinViewContainer::showMessage(const QString& msg, MessageType type)
332 {
333 if (msg.isEmpty()) {
334 return;
335 }
336
337 m_messageWidget->setText(msg);
338
339 // TODO: wrap at arbitrary character positions once QLabel can do this
340 // https://bugreports.qt.io/browse/QTBUG-1276
341 m_messageWidget->setWordWrap(true);
342
343 switch (type) {
344 case Information: m_messageWidget->setMessageType(KMessageWidget::Information); break;
345 case Warning: m_messageWidget->setMessageType(KMessageWidget::Warning); break;
346 case Error: m_messageWidget->setMessageType(KMessageWidget::Error); break;
347 default:
348 Q_ASSERT(false);
349 break;
350 }
351
352 m_messageWidget->setWordWrap(false);
353 const int unwrappedWidth = m_messageWidget->sizeHint().width();
354 m_messageWidget->setWordWrap(unwrappedWidth > size().width());
355
356 if (m_messageWidget->isVisible()) {
357 m_messageWidget->hide();
358 }
359 m_messageWidget->animatedShow();
360 }
361
362 void DolphinViewContainer::readSettings()
363 {
364 if (GeneralSettings::modifiedStartupSettings()) {
365 // The startup settings should only get applied if they have been
366 // modified by the user. Otherwise keep the (possibly) different current
367 // settings of the URL navigator and the filterbar.
368 m_urlNavigator->setUrlEditable(GeneralSettings::editableUrl());
369 m_urlNavigator->setShowFullPath(GeneralSettings::showFullPath());
370 m_urlNavigator->setHomeUrl(Dolphin::homeUrl());
371 setFilterBarVisible(GeneralSettings::filterBar());
372 }
373
374 m_view->readSettings();
375 m_statusBar->readSettings();
376 }
377
378 bool DolphinViewContainer::isFilterBarVisible() const
379 {
380 return m_filterBar->isVisible();
381 }
382
383 void DolphinViewContainer::setSearchModeEnabled(bool enabled)
384 {
385 m_searchBox->setVisible(enabled);
386 m_navigatorWidget->setVisible(!enabled);
387
388 if (enabled) {
389 const QUrl& locationUrl = m_urlNavigator->locationUrl();
390 m_searchBox->fromSearchUrl(locationUrl);
391 }
392
393 if (enabled == isSearchModeEnabled()) {
394 if (enabled && !m_searchBox->hasFocus()) {
395 m_searchBox->setFocus();
396 m_searchBox->selectAll();
397 }
398 return;
399 }
400
401 if (!enabled) {
402 m_view->setViewPropertiesContext(QString());
403
404 // Restore the URL for the URL navigator. If Dolphin has been
405 // started with a search-URL, the home URL is used as fallback.
406 QUrl url = m_searchBox->searchPath();
407 if (url.isEmpty() || !url.isValid() || isSearchUrl(url)) {
408 url = Dolphin::homeUrl();
409 }
410 m_urlNavigator->setLocationUrl(url);
411 }
412
413 m_searchModeEnabled = enabled;
414
415 emit searchModeEnabledChanged(enabled);
416 }
417
418 bool DolphinViewContainer::isSearchModeEnabled() const
419 {
420 return m_searchModeEnabled;
421 }
422
423 QString DolphinViewContainer::placesText() const
424 {
425 QString text;
426
427 if (isSearchModeEnabled()) {
428 text = i18n("Search for %1 in %2", m_searchBox->text(), m_searchBox->searchPath().fileName());
429 } else {
430 text = url().adjusted(QUrl::StripTrailingSlash).fileName();
431 if (text.isEmpty()) {
432 text = url().host();
433 }
434 if (text.isEmpty()) {
435 text = url().scheme();
436 }
437 }
438
439 return text;
440 }
441
442 void DolphinViewContainer::reload()
443 {
444 view()->reload();
445 m_messageWidget->hide();
446 }
447
448 QString DolphinViewContainer::captionWindowTitle() const
449 {
450 if (GeneralSettings::showFullPathInTitlebar() && !isSearchModeEnabled()) {
451 if (!url().isLocalFile()) {
452 return url().adjusted(QUrl::StripTrailingSlash).toString();
453 }
454 return url().adjusted(QUrl::StripTrailingSlash).path();
455 } else {
456 return DolphinViewContainer::caption();
457 }
458 }
459
460 QString DolphinViewContainer::caption() const
461 {
462 if (isSearchModeEnabled()) {
463 if (currentSearchText().isEmpty()){
464 return i18n("Search");
465 } else {
466 return i18n("Search for %1", currentSearchText());
467 }
468 }
469
470 KFilePlacesModel *placesModel = DolphinPlacesModelSingleton::instance().placesModel();
471 const auto& matchedPlaces = placesModel->match(placesModel->index(0,0), KFilePlacesModel::UrlRole, QUrl(url().adjusted(QUrl::StripTrailingSlash).toString(QUrl::FullyEncoded).append("/?")), 1, Qt::MatchRegExp);
472
473 if (!matchedPlaces.isEmpty()) {
474 return placesModel->text(matchedPlaces.first());
475 }
476
477
478 if (!url().isLocalFile()) {
479 QUrl adjustedUrl = url().adjusted(QUrl::StripTrailingSlash);
480 QString caption;
481 if (!adjustedUrl.fileName().isEmpty()) {
482 caption = adjustedUrl.fileName();
483 } else if (!adjustedUrl.path().isEmpty() && adjustedUrl.path() != "/") {
484 caption = adjustedUrl.path();
485 } else if (!adjustedUrl.host().isEmpty()) {
486 caption = adjustedUrl.host();
487 } else {
488 caption = adjustedUrl.toString();
489 }
490 return caption;
491 }
492
493 QString fileName = url().adjusted(QUrl::StripTrailingSlash).fileName();
494 if (fileName.isEmpty()) {
495 fileName = '/';
496 }
497
498 return fileName;
499 }
500
501 void DolphinViewContainer::setUrl(const QUrl& newUrl)
502 {
503 if (newUrl != m_urlNavigator->locationUrl()) {
504 m_urlNavigator->setLocationUrl(newUrl);
505 }
506
507 #ifdef HAVE_KACTIVITIES
508 m_activityResourceInstance->setUri(newUrl);
509 #endif
510 }
511
512 void DolphinViewContainer::setFilterBarVisible(bool visible)
513 {
514 Q_ASSERT(m_filterBar);
515 if (visible) {
516 m_view->hideToolTip(ToolTipManager::HideBehavior::Instantly);
517 m_filterBar->show();
518 m_filterBar->setFocus();
519 m_filterBar->selectAll();
520 } else {
521 closeFilterBar();
522 }
523 }
524
525 void DolphinViewContainer::delayedStatusBarUpdate()
526 {
527 if (m_statusBarTimer->isActive() && (m_statusBarTimestamp.elapsed() > 2000)) {
528 // No update of the statusbar has been done during the last 2 seconds,
529 // although an update has been requested. Trigger an immediate update.
530 m_statusBarTimer->stop();
531 updateStatusBar();
532 } else {
533 // Invoke updateStatusBar() with a small delay. This assures that
534 // when a lot of delayedStatusBarUpdates() are done in a short time,
535 // no bottleneck is given.
536 m_statusBarTimer->start();
537 }
538 }
539
540 void DolphinViewContainer::updateStatusBar()
541 {
542 m_statusBarTimestamp.start();
543
544 const QString text = m_view->statusBarText();
545 m_statusBar->setDefaultText(text);
546 m_statusBar->resetToDefaultText();
547 }
548
549 void DolphinViewContainer::updateDirectoryLoadingProgress(int percent)
550 {
551 if (m_statusBar->progressText().isEmpty()) {
552 m_statusBar->setProgressText(i18nc("@info:progress", "Loading folder..."));
553 }
554 m_statusBar->setProgress(percent);
555 }
556
557 void DolphinViewContainer::updateDirectorySortingProgress(int percent)
558 {
559 if (m_statusBar->progressText().isEmpty()) {
560 m_statusBar->setProgressText(i18nc("@info:progress", "Sorting..."));
561 }
562 m_statusBar->setProgress(percent);
563 }
564
565 void DolphinViewContainer::slotDirectoryLoadingStarted()
566 {
567 if (isSearchUrl(url())) {
568 // Search KIO-slaves usually don't provide any progress information. Give
569 // a hint to the user that a searching is done:
570 updateStatusBar();
571 m_statusBar->setProgressText(i18nc("@info", "Searching..."));
572 m_statusBar->setProgress(-1);
573 } else {
574 // Trigger an undetermined progress indication. The progress
575 // information in percent will be triggered by the percent() signal
576 // of the directory lister later.
577 m_statusBar->setProgressText(QString());
578 updateDirectoryLoadingProgress(-1);
579 }
580 }
581
582 void DolphinViewContainer::slotDirectoryLoadingCompleted()
583 {
584 if (!m_statusBar->progressText().isEmpty()) {
585 m_statusBar->setProgressText(QString());
586 m_statusBar->setProgress(100);
587 }
588
589 if (isSearchUrl(url()) && m_view->itemsCount() == 0) {
590 // The dir lister has been completed on a Baloo-URI and no items have been found. Instead
591 // of showing the default status bar information ("0 items") a more helpful information is given:
592 m_statusBar->setText(i18nc("@info:status", "No items found."));
593 } else {
594 updateStatusBar();
595 }
596 }
597
598 void DolphinViewContainer::slotDirectoryLoadingCanceled()
599 {
600 if (!m_statusBar->progressText().isEmpty()) {
601 m_statusBar->setProgressText(QString());
602 m_statusBar->setProgress(100);
603 }
604
605 m_statusBar->setText(QString());
606 }
607
608 void DolphinViewContainer::slotUrlIsFileError(const QUrl& url)
609 {
610 const KFileItem item(url);
611
612 // Find out if the file can be opened in the view (for example, this is the
613 // case if the file is an archive). The mime type must be known for that.
614 item.determineMimeType();
615 const QUrl& folderUrl = DolphinView::openItemAsFolderUrl(item, true);
616 if (!folderUrl.isEmpty()) {
617 setUrl(folderUrl);
618 } else {
619 slotItemActivated(item);
620 }
621 }
622
623 void DolphinViewContainer::slotItemActivated(const KFileItem& item)
624 {
625 // It is possible to activate items on inactive views by
626 // drag & drop operations. Assure that activating an item always
627 // results in an active view.
628 m_view->setActive(true);
629
630 const QUrl& url = DolphinView::openItemAsFolderUrl(item, GeneralSettings::browseThroughArchives());
631 if (!url.isEmpty()) {
632 setUrl(url);
633 return;
634 }
635
636 KIO::OpenUrlJob *job = new KIO::OpenUrlJob(item.targetUrl());
637 job->setUiDelegate(new KIO::JobUiDelegate(KJobUiDelegate::AutoHandlingEnabled, this));
638 job->setShowOpenOrExecuteDialog(true);
639 job->start();
640 }
641
642 void DolphinViewContainer::slotItemsActivated(const KFileItemList& items)
643 {
644 Q_ASSERT(items.count() >= 2);
645
646 KFileItemActions fileItemActions(this);
647 fileItemActions.runPreferredApplications(items, QString());
648 }
649
650 void DolphinViewContainer::showItemInfo(const KFileItem& item)
651 {
652 if (item.isNull()) {
653 m_statusBar->resetToDefaultText();
654 } else {
655 m_statusBar->setText(item.getStatusBarInfo());
656 }
657 }
658
659 void DolphinViewContainer::closeFilterBar()
660 {
661 m_filterBar->closeFilterBar();
662 m_view->setFocus();
663 emit showFilterBarChanged(false);
664 }
665
666 void DolphinViewContainer::setNameFilter(const QString& nameFilter)
667 {
668 m_view->hideToolTip(ToolTipManager::HideBehavior::Instantly);
669 m_view->setNameFilter(nameFilter);
670 delayedStatusBarUpdate();
671 }
672
673 void DolphinViewContainer::activate()
674 {
675 setActive(true);
676 }
677
678 void DolphinViewContainer::slotUrlNavigatorLocationAboutToBeChanged(const QUrl&)
679 {
680 saveViewState();
681 }
682
683 void DolphinViewContainer::slotUrlNavigatorLocationChanged(const QUrl& url)
684 {
685 slotReturnPressed();
686
687 if (KProtocolManager::supportsListing(url)) {
688 setSearchModeEnabled(isSearchUrl(url));
689 m_view->setUrl(url);
690 tryRestoreViewState();
691
692 if (m_autoGrabFocus && isActive() && !isSearchUrl(url)) {
693 // When an URL has been entered, the view should get the focus.
694 // The focus must be requested asynchronously, as changing the URL might create
695 // a new view widget.
696 QTimer::singleShot(0, this, &DolphinViewContainer::requestFocus);
697 }
698 } else if (KProtocolManager::isSourceProtocol(url)) {
699 if (url.scheme().startsWith(QLatin1String("http"))) {
700 showMessage(i18nc("@info:status", // krazy:exclude=qmethods
701 "Dolphin does not support web pages, the web browser has been launched"),
702 Information);
703 } else {
704 showMessage(i18nc("@info:status",
705 "Protocol not supported by Dolphin, default application has been launched"),
706 Information);
707 }
708
709 QDesktopServices::openUrl(url);
710 redirect(QUrl(), m_urlNavigator->locationUrl(1));
711 } else {
712 showMessage(i18nc("@info:status", "Invalid protocol"), Error);
713 }
714 }
715
716 void DolphinViewContainer::slotUrlSelectionRequested(const QUrl& url)
717 {
718 m_view->markUrlsAsSelected({url});
719 m_view->markUrlAsCurrent(url); // makes the item scroll into view
720 }
721
722 void DolphinViewContainer::redirect(const QUrl& oldUrl, const QUrl& newUrl)
723 {
724 Q_UNUSED(oldUrl)
725 const bool block = m_urlNavigator->signalsBlocked();
726 m_urlNavigator->blockSignals(true);
727
728 // Assure that the location state is reset for redirection URLs. This
729 // allows to skip redirection URLs when going back or forward in the
730 // URL history.
731 m_urlNavigator->saveLocationState(QByteArray());
732 m_urlNavigator->setLocationUrl(newUrl);
733 setSearchModeEnabled(isSearchUrl(newUrl));
734
735 m_urlNavigator->blockSignals(block);
736 }
737
738 void DolphinViewContainer::requestFocus()
739 {
740 m_view->setFocus();
741 }
742
743 void DolphinViewContainer::saveUrlCompletionMode(KCompletion::CompletionMode completion)
744 {
745 GeneralSettings::setUrlCompletionMode(completion);
746 }
747
748 void DolphinViewContainer::slotReturnPressed()
749 {
750 if (!GeneralSettings::editableUrl()) {
751 m_urlNavigator->setUrlEditable(false);
752 }
753 }
754
755 void DolphinViewContainer::startSearching()
756 {
757 const QUrl url = m_searchBox->urlForSearching();
758 if (url.isValid() && !url.isEmpty()) {
759 m_view->setViewPropertiesContext(QStringLiteral("search"));
760 m_urlNavigator->setLocationUrl(url);
761 }
762 }
763
764 void DolphinViewContainer::closeSearchBox()
765 {
766 setSearchModeEnabled(false);
767 }
768
769 void DolphinViewContainer::stopDirectoryLoading()
770 {
771 m_view->stopLoading();
772 m_statusBar->setProgress(100);
773 }
774
775 void DolphinViewContainer::slotStatusBarZoomLevelChanged(int zoomLevel)
776 {
777 m_view->setZoomLevel(zoomLevel);
778 }
779
780 void DolphinViewContainer::showErrorMessage(const QString& msg)
781 {
782 showMessage(msg, Error);
783 }
784
785 bool DolphinViewContainer::isSearchUrl(const QUrl& url) const
786 {
787 return url.scheme().contains(QLatin1String("search"));
788 }
789
790 void DolphinViewContainer::saveViewState()
791 {
792 QByteArray locationState;
793 QDataStream stream(&locationState, QIODevice::WriteOnly);
794 m_view->saveState(stream);
795 m_urlNavigator->saveLocationState(locationState);
796 }
797
798 void DolphinViewContainer::tryRestoreViewState()
799 {
800 QByteArray locationState = m_urlNavigator->locationState();
801 if (!locationState.isEmpty()) {
802 QDataStream stream(&locationState, QIODevice::ReadOnly);
803 m_view->restoreState(stream);
804 }
805 }