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