Warning, file /plasma-mobile/spacebar/lib/telephonySupport/phonenumber.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001 // SPDX-FileCopyrightText: 2021 Jonah BrĂ¼chert <jbb@kaidan.im> 0002 // 0003 // SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0004 0005 #include "phonenumber.h" 0006 0007 #include <phonenumbers/phonenumberutil.h> 0008 0009 using namespace i18n; 0010 0011 struct PhoneNumberPrivate : QSharedData { 0012 enum Representation { 0013 Parsed, 0014 String, 0015 }; 0016 0017 i18n::phonenumbers::PhoneNumber number; 0018 QString numberString; 0019 Representation representation; 0020 }; 0021 0022 std::string PhoneNumber::countryCode; 0023 0024 PhoneNumber::PhoneNumber() 0025 : d(new PhoneNumberPrivate()) 0026 { 0027 } 0028 PhoneNumber::~PhoneNumber() = default; 0029 PhoneNumber::PhoneNumber(const PhoneNumber &other) = default; 0030 PhoneNumber &PhoneNumber::operator=(const PhoneNumber &other) = default; 0031 0032 PhoneNumber::PhoneNumber(const QString &number) 0033 : PhoneNumber() 0034 { 0035 auto error = phonenumbers::PhoneNumberUtil::GetInstance()->Parse(number.toStdString(), countryCode, &d->number); 0036 if (error == phonenumbers::PhoneNumberUtil::ErrorType::NO_PARSING_ERROR) { 0037 d->representation = PhoneNumberPrivate::Parsed; 0038 } else { 0039 d->numberString = number; 0040 d->representation = PhoneNumberPrivate::String; 0041 } 0042 } 0043 0044 bool PhoneNumber::operator==(const PhoneNumber &other) const 0045 { 0046 if (!isValid()) { 0047 return false; 0048 } 0049 if (d->representation == PhoneNumberPrivate::Parsed && other.d->representation == PhoneNumberPrivate::Parsed) { 0050 return phonenumbers::PhoneNumberUtil::GetInstance()->IsNumberMatch(d->number, other.d->number) == phonenumbers::PhoneNumberUtil::EXACT_MATCH; 0051 } else if (d->representation == PhoneNumberPrivate::String && other.d->representation == PhoneNumberPrivate::String) { 0052 return d->numberString == other.d->numberString; 0053 } 0054 0055 // The same input cannot be successfully parsed one time and the other time not, so assume false here 0056 return false; 0057 } 0058 0059 QString PhoneNumber::toInternational() const 0060 { 0061 if (d->representation == PhoneNumberPrivate::Parsed) { 0062 std::string formattedNumber; 0063 phonenumbers::PhoneNumberUtil::GetInstance()->Format(d->number, phonenumbers::PhoneNumberUtil::PhoneNumberFormat::INTERNATIONAL, &formattedNumber); 0064 return QString::fromStdString(formattedNumber); 0065 } else { 0066 return d->numberString; 0067 } 0068 } 0069 0070 QString PhoneNumber::toNational() const 0071 { 0072 if (d->representation == PhoneNumberPrivate::Parsed) { 0073 std::string formattedNumber; 0074 phonenumbers::PhoneNumberUtil::GetInstance()->Format(d->number, phonenumbers::PhoneNumberUtil::PhoneNumberFormat::NATIONAL, &formattedNumber); 0075 return QString::fromStdString(formattedNumber); 0076 } else { 0077 return d->numberString; 0078 } 0079 } 0080 0081 QString PhoneNumber::toE164() const 0082 { 0083 if (d->representation == PhoneNumberPrivate::Parsed) { 0084 std::string formattedNumber; 0085 phonenumbers::PhoneNumberUtil::GetInstance()->Format(d->number, phonenumbers::PhoneNumberUtil::PhoneNumberFormat::E164, &formattedNumber); 0086 return QString::fromStdString(formattedNumber); 0087 } else { 0088 return d->numberString; 0089 } 0090 } 0091 0092 bool PhoneNumber::isValid() const 0093 { 0094 if (d->representation == PhoneNumberPrivate::Parsed) { 0095 return d->number.IsInitialized(); 0096 } else { 0097 return !d->numberString.isEmpty(); 0098 } 0099 } 0100 0101 void PhoneNumber::setCountryCode(const QString &code) 0102 { 0103 countryCode = code.toStdString(); 0104 }