diff --git a/src/modules/prepare/PrepareViewStep.cpp b/src/modules/prepare/PrepareViewStep.cpp index 86174a153..174c4b9e1 100644 --- a/src/modules/prepare/PrepareViewStep.cpp +++ b/src/modules/prepare/PrepareViewStep.cpp @@ -28,6 +28,8 @@ #include #include #include +#include +#include #include #include @@ -231,22 +233,76 @@ PrepareViewStep::checkEnoughRam( qint64 requiredRam ) 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; } +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 PrepareViewStep::checkHasInternet() { + const QString NM_SVC_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; - QDBusInterface nmIntf( "org.freedesktop.NetworkManager", - "/org/freedesktop/NetworkManager", + QDBusInterface nmIntf( NM_SVC_NAME, + NM_PATH, NM_INTF_NAME, QDBusConnection::systemBus(), 0 ); + bool ok = false; int nmState = nmIntf.property( "state" ).toInt( &ok ); diff --git a/src/modules/prepare/PrepareViewStep.h b/src/modules/prepare/PrepareViewStep.h index a29fc82de..b01f9c62b 100644 --- a/src/modules/prepare/PrepareViewStep.h +++ b/src/modules/prepare/PrepareViewStep.h @@ -58,6 +58,7 @@ public: private: bool checkEnoughStorage( qint64 requiredSpace ); bool checkEnoughRam( qint64 requiredRam ); + bool checkBatteryExists(); bool checkHasPower(); bool checkHasInternet();