modems-list-model.cpp
connectivityqt/modems-list-model.h
connectivityqt/modem.h
internal/modems-list-model-parameters.h
QDebug
modems-list-model.moc
connectivityqt/modem.h
modems-list-model.cpp
connectivityqt/modems-list-model.h
connectivityqt/sim.h
sims-list-model.h
QAbstractItemModel
QDBusConnection
QDBusObjectPath
QDebug
QObject
internal/modems-list-model-parameters.h
lomiri/util/DefinesPtrs.h
memory
modems-list-model.moc
connectivityqt

    /*
    *Copyright©2016CanonicalLtd.
    *
    *Thisprogramisfreesoftware:youcanredistributeitand/ormodifyit
    *underthetermsoftheGNULesserGeneralPublicLicenseversion3,
    *aspublishedbytheFreeSoftwareFoundation.
    *
    *Thisprogramisdistributedinthehopethatitwillbeuseful,
    *butWITHOUTANYWARRANTY;withouteventheimpliedwarrantyof
    *MERCHANTABILITYorFITNESSFORAPARTICULARPURPOSE.Seethe
    *GNULesserGeneralPublicLicenseformoredetails.
    *
    *YoushouldhavereceivedacopyoftheGNULesserGeneralPublicLicense
    *alongwiththisprogram.Ifnot,see<http://www.gnu.org/licenses/>.
    *
    *Authors:
    *AnttiKaijanmäki<antti.kaijanmaki@canonical.com>
    */

    #include<connectivityqt/modems-list-model.h>
    #include<connectivityqt/modem.h>

    #include"internal/modems-list-model-parameters.h"

    #include<QDebug>

    usingnamespacestd;

    namespaceconnectivityqt
    {

    classModemsListModel::Priv:publicQObject
    {
    Q_OBJECT

    public:
    Priv(ModemsListModel&parent):
    p(parent)
    {
    }

    voidupdateModemDBusPaths(QList<QDBusObjectPath>values)
    {
    #ifQT_VERSION<QT_VERSION_CHECK(5,15,0)
    autopaths=values.toSet();
    #else
    autopaths=QSet<QDBusObjectPath>(values.begin(),values.end());
    #endif
    QSet<QDBusObjectPath>current;
    for(constauto&m:m_modems)
    {
    current<<m->path();
    }

    autotoRemove(current);
    toRemove.subtract(paths);

    autotoAdd(paths);
    toAdd.subtract(current);

    QMutableListIterator<Modem::SPtr>i(m_modems);
    intidx=0;
    while(i.hasNext())
    {
    automodem(i.next());
    if(toRemove.contains(modem->path()))
    {
    p.beginRemoveRows(QModelIndex(),idx,idx);
    i.remove();
    p.endRemoveRows();
    }
    else
    {
    ++idx;
    }
    }

    if(!toAdd.isEmpty())
    {
    p.beginInsertRows(QModelIndex(),m_modems.size(),m_modems.size()+toAdd.size()-1);
    for(constauto&path:toAdd)
    {
    automodem=std::make_shared<Modem>(path,m_propertyCache->connection(),m_sims);
    m_objectOwner(modem.get());
    m_modems<<modem;
    connect(modem.get(),&Modem::simChanged,this,&Priv::simChanged);
    }
    p.endInsertRows();
    }
    }

    QModelIndexfindModem(QObject*o)
    {
    automodem=qobject_cast<Modem*>(o);
    for(autoi=m_modems.constBegin();i!=m_modems.constEnd();++i)
    {
    if(i->get()==modem)
    {
    returnp.index(std::distance(m_modems.constBegin(),i));
    }
    }
    returnQModelIndex();
    }

    publicQ_SLOTS:
    voidsimChanged(Sim*sim)
    {
    Q_UNUSED(sim)
    autoidx=findModem(sender());
    p.dataChanged(idx,idx,{ModemsListModel::Roles::RoleSim});
    }

    voidpropertyChanged(constQString&name,constQVariant&value)
    {
    if(name=="Modems")
    {
    QList<QDBusObjectPath>tmp;
    qvariant_cast<QDBusArgument>(value)>>tmp;
    updateModemDBusPaths(tmp);
    }
    }

    public:
    ModemsListModel&p;
    function<void(QObject*)>m_objectOwner;
    SimsListModel::SPtrm_sims;
    QList<QDBusObjectPath>m_dbus_paths;
    QList<Modem::SPtr>m_modems;

    shared_ptr<ComLomiriConnectivity1PrivateInterface>m_writeInterface;
    util::DBusPropertyCache::SPtrm_propertyCache;
    };

    ModemsListModel::ModemsListModel(constinternal::ModemsListModelParameters&parameters):
    QAbstractListModel(0),
    d(newPriv(*this))
    {
    d->m_objectOwner=parameters.objectOwner;
    d->m_sims=parameters.sims;
    d->m_writeInterface=parameters.writeInterface;
    d->m_propertyCache=parameters.propertyCache;

    connect(d->m_propertyCache.get(),
    &util::DBusPropertyCache::propertyChanged,d.get(),
    &Priv::propertyChanged);

    QList<QDBusObjectPath>tmp;
    qvariant_cast<QDBusArgument>(d->m_propertyCache->get("Modems"))>>tmp;
    d->updateModemDBusPaths(tmp);
    }

    ModemsListModel::~ModemsListModel()
    {
    }

    intModemsListModel::columnCount(constQModelIndex&)const
    {
    return1;
    }

    intModemsListModel::rowCount(constQModelIndex&)const
    {
    returnd->m_modems.size();
    }

    QVariantModemsListModel::data(constQModelIndex&index,introle)const
    {
    introw(index.row());
    if(row<0||row>=d->m_modems.size())
    {
    returnQVariant();
    }

    automodem=d->m_modems.value(row);

    switch(role)
    {
    caseRoles::RoleIndex:
    returnmodem->index();
    break;
    caseRoles::RoleSerial:
    returnmodem->serial();
    break;
    caseRoles::RoleModem:
    returnQVariant::fromValue<Modem*>(modem.get());
    break;
    caseRoles::RoleSim:
    returnQVariant::fromValue<Sim*>(modem->sim());
    break;
    }

    returnQVariant();
    }

    }

    #include"modems-list-model.moc"