]>
cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinviewcontainer.cpp
1 /***************************************************************************
2 * Copyright (C) 2007 by Peter Penz <peter.penz@gmx.at> *
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. *
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. *
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 ***************************************************************************/
20 #include "dolphinviewcontainer.h"
21 #include <kprotocolmanager.h>
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>
31 #include <kfileitemdelegate.h>
32 #include <kfileplacesmodel.h>
33 #include <kglobalsettings.h>
35 #include <kiconeffect.h>
36 #include <kio/netaccess.h>
37 #include <kio/previewjob.h>
39 #include <kmimetyperesolver.h>
41 #include <konqmimedata.h>
42 #include <konq_fileitemcapabilities.h>
43 #include <konq_operations.h>
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"
64 DolphinViewContainer::DolphinViewContainer(DolphinMainWindow
* mainWindow
,
68 m_showProgress(false),
69 m_isFolderWritable(false),
70 m_mainWindow(mainWindow
),
81 m_topLayout
= new QVBoxLayout(this);
82 m_topLayout
->setSpacing(0);
83 m_topLayout
->setMargin(0);
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()));
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());
96 m_dirLister
= new DolphinDirLister();
97 m_dirLister
->setAutoUpdate(true);
98 m_dirLister
->setMainWindow(window());
99 m_dirLister
->setDelayedMimeTypes(true);
101 m_dolphinModel
= new DolphinModel(this);
102 m_dolphinModel
->setDirLister(m_dirLister
);
103 m_dolphinModel
->setDropsAllowed(DolphinModel::DropOnDirectory
);
105 m_proxyModel
= new DolphinSortFilterProxyModel(this);
106 m_proxyModel
->setSourceModel(m_dolphinModel
);
107 m_proxyModel
->setFilterCaseSensitivity(Qt::CaseInsensitive
);
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
&)));
124 m_view
= new DolphinView(this,
129 connect(m_view
, SIGNAL(urlChanged(const KUrl
&)),
130 m_urlNavigator
, SLOT(setUrl(const KUrl
&)));
131 connect(m_view
, SIGNAL(requestContextMenu(KFileItem
, const KUrl
&, const QList
<QAction
*>&)),
132 this, SLOT(openContextMenu(KFileItem
, const KUrl
&, const QList
<QAction
*>&)));
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
)));
150 connect(m_urlNavigator
, SIGNAL(urlChanged(const KUrl
&)),
151 this, SLOT(restoreView(const KUrl
&)));
153 m_statusBar
= new DolphinStatusBar(this, m_view
);
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 connect(m_view
, SIGNAL(urlChanged(const KUrl
&)),
162 m_filterBar
, SLOT(clear()));
164 m_topLayout
->addWidget(m_urlNavigator
);
165 m_topLayout
->addWidget(m_view
);
166 m_topLayout
->addWidget(m_filterBar
);
167 m_topLayout
->addWidget(m_statusBar
);
170 DolphinViewContainer::~DolphinViewContainer()
172 m_dirLister
->disconnect();
176 delete m_dolphinModel
;
178 m_dirLister
= 0; // deleted by m_dolphinModel
181 void DolphinViewContainer::setUrl(const KUrl
& newUrl
)
183 m_urlNavigator
->setUrl(newUrl
);
184 if (newUrl
!= m_urlNavigator
->url()) {
185 // Temporary disable the 'File'->'Create New...' menu until
186 // the write permissions can be checked in a fast way at
187 // DolphinViewContainer::slotDirListerCompleted().
188 m_isFolderWritable
= false;
190 m_mainWindow
->newMenu()->menu()->setEnabled(false);
195 const KUrl
& DolphinViewContainer::url() const
197 return m_urlNavigator
->url();
200 void DolphinViewContainer::setActive(bool active
)
202 m_urlNavigator
->setActive(active
);
203 m_view
->setActive(active
);
205 m_mainWindow
->newMenu()->menu()->setEnabled(m_isFolderWritable
);
209 bool DolphinViewContainer::isActive() const
211 Q_ASSERT(m_view
->isActive() == m_urlNavigator
->isActive());
212 return m_view
->isActive();
215 void DolphinViewContainer::refresh()
218 m_statusBar
->refresh();
221 bool DolphinViewContainer::isFilterBarVisible() const
223 return m_filterBar
->isVisible();
226 void DolphinViewContainer::showFilterBar(bool show
)
228 Q_ASSERT(m_filterBar
!= 0);
236 bool DolphinViewContainer::isUrlEditable() const
238 return m_urlNavigator
->isUrlEditable();
241 void DolphinViewContainer::updateProgress(int percent
)
243 if (!m_showProgress
) {
244 // Only show the directory loading progress if the status bar does
245 // not contain another progress information. This means that
246 // the directory loading progress information has the lowest priority.
247 const QString
progressText(m_statusBar
->progressText());
248 const QString
loadingText(i18nc("@info:progress", "Loading folder..."));
249 m_showProgress
= progressText
.isEmpty() || (progressText
== loadingText
);
250 if (m_showProgress
) {
251 m_statusBar
->setProgressText(loadingText
);
252 m_statusBar
->setProgress(0);
256 if (m_showProgress
) {
257 m_statusBar
->setProgress(percent
);
261 void DolphinViewContainer::slotDirListerCompleted()
263 if (m_showProgress
) {
264 m_statusBar
->setProgressText(QString());
265 m_statusBar
->setProgress(100);
266 m_showProgress
= false;
270 QMetaObject::invokeMethod(this, "restoreContentsPos", Qt::QueuedConnection
);
272 // Enable the 'File'->'Create New...' menu only if the directory
274 KFileItem item
= m_dirLister
->rootItem();
276 // it is unclear whether writing is supported
277 m_isFolderWritable
= true;
279 KonqFileItemCapabilities
capabilities(KFileItemList() << item
);
280 m_isFolderWritable
= capabilities
.supportsWriting();
284 m_mainWindow
->newMenu()->menu()->setEnabled(m_isFolderWritable
);
288 void DolphinViewContainer::showItemInfo(const KFileItem
& item
)
291 m_statusBar
->clear();
293 m_statusBar
->setMessage(item
.getStatusBarInfo(), DolphinStatusBar::Default
);
297 void DolphinViewContainer::showInfoMessage(const QString
& msg
)
299 m_statusBar
->setMessage(msg
, DolphinStatusBar::Information
);
302 void DolphinViewContainer::showErrorMessage(const QString
& msg
)
304 m_statusBar
->setMessage(msg
, DolphinStatusBar::Error
);
307 void DolphinViewContainer::showOperationCompletedMessage(const QString
& msg
)
309 m_statusBar
->setMessage(msg
, DolphinStatusBar::OperationCompleted
);
312 void DolphinViewContainer::closeFilterBar()
315 m_filterBar
->clear();
317 emit
showFilterBarChanged(false);
320 void DolphinViewContainer::updateStatusBar()
322 // As the item count information is less important
323 // in comparison with other messages, it should only
325 // - the status bar is empty or
326 // - shows already the item count information or
327 // - shows only a not very important information
328 // - if any progress is given don't show the item count info at all
329 const QString
msg(m_statusBar
->message());
330 const bool updateStatusBarMsg
= (msg
.isEmpty() ||
331 (msg
== m_statusBar
->defaultText()) ||
332 (m_statusBar
->type() == DolphinStatusBar::Information
)) &&
333 (m_statusBar
->progress() == 100);
335 const QString
text(m_view
->statusBarText());
336 m_statusBar
->setDefaultText(text
);
338 if (updateStatusBarMsg
) {
339 m_statusBar
->setMessage(text
, DolphinStatusBar::Default
);
343 void DolphinViewContainer::setNameFilter(const QString
& nameFilter
)
345 m_view
->setNameFilter(nameFilter
);
349 void DolphinViewContainer::openContextMenu(const KFileItem
& item
,
351 const QList
<QAction
*>& customActions
)
353 DolphinContextMenu
contextMenu(m_mainWindow
, item
, url
);
354 contextMenu
.setCustomActions(customActions
);
358 void DolphinViewContainer::saveContentsPos(int x
, int y
)
360 m_urlNavigator
->savePosition(x
, y
);
363 void DolphinViewContainer::restoreContentsPos()
365 if (!url().isEmpty()) {
366 const QPoint pos
= m_urlNavigator
->savedPosition();
367 m_view
->setContentsPosition(pos
.x(), pos
.y());
371 void DolphinViewContainer::activate()
376 void DolphinViewContainer::restoreView(const KUrl
& url
)
378 if (KProtocolManager::supportsListing(url
)) {
379 m_view
->updateView(url
, m_urlNavigator
->savedRootUrl());
381 // When an URL has been entered, the view should get the focus.
382 // The focus must be requested asynchronously, as changing the URL might create
383 // a new view widget. Using QTimer::singleShow() works reliable, however
384 // QMetaObject::invokeMethod() with a queued connection does not work, which might
385 // indicate that we should pass a hint to DolphinView::updateView()
386 // regarding the focus instead. To test: Enter an URL and press CTRL+Enter.
387 // Expected result: The view should get the focus.
388 QTimer::singleShot(0, this, SLOT(requestFocus()));
390 } else if (KProtocolManager::isSourceProtocol(url
)) {
391 QString app
= "konqueror";
392 if (url
.protocol().startsWith(QLatin1String("http"))) {
393 showErrorMessage(i18nc("@info:status",
394 "Dolphin does not support web pages, the web browser has been launched"));
395 const KConfigGroup
config(KSharedConfig::openConfig("kdeglobals"), "General");
396 const QString browser
= config
.readEntry("BrowserApplication");
397 if (!browser
.isEmpty()) {
401 showErrorMessage(i18nc("@info:status",
402 "Protocol not supported by Dolphin, Konqueror has been launched"));
404 const QString command
= app
+ ' ' + url
.pathOrUrl();
405 KRun::runCommand(command
, app
, app
, this);
407 showErrorMessage(i18nc("@info:status", "Invalid protocol"));
411 void DolphinViewContainer::saveRootUrl(const KUrl
& url
)
414 m_urlNavigator
->saveRootUrl(m_view
->rootUrl());
417 void DolphinViewContainer::dropUrls(const KUrl
& destination
, QDropEvent
* event
)
419 DragAndDropHelper::instance().dropUrls(KFileItem(), destination
, event
, this);
422 void DolphinViewContainer::redirect(const KUrl
& oldUrl
, const KUrl
& newUrl
)
425 const bool block
= m_urlNavigator
->signalsBlocked();
426 m_urlNavigator
->blockSignals(true);
427 m_urlNavigator
->setUrl(newUrl
);
428 m_urlNavigator
->blockSignals(block
);
431 void DolphinViewContainer::requestFocus()
436 void DolphinViewContainer::slotItemTriggered(const KFileItem
& item
)
438 KUrl url
= item
.targetUrl();
445 const GeneralSettings
* settings
= DolphinSettings::instance().generalSettings();
446 const bool browseThroughArchives
= settings
->browseThroughArchives();
447 if (browseThroughArchives
&& item
.isFile() && url
.isLocalFile()) {
448 // Generic mechanism for redirecting to tar:/<path>/ when clicking on a tar file,
449 // zip:/<path>/ when clicking on a zip file, etc.
450 // The .protocol file specifies the mimetype that the kioslave handles.
451 // Note that we don't use mimetype inheritance since we don't want to
452 // open OpenDocument files as zip folders...
453 const QString protocol
= KProtocolManager::protocolForArchiveMimetype(item
.mimetype());
454 if (!protocol
.isEmpty()) {
455 url
.setProtocol(protocol
);
464 void DolphinViewContainer::openFile(const KUrl
& url
)
466 // Using m_dolphinModel for getting the file item instance is not possible
467 // here: openFile() is triggered by an error of the directory lister
468 // job, so the file item must be received "manually".
469 const KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, url
);
470 slotItemTriggered(item
);
473 #include "dolphinviewcontainer.moc"