]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinview.cpp
fix possible crash (CID 3607)
[dolphin.git] / src / dolphinview.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz <peter.penz@gmx.at> *
3 * Copyright (C) 2006 by Gregor Kališnik <gregor@podnapisi.net> *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19 ***************************************************************************/
20
21 #include "dolphinview.h"
22
23 #include <QtGui/QApplication>
24 #include <QtGui/QClipboard>
25 #include <QtGui/QKeyEvent>
26 #include <QtGui/QItemSelection>
27 #include <QtGui/QBoxLayout>
28 #include <QtCore/QTimer>
29 #include <QtGui/QScrollBar>
30
31 #include <kdirmodel.h>
32 #include <kfileitemdelegate.h>
33 #include <kfileplacesmodel.h>
34 #include <kglobalsettings.h>
35 #include <klocale.h>
36 #include <kiconeffect.h>
37 #include <kio/netaccess.h>
38 #include <kio/renamedialog.h>
39 #include <kio/previewjob.h>
40 #include <kmimetyperesolver.h>
41 #include <konqmimedata.h>
42 #include <konq_operations.h>
43 #include <kurl.h>
44
45 #include "dolphincolumnview.h"
46 #include "dolphincontroller.h"
47 #include "dolphinstatusbar.h"
48 #include "dolphinmainwindow.h"
49 #include "dolphindirlister.h"
50 #include "dolphinsortfilterproxymodel.h"
51 #include "dolphindetailsview.h"
52 #include "dolphiniconsview.h"
53 #include "dolphincontextmenu.h"
54 #include "dolphinitemcategorizer.h"
55 #include "filterbar.h"
56 #include "renamedialog.h"
57 #include "kurlnavigator.h"
58 #include "viewproperties.h"
59 #include "dolphinsettings.h"
60 #include "dolphin_generalsettings.h"
61
62 DolphinView::DolphinView(DolphinMainWindow* mainWindow,
63 QWidget* parent,
64 const KUrl& url,
65 Mode mode,
66 bool showHiddenFiles) :
67 QWidget(parent),
68 m_showProgress(false),
69 m_blockContentsMovedSignal(false),
70 m_initializeColumnView(false),
71 m_mode(mode),
72 m_iconSize(0),
73 m_folderCount(0),
74 m_fileCount(0),
75 m_mainWindow(mainWindow),
76 m_topLayout(0),
77 m_urlNavigator(0),
78 m_controller(0),
79 m_iconsView(0),
80 m_detailsView(0),
81 m_columnView(0),
82 m_fileItemDelegate(0),
83 m_filterBar(0),
84 m_statusBar(0),
85 m_dirModel(0),
86 m_dirLister(0),
87 m_proxyModel(0)
88 {
89 hide();
90 setFocusPolicy(Qt::StrongFocus);
91 m_topLayout = new QVBoxLayout(this);
92 m_topLayout->setSpacing(0);
93 m_topLayout->setMargin(0);
94
95 connect(m_mainWindow, SIGNAL(activeViewChanged()),
96 this, SLOT(updateActivationState()));
97
98 QClipboard* clipboard = QApplication::clipboard();
99 connect(clipboard, SIGNAL(dataChanged()),
100 this, SLOT(updateCutItems()));
101
102 m_urlNavigator = new KUrlNavigator(DolphinSettings::instance().placesModel(), url, this);
103 m_urlNavigator->setUrlEditable(DolphinSettings::instance().generalSettings()->editableUrl());
104 m_urlNavigator->setHomeUrl(DolphinSettings::instance().generalSettings()->homeUrl());
105 connect(m_urlNavigator, SIGNAL(urlChanged(const KUrl&)),
106 this, SLOT(changeDirectory(const KUrl&)));
107 connect(m_urlNavigator, SIGNAL(urlsDropped(const KUrl::List&, const KUrl&)),
108 this, SLOT(dropUrls(const KUrl::List&, const KUrl&)));
109 connect(m_urlNavigator, SIGNAL(activated()),
110 this, SLOT(requestActivation()));
111 connect(this, SIGNAL(contentsMoved(int, int)),
112 m_urlNavigator, SLOT(savePosition(int, int)));
113
114 m_statusBar = new DolphinStatusBar(this);
115
116 m_dirLister = new DolphinDirLister();
117 m_dirLister->setAutoUpdate(true);
118 m_dirLister->setMainWindow(this);
119 m_dirLister->setShowingDotFiles(showHiddenFiles);
120 m_dirLister->setDelayedMimeTypes(true);
121
122 connect(m_dirLister, SIGNAL(clear()),
123 this, SLOT(updateStatusBar()));
124 connect(m_dirLister, SIGNAL(percent(int)),
125 this, SLOT(updateProgress(int)));
126 connect(m_dirLister, SIGNAL(deleteItem(KFileItem*)),
127 this, SLOT(updateStatusBar()));
128 connect(m_dirLister, SIGNAL(completed()),
129 this, SLOT(updateItemCount()));
130 connect(m_dirLister, SIGNAL(completed()),
131 this, SLOT(updateCutItems()));
132 connect(m_dirLister, SIGNAL(newItems(const KFileItemList&)),
133 this, SLOT(generatePreviews(const KFileItemList&)));
134 connect(m_dirLister, SIGNAL(infoMessage(const QString&)),
135 this, SLOT(showInfoMessage(const QString&)));
136 connect(m_dirLister, SIGNAL(errorMessage(const QString&)),
137 this, SLOT(showErrorMessage(const QString&)));
138
139 m_dirModel = new KDirModel();
140 m_dirModel->setDirLister(m_dirLister);
141 m_dirModel->setDropsAllowed(KDirModel::DropOnDirectory);
142
143 m_proxyModel = new DolphinSortFilterProxyModel(this);
144 m_proxyModel->setSourceModel(m_dirModel);
145
146 m_controller = new DolphinController(this);
147 connect(m_controller, SIGNAL(requestContextMenu(const QPoint&)),
148 this, SLOT(openContextMenu(const QPoint&)));
149 connect(m_controller, SIGNAL(urlsDropped(const KUrl::List&, const QModelIndex&, QWidget*)),
150 this, SLOT(dropUrls(const KUrl::List&, const QModelIndex&, QWidget*)));
151 connect(m_controller, SIGNAL(sortingChanged(DolphinView::Sorting)),
152 this, SLOT(updateSorting(DolphinView::Sorting)));
153 connect(m_controller, SIGNAL(sortOrderChanged(Qt::SortOrder)),
154 this, SLOT(updateSortOrder(Qt::SortOrder)));
155 connect(m_controller, SIGNAL(itemTriggered(const QModelIndex&)),
156 this, SLOT(triggerItem(const QModelIndex&)));
157 connect(m_controller, SIGNAL(selectionChanged()),
158 this, SLOT(emitSelectionChangedSignal()));
159 connect(m_controller, SIGNAL(activated()),
160 this, SLOT(requestActivation()));
161 connect(m_controller, SIGNAL(itemEntered(const QModelIndex&)),
162 this, SLOT(showHoverInformation(const QModelIndex&)));
163 connect(m_controller, SIGNAL(viewportEntered()),
164 this, SLOT(clearHoverInformation()));
165
166 createView();
167
168 m_iconSize = K3Icon::SizeMedium;
169
170 m_filterBar = new FilterBar(this);
171 m_filterBar->hide();
172 connect(m_filterBar, SIGNAL(filterChanged(const QString&)),
173 this, SLOT(changeNameFilter(const QString&)));
174 connect(m_filterBar, SIGNAL(closeRequest()),
175 this, SLOT(closeFilterBar()));
176
177 m_topLayout->addWidget(m_urlNavigator);
178 m_topLayout->addWidget(itemView());
179 m_topLayout->addWidget(m_filterBar);
180 m_topLayout->addWidget(m_statusBar);
181 }
182
183 DolphinView::~DolphinView()
184 {
185 delete m_dirLister;
186 m_dirLister = 0;
187 }
188
189 void DolphinView::setUrl(const KUrl& url)
190 {
191 m_urlNavigator->setUrl(url);
192 m_controller->setUrl(url);
193 }
194
195 const KUrl& DolphinView::url() const
196 {
197 return m_urlNavigator->url();
198 }
199
200 KUrl DolphinView::rootUrl() const
201 {
202 return isColumnViewActive() ? m_dirLister->url() : url();
203 }
204
205 bool DolphinView::isActive() const
206 {
207 return m_mainWindow->activeView() == this;
208 }
209
210 void DolphinView::setMode(Mode mode)
211 {
212 if (mode == m_mode) {
213 return; // the wished mode is already set
214 }
215
216 m_mode = mode;
217
218 if (isColumnViewActive()) {
219 // When changing the mode in the column view, it makes sense
220 // to go back to the root URL of the column view automatically.
221 // Otherwise there it would not be possible to turn off the column view
222 // without focusing the first column.
223 setUrl(m_dirLister->url());
224 }
225
226 ViewProperties props(m_urlNavigator->url());
227 props.setViewMode(m_mode);
228
229 createView();
230 startDirLister(m_urlNavigator->url());
231
232 emit modeChanged();
233 }
234
235 DolphinView::Mode DolphinView::mode() const
236 {
237 return m_mode;
238 }
239
240 void DolphinView::setShowPreview(bool show)
241 {
242 ViewProperties props(m_urlNavigator->url());
243 props.setShowPreview(show);
244
245 m_controller->setShowPreview(show);
246 emit showPreviewChanged();
247
248 startDirLister(m_urlNavigator->url(), true);
249 }
250
251 bool DolphinView::showPreview() const
252 {
253 return m_controller->showPreview();
254 }
255
256 void DolphinView::setShowHiddenFiles(bool show)
257 {
258 if (m_dirLister->showingDotFiles() == show) {
259 return;
260 }
261
262 ViewProperties props(m_urlNavigator->url());
263 props.setShowHiddenFiles(show);
264
265 m_dirLister->setShowingDotFiles(show);
266 emit showHiddenFilesChanged();
267
268 startDirLister(m_urlNavigator->url(), true);
269 }
270
271 bool DolphinView::showHiddenFiles() const
272 {
273 return m_dirLister->showingDotFiles();
274 }
275
276 void DolphinView::setCategorizedSorting(bool categorized)
277 {
278 if (!supportsCategorizedSorting() || (categorized == categorizedSorting())) {
279 return;
280 }
281
282 Q_ASSERT(m_iconsView != 0);
283 if (categorized) {
284 Q_ASSERT(m_iconsView->itemCategorizer() == 0);
285 m_iconsView->setItemCategorizer(new DolphinItemCategorizer());
286 } else {
287 KItemCategorizer* categorizer = m_iconsView->itemCategorizer();
288 m_iconsView->setItemCategorizer(0);
289 delete categorizer;
290 }
291
292 ViewProperties props(m_urlNavigator->url());
293 props.setCategorizedSorting(categorized);
294 props.save();
295
296 emit categorizedSortingChanged();
297 }
298
299 bool DolphinView::categorizedSorting() const
300 {
301 if (!supportsCategorizedSorting()) {
302 return false;
303 }
304
305 Q_ASSERT(m_iconsView != 0);
306 return m_iconsView->itemCategorizer() != 0;
307 }
308
309 bool DolphinView::supportsCategorizedSorting() const
310 {
311 return m_iconsView != 0;
312 }
313
314 void DolphinView::renameSelectedItems()
315 {
316 DolphinView* view = mainWindow()->activeView();
317 const KUrl::List urls = selectedUrls();
318 if (urls.count() > 1) {
319 // More than one item has been selected for renaming. Open
320 // a rename dialog and rename all items afterwards.
321 RenameDialog dialog(urls);
322 if (dialog.exec() == QDialog::Rejected) {
323 return;
324 }
325
326 const QString& newName = dialog.newName();
327 if (newName.isEmpty()) {
328 view->statusBar()->setMessage(dialog.errorString(),
329 DolphinStatusBar::Error);
330 } else {
331 // TODO: check how this can be integrated into KonqUndoManager/KonqOperations
332 // as one operation instead of n rename operations like it is done now...
333 Q_ASSERT(newName.contains('#'));
334
335 // iterate through all selected items and rename them...
336 const int replaceIndex = newName.indexOf('#');
337 Q_ASSERT(replaceIndex >= 0);
338 int index = 1;
339
340 KUrl::List::const_iterator it = urls.begin();
341 KUrl::List::const_iterator end = urls.end();
342 while (it != end) {
343 const KUrl& oldUrl = *it;
344 QString number;
345 number.setNum(index++);
346
347 QString name(newName);
348 name.replace(replaceIndex, 1, number);
349
350 if (oldUrl.fileName() != name) {
351 KUrl newUrl = oldUrl;
352 newUrl.setFileName(name);
353 m_mainWindow->rename(oldUrl, newUrl);
354 }
355 ++it;
356 }
357 }
358 } else {
359 // Only one item has been selected for renaming. Use the custom
360 // renaming mechanism from the views.
361 Q_ASSERT(urls.count() == 1);
362
363 // TODO: Think about using KFileItemDelegate as soon as it supports editing.
364 // Currently the RenameDialog is used, but I'm not sure whether inline renaming
365 // is a benefit for the user at all -> let's wait for some input first...
366 RenameDialog dialog(urls);
367 if (dialog.exec() == QDialog::Rejected) {
368 return;
369 }
370
371 const QString& newName = dialog.newName();
372 if (newName.isEmpty()) {
373 view->statusBar()->setMessage(dialog.errorString(),
374 DolphinStatusBar::Error);
375 } else {
376 const KUrl& oldUrl = urls.first();
377 KUrl newUrl = oldUrl;
378 newUrl.setFileName(newName);
379 m_mainWindow->rename(oldUrl, newUrl);
380 }
381 }
382 }
383
384 void DolphinView::selectAll()
385 {
386 selectAll(QItemSelectionModel::Select);
387 }
388
389 void DolphinView::invertSelection()
390 {
391 selectAll(QItemSelectionModel::Toggle);
392 }
393
394 DolphinStatusBar* DolphinView::statusBar() const
395 {
396 return m_statusBar;
397 }
398
399 int DolphinView::contentsX() const
400 {
401 return itemView()->horizontalScrollBar()->value();
402 }
403
404 int DolphinView::contentsY() const
405 {
406 return itemView()->verticalScrollBar()->value();
407 }
408
409 void DolphinView::emitRequestItemInfo(const KUrl& url)
410 {
411 emit requestItemInfo(url);
412 }
413
414 bool DolphinView::isFilterBarVisible() const
415 {
416 return m_filterBar->isVisible();
417 }
418
419 bool DolphinView::isUrlEditable() const
420 {
421 return m_urlNavigator->isUrlEditable();
422 }
423
424 void DolphinView::zoomIn()
425 {
426 m_controller->triggerZoomIn();
427 }
428
429 void DolphinView::zoomOut()
430 {
431 m_controller->triggerZoomOut();
432 }
433
434 bool DolphinView::isZoomInPossible() const
435 {
436 return m_controller->isZoomInPossible();
437 }
438
439 bool DolphinView::isZoomOutPossible() const
440 {
441 return m_controller->isZoomOutPossible();
442 }
443
444 void DolphinView::setSorting(Sorting sorting)
445 {
446 if (sorting != this->sorting()) {
447 updateSorting(sorting);
448 }
449 }
450
451 DolphinView::Sorting DolphinView::sorting() const
452 {
453 return m_proxyModel->sorting();
454 }
455
456 void DolphinView::setSortOrder(Qt::SortOrder order)
457 {
458 if (sortOrder() != order) {
459 updateSortOrder(order);
460 }
461 }
462
463 Qt::SortOrder DolphinView::sortOrder() const
464 {
465 return m_proxyModel->sortOrder();
466 }
467
468 void DolphinView::setAdditionalInfo(KFileItemDelegate::AdditionalInformation info)
469 {
470 ViewProperties props(m_urlNavigator->url());
471 props.setAdditionalInfo(info);
472
473 m_controller->setShowAdditionalInfo(info != KFileItemDelegate::NoInformation);
474 m_fileItemDelegate->setAdditionalInformation(info);
475
476 emit additionalInfoChanged(info);
477 startDirLister(m_urlNavigator->url(), true);
478 }
479
480 KFileItemDelegate::AdditionalInformation DolphinView::additionalInfo() const
481 {
482 return m_fileItemDelegate->additionalInformation();
483 }
484
485 void DolphinView::goBack()
486 {
487 m_urlNavigator->goBack();
488 }
489
490 void DolphinView::goForward()
491 {
492 m_urlNavigator->goForward();
493 }
494
495 void DolphinView::goUp()
496 {
497 m_urlNavigator->goUp();
498 }
499
500 void DolphinView::goHome()
501 {
502 m_urlNavigator->goHome();
503 }
504
505 void DolphinView::setUrlEditable(bool editable)
506 {
507 m_urlNavigator->setUrlEditable(editable);
508 }
509
510 bool DolphinView::hasSelection() const
511 {
512 return itemView()->selectionModel()->hasSelection();
513 }
514
515 void DolphinView::clearSelection()
516 {
517 itemView()->selectionModel()->clear();
518 }
519
520 KFileItemList DolphinView::selectedItems() const
521 {
522 const QAbstractItemView* view = itemView();
523
524 // Our view has a selection, we will map them back to the DirModel
525 // and then fill the KFileItemList.
526 Q_ASSERT((view != 0) && (view->selectionModel() != 0));
527
528 const QItemSelection selection = m_proxyModel->mapSelectionToSource(view->selectionModel()->selection());
529 KFileItemList itemList;
530
531 const QModelIndexList indexList = selection.indexes();
532 QModelIndexList::const_iterator end = indexList.end();
533 for (QModelIndexList::const_iterator it = indexList.begin(); it != end; ++it) {
534 Q_ASSERT((*it).isValid());
535
536 KFileItem* item = m_dirModel->itemForIndex(*it);
537 if (item != 0) {
538 itemList.append(item);
539 }
540 }
541
542 return itemList;
543 }
544
545 KUrl::List DolphinView::selectedUrls() const
546 {
547 KUrl::List urls;
548
549 const KFileItemList list = selectedItems();
550 KFileItemList::const_iterator it = list.begin();
551 const KFileItemList::const_iterator end = list.end();
552 while (it != end) {
553 KFileItem* item = *it;
554 urls.append(item->url());
555 ++it;
556 }
557
558 return urls;
559 }
560
561 KFileItem* DolphinView::fileItem(const QModelIndex index) const
562 {
563 const QModelIndex dirModelIndex = m_proxyModel->mapToSource(index);
564 return m_dirModel->itemForIndex(dirModelIndex);
565 }
566
567 void DolphinView::rename(const KUrl& source, const QString& newName)
568 {
569 bool ok = false;
570
571 if (newName.isEmpty() || (source.fileName() == newName)) {
572 return;
573 }
574
575 KUrl dest(source.upUrl());
576 dest.addPath(newName);
577
578 const bool destExists = KIO::NetAccess::exists(dest,
579 false,
580 mainWindow()->activeView());
581 if (destExists) {
582 // the destination already exists, hence ask the user
583 // how to proceed...
584 KIO::RenameDialog renameDialog(this,
585 i18n("File Already Exists"),
586 source.path(),
587 dest.path(),
588 KIO::M_OVERWRITE);
589 switch (renameDialog.exec()) {
590 case KIO::R_OVERWRITE:
591 // the destination should be overwritten
592 ok = KIO::NetAccess::file_move(source, dest, -1, true);
593 break;
594
595 case KIO::R_RENAME: {
596 // a new name for the destination has been used
597 KUrl newDest(renameDialog.newDestUrl());
598 ok = KIO::NetAccess::file_move(source, newDest);
599 break;
600 }
601
602 default:
603 // the renaming operation has been canceled
604 return;
605 }
606 } else {
607 // no destination exists, hence just move the file to
608 // do the renaming
609 ok = KIO::NetAccess::file_move(source, dest);
610 }
611
612 const QString destFileName = dest.fileName();
613 if (ok) {
614 m_statusBar->setMessage(i18n("Renamed file '%1' to '%2'.", source.fileName(), destFileName),
615 DolphinStatusBar::OperationCompleted);
616
617 KonqOperations::rename(this, source, destFileName);
618 } else {
619 m_statusBar->setMessage(i18n("Renaming of file '%1' to '%2' failed.", source.fileName(), destFileName),
620 DolphinStatusBar::Error);
621 }
622 }
623
624 void DolphinView::reload()
625 {
626 const KUrl& url = m_urlNavigator->url();
627 changeDirectory(url);
628 startDirLister(url, true);
629 }
630
631 void DolphinView::refresh()
632 {
633 createView();
634 reload();
635 }
636
637 void DolphinView::mouseReleaseEvent(QMouseEvent* event)
638 {
639 QWidget::mouseReleaseEvent(event);
640 mainWindow()->setActiveView(this);
641 }
642
643 DolphinMainWindow* DolphinView::mainWindow() const
644 {
645 return m_mainWindow;
646 }
647
648 void DolphinView::changeDirectory(const KUrl& url)
649 {
650 if (!isActive()) {
651 requestActivation();
652 }
653
654 const ViewProperties props(url);
655
656 const Mode mode = props.viewMode();
657 bool changeMode = (m_mode != mode);
658 if (changeMode && isColumnViewActive()) {
659 // The column view is active. Only change the
660 // mode if the current URL is no child of the column view.
661 if (m_dirLister->url().isParentOf(url)) {
662 changeMode = false;
663 }
664 }
665
666 if (changeMode) {
667 m_mode = mode;
668 createView();
669 emit modeChanged();
670
671 if (m_mode == ColumnView) {
672 // The mode has been changed to the Column View. When starting the dir
673 // lister with DolphinView::startDirLister() it is important to give a
674 // hint that the dir lister may not keep the current directory
675 // although this is the default for showing a hierarchy.
676 m_initializeColumnView = true;
677 }
678 }
679
680 const bool showHiddenFiles = props.showHiddenFiles();
681 if (showHiddenFiles != m_dirLister->showingDotFiles()) {
682 m_dirLister->setShowingDotFiles(showHiddenFiles);
683 emit showHiddenFilesChanged();
684 }
685
686 const bool categorized = props.categorizedSorting();
687 if (categorized != categorizedSorting()) {
688 if (supportsCategorizedSorting()) {
689 Q_ASSERT(m_iconsView != 0);
690 if (categorized) {
691 Q_ASSERT(m_iconsView->itemCategorizer() == 0);
692 m_iconsView->setItemCategorizer(new DolphinItemCategorizer());
693 } else {
694 KItemCategorizer* categorizer = m_iconsView->itemCategorizer();
695 m_iconsView->setItemCategorizer(0);
696 delete categorizer;
697 }
698 }
699 emit categorizedSortingChanged();
700 }
701
702 const DolphinView::Sorting sorting = props.sorting();
703 if (sorting != m_proxyModel->sorting()) {
704 m_proxyModel->setSorting(sorting);
705 emit sortingChanged(sorting);
706 }
707
708 const Qt::SortOrder sortOrder = props.sortOrder();
709 if (sortOrder != m_proxyModel->sortOrder()) {
710 m_proxyModel->setSortOrder(sortOrder);
711 emit sortOrderChanged(sortOrder);
712 }
713
714 KFileItemDelegate::AdditionalInformation info = props.additionalInfo();
715 if (info != m_fileItemDelegate->additionalInformation()) {
716 m_controller->setShowAdditionalInfo(info != KFileItemDelegate::NoInformation);
717 m_fileItemDelegate->setAdditionalInformation(info);
718 emit additionalInfoChanged(info);
719 }
720
721 const bool showPreview = props.showPreview();
722 if (showPreview != m_controller->showPreview()) {
723 m_controller->setShowPreview(showPreview);
724 emit showPreviewChanged();
725 }
726
727 startDirLister(url);
728 emit urlChanged(url);
729
730 m_statusBar->clear();
731 }
732
733 void DolphinView::triggerItem(const QModelIndex& index)
734 {
735 if (!isValidNameIndex(index)) {
736 return;
737 }
738
739 const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();
740 if ((modifier & Qt::ShiftModifier) || (modifier & Qt::ControlModifier)) {
741 // items are selected by the user, hence don't trigger the
742 // item specified by 'index'
743 return;
744 }
745
746 KFileItem* item = m_dirModel->itemForIndex(m_proxyModel->mapToSource(index));
747 if (item == 0) {
748 return;
749 }
750
751 // Prefer the local path over the URL. This assures that the
752 // volume space information is correct. Assuming that the URL is media:/sda1,
753 // and the local path is /windows/C: For the URL the space info is related
754 // to the root partition (and hence wrong) and for the local path the space
755 // info is related to the windows partition (-> correct).
756 const QString localPath(item->localPath());
757 KUrl url;
758 if (localPath.isEmpty()) {
759 url = item->url();
760 } else {
761 url = localPath;
762 }
763
764 if (item->isDir()) {
765 setUrl(url);
766 } else if (item->isFile()) {
767 // allow to browse through ZIP and tar files
768 KMimeType::Ptr mime = item->mimeTypePtr();
769 if (mime->is("application/zip")) {
770 url.setProtocol("zip");
771 setUrl(url);
772 } else if (mime->is("application/x-tar") ||
773 mime->is("application/x-tarz") ||
774 mime->is("application/x-bzip-compressed-tar") ||
775 mime->is("application/x-compressed-tar") ||
776 mime->is("application/x-tzo")) {
777 url.setProtocol("tar");
778 setUrl(url);
779 } else {
780 item->run();
781 }
782 } else {
783 item->run();
784 }
785 }
786
787 void DolphinView::updateProgress(int percent)
788 {
789 if (m_showProgress) {
790 m_statusBar->setProgress(percent);
791 }
792 }
793
794 void DolphinView::updateItemCount()
795 {
796 if (m_showProgress) {
797 m_statusBar->setProgressText(QString());
798 m_statusBar->setProgress(100);
799 m_showProgress = false;
800 }
801
802 KFileItemList items(m_dirLister->items());
803 KFileItemList::const_iterator it = items.begin();
804 const KFileItemList::const_iterator end = items.end();
805
806 m_fileCount = 0;
807 m_folderCount = 0;
808
809 while (it != end) {
810 KFileItem* item = *it;
811 if (item->isDir()) {
812 ++m_folderCount;
813 } else {
814 ++m_fileCount;
815 }
816 ++it;
817 }
818
819 updateStatusBar();
820
821 m_blockContentsMovedSignal = false;
822 QTimer::singleShot(0, this, SLOT(restoreContentsPos()));
823 }
824
825 void DolphinView::generatePreviews(const KFileItemList& items)
826 {
827 if (m_controller->showPreview()) {
828
829 // Must turn QList<KFileItem *> to QList<KFileItem>...
830 QList<KFileItem> itemsToPreview;
831 foreach( KFileItem* it, items )
832 itemsToPreview.append( *it );
833
834 KIO::PreviewJob* job = KIO::filePreview(itemsToPreview, 128);
835 connect(job, SIGNAL(gotPreview(const KFileItem&, const QPixmap&)),
836 this, SLOT(showPreview(const KFileItem&, const QPixmap&)));
837 }
838 }
839
840 void DolphinView::showPreview(const KFileItem& item, const QPixmap& pixmap)
841 {
842 Q_ASSERT(!item.isNull());
843 if (item.url().directory() != m_dirLister->url().path()) {
844 // the preview job is still working on items of an older URL, hence
845 // the item is not part of the directory model anymore
846 return;
847 }
848
849 const QModelIndex idx = m_dirModel->indexForItem(item);
850 if (idx.isValid() && (idx.column() == 0)) {
851 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
852 if (KonqMimeData::decodeIsCutSelection(mimeData) && isCutItem(item)) {
853 KIconEffect iconEffect;
854 const QPixmap cutPixmap = iconEffect.apply(pixmap, K3Icon::Desktop, K3Icon::DisabledState);
855 m_dirModel->setData(idx, QIcon(cutPixmap), Qt::DecorationRole);
856 } else {
857 m_dirModel->setData(idx, QIcon(pixmap), Qt::DecorationRole);
858 }
859 }
860 }
861
862 void DolphinView::restoreContentsPos()
863 {
864 KUrl currentUrl = m_urlNavigator->url();
865 if (!currentUrl.isEmpty()) {
866 QAbstractItemView* view = itemView();
867 // TODO: view->setCurrentItem(m_urlNavigator->currentFileName());
868 QPoint pos = m_urlNavigator->savedPosition();
869 view->horizontalScrollBar()->setValue(pos.x());
870 view->verticalScrollBar()->setValue(pos.y());
871 }
872 }
873
874 void DolphinView::showInfoMessage(const QString& msg)
875 {
876 m_statusBar->setMessage(msg, DolphinStatusBar::Information);
877 }
878
879 void DolphinView::showErrorMessage(const QString& msg)
880 {
881 m_statusBar->setMessage(msg, DolphinStatusBar::Error);
882 }
883
884 void DolphinView::emitSelectionChangedSignal()
885 {
886 emit selectionChanged(DolphinView::selectedItems());
887 }
888
889 void DolphinView::closeFilterBar()
890 {
891 m_filterBar->hide();
892 emit showFilterBarChanged(false);
893 }
894
895 void DolphinView::startDirLister(const KUrl& url, bool reload)
896 {
897 if (!url.isValid()) {
898 const QString location(url.pathOrUrl());
899 if (location.isEmpty()) {
900 m_statusBar->setMessage(i18n("The location is empty."), DolphinStatusBar::Error);
901 } else {
902 m_statusBar->setMessage(i18n("The location '%1' is invalid.", location),
903 DolphinStatusBar::Error);
904 }
905 return;
906 }
907
908 // Only show the directory loading progress if the status bar does
909 // not contain another progress information. This means that
910 // the directory loading progress information has the lowest priority.
911 const QString progressText(m_statusBar->progressText());
912 m_showProgress = progressText.isEmpty() ||
913 (progressText == i18n("Loading folder..."));
914 if (m_showProgress) {
915 m_statusBar->setProgressText(i18n("Loading folder..."));
916 m_statusBar->setProgress(0);
917 }
918
919 m_cutItemsCache.clear();
920 m_blockContentsMovedSignal = true;
921 m_dirLister->stop();
922
923 bool openDir = true;
924 bool keepOldDirs = isColumnViewActive() && !m_initializeColumnView;
925 m_initializeColumnView = false;
926
927 if (keepOldDirs) {
928 if (reload) {
929 keepOldDirs = false;
930
931 const KUrl& dirListerUrl = m_dirLister->url();
932 if (dirListerUrl.isValid()) {
933 const KUrl::List dirs = m_dirLister->directories();
934 KUrl url;
935 foreach(url, dirs) {
936 m_dirLister->updateDirectory(url);
937 }
938 openDir = false;
939 }
940 } else if (m_dirLister->directories().contains(url)) {
941 // The dir lister contains the directory already, so
942 // KDirLister::openUrl() may not been invoked twice.
943 m_dirLister->updateDirectory(url);
944 openDir = false;
945 } else {
946 const KUrl& dirListerUrl = m_dirLister->url();
947 if ((dirListerUrl == url) || !m_dirLister->url().isParentOf(url)) {
948 // The current URL is not a child of the dir lister
949 // URL. This may happen when e. g. a bookmark has been selected
950 // and hence the view must be reset.
951 keepOldDirs = false;
952 }
953 }
954 }
955
956 if (openDir) {
957 m_dirLister->openUrl(url, keepOldDirs, reload);
958 }
959 }
960
961 QString DolphinView::defaultStatusBarText() const
962 {
963 return KIO::itemsSummaryString(m_fileCount + m_folderCount,
964 m_fileCount,
965 m_folderCount,
966 0, false);
967 }
968
969 QString DolphinView::selectionStatusBarText() const
970 {
971 QString text;
972 const KFileItemList list = selectedItems();
973 if (list.isEmpty()) {
974 // when an item is triggered, it is temporary selected but selectedItems()
975 // will return an empty list
976 return QString();
977 }
978
979 int fileCount = 0;
980 int folderCount = 0;
981 KIO::filesize_t byteSize = 0;
982 KFileItemList::const_iterator it = list.begin();
983 const KFileItemList::const_iterator end = list.end();
984 while (it != end) {
985 KFileItem* item = *it;
986 if (item->isDir()) {
987 ++folderCount;
988 } else {
989 ++fileCount;
990 byteSize += item->size();
991 }
992 ++it;
993 }
994
995 if (folderCount > 0) {
996 text = i18np("1 Folder selected", "%1 Folders selected", folderCount);
997 if (fileCount > 0) {
998 text += ", ";
999 }
1000 }
1001
1002 if (fileCount > 0) {
1003 const QString sizeText(KIO::convertSize(byteSize));
1004 text += i18np("1 File selected (%2)", "%1 Files selected (%2)", fileCount, sizeText);
1005 }
1006
1007 return text;
1008 }
1009
1010 void DolphinView::showFilterBar(bool show)
1011 {
1012 Q_ASSERT(m_filterBar != 0);
1013 if (show) {
1014 m_filterBar->show();
1015 } else {
1016 m_filterBar->hide();
1017 }
1018 }
1019
1020 void DolphinView::updateStatusBar()
1021 {
1022 // As the item count information is less important
1023 // in comparison with other messages, it should only
1024 // be shown if:
1025 // - the status bar is empty or
1026 // - shows already the item count information or
1027 // - shows only a not very important information
1028 // - if any progress is given don't show the item count info at all
1029 const QString msg(m_statusBar->message());
1030 const bool updateStatusBarMsg = (msg.isEmpty() ||
1031 (msg == m_statusBar->defaultText()) ||
1032 (m_statusBar->type() == DolphinStatusBar::Information)) &&
1033 (m_statusBar->progress() == 100);
1034
1035 const QString text(hasSelection() ? selectionStatusBarText() : defaultStatusBarText());
1036 m_statusBar->setDefaultText(text);
1037
1038 if (updateStatusBarMsg) {
1039 m_statusBar->setMessage(text, DolphinStatusBar::Default);
1040 }
1041 }
1042
1043 void DolphinView::requestActivation()
1044 {
1045 m_mainWindow->setActiveView(this);
1046 }
1047
1048 void DolphinView::changeSelection(const KFileItemList& selection)
1049 {
1050 clearSelection();
1051 if (selection.isEmpty()) {
1052 return;
1053 }
1054 KUrl baseUrl = url();
1055 KUrl url;
1056 QItemSelection new_selection;
1057 foreach(KFileItem* item, selection) {
1058 url = item->url().upUrl();
1059 if (baseUrl.equals(url, KUrl::CompareWithoutTrailingSlash)) {
1060 QModelIndex index = m_proxyModel->mapFromSource(m_dirModel->indexForItem(*item));
1061 new_selection.select(index, index);
1062 }
1063 }
1064 itemView()->selectionModel()->select(new_selection,
1065 QItemSelectionModel::ClearAndSelect
1066 | QItemSelectionModel::Current);
1067 }
1068
1069 void DolphinView::changeNameFilter(const QString& nameFilter)
1070 {
1071 // The name filter of KDirLister does a 'hard' filtering, which
1072 // means that only the items are shown where the names match
1073 // exactly the filter. This is non-transparent for the user, which
1074 // just wants to have a 'soft' filtering: does the name contain
1075 // the filter string?
1076 QString adjustedFilter(nameFilter);
1077 adjustedFilter.insert(0, '*');
1078 adjustedFilter.append('*');
1079
1080 // Use the ProxyModel to filter:
1081 // This code is #ifdefed as setNameFilter behaves
1082 // slightly different than the QSortFilterProxyModel
1083 // as it will not remove directories. I will ask
1084 // our beloved usability experts for input
1085 // -- z.
1086 #if 0
1087 m_dirLister->setNameFilter(adjustedFilter);
1088 m_dirLister->emitChanges();
1089 #else
1090 m_proxyModel->setFilterRegExp(nameFilter);
1091 #endif
1092 }
1093
1094 void DolphinView::openContextMenu(const QPoint& pos)
1095 {
1096 KFileItem* item = 0;
1097
1098 const QModelIndex index = itemView()->indexAt(pos);
1099 if (isValidNameIndex(index)) {
1100 item = fileItem(index);
1101 }
1102
1103 DolphinContextMenu contextMenu(m_mainWindow, item, url());
1104 contextMenu.open();
1105 }
1106
1107 void DolphinView::dropUrls(const KUrl::List& urls,
1108 const QModelIndex& index,
1109 QWidget* source)
1110 {
1111 KFileItem* directory = 0;
1112 if (isValidNameIndex(index)) {
1113 KFileItem* item = fileItem(index);
1114 Q_ASSERT(item != 0);
1115 if (item->isDir()) {
1116 // the URLs are dropped above a directory
1117 directory = item;
1118 }
1119 }
1120
1121 if ((directory == 0) && (source == itemView())) {
1122 // The dropping is done into the same viewport where
1123 // the dragging has been started. Just ignore this...
1124 return;
1125 }
1126
1127 const KUrl& destination = (directory == 0) ? url() :
1128 directory->url();
1129 dropUrls(urls, destination);
1130 }
1131
1132 void DolphinView::dropUrls(const KUrl::List& urls,
1133 const KUrl& destination)
1134 {
1135 m_mainWindow->dropUrls(urls, destination);
1136 }
1137
1138 void DolphinView::updateSorting(DolphinView::Sorting sorting)
1139 {
1140 ViewProperties props(url());
1141 props.setSorting(sorting);
1142
1143 m_proxyModel->setSorting(sorting);
1144
1145 emit sortingChanged(sorting);
1146 }
1147
1148 void DolphinView::updateSortOrder(Qt::SortOrder order)
1149 {
1150 ViewProperties props(url());
1151 props.setSortOrder(order);
1152
1153 m_proxyModel->setSortOrder(order);
1154
1155 emit sortOrderChanged(order);
1156 }
1157
1158 void DolphinView::emitContentsMoved()
1159 {
1160 if (!m_blockContentsMovedSignal) {
1161 emit contentsMoved(contentsX(), contentsY());
1162 }
1163 }
1164
1165 void DolphinView::updateActivationState()
1166 {
1167 m_urlNavigator->setActive(isActive());
1168
1169 QColor color = KGlobalSettings::baseColor();
1170 if (isActive()) {
1171 emit urlChanged(url());
1172 emit selectionChanged(selectedItems());
1173 } else {
1174 // darken the background if the view is inactive
1175 // TODO: does not work for a black background
1176 color = color.darker(105);
1177 }
1178
1179 QWidget* viewport = itemView()->viewport();
1180 QPalette palette;
1181 palette.setColor(viewport->backgroundRole(), color);
1182 viewport->setPalette(palette);
1183
1184 update();
1185 }
1186
1187 void DolphinView::updateCutItems()
1188 {
1189 // restore the icons of all previously selected items to the
1190 // original state...
1191 QList<CutItem>::const_iterator it = m_cutItemsCache.begin();
1192 QList<CutItem>::const_iterator end = m_cutItemsCache.end();
1193 while (it != end) {
1194 const QModelIndex index = m_dirModel->indexForUrl((*it).url);
1195 if (index.isValid()) {
1196 m_dirModel->setData(index, QIcon((*it).pixmap), Qt::DecorationRole);
1197 }
1198 ++it;
1199 }
1200 m_cutItemsCache.clear();
1201
1202 // ... and apply an item effect to all currently cut items
1203 applyCutItemEffect();
1204 }
1205
1206 void DolphinView::showHoverInformation(const QModelIndex& index)
1207 {
1208 if (hasSelection()) {
1209 return;
1210 }
1211
1212 const KFileItem* item = fileItem(index);
1213 if (item != 0) {
1214 m_statusBar->setMessage(item->getStatusBarInfo(), DolphinStatusBar::Default);
1215 emit requestItemInfo(item->url());
1216 }
1217 }
1218
1219 void DolphinView::clearHoverInformation()
1220 {
1221 m_statusBar->clear();
1222 }
1223
1224
1225 void DolphinView::createView()
1226 {
1227 // delete current view
1228 QAbstractItemView* view = itemView();
1229 if (view != 0) {
1230 m_topLayout->removeWidget(view);
1231 view->close();
1232 if (view == m_iconsView) {
1233 KItemCategorizer* categorizer = m_iconsView->itemCategorizer();
1234 m_iconsView->setItemCategorizer(0);
1235 delete categorizer;
1236 }
1237 view->deleteLater();
1238 view = 0;
1239 m_iconsView = 0;
1240 m_detailsView = 0;
1241 m_columnView = 0;
1242 m_fileItemDelegate = 0;
1243 }
1244
1245 Q_ASSERT(m_iconsView == 0);
1246 Q_ASSERT(m_detailsView == 0);
1247 Q_ASSERT(m_columnView == 0);
1248
1249 // ... and recreate it representing the current mode
1250 switch (m_mode) {
1251 case IconsView:
1252 m_iconsView = new DolphinIconsView(this, m_controller);
1253 view = m_iconsView;
1254 break;
1255
1256 case DetailsView:
1257 m_detailsView = new DolphinDetailsView(this, m_controller);
1258 view = m_detailsView;
1259 break;
1260
1261 case ColumnView:
1262 m_columnView = new DolphinColumnView(this, m_controller);
1263 view = m_columnView;
1264 break;
1265 }
1266
1267 Q_ASSERT(view != 0);
1268
1269 m_fileItemDelegate = new KFileItemDelegate(view);
1270 view->setItemDelegate(m_fileItemDelegate);
1271
1272 view->setModel(m_proxyModel);
1273 view->setSelectionMode(QAbstractItemView::ExtendedSelection);
1274
1275 new KMimeTypeResolver(view, m_dirModel);
1276 m_topLayout->insertWidget(1, view);
1277
1278 connect(view->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
1279 m_controller, SLOT(indicateSelectionChange()));
1280 connect(view->verticalScrollBar(), SIGNAL(valueChanged(int)),
1281 this, SLOT(emitContentsMoved()));
1282 connect(view->horizontalScrollBar(), SIGNAL(valueChanged(int)),
1283 this, SLOT(emitContentsMoved()));
1284 }
1285
1286 void DolphinView::selectAll(QItemSelectionModel::SelectionFlags flags)
1287 {
1288 QItemSelectionModel* selectionModel = itemView()->selectionModel();
1289 const QAbstractItemModel* itemModel = selectionModel->model();
1290
1291 const QModelIndex topLeft = itemModel->index(0, 0);
1292 const QModelIndex bottomRight = itemModel->index(itemModel->rowCount() - 1,
1293 itemModel->columnCount() - 1);
1294
1295 QItemSelection selection(topLeft, bottomRight);
1296 selectionModel->select(selection, flags);
1297 }
1298
1299 QAbstractItemView* DolphinView::itemView() const
1300 {
1301 if (m_detailsView != 0) {
1302 return m_detailsView;
1303 } else if (m_columnView != 0) {
1304 return m_columnView;
1305 }
1306
1307 return m_iconsView;
1308 }
1309
1310 bool DolphinView::isValidNameIndex(const QModelIndex& index) const
1311 {
1312 return index.isValid() && (index.column() == KDirModel::Name);
1313 }
1314
1315 bool DolphinView::isCutItem(const KFileItem& item) const
1316 {
1317 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
1318 const KUrl::List cutUrls = KUrl::List::fromMimeData(mimeData);
1319
1320 const KUrl& itemUrl = item.url();
1321 KUrl::List::const_iterator it = cutUrls.begin();
1322 const KUrl::List::const_iterator end = cutUrls.end();
1323 while (it != end) {
1324 if (*it == itemUrl) {
1325 return true;
1326 }
1327 ++it;
1328 }
1329
1330 return false;
1331 }
1332
1333 void DolphinView::applyCutItemEffect()
1334 {
1335 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
1336 if (!KonqMimeData::decodeIsCutSelection(mimeData)) {
1337 return;
1338 }
1339
1340 KFileItemList items(m_dirLister->items());
1341 KFileItemList::const_iterator it = items.begin();
1342 const KFileItemList::const_iterator end = items.end();
1343 while (it != end) {
1344 KFileItem* item = *it;
1345 if (isCutItem(*item)) {
1346 const QModelIndex index = m_dirModel->indexForItem(*item);
1347 const KFileItem* item = m_dirModel->itemForIndex(index);
1348 const QVariant value = m_dirModel->data(index, Qt::DecorationRole);
1349 if ((value.type() == QVariant::Icon) && (item != 0)) {
1350 const QIcon icon(qvariant_cast<QIcon>(value));
1351 QPixmap pixmap = icon.pixmap(128, 128);
1352
1353 // remember current pixmap for the item to be able
1354 // to restore it when other items get cut
1355 CutItem cutItem;
1356 cutItem.url = item->url();
1357 cutItem.pixmap = pixmap;
1358 m_cutItemsCache.append(cutItem);
1359
1360 // apply icon effect to the cut item
1361 KIconEffect iconEffect;
1362 pixmap = iconEffect.apply(pixmap, K3Icon::Desktop, K3Icon::DisabledState);
1363 m_dirModel->setData(index, QIcon(pixmap), Qt::DecorationRole);
1364 }
1365 }
1366 ++it;
1367 }
1368 }
1369
1370 #include "dolphinview.moc"