// Squawk messenger. // Copyright (C) 2019 Yury Gubich // // 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 . #include "keysmodel.h" const QHash UI::KeysModel::roles = { {Label, "label"}, {FingerPrint, "fingerPrint"}, {TrustLevel, "trustLevel"} }; UI::KeysModel::KeysModel(QObject* parent): QAbstractListModel(parent), keys(), modified() { } UI::KeysModel::~KeysModel() { for (Shared::KeyInfo* key : keys) { delete key; } for (std::pair& pair: modified) { delete pair.second; } } std::deque UI::KeysModel::modifiedKeys() const { std::deque response(modified.size()); int i = 0; for (const std::pair& pair: modified) { response[i] = *(pair.second); ++i; } return response; } void UI::KeysModel::addKey(const Shared::KeyInfo& info) { beginInsertRows(QModelIndex(), keys.size(), keys.size()); keys.push_back(new Shared::KeyInfo(info)); endInsertRows(); } QVariant UI::KeysModel::data(const QModelIndex& index, int role) const { int i = index.row(); const Shared::KeyInfo* info; bool dirty; std::map::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(info->trustLevel); break; case LastInteraction: answer = info->lastInteraction; break; case Dirty: answer = dirty; break; } return answer; } int UI::KeysModel::rowCount(const QModelIndex& parent) const { return keys.size(); } QHash UI::KeysModel::roleNames() const {return roles;} QModelIndex UI::KeysModel::index(int row, int column, const QModelIndex& parent) const { if (!hasIndex(row, column, parent)) { return QModelIndex(); } return createIndex(row, column, keys[row]); } void UI::KeysModel::revertKey(int row) { std::map::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::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}); }