File indexing completed on 2024-11-24 04:39:27

0001 /*
0002     This file is part of Akonadi Contact.
0003 
0004     SPDX-FileCopyrightText: 2009 Stephen Kelly <steveire@gmail.com>
0005     SPDX-FileCopyrightText: 2009 Tobias Koenig <tokoe@kde.org>
0006 
0007     SPDX-License-Identifier: LGPL-2.0-or-later
0008 */
0009 
0010 #include "contactstreemodel.h"
0011 
0012 #include <KContacts/Addressee>
0013 #include <KContacts/ContactGroup>
0014 
0015 #include <KIconLoader>
0016 #include <KLocalizedString>
0017 
0018 #include <QIcon>
0019 #include <QLocale>
0020 
0021 using namespace Akonadi;
0022 
0023 class Akonadi::ContactsTreeModelPrivate
0024 {
0025 public:
0026     ContactsTreeModelPrivate()
0027         : mColumns(ContactsTreeModel::Columns() << ContactsTreeModel::FullName)
0028         , mIconSize(KIconLoader::global()->currentSize(KIconLoader::Small))
0029     {
0030     }
0031 
0032     ContactsTreeModel::Columns mColumns;
0033     const int mIconSize;
0034 };
0035 
0036 ContactsTreeModel::ContactsTreeModel(Monitor *monitor, QObject *parent)
0037     : EntityTreeModel(monitor, parent)
0038     , d(new ContactsTreeModelPrivate)
0039 {
0040 }
0041 
0042 ContactsTreeModel::~ContactsTreeModel() = default;
0043 
0044 void ContactsTreeModel::setColumns(const Columns &columns)
0045 {
0046     beginResetModel();
0047     d->mColumns = columns;
0048     endResetModel();
0049 }
0050 
0051 ContactsTreeModel::Columns ContactsTreeModel::columns() const
0052 {
0053     return d->mColumns;
0054 }
0055 
0056 QVariant ContactsTreeModel::entityData(const Item &item, int column, int role) const
0057 {
0058     if (item.mimeType() == KContacts::Addressee::mimeType()) {
0059         if (!item.hasPayload<KContacts::Addressee>()) {
0060             // Pass modeltest
0061             if (role == Qt::DisplayRole) {
0062                 return item.remoteId();
0063             }
0064 
0065             return {};
0066         }
0067 
0068         const auto contact = item.payload<KContacts::Addressee>();
0069 
0070         if (role == Qt::DecorationRole) {
0071             if (column == 0) {
0072                 const KContacts::Picture picture = contact.photo();
0073                 if (picture.isIntern()) {
0074                     return picture.data().scaled(QSize(d->mIconSize, d->mIconSize), Qt::KeepAspectRatio);
0075                 } else {
0076                     return QIcon::fromTheme(QStringLiteral("user-identity"));
0077                 }
0078             }
0079             return {};
0080         } else if ((role == Qt::DisplayRole) || (role == Qt::EditRole)) {
0081             switch (d->mColumns.at(column)) {
0082             case FullName:
0083                 if (contact.realName().isEmpty()) {
0084                     if (contact.preferredEmail().isEmpty()) {
0085                         return contact.familyName();
0086                     }
0087                     return contact.preferredEmail();
0088                 }
0089                 return contact.realName();
0090             case FamilyName:
0091                 return contact.familyName();
0092             case GivenName:
0093                 return contact.givenName();
0094             case Birthday:
0095                 if (contact.birthday().date().isValid()) {
0096                     return QLocale().toString(contact.birthday().date(), QLocale::ShortFormat);
0097                 }
0098                 break;
0099             case HomeAddress: {
0100                 const KContacts::Address address = contact.address(KContacts::Address::Home);
0101                 if (!address.isEmpty()) {
0102                     return address.formatted(KContacts::AddressFormatStyle::Postal);
0103                 }
0104                 break;
0105             }
0106             case BusinessAddress: {
0107                 const KContacts::Address address = contact.address(KContacts::Address::Work);
0108                 if (!address.isEmpty()) {
0109                     return address.formatted(KContacts::AddressFormatStyle::Postal);
0110                 }
0111                 break;
0112             }
0113             case PhoneNumbers: {
0114                 QStringList values;
0115 
0116                 const KContacts::PhoneNumber::List numbers = contact.phoneNumbers();
0117                 values.reserve(numbers.count());
0118                 for (const KContacts::PhoneNumber &number : numbers) {
0119                     values += number.number();
0120                 }
0121 
0122                 return values.join(QLatin1Char('\n'));
0123                 break;
0124             }
0125             case PreferredEmail:
0126                 return contact.preferredEmail();
0127             case AllEmails:
0128                 return contact.emails().join(QLatin1Char('\n'));
0129             case Organization:
0130                 return contact.organization();
0131             case Role:
0132                 return contact.role();
0133             case Homepage:
0134                 return contact.url().url();
0135             case Note:
0136                 return contact.note();
0137             }
0138         } else if (role == DateRole) {
0139             if (d->mColumns.at(column) == Birthday) {
0140                 return contact.birthday();
0141             } else {
0142                 return QDate();
0143             }
0144         }
0145     } else if (item.mimeType() == KContacts::ContactGroup::mimeType()) {
0146         if (!item.hasPayload<KContacts::ContactGroup>()) {
0147             // Pass modeltest
0148             if (role == Qt::DisplayRole) {
0149                 return item.remoteId();
0150             }
0151 
0152             return {};
0153         }
0154 
0155         if (role == Qt::DecorationRole) {
0156             if (column == 0) {
0157                 return QIcon::fromTheme(QStringLiteral("x-mail-distribution-list"));
0158             } else {
0159                 return {};
0160             }
0161         } else if ((role == Qt::DisplayRole) || (role == Qt::EditRole)) {
0162             switch (d->mColumns.at(column)) {
0163             case FullName: {
0164                 const auto group = item.payload<KContacts::ContactGroup>();
0165                 return group.name();
0166             }
0167             default:
0168                 return {};
0169             }
0170         }
0171     }
0172 
0173     return EntityTreeModel::entityData(item, column, role);
0174 }
0175 
0176 QVariant ContactsTreeModel::entityData(const Collection &collection, int column, int role) const
0177 {
0178     if (role == Qt::DisplayRole) {
0179         switch (column) {
0180         case 0:
0181             return EntityTreeModel::entityData(collection, column, role);
0182         default:
0183             return QString(); // pass model test
0184         }
0185     }
0186 
0187     return EntityTreeModel::entityData(collection, column, role);
0188 }
0189 
0190 int ContactsTreeModel::entityColumnCount(HeaderGroup headerGroup) const
0191 {
0192     if (headerGroup == EntityTreeModel::CollectionTreeHeaders) {
0193         return 1;
0194     } else if (headerGroup == EntityTreeModel::ItemListHeaders) {
0195         return d->mColumns.count();
0196     } else {
0197         return EntityTreeModel::entityColumnCount(headerGroup);
0198     }
0199 }
0200 
0201 QVariant ContactsTreeModel::entityHeaderData(int section, Qt::Orientation orientation, int role, HeaderGroup headerGroup) const
0202 {
0203     if (role == Qt::DisplayRole) {
0204         if (orientation == Qt::Horizontal) {
0205             if (headerGroup == EntityTreeModel::CollectionTreeHeaders) {
0206                 if (section >= 1) {
0207                     return {};
0208                 }
0209 
0210                 switch (section) {
0211                 case 0:
0212                     return i18nc("@title:column address books overview", "Address Books");
0213                     break;
0214                 }
0215             } else if (headerGroup == EntityTreeModel::ItemListHeaders) {
0216                 if (section < 0 || section >= d->mColumns.count()) {
0217                     return {};
0218                 }
0219 
0220                 switch (d->mColumns.at(section)) {
0221                 case FullName:
0222                     return i18nc("@title:column name of a person", "Name");
0223                 case FamilyName:
0224                     return i18nc("@title:column family name of a person", "Family Name");
0225                 case GivenName:
0226                     return i18nc("@title:column given name of a person", "Given Name");
0227                 case Birthday:
0228                     return KContacts::Addressee::birthdayLabel();
0229                 case HomeAddress:
0230                     return i18nc("@title:column home address of a person", "Home");
0231                 case BusinessAddress:
0232                     return i18nc("@title:column work address of a person", "Work");
0233                 case PhoneNumbers:
0234                     return i18nc("@title:column phone numbers of a person", "Phone Numbers");
0235                 case PreferredEmail:
0236                     return i18nc("@title:column the preferred email addresses of a person", "Preferred EMail");
0237                 case AllEmails:
0238                     return i18nc("@title:column all email addresses of a person", "All EMails");
0239                 case Organization:
0240                     return KContacts::Addressee::organizationLabel();
0241                 case Role:
0242                     return KContacts::Addressee::roleLabel();
0243                 case Homepage:
0244                     return KContacts::Addressee::urlLabel();
0245                 case Note:
0246                     return KContacts::Addressee::noteLabel();
0247                 }
0248             }
0249         }
0250     }
0251 
0252     return EntityTreeModel::entityHeaderData(section, orientation, role, headerGroup);
0253 }
0254 
0255 #include "moc_contactstreemodel.cpp"