File indexing completed on 2024-11-24 04:50:41

0001 // SPDX-FileCopyrightText: 2022 Carl Schwan <carl@carlschwan.eu>
0002 // SPDX-License-Identifier: LGPL-2.1-or-later
0003 
0004 #include "addressmodel.h"
0005 
0006 AddressModel::AddressModel(QObject *parent)
0007     : QAbstractListModel(parent)
0008 {
0009 }
0010 
0011 int AddressModel::rowCount(const QModelIndex &parent) const
0012 {
0013     Q_UNUSED(parent)
0014     return m_addresses.count();
0015 }
0016 
0017 QVariant AddressModel::data(const QModelIndex &idx, int role) const
0018 {
0019     const auto &address = m_addresses[idx.row()];
0020     switch (role) {
0021     case CountryRole:
0022         return address.country();
0023     case ExtendedRole:
0024         return address.extended();
0025     case FormattedAddressRole:
0026         return address.formatted(KContacts::AddressFormatStyle::MultiLineInternational);
0027     case HasGeoRole:
0028         return address.geo().isValid();
0029     case LongitudeRole:
0030         return address.geo().longitude();
0031     case LatitudeRole:
0032         return address.geo().latitude();
0033     case IdRole:
0034         return address.id();
0035     case IsEmptyRole:
0036         return address.isEmpty();
0037     case LabelRole:
0038         return address.label();
0039     case PostalCodeRole:
0040         return address.postalCode();
0041     case PostOfficeBoxRole:
0042         return address.postOfficeBox();
0043     case RegionRole:
0044         return address.region();
0045     case StreetRole:
0046         return address.street();
0047     case TypeRole:
0048         return QVariant::fromValue(address.type());
0049     case TypeLabelRole:
0050         return address.typeLabel();
0051     default:
0052         return {};
0053     }
0054 }
0055 
0056 QHash<int, QByteArray> AddressModel::roleNames() const
0057 {
0058     return {
0059         {CountryRole, QByteArrayLiteral("country")},
0060         {ExtendedRole, QByteArrayLiteral("extended")},
0061         {FormattedAddressRole, QByteArrayLiteral("formattedAddress")},
0062         {HasGeoRole, QByteArrayLiteral("hasGeo")},
0063         {LatitudeRole, QByteArrayLiteral("latitude")},
0064         {LongitudeRole, QByteArrayLiteral("longitude")},
0065         {IdRole, QByteArrayLiteral("id")},
0066         {IsEmptyRole, QByteArrayLiteral("isEmpty")},
0067         {LabelRole, QByteArrayLiteral("label")},
0068         {PostalCodeRole, QByteArrayLiteral("postalCode")},
0069         {PostOfficeBoxRole, QByteArrayLiteral("postOfficeBox")},
0070         {RegionRole, QByteArrayLiteral("region")},
0071         {StreetRole, QByteArrayLiteral("street")},
0072         {TypeRole, QByteArrayLiteral("type")},
0073         {TypeLabelRole, QByteArrayLiteral("typeLabel")},
0074     };
0075 }
0076 
0077 void AddressModel::setAddresses(const KContacts::Address::List &addresses)
0078 {
0079     beginResetModel();
0080     m_addresses = addresses;
0081     endResetModel();
0082 }
0083 
0084 #include "moc_addressmodel.cpp"