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