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