qofonomodeminterface2.cpp 3.61 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
/****************************************************************************
**
** Copyright (C) 2014 Jolla Ltd.
** Contact: slava.monich@jolla.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 "qofonomodeminterface2.h"
#include "qofonomodem.h"

class QOfonoModemInterface2::Private
{
public:
    QString modemPath;
    QString interfaceName;
    QSharedPointer<QOfonoModem> modem;
    QDBusAbstractInterface *interface;
    QOfonoModemInterface2::ExtData *ext;

    Private(const QString &iname, QOfonoModemInterface2::ExtData *data) :
        interfaceName(iname), interface(NULL), ext(data) {}
    ~Private() { delete ext; }
};

QOfonoModemInterface2::ExtData::~ExtData()
{
}

QOfonoModemInterface2::QOfonoModemInterface2(const QString &iname, ExtData *ext, QObject *parent) :
    QObject(parent),
    d_ptr(new Private(iname, ext))
{
}

QOfonoModemInterface2::QOfonoModemInterface2(const QString &iname, QObject *parent) :
    QObject(parent),
    d_ptr(new Private(iname, NULL))
{
}

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

QOfonoModemInterface2::ExtData* QOfonoModemInterface2::extData() const
{
    return d_ptr->ext;
}

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

void QOfonoModemInterface2::setModemPath(const QString &path)
{
    if (d_ptr->modemPath != path) {
        bool wasValid = isValid();
        d_ptr->modemPath = path;

        if (!d_ptr->modem.isNull()) {
            disconnect(d_ptr->modem.data(), SIGNAL(interfacesChanged(QStringList)),
                this, SLOT(onModemInterfacesChanged(QStringList)));
            d_ptr->modem.reset();
        }
        if (d_ptr->interface) {
            delete d_ptr->interface;
            d_ptr->interface = NULL;
            dbusInterfaceDropped();
        }

        d_ptr->modem = QOfonoModem::instance(d_ptr->modemPath);
        connect(d_ptr->modem.data(), SIGNAL(interfacesChanged(QStringList)),
            this, SLOT(onModemInterfacesChanged(QStringList)));

        QStringList interfaces = d_ptr->modem->interfaces();
        if (interfaces.contains(d_ptr->interfaceName)) {
            d_ptr->interface = createDbusInterface(d_ptr->modemPath);
        }

        Q_EMIT modemPathChanged(d_ptr->modemPath);

        bool valid = isValid();
        if (valid != wasValid) {
            Q_EMIT validChanged(valid);
        }
    }
}

void QOfonoModemInterface2::dbusInterfaceDropped()
{
}

void QOfonoModemInterface2::onModemInterfacesChanged(const QStringList &interfaces)
{
    bool wasValid = isValid();
    if (interfaces.contains(d_ptr->interfaceName)) {
        if (!d_ptr->interface) {
            d_ptr->interface = createDbusInterface(d_ptr->modemPath);
        }
    } else if (d_ptr->interface) {
        delete d_ptr->interface;
        d_ptr->interface = NULL;
        dbusInterfaceDropped();
    }
    bool valid = isValid();
    if (valid != wasValid) {
        Q_EMIT validChanged(valid);
    }
}

bool QOfonoModemInterface2::isValid() const
{
    return d_ptr->interface && d_ptr->interface->isValid();
}

QDBusAbstractInterface *QOfonoModemInterface2::dbusInterface() const
{
    return d_ptr->interface;
}