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