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 <konq_operations.h>
44 #include <kurlcombobox.h>
45 #include <kurlnavigator.h>
48 #include "dolphin_generalsettings.h"
49 #include "dolphinmainwindow.h"
50 #include "filterbar/filterbar.h"
51 #include "search/dolphinsearchbox.h"
52 #include "settings/dolphinsettings.h"
53 #include "statusbar/dolphinstatusbar.h"
54 #include "views/dolphincolumnview.h"
55 #include "views/dolphindetailsview.h"
56 #include "views/dolphindirlister.h"
57 #include "views/dolphinsortfilterproxymodel.h"
58 #include "views/draganddrophelper.h"
59 #include "views/dolphiniconsview.h"
60 #include "views/dolphinmodel.h"
61 #include "views/dolphinviewcontroller.h"
62 #include "views/viewmodecontroller.h"
63 #include "views/viewproperties.h"
65 DolphinViewContainer::DolphinViewContainer(const KUrl
& url
, QWidget
* parent
) :
74 m_statusBarTimestamp()
78 m_topLayout
= new QVBoxLayout(this);
79 m_topLayout
->setSpacing(0);
80 m_topLayout
->setMargin(0);
82 m_urlNavigator
= new KUrlNavigator(DolphinSettings::instance().placesModel(), url
, this);
83 connect(m_urlNavigator
, SIGNAL(urlsDropped(const KUrl
&, QDropEvent
*)),
84 this, SLOT(dropUrls(const KUrl
&, QDropEvent
*)));
85 connect(m_urlNavigator
, SIGNAL(activated()),
86 this, SLOT(activate()));
87 connect(m_urlNavigator
->editor(), SIGNAL(completionModeChanged(KGlobalSettings::Completion
)),
88 this, SLOT(saveUrlCompletionMode(KGlobalSettings::Completion
)));
90 const GeneralSettings
* settings
= DolphinSettings::instance().generalSettings();
91 m_urlNavigator
->setUrlEditable(settings
->editableUrl());
92 m_urlNavigator
->setShowFullPath(settings
->showFullPath());
93 m_urlNavigator
->setHomeUrl(KUrl(settings
->homeUrl()));
94 KUrlComboBox
* editor
= m_urlNavigator
->editor();
95 editor
->setCompletionMode(KGlobalSettings::Completion(settings
->urlCompletionMode()));
97 m_searchBox
= new DolphinSearchBox(this);
99 connect(m_searchBox
, SIGNAL(closeRequest()), this, SLOT(closeSearchBox()));
100 connect(m_searchBox
, SIGNAL(search(QString
)), this, SLOT(startSearching(QString
)));
101 connect(m_searchBox
, SIGNAL(returnPressed(QString
)), this, SLOT(requestFocus()));
103 DolphinDirLister
* dirLister
= new DolphinDirLister();
104 dirLister
->setAutoUpdate(true);
105 dirLister
->setMainWindow(window());
106 dirLister
->setDelayedMimeTypes(true);
108 DolphinModel
* dolphinModel
= new DolphinModel(this);
109 dolphinModel
->setDirLister(dirLister
); // dolphinModel takes ownership of dirLister
110 dolphinModel
->setDropsAllowed(DolphinModel::DropOnDirectory
);
112 DolphinSortFilterProxyModel
* proxyModel
= new DolphinSortFilterProxyModel(this);
113 proxyModel
->setSourceModel(dolphinModel
);
114 proxyModel
->setFilterCaseSensitivity(Qt::CaseInsensitive
);
116 // TODO: In the case of the column view the directory lister changes. Let the DolphinView
117 // inform the container about this information for KDE SC 4.7
118 connect(dirLister
, SIGNAL(clear()),
119 this, SLOT(delayedStatusBarUpdate()));
120 connect(dirLister
, SIGNAL(percent(int)),
121 this, SLOT(updateProgress(int)));
122 connect(dirLister
, SIGNAL(itemsDeleted(const KFileItemList
&)),
123 this, SLOT(delayedStatusBarUpdate()));
124 connect(dirLister
, SIGNAL(newItems(KFileItemList
)),
125 this, SLOT(delayedStatusBarUpdate()));
126 connect(dirLister
, SIGNAL(infoMessage(const QString
&)),
127 this, SLOT(showInfoMessage(const QString
&)));
128 connect(dirLister
, SIGNAL(errorMessage(const QString
&)),
129 this, SLOT(showErrorMessage(const QString
&)));
130 connect(dirLister
, SIGNAL(urlIsFileError(const KUrl
&)),
131 this, SLOT(openFile(const KUrl
&)));
133 m_view
= new DolphinView(this, url
, proxyModel
);
134 connect(m_view
, SIGNAL(urlChanged(const KUrl
&)),
135 m_urlNavigator
, SLOT(setUrl(const KUrl
&)));
136 connect(m_view
, SIGNAL(requestItemInfo(KFileItem
)),
137 this, SLOT(showItemInfo(KFileItem
)));
138 connect(m_view
, SIGNAL(errorMessage(const QString
&)),
139 this, SLOT(showErrorMessage(const QString
&)));
140 connect(m_view
, SIGNAL(infoMessage(const QString
&)),
141 this, SLOT(showInfoMessage(const QString
&)));
142 connect(m_view
, SIGNAL(operationCompletedMessage(const QString
&)),
143 this, SLOT(showOperationCompletedMessage(const QString
&)));
144 connect(m_view
, SIGNAL(itemTriggered(KFileItem
)),
145 this, SLOT(slotItemTriggered(KFileItem
)));
146 connect(m_view
, SIGNAL(redirection(KUrl
, KUrl
)),
147 this, SLOT(redirect(KUrl
, KUrl
)));
148 connect(m_view
, SIGNAL(selectionChanged(const KFileItemList
&)),
149 this, SLOT(delayedStatusBarUpdate()));
150 connect(m_view
, SIGNAL(startedPathLoading(KUrl
)),
151 this, SLOT(slotStartedPathLoading()));
152 connect(m_view
, SIGNAL(finishedPathLoading(KUrl
)),
153 this, SLOT(slotFinishedPathLoading()));
154 connect(m_view
, SIGNAL(writeStateChanged(bool)),
155 this, SIGNAL(writeStateChanged(bool)));
157 connect(m_urlNavigator
, SIGNAL(urlChanged(const KUrl
&)),
158 this, SLOT(slotUrlNavigatorLocationChanged(const KUrl
&)));
159 connect(m_urlNavigator
, SIGNAL(urlAboutToBeChanged(const KUrl
&)),
160 this, SLOT(saveViewState()));
161 connect(m_urlNavigator
, SIGNAL(historyChanged()),
162 this, SLOT(slotHistoryChanged()));
164 // initialize status bar
165 m_statusBar
= new DolphinStatusBar(this, m_view
);
166 connect(m_statusBar
, SIGNAL(stopPressed()), this, SLOT(stopLoading()));
168 m_statusBarTimer
= new QTimer(this);
169 m_statusBarTimer
->setSingleShot(true);
170 m_statusBarTimer
->setInterval(300);
171 connect(m_statusBarTimer
, SIGNAL(timeout()),
172 this, SLOT(updateStatusBar()));
174 KIO::FileUndoManager
* undoManager
= KIO::FileUndoManager::self();
175 connect(undoManager
, SIGNAL(jobRecordingFinished(CommandType
)),
176 this, SLOT(delayedStatusBarUpdate()));
178 // initialize filter bar
179 m_filterBar
= new FilterBar(this);
180 m_filterBar
->setVisible(settings
->filterBar());
181 connect(m_filterBar
, SIGNAL(filterChanged(const QString
&)),
182 this, SLOT(setNameFilter(const QString
&)));
183 connect(m_filterBar
, SIGNAL(closeRequest()),
184 this, SLOT(closeFilterBar()));
185 connect(m_view
, SIGNAL(urlChanged(const KUrl
&)),
186 m_filterBar
, SLOT(clear()));
188 m_topLayout
->addWidget(m_urlNavigator
);
189 m_topLayout
->addWidget(m_searchBox
);
190 m_topLayout
->addWidget(m_view
);
191 m_topLayout
->addWidget(m_filterBar
);
192 m_topLayout
->addWidget(m_statusBar
);
194 setSearchModeEnabled(isSearchUrl(url
));
197 DolphinViewContainer::~DolphinViewContainer()
201 KUrl
DolphinViewContainer::url() const
203 return m_urlNavigator
->locationUrl();
206 void DolphinViewContainer::setActive(bool active
)
208 m_urlNavigator
->setActive(active
);
209 m_view
->setActive(active
);
212 bool DolphinViewContainer::isActive() const
214 Q_ASSERT(m_view
->isActive() == m_urlNavigator
->isActive());
215 return m_view
->isActive();
218 void DolphinViewContainer::refresh()
220 GeneralSettings
* settings
= DolphinSettings::instance().generalSettings();
221 if (settings
->modifiedStartupSettings()) {
222 // The startup settings should only get applied if they have been
223 // modified by the user. Otherwise keep the (possibly) different current
224 // settings of the URL navigator and the filterbar.
225 m_urlNavigator
->setUrlEditable(settings
->editableUrl());
226 m_urlNavigator
->setShowFullPath(settings
->showFullPath());
227 setFilterBarVisible(settings
->filterBar());
231 m_statusBar
->refresh();
234 bool DolphinViewContainer::isFilterBarVisible() const
236 return m_filterBar
->isVisible();
239 void DolphinViewContainer::setSearchModeEnabled(bool enabled
)
241 if (enabled
== isSearchModeEnabled()) {
242 if (enabled
&& !m_searchBox
->hasFocus()) {
243 m_searchBox
->setFocus();
244 m_searchBox
->selectAll();
249 m_searchBox
->setVisible(enabled
);
250 m_urlNavigator
->setVisible(!enabled
);
253 // Remember the most recent non-search URL as search path
254 // of the search-box, so that it can be restored
255 // when switching back to the URL navigator.
256 KUrl url
= m_urlNavigator
->locationUrl();
258 int index
= m_urlNavigator
->historyIndex();
259 const int historySize
= m_urlNavigator
->historySize();
260 while (isSearchUrl(url
) && (index
< historySize
)) {
262 url
= m_urlNavigator
->locationUrl(index
);
265 if (!isSearchUrl(url
)) {
266 m_searchBox
->setSearchPath(url
);
269 // Restore the URL for the URL navigator. If Dolphin has been
270 // started with a search-URL, the home URL is used as fallback.
271 const KUrl url
= m_searchBox
->searchPath();
272 if (url
.isValid() && !url
.isEmpty()) {
273 if (isSearchUrl(url
)) {
274 m_urlNavigator
->goHome();
276 m_urlNavigator
->setLocationUrl(url
);
281 emit
searchModeChanged(enabled
);
284 bool DolphinViewContainer::isSearchModeEnabled() const
286 return m_searchBox
->isVisible();
289 void DolphinViewContainer::setUrl(const KUrl
& newUrl
)
291 if (newUrl
!= m_urlNavigator
->locationUrl()) {
292 m_urlNavigator
->setLocationUrl(newUrl
);
296 void DolphinViewContainer::setFilterBarVisible(bool visible
)
298 Q_ASSERT(m_filterBar
!= 0);
301 m_filterBar
->setFocus();
302 m_filterBar
->selectAll();
308 void DolphinViewContainer::delayedStatusBarUpdate()
310 if (m_statusBarTimer
->isActive() && (m_statusBarTimestamp
.elapsed() > 2000)) {
311 // No update of the statusbar has been done during the last 2 seconds,
312 // although an update has been requested. Trigger an immediate update.
313 m_statusBarTimer
->stop();
316 // Invoke updateStatusBar() with a small delay. This assures that
317 // when a lot of delayedStatusBarUpdates() are done in a short time,
318 // no bottleneck is given.
319 m_statusBarTimer
->start();
323 void DolphinViewContainer::updateStatusBar()
325 m_statusBarTimestamp
.start();
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 const QString newMessage
= m_view
->statusBarText();
334 const QString currentMessage
= m_statusBar
->message();
335 const bool updateStatusBarMsg
= currentMessage
.isEmpty()
336 || (currentMessage
== m_statusBar
->defaultText())
337 || (m_statusBar
->type() == DolphinStatusBar::Information
);
339 m_statusBar
->setDefaultText(newMessage
);
341 if (updateStatusBarMsg
) {
342 m_statusBar
->setMessage(newMessage
, DolphinStatusBar::Default
);
346 void DolphinViewContainer::updateProgress(int percent
)
348 if (m_statusBar
->progressText().isEmpty()) {
349 m_statusBar
->setProgressText(i18nc("@info:progress", "Loading folder..."));
351 m_statusBar
->setProgress(percent
);
354 void DolphinViewContainer::slotStartedPathLoading()
356 if (isSearchUrl(url())) {
357 // Search KIO-slaves usually don't provide any progress information. Give
358 // a hint to the user that a searching is done:
360 m_statusBar
->setProgressText(i18nc("@info", "Searching..."));
361 m_statusBar
->setProgress(-1);
363 // Trigger an undetermined progress indication. The progress
364 // information in percent will be triggered by the percent() signal
365 // of the directory lister later.
370 void DolphinViewContainer::slotFinishedPathLoading()
372 if (!m_statusBar
->progressText().isEmpty()) {
373 m_statusBar
->setProgressText(QString());
374 m_statusBar
->setProgress(100);
377 if (isSearchUrl(url()) && (m_view
->items().count() == 0)) {
378 // The dir lister has been completed on a Nepomuk-URI and no items have been found. Instead
379 // of showing the default status bar information ("0 items") a more helpful information is given:
380 m_statusBar
->setMessage(i18nc("@info:status", "No items found."), DolphinStatusBar::Information
);
386 void DolphinViewContainer::showItemInfo(const KFileItem
& item
)
389 // Only clear the status bar if unimportant messages are shown.
390 // This prevents that information- or error-messages get hidden
391 // by moving the mouse above the viewport or when closing the
393 if (m_statusBar
->type() == DolphinStatusBar::Default
) {
394 m_statusBar
->clear();
397 m_statusBar
->setMessage(item
.getStatusBarInfo(), DolphinStatusBar::Default
);
401 void DolphinViewContainer::showInfoMessage(const QString
& msg
)
403 m_statusBar
->setMessage(msg
, DolphinStatusBar::Information
);
406 void DolphinViewContainer::showErrorMessage(const QString
& msg
)
408 m_statusBar
->setMessage(msg
, DolphinStatusBar::Error
);
411 void DolphinViewContainer::showOperationCompletedMessage(const QString
& msg
)
413 m_statusBar
->setMessage(msg
, DolphinStatusBar::OperationCompleted
);
416 void DolphinViewContainer::closeFilterBar()
419 m_filterBar
->clear();
421 emit
showFilterBarChanged(false);
424 void DolphinViewContainer::setNameFilter(const QString
& nameFilter
)
426 m_view
->setNameFilter(nameFilter
);
427 delayedStatusBarUpdate();
430 void DolphinViewContainer::activate()
435 void DolphinViewContainer::saveViewState()
437 QByteArray locationState
;
438 QDataStream
stream(&locationState
, QIODevice::WriteOnly
);
439 m_view
->saveState(stream
);
440 m_urlNavigator
->saveLocationState(locationState
);
443 void DolphinViewContainer::slotUrlNavigatorLocationChanged(const KUrl
& url
)
445 if (KProtocolManager::supportsListing(url
)) {
446 setSearchModeEnabled(isSearchUrl(url
));
449 if (isActive() && !isSearchUrl(url
)) {
450 // When an URL has been entered, the view should get the focus.
451 // The focus must be requested asynchronously, as changing the URL might create
452 // a new view widget.
453 QTimer::singleShot(0, this, SLOT(requestFocus()));
455 } else if (KProtocolManager::isSourceProtocol(url
)) {
456 QString app
= "konqueror";
457 if (url
.protocol().startsWith(QLatin1String("http"))) {
458 showErrorMessage(i18nc("@info:status",
459 "Dolphin does not support web pages, the web browser has been launched"));
460 const KConfigGroup
config(KSharedConfig::openConfig("kdeglobals"), "General");
461 const QString browser
= config
.readEntry("BrowserApplication");
462 if (!browser
.isEmpty()) {
464 if (app
.startsWith('!')) {
465 // a literal command has been configured, remove the '!' prefix
470 showErrorMessage(i18nc("@info:status",
471 "Protocol not supported by Dolphin, Konqueror has been launched"));
474 const QString secureUrl
= KShell::quoteArg(url
.pathOrUrl());
475 const QString command
= app
+ ' ' + secureUrl
;
476 KRun::runCommand(command
, app
, app
, this);
478 showErrorMessage(i18nc("@info:status", "Invalid protocol"));
482 void DolphinViewContainer::dropUrls(const KUrl
& destination
, QDropEvent
* event
)
484 DragAndDropHelper::instance().dropUrls(KFileItem(), destination
, event
, this);
487 void DolphinViewContainer::redirect(const KUrl
& oldUrl
, const KUrl
& newUrl
)
490 const bool block
= m_urlNavigator
->signalsBlocked();
491 m_urlNavigator
->blockSignals(true);
493 // Assure that the location state is reset for redirection URLs. This
494 // allows to skip redirection URLs when going back or forward in the
496 m_urlNavigator
->saveLocationState(QByteArray());
497 m_urlNavigator
->setLocationUrl(newUrl
);
498 setSearchModeEnabled(isSearchUrl(newUrl
));
500 m_urlNavigator
->blockSignals(block
);
503 void DolphinViewContainer::requestFocus()
508 void DolphinViewContainer::saveUrlCompletionMode(KGlobalSettings::Completion completion
)
510 DolphinSettings
& settings
= DolphinSettings::instance();
511 settings
.generalSettings()->setUrlCompletionMode(completion
);
515 void DolphinViewContainer::slotHistoryChanged()
517 QByteArray locationState
= m_urlNavigator
->locationState();
519 if (!locationState
.isEmpty()) {
520 QDataStream
stream(&locationState
, QIODevice::ReadOnly
);
521 m_view
->restoreState(stream
);
525 void DolphinViewContainer::startSearching(const QString
&text
)
528 const KUrl url
= m_searchBox
->urlForSearching();
529 if (url
.isValid() && !url
.isEmpty()) {
530 m_urlNavigator
->setLocationUrl(url
);
534 void DolphinViewContainer::closeSearchBox()
536 setSearchModeEnabled(false);
539 void DolphinViewContainer::stopLoading()
541 m_view
->stopLoading();
544 bool DolphinViewContainer::isSearchUrl(const KUrl
& url
) const
546 const QString protocol
= url
.protocol();
547 return protocol
.contains("search") || (protocol
== QLatin1String("nepomuk"));
550 void DolphinViewContainer::slotItemTriggered(const KFileItem
& item
)
552 KUrl url
= item
.targetUrl();
559 const GeneralSettings
* settings
= DolphinSettings::instance().generalSettings();
560 const bool browseThroughArchives
= settings
->browseThroughArchives();
561 if (browseThroughArchives
&& item
.isFile() && url
.isLocalFile()) {
562 // Generic mechanism for redirecting to tar:/<path>/ when clicking on a tar file,
563 // zip:/<path>/ when clicking on a zip file, etc.
564 // The .protocol file specifies the mimetype that the kioslave handles.
565 // Note that we don't use mimetype inheritance since we don't want to
566 // open OpenDocument files as zip folders...
567 const QString protocol
= KProtocolManager::protocolForArchiveMimetype(item
.mimetype());
568 if (!protocol
.isEmpty()) {
569 url
.setProtocol(protocol
);
575 if (item
.mimetype() == "application/x-desktop") {
576 // redirect to the url in Type=Link desktop files
577 KDesktopFile
desktopFile(url
.toLocalFile());
578 if (desktopFile
.hasLinkType()) {
579 url
= desktopFile
.readUrl();
588 void DolphinViewContainer::openFile(const KUrl
& url
)
590 const KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, url
);
591 slotItemTriggered(item
);
594 #include "dolphinviewcontainer.moc"