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