File indexing completed on 2024-05-05 17:32:29
0001 // SPDX-FileCopyrightText: 2020 Jonah BrĂ¼chert <jbb@kaidan.im> 0002 // 0003 // SPDX-License-Identifier: LicenseRef-KDE-Accepted-GPL 0004 0005 #include "contactphonenumbermapper.h" 0006 0007 #include <KPeopleBackend/AbstractContact> 0008 #include <QDebug> 0009 #include <QThread> 0010 0011 #include <phonenumber.h> 0012 0013 ContactPhoneNumberMapper &ContactPhoneNumberMapper::instance() 0014 { 0015 static ContactPhoneNumberMapper instance; 0016 return instance; 0017 } 0018 0019 ContactPhoneNumberMapper::ContactPhoneNumberMapper() 0020 : QObject() 0021 , m_model(new KPeople::PersonsModel(this)) 0022 { 0023 // data updates 0024 // we only care about additional data, not remove one 0025 connect(m_model, &QAbstractItemModel::rowsInserted, this, [this](const QModelIndex &, int first, int last) { 0026 processRows(first, last); 0027 }); 0028 0029 processRows(0, m_model->rowCount() - 1); 0030 } 0031 0032 void ContactPhoneNumberMapper::processRows(const int first, const int last) 0033 { 0034 QVector<PhoneNumber> affectedNumbers; 0035 for (int i = first; i <= last; i++) { 0036 const auto index = m_model->index(i); 0037 0038 // Yes, this code has to be illogical. PersonsModel::PersonVCardRole is actually supposed 0039 // to return an AbstractContact::Ptr, although the name suggests differneltly. Luckily we can get 0040 // the actual VCard from it. 0041 const auto phoneNumbers = m_model->data(index, KPeople::PersonsModel::PersonVCardRole) 0042 .value<KPeople::AbstractContact::Ptr>() 0043 ->customProperty(KPeople::AbstractContact::AllPhoneNumbersProperty) 0044 .toStringList(); 0045 0046 const auto personUri = m_model->data(index, KPeople::PersonsModel::PersonUriRole).toString(); 0047 0048 for (const QString &numberString : phoneNumbers) { 0049 const auto phoneNum = PhoneNumber(numberString); 0050 if (phoneNum.isValid()) { 0051 m_numberToUri[phoneNum] = personUri; 0052 affectedNumbers.append(phoneNum); 0053 } 0054 } 0055 } 0056 0057 Q_EMIT contactsChanged(affectedNumbers); 0058 } 0059 0060 QString ContactPhoneNumberMapper::uriForNumber(const PhoneNumber &phoneNumber) const 0061 { 0062 if (phoneNumber.isValid()) { 0063 if (m_numberToUri.contains(phoneNumber)) { 0064 return m_numberToUri.value(phoneNumber); 0065 } 0066 } 0067 0068 return QString(); 0069 }