22 #include "customfieldseditwidget.h"
24 #include "customfieldeditordialog.h"
25 #include "customfieldmanager_p.h"
26 #include "customfieldsdelegate.h"
27 #include "customfieldsmodel.h"
29 #include <kabc/addressee.h>
30 #include <klocalizedstring.h>
31 #include <kmessagebox.h>
33 #include <QtCore/QPointer>
34 #include <QtCore/QUuid>
35 #include <QGridLayout>
36 #include <QPushButton>
38 #include <QSortFilterProxyModel>
45 value = str.
mid(colon + 1);
50 name = tmp.
mid(dash + 1);
55 CustomFieldsEditWidget::CustomFieldsEditWidget(
QWidget *parent)
64 mView->setRootIsDecorated(
false);
65 mView->setItemDelegate(
new CustomFieldsDelegate(
this));
76 mModel =
new CustomFieldsModel(
this);
80 mView->setModel(proxyModel);
81 mView->setColumnHidden(2,
true);
84 this, SLOT(slotUpdateButtons()));
85 connect(mAddButton, SIGNAL(clicked()),
this, SLOT(slotAdd()));
86 connect(mEditButton, SIGNAL(clicked()),
this, SLOT(slotEdit()));
87 connect(mRemoveButton, SIGNAL(clicked()),
this, SLOT(slotRemove()));
91 CustomFieldsEditWidget::~CustomFieldsEditWidget()
95 void CustomFieldsEditWidget::loadContact(
const KABC::Addressee &contact)
99 CustomField::List globalCustomFields = CustomFieldManager::globalCustomFieldDescriptions();
102 foreach (
const QString &custom, customs) {
105 splitCustomField(custom, app, name, value);
140 bool isLocalCustomField =
false;
141 for (
int i = 0; i < mLocalCustomFields.count(); ++i) {
142 if (mLocalCustomFields[i].key() == name) {
143 mLocalCustomFields[i].setValue(value);
144 isLocalCustomField =
true;
150 bool isGlobalCustomField =
false;
151 for (
int i = 0; i < globalCustomFields.
count(); ++i) {
152 if (globalCustomFields[i].key() == name) {
153 globalCustomFields[i].setValue(value);
154 isGlobalCustomField =
true;
160 if (!isLocalCustomField && !isGlobalCustomField) {
165 customField.setValue(value);
167 mLocalCustomFields << customField;
172 customField.setValue(value);
174 externalCustomFields << customField;
179 mModel->setCustomFields(
CustomField::List() << mLocalCustomFields << globalCustomFields << externalCustomFields);
182 void CustomFieldsEditWidget::storeContact(KABC::Addressee &contact)
const
185 foreach (
const CustomField &customField, customFields) {
188 if (!customField.value().
isEmpty()) {
189 contact.insertCustom(
QLatin1String(
"KADDRESSBOOK"), customField.key(), customField.value());
191 contact.removeCustom(
QLatin1String(
"KADDRESSBOOK"), customField.key());
199 foreach (
const CustomField &oldCustomField, mLocalCustomFields) {
202 bool fieldStillExists =
false;
203 foreach (
const CustomField &newCustomField, customFields) {
205 if (newCustomField.key() == oldCustomField.key()) {
206 fieldStillExists =
true;
212 if (!fieldStillExists) {
213 contact.removeCustom(
QLatin1String(
"KADDRESSBOOK"), oldCustomField.key());
220 foreach (
const CustomField &customField, customFields) {
222 globalCustomFields << customField;
226 CustomFieldManager::setGlobalCustomFieldDescriptions(globalCustomFields);
229 void CustomFieldsEditWidget::setReadOnly(
bool readOnly)
231 mReadOnly = readOnly;
233 mView->setEnabled(!mReadOnly);
238 void CustomFieldsEditWidget::setLocalCustomFieldDescriptions(
const QVariantList &descriptions)
240 mLocalCustomFields.clear();
242 foreach (
const QVariant &description, descriptions) {
247 QVariantList CustomFieldsEditWidget::localCustomFieldDescriptions()
const
251 QVariantList descriptions;
254 descriptions.
append(field.toVariantMap());
261 void CustomFieldsEditWidget::slotAdd()
276 dlg->setCustomField(field);
278 if (dlg->exec() == QDialog::Accepted) {
279 const int lastRow = mModel->rowCount();
280 mModel->insertRow(lastRow);
282 field = dlg->customField();
283 mModel->setData(mModel->index(lastRow, 2), field.key(), Qt::EditRole);
284 mModel->setData(mModel->index(lastRow, 0), field.title(), Qt::EditRole);
285 mModel->setData(mModel->index(lastRow, 0), field.type(), CustomFieldsModel::TypeRole);
286 mModel->setData(mModel->index(lastRow, 0), field.scope(), CustomFieldsModel::ScopeRole);
292 void CustomFieldsEditWidget::slotEdit()
294 const QModelIndex currentIndex = mView->currentIndex();
300 field.setKey(mModel->index(currentIndex.
row(), 2).data(Qt::DisplayRole).toString());
301 field.setTitle(mModel->index(currentIndex.
row(), 0).data(Qt::DisplayRole).toString());
302 field.setType(static_cast<CustomField::Type>(currentIndex.
data(CustomFieldsModel::TypeRole).
toInt()));
303 field.setScope(static_cast<CustomField::Scope>(currentIndex.
data(CustomFieldsModel::ScopeRole).
toInt()));
306 dlg->setCustomField(field);
308 if (dlg->exec() == QDialog::Accepted) {
309 field = dlg->customField();
310 mModel->setData(mModel->index(currentIndex.
row(), 2), field.key(), Qt::EditRole);
311 mModel->setData(mModel->index(currentIndex.
row(), 0), field.title(), Qt::EditRole);
312 mModel->setData(currentIndex, field.type(), CustomFieldsModel::TypeRole);
313 mModel->setData(currentIndex, field.scope(), CustomFieldsModel::ScopeRole);
319 void CustomFieldsEditWidget::slotRemove()
321 const QModelIndex currentIndex = mView->currentIndex();
326 if (KMessageBox::warningContinueCancel(
this,
327 i18nc(
"Custom Fields",
"Do you really want to delete the selected custom field?"),
328 i18n(
"Confirm Delete"), KStandardGuiItem::del()) != KMessageBox::Continue) {
332 mModel->removeRow(currentIndex.
row());
335 void CustomFieldsEditWidget::slotUpdateButtons()
337 const bool hasCurrent = mView->currentIndex().
isValid();
338 const bool isExternal = (hasCurrent &&
341 mAddButton->setEnabled(!mReadOnly);
342 mEditButton->setEnabled(!mReadOnly && hasCurrent && !isExternal);
343 mRemoveButton->setEnabled(!mReadOnly && hasCurrent && !isExternal);
A class that represents non-standard contact fields.
int indexOf(QChar ch, int from, Qt::CaseSensitivity cs) const
void append(const T &value)
virtual void setSourceModel(QAbstractItemModel *sourceModel)
QString & remove(int position, int n)
int toInt(bool *ok) const
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const
void setDynamicSortFilter(bool enable)
Field has been defined by the external data source (e.g. vCard)
void setMargin(int margin)
Field has been defined by user for all contacts.
bool contains(const T &value) const
Field has been defined by user for one contact.
void setSortingEnabled(bool enable)
QMap< QString, QVariant > toMap() const
QString mid(int position, int n) const
QVariant data(int role) const
int count(const T &value) const
QString left(int n) const