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