]>
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>
40 #include <konqmimedata.h>
41 #include <kfileitemlistproperties.h>
42 #include <konq_operations.h>
45 #include <kurlcombobox.h>
46 #include <kurlnavigator.h>
49 #include "dolphin_generalsettings.h"
50 #include "dolphinmainwindow.h"
51 #include "filterbar/filterbar.h"
52 #include "search/dolphinsearchbox.h"
53 #include "settings/dolphinsettings.h"
54 #include "statusbar/dolphinstatusbar.h"
55 #include "views/dolphincolumnview.h"
56 #include "views/dolphindetailsview.h"
57 #include "views/dolphindirlister.h"
58 #include "views/dolphinsortfilterproxymodel.h"
59 #include "views/draganddrophelper.h"
60 #include "views/dolphiniconsview.h"
61 #include "views/dolphinmodel.h"
62 #include "views/dolphinviewcontroller.h"
63 #include "views/viewmodecontroller.h"
64 #include "views/viewproperties.h"
66 DolphinViewContainer::DolphinViewContainer(const KUrl
& url
, QWidget
* parent
) :
68 m_isFolderWritable(false),
76 m_statusBarTimestamp(),
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 // TODO: In the case of the column view the directory lister changes. Let the DolphinView
121 // inform the container about this information for KDE SC 4.7
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(newItems(KFileItemList
)),
129 this, SLOT(delayedStatusBarUpdate()));
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()));
154 connect(m_view
, SIGNAL(startedPathLoading(KUrl
)),
155 this, SLOT(slotStartedPathLoading()));
156 connect(m_view
, SIGNAL(finishedPathLoading(KUrl
)),
157 this, SLOT(slotFinishedPathLoading()));
159 connect(m_urlNavigator
, SIGNAL(urlChanged(const KUrl
&)),
160 this, SLOT(slotUrlNavigatorLocationChanged(const KUrl
&)));
161 connect(m_urlNavigator
, SIGNAL(urlAboutToBeChanged(const KUrl
&)),
162 this, SLOT(saveViewState()));
163 connect(m_urlNavigator
, SIGNAL(historyChanged()),
164 this, SLOT(slotHistoryChanged()));
166 // initialize status bar
167 m_statusBar
= new DolphinStatusBar(this, m_view
);
168 connect(m_statusBar
, SIGNAL(stopPressed()), this, SLOT(stopLoading()));
170 m_statusBarTimer
= new QTimer(this);
171 m_statusBarTimer
->setSingleShot(true);
172 m_statusBarTimer
->setInterval(300);
173 connect(m_statusBarTimer
, SIGNAL(timeout()),
174 this, SLOT(updateStatusBar()));
176 KIO::FileUndoManager
* undoManager
= KIO::FileUndoManager::self();
177 connect(undoManager
, SIGNAL(jobRecordingFinished(CommandType
)),
178 this, SLOT(delayedStatusBarUpdate()));
180 // initialize filter bar
181 m_filterBar
= new FilterBar(this);
182 m_filterBar
->setVisible(settings
->filterBar());
183 connect(m_filterBar
, SIGNAL(filterChanged(const QString
&)),
184 this, SLOT(setNameFilter(const QString
&)));
185 connect(m_filterBar
, SIGNAL(closeRequest()),
186 this, SLOT(closeFilterBar()));
187 connect(m_view
, SIGNAL(urlChanged(const KUrl
&)),
188 m_filterBar
, SLOT(clear()));
190 m_topLayout
->addWidget(m_urlNavigator
);
191 m_topLayout
->addWidget(m_searchBox
);
192 m_topLayout
->addWidget(m_view
);
193 m_topLayout
->addWidget(m_filterBar
);
194 m_topLayout
->addWidget(m_statusBar
);
196 setSearchModeEnabled(isSearchUrl(url
));
199 DolphinViewContainer::~DolphinViewContainer()
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()
225 GeneralSettings
* settings
= DolphinSettings::instance().generalSettings();
226 if (settings
->modifiedStartupSettings()) {
227 // The startup settings should only get applied if they have been
228 // modified by the user. Otherwise keep the (possibly) different current
229 // settings of the URL navigator and the filterbar.
230 m_urlNavigator
->setUrlEditable(settings
->editableUrl());
231 m_urlNavigator
->setShowFullPath(settings
->showFullPath());
232 setFilterBarVisible(settings
->filterBar());
236 m_statusBar
->refresh();
239 bool DolphinViewContainer::isFilterBarVisible() const
241 return m_filterBar
->isVisible();
244 void DolphinViewContainer::setSearchModeEnabled(bool enabled
)
246 if (enabled
== isSearchModeEnabled()) {
250 m_searchBox
->setVisible(enabled
);
251 m_urlNavigator
->setVisible(!enabled
);
254 // Remember the most recent non-search URL as search path
255 // of the search-box, so that it can be restored
256 // when switching back to the URL navigator.
257 KUrl url
= m_urlNavigator
->locationUrl();
259 int index
= m_urlNavigator
->historyIndex();
260 const int historySize
= m_urlNavigator
->historySize();
261 while (isSearchUrl(url
) && (index
< historySize
)) {
263 url
= m_urlNavigator
->locationUrl(index
);
266 if (!isSearchUrl(url
)) {
267 m_searchBox
->setSearchPath(url
);
270 // Restore the URL for the URL navigator. If Dolphin has been
271 // started with a search-URL, the home URL is used as fallback.
272 const KUrl url
= m_searchBox
->searchPath();
273 if (url
.isValid() && !url
.isEmpty()) {
274 if (isSearchUrl(url
)) {
275 m_urlNavigator
->goHome();
277 m_urlNavigator
->setLocationUrl(url
);
282 emit
searchModeChanged(enabled
);
285 bool DolphinViewContainer::isSearchModeEnabled() const
287 return m_searchBox
->isVisible();
290 void DolphinViewContainer::setUrl(const KUrl
& newUrl
)
292 if (newUrl
!= m_urlNavigator
->locationUrl()) {
293 m_urlNavigator
->setLocationUrl(newUrl
);
295 // Temporary disable the 'File'->'Create New...' menu until
296 // the write permissions can be checked in a fast way at
297 // DolphinViewContainer::slotDirListerCompleted().
298 m_isFolderWritable
= false;
300 emit
writeStateChanged(false);
305 void DolphinViewContainer::setFilterBarVisible(bool visible
)
307 Q_ASSERT(m_filterBar
!= 0);
310 m_filterBar
->setFocus();
316 void DolphinViewContainer::delayedStatusBarUpdate()
318 if (m_statusBarTimer
->isActive() && (m_statusBarTimestamp
.elapsed() > 2000)) {
319 // No update of the statusbar has been done during the last 2 seconds,
320 // although an update has been requested. Trigger an immediate update.
321 m_statusBarTimer
->stop();
324 // Invoke updateStatusBar() with a small delay. This assures that
325 // when a lot of delayedStatusBarUpdates() are done in a short time,
326 // no bottleneck is given.
327 m_statusBarTimer
->start();
331 void DolphinViewContainer::updateStatusBar()
333 m_statusBarTimestamp
.start();
335 // As the item count information is less important
336 // in comparison with other messages, it should only
338 // - the status bar is empty or
339 // - shows already the item count information or
340 // - shows only a not very important information
341 const QString newMessage
= m_view
->statusBarText();
342 const QString currentMessage
= m_statusBar
->message();
343 const bool updateStatusBarMsg
= currentMessage
.isEmpty()
344 || (currentMessage
== m_statusBar
->defaultText())
345 || (m_statusBar
->type() == DolphinStatusBar::Information
);
347 m_statusBar
->setDefaultText(newMessage
);
349 if (updateStatusBarMsg
) {
350 m_statusBar
->setMessage(newMessage
, DolphinStatusBar::Default
);
354 void DolphinViewContainer::updateProgress(int percent
)
356 if (m_statusBar
->progressText().isEmpty()) {
357 m_statusBar
->setProgressText(i18nc("@info:progress", "Loading folder..."));
359 m_statusBar
->setProgress(percent
);
362 void DolphinViewContainer::slotStartedPathLoading()
364 if (isSearchUrl(url())) {
365 // Search KIO-slaves usually don't provide any progress information. Give
366 // a hint to the user that a searching is done:
368 m_statusBar
->setProgressText(i18nc("@info", "Searching..."));
369 m_statusBar
->setProgress(-1);
371 // Trigger an undetermined progress indication. The progress
372 // information in percent will be triggered by the percent() signal
373 // of the directory lister later.
378 void DolphinViewContainer::slotFinishedPathLoading()
380 if (!m_statusBar
->progressText().isEmpty()) {
381 m_statusBar
->setProgressText(QString());
382 m_statusBar
->setProgress(100);
385 if (isSearchUrl(url()) && (m_dirLister
->items().count() == 0)) {
386 // The dir lister has been completed on a Nepomuk-URI and no items have been found. Instead
387 // of showing the default status bar information ("0 items") a more helpful information is given:
388 m_statusBar
->setMessage(i18nc("@info:status", "No items found."), DolphinStatusBar::Information
);
393 // Enable the 'File'->'Create New...' menu only if the directory
395 KFileItem item
= m_dirLister
->rootItem();
397 // it is unclear whether writing is supported
398 m_isFolderWritable
= true;
400 KFileItemListProperties
capabilities(KFileItemList() << item
);
401 m_isFolderWritable
= capabilities
.supportsWriting();
405 emit
writeStateChanged(m_isFolderWritable
);
409 void DolphinViewContainer::showItemInfo(const KFileItem
& item
)
412 // Only clear the status bar if unimportant messages are shown.
413 // This prevents that information- or error-messages get hidden
414 // by moving the mouse above the viewport or when closing the
416 if (m_statusBar
->type() == DolphinStatusBar::Default
) {
417 m_statusBar
->clear();
420 m_statusBar
->setMessage(item
.getStatusBarInfo(), DolphinStatusBar::Default
);
424 void DolphinViewContainer::showInfoMessage(const QString
& msg
)
426 m_statusBar
->setMessage(msg
, DolphinStatusBar::Information
);
429 void DolphinViewContainer::showErrorMessage(const QString
& msg
)
431 m_statusBar
->setMessage(msg
, DolphinStatusBar::Error
);
434 void DolphinViewContainer::showOperationCompletedMessage(const QString
& msg
)
436 m_statusBar
->setMessage(msg
, DolphinStatusBar::OperationCompleted
);
439 void DolphinViewContainer::closeFilterBar()
442 m_filterBar
->clear();
444 emit
showFilterBarChanged(false);
447 void DolphinViewContainer::setNameFilter(const QString
& nameFilter
)
449 m_view
->setNameFilter(nameFilter
);
450 delayedStatusBarUpdate();
453 void DolphinViewContainer::activate()
458 void DolphinViewContainer::saveViewState()
460 QByteArray locationState
;
461 QDataStream
stream(&locationState
, QIODevice::WriteOnly
);
462 m_view
->saveState(stream
);
463 m_urlNavigator
->saveLocationState(locationState
);
466 void DolphinViewContainer::slotUrlNavigatorLocationChanged(const KUrl
& url
)
468 if (KProtocolManager::supportsListing(url
)) {
469 setSearchModeEnabled(isSearchUrl(url
));
472 if (isActive() && !isSearchUrl(url
)) {
473 // When an URL has been entered, the view should get the focus.
474 // The focus must be requested asynchronously, as changing the URL might create
475 // a new view widget.
476 QTimer::singleShot(0, this, SLOT(requestFocus()));
478 } else if (KProtocolManager::isSourceProtocol(url
)) {
479 QString app
= "konqueror";
480 if (url
.protocol().startsWith(QLatin1String("http"))) {
481 showErrorMessage(i18nc("@info:status",
482 "Dolphin does not support web pages, the web browser has been launched"));
483 const KConfigGroup
config(KSharedConfig::openConfig("kdeglobals"), "General");
484 const QString browser
= config
.readEntry("BrowserApplication");
485 if (!browser
.isEmpty()) {
487 if (app
.startsWith('!')) {
488 // a literal command has been configured, remove the '!' prefix
493 showErrorMessage(i18nc("@info:status",
494 "Protocol not supported by Dolphin, Konqueror has been launched"));
497 const QString secureUrl
= KShell::quoteArg(url
.pathOrUrl());
498 const QString command
= app
+ ' ' + secureUrl
;
499 KRun::runCommand(command
, app
, app
, this);
501 showErrorMessage(i18nc("@info:status", "Invalid protocol"));
505 void DolphinViewContainer::dropUrls(const KUrl
& destination
, QDropEvent
* event
)
507 DragAndDropHelper::instance().dropUrls(KFileItem(), destination
, event
, this);
510 void DolphinViewContainer::redirect(const KUrl
& oldUrl
, const KUrl
& newUrl
)
513 const bool block
= m_urlNavigator
->signalsBlocked();
514 m_urlNavigator
->blockSignals(true);
516 // Assure that the location state is reset for redirection URLs. This
517 // allows to skip redirection URLs when going back or forward in the
519 m_urlNavigator
->saveLocationState(QByteArray());
520 m_urlNavigator
->setLocationUrl(newUrl
);
521 setSearchModeEnabled(isSearchUrl(newUrl
));
523 m_urlNavigator
->blockSignals(block
);
526 void DolphinViewContainer::requestFocus()
531 void DolphinViewContainer::saveUrlCompletionMode(KGlobalSettings::Completion completion
)
533 DolphinSettings
& settings
= DolphinSettings::instance();
534 settings
.generalSettings()->setUrlCompletionMode(completion
);
538 void DolphinViewContainer::slotHistoryChanged()
540 QByteArray locationState
= m_urlNavigator
->locationState();
542 if (!locationState
.isEmpty()) {
543 QDataStream
stream(&locationState
, QIODevice::ReadOnly
);
544 m_view
->restoreState(stream
);
548 void DolphinViewContainer::startSearching(const QString
&text
)
551 const KUrl url
= m_searchBox
->urlForSearching();
552 if (url
.isValid() && !url
.isEmpty()) {
553 m_urlNavigator
->setLocationUrl(url
);
557 void DolphinViewContainer::closeSearchBox()
559 setSearchModeEnabled(false);
562 void DolphinViewContainer::stopLoading()
564 m_view
->stopLoading();
567 bool DolphinViewContainer::isSearchUrl(const KUrl
& url
) const
569 const QString protocol
= url
.protocol();
570 return protocol
.contains("search") || (protocol
== QLatin1String("nepomuk"));
573 void DolphinViewContainer::slotItemTriggered(const KFileItem
& item
)
575 KUrl url
= item
.targetUrl();
582 const GeneralSettings
* settings
= DolphinSettings::instance().generalSettings();
583 const bool browseThroughArchives
= settings
->browseThroughArchives();
584 if (browseThroughArchives
&& item
.isFile() && url
.isLocalFile()) {
585 // Generic mechanism for redirecting to tar:/<path>/ when clicking on a tar file,
586 // zip:/<path>/ when clicking on a zip file, etc.
587 // The .protocol file specifies the mimetype that the kioslave handles.
588 // Note that we don't use mimetype inheritance since we don't want to
589 // open OpenDocument files as zip folders...
590 const QString protocol
= KProtocolManager::protocolForArchiveMimetype(item
.mimetype());
591 if (!protocol
.isEmpty()) {
592 url
.setProtocol(protocol
);
598 if (item
.mimetype() == "application/x-desktop") {
599 // redirect to the url in Type=Link desktop files
600 KDesktopFile
desktopFile(url
.toLocalFile());
601 if (desktopFile
.hasLinkType()) {
602 url
= desktopFile
.readUrl();
611 void DolphinViewContainer::openFile(const KUrl
& url
)
613 // Using m_dolphinModel for getting the file item instance is not possible
614 // here: openFile() is triggered by an error of the directory lister
615 // job, so the file item must be received "manually".
616 const KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, url
);
617 slotItemTriggered(item
);
620 #include "dolphinviewcontainer.moc"