]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinviewcontainer.cpp
Launch Konqueror if the user enters an unsupported protocol
[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
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 const KUrl& DolphinViewContainer::url() const
187 {
188 return m_urlNavigator->url();
189 }
190
191 void DolphinViewContainer::setActive(bool active)
192 {
193 m_urlNavigator->setActive(active);
194 m_view->setActive(active);
195 }
196
197 bool DolphinViewContainer::isActive() const
198 {
199 Q_ASSERT(m_view->isActive() == m_urlNavigator->isActive());
200 return m_view->isActive();
201 }
202
203 void DolphinViewContainer::refresh()
204 {
205 m_view->refresh();
206 m_statusBar->refresh();
207 }
208
209 bool DolphinViewContainer::isFilterBarVisible() const
210 {
211 return m_filterBar->isVisible();
212 }
213
214 bool DolphinViewContainer::isUrlEditable() const
215 {
216 return m_urlNavigator->isUrlEditable();
217 }
218
219 void DolphinViewContainer::updateProgress(int percent)
220 {
221 if (!m_showProgress) {
222 // Only show the directory loading progress if the status bar does
223 // not contain another progress information. This means that
224 // the directory loading progress information has the lowest priority.
225 const QString progressText(m_statusBar->progressText());
226 const QString loadingText(i18nc("@info:progress", "Loading folder..."));
227 m_showProgress = progressText.isEmpty() || (progressText == loadingText);
228 if (m_showProgress) {
229 m_statusBar->setProgressText(loadingText);
230 m_statusBar->setProgress(0);
231 }
232 }
233
234 if (m_showProgress) {
235 m_statusBar->setProgress(percent);
236 }
237 }
238
239 void DolphinViewContainer::slotDirListerCompleted()
240 {
241 if (m_showProgress) {
242 m_statusBar->setProgressText(QString());
243 m_statusBar->setProgress(100);
244 m_showProgress = false;
245 }
246
247 updateStatusBar();
248 QTimer::singleShot(100, this, SLOT(restoreContentsPos()));
249
250 // Enable the 'File'->'Create New...' menu only if the directory
251 // supports writing.
252 KMenu* createNew = m_mainWindow->newMenu()->menu();
253 KFileItem item = m_dirLister->rootItem();
254 if (item.isNull()) {
255 // it is unclear whether writing is supported
256 createNew->setEnabled(true);
257 } else {
258 KonqFileItemCapabilities capabilities(KFileItemList() << item);
259 createNew->setEnabled(capabilities.supportsWriting());
260 }
261 }
262
263 void DolphinViewContainer::showItemInfo(const KFileItem& item)
264 {
265 if (item.isNull()) {
266 m_statusBar->clear();
267 } else {
268 m_statusBar->setMessage(item.getStatusBarInfo(), DolphinStatusBar::Default);
269 }
270 }
271
272 void DolphinViewContainer::showInfoMessage(const QString& msg)
273 {
274 m_statusBar->setMessage(msg, DolphinStatusBar::Information);
275 }
276
277 void DolphinViewContainer::showErrorMessage(const QString& msg)
278 {
279 m_statusBar->setMessage(msg, DolphinStatusBar::Error);
280 }
281
282 void DolphinViewContainer::showOperationCompletedMessage(const QString& msg)
283 {
284 m_statusBar->setMessage(msg, DolphinStatusBar::OperationCompleted);
285 }
286
287 void DolphinViewContainer::closeFilterBar()
288 {
289 m_filterBar->hide();
290 m_filterBar->clear();
291 emit showFilterBarChanged(false);
292 }
293
294 void DolphinViewContainer::showFilterBar(bool show)
295 {
296 Q_ASSERT(m_filterBar != 0);
297 m_filterBar->setVisible(show);
298 }
299
300 void DolphinViewContainer::updateStatusBar()
301 {
302 // As the item count information is less important
303 // in comparison with other messages, it should only
304 // be shown if:
305 // - the status bar is empty or
306 // - shows already the item count information or
307 // - shows only a not very important information
308 // - if any progress is given don't show the item count info at all
309 const QString msg(m_statusBar->message());
310 const bool updateStatusBarMsg = (msg.isEmpty() ||
311 (msg == m_statusBar->defaultText()) ||
312 (m_statusBar->type() == DolphinStatusBar::Information)) &&
313 (m_statusBar->progress() == 100);
314
315 const QString text(m_view->statusBarText());
316 m_statusBar->setDefaultText(text);
317
318 if (updateStatusBarMsg) {
319 m_statusBar->setMessage(text, DolphinStatusBar::Default);
320 }
321 }
322
323 void DolphinViewContainer::setNameFilter(const QString& nameFilter)
324 {
325 m_view->setNameFilter(nameFilter);
326 updateStatusBar();
327 }
328
329 void DolphinViewContainer::openContextMenu(const KFileItem& item,
330 const KUrl& url)
331 {
332 DolphinContextMenu contextMenu(m_mainWindow, item, url);
333 contextMenu.open();
334 }
335
336 void DolphinViewContainer::saveContentsPos(int x, int y)
337 {
338 m_urlNavigator->savePosition(x, y);
339 }
340
341 void DolphinViewContainer::restoreContentsPos()
342 {
343 if (!url().isEmpty()) {
344 const QPoint pos = m_urlNavigator->savedPosition();
345 m_view->setContentsPosition(pos.x(), pos.y());
346 }
347 }
348
349 void DolphinViewContainer::activate()
350 {
351 setActive(true);
352 }
353
354 void DolphinViewContainer::restoreView(const KUrl& url)
355 {
356 if (KProtocolManager::supportsListing(url)) {
357 m_view->updateView(url, m_urlNavigator->savedRootUrl());
358 } else {
359 // The URL navigator only checks for validity, not
360 // if the URL can be listed.
361 showErrorMessage(i18nc("@info:status", "Protocol not supported by Dolphin, Konqueror has been launched"));
362 QString command = "konqueror " + url.pathOrUrl();
363 KRun::runCommand(command, "konqueror", "konqueror", this);
364 }
365 }
366
367 void DolphinViewContainer::saveRootUrl(const KUrl& url)
368 {
369 Q_UNUSED(url);
370 m_urlNavigator->saveRootUrl(m_view->rootUrl());
371 }
372
373 void DolphinViewContainer::dropUrls(const KUrl& destination, QDropEvent* event)
374 {
375 DragAndDropHelper::instance().dropUrls(KFileItem(), destination, event, this);
376 }
377
378 void DolphinViewContainer::redirect(const KUrl& oldUrl, const KUrl& newUrl)
379 {
380 Q_UNUSED(oldUrl);
381 const bool block = m_urlNavigator->signalsBlocked();
382 m_urlNavigator->blockSignals(true);
383 m_urlNavigator->setUrl(newUrl);
384 m_urlNavigator->blockSignals(block);
385 }
386
387 void DolphinViewContainer::slotItemTriggered(const KFileItem& item)
388 {
389 KUrl url = item.targetUrl();
390
391 if (item.isDir()) {
392 m_view->setUrl(url);
393 return;
394 }
395
396 const GeneralSettings* settings = DolphinSettings::instance().generalSettings();
397 const bool browseThroughArchives = settings->browseThroughArchives();
398 if (browseThroughArchives && item.isFile() && url.isLocalFile()) {
399 // Generic mechanism for redirecting to tar:/<path>/ when clicking on a tar file,
400 // zip:/<path>/ when clicking on a zip file, etc.
401 // The .protocol file specifies the mimetype that the kioslave handles.
402 // Note that we don't use mimetype inheritance since we don't want to
403 // open OpenDocument files as zip folders...
404 const QString protocol = KProtocolManager::protocolForArchiveMimetype(item.mimetype());
405 if (!protocol.isEmpty()) {
406 url.setProtocol(protocol);
407 m_view->setUrl(url);
408 return;
409 }
410 }
411
412 item.run();
413 }
414
415 #include "dolphinviewcontainer.moc"