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