Commit bbf06092 authored by Smitty van Bodegom's avatar Smitty van Bodegom
Browse files

Normalize numbers for Ofono

parent 5ce9e9e0
......@@ -4,6 +4,7 @@
#include "phonenumberutils.h"
#include <QChar>
#include <QLocale>
#include <phonenumbers/phonenumberutil.h>
......@@ -46,4 +47,25 @@ QString normalizeNumber(const QString &numberString, PhoneNumberFormat format)
return numberString;
}
// normalize a number so that Ofono can understand it
QString normalizeForOfono(const QString &numberString)
{
// ofono number parsing:
// https://github.com/rilmodem/ofono/blob/efc9c0a85d32706bc088e449e847be41dcc73b3d/src/common.c#L238
QString normalized;
for (int i = 0; i < numberString.size(); ++i) {
QChar c = numberString.at(i);
if (c == QChar(u'+') && normalized.isEmpty()) {
// plus is allowed but only as first character
normalized.append(u'+');
} else if ((c >= QChar(u'0') && c <= QChar(u'9')) || c == QChar(u'*') ||
c == QChar(u'#')) {
normalized.append(c);
}
// ignore all other characters
}
return normalized;
}
}
......@@ -31,4 +31,5 @@ using NormalizeResult = std::variant<std::string, ErrorType>;
NormalizeResult normalizeNumber(const std::string &numberString, PhoneNumberFormat format = International);
QString normalizeNumber(const QString &numberString, PhoneNumberFormat format = International);
QString normalizeForOfono(const QString &numberString);
};
......@@ -7,6 +7,7 @@
#include <QFutureInterface>
#include <global.h>
#include <phonenumberutils.h>
MessageManager::MessageManager(QObject *parent)
: QOfonoMessageManager(parent)
......@@ -25,20 +26,24 @@ QFuture<SendMessageResult> MessageManager::sendMessage(const QString &to, const
return futureInterface->future();
}
connect(new QDBusPendingCallWatcher(iface->asyncCall(SL("SendMessage"), to, text), iface),
&QDBusPendingCallWatcher::finished,
[=](QDBusPendingCallWatcher *watcher) {
watcher->deleteLater();
QDBusPendingReply<QDBusObjectPath> reply = *watcher;
if (reply.isError()) {
futureInterface->reportResult(reply.error());
futureInterface->reportFinished();
} else {
futureInterface->reportResult(reply.value());
futureInterface->reportFinished();
}
});
connect(new QDBusPendingCallWatcher(
iface->asyncCall(SL("SendMessage"),
phoneNumberUtils::normalizeForOfono(to),
text),
iface),
&QDBusPendingCallWatcher::finished,
[=](QDBusPendingCallWatcher *watcher) {
watcher->deleteLater();
QDBusPendingReply<QDBusObjectPath> reply = *watcher;
if (reply.isError()) {
futureInterface->reportResult(reply.error());
futureInterface->reportFinished();
} else {
futureInterface->reportResult(reply.value());
futureInterface->reportFinished();
}
});
return futureInterface->future();
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment