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