File: phonenumber.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 (109 lines) | stat: -rw-r--r-- 3,434 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// SPDX-FileCopyrightText: 2021 Jonah BrĂ¼chert <jbb@kaidan.im>
//
// SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL

#include "phonenumber.h"

#include <phonenumbers/phonenumberutil.h>

using namespace i18n;

struct PhoneNumberPrivate : QSharedData {
    enum Representation {
        Parsed,
        String,
    };

    i18n::phonenumbers::PhoneNumber number;
    QString numberString;
    Representation representation;
};

std::string PhoneNumber::countryCode;

PhoneNumber::PhoneNumber()
    : d(new PhoneNumberPrivate())
{
}
PhoneNumber::~PhoneNumber() = default;
PhoneNumber::PhoneNumber(const PhoneNumber &other) = default;
PhoneNumber &PhoneNumber::operator=(const PhoneNumber &other) = default;

PhoneNumber::PhoneNumber(const QString &number)
    : PhoneNumber()
{
    auto error = phonenumbers::PhoneNumberUtil::GetInstance()->Parse(number.toStdString(), countryCode, &d->number);
    if (error == phonenumbers::PhoneNumberUtil::ErrorType::NO_PARSING_ERROR) {
        d->representation = PhoneNumberPrivate::Parsed;
    } else {
        d->representation = PhoneNumberPrivate::String;
    }
    d->numberString = number;
}

bool PhoneNumber::operator==(const PhoneNumber &other) const
{
    if (!isValid()) {
        return false;
    }
    if (d->representation == PhoneNumberPrivate::Parsed && other.d->representation == PhoneNumberPrivate::Parsed) {
        return phonenumbers::PhoneNumberUtil::GetInstance()->IsNumberMatch(d->number, other.d->number) == phonenumbers::PhoneNumberUtil::EXACT_MATCH;
    } else if (d->representation == PhoneNumberPrivate::String && other.d->representation == PhoneNumberPrivate::String) {
        return d->numberString == other.d->numberString;
    }

    // The same input cannot be successfully parsed one time and the other time not, so assume false here
    return false;
}

QString PhoneNumber::toInternational() const
{
    if (d->representation == PhoneNumberPrivate::Parsed) {
        std::string formattedNumber;
        phonenumbers::PhoneNumberUtil::GetInstance()->Format(d->number, phonenumbers::PhoneNumberUtil::PhoneNumberFormat::INTERNATIONAL, &formattedNumber);
        return QString::fromStdString(formattedNumber);
    } else {
        return d->numberString;
    }
}

QString PhoneNumber::toNational() const
{
    if (d->representation == PhoneNumberPrivate::Parsed) {
        std::string formattedNumber;
        phonenumbers::PhoneNumberUtil::GetInstance()->Format(d->number, phonenumbers::PhoneNumberUtil::PhoneNumberFormat::NATIONAL, &formattedNumber);
        return QString::fromStdString(formattedNumber);
    } else {
        return d->numberString;
    }
}

QString PhoneNumber::toE164() const
{
    if (d->representation == PhoneNumberPrivate::Parsed) {
        std::string formattedNumber;
        phonenumbers::PhoneNumberUtil::GetInstance()->Format(d->number, phonenumbers::PhoneNumberUtil::PhoneNumberFormat::E164, &formattedNumber);
        return QString::fromStdString(formattedNumber);
    } else {
        return d->numberString;
    }
}

bool PhoneNumber::isValid() const
{
    if (d->representation == PhoneNumberPrivate::Parsed) {
        return d->number.IsInitialized();
    } else {
        return !d->numberString.isEmpty();
    }
}

void PhoneNumber::setCountryCode(const QString &code)
{
    countryCode = code.toStdString();
}

QString PhoneNumber::toString() const
{
    return d->numberString;
}