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 <konq_operations.h>
44 #include <KUrlComboBox>
45 #include <KUrlNavigator>
48 #ifdef KActivities_FOUND
49 #include <KActivities/ResourceInstance>
52 #include "dolphin_generalsettings.h"
53 #include "filterbar/filterbar.h"
54 #include "search/dolphinsearchbox.h"
55 #include "statusbar/dolphinstatusbar.h"
56 #include "views/draganddrophelper.h"
57 #include "views/viewmodecontroller.h"
58 #include "views/viewproperties.h"
60 DolphinViewContainer::DolphinViewContainer(const KUrl
& url
, QWidget
* parent
) :
70 m_statusBarTimestamp(),
71 m_autoGrabFocus(true),
74 #ifdef KActivities_FOUND
75 , m_activityResourceInstance(0)
80 m_topLayout
= new QVBoxLayout(this);
81 m_topLayout
->setSpacing(0);
82 m_topLayout
->setMargin(0);
84 m_urlNavigator
= new KUrlNavigator(new KFilePlacesModel(this), url
, this);
85 connect(m_urlNavigator
, &KUrlNavigator::urlsDropped
,
86 this, &DolphinViewContainer::dropUrls
);
87 connect(m_urlNavigator
, &KUrlNavigator::activated
,
88 this, &DolphinViewContainer::activate
);
89 connect(m_urlNavigator
->editor(), &KUrlComboBox::completionModeChanged
,
90 this, &DolphinViewContainer::saveUrlCompletionMode
);
92 const GeneralSettings
* settings
= GeneralSettings::self();
93 m_urlNavigator
->setUrlEditable(settings
->editableUrl());
94 m_urlNavigator
->setShowFullPath(settings
->showFullPath());
95 m_urlNavigator
->setHomeUrl(KUrl(settings
->homeUrl()));
96 KUrlComboBox
* editor
= m_urlNavigator
->editor();
97 editor
->setCompletionMode(KCompletion::CompletionMode(settings
->urlCompletionMode()));
99 m_searchBox
= new DolphinSearchBox(this);
101 connect(m_searchBox
, &DolphinSearchBox::activated
, this, &DolphinViewContainer::activate
);
102 connect(m_searchBox
, &DolphinSearchBox::closeRequest
, this, &DolphinViewContainer::closeSearchBox
);
103 connect(m_searchBox
, &DolphinSearchBox::searchRequest
, this, &DolphinViewContainer::startSearching
);
104 connect(m_searchBox
, &DolphinSearchBox::returnPressed
, this, &DolphinViewContainer::requestFocus
);
106 m_messageWidget
= new KMessageWidget(this);
107 m_messageWidget
->setCloseButtonVisible(true);
108 m_messageWidget
->hide();
110 m_view
= new DolphinView(url
, this);
111 connect(m_view
, &DolphinView::urlChanged
,
112 m_urlNavigator
, &KUrlNavigator::setUrl
);
113 connect(m_view
, &DolphinView::urlChanged
,
114 m_messageWidget
, &KMessageWidget::hide
);
115 connect(m_view
, &DolphinView::writeStateChanged
,
116 this, &DolphinViewContainer::writeStateChanged
);
117 connect(m_view
, &DolphinView::requestItemInfo
,
118 this, &DolphinViewContainer::showItemInfo
);
119 connect(m_view
, &DolphinView::itemActivated
,
120 this, &DolphinViewContainer::slotItemActivated
);
121 connect(m_view
, &DolphinView::itemsActivated
,
122 this, &DolphinViewContainer::slotItemsActivated
);
123 connect(m_view
, &DolphinView::redirection
,
124 this, &DolphinViewContainer::redirect
);
125 connect(m_view
, &DolphinView::directoryLoadingStarted
,
126 this, &DolphinViewContainer::slotDirectoryLoadingStarted
);
127 connect(m_view
, &DolphinView::directoryLoadingCompleted
,
128 this, &DolphinViewContainer::slotDirectoryLoadingCompleted
);
129 connect(m_view
, &DolphinView::directoryLoadingCanceled
,
130 this, &DolphinViewContainer::slotDirectoryLoadingCanceled
);
131 connect(m_view
, &DolphinView::itemCountChanged
,
132 this, &DolphinViewContainer::delayedStatusBarUpdate
);
133 connect(m_view
, &DolphinView::directoryLoadingProgress
,
134 this, &DolphinViewContainer::updateDirectoryLoadingProgress
);
135 connect(m_view
, &DolphinView::directorySortingProgress
,
136 this, &DolphinViewContainer::updateDirectorySortingProgress
);
137 connect(m_view
, &DolphinView::selectionChanged
,
138 this, &DolphinViewContainer::delayedStatusBarUpdate
);
139 connect(m_view
, &DolphinView::urlAboutToBeChanged
,
140 this, &DolphinViewContainer::slotViewUrlAboutToBeChanged
);
141 connect(m_view
, &DolphinView::errorMessage
,
142 this, &DolphinViewContainer::showErrorMessage
);
143 connect(m_view
, &DolphinView::urlIsFileError
,
144 this, &DolphinViewContainer::slotUrlIsFileError
);
145 connect(m_view
, &DolphinView::activated
,
146 this, &DolphinViewContainer::activate
);
148 connect(m_urlNavigator
, &KUrlNavigator::urlAboutToBeChanged
,
149 this, &DolphinViewContainer::slotUrlNavigatorLocationAboutToBeChanged
);
150 connect(m_urlNavigator
, &KUrlNavigator::urlChanged
,
151 this, &DolphinViewContainer::slotUrlNavigatorLocationChanged
);
152 connect(m_urlNavigator
, &KUrlNavigator::historyChanged
,
153 this, &DolphinViewContainer::slotHistoryChanged
);
154 connect(m_urlNavigator
, &KUrlNavigator::returnPressed
,
155 this, &DolphinViewContainer::slotReturnPressed
);
157 // Initialize status bar
158 m_statusBar
= new DolphinStatusBar(this);
159 m_statusBar
->setUrl(m_view
->url());
160 m_statusBar
->setZoomLevel(m_view
->zoomLevel());
161 connect(m_view
, &DolphinView::urlChanged
,
162 m_statusBar
, &DolphinStatusBar::setUrl
);
163 connect(m_view
, &DolphinView::zoomLevelChanged
,
164 m_statusBar
, &DolphinStatusBar::setZoomLevel
);
165 connect(m_view
, &DolphinView::infoMessage
,
166 m_statusBar
, &DolphinStatusBar::setText
);
167 connect(m_view
, &DolphinView::operationCompletedMessage
,
168 m_statusBar
, &DolphinStatusBar::setText
);
169 connect(m_statusBar
, &DolphinStatusBar::stopPressed
,
170 this, &DolphinViewContainer::stopDirectoryLoading
);
171 connect(m_statusBar
, &DolphinStatusBar::zoomLevelChanged
,
172 this, &DolphinViewContainer::slotStatusBarZoomLevelChanged
);
174 m_statusBarTimer
= new QTimer(this);
175 m_statusBarTimer
->setSingleShot(true);
176 m_statusBarTimer
->setInterval(300);
177 connect(m_statusBarTimer
, &QTimer::timeout
, this, &DolphinViewContainer::updateStatusBar
);
179 KIO::FileUndoManager
* undoManager
= KIO::FileUndoManager::self();
180 connect(undoManager
, &KIO::FileUndoManager::jobRecordingFinished
,
181 this, &DolphinViewContainer::delayedStatusBarUpdate
);
183 // Initialize filter bar
184 m_filterBar
= new FilterBar(this);
185 m_filterBar
->setVisible(settings
->filterBar());
186 connect(m_filterBar
, &FilterBar::filterChanged
,
187 this, &DolphinViewContainer::setNameFilter
);
188 connect(m_filterBar
, &FilterBar::closeRequest
,
189 this, &DolphinViewContainer::closeFilterBar
);
190 connect(m_filterBar
, &FilterBar::focusViewRequest
,
191 this, &DolphinViewContainer::requestFocus
);
192 connect(m_view
, &DolphinView::urlChanged
,
193 m_filterBar
, &FilterBar::slotUrlChanged
);
195 m_topLayout
->addWidget(m_urlNavigator
);
196 m_topLayout
->addWidget(m_searchBox
);
197 m_topLayout
->addWidget(m_messageWidget
);
198 m_topLayout
->addWidget(m_view
);
199 m_topLayout
->addWidget(m_filterBar
);
200 m_topLayout
->addWidget(m_statusBar
);
202 setSearchModeEnabled(isSearchUrl(url
));
204 // Initialize kactivities resource instance
206 #ifdef KActivities_FOUND
207 m_activityResourceInstance
= new KActivities::ResourceInstance(
208 window()->winId(), url
);
209 m_activityResourceInstance
->setParent(this);
213 DolphinViewContainer::~DolphinViewContainer()
217 KUrl
DolphinViewContainer::url() const
219 return m_view
->url();
222 void DolphinViewContainer::setActive(bool active
)
224 m_searchBox
->setActive(active
);
225 m_urlNavigator
->setActive(active
);
226 m_view
->setActive(active
);
228 #ifdef KActivities_FOUND
230 m_activityResourceInstance
->notifyFocusedIn();
232 m_activityResourceInstance
->notifyFocusedOut();
237 bool DolphinViewContainer::isActive() const
239 Q_ASSERT(m_view
->isActive() == m_urlNavigator
->isActive());
240 return m_view
->isActive();
243 void DolphinViewContainer::setAutoGrabFocus(bool grab
)
245 m_autoGrabFocus
= grab
;
248 bool DolphinViewContainer::autoGrabFocus() const
250 return m_autoGrabFocus
;
253 const DolphinStatusBar
* DolphinViewContainer::statusBar() const
258 DolphinStatusBar
* DolphinViewContainer::statusBar()
263 const KUrlNavigator
* DolphinViewContainer::urlNavigator() const
265 return m_urlNavigator
;
268 KUrlNavigator
* DolphinViewContainer::urlNavigator()
270 return m_urlNavigator
;
273 const DolphinView
* DolphinViewContainer::view() const
278 DolphinView
* DolphinViewContainer::view()
283 void DolphinViewContainer::showMessage(const QString
& msg
, MessageType type
)
289 m_messageWidget
->setText(msg
);
292 case Information
: m_messageWidget
->setMessageType(KMessageWidget::Information
); break;
293 case Warning
: m_messageWidget
->setMessageType(KMessageWidget::Warning
); break;
294 case Error
: m_messageWidget
->setMessageType(KMessageWidget::Error
); break;
300 m_messageWidget
->setWordWrap(false);
301 const int unwrappedWidth
= m_messageWidget
->sizeHint().width();
302 m_messageWidget
->setWordWrap(unwrappedWidth
> size().width());
304 m_messageWidget
->animatedShow();
307 void DolphinViewContainer::readSettings()
309 if (GeneralSettings::modifiedStartupSettings()) {
310 // The startup settings should only get applied if they have been
311 // modified by the user. Otherwise keep the (possibly) different current
312 // settings of the URL navigator and the filterbar.
313 m_urlNavigator
->setUrlEditable(GeneralSettings::editableUrl());
314 m_urlNavigator
->setShowFullPath(GeneralSettings::showFullPath());
315 m_urlNavigator
->setHomeUrl(KUrl(GeneralSettings::homeUrl()));
316 setFilterBarVisible(GeneralSettings::filterBar());
319 m_view
->readSettings();
320 m_statusBar
->readSettings();
323 bool DolphinViewContainer::isFilterBarVisible() const
325 return m_filterBar
->isVisible();
328 void DolphinViewContainer::setSearchModeEnabled(bool enabled
)
330 if (enabled
== isSearchModeEnabled()) {
331 if (enabled
&& !m_searchBox
->hasFocus()) {
332 m_searchBox
->setFocus();
333 m_searchBox
->selectAll();
338 m_searchBox
->setVisible(enabled
);
339 m_urlNavigator
->setVisible(!enabled
);
342 const KUrl
& locationUrl
= m_urlNavigator
->locationUrl();
343 m_searchBox
->fromSearchUrl(locationUrl
);
345 m_view
->setViewPropertiesContext(QString());
347 // Restore the URL for the URL navigator. If Dolphin has been
348 // started with a search-URL, the home URL is used as fallback.
349 KUrl url
= m_searchBox
->searchPath();
350 if (url
.isEmpty() || !url
.isValid() || isSearchUrl(url
)) {
351 url
= GeneralSettings::self()->homeUrl();
353 m_urlNavigator
->setLocationUrl(url
);
357 bool DolphinViewContainer::isSearchModeEnabled() const
359 return m_searchBox
->isVisible();
362 QString
DolphinViewContainer::placesText() const
366 if (isSearchModeEnabled()) {
367 text
= m_searchBox
->searchPath().fileName() + QLatin1String(": ") + m_searchBox
->text();
369 text
= url().fileName();
370 if (text
.isEmpty()) {
378 void DolphinViewContainer::setUrl(const KUrl
& 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 KUrl
& url
)
485 const KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, url
);
486 slotItemActivated(item
);
489 void DolphinViewContainer::slotItemActivated(const KFileItem
& item
)
491 // It is possible to activate items on inactive views by
492 // drag & drop operations. Assure that activating an item always
493 // results in an active view.
494 m_view
->setActive(true);
496 const KUrl
& url
= DolphinView::openItemAsFolderUrl(item
, GeneralSettings::browseThroughArchives());
497 if (!url
.isEmpty()) {
502 new KRun(item
.targetUrl(), this);
505 void DolphinViewContainer::slotItemsActivated(const KFileItemList
& items
)
507 Q_ASSERT(items
.count() >= 2);
509 KFileItemActions
fileItemActions(this);
510 fileItemActions
.runPreferredApplications(items
, QString());
513 void DolphinViewContainer::showItemInfo(const KFileItem
& item
)
516 m_statusBar
->resetToDefaultText();
518 m_statusBar
->setText(item
.getStatusBarInfo());
522 void DolphinViewContainer::closeFilterBar()
524 m_filterBar
->closeFilterBar();
526 emit
showFilterBarChanged(false);
529 void DolphinViewContainer::setNameFilter(const QString
& nameFilter
)
531 m_view
->setNameFilter(nameFilter
);
532 delayedStatusBarUpdate();
535 void DolphinViewContainer::activate()
540 void DolphinViewContainer::slotViewUrlAboutToBeChanged(const KUrl
& url
)
542 // URL changes of the view can happen in two ways:
543 // 1. The URL navigator gets changed and will trigger the view to update its URL
544 // 2. The URL of the view gets changed and will trigger the URL navigator to update
545 // its URL (e.g. by clicking on an item)
546 // In this scope the view-state may only get saved in case 2:
547 if (url
!= m_urlNavigator
->locationUrl()) {
552 void DolphinViewContainer::slotUrlNavigatorLocationAboutToBeChanged(const KUrl
& url
)
554 // URL changes of the view can happen in two ways:
555 // 1. The URL navigator gets changed and will trigger the view to update its URL
556 // 2. The URL of the view gets changed and will trigger the URL navigator to update
557 // its URL (e.g. by clicking on an item)
558 // In this scope the view-state may only get saved in case 1:
559 if (url
!= m_view
->url()) {
564 void DolphinViewContainer::slotUrlNavigatorLocationChanged(const KUrl
& url
)
568 if (KProtocolManager::supportsListing(url
)) {
569 setSearchModeEnabled(isSearchUrl(url
));
572 if (m_autoGrabFocus
&& isActive() && !isSearchUrl(url
)) {
573 // When an URL has been entered, the view should get the focus.
574 // The focus must be requested asynchronously, as changing the URL might create
575 // a new view widget.
576 QTimer::singleShot(0, this, SLOT(requestFocus()));
578 } else if (KProtocolManager::isSourceProtocol(url
)) {
579 QString app
= "konqueror";
580 if (url
.protocol().startsWith(QLatin1String("http"))) {
581 showMessage(i18nc("@info:status", // krazy:exclude=qmethods
582 "Dolphin does not support web pages, the web browser has been launched"),
585 const KConfigGroup
config(KSharedConfig::openConfig("kdeglobals"), "General");
586 const QString browser
= config
.readEntry("BrowserApplication");
587 if (!browser
.isEmpty()) {
589 if (app
.startsWith('!')) {
590 // a literal command has been configured, remove the '!' prefix
595 showMessage(i18nc("@info:status",
596 "Protocol not supported by Dolphin, Konqueror has been launched"),
600 const QString secureUrl
= KShell::quoteArg(url
.pathOrUrl());
601 const QString command
= app
+ ' ' + secureUrl
;
602 KRun::runCommand(command
, app
, app
, this);
604 showMessage(i18nc("@info:status", "Invalid protocol"), Error
);
608 void DolphinViewContainer::dropUrls(const KUrl
& destination
, QDropEvent
* event
)
610 m_dropDestination
= destination
;
612 const QMimeData
* mimeData
= event
->mimeData();
613 QMimeData
* mimeDataCopy
= new QMimeData
;
614 foreach (const QString
& format
, mimeData
->formats()) {
615 mimeDataCopy
->setData(format
, mimeData
->data(format
));
618 m_dropEvent
.reset(new QDropEvent(event
->pos(),
619 event
->possibleActions(),
621 event
->mouseButtons(),
622 event
->keyboardModifiers()));
624 QTimer::singleShot(0, this, SLOT(dropUrlsDelayed()));
627 void DolphinViewContainer::dropUrlsDelayed()
629 if (m_dropEvent
.isNull()) {
634 DragAndDropHelper::dropUrls(KFileItem(), m_dropDestination
, m_dropEvent
.data(), error
);
635 if (!error
.isEmpty()) {
636 showMessage(error
, Error
);
639 delete m_dropEvent
->mimeData();
643 void DolphinViewContainer::redirect(const KUrl
& oldUrl
, const KUrl
& newUrl
)
646 const bool block
= m_urlNavigator
->signalsBlocked();
647 m_urlNavigator
->blockSignals(true);
649 // Assure that the location state is reset for redirection URLs. This
650 // allows to skip redirection URLs when going back or forward in the
652 m_urlNavigator
->saveLocationState(QByteArray());
653 m_urlNavigator
->setLocationUrl(newUrl
);
654 setSearchModeEnabled(isSearchUrl(newUrl
));
656 m_urlNavigator
->blockSignals(block
);
659 void DolphinViewContainer::requestFocus()
664 void DolphinViewContainer::saveUrlCompletionMode(KCompletion::CompletionMode completion
)
666 GeneralSettings::setUrlCompletionMode(completion
);
669 void DolphinViewContainer::slotHistoryChanged()
671 QByteArray locationState
= m_urlNavigator
->locationState();
672 if (!locationState
.isEmpty()) {
673 QDataStream
stream(&locationState
, QIODevice::ReadOnly
);
674 m_view
->restoreState(stream
);
678 void DolphinViewContainer::slotReturnPressed()
680 if (!GeneralSettings::editableUrl()) {
681 m_urlNavigator
->setUrlEditable(false);
685 void DolphinViewContainer::startSearching()
687 const KUrl url
= m_searchBox
->urlForSearching();
688 if (url
.isValid() && !url
.isEmpty()) {
689 m_view
->setViewPropertiesContext("search");
690 m_urlNavigator
->setLocationUrl(url
);
694 void DolphinViewContainer::closeSearchBox()
696 setSearchModeEnabled(false);
699 void DolphinViewContainer::stopDirectoryLoading()
701 m_view
->stopLoading();
702 m_statusBar
->setProgress(100);
705 void DolphinViewContainer::slotStatusBarZoomLevelChanged(int zoomLevel
)
707 m_view
->setZoomLevel(zoomLevel
);
710 void DolphinViewContainer::showErrorMessage(const QString
& msg
)
712 showMessage(msg
, Error
);
715 bool DolphinViewContainer::isSearchUrl(const KUrl
& url
) const
717 const QString protocol
= url
.protocol();
718 return protocol
.contains("search");
721 void DolphinViewContainer::saveViewState()
723 QByteArray locationState
;
724 QDataStream
stream(&locationState
, QIODevice::WriteOnly
);
725 m_view
->saveState(stream
);
726 m_urlNavigator
->saveLocationState(locationState
);