1
0
mirror of https://github.com/QRouland/UTPass.git synced 2025-02-11 15:07:16 +00:00
UTPass/plugins/Pass/passkeyringmodel.h

130 lines
4.3 KiB
C
Raw Normal View History

2025-02-03 18:45:25 +01:00
#ifndef PASSKEYRINGMODEL_H
#define PASSKEYRINGMODEL_H
2019-09-20 21:29:39 +02:00
2025-01-30 22:46:46 +01:00
#include <QDebug>
2019-09-20 21:29:39 +02:00
#include <QObject>
2025-02-01 13:45:55 +01:00
#include <QJsonDocument>
#include <QJsonArray>
2025-01-30 22:46:46 +01:00
#include <QSet>
2025-01-14 12:20:55 +01:00
2025-01-15 23:40:35 +01:00
/**
* @class PassKeyModel
2025-02-01 13:45:55 +01:00
* @brief A model representing a GPG (GNU Privacy Guard) key.
*
2025-01-15 23:40:35 +01:00
*/
2025-01-14 12:20:55 +01:00
class PassKeyModel : public QObject
{
Q_OBJECT
2025-02-01 13:45:55 +01:00
Q_PROPERTY(QString fingerprint MEMBER m_fingerprint CONSTANT)
Q_PROPERTY(QString keyid MEMBER m_keyid CONSTANT)
Q_PROPERTY(QVariant userids MEMBER m_userids CONSTANT)
Q_PROPERTY(bool hasSecret MEMBER m_hasSecret CONSTANT)
2025-01-14 12:20:55 +01:00
2025-01-15 23:40:35 +01:00
private:
2025-02-01 13:45:55 +01:00
QString m_fingerprint; /**< The fingerprint of the GPG key, used to uniquely identify the key. */
QString m_keyid; /**< The unique ID associated with the GPG key. */
QVariant m_userids; /**< A list of user IDs associated with the GPG key. */
bool m_hasSecret; /**< Indicates whether the GPG key has an associated secret key. */
2019-09-20 21:29:39 +02:00
2025-01-15 23:40:35 +01:00
public:
/**
2025-02-01 13:45:55 +01:00
* @brief Constructs a PassKeyModel object using the provided GPG key information.
*
* This constructor initializes the PassKeyModel based on a JSON document containing GPG key data.
* The key data typically includes the key's fingerprint, key ID, associated user IDs, and secret key status.
*
* @param key_info A JSON document containing the GPG key data.
2025-01-15 23:40:35 +01:00
*/
2025-02-01 13:45:55 +01:00
PassKeyModel(QJsonDocument key_info)
{
this->m_fingerprint = key_info["fingerprint"].toString();
2025-02-03 21:46:21 +01:00
qDebug() << "[PassKeyModel] fingerprint : " << this->m_fingerprint;
2025-01-15 23:40:35 +01:00
2025-02-01 13:45:55 +01:00
this->m_keyid = key_info["keyid"].toString();
2025-02-03 21:46:21 +01:00
qDebug() << "[PassKeyModel] keyid : " << this->m_keyid;
2019-09-20 21:29:39 +02:00
2025-02-01 13:45:55 +01:00
auto user_ids_json_array = key_info["userids"].toArray();
auto userids = QList<QString>();
for (auto i = user_ids_json_array.begin(), end = user_ids_json_array.end(); i != end; ++i) {
userids.append((*i).toString());
}
this->m_userids = QVariant(userids);
2025-02-03 21:46:21 +01:00
qDebug() << "[PassKeyModel] userids : " << this->m_userids;
2025-02-01 13:45:55 +01:00
this->m_hasSecret = key_info["secret key"]["present"].toBool();
2025-02-03 21:46:21 +01:00
qDebug() << "[PassKeyModel] hasSecret : " << this->m_hasSecret;
2025-02-01 13:45:55 +01:00
}
2025-01-30 22:46:46 +01:00
};
2025-01-14 12:20:55 +01:00
2025-01-30 22:46:46 +01:00
/**
2025-02-01 13:45:55 +01:00
* @class PassKeyringModel
* @brief A model representing a collection of GPG keys.
*
* This class serves as a container for multiple GPG keys, typically representing an entire
* keyring. It provides functionality to manage and retrieve keys, such as fetching all keys
* in the keyring and determining the length of the keyring.
2025-01-30 22:46:46 +01:00
*
2025-02-01 13:45:55 +01:00
* The class also includes logic to distinguish between primary and sub keys, with an option
* to ignore subkeys if desired.
2025-01-30 22:46:46 +01:00
*/
class PassKeyringModel : public QObject
{
Q_OBJECT
2025-02-01 13:45:55 +01:00
Q_PROPERTY(QList<QObject *> keys MEMBER m_keys CONSTANT)
2025-01-30 22:46:46 +01:00
Q_PROPERTY(int length READ length CONSTANT)
2019-09-20 21:29:39 +02:00
2025-01-30 22:46:46 +01:00
private:
2025-02-01 13:45:55 +01:00
QList<QObject *> m_keys; /**< A list of PassKeyModel objects representing the keys in the keyring. */
2025-01-30 22:46:46 +01:00
public:
2025-01-15 23:40:35 +01:00
/**
2025-02-01 13:45:55 +01:00
* @brief Constructs a PassKeyringModel from a list of GPG key JSON documents.
*
* This constructor initializes the PassKeyringModel by parsing a list of JSON documents
* that represent multiple GPG keys. It filters out subkeys and only retains primary keys
* for inclusion in the keyring.
*
* @param key_infos A list of JSON documents representing GPG keys.
2025-01-15 23:40:35 +01:00
*/
2025-02-01 13:45:55 +01:00
PassKeyringModel(QList<QJsonDocument> key_infos)
2019-09-20 21:29:39 +02:00
{
2025-02-01 13:45:55 +01:00
for (auto i = key_infos.begin(), end = key_infos.end(); i != end; ++i) {
2025-02-03 21:46:21 +01:00
qDebug() << "[PassKeyringModel]" << *i;
2025-02-01 13:45:55 +01:00
// Ignore subkeys and only add primary keys to the model.
if ((*i)["primary key grip"].isUndefined()) {
this->m_keys.append(new PassKeyModel(*i));
} else {
2025-02-03 21:46:21 +01:00
qDebug() << "[PassKeyringModel] Subkey info " << (*i)["keyid"].toString() << "ignored";
2025-02-01 13:45:55 +01:00
}
2025-01-30 22:46:46 +01:00
}
}
2025-02-01 13:45:55 +01:00
/**
* @brief Destructor for PassKeyringModel.
*
* Cleans up dynamically allocated PassKeyModel objects within the keyring.
*/
~PassKeyringModel()
{
2025-01-30 22:46:46 +01:00
qDeleteAll(this->m_keys);
}
2025-02-01 13:45:55 +01:00
/**
* @brief Retrieves the number of keys in the keyring.
*
* This function returns the number of primary keys present in the keyring.
*
* @return The number of keys in the keyring.
*/
int length()
{
2025-01-30 22:46:46 +01:00
return this->m_keys.length();
}
2019-09-20 21:29:39 +02:00
};
2025-02-01 13:45:55 +01:00
2025-02-03 18:45:25 +01:00
#endif // PASSKEYRINGMODEL_H