sims-list-model.cpp
connectivityqt/sims-list-model.h
internal/sims-list-model-parameters.h
QDebug
sims-list-model.moc
connectivityqt/sim.h
sims-list-model.cpp
connectivityqt/sims-list-model.h
QAbstractItemModel
QDBusConnection
QDBusObjectPath
QDebug
QObject
internal/sims-list-model-parameters.h
lomiri/util/DefinesPtrs.h
memory
sims-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/sims-list-model.h>

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

    #include<QDebug>

    usingnamespacestd;

    namespaceconnectivityqt
    {

    classSimsListModel::Priv:publicQObject
    {
    Q_OBJECT

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

    voidupdateSimDBusPaths(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_sims)
    {
    current<<m->path();
    }

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

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

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

    if(!toAdd.isEmpty())
    {
    p.beginInsertRows(QModelIndex(),m_sims.size(),m_sims.size()+toAdd.size()-1);
    for(constauto&path:toAdd)
    {
    autosim=std::make_shared<Sim>(path,m_propertyCache->connection(),nullptr);
    m_objectOwner(sim.get());
    m_sims<<sim;
    connect(sim.get(),&Sim::lockedChanged,this,&Priv::lockedChanged);
    connect(sim.get(),&Sim::presentChanged,this,&Priv::presentChanged);
    connect(sim.get(),&Sim::dataRoamingEnabledChanged,this,&Priv::dataRoamingEnabledChanged);
    connect(sim.get(),&Sim::imsiChanged,this,&Priv::imsiChanged);
    connect(sim.get(),&Sim::primaryPhoneNumberChanged,this,&Priv::primaryPhoneNumberChanged);
    connect(sim.get(),&Sim::mccChanged,this,&Priv::mccChanged);
    connect(sim.get(),&Sim::mncChanged,this,&Priv::mncChanged);
    connect(sim.get(),&Sim::preferredLanguagesChanged,this,&Priv::preferredLanguagesChanged);
    }
    p.endInsertRows();
    }

    Q_EMITp.simsUpdated();
    }

    QModelIndexfindSim(QObject*o)
    {
    autosim=qobject_cast<Sim*>(o);
    for(autoi=m_sims.constBegin();i!=m_sims.constEnd();++i)
    {
    if(i->get()==sim)
    {
    returnp.index(std::distance(m_sims.constBegin(),i));
    }
    }
    returnQModelIndex();
    }

    publicQ_SLOTS:
    voidlockedChanged()
    {
    autoidx=findSim(sender());
    p.dataChanged(idx,idx,{SimsListModel::Roles::RoleLocked});
    }

    voidpresentChanged()
    {
    autoidx=findSim(sender());
    p.dataChanged(idx,idx,{SimsListModel::Roles::RolePresent});
    }
    voiddataRoamingEnabledChanged()
    {
    autoidx=findSim(sender());
    p.dataChanged(idx,idx,{SimsListModel::Roles::RoleDataRoamingEnabled});
    }
    voidimsiChanged()
    {
    autoidx=findSim(sender());
    p.dataChanged(idx,idx,{SimsListModel::Roles::RoleImsi});
    }
    voidprimaryPhoneNumberChanged()
    {
    autoidx=findSim(sender());
    p.dataChanged(idx,idx,{SimsListModel::Roles::RolePrimaryPhoneNumber});
    }
    voidmccChanged()
    {
    autoidx=findSim(sender());
    p.dataChanged(idx,idx,{SimsListModel::Roles::RoleMcc});
    }
    voidmncChanged()
    {
    autoidx=findSim(sender());
    p.dataChanged(idx,idx,{SimsListModel::Roles::RoleMnc});
    }

    voidpreferredLanguagesChanged()
    {
    autoidx=findSim(sender());
    p.dataChanged(idx,idx,{SimsListModel::Roles::RolePreferredLanguages});
    }

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

    public:
    SimsListModel&p;
    function<void(QObject*)>m_objectOwner;
    QList<QDBusObjectPath>m_dbus_paths;
    QList<Sim::SPtr>m_sims;

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

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

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

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

    SimsListModel::~SimsListModel()
    {
    }

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

    intSimsListModel::rowCount(constQModelIndex&)const
    {
    returnd->m_sims.size();
    }

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

    autosim=d->m_sims.value(row);

    switch(role)
    {
    caseRoles::RoleIccid:
    returnsim->iccid();
    break;
    caseRoles::RoleImsi:
    returnsim->imsi();
    break;
    caseRoles::RolePrimaryPhoneNumber:
    returnsim->primaryPhoneNumber();
    break;
    caseRoleLocked:
    returnsim->locked();
    break;
    caseRolePresent:
    returnsim->present();
    break;
    caseRoles::RoleMcc:
    returnsim->mcc();
    break;
    caseRoles::RoleMnc:
    returnsim->mnc();
    caseRoles::RolePreferredLanguages:
    returnQVariant::fromValue<QStringList>(sim->preferredLanguages());
    break;
    caseRoles::RoleDataRoamingEnabled:
    returnsim->dataRoamingEnabled();
    break;
    caseRoles::RoleSim:
    returnQVariant::fromValue<Sim*>(sim.get());
    break;
    }

    returnQVariant();
    }

    Sim::SPtrSimsListModel::getSimByPath(constQDBusObjectPath&path)const
    {
    for(autosim:d->m_sims)
    {
    if(sim->path()==path){
    returnsim;
    }
    }
    returnnullptr;
    }

    }

    #include"sims-list-model.moc"