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