]>
cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinviewcontainer.cpp
95b16448a3b553d34aeb94a98ecbb514b287c9a3
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>
45 #include <kurlcombobox.h>
48 #include "dolphinmodel.h"
49 #include "dolphincolumnview.h"
50 #include "dolphincontroller.h"
51 #include "dolphinstatusbar.h"
52 #include "dolphinmainwindow.h"
53 #include "dolphindirlister.h"
54 #include "dolphinsortfilterproxymodel.h"
55 #include "dolphindetailsview.h"
56 #include "dolphiniconsview.h"
57 #include "dolphincontextmenu.h"
58 #include "draganddrophelper.h"
59 #include "filterbar.h"
60 #include "kurlnavigator.h"
61 #include "viewproperties.h"
62 #include "settings/dolphinsettings.h"
63 #include "dolphin_generalsettings.h"
65 DolphinViewContainer::DolphinViewContainer(DolphinMainWindow
* mainWindow
,
69 m_showProgress(false),
70 m_isFolderWritable(false),
71 m_mainWindow(mainWindow
),
82 m_topLayout
= new QVBoxLayout(this);
83 m_topLayout
->setSpacing(0);
84 m_topLayout
->setMargin(0);
86 m_urlNavigator
= new KUrlNavigator(DolphinSettings::instance().placesModel(), url
, this);
87 connect(m_urlNavigator
, SIGNAL(urlsDropped(const KUrl
&, QDropEvent
*)),
88 this, SLOT(dropUrls(const KUrl
&, QDropEvent
*)));
89 connect(m_urlNavigator
, SIGNAL(activated()),
90 this, SLOT(activate()));
91 connect(m_urlNavigator
->editor(), SIGNAL(completionModeChanged(KGlobalSettings::Completion
)),
92 this, SLOT(saveUrlCompletionMode(KGlobalSettings::Completion
)));
94 const GeneralSettings
* settings
= DolphinSettings::instance().generalSettings();
95 m_urlNavigator
->setUrlEditable(settings
->editableUrl());
96 m_urlNavigator
->setShowFullPath(settings
->showFullPath());
97 m_urlNavigator
->setHomeUrl(settings
->homeUrl());
98 KUrlComboBox
* editor
= m_urlNavigator
->editor();
99 editor
->setCompletionMode(KGlobalSettings::Completion(settings
->urlCompletionMode()));
101 m_dirLister
= new DolphinDirLister();
102 m_dirLister
->setAutoUpdate(true);
103 m_dirLister
->setMainWindow(window());
104 m_dirLister
->setDelayedMimeTypes(true);
106 m_dolphinModel
= new DolphinModel(this);
107 m_dolphinModel
->setDirLister(m_dirLister
);
108 m_dolphinModel
->setDropsAllowed(DolphinModel::DropOnDirectory
);
110 m_proxyModel
= new DolphinSortFilterProxyModel(this);
111 m_proxyModel
->setSourceModel(m_dolphinModel
);
112 m_proxyModel
->setFilterCaseSensitivity(Qt::CaseInsensitive
);
114 connect(m_dirLister
, SIGNAL(clear()),
115 this, SLOT(updateStatusBar()));
116 connect(m_dirLister
, SIGNAL(percent(int)),
117 this, SLOT(updateProgress(int)));
118 connect(m_dirLister
, SIGNAL(deleteItem(const KFileItem
&)),
119 this, SLOT(updateStatusBar()));
120 connect(m_dirLister
, SIGNAL(completed()),
121 this, SLOT(slotDirListerCompleted()));
122 connect(m_dirLister
, SIGNAL(infoMessage(const QString
&)),
123 this, SLOT(showInfoMessage(const QString
&)));
124 connect(m_dirLister
, SIGNAL(errorMessage(const QString
&)),
125 this, SLOT(showErrorMessage(const QString
&)));
126 connect(m_dirLister
, SIGNAL(urlIsFileError(const KUrl
&)),
127 this, SLOT(openFile(const KUrl
&)));
129 m_view
= new DolphinView(this,
134 connect(m_view
, SIGNAL(urlChanged(const KUrl
&)),
135 m_urlNavigator
, SLOT(setUrl(const KUrl
&)));
136 connect(m_view
, SIGNAL(requestContextMenu(KFileItem
, const KUrl
&, const QList
<QAction
*>&)),
137 this, SLOT(openContextMenu(KFileItem
, const KUrl
&, const QList
<QAction
*>&)));
138 connect(m_view
, SIGNAL(contentsMoved(int, int)),
139 this, SLOT(saveContentsPos(int, int)));
140 connect(m_view
, SIGNAL(requestItemInfo(KFileItem
)),
141 this, SLOT(showItemInfo(KFileItem
)));
142 connect(m_view
, SIGNAL(errorMessage(const QString
&)),
143 this, SLOT(showErrorMessage(const QString
&)));
144 connect(m_view
, SIGNAL(infoMessage(const QString
&)),
145 this, SLOT(showInfoMessage(const QString
&)));
146 connect(m_view
, SIGNAL(operationCompletedMessage(const QString
&)),
147 this, SLOT(showOperationCompletedMessage(const QString
&)));
148 connect(m_view
, SIGNAL(itemTriggered(KFileItem
)),
149 this, SLOT(slotItemTriggered(KFileItem
)));
150 connect(m_view
, SIGNAL(startedPathLoading(const KUrl
&)),
151 this, SLOT(saveRootUrl(const KUrl
&)));
152 connect(m_view
, SIGNAL(redirection(KUrl
, KUrl
)),
153 this, SLOT(redirect(KUrl
, KUrl
)));
155 connect(m_urlNavigator
, SIGNAL(urlChanged(const KUrl
&)),
156 this, SLOT(restoreView(const KUrl
&)));
158 m_statusBar
= new DolphinStatusBar(this, m_view
);
160 m_filterBar
= new FilterBar(this);
161 m_filterBar
->setVisible(settings
->filterBar());
162 connect(m_filterBar
, SIGNAL(filterChanged(const QString
&)),
163 this, SLOT(setNameFilter(const QString
&)));
164 connect(m_filterBar
, SIGNAL(closeRequest()),
165 this, SLOT(closeFilterBar()));
166 connect(m_view
, SIGNAL(urlChanged(const KUrl
&)),
167 m_filterBar
, SLOT(clear()));
169 m_topLayout
->addWidget(m_urlNavigator
);
170 m_topLayout
->addWidget(m_view
);
171 m_topLayout
->addWidget(m_filterBar
);
172 m_topLayout
->addWidget(m_statusBar
);
175 DolphinViewContainer::~DolphinViewContainer()
177 m_dirLister
->disconnect();
181 delete m_dolphinModel
;
183 m_dirLister
= 0; // deleted by m_dolphinModel
186 void DolphinViewContainer::setUrl(const KUrl
& newUrl
)
188 m_urlNavigator
->setUrl(newUrl
);
189 if (newUrl
!= m_urlNavigator
->url()) {
190 // Temporary disable the 'File'->'Create New...' menu until
191 // the write permissions can be checked in a fast way at
192 // DolphinViewContainer::slotDirListerCompleted().
193 m_isFolderWritable
= false;
195 m_mainWindow
->newMenu()->menu()->setEnabled(false);
200 const KUrl
& DolphinViewContainer::url() const
202 return m_urlNavigator
->url();
205 void DolphinViewContainer::setActive(bool active
)
207 m_urlNavigator
->setActive(active
);
208 m_view
->setActive(active
);
210 m_mainWindow
->newMenu()->menu()->setEnabled(m_isFolderWritable
);
214 bool DolphinViewContainer::isActive() const
216 Q_ASSERT(m_view
->isActive() == m_urlNavigator
->isActive());
217 return m_view
->isActive();
220 void DolphinViewContainer::refresh()
223 m_statusBar
->refresh();
226 bool DolphinViewContainer::isFilterBarVisible() const
228 return m_filterBar
->isVisible();
231 void DolphinViewContainer::showFilterBar(bool show
)
233 Q_ASSERT(m_filterBar
!= 0);
241 bool DolphinViewContainer::isUrlEditable() const
243 return m_urlNavigator
->isUrlEditable();
246 void DolphinViewContainer::updateProgress(int percent
)
248 if (!m_showProgress
) {
249 // Only show the directory loading progress if the status bar does
250 // not contain another progress information. This means that
251 // the directory loading progress information has the lowest priority.
252 const QString
progressText(m_statusBar
->progressText());
253 const QString
loadingText(i18nc("@info:progress", "Loading folder..."));
254 m_showProgress
= progressText
.isEmpty() || (progressText
== loadingText
);
255 if (m_showProgress
) {
256 m_statusBar
->setProgressText(loadingText
);
257 m_statusBar
->setProgress(0);
261 if (m_showProgress
) {
262 m_statusBar
->setProgress(percent
);
266 void DolphinViewContainer::slotDirListerCompleted()
268 if (m_showProgress
) {
269 m_statusBar
->setProgressText(QString());
270 m_statusBar
->setProgress(100);
271 m_showProgress
= false;
275 QMetaObject::invokeMethod(this, "restoreContentsPos", Qt::QueuedConnection
);
277 // Enable the 'File'->'Create New...' menu only if the directory
279 KFileItem item
= m_dirLister
->rootItem();
281 // it is unclear whether writing is supported
282 m_isFolderWritable
= true;
284 KonqFileItemCapabilities
capabilities(KFileItemList() << item
);
285 m_isFolderWritable
= capabilities
.supportsWriting();
289 m_mainWindow
->newMenu()->menu()->setEnabled(m_isFolderWritable
);
293 void DolphinViewContainer::showItemInfo(const KFileItem
& item
)
296 m_statusBar
->clear();
298 m_statusBar
->setMessage(item
.getStatusBarInfo(), DolphinStatusBar::Default
);
302 void DolphinViewContainer::showInfoMessage(const QString
& msg
)
304 m_statusBar
->setMessage(msg
, DolphinStatusBar::Information
);
307 void DolphinViewContainer::showErrorMessage(const QString
& msg
)
309 m_statusBar
->setMessage(msg
, DolphinStatusBar::Error
);
312 void DolphinViewContainer::showOperationCompletedMessage(const QString
& msg
)
314 m_statusBar
->setMessage(msg
, DolphinStatusBar::OperationCompleted
);
317 void DolphinViewContainer::closeFilterBar()
320 m_filterBar
->clear();
322 emit
showFilterBarChanged(false);
325 void DolphinViewContainer::updateStatusBar()
327 // As the item count information is less important
328 // in comparison with other messages, it should only
330 // - the status bar is empty or
331 // - shows already the item count information or
332 // - shows only a not very important information
333 // - if any progress is given don't show the item count info at all
334 const QString
msg(m_statusBar
->message());
335 const bool updateStatusBarMsg
= (msg
.isEmpty() ||
336 (msg
== m_statusBar
->defaultText()) ||
337 (m_statusBar
->type() == DolphinStatusBar::Information
)) &&
338 (m_statusBar
->progress() == 100);
340 const QString
text(m_view
->statusBarText());
341 m_statusBar
->setDefaultText(text
);
343 if (updateStatusBarMsg
) {
344 m_statusBar
->setMessage(text
, DolphinStatusBar::Default
);
348 void DolphinViewContainer::setNameFilter(const QString
& nameFilter
)
350 m_view
->setNameFilter(nameFilter
);
354 void DolphinViewContainer::openContextMenu(const KFileItem
& item
,
356 const QList
<QAction
*>& customActions
)
358 DolphinContextMenu
contextMenu(m_mainWindow
, item
, url
);
359 contextMenu
.setCustomActions(customActions
);
363 void DolphinViewContainer::saveContentsPos(int x
, int y
)
365 m_urlNavigator
->savePosition(x
, y
);
368 void DolphinViewContainer::restoreContentsPos()
370 if (!url().isEmpty()) {
371 const QPoint pos
= m_urlNavigator
->savedPosition();
372 m_view
->setContentsPosition(pos
.x(), pos
.y());
376 void DolphinViewContainer::activate()
381 void DolphinViewContainer::restoreView(const KUrl
& url
)
383 if (KProtocolManager::supportsListing(url
)) {
384 m_view
->updateView(url
, m_urlNavigator
->savedRootUrl());
386 // When an URL has been entered, the view should get the focus.
387 // The focus must be requested asynchronously, as changing the URL might create
388 // a new view widget. Using QTimer::singleShow() works reliable, however
389 // QMetaObject::invokeMethod() with a queued connection does not work, which might
390 // indicate that we should pass a hint to DolphinView::updateView()
391 // regarding the focus instead. To test: Enter an URL and press CTRL+Enter.
392 // Expected result: The view should get the focus.
393 QTimer::singleShot(0, this, SLOT(requestFocus()));
395 } else if (KProtocolManager::isSourceProtocol(url
)) {
396 QString app
= "konqueror";
397 if (url
.protocol().startsWith(QLatin1String("http"))) {
398 showErrorMessage(i18nc("@info:status",
399 "Dolphin does not support web pages, the web browser has been launched"));
400 const KConfigGroup
config(KSharedConfig::openConfig("kdeglobals"), "General");
401 const QString browser
= config
.readEntry("BrowserApplication");
402 if (!browser
.isEmpty()) {
406 showErrorMessage(i18nc("@info:status",
407 "Protocol not supported by Dolphin, Konqueror has been launched"));
409 const QString command
= app
+ ' ' + url
.pathOrUrl();
410 KRun::runCommand(command
, app
, app
, this);
412 showErrorMessage(i18nc("@info:status", "Invalid protocol"));
416 void DolphinViewContainer::saveRootUrl(const KUrl
& url
)
419 m_urlNavigator
->saveRootUrl(m_view
->rootUrl());
422 void DolphinViewContainer::dropUrls(const KUrl
& destination
, QDropEvent
* event
)
424 DragAndDropHelper::instance().dropUrls(KFileItem(), destination
, event
, this);
427 void DolphinViewContainer::redirect(const KUrl
& oldUrl
, const KUrl
& newUrl
)
430 const bool block
= m_urlNavigator
->signalsBlocked();
431 m_urlNavigator
->blockSignals(true);
432 m_urlNavigator
->setUrl(newUrl
);
433 m_urlNavigator
->blockSignals(block
);
436 void DolphinViewContainer::requestFocus()
441 void DolphinViewContainer::saveUrlCompletionMode(KGlobalSettings::Completion completion
)
443 DolphinSettings
& settings
= DolphinSettings::instance();
444 settings
.generalSettings()->setUrlCompletionMode(completion
);
448 void DolphinViewContainer::slotItemTriggered(const KFileItem
& item
)
450 KUrl url
= item
.targetUrl();
457 const GeneralSettings
* settings
= DolphinSettings::instance().generalSettings();
458 const bool browseThroughArchives
= settings
->browseThroughArchives();
459 if (browseThroughArchives
&& item
.isFile() && url
.isLocalFile()) {
460 // Generic mechanism for redirecting to tar:/<path>/ when clicking on a tar file,
461 // zip:/<path>/ when clicking on a zip file, etc.
462 // The .protocol file specifies the mimetype that the kioslave handles.
463 // Note that we don't use mimetype inheritance since we don't want to
464 // open OpenDocument files as zip folders...
465 const QString protocol
= KProtocolManager::protocolForArchiveMimetype(item
.mimetype());
466 if (!protocol
.isEmpty()) {
467 url
.setProtocol(protocol
);
476 void DolphinViewContainer::openFile(const KUrl
& url
)
478 // Using m_dolphinModel for getting the file item instance is not possible
479 // here: openFile() is triggered by an error of the directory lister
480 // job, so the file item must be received "manually".
481 const KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, url
);
482 slotItemTriggered(item
);
485 #include "dolphinviewcontainer.moc"