File: contactphonenumbermapper.cpp

package info (click to toggle)
spacebar 1%3A6.5.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,448 kB
  • sloc: cpp: 4,708; xml: 293; sql: 22; makefile: 3; sh: 1
file content (69 lines) | stat: -rw-r--r-- 2,297 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// SPDX-FileCopyrightText: 2020 Jonah BrĂ¼chert <jbb@kaidan.im>
//
// SPDX-License-Identifier: LicenseRef-KDE-Accepted-GPL

#include "contactphonenumbermapper.h"

#include <KPeopleBackend/AbstractContact>
#include <QDebug>
#include <QThread>

#include <phonenumber.h>

ContactPhoneNumberMapper &ContactPhoneNumberMapper::instance()
{
    static ContactPhoneNumberMapper instance;
    return instance;
}

ContactPhoneNumberMapper::ContactPhoneNumberMapper()
    : QObject()
    , m_model(new KPeople::PersonsModel(this))
{
    // data updates
    // we only care about additional data, not remove one
    connect(m_model, &QAbstractItemModel::rowsInserted, this, [this](const QModelIndex &, int first, int last) {
        processRows(first, last);
    });

    processRows(0, m_model->rowCount() - 1);
}

void ContactPhoneNumberMapper::processRows(const int first, const int last)
{
    QVector<PhoneNumber> affectedNumbers;
    for (int i = first; i <= last; i++) {
        const auto index = m_model->index(i);

        // Yes, this code has to be illogical. PersonsModel::PersonVCardRole is actually supposed
        // to return an AbstractContact::Ptr, although the name suggests differneltly. Luckily we can get
        // the actual VCard from it.
        const auto phoneNumbers = m_model->data(index, KPeople::PersonsModel::PersonVCardRole)
                                      .value<KPeople::AbstractContact::Ptr>()
                                      ->customProperty(KPeople::AbstractContact::AllPhoneNumbersProperty)
                                      .toStringList();

        const auto personUri = m_model->data(index, KPeople::PersonsModel::PersonUriRole).toString();

        for (const QString &numberString : phoneNumbers) {
            const auto phoneNum = PhoneNumber(numberString);
            if (phoneNum.isValid()) {
                m_numberToUri[phoneNum] = personUri;
                affectedNumbers.append(phoneNum);
            }
        }
    }

    Q_EMIT contactsChanged(affectedNumbers);
}

QString ContactPhoneNumberMapper::uriForNumber(const PhoneNumber &phoneNumber) const
{
    if (phoneNumber.isValid()) {
        if (m_numberToUri.contains(phoneNumber)) {
            return m_numberToUri.value(phoneNumber);
        }
    }

    return QString();
}