]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinviewcontainer.cpp
Word-wrap KMessageWidget text
[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 // TODO: wrap at arbitrary character positions once QLabel can do this
316 // https://bugreports.qt.io/browse/QTBUG-1276
317 m_messageWidget->setWordWrap(true);
318
319 switch (type) {
320 case Information: m_messageWidget->setMessageType(KMessageWidget::Information); break;
321 case Warning: m_messageWidget->setMessageType(KMessageWidget::Warning); break;
322 case Error: m_messageWidget->setMessageType(KMessageWidget::Error); break;
323 default:
324 Q_ASSERT(false);
325 break;
326 }
327
328 m_messageWidget->setWordWrap(false);
329 const int unwrappedWidth = m_messageWidget->sizeHint().width();
330 m_messageWidget->setWordWrap(unwrappedWidth > size().width());
331
332 if (m_messageWidget->isVisible()) {
333 m_messageWidget->hide();
334 }
335 m_messageWidget->animatedShow();
336 }
337
338 void DolphinViewContainer::readSettings()
339 {
340 if (GeneralSettings::modifiedStartupSettings()) {
341 // The startup settings should only get applied if they have been
342 // modified by the user. Otherwise keep the (possibly) different current
343 // settings of the URL navigator and the filterbar.
344 m_urlNavigator->setUrlEditable(GeneralSettings::editableUrl());
345 m_urlNavigator->setShowFullPath(GeneralSettings::showFullPath());
346 m_urlNavigator->setHomeUrl(Dolphin::homeUrl());
347 setFilterBarVisible(GeneralSettings::filterBar());
348 }
349
350 m_view->readSettings();
351 m_statusBar->readSettings();
352 }
353
354 bool DolphinViewContainer::isFilterBarVisible() const
355 {
356 return m_filterBar->isVisible();
357 }
358
359 void DolphinViewContainer::setSearchModeEnabled(bool enabled)
360 {
361 if (enabled == isSearchModeEnabled()) {
362 if (enabled && !m_searchBox->hasFocus()) {
363 m_searchBox->setFocus();
364 m_searchBox->selectAll();
365 }
366 return;
367 }
368
369 m_searchBox->setVisible(enabled);
370 m_navigatorWidget->setVisible(!enabled);
371
372 if (enabled) {
373 const QUrl& locationUrl = m_urlNavigator->locationUrl();
374 m_searchBox->fromSearchUrl(locationUrl);
375 } else {
376 m_view->setViewPropertiesContext(QString());
377
378 // Restore the URL for the URL navigator. If Dolphin has been
379 // started with a search-URL, the home URL is used as fallback.
380 QUrl url = m_searchBox->searchPath();
381 if (url.isEmpty() || !url.isValid() || isSearchUrl(url)) {
382 url = Dolphin::homeUrl();
383 }
384 m_urlNavigator->setLocationUrl(url);
385 }
386
387 m_searchModeEnabled = enabled;
388 }
389
390 bool DolphinViewContainer::isSearchModeEnabled() const
391 {
392 return m_searchModeEnabled;
393 }
394
395 QString DolphinViewContainer::placesText() const
396 {
397 QString text;
398
399 if (isSearchModeEnabled()) {
400 text = i18n("Search for %1 in %2", m_searchBox->text(), m_searchBox->searchPath().fileName());
401 } else {
402 text = url().fileName();
403 if (text.isEmpty()) {
404 text = url().host();
405 }
406 if (text.isEmpty()) {
407 text = url().scheme();
408 }
409 }
410
411 return text;
412 }
413
414 void DolphinViewContainer::reload()
415 {
416 view()->reload();
417 m_messageWidget->hide();
418 }
419
420 QString DolphinViewContainer::caption() const
421 {
422 if (GeneralSettings::showFullPathInTitlebar()) {
423 if (!url().isLocalFile()) {
424 return url().adjusted(QUrl::StripTrailingSlash).toString();
425 }
426 return url().adjusted(QUrl::StripTrailingSlash).path();
427 }
428
429 KFilePlacesModel *placesModel = DolphinPlacesModelSingleton::instance().placesModel();
430 const auto& matchedPlaces = placesModel->match(placesModel->index(0,0), KFilePlacesModel::UrlRole, url(), 1, Qt::MatchExactly);
431
432 if (!matchedPlaces.isEmpty()) {
433 return placesModel->text(matchedPlaces.first());
434 }
435
436 if (isSearchModeEnabled()) {
437 if (currentSearchText().isEmpty()){
438 return i18n("Search");
439 } else {
440 return i18n("Search for %1", currentSearchText());
441 }
442 }
443
444 if (!url().isLocalFile()) {
445 QUrl adjustedUrl = url().adjusted(QUrl::StripTrailingSlash);
446 QString caption;
447 if (!adjustedUrl.fileName().isEmpty()) {
448 caption = adjustedUrl.fileName();
449 } else if (!adjustedUrl.path().isEmpty() && adjustedUrl.path() != "/") {
450 caption = adjustedUrl.path();
451 } else if (!adjustedUrl.host().isEmpty()) {
452 caption = adjustedUrl.host();
453 } else {
454 caption = adjustedUrl.toString();
455 }
456 return caption;
457 }
458
459 QString fileName = url().adjusted(QUrl::StripTrailingSlash).fileName();
460 if (fileName.isEmpty()) {
461 fileName = '/';
462 }
463
464 return fileName;
465 }
466
467 void DolphinViewContainer::setUrl(const QUrl& newUrl)
468 {
469 if (newUrl != m_urlNavigator->locationUrl()) {
470 m_urlNavigator->setLocationUrl(newUrl);
471 }
472
473 #ifdef KActivities_FOUND
474 m_activityResourceInstance->setUri(newUrl);
475 #endif
476 }
477
478 void DolphinViewContainer::setFilterBarVisible(bool visible)
479 {
480 Q_ASSERT(m_filterBar);
481 if (visible) {
482 m_filterBar->show();
483 m_filterBar->setFocus();
484 m_filterBar->selectAll();
485 } else {
486 closeFilterBar();
487 }
488 }
489
490 void DolphinViewContainer::delayedStatusBarUpdate()
491 {
492 if (m_statusBarTimer->isActive() && (m_statusBarTimestamp.elapsed() > 2000)) {
493 // No update of the statusbar has been done during the last 2 seconds,
494 // although an update has been requested. Trigger an immediate update.
495 m_statusBarTimer->stop();
496 updateStatusBar();
497 } else {
498 // Invoke updateStatusBar() with a small delay. This assures that
499 // when a lot of delayedStatusBarUpdates() are done in a short time,
500 // no bottleneck is given.
501 m_statusBarTimer->start();
502 }
503 }
504
505 void DolphinViewContainer::updateStatusBar()
506 {
507 m_statusBarTimestamp.start();
508
509 const QString text = m_view->statusBarText();
510 m_statusBar->setDefaultText(text);
511 m_statusBar->resetToDefaultText();
512 }
513
514 void DolphinViewContainer::updateDirectoryLoadingProgress(int percent)
515 {
516 if (m_statusBar->progressText().isEmpty()) {
517 m_statusBar->setProgressText(i18nc("@info:progress", "Loading folder..."));
518 }
519 m_statusBar->setProgress(percent);
520 }
521
522 void DolphinViewContainer::updateDirectorySortingProgress(int percent)
523 {
524 if (m_statusBar->progressText().isEmpty()) {
525 m_statusBar->setProgressText(i18nc("@info:progress", "Sorting..."));
526 }
527 m_statusBar->setProgress(percent);
528 }
529
530 void DolphinViewContainer::slotDirectoryLoadingStarted()
531 {
532 if (isSearchUrl(url())) {
533 // Search KIO-slaves usually don't provide any progress information. Give
534 // a hint to the user that a searching is done:
535 updateStatusBar();
536 m_statusBar->setProgressText(i18nc("@info", "Searching..."));
537 m_statusBar->setProgress(-1);
538 } else {
539 // Trigger an undetermined progress indication. The progress
540 // information in percent will be triggered by the percent() signal
541 // of the directory lister later.
542 updateDirectoryLoadingProgress(-1);
543 }
544 }
545
546 void DolphinViewContainer::slotDirectoryLoadingCompleted()
547 {
548 if (!m_statusBar->progressText().isEmpty()) {
549 m_statusBar->setProgressText(QString());
550 m_statusBar->setProgress(100);
551 }
552
553 if (isSearchUrl(url()) && m_view->itemsCount() == 0) {
554 // The dir lister has been completed on a Baloo-URI and no items have been found. Instead
555 // of showing the default status bar information ("0 items") a more helpful information is given:
556 m_statusBar->setText(i18nc("@info:status", "No items found."));
557 } else {
558 updateStatusBar();
559 }
560 }
561
562 void DolphinViewContainer::slotDirectoryLoadingCanceled()
563 {
564 if (!m_statusBar->progressText().isEmpty()) {
565 m_statusBar->setProgressText(QString());
566 m_statusBar->setProgress(100);
567 }
568
569 m_statusBar->setText(QString());
570 }
571
572 void DolphinViewContainer::slotUrlIsFileError(const QUrl& url)
573 {
574 const KFileItem item(url);
575
576 // Find out if the file can be opened in the view (for example, this is the
577 // case if the file is an archive). The mime type must be known for that.
578 item.determineMimeType();
579 const QUrl& folderUrl = DolphinView::openItemAsFolderUrl(item, true);
580 if (!folderUrl.isEmpty()) {
581 setUrl(folderUrl);
582 } else {
583 slotItemActivated(item);
584 }
585 }
586
587 void DolphinViewContainer::slotItemActivated(const KFileItem& item)
588 {
589 // It is possible to activate items on inactive views by
590 // drag & drop operations. Assure that activating an item always
591 // results in an active view.
592 m_view->setActive(true);
593
594 const QUrl& url = DolphinView::openItemAsFolderUrl(item, GeneralSettings::browseThroughArchives());
595 if (!url.isEmpty()) {
596 setUrl(url);
597 return;
598 }
599
600 KRun *run = new KRun(item.targetUrl(), this);
601 run->setShowScriptExecutionPrompt(true);
602 }
603
604 void DolphinViewContainer::slotItemsActivated(const KFileItemList& items)
605 {
606 Q_ASSERT(items.count() >= 2);
607
608 KFileItemActions fileItemActions(this);
609 fileItemActions.runPreferredApplications(items, QString());
610 }
611
612 void DolphinViewContainer::showItemInfo(const KFileItem& item)
613 {
614 if (item.isNull()) {
615 m_statusBar->resetToDefaultText();
616 } else {
617 m_statusBar->setText(item.getStatusBarInfo());
618 }
619 }
620
621 void DolphinViewContainer::closeFilterBar()
622 {
623 m_filterBar->closeFilterBar();
624 m_view->setFocus();
625 emit showFilterBarChanged(false);
626 }
627
628 void DolphinViewContainer::setNameFilter(const QString& nameFilter)
629 {
630 m_view->setNameFilter(nameFilter);
631 delayedStatusBarUpdate();
632 }
633
634 void DolphinViewContainer::activate()
635 {
636 setActive(true);
637 }
638
639 void DolphinViewContainer::slotUrlNavigatorLocationAboutToBeChanged(const QUrl&)
640 {
641 saveViewState();
642 }
643
644 void DolphinViewContainer::slotUrlNavigatorLocationChanged(const QUrl& url)
645 {
646 slotReturnPressed();
647
648 if (KProtocolManager::supportsListing(url)) {
649 setSearchModeEnabled(isSearchUrl(url));
650 m_view->setUrl(url);
651 tryRestoreViewState();
652
653 if (m_autoGrabFocus && isActive() && !isSearchUrl(url)) {
654 // When an URL has been entered, the view should get the focus.
655 // The focus must be requested asynchronously, as changing the URL might create
656 // a new view widget.
657 QTimer::singleShot(0, this, &DolphinViewContainer::requestFocus);
658 }
659 } else if (KProtocolManager::isSourceProtocol(url)) {
660 QString app = QStringLiteral("konqueror");
661 if (url.scheme().startsWith(QLatin1String("http"))) {
662 showMessage(i18nc("@info:status", // krazy:exclude=qmethods
663 "Dolphin does not support web pages, the web browser has been launched"),
664 Information);
665
666 const KConfigGroup config(KSharedConfig::openConfig(QStringLiteral("kdeglobals")), "General");
667 const QString browser = config.readEntry("BrowserApplication");
668 if (!browser.isEmpty()) {
669 app = browser;
670 if (app.startsWith('!')) {
671 // a literal command has been configured, remove the '!' prefix
672 app = app.mid(1);
673 }
674 }
675 } else {
676 showMessage(i18nc("@info:status",
677 "Protocol not supported by Dolphin, Konqueror has been launched"),
678 Information);
679 }
680
681 const QString secureUrl = KShell::quoteArg(url.toDisplayString(QUrl::PreferLocalFile));
682 const QString command = app + ' ' + secureUrl;
683 KRun::runCommand(command, app, app, this);
684 } else {
685 showMessage(i18nc("@info:status", "Invalid protocol"), Error);
686 }
687 }
688
689 void DolphinViewContainer::slotUrlSelectionRequested(const QUrl& url)
690 {
691 m_view->markUrlsAsSelected({url});
692 m_view->markUrlAsCurrent(url); // makes the item scroll into view
693 }
694
695 void DolphinViewContainer::redirect(const QUrl& oldUrl, const QUrl& newUrl)
696 {
697 Q_UNUSED(oldUrl);
698 const bool block = m_urlNavigator->signalsBlocked();
699 m_urlNavigator->blockSignals(true);
700
701 // Assure that the location state is reset for redirection URLs. This
702 // allows to skip redirection URLs when going back or forward in the
703 // URL history.
704 m_urlNavigator->saveLocationState(QByteArray());
705 m_urlNavigator->setLocationUrl(newUrl);
706 setSearchModeEnabled(isSearchUrl(newUrl));
707
708 m_urlNavigator->blockSignals(block);
709 }
710
711 void DolphinViewContainer::requestFocus()
712 {
713 m_view->setFocus();
714 }
715
716 void DolphinViewContainer::saveUrlCompletionMode(KCompletion::CompletionMode completion)
717 {
718 GeneralSettings::setUrlCompletionMode(completion);
719 }
720
721 void DolphinViewContainer::slotReturnPressed()
722 {
723 if (!GeneralSettings::editableUrl()) {
724 m_urlNavigator->setUrlEditable(false);
725 }
726 }
727
728 void DolphinViewContainer::startSearching()
729 {
730 const QUrl url = m_searchBox->urlForSearching();
731 if (url.isValid() && !url.isEmpty()) {
732 m_view->setViewPropertiesContext(QStringLiteral("search"));
733 m_urlNavigator->setLocationUrl(url);
734 }
735 }
736
737 void DolphinViewContainer::closeSearchBox()
738 {
739 setSearchModeEnabled(false);
740 }
741
742 void DolphinViewContainer::stopDirectoryLoading()
743 {
744 m_view->stopLoading();
745 m_statusBar->setProgress(100);
746 }
747
748 void DolphinViewContainer::slotStatusBarZoomLevelChanged(int zoomLevel)
749 {
750 m_view->setZoomLevel(zoomLevel);
751 }
752
753 void DolphinViewContainer::showErrorMessage(const QString& msg)
754 {
755 showMessage(msg, Error);
756 }
757
758 bool DolphinViewContainer::isSearchUrl(const QUrl& url) const
759 {
760 return url.scheme().contains(QStringLiteral("search"));
761 }
762
763 void DolphinViewContainer::saveViewState()
764 {
765 QByteArray locationState;
766 QDataStream stream(&locationState, QIODevice::WriteOnly);
767 m_view->saveState(stream);
768 m_urlNavigator->saveLocationState(locationState);
769 }
770
771 void DolphinViewContainer::tryRestoreViewState()
772 {
773 QByteArray locationState = m_urlNavigator->locationState();
774 if (!locationState.isEmpty()) {
775 QDataStream stream(&locationState, QIODevice::ReadOnly);
776 m_view->restoreState(stream);
777 }
778 }