1 /***************************************************************************
2 * Copyright (C) 2007 by Peter Penz <peter.penz19@gmail.com> *
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. *
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. *
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 ***************************************************************************/
20 #include "dolphinviewcontainer.h"
21 #include <KProtocolManager>
26 #include <QVBoxLayout>
28 #include <KFileItemActions>
29 #include <KFilePlacesModel>
30 #include <KLocalizedString>
31 #include <KIO/PreviewJob>
32 #include <KMessageWidget>
35 #include <KUrlComboBox>
36 #include <KUrlNavigator>
39 #ifdef KActivities_FOUND
43 #include "dolphin_generalsettings.h"
44 #include "filterbar/filterbar.h"
45 #include "search/dolphinsearchbox.h"
46 #include "statusbar/dolphinstatusbar.h"
47 #include "views/viewmodecontroller.h"
48 #include "views/viewproperties.h"
50 DolphinViewContainer::DolphinViewContainer(const QUrl
& url
, QWidget
* parent
) :
60 m_statusBarTimestamp(),
62 #ifdef KActivities_FOUND
63 , m_activityResourceInstance(0)
68 m_topLayout
= new QVBoxLayout(this);
69 m_topLayout
->setSpacing(0);
70 m_topLayout
->setMargin(0);
72 m_urlNavigator
= new KUrlNavigator(new KFilePlacesModel(this), url
, this);
73 connect(m_urlNavigator
, &KUrlNavigator::activated
,
74 this, &DolphinViewContainer::activate
);
75 connect(m_urlNavigator
->editor(), &KUrlComboBox::completionModeChanged
,
76 this, &DolphinViewContainer::saveUrlCompletionMode
);
78 const GeneralSettings
* settings
= GeneralSettings::self();
79 m_urlNavigator
->setUrlEditable(settings
->editableUrl());
80 m_urlNavigator
->setShowFullPath(settings
->showFullPath());
81 m_urlNavigator
->setHomeUrl(Dolphin::homeUrl());
82 KUrlComboBox
* editor
= m_urlNavigator
->editor();
83 editor
->setCompletionMode(KCompletion::CompletionMode(settings
->urlCompletionMode()));
85 m_searchBox
= new DolphinSearchBox(this);
87 connect(m_searchBox
, &DolphinSearchBox::activated
, this, &DolphinViewContainer::activate
);
88 connect(m_searchBox
, &DolphinSearchBox::closeRequest
, this, &DolphinViewContainer::closeSearchBox
);
89 connect(m_searchBox
, &DolphinSearchBox::searchRequest
, this, &DolphinViewContainer::startSearching
);
90 connect(m_searchBox
, &DolphinSearchBox::returnPressed
, this, &DolphinViewContainer::requestFocus
);
92 m_messageWidget
= new KMessageWidget(this);
93 m_messageWidget
->setCloseButtonVisible(true);
94 m_messageWidget
->hide();
96 m_view
= new DolphinView(url
, this);
97 connect(m_view
, &DolphinView::urlChanged
,
98 m_urlNavigator
, &KUrlNavigator::setLocationUrl
);
99 connect(m_view
, &DolphinView::urlChanged
,
100 m_messageWidget
, &KMessageWidget::hide
);
101 connect(m_view
, &DolphinView::writeStateChanged
,
102 this, &DolphinViewContainer::writeStateChanged
);
103 connect(m_view
, &DolphinView::requestItemInfo
,
104 this, &DolphinViewContainer::showItemInfo
);
105 connect(m_view
, &DolphinView::itemActivated
,
106 this, &DolphinViewContainer::slotItemActivated
);
107 connect(m_view
, &DolphinView::itemsActivated
,
108 this, &DolphinViewContainer::slotItemsActivated
);
109 connect(m_view
, &DolphinView::redirection
,
110 this, &DolphinViewContainer::redirect
);
111 connect(m_view
, &DolphinView::directoryLoadingStarted
,
112 this, &DolphinViewContainer::slotDirectoryLoadingStarted
);
113 connect(m_view
, &DolphinView::directoryLoadingCompleted
,
114 this, &DolphinViewContainer::slotDirectoryLoadingCompleted
);
115 connect(m_view
, &DolphinView::directoryLoadingCanceled
,
116 this, &DolphinViewContainer::slotDirectoryLoadingCanceled
);
117 connect(m_view
, &DolphinView::itemCountChanged
,
118 this, &DolphinViewContainer::delayedStatusBarUpdate
);
119 connect(m_view
, &DolphinView::directoryLoadingProgress
,
120 this, &DolphinViewContainer::updateDirectoryLoadingProgress
);
121 connect(m_view
, &DolphinView::directorySortingProgress
,
122 this, &DolphinViewContainer::updateDirectorySortingProgress
);
123 connect(m_view
, &DolphinView::selectionChanged
,
124 this, &DolphinViewContainer::delayedStatusBarUpdate
);
125 connect(m_view
, &DolphinView::urlAboutToBeChanged
,
126 this, &DolphinViewContainer::slotViewUrlAboutToBeChanged
);
127 connect(m_view
, &DolphinView::errorMessage
,
128 this, &DolphinViewContainer::showErrorMessage
);
129 connect(m_view
, &DolphinView::urlIsFileError
,
130 this, &DolphinViewContainer::slotUrlIsFileError
);
131 connect(m_view
, &DolphinView::activated
,
132 this, &DolphinViewContainer::activate
);
134 connect(m_urlNavigator
, &KUrlNavigator::urlAboutToBeChanged
,
135 this, &DolphinViewContainer::slotUrlNavigatorLocationAboutToBeChanged
);
136 connect(m_urlNavigator
, &KUrlNavigator::urlChanged
,
137 this, &DolphinViewContainer::slotUrlNavigatorLocationChanged
);
138 connect(m_urlNavigator
, &KUrlNavigator::historyChanged
,
139 this, &DolphinViewContainer::slotHistoryChanged
);
140 connect(m_urlNavigator
, &KUrlNavigator::returnPressed
,
141 this, &DolphinViewContainer::slotReturnPressed
);
142 connect(m_urlNavigator
, &KUrlNavigator::urlsDropped
,
143 m_view
, &DolphinView::dropUrls
);
145 // Initialize status bar
146 m_statusBar
= new DolphinStatusBar(this);
147 m_statusBar
->setUrl(m_view
->url());
148 m_statusBar
->setZoomLevel(m_view
->zoomLevel());
149 connect(m_view
, &DolphinView::urlChanged
,
150 m_statusBar
, &DolphinStatusBar::setUrl
);
151 connect(m_view
, &DolphinView::zoomLevelChanged
,
152 m_statusBar
, &DolphinStatusBar::setZoomLevel
);
153 connect(m_view
, &DolphinView::infoMessage
,
154 m_statusBar
, &DolphinStatusBar::setText
);
155 connect(m_view
, &DolphinView::operationCompletedMessage
,
156 m_statusBar
, &DolphinStatusBar::setText
);
157 connect(m_statusBar
, &DolphinStatusBar::stopPressed
,
158 this, &DolphinViewContainer::stopDirectoryLoading
);
159 connect(m_statusBar
, &DolphinStatusBar::zoomLevelChanged
,
160 this, &DolphinViewContainer::slotStatusBarZoomLevelChanged
);
162 m_statusBarTimer
= new QTimer(this);
163 m_statusBarTimer
->setSingleShot(true);
164 m_statusBarTimer
->setInterval(300);
165 connect(m_statusBarTimer
, &QTimer::timeout
, this, &DolphinViewContainer::updateStatusBar
);
167 KIO::FileUndoManager
* undoManager
= KIO::FileUndoManager::self();
168 connect(undoManager
, &KIO::FileUndoManager::jobRecordingFinished
,
169 this, &DolphinViewContainer::delayedStatusBarUpdate
);
171 // Initialize filter bar
172 m_filterBar
= new FilterBar(this);
173 m_filterBar
->setVisible(settings
->filterBar());
174 connect(m_filterBar
, &FilterBar::filterChanged
,
175 this, &DolphinViewContainer::setNameFilter
);
176 connect(m_filterBar
, &FilterBar::closeRequest
,
177 this, &DolphinViewContainer::closeFilterBar
);
178 connect(m_filterBar
, &FilterBar::focusViewRequest
,
179 this, &DolphinViewContainer::requestFocus
);
180 connect(m_view
, &DolphinView::urlChanged
,
181 m_filterBar
, &FilterBar::slotUrlChanged
);
183 m_topLayout
->addWidget(m_urlNavigator
);
184 m_topLayout
->addWidget(m_searchBox
);
185 m_topLayout
->addWidget(m_messageWidget
);
186 m_topLayout
->addWidget(m_view
);
187 m_topLayout
->addWidget(m_filterBar
);
188 m_topLayout
->addWidget(m_statusBar
);
190 setSearchModeEnabled(isSearchUrl(url
));
192 // Initialize kactivities resource instance
194 #ifdef KActivities_FOUND
195 m_activityResourceInstance
= new KActivities::ResourceInstance(
196 window()->winId(), url
);
197 m_activityResourceInstance
->setParent(this);
201 DolphinViewContainer::~DolphinViewContainer()
205 QUrl
DolphinViewContainer::url() const
207 return m_view
->url();
210 void DolphinViewContainer::setActive(bool active
)
212 m_searchBox
->setActive(active
);
213 m_urlNavigator
->setActive(active
);
214 m_view
->setActive(active
);
216 #ifdef KActivities_FOUND
218 m_activityResourceInstance
->notifyFocusedIn();
220 m_activityResourceInstance
->notifyFocusedOut();
225 bool DolphinViewContainer::isActive() const
227 Q_ASSERT(m_view
->isActive() == m_urlNavigator
->isActive());
228 return m_view
->isActive();
231 void DolphinViewContainer::setAutoGrabFocus(bool grab
)
233 m_autoGrabFocus
= grab
;
236 bool DolphinViewContainer::autoGrabFocus() const
238 return m_autoGrabFocus
;
241 const DolphinStatusBar
* DolphinViewContainer::statusBar() const
246 DolphinStatusBar
* DolphinViewContainer::statusBar()
251 const KUrlNavigator
* DolphinViewContainer::urlNavigator() const
253 return m_urlNavigator
;
256 KUrlNavigator
* DolphinViewContainer::urlNavigator()
258 return m_urlNavigator
;
261 const DolphinView
* DolphinViewContainer::view() const
266 DolphinView
* DolphinViewContainer::view()
271 void DolphinViewContainer::showMessage(const QString
& msg
, MessageType type
)
277 m_messageWidget
->setText(msg
);
280 case Information
: m_messageWidget
->setMessageType(KMessageWidget::Information
); break;
281 case Warning
: m_messageWidget
->setMessageType(KMessageWidget::Warning
); break;
282 case Error
: m_messageWidget
->setMessageType(KMessageWidget::Error
); break;
288 m_messageWidget
->setWordWrap(false);
289 const int unwrappedWidth
= m_messageWidget
->sizeHint().width();
290 m_messageWidget
->setWordWrap(unwrappedWidth
> size().width());
292 if (m_messageWidget
->isVisible()) {
293 m_messageWidget
->hide();
295 m_messageWidget
->animatedShow();
298 void DolphinViewContainer::readSettings()
300 if (GeneralSettings::modifiedStartupSettings()) {
301 // The startup settings should only get applied if they have been
302 // modified by the user. Otherwise keep the (possibly) different current
303 // settings of the URL navigator and the filterbar.
304 m_urlNavigator
->setUrlEditable(GeneralSettings::editableUrl());
305 m_urlNavigator
->setShowFullPath(GeneralSettings::showFullPath());
306 m_urlNavigator
->setHomeUrl(Dolphin::homeUrl());
307 setFilterBarVisible(GeneralSettings::filterBar());
310 m_view
->readSettings();
311 m_statusBar
->readSettings();
314 bool DolphinViewContainer::isFilterBarVisible() const
316 return m_filterBar
->isVisible();
319 void DolphinViewContainer::setSearchModeEnabled(bool enabled
)
321 if (enabled
== isSearchModeEnabled()) {
322 if (enabled
&& !m_searchBox
->hasFocus()) {
323 m_searchBox
->setFocus();
324 m_searchBox
->selectAll();
329 m_searchBox
->setVisible(enabled
);
330 m_urlNavigator
->setVisible(!enabled
);
333 const QUrl
& locationUrl
= m_urlNavigator
->locationUrl();
334 m_searchBox
->fromSearchUrl(locationUrl
);
336 m_view
->setViewPropertiesContext(QString());
338 // Restore the URL for the URL navigator. If Dolphin has been
339 // started with a search-URL, the home URL is used as fallback.
340 QUrl url
= m_searchBox
->searchPath();
341 if (url
.isEmpty() || !url
.isValid() || isSearchUrl(url
)) {
342 url
= Dolphin::homeUrl();
344 m_urlNavigator
->setLocationUrl(url
);
348 bool DolphinViewContainer::isSearchModeEnabled() const
350 return m_searchBox
->isVisible();
353 QString
DolphinViewContainer::placesText() const
357 if (isSearchModeEnabled()) {
358 text
= m_searchBox
->searchPath().fileName() + QLatin1String(": ") + m_searchBox
->text();
360 text
= url().fileName();
361 if (text
.isEmpty()) {
364 if (text
.isEmpty()) {
365 text
= url().scheme();
372 void DolphinViewContainer::reload()
375 m_messageWidget
->hide();
378 void DolphinViewContainer::setUrl(const QUrl
& newUrl
)
380 if (newUrl
!= m_urlNavigator
->locationUrl()) {
381 m_urlNavigator
->setLocationUrl(newUrl
);
384 #ifdef KActivities_FOUND
385 m_activityResourceInstance
->setUri(newUrl
);
389 void DolphinViewContainer::setFilterBarVisible(bool visible
)
391 Q_ASSERT(m_filterBar
);
394 m_filterBar
->setFocus();
395 m_filterBar
->selectAll();
401 void DolphinViewContainer::delayedStatusBarUpdate()
403 if (m_statusBarTimer
->isActive() && (m_statusBarTimestamp
.elapsed() > 2000)) {
404 // No update of the statusbar has been done during the last 2 seconds,
405 // although an update has been requested. Trigger an immediate update.
406 m_statusBarTimer
->stop();
409 // Invoke updateStatusBar() with a small delay. This assures that
410 // when a lot of delayedStatusBarUpdates() are done in a short time,
411 // no bottleneck is given.
412 m_statusBarTimer
->start();
416 void DolphinViewContainer::updateStatusBar()
418 m_statusBarTimestamp
.start();
420 const QString text
= m_view
->statusBarText();
421 m_statusBar
->setDefaultText(text
);
422 m_statusBar
->resetToDefaultText();
425 void DolphinViewContainer::updateDirectoryLoadingProgress(int percent
)
427 if (m_statusBar
->progressText().isEmpty()) {
428 m_statusBar
->setProgressText(i18nc("@info:progress", "Loading folder..."));
430 m_statusBar
->setProgress(percent
);
433 void DolphinViewContainer::updateDirectorySortingProgress(int percent
)
435 if (m_statusBar
->progressText().isEmpty()) {
436 m_statusBar
->setProgressText(i18nc("@info:progress", "Sorting..."));
438 m_statusBar
->setProgress(percent
);
441 void DolphinViewContainer::slotDirectoryLoadingStarted()
443 if (isSearchUrl(url())) {
444 // Search KIO-slaves usually don't provide any progress information. Give
445 // a hint to the user that a searching is done:
447 m_statusBar
->setProgressText(i18nc("@info", "Searching..."));
448 m_statusBar
->setProgress(-1);
450 // Trigger an undetermined progress indication. The progress
451 // information in percent will be triggered by the percent() signal
452 // of the directory lister later.
453 updateDirectoryLoadingProgress(-1);
457 void DolphinViewContainer::slotDirectoryLoadingCompleted()
459 if (!m_statusBar
->progressText().isEmpty()) {
460 m_statusBar
->setProgressText(QString());
461 m_statusBar
->setProgress(100);
464 if (isSearchUrl(url()) && m_view
->itemsCount() == 0) {
465 // The dir lister has been completed on a Baloo-URI and no items have been found. Instead
466 // of showing the default status bar information ("0 items") a more helpful information is given:
467 m_statusBar
->setText(i18nc("@info:status", "No items found."));
473 void DolphinViewContainer::slotDirectoryLoadingCanceled()
475 if (!m_statusBar
->progressText().isEmpty()) {
476 m_statusBar
->setProgressText(QString());
477 m_statusBar
->setProgress(100);
480 m_statusBar
->setText(QString());
483 void DolphinViewContainer::slotUrlIsFileError(const QUrl
& url
)
485 const KFileItem
item(url
);
487 // Find out if the file can be opened in the view (for example, this is the
488 // case if the file is an archive). The mime type must be known for that.
489 item
.determineMimeType();
490 const QUrl
& folderUrl
= DolphinView::openItemAsFolderUrl(item
, true);
491 if (!folderUrl
.isEmpty()) {
492 m_view
->setUrl(folderUrl
);
494 slotItemActivated(item
);
498 void DolphinViewContainer::slotItemActivated(const KFileItem
& item
)
500 // It is possible to activate items on inactive views by
501 // drag & drop operations. Assure that activating an item always
502 // results in an active view.
503 m_view
->setActive(true);
505 const QUrl
& url
= DolphinView::openItemAsFolderUrl(item
, GeneralSettings::browseThroughArchives());
506 if (!url
.isEmpty()) {
511 KRun
*run
= new KRun(item
.targetUrl(), this);
512 run
->setShowScriptExecutionPrompt(true);
515 void DolphinViewContainer::slotItemsActivated(const KFileItemList
& items
)
517 Q_ASSERT(items
.count() >= 2);
519 KFileItemActions
fileItemActions(this);
520 fileItemActions
.runPreferredApplications(items
, QString());
523 void DolphinViewContainer::showItemInfo(const KFileItem
& item
)
526 m_statusBar
->resetToDefaultText();
528 m_statusBar
->setText(item
.getStatusBarInfo());
532 void DolphinViewContainer::closeFilterBar()
534 m_filterBar
->closeFilterBar();
536 emit
showFilterBarChanged(false);
539 void DolphinViewContainer::setNameFilter(const QString
& nameFilter
)
541 m_view
->setNameFilter(nameFilter
);
542 delayedStatusBarUpdate();
545 void DolphinViewContainer::activate()
550 void DolphinViewContainer::slotViewUrlAboutToBeChanged(const QUrl
& url
)
552 // URL changes of the view can happen in two ways:
553 // 1. The URL navigator gets changed and will trigger the view to update its URL
554 // 2. The URL of the view gets changed and will trigger the URL navigator to update
555 // its URL (e.g. by clicking on an item)
556 // In this scope the view-state may only get saved in case 2:
557 if (url
!= m_urlNavigator
->locationUrl()) {
562 void DolphinViewContainer::slotUrlNavigatorLocationAboutToBeChanged(const QUrl
& url
)
564 // URL changes of the view can happen in two ways:
565 // 1. The URL navigator gets changed and will trigger the view to update its URL
566 // 2. The URL of the view gets changed and will trigger the URL navigator to update
567 // its URL (e.g. by clicking on an item)
568 // In this scope the view-state may only get saved in case 1:
569 if (url
!= m_view
->url()) {
574 void DolphinViewContainer::slotUrlNavigatorLocationChanged(const QUrl
& url
)
578 if (KProtocolManager::supportsListing(url
)) {
579 setSearchModeEnabled(isSearchUrl(url
));
582 if (m_autoGrabFocus
&& isActive() && !isSearchUrl(url
)) {
583 // When an URL has been entered, the view should get the focus.
584 // The focus must be requested asynchronously, as changing the URL might create
585 // a new view widget.
586 QTimer::singleShot(0, this, &DolphinViewContainer::requestFocus
);
588 } else if (KProtocolManager::isSourceProtocol(url
)) {
589 QString app
= QStringLiteral("konqueror");
590 if (url
.scheme().startsWith(QLatin1String("http"))) {
591 showMessage(i18nc("@info:status", // krazy:exclude=qmethods
592 "Dolphin does not support web pages, the web browser has been launched"),
595 const KConfigGroup
config(KSharedConfig::openConfig(QStringLiteral("kdeglobals")), "General");
596 const QString browser
= config
.readEntry("BrowserApplication");
597 if (!browser
.isEmpty()) {
599 if (app
.startsWith('!')) {
600 // a literal command has been configured, remove the '!' prefix
605 showMessage(i18nc("@info:status",
606 "Protocol not supported by Dolphin, Konqueror has been launched"),
610 const QString secureUrl
= KShell::quoteArg(url
.toDisplayString(QUrl::PreferLocalFile
));
611 const QString command
= app
+ ' ' + secureUrl
;
612 KRun::runCommand(command
, app
, app
, this);
614 showMessage(i18nc("@info:status", "Invalid protocol"), Error
);
618 void DolphinViewContainer::redirect(const QUrl
& oldUrl
, const QUrl
& newUrl
)
621 const bool block
= m_urlNavigator
->signalsBlocked();
622 m_urlNavigator
->blockSignals(true);
624 // Assure that the location state is reset for redirection URLs. This
625 // allows to skip redirection URLs when going back or forward in the
627 m_urlNavigator
->saveLocationState(QByteArray());
628 m_urlNavigator
->setLocationUrl(newUrl
);
629 setSearchModeEnabled(isSearchUrl(newUrl
));
631 m_urlNavigator
->blockSignals(block
);
634 void DolphinViewContainer::requestFocus()
639 void DolphinViewContainer::saveUrlCompletionMode(KCompletion::CompletionMode completion
)
641 GeneralSettings::setUrlCompletionMode(completion
);
644 void DolphinViewContainer::slotHistoryChanged()
646 QByteArray locationState
= m_urlNavigator
->locationState();
647 if (!locationState
.isEmpty()) {
648 QDataStream
stream(&locationState
, QIODevice::ReadOnly
);
649 m_view
->restoreState(stream
);
653 void DolphinViewContainer::slotReturnPressed()
655 if (!GeneralSettings::editableUrl()) {
656 m_urlNavigator
->setUrlEditable(false);
660 void DolphinViewContainer::startSearching()
662 const QUrl url
= m_searchBox
->urlForSearching();
663 if (url
.isValid() && !url
.isEmpty()) {
664 m_view
->setViewPropertiesContext(QStringLiteral("search"));
665 m_urlNavigator
->setLocationUrl(url
);
669 void DolphinViewContainer::closeSearchBox()
671 setSearchModeEnabled(false);
674 void DolphinViewContainer::stopDirectoryLoading()
676 m_view
->stopLoading();
677 m_statusBar
->setProgress(100);
680 void DolphinViewContainer::slotStatusBarZoomLevelChanged(int zoomLevel
)
682 m_view
->setZoomLevel(zoomLevel
);
685 void DolphinViewContainer::showErrorMessage(const QString
& msg
)
687 showMessage(msg
, Error
);
690 bool DolphinViewContainer::isSearchUrl(const QUrl
& url
) const
692 return url
.scheme().contains(QStringLiteral("search"));
695 void DolphinViewContainer::saveViewState()
697 QByteArray locationState
;
698 QDataStream
stream(&locationState
, QIODevice::WriteOnly
);
699 m_view
->saveState(stream
);
700 m_urlNavigator
->saveLocationState(locationState
);