Commit 67a694bb authored by Àlex Fiestas's avatar Àlex Fiestas

First import of the freedesktop backend

As difference with the past, now we are introducing a freedesktop
backend rather than a "upower/udev/systemd/ separate backends. This
allows us to be more flexible and to make sure we support freedesktop
correctly.

This first import comes with component test that checks how we are
handling dbus and what dbus calls we are doing.
parent 921f33d2
......@@ -59,6 +59,9 @@ add_feature_info(Solid::Job WITH_NEW_SOLID_JOB "WIP: Base class for Solid Asynch
option(WITH_NEW_POWER_ASYNC_API "WIP: Asynchronous API for power management" Off)
add_feature_info(Solid::PowerManagement WITH_NEW_POWER_ASYNC_API "WIP: Asynchronous API for power management")
option(WITH_NEW_POWER_ASYNC_FREEDESKTOP "WIP: Freedesktop backend for the asyncrhonous api" Off)
add_feature_info(Solid::PowerManagement WITH_NEW_POWER_ASYNC_FREEDESKTOP "WIP: Freedesktop backend for the asyncrhonous api")
add_subdirectory(src)
add_subdirectory(autotests)
......
......@@ -36,3 +36,8 @@ if (WITH_NEW_SOLID_JOB AND WITH_NEW_POWER_ASYNC_API)
ecm_add_test(solidpowertest.cpp LINK_LIBRARIES Qt5::Test ${LIBS} KF5Solid_static)
endif()
########### solidpowertest ###############
if(WITH_NEW_POWER_ASYNC_API AND WITH_NEW_POWER_ASYNC_FREEDESKTOP)
set(solidFreedesktopTest_SRCS solidfreedesktoptest.cpp fakeUpower.cpp)
ecm_add_test(${solidFreedesktopTest_SRCS} TEST_NAME "solidfreedesktopbackend" LINK_LIBRARIES Qt5::Test Qt5::DBus ${LIBS} KF5Solid_static)
endif()
\ No newline at end of file
/*
Copyright 2014 Alejandro Fiestas Olivares <afiestas@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) version 3, or any
later version accepted by the membership of KDE e.V. (or its
successor approved by the membership of KDE e.V.), which shall
act as a proxy defined in Section 6 of version 3 of the license.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#include "fakeUpower.h"
#include <QDebug>
FakeUpower::FakeUpower(QObject* parent) : QDBusAbstractAdaptor(parent),
m_onBattery(false)
{
}
QString FakeUpower::daemonVersion() const
{
return "POP";
}
QString FakeUpower::GetCriticalAction()
{
return "AAAA";
}
QDBusObjectPath FakeUpower::GetDisplayDevice()
{
return QDBusObjectPath("AA");
}
bool FakeUpower::isDocked() const
{
return true;
}
bool FakeUpower::lidIsClosed() const
{
return false;
}
bool FakeUpower::lidIsPresent() const
{
return true;
}
bool FakeUpower::onBattery() const
{
return m_onBattery;
}
QList< QDBusObjectPath > FakeUpower::EnumerateDevices()
{
QList<QDBusObjectPath> list;
return list;
}
/*
Copyright 2014 Alejandro Fiestas Olivares <afiestas@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) version 3, or any
later version accepted by the membership of KDE e.V. (or its
successor approved by the membership of KDE e.V.), which shall
act as a proxy defined in Section 6 of version 3 of the license.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef SOLID_FAKE_UPOWER_H
#define SOLID_FAKE_UPOWER_H
#include <QList>
#include <QString>
#include <QDBusAbstractAdaptor>
#include <QDBusObjectPath>
class FakeUpower : public QDBusAbstractAdaptor
{
Q_OBJECT
Q_PROPERTY(bool IsDocked READ isDocked)
Q_PROPERTY(bool OnBattery READ onBattery)
Q_CLASSINFO("D-Bus Interface", "org.freedesktop.UPower")
public:
explicit FakeUpower(QObject* parent);
QString daemonVersion() const;
bool isDocked() const;
bool lidIsClosed() const;
bool lidIsPresent() const;
bool onBattery() const;
bool m_onBattery;
public Q_SLOTS:
QList<QDBusObjectPath> EnumerateDevices();
QString GetCriticalAction();
QDBusObjectPath GetDisplayDevice();
};
#endif //SOLID_FAKE_UPOWER_H
\ No newline at end of file
/*
Copyright 2014 Alejandro Fiestas Olivares <afiestas@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) version 3, or any
later version accepted by the membership of KDE e.V. (or its
successor approved by the membership of KDE e.V.), which shall
act as a proxy defined in Section 6 of version 3 of the license.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef SOLID_QTEST_DBUS_H
#define SOLID_QTEST_DBUS_H
#include <QtTest>
#include <QDebug>
#include <stdlib.h>
#define QTEST_GUILESS_MAIN_SYSTEM_DBUS(TestObject) \
int main(int argc, char *argv[]) \
{ \
QProcess dbus; \
dbus.start("dbus-launch"); \
dbus.waitForFinished(10000); \
QByteArray session = dbus.readLine(); \
if (session.isEmpty()) { \
qFatal("Couldn't execute new dbus session"); \
} \
int pos = session.indexOf('='); \
setenv("DBUS_SYSTEM_BUS_ADDRESS", session.right(session.count() - pos - 1).trimmed(), 1); \
qDebug() << session.right(session.count() - pos - 1).trimmed(); \
session = dbus.readLine(); \
pos = session.indexOf('='); \
QByteArray pid = session.right(session.count() - pos - 1).trimmed(); \
QCoreApplication app( argc, argv ); \
app.setApplicationName( QLatin1String("qttest") ); \
TestObject tc; \
int result = QTest::qExec( &tc, argc, argv ); \
dbus.start("kill", QStringList() << "-9" << pid); \
dbus.waitForFinished(); \
return result; \
}
#endif //SOLID_QTEST_DBUS_H
\ No newline at end of file
/*
Copyright 2014 Alejandro Fiestas Olivares <afiestas@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) version 3, or any
later version accepted by the membership of KDE e.V. (or its
successor approved by the membership of KDE e.V.), which shall
act as a proxy defined in Section 6 of version 3 of the license.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#include "qtest_dbus.h"
#include "fakeUpower.h"
#include <QTest>
#include <QDebug>
#include <QSignalSpy>
#include <QDBusConnection>
#include <Solid/Power>
#include <Solid/AcPluggedJob>
#include <Solid/Inhibition>
#include <Solid/InhibitionJob>
#include <Solid/StatesJob>
#include <Solid/RequestStateJob>
using namespace Solid;
class solidFreedesktopTest : public QObject
{
Q_OBJECT
private Q_SLOTS:
void initTestCase();
void testAcPluggedJob();
void testAcPluggedChanged();
void testAddInhibition();
void testSupportedStates();
void testRequestState();
private:
FakeUpower *m_fakeUPower;
};
void solidFreedesktopTest::initTestCase()
{
qputenv("SOLID_POWER_BACKEND", "FREE_DESKTOP");
m_fakeUPower = new FakeUpower(this);
QDBusConnection::systemBus().registerService("org.freedesktop.UPower");
QDBusConnection::systemBus().registerObject("/org/freedesktop/UPower", m_fakeUPower, QDBusConnection::ExportAllContents);
}
void solidFreedesktopTest::testAcPluggedJob()
{
m_fakeUPower->m_onBattery = true;
auto job = new AcPluggedJob(this);
QVERIFY(job->exec());
QCOMPARE(job->isPlugged(), m_fakeUPower->m_onBattery);
m_fakeUPower->m_onBattery = false;
job = Solid::Power::isAcPlugged();
QVERIFY(job->exec());
QCOMPARE(job->isPlugged(), m_fakeUPower->m_onBattery);
}
void solidFreedesktopTest::testAcPluggedChanged()
{
}
void solidFreedesktopTest::testAddInhibition()
{
}
void solidFreedesktopTest::testSupportedStates()
{
}
void solidFreedesktopTest::testRequestState()
{
}
QTEST_GUILESS_MAIN_SYSTEM_DBUS(solidFreedesktopTest)
#include "solidfreedesktoptest.moc"
\ No newline at end of file
......@@ -138,4 +138,4 @@ endif ()
include(ECMGeneratePriFile)
ecm_generate_pri_file(BASE_NAME Solid LIB_NAME KF5Solid DEPS "core" FILENAME_VAR PRI_FILENAME INCLUDE_INSTALL_DIR ${KF5_INCLUDE_INSTALL_DIR}/Solid)
install(FILES ${PRI_FILENAME} DESTINATION ${ECM_MKSPECS_INSTALL_DIR})
install(FILES ${PRI_FILENAME} DESTINATION ${ECM_MKSPECS_INSTALL_DIR})
\ No newline at end of file
......@@ -27,4 +27,8 @@ if(WITH_NEW_POWER_ASYNC_API)
power/backends/dummy/dummystatesjob.cpp
power/backends/dummy/dummyrequeststatejob.cpp
)
endif()
if(WITH_NEW_POWER_ASYNC_API AND WITH_NEW_POWER_ASYNC_FREEDESKTOP)
include(power/backends/freedesktop/CMakeLists.txt)
endif()
\ No newline at end of file
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/power)
set(solid_LIB_SRCS
${solid_LIB_SRCS}
power/backends/freedesktop/fdacpluggedjob.cpp
)
\ No newline at end of file
/*
Copyright 2014 Alejandro Fiestas Olivares <afiestas@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) version 3, or any
later version accepted by the membership of KDE e.V. (or its
successor approved by the membership of KDE e.V.), which shall
act as a proxy defined in Section 6 of version 3 of the license.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#include "fdacpluggedjob.h"
#include <QDebug>
#include <QDBusConnection>
#include <QDBusMessage>
#include <QDBusPendingCall>
#include <QDBusPendingReply>
#include <QDBusInterface>
using namespace Solid;
FDAcPluggedJob::FDAcPluggedJob(QObject* parent)
: AbstractAcPluggedJob(parent)
, m_isPlugged(false)
{
}
void FDAcPluggedJob::doStart()
{
QDBusMessage msg = QDBusMessage::createMethodCall(
QStringLiteral("org.freedesktop.UPower"),
QStringLiteral("/org/freedesktop/UPower"),
QStringLiteral("org.freedesktop.DBus.Properties"),
QStringLiteral("Get"));
QList<QVariant> args;
args << QStringLiteral("org.freedesktop.UPower")
<< QStringLiteral("OnBattery");
msg.setArguments(args);
QDBusConnection::systemBus().callWithCallback(msg, this, SLOT(slotDBusReply(QDBusMessage)), SLOT(slotDBusError(QDBusError)));
}
void FDAcPluggedJob::slotDBusReply(const QDBusMessage& msg)
{
QDBusInterface inter("org.kde.kded5", "/org/kde/kded5");
Q_ASSERT(!msg.arguments().isEmpty());
m_isPlugged = msg.arguments().first().value<QDBusVariant>().variant().toBool();
emitResult();
}
void FDAcPluggedJob::slotDBusError(const QDBusError& error)
{
setError(error.type());
setErrorText(error.message());
emitResult();
}
bool FDAcPluggedJob::isPlugged() const
{
return m_isPlugged;
}
/*
Copyright 2014 Alejandro Fiestas Olivares <afiestas@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) version 3, or any
later version accepted by the membership of KDE e.V. (or its
successor approved by the membership of KDE e.V.), which shall
act as a proxy defined in Section 6 of version 3 of the license.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef FREEDESKTOP_AC_PLUGGED_JOB_H
#define FREEDESKTOP_AC_PLUGGED_JOB_H
#include "backends/abstractacpluggedjob.h"
class QDBusError;
class QDBusMessage;
class QDBusPendingCallWatcher;
namespace Solid
{
class FDAcPluggedJob : public AbstractAcPluggedJob
{
Q_OBJECT
public:
explicit FDAcPluggedJob(QObject* parent = 0);
virtual bool isPlugged() const Q_DECL_OVERRIDE;
private Q_SLOTS:
virtual void doStart() Q_DECL_OVERRIDE;
void slotDBusReply(const QDBusMessage &msg);
void slotDBusError(const QDBusError &error);
private:
void dbusFinished(QDBusPendingCallWatcher* watcher);
bool m_isPlugged;
};
}
#endif //FREEDESKTOP_AC_PLUGGED_JOB_H
......@@ -25,11 +25,15 @@
#include "backends/dummy/dummyinhibitionjob.h"
#include "backends/dummy/dummystatesjob.h"
#include "backends/dummy/dummyrequeststatejob.h"
#include "backends/freedesktop/fdacpluggedjob.h"
using namespace Solid;
AbstractAcPluggedJob* PowerBackendLoader::AcPluggedJob()
{
if (qgetenv("SOLID_POWER_BACKEND") == "FREE_DESKTOP") {
return new FDAcPluggedJob();
}
return new DummyAcPluggedJob();
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment