<commit_before>/****************************************************************************
**
** Copyright (C) 2014 Jolla Ltd.
** Contact: <EMAIL>
**
** 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;
    }
    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;
}<commit_msg>[libqofono] Fixed QOfonoModemInterface2 bevaviour when interface disappears<commit_after>/****************************************************************************
**
** Copyright (C) 2014 Jolla Ltd.
** Contact: <EMAIL>
**
** 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;
}
{'fraction_non_alphanumeric': 0.1542081204624899, 'fraction_numerical': 0.007932239849421888, 'mean_word_length': 3.7931701030927836, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 67, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5874668', 'n_tokens_mistral': 2352, 'n_tokens_neox': 2297, 'n_words': 455}