File indexing completed on 2024-05-12 05:36:17
0001 /* 0002 * SPDX-FileCopyrightText: 2015 Kai Uwe Broulik <kde@privat.broulik.de> 0003 * SPD 0004 * 0005 * SPDX-License-Identifier: GPL-2.0-or-later 0006 */ 0007 0008 #include "batterymodel.h" 0009 0010 #include <Solid/DeviceNotifier> 0011 0012 #include <QQmlEngine> 0013 #include <QtQml> 0014 0015 BatteryModel::BatteryModel(QObject *parent) 0016 : QAbstractListModel(parent) 0017 { 0018 qmlRegisterUncreatableType<Solid::Battery>("org.kde.kinfocenter.energy.private", 1, 0, "Battery", QStringLiteral("Use Solid::Battery")); 0019 0020 m_batteries = Solid::Device::listFromType(Solid::DeviceInterface::Battery); 0021 0022 connect(Solid::DeviceNotifier::instance(), &Solid::DeviceNotifier::deviceAdded, this, [this](const QString &udi) { 0023 auto it = std::find_if(m_batteries.constBegin(), m_batteries.constEnd(), [&udi](const Solid::Device &dev) { 0024 return dev.udi() == udi; 0025 }); 0026 if (it != m_batteries.constEnd()) { 0027 return; 0028 } 0029 0030 const Solid::Device device(udi); 0031 if (device.isValid() && device.is<Solid::Battery>()) { 0032 beginInsertRows(QModelIndex(), m_batteries.count(), m_batteries.count()); 0033 m_batteries.append(device); 0034 endInsertRows(); 0035 0036 Q_EMIT countChanged(); 0037 } 0038 }); 0039 connect(Solid::DeviceNotifier::instance(), &Solid::DeviceNotifier::deviceRemoved, this, [this](const QString &udi) { 0040 auto it = std::find_if(m_batteries.constBegin(), m_batteries.constEnd(), [&udi](const Solid::Device &dev) { 0041 return dev.udi() == udi; 0042 }); 0043 if (it == m_batteries.constEnd()) { 0044 return; 0045 } 0046 0047 int index = std::distance(m_batteries.constBegin(), it); 0048 0049 beginRemoveRows(QModelIndex(), index, index); 0050 m_batteries.removeAt(index); 0051 endRemoveRows(); 0052 0053 Q_EMIT countChanged(); 0054 }); 0055 } 0056 0057 QVariant BatteryModel::data(const QModelIndex &index, int role) const 0058 { 0059 if (index.row() < 0 || index.row() >= m_batteries.count()) { 0060 return QVariant(); 0061 } 0062 0063 if (role == BatteryRole) { 0064 // .as returns a pointer to a casted DeviceInterface. This pointer must 0065 // not, under any circumstances, be deleted outside Solid! 0066 // https://bugs.kde.org/show_bug.cgi?id=413003 0067 const auto battery = m_batteries.value(index.row()).as<Solid::Battery>(); 0068 QQmlEngine::setObjectOwnership(battery, QQmlEngine::CppOwnership); 0069 return QVariant::fromValue(battery); 0070 } else if (role == ProductRole) { 0071 const Solid::Device device = m_batteries.value(index.row()); 0072 return device.product(); 0073 } else if (role == VendorRole) { 0074 const Solid::Device device = m_batteries.value(index.row()); 0075 return device.vendor(); 0076 } else if (role == UdiRole) { 0077 return m_batteries.at(index.row()).udi(); 0078 } 0079 0080 return QVariant(); 0081 } 0082 0083 int BatteryModel::rowCount(const QModelIndex &parent) const 0084 { 0085 Q_UNUSED(parent); 0086 return m_batteries.count(); 0087 } 0088 0089 QHash<int, QByteArray> BatteryModel::roleNames() const 0090 { 0091 return {{BatteryRole, "battery"}, {VendorRole, "vendor"}, {ProductRole, "product"}, {UdiRole, "udi"}}; 0092 }