diff --git a/kded/CMakeLists.txt b/kded/CMakeLists.txt index 303c513..ed848d6 100644 --- a/kded/CMakeLists.txt +++ b/kded/CMakeLists.txt @@ -1,85 +1,87 @@ include_directories ( ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/engine ) set ( kded_plasmavault_SRCS service.cpp engine/vault.cpp engine/backend_p.cpp engine/fusebackend_p.cpp engine/types.cpp engine/commandresult.cpp engine/backends/encfs/encfsbackend.cpp engine/backends/cryfs/cryfsbackend.cpp ui/dialogdsl.cpp ui/activitieslinkingwidget.cpp ui/backendchooserwidget.cpp ui/cryfscypherchooserwidget.cpp ui/directorypairchooserwidget.cpp ui/directorychooserwidget.cpp ui/namechooserwidget.cpp ui/noticewidget.cpp ui/passwordchooserwidget.cpp ui/vaultcreationwizard.cpp ui/vaultconfigurationwizard.cpp + ui/mountdialog.cpp ../common/vaultinfo.cpp ) ki18n_wrap_ui ( kded_plasmavault_SRCS ui/activitieslinkingwidget.ui ui/backendchooserwidget.ui ui/cryfscypherchooserwidget.ui ui/directorypairchooserwidget.ui ui/directorychooserwidget.ui ui/namechooserwidget.ui ui/noticewidget.ui ui/passwordchooserwidget.ui ui/vaultcreationwizard.ui ui/vaultconfigurationwizard.ui + ui/mountdialog.ui ) add_library ( kded_plasmavault MODULE ${kded_plasmavault_SRCS} ) set_target_properties ( kded_plasmavault PROPERTIES OUTPUT_NAME plasmavault ) kcoreaddons_desktop_to_json ( kded_plasmavault plasmavault.desktop ) target_link_libraries ( kded_plasmavault Qt5::Core Qt5::DBus Qt5::Widgets KF5::Activities KF5::ConfigCore KF5::ConfigWidgets KF5::CoreAddons KF5::DBusAddons KF5::I18n KF5::KIOCore KF5::KIOWidgets KF5::SysGuard KF5::WidgetsAddons KF5::ProcessCore ) install ( TARGETS kded_plasmavault DESTINATION ${PLUGIN_INSTALL_DIR}/kf5/kded) diff --git a/kded/service.cpp b/kded/service.cpp index 941b8a6..495c054 100644 --- a/kded/service.cpp +++ b/kded/service.cpp @@ -1,314 +1,275 @@ /* * Copyright 2017 by Ivan Cukic * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of * the License or (at your option) version 3 or any later version * accepted by the membership of KDE e.V. (or its successor approved * by the membership of KDE e.V.), which shall act as a proxy * defined in Section 14 of version 3 of the license. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #include "service.h" #include #include #include #include #include #include #include "engine/vault.h" #include "engine/commandresult.h" #include "ui/vaultcreationwizard.h" #include "ui/vaultconfigurationwizard.h" +#include "ui/mountdialog.h" + +#include K_PLUGIN_FACTORY_WITH_JSON(PlasmaVaultServiceFactory, "plasmavault.json", registerPlugin();) using namespace PlasmaVault; class PlasmaVaultService::Private { public: QHash knownVaults; KActivities::Consumer kamd; Vault* vaultFor(const QString &device_) const { const Device device(device_); if (!knownVaults.contains(device)) { return nullptr; } return knownVaults[device]; } }; PlasmaVaultService::PlasmaVaultService(QObject * parent, const QVariantList&) : KDEDModule(parent) , d(new Private()) { connect(this, &KDEDModule::moduleRegistered, this, &PlasmaVaultService::slotRegistered); connect(&d->kamd, &KActivities::Consumer::currentActivityChanged, this, &PlasmaVaultService::onCurrentActivityChanged); init(); } PlasmaVaultService::~PlasmaVaultService() { } void PlasmaVaultService::init() { for (const Device &device: Vault::availableDevices()) { registerVault(new Vault(device, this)); } } PlasmaVault::VaultInfoList PlasmaVaultService::availableDevices() const { PlasmaVault::VaultInfoList result; for (const auto &vault: d->knownVaults.values()) { const auto vaultData = vault->info(); result << vaultData; } return result; } void PlasmaVaultService::requestNewVault() { const auto dialog = new VaultCreationWizard(); connect(dialog, &VaultCreationWizard::createdVault, this, &PlasmaVaultService::registerVault); dialog->show(); } void PlasmaVaultService::slotRegistered(const QDBusObjectPath &path) { if (path.path() == QLatin1String("/modules/plasmavault")) { emit registered(); } } void PlasmaVaultService::registerVault(Vault *vault) { if (!vault->isValid()) { qWarning() << "Warning: Trying to register an invalid vault: " << vault->device(); return; } if (d->knownVaults.contains(vault->device())) { qWarning() << "Warning: This one is already registered: " << vault->device(); return; } vault->setParent(this); d->knownVaults[vault->device()] = vault; connect(vault, &Vault::statusChanged, this, &PlasmaVaultService::onVaultStatusChanged); connect(vault, &Vault::messageChanged, this, &PlasmaVaultService::onVaultMessageChanged); connect(vault, &Vault::infoChanged, this, &PlasmaVaultService::onVaultInfoChanged); emit vaultAdded(vault->info()); } void PlasmaVaultService::onVaultStatusChanged(VaultInfo::Status status) { Q_UNUSED(status); const auto vault = qobject_cast(sender()); emit vaultChanged(vault->info()); } void PlasmaVaultService::onVaultInfoChanged() { const auto vault = qobject_cast(sender()); emit vaultChanged(vault->info()); } void PlasmaVaultService::onVaultMessageChanged(const QString &message) { Q_UNUSED(message); const auto vault = qobject_cast(sender()); emit vaultChanged(vault->info()); } -template -class PasswordMountDialog: protected KPasswordDialog { //_ -public: - PasswordMountDialog(Vault *vault, Function function) - : m_vault(vault) - , m_function(function) - { - } - - void show() - { - KPasswordDialog::show(); - } - -private: - bool checkPassword() override - { - auto future = m_vault->open({ { KEY_PASSWORD, password() } }); - - const auto result = AsynQt::await(future); - - if (result) { - m_function(); - return true; - - } else { - showErrorMessage(result.error().message()); - return false; - } - } - - void hideEvent(QHideEvent *) override - { - deleteLater(); - } - - Vault *m_vault; - Function m_function; -}; - -template -void showPasswordMountDialog(Vault *vault, Function &&function) +void showPasswordMountDialog(Vault *vault, const std::function &function) { - auto dialog = new PasswordMountDialog( - vault, std::forward(function)); - dialog->show(); + auto dialog = new MountDialog(vault, function); + dialog->open(); } //^ void PlasmaVaultService::openVault(const QString &device) { if (auto vault = d->vaultFor(device)) { if (vault->isOpened()) return; showPasswordMountDialog(vault, [this, vault] { emit vaultChanged(vault->info()); }); } } void PlasmaVaultService::closeVault(const QString &device) { if (auto vault = d->vaultFor(device)) { if (!vault->isOpened()) return; vault->close(); } } void PlasmaVaultService::configureVault(const QString &device) { if (auto vault = d->vaultFor(device)) { const auto dialog = new VaultConfigurationWizard(vault); // connect(dialog, &VaultConfigurationWizard::configurationChanged, // this, &PlasmaVaultService::registerVault); dialog->show(); } } void PlasmaVaultService::forceCloseVault(const QString &device) { if (auto vault = d->vaultFor(device)) { if (!vault->isOpened()) return; vault->forceClose(); } } void PlasmaVaultService::openVaultInFileManager(const QString &device) { if (auto vault = d->vaultFor(device)) { if (vault->isOpened()) { new KRun(QUrl::fromLocalFile((QString)vault->mountPoint()), 0); } else { showPasswordMountDialog(vault, [this, vault] { emit vaultChanged(vault->info()); new KRun(QUrl::fromLocalFile((QString)vault->mountPoint()), 0); }); } } } void PlasmaVaultService::onCurrentActivityChanged( const QString ¤tActivity) { for (auto* vault: d->knownVaults.values()) { const auto vaultActivities = vault->activities(); if (!vaultActivities.isEmpty() && !vaultActivities.contains(currentActivity)) { vault->close(); } } } #include "service.moc" diff --git a/kded/ui/mountdialog.cpp b/kded/ui/mountdialog.cpp new file mode 100644 index 0000000..2cc77d3 --- /dev/null +++ b/kded/ui/mountdialog.cpp @@ -0,0 +1,59 @@ +/* + * Copyright 2017 by Kees vd Broek + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License or (at your option) version 3 or any later version + * accepted by the membership of KDE e.V. (or its successor approved + * by the membership of KDE e.V.), which shall act as a proxy + * defined in Section 14 of version 3 of the license. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include "mountdialog.h" +#include "engine/vault.h" + +#include + + +MountDialog::MountDialog(PlasmaVault::Vault *vault, const std::function &function) + : m_vault(vault), + m_function(function) +{ + m_ui.setupUi(this); + m_ui.errorLabel->setVisible(false); + m_ui.vaultName->setText(vault->name()); + + QStyleOption option; + option.initFrom(this); + const int iconSize = style()->pixelMetric(QStyle::PM_MessageBoxIconSize, &option, this); + m_ui.icon->setPixmap(QIcon::fromTheme(QStringLiteral("dialog-password")).pixmap(iconSize)); +} + +void MountDialog::accept() +{ + setCursor(Qt::WaitCursor); + m_ui.password->lineEdit()->setCursor(Qt::WaitCursor); + QString pwd = m_ui.password->password(); + auto future = m_vault->open({ { KEY_PASSWORD, pwd } }); + const auto result = AsynQt::await(future); + unsetCursor(); + m_ui.password->lineEdit()->unsetCursor(); + + if (result) { + m_function(); + QDialog::accept(); + } else { + qDebug() << "We've got an error" << result.error().message(); + // m_ui.errorLabel->setText(i18n("Failed to open: %1").arg(result.error().message())); + m_ui.errorLabel->setText(i18n("Failed to open: %1", result.error().message())); + m_ui.errorLabel->setVisible(true); + } +} diff --git a/kded/ui/mountdialog.h b/kded/ui/mountdialog.h new file mode 100644 index 0000000..00d4b4d --- /dev/null +++ b/kded/ui/mountdialog.h @@ -0,0 +1,46 @@ +/* + * Copyright 2017 by Kees vd Broek + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License or (at your option) version 3 or any later version + * accepted by the membership of KDE e.V. (or its successor approved + * by the membership of KDE e.V.), which shall act as a proxy + * defined in Section 14 of version 3 of the license. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#ifndef MOUNTDIALOG_H +#define MOUNTDIALOG_H + +#include +#include + +#include "ui_mountdialog.h" + +namespace PlasmaVault { + class Vault; +} + +class MountDialog : public QDialog +{ +public: + MountDialog(PlasmaVault::Vault *vault, const std::function &function); + +protected: + void accept() override; + +private: + PlasmaVault::Vault *m_vault; + std::function m_function; + Ui_MountDialog m_ui; +}; + +#endif diff --git a/kded/ui/mountdialog.ui b/kded/ui/mountdialog.ui new file mode 100644 index 0000000..2bce9a0 --- /dev/null +++ b/kded/ui/mountdialog.ui @@ -0,0 +1,181 @@ + + + MountDialog + + + + 0 + 0 + 488 + 198 + + + + + + + 16 + + + + + + + Qt::Vertical + + + + 0 + 0 + + + + + + + + Please enter the password to open this vault: + + + + + + + + 0 + 0 + + + + + + + + Qt::Vertical + + + + 0 + 0 + + + + + + + + + + Password: + + + password + + + + + + + QLineEdit::Password + + + + + + + + 64 + 64 + + + + + + + 0 + + + + + + + Qt::Vertical + + + + 0 + 0 + + + + + + + + + + + false + + + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + false + + + + + + + + KPasswordLineEdit + QWidget +
kpasswordlineedit.h
+
+
+ + + + buttonBox + accepted() + MountDialog + accept() + + + 248 + 254 + + + 157 + 274 + + + + + buttonBox + rejected() + MountDialog + reject() + + + 316 + 260 + + + 286 + 274 + + + + +