Commit 63fbfda5 authored by Julien Wadel's avatar Julien Wadel

Initial commit

parents
################################################################################
#
# Copyright (c) 2017-2020 Belledonne Communications SARL.
#
# This file is part of linphone-desktop
# (see https://www.linphone.org).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
################################################################################
cmake_minimum_required(VERSION 3.1)
project(linphone-app-contacts-api CXX)
include(GNUInstallDirs)
set(SOURCES src/ContactsImporterNetworkAPI.cpp
src/ContactsImporterDataAPI.cpp)
set(HEADERS src/ContactsImporterPlugin.hpp
src/ContactsImporterNetworkAPI.hpp
src/ContactsImporterDataAPI.hpp)
set(QT5_PACKAGES Core Widgets Network)
set(CMAKE_AUTOMOC ON)
find_package(Qt5 COMPONENTS ${QT5_PACKAGES} REQUIRED)
find_package(LinphoneCxx CONFIG)
#-------------------------------------------------------------------------------
# Install.
#-------------------------------------------------------------------------------
#add_custom_target(contactsPlugin ALL)
add_library(linphone-app-contacts-api STATIC ${SOURCES} ${HEADERS} )
target_link_libraries(linphone-app-contacts-api PRIVATE Qt5::Widgets Qt5::Network ${LINPHONECXX_LIBRARIES})
target_include_directories(linphone-app-contacts-api PRIVATE ${LINPHONECXX_INCLUDE_DIRS})
#install(FILES "src/ContactsImporterPlugin.hpp" DESTINATION "include/linphoneapp/contacts")
configure_file("src/ContactsImporterPlugin.hpp" "${CMAKE_INSTALL_PREFIX}/include/linphoneapp/contacts/ContactsImporterPlugin.hpp" COPYONLY)
configure_file("src/ContactsImporterNetworkAPI.hpp" "${CMAKE_INSTALL_PREFIX}/include/linphoneapp/contacts/ContactsImporterNetworkAPI.hpp" COPYONLY)
configure_file("src/ContactsImporterDataAPI.hpp" "${CMAKE_INSTALL_PREFIX}/include/linphoneapp/contacts/ContactsImporterDataAPI.hpp" COPYONLY)
set_target_properties(linphone-app-contacts-api PROPERTIES OUTPUT_NAME "linphoneAppContactsApi")
install(TARGETS linphone-app-contacts-api
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
)
#include "ContactsImporterDataAPI.hpp"
#include <linphone++/config.hh>
#include <QVariantMap>
#include <QJsonDocument>
// This class regroup Data interface for importing contacts
#include "ContactsImporterPlugin.hpp"
ContactsImporterDataAPI::ContactsImporterDataAPI(ContactsImporterPlugin * plugin, std::shared_ptr<linphone::Core> core) : mPlugin(plugin), mCore(core){
QVariantMap defaultValues;
QJsonDocument doc = QJsonDocument::fromJson(mPlugin->descriptionToJson().toUtf8());
QVariantMap description = doc.toVariant().toMap();
for(auto field : description["fields"].toList()){
auto details = field.toMap();
if( details.contains("fieldId") && details.contains("defaultData")){
mInputFields[details["fieldId"].toString()] = details["defaultData"].toString();
}
}
mInputFields["enabled"] = 0;
}
ContactsImporterDataAPI::~ContactsImporterDataAPI(){}
void ContactsImporterDataAPI::setInputFields(const QVariantMap &inputFields){
if(mInputFields != inputFields) {
mInputFields = inputFields;
if( isValid(false))
saveConfiguration();
emit inputFieldsChanged(mInputFields);
}
}
QVariantMap ContactsImporterDataAPI::getInputFields(){
return mInputFields;
}
//----------------------------- CONFIGURATION ---------------------------------------
void ContactsImporterDataAPI::setSectionConfiguration(const std::string& section){
mSectionConfigurationName = section;
}
void ContactsImporterDataAPI::loadConfiguration(){
if( mSectionConfigurationName != "") {
std::shared_ptr<linphone::Config> config = mCore->getConfig();
QVariantMap importData;
std::list<std::string> keys = config->getKeysNamesList(mSectionConfigurationName);
for(auto key : keys){
std::string value = config->getString(mSectionConfigurationName, key, "");
importData[QString::fromLocal8Bit(key.c_str(), int(key.size()))] = QString::fromLocal8Bit(value.c_str(), int(value.size()));
}
//Do not use setInputFields(importData); as we don't want to save the configuration
mInputFields = importData;
emit inputFieldsChanged(mInputFields);
}
}
void ContactsImporterDataAPI::saveConfiguration(){
if( mSectionConfigurationName != "") {
std::shared_ptr<linphone::Config> config = mCore->getConfig();
QVariantMap inputsToSave = getInputFields();
config->cleanSection(mSectionConfigurationName);// Remove fields that doesn't exist anymore (like temporary variables)
for(auto field = inputsToSave.begin() ; field != inputsToSave.end() ; ++field)
config->setString(mSectionConfigurationName, qPrintable(field.key()), qPrintable(field.value().toString()));
}
}
//----------------------------- -------------------------------------------------------
#ifndef CONTACTSIMPORTERDATAAPI_H
#define CONTACTSIMPORTERDATAAPI_H
#include <QVariantMap>
#include <linphone++/core.hh>
class ContactsImporterPlugin;
// This class regroup Data interface for importing contacts
class ContactsImporterDataAPI : public QObject {
Q_OBJECT
public:
ContactsImporterDataAPI(ContactsImporterPlugin * plugin, std::shared_ptr<linphone::Core> core);
virtual ~ContactsImporterDataAPI();
virtual bool isValid(const bool &pRequestData=true, QString * pError= nullptr) = 0; // Test if the passed data is valid. Used for saving.
virtual void setInputFields(const QVariantMap &inputFields);
virtual QVariantMap getInputFields();
virtual void importContacts()=0;
void setSectionConfiguration(const std::string& section);
virtual void loadConfiguration();
virtual void saveConfiguration();
signals:
void inputFieldsChanged(const QVariantMap &inputFields);
void errorMessage(const QString &message); // There are errors while requesting. Send a message status.
void statusMessage(const QString &message); // Send a message.
void contactsReceived(QVector<QMultiMap<QString,QString> > data);
protected:
QVariantMap mInputFields;
std::shared_ptr<linphone::Core> mCore;
ContactsImporterPlugin * mPlugin;
private:
std::string mSectionConfigurationName;
};
#endif // CONTACTSIMPORTDATAAPI_H
#include "ContactsImporterNetworkAPI.hpp"
#include <QObject>
#include <QtNetwork>
// This class is used to define network operation to retrieve Addresses from Network
ContactsImporterNetworkAPI::ContactsImporterNetworkAPI(){}
ContactsImporterNetworkAPI::~ContactsImporterNetworkAPI(){}
void ContactsImporterNetworkAPI::request(){ // Create QNetworkReply and make network requests
QNetworkRequest request(prepareRequest());
request.setAttribute(QNetworkRequest::FollowRedirectsAttribute, true);
mNetworkReply = mManager.get(request);
#if QT_CONFIG(ssl)
mNetworkReply->ignoreSslErrors();
#endif
QNetworkReply *data = mNetworkReply.data();
QObject::connect(data, &QNetworkReply::readyRead, this, &ContactsImporterNetworkAPI::handleReadyData);
QObject::connect(data, &QNetworkReply::finished, this, &ContactsImporterNetworkAPI::handleFinished);
QObject::connect(data, QNonConstOverload<QNetworkReply::NetworkError>::of(&QNetworkReply::error), this, &ContactsImporterNetworkAPI::handleError);
#if QT_CONFIG(ssl)
QObject::connect(data, &QNetworkReply::sslErrors, this, &ContactsImporterNetworkAPI::handleSslErrors);
#endif
}
void ContactsImporterNetworkAPI::handleReadyData(){
mBuffer.append(mNetworkReply->readAll());
}
void ContactsImporterNetworkAPI::handleFinished (){
if (mNetworkReply->error() == QNetworkReply::NoError){
mBuffer.append(mNetworkReply->readAll());
emit requestFinished(mBuffer);
}else
emit requestError(mNetworkReply->errorString());
mBuffer.clear();
}
void ContactsImporterNetworkAPI::handleError (QNetworkReply::NetworkError code) {
if (code != QNetworkReply::OperationCanceledError) {
QString url = mNetworkReply->url().host();
QString errorString = mNetworkReply->errorString();
qWarning() << QStringLiteral("Download of %1 failed: %2").arg(url).arg(errorString);
}
}
void ContactsImporterNetworkAPI::handleSslErrors (const QList<QSslError> &sslErrors){
#if QT_CONFIG(ssl)
for (const QSslError &error : sslErrors)
qWarning() << QStringLiteral("SSL error %1 : %2").arg(error.error()).arg(error.errorString());
#else
Q_UNUSED(sslErrors);
#endif
}
#ifndef CONTACTSIMPORTNETWORKAPI_H
#define CONTACTSIMPORTNETWORKAPI_H
#include <QObject>
#include <QtNetwork>
// This class is used to define network operation to retrieve Addresses from Network
class ContactsImporterNetworkAPI : public QObject
{
Q_OBJECT
public:
ContactsImporterNetworkAPI();
virtual ~ContactsImporterNetworkAPI();
virtual QString prepareRequest()const=0; // Called when requesting an Url.
void request();
QPointer<QNetworkReply> mNetworkReply;
QNetworkAccessManager mManager;
signals:
void requestFinished(const QByteArray &data); // The request is over and have data
void requestError(const QString &status); // There are errors while requesting. Send a message status.
void requestMessage(const QString &message); // Send a message.
private:
void handleReadyData();
void handleFinished ();
void handleError (QNetworkReply::NetworkError code);
void handleSslErrors (const QList<QSslError> &sslErrors);
QByteArray mBuffer;
};
#endif // CONTACTSIMPORTERNETWORKAPI_H
#ifndef CONTACTSIMPORTPLUGIN_H
#define CONTACTSIMPORTPLUGIN_H
#include <QtPlugin>
#include <QObject>
// Overload this class to make a plugin for the address book importer
#include <linphone++/core.hh>
class ContactsImporterDataAPI;
class ContactsImporterPlugin
{
public:
virtual ~ContactsImporterPlugin() {}
virtual QString descriptionToJson() const = 0;// Describe the plugin. Json are in Utf8
virtual ContactsImporterDataAPI * createInstance(std::shared_ptr<linphone::Core> core) = 0;
};
#define ContactsImporterPlugin_iid "linphone.ContactsImporterPlugin"
Q_DECLARE_INTERFACE(ContactsImporterPlugin, ContactsImporterPlugin_iid)
#endif // CONTACTSIMPORTPLUGIN_H
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment