Commit 38fed6a2 authored by Erwan Croze's avatar Erwan Croze 👋🏻

Adding C API + first test

parent ace63877
......@@ -86,7 +86,9 @@ set(C_API_HEADER_FILES
c-chat-room.h
c-dial-plan.h
c-event-log.h
c-magic-search.h
c-participant.h
c-search-result.h
c-types.h
)
......
......@@ -34,6 +34,8 @@
#include "linphone/api/c-dial-plan.h"
#include "linphone/api/c-event-log.h"
#include "linphone/api/c-participant.h"
#include "linphone/api/c-magic-search.h"
#include "linphone/api/c-search-result.h"
#include "linphone/api/c-types.h"
#endif // ifndef _L_C_API_H_
/*
* c-magic-search.h
* Copyright (C) 2010-2018 Belledonne Communications SARL
*
* 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 2
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef _L_C_MAGIC_SEARCH_H_
#define _L_C_MAGIC_SEARCH_H_
#include "linphone/api/c-types.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @addtogroup misc
* @{
*/
/**
* Constructs a LinphoneMagicSearch object
**/
LINPHONE_PUBLIC LinphoneMagicSearch *linphone_magic_search_new(LinphoneCore *lc);
/**
* Increment reference count of LinphoneMagicSearch object.
**/
LINPHONE_PUBLIC LinphoneMagicSearch *linphone_magic_search_ref(LinphoneMagicSearch *magicSearch);
/**
* Decrement reference count of LinphoneMagicSearch object. When dropped to zero, memory is freed.
**/
LINPHONE_PUBLIC void linphone_magic_search_unref(LinphoneMagicSearch *magicSearch);
/**
* Set the minimum value used to calculate the weight in search
* @param[in] weight minimum weight
**/
LINPHONE_PUBLIC void linphone_magic_search_set_min_weight(LinphoneMagicSearch *magicSearch, const unsigned int weight);
/**
* @return the minimum value used to calculate the weight in search
**/
LINPHONE_PUBLIC unsigned int linphone_magic_search_get_min_weight(const LinphoneMagicSearch *magicSearch);
/**
* Set the maximum value used to calculate the weight in search
* @param[in] weight maximum weight
**/
LINPHONE_PUBLIC void linphone_magic_search_set_max_weight(LinphoneMagicSearch *magicSearch, const unsigned int weight);
/**
* @return the maximum value used to calculate the weight in search
**/
LINPHONE_PUBLIC unsigned int linphone_magic_search_get_max_weight(const LinphoneMagicSearch *magicSearch);
/**
* @return the delimiter used to find matched filter word
**/
LINPHONE_PUBLIC const char *linphone_magic_search_get_delimiter(const LinphoneMagicSearch *magicSearch);
/**
* Set the delimiter used to find matched filter word
* @param[in] delimiter delimiter (example "-_.,")
**/
LINPHONE_PUBLIC void linphone_magic_search_set_delimiter(LinphoneMagicSearch *magicSearch, const char *delimiter);
/**
* @return the number of the maximum SearchResult which will be return
**/
LINPHONE_PUBLIC unsigned int linphone_magic_search_get_search_limit(const LinphoneMagicSearch *magicSearch);
/**
* Set the number of the maximum SearchResult which will be return
* @param[in] limit
**/
LINPHONE_PUBLIC void linphone_magic_search_set_search_limit(LinphoneMagicSearch *magicSearch, const unsigned int limit);
/**
* @return if the search is limited
**/
LINPHONE_PUBLIC bool_t linphone_magic_search_get_limited_search(const LinphoneMagicSearch *magicSearch);
/**
* Enable or disable the limited search
* @param[in] limited
**/
LINPHONE_PUBLIC void linphone_magic_search_set_limited_search(LinphoneMagicSearch *magicSearch, const bool_t limited);
/**
* Create a sorted list of SearchResult from SipUri, Contact name,
* Contact displayname, Contact phone number, which match with a filter word
* @param[in] filter word we search
* @param[in] withDomain domain which we want to search only
* @return sorted list of \bctbx_list{LinphoneSearchResult}
**/
LINPHONE_PUBLIC bctbx_list_t* linphone_magic_search_get_contact_list_from_filter(const LinphoneMagicSearch *magicSearch, const char *filter, const char *withDomain);
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif // _L_C_MAGIC_SEARCH_H_
/*
* c-search-result.h
* Copyright (C) 2010-2018 Belledonne Communications SARL
*
* 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 2
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef _L_C_SEARCH_RESULT_H_
#define _L_C_SEARCH_RESULT_H_
#include "linphone/api/c-types.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @addtogroup misc
* @{
*/
/**
* Increment reference count of LinphoneSearchResult object.
**/
LINPHONE_PUBLIC LinphoneSearchResult *linphone_search_result_ref(LinphoneSearchResult *searchResult);
/**
* Decrement reference count of LinphoneSearchResult object. When dropped to zero, memory is freed.
**/
LINPHONE_PUBLIC void linphone_search_result_unref(LinphoneSearchResult *searchResult);
/**
* @return LinphoneFriend associed
**/
LINPHONE_PUBLIC const LinphoneFriend* linphone_search_result_get_friend(LinphoneSearchResult *searchResult);
/**
* @return LinphoneAddress associed
**/
LINPHONE_PUBLIC const LinphoneAddress* linphone_search_result_get_address(LinphoneSearchResult *searchResult);
/**
* @return the result weight
**/
LINPHONE_PUBLIC unsigned int linphone_search_result_get_weight(LinphoneSearchResult *searchResult);
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif // _L_C_SEARCH_RESULT_H_
......@@ -150,12 +150,24 @@ typedef struct _LinphoneEventLog LinphoneEventLog;
**/
typedef struct _LinphoneDialPlan LinphoneDialPlan;
/**
* A LinphoneMagicSearch is used to do specifics searchs
* @ingroup misc
**/
typedef struct _LinphoneMagicSearch LinphoneMagicSearch;
/**
* The LinphoneParticipant object represents a participant of a conference.
* @ingroup misc
**/
typedef struct _LinphoneParticipant LinphoneParticipant;
/**
* The LinphoneSearchResult object represents a result of a search
* @ingroup misc
**/
typedef struct _LinphoneSearchResult LinphoneSearchResult;
// =============================================================================
// C Enums.
// =============================================================================
......
......@@ -21,7 +21,9 @@
#define _L_UTILS_H_
#include <ctime>
#include <list>
#include <string>
#include <utility>
#include <vector>
#include "linphone/utils/enum-generator.h"
......
......@@ -144,6 +144,10 @@ set(LINPHONE_CXX_OBJECTS_PRIVATE_HEADER_FILES
object/property-container.h
object/singleton.h
sal/sal.h
search/magic-search.h
search/magic-search-p.h
search/search-result.h
search/search-result-p.h
utils/background-task.h
utils/payload-type-handler.h
variant/variant.h
......@@ -167,7 +171,9 @@ set(LINPHONE_CXX_OBJECTS_SOURCE_FILES
c-wrapper/api/c-core.cpp
c-wrapper/api/c-dial-plan.cpp
c-wrapper/api/c-event-log.cpp
c-wrapper/api/c-magic-search.cpp
c-wrapper/api/c-participant.cpp
c-wrapper/api/c-search-result.cpp
c-wrapper/internal/c-sal.cpp
c-wrapper/internal/c-tools.cpp
call/call.cpp
......@@ -249,6 +255,8 @@ set(LINPHONE_CXX_OBJECTS_SOURCE_FILES
sal/refer-op.cpp
sal/register-op.cpp
sal/sal.cpp
search/magic-search.cpp
search/search-result.cpp
utils/background-task.cpp
utils/fs.cpp
utils/general.cpp
......
/*
* c-magic-search.cpp
* Copyright (C) 2010-2018 Belledonne Communications SARL
*
* 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 2
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "search/magic-search.h"
#include "c-wrapper/c-wrapper.h"
using namespace std;
L_DECLARE_C_OBJECT_IMPL(MagicSearch);
LinphoneMagicSearch *linphone_magic_search_new(LinphoneCore *lc) {
shared_ptr<LinphonePrivate::MagicSearch> cppPtr = make_shared<LinphonePrivate::MagicSearch>(L_GET_CPP_PTR_FROM_C_OBJECT(lc));
LinphoneMagicSearch *object = L_INIT(MagicSearch);
L_SET_CPP_PTR_FROM_C_OBJECT(object, cppPtr);
return object;
}
LinphoneMagicSearch *linphone_magic_search_ref(LinphoneMagicSearch *magicSearch) {
belle_sip_object_ref(magicSearch);
return magicSearch;
}
void linphone_magic_search_unref(LinphoneMagicSearch *magicSearch) {
belle_sip_object_unref(magicSearch);
}
void linphone_magic_search_set_min_weight(LinphoneMagicSearch *magicSearch, const unsigned int weight) {
L_GET_CPP_PTR_FROM_C_OBJECT(magicSearch)->setMinWeight(weight);
}
unsigned int linphone_magic_search_get_min_weight(const LinphoneMagicSearch *magicSearch) {
return L_GET_CPP_PTR_FROM_C_OBJECT(magicSearch)->getMinWeight();
}
void linphone_magic_search_set_max_weight(LinphoneMagicSearch *magicSearch, const unsigned int weight) {
L_GET_CPP_PTR_FROM_C_OBJECT(magicSearch)->setMaxWeight(weight);
}
unsigned int linphone_magic_search_get_max_weight(const LinphoneMagicSearch *magicSearch) {
return L_GET_CPP_PTR_FROM_C_OBJECT(magicSearch)->getMaxWeight();
}
const char *linphone_magic_search_get_delimiter(const LinphoneMagicSearch *magicSearch) {
return L_STRING_TO_C(L_GET_CPP_PTR_FROM_C_OBJECT(magicSearch)->getDelimiter());
}
void linphone_magic_search_set_delimiter(LinphoneMagicSearch *magicSearch, const char *delimiter) {
L_GET_CPP_PTR_FROM_C_OBJECT(magicSearch)->setDelimiter(L_C_TO_STRING(delimiter));
}
unsigned int linphone_magic_search_get_search_limit(const LinphoneMagicSearch *magicSearch) {
return L_GET_CPP_PTR_FROM_C_OBJECT(magicSearch)->getSearchLimit();
}
void linphone_magic_search_set_search_limit(LinphoneMagicSearch *magicSearch, const unsigned int limit) {
L_GET_CPP_PTR_FROM_C_OBJECT(magicSearch)->setSearchLimit(limit);
}
bool_t linphone_magic_search_get_limited_search(const LinphoneMagicSearch *magicSearch) {
return L_GET_CPP_PTR_FROM_C_OBJECT(magicSearch)->getLimitedSearch();
}
void linphone_magic_search_set_limited_search(LinphoneMagicSearch *magicSearch, const bool_t limited) {
L_GET_CPP_PTR_FROM_C_OBJECT(magicSearch)->setLimitedSearch(limited);
}
bctbx_list_t* linphone_magic_search_get_contact_list_from_filter(const LinphoneMagicSearch *magicSearch, const char *filter, const char *withDomain) {
return L_GET_RESOLVED_C_LIST_FROM_CPP_LIST(L_GET_CPP_PTR_FROM_C_OBJECT(magicSearch)->getContactListFromFilter(L_C_TO_STRING(filter), L_C_TO_STRING(withDomain)));
}
/*
* c-search-result.cpp
* Copyright (C) 2010-2018 Belledonne Communications SARL
*
* 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 2
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "search/search-result.h"
#include "c-wrapper/c-wrapper.h"
L_DECLARE_C_CLONABLE_OBJECT_IMPL(SearchResult);
LinphoneSearchResult *linphone_search_result_ref(LinphoneSearchResult *searchResult) {
belle_sip_object_ref(searchResult);
return searchResult;
}
void linphone_search_result_unref(LinphoneSearchResult *searchResult) {
belle_sip_object_unref(searchResult);
}
const LinphoneFriend* linphone_search_result_get_friend(LinphoneSearchResult *searchResult) {
return L_GET_CPP_PTR_FROM_C_OBJECT(searchResult)->getFriend();
}
const LinphoneAddress* linphone_search_result_get_address(LinphoneSearchResult *searchResult) {
return L_GET_CPP_PTR_FROM_C_OBJECT(searchResult)->getAddress();
}
unsigned int linphone_search_result_get_weight(LinphoneSearchResult *searchResult) {
return L_GET_CPP_PTR_FROM_C_OBJECT(searchResult)->getWeight();
}
......@@ -39,8 +39,10 @@
F(Core, Core) \
F(DialPlan, DialPlan) \
F(EventLog, EventLog) \
F(MagicSearch, MagicSearch) \
F(MediaSessionParams, CallParams) \
F(Participant, Participant)
F(Participant, Participant) \
F(SearchResult, SearchResult)
#define L_REGISTER_SUBTYPES(F) \
F(AbstractChatRoom, BasicChatRoom) \
......
/*
* magic-search-p.h
* Copyright (C) 2010-2018 Belledonne Communications SARL
*
* 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 2
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef _L_MAGIC_SEARCH_P_H_
#define _L_MAGIC_SEARCH_P_H_
#include "magic-search.h"
#include "object/object-p.h"
LINPHONE_BEGIN_NAMESPACE
class MagicSearchPrivate : public ObjectPrivate{
private:
unsigned int mMaxWeight;
unsigned int mMinWeight;
unsigned int mSearchLimit; // Number of ResultSearch maximum when the search is limited
bool mLimitedSearch; // Limit the search
std::string mDelimiter; // Delimiter use for the search
L_DECLARE_PUBLIC(MagicSearch);
};
LINPHONE_END_NAMESPACE
#endif //_L_MAGIC_SEARCH_P_H_
/*
* magic-search.cpp
* Copyright (C) 2010-2018 Belledonne Communications SARL
*
* 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 2
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "magic-search-p.h"
#include <bctoolbox/list.h>
#include "c-wrapper/internal/c-tools.h"
#include "linphone/utils/utils.h"
#include "linphone/core.h"
#include "linphone/types.h"
#include "private.h"
using namespace std;
LINPHONE_BEGIN_NAMESPACE
MagicSearch::MagicSearch(const std::shared_ptr<Core> &core) : CoreAccessor(core), Object(*new MagicSearchPrivate){
L_D();
d->mMinWeight = 0;
d->mMaxWeight = 1000;
d->mSearchLimit = 10;
d->mLimitedSearch = true;
d->mDelimiter = "+_-";
}
void MagicSearch::setMinWeight(const unsigned int weight) {
L_D();
d->mMinWeight = weight;
}
unsigned int MagicSearch::getMinWeight() const {
L_D();
return d->mMinWeight;
}
void MagicSearch::setMaxWeight(const unsigned int weight) {
L_D();
d->mMaxWeight = weight;
}
unsigned int MagicSearch::getMaxWeight() const {
L_D();
return d->mMaxWeight;
}
const string &MagicSearch::getDelimiter() const {
L_D();
return d->mDelimiter;
}
void MagicSearch::setDelimiter(const string &delimiter) {
L_D();
d->mDelimiter = delimiter;
}
unsigned int MagicSearch::getSearchLimit() const {
L_D();
return d->mSearchLimit;
}
void MagicSearch::setSearchLimit(const unsigned int limit) {
L_D();
d->mSearchLimit = limit;
}
bool MagicSearch::getLimitedSearch() const {
L_D();
return d->mLimitedSearch;
}
void MagicSearch::setLimitedSearch(const bool limited) {
L_D();
d->mLimitedSearch = limited;
}
list<SearchResult> MagicSearch::getContactListFromFilter(const string &filter, const string &withDomain) const {
list<SearchResult> resultList = list<SearchResult>();
LinphoneFriendList* list = linphone_core_get_default_friend_list(this->getCore()->getCCore());
// For all friends or when we reach the search limit
for (bctbx_list_t *f = list->friends ;
f != nullptr && (!getLimitedSearch() || resultList.size() < getSearchLimit());
f = bctbx_list_next(f)
) {
unsigned int weight = getMinWeight();
LinphoneFriend* lFriend = reinterpret_cast<LinphoneFriend*>(f->data);
const LinphoneAddress* lAddress = linphone_friend_get_address(lFriend);
if (lAddress == nullptr) break;
// Check domain
if (!withDomain.empty() &&
withDomain.compare(linphone_address_get_domain(lAddress)) != 0
) break;
// NAME
if (linphone_friend_get_name(lFriend) != nullptr) {
weight += getWeight(linphone_friend_get_name(lFriend), filter);
}
// PHONE NUMBER
bctbx_list_t *phoneNumbers = linphone_friend_get_phone_numbers(lFriend);
while (phoneNumbers != nullptr && phoneNumbers->data != nullptr) {
string number = static_cast<const char*>(phoneNumbers->data);
weight += getWeight(number, filter);
phoneNumbers = phoneNumbers->next;
}
// SIPURI
if (linphone_address_get_username(lAddress) != nullptr) {
weight += getWeight(linphone_address_get_username(lAddress), filter);
}
// DISPLAYNAME
if (linphone_address_get_display_name(lAddress) != nullptr) {
weight += getWeight(linphone_address_get_display_name(lAddress), filter);
}
resultList.push_back(SearchResult(weight, lAddress, lFriend));
}
resultList.sort();
return resultList;
}
unsigned int MagicSearch::getWeight(const string &stringWords, const string &filter) const {
size_t weight = string::npos;
// Finding all occurrences of "filter" in "stringWords"
for (size_t w = stringWords.find(filter) ; w != string::npos ; w = stringWords.find(filter, w)) {
// weight max if occurence find at beginning
if (w == 0) {
weight = getMaxWeight();
} else {
bool isDelimiter = false;
// get the char before the matched filter
const char l = stringWords.at(w - 1);
// Check if it's a delimiter
for (const char d : getDelimiter()) {
if (l == d) {
isDelimiter = true;
break;
}
}
unsigned int newWeight = getMaxWeight() - (unsigned int)((isDelimiter) ? 1 : w + 1);
weight = (weight != string::npos) ? weight + newWeight : newWeight;
}
}
return (weight != string::npos) ? (unsigned int)(weight) : getMinWeight();
}
MagicSearch::~MagicSearch() {}
LINPHONE_END_NAMESPACE
/*
* magic-search.h
* Copyright (C) 2010-2018 Belledonne Communications SARL
*
* 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 2
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef _L_MAGIC_SEARCH_H_
#define _L_MAGIC_SEARCH_H_
#include <string>
#include <list>
#include <memory>
#include "core/core.h"
#include "core/core-accessor.h"
#include "search-result.h"
LINPHONE_BEGIN_NAMESPACE
class MagicSearchPrivate;
class LINPHONE_PUBLIC MagicSearch : public CoreAccessor, public Object{
public:
MagicSearch(const std::shared_ptr<Core> &core);
~MagicSearch();
/**
* Set the minimum value used to calculate the weight in search
* @param[in] weight minimum weight
**/
void setMinWeight(const unsigned int weight);
/**
* @return the minimum value used to calculate the weight in search
**/
unsigned int getMinWeight() const;
/**
* Set the maximum value used to calculate the weight in search
* @param[in] weight maximum weight
**/
void setMaxWeight(const unsigned int weight);
/**
* @return the maximum value used to calculate the weight in search
**/
unsigned int getMaxWeight() const;
/**
* @return the delimiter used to find matched filter word
**/
const std::string& getDelimiter() const;
/**
* Set the delimiter used to find matched filter word
* @param[in] delimiter delimiter (example "-_.,")
**/
void setDelimiter(const std::string &delimiter);
/**
* @return the number of the maximum SearchResult which will be return
**/
unsigned int getSearchLimit() const;
/**
* Set the number of the maximum SearchResult which will be return
* @param[in] limit
**/
void setSearchLimit(const unsigned int limit);
/**
* @return if the search is limited
**/
bool getLimitedSearch() const;
/**
* Enable or disable the limited search
* @param[in] limited
**/
void setLimitedSearch(const bool limited);
/**
* Create a sorted list of SearchResult from SipUri, Contact name,
* Contact displayname, Contact phone number, which match with a filter word
* @param[in] filter word we search
* @param[in] withDomain domain which we want to search only
* @return sorted list of SearchResult with "filter"
**/
std::list<SearchResult> getContactListFromFilter(const std::string &filter, const std::string &withDomain = "") const;
private: