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