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(slotUrlChanged()));
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 const KUrl
& url
= DolphinView::openItemAsFolderUrl(item
, GeneralSettings::browseThroughArchives());
486 if (!url
.isEmpty()) {
494 void DolphinViewContainer::slotItemsActivated(const KFileItemList
& items
)
496 Q_ASSERT(items
.count() >= 2);
498 KFileItemActions
fileItemActions(this);
499 fileItemActions
.runPreferredApplications(items
, QString());
502 void DolphinViewContainer::showItemInfo(const KFileItem
& item
)
505 m_statusBar
->resetToDefaultText();
507 const QString text
= item
.isDir() ? item
.text() : item
.getStatusBarInfo();
508 m_statusBar
->setText(text
);
512 void DolphinViewContainer::closeFilterBar()
514 m_filterBar
->closeFilterBar();
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
)
558 if (KProtocolManager::supportsListing(url
)) {
559 setSearchModeEnabled(isSearchUrl(url
));
562 if (m_autoGrabFocus
&& isActive() && !isSearchUrl(url
)) {
563 // When an URL has been entered, the view should get the focus.
564 // The focus must be requested asynchronously, as changing the URL might create
565 // a new view widget.
566 QTimer::singleShot(0, this, SLOT(requestFocus()));
568 } else if (KProtocolManager::isSourceProtocol(url
)) {
569 QString app
= "konqueror";
570 if (url
.protocol().startsWith(QLatin1String("http"))) {
571 showMessage(i18nc("@info:status", // krazy:exclude=qmethods
572 "Dolphin does not support web pages, the web browser has been launched"),
575 const KConfigGroup
config(KSharedConfig::openConfig("kdeglobals"), "General");
576 const QString browser
= config
.readEntry("BrowserApplication");
577 if (!browser
.isEmpty()) {
579 if (app
.startsWith('!')) {
580 // a literal command has been configured, remove the '!' prefix
585 showMessage(i18nc("@info:status",
586 "Protocol not supported by Dolphin, Konqueror has been launched"),
590 const QString secureUrl
= KShell::quoteArg(url
.pathOrUrl());
591 const QString command
= app
+ ' ' + secureUrl
;
592 KRun::runCommand(command
, app
, app
, this);
594 showMessage(i18nc("@info:status", "Invalid protocol"), Error
);
598 void DolphinViewContainer::dropUrls(const KUrl
& destination
, QDropEvent
* event
)
601 DragAndDropHelper::dropUrls(KFileItem(), destination
, event
, error
);
602 if (!error
.isEmpty()) {
603 showMessage(error
, Error
);
607 void DolphinViewContainer::redirect(const KUrl
& oldUrl
, const KUrl
& newUrl
)
610 const bool block
= m_urlNavigator
->signalsBlocked();
611 m_urlNavigator
->blockSignals(true);
613 // Assure that the location state is reset for redirection URLs. This
614 // allows to skip redirection URLs when going back or forward in the
616 m_urlNavigator
->saveLocationState(QByteArray());
617 m_urlNavigator
->setLocationUrl(newUrl
);
618 setSearchModeEnabled(isSearchUrl(newUrl
));
620 m_urlNavigator
->blockSignals(block
);
623 void DolphinViewContainer::requestFocus()
628 void DolphinViewContainer::saveUrlCompletionMode(KGlobalSettings::Completion completion
)
630 GeneralSettings::setUrlCompletionMode(completion
);
633 void DolphinViewContainer::slotHistoryChanged()
635 QByteArray locationState
= m_urlNavigator
->locationState();
636 if (!locationState
.isEmpty()) {
637 QDataStream
stream(&locationState
, QIODevice::ReadOnly
);
638 m_view
->restoreState(stream
);
642 void DolphinViewContainer::slotReturnPressed()
644 if (!GeneralSettings::editableUrl()) {
645 m_urlNavigator
->setUrlEditable(false);
649 void DolphinViewContainer::startSearching()
651 const KUrl url
= m_searchBox
->urlForSearching();
652 if (url
.isValid() && !url
.isEmpty()) {
653 m_view
->setViewPropertiesContext("search");
654 m_urlNavigator
->setLocationUrl(url
);
658 void DolphinViewContainer::closeSearchBox()
660 setSearchModeEnabled(false);
663 void DolphinViewContainer::stopDirectoryLoading()
665 m_view
->stopLoading();
666 m_statusBar
->setProgress(100);
669 void DolphinViewContainer::slotStatusBarZoomLevelChanged(int zoomLevel
)
671 m_view
->setZoomLevel(zoomLevel
);
674 void DolphinViewContainer::showErrorMessage(const QString
& msg
)
676 showMessage(msg
, Error
);
679 bool DolphinViewContainer::isSearchUrl(const KUrl
& url
) const
681 const QString protocol
= url
.protocol();
682 return protocol
.contains("search") || (protocol
== QLatin1String("nepomuk"));
685 void DolphinViewContainer::saveViewState()
687 QByteArray locationState
;
688 QDataStream
stream(&locationState
, QIODevice::WriteOnly
);
689 m_view
->saveState(stream
);
690 m_urlNavigator
->saveLocationState(locationState
);
693 #include "dolphinviewcontainer.moc"