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