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