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