]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinviewcontainer.cpp
77a2f3f36e89c9e5fbd601eb8535f5594a14bd89
[dolphin.git] / src / dolphinviewcontainer.cpp
1 /***************************************************************************
2 * Copyright (C) 2007 by Peter Penz <peter.penz@gmx.at> *
3 * *
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. *
8 * *
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. *
13 * *
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 ***************************************************************************/
19
20 #include "dolphinviewcontainer.h"
21 #include <KProtocolManager>
22
23 #include <QApplication>
24 #include <QKeyEvent>
25 #include <QItemSelection>
26 #include <QBoxLayout>
27 #include <QTimer>
28 #include <QScrollBar>
29
30 #include <KDesktopFile>
31 #include <KFileItemDelegate>
32 #include <KFilePlacesModel>
33 #include <KGlobalSettings>
34 #include <KLocale>
35 #include <KIconEffect>
36 #include <KIO/NetAccess>
37 #include <KIO/PreviewJob>
38 #include <KMenu>
39 #include <knewmenu.h>
40 #include <konqmimedata.h>
41 #include <konq_operations.h>
42 #include <KShell>
43 #include <KUrl>
44 #include <KUrlComboBox>
45 #include <KUrlNavigator>
46 #include <KRun>
47
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"
62
63 DolphinViewContainer::DolphinViewContainer(const KUrl& url, QWidget* parent) :
64 QWidget(parent),
65 m_topLayout(0),
66 m_urlNavigator(0),
67 m_searchBox(0),
68 m_view(0),
69 m_filterBar(0),
70 m_statusBar(0),
71 m_statusBarTimer(0),
72 m_statusBarTimestamp()
73 {
74 hide();
75
76 m_topLayout = new QVBoxLayout(this);
77 m_topLayout->setSpacing(0);
78 m_topLayout->setMargin(0);
79
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)));
87
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()));
94
95 m_searchBox = new DolphinSearchBox(this);
96 m_searchBox->hide();
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()));
100
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&)));
118
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()));
125
126 // initialize status bar
127 m_statusBar = new DolphinStatusBar(this, m_view);
128 connect(m_statusBar, SIGNAL(stopPressed()), this, SLOT(stopLoading()));
129
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()));
135
136 KIO::FileUndoManager* undoManager = KIO::FileUndoManager::self();
137 connect(undoManager, SIGNAL(jobRecordingFinished(CommandType)),
138 this, SLOT(delayedStatusBarUpdate()));
139
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()));
149
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);
155
156 setSearchModeEnabled(isSearchUrl(url));
157 }
158
159 DolphinViewContainer::~DolphinViewContainer()
160 {
161 }
162
163 KUrl DolphinViewContainer::url() const
164 {
165 return m_urlNavigator->locationUrl();
166 }
167
168 void DolphinViewContainer::setActive(bool active)
169 {
170 m_urlNavigator->setActive(active);
171 m_view->setActive(active);
172 }
173
174 bool DolphinViewContainer::isActive() const
175 {
176 Q_ASSERT(m_view->isActive() == m_urlNavigator->isActive());
177 return m_view->isActive();
178 }
179
180 void DolphinViewContainer::refresh()
181 {
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());
190 }
191
192 m_view->refresh();
193 m_statusBar->refresh();
194 }
195
196 bool DolphinViewContainer::isFilterBarVisible() const
197 {
198 return m_filterBar->isVisible();
199 }
200
201 void DolphinViewContainer::setSearchModeEnabled(bool enabled)
202 {
203 if (enabled == isSearchModeEnabled()) {
204 if (enabled && !m_searchBox->hasFocus()) {
205 m_searchBox->setFocus();
206 m_searchBox->selectAll();
207 }
208 return;
209 }
210
211 m_searchBox->setVisible(enabled);
212 m_urlNavigator->setVisible(!enabled);
213
214 if (enabled) {
215 // Remember the most recent non-search URL as search path
216 // of the search-box, so that it can be restored
217 // when switching back to the URL navigator.
218 KUrl url = m_urlNavigator->locationUrl();
219
220 int index = m_urlNavigator->historyIndex();
221 const int historySize = m_urlNavigator->historySize();
222 while (isSearchUrl(url) && (index < historySize)) {
223 ++index;
224 url = m_urlNavigator->locationUrl(index);
225 }
226
227 if (!isSearchUrl(url)) {
228 m_searchBox->setSearchPath(url);
229 }
230 } else {
231 // Restore the URL for the URL navigator. If Dolphin has been
232 // started with a search-URL, the home URL is used as fallback.
233 const KUrl url = m_searchBox->searchPath();
234 if (url.isValid() && !url.isEmpty()) {
235 if (isSearchUrl(url)) {
236 m_urlNavigator->goHome();
237 } else {
238 m_urlNavigator->setLocationUrl(url);
239 }
240 }
241 }
242
243 emit searchModeChanged(enabled);
244 }
245
246 bool DolphinViewContainer::isSearchModeEnabled() const
247 {
248 return m_searchBox->isVisible();
249 }
250
251 void DolphinViewContainer::setUrl(const KUrl& newUrl)
252 {
253 if (newUrl != m_urlNavigator->locationUrl()) {
254 m_urlNavigator->setLocationUrl(newUrl);
255 }
256 }
257
258 void DolphinViewContainer::setFilterBarVisible(bool visible)
259 {
260 Q_ASSERT(m_filterBar != 0);
261 if (visible) {
262 m_filterBar->show();
263 m_filterBar->setFocus();
264 m_filterBar->selectAll();
265 } else {
266 closeFilterBar();
267 }
268 }
269
270 void DolphinViewContainer::delayedStatusBarUpdate()
271 {
272 if (m_statusBarTimer->isActive() && (m_statusBarTimestamp.elapsed() > 2000)) {
273 // No update of the statusbar has been done during the last 2 seconds,
274 // although an update has been requested. Trigger an immediate update.
275 m_statusBarTimer->stop();
276 updateStatusBar();
277 } else {
278 // Invoke updateStatusBar() with a small delay. This assures that
279 // when a lot of delayedStatusBarUpdates() are done in a short time,
280 // no bottleneck is given.
281 m_statusBarTimer->start();
282 }
283 }
284
285 void DolphinViewContainer::updateStatusBar()
286 {
287 m_statusBarTimestamp.start();
288
289 // As the item count information is less important
290 // in comparison with other messages, it should only
291 // be shown if:
292 // - the status bar is empty or
293 // - shows already the item count information or
294 // - shows only a not very important information
295 const QString newMessage = m_view->statusBarText();
296 const QString currentMessage = m_statusBar->message();
297 const bool updateStatusBarMsg = currentMessage.isEmpty()
298 || (currentMessage == m_statusBar->defaultText())
299 || (m_statusBar->type() == DolphinStatusBar::Information);
300
301 m_statusBar->setDefaultText(newMessage);
302
303 if (updateStatusBarMsg) {
304 m_statusBar->setMessage(newMessage, DolphinStatusBar::Default);
305 }
306 }
307
308 void DolphinViewContainer::updateProgress(int percent)
309 {
310 if (m_statusBar->progressText().isEmpty()) {
311 m_statusBar->setProgressText(i18nc("@info:progress", "Loading folder..."));
312 }
313 m_statusBar->setProgress(percent);
314 }
315
316 void DolphinViewContainer::slotStartedPathLoading()
317 {
318 if (isSearchUrl(url())) {
319 // Search KIO-slaves usually don't provide any progress information. Give
320 // a hint to the user that a searching is done:
321 updateStatusBar();
322 m_statusBar->setProgressText(i18nc("@info", "Searching..."));
323 m_statusBar->setProgress(-1);
324 } else {
325 // Trigger an undetermined progress indication. The progress
326 // information in percent will be triggered by the percent() signal
327 // of the directory lister later.
328 updateProgress(-1);
329 }
330 }
331
332 void DolphinViewContainer::slotFinishedPathLoading()
333 {
334 if (!m_statusBar->progressText().isEmpty()) {
335 m_statusBar->setProgressText(QString());
336 m_statusBar->setProgress(100);
337 }
338
339 if (isSearchUrl(url()) && (m_view->items().count() == 0)) {
340 // The dir lister has been completed on a Nepomuk-URI and no items have been found. Instead
341 // of showing the default status bar information ("0 items") a more helpful information is given:
342 m_statusBar->setMessage(i18nc("@info:status", "No items found."), DolphinStatusBar::Information);
343 } else {
344 updateStatusBar();
345 }
346 }
347
348 void DolphinViewContainer::showItemInfo(const KFileItem& item)
349 {
350 if (item.isNull()) {
351 // Only clear the status bar if unimportant messages are shown.
352 // This prevents that information- or error-messages get hidden
353 // by moving the mouse above the viewport or when closing the
354 // context menu.
355 if (m_statusBar->type() == DolphinStatusBar::Default) {
356 m_statusBar->clear();
357 }
358 } else {
359 m_statusBar->setMessage(item.getStatusBarInfo(), DolphinStatusBar::Default);
360 }
361 }
362
363 void DolphinViewContainer::showInfoMessage(const QString& msg)
364 {
365 m_statusBar->setMessage(msg, DolphinStatusBar::Information);
366 }
367
368 void DolphinViewContainer::showErrorMessage(const QString& msg)
369 {
370 m_statusBar->setMessage(msg, DolphinStatusBar::Error);
371 }
372
373 void DolphinViewContainer::showOperationCompletedMessage(const QString& msg)
374 {
375 m_statusBar->setMessage(msg, DolphinStatusBar::OperationCompleted);
376 }
377
378 void DolphinViewContainer::closeFilterBar()
379 {
380 m_filterBar->hide();
381 m_filterBar->clear();
382 m_view->setFocus();
383 emit showFilterBarChanged(false);
384 }
385
386 void DolphinViewContainer::setNameFilter(const QString& nameFilter)
387 {
388 m_view->setNameFilter(nameFilter);
389 delayedStatusBarUpdate();
390 }
391
392 void DolphinViewContainer::activate()
393 {
394 setActive(true);
395 }
396
397 void DolphinViewContainer::saveViewState()
398 {
399 QByteArray locationState;
400 QDataStream stream(&locationState, QIODevice::WriteOnly);
401 m_view->saveState(stream);
402 m_urlNavigator->saveLocationState(locationState);
403 }
404
405 void DolphinViewContainer::slotUrlNavigatorLocationChanged(const KUrl& url)
406 {
407 if (KProtocolManager::supportsListing(url)) {
408 setSearchModeEnabled(isSearchUrl(url));
409
410 m_view->setUrl(url);
411 if (isActive() && !isSearchUrl(url)) {
412 // When an URL has been entered, the view should get the focus.
413 // The focus must be requested asynchronously, as changing the URL might create
414 // a new view widget.
415 QTimer::singleShot(0, this, SLOT(requestFocus()));
416 }
417 } else if (KProtocolManager::isSourceProtocol(url)) {
418 QString app = "konqueror";
419 if (url.protocol().startsWith(QLatin1String("http"))) {
420 showErrorMessage(i18nc("@info:status",
421 "Dolphin does not support web pages, the web browser has been launched"));
422 const KConfigGroup config(KSharedConfig::openConfig("kdeglobals"), "General");
423 const QString browser = config.readEntry("BrowserApplication");
424 if (!browser.isEmpty()) {
425 app = browser;
426 if (app.startsWith('!')) {
427 // a literal command has been configured, remove the '!' prefix
428 app = app.mid(1);
429 }
430 }
431 } else {
432 showErrorMessage(i18nc("@info:status",
433 "Protocol not supported by Dolphin, Konqueror has been launched"));
434 }
435
436 const QString secureUrl = KShell::quoteArg(url.pathOrUrl());
437 const QString command = app + ' ' + secureUrl;
438 KRun::runCommand(command, app, app, this);
439 } else {
440 showErrorMessage(i18nc("@info:status", "Invalid protocol"));
441 }
442 }
443
444 void DolphinViewContainer::dropUrls(const KUrl& destination, QDropEvent* event)
445 {
446 DragAndDropHelper::instance().dropUrls(KFileItem(), destination, event, this);
447 }
448
449 void DolphinViewContainer::redirect(const KUrl& oldUrl, const KUrl& newUrl)
450 {
451 Q_UNUSED(oldUrl);
452 const bool block = m_urlNavigator->signalsBlocked();
453 m_urlNavigator->blockSignals(true);
454
455 // Assure that the location state is reset for redirection URLs. This
456 // allows to skip redirection URLs when going back or forward in the
457 // URL history.
458 m_urlNavigator->saveLocationState(QByteArray());
459 m_urlNavigator->setLocationUrl(newUrl);
460 setSearchModeEnabled(isSearchUrl(newUrl));
461
462 m_urlNavigator->blockSignals(block);
463 }
464
465 void DolphinViewContainer::requestFocus()
466 {
467 m_view->setFocus();
468 }
469
470 void DolphinViewContainer::saveUrlCompletionMode(KGlobalSettings::Completion completion)
471 {
472 DolphinSettings& settings = DolphinSettings::instance();
473 settings.generalSettings()->setUrlCompletionMode(completion);
474 settings.save();
475 }
476
477 void DolphinViewContainer::slotHistoryChanged()
478 {
479 QByteArray locationState = m_urlNavigator->locationState();
480
481 if (!locationState.isEmpty()) {
482 QDataStream stream(&locationState, QIODevice::ReadOnly);
483 m_view->restoreState(stream);
484 }
485 }
486
487 void DolphinViewContainer::startSearching(const QString &text)
488 {
489 Q_UNUSED(text);
490 const KUrl url = m_searchBox->urlForSearching();
491 if (url.isValid() && !url.isEmpty()) {
492 m_urlNavigator->setLocationUrl(url);
493 }
494 }
495
496 void DolphinViewContainer::closeSearchBox()
497 {
498 setSearchModeEnabled(false);
499 }
500
501 void DolphinViewContainer::stopLoading()
502 {
503 m_view->stopLoading();
504 }
505
506 bool DolphinViewContainer::isSearchUrl(const KUrl& url) const
507 {
508 const QString protocol = url.protocol();
509 return protocol.contains("search") || (protocol == QLatin1String("nepomuk"));
510 }
511
512 void DolphinViewContainer::slotItemTriggered(const KFileItem& item)
513 {
514 KUrl url = item.targetUrl();
515
516 if (item.isDir()) {
517 m_view->setUrl(url);
518 return;
519 }
520
521 const GeneralSettings* settings = DolphinSettings::instance().generalSettings();
522 const bool browseThroughArchives = settings->browseThroughArchives();
523 if (browseThroughArchives && item.isFile() && url.isLocalFile()) {
524 // Generic mechanism for redirecting to tar:/<path>/ when clicking on a tar file,
525 // zip:/<path>/ when clicking on a zip file, etc.
526 // The .protocol file specifies the mimetype that the kioslave handles.
527 // Note that we don't use mimetype inheritance since we don't want to
528 // open OpenDocument files as zip folders...
529 const QString protocol = KProtocolManager::protocolForArchiveMimetype(item.mimetype());
530 if (!protocol.isEmpty()) {
531 url.setProtocol(protocol);
532 m_view->setUrl(url);
533 return;
534 }
535 }
536
537 if (item.mimetype() == "application/x-desktop") {
538 // redirect to the url in Type=Link desktop files
539 KDesktopFile desktopFile(url.toLocalFile());
540 if (desktopFile.hasLinkType()) {
541 url = desktopFile.readUrl();
542 m_view->setUrl(url);
543 return;
544 }
545 }
546
547 item.run();
548 }
549
550 void DolphinViewContainer::openFile(const KUrl& url)
551 {
552 const KFileItem item(KFileItem::Unknown, KFileItem::Unknown, url);
553 slotItemTriggered(item);
554 }
555
556 #include "dolphinviewcontainer.moc"