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(itemCountChanged()), this, SLOT(delayedStatusBarUpdate()));
115 connect(m_view
, SIGNAL(directoryLoadingProgress(int)), this, SLOT(updateDirectoryLoadingProgress(int)));
116 connect(m_view
, SIGNAL(directorySortingProgress(int)), this, SLOT(updateDirectorySortingProgress(int)));
117 connect(m_view
, SIGNAL(selectionChanged(KFileItemList
)), this, SLOT(delayedStatusBarUpdate()));
118 connect(m_view
, SIGNAL(urlAboutToBeChanged(KUrl
)), this, SLOT(slotViewUrlAboutToBeChanged(KUrl
)));
119 connect(m_view
, SIGNAL(errorMessage(QString
)), this, SLOT(showErrorMessage(QString
)));
120 connect(m_view
, SIGNAL(urlIsFileError(KUrl
)), this, SLOT(slotUrlIsFileError(KUrl
)));
122 connect(m_urlNavigator
, SIGNAL(urlAboutToBeChanged(KUrl
)),
123 this, SLOT(slotUrlNavigatorLocationAboutToBeChanged(KUrl
)));
124 connect(m_urlNavigator
, SIGNAL(urlChanged(KUrl
)),
125 this, SLOT(slotUrlNavigatorLocationChanged(KUrl
)));
126 connect(m_urlNavigator
, SIGNAL(historyChanged()),
127 this, SLOT(slotHistoryChanged()));
129 // Initialize status bar
130 m_statusBar
= new DolphinStatusBar(this);
131 m_statusBar
->setUrl(m_view
->url());
132 m_statusBar
->setZoomLevel(m_view
->zoomLevel());
133 connect(m_view
, SIGNAL(urlChanged(KUrl
)), m_statusBar
, SLOT(setUrl(KUrl
)));
134 connect(m_view
, SIGNAL(zoomLevelChanged(int,int)), m_statusBar
, SLOT(setZoomLevel(int)));
135 connect(m_view
, SIGNAL(infoMessage(QString
)), m_statusBar
, SLOT(setText(QString
)));
136 connect(m_view
, SIGNAL(operationCompletedMessage(QString
)), m_statusBar
, SLOT(setText(QString
)));
137 connect(m_statusBar
, SIGNAL(stopPressed()), this, SLOT(stopDirectoryLoading()));
138 connect(m_statusBar
, SIGNAL(zoomLevelChanged(int)), this, SLOT(slotStatusBarZoomLevelChanged(int)));
140 m_statusBarTimer
= new QTimer(this);
141 m_statusBarTimer
->setSingleShot(true);
142 m_statusBarTimer
->setInterval(300);
143 connect(m_statusBarTimer
, SIGNAL(timeout()), this, SLOT(updateStatusBar()));
145 KIO::FileUndoManager
* undoManager
= KIO::FileUndoManager::self();
146 connect(undoManager
, SIGNAL(jobRecordingFinished(CommandType
)),
147 this, SLOT(delayedStatusBarUpdate()));
149 // Initialize filter bar
150 m_filterBar
= new FilterBar(this);
151 m_filterBar
->setVisible(settings
->filterBar());
152 connect(m_filterBar
, SIGNAL(filterChanged(QString
)),
153 this, SLOT(setNameFilter(QString
)));
154 connect(m_filterBar
, SIGNAL(closeRequest()),
155 this, SLOT(closeFilterBar()));
156 connect(m_view
, SIGNAL(urlChanged(KUrl
)),
157 m_filterBar
, SLOT(clear()));
159 m_topLayout
->addWidget(m_urlNavigator
);
160 m_topLayout
->addWidget(m_searchBox
);
161 m_topLayout
->addWidget(m_messageWidget
);
162 m_topLayout
->addWidget(m_view
);
163 m_topLayout
->addWidget(m_filterBar
);
164 m_topLayout
->addWidget(m_statusBar
);
166 setSearchModeEnabled(isSearchUrl(url
));
168 // Initialize kactivities resource instance
170 #ifdef KActivities_FOUND
171 m_activityResourceInstance
= new KActivities::ResourceInstance(
172 window()->winId(), url
);
173 m_activityResourceInstance
->setParent(this);
177 DolphinViewContainer::~DolphinViewContainer()
181 KUrl
DolphinViewContainer::url() const
183 return m_view
->url();
186 void DolphinViewContainer::setActive(bool active
)
188 m_urlNavigator
->setActive(active
);
189 m_view
->setActive(active
);
191 #ifdef KActivities_FOUND
193 m_activityResourceInstance
->notifyFocusedIn();
195 m_activityResourceInstance
->notifyFocusedOut();
200 bool DolphinViewContainer::isActive() const
202 Q_ASSERT(m_view
->isActive() == m_urlNavigator
->isActive());
203 return m_view
->isActive();
206 void DolphinViewContainer::setAutoGrabFocus(bool grab
)
208 m_autoGrabFocus
= grab
;
211 bool DolphinViewContainer::autoGrabFocus() const
213 return m_autoGrabFocus
;
216 const DolphinStatusBar
* DolphinViewContainer::statusBar() const
221 DolphinStatusBar
* DolphinViewContainer::statusBar()
226 const KUrlNavigator
* DolphinViewContainer::urlNavigator() const
228 return m_urlNavigator
;
231 KUrlNavigator
* DolphinViewContainer::urlNavigator()
233 return m_urlNavigator
;
236 const DolphinView
* DolphinViewContainer::view() const
241 DolphinView
* DolphinViewContainer::view()
246 void DolphinViewContainer::showMessage(const QString
& msg
, MessageType type
)
252 m_messageWidget
->setText(msg
);
255 case Information
: m_messageWidget
->setMessageType(KMessageWidget::Information
); break;
256 case Warning
: m_messageWidget
->setMessageType(KMessageWidget::Warning
); break;
257 case Error
: m_messageWidget
->setMessageType(KMessageWidget::Error
); break;
263 m_messageWidget
->setWordWrap(false);
264 const int unwrappedWidth
= m_messageWidget
->sizeHint().width();
265 m_messageWidget
->setWordWrap(unwrappedWidth
> size().width());
267 m_messageWidget
->animatedShow();
270 void DolphinViewContainer::readSettings()
272 if (GeneralSettings::modifiedStartupSettings()) {
273 // The startup settings should only get applied if they have been
274 // modified by the user. Otherwise keep the (possibly) different current
275 // settings of the URL navigator and the filterbar.
276 m_urlNavigator
->setUrlEditable(GeneralSettings::editableUrl());
277 m_urlNavigator
->setShowFullPath(GeneralSettings::showFullPath());
278 m_urlNavigator
->setHomeUrl(KUrl(GeneralSettings::homeUrl()));
279 setFilterBarVisible(GeneralSettings::filterBar());
282 m_view
->readSettings();
283 m_statusBar
->readSettings();
286 bool DolphinViewContainer::isFilterBarVisible() const
288 return m_filterBar
->isVisible();
291 void DolphinViewContainer::setSearchModeEnabled(bool enabled
)
293 if (enabled
== isSearchModeEnabled()) {
294 if (enabled
&& !m_searchBox
->hasFocus()) {
295 m_searchBox
->setFocus();
296 m_searchBox
->selectAll();
301 m_searchBox
->setVisible(enabled
);
302 m_urlNavigator
->setVisible(!enabled
);
305 KUrl url
= m_urlNavigator
->locationUrl();
306 m_searchBox
->setText(QString());
307 m_searchBox
->setReadOnly(isSearchUrl(url
), url
);
309 // Remember the most recent non-search URL as search path
310 // of the search-box, so that it can be restored
311 // when switching back to the URL navigator.
312 int index
= m_urlNavigator
->historyIndex();
313 const int historySize
= m_urlNavigator
->historySize();
314 while (isSearchUrl(url
) && (index
< historySize
)) {
316 url
= m_urlNavigator
->locationUrl(index
);
319 if (!isSearchUrl(url
)) {
320 m_searchBox
->setSearchPath(url
);
323 m_view
->setViewPropertiesContext(QString());
325 // Restore the URL for the URL navigator. If Dolphin has been
326 // started with a search-URL, the home URL is used as fallback.
327 const KUrl url
= m_searchBox
->searchPath();
328 if (url
.isValid() && !url
.isEmpty()) {
329 if (isSearchUrl(url
)) {
330 m_urlNavigator
->goHome();
332 m_urlNavigator
->setLocationUrl(url
);
338 bool DolphinViewContainer::isSearchModeEnabled() const
340 return m_searchBox
->isVisible();
343 QString
DolphinViewContainer::placesText() const
347 if (isSearchModeEnabled()) {
348 text
= m_searchBox
->searchPath().fileName() + QLatin1String(": ") + m_searchBox
->text();
350 text
= url().fileName();
351 if (text
.isEmpty()) {
359 void DolphinViewContainer::setUrl(const KUrl
& newUrl
)
361 if (newUrl
!= m_urlNavigator
->locationUrl()) {
362 m_urlNavigator
->setLocationUrl(newUrl
);
365 #ifdef KActivities_FOUND
366 m_activityResourceInstance
->setUri(newUrl
);
370 void DolphinViewContainer::setFilterBarVisible(bool visible
)
372 Q_ASSERT(m_filterBar
);
375 m_filterBar
->setFocus();
376 m_filterBar
->selectAll();
382 void DolphinViewContainer::delayedStatusBarUpdate()
384 if (m_statusBarTimer
->isActive() && (m_statusBarTimestamp
.elapsed() > 2000)) {
385 // No update of the statusbar has been done during the last 2 seconds,
386 // although an update has been requested. Trigger an immediate update.
387 m_statusBarTimer
->stop();
390 // Invoke updateStatusBar() with a small delay. This assures that
391 // when a lot of delayedStatusBarUpdates() are done in a short time,
392 // no bottleneck is given.
393 m_statusBarTimer
->start();
397 void DolphinViewContainer::updateStatusBar()
399 m_statusBarTimestamp
.start();
401 const QString text
= m_view
->statusBarText();
402 m_statusBar
->setDefaultText(text
);
403 m_statusBar
->resetToDefaultText();
406 void DolphinViewContainer::updateDirectoryLoadingProgress(int percent
)
408 if (m_statusBar
->progressText().isEmpty()) {
409 m_statusBar
->setProgressText(i18nc("@info:progress", "Loading folder..."));
411 m_statusBar
->setProgress(percent
);
414 void DolphinViewContainer::updateDirectorySortingProgress(int percent
)
416 if (m_statusBar
->progressText().isEmpty()) {
417 m_statusBar
->setProgressText(i18nc("@info:progress", "Sorting..."));
419 m_statusBar
->setProgress(percent
);
422 void DolphinViewContainer::slotDirectoryLoadingStarted()
424 if (isSearchUrl(url())) {
425 // Search KIO-slaves usually don't provide any progress information. Give
426 // a hint to the user that a searching is done:
428 m_statusBar
->setProgressText(i18nc("@info", "Searching..."));
429 m_statusBar
->setProgress(-1);
431 // Trigger an undetermined progress indication. The progress
432 // information in percent will be triggered by the percent() signal
433 // of the directory lister later.
434 updateDirectoryLoadingProgress(-1);
438 void DolphinViewContainer::slotDirectoryLoadingCompleted()
440 if (!m_statusBar
->progressText().isEmpty()) {
441 m_statusBar
->setProgressText(QString());
442 m_statusBar
->setProgress(100);
445 if (isSearchUrl(url()) && m_view
->itemsCount() == 0) {
446 // The dir lister has been completed on a Nepomuk-URI and no items have been found. Instead
447 // of showing the default status bar information ("0 items") a more helpful information is given:
448 m_statusBar
->setText(i18nc("@info:status", "No items found."));
454 void DolphinViewContainer::slotUrlIsFileError(const KUrl
& url
)
456 const KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, url
);
457 slotItemActivated(item
);
460 void DolphinViewContainer::slotItemActivated(const KFileItem
& item
)
462 // It is possible to activate items on inactive views by
463 // drag & drop operations. Assure that activating an item always
464 // results in an active view.
465 m_view
->setActive(true);
467 KUrl url
= item
.targetUrl();
474 if (GeneralSettings::browseThroughArchives() && item
.isFile() && url
.isLocalFile()) {
475 // Generic mechanism for redirecting to tar:/<path>/ when clicking on a tar file,
476 // zip:/<path>/ when clicking on a zip file, etc.
477 // The .protocol file specifies the mimetype that the kioslave handles.
478 // Note that we don't use mimetype inheritance since we don't want to
479 // open OpenDocument files as zip folders...
480 const QString protocol
= KProtocolManager::protocolForArchiveMimetype(item
.mimetype());
481 if (!protocol
.isEmpty()) {
482 url
.setProtocol(protocol
);
488 if (item
.mimetype() == QLatin1String("application/x-desktop")) {
489 // Redirect to the URL in Type=Link desktop files
490 KDesktopFile
desktopFile(url
.toLocalFile());
491 if (desktopFile
.hasLinkType()) {
492 url
= desktopFile
.readUrl();
501 void DolphinViewContainer::showItemInfo(const KFileItem
& item
)
504 m_statusBar
->resetToDefaultText();
506 const QString text
= item
.isDir() ? item
.text() : item
.getStatusBarInfo();
507 m_statusBar
->setText(text
);
511 void DolphinViewContainer::closeFilterBar()
514 m_filterBar
->clear();
516 emit
showFilterBarChanged(false);
519 void DolphinViewContainer::setNameFilter(const QString
& nameFilter
)
521 m_view
->setNameFilter(nameFilter
);
522 delayedStatusBarUpdate();
525 void DolphinViewContainer::activate()
530 void DolphinViewContainer::slotViewUrlAboutToBeChanged(const KUrl
& url
)
532 // URL changes of the view can happen in two ways:
533 // 1. The URL navigator gets changed and will trigger the view to update its URL
534 // 2. The URL of the view gets changed and will trigger the URL navigator to update
535 // its URL (e.g. by clicking on an item)
536 // In this scope the view-state may only get saved in case 2:
537 if (url
!= m_urlNavigator
->locationUrl()) {
542 void DolphinViewContainer::slotUrlNavigatorLocationAboutToBeChanged(const KUrl
& url
)
544 // URL changes of the view can happen in two ways:
545 // 1. The URL navigator gets changed and will trigger the view to update its URL
546 // 2. The URL of the view gets changed and will trigger the URL navigator to update
547 // its URL (e.g. by clicking on an item)
548 // In this scope the view-state may only get saved in case 1:
549 if (url
!= m_view
->url()) {
554 void DolphinViewContainer::slotUrlNavigatorLocationChanged(const KUrl
& url
)
556 if (KProtocolManager::supportsListing(url
)) {
557 setSearchModeEnabled(isSearchUrl(url
));
560 if (m_autoGrabFocus
&& isActive() && !isSearchUrl(url
)) {
561 // When an URL has been entered, the view should get the focus.
562 // The focus must be requested asynchronously, as changing the URL might create
563 // a new view widget.
564 QTimer::singleShot(0, this, SLOT(requestFocus()));
566 } else if (KProtocolManager::isSourceProtocol(url
)) {
567 QString app
= "konqueror";
568 if (url
.protocol().startsWith(QLatin1String("http"))) {
569 showMessage(i18nc("@info:status", // krazy:exclude=qmethods
570 "Dolphin does not support web pages, the web browser has been launched"),
573 const KConfigGroup
config(KSharedConfig::openConfig("kdeglobals"), "General");
574 const QString browser
= config
.readEntry("BrowserApplication");
575 if (!browser
.isEmpty()) {
577 if (app
.startsWith('!')) {
578 // a literal command has been configured, remove the '!' prefix
583 showMessage(i18nc("@info:status",
584 "Protocol not supported by Dolphin, Konqueror has been launched"),
588 const QString secureUrl
= KShell::quoteArg(url
.pathOrUrl());
589 const QString command
= app
+ ' ' + secureUrl
;
590 KRun::runCommand(command
, app
, app
, this);
592 showMessage(i18nc("@info:status", "Invalid protocol"), Error
);
596 void DolphinViewContainer::dropUrls(const KUrl
& destination
, QDropEvent
* event
)
598 const QString error
= DragAndDropHelper::dropUrls(KFileItem(), destination
, event
);
599 if (!error
.isEmpty()) {
600 showMessage(error
, Error
);
604 void DolphinViewContainer::redirect(const KUrl
& oldUrl
, const KUrl
& newUrl
)
607 const bool block
= m_urlNavigator
->signalsBlocked();
608 m_urlNavigator
->blockSignals(true);
610 // Assure that the location state is reset for redirection URLs. This
611 // allows to skip redirection URLs when going back or forward in the
613 m_urlNavigator
->saveLocationState(QByteArray());
614 m_urlNavigator
->setLocationUrl(newUrl
);
615 setSearchModeEnabled(isSearchUrl(newUrl
));
617 m_urlNavigator
->blockSignals(block
);
620 void DolphinViewContainer::requestFocus()
625 void DolphinViewContainer::saveUrlCompletionMode(KGlobalSettings::Completion completion
)
627 GeneralSettings::setUrlCompletionMode(completion
);
630 void DolphinViewContainer::slotHistoryChanged()
632 QByteArray locationState
= m_urlNavigator
->locationState();
633 if (!locationState
.isEmpty()) {
634 QDataStream
stream(&locationState
, QIODevice::ReadOnly
);
635 m_view
->restoreState(stream
);
639 void DolphinViewContainer::startSearching()
641 const KUrl url
= m_searchBox
->urlForSearching();
642 if (url
.isValid() && !url
.isEmpty()) {
643 m_view
->setViewPropertiesContext("search");
644 m_urlNavigator
->setLocationUrl(url
);
648 void DolphinViewContainer::closeSearchBox()
650 setSearchModeEnabled(false);
653 void DolphinViewContainer::stopDirectoryLoading()
655 m_view
->stopLoading();
656 m_statusBar
->setProgress(100);
659 void DolphinViewContainer::slotStatusBarZoomLevelChanged(int zoomLevel
)
661 m_view
->setZoomLevel(zoomLevel
);
664 void DolphinViewContainer::showErrorMessage(const QString
& msg
)
666 showMessage(msg
, Error
);
669 bool DolphinViewContainer::isSearchUrl(const KUrl
& url
) const
671 const QString protocol
= url
.protocol();
672 return protocol
.contains("search") || (protocol
== QLatin1String("nepomuk"));
675 void DolphinViewContainer::saveViewState()
677 QByteArray locationState
;
678 QDataStream
stream(&locationState
, QIODevice::WriteOnly
);
679 m_view
->saveState(stream
);
680 m_urlNavigator
->saveLocationState(locationState
);
683 #include "dolphinviewcontainer.moc"