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()) {
245 m_searchBox
->setVisible(enabled
);
246 m_urlNavigator
->setVisible(!enabled
);
249 // Remember the most recent non-search URL as search path
250 // of the search-box, so that it can be restored
251 // when switching back to the URL navigator.
252 KUrl url
= m_urlNavigator
->locationUrl();
254 int index
= m_urlNavigator
->historyIndex();
255 const int historySize
= m_urlNavigator
->historySize();
256 while (isSearchUrl(url
) && (index
< historySize
)) {
258 url
= m_urlNavigator
->locationUrl(index
);
261 if (!isSearchUrl(url
)) {
262 m_searchBox
->setSearchPath(url
);
265 // Restore the URL for the URL navigator. If Dolphin has been
266 // started with a search-URL, the home URL is used as fallback.
267 const KUrl url
= m_searchBox
->searchPath();
268 if (url
.isValid() && !url
.isEmpty()) {
269 if (isSearchUrl(url
)) {
270 m_urlNavigator
->goHome();
272 m_urlNavigator
->setLocationUrl(url
);
277 emit
searchModeChanged(enabled
);
280 bool DolphinViewContainer::isSearchModeEnabled() const
282 return m_searchBox
->isVisible();
285 void DolphinViewContainer::setUrl(const KUrl
& newUrl
)
287 if (newUrl
!= m_urlNavigator
->locationUrl()) {
288 m_urlNavigator
->setLocationUrl(newUrl
);
292 void DolphinViewContainer::setFilterBarVisible(bool visible
)
294 Q_ASSERT(m_filterBar
!= 0);
297 m_filterBar
->setFocus();
303 void DolphinViewContainer::delayedStatusBarUpdate()
305 if (m_statusBarTimer
->isActive() && (m_statusBarTimestamp
.elapsed() > 2000)) {
306 // No update of the statusbar has been done during the last 2 seconds,
307 // although an update has been requested. Trigger an immediate update.
308 m_statusBarTimer
->stop();
311 // Invoke updateStatusBar() with a small delay. This assures that
312 // when a lot of delayedStatusBarUpdates() are done in a short time,
313 // no bottleneck is given.
314 m_statusBarTimer
->start();
318 void DolphinViewContainer::updateStatusBar()
320 m_statusBarTimestamp
.start();
322 // As the item count information is less important
323 // in comparison with other messages, it should only
325 // - the status bar is empty or
326 // - shows already the item count information or
327 // - shows only a not very important information
328 const QString newMessage
= m_view
->statusBarText();
329 const QString currentMessage
= m_statusBar
->message();
330 const bool updateStatusBarMsg
= currentMessage
.isEmpty()
331 || (currentMessage
== m_statusBar
->defaultText())
332 || (m_statusBar
->type() == DolphinStatusBar::Information
);
334 m_statusBar
->setDefaultText(newMessage
);
336 if (updateStatusBarMsg
) {
337 m_statusBar
->setMessage(newMessage
, DolphinStatusBar::Default
);
341 void DolphinViewContainer::updateProgress(int percent
)
343 if (m_statusBar
->progressText().isEmpty()) {
344 m_statusBar
->setProgressText(i18nc("@info:progress", "Loading folder..."));
346 m_statusBar
->setProgress(percent
);
349 void DolphinViewContainer::slotStartedPathLoading()
351 if (isSearchUrl(url())) {
352 // Search KIO-slaves usually don't provide any progress information. Give
353 // a hint to the user that a searching is done:
355 m_statusBar
->setProgressText(i18nc("@info", "Searching..."));
356 m_statusBar
->setProgress(-1);
358 // Trigger an undetermined progress indication. The progress
359 // information in percent will be triggered by the percent() signal
360 // of the directory lister later.
365 void DolphinViewContainer::slotFinishedPathLoading()
367 if (!m_statusBar
->progressText().isEmpty()) {
368 m_statusBar
->setProgressText(QString());
369 m_statusBar
->setProgress(100);
372 if (isSearchUrl(url()) && (m_view
->items().count() == 0)) {
373 // The dir lister has been completed on a Nepomuk-URI and no items have been found. Instead
374 // of showing the default status bar information ("0 items") a more helpful information is given:
375 m_statusBar
->setMessage(i18nc("@info:status", "No items found."), DolphinStatusBar::Information
);
381 void DolphinViewContainer::showItemInfo(const KFileItem
& item
)
384 // Only clear the status bar if unimportant messages are shown.
385 // This prevents that information- or error-messages get hidden
386 // by moving the mouse above the viewport or when closing the
388 if (m_statusBar
->type() == DolphinStatusBar::Default
) {
389 m_statusBar
->clear();
392 m_statusBar
->setMessage(item
.getStatusBarInfo(), DolphinStatusBar::Default
);
396 void DolphinViewContainer::showInfoMessage(const QString
& msg
)
398 m_statusBar
->setMessage(msg
, DolphinStatusBar::Information
);
401 void DolphinViewContainer::showErrorMessage(const QString
& msg
)
403 m_statusBar
->setMessage(msg
, DolphinStatusBar::Error
);
406 void DolphinViewContainer::showOperationCompletedMessage(const QString
& msg
)
408 m_statusBar
->setMessage(msg
, DolphinStatusBar::OperationCompleted
);
411 void DolphinViewContainer::closeFilterBar()
414 m_filterBar
->clear();
416 emit
showFilterBarChanged(false);
419 void DolphinViewContainer::setNameFilter(const QString
& nameFilter
)
421 m_view
->setNameFilter(nameFilter
);
422 delayedStatusBarUpdate();
425 void DolphinViewContainer::activate()
430 void DolphinViewContainer::saveViewState()
432 QByteArray locationState
;
433 QDataStream
stream(&locationState
, QIODevice::WriteOnly
);
434 m_view
->saveState(stream
);
435 m_urlNavigator
->saveLocationState(locationState
);
438 void DolphinViewContainer::slotUrlNavigatorLocationChanged(const KUrl
& url
)
440 if (KProtocolManager::supportsListing(url
)) {
441 setSearchModeEnabled(isSearchUrl(url
));
444 if (isActive() && !isSearchUrl(url
)) {
445 // When an URL has been entered, the view should get the focus.
446 // The focus must be requested asynchronously, as changing the URL might create
447 // a new view widget.
448 QTimer::singleShot(0, this, SLOT(requestFocus()));
450 } else if (KProtocolManager::isSourceProtocol(url
)) {
451 QString app
= "konqueror";
452 if (url
.protocol().startsWith(QLatin1String("http"))) {
453 showErrorMessage(i18nc("@info:status",
454 "Dolphin does not support web pages, the web browser has been launched"));
455 const KConfigGroup
config(KSharedConfig::openConfig("kdeglobals"), "General");
456 const QString browser
= config
.readEntry("BrowserApplication");
457 if (!browser
.isEmpty()) {
459 if (app
.startsWith('!')) {
460 // a literal command has been configured, remove the '!' prefix
465 showErrorMessage(i18nc("@info:status",
466 "Protocol not supported by Dolphin, Konqueror has been launched"));
469 const QString secureUrl
= KShell::quoteArg(url
.pathOrUrl());
470 const QString command
= app
+ ' ' + secureUrl
;
471 KRun::runCommand(command
, app
, app
, this);
473 showErrorMessage(i18nc("@info:status", "Invalid protocol"));
477 void DolphinViewContainer::dropUrls(const KUrl
& destination
, QDropEvent
* event
)
479 DragAndDropHelper::instance().dropUrls(KFileItem(), destination
, event
, this);
482 void DolphinViewContainer::redirect(const KUrl
& oldUrl
, const KUrl
& newUrl
)
485 const bool block
= m_urlNavigator
->signalsBlocked();
486 m_urlNavigator
->blockSignals(true);
488 // Assure that the location state is reset for redirection URLs. This
489 // allows to skip redirection URLs when going back or forward in the
491 m_urlNavigator
->saveLocationState(QByteArray());
492 m_urlNavigator
->setLocationUrl(newUrl
);
493 setSearchModeEnabled(isSearchUrl(newUrl
));
495 m_urlNavigator
->blockSignals(block
);
498 void DolphinViewContainer::requestFocus()
503 void DolphinViewContainer::saveUrlCompletionMode(KGlobalSettings::Completion completion
)
505 DolphinSettings
& settings
= DolphinSettings::instance();
506 settings
.generalSettings()->setUrlCompletionMode(completion
);
510 void DolphinViewContainer::slotHistoryChanged()
512 QByteArray locationState
= m_urlNavigator
->locationState();
514 if (!locationState
.isEmpty()) {
515 QDataStream
stream(&locationState
, QIODevice::ReadOnly
);
516 m_view
->restoreState(stream
);
520 void DolphinViewContainer::startSearching(const QString
&text
)
523 const KUrl url
= m_searchBox
->urlForSearching();
524 if (url
.isValid() && !url
.isEmpty()) {
525 m_urlNavigator
->setLocationUrl(url
);
529 void DolphinViewContainer::closeSearchBox()
531 setSearchModeEnabled(false);
534 void DolphinViewContainer::stopLoading()
536 m_view
->stopLoading();
539 bool DolphinViewContainer::isSearchUrl(const KUrl
& url
) const
541 const QString protocol
= url
.protocol();
542 return protocol
.contains("search") || (protocol
== QLatin1String("nepomuk"));
545 void DolphinViewContainer::slotItemTriggered(const KFileItem
& item
)
547 KUrl url
= item
.targetUrl();
554 const GeneralSettings
* settings
= DolphinSettings::instance().generalSettings();
555 const bool browseThroughArchives
= settings
->browseThroughArchives();
556 if (browseThroughArchives
&& item
.isFile() && url
.isLocalFile()) {
557 // Generic mechanism for redirecting to tar:/<path>/ when clicking on a tar file,
558 // zip:/<path>/ when clicking on a zip file, etc.
559 // The .protocol file specifies the mimetype that the kioslave handles.
560 // Note that we don't use mimetype inheritance since we don't want to
561 // open OpenDocument files as zip folders...
562 const QString protocol
= KProtocolManager::protocolForArchiveMimetype(item
.mimetype());
563 if (!protocol
.isEmpty()) {
564 url
.setProtocol(protocol
);
570 if (item
.mimetype() == "application/x-desktop") {
571 // redirect to the url in Type=Link desktop files
572 KDesktopFile
desktopFile(url
.toLocalFile());
573 if (desktopFile
.hasLinkType()) {
574 url
= desktopFile
.readUrl();
583 void DolphinViewContainer::openFile(const KUrl
& url
)
585 const KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, url
);
586 slotItemTriggered(item
);
589 #include "dolphinviewcontainer.moc"