File indexing completed on 2024-11-24 04:50:42
0001 // SPDX-FileCopyrightText: 2022 Carl Schwan <carl@carlschwan.eu> 0002 // SPDX-License-Identifier: LGPL-2.1-or-later 0003 0004 #include "emailmodel.h" 0005 #include <KLocalizedString> 0006 0007 EmailModel::EmailModel(QObject *parent) 0008 : QAbstractListModel(parent) 0009 { 0010 } 0011 0012 void EmailModel::loadContact(const KContacts::Addressee &contact) 0013 { 0014 beginResetModel(); 0015 m_emails = contact.emailList(); 0016 endResetModel(); 0017 } 0018 0019 void EmailModel::storeContact(KContacts::Addressee &contact) const 0020 { 0021 contact.setEmailList(m_emails); 0022 } 0023 0024 int EmailModel::rowCount(const QModelIndex &parent) const 0025 { 0026 Q_UNUSED(parent); 0027 return m_emails.count(); 0028 } 0029 0030 QVariant EmailModel::data(const QModelIndex &idx, int role) const 0031 { 0032 const auto email = m_emails[idx.row()]; 0033 switch (role) { 0034 case Qt::DisplayRole: 0035 case EmailRole: 0036 return email.mail(); 0037 case TypeRole: 0038 if (email.type() & KContacts::Email::Home & KContacts::Email::Work) { 0039 return i18n("Both:"); 0040 } 0041 if (email.type() & KContacts::Email::Work) { 0042 return i18n("Work:"); 0043 } 0044 if (email.type() & KContacts::Email::Home) { 0045 return i18n("Home:"); 0046 } 0047 return i18n("Other:"); 0048 case TypeValueRole: 0049 return (int)email.type(); 0050 case DefaultRole: 0051 return email.isPreferred(); 0052 } 0053 0054 return {}; 0055 } 0056 0057 bool EmailModel::setData(const QModelIndex &index, const QVariant &value, int role) 0058 { 0059 auto email = m_emails[index.row()]; 0060 switch (role) { 0061 case Qt::DisplayRole: 0062 case EmailRole: 0063 email.setEmail(value.toString()); 0064 m_emails.replace(index.row(), email); 0065 Q_EMIT changed(m_emails); 0066 return true; 0067 case TypeRole: 0068 case TypeValueRole: 0069 email.setType((KContacts::Email::Type)value.toInt()); 0070 m_emails.replace(index.row(), email); 0071 Q_EMIT changed(m_emails); 0072 return true; 0073 case DefaultRole: 0074 email.setPreferred(value.toBool()); 0075 m_emails.replace(index.row(), email); 0076 Q_EMIT changed(m_emails); 0077 return true; 0078 } 0079 return false; 0080 } 0081 0082 QHash<int, QByteArray> EmailModel::roleNames() const 0083 { 0084 return { 0085 {Qt::DisplayRole, QByteArrayLiteral("display")}, 0086 {EmailRole, QByteArrayLiteral("email")}, 0087 {TypeRole, QByteArrayLiteral("type")}, 0088 {TypeValueRole, QByteArrayLiteral("typeValue")}, 0089 {DefaultRole, QByteArrayLiteral("default")}, 0090 }; 0091 } 0092 0093 void EmailModel::addEmail(const QString &email, Type type) 0094 { 0095 beginInsertRows({}, m_emails.count(), m_emails.count()); 0096 KContacts::Email emailObject(email); 0097 emailObject.setType((KContacts::Email::Type)type); 0098 m_emails.append(emailObject); 0099 endInsertRows(); 0100 Q_EMIT changed(m_emails); 0101 } 0102 0103 void EmailModel::deleteEmail(int row) 0104 { 0105 if (!hasIndex(row, 0)) { 0106 return; 0107 } 0108 beginRemoveRows({}, row, row); 0109 m_emails.removeAt(row); 0110 endRemoveRows(); 0111 Q_EMIT changed(m_emails); 0112 } 0113 0114 #include "moc_emailmodel.cpp"