diff --git a/src/dbusifs/upowerhelper.cpp b/src/dbusifs/upowerhelper.cpp index fbc1cd5..90bef8a 100644 --- a/src/dbusifs/upowerhelper.cpp +++ b/src/dbusifs/upowerhelper.cpp @@ -37,8 +37,8 @@ UpowerHelper::UpowerHelper(QObject *parent) : QObject(parent) = new QDBusInterface(upowerService, upowerPath, upowerInterface, QDBusConnection::systemBus(), this); QString batteryPath = ""; - QDBusReply> reply = m_upowerService->call("EnumerateDevices"); if (m_upowerService->isValid()) { + QDBusReply> reply = m_upowerService->call("EnumerateDevices"); for (QDBusObjectPath op : reply.value()) { if (op.path().contains("battery_")) { batteryPath = op.path(); @@ -50,15 +50,16 @@ UpowerHelper::UpowerHelper(QObject *parent) : QObject(parent) qDebug() << "Enumerate devices failed"; } - QDBusConnection::systemBus().connect( - upowerService, batteryPath, propertiesInterface, "PropertiesChanged", this, SLOT(dealMessage(QDBusMessage))); + if (!batteryPath.isEmpty()) { + QDBusConnection::systemBus().connect( + upowerService, batteryPath, propertiesInterface, "PropertiesChanged", this, SLOT(dealMessage(QDBusMessage))); + m_batInterface + = new QDBusInterface(upowerService, batteryPath, propertiesInterface, QDBusConnection::systemBus(), this); + } m_upowerInterface = new QDBusInterface(upowerService, upowerPath, propertiesInterface, QDBusConnection::systemBus(), this); - m_batInterface - = new QDBusInterface(upowerService, batteryPath, propertiesInterface, QDBusConnection::systemBus(), this); - if (m_upowerInterface->isValid()) { QDBusReply interfaceReply = m_upowerInterface->call("Get", "org.freedesktop.UPower", "LidIsPresent"); if (interfaceReply.isValid()) { @@ -69,21 +70,23 @@ UpowerHelper::UpowerHelper(QObject *parent) : QObject(parent) upowerService, upowerPath, propertiesInterface, "PropertiesChanged", this, SLOT(onLidWatcherMessage())); } - m_engineDevice = EngineDevice::getInstance(); - // onBatteryChanged(m_engineDevice->engine_get_state()); + if (m_upowerService->isValid()) { + m_engineDevice = EngineDevice::getInstance(); + // onBatteryChanged(m_engineDevice->engine_get_state()); - int size; - size = m_engineDevice->devices.size(); - for (int i = 0; i < size; i++) { - DEVICE *dv; - dv = m_engineDevice->devices.at(i); + int size; + size = m_engineDevice->devices.size(); + for (int i = 0; i < size; i++) { + DEVICE *dv; + dv = m_engineDevice->devices.at(i); - if (dv->m_dev.kind == UP_DEVICE_KIND_LINE_POWER) { + if (dv->m_dev.kind == UP_DEVICE_KIND_LINE_POWER) { + continue; + } + + connect(m_engineDevice, &EngineDevice::engine_signal_Battery_State, this, &UpowerHelper::onBatteryChanged); continue; } - - connect(m_engineDevice, &EngineDevice::engine_signal_Battery_State, this, &UpowerHelper::onBatteryChanged); - continue; } } @@ -91,7 +94,7 @@ UpowerHelper::~UpowerHelper() {} QString UpowerHelper::getBatteryIconName() { - if (m_upowerService->isValid() && m_upowerInterface->isValid()) { + if (m_upowerService->isValid() && m_upowerInterface->isValid() && m_batInterface) { bool batteryState = false; QDBusReply reply = m_upowerInterface->call("Get", upowerService, "OnBattery"); if (reply.isValid()) { @@ -114,6 +117,9 @@ QString UpowerHelper::getBatteryIconName() QStringList UpowerHelper::getBatteryArgs() { + if (!m_engineDevice) { + return QStringList(); + } return m_engineDevice->engine_get_state(); } diff --git a/src/dbusifs/upowerhelper.h b/src/dbusifs/upowerhelper.h index 74d9643..7f31af6 100644 --- a/src/dbusifs/upowerhelper.h +++ b/src/dbusifs/upowerhelper.h @@ -72,7 +72,7 @@ private: QDBusInterface *m_batInterface = nullptr; QDBusInterface *m_upowerService = nullptr; - EngineDevice *m_engineDevice; + EngineDevice *m_engineDevice = nullptr; bool m_isBattery = false; };