File indexing completed on 2024-11-24 04:39:28
0001 /* 0002 This file is part of Akonadi Contact. 0003 0004 SPDX-FileCopyrightText: 2010 KDAB 0005 SPDX-FileContributor: Tobias Koenig <tokoe@kde.org> 0006 0007 SPDX-License-Identifier: LGPL-2.0-or-later 0008 */ 0009 0010 #include "emailaddressselectionproxymodel_p.h" 0011 0012 #include <Akonadi/Item> 0013 #include <KContacts/Addressee> 0014 #include <KContacts/ContactGroup> 0015 #include <KLocalizedString> 0016 0017 using namespace Akonadi; 0018 0019 static QString createToolTip(const KContacts::ContactGroup &group) 0020 { 0021 QString txt = QStringLiteral("<qt>"); 0022 0023 txt += QStringLiteral("<b>%1</b>").arg(i18n("Distribution List %1", group.name())); 0024 txt += QLatin1StringView("<ul>"); 0025 const uint groupDataCount(group.dataCount()); 0026 for (uint i = 0; i < groupDataCount; ++i) { 0027 txt += QLatin1StringView("<li>"); 0028 txt += group.data(i).name() + QLatin1Char(' '); 0029 txt += QLatin1StringView("<em>"); 0030 txt += group.data(i).email(); 0031 txt += QLatin1StringView("</em></li>"); 0032 } 0033 txt += QLatin1StringView("</ul>"); 0034 txt += QLatin1StringView("</qt>"); 0035 0036 return txt; 0037 } 0038 0039 static QString createToolTip(const QString &name, const QString &email) 0040 { 0041 return QStringLiteral("<qt>%1<b>%2</b></qt>").arg(name.isEmpty() ? QString() : name + QLatin1StringView("<br/>"), email); 0042 } 0043 0044 EmailAddressSelectionProxyModel::EmailAddressSelectionProxyModel(QObject *parent) 0045 : LeafExtensionProxyModel(parent) 0046 { 0047 } 0048 0049 EmailAddressSelectionProxyModel::~EmailAddressSelectionProxyModel() = default; 0050 0051 QVariant EmailAddressSelectionProxyModel::data(const QModelIndex &index, int role) const 0052 { 0053 const QVariant value = LeafExtensionProxyModel::data(index, role); 0054 0055 if (!value.isValid()) { // index is not a leaf child 0056 if (role == NameRole) { 0057 const auto item = index.data(ContactsTreeModel::ItemRole).value<Akonadi::Item>(); 0058 if (item.hasPayload<KContacts::Addressee>()) { 0059 const auto contact = item.payload<KContacts::Addressee>(); 0060 return contact.realName(); 0061 } else if (item.hasPayload<KContacts::ContactGroup>()) { 0062 const auto group = item.payload<KContacts::ContactGroup>(); 0063 return group.name(); 0064 } 0065 } else if (role == EmailAddressRole) { 0066 const auto item = index.data(ContactsTreeModel::ItemRole).value<Akonadi::Item>(); 0067 if (item.hasPayload<KContacts::Addressee>()) { 0068 const auto contact = item.payload<KContacts::Addressee>(); 0069 return contact.preferredEmail(); 0070 } else if (item.hasPayload<KContacts::ContactGroup>()) { 0071 const auto group = item.payload<KContacts::ContactGroup>(); 0072 return group.name(); // the name must be resolved by the caller 0073 } 0074 } else if (role == Qt::ToolTipRole) { 0075 const auto item = index.data(ContactsTreeModel::ItemRole).value<Akonadi::Item>(); 0076 if (item.hasPayload<KContacts::Addressee>()) { 0077 const auto contact = item.payload<KContacts::Addressee>(); 0078 return createToolTip(contact.realName(), contact.preferredEmail()); 0079 } else if (item.hasPayload<KContacts::ContactGroup>()) { 0080 return createToolTip(item.payload<KContacts::ContactGroup>()); 0081 } 0082 } 0083 } 0084 0085 return value; 0086 } 0087 0088 int EmailAddressSelectionProxyModel::leafRowCount(const QModelIndex &index) const 0089 { 0090 const auto item = index.data(ContactsTreeModel::ItemRole).value<Akonadi::Item>(); 0091 if (item.hasPayload<KContacts::Addressee>()) { 0092 const auto contact = item.payload<KContacts::Addressee>(); 0093 if (contact.emails().count() == 1) { 0094 return 0; 0095 } else { 0096 return contact.emails().count(); 0097 } 0098 } else if (item.hasPayload<KContacts::ContactGroup>()) { 0099 const auto group = item.payload<KContacts::ContactGroup>(); 0100 return group.dataCount(); 0101 } else { 0102 return 0; 0103 } 0104 } 0105 0106 int EmailAddressSelectionProxyModel::leafColumnCount(const QModelIndex &index) const 0107 { 0108 const auto item = index.data(ContactsTreeModel::ItemRole).value<Akonadi::Item>(); 0109 if (item.hasPayload<KContacts::Addressee>()) { 0110 return 1; 0111 } else if (item.hasPayload<KContacts::ContactGroup>()) { 0112 return 1; 0113 } else { 0114 return 0; 0115 } 0116 } 0117 0118 QVariant EmailAddressSelectionProxyModel::leafData(const QModelIndex &index, int row, int, int role) const 0119 { 0120 if (role == Qt::DisplayRole) { 0121 const auto item = index.data(ContactsTreeModel::ItemRole).value<Akonadi::Item>(); 0122 if (item.hasPayload<KContacts::Addressee>()) { 0123 const auto contact = item.payload<KContacts::Addressee>(); 0124 if (row >= 0 && row < contact.emails().count()) { 0125 return contact.emails().at(row); 0126 } 0127 } else if (item.hasPayload<KContacts::ContactGroup>()) { 0128 const auto group = item.payload<KContacts::ContactGroup>(); 0129 if (row >= 0 && row < (int)group.dataCount()) { 0130 return i18nc("Name and email address of a contact", "%1 <%2>", group.data(row).name(), group.data(row).email()); 0131 } 0132 } 0133 } else if (role == NameRole) { 0134 const auto item = index.data(ContactsTreeModel::ItemRole).value<Akonadi::Item>(); 0135 if (item.hasPayload<KContacts::Addressee>()) { 0136 const auto contact = item.payload<KContacts::Addressee>(); 0137 return contact.realName(); 0138 } else if (item.hasPayload<KContacts::ContactGroup>()) { 0139 const auto group = item.payload<KContacts::ContactGroup>(); 0140 if (row >= 0 && row < (int)group.dataCount()) { 0141 return group.data(row).name(); 0142 } 0143 } 0144 } else if (role == EmailAddressRole) { 0145 const auto item = index.data(ContactsTreeModel::ItemRole).value<Akonadi::Item>(); 0146 if (item.hasPayload<KContacts::Addressee>()) { 0147 const auto contact = item.payload<KContacts::Addressee>(); 0148 if (row >= 0 && row < contact.emails().count()) { 0149 return contact.emails().at(row); 0150 } 0151 } else if (item.hasPayload<KContacts::ContactGroup>()) { 0152 const auto group = item.payload<KContacts::ContactGroup>(); 0153 if (row >= 0 && row < (int)group.dataCount()) { 0154 return group.data(row).email(); 0155 } 0156 } 0157 } else if (role == Qt::ToolTipRole) { 0158 const auto item = index.data(ContactsTreeModel::ItemRole).value<Akonadi::Item>(); 0159 if (item.hasPayload<KContacts::Addressee>()) { 0160 const auto contact = item.payload<KContacts::Addressee>(); 0161 if (row >= 0 && row < contact.emails().count()) { 0162 return createToolTip(contact.realName(), contact.emails().at(row)); 0163 } 0164 } else if (item.hasPayload<KContacts::ContactGroup>()) { 0165 const auto group = item.payload<KContacts::ContactGroup>(); 0166 if (row >= 0 && row < (int)group.dataCount()) { 0167 return createToolTip(group.data(row).name(), group.data(row).email()); 0168 } 0169 } 0170 } else { 0171 return index.data(role); 0172 } 0173 0174 return {}; 0175 } 0176 0177 #include "moc_emailaddressselectionproxymodel_p.cpp"