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