summaryrefslogtreecommitdiffstats
path: root/tdecore/tdehw/disksHelper.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'tdecore/tdehw/disksHelper.cpp')
-rw-r--r--tdecore/tdehw/disksHelper.cpp191
1 files changed, 175 insertions, 16 deletions
diff --git a/tdecore/tdehw/disksHelper.cpp b/tdecore/tdehw/disksHelper.cpp
index 2589e5053..c47bc5e04 100644
--- a/tdecore/tdehw/disksHelper.cpp
+++ b/tdecore/tdehw/disksHelper.cpp
@@ -17,10 +17,6 @@
Boston, MA 02110-1301, USA.
*/
-#include "disksHelper.h"
-#include "tdelocale.h"
-#include "tdestoragedevice.h"
-
#if defined(WITH_UDISKS) || defined(WITH_UDISKS2)
#include <tqdbusdata.h>
#include <tqdbusmessage.h>
@@ -31,6 +27,11 @@
#include <tqdbusdatamap.h>
#include <tqdbusobjectpath.h>
#include "tqdbusdatalist.h"
+ #include "tqstring.h"
+
+ #include "tdelocale.h"
+ #include "tdestoragedevice.h"
+ #include "disksHelper.h"
#endif
@@ -38,7 +39,7 @@
//-------------------------------
// UDisks
//-------------------------------
-TQStringVariantMap UDisksEjectDrive(TDEStorageDevice *sdevice) {
+TQStringVariantMap udisksEjectDrive(TDEStorageDevice *sdevice) {
TQStringVariantMap result;
result["result"] = false;
@@ -63,15 +64,48 @@ TQStringVariantMap UDisksEjectDrive(TDEStorageDevice *sdevice) {
return result;
}
else {
- result["result"] = true;
- return result;
+ // Eject was successful. Check if the media can be powered off and do so in case
+ TQT_DBusProxy driveInformation("org.freedesktop.UDisks", blockDeviceString,
+ "org.freedesktop.DBus.Properties", dbusConn);
+ params.clear();
+ params << TQT_DBusData::fromString("org.freedesktop.UDisks.Drive") << TQT_DBusData::fromString("DriveCanDetach");
+ TQT_DBusMessage reply = driveInformation.sendWithReply("Get", params, &error);
+ if (error.isValid()) {
+ // Error!
+ result["errStr"] = error.name() + ": " + error.message();
+ return result;
+ }
+
+ if (reply.type() == TQT_DBusMessage::ReplyMessage && reply.count() == 1) {
+ bool canPowerOff = reply[0].toVariant().value.toBool();
+ if (!canPowerOff) {
+ // This drive does not support power off. Just return since the eject operation has finished.
+ result["result"] = true;
+ return result;
+ }
+
+ // Power off the drive!
+ params.clear();
+ TQT_DBusDataMap<TQString> options(TQT_DBusData::Variant);
+ params << TQT_DBusData::fromStringKeyMap(options);
+ TQT_DBusMessage reply = driveControl.sendWithReply("DriveDetach", params, &error);
+ if (error.isValid()) {
+ // Error!
+ result["errStr"] = error.name() + ": " + error.message();
+ return result;
+ }
+ else {
+ result["result"] = true;
+ return result;
+ }
+ }
}
}
}
return result;
}
-TQStringVariantMap UDisksMountDrive(TQString deviceNode, TQString fileSystemType, TQStringList mountOptions) {
+TQStringVariantMap udisksMountDrive(const TQString &deviceNode, const TQString &fileSystemType, TQStringList mountOptions) {
TQStringVariantMap result;
result["result"] = false;
result["retcode"] = -2;
@@ -113,7 +147,7 @@ TQStringVariantMap UDisksMountDrive(TQString deviceNode, TQString fileSystemType
return result;
}
-TQStringVariantMap UDisksUnmountDrive(TQString deviceNode, TQStringList unmountOptions) {
+TQStringVariantMap udisksUnmountDrive(const TQString &deviceNode, TQStringList unmountOptions) {
TQStringVariantMap result;
result["result"] = false;
result["retcode"] = -2;
@@ -160,7 +194,7 @@ TQStringVariantMap UDisksUnmountDrive(TQString deviceNode, TQStringList unmountO
//-------------------------------
// UDisks2
//-------------------------------
-TQStringVariantMap UDisks2EjectDrive(TDEStorageDevice *sdevice) {
+TQStringVariantMap udisks2EjectDrive(TDEStorageDevice *sdevice) {
TQStringVariantMap result;
result["result"] = false;
@@ -192,7 +226,7 @@ TQStringVariantMap UDisks2EjectDrive(TDEStorageDevice *sdevice) {
TQT_DBusProxy driveInformation("org.freedesktop.UDisks2", driveObjectPath,
"org.freedesktop.DBus.Properties", dbusConn);
// can eject?
- TQValueList<TQT_DBusData> params;
+ params.clear();
params << TQT_DBusData::fromString("org.freedesktop.UDisks2.Drive") << TQT_DBusData::fromString("Ejectable");
TQT_DBusMessage reply = driveInformation.sendWithReply("Get", params, &error);
if (error.isValid()) {
@@ -210,7 +244,7 @@ TQStringVariantMap UDisks2EjectDrive(TDEStorageDevice *sdevice) {
// Eject the drive!
TQT_DBusProxy driveControl("org.freedesktop.UDisks2", driveObjectPath, "org.freedesktop.UDisks2.Drive", dbusConn);
- TQValueList<TQT_DBusData> params;
+ params.clear();
TQT_DBusDataMap<TQString> options(TQT_DBusData::Variant);
params << TQT_DBusData::fromStringKeyMap(options);
TQT_DBusMessage reply = driveControl.sendWithReply("Eject", params, &error);
@@ -220,8 +254,39 @@ TQStringVariantMap UDisks2EjectDrive(TDEStorageDevice *sdevice) {
return result;
}
else {
- result["result"] = true;
- return result;
+ // Eject was successful. Check if the media can be powered off and do so in case
+ params.clear();
+ params << TQT_DBusData::fromString("org.freedesktop.UDisks2.Drive") << TQT_DBusData::fromString("CanPowerOff");
+ TQT_DBusMessage reply = driveInformation.sendWithReply("Get", params, &error);
+ if (error.isValid()) {
+ // Error!
+ result["errStr"] = error.name() + ": " + error.message();
+ return result;
+ }
+
+ if (reply.type() == TQT_DBusMessage::ReplyMessage && reply.count() == 1) {
+ bool canPowerOff = reply[0].toVariant().value.toBool();
+ if (!canPowerOff) {
+ // This drive does not support power off. Just return since the eject operation has finished.
+ result["result"] = true;
+ return result;
+ }
+
+ // Power off the drive!
+ params.clear();
+ TQT_DBusDataMap<TQString> options(TQT_DBusData::Variant);
+ params << TQT_DBusData::fromStringKeyMap(options);
+ TQT_DBusMessage reply = driveControl.sendWithReply("PowerOff", params, &error);
+ if (error.isValid()) {
+ // Error!
+ result["errStr"] = error.name() + ": " + error.message();
+ return result;
+ }
+ else {
+ result["result"] = true;
+ return result;
+ }
+ }
}
}
}
@@ -230,7 +295,7 @@ TQStringVariantMap UDisks2EjectDrive(TDEStorageDevice *sdevice) {
return result;
}
-TQStringVariantMap UDisks2MountDrive(TQString deviceNode, TQString fileSystemType, TQString mountOptions) {
+TQStringVariantMap udisks2MountDrive(const TQString &deviceNode, const TQString &fileSystemType, const TQString &mountOptions) {
TQStringVariantMap result;
result["result"] = false;
result["retcode"] = -2;
@@ -276,7 +341,7 @@ TQStringVariantMap UDisks2MountDrive(TQString deviceNode, TQString fileSystemTyp
return result;
}
-TQStringVariantMap UDisks2UnmountDrive(TQString deviceNode, TQString unmountOptions) {
+TQStringVariantMap udisks2UnmountDrive(const TQString &deviceNode, const TQString &unmountOptions) {
TQStringVariantMap result;
result["result"] = false;
result["retcode"] = -2;
@@ -318,4 +383,98 @@ TQStringVariantMap UDisks2UnmountDrive(TQString deviceNode, TQString unmountOpti
}
return result;
}
+
+TQStringVariantMap udisks2UnlockDrive(const TQString &deviceNode, const TQString &passphrase) {
+ TQStringVariantMap result;
+ result["result"] = false;
+ result["retcode"] = -2;
+
+ TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus);
+ if (dbusConn.isConnected()) {
+ TQString blockDeviceString = deviceNode;
+ blockDeviceString.replace("/dev/", "");
+ blockDeviceString.replace("-", "_2d");
+ blockDeviceString = "/org/freedesktop/UDisks2/block_devices/" + blockDeviceString;
+
+ // Unlock/decrypt the drive!
+ TQT_DBusError error;
+ TQT_DBusProxy driveControl("org.freedesktop.UDisks2", blockDeviceString, "org.freedesktop.UDisks2.Encrypted", dbusConn);
+ if (driveControl.canSend()) {
+ TQValueList<TQT_DBusData> params;
+ params << TQT_DBusData::fromString(passphrase);
+ TQMap<TQString, TQT_DBusVariant> optionsMap;
+ params << TQT_DBusData::fromStringKeyMap(TQT_DBusDataMap<TQString>(optionsMap));
+ TQT_DBusMessage reply = driveControl.sendWithReply("Unlock", params, &error);
+ if (!error.isValid()) {
+ if (reply.type() == TQT_DBusMessage::ReplyMessage && reply.count() == 1) {
+ TQT_DBusObjectPath deviceObjectPath = reply[0].toObjectPath();
+ if (deviceObjectPath.isValid()) {
+ // Success
+ result["unlockedDevice"] = deviceObjectPath;
+ result["retcode"] = 0;
+ result["result"] = true;
+ return result;
+ }
+ }
+ result["errStr"] = i18n("Unknown error during unlocking operation.");
+ result["retcode"] = -1;
+ return result;
+ }
+ else {
+ // Error!
+ if (error.name() == "org.freedesktop.DBus.Error.ServiceUnknown") {
+ return result; // Service not installed or unavailable
+ }
+ else {
+ result["errStr"] = error.name() + ": " + error.message();
+ result["retcode"] = -1;
+ return result;
+ }
+ }
+ }
+ }
+ return result;
+}
+
+TQStringVariantMap udisks2LockDrive(const TQString &deviceNode) {
+ TQStringVariantMap result;
+ result["result"] = false;
+ result["retcode"] = -2;
+
+ TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus);
+ if (dbusConn.isConnected()) {
+ TQString blockDeviceString = deviceNode;
+ blockDeviceString.replace("/dev/", "");
+ blockDeviceString.replace("-", "_2d");
+ blockDeviceString = "/org/freedesktop/UDisks2/block_devices/" + blockDeviceString;
+
+ // Lock/encrypt the drive!
+ TQT_DBusError error;
+ TQT_DBusProxy driveControl("org.freedesktop.UDisks2", blockDeviceString, "org.freedesktop.UDisks2.Encrypted", dbusConn);
+ if (driveControl.canSend()) {
+ TQValueList<TQT_DBusData> params;
+ TQMap<TQString, TQT_DBusVariant> optionsMap;
+ params << TQT_DBusData::fromStringKeyMap(TQT_DBusDataMap<TQString>(optionsMap));
+ TQT_DBusMessage reply = driveControl.sendWithReply("Lock", params, &error);
+ if (!error.isValid()) {
+ // Success
+ result["retcode"] = 0;
+ result["result"] = true;
+ return result;
+ }
+ else {
+ // Error!
+ if (error.name() == "org.freedesktop.DBus.Error.ServiceUnknown") {
+ return result; // Service not installed or unavailable
+ }
+ else {
+ result["errStr"] = error.name() + ": " + error.message();
+ result["retcode"] = -1;
+ return result;
+ }
+ }
+ }
+ }
+ return result;
+}
#endif