~ken-vandine/libqofono/0.90-suggests

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
/****************************************************************************
**
** Copyright (C) 2013-2015 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 "qofonohandsfreeaudiocard.h"
#include "ofono_handsfree_audio_card_interface.h"


class QOfonoHandsfreeAudioCardPrivate
{
public:
    QOfonoHandsfreeAudioCardPrivate();
    QString modemPath;
    OfonoHandsfreeAudioCard *ofonoHandsfreeAudioCard;
    QVariantMap properties;

};

QOfonoHandsfreeAudioCardPrivate::QOfonoHandsfreeAudioCardPrivate() :
    modemPath(QString())
  , ofonoHandsfreeAudioCard(0)
{
}

QOfonoHandsfreeAudioCard::QOfonoHandsfreeAudioCard(QObject *parent) :
    QObject(parent)
  , d_ptr(new QOfonoHandsfreeAudioCardPrivate)
{
}

QOfonoHandsfreeAudioCard::~QOfonoHandsfreeAudioCard()
{
    delete d_ptr;
}

void QOfonoHandsfreeAudioCard::setModemPath(const QString &path)
{
    if (path == d_ptr->modemPath ||
            path.isEmpty())
        return;

    if (path != modemPath()) {
        if (d_ptr->ofonoHandsfreeAudioCard) {
            delete d_ptr->ofonoHandsfreeAudioCard;
            d_ptr->ofonoHandsfreeAudioCard = 0;
            d_ptr->properties.clear();
        }
        d_ptr->modemPath = path;
        d_ptr->ofonoHandsfreeAudioCard = new OfonoHandsfreeAudioCard("org.ofono", path, QDBusConnection::systemBus(),this);

        if (d_ptr->ofonoHandsfreeAudioCard) {

            QDBusPendingReply<QVariantMap> reply;
            reply = d_ptr->ofonoHandsfreeAudioCard->GetProperties();
            reply.waitForFinished();
            d_ptr->properties = reply.value();
            Q_EMIT modemPathChanged(path);
        }
    }
}

QString QOfonoHandsfreeAudioCard::modemPath() const
{
    return d_ptr->modemPath;
}

void QOfonoHandsfreeAudioCard::propertyChanged(const QString& property, const QDBusVariant& dbusvalue)
{
    QVariant value = dbusvalue.variant();
    d_ptr->properties.insert(property,value);

}

QString QOfonoHandsfreeAudioCard::remoteAddress() const
{
    if (d_ptr->ofonoHandsfreeAudioCard) {
        return d_ptr->properties["RemoteAddress"].value<QString>();
    }
    return QString();
}

QString QOfonoHandsfreeAudioCard::localAddress() const
{
    if (d_ptr->ofonoHandsfreeAudioCard) {
        return d_ptr->properties["LocalAddress"].value<QString>();
    }
    return QString();
}

void QOfonoHandsfreeAudioCard::connectAudio()
{
    if (d_ptr->ofonoHandsfreeAudioCard) {
        QDBusPendingReply<> result = d_ptr->ofonoHandsfreeAudioCard->Connect();

        QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(result, this);
        connect(watcher, SIGNAL(finished(QDBusPendingCallWatcher*)),
                SLOT(connectAudioFinished(QDBusPendingCallWatcher*)));
    }
}

bool QOfonoHandsfreeAudioCard::isValid() const
{
    return d_ptr->ofonoHandsfreeAudioCard->isValid();
}

void QOfonoHandsfreeAudioCard::connectAudioFinished(QDBusPendingCallWatcher *call)
{
    call->deleteLater();
    QDBusPendingReply<> reply = *call;
    QOfonoHandsfreeAudioCard::Error error = NoError;
    QString errorString;

    if (reply.isError()) {
         qWarning() << "QOfonoHandsfreeAudioCard::connectAudio() failed:" << reply.error();
         error = errorNameToEnum(reply.error().name());
         errorString = reply.error().name() + " " + reply.error().message();
    }

    emit connectAudioComplete(error, errorString);
}

QOfonoHandsfreeAudioCard::Error QOfonoHandsfreeAudioCard::errorNameToEnum(const QString &errorName)
{
    if (errorName == "")
        return NoError;
    else if (errorName == "org.ofono.Error.NotImplemented")
        return NotImplementedError;
    else if (errorName == "org.ofono.Error.InProgress")
        return InProgressError;
    else if (errorName == "org.ofono.Error.InvalidArguments")
        return InvalidArgumentsError;
    else if (errorName == "org.ofono.Error.InvalidFormat")
        return InvalidFormatError;
    else if (errorName == "org.ofono.Error.Failed")
        return FailedError;
    else
        return UnknownError;
}