]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/dolphinview.cpp
Don't use mixed units in size-column of details-view
[dolphin.git] / src / views / dolphinview.cpp
1 /***************************************************************************
2 * Copyright (C) 2006-2009 by Peter Penz <peter.penz19@gmail.com> *
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 <QAbstractItemView>
24 #include <QApplication>
25 #include <QBoxLayout>
26 #include <QClipboard>
27 #include <QDropEvent>
28 #include <QGraphicsSceneDragDropEvent>
29 #include <QKeyEvent>
30 #include <QItemSelection>
31 #include <QTimer>
32 #include <QScrollBar>
33
34 #include <KActionCollection>
35 #include <KColorScheme>
36 #include <KDirLister>
37 #include <KDirModel>
38 #include <KIconEffect>
39 #include <KFileItem>
40 #include <KFileItemListProperties>
41 #include <KLocale>
42 #include <kitemviews/kfileitemmodel.h>
43 #include <kitemviews/kfileitemlistview.h>
44 #include <kitemviews/kitemlistselectionmanager.h>
45 #include <kitemviews/kitemlistview.h>
46 #include <kitemviews/kitemlistcontroller.h>
47 #include <KIO/DeleteJob>
48 #include <KIO/NetAccess>
49 #include <KIO/PreviewJob>
50 #include <KJob>
51 #include <KMenu>
52 #include <KMessageBox>
53 #include <konq_fileitemcapabilities.h>
54 #include <konq_operations.h>
55 #include <konqmimedata.h>
56 #include <KToggleAction>
57 #include <KUrl>
58
59 #include "additionalinfoaccessor.h"
60 #include "dolphindirlister.h"
61 #include "dolphinnewfilemenuobserver.h"
62 #include "dolphin_detailsmodesettings.h"
63 #include "dolphin_generalsettings.h"
64 #include "dolphinitemlistcontainer.h"
65 #include "draganddrophelper.h"
66 #include "renamedialog.h"
67 #include "settings/dolphinsettings.h"
68 #include "versioncontrol/versioncontrolobserver.h"
69 #include "viewmodecontroller.h"
70 #include "viewproperties.h"
71 #include "views/tooltips/tooltipmanager.h"
72 #include "zoomlevelinfo.h"
73
74 namespace {
75 const int MaxModeEnum = DolphinView::CompactView;
76 const int MaxSortingEnum = DolphinView::SortByPath;
77 };
78
79 DolphinView::DolphinView(const KUrl& url, QWidget* parent) :
80 QWidget(parent),
81 m_active(true),
82 m_tabsForFiles(false),
83 m_assureVisibleCurrentIndex(false),
84 m_isFolderWritable(true),
85 m_url(url),
86 m_mode(DolphinView::IconsView),
87 m_additionalInfoList(),
88 m_topLayout(0),
89 m_dirLister(0),
90 m_container(0),
91 m_toolTipManager(0),
92 m_selectionChangedTimer(0),
93 m_currentItemUrl(),
94 m_restoredContentsPosition(),
95 m_createdItemUrl(),
96 m_selectedUrls(),
97 m_versionControlObserver(0)
98 {
99 m_topLayout = new QVBoxLayout(this);
100 m_topLayout->setSpacing(0);
101 m_topLayout->setMargin(0);
102
103 //m_dolphinViewController = new DolphinViewController(this);
104
105 //m_viewModeController = new ViewModeController(this);
106 //m_viewModeController->setUrl(url);
107
108 /*connect(m_viewModeController, SIGNAL(urlChanged(KUrl)),
109 this, SIGNAL(urlChanged(KUrl)));
110
111 connect(m_dolphinViewController, SIGNAL(requestContextMenu(QPoint,QList<QAction*>)),
112 this, SLOT(openContextMenu(QPoint,QList<QAction*>)));
113 connect(m_dolphinViewController, SIGNAL(urlsDropped(KFileItem,KUrl,QDropEvent*)),
114 this, SLOT(dropUrls(KFileItem,KUrl,QDropEvent*)));
115 connect(m_dolphinViewController, SIGNAL(sortingChanged(DolphinView::Sorting)),
116 this, SLOT(updateSorting(DolphinView::Sorting)));
117 connect(m_dolphinViewController, SIGNAL(sortOrderChanged(Qt::SortOrder)),
118 this, SLOT(updateSortOrder(Qt::SortOrder)));
119 connect(m_dolphinViewController, SIGNAL(sortFoldersFirstChanged(bool)),
120 this, SLOT(updateSortFoldersFirst(bool)));
121 connect(m_dolphinViewController, SIGNAL(additionalInfoChanged(QList<DolphinView::AdditionalInfo>)),
122 this, SLOT(updateAdditionalInfo(QList<DolphinView::AdditionalInfo>)));*/
123 //connect(m_dolphinViewController, SIGNAL(itemActivated(KFileItem)),
124 // this, SLOT(triggerItem(KFileItem)));
125 //connect(m_dolphinViewController, SIGNAL(tabRequested(KUrl)),
126 // this, SIGNAL(tabRequested(KUrl)));
127 /*connect(m_dolphinViewController, SIGNAL(activated()),
128 this, SLOT(activate()));
129 connect(m_dolphinViewController, SIGNAL(itemEntered(KFileItem)),
130 this, SLOT(showHoverInformation(KFileItem)));
131 connect(m_dolphinViewController, SIGNAL(viewportEntered()),
132 this, SLOT(clearHoverInformation()));
133 connect(m_dolphinViewController, SIGNAL(urlChangeRequested(KUrl)),
134 this, SLOT(slotUrlChangeRequested(KUrl)));*/
135
136 // When a new item has been created by the "Create New..." menu, the item should
137 // get selected and it must be assured that the item will get visible. As the
138 // creation is done asynchronously, several signals must be checked:
139 connect(&DolphinNewFileMenuObserver::instance(), SIGNAL(itemCreated(KUrl)),
140 this, SLOT(observeCreatedItem(KUrl)));
141
142 m_selectionChangedTimer = new QTimer(this);
143 m_selectionChangedTimer->setSingleShot(true);
144 m_selectionChangedTimer->setInterval(300);
145 connect(m_selectionChangedTimer, SIGNAL(timeout()),
146 this, SLOT(emitSelectionChangedSignal()));
147
148 m_dirLister = new DolphinDirLister(this);
149 m_dirLister->setAutoUpdate(true);
150 m_dirLister->setDelayedMimeTypes(true);
151
152 connect(m_dirLister, SIGNAL(redirection(KUrl,KUrl)), this, SLOT(slotRedirection(KUrl,KUrl)));
153 connect(m_dirLister, SIGNAL(started(KUrl)), this, SLOT(slotDirListerStarted(KUrl)));
154 connect(m_dirLister, SIGNAL(refreshItems(QList<QPair<KFileItem,KFileItem> >)),
155 this, SLOT(slotRefreshItems()));
156
157 connect(m_dirLister, SIGNAL(clear()), this, SIGNAL(itemCountChanged()));
158 connect(m_dirLister, SIGNAL(newItems(KFileItemList)), this, SIGNAL(itemCountChanged()));
159 connect(m_dirLister, SIGNAL(infoMessage(QString)), this, SIGNAL(infoMessage(QString)));
160 connect(m_dirLister, SIGNAL(errorMessage(QString)), this, SIGNAL(infoMessage(QString)));
161 connect(m_dirLister, SIGNAL(percent(int)), this, SIGNAL(pathLoadingProgress(int)));
162 connect(m_dirLister, SIGNAL(urlIsFileError(KUrl)), this, SIGNAL(urlIsFileError(KUrl)));
163 connect(m_dirLister, SIGNAL(itemsDeleted(KFileItemList)), this, SIGNAL(itemCountChanged()));
164
165 m_container = new DolphinItemListContainer(m_dirLister, this);
166 m_container->setVisibleRoles(QList<QByteArray>() << "name");
167 m_container->installEventFilter(this);
168
169 KItemListController* controller = m_container->controller();
170 controller->setSelectionBehavior(KItemListController::MultiSelection);
171 connect(controller, SIGNAL(itemActivated(int)),
172 this, SLOT(slotItemActivated(int)));
173 connect(controller, SIGNAL(itemMiddleClicked(int)), this, SLOT(slotItemMiddleClicked(int)));
174 connect(controller, SIGNAL(itemContextMenuRequested(int,QPointF)), this, SLOT(slotItemContextMenuRequested(int,QPointF)));
175 connect(controller, SIGNAL(viewContextMenuRequested(QPointF)), this, SLOT(slotViewContextMenuRequested(QPointF)));
176 connect(controller, SIGNAL(headerContextMenuRequested(QPointF)), this, SLOT(slotHeaderContextMenuRequested(QPointF)));
177 connect(controller, SIGNAL(itemExpansionToggleClicked(int)), this, SLOT(slotItemExpansionToggleClicked(int)));
178 connect(controller, SIGNAL(itemHovered(int)), this, SLOT(slotItemHovered(int)));
179 connect(controller, SIGNAL(itemUnhovered(int)), this, SLOT(slotItemUnhovered(int)));
180 connect(controller, SIGNAL(itemDropEvent(int,QGraphicsSceneDragDropEvent*)), this, SLOT(slotItemDropEvent(int,QGraphicsSceneDragDropEvent*)));
181 connect(controller, SIGNAL(modelChanged(KItemModelBase*,KItemModelBase*)), this, SLOT(slotModelChanged(KItemModelBase*,KItemModelBase*)));
182
183 KFileItemModel* model = fileItemModel();
184 if (model) {
185 connect(model, SIGNAL(loadingCompleted()), this, SLOT(slotLoadingCompleted()));
186 }
187
188 KItemListSelectionManager* selectionManager = controller->selectionManager();
189 connect(selectionManager, SIGNAL(selectionChanged(QSet<int>,QSet<int>)),
190 this, SLOT(slotSelectionChanged(QSet<int>,QSet<int>)));
191
192 m_toolTipManager = new ToolTipManager(this);
193
194 m_versionControlObserver = new VersionControlObserver(this);
195 m_versionControlObserver->setModel(model);
196 connect(m_versionControlObserver, SIGNAL(infoMessage(QString)), this, SIGNAL(infoMessage(QString)));
197 connect(m_versionControlObserver, SIGNAL(errorMessage(QString)), this, SIGNAL(errorMessage(QString)));
198 connect(m_versionControlObserver, SIGNAL(operationCompletedMessage(QString)), this, SIGNAL(operationCompletedMessage(QString)));
199
200 applyViewProperties();
201 m_topLayout->addWidget(m_container);
202
203 loadDirectory(url);
204 }
205
206 DolphinView::~DolphinView()
207 {
208 }
209
210 KUrl DolphinView::url() const
211 {
212 return m_url;
213 }
214
215 void DolphinView::setActive(bool active)
216 {
217 if (active == m_active) {
218 return;
219 }
220
221 m_active = active;
222
223 QColor color = KColorScheme(QPalette::Active, KColorScheme::View).background().color();
224 if (!active) {
225 color.setAlpha(150);
226 }
227
228 /*QAbstractItemView* view = m_viewAccessor.itemView();
229 QWidget* viewport = view ? view->viewport() : 0;
230 if (viewport) {
231 QPalette palette;
232 palette.setColor(viewport->backgroundRole(), color);
233 viewport->setPalette(palette);
234 }*/
235
236 update();
237
238 if (active) {
239 //if (view) {
240 // view->setFocus();
241 //}
242 emit activated();
243 emit writeStateChanged(m_isFolderWritable);
244 }
245
246 //m_viewModeController->indicateActivationChange(active);
247 }
248
249 bool DolphinView::isActive() const
250 {
251 return m_active;
252 }
253
254 void DolphinView::setMode(Mode mode)
255 {
256 if (mode != m_mode) {
257 ViewProperties props(url());
258 props.setViewMode(mode);
259 props.save();
260
261 applyViewProperties();
262 }
263 }
264
265 DolphinView::Mode DolphinView::mode() const
266 {
267 return m_mode;
268 }
269
270 void DolphinView::setPreviewsShown(bool show)
271 {
272 if (previewsShown() == show) {
273 return;
274 }
275
276 ViewProperties props(url());
277 props.setPreviewsShown(show);
278
279 m_container->setPreviewsShown(show);
280 emit previewsShownChanged(show);
281 }
282
283 bool DolphinView::previewsShown() const
284 {
285 return m_container->previewsShown();
286 }
287
288 void DolphinView::setHiddenFilesShown(bool show)
289 {
290 if (m_dirLister->showingDotFiles() == show) {
291 return;
292 }
293
294 const KFileItemList itemList = selectedItems();
295 m_selectedUrls.clear();
296 m_selectedUrls = itemList.urlList();
297
298 ViewProperties props(url());
299 props.setHiddenFilesShown(show);
300
301 m_dirLister->setShowingDotFiles(show);
302 m_dirLister->emitChanges();
303 emit hiddenFilesShownChanged(show);
304 }
305
306 bool DolphinView::hiddenFilesShown() const
307 {
308 return m_dirLister->showingDotFiles();
309 }
310
311 void DolphinView::setGroupedSorting(bool grouped)
312 {
313 if (grouped == groupedSorting()) {
314 return;
315 }
316
317 ViewProperties props(url());
318 props.setGroupedSorting(grouped);
319 props.save();
320
321 m_container->controller()->model()->setGroupedSorting(grouped);
322
323 emit groupedSortingChanged(grouped);
324 }
325
326 bool DolphinView::groupedSorting() const
327 {
328 return fileItemModel()->groupedSorting();
329 }
330
331 KFileItemList DolphinView::items() const
332 {
333 return m_dirLister->items();
334 }
335
336 KFileItemList DolphinView::selectedItems() const
337 {
338 const KFileItemModel* model = fileItemModel();
339 const KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager();
340 const QSet<int> selectedIndexes = selectionManager->selectedItems();
341
342 QList<int> sortedIndexes = selectedIndexes.toList();
343 qSort(sortedIndexes);
344
345 KFileItemList selectedItems;
346 QListIterator<int> it(sortedIndexes);
347 while (it.hasNext()) {
348 const int index = it.next();
349 selectedItems.append(model->fileItem(index));
350 }
351 return selectedItems;
352 }
353
354 int DolphinView::selectedItemsCount() const
355 {
356 const KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager();
357 return selectionManager->selectedItems().count();
358 }
359
360 void DolphinView::markUrlsAsSelected(const QList<KUrl>& urls)
361 {
362 m_selectedUrls = urls;
363 }
364
365 void DolphinView::markUrlAsCurrent(const KUrl& url)
366 {
367 m_currentItemUrl = url;
368 }
369
370 void DolphinView::setItemSelectionEnabled(const QRegExp& pattern, bool enabled)
371 {
372 const KItemListSelectionManager::SelectionMode mode = enabled
373 ? KItemListSelectionManager::Select
374 : KItemListSelectionManager::Deselect;
375 const KFileItemModel* model = fileItemModel();
376 KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager();
377
378 for (int index = 0; index < model->count(); index++) {
379 const KFileItem item = model->fileItem(index);
380 if (pattern.exactMatch(item.name())) {
381 // An alternative approach would be to store the matching items in a QSet<int> and
382 // select them in one go after the loop, but we'd need a new function
383 // KItemListSelectionManager::setSelected(QSet<int>, SelectionMode mode)
384 // for that.
385 selectionManager->setSelected(index, 1, mode);
386 }
387 }
388 }
389
390 void DolphinView::setZoomLevel(int level)
391 {
392 const int oldZoomLevel = zoomLevel();
393 m_container->setZoomLevel(level);
394 if (zoomLevel() != oldZoomLevel) {
395 emit zoomLevelChanged(zoomLevel(), oldZoomLevel);
396 }
397 }
398
399 int DolphinView::zoomLevel() const
400 {
401 return m_container->zoomLevel();
402 }
403
404 void DolphinView::setSorting(Sorting sorting)
405 {
406 if (sorting != this->sorting()) {
407 updateSorting(sorting);
408 }
409 }
410
411 DolphinView::Sorting DolphinView::sorting() const
412 {
413 KItemModelBase* model = m_container->controller()->model();
414 return sortingForSortRole(model->sortRole());
415 }
416
417 void DolphinView::setSortOrder(Qt::SortOrder order)
418 {
419 if (sortOrder() != order) {
420 updateSortOrder(order);
421 }
422 }
423
424 Qt::SortOrder DolphinView::sortOrder() const
425 {
426 KItemModelBase* model = fileItemModel();
427 return model->sortOrder();
428 }
429
430 void DolphinView::setSortFoldersFirst(bool foldersFirst)
431 {
432 if (sortFoldersFirst() != foldersFirst) {
433 updateSortFoldersFirst(foldersFirst);
434 }
435 }
436
437 bool DolphinView::sortFoldersFirst() const
438 {
439 KFileItemModel* model = fileItemModel();
440 return model->sortFoldersFirst();
441 }
442
443 void DolphinView::setAdditionalInfoList(const QList<AdditionalInfo>& info)
444 {
445 const QList<AdditionalInfo> previousList = info;
446
447 ViewProperties props(url());
448 props.setAdditionalInfoList(info);
449
450 m_additionalInfoList = info;
451 applyAdditionalInfoListToView();
452
453 emit additionalInfoListChanged(m_additionalInfoList, previousList);
454 }
455
456 QList<DolphinView::AdditionalInfo> DolphinView::additionalInfoList() const
457 {
458 return m_additionalInfoList;
459 }
460
461 void DolphinView::reload()
462 {
463 QByteArray viewState;
464 QDataStream saveStream(&viewState, QIODevice::WriteOnly);
465 saveState(saveStream);
466
467 const KFileItemList itemList = selectedItems();
468 m_selectedUrls.clear();
469 m_selectedUrls = itemList.urlList();
470
471 setUrl(url());
472 loadDirectory(url(), true);
473
474 QDataStream restoreStream(viewState);
475 restoreState(restoreStream);
476 }
477
478 void DolphinView::stopLoading()
479 {
480 m_dirLister->stop();
481 }
482
483 void DolphinView::refresh()
484 {
485 const bool oldActivationState = m_active;
486 const int oldZoomLevel = zoomLevel();
487 m_active = true;
488
489 applyViewProperties();
490 reload();
491
492 setActive(oldActivationState);
493 updateZoomLevel(oldZoomLevel);
494 }
495
496 void DolphinView::setNameFilter(const QString& nameFilter)
497 {
498 Q_UNUSED(nameFilter);
499 //m_viewModeController->setNameFilter(nameFilter);
500 }
501
502 QString DolphinView::nameFilter() const
503 {
504 return QString(); //m_viewModeController->nameFilter();
505 }
506
507 void DolphinView::calculateItemCount(int& fileCount,
508 int& folderCount,
509 KIO::filesize_t& totalFileSize) const
510 {
511 foreach (const KFileItem& item, m_dirLister->items()) {
512 if (item.isDir()) {
513 ++folderCount;
514 } else {
515 ++fileCount;
516 totalFileSize += item.size();
517 }
518 }
519 }
520
521 QString DolphinView::statusBarText() const
522 {
523 QString summary;
524 QString foldersText;
525 QString filesText;
526
527 int folderCount = 0;
528 int fileCount = 0;
529 KIO::filesize_t totalFileSize = 0;
530
531 if (hasSelection()) {
532 // Give a summary of the status of the selected files
533 const KFileItemList list = selectedItems();
534 foreach (const KFileItem& item, list) {
535 if (item.isDir()) {
536 ++folderCount;
537 } else {
538 ++fileCount;
539 totalFileSize += item.size();
540 }
541 }
542
543 if (folderCount + fileCount == 1) {
544 // If only one item is selected, show the filename
545 filesText = i18nc("@info:status", "<filename>%1</filename> selected", list.first().text());
546 } else {
547 // At least 2 items are selected
548 foldersText = i18ncp("@info:status", "1 Folder selected", "%1 Folders selected", folderCount);
549 filesText = i18ncp("@info:status", "1 File selected", "%1 Files selected", fileCount);
550 }
551 } else {
552 calculateItemCount(fileCount, folderCount, totalFileSize);
553 foldersText = i18ncp("@info:status", "1 Folder", "%1 Folders", folderCount);
554 filesText = i18ncp("@info:status", "1 File", "%1 Files", fileCount);
555 }
556
557 if (fileCount > 0 && folderCount > 0) {
558 summary = i18nc("@info:status folders, files (size)", "%1, %2 (%3)",
559 foldersText, filesText, fileSizeText(totalFileSize));
560 } else if (fileCount > 0) {
561 summary = i18nc("@info:status files (size)", "%1 (%2)", filesText, fileSizeText(totalFileSize));
562 } else if (folderCount > 0) {
563 summary = foldersText;
564 }
565
566 return summary;
567 }
568
569 QList<QAction*> DolphinView::versionControlActions(const KFileItemList& items) const
570 {
571 QList<QAction*> actions;
572
573 if (items.isEmpty()) {
574 const KFileItem item = fileItemModel()->rootItem();
575 actions = m_versionControlObserver->actions(KFileItemList() << item);
576 } else {
577 actions = m_versionControlObserver->actions(items);
578 }
579
580 return actions;
581 }
582
583 void DolphinView::setUrl(const KUrl& url)
584 {
585 if (url == m_url) {
586 return;
587 }
588
589 emit urlAboutToBeChanged(url);
590 m_url = url;
591
592 if (GeneralSettings::showToolTips()) {
593 m_toolTipManager->hideToolTip();
594 }
595
596 // It is important to clear the items from the model before
597 // applying the view properties, otherwise expensive operations
598 // might be done on the existing items although they get cleared
599 // anyhow afterwards by loadDirectory().
600 fileItemModel()->clear();
601 applyViewProperties();
602 loadDirectory(url);
603
604 emit urlChanged(url);
605 }
606
607 void DolphinView::selectAll()
608 {
609 KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager();
610 selectionManager->setSelected(0, fileItemModel()->count());
611 }
612
613 void DolphinView::invertSelection()
614 {
615 KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager();
616 selectionManager->setSelected(0, fileItemModel()->count(), KItemListSelectionManager::Toggle);
617 }
618
619 void DolphinView::clearSelection()
620 {
621 m_container->controller()->selectionManager()->clearSelection();
622 }
623
624 void DolphinView::renameSelectedItems()
625 {
626 KFileItemList items = selectedItems();
627 const int itemCount = items.count();
628 if (itemCount < 1) {
629 return;
630 }
631
632 /*if ((itemCount == 1) && DolphinSettings::instance().generalSettings()->renameInline()) {
633 const QModelIndex dirIndex = m_viewAccessor.dirModel()->indexForItem(items.first());
634 const QModelIndex proxyIndex = m_viewAccessor.proxyModel()->mapFromSource(dirIndex);
635 m_viewAccessor.itemView()->edit(proxyIndex);
636 } else {*/
637 RenameDialog* dialog = new RenameDialog(this, items);
638 dialog->setAttribute(Qt::WA_DeleteOnClose);
639 dialog->show();
640 dialog->raise();
641 dialog->activateWindow();
642 //}
643
644 // assure that the current index remains visible when KDirLister
645 // will notify the view about changed items
646 m_assureVisibleCurrentIndex = true;
647 }
648
649 void DolphinView::trashSelectedItems()
650 {
651 const KUrl::List list = simplifiedSelectedUrls();
652 KonqOperations::del(this, KonqOperations::TRASH, list);
653 }
654
655 void DolphinView::deleteSelectedItems()
656 {
657 const KUrl::List list = simplifiedSelectedUrls();
658 const bool del = KonqOperations::askDeleteConfirmation(list,
659 KonqOperations::DEL,
660 KonqOperations::DEFAULT_CONFIRMATION,
661 this);
662
663 if (del) {
664 KIO::Job* job = KIO::del(list);
665 connect(job, SIGNAL(result(KJob*)),
666 this, SLOT(slotDeleteFileFinished(KJob*)));
667 }
668 }
669
670 void DolphinView::cutSelectedItems()
671 {
672 QMimeData* mimeData = selectionMimeData();
673 KonqMimeData::addIsCutSelection(mimeData, true);
674 QApplication::clipboard()->setMimeData(mimeData);
675 }
676
677 void DolphinView::copySelectedItems()
678 {
679 QMimeData* mimeData = selectionMimeData();
680 QApplication::clipboard()->setMimeData(mimeData);
681 }
682
683 void DolphinView::paste()
684 {
685 pasteToUrl(url());
686 }
687
688 void DolphinView::pasteIntoFolder()
689 {
690 const KFileItemList items = selectedItems();
691 if ((items.count() == 1) && items.first().isDir()) {
692 pasteToUrl(items.first().url());
693 }
694 }
695
696 bool DolphinView::eventFilter(QObject* watched, QEvent* event)
697 {
698 switch (event->type()) {
699 case QEvent::FocusIn:
700 if (watched == m_container) {
701 setActive(true);
702 }
703 break;
704
705 default:
706 break;
707 }
708
709 return QWidget::eventFilter(watched, event);
710 }
711
712 void DolphinView::wheelEvent(QWheelEvent* event)
713 {
714 if (event->modifiers().testFlag(Qt::ControlModifier)) {
715 const int numDegrees = event->delta() / 8;
716 const int numSteps = numDegrees / 15;
717
718 setZoomLevel(zoomLevel() + numSteps);
719 event->accept();
720 } else {
721 event->ignore();
722 }
723 }
724
725 void DolphinView::activate()
726 {
727 setActive(true);
728 }
729
730 void DolphinView::slotItemActivated(int index)
731 {
732 Q_UNUSED(index);
733
734 const KFileItemList items = selectedItems();
735 if (items.isEmpty()) {
736 return;
737 }
738
739 if (items.count() == 1) {
740 emit itemActivated(items.at(0)); // caught by DolphinViewContainer or DolphinPart
741 } else {
742 foreach (const KFileItem& item, items) {
743 if (item.isDir()) {
744 emit tabRequested(item.url());
745 } else {
746 emit itemActivated(item);
747 }
748 }
749 }
750 }
751
752 void DolphinView::slotItemMiddleClicked(int index)
753 {
754 const KFileItem item = fileItemModel()->fileItem(index);
755 if (item.isDir() || isTabsForFilesEnabled()) {
756 emit tabRequested(item.url());
757 }
758 }
759
760 void DolphinView::slotItemContextMenuRequested(int index, const QPointF& pos)
761 {
762 if (GeneralSettings::showToolTips()) {
763 m_toolTipManager->hideToolTip();
764 }
765 const KFileItem item = fileItemModel()->fileItem(index);
766 emit requestContextMenu(pos.toPoint(), item, url(), QList<QAction*>());
767 }
768
769 void DolphinView::slotViewContextMenuRequested(const QPointF& pos)
770 {
771 if (GeneralSettings::showToolTips()) {
772 m_toolTipManager->hideToolTip();
773 }
774 emit requestContextMenu(pos.toPoint(), KFileItem(), url(), QList<QAction*>());
775 }
776
777 void DolphinView::slotHeaderContextMenuRequested(const QPointF& pos)
778 {
779 QWeakPointer<KMenu> menu = new KMenu(QApplication::activeWindow());
780
781 KItemListView* view = m_container->controller()->view();
782 const QSet<QByteArray> visibleRolesSet = view->visibleRoles().toSet();
783
784 // Add all roles to the menu that can be shown or hidden by the user
785 const AdditionalInfoAccessor& infoAccessor = AdditionalInfoAccessor::instance();
786 const QList<DolphinView::AdditionalInfo> keys = infoAccessor.keys();
787 foreach (const DolphinView::AdditionalInfo info, keys) {
788 const QByteArray& role = infoAccessor.role(info);
789 if (role != "name") {
790 const QString text = fileItemModel()->roleDescription(role);
791
792 QAction* action = menu.data()->addAction(text);
793 action->setCheckable(true);
794 action->setChecked(visibleRolesSet.contains(role));
795 action->setData(info);
796 }
797 }
798
799 QAction* action = menu.data()->exec(pos.toPoint());
800 if (action) {
801 // Show or hide the selected role
802 const DolphinView::AdditionalInfo info =
803 static_cast<DolphinView::AdditionalInfo>(action->data().toInt());
804
805 ViewProperties props(url());
806 QList<DolphinView::AdditionalInfo> infoList = props.additionalInfoList();
807
808 const QByteArray selectedRole = infoAccessor.role(info);
809 QList<QByteArray> visibleRoles = view->visibleRoles();
810
811 if (action->isChecked()) {
812 const int index = keys.indexOf(info) + 1;
813 visibleRoles.insert(index, selectedRole);
814 infoList.insert(index, info);
815 } else {
816 visibleRoles.removeOne(selectedRole);
817 infoList.removeOne(info);
818 }
819
820 view->setVisibleRoles(visibleRoles);
821 props.setAdditionalInfoList(infoList);
822 }
823
824 delete menu.data();
825 }
826
827 void DolphinView::slotItemExpansionToggleClicked(int index)
828 {
829 // TODO: When doing a model->setExpanded(false) it should
830 // be checked here whether the current index is part of the
831 // closed sub-tree. If this is the case, the current index
832 // should be adjusted to the parent index.
833 KFileItemModel* model = fileItemModel();
834 const bool expanded = model->isExpanded(index);
835 model->setExpanded(index, !expanded);
836 }
837
838 void DolphinView::slotItemHovered(int index)
839 {
840 const KFileItem item = fileItemModel()->fileItem(index);
841
842 if (GeneralSettings::showToolTips()) {
843 QRectF itemRect = m_container->controller()->view()->itemRect(index);
844 const QPoint pos = m_container->mapToGlobal(itemRect.topLeft().toPoint());
845 itemRect.moveTo(pos);
846
847 m_toolTipManager->showToolTip(item, itemRect);
848 }
849
850 emit requestItemInfo(item);
851 }
852
853 void DolphinView::slotItemUnhovered(int index)
854 {
855 Q_UNUSED(index);
856 if (GeneralSettings::showToolTips()) {
857 m_toolTipManager->hideToolTip();
858 }
859 emit requestItemInfo(KFileItem());
860 }
861
862 void DolphinView::slotItemDropEvent(int index, QGraphicsSceneDragDropEvent* event)
863 {
864 const KFileItem destItem = fileItemModel()->fileItem(index);
865
866 QDropEvent dropEvent(event->pos().toPoint(),
867 event->possibleActions(),
868 event->mimeData(),
869 event->buttons(),
870 event->modifiers());
871
872 DragAndDropHelper::dropUrls(destItem, url(), &dropEvent, this);
873 }
874
875 void DolphinView::slotModelChanged(KItemModelBase* current, KItemModelBase* previous)
876 {
877 if (previous != 0) {
878 disconnect(previous, SIGNAL(loadingCompleted()), this, SLOT(slotLoadingCompleted()));
879 }
880
881 Q_ASSERT(qobject_cast<KFileItemModel*>(current));
882 connect(current, SIGNAL(loadingCompleted()), this, SLOT(slotLoadingCompleted()));
883
884 KFileItemModel* fileItemModel = static_cast<KFileItemModel*>(current);
885 m_versionControlObserver->setModel(fileItemModel);
886 }
887
888 void DolphinView::slotSelectionChanged(const QSet<int>& current, const QSet<int>& previous)
889 {
890 const int currentCount = current.count();
891 const int previousCount = previous.count();
892 const bool selectionStateChanged = (currentCount == 0 && previousCount > 0) ||
893 (currentCount > 0 && previousCount == 0);
894
895 // If nothing has been selected before and something got selected (or if something
896 // was selected before and now nothing is selected) the selectionChangedSignal must
897 // be emitted asynchronously as fast as possible to update the edit-actions.
898 m_selectionChangedTimer->setInterval(selectionStateChanged ? 0 : 300);
899 m_selectionChangedTimer->start();
900 }
901
902 void DolphinView::emitSelectionChangedSignal()
903 {
904 m_selectionChangedTimer->stop();
905 emit selectionChanged(selectedItems());
906 }
907
908 void DolphinView::dropUrls(const KFileItem& destItem,
909 const KUrl& destPath,
910 QDropEvent* event)
911 {
912 Q_UNUSED(destItem);
913 Q_UNUSED(destPath);
914 markPastedUrlsAsSelected(event->mimeData());
915 //DragAndDropHelper::instance().dropUrls(destItem, destPath, event, this);
916 }
917
918 void DolphinView::updateSorting(DolphinView::Sorting sorting)
919 {
920 ViewProperties props(url());
921 props.setSorting(sorting);
922
923 KItemModelBase* model = m_container->controller()->model();
924 model->setSortRole(sortRoleForSorting(sorting));
925
926 emit sortingChanged(sorting);
927 }
928
929 void DolphinView::updateSortOrder(Qt::SortOrder order)
930 {
931 ViewProperties props(url());
932 props.setSortOrder(order);
933
934 KItemModelBase* model = fileItemModel();
935 model->setSortOrder(order);
936
937 emit sortOrderChanged(order);
938 }
939
940 void DolphinView::updateSortFoldersFirst(bool foldersFirst)
941 {
942 ViewProperties props(url());
943 props.setSortFoldersFirst(foldersFirst);
944
945 KFileItemModel* model = fileItemModel();
946 model->setSortFoldersFirst(foldersFirst);
947
948 emit sortFoldersFirstChanged(foldersFirst);
949 }
950
951 QPair<bool, QString> DolphinView::pasteInfo() const
952 {
953 return KonqOperations::pasteInfo(url());
954 }
955
956 void DolphinView::setTabsForFilesEnabled(bool tabsForFiles)
957 {
958 m_tabsForFiles = tabsForFiles;
959 }
960
961 bool DolphinView::isTabsForFilesEnabled() const
962 {
963 return m_tabsForFiles;
964 }
965
966 bool DolphinView::itemsExpandable() const
967 {
968 return m_mode == DetailsView;
969 }
970
971 void DolphinView::restoreState(QDataStream& stream)
972 {
973 // Restore the current item that had the keyboard focus
974 stream >> m_currentItemUrl;
975
976 // Restore the view position
977 stream >> m_restoredContentsPosition;
978
979 // Restore expanded folders (only relevant for the details view - will be ignored by the view in other view modes)
980 QSet<KUrl> urls;
981 stream >> urls;
982 fileItemModel()->restoreExpandedUrls(urls);
983 }
984
985 void DolphinView::saveState(QDataStream& stream)
986 {
987 // Save the current item that has the keyboard focus
988 const int currentIndex = m_container->controller()->selectionManager()->currentItem();
989 if (currentIndex != -1) {
990 KFileItem item = fileItemModel()->fileItem(currentIndex);
991 KUrl currentItemUrl = item.url();
992 stream << currentItemUrl;
993 } else {
994 stream << KUrl();
995 }
996
997 // Save view position
998 const qreal x = m_container->horizontalScrollBar()->value();
999 const qreal y = m_container->verticalScrollBar()->value();
1000 stream << QPoint(x, y);
1001
1002 // Save expanded folders (only relevant for the details view - the set will be empty in other view modes)
1003 stream << fileItemModel()->expandedUrls();
1004 }
1005
1006 bool DolphinView::hasSelection() const
1007 {
1008 return m_container->controller()->selectionManager()->hasSelection();
1009 }
1010
1011 KFileItem DolphinView::rootItem() const
1012 {
1013 return m_dirLister->rootItem();
1014 }
1015
1016 void DolphinView::observeCreatedItem(const KUrl& url)
1017 {
1018 m_createdItemUrl = url;
1019 //connect(m_dirModel, SIGNAL(rowsInserted(QModelIndex,int,int)),
1020 // this, SLOT(selectAndScrollToCreatedItem()));
1021 }
1022
1023 void DolphinView::selectAndScrollToCreatedItem()
1024 {
1025 /*const QModelIndex dirIndex = m_viewAccessor.dirModel()->indexForUrl(m_createdItemUrl);
1026 if (dirIndex.isValid()) {
1027 const QModelIndex proxyIndex = m_viewAccessor.proxyModel()->mapFromSource(dirIndex);
1028 QAbstractItemView* view = m_viewAccessor.itemView();
1029 if (view) {
1030 view->setCurrentIndex(proxyIndex);
1031 }
1032 }
1033
1034 disconnect(m_viewAccessor.dirModel(), SIGNAL(rowsInserted(QModelIndex,int,int)),
1035 this, SLOT(selectAndScrollToCreatedItem()));*/
1036 m_createdItemUrl = KUrl();
1037 }
1038
1039 void DolphinView::slotRedirection(const KUrl& oldUrl, const KUrl& newUrl)
1040 {
1041 if (oldUrl.equals(url(), KUrl::CompareWithoutTrailingSlash)) {
1042 emit redirection(oldUrl, newUrl);
1043 m_url = newUrl; // #186947
1044 }
1045 }
1046
1047 void DolphinView::updateViewState()
1048 {
1049 if (m_currentItemUrl != KUrl()) {
1050 KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager();
1051 const int currentIndex = fileItemModel()->index(m_currentItemUrl);
1052 if (currentIndex != -1) {
1053 selectionManager->setCurrentItem(currentIndex);
1054 } else {
1055 selectionManager->setCurrentItem(0);
1056 }
1057 m_currentItemUrl = KUrl();
1058 }
1059
1060 if (!m_restoredContentsPosition.isNull()) {
1061 const int x = m_restoredContentsPosition.x();
1062 const int y = m_restoredContentsPosition.y();
1063 m_restoredContentsPosition = QPoint();
1064
1065 m_container->horizontalScrollBar()->setValue(x);
1066 m_container->verticalScrollBar()->setValue(y);
1067 }
1068
1069 if (!m_selectedUrls.isEmpty()) {
1070 KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager();
1071 QSet<int> selectedItems = selectionManager->selectedItems();
1072 const KFileItemModel* model = fileItemModel();
1073
1074 foreach (const KUrl& url, m_selectedUrls) {
1075 const int index = model->index(url);
1076 if (index >= 0) {
1077 selectedItems.insert(index);
1078 }
1079 }
1080
1081 selectionManager->setSelectedItems(selectedItems);
1082 m_selectedUrls.clear();
1083 }
1084 }
1085
1086 void DolphinView::showHoverInformation(const KFileItem& item)
1087 {
1088 emit requestItemInfo(item);
1089 }
1090
1091 void DolphinView::clearHoverInformation()
1092 {
1093 emit requestItemInfo(KFileItem());
1094 }
1095
1096 void DolphinView::slotDeleteFileFinished(KJob* job)
1097 {
1098 if (job->error() == 0) {
1099 emit operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
1100 } else if (job->error() != KIO::ERR_USER_CANCELED) {
1101 emit errorMessage(job->errorString());
1102 }
1103 }
1104
1105 void DolphinView::slotDirListerStarted(const KUrl& url)
1106 {
1107 // Disable the writestate temporary until it can be determined in a fast way
1108 // in DolphinView::slotLoadingCompleted()
1109 if (m_isFolderWritable) {
1110 m_isFolderWritable = false;
1111 emit writeStateChanged(m_isFolderWritable);
1112 }
1113
1114 emit startedPathLoading(url);
1115 }
1116
1117 void DolphinView::slotLoadingCompleted()
1118 {
1119 // Update the view-state. This has to be done using a Qt::QueuedConnection
1120 // because the view might not be in its final state yet (the view also
1121 // listens to the completed()-signal from KDirLister and the order of
1122 // of slots is undefined).
1123 QTimer::singleShot(0, this, SLOT(updateViewState()));
1124
1125 emit finishedPathLoading(url());
1126
1127 updateWritableState();
1128 }
1129
1130 void DolphinView::slotRefreshItems()
1131 {
1132 if (m_assureVisibleCurrentIndex) {
1133 m_assureVisibleCurrentIndex = false;
1134 //QAbstractItemView* view = m_viewAccessor.itemView();
1135 //if (view) {
1136 // m_viewAccessor.itemView()->scrollTo(m_viewAccessor.itemView()->currentIndex());
1137 //}
1138 }
1139 }
1140
1141 KFileItemModel* DolphinView::fileItemModel() const
1142 {
1143 return static_cast<KFileItemModel*>(m_container->controller()->model());
1144 }
1145
1146 void DolphinView::loadDirectory(const KUrl& url, bool reload)
1147 {
1148 if (!url.isValid()) {
1149 const QString location(url.pathOrUrl());
1150 if (location.isEmpty()) {
1151 emit errorMessage(i18nc("@info:status", "The location is empty."));
1152 } else {
1153 emit errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location));
1154 }
1155 return;
1156 }
1157
1158 m_dirLister->openUrl(url, reload ? KDirLister::Reload : KDirLister::NoFlags);
1159 }
1160
1161 void DolphinView::applyViewProperties()
1162 {
1163 m_container->beginTransaction();
1164
1165 const ViewProperties props(url());
1166
1167 const Mode mode = props.viewMode();
1168 if (m_mode != mode) {
1169 const Mode previousMode = m_mode;
1170 m_mode = mode;
1171
1172 // Changing the mode might result in changing
1173 // the zoom level. Remember the old zoom level so
1174 // that zoomLevelChanged() can get emitted.
1175 const int oldZoomLevel = m_container->zoomLevel();
1176
1177 switch (m_mode) {
1178 case IconsView: m_container->setItemLayout(KFileItemListView::IconsLayout); break;
1179 case CompactView: m_container->setItemLayout(KFileItemListView::CompactLayout); break;
1180 case DetailsView: m_container->setItemLayout(KFileItemListView::DetailsLayout); break;
1181 default: Q_ASSERT(false); break;
1182 }
1183
1184 emit modeChanged(m_mode, previousMode);
1185
1186 if (m_container->zoomLevel() != oldZoomLevel) {
1187 emit zoomLevelChanged(m_container->zoomLevel(), oldZoomLevel);
1188 }
1189 }
1190
1191 const bool hiddenFilesShown = props.hiddenFilesShown();
1192 if (hiddenFilesShown != m_dirLister->showingDotFiles()) {
1193 m_dirLister->setShowingDotFiles(hiddenFilesShown);
1194 m_dirLister->emitChanges();
1195 emit hiddenFilesShownChanged(hiddenFilesShown);
1196 }
1197
1198 KFileItemModel* model = fileItemModel();
1199
1200 const bool groupedSorting = props.groupedSorting();
1201 if (groupedSorting != model->groupedSorting()) {
1202 model->setGroupedSorting(groupedSorting);
1203 emit groupedSortingChanged(groupedSorting);
1204 }
1205
1206 const DolphinView::Sorting sorting = props.sorting();
1207 const QByteArray newSortRole = sortRoleForSorting(sorting);
1208 if (newSortRole != model->sortRole()) {
1209 model->setSortRole(newSortRole);
1210 emit sortingChanged(sorting);
1211 }
1212
1213 const Qt::SortOrder sortOrder = props.sortOrder();
1214 if (sortOrder != model->sortOrder()) {
1215 model->setSortOrder(sortOrder);
1216 emit sortOrderChanged(sortOrder);
1217 }
1218
1219 const bool sortFoldersFirst = props.sortFoldersFirst();
1220 if (sortFoldersFirst != model->sortFoldersFirst()) {
1221 model->setSortFoldersFirst(sortFoldersFirst);
1222 emit sortFoldersFirstChanged(sortFoldersFirst);
1223 }
1224
1225 const QList<DolphinView::AdditionalInfo> infoList = props.additionalInfoList();
1226 if (infoList != m_additionalInfoList) {
1227 const QList<DolphinView::AdditionalInfo> previousList = m_additionalInfoList;
1228 m_additionalInfoList = infoList;
1229 applyAdditionalInfoListToView();
1230 emit additionalInfoListChanged(m_additionalInfoList, previousList);
1231 }
1232
1233 const bool previewsShown = props.previewsShown();
1234 if (previewsShown != m_container->previewsShown()) {
1235 const int oldZoomLevel = zoomLevel();
1236
1237 m_container->setPreviewsShown(previewsShown);
1238 emit previewsShownChanged(previewsShown);
1239
1240 // Changing the preview-state might result in a changed zoom-level
1241 if (oldZoomLevel != zoomLevel()) {
1242 emit zoomLevelChanged(zoomLevel(), oldZoomLevel);
1243 }
1244 }
1245
1246 m_container->endTransaction();
1247 }
1248
1249 void DolphinView::applyAdditionalInfoListToView()
1250 {
1251 const AdditionalInfoAccessor& infoAccessor = AdditionalInfoAccessor::instance();
1252
1253 QList<QByteArray> visibleRoles;
1254 visibleRoles.reserve(m_additionalInfoList.count() + 1);
1255 visibleRoles.append("name");
1256
1257 foreach (AdditionalInfo info, m_additionalInfoList) {
1258 visibleRoles.append(infoAccessor.role(info));
1259 }
1260
1261 m_container->setVisibleRoles(visibleRoles);
1262 }
1263
1264 void DolphinView::pasteToUrl(const KUrl& url)
1265 {
1266 markPastedUrlsAsSelected(QApplication::clipboard()->mimeData());
1267 KonqOperations::doPaste(this, url);
1268 }
1269
1270 void DolphinView::updateZoomLevel(int oldZoomLevel)
1271 {
1272 Q_UNUSED(oldZoomLevel);
1273 /* const int newZoomLevel = ZoomLevelInfo::zoomLevelForIconSize(m_viewAccessor.itemView()->iconSize());
1274 if (oldZoomLevel != newZoomLevel) {
1275 m_viewModeController->setZoomLevel(newZoomLevel);
1276 emit zoomLevelChanged(newZoomLevel);
1277 }*/
1278 }
1279
1280 KUrl::List DolphinView::simplifiedSelectedUrls() const
1281 {
1282 KUrl::List urls;
1283
1284 const KFileItemList items = selectedItems();
1285 foreach (const KFileItem &item, items) {
1286 urls.append(item.url());
1287 }
1288
1289 if (itemsExpandable()) {
1290 // TODO: Check if we still need KDirModel for this in KDE 5.0
1291 urls = KDirModel::simplifiedUrlList(urls);
1292 }
1293
1294 return urls;
1295 }
1296
1297 QMimeData* DolphinView::selectionMimeData() const
1298 {
1299 const KFileItemModel* model = fileItemModel();
1300 const KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager();
1301 const QSet<int> selectedIndexes = selectionManager->selectedItems();
1302
1303 return model->createMimeData(selectedIndexes);
1304 }
1305
1306 void DolphinView::markPastedUrlsAsSelected(const QMimeData* mimeData)
1307 {
1308 const KUrl::List urls = KUrl::List::fromMimeData(mimeData);
1309 markUrlsAsSelected(urls);
1310 }
1311
1312 void DolphinView::updateWritableState()
1313 {
1314 const bool wasFolderWritable = m_isFolderWritable;
1315 m_isFolderWritable = true;
1316
1317 const KFileItem item = m_dirLister->rootItem();
1318 if (!item.isNull()) {
1319 KFileItemListProperties capabilities(KFileItemList() << item);
1320 m_isFolderWritable = capabilities.supportsWriting();
1321 }
1322 if (m_isFolderWritable != wasFolderWritable) {
1323 emit writeStateChanged(m_isFolderWritable);
1324 }
1325 }
1326
1327 QByteArray DolphinView::sortRoleForSorting(Sorting sorting) const
1328 {
1329 switch (sorting) {
1330 case SortByName: return "name";
1331 case SortBySize: return "size";
1332 case SortByDate: return "date";
1333 case SortByPermissions: return "permissions";
1334 case SortByOwner: return "owner";
1335 case SortByGroup: return "group";
1336 case SortByType: return "type";
1337 case SortByDestination: return "destination";
1338 case SortByPath: return "path";
1339 default: break;
1340 }
1341
1342 return QByteArray();
1343 }
1344
1345 DolphinView::Sorting DolphinView::sortingForSortRole(const QByteArray& sortRole) const
1346 {
1347 static QHash<QByteArray, DolphinView::Sorting> sortHash;
1348 if (sortHash.isEmpty()) {
1349 sortHash.insert("name", SortByName);
1350 sortHash.insert("size", SortBySize);
1351 sortHash.insert("date", SortByDate);
1352 sortHash.insert("permissions", SortByPermissions);
1353 sortHash.insert("owner", SortByOwner);
1354 sortHash.insert("group", SortByGroup);
1355 sortHash.insert("type", SortByType);
1356 sortHash.insert("destination", SortByDestination);
1357 sortHash.insert("path", SortByPath);
1358 }
1359 return sortHash.value(sortRole);
1360 }
1361
1362 QString DolphinView::fileSizeText(KIO::filesize_t fileSize)
1363 {
1364 const KLocale* locale = KGlobal::locale();
1365 const unsigned int multiplier = (locale->binaryUnitDialect() == KLocale::MetricBinaryDialect)
1366 ? 1000 : 1024;
1367
1368 QString text;
1369 if (fileSize < multiplier) {
1370 // Show the size in bytes
1371 text = locale->formatByteSize(fileSize, 0, KLocale::DefaultBinaryDialect, KLocale::UnitByte);
1372 } else if (fileSize < multiplier * multiplier) {
1373 // Show the size in kilobytes and always round up. This is done
1374 // for consistency with the values shown e.g. in the "Size" column
1375 // of the details-view.
1376 fileSize += (multiplier / 2) - 1;
1377 text = locale->formatByteSize(fileSize, 0, KLocale::DefaultBinaryDialect, KLocale::UnitKiloByte);
1378 } else {
1379 // Show the size in the best fitting unit having one decimal
1380 text = locale->formatByteSize(fileSize, 1);
1381 }
1382 return text;
1383 }
1384
1385 #include "dolphinview.moc"