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