Commit 16b2dc84 authored by Laurent Montel's avatar Laurent Montel 😁

Create plugin for contact editor

parent 2b4c0d07
......@@ -84,7 +84,7 @@ set(KPIMTEXTEDIT_LIB_VERSION "5.6.40")
set(KIMAP_LIB_VERSION "5.6.40")
set(LIBKSIEVE_LIB_VERSION_LIB "5.6.40")
set(KMAILTRANSPORT_LIB_VERSION "5.6.40")
set(AKONADICONTACT_LIB_VERSION "5.6.40")
set(AKONADICONTACT_LIB_VERSION "5.6.41")
set(IMPORTWIZARD_LIB_VERSION "5.6.40")
set(MAILIMPORTER_LIB_VERSION "5.6.40")
......
add_definitions(-DTRANSLATION_DOMAIN=\"kaddressbook_contacteditor\")
set(contacteditor_addresslocationeditor_plugin_SRCS
addresseditor/addresseslocationgrantleeformater.cpp
addresseditor/addresseslocationwidget.cpp
addresseditor/addressgrantleeobject.cpp
addresseditor/addresslocationwidget.cpp
addresseditor/addresstypedialog.cpp
addresseditor/iconnamecache.cpp
addresseditor/selectaddresstypecombobox.cpp
addresseditor/webengine/addresseslocationenginepage.cpp
addresseditor/webengine/addresseslocationengineviewer.cpp
)
ecm_qt_declare_logging_category(contacteditor_addresslocationeditor_plugin_SRCS HEADER addresslocationeditorplugin_debug.h IDENTIFIER
ADDRESSLOCATIONEDITORPLUGIN_LOG CATEGORY_NAME org.kde.pim.addresslocationeditorplugin)
#if(BUILD_TESTING)
# add_subdirectory(addresseditor/tests)
# add_subdirectory(addresseditor/autotests)
#endif()
set(addresslocationeditorplugin_SRCS
addresslocationeditorplugin.cpp
${contacteditor_addresslocationeditor_plugin_SRCS}
)
kcoreaddons_add_plugin(addresslocationeditorplugin JSON addresslocationeditorplugin.json SOURCES
${addresslocationeditorplugin_SRCS} INSTALL_NAMESPACE contacteditor)
target_link_libraries(addresslocationeditorplugin
KF5::I18n
KF5::AkonadiContact
KF5::ContactEditor
Grantlee5::Templates
Qt5::WebEngine
Qt5::WebEngineWidgets
KF5::IconThemes
KF5::Completion
)
add_subdirectory(addresseditor/html)
#! /bin/sh
$XGETTEXT `find . -name '*.cpp' -o -name '*.h' | grep -v '/tests/' | grep -v '/autotests/'` -o $podir/kaddressbook_contacteditor.pot
/*
This file is part of Contact Editor.
Copyright (C) 2016 eyeOS S.L.U., a Telefonica company, sales@eyeos.com
Copyright (C) 2016-2017 Laurent Montel <laurent.montel@kdab.com>
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 "addresseslocationgrantleeformater.h"
#include "addressgrantleeobject.h"
#include <grantlee/engine.h>
#include <QVariantList>
#include <QStandardPaths>
AddressesLocationGrantleeFormater::AddressesLocationGrantleeFormater(QObject *parent)
: QObject(parent)
, mEngine(new Grantlee::Engine)
, mReadOnly(false)
{
mTemplateLoader = QSharedPointer<Grantlee::FileSystemTemplateLoader>(new Grantlee::FileSystemTemplateLoader);
mGrantleeThemePath = QStandardPaths::locate(QStandardPaths::GenericDataLocation,
QStringLiteral("contacteditor/grantleetheme/default/"),
QStandardPaths::LocateDirectory);
changeGrantleePath(mGrantleeThemePath);
}
AddressesLocationGrantleeFormater::~AddressesLocationGrantleeFormater()
{
delete mEngine;
mTemplateLoader.clear();
}
QString AddressesLocationGrantleeFormater::formatAddresses(const KContacts::Address::List &addresses)
{
QVariantList addressList;
const int nbAddress(addresses.count());
addressList.reserve(nbAddress);
QList<AddressGrantleeObject *> lstAddress;
lstAddress.reserve(nbAddress);
for (int i = 0; i < nbAddress; ++i) {
AddressGrantleeObject *addressObj = new AddressGrantleeObject(addresses.at(i), i);
addressList << QVariant::fromValue(static_cast<QObject *>(addressObj));
lstAddress.append(addressObj);
}
QVariantHash addressHash;
addressHash.insert(QStringLiteral("addresses"), addressList);
addressHash.insert(QStringLiteral("absoluteThemePath"), mGrantleeThemePath);
addressHash.insert(QStringLiteral("readOnly"), mReadOnly);
Grantlee::Context context(addressHash);
const QString contentHtml = mSelfcontainedTemplate->render(&context);
qDeleteAll(lstAddress);
return contentHtml;
}
void AddressesLocationGrantleeFormater::setReadOnly(bool readOnly)
{
mReadOnly = readOnly;
}
void AddressesLocationGrantleeFormater::changeGrantleePath(const QString &path)
{
if (!mTemplateLoader) {
mTemplateLoader = QSharedPointer<Grantlee::FileSystemTemplateLoader>(new Grantlee::FileSystemTemplateLoader);
}
mTemplateLoader->setTemplateDirs(QStringList() << path);
mEngine->addTemplateLoader(mTemplateLoader);
mSelfcontainedTemplate = mEngine->loadByName(QStringLiteral("addresseslocation.html"));
if (mSelfcontainedTemplate->error()) {
mErrorMessage += mSelfcontainedTemplate->errorString() + QLatin1String("<br>");
}
}
/*
This file is part of Contact Editor.
Copyright (C) 2016 eyeOS S.L.U., a Telefonica company, sales@eyeos.com
Copyright (C) 2016-2017 Laurent Montel <laurent.montel@kdab.com>
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 ADDRESSESLOCATIONGRANTLEEFORMATER_H
#define ADDRESSESLOCATIONGRANTLEEFORMATER_H
#include <QObject>
#include <KContacts/Address>
#include <QSharedPointer>
#include <grantlee/templateloader.h>
namespace Grantlee {
class Engine;
}
class AddressesLocationGrantleeFormater : public QObject
{
Q_OBJECT
public:
explicit AddressesLocationGrantleeFormater(QObject *parent = nullptr);
~AddressesLocationGrantleeFormater();
QString formatAddresses(const KContacts::Address::List &addresses);
void setReadOnly(bool readOnly);
private:
void changeGrantleePath(const QString &path);
Grantlee::Engine *mEngine = nullptr;
QString mErrorMessage;
QString mGrantleeThemePath;
QSharedPointer<Grantlee::FileSystemTemplateLoader> mTemplateLoader;
Grantlee::Template mSelfcontainedTemplate;
bool mReadOnly;
};
#endif // ADDRESSESLOCATIONGRANTLEEFORMATER_H
/*
This file is part of Contact Editor.
Copyright (C) 2016 eyeOS S.L.U., a Telefonica company, sales@eyeos.com
Copyright (C) 2016-2017 Laurent Montel <laurent.montel@kdab.com>
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 "addresseslocationwidget.h"
#include "webengine/addresseslocationengineviewer.h"
#include "addresslocationwidget.h"
AddressesLocationWidget::AddressesLocationWidget(QWidget *parent)
: QSplitter(parent)
{
setChildrenCollapsible(false);
mAddressLocationWidget = new AddressLocationWidget(this);
mAddressLocationWidget->setObjectName(QStringLiteral("addresslocationwidget"));
addWidget(mAddressLocationWidget);
mAddressesLocationViewer = new AddressesLocationEngineViewer(this);
mAddressesLocationViewer->setObjectName(QStringLiteral("addresseslocationviewer"));
addWidget(mAddressesLocationViewer);
connect(mAddressesLocationViewer, &AddressesLocationEngineViewer::modifyAddress, mAddressLocationWidget, &AddressLocationWidget::slotModifyAddress);
connect(mAddressLocationWidget, &AddressLocationWidget::addNewAddress, mAddressesLocationViewer, &AddressesLocationEngineViewer::addAddress);
connect(mAddressLocationWidget, &AddressLocationWidget::updateAddressCanceled, mAddressesLocationViewer, &AddressesLocationEngineViewer::updateAddressCanceled);
connect(mAddressLocationWidget, &AddressLocationWidget::updateAddress, mAddressesLocationViewer, &AddressesLocationEngineViewer::replaceAddress);
setSizes(QList<int>() << 400 << 300);
}
AddressesLocationWidget::~AddressesLocationWidget()
{
}
void AddressesLocationWidget::loadContact(const KContacts::Addressee &contact)
{
mAddressesLocationViewer->setAddresses(contact.addresses());
}
void AddressesLocationWidget::storeContact(KContacts::Addressee &contact) const
{
// delete all previous addresses
const KContacts::Address::List oldAddresses = contact.addresses();
const int numberOfOldAddresses(oldAddresses.count());
for (int i = 0; i < numberOfOldAddresses; ++i) {
contact.removeAddress(oldAddresses.at(i));
}
const KContacts::Address::List addressList = mAddressesLocationViewer->addresses();
// insert the new ones
const int numberOfAddress(addressList.count());
for (int i = 0; i < numberOfAddress; ++i) {
const KContacts::Address address(addressList.at(i));
if (!address.isEmpty()) {
contact.insertAddress(address);
}
}
}
void AddressesLocationWidget::setReadOnly(bool readOnly)
{
mAddressesLocationViewer->setReadOnly(readOnly);
mAddressLocationWidget->setReadOnly(readOnly);
}
/*
This file is part of Contact Editor.
Copyright (C) 2016 eyeOS S.L.U., a Telefonica company, sales@eyeos.com
Copyright (C) 2016-2017 Laurent Montel <laurent.montel@kdab.com>
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 ADDRESSESLOCATIONWIDGET_H
#define ADDRESSESLOCATIONWIDGET_H
#include <QSplitter>
#include <KContacts/Addressee>
class AddressLocationWidget;
class AddressesLocationEngineViewer;
class AddressesLocationWidget : public QSplitter
{
Q_OBJECT
public:
explicit AddressesLocationWidget(QWidget *parent = nullptr);
~AddressesLocationWidget();
void loadContact(const KContacts::Addressee &contact);
void storeContact(KContacts::Addressee &contact) const;
void setReadOnly(bool readOnly);
private:
AddressesLocationEngineViewer *mAddressesLocationViewer = nullptr;
AddressLocationWidget *mAddressLocationWidget = nullptr;
};
#endif // ADDRESSESLOCATIONWIDGET_H
/*
This file is part of Contact Editor.
Copyright (C) 2016 eyeOS S.L.U., a Telefonica company, sales@eyeos.com
Copyright (C) 2016-2017 Laurent Montel <laurent.montel@kdab.com>
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 "addressgrantleeobject.h"
#include "iconnamecache_p.h"
#include <KIconLoader>
#include <KLocalizedString>
#include <QUrlQuery>
AddressGrantleeObject::AddressGrantleeObject(const KContacts::Address &address, int addressIndex, QObject *parent)
: QObject(parent)
, mAddress(address)
, mAddressIndex(addressIndex)
{
mIconSize = KIconLoader::global()->currentSize(KIconLoader::Small);
}
AddressGrantleeObject::~AddressGrantleeObject()
{
}
QString AddressGrantleeObject::formattedAddress() const
{
QString str;
if (mAddress.label().isEmpty()) {
str = mAddress.formattedAddress().trimmed().toHtmlEscaped();
} else {
str = mAddress.label().toHtmlEscaped();
}
return str;
}
QString AddressGrantleeObject::createActionUrl(const QString &actionName) const
{
QUrl url;
url.setScheme(QStringLiteral("addresslocationaction"));
url.setPath(actionName);
QUrlQuery query;
query.addQueryItem(QStringLiteral("id"), QString::number(mAddressIndex));
url.setQuery(query);
return url.toDisplayString();
}
QString AddressGrantleeObject::preferredAddressAction() const
{
QString text;
if (preferredAddress()) {
const QString iconPath = QUrl::fromLocalFile(IconNameCache::instance()->iconPath(QStringLiteral("bookmarks"), KIconLoader::Small)).url();
text = QStringLiteral("<img class=\"headimage\" height=\"%3\" width=\"%3\" title=\"%1\" src=\"%2\"></a>\n")
.arg(i18n("Remove Address"), iconPath, QString::number(mIconSize));
}
return text;
}
QString AddressGrantleeObject::postOfficeBox() const
{
return mAddress.postOfficeBox();
}
QString AddressGrantleeObject::street() const
{
return mAddress.street();
}
QString AddressGrantleeObject::locality() const
{
return mAddress.locality();
}
QString AddressGrantleeObject::region() const
{
return mAddress.region();
}
QString AddressGrantleeObject::postalCode() const
{
return mAddress.postalCode();
}
QString AddressGrantleeObject::country() const
{
return mAddress.country();
}
QString AddressGrantleeObject::removeAddressAction() const
{
const QString iconPath = QUrl::fromLocalFile(IconNameCache::instance()->iconPath(QStringLiteral("edit-delete"), KIconLoader::Small)).url();
const QString text = QStringLiteral("<a href=\"%1\"><img class=\"headimage\" height=\"%4\" width=\"%4\" title=\"%3\" src=\"%2\"></a>\n")
.arg(createActionUrl(QStringLiteral("removeaddress")), iconPath, i18n("Remove Address"), QString::number(mIconSize));
return text;
}
QString AddressGrantleeObject::modifyAddressAction() const
{
const QString iconPath = QUrl::fromLocalFile(IconNameCache::instance()->iconPath(QStringLiteral("document-edit"), KIconLoader::Small)).url();
const QString text = QStringLiteral("<a href=\"%1\"><img class=\"headimage\" height=\"%4\" width=\"%4\" title=\"%3\" src=\"%2\"></a>\n")
.arg(createActionUrl(QStringLiteral("editaddress")), iconPath, i18n("Edit Address"), QString::number(mIconSize));
return text;
}
QString AddressGrantleeObject::addressType() const
{
return KContacts::Address::typeLabel(mAddress.type());
}
bool AddressGrantleeObject::preferredAddress() const
{
return mAddress.type() & KContacts::Address::Pref;
}
/*
This file is part of Contact Editor.
Copyright (C) 2016 eyeOS S.L.U., a Telefonica company, sales@eyeos.com
Copyright (C) 2016-2017 Laurent Montel <laurent.montel@kdab.com>
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 ADDRESSGRANTLEEOBJECT_H
#define ADDRESSGRANTLEEOBJECT_H
#include <QObject>
#include <KContacts/Address>
class AddressGrantleeObject : public QObject
{
Q_OBJECT
Q_PROPERTY(QString removeAddressAction READ removeAddressAction)
Q_PROPERTY(QString modifyAddressAction READ modifyAddressAction)
Q_PROPERTY(QString formattedAddress READ formattedAddress)
Q_PROPERTY(QString preferredAddressAction READ preferredAddressAction)
Q_PROPERTY(QString addressType READ addressType)
Q_PROPERTY(QString postOfficeBox READ postOfficeBox)
Q_PROPERTY(QString street READ street)
Q_PROPERTY(QString locality READ locality)
Q_PROPERTY(QString region READ region)
Q_PROPERTY(QString postalCode READ postalCode)
Q_PROPERTY(QString country READ country)
Q_PROPERTY(bool preferredAddress READ preferredAddress)
public:
explicit AddressGrantleeObject(const KContacts::Address &address, int addressIndex, QObject *parent = nullptr);
~AddressGrantleeObject();
QString removeAddressAction() const;
QString modifyAddressAction() const;
QString formattedAddress() const;
QString addressType() const;
bool preferredAddress() const;
QString preferredAddressAction() const;
QString postOfficeBox() const;
QString street() const;
QString locality() const;
QString region() const;
QString postalCode() const;
QString country() const;
private:
QString createActionUrl(const QString &actionName) const;
KContacts::Address mAddress;
int mAddressIndex;
int mIconSize;
};
#endif // ADDRESSGRANTLEEOBJECT_H
/*
This file is part of Contact Editor.
Copyright (C) 2016 eyeOS S.L.U., a Telefonica company, sales@eyeos.com
Copyright (C) 2016-2017 Laurent Montel <laurent.montel@kdab.com>
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 ADDRESSLOCATIONWIDGET_H
#define ADDRESSLOCATIONWIDGET_H
#include <QWidget>
#include <KContacts/Address>
class QStackedWidget;
class QCheckBox;
class KLineEdit;
class KComboBox;
class QPushButton;
class SelectAddressTypeComboBox;
class AddressLocationWidget : public QWidget
{
Q_OBJECT
public:
enum Mode {
CreateAddress = 0,
ModifyAddress
};
explicit AddressLocationWidget(QWidget *parent = nullptr);
~AddressLocationWidget();
void setAddress(const KContacts::Address &address);
KContacts::Address address() const;
void slotModifyAddress(const KContacts::Address &address, int currentIndex);
void clear();
void setReadOnly(bool readOnly);
Q_SIGNALS:
void addNewAddress(const KContacts::Address &address);
void updateAddress(const KContacts::Address &address, int index);
void updateAddressCanceled();
private:
void slotAddAddress();
void slotUpdateAddress();
void slotCancelModifyAddress();
void fillCountryCombo();
void switchMode();
void reset();
KContacts::Address mAddress;
QCheckBox *mPreferredCheckBox = nullptr;
KLineEdit *mPOBoxEdit = nullptr;
KLineEdit *mLocalityEdit = nullptr;
KLineEdit *mRegionEdit = nullptr;
KLineEdit *mPostalCodeEdit = nullptr;
KLineEdit *mStreetEdit = nullptr;
KComboBox *mCountryCombo = nullptr;
QPushButton *mAddAddress = nullptr;
QPushButton *mModifyAddress = nullptr;
QPushButton *mCancelAddress = nullptr;
QStackedWidget *mButtonStack = nullptr;
SelectAddressTypeComboBox *mTypeCombo = nullptr;
int mCurrentAddress;
Mode mCurrentMode;
};
#endif // ADDRESSLOCATIONWIDGET_H
/*
This file is part of Contact Editor.
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.
*/
#include "addresstypedialog.h"
#include <KLocalizedString>
#include <QVBoxLayout>
#include <QPushButton>
#include <QDialogButtonBox>
#include <QGroupBox>
#include <QCheckBox>
#include <QButtonGroup>
AddressTypeDialog::AddressTypeDialog(KContacts::Address::Type type, QWidget *parent)
: QDialog(parent)
{
QVBoxLayout *mainLayout = new QVBoxLayout(this);
setWindowTitle(i18nc("street/postal", "Edit Address Type"));