#include "device.hpp" #include #include #include #include #include #include #include #include "../../dbus/properties.hpp" #include "dbus_device.h" using namespace qs::dbus; namespace qs::service::upower { Q_LOGGING_CATEGORY(logUPowerDevice, "quickshell.service.upower.device", QtWarningMsg); QString UPowerDeviceState::toString(UPowerDeviceState::Enum status) { switch (status) { case UPowerDeviceState::Unknown: return "Unknown"; case UPowerDeviceState::Charging: return "Charging"; case UPowerDeviceState::Discharging: return "Discharging"; case UPowerDeviceState::Empty: return "Empty"; case UPowerDeviceState::FullyCharged: return "Fully Charged"; case UPowerDeviceState::PendingCharge: return "Pending Charge"; case UPowerDeviceState::PendingDischarge: return "Pending Discharge"; default: return "Invalid Status"; } } QString UPowerDeviceType::toString(UPowerDeviceType::Enum type) { switch (type) { case UPowerDeviceType::Unknown: return "Unknown"; case UPowerDeviceType::LinePower: return "Line Power"; case UPowerDeviceType::Battery: return "Battery"; case UPowerDeviceType::Ups: return "Ups"; case UPowerDeviceType::Monitor: return "Monitor"; case UPowerDeviceType::Mouse: return "Mouse"; case UPowerDeviceType::Keyboard: return "Keyboard"; case UPowerDeviceType::Pda: return "Pda"; case UPowerDeviceType::Phone: return "Phone"; case UPowerDeviceType::MediaPlayer: return "Media Player"; case UPowerDeviceType::Tablet: return "Tablet"; case UPowerDeviceType::Computer: return "Computer"; case UPowerDeviceType::GamingInput: return "Gaming Input"; case UPowerDeviceType::Pen: return "Pen"; case UPowerDeviceType::Touchpad: return "Touchpad"; case UPowerDeviceType::Modem: return "Modem"; case UPowerDeviceType::Network: return "Network"; case UPowerDeviceType::Headset: return "Headset"; case UPowerDeviceType::Speakers: return "Speakers"; case UPowerDeviceType::Headphones: return "Headphones"; case UPowerDeviceType::Video: return "Video"; case UPowerDeviceType::OtherAudio: return "Other Audio"; case UPowerDeviceType::RemoteControl: return "Remote Control"; case UPowerDeviceType::Printer: return "Printer"; case UPowerDeviceType::Scanner: return "Scanner"; case UPowerDeviceType::Camera: return "Camera"; case UPowerDeviceType::Wearable: return "Wearable"; case UPowerDeviceType::Toy: return "Toy"; case UPowerDeviceType::BluetoothGeneric: return "Bluetooth Generic"; default: return "Invalid Type"; } } UPowerDevice::UPowerDevice(const QString& path, QObject* parent): QObject(parent) { this->device = new DBusUPowerDevice("org.freedesktop.UPower", path, QDBusConnection::systemBus(), this); if (!this->device->isValid()) { qCWarning(logUPowerDevice) << "Cannot create UPowerDevice for" << path; return; } this->bIsLaptopBattery.setBinding([this]() { return this->bType == UPowerDeviceType::Battery && this->bPowerSupply; }); this->bHealthSupported.setBinding([this]() { return this->bHealthPercentage != 0; }); QObject::connect( &this->deviceProperties, &DBusPropertyGroup::getAllFinished, this, &UPowerDevice::ready ); this->deviceProperties.setInterface(this->device); this->deviceProperties.updateAllViaGetAll(); } bool UPowerDevice::isValid() const { return this->device->isValid(); } QString UPowerDevice::address() const { return this->device->service(); } QString UPowerDevice::path() const { return this->device->path(); } } // namespace qs::service::upower namespace qs::dbus { using namespace qs::service::upower; DBusResult DBusDataTransform::fromWire(qreal wire) { return DBusResult(wire * 0.01); } qreal DBusDataTransform::toWire(const qreal& value) { return value * 100; } DBusResult DBusDataTransform::fromWire(quint32 wire) { if (wire != UPowerDeviceType::Battery && wire >= UPowerDeviceState::Unknown && wire <= UPowerDeviceState::PendingDischarge) { return DBusResult(static_cast(wire)); } return DBusResult( QDBusError(QDBusError::InvalidArgs, QString("Invalid UPowerDeviceState: %1").arg(wire)) ); } quint32 DBusDataTransform::toWire(const UPowerDeviceState::Enum& value) { return static_cast(value); } DBusResult DBusDataTransform::fromWire(quint32 wire ) { if (wire >= UPowerDeviceType::Unknown && wire <= UPowerDeviceType::BluetoothGeneric) { return DBusResult(static_cast(wire)); } return DBusResult( QDBusError(QDBusError::InvalidArgs, QString("Invalid UPowerDeviceType: %1").arg(wire)) ); } quint32 DBusDataTransform::toWire(const UPowerDeviceType::Enum& value) { return static_cast(value); } } // namespace qs::dbus