]>
cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinviewcontainer.cpp
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 #include "dolphin_generalsettings.h"
48 #include "dolphinmainwindow.h"
49 #include "filterbar/filterbar.h"
50 #include "search/dolphinsearchbox.h"
51 #include "statusbar/dolphinstatusbar.h"
52 #include "views/dolphinplacesmodel.h"
53 #include "views/draganddrophelper.h"
54 #include "views/viewmodecontroller.h"
55 #include "views/viewproperties.h"
57 DolphinViewContainer::DolphinViewContainer(const KUrl
& url
, QWidget
* parent
) :
67 m_statusBarTimestamp()
71 m_topLayout
= new QVBoxLayout(this);
72 m_topLayout
->setSpacing(0);
73 m_topLayout
->setMargin(0);
75 m_urlNavigator
= new KUrlNavigator(DolphinPlacesModel::instance(), url
, this);
76 connect(m_urlNavigator
, SIGNAL(urlsDropped(KUrl
,QDropEvent
*)),
77 this, SLOT(dropUrls(KUrl
,QDropEvent
*)));
78 connect(m_urlNavigator
, SIGNAL(activated()),
79 this, SLOT(activate()));
80 connect(m_urlNavigator
->editor(), SIGNAL(completionModeChanged(KGlobalSettings::Completion
)),
81 this, SLOT(saveUrlCompletionMode(KGlobalSettings::Completion
)));
83 const GeneralSettings
* settings
= GeneralSettings::self();
84 m_urlNavigator
->setUrlEditable(settings
->editableUrl());
85 m_urlNavigator
->setShowFullPath(settings
->showFullPath());
86 m_urlNavigator
->setHomeUrl(KUrl(settings
->homeUrl()));
87 KUrlComboBox
* editor
= m_urlNavigator
->editor();
88 editor
->setCompletionMode(KGlobalSettings::Completion(settings
->urlCompletionMode()));
90 m_searchBox
= new DolphinSearchBox(this);
92 connect(m_searchBox
, SIGNAL(closeRequest()), this, SLOT(closeSearchBox()));
93 connect(m_searchBox
, SIGNAL(search(QString
)), this, SLOT(startSearching(QString
)));
94 connect(m_searchBox
, SIGNAL(returnPressed(QString
)), this, SLOT(requestFocus()));
96 m_messageWidget
= new KMessageWidget(this);
97 m_messageWidget
->setCloseButtonVisible(true);
98 m_messageWidget
->hide();
100 m_view
= new DolphinView(url
, this);
101 connect(m_view
, SIGNAL(urlChanged(KUrl
)), m_urlNavigator
, SLOT(setUrl(KUrl
)));
102 connect(m_view
, SIGNAL(writeStateChanged(bool)), this, SIGNAL(writeStateChanged(bool)));
103 connect(m_view
, SIGNAL(requestItemInfo(KFileItem
)), this, SLOT(showItemInfo(KFileItem
)));
104 connect(m_view
, SIGNAL(itemActivated(KFileItem
)), this, SLOT(slotItemActivated(KFileItem
)));
105 connect(m_view
, SIGNAL(redirection(KUrl
,KUrl
)), this, SLOT(redirect(KUrl
,KUrl
)));
106 connect(m_view
, SIGNAL(directoryLoadingStarted()), this, SLOT(slotDirectoryLoadingStarted()));
107 connect(m_view
, SIGNAL(directoryLoadingCompleted()), this, SLOT(slotDirectoryLoadingCompleted()));
108 connect(m_view
, SIGNAL(itemCountChanged()), this, SLOT(delayedStatusBarUpdate()));
109 connect(m_view
, SIGNAL(directoryLoadingProgress(int)), this, SLOT(updateDirectoryLoadingProgress(int)));
110 connect(m_view
, SIGNAL(directorySortingProgress(int)), this, SLOT(updateDirectorySortingProgress(int)));
111 connect(m_view
, SIGNAL(selectionChanged(KFileItemList
)), this, SLOT(delayedStatusBarUpdate()));
112 connect(m_view
, SIGNAL(urlAboutToBeChanged(KUrl
)), this, SLOT(slotViewUrlAboutToBeChanged(KUrl
)));
113 connect(m_view
, SIGNAL(errorMessage(QString
)), this, SLOT(showErrorMessage(QString
)));
115 connect(m_urlNavigator
, SIGNAL(urlAboutToBeChanged(KUrl
)),
116 this, SLOT(slotUrlNavigatorLocationAboutToBeChanged(KUrl
)));
117 connect(m_urlNavigator
, SIGNAL(urlChanged(KUrl
)),
118 this, SLOT(slotUrlNavigatorLocationChanged(KUrl
)));
119 connect(m_urlNavigator
, SIGNAL(historyChanged()),
120 this, SLOT(slotHistoryChanged()));
122 // Initialize status bar
123 m_statusBar
= new DolphinStatusBar(this);
124 m_statusBar
->setUrl(m_view
->url());
125 m_statusBar
->setZoomLevel(m_view
->zoomLevel());
126 connect(m_view
, SIGNAL(urlChanged(KUrl
)), m_statusBar
, SLOT(setUrl(KUrl
)));
127 connect(m_view
, SIGNAL(zoomLevelChanged(int,int)), m_statusBar
, SLOT(setZoomLevel(int)));
128 connect(m_view
, SIGNAL(infoMessage(QString
)), m_statusBar
, SLOT(setText(QString
)));
129 connect(m_view
, SIGNAL(operationCompletedMessage(QString
)), m_statusBar
, SLOT(setText(QString
)));
130 connect(m_statusBar
, SIGNAL(stopPressed()), this, SLOT(stopDirectoryLoading()));
131 connect(m_statusBar
, SIGNAL(zoomLevelChanged(int)), this, SLOT(slotStatusBarZoomLevelChanged(int)));
133 m_statusBarTimer
= new QTimer(this);
134 m_statusBarTimer
->setSingleShot(true);
135 m_statusBarTimer
->setInterval(300);
136 connect(m_statusBarTimer
, SIGNAL(timeout()), this, SLOT(updateStatusBar()));
138 KIO::FileUndoManager
* undoManager
= KIO::FileUndoManager::self();
139 connect(undoManager
, SIGNAL(jobRecordingFinished(CommandType
)),
140 this, SLOT(delayedStatusBarUpdate()));
142 // Initialize filter bar
143 m_filterBar
= new FilterBar(this);
144 m_filterBar
->setVisible(settings
->filterBar());
145 connect(m_filterBar
, SIGNAL(filterChanged(QString
)),
146 this, SLOT(setNameFilter(QString
)));
147 connect(m_filterBar
, SIGNAL(closeRequest()),
148 this, SLOT(closeFilterBar()));
149 connect(m_view
, SIGNAL(urlChanged(KUrl
)),
150 m_filterBar
, SLOT(clear()));
152 m_topLayout
->addWidget(m_urlNavigator
);
153 m_topLayout
->addWidget(m_searchBox
);
154 m_topLayout
->addWidget(m_messageWidget
);
155 m_topLayout
->addWidget(m_view
);
156 m_topLayout
->addWidget(m_filterBar
);
157 m_topLayout
->addWidget(m_statusBar
);
159 setSearchModeEnabled(isSearchUrl(url
));
162 DolphinViewContainer::~DolphinViewContainer()
166 KUrl
DolphinViewContainer::url() const
168 return m_view
->url();
171 void DolphinViewContainer::setActive(bool active
)
173 m_urlNavigator
->setActive(active
);
174 m_view
->setActive(active
);
177 bool DolphinViewContainer::isActive() const
179 Q_ASSERT(m_view
->isActive() == m_urlNavigator
->isActive());
180 return m_view
->isActive();
183 const DolphinStatusBar
* DolphinViewContainer::statusBar() const
188 DolphinStatusBar
* DolphinViewContainer::statusBar()
193 const KUrlNavigator
* DolphinViewContainer::urlNavigator() const
195 return m_urlNavigator
;
198 KUrlNavigator
* DolphinViewContainer::urlNavigator()
200 return m_urlNavigator
;
203 const DolphinView
* DolphinViewContainer::view() const
208 DolphinView
* DolphinViewContainer::view()
213 const DolphinSearchBox
* DolphinViewContainer::searchBox() const
218 DolphinSearchBox
* DolphinViewContainer::searchBox()
223 void DolphinViewContainer::showMessage(const QString
& msg
, MessageType type
)
229 m_messageWidget
->setText(msg
);
232 case Information
: m_messageWidget
->setMessageType(KMessageWidget::Information
); break;
233 case Warning
: m_messageWidget
->setMessageType(KMessageWidget::Warning
); break;
234 case Error
: m_messageWidget
->setMessageType(KMessageWidget::Error
); break;
240 m_messageWidget
->setWordWrap(false);
241 const int unwrappedWidth
= m_messageWidget
->sizeHint().width();
242 m_messageWidget
->setWordWrap(unwrappedWidth
> size().width());
244 m_messageWidget
->animatedShow();
247 void DolphinViewContainer::readSettings()
249 if (GeneralSettings::modifiedStartupSettings()) {
250 // The startup settings should only get applied if they have been
251 // modified by the user. Otherwise keep the (possibly) different current
252 // settings of the URL navigator and the filterbar.
253 m_urlNavigator
->setUrlEditable(GeneralSettings::editableUrl());
254 m_urlNavigator
->setShowFullPath(GeneralSettings::showFullPath());
255 m_urlNavigator
->setHomeUrl(KUrl(GeneralSettings::homeUrl()));
256 setFilterBarVisible(GeneralSettings::filterBar());
259 m_view
->readSettings();
260 m_statusBar
->readSettings();
263 bool DolphinViewContainer::isFilterBarVisible() const
265 return m_filterBar
->isVisible();
268 void DolphinViewContainer::setSearchModeEnabled(bool enabled
)
270 if (enabled
== isSearchModeEnabled()) {
271 if (enabled
&& !m_searchBox
->hasFocus()) {
272 m_searchBox
->setFocus();
273 m_searchBox
->selectAll();
278 m_searchBox
->setVisible(enabled
);
279 m_urlNavigator
->setVisible(!enabled
);
282 KUrl url
= m_urlNavigator
->locationUrl();
283 m_searchBox
->setText(QString());
284 m_searchBox
->setReadOnly(isSearchUrl(url
), url
);
286 // Remember the most recent non-search URL as search path
287 // of the search-box, so that it can be restored
288 // when switching back to the URL navigator.
289 int index
= m_urlNavigator
->historyIndex();
290 const int historySize
= m_urlNavigator
->historySize();
291 while (isSearchUrl(url
) && (index
< historySize
)) {
293 url
= m_urlNavigator
->locationUrl(index
);
296 if (!isSearchUrl(url
)) {
297 m_searchBox
->setSearchPath(url
);
300 // Restore the URL for the URL navigator. If Dolphin has been
301 // started with a search-URL, the home URL is used as fallback.
302 const KUrl url
= m_searchBox
->searchPath();
303 if (url
.isValid() && !url
.isEmpty()) {
304 if (isSearchUrl(url
)) {
305 m_urlNavigator
->goHome();
307 m_urlNavigator
->setLocationUrl(url
);
312 emit
searchModeChanged(enabled
);
315 bool DolphinViewContainer::isSearchModeEnabled() const
317 return m_searchBox
->isVisible();
320 void DolphinViewContainer::setUrl(const KUrl
& newUrl
)
322 if (newUrl
!= m_urlNavigator
->locationUrl()) {
323 m_urlNavigator
->setLocationUrl(newUrl
);
327 void DolphinViewContainer::setFilterBarVisible(bool visible
)
329 Q_ASSERT(m_filterBar
);
332 m_filterBar
->setFocus();
333 m_filterBar
->selectAll();
339 void DolphinViewContainer::delayedStatusBarUpdate()
341 if (m_statusBarTimer
->isActive() && (m_statusBarTimestamp
.elapsed() > 2000)) {
342 // No update of the statusbar has been done during the last 2 seconds,
343 // although an update has been requested. Trigger an immediate update.
344 m_statusBarTimer
->stop();
347 // Invoke updateStatusBar() with a small delay. This assures that
348 // when a lot of delayedStatusBarUpdates() are done in a short time,
349 // no bottleneck is given.
350 m_statusBarTimer
->start();
354 void DolphinViewContainer::updateStatusBar()
356 m_statusBarTimestamp
.start();
358 const QString text
= m_view
->statusBarText();
359 m_statusBar
->setDefaultText(text
);
360 m_statusBar
->resetToDefaultText();
363 void DolphinViewContainer::updateDirectoryLoadingProgress(int percent
)
365 if (m_statusBar
->progressText().isEmpty()) {
366 m_statusBar
->setProgressText(i18nc("@info:progress", "Loading folder..."));
368 m_statusBar
->setProgress(percent
);
371 void DolphinViewContainer::updateDirectorySortingProgress(int percent
)
373 if (m_statusBar
->progressText().isEmpty()) {
374 m_statusBar
->setProgressText(i18nc("@info:progress", "Sorting..."));
376 m_statusBar
->setProgress(percent
);
379 void DolphinViewContainer::slotDirectoryLoadingStarted()
381 if (isSearchUrl(url())) {
382 // Search KIO-slaves usually don't provide any progress information. Give
383 // a hint to the user that a searching is done:
385 m_statusBar
->setProgressText(i18nc("@info", "Searching..."));
386 m_statusBar
->setProgress(-1);
388 // Trigger an undetermined progress indication. The progress
389 // information in percent will be triggered by the percent() signal
390 // of the directory lister later.
391 updateDirectoryLoadingProgress(-1);
395 void DolphinViewContainer::slotDirectoryLoadingCompleted()
397 if (!m_statusBar
->progressText().isEmpty()) {
398 m_statusBar
->setProgressText(QString());
399 m_statusBar
->setProgress(100);
402 if (isSearchUrl(url()) && m_view
->itemsCount() == 0) {
403 // The dir lister has been completed on a Nepomuk-URI and no items have been found. Instead
404 // of showing the default status bar information ("0 items") a more helpful information is given:
405 m_statusBar
->setText(i18nc("@info:status", "No items found."));
411 void DolphinViewContainer::slotItemActivated(const KFileItem
& item
)
413 // It is possible to activate items on inactive views by
414 // drag & drop operations. Assure that activating an item always
415 // results in an active view.
416 m_view
->setActive(true);
418 KUrl url
= item
.targetUrl();
425 if (GeneralSettings::browseThroughArchives() && item
.isFile() && url
.isLocalFile()) {
426 // Generic mechanism for redirecting to tar:/<path>/ when clicking on a tar file,
427 // zip:/<path>/ when clicking on a zip file, etc.
428 // The .protocol file specifies the mimetype that the kioslave handles.
429 // Note that we don't use mimetype inheritance since we don't want to
430 // open OpenDocument files as zip folders...
431 const QString protocol
= KProtocolManager::protocolForArchiveMimetype(item
.mimetype());
432 if (!protocol
.isEmpty()) {
433 url
.setProtocol(protocol
);
439 if (item
.mimetype() == QLatin1String("application/x-desktop")) {
440 // Redirect to the URL in Type=Link desktop files
441 KDesktopFile
desktopFile(url
.toLocalFile());
442 if (desktopFile
.hasLinkType()) {
443 url
= desktopFile
.readUrl();
452 void DolphinViewContainer::showItemInfo(const KFileItem
& item
)
455 m_statusBar
->resetToDefaultText();
457 const QString text
= item
.isDir() ? item
.text() : item
.getStatusBarInfo();
458 m_statusBar
->setText(text
);
462 void DolphinViewContainer::closeFilterBar()
465 m_filterBar
->clear();
467 emit
showFilterBarChanged(false);
470 void DolphinViewContainer::setNameFilter(const QString
& nameFilter
)
472 m_view
->setNameFilter(nameFilter
);
473 delayedStatusBarUpdate();
476 void DolphinViewContainer::activate()
481 void DolphinViewContainer::slotViewUrlAboutToBeChanged(const KUrl
& url
)
483 // URL changes of the view can happen in two ways:
484 // 1. The URL navigator gets changed and will trigger the view to update its URL
485 // 2. The URL of the view gets changed and will trigger the URL navigator to update
486 // its URL (e.g. by clicking on an item)
487 // In this scope the view-state may only get saved in case 2:
488 if (url
!= m_urlNavigator
->locationUrl()) {
493 void DolphinViewContainer::slotUrlNavigatorLocationAboutToBeChanged(const KUrl
& url
)
495 // URL changes of the view can happen in two ways:
496 // 1. The URL navigator gets changed and will trigger the view to update its URL
497 // 2. The URL of the view gets changed and will trigger the URL navigator to update
498 // its URL (e.g. by clicking on an item)
499 // In this scope the view-state may only get saved in case 1:
500 if (url
!= m_view
->url()) {
505 void DolphinViewContainer::slotUrlNavigatorLocationChanged(const KUrl
& url
)
507 if (KProtocolManager::supportsListing(url
)) {
508 setSearchModeEnabled(isSearchUrl(url
));
511 if (isActive() && !isSearchUrl(url
)) {
512 // When an URL has been entered, the view should get the focus.
513 // The focus must be requested asynchronously, as changing the URL might create
514 // a new view widget.
515 QTimer::singleShot(0, this, SLOT(requestFocus()));
517 } else if (KProtocolManager::isSourceProtocol(url
)) {
518 QString app
= "konqueror";
519 if (url
.protocol().startsWith(QLatin1String("http"))) {
520 showMessage(i18nc("@info:status",
521 "Dolphin does not support web pages, the web browser has been launched"),
524 const KConfigGroup
config(KSharedConfig::openConfig("kdeglobals"), "General");
525 const QString browser
= config
.readEntry("BrowserApplication");
526 if (!browser
.isEmpty()) {
528 if (app
.startsWith('!')) {
529 // a literal command has been configured, remove the '!' prefix
534 showMessage(i18nc("@info:status",
535 "Protocol not supported by Dolphin, Konqueror has been launched"),
539 const QString secureUrl
= KShell::quoteArg(url
.pathOrUrl());
540 const QString command
= app
+ ' ' + secureUrl
;
541 KRun::runCommand(command
, app
, app
, this);
543 showMessage(i18nc("@info:status", "Invalid protocol"), Error
);
547 void DolphinViewContainer::dropUrls(const KUrl
& destination
, QDropEvent
* event
)
549 const QString error
= DragAndDropHelper::dropUrls(KFileItem(), destination
, event
);
550 if (!error
.isEmpty()) {
551 showMessage(error
, Error
);
555 void DolphinViewContainer::redirect(const KUrl
& oldUrl
, const KUrl
& newUrl
)
558 const bool block
= m_urlNavigator
->signalsBlocked();
559 m_urlNavigator
->blockSignals(true);
561 // Assure that the location state is reset for redirection URLs. This
562 // allows to skip redirection URLs when going back or forward in the
564 m_urlNavigator
->saveLocationState(QByteArray());
565 m_urlNavigator
->setLocationUrl(newUrl
);
566 setSearchModeEnabled(isSearchUrl(newUrl
));
568 m_urlNavigator
->blockSignals(block
);
571 void DolphinViewContainer::requestFocus()
576 void DolphinViewContainer::saveUrlCompletionMode(KGlobalSettings::Completion completion
)
578 GeneralSettings::setUrlCompletionMode(completion
);
581 void DolphinViewContainer::slotHistoryChanged()
583 QByteArray locationState
= m_urlNavigator
->locationState();
584 if (!locationState
.isEmpty()) {
585 QDataStream
stream(&locationState
, QIODevice::ReadOnly
);
586 m_view
->restoreState(stream
);
590 void DolphinViewContainer::startSearching(const QString
&text
)
593 const KUrl url
= m_searchBox
->urlForSearching();
594 if (url
.isValid() && !url
.isEmpty()) {
595 m_urlNavigator
->setLocationUrl(url
);
599 void DolphinViewContainer::closeSearchBox()
601 setSearchModeEnabled(false);
604 void DolphinViewContainer::stopDirectoryLoading()
606 m_view
->stopLoading();
607 m_statusBar
->setProgress(100);
610 void DolphinViewContainer::slotStatusBarZoomLevelChanged(int zoomLevel
)
612 m_view
->setZoomLevel(zoomLevel
);
615 void DolphinViewContainer::showErrorMessage(const QString
& msg
)
617 showMessage(msg
, Error
);
620 bool DolphinViewContainer::isSearchUrl(const KUrl
& url
) const
622 const QString protocol
= url
.protocol();
623 return protocol
.contains("search") || (protocol
== QLatin1String("nepomuk"));
626 void DolphinViewContainer::saveViewState()
628 QByteArray locationState
;
629 QDataStream
stream(&locationState
, QIODevice::WriteOnly
);
630 m_view
->saveState(stream
);
631 m_urlNavigator
->saveLocationState(locationState
);
634 #include "dolphinviewcontainer.moc"