context menu to trust or distrust keys

This commit is contained in:
Blue 2023-01-15 21:17:38 +03:00
parent b72a837754
commit 73d83f55af
Signed by untrusted user: blue
GPG key ID: 9B203B252A63EE38
5 changed files with 144 additions and 18 deletions

View file

@ -24,12 +24,32 @@ const QHash<int, QByteArray> UI::KeysModel::roles = {
UI::KeysModel::KeysModel(QObject* parent):
QAbstractListModel(parent),
keys()
keys(),
modified()
{
}
UI::KeysModel::~KeysModel() {
for (Shared::KeyInfo* key : keys) {
delete key;
}
for (std::pair<const int, Shared::KeyInfo*>& pair: modified) {
delete pair.second;
}
}
std::deque<Shared::KeyInfo> UI::KeysModel::modifiedKeys() const {
std::deque<Shared::KeyInfo> response(modified.size());
int i = 0;
for (const std::pair<const int, Shared::KeyInfo*>& pair: modified) {
response[i] = *(pair.second);
++i;
}
return response;
}
void UI::KeysModel::addKey(const Shared::KeyInfo& info) {
@ -40,24 +60,34 @@ void UI::KeysModel::addKey(const Shared::KeyInfo& info) {
QVariant UI::KeysModel::data(const QModelIndex& index, int role) const {
int i = index.row();
const Shared::KeyInfo* info;
bool dirty;
std::map<int, Shared::KeyInfo*>::const_iterator itr = modified.find(i);
if (itr != modified.end()) {
info = itr->second;
dirty = true;
} else {
dirty = false;
info = keys[i];
}
QVariant answer;
switch (role) {
case Qt::DisplayRole:
case Label:
answer = keys[i]->label;
answer = info->label;
break;
case FingerPrint:
answer = keys[i]->fingerPrint;
answer = info->fingerPrint;
break;
case TrustLevel:
answer = static_cast<uint8_t>(keys[i]->trustLevel);
answer = static_cast<uint8_t>(info->trustLevel);
break;
case LastInteraction:
answer = keys[i]->lastInteraction;
answer = info->lastInteraction;
break;
case Dirty:
answer = false;
answer = dirty;
break;
}
@ -78,7 +108,34 @@ QModelIndex UI::KeysModel::index(int row, int column, const QModelIndex& parent)
return createIndex(row, column, keys[row]);
}
void UI::KeysModel::revertKey(int row) {
std::map<int, Shared::KeyInfo*>::const_iterator itr = modified.find(row);
if (itr != modified.end()) {
modified.erase(itr);
QModelIndex index = createIndex(row, 0, keys[row]);
dataChanged(index, index);
}
}
void UI::KeysModel::setTrustLevel(int row, Shared::TrustLevel level) {
std::map<int, Shared::KeyInfo*>::const_iterator itr = modified.find(row);
Shared::KeyInfo* info;
if (itr == modified.end()) {
if (row < rowCount()) {
info = new Shared::KeyInfo(*(keys[row]));
modified.insert(std::make_pair(row, info));
} else {
return;
}
} else {
info = itr->second;
}
info->trustLevel = level;
QModelIndex index = createIndex(row, 0, info);
dataChanged(index, index, {KeysModel::Dirty});
}