add more spacing
[personal-kdebase.git] / apps / dolphin / src / dolphinviewcontainer.cpp
blobf09d3d1c926895573bf4fc55602c31ebe250f7f7
1 /***************************************************************************
2 * Copyright (C) 2007 by Peter Penz <peter.penz@gmx.at> *
3 * *
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. *
8 * *
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. *
13 * *
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/QClipboard>
25 #include <QtGui/QKeyEvent>
26 #include <QtGui/QItemSelection>
27 #include <QtGui/QBoxLayout>
28 #include <QtCore/QTimer>
29 #include <QtGui/QScrollBar>
31 #include <kfileitemdelegate.h>
32 #include <kfileplacesmodel.h>
33 #include <kglobalsettings.h>
34 #include <klocale.h>
35 #include <kiconeffect.h>
36 #include <kio/netaccess.h>
37 #include <kio/previewjob.h>
38 #include <kmenu.h>
39 #include <kmimetyperesolver.h>
40 #include <knewmenu.h>
41 #include <konqmimedata.h>
42 #include <konq_fileitemcapabilities.h>
43 #include <konq_operations.h>
44 #include <kurl.h>
45 #include <krun.h>
47 #include "dolphinmodel.h"
48 #include "dolphincolumnview.h"
49 #include "dolphincontroller.h"
50 #include "dolphinstatusbar.h"
51 #include "dolphinmainwindow.h"
52 #include "dolphindirlister.h"
53 #include "dolphinsortfilterproxymodel.h"
54 #include "dolphindetailsview.h"
55 #include "dolphiniconsview.h"
56 #include "dolphincontextmenu.h"
57 #include "draganddrophelper.h"
58 #include "filterbar.h"
59 #include "kurlnavigator.h"
60 #include "viewproperties.h"
61 #include "settings/dolphinsettings.h"
62 #include "dolphin_generalsettings.h"
64 DolphinViewContainer::DolphinViewContainer(DolphinMainWindow* mainWindow,
65 QWidget* parent,
66 const KUrl& url) :
67 QWidget(parent),
68 m_showProgress(false),
69 m_mainWindow(mainWindow),
70 m_topLayout(0),
71 m_urlNavigator(0),
72 m_view(0),
73 m_filterBar(0),
74 m_statusBar(0),
75 m_dirLister(0),
76 m_proxyModel(0)
78 hide();
80 m_topLayout = new QVBoxLayout(this);
81 m_topLayout->setSpacing(0);
82 m_topLayout->setMargin(0);
84 m_urlNavigator = new KUrlNavigator(DolphinSettings::instance().placesModel(), url, this);
85 connect(m_urlNavigator, SIGNAL(urlsDropped(const KUrl&, QDropEvent*)),
86 this, SLOT(dropUrls(const KUrl&, QDropEvent*)));
87 connect(m_urlNavigator, SIGNAL(activated()),
88 this, SLOT(activate()));
90 const GeneralSettings* settings = DolphinSettings::instance().generalSettings();
91 m_urlNavigator->setUrlEditable(settings->editableUrl());
92 m_urlNavigator->setShowFullPath(settings->showFullPath());
93 m_urlNavigator->setHomeUrl(settings->homeUrl());
95 m_dirLister = new DolphinDirLister();
96 m_dirLister->setAutoUpdate(true);
97 m_dirLister->setMainWindow(window());
98 m_dirLister->setDelayedMimeTypes(true);
100 m_dolphinModel = new DolphinModel(this);
101 m_dolphinModel->setDirLister(m_dirLister);
102 m_dolphinModel->setDropsAllowed(DolphinModel::DropOnDirectory);
104 m_proxyModel = new DolphinSortFilterProxyModel(this);
105 m_proxyModel->setSourceModel(m_dolphinModel);
106 m_proxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
108 connect(m_dirLister, SIGNAL(clear()),
109 this, SLOT(updateStatusBar()));
110 connect(m_dirLister, SIGNAL(percent(int)),
111 this, SLOT(updateProgress(int)));
112 connect(m_dirLister, SIGNAL(deleteItem(const KFileItem&)),
113 this, SLOT(updateStatusBar()));
114 connect(m_dirLister, SIGNAL(completed()),
115 this, SLOT(slotDirListerCompleted()));
116 connect(m_dirLister, SIGNAL(infoMessage(const QString&)),
117 this, SLOT(showInfoMessage(const QString&)));
118 connect(m_dirLister, SIGNAL(errorMessage(const QString&)),
119 this, SLOT(showErrorMessage(const QString&)));
120 connect(m_dirLister, SIGNAL(urlIsFileError(const KUrl&)),
121 this, SLOT(openFile(const KUrl&)));
123 m_view = new DolphinView(this,
124 url,
125 m_dirLister,
126 m_dolphinModel,
127 m_proxyModel);
128 connect(m_view, SIGNAL(urlChanged(const KUrl&)),
129 m_urlNavigator, SLOT(setUrl(const KUrl&)));
130 connect(m_view, SIGNAL(requestContextMenu(KFileItem, const KUrl&)),
131 this, SLOT(openContextMenu(KFileItem, const KUrl&)));
132 connect(m_view, SIGNAL(contentsMoved(int, int)),
133 this, SLOT(saveContentsPos(int, int)));
134 connect(m_view, SIGNAL(requestItemInfo(KFileItem)),
135 this, SLOT(showItemInfo(KFileItem)));
136 connect(m_view, SIGNAL(errorMessage(const QString&)),
137 this, SLOT(showErrorMessage(const QString&)));
138 connect(m_view, SIGNAL(infoMessage(const QString&)),
139 this, SLOT(showInfoMessage(const QString&)));
140 connect(m_view, SIGNAL(operationCompletedMessage(const QString&)),
141 this, SLOT(showOperationCompletedMessage(const QString&)));
142 connect(m_view, SIGNAL(itemTriggered(KFileItem)),
143 this, SLOT(slotItemTriggered(KFileItem)));
144 connect(m_view, SIGNAL(startedPathLoading(const KUrl&)),
145 this, SLOT(saveRootUrl(const KUrl&)));
146 connect(m_view, SIGNAL(redirection(KUrl, KUrl)),
147 this, SLOT(redirect(KUrl, KUrl)));
149 connect(m_urlNavigator, SIGNAL(urlChanged(const KUrl&)),
150 this, SLOT(restoreView(const KUrl&)));
152 m_statusBar = new DolphinStatusBar(this, m_view);
154 m_filterBar = new FilterBar(this);
155 m_filterBar->setVisible(settings->filterBar());
156 connect(m_filterBar, SIGNAL(filterChanged(const QString&)),
157 this, SLOT(setNameFilter(const QString&)));
158 connect(m_filterBar, SIGNAL(closeRequest()),
159 this, SLOT(closeFilterBar()));
161 m_topLayout->addWidget(m_urlNavigator);
162 m_topLayout->addWidget(m_view);
163 m_topLayout->addWidget(m_filterBar);
164 m_topLayout->addWidget(m_statusBar);
167 DolphinViewContainer::~DolphinViewContainer()
169 m_dirLister->disconnect();
171 delete m_proxyModel;
172 m_proxyModel = 0;
173 delete m_dolphinModel;
174 m_dolphinModel = 0;
175 m_dirLister = 0; // deleted by m_dolphinModel
178 void DolphinViewContainer::setUrl(const KUrl& newUrl)
180 m_urlNavigator->setUrl(newUrl);
181 if (newUrl != m_urlNavigator->url()) {
182 // Temporary disable the 'File'->'Create New...' menu until
183 // the write permissions can be checked in a fast way at
184 // DolphinViewContainer::slotDirListerCompleted().
185 m_mainWindow->newMenu()->menu()->setEnabled(false);
189 const KUrl& DolphinViewContainer::url() const
191 return m_urlNavigator->url();
194 void DolphinViewContainer::setActive(bool active)
196 m_urlNavigator->setActive(active);
197 m_view->setActive(active);
200 bool DolphinViewContainer::isActive() const
202 Q_ASSERT(m_view->isActive() == m_urlNavigator->isActive());
203 return m_view->isActive();
206 void DolphinViewContainer::refresh()
208 m_view->refresh();
209 m_statusBar->refresh();
212 bool DolphinViewContainer::isFilterBarVisible() const
214 return m_filterBar->isVisible();
217 void DolphinViewContainer::showFilterBar(bool show)
219 Q_ASSERT(m_filterBar != 0);
220 if (show) {
221 m_filterBar->show();
222 } else {
223 closeFilterBar();
227 bool DolphinViewContainer::isUrlEditable() const
229 return m_urlNavigator->isUrlEditable();
232 void DolphinViewContainer::updateProgress(int percent)
234 if (!m_showProgress) {
235 // Only show the directory loading progress if the status bar does
236 // not contain another progress information. This means that
237 // the directory loading progress information has the lowest priority.
238 const QString progressText(m_statusBar->progressText());
239 const QString loadingText(i18nc("@info:progress", "Loading folder..."));
240 m_showProgress = progressText.isEmpty() || (progressText == loadingText);
241 if (m_showProgress) {
242 m_statusBar->setProgressText(loadingText);
243 m_statusBar->setProgress(0);
247 if (m_showProgress) {
248 m_statusBar->setProgress(percent);
252 void DolphinViewContainer::slotDirListerCompleted()
254 if (m_showProgress) {
255 m_statusBar->setProgressText(QString());
256 m_statusBar->setProgress(100);
257 m_showProgress = false;
260 updateStatusBar();
261 QMetaObject::invokeMethod(this, "restoreContentsPos", Qt::QueuedConnection);
263 // Enable the 'File'->'Create New...' menu only if the directory
264 // supports writing.
265 KMenu* createNew = m_mainWindow->newMenu()->menu();
266 KFileItem item = m_dirLister->rootItem();
267 if (item.isNull()) {
268 // it is unclear whether writing is supported
269 createNew->setEnabled(true);
270 } else {
271 KonqFileItemCapabilities capabilities(KFileItemList() << item);
272 createNew->setEnabled(capabilities.supportsWriting());
276 void DolphinViewContainer::showItemInfo(const KFileItem& item)
278 if (item.isNull()) {
279 m_statusBar->clear();
280 } else {
281 m_statusBar->setMessage(item.getStatusBarInfo(), DolphinStatusBar::Default);
285 void DolphinViewContainer::showInfoMessage(const QString& msg)
287 m_statusBar->setMessage(msg, DolphinStatusBar::Information);
290 void DolphinViewContainer::showErrorMessage(const QString& msg)
292 m_statusBar->setMessage(msg, DolphinStatusBar::Error);
295 void DolphinViewContainer::showOperationCompletedMessage(const QString& msg)
297 m_statusBar->setMessage(msg, DolphinStatusBar::OperationCompleted);
300 void DolphinViewContainer::closeFilterBar()
302 m_filterBar->hide();
303 m_filterBar->clear();
304 m_view->setFocus();
305 emit showFilterBarChanged(false);
308 void DolphinViewContainer::updateStatusBar()
310 // As the item count information is less important
311 // in comparison with other messages, it should only
312 // be shown if:
313 // - the status bar is empty or
314 // - shows already the item count information or
315 // - shows only a not very important information
316 // - if any progress is given don't show the item count info at all
317 const QString msg(m_statusBar->message());
318 const bool updateStatusBarMsg = (msg.isEmpty() ||
319 (msg == m_statusBar->defaultText()) ||
320 (m_statusBar->type() == DolphinStatusBar::Information)) &&
321 (m_statusBar->progress() == 100);
323 const QString text(m_view->statusBarText());
324 m_statusBar->setDefaultText(text);
326 if (updateStatusBarMsg) {
327 m_statusBar->setMessage(text, DolphinStatusBar::Default);
331 void DolphinViewContainer::setNameFilter(const QString& nameFilter)
333 m_view->setNameFilter(nameFilter);
334 updateStatusBar();
337 void DolphinViewContainer::openContextMenu(const KFileItem& item,
338 const KUrl& url)
340 DolphinContextMenu contextMenu(m_mainWindow, item, url);
341 contextMenu.open();
344 void DolphinViewContainer::saveContentsPos(int x, int y)
346 m_urlNavigator->savePosition(x, y);
349 void DolphinViewContainer::restoreContentsPos()
351 if (!url().isEmpty()) {
352 const QPoint pos = m_urlNavigator->savedPosition();
353 m_view->setContentsPosition(pos.x(), pos.y());
357 void DolphinViewContainer::activate()
359 setActive(true);
362 void DolphinViewContainer::restoreView(const KUrl& url)
364 if (KProtocolManager::supportsListing(url)) {
365 m_view->updateView(url, m_urlNavigator->savedRootUrl());
366 if (isActive()) {
367 // When an URL has been entered, the view should get the focus.
368 // The focus must be requested asynchronously, as changing the URL might create
369 // a new view widget. Using QTimer::singleShow() works reliable, however
370 // QMetaObject::invokeMethod() with a queued connection does not work, which might
371 // indicate that we should pass a hint to DolphinView::updateView()
372 // regarding the focus instead. To test: Enter an URL and press CTRL+Enter.
373 // Expected result: The view should get the focus.
374 QTimer::singleShot(0, this, SLOT(requestFocus()));
376 } else if (KProtocolManager::isSourceProtocol(url)) {
377 QString app = "konqueror";
378 if (url.protocol().startsWith(QLatin1String("http"))) {
379 showErrorMessage(i18nc("@info:status",
380 "Dolphin does not support web pages, the web browser has been launched"));
381 const KConfigGroup config(KSharedConfig::openConfig("kdeglobals"), "General");
382 const QString browser = config.readEntry("BrowserApplication");
383 if (!browser.isEmpty()) {
384 app = browser;
386 } else {
387 showErrorMessage(i18nc("@info:status",
388 "Protocol not supported by Dolphin, Konqueror has been launched"));
390 const QString command = app + ' ' + url.pathOrUrl();
391 KRun::runCommand(command, app, app, this);
392 } else {
393 showErrorMessage(i18nc("@info:status", "Invalid protocol"));
397 void DolphinViewContainer::saveRootUrl(const KUrl& url)
399 Q_UNUSED(url);
400 m_urlNavigator->saveRootUrl(m_view->rootUrl());
403 void DolphinViewContainer::dropUrls(const KUrl& destination, QDropEvent* event)
405 DragAndDropHelper::instance().dropUrls(KFileItem(), destination, event, this);
408 void DolphinViewContainer::redirect(const KUrl& oldUrl, const KUrl& newUrl)
410 Q_UNUSED(oldUrl);
411 const bool block = m_urlNavigator->signalsBlocked();
412 m_urlNavigator->blockSignals(true);
413 m_urlNavigator->setUrl(newUrl);
414 m_urlNavigator->blockSignals(block);
417 void DolphinViewContainer::requestFocus()
419 m_view->setFocus();
422 void DolphinViewContainer::slotItemTriggered(const KFileItem& item)
424 KUrl url = item.targetUrl();
426 if (item.isDir()) {
427 m_view->setUrl(url);
428 return;
431 const GeneralSettings* settings = DolphinSettings::instance().generalSettings();
432 const bool browseThroughArchives = settings->browseThroughArchives();
433 if (browseThroughArchives && item.isFile() && url.isLocalFile()) {
434 // Generic mechanism for redirecting to tar:/<path>/ when clicking on a tar file,
435 // zip:/<path>/ when clicking on a zip file, etc.
436 // The .protocol file specifies the mimetype that the kioslave handles.
437 // Note that we don't use mimetype inheritance since we don't want to
438 // open OpenDocument files as zip folders...
439 const QString protocol = KProtocolManager::protocolForArchiveMimetype(item.mimetype());
440 if (!protocol.isEmpty()) {
441 url.setProtocol(protocol);
442 m_view->setUrl(url);
443 return;
447 item.run();
450 void DolphinViewContainer::openFile(const KUrl& url)
452 // Using m_dolphinModel for getting the file item instance is not possible
453 // here: openFile() is triggered by an error of the directory lister
454 // job, so the file item must be received "manually".
455 const KFileItem item(KFileItem::Unknown, KFileItem::Unknown, url);
456 slotItemTriggered(item);
459 #include "dolphinviewcontainer.moc"