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