Commit 71980f21 authored by Ingo Klöcker's avatar Ingo Klöcker
Browse files

Add command for editing a group of keys

GnuPG-bug-id: 5175, 5239
parent faf3fcf1
......@@ -15,7 +15,7 @@ option(DISABLE_KWATCHGNUPG "Don't build the kwatchgnupg tool [default=OFF]" OFF)
# Standalone build. Find / include everything necessary.
set(KF5_MIN_VERSION "5.78.0")
set(KMIME_VERSION "5.16.40")
set(LIBKLEO_VERSION "5.16.49")
set(LIBKLEO_VERSION "5.16.50")
set(QT_REQUIRED_VERSION "5.14.0")
set(GPGME_REQUIRED_VERSION "1.13.1")
......
......@@ -255,6 +255,7 @@ set(_kleopatra_SRCS
commands/importcertificatefrompivcardcommand.cpp
commands/createopenpgpkeyfromcardkeyscommand.cpp
commands/createcsrforcardkeycommand.cpp
commands/editgroupcommand.cpp
${_kleopatra_uiserver_files}
......
/* -*- mode: c++; c-basic-offset:4 -*-
commands/editgroupcommand.cpp
This file is part of Kleopatra, the KDE keymanager
SPDX-FileCopyrightText: 2021 g10 Code GmbH
SPDX-FileContributor: Ingo Klöcker <dev@ingo-kloecker.de>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "editgroupcommand.h"
#include "command_p.h"
#include "dialogs/editgroupdialog.h"
#include <Libkleo/KeyCache>
#include <Libkleo/KeyGroup>
#include <KLocalizedString>
using namespace Kleo;
using namespace Kleo::Commands;
using namespace Kleo::Dialogs;
class EditGroupCommand::Private : public Command::Private
{
friend class ::Kleo::Commands::EditGroupCommand;
EditGroupCommand *q_func() const
{
return static_cast<EditGroupCommand *>(q);
}
public:
explicit Private(EditGroupCommand *qq, const KeyGroup &group, QWidget *parent);
~Private();
private:
void start();
void slotDialogAccepted();
void slotDialogRejected();
void ensureDialogCreated();
private:
KeyGroup group;
QPointer<EditGroupDialog> dialog;
};
EditGroupCommand::Private *EditGroupCommand::d_func()
{
return static_cast<Private *>(d.get());
}
const EditGroupCommand::Private *EditGroupCommand::d_func() const
{
return static_cast<const Private *>(d.get());
}
#define d d_func()
#define q q_func()
EditGroupCommand::Private::Private(EditGroupCommand *qq, const KeyGroup &group, QWidget *parent)
: Command::Private(qq, parent)
, group(group)
{
}
EditGroupCommand::Private::~Private()
{
}
void EditGroupCommand::Private::start()
{
ensureDialogCreated();
dialog->setWindowTitle(i18nc("@title:window", "Edit Group"));
dialog->setGroupName(group.name());
const KeyGroup::Keys &keys = group.keys();
dialog->setGroupKeys(std::vector<GpgME::Key>(keys.cbegin(), keys.cend()));
dialog->show();
}
void EditGroupCommand::Private::slotDialogAccepted()
{
group.setKeys(dialog->groupKeys());
KeyCache::mutableInstance()->update(group);
finished();
}
void EditGroupCommand::Private::slotDialogRejected()
{
canceled();
}
void EditGroupCommand::Private::ensureDialogCreated()
{
if (dialog) {
return;
}
dialog = new EditGroupDialog;
applyWindowID(dialog);
dialog->setAttribute(Qt::WA_DeleteOnClose);
connect(dialog, &QDialog::accepted, q, [this] () { slotDialogAccepted(); });
connect(dialog, &QDialog::rejected, q, [this] () { slotDialogRejected(); });
}
EditGroupCommand::EditGroupCommand(const KeyGroup &group, QWidget *parent)
: Command(new Private(this, group, parent))
{
}
EditGroupCommand::~EditGroupCommand()
{
}
void EditGroupCommand::doStart()
{
d->start();
}
void EditGroupCommand::doCancel()
{
}
#undef d
#undef q
/* -*- mode: c++; c-basic-offset:4 -*-
commands/editgroupcommand.h
This file is part of Kleopatra, the KDE keymanager
SPDX-FileCopyrightText: 2021 g10 Code GmbH
SPDX-FileContributor: Ingo Klöcker <dev@ingo-kloecker.de>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef __KLEOPATRA_COMMANDS_EDITGROUPCOMMAND_H__
#define __KLEOPATRA_COMMANDS_EDITGROUPCOMMAND_H__
#include "command.h"
namespace Kleo
{
class KeyGroup;
namespace Commands
{
class EditGroupCommand : public Command
{
Q_OBJECT
public:
explicit EditGroupCommand(const KeyGroup &group, QWidget *parent = nullptr);
~EditGroupCommand() override;
private:
void doStart() override;
void doCancel() override;
private:
class Private;
inline Private *d_func();
inline const Private *d_func() const;
};
} // namespace Commands
} // namespace Kleo
#endif // __KLEOPATRA_COMMANDS_EDITGROUPCOMMAND_H__
......@@ -10,8 +10,6 @@
#include "editgroupdialog.h"
#include <Libkleo/KeyCache>
#include <Libkleo/KeyGroup>
#include <Libkleo/KeyListModel>
#include <Libkleo/KeyListSortFilterProxyModel>
......@@ -81,7 +79,6 @@ class EditGroupDialog::Private
ProxyModel *availableKeysFilterModel = nullptr;
AbstractKeyListModel *groupKeysModel = nullptr;
ProxyModel *groupKeysFilterModel = nullptr;
KeyGroup group;
public:
Private(EditGroupDialog *qq)
......@@ -221,8 +218,6 @@ private:
void addKeysToGroup();
void removeKeysFromGroup();
void updateGroupKeysKeepingSelection();
};
namespace {
......@@ -252,32 +247,20 @@ void setSelectedKeys(const QListView *view, const std::vector<Key> &keys)
void EditGroupDialog::Private::addKeysToGroup()
{
const std::vector<Key> selectedGroupKeys = getSelectedKeys(ui.groupKeysList);
const std::vector<Key> selectedKeys = getSelectedKeys(ui.availableKeysList);
qCDebug(KLEOPATRA_LOG) << "Number of selected keys:" << selectedKeys.size();
const auto numKeysAdded = std::count_if(selectedKeys.cbegin(), selectedKeys.cend(),
[this] (const Key &key) { return group.insert(key); });
qCDebug(KLEOPATRA_LOG) << "Number of added keys:" << numKeysAdded;
if (numKeysAdded > 0) {
updateGroupKeysKeepingSelection();
}
groupKeysModel->addKeys(selectedKeys);
setSelectedKeys(ui.groupKeysList, selectedGroupKeys);
}
void EditGroupDialog::Private::removeKeysFromGroup()
{
const std::vector<Key> selectedKeys = getSelectedKeys(ui.groupKeysList);
for (const Key &key : selectedKeys) {
group.erase(key);
groupKeysModel->removeKey(key);
}
const KeyGroup::Keys &keys = group.keys();
groupKeysModel->setKeys(std::vector<GpgME::Key>(keys.cbegin(), keys.cend()));
}
void EditGroupDialog::Private::updateGroupKeysKeepingSelection()
{
const std::vector<Key> selectedGroupKeys = getSelectedKeys(ui.groupKeysList);
const KeyGroup::Keys &keys = group.keys();
groupKeysModel->setKeys(std::vector<GpgME::Key>(keys.cbegin(), keys.cend()));
setSelectedKeys(ui.groupKeysList, selectedGroupKeys);
}
EditGroupDialog::EditGroupDialog(QWidget *parent)
......@@ -291,18 +274,25 @@ EditGroupDialog::~EditGroupDialog()
{
}
void EditGroupDialog::setGroup(const KeyGroup &group)
void EditGroupDialog::setGroupName(const QString &name)
{
d->group = group;
d->ui.groupNameLabel->setText(group.name());
const KeyGroup::Keys &keys = group.keys();
d->groupKeysModel->setKeys(std::vector<GpgME::Key>(keys.cbegin(), keys.cend()));
d->ui.groupNameLabel->setText(name);
}
void EditGroupDialog::accept()
void EditGroupDialog::setGroupKeys(const std::vector<Key> &keys)
{
KeyCache::mutableInstance()->update(d->group);
QDialog::accept();
d->groupKeysModel->setKeys(keys);
}
std::vector<Key> EditGroupDialog::groupKeys() const
{
std::vector<Key> keys;
keys.reserve(d->groupKeysModel->rowCount());
for (int row = 0; row < d->groupKeysModel->rowCount(); ++row) {
const QModelIndex index = d->groupKeysModel->index(row, 0);
keys.push_back(d->groupKeysModel->key(index));
}
return keys;
}
#include "editgroupdialog.moc"
......@@ -13,10 +13,15 @@
#include <QDialog>
namespace Kleo
#include <vector>
namespace GpgME
{
class KeyGroup;
class Key;
}
namespace Kleo
{
namespace Dialogs
{
......@@ -27,9 +32,10 @@ public:
explicit EditGroupDialog(QWidget *parent = nullptr);
~EditGroupDialog() override;
void setGroup(const Kleo::KeyGroup &group);
void setGroupName(const QString &name);
void accept() override;
void setGroupKeys(const std::vector<GpgME::Key> &keys);
std::vector<GpgME::Key> groupKeys() const;
private:
class Private;
......
......@@ -10,8 +10,8 @@
#include "groupdetailsdialog.h"
#include "commands/editgroupcommand.h"
#include "commands/detailscommand.h"
#include "dialogs/editgroupdialog.h"
#include "view/keytreeview.h"
#include <Libkleo/KeyGroup>
......@@ -122,9 +122,8 @@ private:
void editGroup()
{
auto dialog = new EditGroupDialog(q->parentWidget());
dialog->setGroup(group);
dialog->show();
auto cmd = new EditGroupCommand(group, q->parentWidget());
cmd->start();
q->reject();
}
};
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment