]>
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 <KNewFileMenu>
38 #include <konqmimedata.h>
39 #include <konq_operations.h>
42 #include <KUrlComboBox>
43 #include <KUrlNavigator>
46 #include "dolphin_generalsettings.h"
47 #include "dolphinmainwindow.h"
48 #include "filterbar/filterbar.h"
49 #include "search/dolphinsearchbox.h"
50 #include "statusbar/dolphinstatusbar.h"
51 #include "views/dolphinplacesmodel.h"
52 #include "views/viewmodecontroller.h"
53 #include "views/viewproperties.h"
55 DolphinViewContainer::DolphinViewContainer(const KUrl
& url
, QWidget
* parent
) :
64 m_statusBarTimestamp()
68 m_topLayout
= new QVBoxLayout(this);
69 m_topLayout
->setSpacing(0);
70 m_topLayout
->setMargin(0);
72 m_urlNavigator
= new KUrlNavigator(DolphinPlacesModel::instance(), url
, this);
73 connect(m_urlNavigator
, SIGNAL(urlsDropped(KUrl
,QDropEvent
*)),
74 this, SLOT(dropUrls(KUrl
,QDropEvent
*)));
75 connect(m_urlNavigator
, SIGNAL(activated()),
76 this, SLOT(activate()));
77 connect(m_urlNavigator
->editor(), SIGNAL(completionModeChanged(KGlobalSettings::Completion
)),
78 this, SLOT(saveUrlCompletionMode(KGlobalSettings::Completion
)));
80 const GeneralSettings
* settings
= GeneralSettings::self();
81 m_urlNavigator
->setUrlEditable(settings
->editableUrl());
82 m_urlNavigator
->setShowFullPath(settings
->showFullPath());
83 m_urlNavigator
->setHomeUrl(KUrl(settings
->homeUrl()));
84 KUrlComboBox
* editor
= m_urlNavigator
->editor();
85 editor
->setCompletionMode(KGlobalSettings::Completion(settings
->urlCompletionMode()));
87 m_searchBox
= new DolphinSearchBox(this);
89 connect(m_searchBox
, SIGNAL(closeRequest()), this, SLOT(closeSearchBox()));
90 connect(m_searchBox
, SIGNAL(search(QString
)), this, SLOT(startSearching(QString
)));
91 connect(m_searchBox
, SIGNAL(returnPressed(QString
)), this, SLOT(requestFocus()));
93 m_view
= new DolphinView(url
, this);
94 connect(m_view
, SIGNAL(urlChanged(KUrl
)), m_urlNavigator
, SLOT(setUrl(KUrl
)));
95 connect(m_view
, SIGNAL(writeStateChanged(bool)), this, SIGNAL(writeStateChanged(bool)));
96 connect(m_view
, SIGNAL(requestItemInfo(KFileItem
)), this, SLOT(showItemInfo(KFileItem
)));
97 connect(m_view
, SIGNAL(errorMessage(QString
)), this, SLOT(showErrorMessage(QString
)));
98 connect(m_view
, SIGNAL(infoMessage(QString
)), this, SLOT(showInfoMessage(QString
)));
99 connect(m_view
, SIGNAL(itemActivated(KFileItem
)), this, SLOT(slotItemActivated(KFileItem
)));
100 connect(m_view
, SIGNAL(redirection(KUrl
,KUrl
)), this, SLOT(redirect(KUrl
,KUrl
)));
101 connect(m_view
, SIGNAL(startedPathLoading(KUrl
)), this, SLOT(slotStartedPathLoading()));
102 connect(m_view
, SIGNAL(finishedPathLoading(KUrl
)), this, SLOT(slotFinishedPathLoading()));
103 connect(m_view
, SIGNAL(itemCountChanged()), this, SLOT(delayedStatusBarUpdate()));
104 connect(m_view
, SIGNAL(pathLoadingProgress(int)), this, SLOT(updateProgress(int)));
105 connect(m_view
, SIGNAL(infoMessage(QString
)), this, SLOT(showInfoMessage(QString
)));
106 connect(m_view
, SIGNAL(errorMessage(QString
)), this, SLOT(showErrorMessage(QString
)));
107 connect(m_view
, SIGNAL(urlIsFileError(KUrl
)), this, SLOT(openFile(KUrl
)));
108 connect(m_view
, SIGNAL(selectionChanged(KFileItemList
)), this, SLOT(delayedStatusBarUpdate()));
109 connect(m_view
, SIGNAL(operationCompletedMessage(QString
)), this, SLOT(showOperationCompletedMessage(QString
)));
110 connect(m_view
, SIGNAL(urlAboutToBeChanged(KUrl
)), this, SLOT(slotViewUrlAboutToBeChanged(KUrl
)));
112 connect(m_urlNavigator
, SIGNAL(urlAboutToBeChanged(KUrl
)),
113 this, SLOT(slotUrlNavigatorLocationAboutToBeChanged(KUrl
)));
114 connect(m_urlNavigator
, SIGNAL(urlChanged(KUrl
)),
115 this, SLOT(slotUrlNavigatorLocationChanged(KUrl
)));
116 connect(m_urlNavigator
, SIGNAL(historyChanged()),
117 this, SLOT(slotHistoryChanged()));
119 // initialize status bar
120 m_statusBar
= new DolphinStatusBar(this, m_view
);
121 connect(m_statusBar
, SIGNAL(stopPressed()), this, SLOT(stopLoading()));
123 m_statusBarTimer
= new QTimer(this);
124 m_statusBarTimer
->setSingleShot(true);
125 m_statusBarTimer
->setInterval(300);
126 connect(m_statusBarTimer
, SIGNAL(timeout()),
127 this, SLOT(updateStatusBar()));
129 KIO::FileUndoManager
* undoManager
= KIO::FileUndoManager::self();
130 connect(undoManager
, SIGNAL(jobRecordingFinished(CommandType
)),
131 this, SLOT(delayedStatusBarUpdate()));
133 // initialize filter bar
134 m_filterBar
= new FilterBar(this);
135 m_filterBar
->setVisible(settings
->filterBar());
136 connect(m_filterBar
, SIGNAL(filterChanged(QString
)),
137 this, SLOT(setNameFilter(QString
)));
138 connect(m_filterBar
, SIGNAL(closeRequest()),
139 this, SLOT(closeFilterBar()));
140 connect(m_view
, SIGNAL(urlChanged(KUrl
)),
141 m_filterBar
, SLOT(clear()));
143 m_topLayout
->addWidget(m_urlNavigator
);
144 m_topLayout
->addWidget(m_searchBox
);
145 m_topLayout
->addWidget(m_view
);
146 m_topLayout
->addWidget(m_filterBar
);
147 m_topLayout
->addWidget(m_statusBar
);
149 setSearchModeEnabled(isSearchUrl(url
));
152 DolphinViewContainer::~DolphinViewContainer()
156 KUrl
DolphinViewContainer::url() const
158 return m_view
->url();
161 void DolphinViewContainer::setActive(bool active
)
163 m_urlNavigator
->setActive(active
);
164 m_view
->setActive(active
);
167 bool DolphinViewContainer::isActive() const
169 Q_ASSERT(m_view
->isActive() == m_urlNavigator
->isActive());
170 return m_view
->isActive();
173 const DolphinStatusBar
* DolphinViewContainer::statusBar() const
178 DolphinStatusBar
* DolphinViewContainer::statusBar()
183 const KUrlNavigator
* DolphinViewContainer::urlNavigator() const
185 return m_urlNavigator
;
188 KUrlNavigator
* DolphinViewContainer::urlNavigator()
190 return m_urlNavigator
;
193 const DolphinView
* DolphinViewContainer::view() const
198 DolphinView
* DolphinViewContainer::view()
203 const DolphinSearchBox
* DolphinViewContainer::searchBox() const
208 DolphinSearchBox
* DolphinViewContainer::searchBox()
213 void DolphinViewContainer::refresh()
215 if (GeneralSettings::modifiedStartupSettings()) {
216 // The startup settings should only get applied if they have been
217 // modified by the user. Otherwise keep the (possibly) different current
218 // settings of the URL navigator and the filterbar.
219 m_urlNavigator
->setUrlEditable(GeneralSettings::editableUrl());
220 m_urlNavigator
->setShowFullPath(GeneralSettings::showFullPath());
221 setFilterBarVisible(GeneralSettings::filterBar());
225 m_statusBar
->refresh();
228 bool DolphinViewContainer::isFilterBarVisible() const
230 return m_filterBar
->isVisible();
233 void DolphinViewContainer::setSearchModeEnabled(bool enabled
)
235 if (enabled
== isSearchModeEnabled()) {
236 if (enabled
&& !m_searchBox
->hasFocus()) {
237 m_searchBox
->setFocus();
238 m_searchBox
->selectAll();
243 m_searchBox
->setVisible(enabled
);
244 m_urlNavigator
->setVisible(!enabled
);
247 KUrl url
= m_urlNavigator
->locationUrl();
248 m_searchBox
->setText(QString());
249 m_searchBox
->setReadOnly(isSearchUrl(url
), url
);
251 // Remember the most recent non-search URL as search path
252 // of the search-box, so that it can be restored
253 // when switching back to the URL navigator.
254 int index
= m_urlNavigator
->historyIndex();
255 const int historySize
= m_urlNavigator
->historySize();
256 while (isSearchUrl(url
) && (index
< historySize
)) {
258 url
= m_urlNavigator
->locationUrl(index
);
261 if (!isSearchUrl(url
)) {
262 m_searchBox
->setSearchPath(url
);
265 // Restore the URL for the URL navigator. If Dolphin has been
266 // started with a search-URL, the home URL is used as fallback.
267 const KUrl url
= m_searchBox
->searchPath();
268 if (url
.isValid() && !url
.isEmpty()) {
269 if (isSearchUrl(url
)) {
270 m_urlNavigator
->goHome();
272 m_urlNavigator
->setLocationUrl(url
);
277 emit
searchModeChanged(enabled
);
280 bool DolphinViewContainer::isSearchModeEnabled() const
282 return m_searchBox
->isVisible();
285 void DolphinViewContainer::setUrl(const KUrl
& newUrl
)
287 if (newUrl
!= m_urlNavigator
->locationUrl()) {
288 m_urlNavigator
->setLocationUrl(newUrl
);
292 void DolphinViewContainer::setFilterBarVisible(bool visible
)
294 Q_ASSERT(m_filterBar
);
297 m_filterBar
->setFocus();
298 m_filterBar
->selectAll();
304 void DolphinViewContainer::delayedStatusBarUpdate()
306 if (m_statusBarTimer
->isActive() && (m_statusBarTimestamp
.elapsed() > 2000)) {
307 // No update of the statusbar has been done during the last 2 seconds,
308 // although an update has been requested. Trigger an immediate update.
309 m_statusBarTimer
->stop();
312 // Invoke updateStatusBar() with a small delay. This assures that
313 // when a lot of delayedStatusBarUpdates() are done in a short time,
314 // no bottleneck is given.
315 m_statusBarTimer
->start();
319 void DolphinViewContainer::updateStatusBar()
321 m_statusBarTimestamp
.start();
323 const QString newMessage
= m_view
->statusBarText();
324 m_statusBar
->setDefaultText(newMessage
);
326 // We don't want to override errors. Other messages are only protected by
327 // the Statusbar itself depending on timings (see DolphinStatusBar::setMessage).
328 if (m_statusBar
->type() != DolphinStatusBar::Error
) {
329 m_statusBar
->setMessage(newMessage
, DolphinStatusBar::Default
);
333 void DolphinViewContainer::updateProgress(int percent
)
335 if (m_statusBar
->progressText().isEmpty()) {
336 m_statusBar
->setProgressText(i18nc("@info:progress", "Loading folder..."));
338 m_statusBar
->setProgress(percent
);
341 void DolphinViewContainer::slotStartedPathLoading()
343 if (isSearchUrl(url())) {
344 // Search KIO-slaves usually don't provide any progress information. Give
345 // a hint to the user that a searching is done:
347 m_statusBar
->setProgressText(i18nc("@info", "Searching..."));
348 m_statusBar
->setProgress(-1);
350 // Trigger an undetermined progress indication. The progress
351 // information in percent will be triggered by the percent() signal
352 // of the directory lister later.
357 void DolphinViewContainer::slotFinishedPathLoading()
359 if (!m_statusBar
->progressText().isEmpty()) {
360 m_statusBar
->setProgressText(QString());
361 m_statusBar
->setProgress(100);
364 if (isSearchUrl(url()) && m_view
->items().isEmpty()) {
365 // The dir lister has been completed on a Nepomuk-URI and no items have been found. Instead
366 // of showing the default status bar information ("0 items") a more helpful information is given:
367 m_statusBar
->setMessage(i18nc("@info:status", "No items found."), DolphinStatusBar::Information
);
373 void DolphinViewContainer::slotItemActivated(const KFileItem
& item
)
375 KUrl url
= item
.targetUrl();
382 if (GeneralSettings::browseThroughArchives() && item
.isFile() && url
.isLocalFile()) {
383 // Generic mechanism for redirecting to tar:/<path>/ when clicking on a tar file,
384 // zip:/<path>/ when clicking on a zip file, etc.
385 // The .protocol file specifies the mimetype that the kioslave handles.
386 // Note that we don't use mimetype inheritance since we don't want to
387 // open OpenDocument files as zip folders...
388 const QString protocol
= KProtocolManager::protocolForArchiveMimetype(item
.mimetype());
389 if (!protocol
.isEmpty()) {
390 url
.setProtocol(protocol
);
396 if (item
.mimetype() == QLatin1String("application/x-desktop")) {
397 // Redirect to the URL in Type=Link desktop files
398 KDesktopFile
desktopFile(url
.toLocalFile());
399 if (desktopFile
.hasLinkType()) {
400 url
= desktopFile
.readUrl();
409 void DolphinViewContainer::openFile(const KUrl
& url
)
411 const KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, url
);
412 slotItemActivated(item
);
415 void DolphinViewContainer::showItemInfo(const KFileItem
& item
)
418 // Only clear the status bar if unimportant messages are shown.
419 // This prevents that information- or error-messages get hidden
420 // by moving the mouse above the viewport or when closing the
422 if (m_statusBar
->type() == DolphinStatusBar::Default
) {
423 m_statusBar
->clear();
428 message
= item
.name();
430 message
= i18nc("@info:status filename (type)", "%1 (%2)", item
.name(), item
.mimeComment());
432 m_statusBar
->setMessage(message
, DolphinStatusBar::Default
);
436 void DolphinViewContainer::showInfoMessage(const QString
& msg
)
438 m_statusBar
->setMessage(msg
, DolphinStatusBar::Information
);
441 void DolphinViewContainer::showErrorMessage(const QString
& msg
)
443 m_statusBar
->setMessage(msg
, DolphinStatusBar::Error
);
446 void DolphinViewContainer::showOperationCompletedMessage(const QString
& msg
)
448 m_statusBar
->setMessage(msg
, DolphinStatusBar::OperationCompleted
);
451 void DolphinViewContainer::closeFilterBar()
454 m_filterBar
->clear();
456 emit
showFilterBarChanged(false);
459 void DolphinViewContainer::setNameFilter(const QString
& nameFilter
)
461 m_view
->setNameFilter(nameFilter
);
462 delayedStatusBarUpdate();
465 void DolphinViewContainer::activate()
470 void DolphinViewContainer::slotViewUrlAboutToBeChanged(const KUrl
& url
)
472 // URL changes of the view can happen in two ways:
473 // 1. The URL navigator gets changed and will trigger the view to update its URL
474 // 2. The URL of the view gets changed and will trigger the URL navigator to update
475 // its URL (e.g. by clicking on an item)
476 // In this scope the view-state may only get saved in case 2:
477 if (url
!= m_urlNavigator
->locationUrl()) {
482 void DolphinViewContainer::slotUrlNavigatorLocationAboutToBeChanged(const KUrl
& url
)
484 // URL changes of the view can happen in two ways:
485 // 1. The URL navigator gets changed and will trigger the view to update its URL
486 // 2. The URL of the view gets changed and will trigger the URL navigator to update
487 // its URL (e.g. by clicking on an item)
488 // In this scope the view-state may only get saved in case 1:
489 if (url
!= m_view
->url()) {
494 void DolphinViewContainer::slotUrlNavigatorLocationChanged(const KUrl
& url
)
496 if (KProtocolManager::supportsListing(url
)) {
497 setSearchModeEnabled(isSearchUrl(url
));
500 if (isActive() && !isSearchUrl(url
)) {
501 // When an URL has been entered, the view should get the focus.
502 // The focus must be requested asynchronously, as changing the URL might create
503 // a new view widget.
504 QTimer::singleShot(0, this, SLOT(requestFocus()));
506 } else if (KProtocolManager::isSourceProtocol(url
)) {
507 QString app
= "konqueror";
508 if (url
.protocol().startsWith(QLatin1String("http"))) {
509 showErrorMessage(i18nc("@info:status",
510 "Dolphin does not support web pages, the web browser has been launched"));
511 const KConfigGroup
config(KSharedConfig::openConfig("kdeglobals"), "General");
512 const QString browser
= config
.readEntry("BrowserApplication");
513 if (!browser
.isEmpty()) {
515 if (app
.startsWith('!')) {
516 // a literal command has been configured, remove the '!' prefix
521 showErrorMessage(i18nc("@info:status",
522 "Protocol not supported by Dolphin, Konqueror has been launched"));
525 const QString secureUrl
= KShell::quoteArg(url
.pathOrUrl());
526 const QString command
= app
+ ' ' + secureUrl
;
527 KRun::runCommand(command
, app
, app
, this);
529 showErrorMessage(i18nc("@info:status", "Invalid protocol"));
533 void DolphinViewContainer::dropUrls(const KUrl
& destination
, QDropEvent
* event
)
535 Q_UNUSED(destination
);
537 //DragAndDropHelper::instance().dropUrls(KFileItem(), destination, event, this);
540 void DolphinViewContainer::redirect(const KUrl
& oldUrl
, const KUrl
& newUrl
)
543 const bool block
= m_urlNavigator
->signalsBlocked();
544 m_urlNavigator
->blockSignals(true);
546 // Assure that the location state is reset for redirection URLs. This
547 // allows to skip redirection URLs when going back or forward in the
549 m_urlNavigator
->saveLocationState(QByteArray());
550 m_urlNavigator
->setLocationUrl(newUrl
);
551 setSearchModeEnabled(isSearchUrl(newUrl
));
553 m_urlNavigator
->blockSignals(block
);
556 void DolphinViewContainer::requestFocus()
561 void DolphinViewContainer::saveUrlCompletionMode(KGlobalSettings::Completion completion
)
563 GeneralSettings::setUrlCompletionMode(completion
);
566 void DolphinViewContainer::slotHistoryChanged()
568 QByteArray locationState
= m_urlNavigator
->locationState();
569 if (!locationState
.isEmpty()) {
570 QDataStream
stream(&locationState
, QIODevice::ReadOnly
);
571 m_view
->restoreState(stream
);
575 void DolphinViewContainer::startSearching(const QString
&text
)
578 const KUrl url
= m_searchBox
->urlForSearching();
579 if (url
.isValid() && !url
.isEmpty()) {
580 m_urlNavigator
->setLocationUrl(url
);
584 void DolphinViewContainer::closeSearchBox()
586 setSearchModeEnabled(false);
589 void DolphinViewContainer::stopLoading()
591 m_view
->stopLoading();
592 m_statusBar
->setProgress(100);
595 bool DolphinViewContainer::isSearchUrl(const KUrl
& url
) const
597 const QString protocol
= url
.protocol();
598 return protocol
.contains("search") || (protocol
== QLatin1String("nepomuk"));
601 void DolphinViewContainer::saveViewState()
603 QByteArray locationState
;
604 QDataStream
stream(&locationState
, QIODevice::WriteOnly
);
605 m_view
->saveState(stream
);
606 m_urlNavigator
->saveLocationState(locationState
);
609 #include "dolphinviewcontainer.moc"