]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinviewcontainer.cpp
Automatically start the searching if the user did not change the search-text for...
[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 <kmimetyperesolver.h>
40 #include <knewmenu.h>
41 #include <konqmimedata.h>
42 #include <kfileitemlistproperties.h>
43 #include <konq_operations.h>
44 #include <kshell.h>
45 #include <kurl.h>
46 #include <kurlcombobox.h>
47 #include <kurlnavigator.h>
48 #include <krun.h>
49
50 #include "dolphin_generalsettings.h"
51 #include "dolphinmainwindow.h"
52 #include "filterbar/filterbar.h"
53 #include "search/dolphinsearchbox.h"
54 #include "settings/dolphinsettings.h"
55 #include "statusbar/dolphinstatusbar.h"
56 #include "views/dolphincolumnview.h"
57 #include "views/dolphindetailsview.h"
58 #include "views/dolphindirlister.h"
59 #include "views/dolphinsortfilterproxymodel.h"
60 #include "views/draganddrophelper.h"
61 #include "views/dolphiniconsview.h"
62 #include "views/dolphinmodel.h"
63 #include "views/dolphinviewcontroller.h"
64 #include "views/viewmodecontroller.h"
65 #include "views/viewproperties.h"
66
67 DolphinViewContainer::DolphinViewContainer(const KUrl& url, QWidget* parent) :
68 QWidget(parent),
69 m_isFolderWritable(false),
70 m_topLayout(0),
71 m_urlNavigator(0),
72 m_searchBox(0),
73 m_view(0),
74 m_filterBar(0),
75 m_statusBar(0),
76 m_statusBarTimer(0),
77 m_dirLister(0),
78 m_proxyModel(0)
79 {
80 hide();
81
82 m_topLayout = new QVBoxLayout(this);
83 m_topLayout->setSpacing(0);
84 m_topLayout->setMargin(0);
85
86 m_urlNavigator = new KUrlNavigator(DolphinSettings::instance().placesModel(), url, this);
87 connect(m_urlNavigator, SIGNAL(urlsDropped(const KUrl&, QDropEvent*)),
88 this, SLOT(dropUrls(const KUrl&, QDropEvent*)));
89 connect(m_urlNavigator, SIGNAL(activated()),
90 this, SLOT(activate()));
91 connect(m_urlNavigator->editor(), SIGNAL(completionModeChanged(KGlobalSettings::Completion)),
92 this, SLOT(saveUrlCompletionMode(KGlobalSettings::Completion)));
93
94 const GeneralSettings* settings = DolphinSettings::instance().generalSettings();
95 m_urlNavigator->setUrlEditable(settings->editableUrl());
96 m_urlNavigator->setShowFullPath(settings->showFullPath());
97 m_urlNavigator->setHomeUrl(KUrl(settings->homeUrl()));
98 KUrlComboBox* editor = m_urlNavigator->editor();
99 editor->setCompletionMode(KGlobalSettings::Completion(settings->urlCompletionMode()));
100
101 m_searchBox = new DolphinSearchBox(this);
102 m_searchBox->hide();
103 connect(m_searchBox, SIGNAL(closeRequest()), this, SLOT(closeSearchBox()));
104 connect(m_searchBox, SIGNAL(search(QString)), this, SLOT(startSearching(QString)));
105 connect(m_searchBox, SIGNAL(returnPressed(QString)), this, SLOT(requestFocus()));
106
107 m_dirLister = new DolphinDirLister();
108 m_dirLister->setAutoUpdate(true);
109 m_dirLister->setMainWindow(window());
110 m_dirLister->setDelayedMimeTypes(true);
111
112 m_dolphinModel = new DolphinModel(this);
113 m_dolphinModel->setDirLister(m_dirLister); // m_dolphinModel takes ownership of m_dirLister
114 m_dolphinModel->setDropsAllowed(DolphinModel::DropOnDirectory);
115
116 m_proxyModel = new DolphinSortFilterProxyModel(this);
117 m_proxyModel->setSourceModel(m_dolphinModel);
118 m_proxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
119
120 connect(m_dirLister, SIGNAL(started(KUrl)),
121 this, SLOT(initializeProgress()));
122 connect(m_dirLister, SIGNAL(clear()),
123 this, SLOT(delayedStatusBarUpdate()));
124 connect(m_dirLister, SIGNAL(percent(int)),
125 this, SLOT(updateProgress(int)));
126 connect(m_dirLister, SIGNAL(itemsDeleted(const KFileItemList&)),
127 this, SLOT(delayedStatusBarUpdate()));
128 connect(m_dirLister, SIGNAL(completed()),
129 this, SLOT(slotDirListerCompleted()));
130 connect(m_dirLister, SIGNAL(infoMessage(const QString&)),
131 this, SLOT(showInfoMessage(const QString&)));
132 connect(m_dirLister, SIGNAL(errorMessage(const QString&)),
133 this, SLOT(showErrorMessage(const QString&)));
134 connect(m_dirLister, SIGNAL(urlIsFileError(const KUrl&)),
135 this, SLOT(openFile(const KUrl&)));
136
137 m_view = new DolphinView(this, url, m_proxyModel);
138 connect(m_view, SIGNAL(urlChanged(const KUrl&)),
139 m_urlNavigator, SLOT(setUrl(const KUrl&)));
140 connect(m_view, SIGNAL(requestItemInfo(KFileItem)),
141 this, SLOT(showItemInfo(KFileItem)));
142 connect(m_view, SIGNAL(errorMessage(const QString&)),
143 this, SLOT(showErrorMessage(const QString&)));
144 connect(m_view, SIGNAL(infoMessage(const QString&)),
145 this, SLOT(showInfoMessage(const QString&)));
146 connect(m_view, SIGNAL(operationCompletedMessage(const QString&)),
147 this, SLOT(showOperationCompletedMessage(const QString&)));
148 connect(m_view, SIGNAL(itemTriggered(KFileItem)),
149 this, SLOT(slotItemTriggered(KFileItem)));
150 connect(m_view, SIGNAL(redirection(KUrl, KUrl)),
151 this, SLOT(redirect(KUrl, KUrl)));
152 connect(m_view, SIGNAL(selectionChanged(const KFileItemList&)),
153 this, SLOT(delayedStatusBarUpdate()));
154
155 connect(m_urlNavigator, SIGNAL(urlChanged(const KUrl&)),
156 this, SLOT(slotUrlNavigatorLocationChanged(const KUrl&)));
157 connect(m_urlNavigator, SIGNAL(urlAboutToBeChanged(const KUrl&)),
158 this, SLOT(saveViewState()));
159 connect(m_urlNavigator, SIGNAL(historyChanged()),
160 this, SLOT(slotHistoryChanged()));
161
162 // initialize status bar
163 m_statusBar = new DolphinStatusBar(this, m_view);
164 m_statusBarTimer = new QTimer(this);
165 m_statusBarTimer->setSingleShot(true);
166 m_statusBarTimer->setInterval(300);
167 connect(m_statusBarTimer, SIGNAL(timeout()),
168 this, SLOT(updateStatusBar()));
169
170 KIO::FileUndoManager* undoManager = KIO::FileUndoManager::self();
171 connect(undoManager, SIGNAL(jobRecordingFinished(CommandType)),
172 this, SLOT(delayedStatusBarUpdate()));
173
174 // initialize filter bar
175 m_filterBar = new FilterBar(this);
176 m_filterBar->setVisible(settings->filterBar());
177 connect(m_filterBar, SIGNAL(filterChanged(const QString&)),
178 this, SLOT(setNameFilter(const QString&)));
179 connect(m_filterBar, SIGNAL(closeRequest()),
180 this, SLOT(closeFilterBar()));
181 connect(m_view, SIGNAL(urlChanged(const KUrl&)),
182 m_filterBar, SLOT(clear()));
183
184 m_topLayout->addWidget(m_urlNavigator);
185 m_topLayout->addWidget(m_searchBox);
186 m_topLayout->addWidget(m_view);
187 m_topLayout->addWidget(m_filterBar);
188 m_topLayout->addWidget(m_statusBar);
189
190 setSearchModeEnabled(isSearchUrl(url));
191 }
192
193 DolphinViewContainer::~DolphinViewContainer()
194 {
195 }
196
197 KUrl DolphinViewContainer::url() const
198 {
199 return m_urlNavigator->locationUrl();
200 }
201
202 void DolphinViewContainer::setActive(bool active)
203 {
204 m_urlNavigator->setActive(active);
205 m_view->setActive(active);
206 if (active) {
207 emit writeStateChanged(m_isFolderWritable);
208 }
209 }
210
211 bool DolphinViewContainer::isActive() const
212 {
213 Q_ASSERT(m_view->isActive() == m_urlNavigator->isActive());
214 return m_view->isActive();
215 }
216
217 void DolphinViewContainer::refresh()
218 {
219 m_view->refresh();
220 m_statusBar->refresh();
221 }
222
223 bool DolphinViewContainer::isFilterBarVisible() const
224 {
225 return m_filterBar->isVisible();
226 }
227
228 void DolphinViewContainer::setSearchModeEnabled(bool enabled)
229 {
230 m_searchBox->setVisible(enabled);
231 m_urlNavigator->setVisible(!enabled);
232
233 if (enabled) {
234 // Remember the current URL, so that it can be restored
235 // when switching back to the URL navigator
236 const KUrl url = m_urlNavigator->locationUrl();
237 m_searchBox->setSearchPath(url);
238 } else {
239 // Restore the URL for the URL navigator. If Dolphin has been
240 // started with a search-URL, the home URL is used as fallback.
241 const KUrl url = m_searchBox->searchPath();
242 if (url.isValid() && !url.isEmpty()) {
243 if (isSearchUrl(url)) {
244 m_urlNavigator->goHome();
245 } else {
246 m_urlNavigator->setLocationUrl(url);
247 }
248 }
249 }
250 }
251
252 bool DolphinViewContainer::isSearchModeEnabled() const
253 {
254 return m_searchBox->isVisible();
255 }
256
257 void DolphinViewContainer::setUrl(const KUrl& newUrl)
258 {
259 if (newUrl != m_urlNavigator->locationUrl()) {
260 m_urlNavigator->setLocationUrl(newUrl);
261 // Temporary disable the 'File'->'Create New...' menu until
262 // the write permissions can be checked in a fast way at
263 // DolphinViewContainer::slotDirListerCompleted().
264 m_isFolderWritable = false;
265 if (isActive()) {
266 emit writeStateChanged(false);
267 }
268 }
269 }
270
271 void DolphinViewContainer::showFilterBar(bool show)
272 {
273 Q_ASSERT(m_filterBar != 0);
274 if (show) {
275 m_filterBar->show();
276 } else {
277 closeFilterBar();
278 }
279 }
280
281 void DolphinViewContainer::delayedStatusBarUpdate()
282 {
283 // Invoke updateStatusBar() with a small delay. This assures that
284 // when a lot of delayedStatusBarUpdates() are done in a short time,
285 // no bottleneck is given.
286 m_statusBarTimer->start();
287 }
288
289 void DolphinViewContainer::updateStatusBar()
290 {
291 // As the item count information is less important
292 // in comparison with other messages, it should only
293 // be shown if:
294 // - the status bar is empty or
295 // - shows already the item count information or
296 // - shows only a not very important information
297 // - if any progress is given don't show the item count info at all
298 const QString msg = m_statusBar->message();
299 const bool updateStatusBarMsg = (msg.isEmpty()
300 || (msg == m_statusBar->defaultText())
301 || (m_statusBar->type() == DolphinStatusBar::Information))
302 && (m_statusBar->progress() == 100);
303
304 const QString text = m_view->statusBarText();
305 m_statusBar->setDefaultText(text);
306
307 if (updateStatusBarMsg) {
308 m_statusBar->setMessage(text, DolphinStatusBar::Default);
309 }
310 }
311
312 void DolphinViewContainer::initializeProgress()
313 {
314 if (isSearchUrl(url())) {
315 // Search KIO-slaves usually don't provide any progress information. Give
316 // an immediate hint to the user that a searching is done:
317 updateStatusBar();
318 m_statusBar->setProgressText(i18nc("@info", "Searching..."));
319 m_statusBar->setProgress(-1);
320 }
321 }
322
323 void DolphinViewContainer::updateProgress(int percent)
324 {
325 if (m_statusBar->progressText().isEmpty()) {
326 m_statusBar->setProgressText(i18nc("@info:progress", "Loading folder..."));
327 }
328 m_statusBar->setProgress(percent);
329 }
330
331 void DolphinViewContainer::slotDirListerCompleted()
332 {
333 if (!m_statusBar->progressText().isEmpty()) {
334 m_statusBar->setProgressText(QString());
335 m_statusBar->setProgress(100);
336 }
337
338 if (isSearchUrl(url()) && (m_dirLister->items().count() == 0)) {
339 // The dir lister has been completed on a Nepomuk-URI and no items have been found. Instead
340 // of showing the default status bar information ("0 items") a more helpful information is given:
341 m_statusBar->setMessage(i18nc("@info:status", "No items found."), DolphinStatusBar::Information);
342 } else {
343 updateStatusBar();
344 }
345
346 // Enable the 'File'->'Create New...' menu only if the directory
347 // supports writing.
348 KFileItem item = m_dirLister->rootItem();
349 if (item.isNull()) {
350 // it is unclear whether writing is supported
351 m_isFolderWritable = true;
352 } else {
353 KFileItemListProperties capabilities(KFileItemList() << item);
354 m_isFolderWritable = capabilities.supportsWriting();
355 }
356
357 if (isActive()) {
358 emit writeStateChanged(m_isFolderWritable);
359 }
360 }
361
362 void DolphinViewContainer::showItemInfo(const KFileItem& item)
363 {
364 if (item.isNull()) {
365 // Only clear the status bar if unimportant messages are shown.
366 // This prevents that information- or error-messages get hidden
367 // by moving the mouse above the viewport or when closing the
368 // context menu.
369 if (m_statusBar->type() == DolphinStatusBar::Default) {
370 m_statusBar->clear();
371 }
372 } else {
373 m_statusBar->setMessage(item.getStatusBarInfo(), DolphinStatusBar::Default);
374 }
375 }
376
377 void DolphinViewContainer::showInfoMessage(const QString& msg)
378 {
379 m_statusBar->setMessage(msg, DolphinStatusBar::Information);
380 }
381
382 void DolphinViewContainer::showErrorMessage(const QString& msg)
383 {
384 m_statusBar->setMessage(msg, DolphinStatusBar::Error);
385 }
386
387 void DolphinViewContainer::showOperationCompletedMessage(const QString& msg)
388 {
389 m_statusBar->setMessage(msg, DolphinStatusBar::OperationCompleted);
390 }
391
392 void DolphinViewContainer::closeFilterBar()
393 {
394 m_filterBar->hide();
395 m_filterBar->clear();
396 m_view->setFocus();
397 emit showFilterBarChanged(false);
398 }
399
400 void DolphinViewContainer::setNameFilter(const QString& nameFilter)
401 {
402 m_view->setNameFilter(nameFilter);
403 delayedStatusBarUpdate();
404 }
405
406 void DolphinViewContainer::activate()
407 {
408 setActive(true);
409 }
410
411 void DolphinViewContainer::saveViewState()
412 {
413 QByteArray locationState;
414 QDataStream stream(&locationState, QIODevice::WriteOnly);
415 m_view->saveState(stream);
416 m_urlNavigator->saveLocationState(locationState);
417 }
418
419 void DolphinViewContainer::slotUrlNavigatorLocationChanged(const KUrl& url)
420 {
421 if (KProtocolManager::supportsListing(url)) {
422 // Assure that the search box is shown instead of the URL navigator in case
423 // that the URL \p url is a search URL (e. g. nepomuksearch:// or filenamesearch://).
424 if (isSearchUrl(url)) {
425 if (!m_searchBox->isVisible()) {
426 m_searchBox->setVisible(true);
427 m_urlNavigator->setVisible(false);
428 }
429 } else if (!m_urlNavigator->isVisible()) {
430 m_urlNavigator->setVisible(true);
431 m_searchBox->setVisible(false);
432 }
433
434 m_view->setUrl(url);
435 if (isActive() && !isSearchUrl(url)) {
436 // When an URL has been entered, the view should get the focus.
437 // The focus must be requested asynchronously, as changing the URL might create
438 // a new view widget.
439 QTimer::singleShot(0, this, SLOT(requestFocus()));
440 }
441 } else if (KProtocolManager::isSourceProtocol(url)) {
442 QString app = "konqueror";
443 if (url.protocol().startsWith(QLatin1String("http"))) {
444 showErrorMessage(i18nc("@info:status",
445 "Dolphin does not support web pages, the web browser has been launched"));
446 const KConfigGroup config(KSharedConfig::openConfig("kdeglobals"), "General");
447 const QString browser = config.readEntry("BrowserApplication");
448 if (!browser.isEmpty()) {
449 app = browser;
450 if (app.startsWith('!')) {
451 // a literal command has been configured, remove the '!' prefix
452 app = app.mid(1);
453 }
454 }
455 } else {
456 showErrorMessage(i18nc("@info:status",
457 "Protocol not supported by Dolphin, Konqueror has been launched"));
458 }
459
460 const QString secureUrl = KShell::quoteArg(url.pathOrUrl());
461 const QString command = app + ' ' + secureUrl;
462 KRun::runCommand(command, app, app, this);
463 } else {
464 showErrorMessage(i18nc("@info:status", "Invalid protocol"));
465 }
466 }
467
468 void DolphinViewContainer::dropUrls(const KUrl& destination, QDropEvent* event)
469 {
470 DragAndDropHelper::instance().dropUrls(KFileItem(), destination, event, this);
471 }
472
473 void DolphinViewContainer::redirect(const KUrl& oldUrl, const KUrl& newUrl)
474 {
475 Q_UNUSED(oldUrl);
476 const bool block = m_urlNavigator->signalsBlocked();
477 m_urlNavigator->blockSignals(true);
478
479 // Assure that the location state is reset for redirection URLs. This
480 // allows to skip redirection URLs when going back or forward in the
481 // URL history.
482 m_urlNavigator->saveLocationState(QByteArray());
483 m_urlNavigator->setLocationUrl(newUrl);
484
485 m_urlNavigator->blockSignals(block);
486 }
487
488 void DolphinViewContainer::requestFocus()
489 {
490 m_view->setFocus();
491 }
492
493 void DolphinViewContainer::saveUrlCompletionMode(KGlobalSettings::Completion completion)
494 {
495 DolphinSettings& settings = DolphinSettings::instance();
496 settings.generalSettings()->setUrlCompletionMode(completion);
497 settings.save();
498 }
499
500 void DolphinViewContainer::slotHistoryChanged()
501 {
502 QByteArray locationState = m_urlNavigator->locationState();
503
504 if (!locationState.isEmpty()) {
505 QDataStream stream(&locationState, QIODevice::ReadOnly);
506 m_view->restoreState(stream);
507 }
508 }
509
510 void DolphinViewContainer::startSearching(const QString &text)
511 {
512 Q_UNUSED(text);
513 const KUrl url = m_searchBox->urlForSearching();
514 if (url.isValid() && !url.isEmpty()) {
515 m_urlNavigator->setLocationUrl(url);
516 }
517 }
518
519 void DolphinViewContainer::closeSearchBox()
520 {
521 setSearchModeEnabled(false);
522 }
523
524 bool DolphinViewContainer::isSearchUrl(const KUrl& url) const
525 {
526 return url.protocol().contains("search");
527 }
528
529 void DolphinViewContainer::slotItemTriggered(const KFileItem& item)
530 {
531 KUrl url = item.targetUrl();
532
533 if (item.isDir()) {
534 m_view->setUrl(url);
535 return;
536 }
537
538 const GeneralSettings* settings = DolphinSettings::instance().generalSettings();
539 const bool browseThroughArchives = settings->browseThroughArchives();
540 if (browseThroughArchives && item.isFile() && url.isLocalFile()) {
541 // Generic mechanism for redirecting to tar:/<path>/ when clicking on a tar file,
542 // zip:/<path>/ when clicking on a zip file, etc.
543 // The .protocol file specifies the mimetype that the kioslave handles.
544 // Note that we don't use mimetype inheritance since we don't want to
545 // open OpenDocument files as zip folders...
546 const QString protocol = KProtocolManager::protocolForArchiveMimetype(item.mimetype());
547 if (!protocol.isEmpty()) {
548 url.setProtocol(protocol);
549 m_view->setUrl(url);
550 return;
551 }
552 }
553
554 if (item.mimetype() == "application/x-desktop") {
555 // redirect to the url in Type=Link desktop files
556 KDesktopFile desktopFile(url.toLocalFile());
557 if (desktopFile.hasLinkType()) {
558 url = desktopFile.readUrl();
559 m_view->setUrl(url);
560 return;
561 }
562 }
563
564 item.run();
565 }
566
567 void DolphinViewContainer::openFile(const KUrl& url)
568 {
569 // Using m_dolphinModel for getting the file item instance is not possible
570 // here: openFile() is triggered by an error of the directory lister
571 // job, so the file item must be received "manually".
572 const KFileItem item(KFileItem::Unknown, KFileItem::Unknown, url);
573 slotItemTriggered(item);
574 }
575
576 #include "dolphinviewcontainer.moc"