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