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::directoryLoadingCompleted
,
102 m_messageWidget
, &KMessageWidget::hide
);
103 connect(m_view
, &DolphinView::writeStateChanged
,
104 this, &DolphinViewContainer::writeStateChanged
);
105 connect(m_view
, &DolphinView::requestItemInfo
,
106 this, &DolphinViewContainer::showItemInfo
);
107 connect(m_view
, &DolphinView::itemActivated
,
108 this, &DolphinViewContainer::slotItemActivated
);
109 connect(m_view
, &DolphinView::itemsActivated
,
110 this, &DolphinViewContainer::slotItemsActivated
);
111 connect(m_view
, &DolphinView::redirection
,
112 this, &DolphinViewContainer::redirect
);
113 connect(m_view
, &DolphinView::directoryLoadingStarted
,
114 this, &DolphinViewContainer::slotDirectoryLoadingStarted
);
115 connect(m_view
, &DolphinView::directoryLoadingCompleted
,
116 this, &DolphinViewContainer::slotDirectoryLoadingCompleted
);
117 connect(m_view
, &DolphinView::directoryLoadingCanceled
,
118 this, &DolphinViewContainer::slotDirectoryLoadingCanceled
);
119 connect(m_view
, &DolphinView::itemCountChanged
,
120 this, &DolphinViewContainer::delayedStatusBarUpdate
);
121 connect(m_view
, &DolphinView::directoryLoadingProgress
,
122 this, &DolphinViewContainer::updateDirectoryLoadingProgress
);
123 connect(m_view
, &DolphinView::directorySortingProgress
,
124 this, &DolphinViewContainer::updateDirectorySortingProgress
);
125 connect(m_view
, &DolphinView::selectionChanged
,
126 this, &DolphinViewContainer::delayedStatusBarUpdate
);
127 connect(m_view
, &DolphinView::urlAboutToBeChanged
,
128 this, &DolphinViewContainer::slotViewUrlAboutToBeChanged
);
129 connect(m_view
, &DolphinView::errorMessage
,
130 this, &DolphinViewContainer::showErrorMessage
);
131 connect(m_view
, &DolphinView::urlIsFileError
,
132 this, &DolphinViewContainer::slotUrlIsFileError
);
133 connect(m_view
, &DolphinView::activated
,
134 this, &DolphinViewContainer::activate
);
136 connect(m_urlNavigator
, &KUrlNavigator::urlAboutToBeChanged
,
137 this, &DolphinViewContainer::slotUrlNavigatorLocationAboutToBeChanged
);
138 connect(m_urlNavigator
, &KUrlNavigator::urlChanged
,
139 this, &DolphinViewContainer::slotUrlNavigatorLocationChanged
);
140 connect(m_urlNavigator
, &KUrlNavigator::historyChanged
,
141 this, &DolphinViewContainer::slotHistoryChanged
);
142 connect(m_urlNavigator
, &KUrlNavigator::returnPressed
,
143 this, &DolphinViewContainer::slotReturnPressed
);
144 connect(m_urlNavigator
, &KUrlNavigator::urlsDropped
,
145 m_view
, &DolphinView::dropUrls
);
147 // Initialize status bar
148 m_statusBar
= new DolphinStatusBar(this);
149 m_statusBar
->setUrl(m_view
->url());
150 m_statusBar
->setZoomLevel(m_view
->zoomLevel());
151 connect(m_view
, &DolphinView::urlChanged
,
152 m_statusBar
, &DolphinStatusBar::setUrl
);
153 connect(m_view
, &DolphinView::zoomLevelChanged
,
154 m_statusBar
, &DolphinStatusBar::setZoomLevel
);
155 connect(m_view
, &DolphinView::infoMessage
,
156 m_statusBar
, &DolphinStatusBar::setText
);
157 connect(m_view
, &DolphinView::operationCompletedMessage
,
158 m_statusBar
, &DolphinStatusBar::setText
);
159 connect(m_statusBar
, &DolphinStatusBar::stopPressed
,
160 this, &DolphinViewContainer::stopDirectoryLoading
);
161 connect(m_statusBar
, &DolphinStatusBar::zoomLevelChanged
,
162 this, &DolphinViewContainer::slotStatusBarZoomLevelChanged
);
164 m_statusBarTimer
= new QTimer(this);
165 m_statusBarTimer
->setSingleShot(true);
166 m_statusBarTimer
->setInterval(300);
167 connect(m_statusBarTimer
, &QTimer::timeout
, this, &DolphinViewContainer::updateStatusBar
);
169 KIO::FileUndoManager
* undoManager
= KIO::FileUndoManager::self();
170 connect(undoManager
, &KIO::FileUndoManager::jobRecordingFinished
,
171 this, &DolphinViewContainer::delayedStatusBarUpdate
);
173 // Initialize filter bar
174 m_filterBar
= new FilterBar(this);
175 m_filterBar
->setVisible(settings
->filterBar());
176 connect(m_filterBar
, &FilterBar::filterChanged
,
177 this, &DolphinViewContainer::setNameFilter
);
178 connect(m_filterBar
, &FilterBar::closeRequest
,
179 this, &DolphinViewContainer::closeFilterBar
);
180 connect(m_filterBar
, &FilterBar::focusViewRequest
,
181 this, &DolphinViewContainer::requestFocus
);
182 connect(m_view
, &DolphinView::urlChanged
,
183 m_filterBar
, &FilterBar::slotUrlChanged
);
185 m_topLayout
->addWidget(m_urlNavigator
);
186 m_topLayout
->addWidget(m_searchBox
);
187 m_topLayout
->addWidget(m_messageWidget
);
188 m_topLayout
->addWidget(m_view
);
189 m_topLayout
->addWidget(m_filterBar
);
190 m_topLayout
->addWidget(m_statusBar
);
192 setSearchModeEnabled(isSearchUrl(url
));
194 // Initialize kactivities resource instance
196 #ifdef KActivities_FOUND
197 m_activityResourceInstance
= new KActivities::ResourceInstance(
198 window()->winId(), url
);
199 m_activityResourceInstance
->setParent(this);
203 DolphinViewContainer::~DolphinViewContainer()
207 QUrl
DolphinViewContainer::url() const
209 return m_view
->url();
212 void DolphinViewContainer::setActive(bool active
)
214 m_searchBox
->setActive(active
);
215 m_urlNavigator
->setActive(active
);
216 m_view
->setActive(active
);
218 #ifdef KActivities_FOUND
220 m_activityResourceInstance
->notifyFocusedIn();
222 m_activityResourceInstance
->notifyFocusedOut();
227 bool DolphinViewContainer::isActive() const
229 Q_ASSERT(m_view
->isActive() == m_urlNavigator
->isActive());
230 return m_view
->isActive();
233 void DolphinViewContainer::setAutoGrabFocus(bool grab
)
235 m_autoGrabFocus
= grab
;
238 bool DolphinViewContainer::autoGrabFocus() const
240 return m_autoGrabFocus
;
243 const DolphinStatusBar
* DolphinViewContainer::statusBar() const
248 DolphinStatusBar
* DolphinViewContainer::statusBar()
253 const KUrlNavigator
* DolphinViewContainer::urlNavigator() const
255 return m_urlNavigator
;
258 KUrlNavigator
* DolphinViewContainer::urlNavigator()
260 return m_urlNavigator
;
263 const DolphinView
* DolphinViewContainer::view() const
268 DolphinView
* DolphinViewContainer::view()
273 void DolphinViewContainer::showMessage(const QString
& msg
, MessageType type
)
279 m_messageWidget
->setText(msg
);
282 case Information
: m_messageWidget
->setMessageType(KMessageWidget::Information
); break;
283 case Warning
: m_messageWidget
->setMessageType(KMessageWidget::Warning
); break;
284 case Error
: m_messageWidget
->setMessageType(KMessageWidget::Error
); break;
290 m_messageWidget
->setWordWrap(false);
291 const int unwrappedWidth
= m_messageWidget
->sizeHint().width();
292 m_messageWidget
->setWordWrap(unwrappedWidth
> size().width());
294 if (m_messageWidget
->isVisible()) {
295 m_messageWidget
->hide();
297 m_messageWidget
->animatedShow();
300 void DolphinViewContainer::readSettings()
302 if (GeneralSettings::modifiedStartupSettings()) {
303 // The startup settings should only get applied if they have been
304 // modified by the user. Otherwise keep the (possibly) different current
305 // settings of the URL navigator and the filterbar.
306 m_urlNavigator
->setUrlEditable(GeneralSettings::editableUrl());
307 m_urlNavigator
->setShowFullPath(GeneralSettings::showFullPath());
308 m_urlNavigator
->setHomeUrl(Dolphin::homeUrl());
309 setFilterBarVisible(GeneralSettings::filterBar());
312 m_view
->readSettings();
313 m_statusBar
->readSettings();
316 bool DolphinViewContainer::isFilterBarVisible() const
318 return m_filterBar
->isVisible();
321 void DolphinViewContainer::setSearchModeEnabled(bool enabled
)
323 if (enabled
== isSearchModeEnabled()) {
324 if (enabled
&& !m_searchBox
->hasFocus()) {
325 m_searchBox
->setFocus();
326 m_searchBox
->selectAll();
331 m_searchBox
->setVisible(enabled
);
332 m_urlNavigator
->setVisible(!enabled
);
335 const QUrl
& locationUrl
= m_urlNavigator
->locationUrl();
336 m_searchBox
->fromSearchUrl(locationUrl
);
338 m_view
->setViewPropertiesContext(QString());
340 // Restore the URL for the URL navigator. If Dolphin has been
341 // started with a search-URL, the home URL is used as fallback.
342 QUrl url
= m_searchBox
->searchPath();
343 if (url
.isEmpty() || !url
.isValid() || isSearchUrl(url
)) {
344 url
= Dolphin::homeUrl();
346 m_urlNavigator
->setLocationUrl(url
);
350 bool DolphinViewContainer::isSearchModeEnabled() const
352 return m_searchBox
->isVisible();
355 QString
DolphinViewContainer::placesText() const
359 if (isSearchModeEnabled()) {
360 text
= m_searchBox
->searchPath().fileName() + QLatin1String(": ") + m_searchBox
->text();
362 text
= url().fileName();
363 if (text
.isEmpty()) {
371 void DolphinViewContainer::setUrl(const QUrl
& newUrl
)
373 if (newUrl
!= m_urlNavigator
->locationUrl()) {
374 m_urlNavigator
->setLocationUrl(newUrl
);
377 #ifdef KActivities_FOUND
378 m_activityResourceInstance
->setUri(newUrl
);
382 void DolphinViewContainer::setFilterBarVisible(bool visible
)
384 Q_ASSERT(m_filterBar
);
387 m_filterBar
->setFocus();
388 m_filterBar
->selectAll();
394 void DolphinViewContainer::delayedStatusBarUpdate()
396 if (m_statusBarTimer
->isActive() && (m_statusBarTimestamp
.elapsed() > 2000)) {
397 // No update of the statusbar has been done during the last 2 seconds,
398 // although an update has been requested. Trigger an immediate update.
399 m_statusBarTimer
->stop();
402 // Invoke updateStatusBar() with a small delay. This assures that
403 // when a lot of delayedStatusBarUpdates() are done in a short time,
404 // no bottleneck is given.
405 m_statusBarTimer
->start();
409 void DolphinViewContainer::updateStatusBar()
411 m_statusBarTimestamp
.start();
413 const QString text
= m_view
->statusBarText();
414 m_statusBar
->setDefaultText(text
);
415 m_statusBar
->resetToDefaultText();
418 void DolphinViewContainer::updateDirectoryLoadingProgress(int percent
)
420 if (m_statusBar
->progressText().isEmpty()) {
421 m_statusBar
->setProgressText(i18nc("@info:progress", "Loading folder..."));
423 m_statusBar
->setProgress(percent
);
426 void DolphinViewContainer::updateDirectorySortingProgress(int percent
)
428 if (m_statusBar
->progressText().isEmpty()) {
429 m_statusBar
->setProgressText(i18nc("@info:progress", "Sorting..."));
431 m_statusBar
->setProgress(percent
);
434 void DolphinViewContainer::slotDirectoryLoadingStarted()
436 if (isSearchUrl(url())) {
437 // Search KIO-slaves usually don't provide any progress information. Give
438 // a hint to the user that a searching is done:
440 m_statusBar
->setProgressText(i18nc("@info", "Searching..."));
441 m_statusBar
->setProgress(-1);
443 // Trigger an undetermined progress indication. The progress
444 // information in percent will be triggered by the percent() signal
445 // of the directory lister later.
446 updateDirectoryLoadingProgress(-1);
450 void DolphinViewContainer::slotDirectoryLoadingCompleted()
452 if (!m_statusBar
->progressText().isEmpty()) {
453 m_statusBar
->setProgressText(QString());
454 m_statusBar
->setProgress(100);
457 if (isSearchUrl(url()) && m_view
->itemsCount() == 0) {
458 // The dir lister has been completed on a Baloo-URI and no items have been found. Instead
459 // of showing the default status bar information ("0 items") a more helpful information is given:
460 m_statusBar
->setText(i18nc("@info:status", "No items found."));
466 void DolphinViewContainer::slotDirectoryLoadingCanceled()
468 if (!m_statusBar
->progressText().isEmpty()) {
469 m_statusBar
->setProgressText(QString());
470 m_statusBar
->setProgress(100);
473 m_statusBar
->setText(QString());
476 void DolphinViewContainer::slotUrlIsFileError(const QUrl
& url
)
478 const KFileItem
item(url
);
480 // Find out if the file can be opened in the view (for example, this is the
481 // case if the file is an archive). The mime type must be known for that.
482 item
.determineMimeType();
483 const QUrl
& folderUrl
= DolphinView::openItemAsFolderUrl(item
, true);
484 if (!folderUrl
.isEmpty()) {
485 m_view
->setUrl(folderUrl
);
487 slotItemActivated(item
);
491 void DolphinViewContainer::slotItemActivated(const KFileItem
& item
)
493 // It is possible to activate items on inactive views by
494 // drag & drop operations. Assure that activating an item always
495 // results in an active view.
496 m_view
->setActive(true);
498 const QUrl
& url
= DolphinView::openItemAsFolderUrl(item
, GeneralSettings::browseThroughArchives());
499 if (!url
.isEmpty()) {
504 KRun
*run
= new KRun(item
.targetUrl(), this);
505 run
->setShowScriptExecutionPrompt(true);
508 void DolphinViewContainer::slotItemsActivated(const KFileItemList
& items
)
510 Q_ASSERT(items
.count() >= 2);
512 KFileItemActions
fileItemActions(this);
513 fileItemActions
.runPreferredApplications(items
, QString());
516 void DolphinViewContainer::showItemInfo(const KFileItem
& item
)
519 m_statusBar
->resetToDefaultText();
521 m_statusBar
->setText(item
.getStatusBarInfo());
525 void DolphinViewContainer::closeFilterBar()
527 m_filterBar
->closeFilterBar();
529 emit
showFilterBarChanged(false);
532 void DolphinViewContainer::setNameFilter(const QString
& nameFilter
)
534 m_view
->setNameFilter(nameFilter
);
535 delayedStatusBarUpdate();
538 void DolphinViewContainer::activate()
543 void DolphinViewContainer::slotViewUrlAboutToBeChanged(const QUrl
& url
)
545 // URL changes of the view can happen in two ways:
546 // 1. The URL navigator gets changed and will trigger the view to update its URL
547 // 2. The URL of the view gets changed and will trigger the URL navigator to update
548 // its URL (e.g. by clicking on an item)
549 // In this scope the view-state may only get saved in case 2:
550 if (url
!= m_urlNavigator
->locationUrl()) {
555 void DolphinViewContainer::slotUrlNavigatorLocationAboutToBeChanged(const QUrl
& url
)
557 // URL changes of the view can happen in two ways:
558 // 1. The URL navigator gets changed and will trigger the view to update its URL
559 // 2. The URL of the view gets changed and will trigger the URL navigator to update
560 // its URL (e.g. by clicking on an item)
561 // In this scope the view-state may only get saved in case 1:
562 if (url
!= m_view
->url()) {
567 void DolphinViewContainer::slotUrlNavigatorLocationChanged(const QUrl
& url
)
571 if (KProtocolManager::supportsListing(url
)) {
572 setSearchModeEnabled(isSearchUrl(url
));
575 if (m_autoGrabFocus
&& isActive() && !isSearchUrl(url
)) {
576 // When an URL has been entered, the view should get the focus.
577 // The focus must be requested asynchronously, as changing the URL might create
578 // a new view widget.
579 QTimer::singleShot(0, this, &DolphinViewContainer::requestFocus
);
581 } else if (KProtocolManager::isSourceProtocol(url
)) {
582 QString app
= QStringLiteral("konqueror");
583 if (url
.scheme().startsWith(QLatin1String("http"))) {
584 showMessage(i18nc("@info:status", // krazy:exclude=qmethods
585 "Dolphin does not support web pages, the web browser has been launched"),
588 const KConfigGroup
config(KSharedConfig::openConfig(QStringLiteral("kdeglobals")), "General");
589 const QString browser
= config
.readEntry("BrowserApplication");
590 if (!browser
.isEmpty()) {
592 if (app
.startsWith('!')) {
593 // a literal command has been configured, remove the '!' prefix
598 showMessage(i18nc("@info:status",
599 "Protocol not supported by Dolphin, Konqueror has been launched"),
603 const QString secureUrl
= KShell::quoteArg(url
.toDisplayString(QUrl::PreferLocalFile
));
604 const QString command
= app
+ ' ' + secureUrl
;
605 KRun::runCommand(command
, app
, app
, this);
607 showMessage(i18nc("@info:status", "Invalid protocol"), Error
);
611 void DolphinViewContainer::redirect(const QUrl
& oldUrl
, const QUrl
& newUrl
)
614 const bool block
= m_urlNavigator
->signalsBlocked();
615 m_urlNavigator
->blockSignals(true);
617 // Assure that the location state is reset for redirection URLs. This
618 // allows to skip redirection URLs when going back or forward in the
620 m_urlNavigator
->saveLocationState(QByteArray());
621 m_urlNavigator
->setLocationUrl(newUrl
);
622 setSearchModeEnabled(isSearchUrl(newUrl
));
624 m_urlNavigator
->blockSignals(block
);
627 void DolphinViewContainer::requestFocus()
632 void DolphinViewContainer::saveUrlCompletionMode(KCompletion::CompletionMode completion
)
634 GeneralSettings::setUrlCompletionMode(completion
);
637 void DolphinViewContainer::slotHistoryChanged()
639 QByteArray locationState
= m_urlNavigator
->locationState();
640 if (!locationState
.isEmpty()) {
641 QDataStream
stream(&locationState
, QIODevice::ReadOnly
);
642 m_view
->restoreState(stream
);
646 void DolphinViewContainer::slotReturnPressed()
648 if (!GeneralSettings::editableUrl()) {
649 m_urlNavigator
->setUrlEditable(false);
653 void DolphinViewContainer::startSearching()
655 const QUrl url
= m_searchBox
->urlForSearching();
656 if (url
.isValid() && !url
.isEmpty()) {
657 m_view
->setViewPropertiesContext(QStringLiteral("search"));
658 m_urlNavigator
->setLocationUrl(url
);
662 void DolphinViewContainer::closeSearchBox()
664 setSearchModeEnabled(false);
667 void DolphinViewContainer::stopDirectoryLoading()
669 m_view
->stopLoading();
670 m_statusBar
->setProgress(100);
673 void DolphinViewContainer::slotStatusBarZoomLevelChanged(int zoomLevel
)
675 m_view
->setZoomLevel(zoomLevel
);
678 void DolphinViewContainer::showErrorMessage(const QString
& msg
)
680 showMessage(msg
, Error
);
683 bool DolphinViewContainer::isSearchUrl(const QUrl
& url
) const
685 return url
.scheme().contains(QStringLiteral("search"));
688 void DolphinViewContainer::saveViewState()
690 QByteArray locationState
;
691 QDataStream
stream(&locationState
, QIODevice::WriteOnly
);
692 m_view
->saveState(stream
);
693 m_urlNavigator
->saveLocationState(locationState
);