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