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