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