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