]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinviewcontainer.cpp
Improvements for slow sorting roles
[dolphin.git] / src / dolphinviewcontainer.cpp
1 /***************************************************************************
2 * Copyright (C) 2007 by Peter Penz <peter.penz19@gmail.com> *
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>
22
23 #include <QApplication>
24 #include <QKeyEvent>
25 #include <QItemSelection>
26 #include <QBoxLayout>
27 #include <QTimer>
28 #include <QScrollBar>
29
30 #include <KDesktopFile>
31 #include <KFileItemDelegate>
32 #include <KFilePlacesModel>
33 #include <KLocale>
34 #include <KIconEffect>
35 #include <KIO/NetAccess>
36 #include <KIO/PreviewJob>
37 #include <KNewFileMenu>
38 #include <konqmimedata.h>
39 #include <konq_operations.h>
40 #include <KShell>
41 #include <KUrl>
42 #include <KUrlComboBox>
43 #include <KUrlNavigator>
44 #include <KRun>
45
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"
55
56 DolphinViewContainer::DolphinViewContainer(const KUrl& url, QWidget* parent) :
57 QWidget(parent),
58 m_topLayout(0),
59 m_urlNavigator(0),
60 m_searchBox(0),
61 m_view(0),
62 m_filterBar(0),
63 m_statusBar(0),
64 m_statusBarTimer(0),
65 m_statusBarTimestamp()
66 {
67 hide();
68
69 m_topLayout = new QVBoxLayout(this);
70 m_topLayout->setSpacing(0);
71 m_topLayout->setMargin(0);
72
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)));
80
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()));
87
88 m_searchBox = new DolphinSearchBox(this);
89 m_searchBox->hide();
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()));
93
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(startedPathLoading(KUrl)), this, SLOT(slotStartedPathLoading()));
103 connect(m_view, SIGNAL(finishedPathLoading(KUrl)), this, SLOT(slotFinishedPathLoading()));
104 connect(m_view, SIGNAL(itemCountChanged()), this, SLOT(delayedStatusBarUpdate()));
105 connect(m_view, SIGNAL(pathLoadingProgress(int)), this, SLOT(updateLoadingProgress(int)));
106 connect(m_view, SIGNAL(sortProgress(int)), this, SLOT(updateSortProgress(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(urlIsFileError(KUrl)), this, SLOT(openFile(KUrl)));
110 connect(m_view, SIGNAL(selectionChanged(KFileItemList)), this, SLOT(delayedStatusBarUpdate()));
111 connect(m_view, SIGNAL(operationCompletedMessage(QString)), this, SLOT(showOperationCompletedMessage(QString)));
112 connect(m_view, SIGNAL(urlAboutToBeChanged(KUrl)), this, SLOT(slotViewUrlAboutToBeChanged(KUrl)));
113
114 connect(m_urlNavigator, SIGNAL(urlAboutToBeChanged(KUrl)),
115 this, SLOT(slotUrlNavigatorLocationAboutToBeChanged(KUrl)));
116 connect(m_urlNavigator, SIGNAL(urlChanged(KUrl)),
117 this, SLOT(slotUrlNavigatorLocationChanged(KUrl)));
118 connect(m_urlNavigator, SIGNAL(historyChanged()),
119 this, SLOT(slotHistoryChanged()));
120
121 // initialize status bar
122 m_statusBar = new DolphinStatusBar(this, m_view);
123 connect(m_statusBar, SIGNAL(stopPressed()), this, SLOT(stopLoading()));
124
125 m_statusBarTimer = new QTimer(this);
126 m_statusBarTimer->setSingleShot(true);
127 m_statusBarTimer->setInterval(300);
128 connect(m_statusBarTimer, SIGNAL(timeout()),
129 this, SLOT(updateStatusBar()));
130
131 KIO::FileUndoManager* undoManager = KIO::FileUndoManager::self();
132 connect(undoManager, SIGNAL(jobRecordingFinished(CommandType)),
133 this, SLOT(delayedStatusBarUpdate()));
134
135 // initialize filter bar
136 m_filterBar = new FilterBar(this);
137 m_filterBar->setVisible(settings->filterBar());
138 connect(m_filterBar, SIGNAL(filterChanged(QString)),
139 this, SLOT(setNameFilter(QString)));
140 connect(m_filterBar, SIGNAL(closeRequest()),
141 this, SLOT(closeFilterBar()));
142 connect(m_view, SIGNAL(urlChanged(KUrl)),
143 m_filterBar, SLOT(clear()));
144
145 m_topLayout->addWidget(m_urlNavigator);
146 m_topLayout->addWidget(m_searchBox);
147 m_topLayout->addWidget(m_view);
148 m_topLayout->addWidget(m_filterBar);
149 m_topLayout->addWidget(m_statusBar);
150
151 setSearchModeEnabled(isSearchUrl(url));
152 }
153
154 DolphinViewContainer::~DolphinViewContainer()
155 {
156 }
157
158 KUrl DolphinViewContainer::url() const
159 {
160 return m_view->url();
161 }
162
163 void DolphinViewContainer::setActive(bool active)
164 {
165 m_urlNavigator->setActive(active);
166 m_view->setActive(active);
167 }
168
169 bool DolphinViewContainer::isActive() const
170 {
171 Q_ASSERT(m_view->isActive() == m_urlNavigator->isActive());
172 return m_view->isActive();
173 }
174
175 const DolphinStatusBar* DolphinViewContainer::statusBar() const
176 {
177 return m_statusBar;
178 }
179
180 DolphinStatusBar* DolphinViewContainer::statusBar()
181 {
182 return m_statusBar;
183 }
184
185 const KUrlNavigator* DolphinViewContainer::urlNavigator() const
186 {
187 return m_urlNavigator;
188 }
189
190 KUrlNavigator* DolphinViewContainer::urlNavigator()
191 {
192 return m_urlNavigator;
193 }
194
195 const DolphinView* DolphinViewContainer::view() const
196 {
197 return m_view;
198 }
199
200 DolphinView* DolphinViewContainer::view()
201 {
202 return m_view;
203 }
204
205 const DolphinSearchBox* DolphinViewContainer::searchBox() const
206 {
207 return m_searchBox;
208 }
209
210 DolphinSearchBox* DolphinViewContainer::searchBox()
211 {
212 return m_searchBox;
213 }
214
215 void DolphinViewContainer::readSettings()
216 {
217 if (GeneralSettings::modifiedStartupSettings()) {
218 // The startup settings should only get applied if they have been
219 // modified by the user. Otherwise keep the (possibly) different current
220 // settings of the URL navigator and the filterbar.
221 m_urlNavigator->setUrlEditable(GeneralSettings::editableUrl());
222 m_urlNavigator->setShowFullPath(GeneralSettings::showFullPath());
223 m_urlNavigator->setHomeUrl(KUrl(GeneralSettings::homeUrl()));
224 setFilterBarVisible(GeneralSettings::filterBar());
225 }
226
227 m_view->readSettings();
228 m_statusBar->readSettings();
229 }
230
231 bool DolphinViewContainer::isFilterBarVisible() const
232 {
233 return m_filterBar->isVisible();
234 }
235
236 void DolphinViewContainer::setSearchModeEnabled(bool enabled)
237 {
238 if (enabled == isSearchModeEnabled()) {
239 if (enabled && !m_searchBox->hasFocus()) {
240 m_searchBox->setFocus();
241 m_searchBox->selectAll();
242 }
243 return;
244 }
245
246 m_searchBox->setVisible(enabled);
247 m_urlNavigator->setVisible(!enabled);
248
249 if (enabled) {
250 KUrl url = m_urlNavigator->locationUrl();
251 m_searchBox->setText(QString());
252 m_searchBox->setReadOnly(isSearchUrl(url), url);
253
254 // Remember the most recent non-search URL as search path
255 // of the search-box, so that it can be restored
256 // when switching back to the URL navigator.
257 int index = m_urlNavigator->historyIndex();
258 const int historySize = m_urlNavigator->historySize();
259 while (isSearchUrl(url) && (index < historySize)) {
260 ++index;
261 url = m_urlNavigator->locationUrl(index);
262 }
263
264 if (!isSearchUrl(url)) {
265 m_searchBox->setSearchPath(url);
266 }
267 } else {
268 // Restore the URL for the URL navigator. If Dolphin has been
269 // started with a search-URL, the home URL is used as fallback.
270 const KUrl url = m_searchBox->searchPath();
271 if (url.isValid() && !url.isEmpty()) {
272 if (isSearchUrl(url)) {
273 m_urlNavigator->goHome();
274 } else {
275 m_urlNavigator->setLocationUrl(url);
276 }
277 }
278 }
279
280 emit searchModeChanged(enabled);
281 }
282
283 bool DolphinViewContainer::isSearchModeEnabled() const
284 {
285 return m_searchBox->isVisible();
286 }
287
288 void DolphinViewContainer::setUrl(const KUrl& newUrl)
289 {
290 if (newUrl != m_urlNavigator->locationUrl()) {
291 m_urlNavigator->setLocationUrl(newUrl);
292 }
293 }
294
295 void DolphinViewContainer::setFilterBarVisible(bool visible)
296 {
297 Q_ASSERT(m_filterBar);
298 if (visible) {
299 m_filterBar->show();
300 m_filterBar->setFocus();
301 m_filterBar->selectAll();
302 } else {
303 closeFilterBar();
304 }
305 }
306
307 void DolphinViewContainer::delayedStatusBarUpdate()
308 {
309 if (m_statusBarTimer->isActive() && (m_statusBarTimestamp.elapsed() > 2000)) {
310 // No update of the statusbar has been done during the last 2 seconds,
311 // although an update has been requested. Trigger an immediate update.
312 m_statusBarTimer->stop();
313 updateStatusBar();
314 } else {
315 // Invoke updateStatusBar() with a small delay. This assures that
316 // when a lot of delayedStatusBarUpdates() are done in a short time,
317 // no bottleneck is given.
318 m_statusBarTimer->start();
319 }
320 }
321
322 void DolphinViewContainer::updateStatusBar()
323 {
324 m_statusBarTimestamp.start();
325
326 const QString newMessage = m_view->statusBarText();
327 m_statusBar->setDefaultText(newMessage);
328
329 // We don't want to override errors. Other messages are only protected by
330 // the Statusbar itself depending on timings (see DolphinStatusBar::setMessage).
331 if (m_statusBar->type() != DolphinStatusBar::Error) {
332 m_statusBar->setMessage(newMessage, DolphinStatusBar::Default);
333 }
334 }
335
336 void DolphinViewContainer::updateLoadingProgress(int percent)
337 {
338 if (m_statusBar->progressText().isEmpty()) {
339 m_statusBar->setProgressText(i18nc("@info:progress", "Loading folder..."));
340 }
341 m_statusBar->setProgress(percent);
342 }
343
344 void DolphinViewContainer::updateSortProgress(int percent)
345 {
346 if (m_statusBar->progressText().isEmpty()) {
347 m_statusBar->setProgressText(i18nc("@info:progress", "Sorting..."));
348 }
349 m_statusBar->setProgress(percent);
350 }
351
352 void DolphinViewContainer::slotStartedPathLoading()
353 {
354 if (isSearchUrl(url())) {
355 // Search KIO-slaves usually don't provide any progress information. Give
356 // a hint to the user that a searching is done:
357 updateStatusBar();
358 m_statusBar->setProgressText(i18nc("@info", "Searching..."));
359 m_statusBar->setProgress(-1);
360 } else {
361 // Trigger an undetermined progress indication. The progress
362 // information in percent will be triggered by the percent() signal
363 // of the directory lister later.
364 updateLoadingProgress(-1);
365 }
366 }
367
368 void DolphinViewContainer::slotFinishedPathLoading()
369 {
370 if (!m_statusBar->progressText().isEmpty()) {
371 m_statusBar->setProgressText(QString());
372 m_statusBar->setProgress(100);
373 }
374
375 if (isSearchUrl(url()) && m_view->items().isEmpty()) {
376 // The dir lister has been completed on a Nepomuk-URI and no items have been found. Instead
377 // of showing the default status bar information ("0 items") a more helpful information is given:
378 m_statusBar->setMessage(i18nc("@info:status", "No items found."), DolphinStatusBar::Information);
379 } else {
380 updateStatusBar();
381 }
382 }
383
384 void DolphinViewContainer::slotItemActivated(const KFileItem& item)
385 {
386 // It is possible to activate items on inactive views by
387 // drag & drop operations. Assure that activating an item always
388 // results in an active view.
389 m_view->setActive(true);
390
391 KUrl url = item.targetUrl();
392
393 if (item.isDir()) {
394 m_view->setUrl(url);
395 return;
396 }
397
398 if (GeneralSettings::browseThroughArchives() && item.isFile() && url.isLocalFile()) {
399 // Generic mechanism for redirecting to tar:/<path>/ when clicking on a tar file,
400 // zip:/<path>/ when clicking on a zip file, etc.
401 // The .protocol file specifies the mimetype that the kioslave handles.
402 // Note that we don't use mimetype inheritance since we don't want to
403 // open OpenDocument files as zip folders...
404 const QString protocol = KProtocolManager::protocolForArchiveMimetype(item.mimetype());
405 if (!protocol.isEmpty()) {
406 url.setProtocol(protocol);
407 m_view->setUrl(url);
408 return;
409 }
410 }
411
412 if (item.mimetype() == QLatin1String("application/x-desktop")) {
413 // Redirect to the URL in Type=Link desktop files
414 KDesktopFile desktopFile(url.toLocalFile());
415 if (desktopFile.hasLinkType()) {
416 url = desktopFile.readUrl();
417 m_view->setUrl(url);
418 return;
419 }
420 }
421
422 item.run();
423 }
424
425 void DolphinViewContainer::openFile(const KUrl& url)
426 {
427 const KFileItem item(KFileItem::Unknown, KFileItem::Unknown, url);
428 slotItemActivated(item);
429 }
430
431 void DolphinViewContainer::showItemInfo(const KFileItem& item)
432 {
433 if (item.isNull()) {
434 // Only clear the status bar if unimportant messages are shown.
435 // This prevents that information- or error-messages get hidden
436 // by moving the mouse above the viewport or when closing the
437 // context menu.
438 if (m_statusBar->type() == DolphinStatusBar::Default) {
439 m_statusBar->clear();
440 }
441 } else {
442 const QString message = item.isDir() ? item.text() : item.getStatusBarInfo();
443 m_statusBar->setMessage(message, DolphinStatusBar::Default);
444 }
445 }
446
447 void DolphinViewContainer::showInfoMessage(const QString& msg)
448 {
449 m_statusBar->setMessage(msg, DolphinStatusBar::Information);
450 }
451
452 void DolphinViewContainer::showErrorMessage(const QString& msg)
453 {
454 m_statusBar->setMessage(msg, DolphinStatusBar::Error);
455 }
456
457 void DolphinViewContainer::showOperationCompletedMessage(const QString& msg)
458 {
459 m_statusBar->setMessage(msg, DolphinStatusBar::OperationCompleted);
460 }
461
462 void DolphinViewContainer::closeFilterBar()
463 {
464 m_filterBar->hide();
465 m_filterBar->clear();
466 m_view->setFocus();
467 emit showFilterBarChanged(false);
468 }
469
470 void DolphinViewContainer::setNameFilter(const QString& nameFilter)
471 {
472 m_view->setNameFilter(nameFilter);
473 delayedStatusBarUpdate();
474 }
475
476 void DolphinViewContainer::activate()
477 {
478 setActive(true);
479 }
480
481 void DolphinViewContainer::slotViewUrlAboutToBeChanged(const KUrl& url)
482 {
483 // URL changes of the view can happen in two ways:
484 // 1. The URL navigator gets changed and will trigger the view to update its URL
485 // 2. The URL of the view gets changed and will trigger the URL navigator to update
486 // its URL (e.g. by clicking on an item)
487 // In this scope the view-state may only get saved in case 2:
488 if (url != m_urlNavigator->locationUrl()) {
489 saveViewState();
490 }
491 }
492
493 void DolphinViewContainer::slotUrlNavigatorLocationAboutToBeChanged(const KUrl& url)
494 {
495 // URL changes of the view can happen in two ways:
496 // 1. The URL navigator gets changed and will trigger the view to update its URL
497 // 2. The URL of the view gets changed and will trigger the URL navigator to update
498 // its URL (e.g. by clicking on an item)
499 // In this scope the view-state may only get saved in case 1:
500 if (url != m_view->url()) {
501 saveViewState();
502 }
503 }
504
505 void DolphinViewContainer::slotUrlNavigatorLocationChanged(const KUrl& url)
506 {
507 if (KProtocolManager::supportsListing(url)) {
508 setSearchModeEnabled(isSearchUrl(url));
509 m_view->setUrl(url);
510
511 if (isActive() && !isSearchUrl(url)) {
512 // When an URL has been entered, the view should get the focus.
513 // The focus must be requested asynchronously, as changing the URL might create
514 // a new view widget.
515 QTimer::singleShot(0, this, SLOT(requestFocus()));
516 }
517 } else if (KProtocolManager::isSourceProtocol(url)) {
518 QString app = "konqueror";
519 if (url.protocol().startsWith(QLatin1String("http"))) {
520 showErrorMessage(i18nc("@info:status",
521 "Dolphin does not support web pages, the web browser has been launched"));
522 const KConfigGroup config(KSharedConfig::openConfig("kdeglobals"), "General");
523 const QString browser = config.readEntry("BrowserApplication");
524 if (!browser.isEmpty()) {
525 app = browser;
526 if (app.startsWith('!')) {
527 // a literal command has been configured, remove the '!' prefix
528 app = app.mid(1);
529 }
530 }
531 } else {
532 showErrorMessage(i18nc("@info:status",
533 "Protocol not supported by Dolphin, Konqueror has been launched"));
534 }
535
536 const QString secureUrl = KShell::quoteArg(url.pathOrUrl());
537 const QString command = app + ' ' + secureUrl;
538 KRun::runCommand(command, app, app, this);
539 } else {
540 showErrorMessage(i18nc("@info:status", "Invalid protocol"));
541 }
542 }
543
544 void DolphinViewContainer::dropUrls(const KUrl& destination, QDropEvent* event)
545 {
546 DragAndDropHelper::dropUrls(KFileItem(), destination, event);
547 }
548
549 void DolphinViewContainer::redirect(const KUrl& oldUrl, const KUrl& newUrl)
550 {
551 Q_UNUSED(oldUrl);
552 const bool block = m_urlNavigator->signalsBlocked();
553 m_urlNavigator->blockSignals(true);
554
555 // Assure that the location state is reset for redirection URLs. This
556 // allows to skip redirection URLs when going back or forward in the
557 // URL history.
558 m_urlNavigator->saveLocationState(QByteArray());
559 m_urlNavigator->setLocationUrl(newUrl);
560 setSearchModeEnabled(isSearchUrl(newUrl));
561
562 m_urlNavigator->blockSignals(block);
563 }
564
565 void DolphinViewContainer::requestFocus()
566 {
567 m_view->setFocus();
568 }
569
570 void DolphinViewContainer::saveUrlCompletionMode(KGlobalSettings::Completion completion)
571 {
572 GeneralSettings::setUrlCompletionMode(completion);
573 }
574
575 void DolphinViewContainer::slotHistoryChanged()
576 {
577 QByteArray locationState = m_urlNavigator->locationState();
578 if (!locationState.isEmpty()) {
579 QDataStream stream(&locationState, QIODevice::ReadOnly);
580 m_view->restoreState(stream);
581 }
582 }
583
584 void DolphinViewContainer::startSearching(const QString &text)
585 {
586 Q_UNUSED(text);
587 const KUrl url = m_searchBox->urlForSearching();
588 if (url.isValid() && !url.isEmpty()) {
589 m_urlNavigator->setLocationUrl(url);
590 }
591 }
592
593 void DolphinViewContainer::closeSearchBox()
594 {
595 setSearchModeEnabled(false);
596 }
597
598 void DolphinViewContainer::stopLoading()
599 {
600 m_view->stopLoading();
601 m_statusBar->setProgress(100);
602 }
603
604 bool DolphinViewContainer::isSearchUrl(const KUrl& url) const
605 {
606 const QString protocol = url.protocol();
607 return protocol.contains("search") || (protocol == QLatin1String("nepomuk"));
608 }
609
610 void DolphinViewContainer::saveViewState()
611 {
612 QByteArray locationState;
613 QDataStream stream(&locationState, QIODevice::WriteOnly);
614 m_view->saveState(stream);
615 m_urlNavigator->saveLocationState(locationState);
616 }
617
618 #include "dolphinviewcontainer.moc"