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