]>
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
)));
105 connect(m_searchBox
, SIGNAL(returnPressed(QString
)), this, SLOT(requestFocus()));
107 m_dirLister
= new DolphinDirLister();
108 m_dirLister
->setAutoUpdate(true);
109 m_dirLister
->setMainWindow(window());
110 m_dirLister
->setDelayedMimeTypes(true);
112 m_dolphinModel
= new DolphinModel(this);
113 m_dolphinModel
->setDirLister(m_dirLister
); // m_dolphinModel takes ownership of m_dirLister
114 m_dolphinModel
->setDropsAllowed(DolphinModel::DropOnDirectory
);
116 m_proxyModel
= new DolphinSortFilterProxyModel(this);
117 m_proxyModel
->setSourceModel(m_dolphinModel
);
118 m_proxyModel
->setFilterCaseSensitivity(Qt::CaseInsensitive
);
120 connect(m_dirLister
, SIGNAL(started(KUrl
)),
121 this, SLOT(initializeProgress()));
122 connect(m_dirLister
, SIGNAL(clear()),
123 this, SLOT(delayedStatusBarUpdate()));
124 connect(m_dirLister
, SIGNAL(percent(int)),
125 this, SLOT(updateProgress(int)));
126 connect(m_dirLister
, SIGNAL(itemsDeleted(const KFileItemList
&)),
127 this, SLOT(delayedStatusBarUpdate()));
128 connect(m_dirLister
, SIGNAL(completed()),
129 this, SLOT(slotDirListerCompleted()));
130 connect(m_dirLister
, SIGNAL(infoMessage(const QString
&)),
131 this, SLOT(showInfoMessage(const QString
&)));
132 connect(m_dirLister
, SIGNAL(errorMessage(const QString
&)),
133 this, SLOT(showErrorMessage(const QString
&)));
134 connect(m_dirLister
, SIGNAL(urlIsFileError(const KUrl
&)),
135 this, SLOT(openFile(const KUrl
&)));
137 m_view
= new DolphinView(this, url
, m_proxyModel
);
138 connect(m_view
, SIGNAL(urlChanged(const KUrl
&)),
139 m_urlNavigator
, SLOT(setUrl(const KUrl
&)));
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(redirection(KUrl
, KUrl
)),
151 this, SLOT(redirect(KUrl
, KUrl
)));
152 connect(m_view
, SIGNAL(selectionChanged(const KFileItemList
&)),
153 this, SLOT(delayedStatusBarUpdate()));
155 connect(m_urlNavigator
, SIGNAL(urlChanged(const KUrl
&)),
156 this, SLOT(slotUrlNavigatorLocationChanged(const KUrl
&)));
157 connect(m_urlNavigator
, SIGNAL(urlAboutToBeChanged(const KUrl
&)),
158 this, SLOT(saveViewState()));
159 connect(m_urlNavigator
, SIGNAL(historyChanged()),
160 this, SLOT(slotHistoryChanged()));
162 // initialize status bar
163 m_statusBar
= new DolphinStatusBar(this, m_view
);
164 m_statusBarTimer
= new QTimer(this);
165 m_statusBarTimer
->setSingleShot(true);
166 m_statusBarTimer
->setInterval(300);
167 connect(m_statusBarTimer
, SIGNAL(timeout()),
168 this, SLOT(updateStatusBar()));
170 KIO::FileUndoManager
* undoManager
= KIO::FileUndoManager::self();
171 connect(undoManager
, SIGNAL(jobRecordingFinished(CommandType
)),
172 this, SLOT(delayedStatusBarUpdate()));
174 // initialize filter bar
175 m_filterBar
= new FilterBar(this);
176 m_filterBar
->setVisible(settings
->filterBar());
177 connect(m_filterBar
, SIGNAL(filterChanged(const QString
&)),
178 this, SLOT(setNameFilter(const QString
&)));
179 connect(m_filterBar
, SIGNAL(closeRequest()),
180 this, SLOT(closeFilterBar()));
181 connect(m_view
, SIGNAL(urlChanged(const KUrl
&)),
182 m_filterBar
, SLOT(clear()));
184 m_topLayout
->addWidget(m_urlNavigator
);
185 m_topLayout
->addWidget(m_searchBox
);
186 m_topLayout
->addWidget(m_view
);
187 m_topLayout
->addWidget(m_filterBar
);
188 m_topLayout
->addWidget(m_statusBar
);
190 setSearchModeEnabled(isSearchUrl(url
));
193 DolphinViewContainer::~DolphinViewContainer()
197 KUrl
DolphinViewContainer::url() const
199 return m_urlNavigator
->locationUrl();
202 void DolphinViewContainer::setActive(bool active
)
204 m_urlNavigator
->setActive(active
);
205 m_view
->setActive(active
);
207 emit
writeStateChanged(m_isFolderWritable
);
211 bool DolphinViewContainer::isActive() const
213 Q_ASSERT(m_view
->isActive() == m_urlNavigator
->isActive());
214 return m_view
->isActive();
217 void DolphinViewContainer::refresh()
220 m_statusBar
->refresh();
223 bool DolphinViewContainer::isFilterBarVisible() const
225 return m_filterBar
->isVisible();
228 void DolphinViewContainer::setSearchModeEnabled(bool enabled
)
230 m_searchBox
->setVisible(enabled
);
231 m_urlNavigator
->setVisible(!enabled
);
234 // Remember the current URL, so that it can be restored
235 // when switching back to the URL navigator
236 const KUrl url
= m_urlNavigator
->locationUrl();
237 m_searchBox
->setSearchPath(url
);
239 // Restore the URL for the URL navigator. If Dolphin has been
240 // started with a search-URL, the home URL is used as fallback.
241 const KUrl url
= m_searchBox
->searchPath();
242 if (url
.isValid() && !url
.isEmpty()) {
243 if (isSearchUrl(url
)) {
244 m_urlNavigator
->goHome();
246 m_urlNavigator
->setLocationUrl(url
);
252 bool DolphinViewContainer::isSearchModeEnabled() const
254 return m_searchBox
->isVisible();
257 void DolphinViewContainer::setUrl(const KUrl
& newUrl
)
259 if (newUrl
!= m_urlNavigator
->locationUrl()) {
260 m_urlNavigator
->setLocationUrl(newUrl
);
261 // Temporary disable the 'File'->'Create New...' menu until
262 // the write permissions can be checked in a fast way at
263 // DolphinViewContainer::slotDirListerCompleted().
264 m_isFolderWritable
= false;
266 emit
writeStateChanged(false);
271 void DolphinViewContainer::showFilterBar(bool show
)
273 Q_ASSERT(m_filterBar
!= 0);
281 void DolphinViewContainer::delayedStatusBarUpdate()
283 // Invoke updateStatusBar() with a small delay. This assures that
284 // when a lot of delayedStatusBarUpdates() are done in a short time,
285 // no bottleneck is given.
286 m_statusBarTimer
->start();
289 void DolphinViewContainer::updateStatusBar()
291 // As the item count information is less important
292 // in comparison with other messages, it should only
294 // - the status bar is empty or
295 // - shows already the item count information or
296 // - shows only a not very important information
297 // - if any progress is given don't show the item count info at all
298 const QString msg
= m_statusBar
->message();
299 const bool updateStatusBarMsg
= (msg
.isEmpty()
300 || (msg
== m_statusBar
->defaultText())
301 || (m_statusBar
->type() == DolphinStatusBar::Information
))
302 && (m_statusBar
->progress() == 100);
304 const QString text
= m_view
->statusBarText();
305 m_statusBar
->setDefaultText(text
);
307 if (updateStatusBarMsg
) {
308 m_statusBar
->setMessage(text
, DolphinStatusBar::Default
);
312 void DolphinViewContainer::initializeProgress()
314 if (isSearchUrl(url())) {
315 // Search KIO-slaves usually don't provide any progress information. Give
316 // an immediate hint to the user that a searching is done:
318 m_statusBar
->setProgressText(i18nc("@info", "Searching..."));
319 m_statusBar
->setProgress(-1);
323 void DolphinViewContainer::updateProgress(int percent
)
325 if (m_statusBar
->progressText().isEmpty()) {
326 m_statusBar
->setProgressText(i18nc("@info:progress", "Loading folder..."));
328 m_statusBar
->setProgress(percent
);
331 void DolphinViewContainer::slotDirListerCompleted()
333 if (!m_statusBar
->progressText().isEmpty()) {
334 m_statusBar
->setProgressText(QString());
335 m_statusBar
->setProgress(100);
338 if (isSearchUrl(url()) && (m_dirLister
->items().count() == 0)) {
339 // The dir lister has been completed on a Nepomuk-URI and no items have been found. Instead
340 // of showing the default status bar information ("0 items") a more helpful information is given:
341 m_statusBar
->setMessage(i18nc("@info:status", "No items found."), DolphinStatusBar::Information
);
346 // Enable the 'File'->'Create New...' menu only if the directory
348 KFileItem item
= m_dirLister
->rootItem();
350 // it is unclear whether writing is supported
351 m_isFolderWritable
= true;
353 KFileItemListProperties
capabilities(KFileItemList() << item
);
354 m_isFolderWritable
= capabilities
.supportsWriting();
358 emit
writeStateChanged(m_isFolderWritable
);
362 void DolphinViewContainer::showItemInfo(const KFileItem
& item
)
365 // Only clear the status bar if unimportant messages are shown.
366 // This prevents that information- or error-messages get hidden
367 // by moving the mouse above the viewport or when closing the
369 if (m_statusBar
->type() == DolphinStatusBar::Default
) {
370 m_statusBar
->clear();
373 m_statusBar
->setMessage(item
.getStatusBarInfo(), DolphinStatusBar::Default
);
377 void DolphinViewContainer::showInfoMessage(const QString
& msg
)
379 m_statusBar
->setMessage(msg
, DolphinStatusBar::Information
);
382 void DolphinViewContainer::showErrorMessage(const QString
& msg
)
384 m_statusBar
->setMessage(msg
, DolphinStatusBar::Error
);
387 void DolphinViewContainer::showOperationCompletedMessage(const QString
& msg
)
389 m_statusBar
->setMessage(msg
, DolphinStatusBar::OperationCompleted
);
392 void DolphinViewContainer::closeFilterBar()
395 m_filterBar
->clear();
397 emit
showFilterBarChanged(false);
400 void DolphinViewContainer::setNameFilter(const QString
& nameFilter
)
402 m_view
->setNameFilter(nameFilter
);
403 delayedStatusBarUpdate();
406 void DolphinViewContainer::activate()
411 void DolphinViewContainer::saveViewState()
413 QByteArray locationState
;
414 QDataStream
stream(&locationState
, QIODevice::WriteOnly
);
415 m_view
->saveState(stream
);
416 m_urlNavigator
->saveLocationState(locationState
);
419 void DolphinViewContainer::slotUrlNavigatorLocationChanged(const KUrl
& url
)
421 if (KProtocolManager::supportsListing(url
)) {
422 // Assure that the search box is shown instead of the URL navigator in case
423 // that the URL \p url is a search URL (e. g. nepomuksearch:// or filenamesearch://).
424 if (isSearchUrl(url
)) {
425 if (!m_searchBox
->isVisible()) {
426 m_searchBox
->setVisible(true);
427 m_urlNavigator
->setVisible(false);
429 } else if (!m_urlNavigator
->isVisible()) {
430 m_urlNavigator
->setVisible(true);
431 m_searchBox
->setVisible(false);
435 if (isActive() && !isSearchUrl(url
)) {
436 // When an URL has been entered, the view should get the focus.
437 // The focus must be requested asynchronously, as changing the URL might create
438 // a new view widget.
439 QTimer::singleShot(0, this, SLOT(requestFocus()));
441 } else if (KProtocolManager::isSourceProtocol(url
)) {
442 QString app
= "konqueror";
443 if (url
.protocol().startsWith(QLatin1String("http"))) {
444 showErrorMessage(i18nc("@info:status",
445 "Dolphin does not support web pages, the web browser has been launched"));
446 const KConfigGroup
config(KSharedConfig::openConfig("kdeglobals"), "General");
447 const QString browser
= config
.readEntry("BrowserApplication");
448 if (!browser
.isEmpty()) {
450 if (app
.startsWith('!')) {
451 // a literal command has been configured, remove the '!' prefix
456 showErrorMessage(i18nc("@info:status",
457 "Protocol not supported by Dolphin, Konqueror has been launched"));
460 const QString secureUrl
= KShell::quoteArg(url
.pathOrUrl());
461 const QString command
= app
+ ' ' + secureUrl
;
462 KRun::runCommand(command
, app
, app
, this);
464 showErrorMessage(i18nc("@info:status", "Invalid protocol"));
468 void DolphinViewContainer::dropUrls(const KUrl
& destination
, QDropEvent
* event
)
470 DragAndDropHelper::instance().dropUrls(KFileItem(), destination
, event
, this);
473 void DolphinViewContainer::redirect(const KUrl
& oldUrl
, const KUrl
& newUrl
)
476 const bool block
= m_urlNavigator
->signalsBlocked();
477 m_urlNavigator
->blockSignals(true);
479 // Assure that the location state is reset for redirection URLs. This
480 // allows to skip redirection URLs when going back or forward in the
482 m_urlNavigator
->saveLocationState(QByteArray());
483 m_urlNavigator
->setLocationUrl(newUrl
);
485 m_urlNavigator
->blockSignals(block
);
488 void DolphinViewContainer::requestFocus()
493 void DolphinViewContainer::saveUrlCompletionMode(KGlobalSettings::Completion completion
)
495 DolphinSettings
& settings
= DolphinSettings::instance();
496 settings
.generalSettings()->setUrlCompletionMode(completion
);
500 void DolphinViewContainer::slotHistoryChanged()
502 QByteArray locationState
= m_urlNavigator
->locationState();
504 if (!locationState
.isEmpty()) {
505 QDataStream
stream(&locationState
, QIODevice::ReadOnly
);
506 m_view
->restoreState(stream
);
510 void DolphinViewContainer::startSearching(const QString
&text
)
513 const KUrl url
= m_searchBox
->urlForSearching();
514 if (url
.isValid() && !url
.isEmpty()) {
515 m_urlNavigator
->setLocationUrl(url
);
519 void DolphinViewContainer::closeSearchBox()
521 setSearchModeEnabled(false);
524 bool DolphinViewContainer::isSearchUrl(const KUrl
& url
) const
526 return url
.protocol().contains("search");
529 void DolphinViewContainer::slotItemTriggered(const KFileItem
& item
)
531 KUrl url
= item
.targetUrl();
538 const GeneralSettings
* settings
= DolphinSettings::instance().generalSettings();
539 const bool browseThroughArchives
= settings
->browseThroughArchives();
540 if (browseThroughArchives
&& item
.isFile() && url
.isLocalFile()) {
541 // Generic mechanism for redirecting to tar:/<path>/ when clicking on a tar file,
542 // zip:/<path>/ when clicking on a zip file, etc.
543 // The .protocol file specifies the mimetype that the kioslave handles.
544 // Note that we don't use mimetype inheritance since we don't want to
545 // open OpenDocument files as zip folders...
546 const QString protocol
= KProtocolManager::protocolForArchiveMimetype(item
.mimetype());
547 if (!protocol
.isEmpty()) {
548 url
.setProtocol(protocol
);
554 if (item
.mimetype() == "application/x-desktop") {
555 // redirect to the url in Type=Link desktop files
556 KDesktopFile
desktopFile(url
.toLocalFile());
557 if (desktopFile
.hasLinkType()) {
558 url
= desktopFile
.readUrl();
567 void DolphinViewContainer::openFile(const KUrl
& url
)
569 // Using m_dolphinModel for getting the file item instance is not possible
570 // here: openFile() is triggered by an error of the directory lister
571 // job, so the file item must be received "manually".
572 const KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, url
);
573 slotItemTriggered(item
);
576 #include "dolphinviewcontainer.moc"