]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinviewcontainer.cpp
assure that the size hint calculation and the painting code are consistent
[dolphin.git] / src / dolphinviewcontainer.cpp
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 ***************************************************************************/
19
20 #include "dolphinviewcontainer.h"
21 #include <kprotocolmanager.h>
22
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>
30
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>
46
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"
63
64 DolphinViewContainer::DolphinViewContainer(DolphinMainWindow* mainWindow,
65 QWidget* parent,
66 const KUrl& url) :
67 QWidget(parent),
68 m_showProgress(false),
69 m_isFolderWritable(false),
70 m_mainWindow(mainWindow),
71 m_topLayout(0),
72 m_urlNavigator(0),
73 m_view(0),
74 m_filterBar(0),
75 m_statusBar(0),
76 m_dirLister(0),
77 m_proxyModel(0)
78 {
79 hide();
80
81 m_topLayout = new QVBoxLayout(this);
82 m_topLayout->setSpacing(0);
83 m_topLayout->setMargin(0);
84
85 m_urlNavigator = new KUrlNavigator(DolphinSettings::instance().placesModel(), url, this);
86 connect(m_urlNavigator, SIGNAL(urlsDropped(const KUrl&, QDropEvent*)),
87 this, SLOT(dropUrls(const KUrl&, QDropEvent*)));
88 connect(m_urlNavigator, SIGNAL(activated()),
89 this, SLOT(activate()));
90
91 const GeneralSettings* settings = DolphinSettings::instance().generalSettings();
92 m_urlNavigator->setUrlEditable(settings->editableUrl());
93 m_urlNavigator->setShowFullPath(settings->showFullPath());
94 m_urlNavigator->setHomeUrl(settings->homeUrl());
95
96 m_dirLister = new DolphinDirLister();
97 m_dirLister->setAutoUpdate(true);
98 m_dirLister->setMainWindow(window());
99 m_dirLister->setDelayedMimeTypes(true);
100
101 m_dolphinModel = new DolphinModel(this);
102 m_dolphinModel->setDirLister(m_dirLister);
103 m_dolphinModel->setDropsAllowed(DolphinModel::DropOnDirectory);
104
105 m_proxyModel = new DolphinSortFilterProxyModel(this);
106 m_proxyModel->setSourceModel(m_dolphinModel);
107 m_proxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
108
109 connect(m_dirLister, SIGNAL(clear()),
110 this, SLOT(updateStatusBar()));
111 connect(m_dirLister, SIGNAL(percent(int)),
112 this, SLOT(updateProgress(int)));
113 connect(m_dirLister, SIGNAL(deleteItem(const KFileItem&)),
114 this, SLOT(updateStatusBar()));
115 connect(m_dirLister, SIGNAL(completed()),
116 this, SLOT(slotDirListerCompleted()));
117 connect(m_dirLister, SIGNAL(infoMessage(const QString&)),
118 this, SLOT(showInfoMessage(const QString&)));
119 connect(m_dirLister, SIGNAL(errorMessage(const QString&)),
120 this, SLOT(showErrorMessage(const QString&)));
121 connect(m_dirLister, SIGNAL(urlIsFileError(const KUrl&)),
122 this, SLOT(openFile(const KUrl&)));
123
124 m_view = new DolphinView(this,
125 url,
126 m_dirLister,
127 m_dolphinModel,
128 m_proxyModel);
129 connect(m_view, SIGNAL(urlChanged(const KUrl&)),
130 m_urlNavigator, SLOT(setUrl(const KUrl&)));
131 connect(m_view, SIGNAL(requestContextMenu(KFileItem, const KUrl&, const QList<QAction*>&)),
132 this, SLOT(openContextMenu(KFileItem, const KUrl&, const QList<QAction*>&)));
133 connect(m_view, SIGNAL(contentsMoved(int, int)),
134 this, SLOT(saveContentsPos(int, int)));
135 connect(m_view, SIGNAL(requestItemInfo(KFileItem)),
136 this, SLOT(showItemInfo(KFileItem)));
137 connect(m_view, SIGNAL(errorMessage(const QString&)),
138 this, SLOT(showErrorMessage(const QString&)));
139 connect(m_view, SIGNAL(infoMessage(const QString&)),
140 this, SLOT(showInfoMessage(const QString&)));
141 connect(m_view, SIGNAL(operationCompletedMessage(const QString&)),
142 this, SLOT(showOperationCompletedMessage(const QString&)));
143 connect(m_view, SIGNAL(itemTriggered(KFileItem)),
144 this, SLOT(slotItemTriggered(KFileItem)));
145 connect(m_view, SIGNAL(startedPathLoading(const KUrl&)),
146 this, SLOT(saveRootUrl(const KUrl&)));
147 connect(m_view, SIGNAL(redirection(KUrl, KUrl)),
148 this, SLOT(redirect(KUrl, KUrl)));
149
150 connect(m_urlNavigator, SIGNAL(urlChanged(const KUrl&)),
151 this, SLOT(restoreView(const KUrl&)));
152
153 m_statusBar = new DolphinStatusBar(this, m_view);
154
155 m_filterBar = new FilterBar(this);
156 m_filterBar->setVisible(settings->filterBar());
157 connect(m_filterBar, SIGNAL(filterChanged(const QString&)),
158 this, SLOT(setNameFilter(const QString&)));
159 connect(m_filterBar, SIGNAL(closeRequest()),
160 this, SLOT(closeFilterBar()));
161 connect(m_view, SIGNAL(urlChanged(const KUrl&)),
162 m_filterBar, SLOT(clear()));
163
164 m_topLayout->addWidget(m_urlNavigator);
165 m_topLayout->addWidget(m_view);
166 m_topLayout->addWidget(m_filterBar);
167 m_topLayout->addWidget(m_statusBar);
168 }
169
170 DolphinViewContainer::~DolphinViewContainer()
171 {
172 m_dirLister->disconnect();
173
174 delete m_proxyModel;
175 m_proxyModel = 0;
176 delete m_dolphinModel;
177 m_dolphinModel = 0;
178 m_dirLister = 0; // deleted by m_dolphinModel
179 }
180
181 void DolphinViewContainer::setUrl(const KUrl& newUrl)
182 {
183 m_urlNavigator->setUrl(newUrl);
184 if (newUrl != m_urlNavigator->url()) {
185 // Temporary disable the 'File'->'Create New...' menu until
186 // the write permissions can be checked in a fast way at
187 // DolphinViewContainer::slotDirListerCompleted().
188 m_isFolderWritable = false;
189 if (isActive()) {
190 m_mainWindow->newMenu()->menu()->setEnabled(false);
191 }
192 }
193 }
194
195 const KUrl& DolphinViewContainer::url() const
196 {
197 return m_urlNavigator->url();
198 }
199
200 void DolphinViewContainer::setActive(bool active)
201 {
202 m_urlNavigator->setActive(active);
203 m_view->setActive(active);
204 if (active) {
205 m_mainWindow->newMenu()->menu()->setEnabled(m_isFolderWritable);
206 }
207 }
208
209 bool DolphinViewContainer::isActive() const
210 {
211 Q_ASSERT(m_view->isActive() == m_urlNavigator->isActive());
212 return m_view->isActive();
213 }
214
215 void DolphinViewContainer::refresh()
216 {
217 m_view->refresh();
218 m_statusBar->refresh();
219 }
220
221 bool DolphinViewContainer::isFilterBarVisible() const
222 {
223 return m_filterBar->isVisible();
224 }
225
226 void DolphinViewContainer::showFilterBar(bool show)
227 {
228 Q_ASSERT(m_filterBar != 0);
229 if (show) {
230 m_filterBar->show();
231 } else {
232 closeFilterBar();
233 }
234 }
235
236 bool DolphinViewContainer::isUrlEditable() const
237 {
238 return m_urlNavigator->isUrlEditable();
239 }
240
241 void DolphinViewContainer::updateProgress(int percent)
242 {
243 if (!m_showProgress) {
244 // Only show the directory loading progress if the status bar does
245 // not contain another progress information. This means that
246 // the directory loading progress information has the lowest priority.
247 const QString progressText(m_statusBar->progressText());
248 const QString loadingText(i18nc("@info:progress", "Loading folder..."));
249 m_showProgress = progressText.isEmpty() || (progressText == loadingText);
250 if (m_showProgress) {
251 m_statusBar->setProgressText(loadingText);
252 m_statusBar->setProgress(0);
253 }
254 }
255
256 if (m_showProgress) {
257 m_statusBar->setProgress(percent);
258 }
259 }
260
261 void DolphinViewContainer::slotDirListerCompleted()
262 {
263 if (m_showProgress) {
264 m_statusBar->setProgressText(QString());
265 m_statusBar->setProgress(100);
266 m_showProgress = false;
267 }
268
269 updateStatusBar();
270 QMetaObject::invokeMethod(this, "restoreContentsPos", Qt::QueuedConnection);
271
272 // Enable the 'File'->'Create New...' menu only if the directory
273 // supports writing.
274 KFileItem item = m_dirLister->rootItem();
275 if (item.isNull()) {
276 // it is unclear whether writing is supported
277 m_isFolderWritable = true;
278 } else {
279 KonqFileItemCapabilities capabilities(KFileItemList() << item);
280 m_isFolderWritable = capabilities.supportsWriting();
281 }
282
283 if (isActive()) {
284 m_mainWindow->newMenu()->menu()->setEnabled(m_isFolderWritable);
285 }
286 }
287
288 void DolphinViewContainer::showItemInfo(const KFileItem& item)
289 {
290 if (item.isNull()) {
291 m_statusBar->clear();
292 } else {
293 m_statusBar->setMessage(item.getStatusBarInfo(), DolphinStatusBar::Default);
294 }
295 }
296
297 void DolphinViewContainer::showInfoMessage(const QString& msg)
298 {
299 m_statusBar->setMessage(msg, DolphinStatusBar::Information);
300 }
301
302 void DolphinViewContainer::showErrorMessage(const QString& msg)
303 {
304 m_statusBar->setMessage(msg, DolphinStatusBar::Error);
305 }
306
307 void DolphinViewContainer::showOperationCompletedMessage(const QString& msg)
308 {
309 m_statusBar->setMessage(msg, DolphinStatusBar::OperationCompleted);
310 }
311
312 void DolphinViewContainer::closeFilterBar()
313 {
314 m_filterBar->hide();
315 m_filterBar->clear();
316 m_view->setFocus();
317 emit showFilterBarChanged(false);
318 }
319
320 void DolphinViewContainer::updateStatusBar()
321 {
322 // As the item count information is less important
323 // in comparison with other messages, it should only
324 // be shown if:
325 // - the status bar is empty or
326 // - shows already the item count information or
327 // - shows only a not very important information
328 // - if any progress is given don't show the item count info at all
329 const QString msg(m_statusBar->message());
330 const bool updateStatusBarMsg = (msg.isEmpty() ||
331 (msg == m_statusBar->defaultText()) ||
332 (m_statusBar->type() == DolphinStatusBar::Information)) &&
333 (m_statusBar->progress() == 100);
334
335 const QString text(m_view->statusBarText());
336 m_statusBar->setDefaultText(text);
337
338 if (updateStatusBarMsg) {
339 m_statusBar->setMessage(text, DolphinStatusBar::Default);
340 }
341 }
342
343 void DolphinViewContainer::setNameFilter(const QString& nameFilter)
344 {
345 m_view->setNameFilter(nameFilter);
346 updateStatusBar();
347 }
348
349 void DolphinViewContainer::openContextMenu(const KFileItem& item,
350 const KUrl& url,
351 const QList<QAction*>& customActions)
352 {
353 DolphinContextMenu contextMenu(m_mainWindow, item, url);
354 contextMenu.setCustomActions(customActions);
355 contextMenu.open();
356 }
357
358 void DolphinViewContainer::saveContentsPos(int x, int y)
359 {
360 m_urlNavigator->savePosition(x, y);
361 }
362
363 void DolphinViewContainer::restoreContentsPos()
364 {
365 if (!url().isEmpty()) {
366 const QPoint pos = m_urlNavigator->savedPosition();
367 m_view->setContentsPosition(pos.x(), pos.y());
368 }
369 }
370
371 void DolphinViewContainer::activate()
372 {
373 setActive(true);
374 }
375
376 void DolphinViewContainer::restoreView(const KUrl& url)
377 {
378 if (KProtocolManager::supportsListing(url)) {
379 m_view->updateView(url, m_urlNavigator->savedRootUrl());
380 if (isActive()) {
381 // When an URL has been entered, the view should get the focus.
382 // The focus must be requested asynchronously, as changing the URL might create
383 // a new view widget. Using QTimer::singleShow() works reliable, however
384 // QMetaObject::invokeMethod() with a queued connection does not work, which might
385 // indicate that we should pass a hint to DolphinView::updateView()
386 // regarding the focus instead. To test: Enter an URL and press CTRL+Enter.
387 // Expected result: The view should get the focus.
388 QTimer::singleShot(0, this, SLOT(requestFocus()));
389 }
390 } else if (KProtocolManager::isSourceProtocol(url)) {
391 QString app = "konqueror";
392 if (url.protocol().startsWith(QLatin1String("http"))) {
393 showErrorMessage(i18nc("@info:status",
394 "Dolphin does not support web pages, the web browser has been launched"));
395 const KConfigGroup config(KSharedConfig::openConfig("kdeglobals"), "General");
396 const QString browser = config.readEntry("BrowserApplication");
397 if (!browser.isEmpty()) {
398 app = browser;
399 }
400 } else {
401 showErrorMessage(i18nc("@info:status",
402 "Protocol not supported by Dolphin, Konqueror has been launched"));
403 }
404 const QString command = app + ' ' + url.pathOrUrl();
405 KRun::runCommand(command, app, app, this);
406 } else {
407 showErrorMessage(i18nc("@info:status", "Invalid protocol"));
408 }
409 }
410
411 void DolphinViewContainer::saveRootUrl(const KUrl& url)
412 {
413 Q_UNUSED(url);
414 m_urlNavigator->saveRootUrl(m_view->rootUrl());
415 }
416
417 void DolphinViewContainer::dropUrls(const KUrl& destination, QDropEvent* event)
418 {
419 DragAndDropHelper::instance().dropUrls(KFileItem(), destination, event, this);
420 }
421
422 void DolphinViewContainer::redirect(const KUrl& oldUrl, const KUrl& newUrl)
423 {
424 Q_UNUSED(oldUrl);
425 const bool block = m_urlNavigator->signalsBlocked();
426 m_urlNavigator->blockSignals(true);
427 m_urlNavigator->setUrl(newUrl);
428 m_urlNavigator->blockSignals(block);
429 }
430
431 void DolphinViewContainer::requestFocus()
432 {
433 m_view->setFocus();
434 }
435
436 void DolphinViewContainer::slotItemTriggered(const KFileItem& item)
437 {
438 KUrl url = item.targetUrl();
439
440 if (item.isDir()) {
441 m_view->setUrl(url);
442 return;
443 }
444
445 const GeneralSettings* settings = DolphinSettings::instance().generalSettings();
446 const bool browseThroughArchives = settings->browseThroughArchives();
447 if (browseThroughArchives && item.isFile() && url.isLocalFile()) {
448 // Generic mechanism for redirecting to tar:/<path>/ when clicking on a tar file,
449 // zip:/<path>/ when clicking on a zip file, etc.
450 // The .protocol file specifies the mimetype that the kioslave handles.
451 // Note that we don't use mimetype inheritance since we don't want to
452 // open OpenDocument files as zip folders...
453 const QString protocol = KProtocolManager::protocolForArchiveMimetype(item.mimetype());
454 if (!protocol.isEmpty()) {
455 url.setProtocol(protocol);
456 m_view->setUrl(url);
457 return;
458 }
459 }
460
461 item.run();
462 }
463
464 void DolphinViewContainer::openFile(const KUrl& url)
465 {
466 // Using m_dolphinModel for getting the file item instance is not possible
467 // here: openFile() is triggered by an error of the directory lister
468 // job, so the file item must be received "manually".
469 const KFileItem item(KFileItem::Unknown, KFileItem::Unknown, url);
470 slotItemTriggered(item);
471 }
472
473 #include "dolphinviewcontainer.moc"