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