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