qofonovoicecallmanager.cpp 10.3 KB
Newer Older
Jenkins's avatar
Jenkins committed
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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
/***************************************************`*************************
**
** Copyright (C) 2013-2016 Jolla Ltd.
** Contact: lorn.potter@jollamobile.com
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
****************************************************************************/

#include "qofonovoicecallmanager.h"
#include "qofonoutils_p.h"
#include "ofono_voicecallmanager_interface.h"

#define SUPER QOfonoModemInterface

class QOfonoVoiceCallManager::Private : public SUPER::ExtData
{
public:
    bool initialized;
    QStringList callList;
    QString errorMessage;

    Private() : initialized(false) {}

    static void getCalls(QOfonoVoiceCallManager *parent, OfonoVoiceCallManager* iface) {
        connect(new QDBusPendingCallWatcher(iface->GetCalls(), iface),
            SIGNAL(finished(QDBusPendingCallWatcher*)), parent,
            SLOT(onGetCallsFinished(QDBusPendingCallWatcher*)));
    }

    class Watcher : public QDBusPendingCallWatcher {
    public:
        typedef void (QOfonoVoiceCallManager::*Signal)(bool);
        const char* name;
        Signal signal;
        Watcher(QOfonoVoiceCallManager* target, OfonoVoiceCallManager *parent,
            const char* callName, const QDBusPendingCall &call, Signal complete) :
            QDBusPendingCallWatcher(call, parent),
            name(callName), signal(complete)
        {
            connect(this, SIGNAL(finished(QDBusPendingCallWatcher*)),
                target, SLOT(onVoidCallFinished(QDBusPendingCallWatcher*)));
        }
    };

    class ObjectPathListWatcher : public QDBusPendingCallWatcher {
    public:
        typedef void (QOfonoVoiceCallManager::*Signal)(bool, const QStringList&);
        const char* name;
        Signal signal;
        ObjectPathListWatcher(QOfonoVoiceCallManager* target, OfonoVoiceCallManager *parent,
            const char* callName, const QDBusPendingCall &call, Signal complete) :
            QDBusPendingCallWatcher(call, parent),
            name(callName), signal(complete)
        {
            connect(this, SIGNAL(finished(QDBusPendingCallWatcher*)),
                target, SLOT(onObjectPathListCallFinished(QDBusPendingCallWatcher*)));
        }
    };
};

QOfonoVoiceCallManager::QOfonoVoiceCallManager(QObject *parent) :
    SUPER(OfonoVoiceCallManager::staticInterfaceName(), new Private, parent)
{
    QOfonoDbusTypes::registerObjectPathProperties();
}

QOfonoVoiceCallManager::~QOfonoVoiceCallManager()
{
}

bool QOfonoVoiceCallManager::isValid() const
{
    return privateData()->initialized && SUPER::isValid();
}

QDBusAbstractInterface *QOfonoVoiceCallManager::createDbusInterface(const QString &path)
{
    OfonoVoiceCallManager* iface = new OfonoVoiceCallManager("org.ofono", path, QDBusConnection::systemBus(), this);
    connect(iface,
        SIGNAL(CallAdded(QDBusObjectPath,QVariantMap)),
        SLOT(onCallAdded(QDBusObjectPath,QVariantMap)));
    connect(iface,
        SIGNAL(CallRemoved(QDBusObjectPath)),
        SLOT(onCallRemoved(QDBusObjectPath)));
    connect(iface, SIGNAL(BarringActive(QString)), SIGNAL(barringActive(QString)));
    connect(iface, SIGNAL(Forwarded(QString)), SIGNAL(forwarded(QString)));
    Private::getCalls(this, iface);
    return iface;
}

void QOfonoVoiceCallManager::dbusInterfaceDropped()
{
    SUPER::dbusInterfaceDropped();

    Private *d_ptr = privateData();
    d_ptr->initialized = false;
    if (!d_ptr->callList.isEmpty()) {
        QStringList list = d_ptr->callList;
        d_ptr->callList.clear();
        for (int i=0; i<list.count(); i++) {
            Q_EMIT callRemoved(list[i]);
        }
    }
}

void QOfonoVoiceCallManager::propertyChanged(const QString &property, const QVariant &value)
{
    SUPER::propertyChanged(property, value);
    if (property == QLatin1String("EmergencyNumbers")) {
        Q_EMIT emergencyNumbersChanged(value.toStringList());
    }
}

QStringList QOfonoVoiceCallManager::emergencyNumbers() const
{
    return getStringList("EmergencyNumbers");
}

QStringList QOfonoVoiceCallManager::getCalls() const
{
    return privateData()->callList;
}

QString QOfonoVoiceCallManager::errorMessage() const
{
    return privateData()->errorMessage;
}

void QOfonoVoiceCallManager::dial(const QString &number, const QString &calleridHide)
{
    OfonoVoiceCallManager *iface = (OfonoVoiceCallManager*)dbusInterface();
    if (iface) {
        new Private::Watcher(this, iface, "Dial",
            iface->Dial(number, calleridHide), &QOfonoVoiceCallManager::dialComplete);
    }
}

void QOfonoVoiceCallManager::hangupAll()
{
    OfonoVoiceCallManager *iface = (OfonoVoiceCallManager*)dbusInterface();
    if (iface) {
         new Private::Watcher(this, iface, "HangupAll",
            iface->HangupAll(), &QOfonoVoiceCallManager::hangupAllComplete);
    }
}

void QOfonoVoiceCallManager::sendTones(const QString &tonestring)
{
    OfonoVoiceCallManager *iface = (OfonoVoiceCallManager*)dbusInterface();
    if (iface) {
        new Private::Watcher(this, iface, "SendTones",
            iface->SendTones(tonestring), &QOfonoVoiceCallManager::sendTonesComplete);
    }
}

void QOfonoVoiceCallManager::transfer()
{
    OfonoVoiceCallManager *iface = (OfonoVoiceCallManager*)dbusInterface();
    if (iface) {
        new Private::Watcher(this, iface, "Transfer",
            iface->Transfer(), &QOfonoVoiceCallManager::transferComplete);
    }
}

void QOfonoVoiceCallManager::swapCalls()
{
    OfonoVoiceCallManager *iface = (OfonoVoiceCallManager*)dbusInterface();
    if (iface) {
        new Private::Watcher(this, iface, "SwapCalls",
            iface->SwapCalls(), &QOfonoVoiceCallManager::swapCallsComplete);
    }
}

void QOfonoVoiceCallManager::releaseAndAnswer()
{
    OfonoVoiceCallManager *iface = (OfonoVoiceCallManager*)dbusInterface();
    if (iface) {
        new Private::Watcher(this, iface, "ReleaseAndAnswer",
            iface->ReleaseAndAnswer(), &QOfonoVoiceCallManager::releaseAndAnswerComplete);
    }
}

void QOfonoVoiceCallManager::holdAndAnswer()
{
    OfonoVoiceCallManager *iface = (OfonoVoiceCallManager*)dbusInterface();
    if (iface) {
        new Private::Watcher(this, iface, "HoldAndAnswer",
            iface->HoldAndAnswer(), &QOfonoVoiceCallManager::holdAndAnswerComplete);
    }
}

void QOfonoVoiceCallManager::createMultiparty()
{
    OfonoVoiceCallManager *iface = (OfonoVoiceCallManager*)dbusInterface();
    if (iface) {
        new Private::ObjectPathListWatcher(this, iface, "CreateMultiparty",
            iface->CreateMultiparty(), &QOfonoVoiceCallManager::createMultipartyComplete);
    }
}

void QOfonoVoiceCallManager::hangupMultiparty()
{
    OfonoVoiceCallManager *iface = (OfonoVoiceCallManager*)dbusInterface();
    if (iface) {
        new Private::Watcher(this, iface, "HangupMultiparty",
            iface->HangupMultiparty(), &QOfonoVoiceCallManager::hangupMultipartyComplete);
    }
}

void QOfonoVoiceCallManager::privateChat(const QString &path)
{
    OfonoVoiceCallManager *iface = (OfonoVoiceCallManager*)dbusInterface();
    if (iface) {
        new Private::ObjectPathListWatcher(this, iface, "PrivateChat",
            iface->PrivateChat(QDBusObjectPath(path)), &QOfonoVoiceCallManager::privateChatComplete);
    }
}

void QOfonoVoiceCallManager::onVoidCallFinished(QDBusPendingCallWatcher *watch)
{
    watch->deleteLater();
    Private::Watcher* call = (Private::Watcher*)watch;
    QDBusPendingReply<> reply(*watch);
    bool ok = true;
    if (reply.isError()) {
        Private *d_ptr = privateData();
        d_ptr->errorMessage = QString("%1: %2 %3").arg(call->name).
            arg(reply.error().name()).arg(reply.error().message());
        qWarning() << d_ptr->errorMessage;
        ok = false;
    }
    (this->*call->signal)(ok);
}

void QOfonoVoiceCallManager::onObjectPathListCallFinished(QDBusPendingCallWatcher *watch)
{
    watch->deleteLater();
    QDBusPendingReply<ObjectPathList> reply(*watch);
    Private::ObjectPathListWatcher* call = (Private::ObjectPathListWatcher*)watch;
    QStringList list;
    bool ok = true;
    if (reply.isError()) {
        Private *d_ptr = privateData();
        d_ptr->errorMessage = QString("%1: %2 %3").arg(call->name).
            arg(reply.error().name()).arg(reply.error().message());
        qWarning() << d_ptr->errorMessage;
        ok = false;
    } else {
        Q_FOREACH(const QDBusObjectPath &path, reply.value()) {
            list << path.path();
        }
    }
    (this->*call->signal)(ok, list);
}

void QOfonoVoiceCallManager::addCall(const QString &callPath)
{
    Private *d_ptr = privateData();
    if (!d_ptr->callList.contains(callPath)) {
        d_ptr->callList.append(callPath);
        Q_EMIT callAdded(callPath);
    }
}

void QOfonoVoiceCallManager::onGetCallsFinished(QDBusPendingCallWatcher *watch)
{
    watch->deleteLater();
    QDBusPendingReply<ObjectPathPropertiesList> reply(*watch);
    if (reply.isError()) {
        if (qofono::isTimeout(reply.error())) {
            // Retry GetCalls call if it times out
            qDebug() << "Retrying GetCalls...";
            Private::getCalls(this, (OfonoVoiceCallManager*)dbusInterface());
        } else {
            qDebug() << reply.error();
            Q_EMIT reportError(reply.error().message());
        }
    } else {
        ValidTracker valid(this);
        ObjectPathPropertiesList list = reply.value();
        privateData()->initialized = true;
        for (int i=0; i<list.count(); i++) {
            addCall(list[i].path.path());
        }
    }
}

void QOfonoVoiceCallManager::onCallRemoved(const QDBusObjectPath &path)
{
    QString callPath = path.path();
    if (privateData()->callList.removeOne(callPath)) {
        Q_EMIT callRemoved(callPath);
    }
}

void QOfonoVoiceCallManager::onCallAdded(const QDBusObjectPath &path, const QVariantMap &)
{
    addCall(path.path());
}

QString QOfonoVoiceCallManager::modemPath() const
{
    return SUPER::modemPath();
}

void QOfonoVoiceCallManager::setModemPath(const QString &path)
{
    SUPER::setModemPath(path);
}

QOfonoVoiceCallManager::Private *QOfonoVoiceCallManager::privateData() const
{
    return (QOfonoVoiceCallManager::Private*)SUPER::extData();
}