]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinview.cpp
Update of the patch from Filip Brcic for browsing through ZIP and TAR files. One...
[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 // Prefer the local path over the URL. This assures that the
642 // volume space information is correct. Assuming that the URL is media:/sda1,
643 // and the local path is /windows/C: For the URL the space info is related
644 // to the root partition (and hence wrong) and for the local path the space
645 // info is related to the windows partition (-> correct).
646 const QString localPath(item->localPath());
647 KUrl url;
648 if (localPath.isEmpty()) {
649 url = item->url();
650 }
651 else {
652 url = localPath;
653 }
654
655 if (item->isDir()) {
656 setUrl(url);
657 }
658 else if (item->isFile()) {
659 // allow to browse through ZIP and tar files
660 KMimeType::Ptr mime = item->mimeTypePtr();
661 if (mime->is("application/x-zip")) {
662 url.setProtocol("zip");
663 setUrl(url);
664 }
665 else if (mime->is("application/x-tar") ||
666 mime->is("application/x-tarz") ||
667 mime->is("application/x-tbz") ||
668 mime->is("application/x-tgz") ||
669 mime->is("application/x-tzo")) {
670 url.setProtocol("tar");
671 setUrl(url);
672 }
673 else {
674 item->run();
675 }
676 }
677 else {
678 item->run();
679 }
680 }
681
682 void DolphinView::updateProgress(int percent)
683 {
684 if (m_showProgress) {
685 m_statusBar->setProgress(percent);
686 }
687 }
688
689 void DolphinView::updateItemCount()
690 {
691 if (m_showProgress) {
692 m_statusBar->setProgressText(QString());
693 m_statusBar->setProgress(100);
694 m_showProgress = false;
695 }
696
697 KFileItemList items(m_dirLister->items());
698 KFileItemList::const_iterator it = items.begin();
699 const KFileItemList::const_iterator end = items.end();
700
701 m_fileCount = 0;
702 m_folderCount = 0;
703
704 while (it != end) {
705 KFileItem* item = *it;
706 if (item->isDir()) {
707 ++m_folderCount;
708 }
709 else {
710 ++m_fileCount;
711 }
712 ++it;
713 }
714
715 updateStatusBar();
716
717 QTimer::singleShot(0, this, SLOT(restoreContentsPos()));
718 }
719
720 void DolphinView::generatePreviews(const KFileItemList& items)
721 {
722 if (m_controller->showPreview()) {
723 KIO::PreviewJob* job = KIO::filePreview(items, 128);
724 connect(job, SIGNAL(gotPreview(const KFileItem*, const QPixmap&)),
725 this, SLOT(showPreview(const KFileItem*, const QPixmap&)));
726 }
727 }
728
729 void DolphinView::showPreview(const KFileItem* item, const QPixmap& pixmap)
730 {
731 Q_ASSERT(item != 0);
732 const QModelIndex idx = m_dirModel->indexForItem(*item);
733 if (idx.isValid() && (idx.column() == 0)) {
734 m_dirModel->setData(idx, pixmap, Qt::DecorationRole);
735 }
736 }
737
738 void DolphinView::restoreContentsPos()
739 {
740 int index = 0;
741 const QLinkedList<UrlNavigator::HistoryElem> history = urlHistory(index);
742 if (!history.isEmpty()) {
743 QAbstractItemView* view = itemView();
744 // TODO: view->setCurrentItem(history[index].currentFileName());
745
746 QLinkedList<UrlNavigator::HistoryElem>::const_iterator it = history.begin();
747 it += index;
748 view->horizontalScrollBar()->setValue((*it).contentsX());
749 view->verticalScrollBar()->setValue((*it).contentsY());
750 }
751 }
752
753 void DolphinView::showInfoMessage(const QString& msg)
754 {
755 m_statusBar->setMessage(msg, DolphinStatusBar::Information);
756 }
757
758 void DolphinView::showErrorMessage(const QString& msg)
759 {
760 m_statusBar->setMessage(msg, DolphinStatusBar::Error);
761 }
762
763 void DolphinView::emitSelectionChangedSignal()
764 {
765 emit selectionChanged();
766 }
767
768 void DolphinView::closeFilterBar()
769 {
770 m_filterBar->hide();
771 emit showFilterBarChanged(false);
772 }
773
774 void DolphinView::startDirLister(const KUrl& url, bool reload)
775 {
776 if (!url.isValid()) {
777 const QString location(url.pathOrUrl());
778 if (location.isEmpty()) {
779 m_statusBar->setMessage(i18n("The location is empty."), DolphinStatusBar::Error);
780 }
781 else {
782 m_statusBar->setMessage(i18n("The location '%1' is invalid.",location),
783 DolphinStatusBar::Error);
784 }
785 return;
786 }
787
788 // Only show the directory loading progress if the status bar does
789 // not contain another progress information. This means that
790 // the directory loading progress information has the lowest priority.
791 const QString progressText(m_statusBar->progressText());
792 m_showProgress = progressText.isEmpty() ||
793 (progressText == i18n("Loading directory..."));
794 if (m_showProgress) {
795 m_statusBar->setProgressText(i18n("Loading directory..."));
796 m_statusBar->setProgress(0);
797 }
798
799 m_dirLister->stop();
800 m_dirLister->openUrl(url, false, reload);
801 }
802
803 QString DolphinView::defaultStatusBarText() const
804 {
805 return KIO::itemsSummaryString(m_fileCount + m_folderCount,
806 m_fileCount,
807 m_folderCount,
808 0, false);
809 }
810
811 QString DolphinView::selectionStatusBarText() const
812 {
813 QString text;
814 const KFileItemList list = selectedItems();
815 if (list.isEmpty()) {
816 // when an item is triggered, it is temporary selected but selectedItems()
817 // will return an empty list
818 return QString();
819 }
820
821 int fileCount = 0;
822 int folderCount = 0;
823 KIO::filesize_t byteSize = 0;
824 KFileItemList::const_iterator it = list.begin();
825 const KFileItemList::const_iterator end = list.end();
826 while (it != end){
827 KFileItem* item = *it;
828 if (item->isDir()) {
829 ++folderCount;
830 }
831 else {
832 ++fileCount;
833 byteSize += item->size();
834 }
835 ++it;
836 }
837
838 if (folderCount > 0) {
839 text = i18np("1 Folder selected", "%1 Folders selected", folderCount);
840 if (fileCount > 0) {
841 text += ", ";
842 }
843 }
844
845 if (fileCount > 0) {
846 const QString sizeText(KIO::convertSize(byteSize));
847 text += i18np("1 File selected (%2)", "%1 Files selected (%2)", fileCount, sizeText);
848 }
849
850 return text;
851 }
852
853 void DolphinView::showFilterBar(bool show)
854 {
855 assert(m_filterBar != 0);
856 if (show) {
857 m_filterBar->show();
858 }
859 else {
860 m_filterBar->hide();
861 }
862 }
863
864 void DolphinView::updateStatusBar()
865 {
866 // As the item count information is less important
867 // in comparison with other messages, it should only
868 // be shown if:
869 // - the status bar is empty or
870 // - shows already the item count information or
871 // - shows only a not very important information
872 // - if any progress is given don't show the item count info at all
873 const QString msg(m_statusBar->message());
874 const bool updateStatusBarMsg = (msg.isEmpty() ||
875 (msg == m_statusBar->defaultText()) ||
876 (m_statusBar->type() == DolphinStatusBar::Information)) &&
877 (m_statusBar->progress() == 100);
878
879 const QString text(hasSelection() ? selectionStatusBarText() : defaultStatusBarText());
880 m_statusBar->setDefaultText(text);
881
882 if (updateStatusBarMsg) {
883 m_statusBar->setMessage(text, DolphinStatusBar::Default);
884 }
885 }
886
887 void DolphinView::requestActivation()
888 {
889 m_mainWindow->setActiveView(this);
890 }
891
892 void DolphinView::changeNameFilter(const QString& nameFilter)
893 {
894 // The name filter of KDirLister does a 'hard' filtering, which
895 // means that only the items are shown where the names match
896 // exactly the filter. This is non-transparent for the user, which
897 // just wants to have a 'soft' filtering: does the name contain
898 // the filter string?
899 QString adjustedFilter(nameFilter);
900 adjustedFilter.insert(0, '*');
901 adjustedFilter.append('*');
902
903 // Use the ProxyModel to filter:
904 // This code is #ifdefed as setNameFilter behaves
905 // slightly different than the QSortFilterProxyModel
906 // as it will not remove directories. I will ask
907 // our beloved usability experts for input
908 // -- z.
909 #if 0
910 m_dirLister->setNameFilter(adjustedFilter);
911 m_dirLister->emitChanges();
912 #else
913 m_proxyModel->setFilterRegExp( nameFilter );
914 #endif
915 }
916
917 void DolphinView::openContextMenu(const QPoint& pos)
918 {
919 KFileItem* item = 0;
920
921 const QModelIndex index = itemView()->indexAt(pos);
922 if (isValidNameIndex(index)) {
923 item = fileItem(index);
924 }
925
926 DolphinContextMenu contextMenu(this, item);
927 contextMenu.open();
928 }
929
930 void DolphinView::dropUrls(const KUrl::List& urls,
931 const QPoint& pos)
932 {
933 KFileItem* directory = 0;
934 const QModelIndex index = itemView()->indexAt(pos);
935 if (isValidNameIndex(index)) {
936 KFileItem* item = fileItem(index);
937 assert(item != 0);
938 if (item->isDir()) {
939 // the URLs are dropped above a directory
940 directory = item;
941 }
942 }
943
944 const KUrl& destination = (directory == 0) ? url() :
945 directory->url();
946 dropUrls(urls, destination);
947 }
948
949 void DolphinView::dropUrls(const KUrl::List& urls,
950 const KUrl& destination)
951 {
952 m_mainWindow->dropUrls(urls, destination);
953 }
954
955
956 void DolphinView::updateSorting(DolphinView::Sorting sorting)
957 {
958 ViewProperties props(url());
959 props.setSorting(sorting);
960
961 m_proxyModel->setSorting(sorting);
962
963 emit sortingChanged(sorting);
964 }
965
966 void DolphinView::updateSortOrder(Qt::SortOrder order)
967 {
968 ViewProperties props(url());
969 props.setSortOrder(order);
970
971 m_proxyModel->setSortOrder(order);
972
973 emit sortOrderChanged(order);
974 }
975
976 void DolphinView::emitContentsMoved()
977 {
978 emit contentsMoved(contentsX(), contentsY());
979 }
980
981 void DolphinView::updateActivationState()
982 {
983 m_urlNavigator->setActive(isActive());
984 }
985
986 void DolphinView::createView()
987 {
988 // delete current view
989 QAbstractItemView* view = itemView();
990 if (view != 0) {
991 m_topLayout->removeWidget(view);
992 view->close();
993 view->deleteLater();
994 m_iconsView = 0;
995 m_detailsView = 0;
996 }
997
998 assert(m_iconsView == 0);
999 assert(m_detailsView == 0);
1000
1001 // ... and recreate it representing the current mode
1002 switch (m_mode) {
1003 case IconsView:
1004 m_iconsView = new DolphinIconsView(this, m_controller);
1005 view = m_iconsView;
1006 break;
1007
1008 case DetailsView:
1009 m_detailsView = new DolphinDetailsView(this, m_controller);
1010 view = m_detailsView;
1011 break;
1012 }
1013
1014 view->setModel(m_proxyModel);
1015 view->setSelectionMode(QAbstractItemView::ExtendedSelection);
1016
1017 new KMimeTypeResolver(view, m_dirModel);
1018 m_topLayout->insertWidget(1, view);
1019
1020 connect(view->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
1021 m_controller, SLOT(indicateSelectionChange()));
1022 connect(view->verticalScrollBar(), SIGNAL(valueChanged(int)),
1023 this, SLOT(emitContentsMoved()));
1024 connect(view->horizontalScrollBar(), SIGNAL(valueChanged(int)),
1025 this, SLOT(emitContentsMoved()));
1026 }
1027
1028 void DolphinView::selectAll(QItemSelectionModel::SelectionFlags flags)
1029 {
1030 QItemSelectionModel* selectionModel = itemView()->selectionModel();
1031 const QAbstractItemModel* itemModel = selectionModel->model();
1032
1033 const QModelIndex topLeft = itemModel->index(0, 0);
1034 const QModelIndex bottomRight = itemModel->index(itemModel->rowCount() - 1,
1035 itemModel->columnCount() - 1);
1036
1037 QItemSelection selection(topLeft, bottomRight);
1038 selectionModel->select(selection, flags);
1039 }
1040
1041 QAbstractItemView* DolphinView::itemView() const
1042 {
1043 Q_ASSERT((m_iconsView == 0) || (m_detailsView == 0));
1044 if (m_detailsView != 0) {
1045 return m_detailsView;
1046 }
1047 return m_iconsView;
1048 }
1049
1050 bool DolphinView::isValidNameIndex(const QModelIndex& index) const
1051 {
1052 return index.isValid() && (index.column() == KDirModel::Name);
1053 }
1054
1055 #include "dolphinview.moc"