]>
cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinviewcontainer.cpp
1 /***************************************************************************
2 * Copyright (C) 2007 by Peter Penz <peter.penz@gmx.at> *
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.h>
23 #include <QtGui/QApplication>
24 #include <QtGui/QKeyEvent>
25 #include <QtGui/QItemSelection>
26 #include <QtGui/QBoxLayout>
27 #include <QtCore/QTimer>
28 #include <QtGui/QScrollBar>
30 #include <kdesktopfile.h>
31 #include <kfileitemdelegate.h>
32 #include <kfileplacesmodel.h>
33 #include <kglobalsettings.h>
35 #include <kiconeffect.h>
36 #include <kio/netaccess.h>
37 #include <kio/previewjob.h>
39 #include <kmimetyperesolver.h>
41 #include <konqmimedata.h>
42 #include <kfileitemlistproperties.h>
43 #include <konq_operations.h>
46 #include <kurlcombobox.h>
47 #include <kurlnavigator.h>
50 #include "dolphin_generalsettings.h"
51 #include "dolphinmainwindow.h"
52 #include "filterbar/filterbar.h"
53 #include "search/dolphinsearchbox.h"
54 #include "settings/dolphinsettings.h"
55 #include "statusbar/dolphinstatusbar.h"
56 #include "views/dolphincolumnview.h"
57 #include "views/dolphindetailsview.h"
58 #include "views/dolphindirlister.h"
59 #include "views/dolphinsortfilterproxymodel.h"
60 #include "views/draganddrophelper.h"
61 #include "views/dolphiniconsview.h"
62 #include "views/dolphinmodel.h"
63 #include "views/dolphinviewcontroller.h"
64 #include "views/viewmodecontroller.h"
65 #include "views/viewproperties.h"
67 DolphinViewContainer::DolphinViewContainer(const KUrl
& url
, QWidget
* parent
) :
69 m_isFolderWritable(false),
77 m_statusBarTimestamp(),
83 m_topLayout
= new QVBoxLayout(this);
84 m_topLayout
->setSpacing(0);
85 m_topLayout
->setMargin(0);
87 m_urlNavigator
= new KUrlNavigator(DolphinSettings::instance().placesModel(), url
, this);
88 connect(m_urlNavigator
, SIGNAL(urlsDropped(const KUrl
&, QDropEvent
*)),
89 this, SLOT(dropUrls(const KUrl
&, QDropEvent
*)));
90 connect(m_urlNavigator
, SIGNAL(activated()),
91 this, SLOT(activate()));
92 connect(m_urlNavigator
->editor(), SIGNAL(completionModeChanged(KGlobalSettings::Completion
)),
93 this, SLOT(saveUrlCompletionMode(KGlobalSettings::Completion
)));
95 const GeneralSettings
* settings
= DolphinSettings::instance().generalSettings();
96 m_urlNavigator
->setUrlEditable(settings
->editableUrl());
97 m_urlNavigator
->setShowFullPath(settings
->showFullPath());
98 m_urlNavigator
->setHomeUrl(KUrl(settings
->homeUrl()));
99 KUrlComboBox
* editor
= m_urlNavigator
->editor();
100 editor
->setCompletionMode(KGlobalSettings::Completion(settings
->urlCompletionMode()));
102 m_searchBox
= new DolphinSearchBox(this);
104 connect(m_searchBox
, SIGNAL(closeRequest()), this, SLOT(closeSearchBox()));
105 connect(m_searchBox
, SIGNAL(search(QString
)), this, SLOT(startSearching(QString
)));
106 connect(m_searchBox
, SIGNAL(returnPressed(QString
)), this, SLOT(requestFocus()));
108 m_dirLister
= new DolphinDirLister();
109 m_dirLister
->setAutoUpdate(true);
110 m_dirLister
->setMainWindow(window());
111 m_dirLister
->setDelayedMimeTypes(true);
113 m_dolphinModel
= new DolphinModel(this);
114 m_dolphinModel
->setDirLister(m_dirLister
); // m_dolphinModel takes ownership of m_dirLister
115 m_dolphinModel
->setDropsAllowed(DolphinModel::DropOnDirectory
);
117 m_proxyModel
= new DolphinSortFilterProxyModel(this);
118 m_proxyModel
->setSourceModel(m_dolphinModel
);
119 m_proxyModel
->setFilterCaseSensitivity(Qt::CaseInsensitive
);
121 connect(m_dirLister
, SIGNAL(started(KUrl
)),
122 this, SLOT(initializeProgress()));
123 connect(m_dirLister
, SIGNAL(clear()),
124 this, SLOT(delayedStatusBarUpdate()));
125 connect(m_dirLister
, SIGNAL(percent(int)),
126 this, SLOT(updateProgress(int)));
127 connect(m_dirLister
, SIGNAL(itemsDeleted(const KFileItemList
&)),
128 this, SLOT(delayedStatusBarUpdate()));
129 connect(m_dirLister
, SIGNAL(newItems(KFileItemList
)),
130 this, SLOT(delayedStatusBarUpdate()));
131 connect(m_dirLister
, SIGNAL(completed()),
132 this, SLOT(slotDirListerCompleted()));
133 connect(m_dirLister
, SIGNAL(infoMessage(const QString
&)),
134 this, SLOT(showInfoMessage(const QString
&)));
135 connect(m_dirLister
, SIGNAL(errorMessage(const QString
&)),
136 this, SLOT(showErrorMessage(const QString
&)));
137 connect(m_dirLister
, SIGNAL(urlIsFileError(const KUrl
&)),
138 this, SLOT(openFile(const KUrl
&)));
140 m_view
= new DolphinView(this, url
, m_proxyModel
);
141 connect(m_view
, SIGNAL(urlChanged(const KUrl
&)),
142 m_urlNavigator
, SLOT(setUrl(const KUrl
&)));
143 connect(m_view
, SIGNAL(requestItemInfo(KFileItem
)),
144 this, SLOT(showItemInfo(KFileItem
)));
145 connect(m_view
, SIGNAL(errorMessage(const QString
&)),
146 this, SLOT(showErrorMessage(const QString
&)));
147 connect(m_view
, SIGNAL(infoMessage(const QString
&)),
148 this, SLOT(showInfoMessage(const QString
&)));
149 connect(m_view
, SIGNAL(operationCompletedMessage(const QString
&)),
150 this, SLOT(showOperationCompletedMessage(const QString
&)));
151 connect(m_view
, SIGNAL(itemTriggered(KFileItem
)),
152 this, SLOT(slotItemTriggered(KFileItem
)));
153 connect(m_view
, SIGNAL(redirection(KUrl
, KUrl
)),
154 this, SLOT(redirect(KUrl
, KUrl
)));
155 connect(m_view
, SIGNAL(selectionChanged(const KFileItemList
&)),
156 this, SLOT(delayedStatusBarUpdate()));
158 connect(m_urlNavigator
, SIGNAL(urlChanged(const KUrl
&)),
159 this, SLOT(slotUrlNavigatorLocationChanged(const KUrl
&)));
160 connect(m_urlNavigator
, SIGNAL(urlAboutToBeChanged(const KUrl
&)),
161 this, SLOT(saveViewState()));
162 connect(m_urlNavigator
, SIGNAL(historyChanged()),
163 this, SLOT(slotHistoryChanged()));
165 // initialize status bar
166 m_statusBar
= new DolphinStatusBar(this, m_view
);
167 connect(m_statusBar
, SIGNAL(stopPressed()), this, SLOT(stopLoading()));
169 m_statusBarTimer
= new QTimer(this);
170 m_statusBarTimer
->setSingleShot(true);
171 m_statusBarTimer
->setInterval(300);
172 connect(m_statusBarTimer
, SIGNAL(timeout()),
173 this, SLOT(updateStatusBar()));
175 KIO::FileUndoManager
* undoManager
= KIO::FileUndoManager::self();
176 connect(undoManager
, SIGNAL(jobRecordingFinished(CommandType
)),
177 this, SLOT(delayedStatusBarUpdate()));
179 // initialize filter bar
180 m_filterBar
= new FilterBar(this);
181 m_filterBar
->setVisible(settings
->filterBar());
182 connect(m_filterBar
, SIGNAL(filterChanged(const QString
&)),
183 this, SLOT(setNameFilter(const QString
&)));
184 connect(m_filterBar
, SIGNAL(closeRequest()),
185 this, SLOT(closeFilterBar()));
186 connect(m_view
, SIGNAL(urlChanged(const KUrl
&)),
187 m_filterBar
, SLOT(clear()));
189 m_topLayout
->addWidget(m_urlNavigator
);
190 m_topLayout
->addWidget(m_searchBox
);
191 m_topLayout
->addWidget(m_view
);
192 m_topLayout
->addWidget(m_filterBar
);
193 m_topLayout
->addWidget(m_statusBar
);
195 setSearchModeEnabled(isSearchUrl(url
));
198 DolphinViewContainer::~DolphinViewContainer()
202 KUrl
DolphinViewContainer::url() const
204 return m_urlNavigator
->locationUrl();
207 void DolphinViewContainer::setActive(bool active
)
209 m_urlNavigator
->setActive(active
);
210 m_view
->setActive(active
);
212 emit
writeStateChanged(m_isFolderWritable
);
216 bool DolphinViewContainer::isActive() const
218 Q_ASSERT(m_view
->isActive() == m_urlNavigator
->isActive());
219 return m_view
->isActive();
222 void DolphinViewContainer::refresh()
224 GeneralSettings
* settings
= DolphinSettings::instance().generalSettings();
225 if (settings
->modifiedStartupSettings()) {
226 // The startup settings should only get applied if they have been
227 // modified by the user. Otherwise keep the (possibly) different current
228 // settings of the URL navigator and the filterbar.
229 m_urlNavigator
->setUrlEditable(settings
->editableUrl());
230 m_urlNavigator
->setShowFullPath(settings
->showFullPath());
231 setFilterBarVisible(settings
->filterBar());
235 m_statusBar
->refresh();
238 bool DolphinViewContainer::isFilterBarVisible() const
240 return m_filterBar
->isVisible();
243 void DolphinViewContainer::setSearchModeEnabled(bool enabled
)
245 if (enabled
== isSearchModeEnabled()) {
249 m_searchBox
->setVisible(enabled
);
250 m_urlNavigator
->setVisible(!enabled
);
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 KUrl url
= m_urlNavigator
->locationUrl();
258 int index
= m_urlNavigator
->historyIndex();
259 const int historySize
= m_urlNavigator
->historySize();
260 while (isSearchUrl(url
) && (index
< historySize
)) {
262 url
= m_urlNavigator
->locationUrl(index
);
265 if (!isSearchUrl(url
)) {
266 m_searchBox
->setSearchPath(url
);
269 // Restore the URL for the URL navigator. If Dolphin has been
270 // started with a search-URL, the home URL is used as fallback.
271 const KUrl url
= m_searchBox
->searchPath();
272 if (url
.isValid() && !url
.isEmpty()) {
273 if (isSearchUrl(url
)) {
274 m_urlNavigator
->goHome();
276 m_urlNavigator
->setLocationUrl(url
);
281 emit
searchModeChanged(enabled
);
284 bool DolphinViewContainer::isSearchModeEnabled() const
286 return m_searchBox
->isVisible();
289 void DolphinViewContainer::setUrl(const KUrl
& newUrl
)
291 if (newUrl
!= m_urlNavigator
->locationUrl()) {
292 m_urlNavigator
->setLocationUrl(newUrl
);
293 // Temporary disable the 'File'->'Create New...' menu until
294 // the write permissions can be checked in a fast way at
295 // DolphinViewContainer::slotDirListerCompleted().
296 m_isFolderWritable
= false;
298 emit
writeStateChanged(false);
303 void DolphinViewContainer::setFilterBarVisible(bool visible
)
305 Q_ASSERT(m_filterBar
!= 0);
308 m_filterBar
->setFocus();
314 void DolphinViewContainer::delayedStatusBarUpdate()
316 if (m_statusBarTimer
->isActive() && (m_statusBarTimestamp
.elapsed() > 2000)) {
317 // No update of the statusbar has been done during the last 2 seconds,
318 // although an update has been requested. Trigger an immediate update.
319 m_statusBarTimer
->stop();
322 // Invoke updateStatusBar() with a small delay. This assures that
323 // when a lot of delayedStatusBarUpdates() are done in a short time,
324 // no bottleneck is given.
325 m_statusBarTimer
->start();
329 void DolphinViewContainer::updateStatusBar()
331 m_statusBarTimestamp
.start();
333 // As the item count information is less important
334 // in comparison with other messages, it should only
336 // - the status bar is empty or
337 // - shows already the item count information or
338 // - shows only a not very important information
339 const QString newMessage
= m_view
->statusBarText();
340 const QString currentMessage
= m_statusBar
->message();
341 const bool updateStatusBarMsg
= currentMessage
.isEmpty()
342 || (currentMessage
== m_statusBar
->defaultText())
343 || (m_statusBar
->type() == DolphinStatusBar::Information
);
345 m_statusBar
->setDefaultText(newMessage
);
347 if (updateStatusBarMsg
) {
348 m_statusBar
->setMessage(newMessage
, DolphinStatusBar::Default
);
352 void DolphinViewContainer::initializeProgress()
354 if (isSearchUrl(url())) {
355 // Search KIO-slaves usually don't provide any progress information. Give
356 // an immediate hint to the user that a searching is done:
358 m_statusBar
->setProgressText(i18nc("@info", "Searching..."));
359 m_statusBar
->setProgress(-1);
363 void DolphinViewContainer::updateProgress(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::slotDirListerCompleted()
373 if (!m_statusBar
->progressText().isEmpty()) {
374 m_statusBar
->setProgressText(QString());
375 m_statusBar
->setProgress(100);
378 if (isSearchUrl(url()) && (m_dirLister
->items().count() == 0)) {
379 // The dir lister has been completed on a Nepomuk-URI and no items have been found. Instead
380 // of showing the default status bar information ("0 items") a more helpful information is given:
381 m_statusBar
->setMessage(i18nc("@info:status", "No items found."), DolphinStatusBar::Information
);
386 // Enable the 'File'->'Create New...' menu only if the directory
388 KFileItem item
= m_dirLister
->rootItem();
390 // it is unclear whether writing is supported
391 m_isFolderWritable
= true;
393 KFileItemListProperties
capabilities(KFileItemList() << item
);
394 m_isFolderWritable
= capabilities
.supportsWriting();
398 emit
writeStateChanged(m_isFolderWritable
);
402 void DolphinViewContainer::showItemInfo(const KFileItem
& item
)
405 // Only clear the status bar if unimportant messages are shown.
406 // This prevents that information- or error-messages get hidden
407 // by moving the mouse above the viewport or when closing the
409 if (m_statusBar
->type() == DolphinStatusBar::Default
) {
410 m_statusBar
->clear();
413 m_statusBar
->setMessage(item
.getStatusBarInfo(), DolphinStatusBar::Default
);
417 void DolphinViewContainer::showInfoMessage(const QString
& msg
)
419 m_statusBar
->setMessage(msg
, DolphinStatusBar::Information
);
422 void DolphinViewContainer::showErrorMessage(const QString
& msg
)
424 m_statusBar
->setMessage(msg
, DolphinStatusBar::Error
);
427 void DolphinViewContainer::showOperationCompletedMessage(const QString
& msg
)
429 m_statusBar
->setMessage(msg
, DolphinStatusBar::OperationCompleted
);
432 void DolphinViewContainer::closeFilterBar()
435 m_filterBar
->clear();
437 emit
showFilterBarChanged(false);
440 void DolphinViewContainer::setNameFilter(const QString
& nameFilter
)
442 m_view
->setNameFilter(nameFilter
);
443 delayedStatusBarUpdate();
446 void DolphinViewContainer::activate()
451 void DolphinViewContainer::saveViewState()
453 QByteArray locationState
;
454 QDataStream
stream(&locationState
, QIODevice::WriteOnly
);
455 m_view
->saveState(stream
);
456 m_urlNavigator
->saveLocationState(locationState
);
459 void DolphinViewContainer::slotUrlNavigatorLocationChanged(const KUrl
& url
)
461 if (KProtocolManager::supportsListing(url
)) {
462 setSearchModeEnabled(isSearchUrl(url
));
465 if (isActive() && !isSearchUrl(url
)) {
466 // When an URL has been entered, the view should get the focus.
467 // The focus must be requested asynchronously, as changing the URL might create
468 // a new view widget.
469 QTimer::singleShot(0, this, SLOT(requestFocus()));
471 } else if (KProtocolManager::isSourceProtocol(url
)) {
472 QString app
= "konqueror";
473 if (url
.protocol().startsWith(QLatin1String("http"))) {
474 showErrorMessage(i18nc("@info:status",
475 "Dolphin does not support web pages, the web browser has been launched"));
476 const KConfigGroup
config(KSharedConfig::openConfig("kdeglobals"), "General");
477 const QString browser
= config
.readEntry("BrowserApplication");
478 if (!browser
.isEmpty()) {
480 if (app
.startsWith('!')) {
481 // a literal command has been configured, remove the '!' prefix
486 showErrorMessage(i18nc("@info:status",
487 "Protocol not supported by Dolphin, Konqueror has been launched"));
490 const QString secureUrl
= KShell::quoteArg(url
.pathOrUrl());
491 const QString command
= app
+ ' ' + secureUrl
;
492 KRun::runCommand(command
, app
, app
, this);
494 showErrorMessage(i18nc("@info:status", "Invalid protocol"));
498 void DolphinViewContainer::dropUrls(const KUrl
& destination
, QDropEvent
* event
)
500 DragAndDropHelper::instance().dropUrls(KFileItem(), destination
, event
, this);
503 void DolphinViewContainer::redirect(const KUrl
& oldUrl
, const KUrl
& newUrl
)
506 const bool block
= m_urlNavigator
->signalsBlocked();
507 m_urlNavigator
->blockSignals(true);
509 // Assure that the location state is reset for redirection URLs. This
510 // allows to skip redirection URLs when going back or forward in the
512 m_urlNavigator
->saveLocationState(QByteArray());
513 m_urlNavigator
->setLocationUrl(newUrl
);
514 setSearchModeEnabled(isSearchUrl(newUrl
));
516 m_urlNavigator
->blockSignals(block
);
519 void DolphinViewContainer::requestFocus()
524 void DolphinViewContainer::saveUrlCompletionMode(KGlobalSettings::Completion completion
)
526 DolphinSettings
& settings
= DolphinSettings::instance();
527 settings
.generalSettings()->setUrlCompletionMode(completion
);
531 void DolphinViewContainer::slotHistoryChanged()
533 QByteArray locationState
= m_urlNavigator
->locationState();
535 if (!locationState
.isEmpty()) {
536 QDataStream
stream(&locationState
, QIODevice::ReadOnly
);
537 m_view
->restoreState(stream
);
541 void DolphinViewContainer::startSearching(const QString
&text
)
544 const KUrl url
= m_searchBox
->urlForSearching();
545 if (url
.isValid() && !url
.isEmpty()) {
546 m_urlNavigator
->setLocationUrl(url
);
550 void DolphinViewContainer::closeSearchBox()
552 setSearchModeEnabled(false);
555 void DolphinViewContainer::stopLoading()
557 m_view
->stopLoading();
560 bool DolphinViewContainer::isSearchUrl(const KUrl
& url
) const
562 const QString protocol
= url
.protocol();
563 return protocol
.contains("search") || (protocol
== QLatin1String("nepomuk"));
566 void DolphinViewContainer::slotItemTriggered(const KFileItem
& item
)
568 KUrl url
= item
.targetUrl();
575 const GeneralSettings
* settings
= DolphinSettings::instance().generalSettings();
576 const bool browseThroughArchives
= settings
->browseThroughArchives();
577 if (browseThroughArchives
&& item
.isFile() && url
.isLocalFile()) {
578 // Generic mechanism for redirecting to tar:/<path>/ when clicking on a tar file,
579 // zip:/<path>/ when clicking on a zip file, etc.
580 // The .protocol file specifies the mimetype that the kioslave handles.
581 // Note that we don't use mimetype inheritance since we don't want to
582 // open OpenDocument files as zip folders...
583 const QString protocol
= KProtocolManager::protocolForArchiveMimetype(item
.mimetype());
584 if (!protocol
.isEmpty()) {
585 url
.setProtocol(protocol
);
591 if (item
.mimetype() == "application/x-desktop") {
592 // redirect to the url in Type=Link desktop files
593 KDesktopFile
desktopFile(url
.toLocalFile());
594 if (desktopFile
.hasLinkType()) {
595 url
= desktopFile
.readUrl();
604 void DolphinViewContainer::openFile(const KUrl
& url
)
606 // Using m_dolphinModel for getting the file item instance is not possible
607 // here: openFile() is triggered by an error of the directory lister
608 // job, so the file item must be received "manually".
609 const KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, url
);
610 slotItemTriggered(item
);
613 #include "dolphinviewcontainer.moc"