From 53f8622a6a4a47a49431bcbb4f63aa09220bb835 Mon Sep 17 00:00:00 2001 From: Michele Calgaro Date: Sun, 25 Oct 2020 14:13:23 +0800 Subject: tdehwmonitor: added Unlock/Lock buttons to LUKS disks. Signed-off-by: Michele Calgaro --- kcontrol/hwmanager/devicepropsdlg.cpp | 97 ++++++++++++++++++++++++++++++++++- 1 file changed, 95 insertions(+), 2 deletions(-) (limited to 'kcontrol/hwmanager/devicepropsdlg.cpp') diff --git a/kcontrol/hwmanager/devicepropsdlg.cpp b/kcontrol/hwmanager/devicepropsdlg.cpp index 05b5832fa..717a23942 100644 --- a/kcontrol/hwmanager/devicepropsdlg.cpp +++ b/kcontrol/hwmanager/devicepropsdlg.cpp @@ -29,6 +29,8 @@ #include #include #include +#include +#include #undef Unsorted // Required for --enable-final (tqdir.h) #include @@ -45,6 +47,7 @@ #include #include "cryptpassworddlg.h" +#include "passworddlg.h" #include "devicepropsdlg.h" @@ -250,7 +253,7 @@ void SensorDisplayWidget::updateDisplay() { } DevicePropertiesDialog::DevicePropertiesDialog(TDEGenericDevice* device, TQWidget *parent) - : KDialogBase(Plain, TQString::null, Ok|Cancel, Ok, parent, 0L, true, true) + : KDialogBase(Plain, TQString::null, Ok|Cancel, Ok, parent, 0L, true, true), m_passDlg(NULL) { m_device = device; enableButtonOK( false ); @@ -301,6 +304,8 @@ DevicePropertiesDialog::DevicePropertiesDialog(TDEGenericDevice* device, TQWidge TDEStorageDevice* sdevice = static_cast(m_device); connect(base->buttonDiskMount, TQT_SIGNAL(clicked()), this, TQT_SLOT(mountDisk())); connect(base->buttonDiskUnmount, TQT_SIGNAL(clicked()), this, TQT_SLOT(unmountDisk())); + connect(base->buttonDiskUnlock, TQT_SIGNAL(clicked()), this, TQT_SLOT(unlockDisk())); + connect(base->buttonDiskLock, TQT_SIGNAL(clicked()), this, TQT_SLOT(lockDisk())); if (sdevice->isDiskOfType(TDEDiskDeviceType::LUKS)) { connect(base->cryptLUKSAddKey, TQT_SIGNAL(clicked()), this, TQT_SLOT(cryptLUKSAddKey())); connect(base->cryptLUKSDelKey, TQT_SIGNAL(clicked()), this, TQT_SLOT(cryptLUKSDelKey())); @@ -345,6 +350,10 @@ DevicePropertiesDialog::DevicePropertiesDialog(TDEGenericDevice* device, TQWidge DevicePropertiesDialog::~DevicePropertiesDialog() { + if (m_passDlg) + { + delete m_passDlg; + } } void DevicePropertiesDialog::processHardwareRemoved(TDEGenericDevice* dev) { @@ -450,16 +459,32 @@ void DevicePropertiesDialog::populateDeviceInformation() { status_text += "Hidden
"; } if (status_text == "") { - status_text += "Unavailable"; + status_text += "Unknown"; } status_text += ""; base->labelDiskStatus->setText(status_text); // Update mount/unmount button status + base->buttonDiskMount->setEnabled(false); + base->buttonDiskUnmount->setEnabled(false); + base->buttonDiskUnlock->setEnabled(false); + base->buttonDiskLock->setEnabled(false); + base->buttonDiskMount->setHidden(true); + base->buttonDiskUnmount->setHidden(true); + base->buttonDiskUnlock->setHidden(true); + base->buttonDiskLock->setHidden(true); if (sdevice->checkDiskStatus(TDEDiskDeviceStatus::Mountable)) { base->groupDiskActions->show(); base->buttonDiskMount->setEnabled((sdevice->mountPath() == "")); base->buttonDiskUnmount->setEnabled((sdevice->mountPath() != "")); + base->buttonDiskMount->setHidden(false); + base->buttonDiskUnmount->setHidden(false); + } + else if (sdevice->isDiskOfType(TDEDiskDeviceType::LUKS)) { + base->buttonDiskUnlock->setEnabled(!sdevice->isDiskOfType(TDEDiskDeviceType::UnlockedCrypt)); + base->buttonDiskLock->setEnabled(sdevice->isDiskOfType(TDEDiskDeviceType::UnlockedCrypt)); + base->buttonDiskUnlock->setHidden(false); + base->buttonDiskLock->setHidden(false); } else { base->groupDiskActions->hide(); @@ -928,6 +953,74 @@ void DevicePropertiesDialog::unmountDisk() { populateDeviceInformation(); } +void DevicePropertiesDialog::unlockDisk() { + TDEStorageDevice* sdevice = static_cast(m_device); + + if (!m_passDlg) + { + m_passDlg = new PasswordDlg(sdevice->deviceNode(), "drive-harddisk-locked"); + connect(m_passDlg, TQT_SIGNAL(user1Clicked()), this, TQT_SLOT(doUnlockDisk())); + } + m_passDlg->show(); +} + +void DevicePropertiesDialog::doUnlockDisk() { + TDEStorageDevice* sdevice = static_cast(m_device); + + // Use DCOP call to unlock the disk to make sure the status and mime type of the underlying medium + // is correctly updated throughout TDE + TQString qerror; + DCOPRef mediamanager("kded", "mediamanager"); + DCOPReply reply = mediamanager.call("unlockByNode", sdevice->deviceNode(), m_passDlg->getPassword()); + TQStringVariantMap unlockResult; + if (reply.isValid()) { + reply.get(unlockResult); + } + if (!unlockResult.contains("result") || !unlockResult["result"].toBool()) { + qerror = i18n("Unable to unlock this device.

Potential reasons include:
Wrong password and/or user privilege level.
Corrupt data on storage device."); + TQString errStr = unlockResult.contains("errStr") ? unlockResult["errStr"].toString() : i18n("Unknown unlock error."); + if (!errStr.isEmpty()) { + qerror.append(i18n("

Technical details:
").append(errStr)); + } + qerror.append(""); + } + else { + m_passDlg->hide(); + qerror = ""; + } + + if (qerror != "") KMessageBox::error(this, qerror, i18n("Unlock Failed")); + + populateDeviceInformation(); +} + +void DevicePropertiesDialog::lockDisk() { + TDEStorageDevice* sdevice = static_cast(m_device); + + // Use DCOP call to lock the disk to make sure the status and mime type of the underlying medium + // is correctly updated throughout TDE + TQString qerror; + DCOPRef mediamanager("kded", "mediamanager"); + DCOPReply reply = mediamanager.call("lockByNode", sdevice->deviceNode()); + TQStringVariantMap lockResult; + if (reply.isValid()) { + reply.get(lockResult); + } + if (lockResult["result"].toBool() == false) { + // Lock failed! + qerror = "" + i18n("Unfortunately, the device could not be locked."); + TQString errStr = lockResult.contains("errStr") ? lockResult["errStr"].toString() : TQString::null; + if (!errStr.isEmpty()) { + qerror.append(i18n("

Technical details:
").append(errStr)); + } + qerror.append(""); + } + + if (qerror != "") KMessageBox::error(this, qerror, i18n("Lock Failed")); + + populateDeviceInformation(); +} + void DevicePropertiesDialog::cryptLUKSAddKey() { int retcode; -- cgit v1.2.1