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(),
72 #ifdef KActivities_FOUND
73 , m_activityResourceInstance(0)
78 m_topLayout
= new QVBoxLayout(this);
79 m_topLayout
->setSpacing(0);
80 m_topLayout
->setMargin(0);
82 m_urlNavigator
= new KUrlNavigator(new KFilePlacesModel(this), url
, this);
83 connect(m_urlNavigator
, SIGNAL(urlsDropped(KUrl
,QDropEvent
*)),
84 this, SLOT(dropUrls(KUrl
,QDropEvent
*)));
85 connect(m_urlNavigator
, SIGNAL(activated()),
86 this, SLOT(activate()));
87 connect(m_urlNavigator
->editor(), SIGNAL(completionModeChanged(KGlobalSettings::Completion
)),
88 this, SLOT(saveUrlCompletionMode(KGlobalSettings::Completion
)));
90 const GeneralSettings
* settings
= GeneralSettings::self();
91 m_urlNavigator
->setUrlEditable(settings
->editableUrl());
92 m_urlNavigator
->setShowFullPath(settings
->showFullPath());
93 m_urlNavigator
->setHomeUrl(KUrl(settings
->homeUrl()));
94 KUrlComboBox
* editor
= m_urlNavigator
->editor();
95 editor
->setCompletionMode(KGlobalSettings::Completion(settings
->urlCompletionMode()));
97 m_searchBox
= new DolphinSearchBox(this);
99 connect(m_searchBox
, SIGNAL(closeRequest()), this, SLOT(closeSearchBox()));
100 connect(m_searchBox
, SIGNAL(searchRequest()), this, SLOT(startSearching()));
101 connect(m_searchBox
, SIGNAL(returnPressed(QString
)), this, SLOT(requestFocus()));
103 m_messageWidget
= new KMessageWidget(this);
104 m_messageWidget
->setCloseButtonVisible(true);
105 m_messageWidget
->hide();
107 m_view
= new DolphinView(url
, this);
108 connect(m_view
, SIGNAL(urlChanged(KUrl
)), m_urlNavigator
, SLOT(setUrl(KUrl
)));
109 connect(m_view
, SIGNAL(writeStateChanged(bool)), this, SIGNAL(writeStateChanged(bool)));
110 connect(m_view
, SIGNAL(requestItemInfo(KFileItem
)), this, SLOT(showItemInfo(KFileItem
)));
111 connect(m_view
, SIGNAL(itemActivated(KFileItem
)), this, SLOT(slotItemActivated(KFileItem
)));
112 connect(m_view
, SIGNAL(itemsActivated(KFileItemList
)), this, SLOT(slotItemsActivated(KFileItemList
)));
113 connect(m_view
, SIGNAL(redirection(KUrl
,KUrl
)), this, SLOT(redirect(KUrl
,KUrl
)));
114 connect(m_view
, SIGNAL(directoryLoadingStarted()), this, SLOT(slotDirectoryLoadingStarted()));
115 connect(m_view
, SIGNAL(directoryLoadingCompleted()), this, SLOT(slotDirectoryLoadingCompleted()));
116 connect(m_view
, SIGNAL(directoryLoadingCanceled()), this, SLOT(slotDirectoryLoadingCanceled()));
117 connect(m_view
, SIGNAL(itemCountChanged()), this, SLOT(delayedStatusBarUpdate()));
118 connect(m_view
, SIGNAL(directoryLoadingProgress(int)), this, SLOT(updateDirectoryLoadingProgress(int)));
119 connect(m_view
, SIGNAL(directorySortingProgress(int)), this, SLOT(updateDirectorySortingProgress(int)));
120 connect(m_view
, SIGNAL(selectionChanged(KFileItemList
)), this, SLOT(delayedStatusBarUpdate()));
121 connect(m_view
, SIGNAL(urlAboutToBeChanged(KUrl
)), this, SLOT(slotViewUrlAboutToBeChanged(KUrl
)));
122 connect(m_view
, SIGNAL(errorMessage(QString
)), this, SLOT(showErrorMessage(QString
)));
123 connect(m_view
, SIGNAL(urlIsFileError(KUrl
)), this, SLOT(slotUrlIsFileError(KUrl
)));
125 connect(m_urlNavigator
, SIGNAL(urlAboutToBeChanged(KUrl
)),
126 this, SLOT(slotUrlNavigatorLocationAboutToBeChanged(KUrl
)));
127 connect(m_urlNavigator
, SIGNAL(urlChanged(KUrl
)),
128 this, SLOT(slotUrlNavigatorLocationChanged(KUrl
)));
129 connect(m_urlNavigator
, SIGNAL(historyChanged()),
130 this, SLOT(slotHistoryChanged()));
131 connect(m_urlNavigator
, SIGNAL(returnPressed()),
132 this, SLOT(slotReturnPressed()));
134 // Initialize status bar
135 m_statusBar
= new DolphinStatusBar(this);
136 m_statusBar
->setUrl(m_view
->url());
137 m_statusBar
->setZoomLevel(m_view
->zoomLevel());
138 connect(m_view
, SIGNAL(urlChanged(KUrl
)), m_statusBar
, SLOT(setUrl(KUrl
)));
139 connect(m_view
, SIGNAL(zoomLevelChanged(int,int)), m_statusBar
, SLOT(setZoomLevel(int)));
140 connect(m_view
, SIGNAL(infoMessage(QString
)), m_statusBar
, SLOT(setText(QString
)));
141 connect(m_view
, SIGNAL(operationCompletedMessage(QString
)), m_statusBar
, SLOT(setText(QString
)));
142 connect(m_statusBar
, SIGNAL(stopPressed()), this, SLOT(stopDirectoryLoading()));
143 connect(m_statusBar
, SIGNAL(zoomLevelChanged(int)), this, SLOT(slotStatusBarZoomLevelChanged(int)));
145 m_statusBarTimer
= new QTimer(this);
146 m_statusBarTimer
->setSingleShot(true);
147 m_statusBarTimer
->setInterval(300);
148 connect(m_statusBarTimer
, SIGNAL(timeout()), this, SLOT(updateStatusBar()));
150 KIO::FileUndoManager
* undoManager
= KIO::FileUndoManager::self();
151 connect(undoManager
, SIGNAL(jobRecordingFinished(CommandType
)),
152 this, SLOT(delayedStatusBarUpdate()));
154 // Initialize filter bar
155 m_filterBar
= new FilterBar(this);
156 m_filterBar
->setVisible(settings
->filterBar());
157 connect(m_filterBar
, SIGNAL(filterChanged(QString
)),
158 this, SLOT(setNameFilter(QString
)));
159 connect(m_filterBar
, SIGNAL(closeRequest()),
160 this, SLOT(closeFilterBar()));
161 connect(m_filterBar
, SIGNAL(focusViewRequest()),
162 this, SLOT(requestFocus()));
163 connect(m_view
, SIGNAL(urlChanged(KUrl
)),
164 m_filterBar
, SLOT(clear()));
166 m_topLayout
->addWidget(m_urlNavigator
);
167 m_topLayout
->addWidget(m_searchBox
);
168 m_topLayout
->addWidget(m_messageWidget
);
169 m_topLayout
->addWidget(m_view
);
170 m_topLayout
->addWidget(m_filterBar
);
171 m_topLayout
->addWidget(m_statusBar
);
173 setSearchModeEnabled(isSearchUrl(url
));
175 // Initialize kactivities resource instance
177 #ifdef KActivities_FOUND
178 m_activityResourceInstance
= new KActivities::ResourceInstance(
179 window()->winId(), url
);
180 m_activityResourceInstance
->setParent(this);
184 DolphinViewContainer::~DolphinViewContainer()
188 KUrl
DolphinViewContainer::url() const
190 return m_view
->url();
193 void DolphinViewContainer::setActive(bool active
)
195 m_urlNavigator
->setActive(active
);
196 m_view
->setActive(active
);
198 #ifdef KActivities_FOUND
200 m_activityResourceInstance
->notifyFocusedIn();
202 m_activityResourceInstance
->notifyFocusedOut();
207 bool DolphinViewContainer::isActive() const
209 Q_ASSERT(m_view
->isActive() == m_urlNavigator
->isActive());
210 return m_view
->isActive();
213 void DolphinViewContainer::setAutoGrabFocus(bool grab
)
215 m_autoGrabFocus
= grab
;
218 bool DolphinViewContainer::autoGrabFocus() const
220 return m_autoGrabFocus
;
223 const DolphinStatusBar
* DolphinViewContainer::statusBar() const
228 DolphinStatusBar
* DolphinViewContainer::statusBar()
233 const KUrlNavigator
* DolphinViewContainer::urlNavigator() const
235 return m_urlNavigator
;
238 KUrlNavigator
* DolphinViewContainer::urlNavigator()
240 return m_urlNavigator
;
243 const DolphinView
* DolphinViewContainer::view() const
248 DolphinView
* DolphinViewContainer::view()
253 void DolphinViewContainer::showMessage(const QString
& msg
, MessageType type
)
259 m_messageWidget
->setText(msg
);
262 case Information
: m_messageWidget
->setMessageType(KMessageWidget::Information
); break;
263 case Warning
: m_messageWidget
->setMessageType(KMessageWidget::Warning
); break;
264 case Error
: m_messageWidget
->setMessageType(KMessageWidget::Error
); break;
270 m_messageWidget
->setWordWrap(false);
271 const int unwrappedWidth
= m_messageWidget
->sizeHint().width();
272 m_messageWidget
->setWordWrap(unwrappedWidth
> size().width());
274 m_messageWidget
->animatedShow();
277 void DolphinViewContainer::readSettings()
279 if (GeneralSettings::modifiedStartupSettings()) {
280 // The startup settings should only get applied if they have been
281 // modified by the user. Otherwise keep the (possibly) different current
282 // settings of the URL navigator and the filterbar.
283 m_urlNavigator
->setUrlEditable(GeneralSettings::editableUrl());
284 m_urlNavigator
->setShowFullPath(GeneralSettings::showFullPath());
285 m_urlNavigator
->setHomeUrl(KUrl(GeneralSettings::homeUrl()));
286 setFilterBarVisible(GeneralSettings::filterBar());
289 m_view
->readSettings();
290 m_statusBar
->readSettings();
293 bool DolphinViewContainer::isFilterBarVisible() const
295 return m_filterBar
->isVisible();
298 void DolphinViewContainer::setSearchModeEnabled(bool enabled
)
300 if (enabled
== isSearchModeEnabled()) {
301 if (enabled
&& !m_searchBox
->hasFocus()) {
302 m_searchBox
->setFocus();
303 m_searchBox
->selectAll();
308 m_searchBox
->setVisible(enabled
);
309 m_urlNavigator
->setVisible(!enabled
);
312 KUrl url
= m_urlNavigator
->locationUrl();
313 m_searchBox
->setText(QString());
314 m_searchBox
->setReadOnly(isSearchUrl(url
), url
);
316 // Remember the most recent non-search URL as search path
317 // of the search-box, so that it can be restored
318 // when switching back to the URL navigator.
319 int index
= m_urlNavigator
->historyIndex();
320 const int historySize
= m_urlNavigator
->historySize();
321 while (isSearchUrl(url
) && (index
< historySize
)) {
323 url
= m_urlNavigator
->locationUrl(index
);
326 if (!isSearchUrl(url
)) {
327 m_searchBox
->setSearchPath(url
);
330 m_view
->setViewPropertiesContext(QString());
332 // Restore the URL for the URL navigator. If Dolphin has been
333 // started with a search-URL, the home URL is used as fallback.
334 const KUrl url
= m_searchBox
->searchPath();
335 if (url
.isValid() && !url
.isEmpty()) {
336 if (isSearchUrl(url
)) {
337 m_urlNavigator
->goHome();
339 m_urlNavigator
->setLocationUrl(url
);
345 bool DolphinViewContainer::isSearchModeEnabled() const
347 return m_searchBox
->isVisible();
350 QString
DolphinViewContainer::placesText() const
354 if (isSearchModeEnabled()) {
355 text
= m_searchBox
->searchPath().fileName() + QLatin1String(": ") + m_searchBox
->text();
357 text
= url().fileName();
358 if (text
.isEmpty()) {
366 void DolphinViewContainer::setUrl(const KUrl
& newUrl
)
368 if (newUrl
!= m_urlNavigator
->locationUrl()) {
369 m_urlNavigator
->setLocationUrl(newUrl
);
372 #ifdef KActivities_FOUND
373 m_activityResourceInstance
->setUri(newUrl
);
377 void DolphinViewContainer::setFilterBarVisible(bool visible
)
379 Q_ASSERT(m_filterBar
);
382 m_filterBar
->setFocus();
383 m_filterBar
->selectAll();
389 void DolphinViewContainer::delayedStatusBarUpdate()
391 if (m_statusBarTimer
->isActive() && (m_statusBarTimestamp
.elapsed() > 2000)) {
392 // No update of the statusbar has been done during the last 2 seconds,
393 // although an update has been requested. Trigger an immediate update.
394 m_statusBarTimer
->stop();
397 // Invoke updateStatusBar() with a small delay. This assures that
398 // when a lot of delayedStatusBarUpdates() are done in a short time,
399 // no bottleneck is given.
400 m_statusBarTimer
->start();
404 void DolphinViewContainer::updateStatusBar()
406 m_statusBarTimestamp
.start();
408 const QString text
= m_view
->statusBarText();
409 m_statusBar
->setDefaultText(text
);
410 m_statusBar
->resetToDefaultText();
413 void DolphinViewContainer::updateDirectoryLoadingProgress(int percent
)
415 if (m_statusBar
->progressText().isEmpty()) {
416 m_statusBar
->setProgressText(i18nc("@info:progress", "Loading folder..."));
418 m_statusBar
->setProgress(percent
);
421 void DolphinViewContainer::updateDirectorySortingProgress(int percent
)
423 if (m_statusBar
->progressText().isEmpty()) {
424 m_statusBar
->setProgressText(i18nc("@info:progress", "Sorting..."));
426 m_statusBar
->setProgress(percent
);
429 void DolphinViewContainer::slotDirectoryLoadingStarted()
431 if (isSearchUrl(url())) {
432 // Search KIO-slaves usually don't provide any progress information. Give
433 // a hint to the user that a searching is done:
435 m_statusBar
->setProgressText(i18nc("@info", "Searching..."));
436 m_statusBar
->setProgress(-1);
438 // Trigger an undetermined progress indication. The progress
439 // information in percent will be triggered by the percent() signal
440 // of the directory lister later.
441 updateDirectoryLoadingProgress(-1);
445 void DolphinViewContainer::slotDirectoryLoadingCompleted()
447 if (!m_statusBar
->progressText().isEmpty()) {
448 m_statusBar
->setProgressText(QString());
449 m_statusBar
->setProgress(100);
452 if (isSearchUrl(url()) && m_view
->itemsCount() == 0) {
453 // The dir lister has been completed on a Nepomuk-URI and no items have been found. Instead
454 // of showing the default status bar information ("0 items") a more helpful information is given:
455 m_statusBar
->setText(i18nc("@info:status", "No items found."));
461 void DolphinViewContainer::slotDirectoryLoadingCanceled()
463 if (!m_statusBar
->progressText().isEmpty()) {
464 m_statusBar
->setProgressText(QString());
465 m_statusBar
->setProgress(100);
468 m_statusBar
->setText(QString());
471 void DolphinViewContainer::slotUrlIsFileError(const KUrl
& url
)
473 const KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, url
);
474 slotItemActivated(item
);
477 void DolphinViewContainer::slotItemActivated(const KFileItem
& item
)
479 // It is possible to activate items on inactive views by
480 // drag & drop operations. Assure that activating an item always
481 // results in an active view.
482 m_view
->setActive(true);
484 KUrl url
= item
.targetUrl();
491 if (GeneralSettings::browseThroughArchives() && item
.isFile() && url
.isLocalFile()) {
492 // Generic mechanism for redirecting to tar:/<path>/ when clicking on a tar file,
493 // zip:/<path>/ when clicking on a zip file, etc.
494 // The .protocol file specifies the mimetype that the kioslave handles.
495 // Note that we don't use mimetype inheritance since we don't want to
496 // open OpenDocument files as zip folders...
497 const QString protocol
= KProtocolManager::protocolForArchiveMimetype(item
.mimetype());
498 if (!protocol
.isEmpty()) {
499 url
.setProtocol(protocol
);
505 if (item
.mimetype() == QLatin1String("application/x-desktop")) {
506 // Redirect to the URL in Type=Link desktop files
507 KDesktopFile
desktopFile(url
.toLocalFile());
508 if (desktopFile
.hasLinkType()) {
509 url
= desktopFile
.readUrl();
518 void DolphinViewContainer::slotItemsActivated(const KFileItemList
& items
)
520 Q_ASSERT(items
.count() >= 2);
522 KFileItemActions
fileItemActions(this);
523 fileItemActions
.runPreferredApplications(items
, QString());
526 void DolphinViewContainer::showItemInfo(const KFileItem
& item
)
529 m_statusBar
->resetToDefaultText();
531 const QString text
= item
.isDir() ? item
.text() : item
.getStatusBarInfo();
532 m_statusBar
->setText(text
);
536 void DolphinViewContainer::closeFilterBar()
539 m_filterBar
->clear();
541 emit
showFilterBarChanged(false);
544 void DolphinViewContainer::setNameFilter(const QString
& nameFilter
)
546 m_view
->setNameFilter(nameFilter
);
547 delayedStatusBarUpdate();
550 void DolphinViewContainer::activate()
555 void DolphinViewContainer::slotViewUrlAboutToBeChanged(const KUrl
& url
)
557 // URL changes of the view can happen in two ways:
558 // 1. The URL navigator gets changed and will trigger the view to update its URL
559 // 2. The URL of the view gets changed and will trigger the URL navigator to update
560 // its URL (e.g. by clicking on an item)
561 // In this scope the view-state may only get saved in case 2:
562 if (url
!= m_urlNavigator
->locationUrl()) {
567 void DolphinViewContainer::slotUrlNavigatorLocationAboutToBeChanged(const KUrl
& url
)
569 // URL changes of the view can happen in two ways:
570 // 1. The URL navigator gets changed and will trigger the view to update its URL
571 // 2. The URL of the view gets changed and will trigger the URL navigator to update
572 // its URL (e.g. by clicking on an item)
573 // In this scope the view-state may only get saved in case 1:
574 if (url
!= m_view
->url()) {
579 void DolphinViewContainer::slotUrlNavigatorLocationChanged(const KUrl
& url
)
583 if (KProtocolManager::supportsListing(url
)) {
584 setSearchModeEnabled(isSearchUrl(url
));
587 if (m_autoGrabFocus
&& isActive() && !isSearchUrl(url
)) {
588 // When an URL has been entered, the view should get the focus.
589 // The focus must be requested asynchronously, as changing the URL might create
590 // a new view widget.
591 QTimer::singleShot(0, this, SLOT(requestFocus()));
593 } else if (KProtocolManager::isSourceProtocol(url
)) {
594 QString app
= "konqueror";
595 if (url
.protocol().startsWith(QLatin1String("http"))) {
596 showMessage(i18nc("@info:status", // krazy:exclude=qmethods
597 "Dolphin does not support web pages, the web browser has been launched"),
600 const KConfigGroup
config(KSharedConfig::openConfig("kdeglobals"), "General");
601 const QString browser
= config
.readEntry("BrowserApplication");
602 if (!browser
.isEmpty()) {
604 if (app
.startsWith('!')) {
605 // a literal command has been configured, remove the '!' prefix
610 showMessage(i18nc("@info:status",
611 "Protocol not supported by Dolphin, Konqueror has been launched"),
615 const QString secureUrl
= KShell::quoteArg(url
.pathOrUrl());
616 const QString command
= app
+ ' ' + secureUrl
;
617 KRun::runCommand(command
, app
, app
, this);
619 showMessage(i18nc("@info:status", "Invalid protocol"), Error
);
623 void DolphinViewContainer::dropUrls(const KUrl
& destination
, QDropEvent
* event
)
626 DragAndDropHelper::dropUrls(KFileItem(), destination
, event
, error
);
627 if (!error
.isEmpty()) {
628 showMessage(error
, Error
);
632 void DolphinViewContainer::redirect(const KUrl
& oldUrl
, const KUrl
& newUrl
)
635 const bool block
= m_urlNavigator
->signalsBlocked();
636 m_urlNavigator
->blockSignals(true);
638 // Assure that the location state is reset for redirection URLs. This
639 // allows to skip redirection URLs when going back or forward in the
641 m_urlNavigator
->saveLocationState(QByteArray());
642 m_urlNavigator
->setLocationUrl(newUrl
);
643 setSearchModeEnabled(isSearchUrl(newUrl
));
645 m_urlNavigator
->blockSignals(block
);
648 void DolphinViewContainer::requestFocus()
653 void DolphinViewContainer::saveUrlCompletionMode(KGlobalSettings::Completion completion
)
655 GeneralSettings::setUrlCompletionMode(completion
);
658 void DolphinViewContainer::slotHistoryChanged()
660 QByteArray locationState
= m_urlNavigator
->locationState();
661 if (!locationState
.isEmpty()) {
662 QDataStream
stream(&locationState
, QIODevice::ReadOnly
);
663 m_view
->restoreState(stream
);
667 void DolphinViewContainer::slotReturnPressed()
669 if (!GeneralSettings::editableUrl()) {
670 m_urlNavigator
->setUrlEditable(false);
674 void DolphinViewContainer::startSearching()
676 const KUrl url
= m_searchBox
->urlForSearching();
677 if (url
.isValid() && !url
.isEmpty()) {
678 m_view
->setViewPropertiesContext("search");
679 m_urlNavigator
->setLocationUrl(url
);
683 void DolphinViewContainer::closeSearchBox()
685 setSearchModeEnabled(false);
688 void DolphinViewContainer::stopDirectoryLoading()
690 m_view
->stopLoading();
691 m_statusBar
->setProgress(100);
694 void DolphinViewContainer::slotStatusBarZoomLevelChanged(int zoomLevel
)
696 m_view
->setZoomLevel(zoomLevel
);
699 void DolphinViewContainer::showErrorMessage(const QString
& msg
)
701 showMessage(msg
, Error
);
704 bool DolphinViewContainer::isSearchUrl(const KUrl
& url
) const
706 const QString protocol
= url
.protocol();
707 return protocol
.contains("search") || (protocol
== QLatin1String("nepomuk"));
710 void DolphinViewContainer::saveViewState()
712 QByteArray locationState
;
713 QDataStream
stream(&locationState
, QIODevice::WriteOnly
);
714 m_view
->saveState(stream
);
715 m_urlNavigator
->saveLocationState(locationState
);
718 #include "dolphinviewcontainer.moc"