]>
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>
33 #include <KGlobalSettings>
35 #include <KIconEffect>
36 #include <KIO/NetAccess>
37 #include <KIO/PreviewJob>
39 #include <KNewFileMenu>
40 #include <konqmimedata.h>
41 #include <konq_operations.h>
44 #include <KUrlComboBox>
45 #include <KUrlNavigator>
48 #include "dolphin_generalsettings.h"
49 #include "dolphinmainwindow.h"
50 #include "filterbar/filterbar.h"
51 #include "search/dolphinsearchbox.h"
52 #include "settings/dolphinsettings.h"
53 #include "statusbar/dolphinstatusbar.h"
54 #include "views/viewmodecontroller.h"
55 #include "views/viewproperties.h"
57 DolphinViewContainer::DolphinViewContainer(const KUrl
& url
, QWidget
* parent
) :
66 m_statusBarTimestamp()
70 m_topLayout
= new QVBoxLayout(this);
71 m_topLayout
->setSpacing(0);
72 m_topLayout
->setMargin(0);
74 m_urlNavigator
= new KUrlNavigator(DolphinSettings::instance().placesModel(), url
, this);
75 connect(m_urlNavigator
, SIGNAL(urlsDropped(KUrl
,QDropEvent
*)),
76 this, SLOT(dropUrls(KUrl
,QDropEvent
*)));
77 connect(m_urlNavigator
, SIGNAL(activated()),
78 this, SLOT(activate()));
79 connect(m_urlNavigator
->editor(), SIGNAL(completionModeChanged(KGlobalSettings::Completion
)),
80 this, SLOT(saveUrlCompletionMode(KGlobalSettings::Completion
)));
82 const GeneralSettings
* settings
= DolphinSettings::instance().generalSettings();
83 m_urlNavigator
->setUrlEditable(settings
->editableUrl());
84 m_urlNavigator
->setShowFullPath(settings
->showFullPath());
85 m_urlNavigator
->setHomeUrl(KUrl(settings
->homeUrl()));
86 KUrlComboBox
* editor
= m_urlNavigator
->editor();
87 editor
->setCompletionMode(KGlobalSettings::Completion(settings
->urlCompletionMode()));
89 m_searchBox
= new DolphinSearchBox(this);
91 connect(m_searchBox
, SIGNAL(closeRequest()), this, SLOT(closeSearchBox()));
92 connect(m_searchBox
, SIGNAL(search(QString
)), this, SLOT(startSearching(QString
)));
93 connect(m_searchBox
, SIGNAL(returnPressed(QString
)), this, SLOT(requestFocus()));
95 m_view
= new DolphinView(url
, this);
96 connect(m_view
, SIGNAL(urlChanged(KUrl
)), m_urlNavigator
, SLOT(setUrl(KUrl
)));
97 connect(m_view
, SIGNAL(writeStateChanged(bool)), this, SIGNAL(writeStateChanged(bool)));
98 connect(m_view
, SIGNAL(requestItemInfo(KFileItem
)), this, SLOT(showItemInfo(KFileItem
)));
99 connect(m_view
, SIGNAL(errorMessage(QString
)), this, SLOT(showErrorMessage(QString
)));
100 connect(m_view
, SIGNAL(infoMessage(QString
)), this, SLOT(showInfoMessage(QString
)));
101 connect(m_view
, SIGNAL(itemTriggered(KFileItem
)), this, SLOT(slotItemTriggered(KFileItem
)));
102 connect(m_view
, SIGNAL(redirection(KUrl
,KUrl
)), this, SLOT(redirect(KUrl
,KUrl
)));
103 connect(m_view
, SIGNAL(startedPathLoading(KUrl
)), this, SLOT(slotStartedPathLoading()));
104 connect(m_view
, SIGNAL(finishedPathLoading(KUrl
)), this, SLOT(slotFinishedPathLoading()));
105 connect(m_view
, SIGNAL(itemCountChanged()), this, SLOT(delayedStatusBarUpdate()));
106 connect(m_view
, SIGNAL(pathLoadingProgress(int)), this, SLOT(updateProgress(int)));
107 connect(m_view
, SIGNAL(infoMessage(QString
)), this, SLOT(showInfoMessage(QString
)));
108 connect(m_view
, SIGNAL(errorMessage(QString
)), this, SLOT(showErrorMessage(QString
)));
109 connect(m_view
, SIGNAL(urlIsFileError(KUrl
)), this, SLOT(openFile(KUrl
)));
110 connect(m_view
, SIGNAL(selectionChanged(KFileItemList
)), this, SLOT(delayedStatusBarUpdate()));
111 connect(m_view
, SIGNAL(operationCompletedMessage(QString
)), this, SLOT(showOperationCompletedMessage(QString
)));
112 connect(m_view
, SIGNAL(urlAboutToBeChanged(KUrl
)), this, SLOT(slotViewUrlAboutToBeChanged(KUrl
)));
114 connect(m_urlNavigator
, SIGNAL(urlAboutToBeChanged(KUrl
)),
115 this, SLOT(slotUrlNavigatorLocationAboutToBeChanged(KUrl
)));
116 connect(m_urlNavigator
, SIGNAL(urlChanged(KUrl
)),
117 this, SLOT(slotUrlNavigatorLocationChanged(KUrl
)));
118 connect(m_urlNavigator
, SIGNAL(historyChanged()),
119 this, SLOT(slotHistoryChanged()));
121 // initialize status bar
122 m_statusBar
= new DolphinStatusBar(this, m_view
);
123 connect(m_statusBar
, SIGNAL(stopPressed()), this, SLOT(stopLoading()));
125 m_statusBarTimer
= new QTimer(this);
126 m_statusBarTimer
->setSingleShot(true);
127 m_statusBarTimer
->setInterval(300);
128 connect(m_statusBarTimer
, SIGNAL(timeout()),
129 this, SLOT(updateStatusBar()));
131 KIO::FileUndoManager
* undoManager
= KIO::FileUndoManager::self();
132 connect(undoManager
, SIGNAL(jobRecordingFinished(CommandType
)),
133 this, SLOT(delayedStatusBarUpdate()));
135 // initialize filter bar
136 m_filterBar
= new FilterBar(this);
137 m_filterBar
->setVisible(settings
->filterBar());
138 connect(m_filterBar
, SIGNAL(filterChanged(QString
)),
139 this, SLOT(setNameFilter(QString
)));
140 connect(m_filterBar
, SIGNAL(closeRequest()),
141 this, SLOT(closeFilterBar()));
142 connect(m_view
, SIGNAL(urlChanged(KUrl
)),
143 m_filterBar
, SLOT(clear()));
145 m_topLayout
->addWidget(m_urlNavigator
);
146 m_topLayout
->addWidget(m_searchBox
);
147 m_topLayout
->addWidget(m_view
);
148 m_topLayout
->addWidget(m_filterBar
);
149 m_topLayout
->addWidget(m_statusBar
);
151 setSearchModeEnabled(isSearchUrl(url
));
154 DolphinViewContainer::~DolphinViewContainer()
158 KUrl
DolphinViewContainer::url() const
160 return m_view
->url();
163 void DolphinViewContainer::setActive(bool active
)
165 m_urlNavigator
->setActive(active
);
166 m_view
->setActive(active
);
169 bool DolphinViewContainer::isActive() const
171 Q_ASSERT(m_view
->isActive() == m_urlNavigator
->isActive());
172 return m_view
->isActive();
175 const DolphinStatusBar
* DolphinViewContainer::statusBar() const
180 DolphinStatusBar
* DolphinViewContainer::statusBar()
185 const KUrlNavigator
* DolphinViewContainer::urlNavigator() const
187 return m_urlNavigator
;
190 KUrlNavigator
* DolphinViewContainer::urlNavigator()
192 return m_urlNavigator
;
195 const DolphinView
* DolphinViewContainer::view() const
200 DolphinView
* DolphinViewContainer::view()
205 const DolphinSearchBox
* DolphinViewContainer::searchBox() const
210 DolphinSearchBox
* DolphinViewContainer::searchBox()
215 void DolphinViewContainer::refresh()
217 GeneralSettings
* settings
= DolphinSettings::instance().generalSettings();
218 if (settings
->modifiedStartupSettings()) {
219 // The startup settings should only get applied if they have been
220 // modified by the user. Otherwise keep the (possibly) different current
221 // settings of the URL navigator and the filterbar.
222 m_urlNavigator
->setUrlEditable(settings
->editableUrl());
223 m_urlNavigator
->setShowFullPath(settings
->showFullPath());
224 setFilterBarVisible(settings
->filterBar());
228 m_statusBar
->refresh();
231 bool DolphinViewContainer::isFilterBarVisible() const
233 return m_filterBar
->isVisible();
236 void DolphinViewContainer::setSearchModeEnabled(bool enabled
)
238 if (enabled
== isSearchModeEnabled()) {
239 if (enabled
&& !m_searchBox
->hasFocus()) {
240 m_searchBox
->setFocus();
241 m_searchBox
->selectAll();
246 m_searchBox
->setVisible(enabled
);
247 m_urlNavigator
->setVisible(!enabled
);
250 KUrl url
= m_urlNavigator
->locationUrl();
251 m_searchBox
->setText(QString());
252 m_searchBox
->setReadOnly(isSearchUrl(url
), url
);
254 // Remember the most recent non-search URL as search path
255 // of the search-box, so that it can be restored
256 // when switching back to the URL navigator.
257 int index
= m_urlNavigator
->historyIndex();
258 const int historySize
= m_urlNavigator
->historySize();
259 while (isSearchUrl(url
) && (index
< historySize
)) {
261 url
= m_urlNavigator
->locationUrl(index
);
264 if (!isSearchUrl(url
)) {
265 m_searchBox
->setSearchPath(url
);
268 // Restore the URL for the URL navigator. If Dolphin has been
269 // started with a search-URL, the home URL is used as fallback.
270 const KUrl url
= m_searchBox
->searchPath();
271 if (url
.isValid() && !url
.isEmpty()) {
272 if (isSearchUrl(url
)) {
273 m_urlNavigator
->goHome();
275 m_urlNavigator
->setLocationUrl(url
);
280 emit
searchModeChanged(enabled
);
283 bool DolphinViewContainer::isSearchModeEnabled() const
285 return m_searchBox
->isVisible();
288 void DolphinViewContainer::setUrl(const KUrl
& newUrl
)
290 if (newUrl
!= m_urlNavigator
->locationUrl()) {
291 m_urlNavigator
->setLocationUrl(newUrl
);
295 void DolphinViewContainer::setFilterBarVisible(bool visible
)
297 Q_ASSERT(m_filterBar
);
300 m_filterBar
->setFocus();
301 m_filterBar
->selectAll();
307 void DolphinViewContainer::delayedStatusBarUpdate()
309 if (m_statusBarTimer
->isActive() && (m_statusBarTimestamp
.elapsed() > 2000)) {
310 // No update of the statusbar has been done during the last 2 seconds,
311 // although an update has been requested. Trigger an immediate update.
312 m_statusBarTimer
->stop();
315 // Invoke updateStatusBar() with a small delay. This assures that
316 // when a lot of delayedStatusBarUpdates() are done in a short time,
317 // no bottleneck is given.
318 m_statusBarTimer
->start();
322 void DolphinViewContainer::updateStatusBar()
324 m_statusBarTimestamp
.start();
326 const QString newMessage
= m_view
->statusBarText();
327 m_statusBar
->setDefaultText(newMessage
);
329 // We don't want to override errors. Other messages are only protected by
330 // the Statusbar itself depending on timings (see DolphinStatusBar::setMessage).
331 if (m_statusBar
->type() != DolphinStatusBar::Error
) {
332 m_statusBar
->setMessage(newMessage
, DolphinStatusBar::Default
);
336 void DolphinViewContainer::updateProgress(int percent
)
338 if (m_statusBar
->progressText().isEmpty()) {
339 m_statusBar
->setProgressText(i18nc("@info:progress", "Loading folder..."));
341 m_statusBar
->setProgress(percent
);
344 void DolphinViewContainer::slotStartedPathLoading()
346 if (isSearchUrl(url())) {
347 // Search KIO-slaves usually don't provide any progress information. Give
348 // a hint to the user that a searching is done:
350 m_statusBar
->setProgressText(i18nc("@info", "Searching..."));
351 m_statusBar
->setProgress(-1);
353 // Trigger an undetermined progress indication. The progress
354 // information in percent will be triggered by the percent() signal
355 // of the directory lister later.
360 void DolphinViewContainer::slotFinishedPathLoading()
362 if (!m_statusBar
->progressText().isEmpty()) {
363 m_statusBar
->setProgressText(QString());
364 m_statusBar
->setProgress(100);
367 if (isSearchUrl(url()) && m_view
->items().isEmpty()) {
368 // The dir lister has been completed on a Nepomuk-URI and no items have been found. Instead
369 // of showing the default status bar information ("0 items") a more helpful information is given:
370 m_statusBar
->setMessage(i18nc("@info:status", "No items found."), DolphinStatusBar::Information
);
376 void DolphinViewContainer::slotItemTriggered(const KFileItem
& item
)
378 KUrl url
= item
.targetUrl();
385 const GeneralSettings
* settings
= DolphinSettings::instance().generalSettings();
386 const bool browseThroughArchives
= settings
->browseThroughArchives();
387 if (browseThroughArchives
&& item
.isFile() && url
.isLocalFile()) {
388 // Generic mechanism for redirecting to tar:/<path>/ when clicking on a tar file,
389 // zip:/<path>/ when clicking on a zip file, etc.
390 // The .protocol file specifies the mimetype that the kioslave handles.
391 // Note that we don't use mimetype inheritance since we don't want to
392 // open OpenDocument files as zip folders...
393 const QString protocol
= KProtocolManager::protocolForArchiveMimetype(item
.mimetype());
394 if (!protocol
.isEmpty()) {
395 url
.setProtocol(protocol
);
401 if (item
.mimetype() == QLatin1String("application/x-desktop")) {
402 // Redirect to the URL in Type=Link desktop files
403 KDesktopFile
desktopFile(url
.toLocalFile());
404 if (desktopFile
.hasLinkType()) {
405 url
= desktopFile
.readUrl();
414 void DolphinViewContainer::openFile(const KUrl
& url
)
416 const KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, url
);
417 slotItemTriggered(item
);
420 void DolphinViewContainer::showItemInfo(const KFileItem
& item
)
423 // Only clear the status bar if unimportant messages are shown.
424 // This prevents that information- or error-messages get hidden
425 // by moving the mouse above the viewport or when closing the
427 if (m_statusBar
->type() == DolphinStatusBar::Default
) {
428 m_statusBar
->clear();
431 m_statusBar
->setMessage(item
.getStatusBarInfo(), DolphinStatusBar::Default
);
435 void DolphinViewContainer::showInfoMessage(const QString
& msg
)
437 m_statusBar
->setMessage(msg
, DolphinStatusBar::Information
);
440 void DolphinViewContainer::showErrorMessage(const QString
& msg
)
442 m_statusBar
->setMessage(msg
, DolphinStatusBar::Error
);
445 void DolphinViewContainer::showOperationCompletedMessage(const QString
& msg
)
447 m_statusBar
->setMessage(msg
, DolphinStatusBar::OperationCompleted
);
450 void DolphinViewContainer::closeFilterBar()
453 m_filterBar
->clear();
455 emit
showFilterBarChanged(false);
458 void DolphinViewContainer::setNameFilter(const QString
& nameFilter
)
460 m_view
->setNameFilter(nameFilter
);
461 delayedStatusBarUpdate();
464 void DolphinViewContainer::activate()
469 void DolphinViewContainer::slotViewUrlAboutToBeChanged(const KUrl
& url
)
471 // URL changes of the view can happen in two ways:
472 // 1. The URL navigator gets changed and will trigger the view to update its URL
473 // 2. The URL of the view gets changed and will trigger the URL navigator to update
474 // its URL (e.g. by clicking on an item)
475 // In this scope the view-state may only get saved in case 2:
476 if (url
!= m_urlNavigator
->locationUrl()) {
481 void DolphinViewContainer::slotUrlNavigatorLocationAboutToBeChanged(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 1:
488 if (url
!= m_view
->url()) {
493 void DolphinViewContainer::slotUrlNavigatorLocationChanged(const KUrl
& url
)
495 if (KProtocolManager::supportsListing(url
)) {
496 setSearchModeEnabled(isSearchUrl(url
));
499 if (isActive() && !isSearchUrl(url
)) {
500 // When an URL has been entered, the view should get the focus.
501 // The focus must be requested asynchronously, as changing the URL might create
502 // a new view widget.
503 QTimer::singleShot(0, this, SLOT(requestFocus()));
505 } else if (KProtocolManager::isSourceProtocol(url
)) {
506 QString app
= "konqueror";
507 if (url
.protocol().startsWith(QLatin1String("http"))) {
508 showErrorMessage(i18nc("@info:status",
509 "Dolphin does not support web pages, the web browser has been launched"));
510 const KConfigGroup
config(KSharedConfig::openConfig("kdeglobals"), "General");
511 const QString browser
= config
.readEntry("BrowserApplication");
512 if (!browser
.isEmpty()) {
514 if (app
.startsWith('!')) {
515 // a literal command has been configured, remove the '!' prefix
520 showErrorMessage(i18nc("@info:status",
521 "Protocol not supported by Dolphin, Konqueror has been launched"));
524 const QString secureUrl
= KShell::quoteArg(url
.pathOrUrl());
525 const QString command
= app
+ ' ' + secureUrl
;
526 KRun::runCommand(command
, app
, app
, this);
528 showErrorMessage(i18nc("@info:status", "Invalid protocol"));
532 void DolphinViewContainer::dropUrls(const KUrl
& destination
, QDropEvent
* event
)
534 Q_UNUSED(destination
);
536 //DragAndDropHelper::instance().dropUrls(KFileItem(), destination, event, this);
539 void DolphinViewContainer::redirect(const KUrl
& oldUrl
, const KUrl
& newUrl
)
542 const bool block
= m_urlNavigator
->signalsBlocked();
543 m_urlNavigator
->blockSignals(true);
545 // Assure that the location state is reset for redirection URLs. This
546 // allows to skip redirection URLs when going back or forward in the
548 m_urlNavigator
->saveLocationState(QByteArray());
549 m_urlNavigator
->setLocationUrl(newUrl
);
550 setSearchModeEnabled(isSearchUrl(newUrl
));
552 m_urlNavigator
->blockSignals(block
);
555 void DolphinViewContainer::requestFocus()
560 void DolphinViewContainer::saveUrlCompletionMode(KGlobalSettings::Completion completion
)
562 DolphinSettings
& settings
= DolphinSettings::instance();
563 settings
.generalSettings()->setUrlCompletionMode(completion
);
567 void DolphinViewContainer::slotHistoryChanged()
569 QByteArray locationState
= m_urlNavigator
->locationState();
570 if (!locationState
.isEmpty()) {
571 QDataStream
stream(&locationState
, QIODevice::ReadOnly
);
572 m_view
->restoreState(stream
);
576 void DolphinViewContainer::startSearching(const QString
&text
)
579 const KUrl url
= m_searchBox
->urlForSearching();
580 if (url
.isValid() && !url
.isEmpty()) {
581 m_urlNavigator
->setLocationUrl(url
);
585 void DolphinViewContainer::closeSearchBox()
587 setSearchModeEnabled(false);
590 void DolphinViewContainer::stopLoading()
592 m_view
->stopLoading();
593 m_statusBar
->setProgress(100);
596 bool DolphinViewContainer::isSearchUrl(const KUrl
& url
) const
598 const QString protocol
= url
.protocol();
599 return protocol
.contains("search") || (protocol
== QLatin1String("nepomuk"));
602 void DolphinViewContainer::saveViewState()
604 QByteArray locationState
;
605 QDataStream
stream(&locationState
, QIODevice::WriteOnly
);
606 m_view
->saveState(stream
);
607 m_urlNavigator
->saveLocationState(locationState
);
610 #include "dolphinviewcontainer.moc"