]>
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/QKeyEvent>
25 #include <QtGui/QItemSelection>
26 #include <QtGui/QBoxLayout>
27 #include <QtCore/QTimer>
28 #include <QtGui/QScrollBar>
30 #include <kdesktopfile.h>
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 "dolphin_generalsettings.h"
51 #include "dolphinmainwindow.h"
52 #include "filterbar/filterbar.h"
53 #include "search/dolphinsearchbox.h"
54 #include "settings/dolphinsettings.h"
55 #include "statusbar/dolphinstatusbar.h"
56 #include "views/dolphincolumnview.h"
57 #include "views/dolphindetailsview.h"
58 #include "views/dolphindirlister.h"
59 #include "views/dolphinsortfilterproxymodel.h"
60 #include "views/draganddrophelper.h"
61 #include "views/dolphiniconsview.h"
62 #include "views/dolphinmodel.h"
63 #include "views/dolphinviewcontroller.h"
64 #include "views/viewmodecontroller.h"
65 #include "views/viewproperties.h"
67 DolphinViewContainer::DolphinViewContainer(const KUrl
& url
, QWidget
* parent
) :
69 m_isFolderWritable(false),
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(KUrl(settings
->homeUrl()));
98 KUrlComboBox
* editor
= m_urlNavigator
->editor();
99 editor
->setCompletionMode(KGlobalSettings::Completion(settings
->urlCompletionMode()));
101 m_searchBox
= new DolphinSearchBox(this);
103 connect(m_searchBox
, SIGNAL(closeRequest()), this, SLOT(closeSearchBox()));
104 connect(m_searchBox
, SIGNAL(search(QString
)), this, SLOT(startSearching(QString
)));
106 m_dirLister
= new DolphinDirLister();
107 m_dirLister
->setAutoUpdate(true);
108 m_dirLister
->setMainWindow(window());
109 m_dirLister
->setDelayedMimeTypes(true);
111 m_dolphinModel
= new DolphinModel(this);
112 m_dolphinModel
->setDirLister(m_dirLister
);
113 m_dolphinModel
->setDropsAllowed(DolphinModel::DropOnDirectory
);
115 m_proxyModel
= new DolphinSortFilterProxyModel(this);
116 m_proxyModel
->setSourceModel(m_dolphinModel
);
117 m_proxyModel
->setFilterCaseSensitivity(Qt::CaseInsensitive
);
119 connect(m_dirLister
, SIGNAL(started(KUrl
)),
120 this, SLOT(initializeProgress()));
121 connect(m_dirLister
, SIGNAL(clear()),
122 this, SLOT(delayedStatusBarUpdate()));
123 connect(m_dirLister
, SIGNAL(percent(int)),
124 this, SLOT(updateProgress(int)));
125 connect(m_dirLister
, SIGNAL(itemsDeleted(const KFileItemList
&)),
126 this, SLOT(delayedStatusBarUpdate()));
127 connect(m_dirLister
, SIGNAL(completed()),
128 this, SLOT(slotDirListerCompleted()));
129 connect(m_dirLister
, SIGNAL(infoMessage(const QString
&)),
130 this, SLOT(showInfoMessage(const QString
&)));
131 connect(m_dirLister
, SIGNAL(errorMessage(const QString
&)),
132 this, SLOT(showErrorMessage(const QString
&)));
133 connect(m_dirLister
, SIGNAL(urlIsFileError(const KUrl
&)),
134 this, SLOT(openFile(const KUrl
&)));
136 m_view
= new DolphinView(this, url
, m_proxyModel
);
137 connect(m_view
, SIGNAL(urlChanged(const KUrl
&)),
138 m_urlNavigator
, SLOT(setUrl(const KUrl
&)));
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(redirection(KUrl
, KUrl
)),
150 this, SLOT(redirect(KUrl
, KUrl
)));
151 connect(m_view
, SIGNAL(selectionChanged(const KFileItemList
&)),
152 this, SLOT(delayedStatusBarUpdate()));
154 connect(m_urlNavigator
, SIGNAL(urlChanged(const KUrl
&)),
155 this, SLOT(slotUrlNavigatorLocationChanged(const KUrl
&)));
156 connect(m_urlNavigator
, SIGNAL(urlAboutToBeChanged(const KUrl
&)),
157 this, SLOT(saveViewState()));
158 connect(m_urlNavigator
, SIGNAL(historyChanged()),
159 this, SLOT(slotHistoryChanged()));
161 // initialize status bar
162 m_statusBar
= new DolphinStatusBar(this, m_view
);
163 m_statusBarTimer
= new QTimer(this);
164 m_statusBarTimer
->setSingleShot(true);
165 m_statusBarTimer
->setInterval(300);
166 connect(m_statusBarTimer
, SIGNAL(timeout()),
167 this, SLOT(updateStatusBar()));
169 KIO::FileUndoManager
* undoManager
= KIO::FileUndoManager::self();
170 connect(undoManager
, SIGNAL(jobRecordingFinished(CommandType
)),
171 this, SLOT(delayedStatusBarUpdate()));
173 // initialize filter bar
174 m_filterBar
= new FilterBar(this);
175 m_filterBar
->setVisible(settings
->filterBar());
176 connect(m_filterBar
, SIGNAL(filterChanged(const QString
&)),
177 this, SLOT(setNameFilter(const QString
&)));
178 connect(m_filterBar
, SIGNAL(closeRequest()),
179 this, SLOT(closeFilterBar()));
180 connect(m_view
, SIGNAL(urlChanged(const KUrl
&)),
181 m_filterBar
, SLOT(clear()));
183 m_topLayout
->addWidget(m_urlNavigator
);
184 m_topLayout
->addWidget(m_searchBox
);
185 m_topLayout
->addWidget(m_view
);
186 m_topLayout
->addWidget(m_filterBar
);
187 m_topLayout
->addWidget(m_statusBar
);
189 setSearchModeEnabled(isSearchUrl(url
));
192 DolphinViewContainer::~DolphinViewContainer()
194 m_dirLister
->disconnect();
198 delete m_dolphinModel
;
200 m_dirLister
= 0; // deleted by m_dolphinModel
203 KUrl
DolphinViewContainer::url() const
205 return m_urlNavigator
->locationUrl();
208 void DolphinViewContainer::setActive(bool active
)
210 m_urlNavigator
->setActive(active
);
211 m_view
->setActive(active
);
213 emit
writeStateChanged(m_isFolderWritable
);
217 bool DolphinViewContainer::isActive() const
219 Q_ASSERT(m_view
->isActive() == m_urlNavigator
->isActive());
220 return m_view
->isActive();
223 void DolphinViewContainer::refresh()
226 m_statusBar
->refresh();
229 bool DolphinViewContainer::isFilterBarVisible() const
231 return m_filterBar
->isVisible();
234 void DolphinViewContainer::setSearchModeEnabled(bool enabled
)
236 m_searchBox
->setVisible(enabled
);
237 m_urlNavigator
->setVisible(!enabled
);
240 // Remember the current URL, so that it can be restored
241 // when switching back to the URL navigator
242 const KUrl url
= m_urlNavigator
->locationUrl();
243 m_searchBox
->setSearchPath(url
);
245 // Restore the URL for the URL navigator. If Dolphin has been
246 // started with a search-URL, the home URL is used as fallback.
247 const KUrl url
= m_searchBox
->searchPath();
248 if (url
.isValid() && !url
.isEmpty() && !isSearchUrl(url
)) {
249 m_urlNavigator
->setLocationUrl(url
);
251 m_urlNavigator
->goHome();
256 bool DolphinViewContainer::isSearchModeEnabled() const
258 return m_searchBox
->isVisible();
261 void DolphinViewContainer::setUrl(const KUrl
& newUrl
)
263 if (newUrl
!= m_urlNavigator
->locationUrl()) {
264 m_urlNavigator
->setLocationUrl(newUrl
);
265 // Temporary disable the 'File'->'Create New...' menu until
266 // the write permissions can be checked in a fast way at
267 // DolphinViewContainer::slotDirListerCompleted().
268 m_isFolderWritable
= false;
270 emit
writeStateChanged(false);
275 void DolphinViewContainer::showFilterBar(bool show
)
277 Q_ASSERT(m_filterBar
!= 0);
285 void DolphinViewContainer::delayedStatusBarUpdate()
287 // Invoke updateStatusBar() with a small delay. This assures that
288 // when a lot of delayedStatusBarUpdates() are done in a short time,
289 // no bottleneck is given.
290 m_statusBarTimer
->start();
293 void DolphinViewContainer::updateStatusBar()
295 // As the item count information is less important
296 // in comparison with other messages, it should only
298 // - the status bar is empty or
299 // - shows already the item count information or
300 // - shows only a not very important information
301 // - if any progress is given don't show the item count info at all
302 const QString msg
= m_statusBar
->message();
303 const bool updateStatusBarMsg
= (msg
.isEmpty()
304 || (msg
== m_statusBar
->defaultText())
305 || (m_statusBar
->type() == DolphinStatusBar::Information
))
306 && (m_statusBar
->progress() == 100);
308 const QString text
= m_view
->statusBarText();
309 m_statusBar
->setDefaultText(text
);
311 if (updateStatusBarMsg
) {
312 m_statusBar
->setMessage(text
, DolphinStatusBar::Default
);
316 void DolphinViewContainer::initializeProgress()
318 if (isSearchUrl(url())) {
319 // Search KIO-slaves usually don't provide any progress information. Give
320 // an immediate hint to the user that a searching is done:
322 m_statusBar
->setProgressText(i18nc("@info", "Searching..."));
323 m_statusBar
->setProgress(-1);
327 void DolphinViewContainer::updateProgress(int percent
)
329 if (m_statusBar
->progressText().isEmpty()) {
330 m_statusBar
->setProgressText(i18nc("@info:progress", "Loading folder..."));
332 m_statusBar
->setProgress(percent
);
335 void DolphinViewContainer::slotDirListerCompleted()
337 if (!m_statusBar
->progressText().isEmpty()) {
338 m_statusBar
->setProgressText(QString());
339 m_statusBar
->setProgress(100);
342 if (isSearchUrl(url()) && (m_dirLister
->items().count() == 0)) {
343 // The dir lister has been completed on a Nepomuk-URI and no items have been found. Instead
344 // of showing the default status bar information ("0 items") a more helpful information is given:
345 m_statusBar
->setMessage(i18nc("@info:status", "No items found."), DolphinStatusBar::Information
);
350 // Enable the 'File'->'Create New...' menu only if the directory
352 KFileItem item
= m_dirLister
->rootItem();
354 // it is unclear whether writing is supported
355 m_isFolderWritable
= true;
357 KFileItemListProperties
capabilities(KFileItemList() << item
);
358 m_isFolderWritable
= capabilities
.supportsWriting();
362 emit
writeStateChanged(m_isFolderWritable
);
366 void DolphinViewContainer::showItemInfo(const KFileItem
& item
)
369 // Only clear the status bar if unimportant messages are shown.
370 // This prevents that information- or error-messages get hidden
371 // by moving the mouse above the viewport or when closing the
373 if (m_statusBar
->type() == DolphinStatusBar::Default
) {
374 m_statusBar
->clear();
377 m_statusBar
->setMessage(item
.getStatusBarInfo(), DolphinStatusBar::Default
);
381 void DolphinViewContainer::showInfoMessage(const QString
& msg
)
383 m_statusBar
->setMessage(msg
, DolphinStatusBar::Information
);
386 void DolphinViewContainer::showErrorMessage(const QString
& msg
)
388 m_statusBar
->setMessage(msg
, DolphinStatusBar::Error
);
391 void DolphinViewContainer::showOperationCompletedMessage(const QString
& msg
)
393 m_statusBar
->setMessage(msg
, DolphinStatusBar::OperationCompleted
);
396 void DolphinViewContainer::closeFilterBar()
399 m_filterBar
->clear();
401 emit
showFilterBarChanged(false);
404 void DolphinViewContainer::setNameFilter(const QString
& nameFilter
)
406 m_view
->setNameFilter(nameFilter
);
407 delayedStatusBarUpdate();
410 void DolphinViewContainer::activate()
415 void DolphinViewContainer::saveViewState()
417 QByteArray locationState
;
418 QDataStream
stream(&locationState
, QIODevice::WriteOnly
);
419 m_view
->saveState(stream
);
420 m_urlNavigator
->saveLocationState(locationState
);
423 void DolphinViewContainer::slotUrlNavigatorLocationChanged(const KUrl
& url
)
425 if (KProtocolManager::supportsListing(url
)) {
426 // Assure that the search box is shown instead of the URL navigator in case
427 // that the URL \p url is a search URL (e. g. nepomuksearch:// or filenamesearch://).
428 if (isSearchUrl(url
)) {
429 if (!m_searchBox
->isVisible()) {
430 m_searchBox
->setVisible(true);
431 m_urlNavigator
->setVisible(false);
433 } else if (!m_urlNavigator
->isVisible()) {
434 m_urlNavigator
->setVisible(true);
435 m_searchBox
->setVisible(false);
440 // When an URL has been entered, the view should get the focus.
441 // The focus must be requested asynchronously, as changing the URL might create
442 // a new view widget.
443 QTimer::singleShot(0, this, SLOT(requestFocus()));
445 } else if (KProtocolManager::isSourceProtocol(url
)) {
446 QString app
= "konqueror";
447 if (url
.protocol().startsWith(QLatin1String("http"))) {
448 showErrorMessage(i18nc("@info:status",
449 "Dolphin does not support web pages, the web browser has been launched"));
450 const KConfigGroup
config(KSharedConfig::openConfig("kdeglobals"), "General");
451 const QString browser
= config
.readEntry("BrowserApplication");
452 if (!browser
.isEmpty()) {
454 if (app
.startsWith('!')) {
455 // a literal command has been configured, remove the '!' prefix
460 showErrorMessage(i18nc("@info:status",
461 "Protocol not supported by Dolphin, Konqueror has been launched"));
464 const QString secureUrl
= KShell::quoteArg(url
.pathOrUrl());
465 const QString command
= app
+ ' ' + secureUrl
;
466 KRun::runCommand(command
, app
, app
, this);
468 showErrorMessage(i18nc("@info:status", "Invalid protocol"));
472 void DolphinViewContainer::dropUrls(const KUrl
& destination
, QDropEvent
* event
)
474 DragAndDropHelper::instance().dropUrls(KFileItem(), destination
, event
, this);
477 void DolphinViewContainer::redirect(const KUrl
& oldUrl
, const KUrl
& newUrl
)
480 const bool block
= m_urlNavigator
->signalsBlocked();
481 m_urlNavigator
->blockSignals(true);
483 // Assure that the location state is reset for redirection URLs. This
484 // allows to skip redirection URLs when going back or forward in the
486 m_urlNavigator
->saveLocationState(QByteArray());
487 m_urlNavigator
->setLocationUrl(newUrl
);
489 m_urlNavigator
->blockSignals(block
);
492 void DolphinViewContainer::requestFocus()
497 void DolphinViewContainer::saveUrlCompletionMode(KGlobalSettings::Completion completion
)
499 DolphinSettings
& settings
= DolphinSettings::instance();
500 settings
.generalSettings()->setUrlCompletionMode(completion
);
504 void DolphinViewContainer::slotHistoryChanged()
506 QByteArray locationState
= m_urlNavigator
->locationState();
508 if (!locationState
.isEmpty()) {
509 QDataStream
stream(&locationState
, QIODevice::ReadOnly
);
510 m_view
->restoreState(stream
);
514 void DolphinViewContainer::startSearching(const QString
&text
)
517 const KUrl url
= m_searchBox
->urlForSearching();
518 if (url
.isValid() && !url
.isEmpty()) {
519 m_urlNavigator
->setLocationUrl(url
);
523 void DolphinViewContainer::closeSearchBox()
525 setSearchModeEnabled(false);
528 bool DolphinViewContainer::isSearchUrl(const KUrl
& url
) const
530 return url
.protocol().contains("search");
533 void DolphinViewContainer::slotItemTriggered(const KFileItem
& item
)
535 KUrl url
= item
.targetUrl();
542 const GeneralSettings
* settings
= DolphinSettings::instance().generalSettings();
543 const bool browseThroughArchives
= settings
->browseThroughArchives();
544 if (browseThroughArchives
&& item
.isFile() && url
.isLocalFile()) {
545 // Generic mechanism for redirecting to tar:/<path>/ when clicking on a tar file,
546 // zip:/<path>/ when clicking on a zip file, etc.
547 // The .protocol file specifies the mimetype that the kioslave handles.
548 // Note that we don't use mimetype inheritance since we don't want to
549 // open OpenDocument files as zip folders...
550 const QString protocol
= KProtocolManager::protocolForArchiveMimetype(item
.mimetype());
551 if (!protocol
.isEmpty()) {
552 url
.setProtocol(protocol
);
558 if (item
.mimetype() == "application/x-desktop") {
559 // redirect to the url in Type=Link desktop files
560 KDesktopFile
desktopFile(url
.toLocalFile());
561 if (desktopFile
.hasLinkType()) {
562 url
= desktopFile
.readUrl();
571 void DolphinViewContainer::openFile(const KUrl
& url
)
573 // Using m_dolphinModel for getting the file item instance is not possible
574 // here: openFile() is triggered by an error of the directory lister
575 // job, so the file item must be received "manually".
576 const KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, url
);
577 slotItemTriggered(item
);
580 #include "dolphinviewcontainer.moc"