Skip to content
Snippets Groups Projects
Commit 3bef0b83 authored by Teo Mrnjavac's avatar Teo Mrnjavac
Browse files

Check for mains power in prepare viewmodule.

parent 26419f2c
No related branches found
No related tags found
No related merge requests found
...@@ -28,6 +28,8 @@ ...@@ -28,6 +28,8 @@
#include <QBoxLayout> #include <QBoxLayout>
#include <QDBusConnection> #include <QDBusConnection>
#include <QDBusInterface> #include <QDBusInterface>
#include <QDir>
#include <QFileInfo>
#include <QLabel> #include <QLabel>
#include <QProcess> #include <QProcess>
...@@ -231,22 +233,76 @@ PrepareViewStep::checkEnoughRam( qint64 requiredRam ) ...@@ -231,22 +233,76 @@ PrepareViewStep::checkEnoughRam( qint64 requiredRam )
bool bool
PrepareViewStep::checkHasPower() PrepareViewStep::checkBatteryExists()
{ {
const QFileInfo basePath( "/sys/class/power_supply" );
if ( !( basePath.exists() && basePath.isDir() ) )
return false;
QDir baseDir( basePath.absoluteFilePath() );
foreach ( auto item, baseDir.entryList( QDir::AllDirs |
QDir::Readable |
QDir::NoDotAndDotDot ) )
{
QFileInfo typePath( baseDir.absoluteFilePath( QString( "%1/type" )
.arg( item ) ) );
QFile typeFile( typePath.absoluteFilePath() );
if ( typeFile.open( QIODevice::ReadOnly | QIODevice::Text ) )
{
if ( typeFile.readAll().startsWith( "Battery" ) )
return true;
}
}
return false; return false;
} }
bool
PrepareViewStep::checkHasPower()
{
const QString UPOWER_SVC_NAME( "org.freedesktop.UPower" );
const QString UPOWER_INTF_NAME( "org.freedesktop.UPower" );
const QString UPOWER_PATH( "/org/freedesktop/UPower" );
if ( !checkBatteryExists() )
return true;
cDebug() << "A battery exists, checking for mains power.";
QDBusInterface upowerIntf( UPOWER_SVC_NAME,
UPOWER_PATH,
UPOWER_INTF_NAME,
QDBusConnection::systemBus(), 0 );
bool onBattery = upowerIntf.property( "OnBattery" ).toBool();
if ( !upowerIntf.isValid() )
{
// We can't talk to upower but we're obviously up and running
// so I guess we got that going for us, which is nice...
return true;
}
// If a battery exists but we're not using it, means we got mains
// power.
return !onBattery;
}
bool bool
PrepareViewStep::checkHasInternet() PrepareViewStep::checkHasInternet()
{ {
const QString NM_SVC_NAME( "org.freedesktop.NetworkManager" );
const QString NM_INTF_NAME( "org.freedesktop.NetworkManager" ); const QString NM_INTF_NAME( "org.freedesktop.NetworkManager" );
const QString NM_PATH( "/org/freedesktop/NetworkManager" );
const int NM_STATE_CONNECTED_GLOBAL = 70; const int NM_STATE_CONNECTED_GLOBAL = 70;
QDBusInterface nmIntf( "org.freedesktop.NetworkManager", QDBusInterface nmIntf( NM_SVC_NAME,
"/org/freedesktop/NetworkManager", NM_PATH,
NM_INTF_NAME, NM_INTF_NAME,
QDBusConnection::systemBus(), 0 ); QDBusConnection::systemBus(), 0 );
bool ok = false; bool ok = false;
int nmState = nmIntf.property( "state" ).toInt( &ok ); int nmState = nmIntf.property( "state" ).toInt( &ok );
......
...@@ -58,6 +58,7 @@ public: ...@@ -58,6 +58,7 @@ public:
private: private:
bool checkEnoughStorage( qint64 requiredSpace ); bool checkEnoughStorage( qint64 requiredSpace );
bool checkEnoughRam( qint64 requiredRam ); bool checkEnoughRam( qint64 requiredRam );
bool checkBatteryExists();
bool checkHasPower(); bool checkHasPower();
bool checkHasInternet(); bool checkHasInternet();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment