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