]>
cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinviewcontainer.cpp
1 /***************************************************************************
2 * Copyright (C) 2007 by Peter Penz <peter.penz19@gmail.com> *
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>
23 #include <QApplication>
25 #include <QItemSelection>
30 #include <KDesktopFile>
31 #include <KFileItemDelegate>
32 #include <KFilePlacesModel>
33 #include <KGlobalSettings>
35 #include <KIconEffect>
36 #include <KIO/NetAccess>
37 #include <KIO/PreviewJob>
39 #include <KNewFileMenu>
40 #include <konqmimedata.h>
41 #include <konq_operations.h>
44 #include <KUrlComboBox>
45 #include <KUrlNavigator>
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/draganddrophelper.h"
57 #include "views/dolphiniconsview.h"
58 #include "views/dolphinmodel.h"
59 #include "views/dolphinviewcontroller.h"
60 #include "views/viewmodecontroller.h"
61 #include "views/viewproperties.h"
63 DolphinViewContainer::DolphinViewContainer(const KUrl
& url
, QWidget
* parent
) :
72 m_statusBarTimestamp()
76 m_topLayout
= new QVBoxLayout(this);
77 m_topLayout
->setSpacing(0);
78 m_topLayout
->setMargin(0);
80 m_urlNavigator
= new KUrlNavigator(DolphinSettings::instance().placesModel(), url
, this);
81 connect(m_urlNavigator
, SIGNAL(urlsDropped(const KUrl
&, QDropEvent
*)),
82 this, SLOT(dropUrls(const KUrl
&, QDropEvent
*)));
83 connect(m_urlNavigator
, SIGNAL(activated()),
84 this, SLOT(activate()));
85 connect(m_urlNavigator
->editor(), SIGNAL(completionModeChanged(KGlobalSettings::Completion
)),
86 this, SLOT(saveUrlCompletionMode(KGlobalSettings::Completion
)));
88 const GeneralSettings
* settings
= DolphinSettings::instance().generalSettings();
89 m_urlNavigator
->setUrlEditable(settings
->editableUrl());
90 m_urlNavigator
->setShowFullPath(settings
->showFullPath());
91 m_urlNavigator
->setHomeUrl(KUrl(settings
->homeUrl()));
92 KUrlComboBox
* editor
= m_urlNavigator
->editor();
93 editor
->setCompletionMode(KGlobalSettings::Completion(settings
->urlCompletionMode()));
95 m_searchBox
= new DolphinSearchBox(this);
97 connect(m_searchBox
, SIGNAL(closeRequest()), this, SLOT(closeSearchBox()));
98 connect(m_searchBox
, SIGNAL(search(QString
)), this, SLOT(startSearching(QString
)));
99 connect(m_searchBox
, SIGNAL(returnPressed(QString
)), this, SLOT(requestFocus()));
101 m_view
= new DolphinView(url
, this);
102 connect(m_view
, SIGNAL(urlChanged(const KUrl
&)), m_urlNavigator
, SLOT(setUrl(const KUrl
&)));
103 connect(m_view
, SIGNAL(writeStateChanged(bool)), this, SIGNAL(writeStateChanged(bool)));
104 connect(m_view
, SIGNAL(requestItemInfo(KFileItem
)), this, SLOT(showItemInfo(KFileItem
)));
105 connect(m_view
, SIGNAL(errorMessage(const QString
&)), this, SLOT(showErrorMessage(const QString
&)));
106 connect(m_view
, SIGNAL(infoMessage(const QString
&)), this, SLOT(showInfoMessage(const QString
&)));
107 connect(m_view
, SIGNAL(itemTriggered(KFileItem
)), this, SLOT(slotItemTriggered(KFileItem
)));
108 connect(m_view
, SIGNAL(redirection(KUrl
, KUrl
)), this, SLOT(redirect(KUrl
, KUrl
)));
109 connect(m_view
, SIGNAL(startedPathLoading(KUrl
)), this, SLOT(slotStartedPathLoading()));
110 connect(m_view
, SIGNAL(finishedPathLoading(KUrl
)), this, SLOT(slotFinishedPathLoading()));
111 connect(m_view
, SIGNAL(itemCountChanged()), this, SLOT(delayedStatusBarUpdate()));
112 connect(m_view
, SIGNAL(pathLoadingProgress(int)), this, SLOT(updateProgress(int)));
113 connect(m_view
, SIGNAL(infoMessage(const QString
&)), this, SLOT(showInfoMessage(const QString
&)));
114 connect(m_view
, SIGNAL(errorMessage(const QString
&)), this, SLOT(showErrorMessage(const QString
&)));
115 connect(m_view
, SIGNAL(urlIsFileError(const KUrl
&)), this, SLOT(openFile(const KUrl
&)));
116 connect(m_view
, SIGNAL(selectionChanged(const KFileItemList
&)), this, SLOT(delayedStatusBarUpdate()));
117 connect(m_view
, SIGNAL(operationCompletedMessage(const QString
&)), this, SLOT(showOperationCompletedMessage(const QString
&)));
119 connect(m_urlNavigator
, SIGNAL(urlChanged(const KUrl
&)),
120 this, SLOT(slotUrlNavigatorLocationChanged(const KUrl
&)));
121 connect(m_urlNavigator
, SIGNAL(urlAboutToBeChanged(const KUrl
&)),
122 this, SLOT(saveViewState()));
123 connect(m_urlNavigator
, SIGNAL(historyChanged()),
124 this, SLOT(slotHistoryChanged()));
126 // initialize status bar
127 m_statusBar
= new DolphinStatusBar(this, m_view
);
128 connect(m_statusBar
, SIGNAL(stopPressed()), this, SLOT(stopLoading()));
130 m_statusBarTimer
= new QTimer(this);
131 m_statusBarTimer
->setSingleShot(true);
132 m_statusBarTimer
->setInterval(300);
133 connect(m_statusBarTimer
, SIGNAL(timeout()),
134 this, SLOT(updateStatusBar()));
136 KIO::FileUndoManager
* undoManager
= KIO::FileUndoManager::self();
137 connect(undoManager
, SIGNAL(jobRecordingFinished(CommandType
)),
138 this, SLOT(delayedStatusBarUpdate()));
140 // initialize filter bar
141 m_filterBar
= new FilterBar(this);
142 m_filterBar
->setVisible(settings
->filterBar());
143 connect(m_filterBar
, SIGNAL(filterChanged(const QString
&)),
144 this, SLOT(setNameFilter(const QString
&)));
145 connect(m_filterBar
, SIGNAL(closeRequest()),
146 this, SLOT(closeFilterBar()));
147 connect(m_view
, SIGNAL(urlChanged(const KUrl
&)),
148 m_filterBar
, SLOT(clear()));
150 m_topLayout
->addWidget(m_urlNavigator
);
151 m_topLayout
->addWidget(m_searchBox
);
152 m_topLayout
->addWidget(m_view
);
153 m_topLayout
->addWidget(m_filterBar
);
154 m_topLayout
->addWidget(m_statusBar
);
156 setSearchModeEnabled(isSearchUrl(url
));
159 DolphinViewContainer::~DolphinViewContainer()
163 KUrl
DolphinViewContainer::url() const
165 return m_urlNavigator
->locationUrl();
168 void DolphinViewContainer::setActive(bool active
)
170 m_urlNavigator
->setActive(active
);
171 m_view
->setActive(active
);
174 bool DolphinViewContainer::isActive() const
176 Q_ASSERT(m_view
->isActive() == m_urlNavigator
->isActive());
177 return m_view
->isActive();
180 void DolphinViewContainer::refresh()
182 GeneralSettings
* settings
= DolphinSettings::instance().generalSettings();
183 if (settings
->modifiedStartupSettings()) {
184 // The startup settings should only get applied if they have been
185 // modified by the user. Otherwise keep the (possibly) different current
186 // settings of the URL navigator and the filterbar.
187 m_urlNavigator
->setUrlEditable(settings
->editableUrl());
188 m_urlNavigator
->setShowFullPath(settings
->showFullPath());
189 setFilterBarVisible(settings
->filterBar());
193 m_statusBar
->refresh();
196 bool DolphinViewContainer::isFilterBarVisible() const
198 return m_filterBar
->isVisible();
201 void DolphinViewContainer::setSearchModeEnabled(bool enabled
)
203 if (enabled
== isSearchModeEnabled()) {
204 if (enabled
&& !m_searchBox
->hasFocus()) {
205 m_searchBox
->setFocus();
206 m_searchBox
->selectAll();
211 m_searchBox
->setVisible(enabled
);
212 m_urlNavigator
->setVisible(!enabled
);
215 KUrl url
= m_urlNavigator
->locationUrl();
216 m_searchBox
->setText(QString());
217 m_searchBox
->setReadOnly(isSearchUrl(url
));
219 // Remember the most recent non-search URL as search path
220 // of the search-box, so that it can be restored
221 // when switching back to the URL navigator.
222 int index
= m_urlNavigator
->historyIndex();
223 const int historySize
= m_urlNavigator
->historySize();
224 while (isSearchUrl(url
) && (index
< historySize
)) {
226 url
= m_urlNavigator
->locationUrl(index
);
229 if (!isSearchUrl(url
)) {
230 m_searchBox
->setSearchPath(url
);
233 // Restore the URL for the URL navigator. If Dolphin has been
234 // started with a search-URL, the home URL is used as fallback.
235 const KUrl url
= m_searchBox
->searchPath();
236 if (url
.isValid() && !url
.isEmpty()) {
237 if (isSearchUrl(url
)) {
238 m_urlNavigator
->goHome();
240 m_urlNavigator
->setLocationUrl(url
);
245 emit
searchModeChanged(enabled
);
248 bool DolphinViewContainer::isSearchModeEnabled() const
250 return m_searchBox
->isVisible();
253 void DolphinViewContainer::setUrl(const KUrl
& newUrl
)
255 if (newUrl
!= m_urlNavigator
->locationUrl()) {
256 m_urlNavigator
->setLocationUrl(newUrl
);
260 void DolphinViewContainer::setFilterBarVisible(bool visible
)
262 Q_ASSERT(m_filterBar
!= 0);
265 m_filterBar
->setFocus();
266 m_filterBar
->selectAll();
272 void DolphinViewContainer::delayedStatusBarUpdate()
274 if (m_statusBarTimer
->isActive() && (m_statusBarTimestamp
.elapsed() > 2000)) {
275 // No update of the statusbar has been done during the last 2 seconds,
276 // although an update has been requested. Trigger an immediate update.
277 m_statusBarTimer
->stop();
280 // Invoke updateStatusBar() with a small delay. This assures that
281 // when a lot of delayedStatusBarUpdates() are done in a short time,
282 // no bottleneck is given.
283 m_statusBarTimer
->start();
287 void DolphinViewContainer::updateStatusBar()
289 m_statusBarTimestamp
.start();
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 const QString newMessage
= m_view
->statusBarText();
298 const QString currentMessage
= m_statusBar
->message();
299 const bool updateStatusBarMsg
= currentMessage
.isEmpty()
300 || (currentMessage
== m_statusBar
->defaultText())
301 || (m_statusBar
->type() == DolphinStatusBar::Information
);
303 m_statusBar
->setDefaultText(newMessage
);
305 if (updateStatusBarMsg
) {
306 m_statusBar
->setMessage(newMessage
, DolphinStatusBar::Default
);
310 void DolphinViewContainer::updateProgress(int percent
)
312 if (m_statusBar
->progressText().isEmpty()) {
313 m_statusBar
->setProgressText(i18nc("@info:progress", "Loading folder..."));
315 m_statusBar
->setProgress(percent
);
318 void DolphinViewContainer::slotStartedPathLoading()
320 if (isSearchUrl(url())) {
321 // Search KIO-slaves usually don't provide any progress information. Give
322 // a hint to the user that a searching is done:
324 m_statusBar
->setProgressText(i18nc("@info", "Searching..."));
325 m_statusBar
->setProgress(-1);
327 // Trigger an undetermined progress indication. The progress
328 // information in percent will be triggered by the percent() signal
329 // of the directory lister later.
334 void DolphinViewContainer::slotFinishedPathLoading()
336 if (!m_statusBar
->progressText().isEmpty()) {
337 m_statusBar
->setProgressText(QString());
338 m_statusBar
->setProgress(100);
341 if (isSearchUrl(url()) && (m_view
->items().count() == 0)) {
342 // The dir lister has been completed on a Nepomuk-URI and no items have been found. Instead
343 // of showing the default status bar information ("0 items") a more helpful information is given:
344 m_statusBar
->setMessage(i18nc("@info:status", "No items found."), DolphinStatusBar::Information
);
350 void DolphinViewContainer::showItemInfo(const KFileItem
& item
)
353 // Only clear the status bar if unimportant messages are shown.
354 // This prevents that information- or error-messages get hidden
355 // by moving the mouse above the viewport or when closing the
357 if (m_statusBar
->type() == DolphinStatusBar::Default
) {
358 m_statusBar
->clear();
361 m_statusBar
->setMessage(item
.getStatusBarInfo(), DolphinStatusBar::Default
);
365 void DolphinViewContainer::showInfoMessage(const QString
& msg
)
367 m_statusBar
->setMessage(msg
, DolphinStatusBar::Information
);
370 void DolphinViewContainer::showErrorMessage(const QString
& msg
)
372 m_statusBar
->setMessage(msg
, DolphinStatusBar::Error
);
375 void DolphinViewContainer::showOperationCompletedMessage(const QString
& msg
)
377 m_statusBar
->setMessage(msg
, DolphinStatusBar::OperationCompleted
);
380 void DolphinViewContainer::closeFilterBar()
383 m_filterBar
->clear();
385 emit
showFilterBarChanged(false);
388 void DolphinViewContainer::setNameFilter(const QString
& nameFilter
)
390 m_view
->setNameFilter(nameFilter
);
391 delayedStatusBarUpdate();
394 void DolphinViewContainer::activate()
399 void DolphinViewContainer::saveViewState()
401 QByteArray locationState
;
402 QDataStream
stream(&locationState
, QIODevice::WriteOnly
);
403 m_view
->saveState(stream
);
404 m_urlNavigator
->saveLocationState(locationState
);
407 void DolphinViewContainer::slotUrlNavigatorLocationChanged(const KUrl
& url
)
409 if (KProtocolManager::supportsListing(url
)) {
410 setSearchModeEnabled(isSearchUrl(url
));
413 if (isActive() && !isSearchUrl(url
)) {
414 // When an URL has been entered, the view should get the focus.
415 // The focus must be requested asynchronously, as changing the URL might create
416 // a new view widget.
417 QTimer::singleShot(0, this, SLOT(requestFocus()));
419 } else if (KProtocolManager::isSourceProtocol(url
)) {
420 QString app
= "konqueror";
421 if (url
.protocol().startsWith(QLatin1String("http"))) {
422 showErrorMessage(i18nc("@info:status",
423 "Dolphin does not support web pages, the web browser has been launched"));
424 const KConfigGroup
config(KSharedConfig::openConfig("kdeglobals"), "General");
425 const QString browser
= config
.readEntry("BrowserApplication");
426 if (!browser
.isEmpty()) {
428 if (app
.startsWith('!')) {
429 // a literal command has been configured, remove the '!' prefix
434 showErrorMessage(i18nc("@info:status",
435 "Protocol not supported by Dolphin, Konqueror has been launched"));
438 const QString secureUrl
= KShell::quoteArg(url
.pathOrUrl());
439 const QString command
= app
+ ' ' + secureUrl
;
440 KRun::runCommand(command
, app
, app
, this);
442 showErrorMessage(i18nc("@info:status", "Invalid protocol"));
446 void DolphinViewContainer::dropUrls(const KUrl
& destination
, QDropEvent
* event
)
448 DragAndDropHelper::instance().dropUrls(KFileItem(), destination
, event
, this);
451 void DolphinViewContainer::redirect(const KUrl
& oldUrl
, const KUrl
& newUrl
)
454 const bool block
= m_urlNavigator
->signalsBlocked();
455 m_urlNavigator
->blockSignals(true);
457 // Assure that the location state is reset for redirection URLs. This
458 // allows to skip redirection URLs when going back or forward in the
460 m_urlNavigator
->saveLocationState(QByteArray());
461 m_urlNavigator
->setLocationUrl(newUrl
);
462 setSearchModeEnabled(isSearchUrl(newUrl
));
464 m_urlNavigator
->blockSignals(block
);
467 void DolphinViewContainer::requestFocus()
472 void DolphinViewContainer::saveUrlCompletionMode(KGlobalSettings::Completion completion
)
474 DolphinSettings
& settings
= DolphinSettings::instance();
475 settings
.generalSettings()->setUrlCompletionMode(completion
);
479 void DolphinViewContainer::slotHistoryChanged()
481 QByteArray locationState
= m_urlNavigator
->locationState();
483 if (!locationState
.isEmpty()) {
484 QDataStream
stream(&locationState
, QIODevice::ReadOnly
);
485 m_view
->restoreState(stream
);
489 void DolphinViewContainer::startSearching(const QString
&text
)
492 const KUrl url
= m_searchBox
->urlForSearching();
493 if (url
.isValid() && !url
.isEmpty()) {
494 m_urlNavigator
->setLocationUrl(url
);
498 void DolphinViewContainer::closeSearchBox()
500 setSearchModeEnabled(false);
503 void DolphinViewContainer::stopLoading()
505 m_view
->stopLoading();
508 bool DolphinViewContainer::isSearchUrl(const KUrl
& url
) const
510 const QString protocol
= url
.protocol();
511 return protocol
.contains("search") || (protocol
== QLatin1String("nepomuk"));
514 void DolphinViewContainer::slotItemTriggered(const KFileItem
& item
)
516 KUrl url
= item
.targetUrl();
523 const GeneralSettings
* settings
= DolphinSettings::instance().generalSettings();
524 const bool browseThroughArchives
= settings
->browseThroughArchives();
525 if (browseThroughArchives
&& item
.isFile() && url
.isLocalFile()) {
526 // Generic mechanism for redirecting to tar:/<path>/ when clicking on a tar file,
527 // zip:/<path>/ when clicking on a zip file, etc.
528 // The .protocol file specifies the mimetype that the kioslave handles.
529 // Note that we don't use mimetype inheritance since we don't want to
530 // open OpenDocument files as zip folders...
531 const QString protocol
= KProtocolManager::protocolForArchiveMimetype(item
.mimetype());
532 if (!protocol
.isEmpty()) {
533 url
.setProtocol(protocol
);
539 if (item
.mimetype() == "application/x-desktop") {
540 // redirect to the url in Type=Link desktop files
541 KDesktopFile
desktopFile(url
.toLocalFile());
542 if (desktopFile
.hasLinkType()) {
543 url
= desktopFile
.readUrl();
552 void DolphinViewContainer::openFile(const KUrl
& url
)
554 const KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, url
);
555 slotItemTriggered(item
);
558 #include "dolphinviewcontainer.moc"