diff --git a/src/smartcard/keypairinfo.cpp b/src/smartcard/keypairinfo.cpp index 602ddfd87..f9db8eada 100644 --- a/src/smartcard/keypairinfo.cpp +++ b/src/smartcard/keypairinfo.cpp @@ -1,84 +1,100 @@ /* smartcard/keypairinfo.cpp This file is part of Kleopatra, the KDE keymanager SPDX-FileCopyrightText: 2020 g10 Code GmbH SPDX-FileContributor: Ingo Klöcker SPDX-License-Identifier: GPL-2.0-or-later */ #include "keypairinfo.h" #include #include using namespace Kleo::SmartCard; // static KeyPairInfo KeyPairInfo::fromStatusLine(const std::string &s) { // The format of a KEYPAIRINFO line is // KEYPAIRINFO [usage] [keytime] [algostr] // The string s does not contain the leading "KEYPAIRINFO ". KeyPairInfo info; const auto values = QString::fromStdString(s).split(QLatin1Char(' ')); if (values.size() < 2) { return info; } info.grip = values[0].toStdString(); info.keyRef = values[1].toStdString(); if (values.size() >= 3) { info.usage = values[2].toStdString(); } if (values.size() >= 4) { info.keyTime = values[3].toStdString(); } if (values.size() >= 5) { info.algorithm = values[4].toStdString(); } return info; } bool KeyPairInfo::canAuthenticate() const { return usage.find('a') != std::string::npos; } bool KeyPairInfo::canCertify() const { return usage.find('c') != std::string::npos; } bool KeyPairInfo::canEncrypt() const { return usage.find('e') != std::string::npos; } bool KeyPairInfo::canSign() const { return usage.find('s') != std::string::npos; } void KeyPairInfo::update(const KeyPairInfo &other) { Q_ASSERT(keyRef == other.keyRef); if (keyRef != other.keyRef) { return; } if (grip != other.grip) { // reset all infos if the grip changed grip = other.grip; usage = std::string(); keyTime = std::string(); algorithm = std::string(); } // now update all infos from other's infos unless other's infos are empty or not specified if (!other.usage.empty() && other.usage != "-") { usage = other.usage; } if (!other.keyTime.empty() && other.keyTime != "-") { keyTime = other.keyTime; } if (!other.algorithm.empty() && other.algorithm != "-") { algorithm = other.algorithm; } } + +// C++20: Replace with defaulted equality operator +bool KeyPairInfo::operator==(const KeyPairInfo &other) const +{ + return keyRef == other.keyRef + && grip == other.grip + && usage == other.usage + && keyTime == other.keyTime + && algorithm == other.algorithm; +} + +// C++20: Remove +bool KeyPairInfo::operator!=(const KeyPairInfo &other) const +{ + return !operator==(other); +} diff --git a/src/smartcard/keypairinfo.h b/src/smartcard/keypairinfo.h index 6abe97dfc..13ac732a6 100644 --- a/src/smartcard/keypairinfo.h +++ b/src/smartcard/keypairinfo.h @@ -1,35 +1,38 @@ /* smartcard/keypairinfo.h This file is part of Kleopatra, the KDE keymanager SPDX-FileCopyrightText: 2020 g10 Code GmbH SPDX-FileContributor: Ingo Klöcker SPDX-License-Identifier: GPL-2.0-or-later */ #pragma once #include namespace Kleo { namespace SmartCard { struct KeyPairInfo { static KeyPairInfo fromStatusLine(const std::string &s); bool canAuthenticate() const; bool canCertify() const; bool canEncrypt() const; bool canSign() const; void update(const KeyPairInfo &other); + bool operator==(const KeyPairInfo &other) const; + bool operator!=(const KeyPairInfo &other) const; + std::string keyRef; std::string grip; std::string usage; std::string keyTime; std::string algorithm; }; } // namespace Smartcard } // namespace Kleopatra