]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinviewcontainer.cpp
SVN_SILENT: warning--, fixed indentations
[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 <krun.h>
48
49 #include "dolphinmodel.h"
50 #include "dolphincolumnview.h"
51 #include "dolphincontroller.h"
52 #include "dolphinstatusbar.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 "kurlnavigator.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,
132 url,
133 m_dirLister,
134 m_dolphinModel,
135 m_proxyModel);
136 connect(m_view, SIGNAL(urlChanged(const KUrl&)),
137 m_urlNavigator, SLOT(setUrl(const KUrl&)));
138 connect(m_view, SIGNAL(requestContextMenu(KFileItem, const KUrl&, const QList<QAction*>&)),
139 this, SLOT(openContextMenu(KFileItem, const KUrl&, const QList<QAction*>&)));
140 connect(m_view, SIGNAL(contentsMoved(int, int)),
141 this, SLOT(saveContentsPos(int, int)));
142 connect(m_view, SIGNAL(requestItemInfo(KFileItem)),
143 this, SLOT(showItemInfo(KFileItem)));
144 connect(m_view, SIGNAL(errorMessage(const QString&)),
145 this, SLOT(showErrorMessage(const QString&)));
146 connect(m_view, SIGNAL(infoMessage(const QString&)),
147 this, SLOT(showInfoMessage(const QString&)));
148 connect(m_view, SIGNAL(operationCompletedMessage(const QString&)),
149 this, SLOT(showOperationCompletedMessage(const QString&)));
150 connect(m_view, SIGNAL(itemTriggered(KFileItem)),
151 this, SLOT(slotItemTriggered(KFileItem)));
152 connect(m_view, SIGNAL(startedPathLoading(const KUrl&)),
153 this, SLOT(saveRootUrl(const KUrl&)));
154 connect(m_view, SIGNAL(redirection(KUrl, KUrl)),
155 this, SLOT(redirect(KUrl, KUrl)));
156 connect(m_view, SIGNAL(selectionChanged(const KFileItemList&)),
157 this, SLOT(delayedStatusBarUpdate()));
158
159 connect(m_urlNavigator, SIGNAL(urlChanged(const KUrl&)),
160 this, SLOT(restoreView(const KUrl&)));
161 connect(m_urlNavigator, SIGNAL(historyChanged()),
162 this, SLOT(slotHistoryChanged()));
163
164 m_statusBar = new DolphinStatusBar(this, m_view);
165 m_statusBarTimer = new QTimer(this);
166 m_statusBarTimer->setSingleShot(true);
167 m_statusBarTimer->setInterval(300);
168 connect(m_statusBarTimer, SIGNAL(timeout()),
169 this, SLOT(updateStatusBar()));
170
171 m_filterBar = new FilterBar(this);
172 m_filterBar->setVisible(settings->filterBar());
173 connect(m_filterBar, SIGNAL(filterChanged(const QString&)),
174 this, SLOT(setNameFilter(const QString&)));
175 connect(m_filterBar, SIGNAL(closeRequest()),
176 this, SLOT(closeFilterBar()));
177 connect(m_view, SIGNAL(urlChanged(const KUrl&)),
178 m_filterBar, SLOT(clear()));
179
180 m_topLayout->addWidget(m_urlNavigator);
181 m_topLayout->addWidget(m_view);
182 m_topLayout->addWidget(m_filterBar);
183 m_topLayout->addWidget(m_statusBar);
184 }
185
186 DolphinViewContainer::~DolphinViewContainer()
187 {
188 m_dirLister->disconnect();
189
190 delete m_proxyModel;
191 m_proxyModel = 0;
192 delete m_dolphinModel;
193 m_dolphinModel = 0;
194 m_dirLister = 0; // deleted by m_dolphinModel
195 }
196
197 void DolphinViewContainer::setUrl(const KUrl& newUrl)
198 {
199 if (newUrl != m_urlNavigator->url()) {
200 m_urlNavigator->setUrl(newUrl);
201 // Temporary disable the 'File'->'Create New...' menu until
202 // the write permissions can be checked in a fast way at
203 // DolphinViewContainer::slotDirListerCompleted().
204 m_isFolderWritable = false;
205 if (isActive()) {
206 m_mainWindow->newMenu()->menu()->setEnabled(false);
207 }
208 }
209 }
210
211 const KUrl& DolphinViewContainer::url() const
212 {
213 return m_urlNavigator->url();
214 }
215
216 void DolphinViewContainer::setActive(bool active)
217 {
218 m_urlNavigator->setActive(active);
219 m_view->setActive(active);
220 if (active) {
221 m_mainWindow->newMenu()->menu()->setEnabled(m_isFolderWritable);
222 }
223 }
224
225 bool DolphinViewContainer::isActive() const
226 {
227 Q_ASSERT(m_view->isActive() == m_urlNavigator->isActive());
228 return m_view->isActive();
229 }
230
231 void DolphinViewContainer::refresh()
232 {
233 m_view->refresh();
234 m_statusBar->refresh();
235 }
236
237 bool DolphinViewContainer::isFilterBarVisible() const
238 {
239 return m_filterBar->isVisible();
240 }
241
242 void DolphinViewContainer::showFilterBar(bool show)
243 {
244 Q_ASSERT(m_filterBar != 0);
245 if (show) {
246 m_filterBar->show();
247 } else {
248 closeFilterBar();
249 }
250 }
251
252 bool DolphinViewContainer::isUrlEditable() const
253 {
254 return m_urlNavigator->isUrlEditable();
255 }
256
257 void DolphinViewContainer::delayedStatusBarUpdate()
258 {
259 // Invoke updateStatusBar() with a small delay. This assures that
260 // when a lot of delayedStatusBarUpdates() are done in a short time,
261 // no bottleneck is given.
262 m_statusBarTimer->start();
263 }
264
265 void DolphinViewContainer::updateStatusBar()
266 {
267 // As the item count information is less important
268 // in comparison with other messages, it should only
269 // be shown if:
270 // - the status bar is empty or
271 // - shows already the item count information or
272 // - shows only a not very important information
273 // - if any progress is given don't show the item count info at all
274 const QString msg(m_statusBar->message());
275 const bool updateStatusBarMsg = (msg.isEmpty()
276 || (msg == m_statusBar->defaultText())
277 || (m_statusBar->type() == DolphinStatusBar::Information))
278 && (m_statusBar->progress() == 100);
279
280 const QString text(m_view->statusBarText());
281 m_statusBar->setDefaultText(text);
282
283 if (updateStatusBarMsg) {
284 m_statusBar->setMessage(text, DolphinStatusBar::Default);
285 }
286 }
287
288 void DolphinViewContainer::updateProgress(int percent)
289 {
290 if (!m_showProgress) {
291 // Only show the directory loading progress if the status bar does
292 // not contain another progress information. This means that
293 // the directory loading progress information has the lowest priority.
294 const QString progressText(m_statusBar->progressText());
295 const QString loadingText(i18nc("@info:progress", "Loading folder..."));
296 m_showProgress = progressText.isEmpty() || (progressText == loadingText);
297 if (m_showProgress) {
298 m_statusBar->setProgressText(loadingText);
299 m_statusBar->setProgress(0);
300 }
301 }
302
303 if (m_showProgress) {
304 m_statusBar->setProgress(percent);
305 }
306 }
307
308 void DolphinViewContainer::slotDirListerCompleted()
309 {
310 if (m_showProgress) {
311 m_statusBar->setProgressText(QString());
312 m_statusBar->setProgress(100);
313 m_showProgress = false;
314 }
315
316 updateStatusBar();
317 QMetaObject::invokeMethod(this, "restoreContentsPos", Qt::QueuedConnection);
318
319 // Enable the 'File'->'Create New...' menu only if the directory
320 // supports writing.
321 KFileItem item = m_dirLister->rootItem();
322 if (item.isNull()) {
323 // it is unclear whether writing is supported
324 m_isFolderWritable = true;
325 } else {
326 KFileItemListProperties capabilities(KFileItemList() << item);
327 m_isFolderWritable = capabilities.supportsWriting();
328 }
329
330 if (isActive()) {
331 m_mainWindow->newMenu()->menu()->setEnabled(m_isFolderWritable);
332 }
333 }
334
335 void DolphinViewContainer::showItemInfo(const KFileItem& item)
336 {
337 if (item.isNull()) {
338 // Only clear the status bar if unimportant messages are shown.
339 // This prevents that information- or error-messages get hidden
340 // by moving the mouse above the viewport or when closing the
341 // context menu.
342 if (m_statusBar->type() == DolphinStatusBar::Default) {
343 m_statusBar->clear();
344 }
345 } else {
346 m_statusBar->setMessage(item.getStatusBarInfo(), DolphinStatusBar::Default);
347 }
348 }
349
350 void DolphinViewContainer::showInfoMessage(const QString& msg)
351 {
352 m_statusBar->setMessage(msg, DolphinStatusBar::Information);
353 }
354
355 void DolphinViewContainer::showErrorMessage(const QString& msg)
356 {
357 m_statusBar->setMessage(msg, DolphinStatusBar::Error);
358 }
359
360 void DolphinViewContainer::showOperationCompletedMessage(const QString& msg)
361 {
362 m_statusBar->setMessage(msg, DolphinStatusBar::OperationCompleted);
363 }
364
365 void DolphinViewContainer::closeFilterBar()
366 {
367 m_filterBar->hide();
368 m_filterBar->clear();
369 m_view->setFocus();
370 emit showFilterBarChanged(false);
371 }
372
373 void DolphinViewContainer::setNameFilter(const QString& nameFilter)
374 {
375 m_view->setNameFilter(nameFilter);
376 delayedStatusBarUpdate();
377 }
378
379 void DolphinViewContainer::openContextMenu(const KFileItem& item,
380 const KUrl& url,
381 const QList<QAction*>& customActions)
382 {
383 DolphinContextMenu contextMenu(m_mainWindow, item, url);
384 contextMenu.setCustomActions(customActions);
385 contextMenu.open();
386 }
387
388 void DolphinViewContainer::saveContentsPos(int x, int y)
389 {
390 m_urlNavigator->savePosition(x, y);
391 }
392
393 void DolphinViewContainer::restoreContentsPos()
394 {
395 if (!url().isEmpty()) {
396 const QPoint pos = m_urlNavigator->savedPosition();
397 m_view->setContentsPosition(pos.x(), pos.y());
398 }
399 }
400
401 void DolphinViewContainer::activate()
402 {
403 setActive(true);
404 }
405
406 void DolphinViewContainer::restoreView(const KUrl& url)
407 {
408 if (KProtocolManager::supportsListing(url)) {
409 m_view->updateView(url, m_urlNavigator->savedRootUrl());
410 if (isActive()) {
411 // When an URL has been entered, the view should get the focus.
412 // The focus must be requested asynchronously, as changing the URL might create
413 // a new view widget. Using QTimer::singleShow() works reliable, however
414 // QMetaObject::invokeMethod() with a queued connection does not work, which might
415 // indicate that we should pass a hint to DolphinView::updateView()
416 // regarding the focus instead. To test: Enter an URL and press CTRL+Enter.
417 // Expected result: The view should get the focus.
418 QTimer::singleShot(0, this, SLOT(requestFocus()));
419 }
420 } else if (KProtocolManager::isSourceProtocol(url)) {
421 QString app = "konqueror";
422 if (url.protocol().startsWith(QLatin1String("http"))) {
423 showErrorMessage(i18nc("@info:status",
424 "Dolphin does not support web pages, the web browser has been launched"));
425 const KConfigGroup config(KSharedConfig::openConfig("kdeglobals"), "General");
426 const QString browser = config.readEntry("BrowserApplication");
427 if (!browser.isEmpty()) {
428 app = browser;
429 if (app.startsWith('!')) {
430 // a literal command has been configured, remove the '!' prefix
431 app = app.mid(1);
432 }
433 }
434 } else {
435 showErrorMessage(i18nc("@info:status",
436 "Protocol not supported by Dolphin, Konqueror has been launched"));
437 }
438
439 QString secureUrl = KShell::quoteArg(url.pathOrUrl());
440 const QString command = app + ' ' + secureUrl;
441 KRun::runCommand(command, app, app, this);
442 } else {
443 showErrorMessage(i18nc("@info:status", "Invalid protocol"));
444 }
445 }
446
447 void DolphinViewContainer::saveRootUrl(const KUrl& url)
448 {
449 Q_UNUSED(url);
450 m_urlNavigator->saveRootUrl(m_view->rootUrl());
451 }
452
453 void DolphinViewContainer::dropUrls(const KUrl& destination, QDropEvent* event)
454 {
455 DragAndDropHelper::instance().dropUrls(KFileItem(), destination, event, this);
456 }
457
458 void DolphinViewContainer::redirect(const KUrl& oldUrl, const KUrl& newUrl)
459 {
460 Q_UNUSED(oldUrl);
461 const bool block = m_urlNavigator->signalsBlocked();
462 m_urlNavigator->blockSignals(true);
463 m_urlNavigator->setUrl(newUrl);
464 m_urlNavigator->blockSignals(block);
465 }
466
467 void DolphinViewContainer::requestFocus()
468 {
469 m_view->setFocus();
470 }
471
472 void DolphinViewContainer::saveUrlCompletionMode(KGlobalSettings::Completion completion)
473 {
474 DolphinSettings& settings = DolphinSettings::instance();
475 settings.generalSettings()->setUrlCompletionMode(completion);
476 settings.save();
477 }
478
479 void DolphinViewContainer::slotHistoryChanged()
480 {
481 const int index = m_urlNavigator->historyIndex();
482 if (index > 0) {
483 // The "Go Forward" action is enabled. Try to mark
484 // the previous directory as active item:
485 const KUrl url = m_urlNavigator->historyUrl(index - 1);
486 m_view->activateItem(url);
487 }
488 }
489
490 void DolphinViewContainer::slotItemTriggered(const KFileItem& item)
491 {
492 KUrl url = item.targetUrl();
493
494 if (item.isDir()) {
495 m_view->setUrl(url);
496 return;
497 }
498
499 const GeneralSettings* settings = DolphinSettings::instance().generalSettings();
500 const bool browseThroughArchives = settings->browseThroughArchives();
501 if (browseThroughArchives && item.isFile() && url.isLocalFile()) {
502 // Generic mechanism for redirecting to tar:/<path>/ when clicking on a tar file,
503 // zip:/<path>/ when clicking on a zip file, etc.
504 // The .protocol file specifies the mimetype that the kioslave handles.
505 // Note that we don't use mimetype inheritance since we don't want to
506 // open OpenDocument files as zip folders...
507 const QString protocol = KProtocolManager::protocolForArchiveMimetype(item.mimetype());
508 if (!protocol.isEmpty()) {
509 url.setProtocol(protocol);
510 m_view->setUrl(url);
511 return;
512 }
513 }
514
515 item.run();
516 }
517
518 void DolphinViewContainer::openFile(const KUrl& url)
519 {
520 // Using m_dolphinModel for getting the file item instance is not possible
521 // here: openFile() is triggered by an error of the directory lister
522 // job, so the file item must be received "manually".
523 const KFileItem item(KFileItem::Unknown, KFileItem::Unknown, url);
524 slotItemTriggered(item);
525 }
526
527 #include "dolphinviewcontainer.moc"