1
0
forked from blue/squawk
squawk/ui/models/info/omemo/keys.cpp

145 lines
3.9 KiB
C++
Raw Normal View History

2023-01-03 15:27:03 +00:00
// Squawk messenger.
// Copyright (C) 2019 Yury Gubich <blue@macaw.me>
//
// 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 3 of the License, or
// (at your option) any later version.
//
// 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 <http://www.gnu.org/licenses/>.
2023-02-01 15:56:00 +00:00
#include "keys.h"
2023-01-03 15:27:03 +00:00
2023-08-15 15:28:25 +00:00
#include "shared/defines.h"
2023-02-01 15:56:00 +00:00
const QHash<int, QByteArray> Models::Keys::roles = {
2023-01-03 15:27:03 +00:00
{Label, "label"},
{FingerPrint, "fingerPrint"},
{TrustLevel, "trustLevel"}
};
2023-02-01 15:56:00 +00:00
Models::Keys::Keys(QObject* parent):
2023-01-03 15:27:03 +00:00
QAbstractListModel(parent),
2023-01-15 18:17:38 +00:00
keys(),
2023-02-01 15:56:00 +00:00
modified() {}
2023-01-15 18:17:38 +00:00
2023-02-01 15:56:00 +00:00
Models::Keys::~Keys() {
2023-08-15 15:28:25 +00:00
for (Shared::KeyInfo* key : keys)
2023-01-15 18:17:38 +00:00
delete key;
2023-08-15 15:28:25 +00:00
for (std::pair<const int, Shared::KeyInfo*>& pair: modified)
2023-01-15 18:17:38 +00:00
delete pair.second;
}
2023-02-01 15:56:00 +00:00
std::deque<Shared::KeyInfo> Models::Keys::modifiedKeys() const {
2023-01-15 18:17:38 +00:00
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;
}
2023-01-03 15:27:03 +00:00
2023-01-15 18:17:38 +00:00
return response;
2023-01-03 15:27:03 +00:00
}
2023-02-01 15:56:00 +00:00
void Models::Keys::addKey(const Shared::KeyInfo& info) {
2023-01-03 15:27:03 +00:00
beginInsertRows(QModelIndex(), keys.size(), keys.size());
keys.push_back(new Shared::KeyInfo(info));
endInsertRows();
}
2023-02-01 15:56:00 +00:00
QVariant Models::Keys::data(const QModelIndex& index, int role) const {
2023-01-03 15:27:03 +00:00
int i = index.row();
2023-01-15 18:17:38 +00:00
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];
}
2023-01-03 15:27:03 +00:00
QVariant answer;
switch (role) {
case Qt::DisplayRole:
case Label:
2023-01-15 18:17:38 +00:00
answer = info->label;
2023-01-03 15:27:03 +00:00
break;
2023-01-07 14:30:22 +00:00
case FingerPrint:
2023-01-15 18:17:38 +00:00
answer = info->fingerPrint;
2023-01-07 14:30:22 +00:00
break;
case TrustLevel:
2023-01-15 18:17:38 +00:00
answer = static_cast<uint8_t>(info->trustLevel);
break;
case LastInteraction:
2023-01-15 18:17:38 +00:00
answer = info->lastInteraction;
break;
case Dirty:
2023-01-15 18:17:38 +00:00
answer = dirty;
break;
2023-01-03 15:27:03 +00:00
}
return answer;
}
2023-02-01 15:56:00 +00:00
int Models::Keys::rowCount(const QModelIndex& parent) const {
2023-08-15 15:28:25 +00:00
SHARED_UNUSED(parent);
2023-01-03 15:27:03 +00:00
return keys.size();
}
2023-02-01 15:56:00 +00:00
QHash<int, QByteArray> Models::Keys::roleNames() const {return roles;}
2023-01-03 15:27:03 +00:00
2023-02-01 15:56:00 +00:00
QModelIndex Models::Keys::index(int row, int column, const QModelIndex& parent) const {
2023-08-15 15:28:25 +00:00
if (!hasIndex(row, column, parent))
2023-01-03 15:27:03 +00:00
return QModelIndex();
return createIndex(row, column, keys[row]);
}
2023-02-01 15:56:00 +00:00
void Models::Keys::revertKey(int row) {
2023-01-15 18:17:38 +00:00
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);
}
}
2023-01-03 15:27:03 +00:00
2023-02-01 15:56:00 +00:00
void Models::Keys::setTrustLevel(int row, Shared::TrustLevel level) {
2023-01-15 18:17:38 +00:00
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);
2023-02-01 15:56:00 +00:00
dataChanged(index, index, {Keys::Dirty});
2023-01-15 18:17:38 +00:00
}
2023-01-03 15:27:03 +00:00
void Models::Keys::clear() {
beginResetModel();
keys.clear();
modified.clear();
endResetModel();
}
2023-01-03 15:27:03 +00:00