]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinview.cpp
differ between pasting one folder, pasting one file and pasting n items
[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 <QKeyEvent>
26 #include <QItemSelection>
27 #include <QBoxLayout>
28 #include <QTimer>
29 #include <QScrollBar>
30
31 #include <kactioncollection.h>
32 #include <kcolorscheme.h>
33 #include <kdirlister.h>
34 #include <kfileitemdelegate.h>
35 #include <kiconeffect.h>
36 #include <klocale.h>
37 #include <kio/deletejob.h>
38 #include <kio/netaccess.h>
39 #include <kio/previewjob.h>
40 #include <kjob.h>
41 #include <kmenu.h>
42 #include <kmessagebox.h>
43 #include <kmimetyperesolver.h>
44 #include <konq_operations.h>
45 #include <konqmimedata.h>
46 #include <ktoggleaction.h>
47 #include <kurl.h>
48
49 #include "dolphindropcontroller.h"
50 #include "dolphinmodel.h"
51 #include "dolphincolumnview.h"
52 #include "dolphincontroller.h"
53 #include "dolphinsortfilterproxymodel.h"
54 #include "dolphindetailsview.h"
55 #include "dolphiniconsview.h"
56 #include "dolphinsettings.h"
57 #include "dolphin_generalsettings.h"
58 #include "iconmanager.h"
59 #include "renamedialog.h"
60 #include "viewproperties.h"
61
62 DolphinView::DolphinView(QWidget* parent,
63 const KUrl& url,
64 KDirLister* dirLister,
65 DolphinModel* dolphinModel,
66 DolphinSortFilterProxyModel* proxyModel) :
67 QWidget(parent),
68 m_active(true),
69 m_showPreview(false),
70 m_loadingDirectory(false),
71 m_storedCategorizedSorting(false),
72 m_mode(DolphinView::IconsView),
73 m_topLayout(0),
74 m_controller(0),
75 m_iconsView(0),
76 m_detailsView(0),
77 m_columnView(0),
78 m_fileItemDelegate(0),
79 m_selectionModel(0),
80 m_dolphinModel(dolphinModel),
81 m_dirLister(dirLister),
82 m_proxyModel(proxyModel),
83 m_iconManager(0)
84 {
85 setFocusPolicy(Qt::StrongFocus);
86 m_topLayout = new QVBoxLayout(this);
87 m_topLayout->setSpacing(0);
88 m_topLayout->setMargin(0);
89
90 m_controller = new DolphinController(this);
91 m_controller->setUrl(url);
92
93 // Receiver of the DolphinView signal 'urlChanged()' don't need
94 // to care whether the internal controller changed the URL already or whether
95 // the controller just requested an URL change and will be updated later.
96 // In both cases the URL has been changed:
97 connect(m_controller, SIGNAL(urlChanged(const KUrl&)),
98 this, SIGNAL(urlChanged(const KUrl&)));
99 connect(m_controller, SIGNAL(requestUrlChange(const KUrl&)),
100 this, SIGNAL(urlChanged(const KUrl&)));
101
102 connect(m_controller, SIGNAL(requestContextMenu(const QPoint&)),
103 this, SLOT(openContextMenu(const QPoint&)));
104 connect(m_controller, SIGNAL(urlsDropped(const KUrl::List&, const KUrl&, const KFileItem&)),
105 this, SLOT(dropUrls(const KUrl::List&, const KUrl&, const KFileItem&)));
106 connect(m_controller, SIGNAL(sortingChanged(DolphinView::Sorting)),
107 this, SLOT(updateSorting(DolphinView::Sorting)));
108 connect(m_controller, SIGNAL(sortOrderChanged(Qt::SortOrder)),
109 this, SLOT(updateSortOrder(Qt::SortOrder)));
110 connect(m_controller, SIGNAL(additionalInfoChanged(const KFileItemDelegate::InformationList&)),
111 this, SLOT(updateAdditionalInfo(const KFileItemDelegate::InformationList&)));
112 connect(m_controller, SIGNAL(itemTriggered(const KFileItem&)),
113 this, SLOT(triggerItem(const KFileItem&)));
114 connect(m_controller, SIGNAL(activated()),
115 this, SLOT(activate()));
116 connect(m_controller, SIGNAL(itemEntered(const KFileItem&)),
117 this, SLOT(showHoverInformation(const KFileItem&)));
118 connect(m_controller, SIGNAL(viewportEntered()),
119 this, SLOT(clearHoverInformation()));
120
121 applyViewProperties(url);
122 m_topLayout->addWidget(itemView());
123 }
124
125 DolphinView::~DolphinView()
126 {
127 }
128
129 const KUrl& DolphinView::url() const
130 {
131 return m_controller->url();
132 }
133
134 KUrl DolphinView::rootUrl() const
135 {
136 return isColumnViewActive() ? m_columnView->rootUrl() : url();
137 }
138
139 void DolphinView::setActive(bool active)
140 {
141 if (active == m_active) {
142 return;
143 }
144
145 m_active = active;
146 m_selectionModel->clearSelection();
147
148 QColor color = KColorScheme(QPalette::Active, KColorScheme::View).background().color();
149 if (active) {
150 // TODO: emitting urlChanged() is a hack, as the URL hasn't really changed. It
151 // bypasses the problem when having a split view and changing the active view to
152 // update the some URL dependent states. A nicer approach should be no big deal...
153 emit urlChanged(url());
154 emit selectionChanged(selectedItems());
155 } else {
156 color.setAlpha(150);
157 }
158
159 QWidget* viewport = itemView()->viewport();
160 QPalette palette;
161 palette.setColor(viewport->backgroundRole(), color);
162 viewport->setPalette(palette);
163
164 update();
165
166 if (active) {
167 emit activated();
168 }
169
170 m_controller->indicateActivationChange(active);
171 }
172
173 bool DolphinView::isActive() const
174 {
175 return m_active;
176 }
177
178 void DolphinView::setMode(Mode mode)
179 {
180 if (mode == m_mode) {
181 return; // the wished mode is already set
182 }
183
184 m_mode = mode;
185
186 deleteView();
187
188 const KUrl viewPropsUrl = viewPropertiesUrl();
189 ViewProperties props(viewPropsUrl);
190 props.setViewMode(m_mode);
191 createView();
192
193 // the file item delegate has been recreated, apply the current
194 // additional information manually
195 const KFileItemDelegate::InformationList infoList = props.additionalInfo();
196 m_fileItemDelegate->setShowInformation(infoList);
197 emit additionalInfoChanged();
198
199 // Not all view modes support categorized sorting. Adjust the sorting model
200 // if changing the view mode results in a change of the categorized sorting
201 // capabilities.
202 m_storedCategorizedSorting = props.categorizedSorting();
203 const bool categorized = m_storedCategorizedSorting && supportsCategorizedSorting();
204 if (categorized != m_proxyModel->isCategorizedModel()) {
205 m_proxyModel->setCategorizedModel(categorized);
206 emit categorizedSortingChanged();
207 }
208
209 emit modeChanged();
210 }
211
212 DolphinView::Mode DolphinView::mode() const
213 {
214 return m_mode;
215 }
216
217 void DolphinView::setShowPreview(bool show)
218 {
219 if (m_showPreview == show) {
220 return;
221 }
222
223 const KUrl viewPropsUrl = viewPropertiesUrl();
224 ViewProperties props(viewPropsUrl);
225 props.setShowPreview(show);
226
227 m_showPreview = show;
228 m_iconManager->setShowPreview(show);
229 emit showPreviewChanged();
230
231 loadDirectory(viewPropsUrl);
232 }
233
234 bool DolphinView::showPreview() const
235 {
236 return m_showPreview;
237 }
238
239 void DolphinView::setShowHiddenFiles(bool show)
240 {
241 if (m_dirLister->showingDotFiles() == show) {
242 return;
243 }
244
245 const KUrl viewPropsUrl = viewPropertiesUrl();
246 ViewProperties props(viewPropsUrl);
247 props.setShowHiddenFiles(show);
248
249 m_dirLister->setShowingDotFiles(show);
250 emit showHiddenFilesChanged();
251
252 loadDirectory(viewPropsUrl);
253 }
254
255 bool DolphinView::showHiddenFiles() const
256 {
257 return m_dirLister->showingDotFiles();
258 }
259
260 void DolphinView::setCategorizedSorting(bool categorized)
261 {
262 if (categorized == categorizedSorting()) {
263 return;
264 }
265
266 // setCategorizedSorting(true) may only get invoked
267 // if the view supports categorized sorting
268 Q_ASSERT(!categorized || supportsCategorizedSorting());
269
270 ViewProperties props(viewPropertiesUrl());
271 props.setCategorizedSorting(categorized);
272 props.save();
273
274 m_storedCategorizedSorting = categorized;
275 m_proxyModel->setCategorizedModel(categorized);
276
277 emit categorizedSortingChanged();
278 }
279
280 bool DolphinView::categorizedSorting() const
281 {
282 // If all view modes would support categorized sorting, returning
283 // m_proxyModel->isCategorizedModel() would be the way to go. As
284 // currently only the icons view supports caterized sorting, we remember
285 // the stored view properties state in m_storedCategorizedSorting and
286 // return this state. The application takes care to disable the corresponding
287 // checkbox by checking DolphinView::supportsCategorizedSorting() to indicate
288 // that this setting is not applied to the current view mode.
289 return m_storedCategorizedSorting;
290 }
291
292 bool DolphinView::supportsCategorizedSorting() const
293 {
294 return m_iconsView != 0;
295 }
296
297 void DolphinView::selectAll()
298 {
299 QAbstractItemView* view = itemView();
300 // TODO: there seems to be a bug in QAbstractItemView::selectAll(); if
301 // the Ctrl-key is pressed (e. g. for Ctrl+A), selectAll() inverts the
302 // selection instead of selecting all items. This is bypassed for KDE 4.0
303 // by invoking clearSelection() first.
304 view->clearSelection();
305 view->selectAll();
306 }
307
308 void DolphinView::invertSelection()
309 {
310 if (isColumnViewActive()) {
311 // QAbstractItemView does not offer a virtual method invertSelection()
312 // as counterpart to QAbstractItemView::selectAll(). This makes it
313 // necessary to delegate the inverting of the selection to the
314 // column view, as only the selection of the active column should
315 // get inverted.
316 m_columnView->invertSelection();
317 } else {
318 QItemSelectionModel* selectionModel = itemView()->selectionModel();
319 const QAbstractItemModel* itemModel = selectionModel->model();
320
321 const QModelIndex topLeft = itemModel->index(0, 0);
322 const QModelIndex bottomRight = itemModel->index(itemModel->rowCount() - 1,
323 itemModel->columnCount() - 1);
324
325 const QItemSelection selection(topLeft, bottomRight);
326 selectionModel->select(selection, QItemSelectionModel::Toggle);
327 }
328 }
329
330 bool DolphinView::hasSelection() const
331 {
332 return itemView()->selectionModel()->hasSelection();
333 }
334
335 void DolphinView::clearSelection()
336 {
337 itemView()->selectionModel()->clear();
338 }
339
340 KFileItemList DolphinView::selectedItems() const
341 {
342 const QAbstractItemView* view = itemView();
343
344 // Our view has a selection, we will map them back to the DolphinModel
345 // and then fill the KFileItemList.
346 Q_ASSERT((view != 0) && (view->selectionModel() != 0));
347
348 const QItemSelection selection = m_proxyModel->mapSelectionToSource(view->selectionModel()->selection());
349 KFileItemList itemList;
350
351 const QModelIndexList indexList = selection.indexes();
352 foreach (QModelIndex index, indexList) {
353 KFileItem item = m_dolphinModel->itemForIndex(index);
354 if (!item.isNull()) {
355 itemList.append(item);
356 }
357 }
358
359 return itemList;
360 }
361
362 KUrl::List DolphinView::selectedUrls() const
363 {
364 KUrl::List urls;
365 const KFileItemList list = selectedItems();
366 foreach (KFileItem item, list) {
367 urls.append(item.url());
368 }
369 return urls;
370 }
371
372 KFileItem DolphinView::fileItem(const QModelIndex& index) const
373 {
374 const QModelIndex dolphinModelIndex = m_proxyModel->mapToSource(index);
375 return m_dolphinModel->itemForIndex(dolphinModelIndex);
376 }
377
378 void DolphinView::setContentsPosition(int x, int y)
379 {
380 QAbstractItemView* view = itemView();
381
382 // the ColumnView takes care itself for the horizontal scrolling
383 if (!isColumnViewActive()) {
384 view->horizontalScrollBar()->setValue(x);
385 }
386 view->verticalScrollBar()->setValue(y);
387
388 m_loadingDirectory = false;
389 }
390
391 QPoint DolphinView::contentsPosition() const
392 {
393 const int x = itemView()->horizontalScrollBar()->value();
394 const int y = itemView()->verticalScrollBar()->value();
395 return QPoint(x, y);
396 }
397
398 void DolphinView::zoomIn()
399 {
400 m_controller->triggerZoomIn();
401 m_iconManager->updatePreviews();
402 }
403
404 void DolphinView::zoomOut()
405 {
406 m_controller->triggerZoomOut();
407 m_iconManager->updatePreviews();
408 }
409
410 bool DolphinView::isZoomInPossible() const
411 {
412 return m_controller->isZoomInPossible();
413 }
414
415 bool DolphinView::isZoomOutPossible() const
416 {
417 return m_controller->isZoomOutPossible();
418 }
419
420 void DolphinView::setSorting(Sorting sorting)
421 {
422 if (sorting != this->sorting()) {
423 updateSorting(sorting);
424 }
425 }
426
427 DolphinView::Sorting DolphinView::sorting() const
428 {
429 return m_proxyModel->sorting();
430 }
431
432 void DolphinView::setSortOrder(Qt::SortOrder order)
433 {
434 if (sortOrder() != order) {
435 updateSortOrder(order);
436 }
437 }
438
439 Qt::SortOrder DolphinView::sortOrder() const
440 {
441 return m_proxyModel->sortOrder();
442 }
443
444 void DolphinView::setAdditionalInfo(KFileItemDelegate::InformationList info)
445 {
446 const KUrl viewPropsUrl = viewPropertiesUrl();
447 ViewProperties props(viewPropsUrl);
448 props.setAdditionalInfo(info);
449 m_fileItemDelegate->setShowInformation(info);
450
451 emit additionalInfoChanged();
452
453 if (itemView() != m_detailsView) {
454 // the details view requires no reloading of the directory, as it maps
455 // the file item delegate info to its columns internally
456 loadDirectory(viewPropsUrl);
457 }
458 }
459
460 KFileItemDelegate::InformationList DolphinView::additionalInfo() const
461 {
462 return m_fileItemDelegate->showInformation();
463 }
464
465 void DolphinView::reload()
466 {
467 setUrl(url());
468 loadDirectory(url(), true);
469 }
470
471 void DolphinView::refresh()
472 {
473 const bool oldActivationState = m_active;
474 m_active = true;
475
476 createView();
477 applyViewProperties(m_controller->url());
478 reload();
479
480 setActive(oldActivationState);
481 }
482
483 void DolphinView::updateView(const KUrl& url, const KUrl& rootUrl)
484 {
485 if (m_controller->url() == url) {
486 return;
487 }
488
489 m_controller->setUrl(url); // emits urlChanged, which we forward
490
491 if (!rootUrl.isEmpty() && rootUrl.isParentOf(url)) {
492 applyViewProperties(rootUrl);
493 loadDirectory(rootUrl);
494 if (itemView() == m_columnView) {
495 m_columnView->setRootUrl(rootUrl);
496 m_columnView->showColumn(url);
497 }
498 } else {
499 applyViewProperties(url);
500 loadDirectory(url);
501 }
502
503 emit startedPathLoading(url);
504 }
505
506 void DolphinView::setNameFilter(const QString& nameFilter)
507 {
508 m_proxyModel->setFilterRegExp(nameFilter);
509
510 if (isColumnViewActive()) {
511 // adjusting the directory lister is not enough in the case of the
512 // column view, as each column has its own directory lister internally...
513 m_columnView->setNameFilter(nameFilter);
514 }
515 }
516
517 void DolphinView::calculateItemCount(int& fileCount, int& folderCount)
518 {
519 foreach (KFileItem item, m_dirLister->items()) {
520 if (item.isDir()) {
521 ++folderCount;
522 } else {
523 ++fileCount;
524 }
525 }
526 }
527
528 void DolphinView::setUrl(const KUrl& url)
529 {
530 updateView(url, KUrl());
531 }
532
533 void DolphinView::mouseReleaseEvent(QMouseEvent* event)
534 {
535 QWidget::mouseReleaseEvent(event);
536 setActive(true);
537 }
538
539 void DolphinView::wheelEvent(QWheelEvent* event)
540 {
541 if (event->modifiers() & Qt::ControlModifier) {
542 const int delta = event->delta();
543 if ((delta > 0) && isZoomInPossible()) {
544 zoomIn();
545 } else if ((delta < 0) && isZoomOutPossible()) {
546 zoomOut();
547 }
548 event->accept();
549 }
550 }
551
552 bool DolphinView::eventFilter(QObject* watched, QEvent* event)
553 {
554 if ((watched == itemView()) && (event->type() == QEvent::FocusIn)) {
555 m_controller->requestActivation();
556 }
557
558 return QWidget::eventFilter(watched, event);
559 }
560
561 void DolphinView::activate()
562 {
563 setActive(true);
564 }
565
566 void DolphinView::triggerItem(const KFileItem& item)
567 {
568 const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();
569 if ((modifier & Qt::ShiftModifier) || (modifier & Qt::ControlModifier)) {
570 // items are selected by the user, hence don't trigger the
571 // item specified by 'index'
572 return;
573 }
574
575 if (item.isNull()) {
576 return;
577 }
578
579 emit itemTriggered(item); // caught by DolphinViewContainer or DolphinPart
580 }
581
582 void DolphinView::emitSelectionChangedSignal()
583 {
584 emit selectionChanged(DolphinView::selectedItems());
585 }
586
587 void DolphinView::loadDirectory(const KUrl& url, bool reload)
588 {
589 if (!url.isValid()) {
590 const QString location(url.pathOrUrl());
591 if (location.isEmpty()) {
592 emit errorMessage(i18nc("@info:status", "The location is empty."));
593 } else {
594 emit errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location));
595 }
596 return;
597 }
598
599 m_loadingDirectory = true;
600
601 m_dirLister->stop();
602 m_dirLister->openUrl(url, reload ? KDirLister::Reload : KDirLister::NoFlags);
603
604 if (isColumnViewActive()) {
605 // adjusting the directory lister is not enough in the case of the
606 // column view, as each column has its own directory lister internally...
607 if (reload) {
608 m_columnView->reload();
609 } else {
610 m_columnView->showColumn(url);
611 }
612 }
613 }
614
615 KUrl DolphinView::viewPropertiesUrl() const
616 {
617 if (isColumnViewActive()) {
618 return m_columnView->rootUrl();
619 }
620
621 return url();
622 }
623
624 void DolphinView::applyViewProperties(const KUrl& url)
625 {
626 if (isColumnViewActive() && rootUrl().isParentOf(url)) {
627 // The column view is active, hence don't apply the view properties
628 // of sub directories (represented by columns) to the view. The
629 // view always represents the properties of the first column.
630 return;
631 }
632
633 const ViewProperties props(url);
634
635 const Mode mode = props.viewMode();
636 if (m_mode != mode) {
637 m_mode = mode;
638 createView();
639 emit modeChanged();
640 }
641 if (itemView() == 0) {
642 createView();
643 }
644 Q_ASSERT(itemView() != 0);
645 Q_ASSERT(m_fileItemDelegate != 0);
646
647 const bool showHiddenFiles = props.showHiddenFiles();
648 if (showHiddenFiles != m_dirLister->showingDotFiles()) {
649 m_dirLister->setShowingDotFiles(showHiddenFiles);
650 emit showHiddenFilesChanged();
651 }
652
653 m_storedCategorizedSorting = props.categorizedSorting();
654 const bool categorized = m_storedCategorizedSorting && supportsCategorizedSorting();
655 if (categorized != m_proxyModel->isCategorizedModel()) {
656 m_proxyModel->setCategorizedModel(categorized);
657 emit categorizedSortingChanged();
658 }
659
660 const DolphinView::Sorting sorting = props.sorting();
661 if (sorting != m_proxyModel->sorting()) {
662 m_proxyModel->setSorting(sorting);
663 emit sortingChanged(sorting);
664 }
665
666 const Qt::SortOrder sortOrder = props.sortOrder();
667 if (sortOrder != m_proxyModel->sortOrder()) {
668 m_proxyModel->setSortOrder(sortOrder);
669 emit sortOrderChanged(sortOrder);
670 }
671
672 KFileItemDelegate::InformationList info = props.additionalInfo();
673 if (info != m_fileItemDelegate->showInformation()) {
674 m_fileItemDelegate->setShowInformation(info);
675 emit additionalInfoChanged();
676 }
677
678 const bool showPreview = props.showPreview();
679 if (showPreview != m_showPreview) {
680 m_showPreview = showPreview;
681 m_iconManager->setShowPreview(showPreview);
682 emit showPreviewChanged();
683 }
684 }
685
686 void DolphinView::changeSelection(const KFileItemList& selection)
687 {
688 clearSelection();
689 if (selection.isEmpty()) {
690 return;
691 }
692 const KUrl& baseUrl = url();
693 KUrl url;
694 QItemSelection new_selection;
695 foreach(const KFileItem& item, selection) {
696 url = item.url().upUrl();
697 if (baseUrl.equals(url, KUrl::CompareWithoutTrailingSlash)) {
698 QModelIndex index = m_proxyModel->mapFromSource(m_dolphinModel->indexForItem(item));
699 new_selection.select(index, index);
700 }
701 }
702 itemView()->selectionModel()->select(new_selection,
703 QItemSelectionModel::ClearAndSelect
704 | QItemSelectionModel::Current);
705 }
706
707 void DolphinView::openContextMenu(const QPoint& pos)
708 {
709 KFileItem item;
710
711 const QModelIndex index = itemView()->indexAt(pos);
712 if (index.isValid() && (index.column() == DolphinModel::Name)) {
713 item = fileItem(index);
714 }
715
716 emit requestContextMenu(item, url());
717 }
718
719 void DolphinView::dropUrls(const KUrl::List& urls,
720 const KUrl& destPath,
721 const KFileItem& destItem)
722 {
723 Q_ASSERT(!urls.isEmpty());
724 const KUrl& destination = !destItem.isNull() && destItem.isDir() ?
725 destItem.url() : destPath;
726 const KUrl sourceDir = KUrl(urls.first().directory());
727 if (sourceDir != destination) {
728 dropUrls(urls, destination);
729 }
730 }
731
732 void DolphinView::dropUrls(const KUrl::List& urls,
733 const KUrl& destination)
734 {
735 DolphinDropController dropController(this);
736 // forward doingOperation signal up to the mainwindow
737 connect(&dropController, SIGNAL(doingOperation(KonqFileUndoManager::CommandType)),
738 this, SIGNAL(doingOperation(KonqFileUndoManager::CommandType)));
739 dropController.dropUrls(urls, destination);
740 }
741
742 void DolphinView::updateSorting(DolphinView::Sorting sorting)
743 {
744 ViewProperties props(viewPropertiesUrl());
745 props.setSorting(sorting);
746
747 m_proxyModel->setSorting(sorting);
748
749 emit sortingChanged(sorting);
750 }
751
752 void DolphinView::updateSortOrder(Qt::SortOrder order)
753 {
754 ViewProperties props(viewPropertiesUrl());
755 props.setSortOrder(order);
756
757 m_proxyModel->setSortOrder(order);
758
759 emit sortOrderChanged(order);
760 }
761
762 void DolphinView::toggleSortOrder()
763 {
764 const Qt::SortOrder order = (sortOrder() == Qt::AscendingOrder) ?
765 Qt::DescendingOrder :
766 Qt::AscendingOrder;
767 setSortOrder(order);
768 }
769
770 void DolphinView::updateAdditionalInfo(const KFileItemDelegate::InformationList& info)
771 {
772 ViewProperties props(viewPropertiesUrl());
773 props.setAdditionalInfo(info);
774 props.save();
775
776 m_fileItemDelegate->setShowInformation(info);
777
778 emit additionalInfoChanged();
779 }
780
781 void DolphinView::updateAdditionalInfoActions(KActionCollection* collection)
782 {
783 const bool enable = (m_mode == DolphinView::DetailsView) ||
784 (m_mode == DolphinView::IconsView);
785
786 QAction* showSizeInfo = collection->action("show_size_info");
787 QAction* showDateInfo = collection->action("show_date_info");
788 QAction* showPermissionsInfo = collection->action("show_permissions_info");
789 QAction* showOwnerInfo = collection->action("show_owner_info");
790 QAction* showGroupInfo = collection->action("show_group_info");
791 QAction* showMimeInfo = collection->action("show_mime_info");
792
793 showSizeInfo->setChecked(false);
794 showDateInfo->setChecked(false);
795 showPermissionsInfo->setChecked(false);
796 showOwnerInfo->setChecked(false);
797 showGroupInfo->setChecked(false);
798 showMimeInfo->setChecked(false);
799
800 showSizeInfo->setEnabled(enable);
801 showDateInfo->setEnabled(enable);
802 showPermissionsInfo->setEnabled(enable);
803 showOwnerInfo->setEnabled(enable);
804 showGroupInfo->setEnabled(enable);
805 showMimeInfo->setEnabled(enable);
806
807 foreach (KFileItemDelegate::Information info, m_fileItemDelegate->showInformation()) {
808 switch (info) {
809 case KFileItemDelegate::Size:
810 showSizeInfo->setChecked(true);
811 break;
812 case KFileItemDelegate::ModificationTime:
813 showDateInfo->setChecked(true);
814 break;
815 case KFileItemDelegate::Permissions:
816 showPermissionsInfo->setChecked(true);
817 break;
818 case KFileItemDelegate::Owner:
819 showOwnerInfo->setChecked(true);
820 break;
821 case KFileItemDelegate::OwnerAndGroup:
822 showGroupInfo->setChecked(true);
823 break;
824 case KFileItemDelegate::FriendlyMimeType:
825 showMimeInfo->setChecked(true);
826 break;
827 default:
828 break;
829 }
830 }
831 }
832
833 void DolphinView::toggleAdditionalInfo(QAction* action)
834 {
835 const KFileItemDelegate::Information info =
836 static_cast<KFileItemDelegate::Information>(action->data().toInt());
837
838 KFileItemDelegate::InformationList list = additionalInfo();
839
840 const bool show = action->isChecked();
841
842 const int index = list.indexOf(info);
843 const bool containsInfo = (index >= 0);
844 if (show && !containsInfo) {
845 list.append(info);
846 setAdditionalInfo(list);
847 } else if (!show && containsInfo) {
848 list.removeAt(index);
849 setAdditionalInfo(list);
850 Q_ASSERT(list.indexOf(info) < 0);
851 }
852 }
853
854 void DolphinView::emitContentsMoved()
855 {
856 // only emit the contents moved signal if:
857 // - no directory loading is ongoing (this would reset the contents position
858 // always to (0, 0))
859 // - if the Column View is active: the column view does an automatic
860 // positioning during the loading operation, which must be remembered
861 if (!m_loadingDirectory || isColumnViewActive()) {
862 const QPoint pos(contentsPosition());
863 emit contentsMoved(pos.x(), pos.y());
864 }
865 }
866
867 void DolphinView::showHoverInformation(const KFileItem& item)
868 {
869 if (hasSelection() || !m_active) {
870 return;
871 }
872
873 emit requestItemInfo(item);
874 }
875
876 void DolphinView::clearHoverInformation()
877 {
878 if (m_active) {
879 emit requestItemInfo(KFileItem());
880 }
881 }
882
883 void DolphinView::createView()
884 {
885 deleteView();
886 Q_ASSERT(m_iconsView == 0);
887 Q_ASSERT(m_detailsView == 0);
888 Q_ASSERT(m_columnView == 0);
889
890 QAbstractItemView* view = 0;
891 switch (m_mode) {
892 case IconsView: {
893 m_iconsView = new DolphinIconsView(this, m_controller);
894 view = m_iconsView;
895 break;
896 }
897
898 case DetailsView:
899 m_detailsView = new DolphinDetailsView(this, m_controller);
900 view = m_detailsView;
901 break;
902
903 case ColumnView:
904 m_columnView = new DolphinColumnView(this, m_controller);
905 view = m_columnView;
906 break;
907 }
908
909 Q_ASSERT(view != 0);
910 view->installEventFilter(this);
911
912 m_controller->setItemView(view);
913
914 m_fileItemDelegate = new KFileItemDelegate(view);
915 view->setItemDelegate(m_fileItemDelegate);
916
917 view->setModel(m_proxyModel);
918 if (m_selectionModel != 0) {
919 view->setSelectionModel(m_selectionModel);
920 } else {
921 m_selectionModel = view->selectionModel();
922 }
923
924 // reparent the selection model, as it should not be deleted
925 // when deleting the model
926 m_selectionModel->setParent(this);
927
928 view->setSelectionMode(QAbstractItemView::ExtendedSelection);
929
930 new KMimeTypeResolver(view, m_dolphinModel);
931 m_iconManager = new IconManager(view, m_proxyModel);
932 m_iconManager->setShowPreview(m_showPreview);
933
934 m_topLayout->insertWidget(1, view);
935
936 connect(view->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
937 this, SLOT(emitSelectionChangedSignal()));
938 connect(view->verticalScrollBar(), SIGNAL(valueChanged(int)),
939 this, SLOT(emitContentsMoved()));
940 connect(view->horizontalScrollBar(), SIGNAL(valueChanged(int)),
941 this, SLOT(emitContentsMoved()));
942 }
943
944 void DolphinView::deleteView()
945 {
946 QAbstractItemView* view = itemView();
947 if (view != 0) {
948 m_topLayout->removeWidget(view);
949 view->close();
950 view->deleteLater();
951 view = 0;
952 m_iconsView = 0;
953 m_detailsView = 0;
954 m_columnView = 0;
955 m_fileItemDelegate = 0;
956 m_iconManager = 0;
957 }
958 }
959
960 QAbstractItemView* DolphinView::itemView() const
961 {
962 if (m_detailsView != 0) {
963 return m_detailsView;
964 } else if (m_columnView != 0) {
965 return m_columnView;
966 }
967
968 return m_iconsView;
969 }
970
971 bool DolphinView::isCutItem(const KFileItem& item) const
972 {
973 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
974 const KUrl::List cutUrls = KUrl::List::fromMimeData(mimeData);
975
976 const KUrl& itemUrl = item.url();
977 KUrl::List::const_iterator it = cutUrls.begin();
978 const KUrl::List::const_iterator end = cutUrls.end();
979 while (it != end) {
980 if (*it == itemUrl) {
981 return true;
982 }
983 ++it;
984 }
985
986 return false;
987 }
988
989 void DolphinView::renameSelectedItems()
990 {
991 const KFileItemList items = selectedItems();
992 if (items.count() > 1) {
993 // More than one item has been selected for renaming. Open
994 // a rename dialog and rename all items afterwards.
995 RenameDialog dialog(this, items);
996 if (dialog.exec() == QDialog::Rejected) {
997 return;
998 }
999
1000 const QString newName = dialog.newName();
1001 if (newName.isEmpty()) {
1002 emit errorMessage(dialog.errorString());
1003 } else {
1004 // TODO: check how this can be integrated into KonqFileUndoManager/KonqOperations
1005 // as one operation instead of n rename operations like it is done now...
1006 Q_ASSERT(newName.contains('#'));
1007
1008 // iterate through all selected items and rename them...
1009 int index = 1;
1010 foreach (KFileItem item, items) {
1011 const KUrl& oldUrl = item.url();
1012 QString number;
1013 number.setNum(index++);
1014
1015 QString name = newName;
1016 name.replace('#', number);
1017
1018 if (oldUrl.fileName() != name) {
1019 KUrl newUrl = oldUrl;
1020 newUrl.setFileName(name);
1021 KonqOperations::rename(this, oldUrl, newUrl);
1022 emit doingOperation(KonqFileUndoManager::RENAME);
1023 }
1024 }
1025 }
1026 } else {
1027 // Only one item has been selected for renaming. Use the custom
1028 // renaming mechanism from the views.
1029 Q_ASSERT(items.count() == 1);
1030
1031 // TODO: Think about using KFileItemDelegate as soon as it supports editing.
1032 // Currently the RenameDialog is used, but I'm not sure whether inline renaming
1033 // is a benefit for the user at all -> let's wait for some input first...
1034 RenameDialog dialog(this, items);
1035 if (dialog.exec() == QDialog::Rejected) {
1036 return;
1037 }
1038
1039 const QString& newName = dialog.newName();
1040 if (newName.isEmpty()) {
1041 emit errorMessage(dialog.errorString());
1042 } else {
1043 const KUrl& oldUrl = items.first().url();
1044 KUrl newUrl = oldUrl;
1045 newUrl.setFileName(newName);
1046 KonqOperations::rename(this, oldUrl, newUrl);
1047 emit doingOperation(KonqFileUndoManager::RENAME);
1048 }
1049 }
1050 }
1051
1052 void DolphinView::trashSelectedItems()
1053 {
1054 emit doingOperation(KonqFileUndoManager::TRASH);
1055 KonqOperations::del(this, KonqOperations::TRASH, selectedUrls());
1056 }
1057
1058 void DolphinView::deleteSelectedItems()
1059 {
1060 const KUrl::List list = selectedUrls();
1061 const bool del = KonqOperations::askDeleteConfirmation(list,
1062 KonqOperations::DEL,
1063 KonqOperations::DEFAULT_CONFIRMATION,
1064 this);
1065
1066 if (del) {
1067 KIO::Job* job = KIO::del(list);
1068 connect(job, SIGNAL(result(KJob*)),
1069 this, SLOT(slotDeleteFileFinished(KJob*)));
1070 }
1071 }
1072
1073 void DolphinView::slotDeleteFileFinished(KJob* job)
1074 {
1075 if (job->error() == 0) {
1076 emit operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
1077 } else {
1078 emit errorMessage(job->errorString());
1079 }
1080 }
1081
1082 void DolphinView::cutSelectedItems()
1083 {
1084 QMimeData* mimeData = new QMimeData();
1085 const KUrl::List kdeUrls = selectedUrls();
1086 const KUrl::List mostLocalUrls;
1087 KonqMimeData::populateMimeData(mimeData, kdeUrls, mostLocalUrls, true);
1088 QApplication::clipboard()->setMimeData(mimeData);
1089 }
1090
1091 void DolphinView::copySelectedItems()
1092 {
1093 QMimeData* mimeData = new QMimeData();
1094 const KUrl::List kdeUrls = selectedUrls();
1095 const KUrl::List mostLocalUrls;
1096 KonqMimeData::populateMimeData(mimeData, kdeUrls, mostLocalUrls, false);
1097 QApplication::clipboard()->setMimeData(mimeData);
1098 }
1099
1100 void DolphinView::paste()
1101 {
1102 QClipboard* clipboard = QApplication::clipboard();
1103 const QMimeData* mimeData = clipboard->mimeData();
1104
1105 const KUrl::List sourceUrls = KUrl::List::fromMimeData(mimeData);
1106
1107 // per default the pasting is done into the current URL of the view
1108 KUrl destUrl = url();
1109
1110 // check whether the pasting should be done into a selected directory
1111 const KUrl::List selectedUrls = this->selectedUrls();
1112 if (selectedUrls.count() == 1) {
1113 const KFileItem fileItem(S_IFDIR,
1114 KFileItem::Unknown,
1115 selectedUrls.first(),
1116 true);
1117 if (fileItem.isDir()) {
1118 // only one item is selected which is a directory, hence paste
1119 // into this directory
1120 destUrl = selectedUrls.first();
1121 if (sourceUrls.contains(destUrl)) {
1122 const QString text = i18nc("@info", "The folder <filename>%1</filename> is pasted into itself. Is this intended?", fileItem.name());
1123 int result = KMessageBox::questionYesNo(window(),
1124 text,
1125 i18nc("@title:window", "Paste into Folder"),
1126 KGuiItem(i18nc("@label", "Paste"), "dialog-ok"),
1127 KGuiItem(i18nc("@label", "Cancel"), "dialog-cancel"));
1128 if (result == KMessageBox::No) {
1129 return;
1130 }
1131 }
1132 }
1133 }
1134
1135 if (KonqMimeData::decodeIsCutSelection(mimeData)) {
1136 KonqOperations::copy(this, KonqOperations::MOVE, sourceUrls, destUrl);
1137 emit doingOperation(KonqFileUndoManager::MOVE);
1138 clipboard->clear();
1139 } else {
1140 KonqOperations::copy(this, KonqOperations::COPY, sourceUrls, destUrl);
1141 emit doingOperation(KonqFileUndoManager::COPY);
1142 }
1143 }
1144
1145 QPair<bool, QString> DolphinView::pasteInfo() const
1146 {
1147 QPair<bool, QString> ret;
1148 QClipboard* clipboard = QApplication::clipboard();
1149 const QMimeData* mimeData = clipboard->mimeData();
1150
1151 KUrl::List urls = KUrl::List::fromMimeData(mimeData);
1152 if (!urls.isEmpty()) {
1153 ret.first = true;
1154 if (urls.count() == 1) {
1155 const KFileItem item(KFileItem::Unknown, KFileItem::Unknown, urls.first(), true);
1156 ret.second = item.isDir() ? i18nc("@action:inmenu", "Paste One Folder") :
1157 i18nc("@action:inmenu", "Paste One File");
1158
1159 } else {
1160 ret.second = i18ncp("@action:inmenu", "Paste One Item", "Paste %1 Items", urls.count());
1161 }
1162 } else {
1163 ret.first = false;
1164 ret.second = i18nc("@action:inmenu", "Paste");
1165 }
1166
1167 if (ret.first) {
1168 const KFileItemList items = selectedItems();
1169 const uint count = items.count();
1170 if (count > 1) {
1171 // pasting should not be allowed when more than one file
1172 // is selected
1173 ret.first = false;
1174 } else if (count == 1) {
1175 // Only one file is selected. Pasting is only allowed if this
1176 // file is a directory.
1177 ret.first = items.first().isDir();
1178 }
1179 }
1180 return ret;
1181 }
1182
1183 #include "dolphinview.moc"