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