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