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