]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinviewcontainer.cpp
SVN_SILENT made messages (.desktop file)
[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 <KLocale>
34 #include <KIconEffect>
35 #include <KIO/NetAccess>
36 #include <KIO/PreviewJob>
37 #include <KNewFileMenu>
38 #include <konqmimedata.h>
39 #include <konq_operations.h>
40 #include <KShell>
41 #include <KUrl>
42 #include <KUrlComboBox>
43 #include <KUrlNavigator>
44 #include <KRun>
45
46 #include "dolphin_generalsettings.h"
47 #include "dolphinmainwindow.h"
48 #include "filterbar/filterbar.h"
49 #include "search/dolphinsearchbox.h"
50 #include "statusbar/dolphinstatusbar.h"
51 #include "views/dolphinplacesmodel.h"
52 #include "views/viewmodecontroller.h"
53 #include "views/viewproperties.h"
54
55 DolphinViewContainer::DolphinViewContainer(const KUrl& url, QWidget* parent) :
56 QWidget(parent),
57 m_topLayout(0),
58 m_urlNavigator(0),
59 m_searchBox(0),
60 m_view(0),
61 m_filterBar(0),
62 m_statusBar(0),
63 m_statusBarTimer(0),
64 m_statusBarTimestamp()
65 {
66 hide();
67
68 m_topLayout = new QVBoxLayout(this);
69 m_topLayout->setSpacing(0);
70 m_topLayout->setMargin(0);
71
72 m_urlNavigator = new KUrlNavigator(DolphinPlacesModel::instance(), url, this);
73 connect(m_urlNavigator, SIGNAL(urlsDropped(KUrl,QDropEvent*)),
74 this, SLOT(dropUrls(KUrl,QDropEvent*)));
75 connect(m_urlNavigator, SIGNAL(activated()),
76 this, SLOT(activate()));
77 connect(m_urlNavigator->editor(), SIGNAL(completionModeChanged(KGlobalSettings::Completion)),
78 this, SLOT(saveUrlCompletionMode(KGlobalSettings::Completion)));
79
80 const GeneralSettings* settings = GeneralSettings::self();
81 m_urlNavigator->setUrlEditable(settings->editableUrl());
82 m_urlNavigator->setShowFullPath(settings->showFullPath());
83 m_urlNavigator->setHomeUrl(KUrl(settings->homeUrl()));
84 KUrlComboBox* editor = m_urlNavigator->editor();
85 editor->setCompletionMode(KGlobalSettings::Completion(settings->urlCompletionMode()));
86
87 m_searchBox = new DolphinSearchBox(this);
88 m_searchBox->hide();
89 connect(m_searchBox, SIGNAL(closeRequest()), this, SLOT(closeSearchBox()));
90 connect(m_searchBox, SIGNAL(search(QString)), this, SLOT(startSearching(QString)));
91 connect(m_searchBox, SIGNAL(returnPressed(QString)), this, SLOT(requestFocus()));
92
93 m_view = new DolphinView(url, this);
94 connect(m_view, SIGNAL(urlChanged(KUrl)), m_urlNavigator, SLOT(setUrl(KUrl)));
95 connect(m_view, SIGNAL(writeStateChanged(bool)), this, SIGNAL(writeStateChanged(bool)));
96 connect(m_view, SIGNAL(requestItemInfo(KFileItem)), this, SLOT(showItemInfo(KFileItem)));
97 connect(m_view, SIGNAL(errorMessage(QString)), this, SLOT(showErrorMessage(QString)));
98 connect(m_view, SIGNAL(infoMessage(QString)), this, SLOT(showInfoMessage(QString)));
99 connect(m_view, SIGNAL(itemActivated(KFileItem)), this, SLOT(slotItemActivated(KFileItem)));
100 connect(m_view, SIGNAL(redirection(KUrl,KUrl)), this, SLOT(redirect(KUrl,KUrl)));
101 connect(m_view, SIGNAL(startedPathLoading(KUrl)), this, SLOT(slotStartedPathLoading()));
102 connect(m_view, SIGNAL(finishedPathLoading(KUrl)), this, SLOT(slotFinishedPathLoading()));
103 connect(m_view, SIGNAL(itemCountChanged()), this, SLOT(delayedStatusBarUpdate()));
104 connect(m_view, SIGNAL(pathLoadingProgress(int)), this, SLOT(updateProgress(int)));
105 connect(m_view, SIGNAL(infoMessage(QString)), this, SLOT(showInfoMessage(QString)));
106 connect(m_view, SIGNAL(errorMessage(QString)), this, SLOT(showErrorMessage(QString)));
107 connect(m_view, SIGNAL(urlIsFileError(KUrl)), this, SLOT(openFile(KUrl)));
108 connect(m_view, SIGNAL(selectionChanged(KFileItemList)), this, SLOT(delayedStatusBarUpdate()));
109 connect(m_view, SIGNAL(operationCompletedMessage(QString)), this, SLOT(showOperationCompletedMessage(QString)));
110 connect(m_view, SIGNAL(urlAboutToBeChanged(KUrl)), this, SLOT(slotViewUrlAboutToBeChanged(KUrl)));
111
112 connect(m_urlNavigator, SIGNAL(urlAboutToBeChanged(KUrl)),
113 this, SLOT(slotUrlNavigatorLocationAboutToBeChanged(KUrl)));
114 connect(m_urlNavigator, SIGNAL(urlChanged(KUrl)),
115 this, SLOT(slotUrlNavigatorLocationChanged(KUrl)));
116 connect(m_urlNavigator, SIGNAL(historyChanged()),
117 this, SLOT(slotHistoryChanged()));
118
119 // initialize status bar
120 m_statusBar = new DolphinStatusBar(this, m_view);
121 connect(m_statusBar, SIGNAL(stopPressed()), this, SLOT(stopLoading()));
122
123 m_statusBarTimer = new QTimer(this);
124 m_statusBarTimer->setSingleShot(true);
125 m_statusBarTimer->setInterval(300);
126 connect(m_statusBarTimer, SIGNAL(timeout()),
127 this, SLOT(updateStatusBar()));
128
129 KIO::FileUndoManager* undoManager = KIO::FileUndoManager::self();
130 connect(undoManager, SIGNAL(jobRecordingFinished(CommandType)),
131 this, SLOT(delayedStatusBarUpdate()));
132
133 // initialize filter bar
134 m_filterBar = new FilterBar(this);
135 m_filterBar->setVisible(settings->filterBar());
136 connect(m_filterBar, SIGNAL(filterChanged(QString)),
137 this, SLOT(setNameFilter(QString)));
138 connect(m_filterBar, SIGNAL(closeRequest()),
139 this, SLOT(closeFilterBar()));
140 connect(m_view, SIGNAL(urlChanged(KUrl)),
141 m_filterBar, SLOT(clear()));
142
143 m_topLayout->addWidget(m_urlNavigator);
144 m_topLayout->addWidget(m_searchBox);
145 m_topLayout->addWidget(m_view);
146 m_topLayout->addWidget(m_filterBar);
147 m_topLayout->addWidget(m_statusBar);
148
149 setSearchModeEnabled(isSearchUrl(url));
150 }
151
152 DolphinViewContainer::~DolphinViewContainer()
153 {
154 }
155
156 KUrl DolphinViewContainer::url() const
157 {
158 return m_view->url();
159 }
160
161 void DolphinViewContainer::setActive(bool active)
162 {
163 m_urlNavigator->setActive(active);
164 m_view->setActive(active);
165 }
166
167 bool DolphinViewContainer::isActive() const
168 {
169 Q_ASSERT(m_view->isActive() == m_urlNavigator->isActive());
170 return m_view->isActive();
171 }
172
173 const DolphinStatusBar* DolphinViewContainer::statusBar() const
174 {
175 return m_statusBar;
176 }
177
178 DolphinStatusBar* DolphinViewContainer::statusBar()
179 {
180 return m_statusBar;
181 }
182
183 const KUrlNavigator* DolphinViewContainer::urlNavigator() const
184 {
185 return m_urlNavigator;
186 }
187
188 KUrlNavigator* DolphinViewContainer::urlNavigator()
189 {
190 return m_urlNavigator;
191 }
192
193 const DolphinView* DolphinViewContainer::view() const
194 {
195 return m_view;
196 }
197
198 DolphinView* DolphinViewContainer::view()
199 {
200 return m_view;
201 }
202
203 const DolphinSearchBox* DolphinViewContainer::searchBox() const
204 {
205 return m_searchBox;
206 }
207
208 DolphinSearchBox* DolphinViewContainer::searchBox()
209 {
210 return m_searchBox;
211 }
212
213 void DolphinViewContainer::refresh()
214 {
215 if (GeneralSettings::modifiedStartupSettings()) {
216 // The startup settings should only get applied if they have been
217 // modified by the user. Otherwise keep the (possibly) different current
218 // settings of the URL navigator and the filterbar.
219 m_urlNavigator->setUrlEditable(GeneralSettings::editableUrl());
220 m_urlNavigator->setShowFullPath(GeneralSettings::showFullPath());
221 setFilterBarVisible(GeneralSettings::filterBar());
222 }
223
224 m_view->refresh();
225 m_statusBar->refresh();
226 }
227
228 bool DolphinViewContainer::isFilterBarVisible() const
229 {
230 return m_filterBar->isVisible();
231 }
232
233 void DolphinViewContainer::setSearchModeEnabled(bool enabled)
234 {
235 if (enabled == isSearchModeEnabled()) {
236 if (enabled && !m_searchBox->hasFocus()) {
237 m_searchBox->setFocus();
238 m_searchBox->selectAll();
239 }
240 return;
241 }
242
243 m_searchBox->setVisible(enabled);
244 m_urlNavigator->setVisible(!enabled);
245
246 if (enabled) {
247 KUrl url = m_urlNavigator->locationUrl();
248 m_searchBox->setText(QString());
249 m_searchBox->setReadOnly(isSearchUrl(url), url);
250
251 // Remember the most recent non-search URL as search path
252 // of the search-box, so that it can be restored
253 // when switching back to the URL navigator.
254 int index = m_urlNavigator->historyIndex();
255 const int historySize = m_urlNavigator->historySize();
256 while (isSearchUrl(url) && (index < historySize)) {
257 ++index;
258 url = m_urlNavigator->locationUrl(index);
259 }
260
261 if (!isSearchUrl(url)) {
262 m_searchBox->setSearchPath(url);
263 }
264 } else {
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();
271 } else {
272 m_urlNavigator->setLocationUrl(url);
273 }
274 }
275 }
276
277 emit searchModeChanged(enabled);
278 }
279
280 bool DolphinViewContainer::isSearchModeEnabled() const
281 {
282 return m_searchBox->isVisible();
283 }
284
285 void DolphinViewContainer::setUrl(const KUrl& newUrl)
286 {
287 if (newUrl != m_urlNavigator->locationUrl()) {
288 m_urlNavigator->setLocationUrl(newUrl);
289 }
290 }
291
292 void DolphinViewContainer::setFilterBarVisible(bool visible)
293 {
294 Q_ASSERT(m_filterBar);
295 if (visible) {
296 m_filterBar->show();
297 m_filterBar->setFocus();
298 m_filterBar->selectAll();
299 } else {
300 closeFilterBar();
301 }
302 }
303
304 void DolphinViewContainer::delayedStatusBarUpdate()
305 {
306 if (m_statusBarTimer->isActive() && (m_statusBarTimestamp.elapsed() > 2000)) {
307 // No update of the statusbar has been done during the last 2 seconds,
308 // although an update has been requested. Trigger an immediate update.
309 m_statusBarTimer->stop();
310 updateStatusBar();
311 } else {
312 // Invoke updateStatusBar() with a small delay. This assures that
313 // when a lot of delayedStatusBarUpdates() are done in a short time,
314 // no bottleneck is given.
315 m_statusBarTimer->start();
316 }
317 }
318
319 void DolphinViewContainer::updateStatusBar()
320 {
321 m_statusBarTimestamp.start();
322
323 const QString newMessage = m_view->statusBarText();
324 m_statusBar->setDefaultText(newMessage);
325
326 // We don't want to override errors. Other messages are only protected by
327 // the Statusbar itself depending on timings (see DolphinStatusBar::setMessage).
328 if (m_statusBar->type() != DolphinStatusBar::Error) {
329 m_statusBar->setMessage(newMessage, DolphinStatusBar::Default);
330 }
331 }
332
333 void DolphinViewContainer::updateProgress(int percent)
334 {
335 if (m_statusBar->progressText().isEmpty()) {
336 m_statusBar->setProgressText(i18nc("@info:progress", "Loading folder..."));
337 }
338 m_statusBar->setProgress(percent);
339 }
340
341 void DolphinViewContainer::slotStartedPathLoading()
342 {
343 if (isSearchUrl(url())) {
344 // Search KIO-slaves usually don't provide any progress information. Give
345 // a hint to the user that a searching is done:
346 updateStatusBar();
347 m_statusBar->setProgressText(i18nc("@info", "Searching..."));
348 m_statusBar->setProgress(-1);
349 } else {
350 // Trigger an undetermined progress indication. The progress
351 // information in percent will be triggered by the percent() signal
352 // of the directory lister later.
353 updateProgress(-1);
354 }
355 }
356
357 void DolphinViewContainer::slotFinishedPathLoading()
358 {
359 if (!m_statusBar->progressText().isEmpty()) {
360 m_statusBar->setProgressText(QString());
361 m_statusBar->setProgress(100);
362 }
363
364 if (isSearchUrl(url()) && m_view->items().isEmpty()) {
365 // The dir lister has been completed on a Nepomuk-URI and no items have been found. Instead
366 // of showing the default status bar information ("0 items") a more helpful information is given:
367 m_statusBar->setMessage(i18nc("@info:status", "No items found."), DolphinStatusBar::Information);
368 } else {
369 updateStatusBar();
370 }
371 }
372
373 void DolphinViewContainer::slotItemActivated(const KFileItem& item)
374 {
375 KUrl url = item.targetUrl();
376
377 if (item.isDir()) {
378 m_view->setUrl(url);
379 return;
380 }
381
382 if (GeneralSettings::browseThroughArchives() && item.isFile() && url.isLocalFile()) {
383 // Generic mechanism for redirecting to tar:/<path>/ when clicking on a tar file,
384 // zip:/<path>/ when clicking on a zip file, etc.
385 // The .protocol file specifies the mimetype that the kioslave handles.
386 // Note that we don't use mimetype inheritance since we don't want to
387 // open OpenDocument files as zip folders...
388 const QString protocol = KProtocolManager::protocolForArchiveMimetype(item.mimetype());
389 if (!protocol.isEmpty()) {
390 url.setProtocol(protocol);
391 m_view->setUrl(url);
392 return;
393 }
394 }
395
396 if (item.mimetype() == QLatin1String("application/x-desktop")) {
397 // Redirect to the URL in Type=Link desktop files
398 KDesktopFile desktopFile(url.toLocalFile());
399 if (desktopFile.hasLinkType()) {
400 url = desktopFile.readUrl();
401 m_view->setUrl(url);
402 return;
403 }
404 }
405
406 item.run();
407 }
408
409 void DolphinViewContainer::openFile(const KUrl& url)
410 {
411 const KFileItem item(KFileItem::Unknown, KFileItem::Unknown, url);
412 slotItemActivated(item);
413 }
414
415 void DolphinViewContainer::showItemInfo(const KFileItem& item)
416 {
417 if (item.isNull()) {
418 // Only clear the status bar if unimportant messages are shown.
419 // This prevents that information- or error-messages get hidden
420 // by moving the mouse above the viewport or when closing the
421 // context menu.
422 if (m_statusBar->type() == DolphinStatusBar::Default) {
423 m_statusBar->clear();
424 }
425 } else {
426 QString message;
427 if (item.isDir()) {
428 message = item.name();
429 } else {
430 message = i18nc("@info:status filename (type)", "%1 (%2)", item.name(), item.mimeComment());
431 }
432 m_statusBar->setMessage(message, DolphinStatusBar::Default);
433 }
434 }
435
436 void DolphinViewContainer::showInfoMessage(const QString& msg)
437 {
438 m_statusBar->setMessage(msg, DolphinStatusBar::Information);
439 }
440
441 void DolphinViewContainer::showErrorMessage(const QString& msg)
442 {
443 m_statusBar->setMessage(msg, DolphinStatusBar::Error);
444 }
445
446 void DolphinViewContainer::showOperationCompletedMessage(const QString& msg)
447 {
448 m_statusBar->setMessage(msg, DolphinStatusBar::OperationCompleted);
449 }
450
451 void DolphinViewContainer::closeFilterBar()
452 {
453 m_filterBar->hide();
454 m_filterBar->clear();
455 m_view->setFocus();
456 emit showFilterBarChanged(false);
457 }
458
459 void DolphinViewContainer::setNameFilter(const QString& nameFilter)
460 {
461 m_view->setNameFilter(nameFilter);
462 delayedStatusBarUpdate();
463 }
464
465 void DolphinViewContainer::activate()
466 {
467 setActive(true);
468 }
469
470 void DolphinViewContainer::slotViewUrlAboutToBeChanged(const KUrl& url)
471 {
472 // URL changes of the view can happen in two ways:
473 // 1. The URL navigator gets changed and will trigger the view to update its URL
474 // 2. The URL of the view gets changed and will trigger the URL navigator to update
475 // its URL (e.g. by clicking on an item)
476 // In this scope the view-state may only get saved in case 2:
477 if (url != m_urlNavigator->locationUrl()) {
478 saveViewState();
479 }
480 }
481
482 void DolphinViewContainer::slotUrlNavigatorLocationAboutToBeChanged(const KUrl& url)
483 {
484 // URL changes of the view can happen in two ways:
485 // 1. The URL navigator gets changed and will trigger the view to update its URL
486 // 2. The URL of the view gets changed and will trigger the URL navigator to update
487 // its URL (e.g. by clicking on an item)
488 // In this scope the view-state may only get saved in case 1:
489 if (url != m_view->url()) {
490 saveViewState();
491 }
492 }
493
494 void DolphinViewContainer::slotUrlNavigatorLocationChanged(const KUrl& url)
495 {
496 if (KProtocolManager::supportsListing(url)) {
497 setSearchModeEnabled(isSearchUrl(url));
498 m_view->setUrl(url);
499
500 if (isActive() && !isSearchUrl(url)) {
501 // When an URL has been entered, the view should get the focus.
502 // The focus must be requested asynchronously, as changing the URL might create
503 // a new view widget.
504 QTimer::singleShot(0, this, SLOT(requestFocus()));
505 }
506 } else if (KProtocolManager::isSourceProtocol(url)) {
507 QString app = "konqueror";
508 if (url.protocol().startsWith(QLatin1String("http"))) {
509 showErrorMessage(i18nc("@info:status",
510 "Dolphin does not support web pages, the web browser has been launched"));
511 const KConfigGroup config(KSharedConfig::openConfig("kdeglobals"), "General");
512 const QString browser = config.readEntry("BrowserApplication");
513 if (!browser.isEmpty()) {
514 app = browser;
515 if (app.startsWith('!')) {
516 // a literal command has been configured, remove the '!' prefix
517 app = app.mid(1);
518 }
519 }
520 } else {
521 showErrorMessage(i18nc("@info:status",
522 "Protocol not supported by Dolphin, Konqueror has been launched"));
523 }
524
525 const QString secureUrl = KShell::quoteArg(url.pathOrUrl());
526 const QString command = app + ' ' + secureUrl;
527 KRun::runCommand(command, app, app, this);
528 } else {
529 showErrorMessage(i18nc("@info:status", "Invalid protocol"));
530 }
531 }
532
533 void DolphinViewContainer::dropUrls(const KUrl& destination, QDropEvent* event)
534 {
535 Q_UNUSED(destination);
536 Q_UNUSED(event);
537 //DragAndDropHelper::instance().dropUrls(KFileItem(), destination, event, this);
538 }
539
540 void DolphinViewContainer::redirect(const KUrl& oldUrl, const KUrl& newUrl)
541 {
542 Q_UNUSED(oldUrl);
543 const bool block = m_urlNavigator->signalsBlocked();
544 m_urlNavigator->blockSignals(true);
545
546 // Assure that the location state is reset for redirection URLs. This
547 // allows to skip redirection URLs when going back or forward in the
548 // URL history.
549 m_urlNavigator->saveLocationState(QByteArray());
550 m_urlNavigator->setLocationUrl(newUrl);
551 setSearchModeEnabled(isSearchUrl(newUrl));
552
553 m_urlNavigator->blockSignals(block);
554 }
555
556 void DolphinViewContainer::requestFocus()
557 {
558 m_view->setFocus();
559 }
560
561 void DolphinViewContainer::saveUrlCompletionMode(KGlobalSettings::Completion completion)
562 {
563 GeneralSettings::setUrlCompletionMode(completion);
564 }
565
566 void DolphinViewContainer::slotHistoryChanged()
567 {
568 QByteArray locationState = m_urlNavigator->locationState();
569 if (!locationState.isEmpty()) {
570 QDataStream stream(&locationState, QIODevice::ReadOnly);
571 m_view->restoreState(stream);
572 }
573 }
574
575 void DolphinViewContainer::startSearching(const QString &text)
576 {
577 Q_UNUSED(text);
578 const KUrl url = m_searchBox->urlForSearching();
579 if (url.isValid() && !url.isEmpty()) {
580 m_urlNavigator->setLocationUrl(url);
581 }
582 }
583
584 void DolphinViewContainer::closeSearchBox()
585 {
586 setSearchModeEnabled(false);
587 }
588
589 void DolphinViewContainer::stopLoading()
590 {
591 m_view->stopLoading();
592 m_statusBar->setProgress(100);
593 }
594
595 bool DolphinViewContainer::isSearchUrl(const KUrl& url) const
596 {
597 const QString protocol = url.protocol();
598 return protocol.contains("search") || (protocol == QLatin1String("nepomuk"));
599 }
600
601 void DolphinViewContainer::saveViewState()
602 {
603 QByteArray locationState;
604 QDataStream stream(&locationState, QIODevice::WriteOnly);
605 m_view->saveState(stream);
606 m_urlNavigator->saveLocationState(locationState);
607 }
608
609 #include "dolphinviewcontainer.moc"