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>
23 #include <QApplication>
25 #include <QItemSelection>
31 #include <KDesktopFile>
32 #include <KFileItemDelegate>
33 #include <KFileItemActions>
34 #include <KFilePlacesModel>
36 #include <KIconEffect>
37 #include <KIO/NetAccess>
38 #include <KIO/PreviewJob>
39 #include <KMessageWidget>
40 #include <KNewFileMenu>
41 #include <konqmimedata.h>
42 #include <konq_operations.h>
45 #include <KUrlComboBox>
46 #include <KUrlNavigator>
49 #ifdef KActivities_FOUND
50 #include <KActivities/ResourceInstance>
53 #include "dolphin_generalsettings.h"
54 #include "filterbar/filterbar.h"
55 #include "search/dolphinsearchbox.h"
56 #include "statusbar/dolphinstatusbar.h"
57 #include "views/draganddrophelper.h"
58 #include "views/viewmodecontroller.h"
59 #include "views/viewproperties.h"
61 DolphinViewContainer::DolphinViewContainer(const KUrl
& url
, QWidget
* parent
) :
71 m_statusBarTimestamp(),
72 m_autoGrabFocus(true),
75 #ifdef KActivities_FOUND
76 , m_activityResourceInstance(0)
81 m_topLayout
= new QVBoxLayout(this);
82 m_topLayout
->setSpacing(0);
83 m_topLayout
->setMargin(0);
85 m_urlNavigator
= new KUrlNavigator(new KFilePlacesModel(this), url
, this);
86 connect(m_urlNavigator
, &KUrlNavigator::urlsDropped
,
87 this, &DolphinViewContainer::dropUrls
);
88 connect(m_urlNavigator
, &KUrlNavigator::activated
,
89 this, &DolphinViewContainer::activate
);
90 connect(m_urlNavigator
->editor(), &KUrlComboBox::completionModeChanged
,
91 this, &DolphinViewContainer::saveUrlCompletionMode
);
93 const GeneralSettings
* settings
= GeneralSettings::self();
94 m_urlNavigator
->setUrlEditable(settings
->editableUrl());
95 m_urlNavigator
->setShowFullPath(settings
->showFullPath());
96 m_urlNavigator
->setHomeUrl(KUrl(settings
->homeUrl()));
97 KUrlComboBox
* editor
= m_urlNavigator
->editor();
98 editor
->setCompletionMode(KCompletion::CompletionMode(settings
->urlCompletionMode()));
100 m_searchBox
= new DolphinSearchBox(this);
102 connect(m_searchBox
, &DolphinSearchBox::activated
, this, &DolphinViewContainer::activate
);
103 connect(m_searchBox
, &DolphinSearchBox::closeRequest
, this, &DolphinViewContainer::closeSearchBox
);
104 connect(m_searchBox
, &DolphinSearchBox::searchRequest
, this, &DolphinViewContainer::startSearching
);
105 connect(m_searchBox
, &DolphinSearchBox::returnPressed
, this, &DolphinViewContainer::requestFocus
);
107 m_messageWidget
= new KMessageWidget(this);
108 m_messageWidget
->setCloseButtonVisible(true);
109 m_messageWidget
->hide();
111 m_view
= new DolphinView(url
, this);
112 connect(m_view
, &DolphinView::urlChanged
,
113 m_urlNavigator
, &KUrlNavigator::setUrl
);
114 connect(m_view
, &DolphinView::urlChanged
,
115 m_messageWidget
, &KMessageWidget::hide
);
116 connect(m_view
, &DolphinView::writeStateChanged
,
117 this, &DolphinViewContainer::writeStateChanged
);
118 connect(m_view
, &DolphinView::requestItemInfo
,
119 this, &DolphinViewContainer::showItemInfo
);
120 connect(m_view
, &DolphinView::itemActivated
,
121 this, &DolphinViewContainer::slotItemActivated
);
122 connect(m_view
, &DolphinView::itemsActivated
,
123 this, &DolphinViewContainer::slotItemsActivated
);
124 connect(m_view
, &DolphinView::redirection
,
125 this, &DolphinViewContainer::redirect
);
126 connect(m_view
, &DolphinView::directoryLoadingStarted
,
127 this, &DolphinViewContainer::slotDirectoryLoadingStarted
);
128 connect(m_view
, &DolphinView::directoryLoadingCompleted
,
129 this, &DolphinViewContainer::slotDirectoryLoadingCompleted
);
130 connect(m_view
, &DolphinView::directoryLoadingCanceled
,
131 this, &DolphinViewContainer::slotDirectoryLoadingCanceled
);
132 connect(m_view
, &DolphinView::itemCountChanged
,
133 this, &DolphinViewContainer::delayedStatusBarUpdate
);
134 connect(m_view
, &DolphinView::directoryLoadingProgress
,
135 this, &DolphinViewContainer::updateDirectoryLoadingProgress
);
136 connect(m_view
, &DolphinView::directorySortingProgress
,
137 this, &DolphinViewContainer::updateDirectorySortingProgress
);
138 connect(m_view
, &DolphinView::selectionChanged
,
139 this, &DolphinViewContainer::delayedStatusBarUpdate
);
140 connect(m_view
, &DolphinView::urlAboutToBeChanged
,
141 this, &DolphinViewContainer::slotViewUrlAboutToBeChanged
);
142 connect(m_view
, &DolphinView::errorMessage
,
143 this, &DolphinViewContainer::showErrorMessage
);
144 connect(m_view
, &DolphinView::urlIsFileError
,
145 this, &DolphinViewContainer::slotUrlIsFileError
);
147 connect(m_urlNavigator
, &KUrlNavigator::urlAboutToBeChanged
,
148 this, &DolphinViewContainer::slotUrlNavigatorLocationAboutToBeChanged
);
149 connect(m_urlNavigator
, &KUrlNavigator::urlChanged
,
150 this, &DolphinViewContainer::slotUrlNavigatorLocationChanged
);
151 connect(m_urlNavigator
, &KUrlNavigator::historyChanged
,
152 this, &DolphinViewContainer::slotHistoryChanged
);
153 connect(m_urlNavigator
, &KUrlNavigator::returnPressed
,
154 this, &DolphinViewContainer::slotReturnPressed
);
156 // Initialize status bar
157 m_statusBar
= new DolphinStatusBar(this);
158 m_statusBar
->setUrl(m_view
->url());
159 m_statusBar
->setZoomLevel(m_view
->zoomLevel());
160 connect(m_view
, &DolphinView::urlChanged
,
161 m_statusBar
, &DolphinStatusBar::setUrl
);
162 connect(m_view
, &DolphinView::zoomLevelChanged
,
163 m_statusBar
, &DolphinStatusBar::setZoomLevel
);
164 connect(m_view
, &DolphinView::infoMessage
,
165 m_statusBar
, &DolphinStatusBar::setText
);
166 connect(m_view
, &DolphinView::operationCompletedMessage
,
167 m_statusBar
, &DolphinStatusBar::setText
);
168 connect(m_statusBar
, &DolphinStatusBar::stopPressed
,
169 this, &DolphinViewContainer::stopDirectoryLoading
);
170 connect(m_statusBar
, &DolphinStatusBar::zoomLevelChanged
,
171 this, &DolphinViewContainer::slotStatusBarZoomLevelChanged
);
173 m_statusBarTimer
= new QTimer(this);
174 m_statusBarTimer
->setSingleShot(true);
175 m_statusBarTimer
->setInterval(300);
176 connect(m_statusBarTimer
, &QTimer::timeout
, this, &DolphinViewContainer::updateStatusBar
);
178 KIO::FileUndoManager
* undoManager
= KIO::FileUndoManager::self();
179 connect(undoManager
, &KIO::FileUndoManager::jobRecordingFinished
,
180 this, &DolphinViewContainer::delayedStatusBarUpdate
);
182 // Initialize filter bar
183 m_filterBar
= new FilterBar(this);
184 m_filterBar
->setVisible(settings
->filterBar());
185 connect(m_filterBar
, &FilterBar::filterChanged
,
186 this, &DolphinViewContainer::setNameFilter
);
187 connect(m_filterBar
, &FilterBar::closeRequest
,
188 this, &DolphinViewContainer::closeFilterBar
);
189 connect(m_filterBar
, &FilterBar::focusViewRequest
,
190 this, &DolphinViewContainer::requestFocus
);
191 connect(m_view
, &DolphinView::urlChanged
,
192 m_filterBar
, &FilterBar::slotUrlChanged
);
194 m_topLayout
->addWidget(m_urlNavigator
);
195 m_topLayout
->addWidget(m_searchBox
);
196 m_topLayout
->addWidget(m_messageWidget
);
197 m_topLayout
->addWidget(m_view
);
198 m_topLayout
->addWidget(m_filterBar
);
199 m_topLayout
->addWidget(m_statusBar
);
201 setSearchModeEnabled(isSearchUrl(url
));
203 // Initialize kactivities resource instance
205 #ifdef KActivities_FOUND
206 m_activityResourceInstance
= new KActivities::ResourceInstance(
207 window()->winId(), url
);
208 m_activityResourceInstance
->setParent(this);
212 DolphinViewContainer::~DolphinViewContainer()
216 KUrl
DolphinViewContainer::url() const
218 return m_view
->url();
221 void DolphinViewContainer::setActive(bool active
)
223 m_searchBox
->setActive(active
);
224 m_urlNavigator
->setActive(active
);
225 m_view
->setActive(active
);
227 #ifdef KActivities_FOUND
229 m_activityResourceInstance
->notifyFocusedIn();
231 m_activityResourceInstance
->notifyFocusedOut();
236 bool DolphinViewContainer::isActive() const
238 Q_ASSERT(m_view
->isActive() == m_urlNavigator
->isActive());
239 return m_view
->isActive();
242 void DolphinViewContainer::setAutoGrabFocus(bool grab
)
244 m_autoGrabFocus
= grab
;
247 bool DolphinViewContainer::autoGrabFocus() const
249 return m_autoGrabFocus
;
252 const DolphinStatusBar
* DolphinViewContainer::statusBar() const
257 DolphinStatusBar
* DolphinViewContainer::statusBar()
262 const KUrlNavigator
* DolphinViewContainer::urlNavigator() const
264 return m_urlNavigator
;
267 KUrlNavigator
* DolphinViewContainer::urlNavigator()
269 return m_urlNavigator
;
272 const DolphinView
* DolphinViewContainer::view() const
277 DolphinView
* DolphinViewContainer::view()
282 void DolphinViewContainer::showMessage(const QString
& msg
, MessageType type
)
288 m_messageWidget
->setText(msg
);
291 case Information
: m_messageWidget
->setMessageType(KMessageWidget::Information
); break;
292 case Warning
: m_messageWidget
->setMessageType(KMessageWidget::Warning
); break;
293 case Error
: m_messageWidget
->setMessageType(KMessageWidget::Error
); break;
299 m_messageWidget
->setWordWrap(false);
300 const int unwrappedWidth
= m_messageWidget
->sizeHint().width();
301 m_messageWidget
->setWordWrap(unwrappedWidth
> size().width());
303 m_messageWidget
->animatedShow();
306 void DolphinViewContainer::readSettings()
308 if (GeneralSettings::modifiedStartupSettings()) {
309 // The startup settings should only get applied if they have been
310 // modified by the user. Otherwise keep the (possibly) different current
311 // settings of the URL navigator and the filterbar.
312 m_urlNavigator
->setUrlEditable(GeneralSettings::editableUrl());
313 m_urlNavigator
->setShowFullPath(GeneralSettings::showFullPath());
314 m_urlNavigator
->setHomeUrl(KUrl(GeneralSettings::homeUrl()));
315 setFilterBarVisible(GeneralSettings::filterBar());
318 m_view
->readSettings();
319 m_statusBar
->readSettings();
322 bool DolphinViewContainer::isFilterBarVisible() const
324 return m_filterBar
->isVisible();
327 void DolphinViewContainer::setSearchModeEnabled(bool enabled
)
329 if (enabled
== isSearchModeEnabled()) {
330 if (enabled
&& !m_searchBox
->hasFocus()) {
331 m_searchBox
->setFocus();
332 m_searchBox
->selectAll();
337 m_searchBox
->setVisible(enabled
);
338 m_urlNavigator
->setVisible(!enabled
);
341 const KUrl
& locationUrl
= m_urlNavigator
->locationUrl();
342 m_searchBox
->fromSearchUrl(locationUrl
);
344 m_view
->setViewPropertiesContext(QString());
346 // Restore the URL for the URL navigator. If Dolphin has been
347 // started with a search-URL, the home URL is used as fallback.
348 KUrl url
= m_searchBox
->searchPath();
349 if (url
.isEmpty() || !url
.isValid() || isSearchUrl(url
)) {
350 url
= GeneralSettings::self()->homeUrl();
352 m_urlNavigator
->setLocationUrl(url
);
356 bool DolphinViewContainer::isSearchModeEnabled() const
358 return m_searchBox
->isVisible();
361 QString
DolphinViewContainer::placesText() const
365 if (isSearchModeEnabled()) {
366 text
= m_searchBox
->searchPath().fileName() + QLatin1String(": ") + m_searchBox
->text();
368 text
= url().fileName();
369 if (text
.isEmpty()) {
377 void DolphinViewContainer::setUrl(const KUrl
& newUrl
)
379 if (newUrl
!= m_urlNavigator
->locationUrl()) {
380 m_urlNavigator
->setLocationUrl(newUrl
);
383 #ifdef KActivities_FOUND
384 m_activityResourceInstance
->setUri(newUrl
);
388 void DolphinViewContainer::setFilterBarVisible(bool visible
)
390 Q_ASSERT(m_filterBar
);
393 m_filterBar
->setFocus();
394 m_filterBar
->selectAll();
400 void DolphinViewContainer::delayedStatusBarUpdate()
402 if (m_statusBarTimer
->isActive() && (m_statusBarTimestamp
.elapsed() > 2000)) {
403 // No update of the statusbar has been done during the last 2 seconds,
404 // although an update has been requested. Trigger an immediate update.
405 m_statusBarTimer
->stop();
408 // Invoke updateStatusBar() with a small delay. This assures that
409 // when a lot of delayedStatusBarUpdates() are done in a short time,
410 // no bottleneck is given.
411 m_statusBarTimer
->start();
415 void DolphinViewContainer::updateStatusBar()
417 m_statusBarTimestamp
.start();
419 const QString text
= m_view
->statusBarText();
420 m_statusBar
->setDefaultText(text
);
421 m_statusBar
->resetToDefaultText();
424 void DolphinViewContainer::updateDirectoryLoadingProgress(int percent
)
426 if (m_statusBar
->progressText().isEmpty()) {
427 m_statusBar
->setProgressText(i18nc("@info:progress", "Loading folder..."));
429 m_statusBar
->setProgress(percent
);
432 void DolphinViewContainer::updateDirectorySortingProgress(int percent
)
434 if (m_statusBar
->progressText().isEmpty()) {
435 m_statusBar
->setProgressText(i18nc("@info:progress", "Sorting..."));
437 m_statusBar
->setProgress(percent
);
440 void DolphinViewContainer::slotDirectoryLoadingStarted()
442 if (isSearchUrl(url())) {
443 // Search KIO-slaves usually don't provide any progress information. Give
444 // a hint to the user that a searching is done:
446 m_statusBar
->setProgressText(i18nc("@info", "Searching..."));
447 m_statusBar
->setProgress(-1);
449 // Trigger an undetermined progress indication. The progress
450 // information in percent will be triggered by the percent() signal
451 // of the directory lister later.
452 updateDirectoryLoadingProgress(-1);
456 void DolphinViewContainer::slotDirectoryLoadingCompleted()
458 if (!m_statusBar
->progressText().isEmpty()) {
459 m_statusBar
->setProgressText(QString());
460 m_statusBar
->setProgress(100);
463 if (isSearchUrl(url()) && m_view
->itemsCount() == 0) {
464 // The dir lister has been completed on a Baloo-URI and no items have been found. Instead
465 // of showing the default status bar information ("0 items") a more helpful information is given:
466 m_statusBar
->setText(i18nc("@info:status", "No items found."));
472 void DolphinViewContainer::slotDirectoryLoadingCanceled()
474 if (!m_statusBar
->progressText().isEmpty()) {
475 m_statusBar
->setProgressText(QString());
476 m_statusBar
->setProgress(100);
479 m_statusBar
->setText(QString());
482 void DolphinViewContainer::slotUrlIsFileError(const KUrl
& url
)
484 const KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, url
);
485 slotItemActivated(item
);
488 void DolphinViewContainer::slotItemActivated(const KFileItem
& item
)
490 // It is possible to activate items on inactive views by
491 // drag & drop operations. Assure that activating an item always
492 // results in an active view.
493 m_view
->setActive(true);
495 const KUrl
& url
= DolphinView::openItemAsFolderUrl(item
, GeneralSettings::browseThroughArchives());
496 if (!url
.isEmpty()) {
501 new KRun(item
.targetUrl(), this);
504 void DolphinViewContainer::slotItemsActivated(const KFileItemList
& items
)
506 Q_ASSERT(items
.count() >= 2);
508 KFileItemActions
fileItemActions(this);
509 fileItemActions
.runPreferredApplications(items
, QString());
512 void DolphinViewContainer::showItemInfo(const KFileItem
& item
)
515 m_statusBar
->resetToDefaultText();
517 m_statusBar
->setText(item
.getStatusBarInfo());
521 void DolphinViewContainer::closeFilterBar()
523 m_filterBar
->closeFilterBar();
525 emit
showFilterBarChanged(false);
528 void DolphinViewContainer::setNameFilter(const QString
& nameFilter
)
530 m_view
->setNameFilter(nameFilter
);
531 delayedStatusBarUpdate();
534 void DolphinViewContainer::activate()
539 void DolphinViewContainer::slotViewUrlAboutToBeChanged(const KUrl
& url
)
541 // URL changes of the view can happen in two ways:
542 // 1. The URL navigator gets changed and will trigger the view to update its URL
543 // 2. The URL of the view gets changed and will trigger the URL navigator to update
544 // its URL (e.g. by clicking on an item)
545 // In this scope the view-state may only get saved in case 2:
546 if (url
!= m_urlNavigator
->locationUrl()) {
551 void DolphinViewContainer::slotUrlNavigatorLocationAboutToBeChanged(const KUrl
& url
)
553 // URL changes of the view can happen in two ways:
554 // 1. The URL navigator gets changed and will trigger the view to update its URL
555 // 2. The URL of the view gets changed and will trigger the URL navigator to update
556 // its URL (e.g. by clicking on an item)
557 // In this scope the view-state may only get saved in case 1:
558 if (url
!= m_view
->url()) {
563 void DolphinViewContainer::slotUrlNavigatorLocationChanged(const KUrl
& url
)
567 if (KProtocolManager::supportsListing(url
)) {
568 setSearchModeEnabled(isSearchUrl(url
));
571 if (m_autoGrabFocus
&& isActive() && !isSearchUrl(url
)) {
572 // When an URL has been entered, the view should get the focus.
573 // The focus must be requested asynchronously, as changing the URL might create
574 // a new view widget.
575 QTimer::singleShot(0, this, SLOT(requestFocus()));
577 } else if (KProtocolManager::isSourceProtocol(url
)) {
578 QString app
= "konqueror";
579 if (url
.protocol().startsWith(QLatin1String("http"))) {
580 showMessage(i18nc("@info:status", // krazy:exclude=qmethods
581 "Dolphin does not support web pages, the web browser has been launched"),
584 const KConfigGroup
config(KSharedConfig::openConfig("kdeglobals"), "General");
585 const QString browser
= config
.readEntry("BrowserApplication");
586 if (!browser
.isEmpty()) {
588 if (app
.startsWith('!')) {
589 // a literal command has been configured, remove the '!' prefix
594 showMessage(i18nc("@info:status",
595 "Protocol not supported by Dolphin, Konqueror has been launched"),
599 const QString secureUrl
= KShell::quoteArg(url
.pathOrUrl());
600 const QString command
= app
+ ' ' + secureUrl
;
601 KRun::runCommand(command
, app
, app
, this);
603 showMessage(i18nc("@info:status", "Invalid protocol"), Error
);
607 void DolphinViewContainer::dropUrls(const KUrl
& destination
, QDropEvent
* event
)
609 m_dropDestination
= destination
;
611 const QMimeData
* mimeData
= event
->mimeData();
612 QMimeData
* mimeDataCopy
= new QMimeData
;
613 foreach (const QString
& format
, mimeData
->formats()) {
614 mimeDataCopy
->setData(format
, mimeData
->data(format
));
617 m_dropEvent
.reset(new QDropEvent(event
->pos(),
618 event
->possibleActions(),
620 event
->mouseButtons(),
621 event
->keyboardModifiers()));
623 QTimer::singleShot(0, this, SLOT(dropUrlsDelayed()));
626 void DolphinViewContainer::dropUrlsDelayed()
628 if (m_dropEvent
.isNull()) {
633 DragAndDropHelper::dropUrls(KFileItem(), m_dropDestination
, m_dropEvent
.data(), error
);
634 if (!error
.isEmpty()) {
635 showMessage(error
, Error
);
638 delete m_dropEvent
->mimeData();
642 void DolphinViewContainer::redirect(const KUrl
& oldUrl
, const KUrl
& newUrl
)
645 const bool block
= m_urlNavigator
->signalsBlocked();
646 m_urlNavigator
->blockSignals(true);
648 // Assure that the location state is reset for redirection URLs. This
649 // allows to skip redirection URLs when going back or forward in the
651 m_urlNavigator
->saveLocationState(QByteArray());
652 m_urlNavigator
->setLocationUrl(newUrl
);
653 setSearchModeEnabled(isSearchUrl(newUrl
));
655 m_urlNavigator
->blockSignals(block
);
658 void DolphinViewContainer::requestFocus()
663 void DolphinViewContainer::saveUrlCompletionMode(KCompletion::CompletionMode completion
)
665 GeneralSettings::setUrlCompletionMode(completion
);
668 void DolphinViewContainer::slotHistoryChanged()
670 QByteArray locationState
= m_urlNavigator
->locationState();
671 if (!locationState
.isEmpty()) {
672 QDataStream
stream(&locationState
, QIODevice::ReadOnly
);
673 m_view
->restoreState(stream
);
677 void DolphinViewContainer::slotReturnPressed()
679 if (!GeneralSettings::editableUrl()) {
680 m_urlNavigator
->setUrlEditable(false);
684 void DolphinViewContainer::startSearching()
686 const KUrl url
= m_searchBox
->urlForSearching();
687 if (url
.isValid() && !url
.isEmpty()) {
688 m_view
->setViewPropertiesContext("search");
689 m_urlNavigator
->setLocationUrl(url
);
693 void DolphinViewContainer::closeSearchBox()
695 setSearchModeEnabled(false);
698 void DolphinViewContainer::stopDirectoryLoading()
700 m_view
->stopLoading();
701 m_statusBar
->setProgress(100);
704 void DolphinViewContainer::slotStatusBarZoomLevelChanged(int zoomLevel
)
706 m_view
->setZoomLevel(zoomLevel
);
709 void DolphinViewContainer::showErrorMessage(const QString
& msg
)
711 showMessage(msg
, Error
);
714 bool DolphinViewContainer::isSearchUrl(const KUrl
& url
) const
716 const QString protocol
= url
.protocol();
717 return protocol
.contains("search");
720 void DolphinViewContainer::saveViewState()
722 QByteArray locationState
;
723 QDataStream
stream(&locationState
, QIODevice::WriteOnly
);
724 m_view
->saveState(stream
);
725 m_urlNavigator
->saveLocationState(locationState
);