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