]>
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>
47 #include <kurlnavigator.h>
50 #include "dolphinmodel.h"
51 #include "dolphincolumnview.h"
52 #include "dolphincontroller.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 "statusbar/dolphinstatusbar.h"
62 #include "viewproperties.h"
63 #include "settings/dolphinsettings.h"
64 #include "dolphin_generalsettings.h"
66 DolphinViewContainer::DolphinViewContainer(DolphinMainWindow
* mainWindow
,
70 m_isFolderWritable(false),
71 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(started(KUrl
)),
116 this, SLOT(initializeProgress()));
117 connect(m_dirLister
, SIGNAL(clear()),
118 this, SLOT(delayedStatusBarUpdate()));
119 connect(m_dirLister
, SIGNAL(percent(int)),
120 this, SLOT(updateProgress(int)));
121 connect(m_dirLister
, SIGNAL(itemsDeleted(const KFileItemList
&)),
122 this, SLOT(delayedStatusBarUpdate()));
123 connect(m_dirLister
, SIGNAL(completed()),
124 this, SLOT(slotDirListerCompleted()));
125 connect(m_dirLister
, SIGNAL(infoMessage(const QString
&)),
126 this, SLOT(showInfoMessage(const QString
&)));
127 connect(m_dirLister
, SIGNAL(errorMessage(const QString
&)),
128 this, SLOT(showErrorMessage(const QString
&)));
129 connect(m_dirLister
, SIGNAL(urlIsFileError(const KUrl
&)),
130 this, SLOT(openFile(const KUrl
&)));
132 m_view
= new DolphinView(this, url
, m_proxyModel
);
133 connect(m_view
, SIGNAL(urlChanged(const KUrl
&)),
134 m_urlNavigator
, SLOT(setUrl(const KUrl
&)));
135 connect(m_view
, SIGNAL(requestContextMenu(KFileItem
, const KUrl
&, const QList
<QAction
*>&)),
136 this, SLOT(openContextMenu(KFileItem
, const KUrl
&, const QList
<QAction
*>&)));
137 connect(m_view
, SIGNAL(contentsMoved(int, int)),
138 this, SLOT(saveContentsPos(int, int)));
139 connect(m_view
, SIGNAL(requestItemInfo(KFileItem
)),
140 this, SLOT(showItemInfo(KFileItem
)));
141 connect(m_view
, SIGNAL(errorMessage(const QString
&)),
142 this, SLOT(showErrorMessage(const QString
&)));
143 connect(m_view
, SIGNAL(infoMessage(const QString
&)),
144 this, SLOT(showInfoMessage(const QString
&)));
145 connect(m_view
, SIGNAL(operationCompletedMessage(const QString
&)),
146 this, SLOT(showOperationCompletedMessage(const QString
&)));
147 connect(m_view
, SIGNAL(itemTriggered(KFileItem
)),
148 this, SLOT(slotItemTriggered(KFileItem
)));
149 connect(m_view
, SIGNAL(startedPathLoading(const KUrl
&)),
150 this, SLOT(saveRootUrl(const KUrl
&)));
151 connect(m_view
, SIGNAL(redirection(KUrl
, KUrl
)),
152 this, SLOT(redirect(KUrl
, KUrl
)));
153 connect(m_view
, SIGNAL(selectionChanged(const KFileItemList
&)),
154 this, SLOT(delayedStatusBarUpdate()));
156 connect(m_urlNavigator
, SIGNAL(urlChanged(const KUrl
&)),
157 this, SLOT(restoreView(const KUrl
&)));
158 connect(m_urlNavigator
, SIGNAL(historyChanged()),
159 this, SLOT(slotHistoryChanged()));
161 m_statusBar
= new DolphinStatusBar(this, m_view
);
162 m_statusBarTimer
= new QTimer(this);
163 m_statusBarTimer
->setSingleShot(true);
164 m_statusBarTimer
->setInterval(300);
165 connect(m_statusBarTimer
, SIGNAL(timeout()),
166 this, SLOT(updateStatusBar()));
168 m_filterBar
= new FilterBar(this);
169 m_filterBar
->setVisible(settings
->filterBar());
170 connect(m_filterBar
, SIGNAL(filterChanged(const QString
&)),
171 this, SLOT(setNameFilter(const QString
&)));
172 connect(m_filterBar
, SIGNAL(closeRequest()),
173 this, SLOT(closeFilterBar()));
174 connect(m_view
, SIGNAL(urlChanged(const KUrl
&)),
175 m_filterBar
, SLOT(clear()));
177 m_topLayout
->addWidget(m_urlNavigator
);
178 m_topLayout
->addWidget(m_view
);
179 m_topLayout
->addWidget(m_filterBar
);
180 m_topLayout
->addWidget(m_statusBar
);
183 DolphinViewContainer::~DolphinViewContainer()
185 m_dirLister
->disconnect();
189 delete m_dolphinModel
;
191 m_dirLister
= 0; // deleted by m_dolphinModel
194 const KUrl
& DolphinViewContainer::url() const
196 return m_urlNavigator
->url();
199 void DolphinViewContainer::setActive(bool active
)
201 m_urlNavigator
->setActive(active
);
202 m_view
->setActive(active
);
204 m_mainWindow
->newMenu()->menu()->setEnabled(m_isFolderWritable
);
208 bool DolphinViewContainer::isActive() const
210 Q_ASSERT(m_view
->isActive() == m_urlNavigator
->isActive());
211 return m_view
->isActive();
214 void DolphinViewContainer::refresh()
217 m_statusBar
->refresh();
220 bool DolphinViewContainer::isFilterBarVisible() const
222 return m_filterBar
->isVisible();
225 void DolphinViewContainer::setUrl(const KUrl
& newUrl
)
227 if (newUrl
!= m_urlNavigator
->url()) {
228 m_urlNavigator
->setUrl(newUrl
);
229 // Temporary disable the 'File'->'Create New...' menu until
230 // the write permissions can be checked in a fast way at
231 // DolphinViewContainer::slotDirListerCompleted().
232 m_isFolderWritable
= false;
234 m_mainWindow
->newMenu()->menu()->setEnabled(false);
239 void DolphinViewContainer::showFilterBar(bool show
)
241 Q_ASSERT(m_filterBar
!= 0);
249 bool DolphinViewContainer::isUrlEditable() const
251 return m_urlNavigator
->isUrlEditable();
254 void DolphinViewContainer::delayedStatusBarUpdate()
256 // Invoke updateStatusBar() with a small delay. This assures that
257 // when a lot of delayedStatusBarUpdates() are done in a short time,
258 // no bottleneck is given.
259 m_statusBarTimer
->start();
262 void DolphinViewContainer::updateStatusBar()
264 // As the item count information is less important
265 // in comparison with other messages, it should only
267 // - the status bar is empty or
268 // - shows already the item count information or
269 // - shows only a not very important information
270 // - if any progress is given don't show the item count info at all
271 const QString msg
= m_statusBar
->message();
272 const bool updateStatusBarMsg
= (msg
.isEmpty()
273 || (msg
== m_statusBar
->defaultText())
274 || (m_statusBar
->type() == DolphinStatusBar::Information
))
275 && (m_statusBar
->progress() == 100);
277 const QString
text(m_view
->statusBarText());
278 m_statusBar
->setDefaultText(text
);
280 if (updateStatusBarMsg
) {
281 m_statusBar
->setMessage(text
, DolphinStatusBar::Default
);
285 void DolphinViewContainer::initializeProgress()
287 if (url().protocol() == "nepomuksearch") {
288 // The Nepomuk IO-slave does not provide any progress information. Give
289 // an immediate hint to the user that a searching is done:
290 m_statusBar
->setProgressText(i18nc("@info", "Searching..."));
291 m_statusBar
->setProgress(-1);
295 void DolphinViewContainer::updateProgress(int percent
)
297 if (m_statusBar
->progressText().isEmpty()) {
298 m_statusBar
->setProgressText(i18nc("@info:progress", "Loading folder..."));
300 m_statusBar
->setProgress(percent
);
303 void DolphinViewContainer::slotDirListerCompleted()
305 if (!m_statusBar
->progressText().isEmpty()) {
306 m_statusBar
->setProgressText(QString());
307 m_statusBar
->setProgress(100);
310 if ((url().protocol() == "nepomuksearch") && (m_dirLister
->items().count() == 0)) {
311 // The dir lister has been completed on a Nepomuk-URI and no items have been found. Instead
312 // of showing the default status bar information ("0 items") a more helpful information is given:
313 m_statusBar
->setMessage(i18nc("@info:status", "No items found."), DolphinStatusBar::Information
);
318 // Enable the 'File'->'Create New...' menu only if the directory
320 KFileItem item
= m_dirLister
->rootItem();
322 // it is unclear whether writing is supported
323 m_isFolderWritable
= true;
325 KFileItemListProperties
capabilities(KFileItemList() << item
);
326 m_isFolderWritable
= capabilities
.supportsWriting();
330 m_mainWindow
->newMenu()->menu()->setEnabled(m_isFolderWritable
);
334 void DolphinViewContainer::showItemInfo(const KFileItem
& item
)
337 // Only clear the status bar if unimportant messages are shown.
338 // This prevents that information- or error-messages get hidden
339 // by moving the mouse above the viewport or when closing the
341 if (m_statusBar
->type() == DolphinStatusBar::Default
) {
342 m_statusBar
->clear();
345 m_statusBar
->setMessage(item
.getStatusBarInfo(), DolphinStatusBar::Default
);
349 void DolphinViewContainer::showInfoMessage(const QString
& msg
)
351 m_statusBar
->setMessage(msg
, DolphinStatusBar::Information
);
354 void DolphinViewContainer::showErrorMessage(const QString
& msg
)
356 m_statusBar
->setMessage(msg
, DolphinStatusBar::Error
);
359 void DolphinViewContainer::showOperationCompletedMessage(const QString
& msg
)
361 m_statusBar
->setMessage(msg
, DolphinStatusBar::OperationCompleted
);
364 void DolphinViewContainer::closeFilterBar()
367 m_filterBar
->clear();
369 emit
showFilterBarChanged(false);
372 void DolphinViewContainer::setNameFilter(const QString
& nameFilter
)
374 m_view
->setNameFilter(nameFilter
);
375 delayedStatusBarUpdate();
378 void DolphinViewContainer::openContextMenu(const KFileItem
& item
,
380 const QList
<QAction
*>& customActions
)
382 DolphinContextMenu
contextMenu(m_mainWindow
, item
, url
);
383 contextMenu
.setCustomActions(customActions
);
387 void DolphinViewContainer::saveContentsPos(int x
, int y
)
389 // TODO: If DolphinViewContainer uses DolphinView::saveState(...) to save the
390 // view state in KDE 4.5, this funciton can be removed.
391 m_urlNavigator
->savePosition(x
, y
);
394 void DolphinViewContainer::activate()
399 void DolphinViewContainer::restoreView(const KUrl
& url
)
401 if (KProtocolManager::supportsListing(url
)) {
402 m_view
->updateView(url
, m_urlNavigator
->savedRootUrl());
404 // When an URL has been entered, the view should get the focus.
405 // The focus must be requested asynchronously, as changing the URL might create
406 // a new view widget. Using QTimer::singleShow() works reliable, however
407 // QMetaObject::invokeMethod() with a queued connection does not work, which might
408 // indicate that we should pass a hint to DolphinView::updateView()
409 // regarding the focus instead. To test: Enter an URL and press CTRL+Enter.
410 // Expected result: The view should get the focus.
411 QTimer::singleShot(0, this, SLOT(requestFocus()));
413 } else if (KProtocolManager::isSourceProtocol(url
)) {
414 QString app
= "konqueror";
415 if (url
.protocol().startsWith(QLatin1String("http"))) {
416 showErrorMessage(i18nc("@info:status",
417 "Dolphin does not support web pages, the web browser has been launched"));
418 const KConfigGroup
config(KSharedConfig::openConfig("kdeglobals"), "General");
419 const QString browser
= config
.readEntry("BrowserApplication");
420 if (!browser
.isEmpty()) {
422 if (app
.startsWith('!')) {
423 // a literal command has been configured, remove the '!' prefix
428 showErrorMessage(i18nc("@info:status",
429 "Protocol not supported by Dolphin, Konqueror has been launched"));
432 const QString secureUrl
= KShell::quoteArg(url
.pathOrUrl());
433 const QString command
= app
+ ' ' + secureUrl
;
434 KRun::runCommand(command
, app
, app
, this);
436 showErrorMessage(i18nc("@info:status", "Invalid protocol"));
440 void DolphinViewContainer::saveRootUrl(const KUrl
& url
)
443 m_urlNavigator
->saveRootUrl(m_view
->rootUrl());
446 void DolphinViewContainer::dropUrls(const KUrl
& destination
, QDropEvent
* event
)
448 DragAndDropHelper::instance().dropUrls(KFileItem(), destination
, event
, this);
451 void DolphinViewContainer::redirect(const KUrl
& oldUrl
, const KUrl
& newUrl
)
454 const bool block
= m_urlNavigator
->signalsBlocked();
455 m_urlNavigator
->blockSignals(true);
456 m_urlNavigator
->setUrl(newUrl
);
457 m_urlNavigator
->blockSignals(block
);
460 void DolphinViewContainer::requestFocus()
465 void DolphinViewContainer::saveUrlCompletionMode(KGlobalSettings::Completion completion
)
467 DolphinSettings
& settings
= DolphinSettings::instance();
468 settings
.generalSettings()->setUrlCompletionMode(completion
);
472 void DolphinViewContainer::slotHistoryChanged()
474 const int index
= m_urlNavigator
->historyIndex();
476 // The "Go Forward" action is enabled. Try to mark
477 // the previous directory as active item:
478 const KUrl url
= m_urlNavigator
->historyUrl(index
- 1);
479 m_view
->activateItem(url
);
480 QPoint pos
= m_urlNavigator
->savedPosition();
481 m_view
->setRestoredContentsPosition(pos
);
485 void DolphinViewContainer::slotItemTriggered(const KFileItem
& item
)
487 KUrl url
= item
.targetUrl();
494 const GeneralSettings
* settings
= DolphinSettings::instance().generalSettings();
495 const bool browseThroughArchives
= settings
->browseThroughArchives();
496 if (browseThroughArchives
&& item
.isFile() && url
.isLocalFile()) {
497 // Generic mechanism for redirecting to tar:/<path>/ when clicking on a tar file,
498 // zip:/<path>/ when clicking on a zip file, etc.
499 // The .protocol file specifies the mimetype that the kioslave handles.
500 // Note that we don't use mimetype inheritance since we don't want to
501 // open OpenDocument files as zip folders...
502 const QString protocol
= KProtocolManager::protocolForArchiveMimetype(item
.mimetype());
503 if (!protocol
.isEmpty()) {
504 url
.setProtocol(protocol
);
513 void DolphinViewContainer::openFile(const KUrl
& url
)
515 // Using m_dolphinModel for getting the file item instance is not possible
516 // here: openFile() is triggered by an error of the directory lister
517 // job, so the file item must be received "manually".
518 const KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, url
);
519 slotItemTriggered(item
);
522 #include "dolphinviewcontainer.moc"