Commit ad454cba authored by Laurent Montel's avatar Laurent Montel 😁

Create contact editor lib

parent 7bd16644
/*
This file is part of Akonadi Contact.
Copyright (c) 2009 Tobias Koenig <tokoe@kde.org>
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
#ifndef AKONADI_ABSTRACTCONTACTEDITORWIDGET_P_H
#define AKONADI_ABSTRACTCONTACTEDITORWIDGET_P_H
#include <QWidget>
namespace KContacts
{
class Addressee;
}
namespace Akonadi
{
class ContactMetaDataBase;
class AbstractContactEditorWidget : public QWidget
{
public:
/**
* Creates a new abstract contact editor widget.
*
* @param parent The parent widget.
*/
explicit AbstractContactEditorWidget(QWidget *parent = nullptr)
: QWidget(parent)
{
}
/**
* Destroys the abstract contact editor widget.
*/
~AbstractContactEditorWidget()
{
}
/**
* @param contact loads the given contact into the editor widget
*/
virtual void loadContact(const KContacts::Addressee &contact, const Akonadi::ContactMetaDataBase &metaData) = 0;
/**
* @param contact store the given contact into the editor widget
*/
virtual void storeContact(KContacts::Addressee &contact, Akonadi::ContactMetaDataBase &metaData) const = 0;
/**
* @param readOnly set read-only mode
*/
virtual void setReadOnly(bool readOnly) = 0;
};
}
#endif
/*
This file is part of Akonadi Contact.
Copyright (c) 2017 Laurent Montel <montel@kde.org>
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
#include "contactmetadatabase_p.h"
#include "attributes/contactmetadataattribute_p.h"
#include <item.h>
using namespace Akonadi;
class Q_DECL_HIDDEN ContactMetaDataBase::Private
{
public:
Private()
: mDisplayNameMode(-1)
{
}
int mDisplayNameMode;
QVariantList mCustomFieldDescriptions;
};
ContactMetaDataBase::ContactMetaDataBase()
: d(new Private)
{
}
ContactMetaDataBase::~ContactMetaDataBase()
{
delete d;
}
void ContactMetaDataBase::loadMetaData(const QVariantMap &metaData)
{
d->mDisplayNameMode = metaData.value(QStringLiteral("DisplayNameMode"), -1).toInt();
d->mCustomFieldDescriptions = metaData.value(QStringLiteral("CustomFieldDescriptions")).toList();
}
QVariantMap ContactMetaDataBase::storeMetaData() const
{
QVariantMap metaData;
if (d->mDisplayNameMode != -1) {
metaData.insert(QStringLiteral("DisplayNameMode"), QVariant(d->mDisplayNameMode));
}
if (!d->mCustomFieldDescriptions.isEmpty()) {
metaData.insert(QStringLiteral("CustomFieldDescriptions"), d->mCustomFieldDescriptions);
}
return metaData;
}
void ContactMetaDataBase::setDisplayNameMode(int mode)
{
d->mDisplayNameMode = mode;
}
int ContactMetaDataBase::displayNameMode() const
{
return d->mDisplayNameMode;
}
void ContactMetaDataBase::setCustomFieldDescriptions(const QVariantList &descriptions)
{
d->mCustomFieldDescriptions = descriptions;
}
QVariantList ContactMetaDataBase::customFieldDescriptions() const
{
return d->mCustomFieldDescriptions;
}
/*
This file is part of Akonadi Contact.
Copyright (c) 2017 Laurent Montel <montel@kde.org>
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
#ifndef AKONADI_CONTACTMETADATABASE_P_H
#define AKONADI_CONTACTMETADATABASE_P_H
#include <QStringList>
#include <QVariant>
namespace Akonadi
{
/**
* @short A helper class for storing contact specific settings.
*/
class ContactMetaDataBase
{
public:
/**
* Creates a contact meta data object.
*/
ContactMetaDataBase();
/**
* Destroys the contact meta data object.
*/
~ContactMetaDataBase();
/**
* Loads the meta data for the given @p contact.
*/
void loadMetaData(const QVariantMap &metaData);
/**
* Stores the meta data to the given @p contact.
*/
QVariantMap storeMetaData() const;
/**
* Sets the mode that is used for the display
* name of that contact.
*/
void setDisplayNameMode(int mode);
/**
* Returns the mode that is used for the display
* name of that contact.
*/
int displayNameMode() const;
/**
* Sets the @p descriptions of the custom fields of that contact.
* @param descriptions the descriptions to set
* The description list contains a QVariantMap for each custom field
* with the following keys:
* - key (string) The identifier of the field
* - title (string) The i18n'ed title of the field
* - type (string) The type description of the field
* Possible values for type description are
* - text
* - numeric
* - boolean
* - date
* - time
* - datetime
*/
void setCustomFieldDescriptions(const QVariantList &descriptions);
/**
* Returns the descriptions of the custom fields of the contact.
*/
QVariantList customFieldDescriptions() const;
private:
//@cond PRIVATE
Q_DISABLE_COPY(ContactMetaDataBase)
class Private;
Private *const d;
//@endcond
};
}
#endif
/*
This file is part of Akonadi Contact.
Copyright (c) 2010 Tobias Koenig <tokoe@kde.org>
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
#include "customfieldmanager_p.h"
#include <kconfig.h>
#include <kconfiggroup.h>
#include <QVector>
void CustomFieldManager::setGlobalCustomFieldDescriptions(const CustomField::List &customFields)
{
KConfig config(QStringLiteral("akonadi_contactrc"));
KConfigGroup group(&config, QStringLiteral("GlobalCustomFields"));
group.deleteGroup();
for (const CustomField &field : customFields) {
const QString key = field.key();
const QString value = CustomField::typeToString(field.type()) + QLatin1Char(':') + field.title();
group.writeEntry(key, value);
}
}
CustomField::List CustomFieldManager::globalCustomFieldDescriptions()
{
KConfig config(QStringLiteral("akonadi_contactrc"));
const KConfigGroup group(&config, QStringLiteral("GlobalCustomFields"));
CustomField::List customFields;
const QStringList keys = group.keyList();
customFields.reserve(keys.count());
for (const QString &key : keys) {
CustomField field;
field.setKey(key);
field.setScope(CustomField::GlobalScope);
const QString value = group.readEntry(key, QString());
const int pos = value.indexOf(QLatin1Char(':'));
if (pos != -1) {
field.setType(CustomField::stringToType(value.left(pos - 1)));
field.setTitle(value.mid(pos + 1));
}
customFields << field;
}
return customFields;
}
/*
This file is part of Akonadi Contact.
Copyright (c) 2010 Tobias Koenig <tokoe@kde.org>
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
#ifndef CUSTOMFIELDMANAGER_P_H
#define CUSTOMFIELDMANAGER_P_H
#include "customfields_p.h"
/**
* @short A class that manages the descriptions of all custom fields with global scope.
*/
class CustomFieldManager
{
public:
static void setGlobalCustomFieldDescriptions(const CustomField::List &customFields);
static CustomField::List globalCustomFieldDescriptions();
};
#endif
/*
This file is part of Akonadi Contact.
Copyright (c) 2010 Tobias Koenig <tokoe@kde.org>
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
#include "customfields_p.h"
CustomField::CustomField()
: mType(TextType)
, mScope(LocalScope)
{
}
CustomField::CustomField(const QString &key, const QString &title, Type type, Scope scope)
: mKey(key)
, mTitle(title)
, mType(type)
, mScope(scope)
{
}
CustomField CustomField::fromVariantMap(const QVariantMap &map, Scope scope)
{
return CustomField(map.value(QStringLiteral("key")).toString(),
map.value(QStringLiteral("title")).toString(),
stringToType(map.value(QStringLiteral("type")).toString()),
scope);
}
void CustomField::setKey(const QString &key)
{
mKey = key;
}
QString CustomField::key() const
{
return mKey;
}
void CustomField::setTitle(const QString &title)
{
mTitle = title;
}
QString CustomField::title() const
{
return mTitle;
}
void CustomField::setType(Type type)
{
mType = type;
}
CustomField::Type CustomField::type() const
{
return mType;
}
void CustomField::setScope(Scope scope)
{
mScope = scope;
}
CustomField::Scope CustomField::scope() const
{
return mScope;
}
void CustomField::setValue(const QString &value)
{
mValue = value;
}
QString CustomField::value() const
{
return mValue;
}
QVariantMap CustomField::toVariantMap() const
{
QVariantMap map;
map.insert(QStringLiteral("key"), mKey);
map.insert(QStringLiteral("title"), mTitle);
map.insert(QStringLiteral("type"), typeToString(mType));
return map;
}
CustomField::Type CustomField::stringToType(const QString &type)
{
if (type == QLatin1String("text")) {
return CustomField::TextType;
}
if (type == QLatin1String("numeric")) {
return CustomField::NumericType;
}
if (type == QLatin1String("boolean")) {
return CustomField::BooleanType;
}
if (type == QLatin1String("date")) {
return CustomField::DateType;
}
if (type == QLatin1String("time")) {
return CustomField::TimeType;
}
if (type == QLatin1String("datetime")) {
return CustomField::DateTimeType;
}
if (type == QLatin1String("url")) {
return CustomField::UrlType;
}
return CustomField::TextType;
}
QString CustomField::typeToString(CustomField::Type type)
{
switch (type) {
case CustomField::TextType:
default:
return QStringLiteral("text");
case CustomField::NumericType:
return QStringLiteral("numeric");
case CustomField::BooleanType:
return QStringLiteral("boolean");
case CustomField::DateType:
return QStringLiteral("date");
case CustomField::TimeType:
return QStringLiteral("time");
case CustomField::DateTimeType:
return QStringLiteral("datetime");
case CustomField::UrlType:
return QStringLiteral("url");
}
}
/*
This file is part of Akonadi Contact.
Copyright (c) 2010 Tobias Koenig <tokoe@kde.org>
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
#ifndef CUSTOMFIELDS_P_H
#define CUSTOMFIELDS_P_H
#include <QString>
#include <QVariant>
template <typename T>
class QVector;
/**
* @short A class that represents non-standard contact fields.
*
* There exists three scopes of fields. To the local scope belong all
* custom fields that are defined by the user and that exists only for one