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::directoryLoadingCompleted
,
116 m_messageWidget
, &KMessageWidget::hide
);
117 connect(m_view
, &DolphinView::writeStateChanged
,
118 this, &DolphinViewContainer::writeStateChanged
);
119 connect(m_view
, &DolphinView::requestItemInfo
,
120 this, &DolphinViewContainer::showItemInfo
);
121 connect(m_view
, &DolphinView::itemActivated
,
122 this, &DolphinViewContainer::slotItemActivated
);
123 connect(m_view
, &DolphinView::itemsActivated
,
124 this, &DolphinViewContainer::slotItemsActivated
);
125 connect(m_view
, &DolphinView::redirection
,
126 this, &DolphinViewContainer::redirect
);
127 connect(m_view
, &DolphinView::directoryLoadingStarted
,
128 this, &DolphinViewContainer::slotDirectoryLoadingStarted
);
129 connect(m_view
, &DolphinView::directoryLoadingCompleted
,
130 this, &DolphinViewContainer::slotDirectoryLoadingCompleted
);
131 connect(m_view
, &DolphinView::directoryLoadingCanceled
,
132 this, &DolphinViewContainer::slotDirectoryLoadingCanceled
);
133 connect(m_view
, &DolphinView::itemCountChanged
,
134 this, &DolphinViewContainer::delayedStatusBarUpdate
);
135 connect(m_view
, &DolphinView::directoryLoadingProgress
,
136 this, &DolphinViewContainer::updateDirectoryLoadingProgress
);
137 connect(m_view
, &DolphinView::directorySortingProgress
,
138 this, &DolphinViewContainer::updateDirectorySortingProgress
);
139 connect(m_view
, &DolphinView::selectionChanged
,
140 this, &DolphinViewContainer::delayedStatusBarUpdate
);
141 connect(m_view
, &DolphinView::urlAboutToBeChanged
,
142 this, &DolphinViewContainer::slotViewUrlAboutToBeChanged
);
143 connect(m_view
, &DolphinView::errorMessage
,
144 this, &DolphinViewContainer::showErrorMessage
);
145 connect(m_view
, &DolphinView::urlIsFileError
,
146 this, &DolphinViewContainer::slotUrlIsFileError
);
147 connect(m_view
, &DolphinView::activated
,
148 this, &DolphinViewContainer::activate
);
150 connect(m_urlNavigator
, &KUrlNavigator::urlAboutToBeChanged
,
151 this, &DolphinViewContainer::slotUrlNavigatorLocationAboutToBeChanged
);
152 connect(m_urlNavigator
, &KUrlNavigator::urlChanged
,
153 this, &DolphinViewContainer::slotUrlNavigatorLocationChanged
);
154 connect(m_urlNavigator
, &KUrlNavigator::historyChanged
,
155 this, &DolphinViewContainer::slotHistoryChanged
);
156 connect(m_urlNavigator
, &KUrlNavigator::returnPressed
,
157 this, &DolphinViewContainer::slotReturnPressed
);
159 // Initialize status bar
160 m_statusBar
= new DolphinStatusBar(this);
161 m_statusBar
->setUrl(m_view
->url());
162 m_statusBar
->setZoomLevel(m_view
->zoomLevel());
163 connect(m_view
, &DolphinView::urlChanged
,
164 m_statusBar
, &DolphinStatusBar::setUrl
);
165 connect(m_view
, &DolphinView::zoomLevelChanged
,
166 m_statusBar
, &DolphinStatusBar::setZoomLevel
);
167 connect(m_view
, &DolphinView::infoMessage
,
168 m_statusBar
, &DolphinStatusBar::setText
);
169 connect(m_view
, &DolphinView::operationCompletedMessage
,
170 m_statusBar
, &DolphinStatusBar::setText
);
171 connect(m_statusBar
, &DolphinStatusBar::stopPressed
,
172 this, &DolphinViewContainer::stopDirectoryLoading
);
173 connect(m_statusBar
, &DolphinStatusBar::zoomLevelChanged
,
174 this, &DolphinViewContainer::slotStatusBarZoomLevelChanged
);
176 m_statusBarTimer
= new QTimer(this);
177 m_statusBarTimer
->setSingleShot(true);
178 m_statusBarTimer
->setInterval(300);
179 connect(m_statusBarTimer
, &QTimer::timeout
, this, &DolphinViewContainer::updateStatusBar
);
181 KIO::FileUndoManager
* undoManager
= KIO::FileUndoManager::self();
182 connect(undoManager
, &KIO::FileUndoManager::jobRecordingFinished
,
183 this, &DolphinViewContainer::delayedStatusBarUpdate
);
185 // Initialize filter bar
186 m_filterBar
= new FilterBar(this);
187 m_filterBar
->setVisible(settings
->filterBar());
188 connect(m_filterBar
, &FilterBar::filterChanged
,
189 this, &DolphinViewContainer::setNameFilter
);
190 connect(m_filterBar
, &FilterBar::closeRequest
,
191 this, &DolphinViewContainer::closeFilterBar
);
192 connect(m_filterBar
, &FilterBar::focusViewRequest
,
193 this, &DolphinViewContainer::requestFocus
);
194 connect(m_view
, &DolphinView::urlChanged
,
195 m_filterBar
, &FilterBar::slotUrlChanged
);
197 m_topLayout
->addWidget(m_urlNavigator
);
198 m_topLayout
->addWidget(m_searchBox
);
199 m_topLayout
->addWidget(m_messageWidget
);
200 m_topLayout
->addWidget(m_view
);
201 m_topLayout
->addWidget(m_filterBar
);
202 m_topLayout
->addWidget(m_statusBar
);
204 setSearchModeEnabled(isSearchUrl(url
));
206 // Initialize kactivities resource instance
208 #ifdef KActivities_FOUND
209 m_activityResourceInstance
= new KActivities::ResourceInstance(
210 window()->winId(), url
);
211 m_activityResourceInstance
->setParent(this);
215 DolphinViewContainer::~DolphinViewContainer()
219 KUrl
DolphinViewContainer::url() const
221 return m_view
->url();
224 void DolphinViewContainer::setActive(bool active
)
226 m_searchBox
->setActive(active
);
227 m_urlNavigator
->setActive(active
);
228 m_view
->setActive(active
);
230 #ifdef KActivities_FOUND
232 m_activityResourceInstance
->notifyFocusedIn();
234 m_activityResourceInstance
->notifyFocusedOut();
239 bool DolphinViewContainer::isActive() const
241 Q_ASSERT(m_view
->isActive() == m_urlNavigator
->isActive());
242 return m_view
->isActive();
245 void DolphinViewContainer::setAutoGrabFocus(bool grab
)
247 m_autoGrabFocus
= grab
;
250 bool DolphinViewContainer::autoGrabFocus() const
252 return m_autoGrabFocus
;
255 const DolphinStatusBar
* DolphinViewContainer::statusBar() const
260 DolphinStatusBar
* DolphinViewContainer::statusBar()
265 const KUrlNavigator
* DolphinViewContainer::urlNavigator() const
267 return m_urlNavigator
;
270 KUrlNavigator
* DolphinViewContainer::urlNavigator()
272 return m_urlNavigator
;
275 const DolphinView
* DolphinViewContainer::view() const
280 DolphinView
* DolphinViewContainer::view()
285 void DolphinViewContainer::showMessage(const QString
& msg
, MessageType type
)
291 m_messageWidget
->setText(msg
);
294 case Information
: m_messageWidget
->setMessageType(KMessageWidget::Information
); break;
295 case Warning
: m_messageWidget
->setMessageType(KMessageWidget::Warning
); break;
296 case Error
: m_messageWidget
->setMessageType(KMessageWidget::Error
); break;
302 m_messageWidget
->setWordWrap(false);
303 const int unwrappedWidth
= m_messageWidget
->sizeHint().width();
304 m_messageWidget
->setWordWrap(unwrappedWidth
> size().width());
306 if (m_messageWidget
->isVisible()) {
307 m_messageWidget
->hide();
309 m_messageWidget
->animatedShow();
312 void DolphinViewContainer::readSettings()
314 if (GeneralSettings::modifiedStartupSettings()) {
315 // The startup settings should only get applied if they have been
316 // modified by the user. Otherwise keep the (possibly) different current
317 // settings of the URL navigator and the filterbar.
318 m_urlNavigator
->setUrlEditable(GeneralSettings::editableUrl());
319 m_urlNavigator
->setShowFullPath(GeneralSettings::showFullPath());
320 m_urlNavigator
->setHomeUrl(KUrl(GeneralSettings::homeUrl()));
321 setFilterBarVisible(GeneralSettings::filterBar());
324 m_view
->readSettings();
325 m_statusBar
->readSettings();
328 bool DolphinViewContainer::isFilterBarVisible() const
330 return m_filterBar
->isVisible();
333 void DolphinViewContainer::setSearchModeEnabled(bool enabled
)
335 if (enabled
== isSearchModeEnabled()) {
336 if (enabled
&& !m_searchBox
->hasFocus()) {
337 m_searchBox
->setFocus();
338 m_searchBox
->selectAll();
343 m_searchBox
->setVisible(enabled
);
344 m_urlNavigator
->setVisible(!enabled
);
347 const KUrl
& locationUrl
= m_urlNavigator
->locationUrl();
348 m_searchBox
->fromSearchUrl(locationUrl
);
350 m_view
->setViewPropertiesContext(QString());
352 // Restore the URL for the URL navigator. If Dolphin has been
353 // started with a search-URL, the home URL is used as fallback.
354 KUrl url
= m_searchBox
->searchPath();
355 if (url
.isEmpty() || !url
.isValid() || isSearchUrl(url
)) {
356 url
= GeneralSettings::self()->homeUrl();
358 m_urlNavigator
->setLocationUrl(url
);
362 bool DolphinViewContainer::isSearchModeEnabled() const
364 return m_searchBox
->isVisible();
367 QString
DolphinViewContainer::placesText() const
371 if (isSearchModeEnabled()) {
372 text
= m_searchBox
->searchPath().fileName() + QLatin1String(": ") + m_searchBox
->text();
374 text
= url().fileName();
375 if (text
.isEmpty()) {
383 void DolphinViewContainer::setUrl(const KUrl
& newUrl
)
385 if (newUrl
!= m_urlNavigator
->locationUrl()) {
386 m_urlNavigator
->setLocationUrl(newUrl
);
389 #ifdef KActivities_FOUND
390 m_activityResourceInstance
->setUri(newUrl
);
394 void DolphinViewContainer::setFilterBarVisible(bool visible
)
396 Q_ASSERT(m_filterBar
);
399 m_filterBar
->setFocus();
400 m_filterBar
->selectAll();
406 void DolphinViewContainer::delayedStatusBarUpdate()
408 if (m_statusBarTimer
->isActive() && (m_statusBarTimestamp
.elapsed() > 2000)) {
409 // No update of the statusbar has been done during the last 2 seconds,
410 // although an update has been requested. Trigger an immediate update.
411 m_statusBarTimer
->stop();
414 // Invoke updateStatusBar() with a small delay. This assures that
415 // when a lot of delayedStatusBarUpdates() are done in a short time,
416 // no bottleneck is given.
417 m_statusBarTimer
->start();
421 void DolphinViewContainer::updateStatusBar()
423 m_statusBarTimestamp
.start();
425 const QString text
= m_view
->statusBarText();
426 m_statusBar
->setDefaultText(text
);
427 m_statusBar
->resetToDefaultText();
430 void DolphinViewContainer::updateDirectoryLoadingProgress(int percent
)
432 if (m_statusBar
->progressText().isEmpty()) {
433 m_statusBar
->setProgressText(i18nc("@info:progress", "Loading folder..."));
435 m_statusBar
->setProgress(percent
);
438 void DolphinViewContainer::updateDirectorySortingProgress(int percent
)
440 if (m_statusBar
->progressText().isEmpty()) {
441 m_statusBar
->setProgressText(i18nc("@info:progress", "Sorting..."));
443 m_statusBar
->setProgress(percent
);
446 void DolphinViewContainer::slotDirectoryLoadingStarted()
448 if (isSearchUrl(url())) {
449 // Search KIO-slaves usually don't provide any progress information. Give
450 // a hint to the user that a searching is done:
452 m_statusBar
->setProgressText(i18nc("@info", "Searching..."));
453 m_statusBar
->setProgress(-1);
455 // Trigger an undetermined progress indication. The progress
456 // information in percent will be triggered by the percent() signal
457 // of the directory lister later.
458 updateDirectoryLoadingProgress(-1);
462 void DolphinViewContainer::slotDirectoryLoadingCompleted()
464 if (!m_statusBar
->progressText().isEmpty()) {
465 m_statusBar
->setProgressText(QString());
466 m_statusBar
->setProgress(100);
469 if (isSearchUrl(url()) && m_view
->itemsCount() == 0) {
470 // The dir lister has been completed on a Baloo-URI and no items have been found. Instead
471 // of showing the default status bar information ("0 items") a more helpful information is given:
472 m_statusBar
->setText(i18nc("@info:status", "No items found."));
478 void DolphinViewContainer::slotDirectoryLoadingCanceled()
480 if (!m_statusBar
->progressText().isEmpty()) {
481 m_statusBar
->setProgressText(QString());
482 m_statusBar
->setProgress(100);
485 m_statusBar
->setText(QString());
488 void DolphinViewContainer::slotUrlIsFileError(const KUrl
& url
)
490 const KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, url
);
491 slotItemActivated(item
);
494 void DolphinViewContainer::slotItemActivated(const KFileItem
& item
)
496 // It is possible to activate items on inactive views by
497 // drag & drop operations. Assure that activating an item always
498 // results in an active view.
499 m_view
->setActive(true);
501 const KUrl
& url
= DolphinView::openItemAsFolderUrl(item
, GeneralSettings::browseThroughArchives());
502 if (!url
.isEmpty()) {
507 new KRun(item
.targetUrl(), this);
510 void DolphinViewContainer::slotItemsActivated(const KFileItemList
& items
)
512 Q_ASSERT(items
.count() >= 2);
514 KFileItemActions
fileItemActions(this);
515 fileItemActions
.runPreferredApplications(items
, QString());
518 void DolphinViewContainer::showItemInfo(const KFileItem
& item
)
521 m_statusBar
->resetToDefaultText();
523 m_statusBar
->setText(item
.getStatusBarInfo());
527 void DolphinViewContainer::closeFilterBar()
529 m_filterBar
->closeFilterBar();
531 emit
showFilterBarChanged(false);
534 void DolphinViewContainer::setNameFilter(const QString
& nameFilter
)
536 m_view
->setNameFilter(nameFilter
);
537 delayedStatusBarUpdate();
540 void DolphinViewContainer::activate()
545 void DolphinViewContainer::slotViewUrlAboutToBeChanged(const KUrl
& url
)
547 // URL changes of the view can happen in two ways:
548 // 1. The URL navigator gets changed and will trigger the view to update its URL
549 // 2. The URL of the view gets changed and will trigger the URL navigator to update
550 // its URL (e.g. by clicking on an item)
551 // In this scope the view-state may only get saved in case 2:
552 if (url
!= m_urlNavigator
->locationUrl()) {
557 void DolphinViewContainer::slotUrlNavigatorLocationAboutToBeChanged(const KUrl
& url
)
559 // URL changes of the view can happen in two ways:
560 // 1. The URL navigator gets changed and will trigger the view to update its URL
561 // 2. The URL of the view gets changed and will trigger the URL navigator to update
562 // its URL (e.g. by clicking on an item)
563 // In this scope the view-state may only get saved in case 1:
564 if (url
!= m_view
->url()) {
569 void DolphinViewContainer::slotUrlNavigatorLocationChanged(const KUrl
& url
)
573 if (KProtocolManager::supportsListing(url
)) {
574 setSearchModeEnabled(isSearchUrl(url
));
577 if (m_autoGrabFocus
&& isActive() && !isSearchUrl(url
)) {
578 // When an URL has been entered, the view should get the focus.
579 // The focus must be requested asynchronously, as changing the URL might create
580 // a new view widget.
581 QTimer::singleShot(0, this, SLOT(requestFocus()));
583 } else if (KProtocolManager::isSourceProtocol(url
)) {
584 QString app
= "konqueror";
585 if (url
.protocol().startsWith(QLatin1String("http"))) {
586 showMessage(i18nc("@info:status", // krazy:exclude=qmethods
587 "Dolphin does not support web pages, the web browser has been launched"),
590 const KConfigGroup
config(KSharedConfig::openConfig("kdeglobals"), "General");
591 const QString browser
= config
.readEntry("BrowserApplication");
592 if (!browser
.isEmpty()) {
594 if (app
.startsWith('!')) {
595 // a literal command has been configured, remove the '!' prefix
600 showMessage(i18nc("@info:status",
601 "Protocol not supported by Dolphin, Konqueror has been launched"),
605 const QString secureUrl
= KShell::quoteArg(url
.pathOrUrl());
606 const QString command
= app
+ ' ' + secureUrl
;
607 KRun::runCommand(command
, app
, app
, this);
609 showMessage(i18nc("@info:status", "Invalid protocol"), Error
);
613 void DolphinViewContainer::dropUrls(const KUrl
& destination
, QDropEvent
* event
)
615 m_dropDestination
= destination
;
617 const QMimeData
* mimeData
= event
->mimeData();
618 QMimeData
* mimeDataCopy
= new QMimeData
;
619 foreach (const QString
& format
, mimeData
->formats()) {
620 mimeDataCopy
->setData(format
, mimeData
->data(format
));
623 m_dropEvent
.reset(new QDropEvent(event
->pos(),
624 event
->possibleActions(),
626 event
->mouseButtons(),
627 event
->keyboardModifiers()));
629 QTimer::singleShot(0, this, SLOT(dropUrlsDelayed()));
632 void DolphinViewContainer::dropUrlsDelayed()
634 if (m_dropEvent
.isNull()) {
639 DragAndDropHelper::dropUrls(KFileItem(), m_dropDestination
, m_dropEvent
.data(), error
);
640 if (!error
.isEmpty()) {
641 showMessage(error
, Error
);
644 delete m_dropEvent
->mimeData();
648 void DolphinViewContainer::redirect(const KUrl
& oldUrl
, const KUrl
& newUrl
)
651 const bool block
= m_urlNavigator
->signalsBlocked();
652 m_urlNavigator
->blockSignals(true);
654 // Assure that the location state is reset for redirection URLs. This
655 // allows to skip redirection URLs when going back or forward in the
657 m_urlNavigator
->saveLocationState(QByteArray());
658 m_urlNavigator
->setLocationUrl(newUrl
);
659 setSearchModeEnabled(isSearchUrl(newUrl
));
661 m_urlNavigator
->blockSignals(block
);
664 void DolphinViewContainer::requestFocus()
669 void DolphinViewContainer::saveUrlCompletionMode(KCompletion::CompletionMode completion
)
671 GeneralSettings::setUrlCompletionMode(completion
);
674 void DolphinViewContainer::slotHistoryChanged()
676 QByteArray locationState
= m_urlNavigator
->locationState();
677 if (!locationState
.isEmpty()) {
678 QDataStream
stream(&locationState
, QIODevice::ReadOnly
);
679 m_view
->restoreState(stream
);
683 void DolphinViewContainer::slotReturnPressed()
685 if (!GeneralSettings::editableUrl()) {
686 m_urlNavigator
->setUrlEditable(false);
690 void DolphinViewContainer::startSearching()
692 const KUrl url
= m_searchBox
->urlForSearching();
693 if (url
.isValid() && !url
.isEmpty()) {
694 m_view
->setViewPropertiesContext("search");
695 m_urlNavigator
->setLocationUrl(url
);
699 void DolphinViewContainer::closeSearchBox()
701 setSearchModeEnabled(false);
704 void DolphinViewContainer::stopDirectoryLoading()
706 m_view
->stopLoading();
707 m_statusBar
->setProgress(100);
710 void DolphinViewContainer::slotStatusBarZoomLevelChanged(int zoomLevel
)
712 m_view
->setZoomLevel(zoomLevel
);
715 void DolphinViewContainer::showErrorMessage(const QString
& msg
)
717 showMessage(msg
, Error
);
720 bool DolphinViewContainer::isSearchUrl(const KUrl
& url
) const
722 const QString protocol
= url
.protocol();
723 return protocol
.contains("search");
726 void DolphinViewContainer::saveViewState()
728 QByteArray locationState
;
729 QDataStream
stream(&locationState
, QIODevice::WriteOnly
);
730 m_view
->saveState(stream
);
731 m_urlNavigator
->saveLocationState(locationState
);