File indexing completed on 2024-04-28 16:42:53

0001 // SPDX-FileCopyrightText: 2015 Marco Martin <mart@kde.org>
0002 // SPDX-FileCopyrightText: 2021 Alexey Andreyev <aa13q@ya.ru>
0003 //
0004 // SPDX-License-Identifier: LGPL-2.1-or-later
0005 
0006 #include "call-utils.h"
0007 
0008 #include <QDebug>
0009 
0010 #include "phonenumbers/asyoutypeformatter.h"
0011 #include "phonenumbers/phonenumberutil.h"
0012 
0013 CallUtils::CallUtils(QObject *parent)
0014     : QObject(parent)
0015 {
0016 }
0017 
0018 CallUtils::~CallUtils() = default;
0019 
0020 void CallUtils::dial(const QString &deviceUni, const QString &number)
0021 {
0022     qDebug() << "Starting a call " << number << " via " << deviceUni;
0023     Q_EMIT dialed(deviceUni, number);
0024 }
0025 
0026 QString CallUtils::formatNumber(const QString &number)
0027 {
0028     using namespace ::i18n::phonenumbers;
0029 
0030     // Get formatter instance
0031     QLocale locale;
0032     QStringList qcountry = locale.name().split(QStringLiteral("_"));
0033     const QString &countrycode(qcountry.constLast());
0034     string country = countrycode.toUtf8().constData();
0035     PhoneNumberUtil *util = PhoneNumberUtil::GetInstance();
0036     AsYouTypeFormatter *formatter = util->PhoneNumberUtil::GetAsYouTypeFormatter(country);
0037 
0038     // Normalize input
0039     string stdnumber = number.toUtf8().constData();
0040     util->NormalizeDiallableCharsOnly(&stdnumber);
0041 
0042     // Format
0043     string formatted;
0044     formatter->Clear();
0045     for (char &c : stdnumber) {
0046         formatter->InputDigit(c, &formatted);
0047     }
0048     delete formatter;
0049 
0050     return QString::fromStdString(formatted);
0051 }
0052 
0053 void CallUtils::accept(const QString &deviceUni, const QString &callUni)
0054 {
0055     Q_EMIT accepted(deviceUni, callUni);
0056 }
0057 
0058 void CallUtils::hangUp(const QString &deviceUni, const QString &callUni)
0059 {
0060     Q_EMIT hungUp(deviceUni, callUni);
0061 }
0062 
0063 void CallUtils::sendDtmf(const QString &deviceUni, const QString &callUni, const QString &tones)
0064 {
0065     Q_EMIT sentDtmf(deviceUni, callUni, tones);
0066 }
0067 
0068 void CallUtils::fetchCalls()
0069 {
0070     Q_EMIT callsRequested();
0071 }
0072 
0073 void CallUtils::setCalls(const DialerTypes::CallDataVector &calls)
0074 {
0075     Q_EMIT callsChanged(calls);
0076 }
0077 
0078 void CallUtils::addCall(const QString &deviceUni,
0079                         const QString &callUni,
0080                         const DialerTypes::CallDirection &callDirection,
0081                         const DialerTypes::CallState &callState,
0082                         const DialerTypes::CallStateReason &callStateReason,
0083                         const QString communicationWith)
0084 {
0085     Q_EMIT callAdded(deviceUni, callUni, callDirection, callState, callStateReason, communicationWith);
0086 }
0087 
0088 void CallUtils::deleteCall(const QString &deviceUni, const QString &callUni)
0089 {
0090     Q_EMIT callDeleted(deviceUni, callUni);
0091 }
0092 
0093 void CallUtils::setCallState(const DialerTypes::CallData &callData)
0094 {
0095     callStateChanged(callData);
0096 }