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