Branch: master

316893d4 2014-03-02 19:49:52 Francois Andriot
Add systemd/logind power management backend to TDE hardware library
This resolves Bug 1941
M CMakeLists.txt
M tdecore/tdehw/CMakeLists.txt
M tdecore/tdehw/tderootsystemdevice.cpp
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 6242410..e97ef57 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -88,6 +88,7 @@
 option( WITH_TDEHWLIB_DAEMONS "Enable daemons for TDE hwlib" ${WITH_ALL_OPTIONS} )
 option( WITH_HAL "Enable HAL support" OFF )
 option( WITH_DEVKITPOWER "Enable DeviceKit Power support" OFF )
+option( WITH_LOGINDPOWER "Enable Logind/Systemd Power support" OFF )
 option( WITH_UPOWER "Enable uPower support" ${WITH_ALL_OPTIONS} )
 option( WITH_UDISKS "Enable uDisks support" ${WITH_ALL_OPTIONS} )
 option( WITH_UDISKS2 "Enable uDisks2 support" ${WITH_ALL_OPTIONS} )
diff --git a/tdecore/tdehw/CMakeLists.txt b/tdecore/tdehw/CMakeLists.txt
index 553cd70..f9354ec 100644
--- a/tdecore/tdehw/CMakeLists.txt
+++ b/tdecore/tdehw/CMakeLists.txt
@@ -36,6 +36,10 @@
   add_definitions( -DWITH_DEVKITPOWER )
 endif( )
 
+if( WITH_LOGINDPOWER )
+  add_definitions( -DWITH_LOGINDPOWER )
+endif( )
+
 if( WITH_UPOWER )
   add_definitions( -DWITH_UPOWER )
 endif( )
diff --git a/tdecore/tdehw/tderootsystemdevice.cpp b/tdecore/tdehw/tderootsystemdevice.cpp
index b62f07d..3951aa7 100644
--- a/tdecore/tdehw/tderootsystemdevice.cpp
+++ b/tdecore/tdehw/tderootsystemdevice.cpp
@@ -221,6 +221,24 @@
 		}
 	}
 
+#ifdef WITH_LOGINDPOWER
+	{
+		TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus);
+		if (dbusConn.isConnected()) {
+			// can suspend?
+			TQT_DBusMessage msg = TQT_DBusMessage::methodCall(
+						"org.freedesktop.login1",
+						"/org/freedesktop/login1",
+						"org.freedesktop.login1.Manager",
+						"CanSuspend");
+			TQT_DBusMessage reply = dbusConn.sendWithReply(msg);
+			if (reply.type() == TQT_DBusMessage::ReplyMessage && reply.count() == 1) {
+				return (reply[0].toString() == "yes");
+			}
+		}
+	}
+#endif // WITH_LOGINDPOWER
+
 #ifdef WITH_UPOWER
 	{
 		TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus);
@@ -322,6 +340,24 @@
 		}
 	}
 
+#ifdef WITH_LOGINDPOWER
+	{
+		TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus);
+		if (dbusConn.isConnected()) {
+			// can hibernate?
+			TQT_DBusMessage msg = TQT_DBusMessage::methodCall(
+						"org.freedesktop.login1",
+						"/org/freedesktop/login1",
+						"org.freedesktop.login1.Manager",
+						"CanHibernate");
+			TQT_DBusMessage reply = dbusConn.sendWithReply(msg);
+			if (reply.type() == TQT_DBusMessage::ReplyMessage && reply.count() == 1) {
+				return (reply[0].toString() == "yes");
+			}
+		}
+	}
+#endif // WITH_LOGINDPOWER
+
 #ifdef WITH_UPOWER
 	{
 		TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus);
@@ -410,41 +446,53 @@
 	config->reparseConfiguration(); // config may have changed in the KControl module
 
 	config->setGroup("General" );
-	bool maysd = false;
-#ifdef WITH_CONSOLEKIT
-	if (config->readBoolEntry( "offerShutdown", true )) {
+	if (!config->readBoolEntry( "offerShutdown", true )) {
+		return FALSE;
+	}
+
+#ifdef WITH_LOGINDPOWER
+	{
 		TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus);
 		if (dbusConn.isConnected()) {
-			TQT_DBusProxy consoleKitManager("org.freedesktop.ConsoleKit", "/org/freedesktop/ConsoleKit/Manager", "org.freedesktop.ConsoleKit.Manager", dbusConn);
-			if (consoleKitManager.canSend()) {
-				// can power off?
-				TQValueList<TQT_DBusData> params;
-				TQT_DBusMessage reply = consoleKitManager.sendWithReply("CanStop", params);
-				if (reply.type() == TQT_DBusMessage::ReplyMessage && reply.count() == 1) {
-					maysd = reply[0].toBool();
-				}
-				else {
-					maysd = false;
-				}
+			// can power off?
+			TQT_DBusMessage msg = TQT_DBusMessage::methodCall(
+						"org.freedesktop.login1",
+						"/org/freedesktop/login1",
+						"org.freedesktop.login1.Manager",
+						"CanPowerOff");
+			TQT_DBusMessage reply = dbusConn.sendWithReply(msg);
+			if (reply.type() == TQT_DBusMessage::ReplyMessage && reply.count() == 1) {
+				return (reply[0].toString() == "yes");
 			}
-			else {
-				maysd = false;
-			}
-		}
-		else {
-			maysd = false;
 		}
 	}
-#else // WITH_CONSOLEKIT
-	// FIXME
-	// Can we power down this system?
-	// This should probably be checked via DCOP and therefore interface with TDM
-	if (config->readBoolEntry( "offerShutdown", true )/* && DM().canShutdown()*/) {	// FIXME
-		maysd = true;
+#endif // WITH_LOGINDPOWER
+
+#ifdef WITH_CONSOLEKIT
+	{
+		TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus);
+		if (dbusConn.isConnected()) {
+			// can power off?
+			TQT_DBusMessage msg = TQT_DBusMessage::methodCall(
+						"org.freedesktop.ConsoleKit",
+						"/org/freedesktop/ConsoleKit/Manager",
+						"org.freedesktop.ConsoleKit.Manager",
+						"CanStop");
+			TQT_DBusMessage reply = dbusConn.sendWithReply(msg);
+			if (reply.type() == TQT_DBusMessage::ReplyMessage && reply.count() == 1) {
+				return reply[0].toBool();
+			}
+		}
 	}
 #endif // WITH_CONSOLEKIT
 
-	return maysd;
+	// FIXME
+	// Can we power down this system?
+	// This should probably be checked via DCOP and therefore interface with TDM
+	// if ( DM().canShutdown() ) {
+	// 	return TRUE;
+	// }
+	return TRUE;
 }
 
 bool TDERootSystemDevice::canReboot() {
@@ -452,41 +500,53 @@
 	config->reparseConfiguration(); // config may have changed in the KControl module
 
 	config->setGroup("General" );
-	bool mayrb = false;
-#ifdef WITH_CONSOLEKIT
-	if (config->readBoolEntry( "offerShutdown", true )) {
+	if (!config->readBoolEntry( "offerShutdown", true )) {
+		return FALSE;
+	}
+
+#ifdef WITH_LOGINDPOWER
+	{
 		TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus);
 		if (dbusConn.isConnected()) {
-			TQT_DBusProxy consoleKitManager("org.freedesktop.ConsoleKit", "/org/freedesktop/ConsoleKit/Manager", "org.freedesktop.ConsoleKit.Manager", dbusConn);
-			if (consoleKitManager.canSend()) {
-				// can reboot?
-				TQValueList<TQT_DBusData> params;
-				TQT_DBusMessage reply = consoleKitManager.sendWithReply("CanRestart", params);
-				if (reply.type() == TQT_DBusMessage::ReplyMessage && reply.count() == 1) {
-					mayrb = reply[0].toBool();
-				}
-				else {
-					mayrb = false;
-				}
+			// can reboot?
+			TQT_DBusMessage msg = TQT_DBusMessage::methodCall(
+						"org.freedesktop.login1",
+						"/org/freedesktop/login1",
+						"org.freedesktop.login1.Manager",
+						"CanReboot");
+			TQT_DBusMessage reply = dbusConn.sendWithReply(msg);
+			if (reply.type() == TQT_DBusMessage::ReplyMessage && reply.count() == 1) {
+				return (reply[0].toString() == "yes");
 			}
-			else {
-				mayrb = false;
-			}
-		}
-		else {
-			mayrb = false;
 		}
 	}
-#else // WITH_CONSOLEKIT
-	// FIXME
-	// Can we power down this system?
-	// This should probably be checked via DCOP and therefore interface with TDM
-	if (config->readBoolEntry( "offerShutdown", true )/* && DM().canShutdown()*/) {	// FIXME
-		mayrb = true;
+#endif // WITH_LOGINDPOWER
+
+#ifdef WITH_CONSOLEKIT
+	{
+		TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus);
+		if (dbusConn.isConnected()) {
+			// can reboot?
+			TQT_DBusMessage msg = TQT_DBusMessage::methodCall(
+						"org.freedesktop.ConsoleKit",
+						"/org/freedesktop/ConsoleKit/Manager",
+						"org.freedesktop.ConsoleKit.Manager",
+						"CanRestart");
+			TQT_DBusMessage reply = dbusConn.sendWithReply(msg);
+			if (reply.type() == TQT_DBusMessage::ReplyMessage && reply.count() == 1) {
+				return reply[0].toBool();
+			}
+		}
 	}
 #endif // WITH_CONSOLEKIT
 
-	return mayrb;
+	// FIXME
+	// Can we power down this system?
+	// This should probably be checked via DCOP and therefore interface with TDM
+	// if ( DM().canShutdown() ) {
+	// 	return TRUE;
+	// }
+	return TRUE;
 }
 
 void TDERootSystemDevice::setHibernationMethod(TDESystemHibernationMethod::TDESystemHibernationMethod hm) {
@@ -574,6 +634,37 @@
 			file.close();
 			return true;
 		}
+
+#ifdef WITH_LOGINDPOWER
+		{
 ** Diff limit reached (max: 250 lines) **