]>
cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinviewcontainer.cpp
9f61360d73a3db98b5305c92a2d08ae2c10ee186
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 <kfileitemlistproperties.h>
43 #include <konq_operations.h>
46 #include <kurlcombobox.h>
49 #include "dolphinmodel.h"
50 #include "dolphincolumnview.h"
51 #include "dolphincontroller.h"
52 #include "dolphinstatusbar.h"
53 #include "dolphinmainwindow.h"
54 #include "dolphindirlister.h"
55 #include "dolphinsortfilterproxymodel.h"
56 #include "dolphindetailsview.h"
57 #include "dolphiniconsview.h"
58 #include "dolphincontextmenu.h"
59 #include "draganddrophelper.h"
60 #include "filterbar.h"
61 #include "kurlnavigator.h"
62 #include "viewproperties.h"
63 #include "settings/dolphinsettings.h"
64 #include "dolphin_generalsettings.h"
66 DolphinViewContainer::DolphinViewContainer(DolphinMainWindow
* mainWindow
,
70 m_showProgress(false),
71 m_isFolderWritable(false),
72 m_mainWindow(mainWindow
),
83 m_topLayout
= new QVBoxLayout(this);
84 m_topLayout
->setSpacing(0);
85 m_topLayout
->setMargin(0);
87 m_urlNavigator
= new KUrlNavigator(DolphinSettings::instance().placesModel(), url
, this);
88 connect(m_urlNavigator
, SIGNAL(urlsDropped(const KUrl
&, QDropEvent
*)),
89 this, SLOT(dropUrls(const KUrl
&, QDropEvent
*)));
90 connect(m_urlNavigator
, SIGNAL(activated()),
91 this, SLOT(activate()));
92 connect(m_urlNavigator
->editor(), SIGNAL(completionModeChanged(KGlobalSettings::Completion
)),
93 this, SLOT(saveUrlCompletionMode(KGlobalSettings::Completion
)));
95 const GeneralSettings
* settings
= DolphinSettings::instance().generalSettings();
96 m_urlNavigator
->setUrlEditable(settings
->editableUrl());
97 m_urlNavigator
->setShowFullPath(settings
->showFullPath());
98 m_urlNavigator
->setHomeUrl(settings
->homeUrl());
99 KUrlComboBox
* editor
= m_urlNavigator
->editor();
100 editor
->setCompletionMode(KGlobalSettings::Completion(settings
->urlCompletionMode()));
102 m_dirLister
= new DolphinDirLister();
103 m_dirLister
->setAutoUpdate(true);
104 m_dirLister
->setMainWindow(window());
105 m_dirLister
->setDelayedMimeTypes(true);
107 m_dolphinModel
= new DolphinModel(this);
108 m_dolphinModel
->setDirLister(m_dirLister
);
109 m_dolphinModel
->setDropsAllowed(DolphinModel::DropOnDirectory
);
111 m_proxyModel
= new DolphinSortFilterProxyModel(this);
112 m_proxyModel
->setSourceModel(m_dolphinModel
);
113 m_proxyModel
->setFilterCaseSensitivity(Qt::CaseInsensitive
);
115 connect(m_dirLister
, SIGNAL(clear()),
116 this, SLOT(updateStatusBar()));
117 connect(m_dirLister
, SIGNAL(percent(int)),
118 this, SLOT(updateProgress(int)));
119 connect(m_dirLister
, SIGNAL(deleteItem(const KFileItem
&)),
120 this, SLOT(updateStatusBar()));
121 connect(m_dirLister
, SIGNAL(completed()),
122 this, SLOT(slotDirListerCompleted()));
123 connect(m_dirLister
, SIGNAL(infoMessage(const QString
&)),
124 this, SLOT(showInfoMessage(const QString
&)));
125 connect(m_dirLister
, SIGNAL(errorMessage(const QString
&)),
126 this, SLOT(showErrorMessage(const QString
&)));
127 connect(m_dirLister
, SIGNAL(urlIsFileError(const KUrl
&)),
128 this, SLOT(openFile(const KUrl
&)));
130 m_view
= new DolphinView(this,
135 connect(m_view
, SIGNAL(urlChanged(const KUrl
&)),
136 m_urlNavigator
, SLOT(setUrl(const KUrl
&)));
137 connect(m_view
, SIGNAL(requestContextMenu(KFileItem
, const KUrl
&, const QList
<QAction
*>&)),
138 this, SLOT(openContextMenu(KFileItem
, const KUrl
&, const QList
<QAction
*>&)));
139 connect(m_view
, SIGNAL(contentsMoved(int, int)),
140 this, SLOT(saveContentsPos(int, int)));
141 connect(m_view
, SIGNAL(requestItemInfo(KFileItem
)),
142 this, SLOT(showItemInfo(KFileItem
)));
143 connect(m_view
, SIGNAL(errorMessage(const QString
&)),
144 this, SLOT(showErrorMessage(const QString
&)));
145 connect(m_view
, SIGNAL(infoMessage(const QString
&)),
146 this, SLOT(showInfoMessage(const QString
&)));
147 connect(m_view
, SIGNAL(operationCompletedMessage(const QString
&)),
148 this, SLOT(showOperationCompletedMessage(const QString
&)));
149 connect(m_view
, SIGNAL(itemTriggered(KFileItem
)),
150 this, SLOT(slotItemTriggered(KFileItem
)));
151 connect(m_view
, SIGNAL(startedPathLoading(const KUrl
&)),
152 this, SLOT(saveRootUrl(const KUrl
&)));
153 connect(m_view
, SIGNAL(redirection(KUrl
, KUrl
)),
154 this, SLOT(redirect(KUrl
, KUrl
)));
156 connect(m_urlNavigator
, SIGNAL(urlChanged(const KUrl
&)),
157 this, SLOT(restoreView(const KUrl
&)));
159 m_statusBar
= new DolphinStatusBar(this, m_view
);
161 m_filterBar
= new FilterBar(this);
162 m_filterBar
->setVisible(settings
->filterBar());
163 connect(m_filterBar
, SIGNAL(filterChanged(const QString
&)),
164 this, SLOT(setNameFilter(const QString
&)));
165 connect(m_filterBar
, SIGNAL(closeRequest()),
166 this, SLOT(closeFilterBar()));
167 connect(m_view
, SIGNAL(urlChanged(const KUrl
&)),
168 m_filterBar
, SLOT(clear()));
170 m_topLayout
->addWidget(m_urlNavigator
);
171 m_topLayout
->addWidget(m_view
);
172 m_topLayout
->addWidget(m_filterBar
);
173 m_topLayout
->addWidget(m_statusBar
);
176 DolphinViewContainer::~DolphinViewContainer()
178 m_dirLister
->disconnect();
182 delete m_dolphinModel
;
184 m_dirLister
= 0; // deleted by m_dolphinModel
187 void DolphinViewContainer::setUrl(const KUrl
& newUrl
)
189 m_urlNavigator
->setUrl(newUrl
);
190 if (newUrl
!= m_urlNavigator
->url()) {
191 // Temporary disable the 'File'->'Create New...' menu until
192 // the write permissions can be checked in a fast way at
193 // DolphinViewContainer::slotDirListerCompleted().
194 m_isFolderWritable
= false;
196 m_mainWindow
->newMenu()->menu()->setEnabled(false);
201 const KUrl
& DolphinViewContainer::url() const
203 return m_urlNavigator
->url();
206 void DolphinViewContainer::setActive(bool active
)
208 m_urlNavigator
->setActive(active
);
209 m_view
->setActive(active
);
211 m_mainWindow
->newMenu()->menu()->setEnabled(m_isFolderWritable
);
215 bool DolphinViewContainer::isActive() const
217 Q_ASSERT(m_view
->isActive() == m_urlNavigator
->isActive());
218 return m_view
->isActive();
221 void DolphinViewContainer::refresh()
224 m_statusBar
->refresh();
227 bool DolphinViewContainer::isFilterBarVisible() const
229 return m_filterBar
->isVisible();
232 void DolphinViewContainer::showFilterBar(bool show
)
234 Q_ASSERT(m_filterBar
!= 0);
242 bool DolphinViewContainer::isUrlEditable() const
244 return m_urlNavigator
->isUrlEditable();
247 void DolphinViewContainer::updateProgress(int percent
)
249 if (!m_showProgress
) {
250 // Only show the directory loading progress if the status bar does
251 // not contain another progress information. This means that
252 // the directory loading progress information has the lowest priority.
253 const QString
progressText(m_statusBar
->progressText());
254 const QString
loadingText(i18nc("@info:progress", "Loading folder..."));
255 m_showProgress
= progressText
.isEmpty() || (progressText
== loadingText
);
256 if (m_showProgress
) {
257 m_statusBar
->setProgressText(loadingText
);
258 m_statusBar
->setProgress(0);
262 if (m_showProgress
) {
263 m_statusBar
->setProgress(percent
);
267 void DolphinViewContainer::slotDirListerCompleted()
269 if (m_showProgress
) {
270 m_statusBar
->setProgressText(QString());
271 m_statusBar
->setProgress(100);
272 m_showProgress
= false;
276 QMetaObject::invokeMethod(this, "restoreContentsPos", Qt::QueuedConnection
);
278 // Enable the 'File'->'Create New...' menu only if the directory
280 KFileItem item
= m_dirLister
->rootItem();
282 // it is unclear whether writing is supported
283 m_isFolderWritable
= true;
285 KFileItemListProperties
capabilities(KFileItemList() << item
);
286 m_isFolderWritable
= capabilities
.supportsWriting();
290 m_mainWindow
->newMenu()->menu()->setEnabled(m_isFolderWritable
);
294 void DolphinViewContainer::showItemInfo(const KFileItem
& item
)
297 m_statusBar
->clear();
299 m_statusBar
->setMessage(item
.getStatusBarInfo(), DolphinStatusBar::Default
);
303 void DolphinViewContainer::showInfoMessage(const QString
& msg
)
305 m_statusBar
->setMessage(msg
, DolphinStatusBar::Information
);
308 void DolphinViewContainer::showErrorMessage(const QString
& msg
)
310 m_statusBar
->setMessage(msg
, DolphinStatusBar::Error
);
313 void DolphinViewContainer::showOperationCompletedMessage(const QString
& msg
)
315 m_statusBar
->setMessage(msg
, DolphinStatusBar::OperationCompleted
);
318 void DolphinViewContainer::closeFilterBar()
321 m_filterBar
->clear();
323 emit
showFilterBarChanged(false);
326 void DolphinViewContainer::updateStatusBar()
328 // As the item count information is less important
329 // in comparison with other messages, it should only
331 // - the status bar is empty or
332 // - shows already the item count information or
333 // - shows only a not very important information
334 // - if any progress is given don't show the item count info at all
335 const QString
msg(m_statusBar
->message());
336 const bool updateStatusBarMsg
= (msg
.isEmpty() ||
337 (msg
== m_statusBar
->defaultText()) ||
338 (m_statusBar
->type() == DolphinStatusBar::Information
)) &&
339 (m_statusBar
->progress() == 100);
341 const QString
text(m_view
->statusBarText());
342 m_statusBar
->setDefaultText(text
);
344 if (updateStatusBarMsg
) {
345 m_statusBar
->setMessage(text
, DolphinStatusBar::Default
);
349 void DolphinViewContainer::setNameFilter(const QString
& nameFilter
)
351 m_view
->setNameFilter(nameFilter
);
355 void DolphinViewContainer::openContextMenu(const KFileItem
& item
,
357 const QList
<QAction
*>& customActions
)
359 DolphinContextMenu
contextMenu(m_mainWindow
, item
, url
);
360 contextMenu
.setCustomActions(customActions
);
364 void DolphinViewContainer::saveContentsPos(int x
, int y
)
366 m_urlNavigator
->savePosition(x
, y
);
369 void DolphinViewContainer::restoreContentsPos()
371 if (!url().isEmpty()) {
372 const QPoint pos
= m_urlNavigator
->savedPosition();
373 m_view
->setContentsPosition(pos
.x(), pos
.y());
377 void DolphinViewContainer::activate()
382 void DolphinViewContainer::restoreView(const KUrl
& url
)
384 if (KProtocolManager::supportsListing(url
)) {
385 m_view
->updateView(url
, m_urlNavigator
->savedRootUrl());
387 // When an URL has been entered, the view should get the focus.
388 // The focus must be requested asynchronously, as changing the URL might create
389 // a new view widget. Using QTimer::singleShow() works reliable, however
390 // QMetaObject::invokeMethod() with a queued connection does not work, which might
391 // indicate that we should pass a hint to DolphinView::updateView()
392 // regarding the focus instead. To test: Enter an URL and press CTRL+Enter.
393 // Expected result: The view should get the focus.
394 QTimer::singleShot(0, this, SLOT(requestFocus()));
396 } else if (KProtocolManager::isSourceProtocol(url
)) {
397 QString app
= "konqueror";
398 if (url
.protocol().startsWith(QLatin1String("http"))) {
399 showErrorMessage(i18nc("@info:status",
400 "Dolphin does not support web pages, the web browser has been launched"));
401 const KConfigGroup
config(KSharedConfig::openConfig("kdeglobals"), "General");
402 const QString browser
= config
.readEntry("BrowserApplication");
403 if (!browser
.isEmpty()) {
405 if (app
.startsWith('!')) {
406 // a literal command has been configured, remove the '!' prefix
411 showErrorMessage(i18nc("@info:status",
412 "Protocol not supported by Dolphin, Konqueror has been launched"));
415 QString secureUrl
= KShell::quoteArg(url
.pathOrUrl());
416 const QString command
= app
+ ' ' + secureUrl
;
417 KRun::runCommand(command
, app
, app
, this);
419 showErrorMessage(i18nc("@info:status", "Invalid protocol"));
423 void DolphinViewContainer::saveRootUrl(const KUrl
& url
)
426 m_urlNavigator
->saveRootUrl(m_view
->rootUrl());
429 void DolphinViewContainer::dropUrls(const KUrl
& destination
, QDropEvent
* event
)
431 DragAndDropHelper::instance().dropUrls(KFileItem(), destination
, event
, this);
434 void DolphinViewContainer::redirect(const KUrl
& oldUrl
, const KUrl
& newUrl
)
437 const bool block
= m_urlNavigator
->signalsBlocked();
438 m_urlNavigator
->blockSignals(true);
439 m_urlNavigator
->setUrl(newUrl
);
440 m_urlNavigator
->blockSignals(block
);
443 void DolphinViewContainer::requestFocus()
448 void DolphinViewContainer::saveUrlCompletionMode(KGlobalSettings::Completion completion
)
450 DolphinSettings
& settings
= DolphinSettings::instance();
451 settings
.generalSettings()->setUrlCompletionMode(completion
);
455 void DolphinViewContainer::slotItemTriggered(const KFileItem
& item
)
457 KUrl url
= item
.targetUrl();
464 const GeneralSettings
* settings
= DolphinSettings::instance().generalSettings();
465 const bool browseThroughArchives
= settings
->browseThroughArchives();
466 if (browseThroughArchives
&& item
.isFile() && url
.isLocalFile()) {
467 // Generic mechanism for redirecting to tar:/<path>/ when clicking on a tar file,
468 // zip:/<path>/ when clicking on a zip file, etc.
469 // The .protocol file specifies the mimetype that the kioslave handles.
470 // Note that we don't use mimetype inheritance since we don't want to
471 // open OpenDocument files as zip folders...
472 const QString protocol
= KProtocolManager::protocolForArchiveMimetype(item
.mimetype());
473 if (!protocol
.isEmpty()) {
474 url
.setProtocol(protocol
);
483 void DolphinViewContainer::openFile(const KUrl
& url
)
485 // Using m_dolphinModel for getting the file item instance is not possible
486 // here: openFile() is triggered by an error of the directory lister
487 // job, so the file item must be received "manually".
488 const KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, url
);
489 slotItemTriggered(item
);
492 #include "dolphinviewcontainer.moc"