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