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(urlChanged(KUrl
)), m_messageWidget
, SLOT(hide()));
110 connect(m_view
, SIGNAL(writeStateChanged(bool)), this, SIGNAL(writeStateChanged(bool)));
111 connect(m_view
, SIGNAL(requestItemInfo(KFileItem
)), this, SLOT(showItemInfo(KFileItem
)));
112 connect(m_view
, SIGNAL(itemActivated(KFileItem
)), this, SLOT(slotItemActivated(KFileItem
)));
113 connect(m_view
, SIGNAL(itemsActivated(KFileItemList
)), this, SLOT(slotItemsActivated(KFileItemList
)));
114 connect(m_view
, SIGNAL(redirection(KUrl
,KUrl
)), this, SLOT(redirect(KUrl
,KUrl
)));
115 connect(m_view
, SIGNAL(directoryLoadingStarted()), this, SLOT(slotDirectoryLoadingStarted()));
116 connect(m_view
, SIGNAL(directoryLoadingCompleted()), this, SLOT(slotDirectoryLoadingCompleted()));
117 connect(m_view
, SIGNAL(directoryLoadingCanceled()), this, SLOT(slotDirectoryLoadingCanceled()));
118 connect(m_view
, SIGNAL(itemCountChanged()), this, SLOT(delayedStatusBarUpdate()));
119 connect(m_view
, SIGNAL(directoryLoadingProgress(int)), this, SLOT(updateDirectoryLoadingProgress(int)));
120 connect(m_view
, SIGNAL(directorySortingProgress(int)), this, SLOT(updateDirectorySortingProgress(int)));
121 connect(m_view
, SIGNAL(selectionChanged(KFileItemList
)), this, SLOT(delayedStatusBarUpdate()));
122 connect(m_view
, SIGNAL(urlAboutToBeChanged(KUrl
)), this, SLOT(slotViewUrlAboutToBeChanged(KUrl
)));
123 connect(m_view
, SIGNAL(errorMessage(QString
)), this, SLOT(showErrorMessage(QString
)));
124 connect(m_view
, SIGNAL(urlIsFileError(KUrl
)), this, SLOT(slotUrlIsFileError(KUrl
)));
126 connect(m_urlNavigator
, SIGNAL(urlAboutToBeChanged(KUrl
)),
127 this, SLOT(slotUrlNavigatorLocationAboutToBeChanged(KUrl
)));
128 connect(m_urlNavigator
, SIGNAL(urlChanged(KUrl
)),
129 this, SLOT(slotUrlNavigatorLocationChanged(KUrl
)));
130 connect(m_urlNavigator
, SIGNAL(historyChanged()),
131 this, SLOT(slotHistoryChanged()));
132 connect(m_urlNavigator
, SIGNAL(returnPressed()),
133 this, SLOT(slotReturnPressed()));
135 // Initialize status bar
136 m_statusBar
= new DolphinStatusBar(this);
137 m_statusBar
->setUrl(m_view
->url());
138 m_statusBar
->setZoomLevel(m_view
->zoomLevel());
139 connect(m_view
, SIGNAL(urlChanged(KUrl
)), m_statusBar
, SLOT(setUrl(KUrl
)));
140 connect(m_view
, SIGNAL(zoomLevelChanged(int,int)), m_statusBar
, SLOT(setZoomLevel(int)));
141 connect(m_view
, SIGNAL(infoMessage(QString
)), m_statusBar
, SLOT(setText(QString
)));
142 connect(m_view
, SIGNAL(operationCompletedMessage(QString
)), m_statusBar
, SLOT(setText(QString
)));
143 connect(m_statusBar
, SIGNAL(stopPressed()), this, SLOT(stopDirectoryLoading()));
144 connect(m_statusBar
, SIGNAL(zoomLevelChanged(int)), this, SLOT(slotStatusBarZoomLevelChanged(int)));
146 m_statusBarTimer
= new QTimer(this);
147 m_statusBarTimer
->setSingleShot(true);
148 m_statusBarTimer
->setInterval(300);
149 connect(m_statusBarTimer
, SIGNAL(timeout()), this, SLOT(updateStatusBar()));
151 KIO::FileUndoManager
* undoManager
= KIO::FileUndoManager::self();
152 connect(undoManager
, SIGNAL(jobRecordingFinished(CommandType
)),
153 this, SLOT(delayedStatusBarUpdate()));
155 // Initialize filter bar
156 m_filterBar
= new FilterBar(this);
157 m_filterBar
->setVisible(settings
->filterBar());
158 connect(m_filterBar
, SIGNAL(filterChanged(QString
)),
159 this, SLOT(setNameFilter(QString
)));
160 connect(m_filterBar
, SIGNAL(closeRequest()),
161 this, SLOT(closeFilterBar()));
162 connect(m_filterBar
, SIGNAL(focusViewRequest()),
163 this, SLOT(requestFocus()));
164 connect(m_view
, SIGNAL(urlChanged(KUrl
)),
165 m_filterBar
, SLOT(clear()));
167 m_topLayout
->addWidget(m_urlNavigator
);
168 m_topLayout
->addWidget(m_searchBox
);
169 m_topLayout
->addWidget(m_messageWidget
);
170 m_topLayout
->addWidget(m_view
);
171 m_topLayout
->addWidget(m_filterBar
);
172 m_topLayout
->addWidget(m_statusBar
);
174 setSearchModeEnabled(isSearchUrl(url
));
176 // Initialize kactivities resource instance
178 #ifdef KActivities_FOUND
179 m_activityResourceInstance
= new KActivities::ResourceInstance(
180 window()->winId(), url
);
181 m_activityResourceInstance
->setParent(this);
185 DolphinViewContainer::~DolphinViewContainer()
189 KUrl
DolphinViewContainer::url() const
191 return m_view
->url();
194 void DolphinViewContainer::setActive(bool active
)
196 m_urlNavigator
->setActive(active
);
197 m_view
->setActive(active
);
199 #ifdef KActivities_FOUND
201 m_activityResourceInstance
->notifyFocusedIn();
203 m_activityResourceInstance
->notifyFocusedOut();
208 bool DolphinViewContainer::isActive() const
210 Q_ASSERT(m_view
->isActive() == m_urlNavigator
->isActive());
211 return m_view
->isActive();
214 void DolphinViewContainer::setAutoGrabFocus(bool grab
)
216 m_autoGrabFocus
= grab
;
219 bool DolphinViewContainer::autoGrabFocus() const
221 return m_autoGrabFocus
;
224 const DolphinStatusBar
* DolphinViewContainer::statusBar() const
229 DolphinStatusBar
* DolphinViewContainer::statusBar()
234 const KUrlNavigator
* DolphinViewContainer::urlNavigator() const
236 return m_urlNavigator
;
239 KUrlNavigator
* DolphinViewContainer::urlNavigator()
241 return m_urlNavigator
;
244 const DolphinView
* DolphinViewContainer::view() const
249 DolphinView
* DolphinViewContainer::view()
254 void DolphinViewContainer::showMessage(const QString
& msg
, MessageType type
)
260 m_messageWidget
->setText(msg
);
263 case Information
: m_messageWidget
->setMessageType(KMessageWidget::Information
); break;
264 case Warning
: m_messageWidget
->setMessageType(KMessageWidget::Warning
); break;
265 case Error
: m_messageWidget
->setMessageType(KMessageWidget::Error
); break;
271 m_messageWidget
->setWordWrap(false);
272 const int unwrappedWidth
= m_messageWidget
->sizeHint().width();
273 m_messageWidget
->setWordWrap(unwrappedWidth
> size().width());
275 m_messageWidget
->animatedShow();
278 void DolphinViewContainer::readSettings()
280 if (GeneralSettings::modifiedStartupSettings()) {
281 // The startup settings should only get applied if they have been
282 // modified by the user. Otherwise keep the (possibly) different current
283 // settings of the URL navigator and the filterbar.
284 m_urlNavigator
->setUrlEditable(GeneralSettings::editableUrl());
285 m_urlNavigator
->setShowFullPath(GeneralSettings::showFullPath());
286 m_urlNavigator
->setHomeUrl(KUrl(GeneralSettings::homeUrl()));
287 setFilterBarVisible(GeneralSettings::filterBar());
290 m_view
->readSettings();
291 m_statusBar
->readSettings();
294 bool DolphinViewContainer::isFilterBarVisible() const
296 return m_filterBar
->isVisible();
299 void DolphinViewContainer::setSearchModeEnabled(bool enabled
)
301 if (enabled
== isSearchModeEnabled()) {
302 if (enabled
&& !m_searchBox
->hasFocus()) {
303 m_searchBox
->setFocus();
304 m_searchBox
->selectAll();
309 m_searchBox
->setVisible(enabled
);
310 m_urlNavigator
->setVisible(!enabled
);
313 KUrl url
= m_urlNavigator
->locationUrl();
314 m_searchBox
->setText(QString());
315 m_searchBox
->setReadOnly(isSearchUrl(url
), url
);
317 // Remember the most recent non-search URL as search path
318 // of the search-box, so that it can be restored
319 // when switching back to the URL navigator.
320 int index
= m_urlNavigator
->historyIndex();
321 const int historySize
= m_urlNavigator
->historySize();
322 while (isSearchUrl(url
) && (index
< historySize
)) {
324 url
= m_urlNavigator
->locationUrl(index
);
327 if (!isSearchUrl(url
)) {
328 m_searchBox
->setSearchPath(url
);
331 m_view
->setViewPropertiesContext(QString());
333 // Restore the URL for the URL navigator. If Dolphin has been
334 // started with a search-URL, the home URL is used as fallback.
335 const KUrl url
= m_searchBox
->searchPath();
336 if (url
.isValid() && !url
.isEmpty()) {
337 if (isSearchUrl(url
)) {
338 m_urlNavigator
->goHome();
340 m_urlNavigator
->setLocationUrl(url
);
346 bool DolphinViewContainer::isSearchModeEnabled() const
348 return m_searchBox
->isVisible();
351 QString
DolphinViewContainer::placesText() const
355 if (isSearchModeEnabled()) {
356 text
= m_searchBox
->searchPath().fileName() + QLatin1String(": ") + m_searchBox
->text();
358 text
= url().fileName();
359 if (text
.isEmpty()) {
367 void DolphinViewContainer::setUrl(const KUrl
& newUrl
)
369 if (newUrl
!= m_urlNavigator
->locationUrl()) {
370 m_urlNavigator
->setLocationUrl(newUrl
);
373 #ifdef KActivities_FOUND
374 m_activityResourceInstance
->setUri(newUrl
);
378 void DolphinViewContainer::setFilterBarVisible(bool visible
)
380 Q_ASSERT(m_filterBar
);
383 m_filterBar
->setFocus();
384 m_filterBar
->selectAll();
390 void DolphinViewContainer::delayedStatusBarUpdate()
392 if (m_statusBarTimer
->isActive() && (m_statusBarTimestamp
.elapsed() > 2000)) {
393 // No update of the statusbar has been done during the last 2 seconds,
394 // although an update has been requested. Trigger an immediate update.
395 m_statusBarTimer
->stop();
398 // Invoke updateStatusBar() with a small delay. This assures that
399 // when a lot of delayedStatusBarUpdates() are done in a short time,
400 // no bottleneck is given.
401 m_statusBarTimer
->start();
405 void DolphinViewContainer::updateStatusBar()
407 m_statusBarTimestamp
.start();
409 const QString text
= m_view
->statusBarText();
410 m_statusBar
->setDefaultText(text
);
411 m_statusBar
->resetToDefaultText();
414 void DolphinViewContainer::updateDirectoryLoadingProgress(int percent
)
416 if (m_statusBar
->progressText().isEmpty()) {
417 m_statusBar
->setProgressText(i18nc("@info:progress", "Loading folder..."));
419 m_statusBar
->setProgress(percent
);
422 void DolphinViewContainer::updateDirectorySortingProgress(int percent
)
424 if (m_statusBar
->progressText().isEmpty()) {
425 m_statusBar
->setProgressText(i18nc("@info:progress", "Sorting..."));
427 m_statusBar
->setProgress(percent
);
430 void DolphinViewContainer::slotDirectoryLoadingStarted()
432 if (isSearchUrl(url())) {
433 // Search KIO-slaves usually don't provide any progress information. Give
434 // a hint to the user that a searching is done:
436 m_statusBar
->setProgressText(i18nc("@info", "Searching..."));
437 m_statusBar
->setProgress(-1);
439 // Trigger an undetermined progress indication. The progress
440 // information in percent will be triggered by the percent() signal
441 // of the directory lister later.
442 updateDirectoryLoadingProgress(-1);
446 void DolphinViewContainer::slotDirectoryLoadingCompleted()
448 if (!m_statusBar
->progressText().isEmpty()) {
449 m_statusBar
->setProgressText(QString());
450 m_statusBar
->setProgress(100);
453 if (isSearchUrl(url()) && m_view
->itemsCount() == 0) {
454 // The dir lister has been completed on a Nepomuk-URI and no items have been found. Instead
455 // of showing the default status bar information ("0 items") a more helpful information is given:
456 m_statusBar
->setText(i18nc("@info:status", "No items found."));
462 void DolphinViewContainer::slotDirectoryLoadingCanceled()
464 if (!m_statusBar
->progressText().isEmpty()) {
465 m_statusBar
->setProgressText(QString());
466 m_statusBar
->setProgress(100);
469 m_statusBar
->setText(QString());
472 void DolphinViewContainer::slotUrlIsFileError(const KUrl
& url
)
474 const KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, url
);
475 slotItemActivated(item
);
478 void DolphinViewContainer::slotItemActivated(const KFileItem
& item
)
480 // It is possible to activate items on inactive views by
481 // drag & drop operations. Assure that activating an item always
482 // results in an active view.
483 m_view
->setActive(true);
485 KUrl url
= item
.targetUrl();
492 if (GeneralSettings::browseThroughArchives() && item
.isFile() && url
.isLocalFile()) {
493 // Generic mechanism for redirecting to tar:/<path>/ when clicking on a tar file,
494 // zip:/<path>/ when clicking on a zip file, etc.
495 // The .protocol file specifies the mimetype that the kioslave handles.
496 // Note that we don't use mimetype inheritance since we don't want to
497 // open OpenDocument files as zip folders...
498 const QString protocol
= KProtocolManager::protocolForArchiveMimetype(item
.mimetype());
499 if (!protocol
.isEmpty()) {
500 url
.setProtocol(protocol
);
506 if (item
.mimetype() == QLatin1String("application/x-desktop")) {
507 // Redirect to the URL in Type=Link desktop files
508 KDesktopFile
desktopFile(url
.toLocalFile());
509 if (desktopFile
.hasLinkType()) {
510 url
= desktopFile
.readUrl();
519 void DolphinViewContainer::slotItemsActivated(const KFileItemList
& items
)
521 Q_ASSERT(items
.count() >= 2);
523 KFileItemActions
fileItemActions(this);
524 fileItemActions
.runPreferredApplications(items
, QString());
527 void DolphinViewContainer::showItemInfo(const KFileItem
& item
)
530 m_statusBar
->resetToDefaultText();
532 const QString text
= item
.isDir() ? item
.text() : item
.getStatusBarInfo();
533 m_statusBar
->setText(text
);
537 void DolphinViewContainer::closeFilterBar()
540 m_filterBar
->clear();
542 emit
showFilterBarChanged(false);
545 void DolphinViewContainer::setNameFilter(const QString
& nameFilter
)
547 m_view
->setNameFilter(nameFilter
);
548 delayedStatusBarUpdate();
551 void DolphinViewContainer::activate()
556 void DolphinViewContainer::slotViewUrlAboutToBeChanged(const KUrl
& url
)
558 // URL changes of the view can happen in two ways:
559 // 1. The URL navigator gets changed and will trigger the view to update its URL
560 // 2. The URL of the view gets changed and will trigger the URL navigator to update
561 // its URL (e.g. by clicking on an item)
562 // In this scope the view-state may only get saved in case 2:
563 if (url
!= m_urlNavigator
->locationUrl()) {
568 void DolphinViewContainer::slotUrlNavigatorLocationAboutToBeChanged(const KUrl
& url
)
570 // URL changes of the view can happen in two ways:
571 // 1. The URL navigator gets changed and will trigger the view to update its URL
572 // 2. The URL of the view gets changed and will trigger the URL navigator to update
573 // its URL (e.g. by clicking on an item)
574 // In this scope the view-state may only get saved in case 1:
575 if (url
!= m_view
->url()) {
580 void DolphinViewContainer::slotUrlNavigatorLocationChanged(const KUrl
& url
)
584 if (KProtocolManager::supportsListing(url
)) {
585 setSearchModeEnabled(isSearchUrl(url
));
588 if (m_autoGrabFocus
&& isActive() && !isSearchUrl(url
)) {
589 // When an URL has been entered, the view should get the focus.
590 // The focus must be requested asynchronously, as changing the URL might create
591 // a new view widget.
592 QTimer::singleShot(0, this, SLOT(requestFocus()));
594 } else if (KProtocolManager::isSourceProtocol(url
)) {
595 QString app
= "konqueror";
596 if (url
.protocol().startsWith(QLatin1String("http"))) {
597 showMessage(i18nc("@info:status", // krazy:exclude=qmethods
598 "Dolphin does not support web pages, the web browser has been launched"),
601 const KConfigGroup
config(KSharedConfig::openConfig("kdeglobals"), "General");
602 const QString browser
= config
.readEntry("BrowserApplication");
603 if (!browser
.isEmpty()) {
605 if (app
.startsWith('!')) {
606 // a literal command has been configured, remove the '!' prefix
611 showMessage(i18nc("@info:status",
612 "Protocol not supported by Dolphin, Konqueror has been launched"),
616 const QString secureUrl
= KShell::quoteArg(url
.pathOrUrl());
617 const QString command
= app
+ ' ' + secureUrl
;
618 KRun::runCommand(command
, app
, app
, this);
620 showMessage(i18nc("@info:status", "Invalid protocol"), Error
);
624 void DolphinViewContainer::dropUrls(const KUrl
& destination
, QDropEvent
* event
)
627 DragAndDropHelper::dropUrls(KFileItem(), destination
, event
, error
);
628 if (!error
.isEmpty()) {
629 showMessage(error
, Error
);
633 void DolphinViewContainer::redirect(const KUrl
& oldUrl
, const KUrl
& newUrl
)
636 const bool block
= m_urlNavigator
->signalsBlocked();
637 m_urlNavigator
->blockSignals(true);
639 // Assure that the location state is reset for redirection URLs. This
640 // allows to skip redirection URLs when going back or forward in the
642 m_urlNavigator
->saveLocationState(QByteArray());
643 m_urlNavigator
->setLocationUrl(newUrl
);
644 setSearchModeEnabled(isSearchUrl(newUrl
));
646 m_urlNavigator
->blockSignals(block
);
649 void DolphinViewContainer::requestFocus()
654 void DolphinViewContainer::saveUrlCompletionMode(KGlobalSettings::Completion completion
)
656 GeneralSettings::setUrlCompletionMode(completion
);
659 void DolphinViewContainer::slotHistoryChanged()
661 QByteArray locationState
= m_urlNavigator
->locationState();
662 if (!locationState
.isEmpty()) {
663 QDataStream
stream(&locationState
, QIODevice::ReadOnly
);
664 m_view
->restoreState(stream
);
668 void DolphinViewContainer::slotReturnPressed()
670 if (!GeneralSettings::editableUrl()) {
671 m_urlNavigator
->setUrlEditable(false);
675 void DolphinViewContainer::startSearching()
677 const KUrl url
= m_searchBox
->urlForSearching();
678 if (url
.isValid() && !url
.isEmpty()) {
679 m_view
->setViewPropertiesContext("search");
680 m_urlNavigator
->setLocationUrl(url
);
684 void DolphinViewContainer::closeSearchBox()
686 setSearchModeEnabled(false);
689 void DolphinViewContainer::stopDirectoryLoading()
691 m_view
->stopLoading();
692 m_statusBar
->setProgress(100);
695 void DolphinViewContainer::slotStatusBarZoomLevelChanged(int zoomLevel
)
697 m_view
->setZoomLevel(zoomLevel
);
700 void DolphinViewContainer::showErrorMessage(const QString
& msg
)
702 showMessage(msg
, Error
);
705 bool DolphinViewContainer::isSearchUrl(const KUrl
& url
) const
707 const QString protocol
= url
.protocol();
708 return protocol
.contains("search") || (protocol
== QLatin1String("nepomuk"));
711 void DolphinViewContainer::saveViewState()
713 QByteArray locationState
;
714 QDataStream
stream(&locationState
, QIODevice::WriteOnly
);
715 m_view
->saveState(stream
);
716 m_urlNavigator
->saveLocationState(locationState
);
719 #include "dolphinviewcontainer.moc"