]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinview.cpp
allow to browse through ZIP files (thanks to Filip Brcic for the patch!)
[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 <assert.h>
24
25 #include <QApplication>
26 #include <QDropEvent>
27 #include <QItemSelectionModel>
28 #include <QMouseEvent>
29 #include <QVBoxLayout>
30
31 #include <kdirmodel.h>
32 #include <kfileitemdelegate.h>
33 #include <klocale.h>
34 #include <kio/netaccess.h>
35 #include <kio/renamedialog.h>
36 #include <kio/previewjob.h>
37 #include <kmimetyperesolver.h>
38 #include <konq_operations.h>
39 #include <kurl.h>
40
41 #include "dolphincontroller.h"
42 #include "dolphinstatusbar.h"
43 #include "dolphinmainwindow.h"
44 #include "dolphindirlister.h"
45 #include "dolphinsortfilterproxymodel.h"
46 #include "dolphindetailsview.h"
47 #include "dolphiniconsview.h"
48 #include "dolphincontextmenu.h"
49 #include "filterbar.h"
50 #include "renamedialog.h"
51 #include "urlnavigator.h"
52 #include "viewproperties.h"
53
54 DolphinView::DolphinView(DolphinMainWindow* mainWindow,
55 QWidget* parent,
56 const KUrl& url,
57 Mode mode,
58 bool showHiddenFiles) :
59 QWidget(parent),
60 m_showProgress(false),
61 m_mode(mode),
62 m_iconSize(0),
63 m_folderCount(0),
64 m_fileCount(0),
65 m_mainWindow(mainWindow),
66 m_topLayout(0),
67 m_urlNavigator(0),
68 m_controller(0),
69 m_iconsView(0),
70 m_detailsView(0),
71 m_filterBar(0),
72 m_statusBar(0),
73 m_dirModel(0),
74 m_dirLister(0),
75 m_proxyModel(0)
76 {
77 hide();
78 setFocusPolicy(Qt::StrongFocus);
79 m_topLayout = new QVBoxLayout(this);
80 m_topLayout->setSpacing(0);
81 m_topLayout->setMargin(0);
82
83 connect(m_mainWindow, SIGNAL(activeViewChanged()),
84 this, SLOT(updateActivationState()));
85
86 m_urlNavigator = new UrlNavigator(url, this);
87 m_urlNavigator->setShowHiddenFiles(showHiddenFiles);
88 connect(m_urlNavigator, SIGNAL(urlChanged(const KUrl&)),
89 this, SLOT(loadDirectory(const KUrl&)));
90 connect(m_urlNavigator, SIGNAL(urlsDropped(const KUrl::List&, const KUrl&)),
91 this, SLOT(dropUrls(const KUrl::List&, const KUrl&)));
92 connect(m_urlNavigator, SIGNAL(activated()),
93 this, SLOT(requestActivation()));
94 connect(this, SIGNAL(contentsMoved(int, int)),
95 m_urlNavigator, SLOT(storeContentsPosition(int, int)));
96
97 m_statusBar = new DolphinStatusBar(this);
98
99 m_dirLister = new DolphinDirLister();
100 m_dirLister->setAutoUpdate(true);
101 m_dirLister->setMainWindow(this);
102 m_dirLister->setShowingDotFiles(showHiddenFiles);
103 m_dirLister->setDelayedMimeTypes(true);
104
105 connect(m_dirLister, SIGNAL(clear()),
106 this, SLOT(updateStatusBar()));
107 connect(m_dirLister, SIGNAL(percent(int)),
108 this, SLOT(updateProgress(int)));
109 connect(m_dirLister, SIGNAL(deleteItem(KFileItem*)),
110 this, SLOT(updateStatusBar()));
111 connect(m_dirLister, SIGNAL(completed()),
112 this, SLOT(updateItemCount()));
113 connect(m_dirLister, SIGNAL(newItems(const KFileItemList&)),
114 this, SLOT(generatePreviews(const KFileItemList&)));
115 connect(m_dirLister, SIGNAL(infoMessage(const QString&)),
116 this, SLOT(showInfoMessage(const QString&)));
117 connect(m_dirLister, SIGNAL(errorMessage(const QString&)),
118 this, SLOT(showErrorMessage(const QString&)));
119
120 m_dirModel = new KDirModel();
121 m_dirModel->setDirLister(m_dirLister);
122 m_dirModel->setDropsAllowed(KDirModel::DropOnDirectory);
123
124 m_proxyModel = new DolphinSortFilterProxyModel(this);
125 m_proxyModel->setSourceModel(m_dirModel);
126
127 m_controller = new DolphinController(this);
128 connect(m_controller, SIGNAL(requestContextMenu(const QPoint&)),
129 this, SLOT(openContextMenu(const QPoint&)));
130 connect(m_controller, SIGNAL(urlsDropped(const KUrl::List&, const QPoint&)),
131 this, SLOT(dropUrls(const KUrl::List&, const QPoint&)));
132 connect(m_controller, SIGNAL(sortingChanged(DolphinView::Sorting)),
133 this, SLOT(updateSorting(DolphinView::Sorting)));
134 connect(m_controller, SIGNAL(sortOrderChanged(Qt::SortOrder)),
135 this, SLOT(updateSortOrder(Qt::SortOrder)));
136 connect(m_controller, SIGNAL(itemTriggered(const QModelIndex&)),
137 this, SLOT(triggerItem(const QModelIndex&)));
138 connect(m_controller, SIGNAL(selectionChanged()),
139 this, SLOT(emitSelectionChangedSignal()));
140 connect(m_controller, SIGNAL(activated()),
141 this, SLOT(requestActivation()));
142
143 createView();
144
145 m_iconSize = K3Icon::SizeMedium;
146
147 m_filterBar = new FilterBar(this);
148 m_filterBar->hide();
149 connect(m_filterBar, SIGNAL(filterChanged(const QString&)),
150 this, SLOT(changeNameFilter(const QString&)));
151 connect(m_filterBar, SIGNAL(closeRequest()),
152 this, SLOT(closeFilterBar()));
153
154 m_topLayout->addWidget(m_urlNavigator);
155 m_topLayout->addWidget(itemView());
156 m_topLayout->addWidget(m_filterBar);
157 m_topLayout->addWidget(m_statusBar);
158
159 loadDirectory(m_urlNavigator->url());
160 }
161
162 DolphinView::~DolphinView()
163 {
164 delete m_dirLister;
165 m_dirLister = 0;
166 }
167
168 void DolphinView::setUrl(const KUrl& url)
169 {
170 m_urlNavigator->setUrl(url);
171 m_controller->setUrl(url);
172 }
173
174 const KUrl& DolphinView::url() const
175 {
176 return m_urlNavigator->url();
177 }
178
179 bool DolphinView::isActive() const
180 {
181 return m_mainWindow->activeView() == this;
182 }
183
184 void DolphinView::setMode(Mode mode)
185 {
186 if (mode == m_mode) {
187 return; // the wished mode is already set
188 }
189
190 m_mode = mode;
191
192 ViewProperties props(m_urlNavigator->url());
193 props.setViewMode(m_mode);
194
195 createView();
196 startDirLister(m_urlNavigator->url());
197
198 emit modeChanged();
199 }
200
201 DolphinView::Mode DolphinView::mode() const
202 {
203 return m_mode;
204 }
205
206 void DolphinView::setShowPreview(bool show)
207 {
208 ViewProperties props(m_urlNavigator->url());
209 props.setShowPreview(show);
210
211 m_controller->setShowPreview(show);
212
213 emit showPreviewChanged();
214 reload();
215 }
216
217 bool DolphinView::showPreview() const
218 {
219 return m_controller->showPreview();
220 }
221
222 void DolphinView::setShowHiddenFiles(bool show)
223 {
224 if (m_dirLister->showingDotFiles() == show) {
225 return;
226 }
227
228 ViewProperties props(m_urlNavigator->url());
229 props.setShowHiddenFiles(show);
230 props.save();
231
232 m_dirLister->setShowingDotFiles(show);
233 m_urlNavigator->setShowHiddenFiles(show);
234
235 emit showHiddenFilesChanged();
236
237 reload();
238 }
239
240 bool DolphinView::showHiddenFiles() const
241 {
242 return m_dirLister->showingDotFiles();
243 }
244
245 void DolphinView::renameSelectedItems()
246 {
247 const KUrl::List urls = selectedUrls();
248 if (urls.count() > 1) {
249 // More than one item has been selected for renaming. Open
250 // a rename dialog and rename all items afterwards.
251 RenameDialog dialog(urls);
252 if (dialog.exec() == QDialog::Rejected) {
253 return;
254 }
255
256 DolphinView* view = mainWindow()->activeView();
257 const QString& newName = dialog.newName();
258 if (newName.isEmpty()) {
259 view->statusBar()->setMessage(i18n("The new item name is invalid."),
260 DolphinStatusBar::Error);
261 }
262 else {
263 // TODO: check how this can be integrated into KonqUndoManager/KonqOperations
264
265 //UndoManager& undoMan = UndoManager::instance();
266 //undoMan.beginMacro();
267
268 assert(newName.contains('#'));
269
270 const int urlsCount = urls.count();
271
272 // iterate through all selected items and rename them...
273 const int replaceIndex = newName.indexOf('#');
274 assert(replaceIndex >= 0);
275 for (int i = 0; i < urlsCount; ++i) {
276 const KUrl& source = urls[i];
277 QString number;
278 number.setNum(i + 1);
279
280 QString name(newName);
281 name.replace(replaceIndex, 1, number);
282
283 if (source.fileName() != name) {
284 KUrl dest(source.upUrl());
285 dest.addPath(name);
286
287 const bool destExists = KIO::NetAccess::exists(dest, false, view);
288 if (destExists) {
289 view->statusBar()->setMessage(i18n("Renaming failed (item '%1' already exists).",name),
290 DolphinStatusBar::Error);
291 break;
292 }
293 else if (KIO::NetAccess::file_move(source, dest)) {
294 // TODO: From the users point of view he executed one 'rename n files' operation,
295 // but internally we store it as n 'rename 1 file' operations for the undo mechanism.
296 //DolphinCommand command(DolphinCommand::Rename, source, dest);
297 //undoMan.addCommand(command);
298 }
299 }
300 }
301
302 //undoMan.endMacro();
303 }
304 }
305 else {
306 // Only one item has been selected for renaming. Use the custom
307 // renaming mechanism from the views.
308 assert(urls.count() == 1);
309 // TODO:
310 /*if (m_mode == DetailsView) {
311 Q3ListViewItem* item = m_iconsView->firstChild();
312 while (item != 0) {
313 if (item->isSelected()) {
314 m_iconsView->rename(item, DolphinDetailsView::NameColumn);
315 break;
316 }
317 item = item->nextSibling();
318 }
319 }
320 else {
321 KFileIconViewItem* item = static_cast<KFileIconViewItem*>(m_iconsView->firstItem());
322 while (item != 0) {
323 if (item->isSelected()) {
324 item->rename();
325 break;
326 }
327 item = static_cast<KFileIconViewItem*>(item->nextItem());
328 }
329 }*/
330 }
331 }
332
333 void DolphinView::selectAll()
334 {
335 selectAll(QItemSelectionModel::Select);
336 }
337
338 void DolphinView::invertSelection()
339 {
340 selectAll(QItemSelectionModel::Toggle);
341 }
342
343 DolphinStatusBar* DolphinView::statusBar() const
344 {
345 return m_statusBar;
346 }
347
348 int DolphinView::contentsX() const
349 {
350
351 return itemView()->horizontalScrollBar()->value();
352 }
353
354 int DolphinView::contentsY() const
355 {
356 return itemView()->verticalScrollBar()->value();
357 }
358
359 void DolphinView::refreshSettings()
360 {
361 startDirLister(m_urlNavigator->url());
362 }
363
364 void DolphinView::emitRequestItemInfo(const KUrl& url)
365 {
366 emit requestItemInfo(url);
367 }
368
369 bool DolphinView::isFilterBarVisible() const
370 {
371 return m_filterBar->isVisible();
372 }
373
374 bool DolphinView::isUrlEditable() const
375 {
376 return m_urlNavigator->isUrlEditable();
377 }
378
379 void DolphinView::zoomIn()
380 {
381 m_controller->triggerZoomIn();
382 }
383
384 void DolphinView::zoomOut()
385 {
386 m_controller->triggerZoomOut();
387 }
388
389 bool DolphinView::isZoomInPossible() const
390 {
391 return m_controller->isZoomInPossible();
392 }
393
394 bool DolphinView::isZoomOutPossible() const
395 {
396 return m_controller->isZoomOutPossible();
397 }
398
399 void DolphinView::setSorting(Sorting sorting)
400 {
401 if (sorting != this->sorting()) {
402 updateSorting(sorting);
403 }
404 }
405
406 DolphinView::Sorting DolphinView::sorting() const
407 {
408 return m_proxyModel->sorting();
409 }
410
411 void DolphinView::setSortOrder(Qt::SortOrder order)
412 {
413 if (sortOrder() != order) {
414 updateSortOrder(order);
415 }
416 }
417
418 Qt::SortOrder DolphinView::sortOrder() const
419 {
420 return m_proxyModel->sortOrder();
421 }
422
423 void DolphinView::goBack()
424 {
425 m_urlNavigator->goBack();
426 }
427
428 void DolphinView::goForward()
429 {
430 m_urlNavigator->goForward();
431 }
432
433 void DolphinView::goUp()
434 {
435 m_urlNavigator->goUp();
436 }
437
438 void DolphinView::goHome()
439 {
440 m_urlNavigator->goHome();
441 }
442
443 void DolphinView::setUrlEditable(bool editable)
444 {
445 m_urlNavigator->editUrl(editable);
446 }
447
448 const QLinkedList<UrlNavigator::HistoryElem> DolphinView::urlHistory(int& index) const
449 {
450 return m_urlNavigator->history(index);
451 }
452
453 bool DolphinView::hasSelection() const
454 {
455 return itemView()->selectionModel()->hasSelection();
456 }
457
458 KFileItemList DolphinView::selectedItems() const
459 {
460 const QAbstractItemView* view = itemView();
461
462 // Our view has a selection, we will map them back to the DirModel
463 // and then fill the KFileItemList.
464 Q_ASSERT((view != 0) && (view->selectionModel() != 0));
465
466 const QItemSelection selection = m_proxyModel->mapSelectionToSource(view->selectionModel()->selection());
467 KFileItemList itemList;
468
469 const QModelIndexList indexList = selection.indexes();
470 QModelIndexList::const_iterator end = indexList.end();
471 for (QModelIndexList::const_iterator it = indexList.begin(); it != end; ++it) {
472 Q_ASSERT((*it).isValid());
473
474 KFileItem* item = m_dirModel->itemForIndex(*it);
475 if (item != 0) {
476 itemList.append(item);
477 }
478 }
479
480 return itemList;
481 }
482
483 KUrl::List DolphinView::selectedUrls() const
484 {
485 KUrl::List urls;
486
487 const KFileItemList list = selectedItems();
488 KFileItemList::const_iterator it = list.begin();
489 const KFileItemList::const_iterator end = list.end();
490 while (it != end) {
491 KFileItem* item = *it;
492 urls.append(item->url());
493 ++it;
494 }
495
496 return urls;
497 }
498
499 KFileItem* DolphinView::fileItem(const QModelIndex index) const
500 {
501 const QModelIndex dirModelIndex = m_proxyModel->mapToSource(index);
502 return m_dirModel->itemForIndex(dirModelIndex);
503 }
504
505 void DolphinView::rename(const KUrl& source, const QString& newName)
506 {
507 bool ok = false;
508
509 if (newName.isEmpty() || (source.fileName() == newName)) {
510 return;
511 }
512
513 KUrl dest(source.upUrl());
514 dest.addPath(newName);
515
516 const bool destExists = KIO::NetAccess::exists(dest,
517 false,
518 mainWindow()->activeView());
519 if (destExists) {
520 // the destination already exists, hence ask the user
521 // how to proceed...
522 KIO::RenameDialog renameDialog(this,
523 i18n("File Already Exists"),
524 source.path(),
525 dest.path(),
526 KIO::M_OVERWRITE);
527 switch (renameDialog.exec()) {
528 case KIO::R_OVERWRITE:
529 // the destination should be overwritten
530 ok = KIO::NetAccess::file_move(source, dest, -1, true);
531 break;
532
533 case KIO::R_RENAME: {
534 // a new name for the destination has been used
535 KUrl newDest(renameDialog.newDestUrl());
536 ok = KIO::NetAccess::file_move(source, newDest);
537 break;
538 }
539
540 default:
541 // the renaming operation has been canceled
542 reload();
543 return;
544 }
545 }
546 else {
547 // no destination exists, hence just move the file to
548 // do the renaming
549 ok = KIO::NetAccess::file_move(source, dest);
550 }
551
552 const QString destFileName = dest.fileName();
553 if (ok) {
554 m_statusBar->setMessage(i18n("Renamed file '%1' to '%2'.",source.fileName(), destFileName),
555 DolphinStatusBar::OperationCompleted);
556
557 KonqOperations::rename(this, source, destFileName);
558 }
559 else {
560 m_statusBar->setMessage(i18n("Renaming of file '%1' to '%2' failed.",source.fileName(), destFileName),
561 DolphinStatusBar::Error);
562 reload();
563 }
564 }
565
566 void DolphinView::reload()
567 {
568 startDirLister(m_urlNavigator->url(), true);
569 }
570
571 void DolphinView::mouseReleaseEvent(QMouseEvent* event)
572 {
573 QWidget::mouseReleaseEvent(event);
574 mainWindow()->setActiveView(this);
575 }
576
577 DolphinMainWindow* DolphinView::mainWindow() const
578 {
579 return m_mainWindow;
580 }
581
582 void DolphinView::loadDirectory(const KUrl& url)
583 {
584 const ViewProperties props(url);
585
586 const Mode mode = props.viewMode();
587 if (m_mode != mode) {
588 m_mode = mode;
589 createView();
590 emit modeChanged();
591 }
592
593 const bool showHiddenFiles = props.showHiddenFiles();
594 if (showHiddenFiles != m_dirLister->showingDotFiles()) {
595 m_dirLister->setShowingDotFiles(showHiddenFiles);
596 emit showHiddenFilesChanged();
597 }
598
599 const DolphinView::Sorting sorting = props.sorting();
600 if (sorting != m_proxyModel->sorting()) {
601 m_proxyModel->setSorting(sorting);
602 emit sortingChanged(sorting);
603 }
604
605 const Qt::SortOrder sortOrder = props.sortOrder();
606 if (sortOrder != m_proxyModel->sortOrder()) {
607 m_proxyModel->setSortOrder(sortOrder);
608 emit sortOrderChanged(sortOrder);
609 }
610
611 const bool showPreview = props.showPreview();
612 if (showPreview != m_controller->showPreview()) {
613 m_controller->setShowPreview(showPreview);
614 emit showPreviewChanged();
615 }
616
617 startDirLister(url);
618 emit urlChanged(url);
619
620 m_statusBar->clear();
621 }
622
623 void DolphinView::triggerItem(const QModelIndex& index)
624 {
625 if (!isValidNameIndex(index)) {
626 return;
627 }
628
629 const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();
630 if ((modifier & Qt::ShiftModifier) || (modifier & Qt::ControlModifier)) {
631 // items are selected by the user, hence don't trigger the
632 // item specified by 'index'
633 return;
634 }
635
636 KFileItem* item = m_dirModel->itemForIndex(m_proxyModel->mapToSource(index));
637 if (item == 0) {
638 return;
639 }
640
641 if (item->isDir()) {
642 // Prefer the local path over the URL. This assures that the
643 // volume space information is correct. Assuming that the URL is media:/sda1,
644 // and the local path is /windows/C: For the URL the space info is related
645 // to the root partition (and hence wrong) and for the local path the space
646 // info is related to the windows partition (-> correct).
647 const QString localPath(item->localPath());
648 if (localPath.isEmpty()) {
649 setUrl(item->url());
650 }
651 else {
652 setUrl(KUrl(localPath));
653 }
654 }
655 else if (item->isFile() && item->mimeTypePtr()->is("application/x-zip")) {
656 // allow to browse through ZIP files
657 const QString localPath(item->localPath());
658 KUrl url;
659 if (localPath.isEmpty()) {
660 url = item->url();
661 }
662 else {
663 url = localPath;
664 }
665 url.setProtocol("zip");
666 setUrl(url);
667 }
668 else {
669 item->run();
670 }
671 }
672
673 void DolphinView::updateProgress(int percent)
674 {
675 if (m_showProgress) {
676 m_statusBar->setProgress(percent);
677 }
678 }
679
680 void DolphinView::updateItemCount()
681 {
682 if (m_showProgress) {
683 m_statusBar->setProgressText(QString());
684 m_statusBar->setProgress(100);
685 m_showProgress = false;
686 }
687
688 KFileItemList items(m_dirLister->items());
689 KFileItemList::const_iterator it = items.begin();
690 const KFileItemList::const_iterator end = items.end();
691
692 m_fileCount = 0;
693 m_folderCount = 0;
694
695 while (it != end) {
696 KFileItem* item = *it;
697 if (item->isDir()) {
698 ++m_folderCount;
699 }
700 else {
701 ++m_fileCount;
702 }
703 ++it;
704 }
705
706 updateStatusBar();
707
708 QTimer::singleShot(0, this, SLOT(restoreContentsPos()));
709 }
710
711 void DolphinView::generatePreviews(const KFileItemList& items)
712 {
713 if (m_controller->showPreview()) {
714 KIO::PreviewJob* job = KIO::filePreview(items, 128);
715 connect(job, SIGNAL(gotPreview(const KFileItem*, const QPixmap&)),
716 this, SLOT(showPreview(const KFileItem*, const QPixmap&)));
717 }
718 }
719
720 void DolphinView::showPreview(const KFileItem* item, const QPixmap& pixmap)
721 {
722 Q_ASSERT(item != 0);
723 const QModelIndex idx = m_dirModel->indexForItem(*item);
724 if (idx.isValid() && (idx.column() == 0)) {
725 m_dirModel->setData(idx, pixmap, Qt::DecorationRole);
726 }
727 }
728
729 void DolphinView::restoreContentsPos()
730 {
731 int index = 0;
732 const QLinkedList<UrlNavigator::HistoryElem> history = urlHistory(index);
733 if (!history.isEmpty()) {
734 QAbstractItemView* view = itemView();
735 // TODO: view->setCurrentItem(history[index].currentFileName());
736
737 QLinkedList<UrlNavigator::HistoryElem>::const_iterator it = history.begin();
738 it += index;
739 view->horizontalScrollBar()->setValue((*it).contentsX());
740 view->verticalScrollBar()->setValue((*it).contentsY());
741 }
742 }
743
744 void DolphinView::showInfoMessage(const QString& msg)
745 {
746 m_statusBar->setMessage(msg, DolphinStatusBar::Information);
747 }
748
749 void DolphinView::showErrorMessage(const QString& msg)
750 {
751 m_statusBar->setMessage(msg, DolphinStatusBar::Error);
752 }
753
754 void DolphinView::emitSelectionChangedSignal()
755 {
756 emit selectionChanged();
757 }
758
759 void DolphinView::closeFilterBar()
760 {
761 m_filterBar->hide();
762 emit showFilterBarChanged(false);
763 }
764
765 void DolphinView::startDirLister(const KUrl& url, bool reload)
766 {
767 if (!url.isValid()) {
768 const QString location(url.pathOrUrl());
769 if (location.isEmpty()) {
770 m_statusBar->setMessage(i18n("The location is empty."), DolphinStatusBar::Error);
771 }
772 else {
773 m_statusBar->setMessage(i18n("The location '%1' is invalid.",location),
774 DolphinStatusBar::Error);
775 }
776 return;
777 }
778
779 // Only show the directory loading progress if the status bar does
780 // not contain another progress information. This means that
781 // the directory loading progress information has the lowest priority.
782 const QString progressText(m_statusBar->progressText());
783 m_showProgress = progressText.isEmpty() ||
784 (progressText == i18n("Loading directory..."));
785 if (m_showProgress) {
786 m_statusBar->setProgressText(i18n("Loading directory..."));
787 m_statusBar->setProgress(0);
788 }
789
790 m_dirLister->stop();
791 m_dirLister->openUrl(url, false, reload);
792 }
793
794 QString DolphinView::defaultStatusBarText() const
795 {
796 return KIO::itemsSummaryString(m_fileCount + m_folderCount,
797 m_fileCount,
798 m_folderCount,
799 0, false);
800 }
801
802 QString DolphinView::selectionStatusBarText() const
803 {
804 QString text;
805 const KFileItemList list = selectedItems();
806 if (list.isEmpty()) {
807 // when an item is triggered, it is temporary selected but selectedItems()
808 // will return an empty list
809 return QString();
810 }
811
812 int fileCount = 0;
813 int folderCount = 0;
814 KIO::filesize_t byteSize = 0;
815 KFileItemList::const_iterator it = list.begin();
816 const KFileItemList::const_iterator end = list.end();
817 while (it != end){
818 KFileItem* item = *it;
819 if (item->isDir()) {
820 ++folderCount;
821 }
822 else {
823 ++fileCount;
824 byteSize += item->size();
825 }
826 ++it;
827 }
828
829 if (folderCount > 0) {
830 text = i18np("1 Folder selected", "%1 Folders selected", folderCount);
831 if (fileCount > 0) {
832 text += ", ";
833 }
834 }
835
836 if (fileCount > 0) {
837 const QString sizeText(KIO::convertSize(byteSize));
838 text += i18np("1 File selected (%2)", "%1 Files selected (%2)", fileCount, sizeText);
839 }
840
841 return text;
842 }
843
844 void DolphinView::showFilterBar(bool show)
845 {
846 assert(m_filterBar != 0);
847 if (show) {
848 m_filterBar->show();
849 }
850 else {
851 m_filterBar->hide();
852 }
853 }
854
855 void DolphinView::updateStatusBar()
856 {
857 // As the item count information is less important
858 // in comparison with other messages, it should only
859 // be shown if:
860 // - the status bar is empty or
861 // - shows already the item count information or
862 // - shows only a not very important information
863 // - if any progress is given don't show the item count info at all
864 const QString msg(m_statusBar->message());
865 const bool updateStatusBarMsg = (msg.isEmpty() ||
866 (msg == m_statusBar->defaultText()) ||
867 (m_statusBar->type() == DolphinStatusBar::Information)) &&
868 (m_statusBar->progress() == 100);
869
870 const QString text(hasSelection() ? selectionStatusBarText() : defaultStatusBarText());
871 m_statusBar->setDefaultText(text);
872
873 if (updateStatusBarMsg) {
874 m_statusBar->setMessage(text, DolphinStatusBar::Default);
875 }
876 }
877
878 void DolphinView::requestActivation()
879 {
880 m_mainWindow->setActiveView(this);
881 }
882
883 void DolphinView::changeNameFilter(const QString& nameFilter)
884 {
885 // The name filter of KDirLister does a 'hard' filtering, which
886 // means that only the items are shown where the names match
887 // exactly the filter. This is non-transparent for the user, which
888 // just wants to have a 'soft' filtering: does the name contain
889 // the filter string?
890 QString adjustedFilter(nameFilter);
891 adjustedFilter.insert(0, '*');
892 adjustedFilter.append('*');
893
894 // Use the ProxyModel to filter:
895 // This code is #ifdefed as setNameFilter behaves
896 // slightly different than the QSortFilterProxyModel
897 // as it will not remove directories. I will ask
898 // our beloved usability experts for input
899 // -- z.
900 #if 0
901 m_dirLister->setNameFilter(adjustedFilter);
902 m_dirLister->emitChanges();
903 #else
904 m_proxyModel->setFilterRegExp( nameFilter );
905 #endif
906 }
907
908 void DolphinView::openContextMenu(const QPoint& pos)
909 {
910 KFileItem* item = 0;
911
912 const QModelIndex index = itemView()->indexAt(pos);
913 if (isValidNameIndex(index)) {
914 item = fileItem(index);
915 }
916
917 DolphinContextMenu contextMenu(this, item);
918 contextMenu.open();
919 }
920
921 void DolphinView::dropUrls(const KUrl::List& urls,
922 const QPoint& pos)
923 {
924 KFileItem* directory = 0;
925 const QModelIndex index = itemView()->indexAt(pos);
926 if (isValidNameIndex(index)) {
927 KFileItem* item = fileItem(index);
928 assert(item != 0);
929 if (item->isDir()) {
930 // the URLs are dropped above a directory
931 directory = item;
932 }
933 }
934
935 const KUrl& destination = (directory == 0) ? url() :
936 directory->url();
937 dropUrls(urls, destination);
938 }
939
940 void DolphinView::dropUrls(const KUrl::List& urls,
941 const KUrl& destination)
942 {
943 m_mainWindow->dropUrls(urls, destination);
944 }
945
946
947 void DolphinView::updateSorting(DolphinView::Sorting sorting)
948 {
949 ViewProperties props(url());
950 props.setSorting(sorting);
951
952 m_proxyModel->setSorting(sorting);
953
954 emit sortingChanged(sorting);
955 }
956
957 void DolphinView::updateSortOrder(Qt::SortOrder order)
958 {
959 ViewProperties props(url());
960 props.setSortOrder(order);
961
962 m_proxyModel->setSortOrder(order);
963
964 emit sortOrderChanged(order);
965 }
966
967 void DolphinView::emitContentsMoved()
968 {
969 emit contentsMoved(contentsX(), contentsY());
970 }
971
972 void DolphinView::updateActivationState()
973 {
974 m_urlNavigator->setActive(isActive());
975 }
976
977 void DolphinView::createView()
978 {
979 // delete current view
980 QAbstractItemView* view = itemView();
981 if (view != 0) {
982 m_topLayout->removeWidget(view);
983 view->close();
984 view->deleteLater();
985 m_iconsView = 0;
986 m_detailsView = 0;
987 }
988
989 assert(m_iconsView == 0);
990 assert(m_detailsView == 0);
991
992 // ... and recreate it representing the current mode
993 switch (m_mode) {
994 case IconsView:
995 m_iconsView = new DolphinIconsView(this, m_controller);
996 view = m_iconsView;
997 break;
998
999 case DetailsView:
1000 m_detailsView = new DolphinDetailsView(this, m_controller);
1001 view = m_detailsView;
1002 break;
1003 }
1004
1005 view->setModel(m_proxyModel);
1006 view->setSelectionMode(QAbstractItemView::ExtendedSelection);
1007
1008 new KMimeTypeResolver(view, m_dirModel);
1009 m_topLayout->insertWidget(1, view);
1010
1011 connect(view->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
1012 m_controller, SLOT(indicateSelectionChange()));
1013 connect(view->verticalScrollBar(), SIGNAL(valueChanged(int)),
1014 this, SLOT(emitContentsMoved()));
1015 connect(view->horizontalScrollBar(), SIGNAL(valueChanged(int)),
1016 this, SLOT(emitContentsMoved()));
1017 }
1018
1019 void DolphinView::selectAll(QItemSelectionModel::SelectionFlags flags)
1020 {
1021 QItemSelectionModel* selectionModel = itemView()->selectionModel();
1022 const QAbstractItemModel* itemModel = selectionModel->model();
1023
1024 const QModelIndex topLeft = itemModel->index(0, 0);
1025 const QModelIndex bottomRight = itemModel->index(itemModel->rowCount() - 1,
1026 itemModel->columnCount() - 1);
1027
1028 QItemSelection selection(topLeft, bottomRight);
1029 selectionModel->select(selection, flags);
1030 }
1031
1032 QAbstractItemView* DolphinView::itemView() const
1033 {
1034 Q_ASSERT((m_iconsView == 0) || (m_detailsView == 0));
1035 if (m_detailsView != 0) {
1036 return m_detailsView;
1037 }
1038 return m_iconsView;
1039 }
1040
1041 bool DolphinView::isValidNameIndex(const QModelIndex& index) const
1042 {
1043 return index.isValid() && (index.column() == KDirModel::Name);
1044 }
1045
1046 #include "dolphinview.moc"