started refactoring of the VCard UI

This commit is contained in:
Blue 2023-02-01 18:56:00 +03:00
parent bb304ce774
commit 4af16b75bf
Signed by untrusted user: blue
GPG key ID: 9B203B252A63EE38
32 changed files with 1250 additions and 166 deletions

View file

@ -0,0 +1,10 @@
target_sources(squawk PRIVATE
emails.cpp
emails.h
phones.cpp
phones.h
)
if (WITH_OMEMO)
add_subdirectory(omemo)
endif()

189
ui/models/info/emails.cpp Normal file
View file

@ -0,0 +1,189 @@
/*
* 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 "emails.h"
#include "shared/icons.h"
#include <QCoreApplication>
Models::EMails::EMails(bool p_edit, QObject* parent):
QAbstractTableModel(parent),
edit(p_edit),
deque() {}
int Models::EMails::columnCount(const QModelIndex& parent) const {
return 3;}
int Models::EMails::rowCount(const QModelIndex& parent) const {
return deque.size();}
void Models::EMails::revertPreferred(quint32 row) {
setData(createIndex(row, 2), !isPreferred(row));}
QString Models::EMails::getEmail(quint32 row) const {
return deque[row].address;}
QVariant Models::EMails::data(const QModelIndex& index, int role) const {
if (index.isValid()) {
int col = index.column();
switch (col) {
case 0:
switch (role) {
case Qt::DisplayRole:
case Qt::EditRole:
return deque[index.row()].address;
default:
return QVariant();
}
break;
case 1:
switch (role) {
case Qt::DisplayRole:
return QCoreApplication::translate("Global", Shared::VCard::Email::roleNames[deque[index.row()].role].toStdString().c_str());
case Qt::EditRole:
return deque[index.row()].role;
default:
return QVariant();
}
break;
case 2:
switch (role) {
case Qt::DisplayRole:
return QVariant();
case Qt::DecorationRole:
if (deque[index.row()].prefered) {
return Shared::icon("favorite", false);
}
return QVariant();
default:
return QVariant();
}
break;
default:
return QVariant();
}
}
return QVariant();
}
Qt::ItemFlags Models::EMails::flags(const QModelIndex& index) const {
Qt::ItemFlags f = QAbstractTableModel::flags(index);
if (edit && index.column() != 2) {
f = Qt::ItemIsEditable | f;
}
return f;
}
bool Models::EMails::setData(const QModelIndex& index, const QVariant& value, int role) {
if (role == Qt::EditRole && checkIndex(index)) {
Shared::VCard::Email& item = deque[index.row()];
switch (index.column()) {
case 0:
item.address = value.toString();
return true;
case 1: {
quint8 newRole = value.toUInt();
if (newRole > Shared::VCard::Email::work) {
return false;
}
item.role = static_cast<Shared::VCard::Email::Role>(newRole);
return true;
}
case 2: {
bool newDef = value.toBool();
if (newDef != item.prefered) {
if (newDef) {
//dropPrefered();
}
item.prefered = newDef;
return true;
}
}
}
return true;
}
return false;
}
bool Models::EMails::dropPrefered() {
bool dropped = false;
int i = 0;
for (Shared::VCard::Email& email : deque) {
if (email.prefered) {
email.prefered = false;
QModelIndex ci = createIndex(i, 2, &email);
emit dataChanged(ci, ci);
dropped = true;
}
++i;
}
return dropped;
}
QModelIndex Models::EMails::addNewEmptyLine() {
beginInsertRows(QModelIndex(), deque.size(), deque.size());
deque.emplace_back("");
endInsertRows();
return createIndex(deque.size() - 1, 0, &(deque.back()));
}
bool Models::EMails::isPreferred(quint32 row) const {
if (row < deque.size()) {
return deque[row].prefered;
} else {
return false;
}
}
void Models::EMails::removeLines(quint32 index, quint32 count) {
if (index < deque.size()) {
quint32 maxCount = deque.size() - index;
if (count > maxCount) {
count = maxCount;
}
if (count > 0) {
beginRemoveRows(QModelIndex(), index, index + count - 1);
std::deque<Shared::VCard::Email>::const_iterator itr = deque.begin() + index;
std::deque<Shared::VCard::Email>::const_iterator end = itr + count;
deque.erase(itr, end);
endRemoveRows();
}
}
}
void Models::EMails::getEmails(std::deque<Shared::VCard::Email>& emails) const {
for (const Shared::VCard::Email& my : deque) {
emails.emplace_back(my);
}
}
void Models::EMails::setEmails(const std::deque<Shared::VCard::Email>& emails) {
if (deque.size() > 0) {
removeLines(0, deque.size());
}
if (emails.size() > 0) {
beginInsertRows(QModelIndex(), 0, emails.size() - 1);
for (const Shared::VCard::Email& comming : emails) {
deque.emplace_back(comming);
}
endInsertRows();
}
}

62
ui/models/info/emails.h Normal file
View file

@ -0,0 +1,62 @@
/*
* 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/>.
*/
#ifndef MODELS_EMAILS_H
#define MODELS_EMAILS_H
#include <QAbstractTableModel>
#include <QIcon>
#include <deque>
#include "shared/vcard.h"
namespace Models {
class EMails : public QAbstractTableModel {
Q_OBJECT
public:
EMails(bool edit = false, QObject *parent = nullptr);
int rowCount(const QModelIndex& parent = QModelIndex()) const override;
int columnCount(const QModelIndex& parent = QModelIndex()) const override;
QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
Qt::ItemFlags flags(const QModelIndex &index) const override;
bool isPreferred(quint32 row) const;
void removeLines(quint32 index, quint32 count);
void setEmails(const std::deque<Shared::VCard::Email>& emails);
void getEmails(std::deque<Shared::VCard::Email>& emails) const;
QString getEmail(quint32 row) const;
public slots:
QModelIndex addNewEmptyLine();
void revertPreferred(quint32 row);
private:
bool edit;
std::deque<Shared::VCard::Email> deque;
private:
bool dropPrefered();
};
}
#endif // MODELS_EMAILS_H

View file

@ -0,0 +1,4 @@
target_sources(squawk PRIVATE
keys.cpp
keys.h
)

View file

@ -0,0 +1,138 @@
// 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"
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::deque<Shared::KeyInfo> Models::Keys::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 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 {
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});
}

View file

@ -0,0 +1,67 @@
// 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/>.
#ifndef MODELS_KEYS_H
#define MODELS_KEYS_H
#include <QAbstractListModel>
#include <shared/keyinfo.h>
namespace Models {
/**
* @todo write docs
*/
class Keys : public QAbstractListModel
{
public:
Keys(QObject *parent = nullptr);
~Keys();
void addKey(const Shared::KeyInfo& info);
QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const override;
int rowCount(const QModelIndex& parent = QModelIndex()) const override;
QHash<int, QByteArray> roleNames() const override;
QModelIndex index(int row, int column, const QModelIndex & parent) const override;
std::deque<Shared::KeyInfo> modifiedKeys() const;
enum Roles {
Label = Qt::UserRole + 1,
FingerPrint,
TrustLevel,
LastInteraction,
Dirty
};
public slots:
void revertKey(int index);
void setTrustLevel(int index, Shared::TrustLevel level);
private:
std::deque<Shared::KeyInfo*> keys;
std::map<int, Shared::KeyInfo*> modified;
private:
static const QHash<int, QByteArray> roles;
};
}
#endif // MODELS_KEYS_H

207
ui/models/info/phones.cpp Normal file
View file

@ -0,0 +1,207 @@
/*
* 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 "phones.h"
#include "shared/icons.h"
#include <QCoreApplication>
Models::Phones::Phones(bool p_edit, QObject* parent):
QAbstractTableModel(parent),
edit(p_edit),
deque() {}
int Models::Phones::columnCount(const QModelIndex& parent) const {
return 4;}
int Models::Phones::rowCount(const QModelIndex& parent) const {
return deque.size();}
void Models::Phones::revertPreferred(quint32 row) {
setData(createIndex(row, 3), !isPreferred(row));
}
QString Models::Phones::getPhone(quint32 row) const {
return deque[row].number;}
QVariant Models::Phones::data(const QModelIndex& index, int role) const {
if (index.isValid()) {
int col = index.column();
switch (col) {
case 0:
switch (role) {
case Qt::DisplayRole:
case Qt::EditRole:
return deque[index.row()].number;
default:
return QVariant();
}
break;
case 1:
switch (role) {
case Qt::DisplayRole:
return QCoreApplication::translate("Global", Shared::VCard::Phone::roleNames[deque[index.row()].role].toStdString().c_str());
case Qt::EditRole:
return deque[index.row()].role;
default:
return QVariant();
}
break;
case 2:
switch (role) {
case Qt::DisplayRole:
return QCoreApplication::translate("Global", Shared::VCard::Phone::typeNames[deque[index.row()].type].toStdString().c_str());
case Qt::EditRole:
return deque[index.row()].type;
default:
return QVariant();
}
break;
case 3:
switch (role) {
case Qt::DisplayRole:
return QVariant();
case Qt::DecorationRole:
if (deque[index.row()].prefered) {
return Shared::icon("favorite", false);
}
return QVariant();
default:
return QVariant();
}
break;
default:
return QVariant();
}
}
return QVariant();
}
QModelIndex Models::Phones::addNewEmptyLine() {
beginInsertRows(QModelIndex(), deque.size(), deque.size());
deque.emplace_back("", Shared::VCard::Phone::other);
endInsertRows();
return createIndex(deque.size() - 1, 0, &(deque.back()));
}
Qt::ItemFlags Models::Phones::flags(const QModelIndex& index) const {
Qt::ItemFlags f = QAbstractTableModel::flags(index);
if (edit && index.column() != 3) {
f = Qt::ItemIsEditable | f;
}
return f;
}
bool Models::Phones::dropPrefered() {
bool dropped = false;
int i = 0;
for (Shared::VCard::Phone& phone : deque) {
if (phone.prefered) {
phone.prefered = false;
QModelIndex ci = createIndex(i, 2, &phone);
emit dataChanged(ci, ci);
dropped = true;
}
++i;
}
return dropped;
}
void Models::Phones::getPhones(std::deque<Shared::VCard::Phone>& phones) const {
for (const Shared::VCard::Phone& my : deque) {
phones.emplace_back(my);
}
}
bool Models::Phones::isPreferred(quint32 row) const {
if (row < deque.size()) {
return deque[row].prefered;
} else {
return false;
}
}
void Models::Phones::removeLines(quint32 index, quint32 count) {
if (index < deque.size()) {
quint32 maxCount = deque.size() - index;
if (count > maxCount) {
count = maxCount;
}
if (count > 0) {
beginRemoveRows(QModelIndex(), index, index + count - 1);
std::deque<Shared::VCard::Phone>::const_iterator itr = deque.begin() + index;
std::deque<Shared::VCard::Phone>::const_iterator end = itr + count;
deque.erase(itr, end);
endRemoveRows();
}
}
}
bool Models::Phones::setData(const QModelIndex& index, const QVariant& value, int role) {
if (role == Qt::EditRole && checkIndex(index)) {
Shared::VCard::Phone& item = deque[index.row()];
switch (index.column()) {
case 0:
item.number = value.toString();
return true;
case 1: {
quint8 newRole = value.toUInt();
if (newRole > Shared::VCard::Phone::work) {
return false;
}
item.role = static_cast<Shared::VCard::Phone::Role>(newRole);
return true;
}
case 2: {
quint8 newType = value.toUInt();
if (newType > Shared::VCard::Phone::other) {
return false;
}
item.type = static_cast<Shared::VCard::Phone::Type>(newType);
return true;
}
case 3: {
bool newDef = value.toBool();
if (newDef != item.prefered) {
if (newDef) {
//dropPrefered();
}
item.prefered = newDef;
return true;
}
}
}
return true;
}
return false;
}
void Models::Phones::setPhones(const std::deque<Shared::VCard::Phone>& phones) {
if (deque.size() > 0) {
removeLines(0, deque.size());
}
if (phones.size() > 0) {
beginInsertRows(QModelIndex(), 0, phones.size() - 1);
for (const Shared::VCard::Phone& comming : phones) {
deque.emplace_back(comming);
}
endInsertRows();
}
}

59
ui/models/info/phones.h Normal file
View file

@ -0,0 +1,59 @@
/*
* 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/>.
*/
#ifndef MODELS_PHONES_H
#define MODELS_PHONES_H
#include <QAbstractTableModel>
#include <QIcon>
#include "shared/vcard.h"
namespace Models {
class Phones : public QAbstractTableModel {
Q_OBJECT
public:
Phones(bool edit = false, QObject *parent = nullptr);
QVariant data(const QModelIndex& index, int role) const override;
int columnCount(const QModelIndex& parent) const override;
int rowCount(const QModelIndex& parent) const override;
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
Qt::ItemFlags flags(const QModelIndex &index) const override;
bool isPreferred(quint32 row) const;
void removeLines(quint32 index, quint32 count);
void setPhones(const std::deque<Shared::VCard::Phone>& phones);
void getPhones(std::deque<Shared::VCard::Phone>& phones) const;
QString getPhone(quint32 row) const;
public slots:
QModelIndex addNewEmptyLine();
void revertPreferred(quint32 row);
private:
bool edit;
std::deque<Shared::VCard::Phone> deque;
private:
bool dropPrefered();
};
}
#endif // MODELS_PHONES_H