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