2015-04-26 13:50:59 +00:00
|
|
|
/*
|
|
|
|
|
* Copyright 2015 Marco Martin <mart@kde.org>
|
|
|
|
|
*
|
2018-11-25 12:23:25 +00:00
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
|
|
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
|
**/
|
2015-04-26 13:50:59 +00:00
|
|
|
|
|
|
|
|
#include "dialerutils.h"
|
|
|
|
|
|
|
|
|
|
#include <QDebug>
|
|
|
|
|
|
2015-06-18 23:24:46 +00:00
|
|
|
#include <TelepathyQt/PendingOperation>
|
|
|
|
|
#include <TelepathyQt/PendingChannelRequest>
|
|
|
|
|
#include <TelepathyQt/PendingReady>
|
|
|
|
|
#include <TelepathyQt/Constants>
|
|
|
|
|
#include <TelepathyQt/PendingContacts>
|
|
|
|
|
#include <TelepathyQt/Types>
|
|
|
|
|
#include <TelepathyQt/ContactManager>
|
2015-04-26 14:42:14 +00:00
|
|
|
|
2018-07-13 21:33:28 +00:00
|
|
|
#include "phonenumbers/phonenumberutil.h"
|
|
|
|
|
#include "phonenumbers/asyoutypeformatter.h"
|
|
|
|
|
|
2015-06-18 23:24:46 +00:00
|
|
|
DialerUtils::DialerUtils(const Tp::AccountPtr &simAccount, QObject *parent)
|
2015-04-26 14:42:14 +00:00
|
|
|
: QObject(parent),
|
2015-06-18 23:24:46 +00:00
|
|
|
m_missedCalls(0),
|
2015-06-29 16:16:10 +00:00
|
|
|
m_simAccount(simAccount),
|
|
|
|
|
m_callDuration(0),
|
2015-06-29 17:53:49 +00:00
|
|
|
m_callContactAlias(QString())
|
2015-04-26 13:50:59 +00:00
|
|
|
{
|
2015-06-18 23:24:46 +00:00
|
|
|
Tp::PendingReady *op = m_simAccount->becomeReady(Tp::Features() << Tp::Account::FeatureCore);
|
|
|
|
|
|
|
|
|
|
connect(op, &Tp::PendingOperation::finished, [=](){
|
|
|
|
|
if (op->isError()) {
|
|
|
|
|
qWarning() << "SIM card account failed to get ready:" << op->errorMessage();
|
|
|
|
|
} else {
|
|
|
|
|
qDebug() << "SIM Account ready to use";
|
|
|
|
|
}
|
|
|
|
|
});
|
2015-04-26 13:50:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DialerUtils::~DialerUtils()
|
2018-12-31 03:13:55 +00:00
|
|
|
= default;
|
2015-04-26 13:50:59 +00:00
|
|
|
|
2015-06-18 23:24:46 +00:00
|
|
|
void DialerUtils::dial(const QString &number)
|
|
|
|
|
{
|
|
|
|
|
// FIXME: this should be replaced by kpeople thing
|
|
|
|
|
auto pendingContact = m_simAccount->connection()->contactManager()->contactsForIdentifiers(QStringList() << number);
|
|
|
|
|
|
|
|
|
|
connect(pendingContact, &Tp::PendingOperation::finished, [=](){
|
|
|
|
|
if (pendingContact->contacts().size() < 1) {
|
|
|
|
|
qWarning() << " no contacts";
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
qDebug() << "Starting call...";
|
|
|
|
|
Tp::PendingChannelRequest *pendingChannel = m_simAccount->ensureAudioCall(pendingContact->contacts().first());
|
|
|
|
|
connect(pendingChannel, &Tp::PendingChannelRequest::finished, [=](){
|
|
|
|
|
if (pendingChannel->isError()) {
|
|
|
|
|
qWarning() << "Error when requesting channel" << pendingChannel->errorMessage();
|
2015-06-29 16:18:27 +00:00
|
|
|
setCallState("failed");
|
2015-06-18 23:24:46 +00:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString DialerUtils::callState() const
|
|
|
|
|
{
|
|
|
|
|
return m_callState;
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-13 21:33:28 +00:00
|
|
|
const QString DialerUtils::formatNumber(const QString& number)
|
|
|
|
|
{
|
|
|
|
|
using namespace ::i18n::phonenumbers;
|
|
|
|
|
|
|
|
|
|
// Get formatter instance
|
|
|
|
|
QLocale locale;
|
|
|
|
|
QStringList qcountry = locale.name().split('_');
|
|
|
|
|
QString countrycode(qcountry.constLast());
|
|
|
|
|
const char* country = countrycode.toUtf8().constData();
|
|
|
|
|
PhoneNumberUtil* util = PhoneNumberUtil::GetInstance();
|
|
|
|
|
AsYouTypeFormatter* formatter = util->PhoneNumberUtil::GetAsYouTypeFormatter(country);
|
|
|
|
|
|
|
|
|
|
// Normalize input
|
|
|
|
|
string stdnumber = number.toUtf8().constData();
|
|
|
|
|
util->NormalizeDiallableCharsOnly(&stdnumber);
|
|
|
|
|
|
|
|
|
|
// Format
|
|
|
|
|
string formatted;
|
|
|
|
|
formatter->Clear();
|
|
|
|
|
for (char& c : stdnumber) {
|
|
|
|
|
formatter->InputDigit(c, &formatted);
|
|
|
|
|
}
|
|
|
|
|
delete formatter;
|
|
|
|
|
|
|
|
|
|
return QString::fromStdString(formatted);
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-18 23:24:46 +00:00
|
|
|
void DialerUtils::setCallState(const QString &state)
|
|
|
|
|
{
|
|
|
|
|
if (m_callState != state) {
|
|
|
|
|
m_callState = state;
|
|
|
|
|
Q_EMIT callStateChanged();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-29 16:16:10 +00:00
|
|
|
uint DialerUtils::callDuration() const
|
|
|
|
|
{
|
|
|
|
|
return m_callDuration;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DialerUtils::setCallDuration(uint duration)
|
|
|
|
|
{
|
|
|
|
|
m_callDuration = duration;
|
|
|
|
|
Q_EMIT callDurationChanged();
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-29 17:53:49 +00:00
|
|
|
QString DialerUtils::callContactAlias() const
|
2015-06-29 16:17:19 +00:00
|
|
|
{
|
2015-06-29 17:53:49 +00:00
|
|
|
return m_callContactAlias;
|
2015-06-29 16:17:19 +00:00
|
|
|
}
|
|
|
|
|
|
2015-06-29 17:53:49 +00:00
|
|
|
void DialerUtils::setCallContactAlias(const QString &contactAlias)
|
2015-06-29 16:17:19 +00:00
|
|
|
{
|
2015-06-29 17:53:49 +00:00
|
|
|
if (m_callContactAlias != contactAlias) {
|
|
|
|
|
m_callContactAlias = contactAlias;
|
|
|
|
|
Q_EMIT callContactAliasChanged();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString DialerUtils::callContactNumber() const
|
|
|
|
|
{
|
|
|
|
|
return m_callContactNumber;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DialerUtils::setCallContactNumber(const QString &contactNumber)
|
|
|
|
|
{
|
|
|
|
|
if (m_callContactNumber != contactNumber) {
|
|
|
|
|
m_callContactNumber = contactNumber;
|
|
|
|
|
Q_EMIT callContactNumberChanged();
|
2015-06-29 16:17:19 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-29 18:11:16 +00:00
|
|
|
bool DialerUtils::isIncomingCall() const
|
|
|
|
|
{
|
|
|
|
|
return m_isIncomingCall;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DialerUtils::setIsIncomingCall(bool isIncomingCall)
|
|
|
|
|
{
|
|
|
|
|
if (m_isIncomingCall != isIncomingCall) {
|
|
|
|
|
m_isIncomingCall = isIncomingCall;
|
|
|
|
|
Q_EMIT isIncomingCallChanged();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-29 17:54:44 +00:00
|
|
|
void DialerUtils::emitCallEnded()
|
|
|
|
|
{
|
|
|
|
|
qDebug() << "Call ended:" << m_callContactNumber << m_callDuration;
|
2015-06-29 18:11:16 +00:00
|
|
|
Q_EMIT callEnded(m_callContactNumber, m_callDuration, m_isIncomingCall);
|
2015-06-29 17:54:44 +00:00
|
|
|
m_callDuration = 0;
|
|
|
|
|
m_callContactNumber = QString();
|
|
|
|
|
m_callContactAlias = QString();
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-26 14:42:14 +00:00
|
|
|
void DialerUtils::resetMissedCalls()
|
|
|
|
|
{
|
|
|
|
|
m_missedCalls = 0;
|
|
|
|
|
if (m_callsNotification) {
|
|
|
|
|
m_callsNotification->close();
|
|
|
|
|
}
|
|
|
|
|
m_callsNotification.clear();
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-26 13:50:59 +00:00
|
|
|
#include "moc_dialerutils.cpp"
|