]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinviewcontainer.cpp
Show an undetermined progress indication within one second after changing an URL.
[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/QKeyEvent>
25 #include <QtGui/QItemSelection>
26 #include <QtGui/QBoxLayout>
27 #include <QtCore/QTimer>
28 #include <QtGui/QScrollBar>
29
30 #include <kdesktopfile.h>
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 <knewmenu.h>
40 #include <konqmimedata.h>
41 #include <kfileitemlistproperties.h>
42 #include <konq_operations.h>
43 #include <kshell.h>
44 #include <kurl.h>
45 #include <kurlcombobox.h>
46 #include <kurlnavigator.h>
47 #include <krun.h>
48
49 #include "dolphin_generalsettings.h"
50 #include "dolphinmainwindow.h"
51 #include "filterbar/filterbar.h"
52 #include "search/dolphinsearchbox.h"
53 #include "settings/dolphinsettings.h"
54 #include "statusbar/dolphinstatusbar.h"
55 #include "views/dolphincolumnview.h"
56 #include "views/dolphindetailsview.h"
57 #include "views/dolphindirlister.h"
58 #include "views/dolphinsortfilterproxymodel.h"
59 #include "views/draganddrophelper.h"
60 #include "views/dolphiniconsview.h"
61 #include "views/dolphinmodel.h"
62 #include "views/dolphinviewcontroller.h"
63 #include "views/viewmodecontroller.h"
64 #include "views/viewproperties.h"
65
66 DolphinViewContainer::DolphinViewContainer(const KUrl& url, QWidget* parent) :
67 QWidget(parent),
68 m_isFolderWritable(false),
69 m_topLayout(0),
70 m_urlNavigator(0),
71 m_searchBox(0),
72 m_view(0),
73 m_filterBar(0),
74 m_statusBar(0),
75 m_statusBarTimer(0),
76 m_statusBarTimestamp(),
77 m_dirLister(0),
78 m_proxyModel(0)
79 {
80 hide();
81
82 m_topLayout = new QVBoxLayout(this);
83 m_topLayout->setSpacing(0);
84 m_topLayout->setMargin(0);
85
86 m_urlNavigator = new KUrlNavigator(DolphinSettings::instance().placesModel(), url, this);
87 connect(m_urlNavigator, SIGNAL(urlsDropped(const KUrl&, QDropEvent*)),
88 this, SLOT(dropUrls(const KUrl&, QDropEvent*)));
89 connect(m_urlNavigator, SIGNAL(activated()),
90 this, SLOT(activate()));
91 connect(m_urlNavigator->editor(), SIGNAL(completionModeChanged(KGlobalSettings::Completion)),
92 this, SLOT(saveUrlCompletionMode(KGlobalSettings::Completion)));
93
94 const GeneralSettings* settings = DolphinSettings::instance().generalSettings();
95 m_urlNavigator->setUrlEditable(settings->editableUrl());
96 m_urlNavigator->setShowFullPath(settings->showFullPath());
97 m_urlNavigator->setHomeUrl(KUrl(settings->homeUrl()));
98 KUrlComboBox* editor = m_urlNavigator->editor();
99 editor->setCompletionMode(KGlobalSettings::Completion(settings->urlCompletionMode()));
100
101 m_searchBox = new DolphinSearchBox(this);
102 m_searchBox->hide();
103 connect(m_searchBox, SIGNAL(closeRequest()), this, SLOT(closeSearchBox()));
104 connect(m_searchBox, SIGNAL(search(QString)), this, SLOT(startSearching(QString)));
105 connect(m_searchBox, SIGNAL(returnPressed(QString)), this, SLOT(requestFocus()));
106
107 m_dirLister = new DolphinDirLister();
108 m_dirLister->setAutoUpdate(true);
109 m_dirLister->setMainWindow(window());
110 m_dirLister->setDelayedMimeTypes(true);
111
112 m_dolphinModel = new DolphinModel(this);
113 m_dolphinModel->setDirLister(m_dirLister); // m_dolphinModel takes ownership of m_dirLister
114 m_dolphinModel->setDropsAllowed(DolphinModel::DropOnDirectory);
115
116 m_proxyModel = new DolphinSortFilterProxyModel(this);
117 m_proxyModel->setSourceModel(m_dolphinModel);
118 m_proxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
119
120 // TODO: In the case of the column view the directory lister changes. Let the DolphinView
121 // inform the container about this information for KDE SC 4.7
122 connect(m_dirLister, SIGNAL(clear()),
123 this, SLOT(delayedStatusBarUpdate()));
124 connect(m_dirLister, SIGNAL(percent(int)),
125 this, SLOT(updateProgress(int)));
126 connect(m_dirLister, SIGNAL(itemsDeleted(const KFileItemList&)),
127 this, SLOT(delayedStatusBarUpdate()));
128 connect(m_dirLister, SIGNAL(newItems(KFileItemList)),
129 this, SLOT(delayedStatusBarUpdate()));
130 connect(m_dirLister, SIGNAL(infoMessage(const QString&)),
131 this, SLOT(showInfoMessage(const QString&)));
132 connect(m_dirLister, SIGNAL(errorMessage(const QString&)),
133 this, SLOT(showErrorMessage(const QString&)));
134 connect(m_dirLister, SIGNAL(urlIsFileError(const KUrl&)),
135 this, SLOT(openFile(const KUrl&)));
136
137 m_view = new DolphinView(this, url, m_proxyModel);
138 connect(m_view, SIGNAL(urlChanged(const KUrl&)),
139 m_urlNavigator, SLOT(setUrl(const KUrl&)));
140 connect(m_view, SIGNAL(requestItemInfo(KFileItem)),
141 this, SLOT(showItemInfo(KFileItem)));
142 connect(m_view, SIGNAL(errorMessage(const QString&)),
143 this, SLOT(showErrorMessage(const QString&)));
144 connect(m_view, SIGNAL(infoMessage(const QString&)),
145 this, SLOT(showInfoMessage(const QString&)));
146 connect(m_view, SIGNAL(operationCompletedMessage(const QString&)),
147 this, SLOT(showOperationCompletedMessage(const QString&)));
148 connect(m_view, SIGNAL(itemTriggered(KFileItem)),
149 this, SLOT(slotItemTriggered(KFileItem)));
150 connect(m_view, SIGNAL(redirection(KUrl, KUrl)),
151 this, SLOT(redirect(KUrl, KUrl)));
152 connect(m_view, SIGNAL(selectionChanged(const KFileItemList&)),
153 this, SLOT(delayedStatusBarUpdate()));
154 connect(m_view, SIGNAL(startedPathLoading(KUrl)),
155 this, SLOT(slotStartedPathLoading()));
156 connect(m_view, SIGNAL(finishedPathLoading(KUrl)),
157 this, SLOT(slotFinishedPathLoading()));
158
159 connect(m_urlNavigator, SIGNAL(urlChanged(const KUrl&)),
160 this, SLOT(slotUrlNavigatorLocationChanged(const KUrl&)));
161 connect(m_urlNavigator, SIGNAL(urlAboutToBeChanged(const KUrl&)),
162 this, SLOT(saveViewState()));
163 connect(m_urlNavigator, SIGNAL(historyChanged()),
164 this, SLOT(slotHistoryChanged()));
165
166 // initialize status bar
167 m_statusBar = new DolphinStatusBar(this, m_view);
168 connect(m_statusBar, SIGNAL(stopPressed()), this, SLOT(stopLoading()));
169
170 m_statusBarTimer = new QTimer(this);
171 m_statusBarTimer->setSingleShot(true);
172 m_statusBarTimer->setInterval(300);
173 connect(m_statusBarTimer, SIGNAL(timeout()),
174 this, SLOT(updateStatusBar()));
175
176 KIO::FileUndoManager* undoManager = KIO::FileUndoManager::self();
177 connect(undoManager, SIGNAL(jobRecordingFinished(CommandType)),
178 this, SLOT(delayedStatusBarUpdate()));
179
180 // initialize filter bar
181 m_filterBar = new FilterBar(this);
182 m_filterBar->setVisible(settings->filterBar());
183 connect(m_filterBar, SIGNAL(filterChanged(const QString&)),
184 this, SLOT(setNameFilter(const QString&)));
185 connect(m_filterBar, SIGNAL(closeRequest()),
186 this, SLOT(closeFilterBar()));
187 connect(m_view, SIGNAL(urlChanged(const KUrl&)),
188 m_filterBar, SLOT(clear()));
189
190 m_topLayout->addWidget(m_urlNavigator);
191 m_topLayout->addWidget(m_searchBox);
192 m_topLayout->addWidget(m_view);
193 m_topLayout->addWidget(m_filterBar);
194 m_topLayout->addWidget(m_statusBar);
195
196 setSearchModeEnabled(isSearchUrl(url));
197 }
198
199 DolphinViewContainer::~DolphinViewContainer()
200 {
201 }
202
203 KUrl DolphinViewContainer::url() const
204 {
205 return m_urlNavigator->locationUrl();
206 }
207
208 void DolphinViewContainer::setActive(bool active)
209 {
210 m_urlNavigator->setActive(active);
211 m_view->setActive(active);
212 if (active) {
213 emit writeStateChanged(m_isFolderWritable);
214 }
215 }
216
217 bool DolphinViewContainer::isActive() const
218 {
219 Q_ASSERT(m_view->isActive() == m_urlNavigator->isActive());
220 return m_view->isActive();
221 }
222
223 void DolphinViewContainer::refresh()
224 {
225 GeneralSettings* settings = DolphinSettings::instance().generalSettings();
226 if (settings->modifiedStartupSettings()) {
227 // The startup settings should only get applied if they have been
228 // modified by the user. Otherwise keep the (possibly) different current
229 // settings of the URL navigator and the filterbar.
230 m_urlNavigator->setUrlEditable(settings->editableUrl());
231 m_urlNavigator->setShowFullPath(settings->showFullPath());
232 setFilterBarVisible(settings->filterBar());
233 }
234
235 m_view->refresh();
236 m_statusBar->refresh();
237 }
238
239 bool DolphinViewContainer::isFilterBarVisible() const
240 {
241 return m_filterBar->isVisible();
242 }
243
244 void DolphinViewContainer::setSearchModeEnabled(bool enabled)
245 {
246 if (enabled == isSearchModeEnabled()) {
247 return;
248 }
249
250 m_searchBox->setVisible(enabled);
251 m_urlNavigator->setVisible(!enabled);
252
253 if (enabled) {
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 KUrl url = m_urlNavigator->locationUrl();
258
259 int index = m_urlNavigator->historyIndex();
260 const int historySize = m_urlNavigator->historySize();
261 while (isSearchUrl(url) && (index < historySize)) {
262 ++index;
263 url = m_urlNavigator->locationUrl(index);
264 }
265
266 if (!isSearchUrl(url)) {
267 m_searchBox->setSearchPath(url);
268 }
269 } else {
270 // Restore the URL for the URL navigator. If Dolphin has been
271 // started with a search-URL, the home URL is used as fallback.
272 const KUrl url = m_searchBox->searchPath();
273 if (url.isValid() && !url.isEmpty()) {
274 if (isSearchUrl(url)) {
275 m_urlNavigator->goHome();
276 } else {
277 m_urlNavigator->setLocationUrl(url);
278 }
279 }
280 }
281
282 emit searchModeChanged(enabled);
283 }
284
285 bool DolphinViewContainer::isSearchModeEnabled() const
286 {
287 return m_searchBox->isVisible();
288 }
289
290 void DolphinViewContainer::setUrl(const KUrl& newUrl)
291 {
292 if (newUrl != m_urlNavigator->locationUrl()) {
293 m_urlNavigator->setLocationUrl(newUrl);
294
295 // Temporary disable the 'File'->'Create New...' menu until
296 // the write permissions can be checked in a fast way at
297 // DolphinViewContainer::slotDirListerCompleted().
298 m_isFolderWritable = false;
299 if (isActive()) {
300 emit writeStateChanged(false);
301 }
302 }
303 }
304
305 void DolphinViewContainer::setFilterBarVisible(bool visible)
306 {
307 Q_ASSERT(m_filterBar != 0);
308 if (visible) {
309 m_filterBar->show();
310 m_filterBar->setFocus();
311 } else {
312 closeFilterBar();
313 }
314 }
315
316 void DolphinViewContainer::delayedStatusBarUpdate()
317 {
318 if (m_statusBarTimer->isActive() && (m_statusBarTimestamp.elapsed() > 2000)) {
319 // No update of the statusbar has been done during the last 2 seconds,
320 // although an update has been requested. Trigger an immediate update.
321 m_statusBarTimer->stop();
322 updateStatusBar();
323 } else {
324 // Invoke updateStatusBar() with a small delay. This assures that
325 // when a lot of delayedStatusBarUpdates() are done in a short time,
326 // no bottleneck is given.
327 m_statusBarTimer->start();
328 }
329 }
330
331 void DolphinViewContainer::updateStatusBar()
332 {
333 m_statusBarTimestamp.start();
334
335 // As the item count information is less important
336 // in comparison with other messages, it should only
337 // be shown if:
338 // - the status bar is empty or
339 // - shows already the item count information or
340 // - shows only a not very important information
341 const QString newMessage = m_view->statusBarText();
342 const QString currentMessage = m_statusBar->message();
343 const bool updateStatusBarMsg = currentMessage.isEmpty()
344 || (currentMessage == m_statusBar->defaultText())
345 || (m_statusBar->type() == DolphinStatusBar::Information);
346
347 m_statusBar->setDefaultText(newMessage);
348
349 if (updateStatusBarMsg) {
350 m_statusBar->setMessage(newMessage, DolphinStatusBar::Default);
351 }
352 }
353
354 void DolphinViewContainer::updateProgress(int percent)
355 {
356 if (m_statusBar->progressText().isEmpty()) {
357 m_statusBar->setProgressText(i18nc("@info:progress", "Loading folder..."));
358 }
359 m_statusBar->setProgress(percent);
360 }
361
362 void DolphinViewContainer::slotStartedPathLoading()
363 {
364 if (isSearchUrl(url())) {
365 // Search KIO-slaves usually don't provide any progress information. Give
366 // a hint to the user that a searching is done:
367 updateStatusBar();
368 m_statusBar->setProgressText(i18nc("@info", "Searching..."));
369 m_statusBar->setProgress(-1);
370 } else {
371 // Trigger an undetermined progress indication. The progress
372 // information in percent will be triggered by the percent() signal
373 // of the directory lister later.
374 updateProgress(-1);
375 }
376 }
377
378 void DolphinViewContainer::slotFinishedPathLoading()
379 {
380 if (!m_statusBar->progressText().isEmpty()) {
381 m_statusBar->setProgressText(QString());
382 m_statusBar->setProgress(100);
383 }
384
385 if (isSearchUrl(url()) && (m_dirLister->items().count() == 0)) {
386 // The dir lister has been completed on a Nepomuk-URI and no items have been found. Instead
387 // of showing the default status bar information ("0 items") a more helpful information is given:
388 m_statusBar->setMessage(i18nc("@info:status", "No items found."), DolphinStatusBar::Information);
389 } else {
390 updateStatusBar();
391 }
392
393 // Enable the 'File'->'Create New...' menu only if the directory
394 // supports writing.
395 KFileItem item = m_dirLister->rootItem();
396 if (item.isNull()) {
397 // it is unclear whether writing is supported
398 m_isFolderWritable = true;
399 } else {
400 KFileItemListProperties capabilities(KFileItemList() << item);
401 m_isFolderWritable = capabilities.supportsWriting();
402 }
403
404 if (isActive()) {
405 emit writeStateChanged(m_isFolderWritable);
406 }
407 }
408
409 void DolphinViewContainer::showItemInfo(const KFileItem& item)
410 {
411 if (item.isNull()) {
412 // Only clear the status bar if unimportant messages are shown.
413 // This prevents that information- or error-messages get hidden
414 // by moving the mouse above the viewport or when closing the
415 // context menu.
416 if (m_statusBar->type() == DolphinStatusBar::Default) {
417 m_statusBar->clear();
418 }
419 } else {
420 m_statusBar->setMessage(item.getStatusBarInfo(), DolphinStatusBar::Default);
421 }
422 }
423
424 void DolphinViewContainer::showInfoMessage(const QString& msg)
425 {
426 m_statusBar->setMessage(msg, DolphinStatusBar::Information);
427 }
428
429 void DolphinViewContainer::showErrorMessage(const QString& msg)
430 {
431 m_statusBar->setMessage(msg, DolphinStatusBar::Error);
432 }
433
434 void DolphinViewContainer::showOperationCompletedMessage(const QString& msg)
435 {
436 m_statusBar->setMessage(msg, DolphinStatusBar::OperationCompleted);
437 }
438
439 void DolphinViewContainer::closeFilterBar()
440 {
441 m_filterBar->hide();
442 m_filterBar->clear();
443 m_view->setFocus();
444 emit showFilterBarChanged(false);
445 }
446
447 void DolphinViewContainer::setNameFilter(const QString& nameFilter)
448 {
449 m_view->setNameFilter(nameFilter);
450 delayedStatusBarUpdate();
451 }
452
453 void DolphinViewContainer::activate()
454 {
455 setActive(true);
456 }
457
458 void DolphinViewContainer::saveViewState()
459 {
460 QByteArray locationState;
461 QDataStream stream(&locationState, QIODevice::WriteOnly);
462 m_view->saveState(stream);
463 m_urlNavigator->saveLocationState(locationState);
464 }
465
466 void DolphinViewContainer::slotUrlNavigatorLocationChanged(const KUrl& url)
467 {
468 if (KProtocolManager::supportsListing(url)) {
469 setSearchModeEnabled(isSearchUrl(url));
470
471 m_view->setUrl(url);
472 if (isActive() && !isSearchUrl(url)) {
473 // When an URL has been entered, the view should get the focus.
474 // The focus must be requested asynchronously, as changing the URL might create
475 // a new view widget.
476 QTimer::singleShot(0, this, SLOT(requestFocus()));
477 }
478 } else if (KProtocolManager::isSourceProtocol(url)) {
479 QString app = "konqueror";
480 if (url.protocol().startsWith(QLatin1String("http"))) {
481 showErrorMessage(i18nc("@info:status",
482 "Dolphin does not support web pages, the web browser has been launched"));
483 const KConfigGroup config(KSharedConfig::openConfig("kdeglobals"), "General");
484 const QString browser = config.readEntry("BrowserApplication");
485 if (!browser.isEmpty()) {
486 app = browser;
487 if (app.startsWith('!')) {
488 // a literal command has been configured, remove the '!' prefix
489 app = app.mid(1);
490 }
491 }
492 } else {
493 showErrorMessage(i18nc("@info:status",
494 "Protocol not supported by Dolphin, Konqueror has been launched"));
495 }
496
497 const QString secureUrl = KShell::quoteArg(url.pathOrUrl());
498 const QString command = app + ' ' + secureUrl;
499 KRun::runCommand(command, app, app, this);
500 } else {
501 showErrorMessage(i18nc("@info:status", "Invalid protocol"));
502 }
503 }
504
505 void DolphinViewContainer::dropUrls(const KUrl& destination, QDropEvent* event)
506 {
507 DragAndDropHelper::instance().dropUrls(KFileItem(), destination, event, this);
508 }
509
510 void DolphinViewContainer::redirect(const KUrl& oldUrl, const KUrl& newUrl)
511 {
512 Q_UNUSED(oldUrl);
513 const bool block = m_urlNavigator->signalsBlocked();
514 m_urlNavigator->blockSignals(true);
515
516 // Assure that the location state is reset for redirection URLs. This
517 // allows to skip redirection URLs when going back or forward in the
518 // URL history.
519 m_urlNavigator->saveLocationState(QByteArray());
520 m_urlNavigator->setLocationUrl(newUrl);
521 setSearchModeEnabled(isSearchUrl(newUrl));
522
523 m_urlNavigator->blockSignals(block);
524 }
525
526 void DolphinViewContainer::requestFocus()
527 {
528 m_view->setFocus();
529 }
530
531 void DolphinViewContainer::saveUrlCompletionMode(KGlobalSettings::Completion completion)
532 {
533 DolphinSettings& settings = DolphinSettings::instance();
534 settings.generalSettings()->setUrlCompletionMode(completion);
535 settings.save();
536 }
537
538 void DolphinViewContainer::slotHistoryChanged()
539 {
540 QByteArray locationState = m_urlNavigator->locationState();
541
542 if (!locationState.isEmpty()) {
543 QDataStream stream(&locationState, QIODevice::ReadOnly);
544 m_view->restoreState(stream);
545 }
546 }
547
548 void DolphinViewContainer::startSearching(const QString &text)
549 {
550 Q_UNUSED(text);
551 const KUrl url = m_searchBox->urlForSearching();
552 if (url.isValid() && !url.isEmpty()) {
553 m_urlNavigator->setLocationUrl(url);
554 }
555 }
556
557 void DolphinViewContainer::closeSearchBox()
558 {
559 setSearchModeEnabled(false);
560 }
561
562 void DolphinViewContainer::stopLoading()
563 {
564 m_view->stopLoading();
565 }
566
567 bool DolphinViewContainer::isSearchUrl(const KUrl& url) const
568 {
569 const QString protocol = url.protocol();
570 return protocol.contains("search") || (protocol == QLatin1String("nepomuk"));
571 }
572
573 void DolphinViewContainer::slotItemTriggered(const KFileItem& item)
574 {
575 KUrl url = item.targetUrl();
576
577 if (item.isDir()) {
578 m_view->setUrl(url);
579 return;
580 }
581
582 const GeneralSettings* settings = DolphinSettings::instance().generalSettings();
583 const bool browseThroughArchives = settings->browseThroughArchives();
584 if (browseThroughArchives && item.isFile() && url.isLocalFile()) {
585 // Generic mechanism for redirecting to tar:/<path>/ when clicking on a tar file,
586 // zip:/<path>/ when clicking on a zip file, etc.
587 // The .protocol file specifies the mimetype that the kioslave handles.
588 // Note that we don't use mimetype inheritance since we don't want to
589 // open OpenDocument files as zip folders...
590 const QString protocol = KProtocolManager::protocolForArchiveMimetype(item.mimetype());
591 if (!protocol.isEmpty()) {
592 url.setProtocol(protocol);
593 m_view->setUrl(url);
594 return;
595 }
596 }
597
598 if (item.mimetype() == "application/x-desktop") {
599 // redirect to the url in Type=Link desktop files
600 KDesktopFile desktopFile(url.toLocalFile());
601 if (desktopFile.hasLinkType()) {
602 url = desktopFile.readUrl();
603 m_view->setUrl(url);
604 return;
605 }
606 }
607
608 item.run();
609 }
610
611 void DolphinViewContainer::openFile(const KUrl& url)
612 {
613 // Using m_dolphinModel for getting the file item instance is not possible
614 // here: openFile() is triggered by an error of the directory lister
615 // job, so the file item must be received "manually".
616 const KFileItem item(KFileItem::Unknown, KFileItem::Unknown, url);
617 slotItemTriggered(item);
618 }
619
620 #include "dolphinviewcontainer.moc"