10#include "customfieldslistwidget.h"
11#include "../utils/utils.h"
13#include "customfieldmanager_p.h"
14#include "customfieldslistdelegate.h"
15#include <KContacts/Addressee>
16#include <QSortFilterProxyModel>
22CustomFieldsListWidget::CustomFieldsListWidget(
QWidget *parent)
25 , mModel(new CustomFieldsModel(this))
29 mCustomFieldList->setSortingEnabled(
true);
30 mCustomFieldList->setRootIsDecorated(
false);
31 auto customFieldDelegate =
new Akonadi::CustomFieldsListDelegate(mCustomFieldList,
this);
32 mCustomFieldList->setItemDelegate(customFieldDelegate);
33 topLayout->addWidget(mCustomFieldList);
36 proxyModel->setSourceModel(mModel);
37 mCustomFieldList->setModel(proxyModel);
38 mCustomFieldList->setColumnHidden(2,
true);
41CustomFieldsListWidget::~CustomFieldsListWidget() =
default;
45 CustomField::List externalCustomFields;
47 CustomField::List globalCustomFields = CustomFieldManager::globalCustomFieldDescriptions();
50 for (
const QString &custom : customs) {
54 Akonadi::Utils::splitCustomField(custom, app, name, value);
64 blacklist << QStringLiteral(
"BlogFeed") << QStringLiteral(
"X-IMAddress") << QStringLiteral(
"X-Profession") << QStringLiteral(
"X-Office")
65 << QStringLiteral(
"X-ManagersName") << QStringLiteral(
"X-AssistantsName") << QStringLiteral(
"X-Anniversary")
66 << QStringLiteral(
"X-SpousesName") << QStringLiteral(
"MailPreferedFormatting") << QStringLiteral(
"MailAllowToRemoteContent")
67 << QStringLiteral(
"CRYPTOPROTOPREF") << QStringLiteral(
"OPENPGPFP") << QStringLiteral(
"SMIMEFP") << QStringLiteral(
"CRYPTOSIGNPREF")
68 << QStringLiteral(
"CRYPTOENCRYPTPREF");
71 for (
const QString &blacklistEntry : std::as_const(blacklist)) {
72 upperCaseBlacklist << blacklistEntry.toUpper();
74 blacklist.
unite(upperCaseBlacklist);
81 bool isLocalCustomField =
false;
82 const int localCustomFieldsCount(mLocalCustomFields.count());
83 for (
int i = 0; i < localCustomFieldsCount; ++i) {
84 if (mLocalCustomFields[i].key() == name) {
85 mLocalCustomFields[i].setValue(value);
86 isLocalCustomField =
true;
92 bool isGlobalCustomField =
false;
93 const int globalCustomFieldsCount(globalCustomFields.count());
94 for (
int i = 0; i < globalCustomFieldsCount; ++i) {
95 if (globalCustomFields[i].key() == name) {
96 globalCustomFields[i].setValue(value);
97 isGlobalCustomField =
true;
103 if (!isLocalCustomField && !isGlobalCustomField) {
107 CustomField customField(name, name, CustomField::TextType, CustomField::LocalScope);
108 customField.setValue(value);
110 mLocalCustomFields << customField;
114 CustomField customField(key, key, CustomField::TextType, CustomField::ExternalScope);
115 customField.setValue(value);
117 externalCustomFields << customField;
122 mModel->setCustomFields(CustomField::List() << mLocalCustomFields << globalCustomFields << externalCustomFields);
127 const CustomField::List customFields = mModel->customFields();
128 for (
const CustomField &customField : customFields) {
130 if (customField.scope() != CustomField::ExternalScope) {
131 if (!customField.value().isEmpty()) {
132 contact.
insertCustom(QStringLiteral(
"KADDRESSBOOK"), customField.key(), customField.value());
134 contact.
removeCustom(QStringLiteral(
"KADDRESSBOOK"), customField.key());
142 for (
const CustomField &oldCustomField : std::as_const(mLocalCustomFields)) {
143 if (oldCustomField.scope() != CustomField::ExternalScope) {
144 bool fieldStillExists =
false;
145 for (
const CustomField &newCustomField : std::as_const(customFields)) {
146 if (newCustomField.scope() != CustomField::ExternalScope) {
147 if (newCustomField.key() == oldCustomField.key()) {
148 fieldStillExists =
true;
154 if (!fieldStillExists) {
155 contact.
removeCustom(QStringLiteral(
"KADDRESSBOOK"), oldCustomField.key());
161 CustomField::List globalCustomFields;
162 for (
const CustomField &customField : std::as_const(customFields)) {
163 if (customField.scope() == CustomField::GlobalScope) {
164 globalCustomFields << customField;
168 CustomFieldManager::setGlobalCustomFieldDescriptions(globalCustomFields);
171void CustomFieldsListWidget::setReadOnly(
bool readOnly)
176void CustomFieldsListWidget::slotAddNewField(
const CustomField &field)
178 const int lastRow = mModel->rowCount();
180 mModel->setData(mModel->index(lastRow, 2), field.key(),
Qt::EditRole);
181 mModel->setData(mModel->index(lastRow, 0), field.title(),
Qt::EditRole);
182 mModel->setData(mModel->index(lastRow, 0), field.type(), CustomFieldsModel::TypeRole);
183 mModel->setData(mModel->index(lastRow, 0), field.scope(), CustomFieldsModel::ScopeRole);
186void CustomFieldsListWidget::setLocalCustomFieldDescriptions(
const QVariantList &descriptions)
188 mLocalCustomFields.clear();
189 mLocalCustomFields.reserve(descriptions.count());
190 for (
const QVariant &description : descriptions) {
191 mLocalCustomFields.append(CustomField::fromVariantMap(description.toMap(), CustomField::LocalScope));
195QVariantList CustomFieldsListWidget::localCustomFieldDescriptions()
const
197 const CustomField::List customFields = mModel->customFields();
199 QVariantList descriptions;
200 for (
const CustomField &field : customFields) {
201 if (field.scope() == CustomField::LocalScope) {
202 descriptions.append(field.toVariantMap());
209#include "moc_customfieldslistwidget.cpp"
A widget for editing the display name of a contact.
QString name(StandardAction id)
bool insertRow(int row, const QModelIndex &parent)
bool contains(const QSet< T > &other) const const
bool isEmpty() const const
QSet< T > & unite(const QSet< T > &other)