]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinviewcontainer.cpp
67ea84d1b42a5e4f8e93ba05efd52d39399be5b9
[dolphin.git] / src / dolphinviewcontainer.cpp
1 /***************************************************************************
2 * Copyright (C) 2007 by Peter Penz <peter.penz19@gmail.com> *
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 <KNewFileMenu>
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_view->url();
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 const DolphinStatusBar* DolphinViewContainer::statusBar() const
181 {
182 return m_statusBar;
183 }
184
185 DolphinStatusBar* DolphinViewContainer::statusBar()
186 {
187 return m_statusBar;
188 }
189
190 const KUrlNavigator* DolphinViewContainer::urlNavigator() const
191 {
192 return m_urlNavigator;
193 }
194
195 KUrlNavigator* DolphinViewContainer::urlNavigator()
196 {
197 return m_urlNavigator;
198 }
199
200 const DolphinView* DolphinViewContainer::view() const
201 {
202 return m_view;
203 }
204
205 DolphinView* DolphinViewContainer::view()
206 {
207 return m_view;
208 }
209
210 const DolphinSearchBox* DolphinViewContainer::searchBox() const
211 {
212 return m_searchBox;
213 }
214
215 DolphinSearchBox* DolphinViewContainer::searchBox()
216 {
217 return m_searchBox;
218 }
219
220 void DolphinViewContainer::refresh()
221 {
222 GeneralSettings* settings = DolphinSettings::instance().generalSettings();
223 if (settings->modifiedStartupSettings()) {
224 // The startup settings should only get applied if they have been
225 // modified by the user. Otherwise keep the (possibly) different current
226 // settings of the URL navigator and the filterbar.
227 m_urlNavigator->setUrlEditable(settings->editableUrl());
228 m_urlNavigator->setShowFullPath(settings->showFullPath());
229 setFilterBarVisible(settings->filterBar());
230 }
231
232 m_view->refresh();
233 m_statusBar->refresh();
234 }
235
236 bool DolphinViewContainer::isFilterBarVisible() const
237 {
238 return m_filterBar->isVisible();
239 }
240
241 void DolphinViewContainer::setSearchModeEnabled(bool enabled)
242 {
243 if (enabled == isSearchModeEnabled()) {
244 if (enabled && !m_searchBox->hasFocus()) {
245 m_searchBox->setFocus();
246 m_searchBox->selectAll();
247 }
248 return;
249 }
250
251 m_searchBox->setVisible(enabled);
252 m_urlNavigator->setVisible(!enabled);
253
254 if (enabled) {
255 KUrl url = m_urlNavigator->locationUrl();
256 m_searchBox->setText(QString());
257 m_searchBox->setReadOnly(isSearchUrl(url), url);
258
259 // Remember the most recent non-search URL as search path
260 // of the search-box, so that it can be restored
261 // when switching back to the URL navigator.
262 int index = m_urlNavigator->historyIndex();
263 const int historySize = m_urlNavigator->historySize();
264 while (isSearchUrl(url) && (index < historySize)) {
265 ++index;
266 url = m_urlNavigator->locationUrl(index);
267 }
268
269 if (!isSearchUrl(url)) {
270 m_searchBox->setSearchPath(url);
271 }
272 } else {
273 // Restore the URL for the URL navigator. If Dolphin has been
274 // started with a search-URL, the home URL is used as fallback.
275 const KUrl url = m_searchBox->searchPath();
276 if (url.isValid() && !url.isEmpty()) {
277 if (isSearchUrl(url)) {
278 m_urlNavigator->goHome();
279 } else {
280 m_urlNavigator->setLocationUrl(url);
281 }
282 }
283 }
284
285 emit searchModeChanged(enabled);
286 }
287
288 bool DolphinViewContainer::isSearchModeEnabled() const
289 {
290 return m_searchBox->isVisible();
291 }
292
293 void DolphinViewContainer::setUrl(const KUrl& newUrl)
294 {
295 if (newUrl != m_urlNavigator->locationUrl()) {
296 m_urlNavigator->setLocationUrl(newUrl);
297 }
298 }
299
300 void DolphinViewContainer::setFilterBarVisible(bool visible)
301 {
302 Q_ASSERT(m_filterBar);
303 if (visible) {
304 m_filterBar->show();
305 m_filterBar->setFocus();
306 m_filterBar->selectAll();
307 } else {
308 closeFilterBar();
309 }
310 }
311
312 void DolphinViewContainer::delayedStatusBarUpdate()
313 {
314 if (m_statusBarTimer->isActive() && (m_statusBarTimestamp.elapsed() > 2000)) {
315 // No update of the statusbar has been done during the last 2 seconds,
316 // although an update has been requested. Trigger an immediate update.
317 m_statusBarTimer->stop();
318 updateStatusBar();
319 } else {
320 // Invoke updateStatusBar() with a small delay. This assures that
321 // when a lot of delayedStatusBarUpdates() are done in a short time,
322 // no bottleneck is given.
323 m_statusBarTimer->start();
324 }
325 }
326
327 void DolphinViewContainer::updateStatusBar()
328 {
329 m_statusBarTimestamp.start();
330
331 const QString newMessage = m_view->statusBarText();
332 m_statusBar->setDefaultText(newMessage);
333
334 // We don't want to override errors. Other messages are only protected by
335 // the Statusbar itself depending on timings (see DolphinStatusBar::setMessage).
336 if (m_statusBar->type() != DolphinStatusBar::Error) {
337 m_statusBar->setMessage(newMessage, DolphinStatusBar::Default);
338 }
339 }
340
341 void DolphinViewContainer::updateProgress(int percent)
342 {
343 if (m_statusBar->progressText().isEmpty()) {
344 m_statusBar->setProgressText(i18nc("@info:progress", "Loading folder..."));
345 }
346 m_statusBar->setProgress(percent);
347 }
348
349 void DolphinViewContainer::slotStartedPathLoading()
350 {
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:
354 updateStatusBar();
355 m_statusBar->setProgressText(i18nc("@info", "Searching..."));
356 m_statusBar->setProgress(-1);
357 } else {
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.
361 updateProgress(-1);
362 }
363 }
364
365 void DolphinViewContainer::slotFinishedPathLoading()
366 {
367 if (!m_statusBar->progressText().isEmpty()) {
368 m_statusBar->setProgressText(QString());
369 m_statusBar->setProgress(100);
370 }
371
372 if (isSearchUrl(url()) && m_view->items().isEmpty()) {
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);
376 } else {
377 updateStatusBar();
378 }
379 }
380
381 void DolphinViewContainer::showItemInfo(const KFileItem& item)
382 {
383 if (item.isNull()) {
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
387 // context menu.
388 if (m_statusBar->type() == DolphinStatusBar::Default) {
389 m_statusBar->clear();
390 }
391 } else {
392 m_statusBar->setMessage(item.getStatusBarInfo(), DolphinStatusBar::Default);
393 }
394 }
395
396 void DolphinViewContainer::showInfoMessage(const QString& msg)
397 {
398 m_statusBar->setMessage(msg, DolphinStatusBar::Information);
399 }
400
401 void DolphinViewContainer::showErrorMessage(const QString& msg)
402 {
403 m_statusBar->setMessage(msg, DolphinStatusBar::Error);
404 }
405
406 void DolphinViewContainer::showOperationCompletedMessage(const QString& msg)
407 {
408 m_statusBar->setMessage(msg, DolphinStatusBar::OperationCompleted);
409 }
410
411 void DolphinViewContainer::closeFilterBar()
412 {
413 m_filterBar->hide();
414 m_filterBar->clear();
415 m_view->setFocus();
416 emit showFilterBarChanged(false);
417 }
418
419 void DolphinViewContainer::setNameFilter(const QString& nameFilter)
420 {
421 m_view->setNameFilter(nameFilter);
422 delayedStatusBarUpdate();
423 }
424
425 void DolphinViewContainer::activate()
426 {
427 setActive(true);
428 }
429
430 void DolphinViewContainer::saveViewState()
431 {
432 QByteArray locationState;
433 QDataStream stream(&locationState, QIODevice::WriteOnly);
434 m_view->saveState(stream);
435 m_urlNavigator->saveLocationState(locationState);
436 }
437
438 void DolphinViewContainer::slotUrlNavigatorLocationChanged(const KUrl& url)
439 {
440 if (KProtocolManager::supportsListing(url)) {
441 setSearchModeEnabled(isSearchUrl(url));
442
443 m_view->setUrl(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()));
449 }
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()) {
458 app = browser;
459 if (app.startsWith('!')) {
460 // a literal command has been configured, remove the '!' prefix
461 app = app.mid(1);
462 }
463 }
464 } else {
465 showErrorMessage(i18nc("@info:status",
466 "Protocol not supported by Dolphin, Konqueror has been launched"));
467 }
468
469 const QString secureUrl = KShell::quoteArg(url.pathOrUrl());
470 const QString command = app + ' ' + secureUrl;
471 KRun::runCommand(command, app, app, this);
472 } else {
473 showErrorMessage(i18nc("@info:status", "Invalid protocol"));
474 }
475 }
476
477 void DolphinViewContainer::dropUrls(const KUrl& destination, QDropEvent* event)
478 {
479 DragAndDropHelper::instance().dropUrls(KFileItem(), destination, event, this);
480 }
481
482 void DolphinViewContainer::redirect(const KUrl& oldUrl, const KUrl& newUrl)
483 {
484 Q_UNUSED(oldUrl);
485 const bool block = m_urlNavigator->signalsBlocked();
486 m_urlNavigator->blockSignals(true);
487
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
490 // URL history.
491 m_urlNavigator->saveLocationState(QByteArray());
492 m_urlNavigator->setLocationUrl(newUrl);
493 setSearchModeEnabled(isSearchUrl(newUrl));
494
495 m_urlNavigator->blockSignals(block);
496 }
497
498 void DolphinViewContainer::requestFocus()
499 {
500 m_view->setFocus();
501 }
502
503 void DolphinViewContainer::saveUrlCompletionMode(KGlobalSettings::Completion completion)
504 {
505 DolphinSettings& settings = DolphinSettings::instance();
506 settings.generalSettings()->setUrlCompletionMode(completion);
507 settings.save();
508 }
509
510 void DolphinViewContainer::slotHistoryChanged()
511 {
512 QByteArray locationState = m_urlNavigator->locationState();
513
514 if (!locationState.isEmpty()) {
515 QDataStream stream(&locationState, QIODevice::ReadOnly);
516 m_view->restoreState(stream);
517 }
518 }
519
520 void DolphinViewContainer::startSearching(const QString &text)
521 {
522 Q_UNUSED(text);
523 const KUrl url = m_searchBox->urlForSearching();
524 if (url.isValid() && !url.isEmpty()) {
525 m_urlNavigator->setLocationUrl(url);
526 }
527 }
528
529 void DolphinViewContainer::closeSearchBox()
530 {
531 setSearchModeEnabled(false);
532 }
533
534 void DolphinViewContainer::stopLoading()
535 {
536 m_view->stopLoading();
537 m_statusBar->setProgress(100);
538 }
539
540 bool DolphinViewContainer::isSearchUrl(const KUrl& url) const
541 {
542 const QString protocol = url.protocol();
543 return protocol.contains("search") || (protocol == QLatin1String("nepomuk"));
544 }
545
546 void DolphinViewContainer::slotItemTriggered(const KFileItem& item)
547 {
548 KUrl url = item.targetUrl();
549
550 if (item.isDir()) {
551 m_view->setUrl(url);
552 return;
553 }
554
555 const GeneralSettings* settings = DolphinSettings::instance().generalSettings();
556 const bool browseThroughArchives = settings->browseThroughArchives();
557 if (browseThroughArchives && item.isFile() && url.isLocalFile()) {
558 // Generic mechanism for redirecting to tar:/<path>/ when clicking on a tar file,
559 // zip:/<path>/ when clicking on a zip file, etc.
560 // The .protocol file specifies the mimetype that the kioslave handles.
561 // Note that we don't use mimetype inheritance since we don't want to
562 // open OpenDocument files as zip folders...
563 const QString protocol = KProtocolManager::protocolForArchiveMimetype(item.mimetype());
564 if (!protocol.isEmpty()) {
565 url.setProtocol(protocol);
566 m_view->setUrl(url);
567 return;
568 }
569 }
570
571 if (item.mimetype() == "application/x-desktop") {
572 // redirect to the url in Type=Link desktop files
573 KDesktopFile desktopFile(url.toLocalFile());
574 if (desktopFile.hasLinkType()) {
575 url = desktopFile.readUrl();
576 m_view->setUrl(url);
577 return;
578 }
579 }
580
581 item.run();
582 }
583
584 void DolphinViewContainer::openFile(const KUrl& url)
585 {
586 const KFileItem item(KFileItem::Unknown, KFileItem::Unknown, url);
587 slotItemTriggered(item);
588 }
589
590 #include "dolphinviewcontainer.moc"