]>
cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinviewcontainer.cpp
7b9152d1e54434af7223908562d36232f5877921
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/draganddrophelper.h"
53 #include "views/viewmodecontroller.h"
54 #include "views/viewproperties.h"
56 DolphinViewContainer::DolphinViewContainer(const KUrl
& url
, QWidget
* parent
) :
65 m_statusBarTimestamp()
69 m_topLayout
= new QVBoxLayout(this);
70 m_topLayout
->setSpacing(0);
71 m_topLayout
->setMargin(0);
73 m_urlNavigator
= new KUrlNavigator(DolphinPlacesModel::instance(), url
, this);
74 connect(m_urlNavigator
, SIGNAL(urlsDropped(KUrl
,QDropEvent
*)),
75 this, SLOT(dropUrls(KUrl
,QDropEvent
*)));
76 connect(m_urlNavigator
, SIGNAL(activated()),
77 this, SLOT(activate()));
78 connect(m_urlNavigator
->editor(), SIGNAL(completionModeChanged(KGlobalSettings::Completion
)),
79 this, SLOT(saveUrlCompletionMode(KGlobalSettings::Completion
)));
81 const GeneralSettings
* settings
= GeneralSettings::self();
82 m_urlNavigator
->setUrlEditable(settings
->editableUrl());
83 m_urlNavigator
->setShowFullPath(settings
->showFullPath());
84 m_urlNavigator
->setHomeUrl(KUrl(settings
->homeUrl()));
85 KUrlComboBox
* editor
= m_urlNavigator
->editor();
86 editor
->setCompletionMode(KGlobalSettings::Completion(settings
->urlCompletionMode()));
88 m_searchBox
= new DolphinSearchBox(this);
90 connect(m_searchBox
, SIGNAL(closeRequest()), this, SLOT(closeSearchBox()));
91 connect(m_searchBox
, SIGNAL(search(QString
)), this, SLOT(startSearching(QString
)));
92 connect(m_searchBox
, SIGNAL(returnPressed(QString
)), this, SLOT(requestFocus()));
94 m_view
= new DolphinView(url
, this);
95 connect(m_view
, SIGNAL(urlChanged(KUrl
)), m_urlNavigator
, SLOT(setUrl(KUrl
)));
96 connect(m_view
, SIGNAL(writeStateChanged(bool)), this, SIGNAL(writeStateChanged(bool)));
97 connect(m_view
, SIGNAL(requestItemInfo(KFileItem
)), this, SLOT(showItemInfo(KFileItem
)));
98 connect(m_view
, SIGNAL(errorMessage(QString
)), this, SLOT(showErrorMessage(QString
)));
99 connect(m_view
, SIGNAL(infoMessage(QString
)), this, SLOT(showInfoMessage(QString
)));
100 connect(m_view
, SIGNAL(itemActivated(KFileItem
)), this, SLOT(slotItemActivated(KFileItem
)));
101 connect(m_view
, SIGNAL(redirection(KUrl
,KUrl
)), this, SLOT(redirect(KUrl
,KUrl
)));
102 connect(m_view
, SIGNAL(dirLoadingStarted()), this, SLOT(slotDirLoadingStarted()));
103 connect(m_view
, SIGNAL(dirLoadingCompleted()), this, SLOT(slotDirLoadingCompleted()));
104 connect(m_view
, SIGNAL(itemCountChanged()), this, SLOT(delayedStatusBarUpdate()));
105 connect(m_view
, SIGNAL(dirLoadingProgress(int)), this, SLOT(updateDirLoadingProgress(int)));
106 connect(m_view
, SIGNAL(dirSortingProgress(int)), this, SLOT(updateSortingProgress(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(selectionChanged(KFileItemList
)), this, SLOT(delayedStatusBarUpdate()));
110 connect(m_view
, SIGNAL(operationCompletedMessage(QString
)), this, SLOT(showOperationCompletedMessage(QString
)));
111 connect(m_view
, SIGNAL(urlAboutToBeChanged(KUrl
)), this, SLOT(slotViewUrlAboutToBeChanged(KUrl
)));
113 connect(m_urlNavigator
, SIGNAL(urlAboutToBeChanged(KUrl
)),
114 this, SLOT(slotUrlNavigatorLocationAboutToBeChanged(KUrl
)));
115 connect(m_urlNavigator
, SIGNAL(urlChanged(KUrl
)),
116 this, SLOT(slotUrlNavigatorLocationChanged(KUrl
)));
117 connect(m_urlNavigator
, SIGNAL(historyChanged()),
118 this, SLOT(slotHistoryChanged()));
120 // initialize status bar
121 m_statusBar
= new DolphinStatusBar(this, m_view
);
122 connect(m_statusBar
, SIGNAL(stopPressed()), this, SLOT(stopLoading()));
124 m_statusBarTimer
= new QTimer(this);
125 m_statusBarTimer
->setSingleShot(true);
126 m_statusBarTimer
->setInterval(300);
127 connect(m_statusBarTimer
, SIGNAL(timeout()),
128 this, SLOT(updateStatusBar()));
130 KIO::FileUndoManager
* undoManager
= KIO::FileUndoManager::self();
131 connect(undoManager
, SIGNAL(jobRecordingFinished(CommandType
)),
132 this, SLOT(delayedStatusBarUpdate()));
134 // initialize filter bar
135 m_filterBar
= new FilterBar(this);
136 m_filterBar
->setVisible(settings
->filterBar());
137 connect(m_filterBar
, SIGNAL(filterChanged(QString
)),
138 this, SLOT(setNameFilter(QString
)));
139 connect(m_filterBar
, SIGNAL(closeRequest()),
140 this, SLOT(closeFilterBar()));
141 connect(m_view
, SIGNAL(urlChanged(KUrl
)),
142 m_filterBar
, SLOT(clear()));
144 m_topLayout
->addWidget(m_urlNavigator
);
145 m_topLayout
->addWidget(m_searchBox
);
146 m_topLayout
->addWidget(m_view
);
147 m_topLayout
->addWidget(m_filterBar
);
148 m_topLayout
->addWidget(m_statusBar
);
150 setSearchModeEnabled(isSearchUrl(url
));
153 DolphinViewContainer::~DolphinViewContainer()
157 KUrl
DolphinViewContainer::url() const
159 return m_view
->url();
162 void DolphinViewContainer::setActive(bool active
)
164 m_urlNavigator
->setActive(active
);
165 m_view
->setActive(active
);
168 bool DolphinViewContainer::isActive() const
170 Q_ASSERT(m_view
->isActive() == m_urlNavigator
->isActive());
171 return m_view
->isActive();
174 const DolphinStatusBar
* DolphinViewContainer::statusBar() const
179 DolphinStatusBar
* DolphinViewContainer::statusBar()
184 const KUrlNavigator
* DolphinViewContainer::urlNavigator() const
186 return m_urlNavigator
;
189 KUrlNavigator
* DolphinViewContainer::urlNavigator()
191 return m_urlNavigator
;
194 const DolphinView
* DolphinViewContainer::view() const
199 DolphinView
* DolphinViewContainer::view()
204 const DolphinSearchBox
* DolphinViewContainer::searchBox() const
209 DolphinSearchBox
* DolphinViewContainer::searchBox()
214 void DolphinViewContainer::readSettings()
216 if (GeneralSettings::modifiedStartupSettings()) {
217 // The startup settings should only get applied if they have been
218 // modified by the user. Otherwise keep the (possibly) different current
219 // settings of the URL navigator and the filterbar.
220 m_urlNavigator
->setUrlEditable(GeneralSettings::editableUrl());
221 m_urlNavigator
->setShowFullPath(GeneralSettings::showFullPath());
222 m_urlNavigator
->setHomeUrl(KUrl(GeneralSettings::homeUrl()));
223 setFilterBarVisible(GeneralSettings::filterBar());
226 m_view
->readSettings();
227 m_statusBar
->readSettings();
230 bool DolphinViewContainer::isFilterBarVisible() const
232 return m_filterBar
->isVisible();
235 void DolphinViewContainer::setSearchModeEnabled(bool enabled
)
237 if (enabled
== isSearchModeEnabled()) {
238 if (enabled
&& !m_searchBox
->hasFocus()) {
239 m_searchBox
->setFocus();
240 m_searchBox
->selectAll();
245 m_searchBox
->setVisible(enabled
);
246 m_urlNavigator
->setVisible(!enabled
);
249 KUrl url
= m_urlNavigator
->locationUrl();
250 m_searchBox
->setText(QString());
251 m_searchBox
->setReadOnly(isSearchUrl(url
), url
);
253 // Remember the most recent non-search URL as search path
254 // of the search-box, so that it can be restored
255 // when switching back to the URL navigator.
256 int index
= m_urlNavigator
->historyIndex();
257 const int historySize
= m_urlNavigator
->historySize();
258 while (isSearchUrl(url
) && (index
< historySize
)) {
260 url
= m_urlNavigator
->locationUrl(index
);
263 if (!isSearchUrl(url
)) {
264 m_searchBox
->setSearchPath(url
);
267 // Restore the URL for the URL navigator. If Dolphin has been
268 // started with a search-URL, the home URL is used as fallback.
269 const KUrl url
= m_searchBox
->searchPath();
270 if (url
.isValid() && !url
.isEmpty()) {
271 if (isSearchUrl(url
)) {
272 m_urlNavigator
->goHome();
274 m_urlNavigator
->setLocationUrl(url
);
279 emit
searchModeChanged(enabled
);
282 bool DolphinViewContainer::isSearchModeEnabled() const
284 return m_searchBox
->isVisible();
287 void DolphinViewContainer::setUrl(const KUrl
& newUrl
)
289 if (newUrl
!= m_urlNavigator
->locationUrl()) {
290 m_urlNavigator
->setLocationUrl(newUrl
);
294 void DolphinViewContainer::setFilterBarVisible(bool visible
)
296 Q_ASSERT(m_filterBar
);
299 m_filterBar
->setFocus();
300 m_filterBar
->selectAll();
306 void DolphinViewContainer::delayedStatusBarUpdate()
308 if (m_statusBarTimer
->isActive() && (m_statusBarTimestamp
.elapsed() > 2000)) {
309 // No update of the statusbar has been done during the last 2 seconds,
310 // although an update has been requested. Trigger an immediate update.
311 m_statusBarTimer
->stop();
314 // Invoke updateStatusBar() with a small delay. This assures that
315 // when a lot of delayedStatusBarUpdates() are done in a short time,
316 // no bottleneck is given.
317 m_statusBarTimer
->start();
321 void DolphinViewContainer::updateStatusBar()
323 m_statusBarTimestamp
.start();
325 const QString newMessage
= m_view
->statusBarText();
326 m_statusBar
->setDefaultText(newMessage
);
328 // We don't want to override errors. Other messages are only protected by
329 // the Statusbar itself depending on timings (see DolphinStatusBar::setMessage).
330 if (m_statusBar
->type() != DolphinStatusBar::Error
) {
331 m_statusBar
->setMessage(newMessage
, DolphinStatusBar::Default
);
335 void DolphinViewContainer::updateDirLoadingProgress(int percent
)
337 if (m_statusBar
->progressText().isEmpty()) {
338 m_statusBar
->setProgressText(i18nc("@info:progress", "Loading folder..."));
340 m_statusBar
->setProgress(percent
);
343 void DolphinViewContainer::updateSortingProgress(int percent
)
345 if (m_statusBar
->progressText().isEmpty()) {
346 m_statusBar
->setProgressText(i18nc("@info:progress", "Sorting..."));
348 m_statusBar
->setProgress(percent
);
351 void DolphinViewContainer::slotDirLoadingStarted()
353 if (isSearchUrl(url())) {
354 // Search KIO-slaves usually don't provide any progress information. Give
355 // a hint to the user that a searching is done:
357 m_statusBar
->setProgressText(i18nc("@info", "Searching..."));
358 m_statusBar
->setProgress(-1);
360 // Trigger an undetermined progress indication. The progress
361 // information in percent will be triggered by the percent() signal
362 // of the directory lister later.
363 updateDirLoadingProgress(-1);
367 void DolphinViewContainer::slotDirLoadingCompleted()
369 if (!m_statusBar
->progressText().isEmpty()) {
370 m_statusBar
->setProgressText(QString());
371 m_statusBar
->setProgress(100);
374 if (isSearchUrl(url()) && m_view
->itemsCount() == 0) {
375 // The dir lister has been completed on a Nepomuk-URI and no items have been found. Instead
376 // of showing the default status bar information ("0 items") a more helpful information is given:
377 m_statusBar
->setMessage(i18nc("@info:status", "No items found."), DolphinStatusBar::Information
);
383 void DolphinViewContainer::slotItemActivated(const KFileItem
& item
)
385 // It is possible to activate items on inactive views by
386 // drag & drop operations. Assure that activating an item always
387 // results in an active view.
388 m_view
->setActive(true);
390 KUrl url
= item
.targetUrl();
397 if (GeneralSettings::browseThroughArchives() && item
.isFile() && url
.isLocalFile()) {
398 // Generic mechanism for redirecting to tar:/<path>/ when clicking on a tar file,
399 // zip:/<path>/ when clicking on a zip file, etc.
400 // The .protocol file specifies the mimetype that the kioslave handles.
401 // Note that we don't use mimetype inheritance since we don't want to
402 // open OpenDocument files as zip folders...
403 const QString protocol
= KProtocolManager::protocolForArchiveMimetype(item
.mimetype());
404 if (!protocol
.isEmpty()) {
405 url
.setProtocol(protocol
);
411 if (item
.mimetype() == QLatin1String("application/x-desktop")) {
412 // Redirect to the URL in Type=Link desktop files
413 KDesktopFile
desktopFile(url
.toLocalFile());
414 if (desktopFile
.hasLinkType()) {
415 url
= desktopFile
.readUrl();
424 void DolphinViewContainer::showItemInfo(const KFileItem
& item
)
427 // Only clear the status bar if unimportant messages are shown.
428 // This prevents that information- or error-messages get hidden
429 // by moving the mouse above the viewport or when closing the
431 if (m_statusBar
->type() == DolphinStatusBar::Default
) {
432 m_statusBar
->clear();
435 const QString message
= item
.isDir() ? item
.text() : item
.getStatusBarInfo();
436 m_statusBar
->setMessage(message
, DolphinStatusBar::Default
);
440 void DolphinViewContainer::showInfoMessage(const QString
& msg
)
442 m_statusBar
->setMessage(msg
, DolphinStatusBar::Information
);
445 void DolphinViewContainer::showErrorMessage(const QString
& msg
)
447 m_statusBar
->setMessage(msg
, DolphinStatusBar::Error
);
450 void DolphinViewContainer::showOperationCompletedMessage(const QString
& msg
)
452 m_statusBar
->setMessage(msg
, DolphinStatusBar::OperationCompleted
);
455 void DolphinViewContainer::closeFilterBar()
458 m_filterBar
->clear();
460 emit
showFilterBarChanged(false);
463 void DolphinViewContainer::setNameFilter(const QString
& nameFilter
)
465 m_view
->setNameFilter(nameFilter
);
466 delayedStatusBarUpdate();
469 void DolphinViewContainer::activate()
474 void DolphinViewContainer::slotViewUrlAboutToBeChanged(const KUrl
& url
)
476 // URL changes of the view can happen in two ways:
477 // 1. The URL navigator gets changed and will trigger the view to update its URL
478 // 2. The URL of the view gets changed and will trigger the URL navigator to update
479 // its URL (e.g. by clicking on an item)
480 // In this scope the view-state may only get saved in case 2:
481 if (url
!= m_urlNavigator
->locationUrl()) {
486 void DolphinViewContainer::slotUrlNavigatorLocationAboutToBeChanged(const KUrl
& url
)
488 // URL changes of the view can happen in two ways:
489 // 1. The URL navigator gets changed and will trigger the view to update its URL
490 // 2. The URL of the view gets changed and will trigger the URL navigator to update
491 // its URL (e.g. by clicking on an item)
492 // In this scope the view-state may only get saved in case 1:
493 if (url
!= m_view
->url()) {
498 void DolphinViewContainer::slotUrlNavigatorLocationChanged(const KUrl
& url
)
500 if (KProtocolManager::supportsListing(url
)) {
501 setSearchModeEnabled(isSearchUrl(url
));
504 if (isActive() && !isSearchUrl(url
)) {
505 // When an URL has been entered, the view should get the focus.
506 // The focus must be requested asynchronously, as changing the URL might create
507 // a new view widget.
508 QTimer::singleShot(0, this, SLOT(requestFocus()));
510 } else if (KProtocolManager::isSourceProtocol(url
)) {
511 QString app
= "konqueror";
512 if (url
.protocol().startsWith(QLatin1String("http"))) {
513 showErrorMessage(i18nc("@info:status",
514 "Dolphin does not support web pages, the web browser has been launched"));
515 const KConfigGroup
config(KSharedConfig::openConfig("kdeglobals"), "General");
516 const QString browser
= config
.readEntry("BrowserApplication");
517 if (!browser
.isEmpty()) {
519 if (app
.startsWith('!')) {
520 // a literal command has been configured, remove the '!' prefix
525 showErrorMessage(i18nc("@info:status",
526 "Protocol not supported by Dolphin, Konqueror has been launched"));
529 const QString secureUrl
= KShell::quoteArg(url
.pathOrUrl());
530 const QString command
= app
+ ' ' + secureUrl
;
531 KRun::runCommand(command
, app
, app
, this);
533 showErrorMessage(i18nc("@info:status", "Invalid protocol"));
537 void DolphinViewContainer::dropUrls(const KUrl
& destination
, QDropEvent
* event
)
539 DragAndDropHelper::dropUrls(KFileItem(), destination
, event
);
542 void DolphinViewContainer::redirect(const KUrl
& oldUrl
, const KUrl
& newUrl
)
545 const bool block
= m_urlNavigator
->signalsBlocked();
546 m_urlNavigator
->blockSignals(true);
548 // Assure that the location state is reset for redirection URLs. This
549 // allows to skip redirection URLs when going back or forward in the
551 m_urlNavigator
->saveLocationState(QByteArray());
552 m_urlNavigator
->setLocationUrl(newUrl
);
553 setSearchModeEnabled(isSearchUrl(newUrl
));
555 m_urlNavigator
->blockSignals(block
);
558 void DolphinViewContainer::requestFocus()
563 void DolphinViewContainer::saveUrlCompletionMode(KGlobalSettings::Completion completion
)
565 GeneralSettings::setUrlCompletionMode(completion
);
568 void DolphinViewContainer::slotHistoryChanged()
570 QByteArray locationState
= m_urlNavigator
->locationState();
571 if (!locationState
.isEmpty()) {
572 QDataStream
stream(&locationState
, QIODevice::ReadOnly
);
573 m_view
->restoreState(stream
);
577 void DolphinViewContainer::startSearching(const QString
&text
)
580 const KUrl url
= m_searchBox
->urlForSearching();
581 if (url
.isValid() && !url
.isEmpty()) {
582 m_urlNavigator
->setLocationUrl(url
);
586 void DolphinViewContainer::closeSearchBox()
588 setSearchModeEnabled(false);
591 void DolphinViewContainer::stopLoading()
593 m_view
->stopLoading();
594 m_statusBar
->setProgress(100);
597 bool DolphinViewContainer::isSearchUrl(const KUrl
& url
) const
599 const QString protocol
= url
.protocol();
600 return protocol
.contains("search") || (protocol
== QLatin1String("nepomuk"));
603 void DolphinViewContainer::saveViewState()
605 QByteArray locationState
;
606 QDataStream
stream(&locationState
, QIODevice::WriteOnly
);
607 m_view
->saveState(stream
);
608 m_urlNavigator
->saveLocationState(locationState
);
611 #include "dolphinviewcontainer.moc"