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 <KFilePlacesModel>
34 #include <KIconEffect>
35 #include <KIO/NetAccess>
36 #include <KIO/PreviewJob>
37 #include <KMessageWidget>
38 #include <KNewFileMenu>
39 #include <konqmimedata.h>
40 #include <konq_operations.h>
43 #include <KUrlComboBox>
44 #include <KUrlNavigator>
47 #ifdef KActivities_FOUND
48 #include <KActivities/ResourceInstance>
51 #include "dolphin_generalsettings.h"
52 #include "filterbar/filterbar.h"
53 #include "search/dolphinsearchbox.h"
54 #include "statusbar/dolphinstatusbar.h"
55 #include "views/draganddrophelper.h"
56 #include "views/viewmodecontroller.h"
57 #include "views/viewproperties.h"
59 DolphinViewContainer::DolphinViewContainer(const KUrl
& url
, QWidget
* parent
) :
69 m_statusBarTimestamp(),
71 #ifdef KActivities_FOUND
72 , m_activityResourceInstance(0)
77 m_topLayout
= new QVBoxLayout(this);
78 m_topLayout
->setSpacing(0);
79 m_topLayout
->setMargin(0);
81 m_urlNavigator
= new KUrlNavigator(new KFilePlacesModel(this), url
, this);
82 connect(m_urlNavigator
, SIGNAL(urlsDropped(KUrl
,QDropEvent
*)),
83 this, SLOT(dropUrls(KUrl
,QDropEvent
*)));
84 connect(m_urlNavigator
, SIGNAL(activated()),
85 this, SLOT(activate()));
86 connect(m_urlNavigator
->editor(), SIGNAL(completionModeChanged(KGlobalSettings::Completion
)),
87 this, SLOT(saveUrlCompletionMode(KGlobalSettings::Completion
)));
89 const GeneralSettings
* settings
= GeneralSettings::self();
90 m_urlNavigator
->setUrlEditable(settings
->editableUrl());
91 m_urlNavigator
->setShowFullPath(settings
->showFullPath());
92 m_urlNavigator
->setHomeUrl(KUrl(settings
->homeUrl()));
93 KUrlComboBox
* editor
= m_urlNavigator
->editor();
94 editor
->setCompletionMode(KGlobalSettings::Completion(settings
->urlCompletionMode()));
96 m_searchBox
= new DolphinSearchBox(this);
98 connect(m_searchBox
, SIGNAL(closeRequest()), this, SLOT(closeSearchBox()));
99 connect(m_searchBox
, SIGNAL(searchRequest()), this, SLOT(startSearching()));
100 connect(m_searchBox
, SIGNAL(returnPressed(QString
)), this, SLOT(requestFocus()));
102 m_messageWidget
= new KMessageWidget(this);
103 m_messageWidget
->setCloseButtonVisible(true);
104 m_messageWidget
->hide();
106 m_view
= new DolphinView(url
, this);
107 connect(m_view
, SIGNAL(urlChanged(KUrl
)), m_urlNavigator
, SLOT(setUrl(KUrl
)));
108 connect(m_view
, SIGNAL(writeStateChanged(bool)), this, SIGNAL(writeStateChanged(bool)));
109 connect(m_view
, SIGNAL(requestItemInfo(KFileItem
)), this, SLOT(showItemInfo(KFileItem
)));
110 connect(m_view
, SIGNAL(itemActivated(KFileItem
)), this, SLOT(slotItemActivated(KFileItem
)));
111 connect(m_view
, SIGNAL(redirection(KUrl
,KUrl
)), this, SLOT(redirect(KUrl
,KUrl
)));
112 connect(m_view
, SIGNAL(directoryLoadingStarted()), this, SLOT(slotDirectoryLoadingStarted()));
113 connect(m_view
, SIGNAL(directoryLoadingCompleted()), this, SLOT(slotDirectoryLoadingCompleted()));
114 connect(m_view
, SIGNAL(directoryLoadingCanceled()), this, SLOT(slotDirectoryLoadingCanceled()));
115 connect(m_view
, SIGNAL(itemCountChanged()), this, SLOT(delayedStatusBarUpdate()));
116 connect(m_view
, SIGNAL(directoryLoadingProgress(int)), this, SLOT(updateDirectoryLoadingProgress(int)));
117 connect(m_view
, SIGNAL(directorySortingProgress(int)), this, SLOT(updateDirectorySortingProgress(int)));
118 connect(m_view
, SIGNAL(selectionChanged(KFileItemList
)), this, SLOT(delayedStatusBarUpdate()));
119 connect(m_view
, SIGNAL(urlAboutToBeChanged(KUrl
)), this, SLOT(slotViewUrlAboutToBeChanged(KUrl
)));
120 connect(m_view
, SIGNAL(errorMessage(QString
)), this, SLOT(showErrorMessage(QString
)));
121 connect(m_view
, SIGNAL(urlIsFileError(KUrl
)), this, SLOT(slotUrlIsFileError(KUrl
)));
123 connect(m_urlNavigator
, SIGNAL(urlAboutToBeChanged(KUrl
)),
124 this, SLOT(slotUrlNavigatorLocationAboutToBeChanged(KUrl
)));
125 connect(m_urlNavigator
, SIGNAL(urlChanged(KUrl
)),
126 this, SLOT(slotUrlNavigatorLocationChanged(KUrl
)));
127 connect(m_urlNavigator
, SIGNAL(historyChanged()),
128 this, SLOT(slotHistoryChanged()));
130 // Initialize status bar
131 m_statusBar
= new DolphinStatusBar(this);
132 m_statusBar
->setUrl(m_view
->url());
133 m_statusBar
->setZoomLevel(m_view
->zoomLevel());
134 connect(m_view
, SIGNAL(urlChanged(KUrl
)), m_statusBar
, SLOT(setUrl(KUrl
)));
135 connect(m_view
, SIGNAL(zoomLevelChanged(int,int)), m_statusBar
, SLOT(setZoomLevel(int)));
136 connect(m_view
, SIGNAL(infoMessage(QString
)), m_statusBar
, SLOT(setText(QString
)));
137 connect(m_view
, SIGNAL(operationCompletedMessage(QString
)), m_statusBar
, SLOT(setText(QString
)));
138 connect(m_statusBar
, SIGNAL(stopPressed()), this, SLOT(stopDirectoryLoading()));
139 connect(m_statusBar
, SIGNAL(zoomLevelChanged(int)), this, SLOT(slotStatusBarZoomLevelChanged(int)));
141 m_statusBarTimer
= new QTimer(this);
142 m_statusBarTimer
->setSingleShot(true);
143 m_statusBarTimer
->setInterval(300);
144 connect(m_statusBarTimer
, SIGNAL(timeout()), this, SLOT(updateStatusBar()));
146 KIO::FileUndoManager
* undoManager
= KIO::FileUndoManager::self();
147 connect(undoManager
, SIGNAL(jobRecordingFinished(CommandType
)),
148 this, SLOT(delayedStatusBarUpdate()));
150 // Initialize filter bar
151 m_filterBar
= new FilterBar(this);
152 m_filterBar
->setVisible(settings
->filterBar());
153 connect(m_filterBar
, SIGNAL(filterChanged(QString
)),
154 this, SLOT(setNameFilter(QString
)));
155 connect(m_filterBar
, SIGNAL(closeRequest()),
156 this, SLOT(closeFilterBar()));
157 connect(m_view
, SIGNAL(urlChanged(KUrl
)),
158 m_filterBar
, SLOT(clear()));
160 m_topLayout
->addWidget(m_urlNavigator
);
161 m_topLayout
->addWidget(m_searchBox
);
162 m_topLayout
->addWidget(m_messageWidget
);
163 m_topLayout
->addWidget(m_view
);
164 m_topLayout
->addWidget(m_filterBar
);
165 m_topLayout
->addWidget(m_statusBar
);
167 setSearchModeEnabled(isSearchUrl(url
));
169 // Initialize kactivities resource instance
171 #ifdef KActivities_FOUND
172 m_activityResourceInstance
= new KActivities::ResourceInstance(
173 window()->winId(), url
);
174 m_activityResourceInstance
->setParent(this);
178 DolphinViewContainer::~DolphinViewContainer()
182 KUrl
DolphinViewContainer::url() const
184 return m_view
->url();
187 void DolphinViewContainer::setActive(bool active
)
189 m_urlNavigator
->setActive(active
);
190 m_view
->setActive(active
);
192 #ifdef KActivities_FOUND
194 m_activityResourceInstance
->notifyFocusedIn();
196 m_activityResourceInstance
->notifyFocusedOut();
201 bool DolphinViewContainer::isActive() const
203 Q_ASSERT(m_view
->isActive() == m_urlNavigator
->isActive());
204 return m_view
->isActive();
207 void DolphinViewContainer::setAutoGrabFocus(bool grab
)
209 m_autoGrabFocus
= grab
;
212 bool DolphinViewContainer::autoGrabFocus() const
214 return m_autoGrabFocus
;
217 const DolphinStatusBar
* DolphinViewContainer::statusBar() const
222 DolphinStatusBar
* DolphinViewContainer::statusBar()
227 const KUrlNavigator
* DolphinViewContainer::urlNavigator() const
229 return m_urlNavigator
;
232 KUrlNavigator
* DolphinViewContainer::urlNavigator()
234 return m_urlNavigator
;
237 const DolphinView
* DolphinViewContainer::view() const
242 DolphinView
* DolphinViewContainer::view()
247 void DolphinViewContainer::showMessage(const QString
& msg
, MessageType type
)
253 m_messageWidget
->setText(msg
);
256 case Information
: m_messageWidget
->setMessageType(KMessageWidget::Information
); break;
257 case Warning
: m_messageWidget
->setMessageType(KMessageWidget::Warning
); break;
258 case Error
: m_messageWidget
->setMessageType(KMessageWidget::Error
); break;
264 m_messageWidget
->setWordWrap(false);
265 const int unwrappedWidth
= m_messageWidget
->sizeHint().width();
266 m_messageWidget
->setWordWrap(unwrappedWidth
> size().width());
268 m_messageWidget
->animatedShow();
271 void DolphinViewContainer::readSettings()
273 if (GeneralSettings::modifiedStartupSettings()) {
274 // The startup settings should only get applied if they have been
275 // modified by the user. Otherwise keep the (possibly) different current
276 // settings of the URL navigator and the filterbar.
277 m_urlNavigator
->setUrlEditable(GeneralSettings::editableUrl());
278 m_urlNavigator
->setShowFullPath(GeneralSettings::showFullPath());
279 m_urlNavigator
->setHomeUrl(KUrl(GeneralSettings::homeUrl()));
280 setFilterBarVisible(GeneralSettings::filterBar());
283 m_view
->readSettings();
284 m_statusBar
->readSettings();
287 bool DolphinViewContainer::isFilterBarVisible() const
289 return m_filterBar
->isVisible();
292 void DolphinViewContainer::setSearchModeEnabled(bool enabled
)
294 if (enabled
== isSearchModeEnabled()) {
295 if (enabled
&& !m_searchBox
->hasFocus()) {
296 m_searchBox
->setFocus();
297 m_searchBox
->selectAll();
302 m_searchBox
->setVisible(enabled
);
303 m_urlNavigator
->setVisible(!enabled
);
306 KUrl url
= m_urlNavigator
->locationUrl();
307 m_searchBox
->setText(QString());
308 m_searchBox
->setReadOnly(isSearchUrl(url
), url
);
310 // Remember the most recent non-search URL as search path
311 // of the search-box, so that it can be restored
312 // when switching back to the URL navigator.
313 int index
= m_urlNavigator
->historyIndex();
314 const int historySize
= m_urlNavigator
->historySize();
315 while (isSearchUrl(url
) && (index
< historySize
)) {
317 url
= m_urlNavigator
->locationUrl(index
);
320 if (!isSearchUrl(url
)) {
321 m_searchBox
->setSearchPath(url
);
324 m_view
->setViewPropertiesContext(QString());
326 // Restore the URL for the URL navigator. If Dolphin has been
327 // started with a search-URL, the home URL is used as fallback.
328 const KUrl url
= m_searchBox
->searchPath();
329 if (url
.isValid() && !url
.isEmpty()) {
330 if (isSearchUrl(url
)) {
331 m_urlNavigator
->goHome();
333 m_urlNavigator
->setLocationUrl(url
);
339 bool DolphinViewContainer::isSearchModeEnabled() const
341 return m_searchBox
->isVisible();
344 QString
DolphinViewContainer::placesText() const
348 if (isSearchModeEnabled()) {
349 text
= m_searchBox
->searchPath().fileName() + QLatin1String(": ") + m_searchBox
->text();
351 text
= url().fileName();
352 if (text
.isEmpty()) {
360 void DolphinViewContainer::setUrl(const KUrl
& newUrl
)
362 if (newUrl
!= m_urlNavigator
->locationUrl()) {
363 m_urlNavigator
->setLocationUrl(newUrl
);
366 #ifdef KActivities_FOUND
367 m_activityResourceInstance
->setUri(newUrl
);
371 void DolphinViewContainer::setFilterBarVisible(bool visible
)
373 Q_ASSERT(m_filterBar
);
376 m_filterBar
->setFocus();
377 m_filterBar
->selectAll();
383 void DolphinViewContainer::delayedStatusBarUpdate()
385 if (m_statusBarTimer
->isActive() && (m_statusBarTimestamp
.elapsed() > 2000)) {
386 // No update of the statusbar has been done during the last 2 seconds,
387 // although an update has been requested. Trigger an immediate update.
388 m_statusBarTimer
->stop();
391 // Invoke updateStatusBar() with a small delay. This assures that
392 // when a lot of delayedStatusBarUpdates() are done in a short time,
393 // no bottleneck is given.
394 m_statusBarTimer
->start();
398 void DolphinViewContainer::updateStatusBar()
400 m_statusBarTimestamp
.start();
402 const QString text
= m_view
->statusBarText();
403 m_statusBar
->setDefaultText(text
);
404 m_statusBar
->resetToDefaultText();
407 void DolphinViewContainer::updateDirectoryLoadingProgress(int percent
)
409 if (m_statusBar
->progressText().isEmpty()) {
410 m_statusBar
->setProgressText(i18nc("@info:progress", "Loading folder..."));
412 m_statusBar
->setProgress(percent
);
415 void DolphinViewContainer::updateDirectorySortingProgress(int percent
)
417 if (m_statusBar
->progressText().isEmpty()) {
418 m_statusBar
->setProgressText(i18nc("@info:progress", "Sorting..."));
420 m_statusBar
->setProgress(percent
);
423 void DolphinViewContainer::slotDirectoryLoadingStarted()
425 if (isSearchUrl(url())) {
426 // Search KIO-slaves usually don't provide any progress information. Give
427 // a hint to the user that a searching is done:
429 m_statusBar
->setProgressText(i18nc("@info", "Searching..."));
430 m_statusBar
->setProgress(-1);
432 // Trigger an undetermined progress indication. The progress
433 // information in percent will be triggered by the percent() signal
434 // of the directory lister later.
435 updateDirectoryLoadingProgress(-1);
439 void DolphinViewContainer::slotDirectoryLoadingCompleted()
441 if (!m_statusBar
->progressText().isEmpty()) {
442 m_statusBar
->setProgressText(QString());
443 m_statusBar
->setProgress(100);
446 if (isSearchUrl(url()) && m_view
->itemsCount() == 0) {
447 // The dir lister has been completed on a Nepomuk-URI and no items have been found. Instead
448 // of showing the default status bar information ("0 items") a more helpful information is given:
449 m_statusBar
->setText(i18nc("@info:status", "No items found."));
455 void DolphinViewContainer::slotDirectoryLoadingCanceled()
457 if (!m_statusBar
->progressText().isEmpty()) {
458 m_statusBar
->setProgressText(QString());
459 m_statusBar
->setProgress(100);
462 showErrorMessage("Directory loading has been canceled.");
465 void DolphinViewContainer::slotUrlIsFileError(const KUrl
& url
)
467 const KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, url
);
468 slotItemActivated(item
);
471 void DolphinViewContainer::slotItemActivated(const KFileItem
& item
)
473 // It is possible to activate items on inactive views by
474 // drag & drop operations. Assure that activating an item always
475 // results in an active view.
476 m_view
->setActive(true);
478 KUrl url
= item
.targetUrl();
485 if (GeneralSettings::browseThroughArchives() && item
.isFile() && url
.isLocalFile()) {
486 // Generic mechanism for redirecting to tar:/<path>/ when clicking on a tar file,
487 // zip:/<path>/ when clicking on a zip file, etc.
488 // The .protocol file specifies the mimetype that the kioslave handles.
489 // Note that we don't use mimetype inheritance since we don't want to
490 // open OpenDocument files as zip folders...
491 const QString protocol
= KProtocolManager::protocolForArchiveMimetype(item
.mimetype());
492 if (!protocol
.isEmpty()) {
493 url
.setProtocol(protocol
);
499 if (item
.mimetype() == QLatin1String("application/x-desktop")) {
500 // Redirect to the URL in Type=Link desktop files
501 KDesktopFile
desktopFile(url
.toLocalFile());
502 if (desktopFile
.hasLinkType()) {
503 url
= desktopFile
.readUrl();
512 void DolphinViewContainer::showItemInfo(const KFileItem
& item
)
515 m_statusBar
->resetToDefaultText();
517 const QString text
= item
.isDir() ? item
.text() : item
.getStatusBarInfo();
518 m_statusBar
->setText(text
);
522 void DolphinViewContainer::closeFilterBar()
525 m_filterBar
->clear();
527 emit
showFilterBarChanged(false);
530 void DolphinViewContainer::setNameFilter(const QString
& nameFilter
)
532 m_view
->setNameFilter(nameFilter
);
533 delayedStatusBarUpdate();
536 void DolphinViewContainer::activate()
541 void DolphinViewContainer::slotViewUrlAboutToBeChanged(const KUrl
& url
)
543 // URL changes of the view can happen in two ways:
544 // 1. The URL navigator gets changed and will trigger the view to update its URL
545 // 2. The URL of the view gets changed and will trigger the URL navigator to update
546 // its URL (e.g. by clicking on an item)
547 // In this scope the view-state may only get saved in case 2:
548 if (url
!= m_urlNavigator
->locationUrl()) {
553 void DolphinViewContainer::slotUrlNavigatorLocationAboutToBeChanged(const KUrl
& url
)
555 // URL changes of the view can happen in two ways:
556 // 1. The URL navigator gets changed and will trigger the view to update its URL
557 // 2. The URL of the view gets changed and will trigger the URL navigator to update
558 // its URL (e.g. by clicking on an item)
559 // In this scope the view-state may only get saved in case 1:
560 if (url
!= m_view
->url()) {
565 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 const QString error
= DragAndDropHelper::dropUrls(KFileItem(), destination
, event
);
610 if (!error
.isEmpty()) {
611 showMessage(error
, Error
);
615 void DolphinViewContainer::redirect(const KUrl
& oldUrl
, const KUrl
& newUrl
)
618 const bool block
= m_urlNavigator
->signalsBlocked();
619 m_urlNavigator
->blockSignals(true);
621 // Assure that the location state is reset for redirection URLs. This
622 // allows to skip redirection URLs when going back or forward in the
624 m_urlNavigator
->saveLocationState(QByteArray());
625 m_urlNavigator
->setLocationUrl(newUrl
);
626 setSearchModeEnabled(isSearchUrl(newUrl
));
628 m_urlNavigator
->blockSignals(block
);
631 void DolphinViewContainer::requestFocus()
636 void DolphinViewContainer::saveUrlCompletionMode(KGlobalSettings::Completion completion
)
638 GeneralSettings::setUrlCompletionMode(completion
);
641 void DolphinViewContainer::slotHistoryChanged()
643 QByteArray locationState
= m_urlNavigator
->locationState();
644 if (!locationState
.isEmpty()) {
645 QDataStream
stream(&locationState
, QIODevice::ReadOnly
);
646 m_view
->restoreState(stream
);
650 void DolphinViewContainer::startSearching()
652 const KUrl url
= m_searchBox
->urlForSearching();
653 if (url
.isValid() && !url
.isEmpty()) {
654 m_view
->setViewPropertiesContext("search");
655 m_urlNavigator
->setLocationUrl(url
);
659 void DolphinViewContainer::closeSearchBox()
661 setSearchModeEnabled(false);
664 void DolphinViewContainer::stopDirectoryLoading()
666 m_view
->stopLoading();
667 m_statusBar
->setProgress(100);
670 void DolphinViewContainer::slotStatusBarZoomLevelChanged(int zoomLevel
)
672 m_view
->setZoomLevel(zoomLevel
);
675 void DolphinViewContainer::showErrorMessage(const QString
& msg
)
677 showMessage(msg
, Error
);
680 bool DolphinViewContainer::isSearchUrl(const KUrl
& url
) const
682 const QString protocol
= url
.protocol();
683 return protocol
.contains("search") || (protocol
== QLatin1String("nepomuk"));
686 void DolphinViewContainer::saveViewState()
688 QByteArray locationState
;
689 QDataStream
stream(&locationState
, QIODevice::WriteOnly
);
690 m_view
->saveState(stream
);
691 m_urlNavigator
->saveLocationState(locationState
);
694 #include "dolphinviewcontainer.moc"