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 "phonemodel.h" 0005 #include <KContacts/PhoneNumber> 0006 0007 PhoneModel::PhoneModel(QObject *parent) 0008 : QAbstractListModel(parent) 0009 { 0010 } 0011 0012 void PhoneModel::loadContact(const KContacts::Addressee &contact) 0013 { 0014 beginResetModel(); 0015 m_phoneNumbers = contact.phoneNumbers(); 0016 endResetModel(); 0017 } 0018 0019 void PhoneModel::storeContact(KContacts::Addressee &contact) const 0020 { 0021 contact.setPhoneNumbers(m_phoneNumbers); 0022 } 0023 0024 int PhoneModel::rowCount(const QModelIndex &parent) const 0025 { 0026 Q_UNUSED(parent); 0027 return m_phoneNumbers.count(); 0028 } 0029 0030 QVariant PhoneModel::data(const QModelIndex &idx, int role) const 0031 { 0032 const auto phone = m_phoneNumbers[idx.row()]; 0033 switch (role) { 0034 case Qt::DisplayRole: 0035 case PhoneNumberRole: 0036 return phone.number(); 0037 case TypeRole: 0038 return phone.typeLabel(); 0039 case TypeValueRole: 0040 return (int)phone.type(); 0041 case SupportSmsRole: 0042 return phone.supportsSms(); 0043 case DefaultRole: 0044 return phone.isPreferred(); 0045 } 0046 0047 return {}; 0048 } 0049 0050 bool PhoneModel::setData(const QModelIndex &index, const QVariant &value, int role) 0051 { 0052 auto phoneNumber = m_phoneNumbers[index.row()]; 0053 switch (role) { 0054 case Qt::DisplayRole: 0055 case PhoneNumberRole: 0056 phoneNumber.setNumber(value.toString()); 0057 Q_EMIT changed(m_phoneNumbers); 0058 return true; 0059 case TypeRole: 0060 case TypeValueRole: 0061 phoneNumber.setType((KContacts::PhoneNumber::Type)value.toInt()); 0062 Q_EMIT changed(m_phoneNumbers); 0063 return true; 0064 } 0065 return false; 0066 } 0067 0068 QHash<int, QByteArray> PhoneModel::roleNames() const 0069 { 0070 return { 0071 {Qt::DisplayRole, QByteArrayLiteral("display")}, 0072 {PhoneNumberRole, QByteArrayLiteral("phoneNumber")}, 0073 {TypeRole, QByteArrayLiteral("type")}, 0074 {TypeValueRole, QByteArrayLiteral("typeValue")}, 0075 {DefaultRole, QByteArrayLiteral("default")}, 0076 }; 0077 } 0078 0079 void PhoneModel::addPhoneNumber(const QString &phoneNumber, Type type) 0080 { 0081 beginInsertRows({}, m_phoneNumbers.count(), m_phoneNumbers.count()); 0082 m_phoneNumbers.append({phoneNumber, (KContacts::PhoneNumber::Type)type}); 0083 endInsertRows(); 0084 Q_EMIT changed(m_phoneNumbers); 0085 } 0086 0087 void PhoneModel::deletePhoneNumber(int row) 0088 { 0089 if (!hasIndex(row, 0)) { 0090 return; 0091 } 0092 beginRemoveRows({}, row, row); 0093 m_phoneNumbers.removeAt(row); 0094 endRemoveRows(); 0095 Q_EMIT changed(m_phoneNumbers); 0096 } 0097 0098 #include "moc_phonemodel.cpp"