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