]>
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 <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
),
84 m_topLayout
= new QVBoxLayout(this);
85 m_topLayout
->setSpacing(0);
86 m_topLayout
->setMargin(0);
88 m_urlNavigator
= new KUrlNavigator(DolphinSettings::instance().placesModel(), url
, this);
89 connect(m_urlNavigator
, SIGNAL(urlsDropped(const KUrl
&, QDropEvent
*)),
90 this, SLOT(dropUrls(const KUrl
&, QDropEvent
*)));
91 connect(m_urlNavigator
, SIGNAL(activated()),
92 this, SLOT(activate()));
93 connect(m_urlNavigator
->editor(), SIGNAL(completionModeChanged(KGlobalSettings::Completion
)),
94 this, SLOT(saveUrlCompletionMode(KGlobalSettings::Completion
)));
96 const GeneralSettings
* settings
= DolphinSettings::instance().generalSettings();
97 m_urlNavigator
->setUrlEditable(settings
->editableUrl());
98 m_urlNavigator
->setShowFullPath(settings
->showFullPath());
99 m_urlNavigator
->setHomeUrl(settings
->homeUrl());
100 KUrlComboBox
* editor
= m_urlNavigator
->editor();
101 editor
->setCompletionMode(KGlobalSettings::Completion(settings
->urlCompletionMode()));
103 m_dirLister
= new DolphinDirLister();
104 m_dirLister
->setAutoUpdate(true);
105 m_dirLister
->setMainWindow(window());
106 m_dirLister
->setDelayedMimeTypes(true);
108 m_dolphinModel
= new DolphinModel(this);
109 m_dolphinModel
->setDirLister(m_dirLister
);
110 m_dolphinModel
->setDropsAllowed(DolphinModel::DropOnDirectory
);
112 m_proxyModel
= new DolphinSortFilterProxyModel(this);
113 m_proxyModel
->setSourceModel(m_dolphinModel
);
114 m_proxyModel
->setFilterCaseSensitivity(Qt::CaseInsensitive
);
116 connect(m_dirLister
, SIGNAL(clear()),
117 this, SLOT(delayedStatusBarUpdate()));
118 connect(m_dirLister
, SIGNAL(percent(int)),
119 this, SLOT(updateProgress(int)));
120 connect(m_dirLister
, SIGNAL(itemsDeleted(const KFileItemList
&)),
121 this, SLOT(delayedStatusBarUpdate()));
122 connect(m_dirLister
, SIGNAL(completed()),
123 this, SLOT(slotDirListerCompleted()));
124 connect(m_dirLister
, SIGNAL(infoMessage(const QString
&)),
125 this, SLOT(showInfoMessage(const QString
&)));
126 connect(m_dirLister
, SIGNAL(errorMessage(const QString
&)),
127 this, SLOT(showErrorMessage(const QString
&)));
128 connect(m_dirLister
, SIGNAL(urlIsFileError(const KUrl
&)),
129 this, SLOT(openFile(const KUrl
&)));
131 m_view
= new DolphinView(this,
136 connect(m_view
, SIGNAL(urlChanged(const KUrl
&)),
137 m_urlNavigator
, SLOT(setUrl(const KUrl
&)));
138 connect(m_view
, SIGNAL(requestContextMenu(KFileItem
, const KUrl
&, const QList
<QAction
*>&)),
139 this, SLOT(openContextMenu(KFileItem
, const KUrl
&, const QList
<QAction
*>&)));
140 connect(m_view
, SIGNAL(contentsMoved(int, int)),
141 this, SLOT(saveContentsPos(int, int)));
142 connect(m_view
, SIGNAL(requestItemInfo(KFileItem
)),
143 this, SLOT(showItemInfo(KFileItem
)));
144 connect(m_view
, SIGNAL(errorMessage(const QString
&)),
145 this, SLOT(showErrorMessage(const QString
&)));
146 connect(m_view
, SIGNAL(infoMessage(const QString
&)),
147 this, SLOT(showInfoMessage(const QString
&)));
148 connect(m_view
, SIGNAL(operationCompletedMessage(const QString
&)),
149 this, SLOT(showOperationCompletedMessage(const QString
&)));
150 connect(m_view
, SIGNAL(itemTriggered(KFileItem
)),
151 this, SLOT(slotItemTriggered(KFileItem
)));
152 connect(m_view
, SIGNAL(startedPathLoading(const KUrl
&)),
153 this, SLOT(saveRootUrl(const KUrl
&)));
154 connect(m_view
, SIGNAL(redirection(KUrl
, KUrl
)),
155 this, SLOT(redirect(KUrl
, KUrl
)));
156 connect(m_view
, SIGNAL(selectionChanged(const KFileItemList
&)),
157 this, SLOT(delayedStatusBarUpdate()));
159 connect(m_urlNavigator
, SIGNAL(urlChanged(const KUrl
&)),
160 this, SLOT(restoreView(const KUrl
&)));
161 connect(m_urlNavigator
, SIGNAL(historyChanged()),
162 this, SLOT(slotHistoryChanged()));
164 m_statusBar
= new DolphinStatusBar(this, m_view
);
165 m_statusBarTimer
= new QTimer(this);
166 m_statusBarTimer
->setSingleShot(true);
167 m_statusBarTimer
->setInterval(300);
168 connect(m_statusBarTimer
, SIGNAL(timeout()),
169 this, SLOT(updateStatusBar()));
171 m_filterBar
= new FilterBar(this);
172 m_filterBar
->setVisible(settings
->filterBar());
173 connect(m_filterBar
, SIGNAL(filterChanged(const QString
&)),
174 this, SLOT(setNameFilter(const QString
&)));
175 connect(m_filterBar
, SIGNAL(closeRequest()),
176 this, SLOT(closeFilterBar()));
177 connect(m_view
, SIGNAL(urlChanged(const KUrl
&)),
178 m_filterBar
, SLOT(clear()));
180 m_topLayout
->addWidget(m_urlNavigator
);
181 m_topLayout
->addWidget(m_view
);
182 m_topLayout
->addWidget(m_filterBar
);
183 m_topLayout
->addWidget(m_statusBar
);
186 DolphinViewContainer::~DolphinViewContainer()
188 m_dirLister
->disconnect();
192 delete m_dolphinModel
;
194 m_dirLister
= 0; // deleted by m_dolphinModel
197 void DolphinViewContainer::setUrl(const KUrl
& newUrl
)
199 if (newUrl
!= m_urlNavigator
->url()) {
200 m_urlNavigator
->setUrl(newUrl
);
201 // Temporary disable the 'File'->'Create New...' menu until
202 // the write permissions can be checked in a fast way at
203 // DolphinViewContainer::slotDirListerCompleted().
204 m_isFolderWritable
= false;
206 m_mainWindow
->newMenu()->menu()->setEnabled(false);
211 const KUrl
& DolphinViewContainer::url() const
213 return m_urlNavigator
->url();
216 void DolphinViewContainer::setActive(bool active
)
218 m_urlNavigator
->setActive(active
);
219 m_view
->setActive(active
);
221 m_mainWindow
->newMenu()->menu()->setEnabled(m_isFolderWritable
);
225 bool DolphinViewContainer::isActive() const
227 Q_ASSERT(m_view
->isActive() == m_urlNavigator
->isActive());
228 return m_view
->isActive();
231 void DolphinViewContainer::refresh()
234 m_statusBar
->refresh();
237 bool DolphinViewContainer::isFilterBarVisible() const
239 return m_filterBar
->isVisible();
242 void DolphinViewContainer::showFilterBar(bool show
)
244 Q_ASSERT(m_filterBar
!= 0);
252 bool DolphinViewContainer::isUrlEditable() const
254 return m_urlNavigator
->isUrlEditable();
257 void DolphinViewContainer::delayedStatusBarUpdate()
259 // Invoke updateStatusBar() with a small delay. This assures that
260 // when a lot of delayedStatusBarUpdates() are done in a short time,
261 // no bottleneck is given.
262 m_statusBarTimer
->start();
265 void DolphinViewContainer::updateStatusBar()
267 // As the item count information is less important
268 // in comparison with other messages, it should only
270 // - the status bar is empty or
271 // - shows already the item count information or
272 // - shows only a not very important information
273 // - if any progress is given don't show the item count info at all
274 const QString
msg(m_statusBar
->message());
275 const bool updateStatusBarMsg
= (msg
.isEmpty()
276 || (msg
== m_statusBar
->defaultText())
277 || (m_statusBar
->type() == DolphinStatusBar::Information
))
278 && (m_statusBar
->progress() == 100);
280 const QString
text(m_view
->statusBarText());
281 m_statusBar
->setDefaultText(text
);
283 if (updateStatusBarMsg
) {
284 m_statusBar
->setMessage(text
, DolphinStatusBar::Default
);
288 void DolphinViewContainer::updateProgress(int percent
)
290 if (!m_showProgress
) {
291 // Only show the directory loading progress if the status bar does
292 // not contain another progress information. This means that
293 // the directory loading progress information has the lowest priority.
294 const QString
progressText(m_statusBar
->progressText());
295 const QString
loadingText(i18nc("@info:progress", "Loading folder..."));
296 m_showProgress
= progressText
.isEmpty() || (progressText
== loadingText
);
297 if (m_showProgress
) {
298 m_statusBar
->setProgressText(loadingText
);
299 m_statusBar
->setProgress(0);
303 if (m_showProgress
) {
304 m_statusBar
->setProgress(percent
);
308 void DolphinViewContainer::slotDirListerCompleted()
310 if (m_showProgress
) {
311 m_statusBar
->setProgressText(QString());
312 m_statusBar
->setProgress(100);
313 m_showProgress
= false;
316 delayedStatusBarUpdate();
317 QMetaObject::invokeMethod(this, "restoreContentsPos", Qt::QueuedConnection
);
319 // Enable the 'File'->'Create New...' menu only if the directory
321 KFileItem item
= m_dirLister
->rootItem();
323 // it is unclear whether writing is supported
324 m_isFolderWritable
= true;
326 KFileItemListProperties
capabilities(KFileItemList() << item
);
327 m_isFolderWritable
= capabilities
.supportsWriting();
331 m_mainWindow
->newMenu()->menu()->setEnabled(m_isFolderWritable
);
335 void DolphinViewContainer::showItemInfo(const KFileItem
& item
)
338 m_statusBar
->clear();
340 m_statusBar
->setMessage(item
.getStatusBarInfo(), DolphinStatusBar::Default
);
344 void DolphinViewContainer::showInfoMessage(const QString
& msg
)
346 m_statusBar
->setMessage(msg
, DolphinStatusBar::Information
);
349 void DolphinViewContainer::showErrorMessage(const QString
& msg
)
351 m_statusBar
->setMessage(msg
, DolphinStatusBar::Error
);
354 void DolphinViewContainer::showOperationCompletedMessage(const QString
& msg
)
356 m_statusBar
->setMessage(msg
, DolphinStatusBar::OperationCompleted
);
359 void DolphinViewContainer::closeFilterBar()
362 m_filterBar
->clear();
364 emit
showFilterBarChanged(false);
367 void DolphinViewContainer::setNameFilter(const QString
& nameFilter
)
369 m_view
->setNameFilter(nameFilter
);
370 delayedStatusBarUpdate();
373 void DolphinViewContainer::openContextMenu(const KFileItem
& item
,
375 const QList
<QAction
*>& customActions
)
377 DolphinContextMenu
contextMenu(m_mainWindow
, item
, url
);
378 contextMenu
.setCustomActions(customActions
);
382 void DolphinViewContainer::saveContentsPos(int x
, int y
)
384 m_urlNavigator
->savePosition(x
, y
);
387 void DolphinViewContainer::restoreContentsPos()
389 if (!url().isEmpty()) {
390 const QPoint pos
= m_urlNavigator
->savedPosition();
391 m_view
->setContentsPosition(pos
.x(), pos
.y());
395 void DolphinViewContainer::activate()
400 void DolphinViewContainer::restoreView(const KUrl
& url
)
402 if (KProtocolManager::supportsListing(url
)) {
403 m_view
->updateView(url
, m_urlNavigator
->savedRootUrl());
405 // When an URL has been entered, the view should get the focus.
406 // The focus must be requested asynchronously, as changing the URL might create
407 // a new view widget. Using QTimer::singleShow() works reliable, however
408 // QMetaObject::invokeMethod() with a queued connection does not work, which might
409 // indicate that we should pass a hint to DolphinView::updateView()
410 // regarding the focus instead. To test: Enter an URL and press CTRL+Enter.
411 // Expected result: The view should get the focus.
412 QTimer::singleShot(0, this, SLOT(requestFocus()));
414 } else if (KProtocolManager::isSourceProtocol(url
)) {
415 QString app
= "konqueror";
416 if (url
.protocol().startsWith(QLatin1String("http"))) {
417 showErrorMessage(i18nc("@info:status",
418 "Dolphin does not support web pages, the web browser has been launched"));
419 const KConfigGroup
config(KSharedConfig::openConfig("kdeglobals"), "General");
420 const QString browser
= config
.readEntry("BrowserApplication");
421 if (!browser
.isEmpty()) {
423 if (app
.startsWith('!')) {
424 // a literal command has been configured, remove the '!' prefix
429 showErrorMessage(i18nc("@info:status",
430 "Protocol not supported by Dolphin, Konqueror has been launched"));
433 QString secureUrl
= KShell::quoteArg(url
.pathOrUrl());
434 const QString command
= app
+ ' ' + secureUrl
;
435 KRun::runCommand(command
, app
, app
, this);
437 showErrorMessage(i18nc("@info:status", "Invalid protocol"));
441 void DolphinViewContainer::saveRootUrl(const KUrl
& url
)
444 m_urlNavigator
->saveRootUrl(m_view
->rootUrl());
447 void DolphinViewContainer::dropUrls(const KUrl
& destination
, QDropEvent
* event
)
449 DragAndDropHelper::instance().dropUrls(KFileItem(), destination
, event
, this);
452 void DolphinViewContainer::redirect(const KUrl
& oldUrl
, const KUrl
& newUrl
)
455 const bool block
= m_urlNavigator
->signalsBlocked();
456 m_urlNavigator
->blockSignals(true);
457 m_urlNavigator
->setUrl(newUrl
);
458 m_urlNavigator
->blockSignals(block
);
461 void DolphinViewContainer::requestFocus()
466 void DolphinViewContainer::saveUrlCompletionMode(KGlobalSettings::Completion completion
)
468 DolphinSettings
& settings
= DolphinSettings::instance();
469 settings
.generalSettings()->setUrlCompletionMode(completion
);
473 void DolphinViewContainer::slotHistoryChanged()
475 const int index
= m_urlNavigator
->historyIndex();
477 // The "Go Forward" action is enabled. Try to mark
478 // the previous directory as active item:
479 const KUrl url
= m_urlNavigator
->historyUrl(index
- 1);
480 m_view
->activateItem(url
);
484 void DolphinViewContainer::slotItemTriggered(const KFileItem
& item
)
486 KUrl url
= item
.targetUrl();
493 const GeneralSettings
* settings
= DolphinSettings::instance().generalSettings();
494 const bool browseThroughArchives
= settings
->browseThroughArchives();
495 if (browseThroughArchives
&& item
.isFile() && url
.isLocalFile()) {
496 // Generic mechanism for redirecting to tar:/<path>/ when clicking on a tar file,
497 // zip:/<path>/ when clicking on a zip file, etc.
498 // The .protocol file specifies the mimetype that the kioslave handles.
499 // Note that we don't use mimetype inheritance since we don't want to
500 // open OpenDocument files as zip folders...
501 const QString protocol
= KProtocolManager::protocolForArchiveMimetype(item
.mimetype());
502 if (!protocol
.isEmpty()) {
503 url
.setProtocol(protocol
);
512 void DolphinViewContainer::openFile(const KUrl
& url
)
514 // Using m_dolphinModel for getting the file item instance is not possible
515 // here: openFile() is triggered by an error of the directory lister
516 // job, so the file item must be received "manually".
517 const KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, url
);
518 slotItemTriggered(item
);
521 #include "dolphinviewcontainer.moc"