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
42 #include "dolphin_generalsettings.h"
43 #include "filterbar/filterbar.h"
44 #include "search/dolphinsearchbox.h"
45 #include "statusbar/dolphinstatusbar.h"
46 #include "views/viewmodecontroller.h"
47 #include "views/viewproperties.h"
49 DolphinViewContainer::DolphinViewContainer(const QUrl
& url
, QWidget
* parent
) :
59 m_statusBarTimestamp(),
61 #ifdef KActivities_FOUND
62 , m_activityResourceInstance(0)
67 m_topLayout
= new QVBoxLayout(this);
68 m_topLayout
->setSpacing(0);
69 m_topLayout
->setMargin(0);
71 m_urlNavigator
= new KUrlNavigator(new KFilePlacesModel(this), url
, this);
72 connect(m_urlNavigator
, &KUrlNavigator::activated
,
73 this, &DolphinViewContainer::activate
);
74 connect(m_urlNavigator
->editor(), &KUrlComboBox::completionModeChanged
,
75 this, &DolphinViewContainer::saveUrlCompletionMode
);
77 const GeneralSettings
* settings
= GeneralSettings::self();
78 m_urlNavigator
->setUrlEditable(settings
->editableUrl());
79 m_urlNavigator
->setShowFullPath(settings
->showFullPath());
80 m_urlNavigator
->setHomeUrl(QUrl::fromLocalFile(settings
->homeUrl()));
81 KUrlComboBox
* editor
= m_urlNavigator
->editor();
82 editor
->setCompletionMode(KCompletion::CompletionMode(settings
->urlCompletionMode()));
84 m_searchBox
= new DolphinSearchBox(this);
86 connect(m_searchBox
, &DolphinSearchBox::activated
, this, &DolphinViewContainer::activate
);
87 connect(m_searchBox
, &DolphinSearchBox::closeRequest
, this, &DolphinViewContainer::closeSearchBox
);
88 connect(m_searchBox
, &DolphinSearchBox::searchRequest
, this, &DolphinViewContainer::startSearching
);
89 connect(m_searchBox
, &DolphinSearchBox::returnPressed
, this, &DolphinViewContainer::requestFocus
);
91 m_messageWidget
= new KMessageWidget(this);
92 m_messageWidget
->setCloseButtonVisible(true);
93 m_messageWidget
->hide();
95 m_view
= new DolphinView(url
, this);
96 connect(m_view
, &DolphinView::urlChanged
,
97 m_urlNavigator
, &KUrlNavigator::setUrl
);
98 connect(m_view
, &DolphinView::urlChanged
,
99 m_messageWidget
, &KMessageWidget::hide
);
100 connect(m_view
, &DolphinView::directoryLoadingCompleted
,
101 m_messageWidget
, &KMessageWidget::hide
);
102 connect(m_view
, &DolphinView::writeStateChanged
,
103 this, &DolphinViewContainer::writeStateChanged
);
104 connect(m_view
, &DolphinView::requestItemInfo
,
105 this, &DolphinViewContainer::showItemInfo
);
106 connect(m_view
, &DolphinView::itemActivated
,
107 this, &DolphinViewContainer::slotItemActivated
);
108 connect(m_view
, &DolphinView::itemsActivated
,
109 this, &DolphinViewContainer::slotItemsActivated
);
110 connect(m_view
, &DolphinView::redirection
,
111 this, &DolphinViewContainer::redirect
);
112 connect(m_view
, &DolphinView::directoryLoadingStarted
,
113 this, &DolphinViewContainer::slotDirectoryLoadingStarted
);
114 connect(m_view
, &DolphinView::directoryLoadingCompleted
,
115 this, &DolphinViewContainer::slotDirectoryLoadingCompleted
);
116 connect(m_view
, &DolphinView::directoryLoadingCanceled
,
117 this, &DolphinViewContainer::slotDirectoryLoadingCanceled
);
118 connect(m_view
, &DolphinView::itemCountChanged
,
119 this, &DolphinViewContainer::delayedStatusBarUpdate
);
120 connect(m_view
, &DolphinView::directoryLoadingProgress
,
121 this, &DolphinViewContainer::updateDirectoryLoadingProgress
);
122 connect(m_view
, &DolphinView::directorySortingProgress
,
123 this, &DolphinViewContainer::updateDirectorySortingProgress
);
124 connect(m_view
, &DolphinView::selectionChanged
,
125 this, &DolphinViewContainer::delayedStatusBarUpdate
);
126 connect(m_view
, &DolphinView::urlAboutToBeChanged
,
127 this, &DolphinViewContainer::slotViewUrlAboutToBeChanged
);
128 connect(m_view
, &DolphinView::errorMessage
,
129 this, &DolphinViewContainer::showErrorMessage
);
130 connect(m_view
, &DolphinView::urlIsFileError
,
131 this, &DolphinViewContainer::slotUrlIsFileError
);
132 connect(m_view
, &DolphinView::activated
,
133 this, &DolphinViewContainer::activate
);
135 connect(m_urlNavigator
, &KUrlNavigator::urlAboutToBeChanged
,
136 this, &DolphinViewContainer::slotUrlNavigatorLocationAboutToBeChanged
);
137 connect(m_urlNavigator
, &KUrlNavigator::urlChanged
,
138 this, &DolphinViewContainer::slotUrlNavigatorLocationChanged
);
139 connect(m_urlNavigator
, &KUrlNavigator::historyChanged
,
140 this, &DolphinViewContainer::slotHistoryChanged
);
141 connect(m_urlNavigator
, &KUrlNavigator::returnPressed
,
142 this, &DolphinViewContainer::slotReturnPressed
);
143 connect(m_urlNavigator
, &KUrlNavigator::urlsDropped
,
144 m_view
, &DolphinView::dropUrls
);
146 // Initialize status bar
147 m_statusBar
= new DolphinStatusBar(this);
148 m_statusBar
->setUrl(m_view
->url());
149 m_statusBar
->setZoomLevel(m_view
->zoomLevel());
150 connect(m_view
, &DolphinView::urlChanged
,
151 m_statusBar
, &DolphinStatusBar::setUrl
);
152 connect(m_view
, &DolphinView::zoomLevelChanged
,
153 m_statusBar
, &DolphinStatusBar::setZoomLevel
);
154 connect(m_view
, &DolphinView::infoMessage
,
155 m_statusBar
, &DolphinStatusBar::setText
);
156 connect(m_view
, &DolphinView::operationCompletedMessage
,
157 m_statusBar
, &DolphinStatusBar::setText
);
158 connect(m_statusBar
, &DolphinStatusBar::stopPressed
,
159 this, &DolphinViewContainer::stopDirectoryLoading
);
160 connect(m_statusBar
, &DolphinStatusBar::zoomLevelChanged
,
161 this, &DolphinViewContainer::slotStatusBarZoomLevelChanged
);
163 m_statusBarTimer
= new QTimer(this);
164 m_statusBarTimer
->setSingleShot(true);
165 m_statusBarTimer
->setInterval(300);
166 connect(m_statusBarTimer
, &QTimer::timeout
, this, &DolphinViewContainer::updateStatusBar
);
168 KIO::FileUndoManager
* undoManager
= KIO::FileUndoManager::self();
169 connect(undoManager
, &KIO::FileUndoManager::jobRecordingFinished
,
170 this, &DolphinViewContainer::delayedStatusBarUpdate
);
172 // Initialize filter bar
173 m_filterBar
= new FilterBar(this);
174 m_filterBar
->setVisible(settings
->filterBar());
175 connect(m_filterBar
, &FilterBar::filterChanged
,
176 this, &DolphinViewContainer::setNameFilter
);
177 connect(m_filterBar
, &FilterBar::closeRequest
,
178 this, &DolphinViewContainer::closeFilterBar
);
179 connect(m_filterBar
, &FilterBar::focusViewRequest
,
180 this, &DolphinViewContainer::requestFocus
);
181 connect(m_view
, &DolphinView::urlChanged
,
182 m_filterBar
, &FilterBar::slotUrlChanged
);
184 m_topLayout
->addWidget(m_urlNavigator
);
185 m_topLayout
->addWidget(m_searchBox
);
186 m_topLayout
->addWidget(m_messageWidget
);
187 m_topLayout
->addWidget(m_view
);
188 m_topLayout
->addWidget(m_filterBar
);
189 m_topLayout
->addWidget(m_statusBar
);
191 setSearchModeEnabled(isSearchUrl(url
));
193 // Initialize kactivities resource instance
195 #ifdef KActivities_FOUND
196 m_activityResourceInstance
= new KActivities::ResourceInstance(
197 window()->winId(), url
);
198 m_activityResourceInstance
->setParent(this);
202 DolphinViewContainer::~DolphinViewContainer()
206 QUrl
DolphinViewContainer::url() const
208 return m_view
->url();
211 void DolphinViewContainer::setActive(bool active
)
213 m_searchBox
->setActive(active
);
214 m_urlNavigator
->setActive(active
);
215 m_view
->setActive(active
);
217 #ifdef KActivities_FOUND
219 m_activityResourceInstance
->notifyFocusedIn();
221 m_activityResourceInstance
->notifyFocusedOut();
226 bool DolphinViewContainer::isActive() const
228 Q_ASSERT(m_view
->isActive() == m_urlNavigator
->isActive());
229 return m_view
->isActive();
232 void DolphinViewContainer::setAutoGrabFocus(bool grab
)
234 m_autoGrabFocus
= grab
;
237 bool DolphinViewContainer::autoGrabFocus() const
239 return m_autoGrabFocus
;
242 const DolphinStatusBar
* DolphinViewContainer::statusBar() const
247 DolphinStatusBar
* DolphinViewContainer::statusBar()
252 const KUrlNavigator
* DolphinViewContainer::urlNavigator() const
254 return m_urlNavigator
;
257 KUrlNavigator
* DolphinViewContainer::urlNavigator()
259 return m_urlNavigator
;
262 const DolphinView
* DolphinViewContainer::view() const
267 DolphinView
* DolphinViewContainer::view()
272 void DolphinViewContainer::showMessage(const QString
& msg
, MessageType type
)
278 m_messageWidget
->setText(msg
);
281 case Information
: m_messageWidget
->setMessageType(KMessageWidget::Information
); break;
282 case Warning
: m_messageWidget
->setMessageType(KMessageWidget::Warning
); break;
283 case Error
: m_messageWidget
->setMessageType(KMessageWidget::Error
); break;
289 m_messageWidget
->setWordWrap(false);
290 const int unwrappedWidth
= m_messageWidget
->sizeHint().width();
291 m_messageWidget
->setWordWrap(unwrappedWidth
> size().width());
293 if (m_messageWidget
->isVisible()) {
294 m_messageWidget
->hide();
296 m_messageWidget
->animatedShow();
299 void DolphinViewContainer::readSettings()
301 if (GeneralSettings::modifiedStartupSettings()) {
302 // The startup settings should only get applied if they have been
303 // modified by the user. Otherwise keep the (possibly) different current
304 // settings of the URL navigator and the filterbar.
305 m_urlNavigator
->setUrlEditable(GeneralSettings::editableUrl());
306 m_urlNavigator
->setShowFullPath(GeneralSettings::showFullPath());
307 m_urlNavigator
->setHomeUrl(QUrl::fromLocalFile(GeneralSettings::homeUrl()));
308 setFilterBarVisible(GeneralSettings::filterBar());
311 m_view
->readSettings();
312 m_statusBar
->readSettings();
315 bool DolphinViewContainer::isFilterBarVisible() const
317 return m_filterBar
->isVisible();
320 void DolphinViewContainer::setSearchModeEnabled(bool enabled
)
322 if (enabled
== isSearchModeEnabled()) {
323 if (enabled
&& !m_searchBox
->hasFocus()) {
324 m_searchBox
->setFocus();
325 m_searchBox
->selectAll();
330 m_searchBox
->setVisible(enabled
);
331 m_urlNavigator
->setVisible(!enabled
);
334 const QUrl
& locationUrl
= m_urlNavigator
->locationUrl();
335 m_searchBox
->fromSearchUrl(locationUrl
);
337 m_view
->setViewPropertiesContext(QString());
339 // Restore the URL for the URL navigator. If Dolphin has been
340 // started with a search-URL, the home URL is used as fallback.
341 QUrl url
= m_searchBox
->searchPath();
342 if (url
.isEmpty() || !url
.isValid() || isSearchUrl(url
)) {
343 url
= QUrl::fromLocalFile(GeneralSettings::self()->homeUrl());
345 m_urlNavigator
->setLocationUrl(url
);
349 bool DolphinViewContainer::isSearchModeEnabled() const
351 return m_searchBox
->isVisible();
354 QString
DolphinViewContainer::placesText() const
358 if (isSearchModeEnabled()) {
359 text
= m_searchBox
->searchPath().fileName() + QLatin1String(": ") + m_searchBox
->text();
361 text
= url().fileName();
362 if (text
.isEmpty()) {
370 void DolphinViewContainer::setUrl(const QUrl
& newUrl
)
372 if (newUrl
!= m_urlNavigator
->locationUrl()) {
373 m_urlNavigator
->setLocationUrl(newUrl
);
376 #ifdef KActivities_FOUND
377 m_activityResourceInstance
->setUri(newUrl
);
381 void DolphinViewContainer::setFilterBarVisible(bool visible
)
383 Q_ASSERT(m_filterBar
);
386 m_filterBar
->setFocus();
387 m_filterBar
->selectAll();
393 void DolphinViewContainer::delayedStatusBarUpdate()
395 if (m_statusBarTimer
->isActive() && (m_statusBarTimestamp
.elapsed() > 2000)) {
396 // No update of the statusbar has been done during the last 2 seconds,
397 // although an update has been requested. Trigger an immediate update.
398 m_statusBarTimer
->stop();
401 // Invoke updateStatusBar() with a small delay. This assures that
402 // when a lot of delayedStatusBarUpdates() are done in a short time,
403 // no bottleneck is given.
404 m_statusBarTimer
->start();
408 void DolphinViewContainer::updateStatusBar()
410 m_statusBarTimestamp
.start();
412 const QString text
= m_view
->statusBarText();
413 m_statusBar
->setDefaultText(text
);
414 m_statusBar
->resetToDefaultText();
417 void DolphinViewContainer::updateDirectoryLoadingProgress(int percent
)
419 if (m_statusBar
->progressText().isEmpty()) {
420 m_statusBar
->setProgressText(i18nc("@info:progress", "Loading folder..."));
422 m_statusBar
->setProgress(percent
);
425 void DolphinViewContainer::updateDirectorySortingProgress(int percent
)
427 if (m_statusBar
->progressText().isEmpty()) {
428 m_statusBar
->setProgressText(i18nc("@info:progress", "Sorting..."));
430 m_statusBar
->setProgress(percent
);
433 void DolphinViewContainer::slotDirectoryLoadingStarted()
435 if (isSearchUrl(url())) {
436 // Search KIO-slaves usually don't provide any progress information. Give
437 // a hint to the user that a searching is done:
439 m_statusBar
->setProgressText(i18nc("@info", "Searching..."));
440 m_statusBar
->setProgress(-1);
442 // Trigger an undetermined progress indication. The progress
443 // information in percent will be triggered by the percent() signal
444 // of the directory lister later.
445 updateDirectoryLoadingProgress(-1);
449 void DolphinViewContainer::slotDirectoryLoadingCompleted()
451 if (!m_statusBar
->progressText().isEmpty()) {
452 m_statusBar
->setProgressText(QString());
453 m_statusBar
->setProgress(100);
456 if (isSearchUrl(url()) && m_view
->itemsCount() == 0) {
457 // The dir lister has been completed on a Baloo-URI and no items have been found. Instead
458 // of showing the default status bar information ("0 items") a more helpful information is given:
459 m_statusBar
->setText(i18nc("@info:status", "No items found."));
465 void DolphinViewContainer::slotDirectoryLoadingCanceled()
467 if (!m_statusBar
->progressText().isEmpty()) {
468 m_statusBar
->setProgressText(QString());
469 m_statusBar
->setProgress(100);
472 m_statusBar
->setText(QString());
475 void DolphinViewContainer::slotUrlIsFileError(const QUrl
& url
)
477 const KFileItem
item(url
);
479 // Find out if the file can be opened in the view (for example, this is the
480 // case if the file is an archive). The mime type must be known for that.
481 item
.determineMimeType();
482 const QUrl
& folderUrl
= DolphinView::openItemAsFolderUrl(item
, true);
483 if (!folderUrl
.isEmpty()) {
484 m_view
->setUrl(folderUrl
);
486 slotItemActivated(item
);
490 void DolphinViewContainer::slotItemActivated(const KFileItem
& item
)
492 // It is possible to activate items on inactive views by
493 // drag & drop operations. Assure that activating an item always
494 // results in an active view.
495 m_view
->setActive(true);
497 const QUrl
& url
= DolphinView::openItemAsFolderUrl(item
, GeneralSettings::browseThroughArchives());
498 if (!url
.isEmpty()) {
503 KRun
*run
= new KRun(item
.targetUrl(), this);
504 run
->setShowScriptExecutionPrompt(true);
507 void DolphinViewContainer::slotItemsActivated(const KFileItemList
& items
)
509 Q_ASSERT(items
.count() >= 2);
511 KFileItemActions
fileItemActions(this);
512 fileItemActions
.runPreferredApplications(items
, QString());
515 void DolphinViewContainer::showItemInfo(const KFileItem
& item
)
518 m_statusBar
->resetToDefaultText();
520 m_statusBar
->setText(item
.getStatusBarInfo());
524 void DolphinViewContainer::closeFilterBar()
526 m_filterBar
->closeFilterBar();
528 emit
showFilterBarChanged(false);
531 void DolphinViewContainer::setNameFilter(const QString
& nameFilter
)
533 m_view
->setNameFilter(nameFilter
);
534 delayedStatusBarUpdate();
537 void DolphinViewContainer::activate()
542 void DolphinViewContainer::slotViewUrlAboutToBeChanged(const QUrl
& url
)
544 // URL changes of the view can happen in two ways:
545 // 1. The URL navigator gets changed and will trigger the view to update its URL
546 // 2. The URL of the view gets changed and will trigger the URL navigator to update
547 // its URL (e.g. by clicking on an item)
548 // In this scope the view-state may only get saved in case 2:
549 if (url
!= m_urlNavigator
->locationUrl()) {
554 void DolphinViewContainer::slotUrlNavigatorLocationAboutToBeChanged(const QUrl
& url
)
556 // URL changes of the view can happen in two ways:
557 // 1. The URL navigator gets changed and will trigger the view to update its URL
558 // 2. The URL of the view gets changed and will trigger the URL navigator to update
559 // its URL (e.g. by clicking on an item)
560 // In this scope the view-state may only get saved in case 1:
561 if (url
!= m_view
->url()) {
566 void DolphinViewContainer::slotUrlNavigatorLocationChanged(const QUrl
& url
)
570 if (KProtocolManager::supportsListing(url
)) {
571 setSearchModeEnabled(isSearchUrl(url
));
574 if (m_autoGrabFocus
&& isActive() && !isSearchUrl(url
)) {
575 // When an URL has been entered, the view should get the focus.
576 // The focus must be requested asynchronously, as changing the URL might create
577 // a new view widget.
578 QTimer::singleShot(0, this, SLOT(requestFocus()));
580 } else if (KProtocolManager::isSourceProtocol(url
)) {
581 QString app
= "konqueror";
582 if (url
.scheme().startsWith(QLatin1String("http"))) {
583 showMessage(i18nc("@info:status", // krazy:exclude=qmethods
584 "Dolphin does not support web pages, the web browser has been launched"),
587 const KConfigGroup
config(KSharedConfig::openConfig("kdeglobals"), "General");
588 const QString browser
= config
.readEntry("BrowserApplication");
589 if (!browser
.isEmpty()) {
591 if (app
.startsWith('!')) {
592 // a literal command has been configured, remove the '!' prefix
597 showMessage(i18nc("@info:status",
598 "Protocol not supported by Dolphin, Konqueror has been launched"),
602 const QString secureUrl
= KShell::quoteArg(url
.toDisplayString(QUrl::PreferLocalFile
));
603 const QString command
= app
+ ' ' + secureUrl
;
604 KRun::runCommand(command
, app
, app
, this);
606 showMessage(i18nc("@info:status", "Invalid protocol"), Error
);
610 void DolphinViewContainer::redirect(const QUrl
& oldUrl
, const QUrl
& newUrl
)
613 const bool block
= m_urlNavigator
->signalsBlocked();
614 m_urlNavigator
->blockSignals(true);
616 // Assure that the location state is reset for redirection URLs. This
617 // allows to skip redirection URLs when going back or forward in the
619 m_urlNavigator
->saveLocationState(QByteArray());
620 m_urlNavigator
->setLocationUrl(newUrl
);
621 setSearchModeEnabled(isSearchUrl(newUrl
));
623 m_urlNavigator
->blockSignals(block
);
626 void DolphinViewContainer::requestFocus()
631 void DolphinViewContainer::saveUrlCompletionMode(KCompletion::CompletionMode completion
)
633 GeneralSettings::setUrlCompletionMode(completion
);
636 void DolphinViewContainer::slotHistoryChanged()
638 QByteArray locationState
= m_urlNavigator
->locationState();
639 if (!locationState
.isEmpty()) {
640 QDataStream
stream(&locationState
, QIODevice::ReadOnly
);
641 m_view
->restoreState(stream
);
645 void DolphinViewContainer::slotReturnPressed()
647 if (!GeneralSettings::editableUrl()) {
648 m_urlNavigator
->setUrlEditable(false);
652 void DolphinViewContainer::startSearching()
654 const QUrl url
= m_searchBox
->urlForSearching();
655 if (url
.isValid() && !url
.isEmpty()) {
656 m_view
->setViewPropertiesContext("search");
657 m_urlNavigator
->setLocationUrl(url
);
661 void DolphinViewContainer::closeSearchBox()
663 setSearchModeEnabled(false);
666 void DolphinViewContainer::stopDirectoryLoading()
668 m_view
->stopLoading();
669 m_statusBar
->setProgress(100);
672 void DolphinViewContainer::slotStatusBarZoomLevelChanged(int zoomLevel
)
674 m_view
->setZoomLevel(zoomLevel
);
677 void DolphinViewContainer::showErrorMessage(const QString
& msg
)
679 showMessage(msg
, Error
);
682 bool DolphinViewContainer::isSearchUrl(const QUrl
& url
) const
684 return url
.scheme().contains("search");
687 void DolphinViewContainer::saveViewState()
689 QByteArray locationState
;
690 QDataStream
stream(&locationState
, QIODevice::WriteOnly
);
691 m_view
->saveState(stream
);
692 m_urlNavigator
->saveLocationState(locationState
);