]>
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
&)),
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
)));
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()));
162 m_topLayout
->addWidget(m_urlNavigator
);
163 m_topLayout
->addWidget(m_view
);
164 m_topLayout
->addWidget(m_filterBar
);
165 m_topLayout
->addWidget(m_statusBar
);
168 DolphinViewContainer::~DolphinViewContainer()
170 m_dirLister
->disconnect();
174 delete m_dolphinModel
;
176 m_dirLister
= 0; // deleted by m_dolphinModel
179 void DolphinViewContainer::setUrl(const KUrl
& newUrl
)
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;
188 m_mainWindow
->newMenu()->menu()->setEnabled(false);
193 const KUrl
& DolphinViewContainer::url() const
195 return m_urlNavigator
->url();
198 void DolphinViewContainer::setActive(bool active
)
200 m_urlNavigator
->setActive(active
);
201 m_view
->setActive(active
);
203 m_mainWindow
->newMenu()->menu()->setEnabled(m_isFolderWritable
);
207 bool DolphinViewContainer::isActive() const
209 Q_ASSERT(m_view
->isActive() == m_urlNavigator
->isActive());
210 return m_view
->isActive();
213 void DolphinViewContainer::refresh()
216 m_statusBar
->refresh();
219 bool DolphinViewContainer::isFilterBarVisible() const
221 return m_filterBar
->isVisible();
224 void DolphinViewContainer::showFilterBar(bool show
)
226 Q_ASSERT(m_filterBar
!= 0);
234 bool DolphinViewContainer::isUrlEditable() const
236 return m_urlNavigator
->isUrlEditable();
239 void DolphinViewContainer::updateProgress(int percent
)
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);
254 if (m_showProgress
) {
255 m_statusBar
->setProgress(percent
);
259 void DolphinViewContainer::slotDirListerCompleted()
261 if (m_showProgress
) {
262 m_statusBar
->setProgressText(QString());
263 m_statusBar
->setProgress(100);
264 m_showProgress
= false;
268 QMetaObject::invokeMethod(this, "restoreContentsPos", Qt::QueuedConnection
);
270 // Enable the 'File'->'Create New...' menu only if the directory
272 KFileItem item
= m_dirLister
->rootItem();
274 // it is unclear whether writing is supported
275 m_isFolderWritable
= true;
277 KonqFileItemCapabilities
capabilities(KFileItemList() << item
);
278 m_isFolderWritable
= capabilities
.supportsWriting();
282 m_mainWindow
->newMenu()->menu()->setEnabled(m_isFolderWritable
);
286 void DolphinViewContainer::showItemInfo(const KFileItem
& item
)
289 m_statusBar
->clear();
291 m_statusBar
->setMessage(item
.getStatusBarInfo(), DolphinStatusBar::Default
);
295 void DolphinViewContainer::showInfoMessage(const QString
& msg
)
297 m_statusBar
->setMessage(msg
, DolphinStatusBar::Information
);
300 void DolphinViewContainer::showErrorMessage(const QString
& msg
)
302 m_statusBar
->setMessage(msg
, DolphinStatusBar::Error
);
305 void DolphinViewContainer::showOperationCompletedMessage(const QString
& msg
)
307 m_statusBar
->setMessage(msg
, DolphinStatusBar::OperationCompleted
);
310 void DolphinViewContainer::closeFilterBar()
313 m_filterBar
->clear();
315 emit
showFilterBarChanged(false);
318 void DolphinViewContainer::updateStatusBar()
320 // As the item count information is less important
321 // in comparison with other messages, it should only
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);
333 const QString
text(m_view
->statusBarText());
334 m_statusBar
->setDefaultText(text
);
336 if (updateStatusBarMsg
) {
337 m_statusBar
->setMessage(text
, DolphinStatusBar::Default
);
341 void DolphinViewContainer::setNameFilter(const QString
& nameFilter
)
343 m_view
->setNameFilter(nameFilter
);
347 void DolphinViewContainer::openContextMenu(const KFileItem
& item
,
350 DolphinContextMenu
contextMenu(m_mainWindow
, item
, url
);
354 void DolphinViewContainer::saveContentsPos(int x
, int y
)
356 m_urlNavigator
->savePosition(x
, y
);
359 void DolphinViewContainer::restoreContentsPos()
361 if (!url().isEmpty()) {
362 const QPoint pos
= m_urlNavigator
->savedPosition();
363 m_view
->setContentsPosition(pos
.x(), pos
.y());
367 void DolphinViewContainer::activate()
372 void DolphinViewContainer::restoreView(const KUrl
& url
)
374 if (KProtocolManager::supportsListing(url
)) {
375 m_view
->updateView(url
, m_urlNavigator
->savedRootUrl());
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()));
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()) {
397 showErrorMessage(i18nc("@info:status",
398 "Protocol not supported by Dolphin, Konqueror has been launched"));
400 const QString command
= app
+ ' ' + url
.pathOrUrl();
401 KRun::runCommand(command
, app
, app
, this);
403 showErrorMessage(i18nc("@info:status", "Invalid protocol"));
407 void DolphinViewContainer::saveRootUrl(const KUrl
& url
)
410 m_urlNavigator
->saveRootUrl(m_view
->rootUrl());
413 void DolphinViewContainer::dropUrls(const KUrl
& destination
, QDropEvent
* event
)
415 DragAndDropHelper::instance().dropUrls(KFileItem(), destination
, event
, this);
418 void DolphinViewContainer::redirect(const KUrl
& oldUrl
, const KUrl
& newUrl
)
421 const bool block
= m_urlNavigator
->signalsBlocked();
422 m_urlNavigator
->blockSignals(true);
423 m_urlNavigator
->setUrl(newUrl
);
424 m_urlNavigator
->blockSignals(block
);
427 void DolphinViewContainer::requestFocus()
432 void DolphinViewContainer::slotItemTriggered(const KFileItem
& item
)
434 KUrl url
= item
.targetUrl();
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
);
460 void DolphinViewContainer::openFile(const KUrl
& url
)
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
);
469 #include "dolphinviewcontainer.moc"