]> 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 connect(m_dirLister, SIGNAL(started(KUrl)),
121 this, SLOT(initializeProgress()));
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(completed()),
131 this, SLOT(slotDirListerCompleted()));
132 connect(m_dirLister, SIGNAL(infoMessage(const QString&)),
133 this, SLOT(showInfoMessage(const QString&)));
134 connect(m_dirLister, SIGNAL(errorMessage(const QString&)),
135 this, SLOT(showErrorMessage(const QString&)));
136 connect(m_dirLister, SIGNAL(urlIsFileError(const KUrl&)),
137 this, SLOT(openFile(const KUrl&)));
138
139 m_view = new DolphinView(this, url, m_proxyModel);
140 connect(m_view, SIGNAL(urlChanged(const KUrl&)),
141 m_urlNavigator, SLOT(setUrl(const KUrl&)));
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(redirection(KUrl, KUrl)),
153 this, SLOT(redirect(KUrl, KUrl)));
154 connect(m_view, SIGNAL(selectionChanged(const KFileItemList&)),
155 this, SLOT(delayedStatusBarUpdate()));
156
157 connect(m_urlNavigator, SIGNAL(urlChanged(const KUrl&)),
158 this, SLOT(slotUrlNavigatorLocationChanged(const KUrl&)));
159 connect(m_urlNavigator, SIGNAL(urlAboutToBeChanged(const KUrl&)),
160 this, SLOT(saveViewState()));
161 connect(m_urlNavigator, SIGNAL(historyChanged()),
162 this, SLOT(slotHistoryChanged()));
163
164 // initialize status bar
165 m_statusBar = new DolphinStatusBar(this, m_view);
166 connect(m_statusBar, SIGNAL(stopPressed()), this, SLOT(stopLoading()));
167
168 m_statusBarTimer = new QTimer(this);
169 m_statusBarTimer->setSingleShot(true);
170 m_statusBarTimer->setInterval(300);
171 connect(m_statusBarTimer, SIGNAL(timeout()),
172 this, SLOT(updateStatusBar()));
173
174 KIO::FileUndoManager* undoManager = KIO::FileUndoManager::self();
175 connect(undoManager, SIGNAL(jobRecordingFinished(CommandType)),
176 this, SLOT(delayedStatusBarUpdate()));
177
178 // initialize filter bar
179 m_filterBar = new FilterBar(this);
180 m_filterBar->setVisible(settings->filterBar());
181 connect(m_filterBar, SIGNAL(filterChanged(const QString&)),
182 this, SLOT(setNameFilter(const QString&)));
183 connect(m_filterBar, SIGNAL(closeRequest()),
184 this, SLOT(closeFilterBar()));
185 connect(m_view, SIGNAL(urlChanged(const KUrl&)),
186 m_filterBar, SLOT(clear()));
187
188 m_topLayout->addWidget(m_urlNavigator);
189 m_topLayout->addWidget(m_searchBox);
190 m_topLayout->addWidget(m_view);
191 m_topLayout->addWidget(m_filterBar);
192 m_topLayout->addWidget(m_statusBar);
193
194 setSearchModeEnabled(isSearchUrl(url));
195 }
196
197 DolphinViewContainer::~DolphinViewContainer()
198 {
199 }
200
201 KUrl DolphinViewContainer::url() const
202 {
203 return m_urlNavigator->locationUrl();
204 }
205
206 void DolphinViewContainer::setActive(bool active)
207 {
208 m_urlNavigator->setActive(active);
209 m_view->setActive(active);
210 if (active) {
211 emit writeStateChanged(m_isFolderWritable);
212 }
213 }
214
215 bool DolphinViewContainer::isActive() const
216 {
217 Q_ASSERT(m_view->isActive() == m_urlNavigator->isActive());
218 return m_view->isActive();
219 }
220
221 void DolphinViewContainer::refresh()
222 {
223 GeneralSettings* settings = DolphinSettings::instance().generalSettings();
224 if (settings->modifiedStartupSettings()) {
225 // The startup settings should only get applied if they have been
226 // modified by the user. Otherwise keep the (possibly) different current
227 // settings of the URL navigator and the filterbar.
228 m_urlNavigator->setUrlEditable(settings->editableUrl());
229 m_urlNavigator->setShowFullPath(settings->showFullPath());
230 setFilterBarVisible(settings->filterBar());
231 }
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::setSearchModeEnabled(bool enabled)
243 {
244 if (enabled == isSearchModeEnabled()) {
245 return;
246 }
247
248 m_searchBox->setVisible(enabled);
249 m_urlNavigator->setVisible(!enabled);
250
251 if (enabled) {
252 // Remember the most recent non-search URL as search path
253 // of the search-box, so that it can be restored
254 // when switching back to the URL navigator.
255 KUrl url = m_urlNavigator->locationUrl();
256
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 // Temporary disable the 'File'->'Create New...' menu until
294 // the write permissions can be checked in a fast way at
295 // DolphinViewContainer::slotDirListerCompleted().
296 m_isFolderWritable = false;
297 if (isActive()) {
298 emit writeStateChanged(false);
299 }
300
301 // Trigger an undetermined progress indication. The progress
302 // information in percent will be triggered by the percent() signal
303 // of the directory lister later.
304 updateProgress(-1);
305 }
306 }
307
308 void DolphinViewContainer::setFilterBarVisible(bool visible)
309 {
310 Q_ASSERT(m_filterBar != 0);
311 if (visible) {
312 m_filterBar->show();
313 m_filterBar->setFocus();
314 } else {
315 closeFilterBar();
316 }
317 }
318
319 void DolphinViewContainer::delayedStatusBarUpdate()
320 {
321 if (m_statusBarTimer->isActive() && (m_statusBarTimestamp.elapsed() > 2000)) {
322 // No update of the statusbar has been done during the last 2 seconds,
323 // although an update has been requested. Trigger an immediate update.
324 m_statusBarTimer->stop();
325 updateStatusBar();
326 } else {
327 // Invoke updateStatusBar() with a small delay. This assures that
328 // when a lot of delayedStatusBarUpdates() are done in a short time,
329 // no bottleneck is given.
330 m_statusBarTimer->start();
331 }
332 }
333
334 void DolphinViewContainer::updateStatusBar()
335 {
336 m_statusBarTimestamp.start();
337
338 // As the item count information is less important
339 // in comparison with other messages, it should only
340 // be shown if:
341 // - the status bar is empty or
342 // - shows already the item count information or
343 // - shows only a not very important information
344 const QString newMessage = m_view->statusBarText();
345 const QString currentMessage = m_statusBar->message();
346 const bool updateStatusBarMsg = currentMessage.isEmpty()
347 || (currentMessage == m_statusBar->defaultText())
348 || (m_statusBar->type() == DolphinStatusBar::Information);
349
350 m_statusBar->setDefaultText(newMessage);
351
352 if (updateStatusBarMsg) {
353 m_statusBar->setMessage(newMessage, DolphinStatusBar::Default);
354 }
355 }
356
357 void DolphinViewContainer::initializeProgress()
358 {
359 if (isSearchUrl(url())) {
360 // Search KIO-slaves usually don't provide any progress information. Give
361 // an immediate hint to the user that a searching is done:
362 updateStatusBar();
363 m_statusBar->setProgressText(i18nc("@info", "Searching..."));
364 m_statusBar->setProgress(-1);
365 }
366 }
367
368 void DolphinViewContainer::updateProgress(int percent)
369 {
370 if (m_statusBar->progressText().isEmpty()) {
371 m_statusBar->setProgressText(i18nc("@info:progress", "Loading folder..."));
372 }
373 m_statusBar->setProgress(percent);
374 }
375
376 void DolphinViewContainer::slotDirListerCompleted()
377 {
378 if (!m_statusBar->progressText().isEmpty()) {
379 m_statusBar->setProgressText(QString());
380 m_statusBar->setProgress(100);
381 }
382
383 if (isSearchUrl(url()) && (m_dirLister->items().count() == 0)) {
384 // The dir lister has been completed on a Nepomuk-URI and no items have been found. Instead
385 // of showing the default status bar information ("0 items") a more helpful information is given:
386 m_statusBar->setMessage(i18nc("@info:status", "No items found."), DolphinStatusBar::Information);
387 } else {
388 updateStatusBar();
389 }
390
391 // Enable the 'File'->'Create New...' menu only if the directory
392 // supports writing.
393 KFileItem item = m_dirLister->rootItem();
394 if (item.isNull()) {
395 // it is unclear whether writing is supported
396 m_isFolderWritable = true;
397 } else {
398 KFileItemListProperties capabilities(KFileItemList() << item);
399 m_isFolderWritable = capabilities.supportsWriting();
400 }
401
402 if (isActive()) {
403 emit writeStateChanged(m_isFolderWritable);
404 }
405 }
406
407 void DolphinViewContainer::showItemInfo(const KFileItem& item)
408 {
409 if (item.isNull()) {
410 // Only clear the status bar if unimportant messages are shown.
411 // This prevents that information- or error-messages get hidden
412 // by moving the mouse above the viewport or when closing the
413 // context menu.
414 if (m_statusBar->type() == DolphinStatusBar::Default) {
415 m_statusBar->clear();
416 }
417 } else {
418 m_statusBar->setMessage(item.getStatusBarInfo(), DolphinStatusBar::Default);
419 }
420 }
421
422 void DolphinViewContainer::showInfoMessage(const QString& msg)
423 {
424 m_statusBar->setMessage(msg, DolphinStatusBar::Information);
425 }
426
427 void DolphinViewContainer::showErrorMessage(const QString& msg)
428 {
429 m_statusBar->setMessage(msg, DolphinStatusBar::Error);
430 }
431
432 void DolphinViewContainer::showOperationCompletedMessage(const QString& msg)
433 {
434 m_statusBar->setMessage(msg, DolphinStatusBar::OperationCompleted);
435 }
436
437 void DolphinViewContainer::closeFilterBar()
438 {
439 m_filterBar->hide();
440 m_filterBar->clear();
441 m_view->setFocus();
442 emit showFilterBarChanged(false);
443 }
444
445 void DolphinViewContainer::setNameFilter(const QString& nameFilter)
446 {
447 m_view->setNameFilter(nameFilter);
448 delayedStatusBarUpdate();
449 }
450
451 void DolphinViewContainer::activate()
452 {
453 setActive(true);
454 }
455
456 void DolphinViewContainer::saveViewState()
457 {
458 QByteArray locationState;
459 QDataStream stream(&locationState, QIODevice::WriteOnly);
460 m_view->saveState(stream);
461 m_urlNavigator->saveLocationState(locationState);
462 }
463
464 void DolphinViewContainer::slotUrlNavigatorLocationChanged(const KUrl& url)
465 {
466 if (KProtocolManager::supportsListing(url)) {
467 setSearchModeEnabled(isSearchUrl(url));
468
469 m_view->setUrl(url);
470 if (isActive() && !isSearchUrl(url)) {
471 // When an URL has been entered, the view should get the focus.
472 // The focus must be requested asynchronously, as changing the URL might create
473 // a new view widget.
474 QTimer::singleShot(0, this, SLOT(requestFocus()));
475 }
476 } else if (KProtocolManager::isSourceProtocol(url)) {
477 QString app = "konqueror";
478 if (url.protocol().startsWith(QLatin1String("http"))) {
479 showErrorMessage(i18nc("@info:status",
480 "Dolphin does not support web pages, the web browser has been launched"));
481 const KConfigGroup config(KSharedConfig::openConfig("kdeglobals"), "General");
482 const QString browser = config.readEntry("BrowserApplication");
483 if (!browser.isEmpty()) {
484 app = browser;
485 if (app.startsWith('!')) {
486 // a literal command has been configured, remove the '!' prefix
487 app = app.mid(1);
488 }
489 }
490 } else {
491 showErrorMessage(i18nc("@info:status",
492 "Protocol not supported by Dolphin, Konqueror has been launched"));
493 }
494
495 const QString secureUrl = KShell::quoteArg(url.pathOrUrl());
496 const QString command = app + ' ' + secureUrl;
497 KRun::runCommand(command, app, app, this);
498 } else {
499 showErrorMessage(i18nc("@info:status", "Invalid protocol"));
500 }
501 }
502
503 void DolphinViewContainer::dropUrls(const KUrl& destination, QDropEvent* event)
504 {
505 DragAndDropHelper::instance().dropUrls(KFileItem(), destination, event, this);
506 }
507
508 void DolphinViewContainer::redirect(const KUrl& oldUrl, const KUrl& newUrl)
509 {
510 Q_UNUSED(oldUrl);
511 const bool block = m_urlNavigator->signalsBlocked();
512 m_urlNavigator->blockSignals(true);
513
514 // Assure that the location state is reset for redirection URLs. This
515 // allows to skip redirection URLs when going back or forward in the
516 // URL history.
517 m_urlNavigator->saveLocationState(QByteArray());
518 m_urlNavigator->setLocationUrl(newUrl);
519 setSearchModeEnabled(isSearchUrl(newUrl));
520
521 m_urlNavigator->blockSignals(block);
522 }
523
524 void DolphinViewContainer::requestFocus()
525 {
526 m_view->setFocus();
527 }
528
529 void DolphinViewContainer::saveUrlCompletionMode(KGlobalSettings::Completion completion)
530 {
531 DolphinSettings& settings = DolphinSettings::instance();
532 settings.generalSettings()->setUrlCompletionMode(completion);
533 settings.save();
534 }
535
536 void DolphinViewContainer::slotHistoryChanged()
537 {
538 QByteArray locationState = m_urlNavigator->locationState();
539
540 if (!locationState.isEmpty()) {
541 QDataStream stream(&locationState, QIODevice::ReadOnly);
542 m_view->restoreState(stream);
543 }
544 }
545
546 void DolphinViewContainer::startSearching(const QString &text)
547 {
548 Q_UNUSED(text);
549 const KUrl url = m_searchBox->urlForSearching();
550 if (url.isValid() && !url.isEmpty()) {
551 m_urlNavigator->setLocationUrl(url);
552 }
553 }
554
555 void DolphinViewContainer::closeSearchBox()
556 {
557 setSearchModeEnabled(false);
558 }
559
560 void DolphinViewContainer::stopLoading()
561 {
562 m_view->stopLoading();
563 }
564
565 bool DolphinViewContainer::isSearchUrl(const KUrl& url) const
566 {
567 const QString protocol = url.protocol();
568 return protocol.contains("search") || (protocol == QLatin1String("nepomuk"));
569 }
570
571 void DolphinViewContainer::slotItemTriggered(const KFileItem& item)
572 {
573 KUrl url = item.targetUrl();
574
575 if (item.isDir()) {
576 m_view->setUrl(url);
577 return;
578 }
579
580 const GeneralSettings* settings = DolphinSettings::instance().generalSettings();
581 const bool browseThroughArchives = settings->browseThroughArchives();
582 if (browseThroughArchives && item.isFile() && url.isLocalFile()) {
583 // Generic mechanism for redirecting to tar:/<path>/ when clicking on a tar file,
584 // zip:/<path>/ when clicking on a zip file, etc.
585 // The .protocol file specifies the mimetype that the kioslave handles.
586 // Note that we don't use mimetype inheritance since we don't want to
587 // open OpenDocument files as zip folders...
588 const QString protocol = KProtocolManager::protocolForArchiveMimetype(item.mimetype());
589 if (!protocol.isEmpty()) {
590 url.setProtocol(protocol);
591 m_view->setUrl(url);
592 return;
593 }
594 }
595
596 if (item.mimetype() == "application/x-desktop") {
597 // redirect to the url in Type=Link desktop files
598 KDesktopFile desktopFile(url.toLocalFile());
599 if (desktopFile.hasLinkType()) {
600 url = desktopFile.readUrl();
601 m_view->setUrl(url);
602 return;
603 }
604 }
605
606 item.run();
607 }
608
609 void DolphinViewContainer::openFile(const KUrl& url)
610 {
611 // Using m_dolphinModel for getting the file item instance is not possible
612 // here: openFile() is triggered by an error of the directory lister
613 // job, so the file item must be received "manually".
614 const KFileItem item(KFileItem::Unknown, KFileItem::Unknown, url);
615 slotItemTriggered(item);
616 }
617
618 #include "dolphinviewcontainer.moc"