File indexing completed on 2024-05-19 05:37:51

0001 /*
0002     SPDX-FileCopyrightText: 2014 Marco Martin <mart@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-or-later
0005 */
0006 
0007 #include "placesproxymodel.h"
0008 
0009 #include <QDebug>
0010 #include <QIcon>
0011 #include <QStorageInfo>
0012 
0013 PlacesProxyModel::PlacesProxyModel(QObject *parent, KFilePlacesModel *model)
0014     : QIdentityProxyModel(parent)
0015     , m_placesModel(model)
0016 {
0017     setSourceModel(model);
0018 }
0019 
0020 QHash<int, QByteArray> PlacesProxyModel::roleNames() const
0021 {
0022     QHash<int, QByteArray> roles;
0023     roles.insert(Qt::DisplayRole, "display");
0024     roles.insert(Qt::DecorationRole, "decoration");
0025     roles.insert(KFilePlacesModel::UrlRole, "url");
0026     roles.insert(KFilePlacesModel::HiddenRole, "hidden");
0027     roles.insert(KFilePlacesModel::SetupNeededRole, "setupNeeded");
0028     roles.insert(KFilePlacesModel::FixedDeviceRole, "fixedDevice");
0029     roles.insert(KFilePlacesModel::CapacityBarRecommendedRole, "capacityBarRecommended");
0030     roles.insert(PlaceIndexRole, "placeIndex");
0031     roles.insert(IsDeviceRole, "isDevice");
0032     roles.insert(PathRole, "path");
0033     roles.insert(SizeRole, "size");
0034     roles.insert(UsedRole, "used");
0035     roles.insert(AvailableRole, "available");
0036     return roles;
0037 }
0038 
0039 QVariant PlacesProxyModel::data(const QModelIndex &index, int role) const
0040 {
0041     switch (role) {
0042     case PlaceIndexRole:
0043         return index.row();
0044     case IsDeviceRole:
0045         return m_placesModel->deviceForIndex(index).isValid();
0046     case PathRole:
0047         return m_placesModel->url(index).path();
0048 
0049     case SizeRole: {
0050         const QString path = m_placesModel->url(index).path();
0051         QStorageInfo info{path};
0052         return info.isValid() && info.isReady() ? info.bytesTotal() : QVariant{};
0053     }
0054     case UsedRole: {
0055         const QString path = m_placesModel->url(index).path();
0056         QStorageInfo info{path};
0057         return info.isValid() && info.isReady() ? info.bytesTotal() - info.bytesAvailable() : QVariant{};
0058     }
0059     case AvailableRole: {
0060         const QString path = m_placesModel->url(index).path();
0061         QStorageInfo info{path};
0062         return info.isValid() && info.isReady() ? info.bytesAvailable() : QVariant{};
0063     }
0064     default:
0065         return QIdentityProxyModel::data(index, role);
0066     }
0067 }