]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinviewcontainer.cpp
Fix triggering of an assertion, if a new tab is opened. Thanks to Frank Reininghaus...
[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()) {
249 if (isSearchUrl(url)) {
250 m_urlNavigator->goHome();
251 } else {
252 m_urlNavigator->setLocationUrl(url);
253 }
254 }
255 }
256 }
257
258 bool DolphinViewContainer::isSearchModeEnabled() const
259 {
260 return m_searchBox->isVisible();
261 }
262
263 void DolphinViewContainer::setUrl(const KUrl& newUrl)
264 {
265 if (newUrl != m_urlNavigator->locationUrl()) {
266 m_urlNavigator->setLocationUrl(newUrl);
267 // Temporary disable the 'File'->'Create New...' menu until
268 // the write permissions can be checked in a fast way at
269 // DolphinViewContainer::slotDirListerCompleted().
270 m_isFolderWritable = false;
271 if (isActive()) {
272 emit writeStateChanged(false);
273 }
274 }
275 }
276
277 void DolphinViewContainer::showFilterBar(bool show)
278 {
279 Q_ASSERT(m_filterBar != 0);
280 if (show) {
281 m_filterBar->show();
282 } else {
283 closeFilterBar();
284 }
285 }
286
287 void DolphinViewContainer::delayedStatusBarUpdate()
288 {
289 // Invoke updateStatusBar() with a small delay. This assures that
290 // when a lot of delayedStatusBarUpdates() are done in a short time,
291 // no bottleneck is given.
292 m_statusBarTimer->start();
293 }
294
295 void DolphinViewContainer::updateStatusBar()
296 {
297 // As the item count information is less important
298 // in comparison with other messages, it should only
299 // be shown if:
300 // - the status bar is empty or
301 // - shows already the item count information or
302 // - shows only a not very important information
303 // - if any progress is given don't show the item count info at all
304 const QString msg = m_statusBar->message();
305 const bool updateStatusBarMsg = (msg.isEmpty()
306 || (msg == m_statusBar->defaultText())
307 || (m_statusBar->type() == DolphinStatusBar::Information))
308 && (m_statusBar->progress() == 100);
309
310 const QString text = m_view->statusBarText();
311 m_statusBar->setDefaultText(text);
312
313 if (updateStatusBarMsg) {
314 m_statusBar->setMessage(text, DolphinStatusBar::Default);
315 }
316 }
317
318 void DolphinViewContainer::initializeProgress()
319 {
320 if (isSearchUrl(url())) {
321 // Search KIO-slaves usually don't provide any progress information. Give
322 // an immediate hint to the user that a searching is done:
323 updateStatusBar();
324 m_statusBar->setProgressText(i18nc("@info", "Searching..."));
325 m_statusBar->setProgress(-1);
326 }
327 }
328
329 void DolphinViewContainer::updateProgress(int percent)
330 {
331 if (m_statusBar->progressText().isEmpty()) {
332 m_statusBar->setProgressText(i18nc("@info:progress", "Loading folder..."));
333 }
334 m_statusBar->setProgress(percent);
335 }
336
337 void DolphinViewContainer::slotDirListerCompleted()
338 {
339 if (!m_statusBar->progressText().isEmpty()) {
340 m_statusBar->setProgressText(QString());
341 m_statusBar->setProgress(100);
342 }
343
344 if (isSearchUrl(url()) && (m_dirLister->items().count() == 0)) {
345 // The dir lister has been completed on a Nepomuk-URI and no items have been found. Instead
346 // of showing the default status bar information ("0 items") a more helpful information is given:
347 m_statusBar->setMessage(i18nc("@info:status", "No items found."), DolphinStatusBar::Information);
348 } else {
349 updateStatusBar();
350 }
351
352 // Enable the 'File'->'Create New...' menu only if the directory
353 // supports writing.
354 KFileItem item = m_dirLister->rootItem();
355 if (item.isNull()) {
356 // it is unclear whether writing is supported
357 m_isFolderWritable = true;
358 } else {
359 KFileItemListProperties capabilities(KFileItemList() << item);
360 m_isFolderWritable = capabilities.supportsWriting();
361 }
362
363 if (isActive()) {
364 emit writeStateChanged(m_isFolderWritable);
365 }
366 }
367
368 void DolphinViewContainer::showItemInfo(const KFileItem& item)
369 {
370 if (item.isNull()) {
371 // Only clear the status bar if unimportant messages are shown.
372 // This prevents that information- or error-messages get hidden
373 // by moving the mouse above the viewport or when closing the
374 // context menu.
375 if (m_statusBar->type() == DolphinStatusBar::Default) {
376 m_statusBar->clear();
377 }
378 } else {
379 m_statusBar->setMessage(item.getStatusBarInfo(), DolphinStatusBar::Default);
380 }
381 }
382
383 void DolphinViewContainer::showInfoMessage(const QString& msg)
384 {
385 m_statusBar->setMessage(msg, DolphinStatusBar::Information);
386 }
387
388 void DolphinViewContainer::showErrorMessage(const QString& msg)
389 {
390 m_statusBar->setMessage(msg, DolphinStatusBar::Error);
391 }
392
393 void DolphinViewContainer::showOperationCompletedMessage(const QString& msg)
394 {
395 m_statusBar->setMessage(msg, DolphinStatusBar::OperationCompleted);
396 }
397
398 void DolphinViewContainer::closeFilterBar()
399 {
400 m_filterBar->hide();
401 m_filterBar->clear();
402 m_view->setFocus();
403 emit showFilterBarChanged(false);
404 }
405
406 void DolphinViewContainer::setNameFilter(const QString& nameFilter)
407 {
408 m_view->setNameFilter(nameFilter);
409 delayedStatusBarUpdate();
410 }
411
412 void DolphinViewContainer::activate()
413 {
414 setActive(true);
415 }
416
417 void DolphinViewContainer::saveViewState()
418 {
419 QByteArray locationState;
420 QDataStream stream(&locationState, QIODevice::WriteOnly);
421 m_view->saveState(stream);
422 m_urlNavigator->saveLocationState(locationState);
423 }
424
425 void DolphinViewContainer::slotUrlNavigatorLocationChanged(const KUrl& url)
426 {
427 if (KProtocolManager::supportsListing(url)) {
428 // Assure that the search box is shown instead of the URL navigator in case
429 // that the URL \p url is a search URL (e. g. nepomuksearch:// or filenamesearch://).
430 if (isSearchUrl(url)) {
431 if (!m_searchBox->isVisible()) {
432 m_searchBox->setVisible(true);
433 m_urlNavigator->setVisible(false);
434 }
435 } else if (!m_urlNavigator->isVisible()) {
436 m_urlNavigator->setVisible(true);
437 m_searchBox->setVisible(false);
438 }
439
440 m_view->setUrl(url);
441 if (isActive()) {
442 // When an URL has been entered, the view should get the focus.
443 // The focus must be requested asynchronously, as changing the URL might create
444 // a new view widget.
445 QTimer::singleShot(0, this, SLOT(requestFocus()));
446 }
447 } else if (KProtocolManager::isSourceProtocol(url)) {
448 QString app = "konqueror";
449 if (url.protocol().startsWith(QLatin1String("http"))) {
450 showErrorMessage(i18nc("@info:status",
451 "Dolphin does not support web pages, the web browser has been launched"));
452 const KConfigGroup config(KSharedConfig::openConfig("kdeglobals"), "General");
453 const QString browser = config.readEntry("BrowserApplication");
454 if (!browser.isEmpty()) {
455 app = browser;
456 if (app.startsWith('!')) {
457 // a literal command has been configured, remove the '!' prefix
458 app = app.mid(1);
459 }
460 }
461 } else {
462 showErrorMessage(i18nc("@info:status",
463 "Protocol not supported by Dolphin, Konqueror has been launched"));
464 }
465
466 const QString secureUrl = KShell::quoteArg(url.pathOrUrl());
467 const QString command = app + ' ' + secureUrl;
468 KRun::runCommand(command, app, app, this);
469 } else {
470 showErrorMessage(i18nc("@info:status", "Invalid protocol"));
471 }
472 }
473
474 void DolphinViewContainer::dropUrls(const KUrl& destination, QDropEvent* event)
475 {
476 DragAndDropHelper::instance().dropUrls(KFileItem(), destination, event, this);
477 }
478
479 void DolphinViewContainer::redirect(const KUrl& oldUrl, const KUrl& newUrl)
480 {
481 Q_UNUSED(oldUrl);
482 const bool block = m_urlNavigator->signalsBlocked();
483 m_urlNavigator->blockSignals(true);
484
485 // Assure that the location state is reset for redirection URLs. This
486 // allows to skip redirection URLs when going back or forward in the
487 // URL history.
488 m_urlNavigator->saveLocationState(QByteArray());
489 m_urlNavigator->setLocationUrl(newUrl);
490
491 m_urlNavigator->blockSignals(block);
492 }
493
494 void DolphinViewContainer::requestFocus()
495 {
496 m_view->setFocus();
497 }
498
499 void DolphinViewContainer::saveUrlCompletionMode(KGlobalSettings::Completion completion)
500 {
501 DolphinSettings& settings = DolphinSettings::instance();
502 settings.generalSettings()->setUrlCompletionMode(completion);
503 settings.save();
504 }
505
506 void DolphinViewContainer::slotHistoryChanged()
507 {
508 QByteArray locationState = m_urlNavigator->locationState();
509
510 if (!locationState.isEmpty()) {
511 QDataStream stream(&locationState, QIODevice::ReadOnly);
512 m_view->restoreState(stream);
513 }
514 }
515
516 void DolphinViewContainer::startSearching(const QString &text)
517 {
518 Q_UNUSED(text);
519 const KUrl url = m_searchBox->urlForSearching();
520 if (url.isValid() && !url.isEmpty()) {
521 m_urlNavigator->setLocationUrl(url);
522 }
523 }
524
525 void DolphinViewContainer::closeSearchBox()
526 {
527 setSearchModeEnabled(false);
528 }
529
530 bool DolphinViewContainer::isSearchUrl(const KUrl& url) const
531 {
532 return url.protocol().contains("search");
533 }
534
535 void DolphinViewContainer::slotItemTriggered(const KFileItem& item)
536 {
537 KUrl url = item.targetUrl();
538
539 if (item.isDir()) {
540 m_view->setUrl(url);
541 return;
542 }
543
544 const GeneralSettings* settings = DolphinSettings::instance().generalSettings();
545 const bool browseThroughArchives = settings->browseThroughArchives();
546 if (browseThroughArchives && item.isFile() && url.isLocalFile()) {
547 // Generic mechanism for redirecting to tar:/<path>/ when clicking on a tar file,
548 // zip:/<path>/ when clicking on a zip file, etc.
549 // The .protocol file specifies the mimetype that the kioslave handles.
550 // Note that we don't use mimetype inheritance since we don't want to
551 // open OpenDocument files as zip folders...
552 const QString protocol = KProtocolManager::protocolForArchiveMimetype(item.mimetype());
553 if (!protocol.isEmpty()) {
554 url.setProtocol(protocol);
555 m_view->setUrl(url);
556 return;
557 }
558 }
559
560 if (item.mimetype() == "application/x-desktop") {
561 // redirect to the url in Type=Link desktop files
562 KDesktopFile desktopFile(url.toLocalFile());
563 if (desktopFile.hasLinkType()) {
564 url = desktopFile.readUrl();
565 m_view->setUrl(url);
566 return;
567 }
568 }
569
570 item.run();
571 }
572
573 void DolphinViewContainer::openFile(const KUrl& url)
574 {
575 // Using m_dolphinModel for getting the file item instance is not possible
576 // here: openFile() is triggered by an error of the directory lister
577 // job, so the file item must be received "manually".
578 const KFileItem item(KFileItem::Unknown, KFileItem::Unknown, url);
579 slotItemTriggered(item);
580 }
581
582 #include "dolphinviewcontainer.moc"