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