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