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