2019-11-02 20:50:25 +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 "emails.h"
|
2019-11-02 20:50:25 +00:00
|
|
|
|
2020-04-03 22:28:15 +00:00
|
|
|
#include "shared/icons.h"
|
|
|
|
#include <QCoreApplication>
|
|
|
|
|
2023-02-01 15:56:00 +00:00
|
|
|
Models::EMails::EMails(bool p_edit, QObject* parent):
|
2019-11-02 20:50:25 +00:00
|
|
|
QAbstractTableModel(parent),
|
|
|
|
edit(p_edit),
|
2023-02-01 15:56:00 +00:00
|
|
|
deque() {}
|
2019-11-02 20:50:25 +00:00
|
|
|
|
2023-02-01 15:56:00 +00:00
|
|
|
int Models::EMails::columnCount(const QModelIndex& parent) const {
|
|
|
|
return 3;}
|
2019-11-02 20:50:25 +00:00
|
|
|
|
2023-02-01 15:56:00 +00:00
|
|
|
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;}
|
2019-11-02 20:50:25 +00:00
|
|
|
|
2023-02-01 15:56:00 +00:00
|
|
|
QVariant Models::EMails::data(const QModelIndex& index, int role) const {
|
2019-11-02 20:50:25 +00:00
|
|
|
if (index.isValid()) {
|
|
|
|
int col = index.column();
|
|
|
|
switch (col) {
|
|
|
|
case 0:
|
|
|
|
switch (role) {
|
|
|
|
case Qt::DisplayRole:
|
2019-11-04 15:22:39 +00:00
|
|
|
case Qt::EditRole:
|
2019-11-02 20:50:25 +00:00
|
|
|
return deque[index.row()].address;
|
|
|
|
default:
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
switch (role) {
|
|
|
|
case Qt::DisplayRole:
|
2019-11-07 11:17:46 +00:00
|
|
|
return QCoreApplication::translate("Global", Shared::VCard::Email::roleNames[deque[index.row()].role].toStdString().c_str());
|
2019-11-02 20:50:25 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2023-02-01 15:56:00 +00:00
|
|
|
Qt::ItemFlags Models::EMails::flags(const QModelIndex& index) const {
|
2019-11-02 20:50:25 +00:00
|
|
|
Qt::ItemFlags f = QAbstractTableModel::flags(index);
|
|
|
|
if (edit && index.column() != 2) {
|
|
|
|
f = Qt::ItemIsEditable | f;
|
|
|
|
}
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
2023-02-01 15:56:00 +00:00
|
|
|
bool Models::EMails::setData(const QModelIndex& index, const QVariant& value, int role) {
|
2019-11-02 20:50:25 +00:00
|
|
|
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) {
|
2019-11-05 18:55:21 +00:00
|
|
|
//dropPrefered();
|
2019-11-02 20:50:25 +00:00
|
|
|
}
|
|
|
|
item.prefered = newDef;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-02-01 15:56:00 +00:00
|
|
|
bool Models::EMails::dropPrefered() {
|
2019-11-02 20:50:25 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-02-01 15:56:00 +00:00
|
|
|
QModelIndex Models::EMails::addNewEmptyLine() {
|
2019-11-02 20:50:25 +00:00
|
|
|
beginInsertRows(QModelIndex(), deque.size(), deque.size());
|
|
|
|
deque.emplace_back("");
|
|
|
|
endInsertRows();
|
|
|
|
return createIndex(deque.size() - 1, 0, &(deque.back()));
|
|
|
|
}
|
2019-11-04 15:22:39 +00:00
|
|
|
|
2023-02-01 15:56:00 +00:00
|
|
|
bool Models::EMails::isPreferred(quint32 row) const {
|
2019-11-04 15:22:39 +00:00
|
|
|
if (row < deque.size()) {
|
|
|
|
return deque[row].prefered;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-01 15:56:00 +00:00
|
|
|
void Models::EMails::removeLines(quint32 index, quint32 count) {
|
2019-11-04 15:22:39 +00:00
|
|
|
if (index < deque.size()) {
|
2019-11-17 10:24:12 +00:00
|
|
|
quint32 maxCount = deque.size() - index;
|
2019-11-04 15:22:39 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-01 15:56:00 +00:00
|
|
|
void Models::EMails::getEmails(std::deque<Shared::VCard::Email>& emails) const {
|
2019-11-04 15:22:39 +00:00
|
|
|
for (const Shared::VCard::Email& my : deque) {
|
|
|
|
emails.emplace_back(my);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-01 15:56:00 +00:00
|
|
|
void Models::EMails::setEmails(const std::deque<Shared::VCard::Email>& emails) {
|
2019-11-04 15:22:39 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|