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