forked from blue/squawk
163 lines
4.3 KiB
C++
163 lines
4.3 KiB
C++
/*
|
|
* 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/>.
|
|
*/
|
|
|
|
#include "keys.h"
|
|
|
|
#include "shared/defines.h"
|
|
|
|
const QHash<int, QByteArray> Models::Keys::roles = {
|
|
{Label, "label"},
|
|
{FingerPrint, "fingerPrint"},
|
|
{TrustLevel, "trustLevel"}
|
|
};
|
|
|
|
Models::Keys::Keys(QObject* parent):
|
|
QAbstractListModel(parent),
|
|
keys(),
|
|
modified() {}
|
|
|
|
Models::Keys::~Keys() {
|
|
for (Shared::KeyInfo* key : keys)
|
|
delete key;
|
|
|
|
for (std::pair<const int, Shared::KeyInfo*>& pair: modified)
|
|
delete pair.second;
|
|
}
|
|
|
|
std::list<Shared::KeyInfo> Models::Keys::modifiedKeys() const {
|
|
std::list<Shared::KeyInfo> response;
|
|
for (const std::pair<const int, Shared::KeyInfo*>& pair: modified)
|
|
response.push_back(*(pair.second));
|
|
|
|
|
|
return response;
|
|
}
|
|
|
|
std::list<Shared::KeyInfo> Models::Keys::finalKeys() const {
|
|
std::list<Shared::KeyInfo> result;
|
|
finalKeys(result);
|
|
return result;
|
|
}
|
|
|
|
void Models::Keys::finalKeys(std::list<Shared::KeyInfo>& out) const {
|
|
for (int i = 0; i < rowCount(); ++i)
|
|
out.push_back(key(i));
|
|
}
|
|
|
|
const Shared::KeyInfo & Models::Keys::key(unsigned int i) const {
|
|
std::map<int, Shared::KeyInfo*>::const_iterator itr = modified.find(i);
|
|
if (itr != modified.end())
|
|
return*(itr->second);
|
|
else
|
|
return *(keys[i]);
|
|
}
|
|
|
|
void Models::Keys::addKey(const Shared::KeyInfo& info) {
|
|
beginInsertRows(QModelIndex(), keys.size(), keys.size());
|
|
keys.push_back(new Shared::KeyInfo(info));
|
|
endInsertRows();
|
|
}
|
|
|
|
QVariant Models::Keys::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 = info->label;
|
|
break;
|
|
case FingerPrint:
|
|
answer = info->fingerPrint;
|
|
break;
|
|
case TrustLevel:
|
|
answer = static_cast<uint8_t>(info->trustLevel);
|
|
break;
|
|
case LastInteraction:
|
|
answer = info->lastInteraction;
|
|
break;
|
|
case Dirty:
|
|
answer = dirty;
|
|
break;
|
|
}
|
|
|
|
return answer;
|
|
}
|
|
|
|
int Models::Keys::rowCount(const QModelIndex& parent) const {
|
|
SHARED_UNUSED(parent);
|
|
return keys.size();
|
|
}
|
|
|
|
QHash<int, QByteArray> Models::Keys::roleNames() const {return roles;}
|
|
|
|
QModelIndex Models::Keys::index(int row, int column, const QModelIndex& parent) const {
|
|
if (!hasIndex(row, column, parent))
|
|
return QModelIndex();
|
|
|
|
return createIndex(row, column, keys[row]);
|
|
}
|
|
|
|
void Models::Keys::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 Models::Keys::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, {Keys::Dirty});
|
|
}
|
|
|
|
void Models::Keys::clear() {
|
|
beginResetModel();
|
|
keys.clear();
|
|
modified.clear();
|
|
endResetModel();
|
|
}
|
|
|
|
|