Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[mce-qt] Add support for battery charging state tracking. JB#44852
Required constants are defined in mce-dev >= 1.28.0

The D-Bus methods and signals are available in mce >= 1.104.0

Signed-off-by: Simo Piiroinen <simo.piiroinen@jollamobile.com>
  • Loading branch information
spiiroin committed Oct 9, 2019
1 parent 7d8fd13 commit ee59c30
Show file tree
Hide file tree
Showing 7 changed files with 248 additions and 2 deletions.
3 changes: 3 additions & 0 deletions lib/dbus/com.nokia.mce.request.xml
Expand Up @@ -128,6 +128,9 @@
<method name="get_battery_level">
<arg direction="out" name="battery_level" type="i"/>
</method>
<method name="get_battery_state">
<arg direction="out" name="battery_state" type="s"/>
</method>
<method name="get_battery_status">
<arg direction="out" name="battery_status" type="s"/>
</method>
Expand Down
3 changes: 3 additions & 0 deletions lib/dbus/com.nokia.mce.signal.xml
Expand Up @@ -51,6 +51,9 @@
<signal name="battery_level_ind">
<arg name="battery_level" type="i"/>
</signal>
<signal name="battery_state_ind">
<arg name="battery_state" type="s"/>
</signal>
<signal name="battery_status_ind">
<arg name="battery_status" type="s"/>
</signal>
Expand Down
67 changes: 67 additions & 0 deletions lib/include/qmcebatterystate.h
@@ -0,0 +1,67 @@
/*
* Copyright (C) 2016-2019 Jolla Ltd.
* Copyright (c) 2019 Open Mobile Platform LLC.
*
* You may use this file under the terms of BSD license as follows:
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of Jolla Ltd nor the names of its contributors may
* be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation
* are those of the authors and should not be interpreted as representing
* any official policies, either expressed or implied.
*/

#ifndef QMCE_BATTERYSTATE_H_
#define QMCE_BATTERYSTATE_H_

#include "qmcetypes.h"

class QMCE_EXPORT QMceBatteryState : public QObject
{
Q_OBJECT
Q_PROPERTY(bool valid READ valid NOTIFY validChanged)
Q_PROPERTY(State value READ state NOTIFY stateChanged)
Q_ENUMS(State)
public:
enum State {
Unknown,
Charging,
Discharging,
NotCharging,
Full
};
QMceBatteryState(QObject* aParent = NULL);
bool valid() const;
State state() const;
Q_SIGNALS:
void validChanged();
void stateChanged();
private:
class Private;
Private* iPrivate;
};

#endif // QMCE_BATTERYSTATE_H_
2 changes: 2 additions & 0 deletions lib/lib.pro
Expand Up @@ -25,6 +25,7 @@ OTHER_FILES += $$XML_FILES
SOURCES += \
src/qmcebatterylevel.cpp \
src/qmcebatterystatus.cpp \
src/qmcebatterystate.cpp \
src/qmcecablestate.cpp \
src/qmcecallstate.cpp \
src/qmcechargertype.cpp \
Expand All @@ -37,6 +38,7 @@ SOURCES += \
PUBLIC_HEADERS += \
include/qmcebatterylevel.h \
include/qmcebatterystatus.h \
include/qmcebatterystate.h \
include/qmcecablestate.h \
include/qmcecallstate.h \
include/qmcechargertype.h \
Expand Down
169 changes: 169 additions & 0 deletions lib/src/qmcebatterystate.cpp
@@ -0,0 +1,169 @@
/*
* Copyright (C) 2016-2019 Jolla Ltd.
* Copyright (c) 2019 Open Mobile Platform LLC.
*
* You may use this file under the terms of BSD license as follows:
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of Jolla Ltd nor the names of its contributors may
* be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation
* are those of the authors and should not be interpreted as representing
* any official policies, either expressed or implied.
*/

#include "qmcebatterystate.h"
#include "qmceproxy.h"

#include <mce/mode-names.h>

// ==========================================================================
// QMceBatteryState::Private
// ==========================================================================

class QMceBatteryState::Private : public QObject {
Q_OBJECT
public:
Private(QMceBatteryState* aParent);
bool valid() const;
QMceBatteryState::State value() const;
private:
void queryValue();
void setValid(bool valid);
private Q_SLOTS:
void onProxyValidChanged();
void onQueryFinished(QDBusPendingCallWatcher* aWatcher);
void updateValue(QString state);
private:
QMceBatteryState* iParent;
QSharedPointer<QMceProxy> iProxy;
bool iValid;
QMceBatteryState::State iValue;
};

QMceBatteryState::Private::Private(QMceBatteryState* aParent) :
QObject(aParent),
iParent(aParent),
iProxy(QMceProxy::instance()),
iValid(false),
iValue(Unknown)
{
connect(iProxy->signalProxy(),
SIGNAL(battery_state_ind(QString)),
SLOT(updateValue(QString)));
connect(iProxy.data(),
SIGNAL(validChanged()),
SLOT(onProxyValidChanged()));
if (iProxy->valid()) {
queryValue();
}
}

bool QMceBatteryState::Private::valid() const
{
return iValid;
}

void QMceBatteryState::Private::setValid(bool valid)
{
if (iValid != valid) {
iValid = valid;
Q_EMIT iParent->validChanged();
}
}

QMceBatteryState::State QMceBatteryState::Private::value() const
{
return iValue;
}

void QMceBatteryState::Private::updateValue(QString state)
{
QMceBatteryState::State value = Unknown;

if (state == QStringLiteral(MCE_BATTERY_STATE_CHARGING)) {
value = Charging;
} else if (state == QStringLiteral(MCE_BATTERY_STATE_DISCHARGING)) {
value = Discharging;
} else if (state == QStringLiteral(MCE_BATTERY_STATE_NOT_CHARGING)) {
value = NotCharging;
} else if (state == QStringLiteral(MCE_BATTERY_STATE_FULL)) {
value = Full;
}

if (iValue != value) {
iValue = value;
Q_EMIT iParent->stateChanged();
}
setValid(true);
}

void QMceBatteryState::Private::queryValue()
{
connect(new QDBusPendingCallWatcher(
iProxy->requestProxy()->get_battery_state(), this),
SIGNAL(finished(QDBusPendingCallWatcher*)),
SLOT(onQueryFinished(QDBusPendingCallWatcher*)));
}

void QMceBatteryState::Private::onQueryFinished(QDBusPendingCallWatcher* aWatcher)
{
QDBusPendingReply<QString> reply(*aWatcher);
if (!reply.isError()) {
updateValue(reply.value());
}
aWatcher->deleteLater();
}

void QMceBatteryState::Private::onProxyValidChanged()
{
if (iProxy->valid()) {
queryValue();
} else {
setValid(false);
}
}

// ==========================================================================
// QMceBatteryState
// ==========================================================================

QMceBatteryState::QMceBatteryState(QObject* aParent) :
QObject(aParent),
iPrivate(new Private(this))
{
}

bool QMceBatteryState::valid() const
{
return iPrivate->valid();
}

QMceBatteryState::State QMceBatteryState::state() const
{
return iPrivate->value();
}

#include "qmcebatterystate.moc"
2 changes: 2 additions & 0 deletions plugin/qmcedeclarativeplugin.cpp
Expand Up @@ -39,6 +39,7 @@
#include "qmcetklock.h"
#include "qmcebatterylevel.h"
#include "qmcebatterystatus.h"
#include "qmcebatterystate.h"
#include "qmcecablestate.h"
#include "qmcechargerstate.h"
#include "qmcepowersavemode.h"
Expand All @@ -63,6 +64,7 @@ void QMceDeclarativePlugin::registerTypes(const char* aUri, int aMajor, int aMin
qmlRegisterType<QMceTkLock>(aUri, aMajor, aMinor, "MceTkLock");
qmlRegisterType<QMceBatteryLevel>(aUri, aMajor, aMinor, "MceBatteryLevel");
qmlRegisterType<QMceBatteryStatus>(aUri, aMajor, aMinor, "MceBatteryStatus");
qmlRegisterType<QMceBatteryState>(aUri, aMajor, aMinor, "MceBatteryState");
qmlRegisterType<QMceCableState>(aUri, aMajor, aMinor, "MceCableState");
qmlRegisterType<QMceChargerState>(aUri, aMajor, aMinor, "MceChargerState");
qmlRegisterType<QMcePowerSaveMode>(aUri, aMajor, aMinor, "McePowerSaveMode");
Expand Down
4 changes: 2 additions & 2 deletions rpm/libmce-qt5.spec
Expand Up @@ -12,8 +12,8 @@ Requires(postun): /sbin/ldconfig
BuildRequires: pkgconfig(Qt5Core)
BuildRequires: pkgconfig(Qt5DBus)
BuildRequires: pkgconfig(Qt5Quick)
BuildRequires: pkgconfig(mce) >= 1.27.0
Requires: mce >= 1.102.0
BuildRequires: pkgconfig(mce) >= 1.28.0
Requires: mce >= 1.104.0

%{!?qtc_qmake5:%define qtc_qmake5 %qmake5}
%{!?qtc_make:%define qtc_make make}
Expand Down

0 comments on commit ee59c30

Please sign in to comment.