From 3bef0b835a03f868eda0f5097d93daed2a42a8fe Mon Sep 17 00:00:00 2001
From: Teo Mrnjavac <teo@kde.org>
Date: Mon, 25 Aug 2014 15:02:09 +0200
Subject: [PATCH] Check for mains power in prepare viewmodule.

---
 src/modules/prepare/PrepareViewStep.cpp | 62 +++++++++++++++++++++++--
 src/modules/prepare/PrepareViewStep.h   |  1 +
 2 files changed, 60 insertions(+), 3 deletions(-)

diff --git a/src/modules/prepare/PrepareViewStep.cpp b/src/modules/prepare/PrepareViewStep.cpp
index 86174a1534..174c4b9e17 100644
--- a/src/modules/prepare/PrepareViewStep.cpp
+++ b/src/modules/prepare/PrepareViewStep.cpp
@@ -28,6 +28,8 @@
 #include <QBoxLayout>
 #include <QDBusConnection>
 #include <QDBusInterface>
+#include <QDir>
+#include <QFileInfo>
 #include <QLabel>
 #include <QProcess>
 
@@ -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 a29fc82de2..b01f9c62b5 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();
 
-- 
GitLab