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>
30 #include <KDesktopFile>
31 #include <KFileItemDelegate>
32 #include <KFileItemActions>
33 #include <KFilePlacesModel>
35 #include <KIconEffect>
36 #include <KIO/NetAccess>
37 #include <KIO/PreviewJob>
38 #include <KMessageWidget>
39 #include <KNewFileMenu>
40 #include <konqmimedata.h>
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
, SIGNAL(urlsDropped(KUrl
,QDropEvent
*)),
86 this, SLOT(dropUrls(KUrl
,QDropEvent
*)));
87 connect(m_urlNavigator
, SIGNAL(activated()),
88 this, SLOT(activate()));
89 connect(m_urlNavigator
->editor(), SIGNAL(completionModeChanged(KGlobalSettings::Completion
)),
90 this, SLOT(saveUrlCompletionMode(KGlobalSettings::Completion
)));
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(KGlobalSettings::Completion(settings
->urlCompletionMode()));
99 m_searchBox
= new DolphinSearchBox(this);
101 connect(m_searchBox
, SIGNAL(activated()), this, SLOT(activate()));
102 connect(m_searchBox
, SIGNAL(closeRequest()), this, SLOT(closeSearchBox()));
103 connect(m_searchBox
, SIGNAL(searchRequest()), this, SLOT(startSearching()));
104 connect(m_searchBox
, SIGNAL(returnPressed(QString
)), this, SLOT(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
, SIGNAL(urlChanged(KUrl
)), m_urlNavigator
, SLOT(setUrl(KUrl
)));
112 connect(m_view
, SIGNAL(urlChanged(KUrl
)), m_messageWidget
, SLOT(hide()));
113 connect(m_view
, SIGNAL(writeStateChanged(bool)), this, SIGNAL(writeStateChanged(bool)));
114 connect(m_view
, SIGNAL(requestItemInfo(KFileItem
)), this, SLOT(showItemInfo(KFileItem
)));
115 connect(m_view
, SIGNAL(itemActivated(KFileItem
)), this, SLOT(slotItemActivated(KFileItem
)));
116 connect(m_view
, SIGNAL(itemsActivated(KFileItemList
)), this, SLOT(slotItemsActivated(KFileItemList
)));
117 connect(m_view
, SIGNAL(redirection(KUrl
,KUrl
)), this, SLOT(redirect(KUrl
,KUrl
)));
118 connect(m_view
, SIGNAL(directoryLoadingStarted()), this, SLOT(slotDirectoryLoadingStarted()));
119 connect(m_view
, SIGNAL(directoryLoadingCompleted()), this, SLOT(slotDirectoryLoadingCompleted()));
120 connect(m_view
, SIGNAL(directoryLoadingCanceled()), this, SLOT(slotDirectoryLoadingCanceled()));
121 connect(m_view
, SIGNAL(itemCountChanged()), this, SLOT(delayedStatusBarUpdate()));
122 connect(m_view
, SIGNAL(directoryLoadingProgress(int)), this, SLOT(updateDirectoryLoadingProgress(int)));
123 connect(m_view
, SIGNAL(directorySortingProgress(int)), this, SLOT(updateDirectorySortingProgress(int)));
124 connect(m_view
, SIGNAL(selectionChanged(KFileItemList
)), this, SLOT(delayedStatusBarUpdate()));
125 connect(m_view
, SIGNAL(urlAboutToBeChanged(KUrl
)), this, SLOT(slotViewUrlAboutToBeChanged(KUrl
)));
126 connect(m_view
, SIGNAL(errorMessage(QString
)), this, SLOT(showErrorMessage(QString
)));
127 connect(m_view
, SIGNAL(urlIsFileError(KUrl
)), this, SLOT(slotUrlIsFileError(KUrl
)));
129 connect(m_urlNavigator
, SIGNAL(urlAboutToBeChanged(KUrl
)),
130 this, SLOT(slotUrlNavigatorLocationAboutToBeChanged(KUrl
)));
131 connect(m_urlNavigator
, SIGNAL(urlChanged(KUrl
)),
132 this, SLOT(slotUrlNavigatorLocationChanged(KUrl
)));
133 connect(m_urlNavigator
, SIGNAL(historyChanged()),
134 this, SLOT(slotHistoryChanged()));
135 connect(m_urlNavigator
, SIGNAL(returnPressed()),
136 this, SLOT(slotReturnPressed()));
138 // Initialize status bar
139 m_statusBar
= new DolphinStatusBar(this);
140 m_statusBar
->setUrl(m_view
->url());
141 m_statusBar
->setZoomLevel(m_view
->zoomLevel());
142 connect(m_view
, SIGNAL(urlChanged(KUrl
)), m_statusBar
, SLOT(setUrl(KUrl
)));
143 connect(m_view
, SIGNAL(zoomLevelChanged(int,int)), m_statusBar
, SLOT(setZoomLevel(int)));
144 connect(m_view
, SIGNAL(infoMessage(QString
)), m_statusBar
, SLOT(setText(QString
)));
145 connect(m_view
, SIGNAL(operationCompletedMessage(QString
)), m_statusBar
, SLOT(setText(QString
)));
146 connect(m_statusBar
, SIGNAL(stopPressed()), this, SLOT(stopDirectoryLoading()));
147 connect(m_statusBar
, SIGNAL(zoomLevelChanged(int)), this, SLOT(slotStatusBarZoomLevelChanged(int)));
149 m_statusBarTimer
= new QTimer(this);
150 m_statusBarTimer
->setSingleShot(true);
151 m_statusBarTimer
->setInterval(300);
152 connect(m_statusBarTimer
, SIGNAL(timeout()), this, SLOT(updateStatusBar()));
154 KIO::FileUndoManager
* undoManager
= KIO::FileUndoManager::self();
155 connect(undoManager
, SIGNAL(jobRecordingFinished(CommandType
)),
156 this, SLOT(delayedStatusBarUpdate()));
158 // Initialize filter bar
159 m_filterBar
= new FilterBar(this);
160 m_filterBar
->setVisible(settings
->filterBar());
161 connect(m_filterBar
, SIGNAL(filterChanged(QString
)),
162 this, SLOT(setNameFilter(QString
)));
163 connect(m_filterBar
, SIGNAL(closeRequest()),
164 this, SLOT(closeFilterBar()));
165 connect(m_filterBar
, SIGNAL(focusViewRequest()),
166 this, SLOT(requestFocus()));
167 connect(m_view
, SIGNAL(urlChanged(KUrl
)),
168 m_filterBar
, SLOT(slotUrlChanged()));
170 m_topLayout
->addWidget(m_urlNavigator
);
171 m_topLayout
->addWidget(m_searchBox
);
172 m_topLayout
->addWidget(m_messageWidget
);
173 m_topLayout
->addWidget(m_view
);
174 m_topLayout
->addWidget(m_filterBar
);
175 m_topLayout
->addWidget(m_statusBar
);
177 setSearchModeEnabled(isSearchUrl(url
));
179 // Initialize kactivities resource instance
181 #ifdef KActivities_FOUND
182 m_activityResourceInstance
= new KActivities::ResourceInstance(
183 window()->winId(), url
);
184 m_activityResourceInstance
->setParent(this);
188 DolphinViewContainer::~DolphinViewContainer()
192 KUrl
DolphinViewContainer::url() const
194 return m_view
->url();
197 void DolphinViewContainer::setActive(bool active
)
199 m_searchBox
->setActive(active
);
200 m_urlNavigator
->setActive(active
);
201 m_view
->setActive(active
);
203 #ifdef KActivities_FOUND
205 m_activityResourceInstance
->notifyFocusedIn();
207 m_activityResourceInstance
->notifyFocusedOut();
212 bool DolphinViewContainer::isActive() const
214 Q_ASSERT(m_view
->isActive() == m_urlNavigator
->isActive());
215 return m_view
->isActive();
218 void DolphinViewContainer::setAutoGrabFocus(bool grab
)
220 m_autoGrabFocus
= grab
;
223 bool DolphinViewContainer::autoGrabFocus() const
225 return m_autoGrabFocus
;
228 const DolphinStatusBar
* DolphinViewContainer::statusBar() const
233 DolphinStatusBar
* DolphinViewContainer::statusBar()
238 const KUrlNavigator
* DolphinViewContainer::urlNavigator() const
240 return m_urlNavigator
;
243 KUrlNavigator
* DolphinViewContainer::urlNavigator()
245 return m_urlNavigator
;
248 const DolphinView
* DolphinViewContainer::view() const
253 DolphinView
* DolphinViewContainer::view()
258 void DolphinViewContainer::showMessage(const QString
& msg
, MessageType type
)
264 m_messageWidget
->setText(msg
);
267 case Information
: m_messageWidget
->setMessageType(KMessageWidget::Information
); break;
268 case Warning
: m_messageWidget
->setMessageType(KMessageWidget::Warning
); break;
269 case Error
: m_messageWidget
->setMessageType(KMessageWidget::Error
); break;
275 m_messageWidget
->setWordWrap(false);
276 const int unwrappedWidth
= m_messageWidget
->sizeHint().width();
277 m_messageWidget
->setWordWrap(unwrappedWidth
> size().width());
279 m_messageWidget
->animatedShow();
282 void DolphinViewContainer::readSettings()
284 if (GeneralSettings::modifiedStartupSettings()) {
285 // The startup settings should only get applied if they have been
286 // modified by the user. Otherwise keep the (possibly) different current
287 // settings of the URL navigator and the filterbar.
288 m_urlNavigator
->setUrlEditable(GeneralSettings::editableUrl());
289 m_urlNavigator
->setShowFullPath(GeneralSettings::showFullPath());
290 m_urlNavigator
->setHomeUrl(KUrl(GeneralSettings::homeUrl()));
291 setFilterBarVisible(GeneralSettings::filterBar());
294 m_view
->readSettings();
295 m_statusBar
->readSettings();
298 bool DolphinViewContainer::isFilterBarVisible() const
300 return m_filterBar
->isVisible();
303 void DolphinViewContainer::setSearchModeEnabled(bool enabled
)
305 if (enabled
== isSearchModeEnabled()) {
306 if (enabled
&& !m_searchBox
->hasFocus()) {
307 m_searchBox
->setFocus();
308 m_searchBox
->selectAll();
313 m_searchBox
->setVisible(enabled
);
314 m_urlNavigator
->setVisible(!enabled
);
317 const KUrl
& locationUrl
= m_urlNavigator
->locationUrl();
318 m_searchBox
->fromSearchUrl(locationUrl
);
320 m_view
->setViewPropertiesContext(QString());
322 // Restore the URL for the URL navigator. If Dolphin has been
323 // started with a search-URL, the home URL is used as fallback.
324 KUrl url
= m_searchBox
->searchPath();
325 if (url
.isEmpty() || !url
.isValid() || isSearchUrl(url
)) {
326 url
= GeneralSettings::self()->homeUrl();
328 m_urlNavigator
->setLocationUrl(url
);
332 bool DolphinViewContainer::isSearchModeEnabled() const
334 return m_searchBox
->isVisible();
337 QString
DolphinViewContainer::placesText() const
341 if (isSearchModeEnabled()) {
342 text
= m_searchBox
->searchPath().fileName() + QLatin1String(": ") + m_searchBox
->text();
344 text
= url().fileName();
345 if (text
.isEmpty()) {
353 void DolphinViewContainer::setUrl(const KUrl
& newUrl
)
355 if (newUrl
!= m_urlNavigator
->locationUrl()) {
356 m_urlNavigator
->setLocationUrl(newUrl
);
359 #ifdef KActivities_FOUND
360 m_activityResourceInstance
->setUri(newUrl
);
364 void DolphinViewContainer::setFilterBarVisible(bool visible
)
366 Q_ASSERT(m_filterBar
);
369 m_filterBar
->setFocus();
370 m_filterBar
->selectAll();
376 void DolphinViewContainer::delayedStatusBarUpdate()
378 if (m_statusBarTimer
->isActive() && (m_statusBarTimestamp
.elapsed() > 2000)) {
379 // No update of the statusbar has been done during the last 2 seconds,
380 // although an update has been requested. Trigger an immediate update.
381 m_statusBarTimer
->stop();
384 // Invoke updateStatusBar() with a small delay. This assures that
385 // when a lot of delayedStatusBarUpdates() are done in a short time,
386 // no bottleneck is given.
387 m_statusBarTimer
->start();
391 void DolphinViewContainer::updateStatusBar()
393 m_statusBarTimestamp
.start();
395 const QString text
= m_view
->statusBarText();
396 m_statusBar
->setDefaultText(text
);
397 m_statusBar
->resetToDefaultText();
400 void DolphinViewContainer::updateDirectoryLoadingProgress(int percent
)
402 if (m_statusBar
->progressText().isEmpty()) {
403 m_statusBar
->setProgressText(i18nc("@info:progress", "Loading folder..."));
405 m_statusBar
->setProgress(percent
);
408 void DolphinViewContainer::updateDirectorySortingProgress(int percent
)
410 if (m_statusBar
->progressText().isEmpty()) {
411 m_statusBar
->setProgressText(i18nc("@info:progress", "Sorting..."));
413 m_statusBar
->setProgress(percent
);
416 void DolphinViewContainer::slotDirectoryLoadingStarted()
418 if (isSearchUrl(url())) {
419 // Search KIO-slaves usually don't provide any progress information. Give
420 // a hint to the user that a searching is done:
422 m_statusBar
->setProgressText(i18nc("@info", "Searching..."));
423 m_statusBar
->setProgress(-1);
425 // Trigger an undetermined progress indication. The progress
426 // information in percent will be triggered by the percent() signal
427 // of the directory lister later.
428 updateDirectoryLoadingProgress(-1);
432 void DolphinViewContainer::slotDirectoryLoadingCompleted()
434 if (!m_statusBar
->progressText().isEmpty()) {
435 m_statusBar
->setProgressText(QString());
436 m_statusBar
->setProgress(100);
439 if (isSearchUrl(url()) && m_view
->itemsCount() == 0) {
440 // The dir lister has been completed on a Baloo-URI and no items have been found. Instead
441 // of showing the default status bar information ("0 items") a more helpful information is given:
442 m_statusBar
->setText(i18nc("@info:status", "No items found."));
448 void DolphinViewContainer::slotDirectoryLoadingCanceled()
450 if (!m_statusBar
->progressText().isEmpty()) {
451 m_statusBar
->setProgressText(QString());
452 m_statusBar
->setProgress(100);
455 m_statusBar
->setText(QString());
458 void DolphinViewContainer::slotUrlIsFileError(const KUrl
& url
)
460 const KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, url
);
461 slotItemActivated(item
);
464 void DolphinViewContainer::slotItemActivated(const KFileItem
& item
)
466 // It is possible to activate items on inactive views by
467 // drag & drop operations. Assure that activating an item always
468 // results in an active view.
469 m_view
->setActive(true);
471 const KUrl
& url
= DolphinView::openItemAsFolderUrl(item
, GeneralSettings::browseThroughArchives());
472 if (!url
.isEmpty()) {
480 void DolphinViewContainer::slotItemsActivated(const KFileItemList
& items
)
482 Q_ASSERT(items
.count() >= 2);
484 KFileItemActions
fileItemActions(this);
485 fileItemActions
.runPreferredApplications(items
, QString());
488 void DolphinViewContainer::showItemInfo(const KFileItem
& item
)
491 m_statusBar
->resetToDefaultText();
493 m_statusBar
->setText(item
.getStatusBarInfo());
497 void DolphinViewContainer::closeFilterBar()
499 m_filterBar
->closeFilterBar();
501 emit
showFilterBarChanged(false);
504 void DolphinViewContainer::setNameFilter(const QString
& nameFilter
)
506 m_view
->setNameFilter(nameFilter
);
507 delayedStatusBarUpdate();
510 void DolphinViewContainer::activate()
515 void DolphinViewContainer::slotViewUrlAboutToBeChanged(const KUrl
& url
)
517 // URL changes of the view can happen in two ways:
518 // 1. The URL navigator gets changed and will trigger the view to update its URL
519 // 2. The URL of the view gets changed and will trigger the URL navigator to update
520 // its URL (e.g. by clicking on an item)
521 // In this scope the view-state may only get saved in case 2:
522 if (url
!= m_urlNavigator
->locationUrl()) {
527 void DolphinViewContainer::slotUrlNavigatorLocationAboutToBeChanged(const KUrl
& url
)
529 // URL changes of the view can happen in two ways:
530 // 1. The URL navigator gets changed and will trigger the view to update its URL
531 // 2. The URL of the view gets changed and will trigger the URL navigator to update
532 // its URL (e.g. by clicking on an item)
533 // In this scope the view-state may only get saved in case 1:
534 if (url
!= m_view
->url()) {
539 void DolphinViewContainer::slotUrlNavigatorLocationChanged(const KUrl
& url
)
543 if (KProtocolManager::supportsListing(url
)) {
544 setSearchModeEnabled(isSearchUrl(url
));
547 if (m_autoGrabFocus
&& isActive() && !isSearchUrl(url
)) {
548 // When an URL has been entered, the view should get the focus.
549 // The focus must be requested asynchronously, as changing the URL might create
550 // a new view widget.
551 QTimer::singleShot(0, this, SLOT(requestFocus()));
553 } else if (KProtocolManager::isSourceProtocol(url
)) {
554 QString app
= "konqueror";
555 if (url
.protocol().startsWith(QLatin1String("http"))) {
556 showMessage(i18nc("@info:status", // krazy:exclude=qmethods
557 "Dolphin does not support web pages, the web browser has been launched"),
560 const KConfigGroup
config(KSharedConfig::openConfig("kdeglobals"), "General");
561 const QString browser
= config
.readEntry("BrowserApplication");
562 if (!browser
.isEmpty()) {
564 if (app
.startsWith('!')) {
565 // a literal command has been configured, remove the '!' prefix
570 showMessage(i18nc("@info:status",
571 "Protocol not supported by Dolphin, Konqueror has been launched"),
575 const QString secureUrl
= KShell::quoteArg(url
.pathOrUrl());
576 const QString command
= app
+ ' ' + secureUrl
;
577 KRun::runCommand(command
, app
, app
, this);
579 showMessage(i18nc("@info:status", "Invalid protocol"), Error
);
583 void DolphinViewContainer::dropUrls(const KUrl
& destination
, QDropEvent
* event
)
585 m_dropDestination
= destination
;
587 const QMimeData
* mimeData
= event
->mimeData();
588 QMimeData
* mimeDataCopy
= new QMimeData
;
589 foreach (const QString
& format
, mimeData
->formats()) {
590 mimeDataCopy
->setData(format
, mimeData
->data(format
));
593 m_dropEvent
.reset(new QDropEvent(event
->pos(),
594 event
->possibleActions(),
596 event
->mouseButtons(),
597 event
->keyboardModifiers()));
599 QTimer::singleShot(0, this, SLOT(dropUrlsDelayed()));
602 void DolphinViewContainer::dropUrlsDelayed()
604 if (m_dropEvent
.isNull()) {
609 DragAndDropHelper::dropUrls(KFileItem(), m_dropDestination
, m_dropEvent
.data(), error
);
610 if (!error
.isEmpty()) {
611 showMessage(error
, Error
);
614 delete m_dropEvent
->mimeData();
618 void DolphinViewContainer::redirect(const KUrl
& oldUrl
, const KUrl
& newUrl
)
621 const bool block
= m_urlNavigator
->signalsBlocked();
622 m_urlNavigator
->blockSignals(true);
624 // Assure that the location state is reset for redirection URLs. This
625 // allows to skip redirection URLs when going back or forward in the
627 m_urlNavigator
->saveLocationState(QByteArray());
628 m_urlNavigator
->setLocationUrl(newUrl
);
629 setSearchModeEnabled(isSearchUrl(newUrl
));
631 m_urlNavigator
->blockSignals(block
);
634 void DolphinViewContainer::requestFocus()
639 void DolphinViewContainer::saveUrlCompletionMode(KGlobalSettings::Completion completion
)
641 GeneralSettings::setUrlCompletionMode(completion
);
644 void DolphinViewContainer::slotHistoryChanged()
646 QByteArray locationState
= m_urlNavigator
->locationState();
647 if (!locationState
.isEmpty()) {
648 QDataStream
stream(&locationState
, QIODevice::ReadOnly
);
649 m_view
->restoreState(stream
);
653 void DolphinViewContainer::slotReturnPressed()
655 if (!GeneralSettings::editableUrl()) {
656 m_urlNavigator
->setUrlEditable(false);
660 void DolphinViewContainer::startSearching()
662 const KUrl url
= m_searchBox
->urlForSearching();
663 if (url
.isValid() && !url
.isEmpty()) {
664 m_view
->setViewPropertiesContext("search");
665 m_urlNavigator
->setLocationUrl(url
);
669 void DolphinViewContainer::closeSearchBox()
671 setSearchModeEnabled(false);
674 void DolphinViewContainer::stopDirectoryLoading()
676 m_view
->stopLoading();
677 m_statusBar
->setProgress(100);
680 void DolphinViewContainer::slotStatusBarZoomLevelChanged(int zoomLevel
)
682 m_view
->setZoomLevel(zoomLevel
);
685 void DolphinViewContainer::showErrorMessage(const QString
& msg
)
687 showMessage(msg
, Error
);
690 bool DolphinViewContainer::isSearchUrl(const KUrl
& url
) const
692 const QString protocol
= url
.protocol();
693 return protocol
.contains("search");
696 void DolphinViewContainer::saveViewState()
698 QByteArray locationState
;
699 QDataStream
stream(&locationState
, QIODevice::WriteOnly
);
700 m_view
->saveState(stream
);
701 m_urlNavigator
->saveLocationState(locationState
);
704 #include "dolphinviewcontainer.moc"