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