From 34b2882e3831f9b176a88a4f99618e7058a9f5bd Mon Sep 17 00:00:00 2001 From: Ronan Abhamon Date: Thu, 26 Oct 2017 10:08:53 +0200 Subject: [PATCH] feat(Core): use chat room api from new core --- coreapi/callbacks.c | 16 +- coreapi/chat.c | 100 ++--------- coreapi/linphonecore.c | 70 ++------ coreapi/private.h | 18 +- coreapi/tester_utils.h | 3 +- include/linphone/utils/utils.h | 1 + src/c-wrapper/api/c-chat-room.cpp | 16 +- src/chat/chat-room/basic-chat-room.cpp | 3 +- src/chat/chat-room/chat-room.h | 1 + src/chat/chat-room/client-group-chat-room.cpp | 14 +- .../chat-room/real-time-text-chat-room.cpp | 5 +- src/core/core-p.h | 12 +- src/core/core.cpp | 165 +++++++++++++++--- src/core/core.h | 21 ++- src/core/paths/paths-android.cpp | 19 +- src/core/paths/paths-android.h | 1 + src/core/paths/paths-apple.h | 1 + src/core/paths/paths-linux.cpp | 24 ++- src/core/paths/paths-linux.h | 1 + src/core/paths/paths-windows.cpp | 6 +- src/core/paths/paths-windows.h | 1 + src/core/paths/paths.cpp | 16 +- src/core/paths/paths.h | 2 +- .../platform-helpers/platform-helpers.cpp | 6 +- src/db/main-db-p.h | 2 + src/db/main-db.cpp | 95 ++++++---- src/db/main-db.h | 5 +- src/utils/utils.cpp | 4 + tester/main-db-tester.cpp | 10 +- 29 files changed, 359 insertions(+), 279 deletions(-) diff --git a/coreapi/callbacks.c b/coreapi/callbacks.c index b1dd16297..ba4cea411 100644 --- a/coreapi/callbacks.c +++ b/coreapi/callbacks.c @@ -45,6 +45,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include "conference/session/call-session.h" #include "conference/session/media-session-p.h" #include "conference/session/media-session.h" +#include "core/core-p.h" using namespace LinphonePrivate; @@ -724,7 +725,7 @@ static void refer_received(SalOp *op, const SalAddress *refer_to){ LinphoneCore *lc = reinterpret_cast(op->get_sal()->get_user_pointer()); if (addr.hasUriParam("method") && (addr.getUriParamValue("method") == "BYE")) { // The server asks a participant to leave a chat room - LinphoneChatRoom *cr = _linphone_core_find_group_chat_room(lc, addr.asStringUriOnly().c_str()); + LinphoneChatRoom *cr = L_GET_C_BACK_PTR(lc->cppCore->findChatRoom(addr)); if (cr) { L_GET_CPP_PTR_FROM_C_OBJECT(cr)->leave(); static_cast(op)->reply(SalReasonNone); @@ -732,7 +733,7 @@ static void refer_received(SalOp *op, const SalAddress *refer_to){ } static_cast(op)->reply(SalReasonDeclined); } else if (addr.hasParam("admin")) { - LinphoneChatRoom *cr = _linphone_core_find_group_chat_room(lc, op->get_to()); + LinphoneChatRoom *cr = L_GET_C_BACK_PTR(lc->cppCore->findChatRoom(Address(op->get_to()))); if (cr) { Address fromAddr(op->get_from()); std::shared_ptr participant = L_GET_CPP_PTR_FROM_C_OBJECT(cr)->findParticipant(fromAddr); @@ -749,11 +750,12 @@ static void refer_received(SalOp *op, const SalAddress *refer_to){ return; } } else { - LinphoneChatRoom *cr = _linphone_core_join_client_group_chat_room(lc, addr); - if (cr) { - static_cast(op)->reply(SalReasonNone); - return; - } + LinphoneChatRoom *cr = _linphone_client_group_chat_room_new(lc, addr.asString().c_str(), nullptr); + L_GET_CPP_PTR_FROM_C_OBJECT(cr)->join(); + L_GET_PRIVATE(lc->cppCore)->insertChatRoomWithDb(L_GET_CPP_PTR_FROM_C_OBJECT(cr)); + + static_cast(op)->reply(SalReasonNone); + return; } } } diff --git a/coreapi/chat.c b/coreapi/chat.c index dad0c6940..75a4e9636 100644 --- a/coreapi/chat.c +++ b/coreapi/chat.c @@ -38,9 +38,10 @@ #include "c-wrapper/c-wrapper.h" #include "chat/chat-room/basic-chat-room.h" #include "chat/chat-room/client-group-chat-room.h" -#include "chat/chat-room/real-time-text-chat-room.h" #include "chat/chat-room/real-time-text-chat-room-p.h" +#include "chat/chat-room/real-time-text-chat-room.h" #include "content/content-type.h" +#include "core/core.h" using namespace std; @@ -56,14 +57,11 @@ bool_t linphone_core_chat_enabled(const LinphoneCore *lc) { return lc->chat_deny_code != LinphoneReasonNone; } -const bctbx_list_t *linphone_core_get_chat_rooms(LinphoneCore *lc) { - return lc->chatrooms; -} - -static LinphoneChatRoom *_linphone_core_create_chat_room(LinphoneCore *lc, const LinphoneAddress *addr) { - LinphoneChatRoom *cr = linphone_chat_room_new(lc, addr); - lc->chatrooms = bctbx_list_append(lc->chatrooms, (void *)cr); - return cr; +const bctbx_list_t *linphone_core_get_chat_rooms (LinphoneCore *lc) { + if (lc->chat_rooms) + bctbx_list_free_with_data(lc->chat_rooms, (bctbx_list_free_func)linphone_chat_room_unref); + lc->chat_rooms = L_GET_RESOLVED_C_LIST_FROM_CPP_LIST(lc->cppCore->getChatRooms()); + return lc->chat_rooms; } LinphoneChatRoom *_linphone_core_create_chat_room_from_call(LinphoneCall *call){ @@ -73,91 +71,25 @@ LinphoneChatRoom *_linphone_core_create_chat_room_from_call(LinphoneCall *call){ return cr; } -static LinphoneChatRoom *_linphone_core_create_chat_room_from_url(LinphoneCore *lc, const char *to) { - LinphoneAddress *parsed_url = NULL; - if ((parsed_url = linphone_core_interpret_url(lc, to)) != NULL) { - return _linphone_core_create_chat_room(lc, parsed_url); - } - return NULL; +LinphoneChatRoom *linphone_core_get_chat_room (LinphoneCore *lc, const LinphoneAddress *addr) { + return L_GET_C_BACK_PTR(lc->cppCore->getOrCreateBasicChatRoom(*L_GET_CPP_PTR_FROM_C_OBJECT(addr))); } -static bool_t linphone_chat_room_matches(LinphoneChatRoom *cr, const LinphoneAddress *from) { - LinphoneAddress *addr = linphone_address_new(L_GET_CPP_PTR_FROM_C_OBJECT(cr)->getPeerAddress().asString().c_str()); - bool_t result = linphone_address_weak_equal(addr, from); - linphone_address_unref(addr); - return result; +LinphoneChatRoom *linphone_core_create_client_group_chat_room (LinphoneCore *lc, const char *subject) { + return L_GET_C_BACK_PTR(lc->cppCore->createClientGroupChatRoom(L_C_TO_STRING(subject))); } -LinphoneChatRoom *_linphone_core_get_chat_room(LinphoneCore *lc, const LinphoneAddress *addr) { - LinphoneChatRoom *cr = NULL; - bctbx_list_t *elem; - for (elem = lc->chatrooms; elem != NULL; elem = bctbx_list_next(elem)) { - cr = (LinphoneChatRoom *)elem->data; - if (linphone_chat_room_matches(cr, addr)) { - break; - } - cr = NULL; - } - return cr; -} - -static LinphoneChatRoom *_linphone_core_get_or_create_chat_room(LinphoneCore *lc, const char *to) { - LinphoneAddress *to_addr = linphone_core_interpret_url(lc, to); - LinphoneChatRoom *ret; - - if (to_addr == NULL) { - ms_error("linphone_core_get_or_create_chat_room(): Cannot make a valid address with %s", to); - return NULL; - } - ret = _linphone_core_get_chat_room(lc, to_addr); - linphone_address_unref(to_addr); - if (!ret) { - ret = _linphone_core_create_chat_room_from_url(lc, to); - } - return ret; -} - -LinphoneChatRoom *linphone_core_get_chat_room(LinphoneCore *lc, const LinphoneAddress *addr) { - LinphoneChatRoom *ret = _linphone_core_get_chat_room(lc, addr); - if (!ret) { - ret = _linphone_core_create_chat_room(lc, addr); - } - return ret; -} - -LinphoneChatRoom * linphone_core_create_client_group_chat_room(LinphoneCore *lc, const char *subject) { - const char *factoryUri = linphone_core_get_conference_factory_uri(lc); - if (!factoryUri) - return nullptr; - LinphoneChatRoom *cr = _linphone_client_group_chat_room_new(lc, factoryUri, subject); - lc->chatrooms = bctbx_list_append(lc->chatrooms, cr); - return cr; -} - -LinphoneChatRoom *_linphone_core_join_client_group_chat_room (LinphoneCore *lc, const LinphonePrivate::Address &addr) { - LinphoneChatRoom *cr = _linphone_client_group_chat_room_new(lc, addr.asString().c_str(), nullptr); - L_GET_CPP_PTR_FROM_C_OBJECT(cr)->join(); - lc->chatrooms = bctbx_list_append(lc->chatrooms, cr); - return cr; -} - -void linphone_core_delete_chat_room(LinphoneCore *lc, LinphoneChatRoom *cr) { - if (bctbx_list_find(lc->chatrooms, cr)) { - lc->chatrooms = bctbx_list_remove(lc->chatrooms, cr); - linphone_chat_room_delete_history(cr); - linphone_chat_room_unref(cr); - } else { - ms_error("linphone_core_delete_chat_room(): chatroom [%p] isn't part of LinphoneCore.", cr); - } +void linphone_core_delete_chat_room (LinphoneCore *, LinphoneChatRoom *cr) { + LinphonePrivate::Core::deleteChatRoom(L_GET_CPP_PTR_FROM_C_OBJECT(cr)); } LinphoneChatRoom *linphone_core_get_chat_room_from_uri(LinphoneCore *lc, const char *to) { - return _linphone_core_get_or_create_chat_room(lc, to); + return L_GET_C_BACK_PTR(lc->cppCore->getOrCreateBasicChatRoom(L_C_TO_STRING(to))); } int linphone_core_message_received(LinphoneCore *lc, LinphonePrivate::SalOp *op, const SalMessage *sal_msg) { LinphoneReason reason = LinphoneReasonNotAcceptable; - LinphoneChatRoom *cr = _linphone_core_find_group_chat_room(lc, op->get_from()); + LinphoneChatRoom *cr = L_GET_C_BACK_PTR(lc->cppCore->findChatRoom(LinphonePrivate::Address(op->get_from()))); if (cr) reason = L_GET_PRIVATE_FROM_C_OBJECT(cr)->messageReceived(op, sal_msg); else { @@ -171,8 +103,6 @@ int linphone_core_message_received(LinphoneCore *lc, LinphonePrivate::SalOp *op, return reason; } - - void linphone_core_real_time_text_received(LinphoneCore *lc, LinphoneChatRoom *cr, uint32_t character, LinphoneCall *call) { if (linphone_core_realtime_text_enabled(lc)) { shared_ptr rttcr = diff --git a/coreapi/linphonecore.c b/coreapi/linphonecore.c index 47be80186..2221eef8b 100644 --- a/coreapi/linphonecore.c +++ b/coreapi/linphonecore.c @@ -37,6 +37,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include #include #include + #include "mediastreamer2/dtmfgen.h" #include "mediastreamer2/mediastream.h" #include "mediastreamer2/msequalizer.h" @@ -45,8 +46,10 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include "mediastreamer2/msjpegwriter.h" #include "mediastreamer2/msogl.h" #include "mediastreamer2/msvolume.h" + #include "chat/chat-room/client-group-chat-room-p.h" #include "conference/remote-conference-event-handler.h" +#include "core/core.h" // For migration purpose. #include "address/address-p.h" @@ -2128,9 +2131,7 @@ static void linphone_core_internal_notify_received(LinphoneCore *lc, LinphoneEve } } else if (strcmp(notified_event, "conference") == 0) { const LinphoneAddress *resource = linphone_event_get_resource(lev); - char *resourceUri = linphone_address_as_string_uri_only(resource); - LinphoneChatRoom *cr = _linphone_core_find_group_chat_room(lc, resourceUri); - bctbx_free(resourceUri); + LinphoneChatRoom *cr = L_GET_C_BACK_PTR(lc->cppCore->findChatRoom(*L_GET_CPP_PTR_FROM_C_OBJECT(resource))); if (cr) L_GET_PRIVATE_FROM_C_OBJECT(cr, ClientGroupChatRoom)->notifyReceived(linphone_content_get_string_buffer(body)); } @@ -2234,7 +2235,8 @@ static void linphone_core_init(LinphoneCore * lc, LinphoneCoreCbs *cbs, LpConfig lc->sal->set_user_pointer(lc); lc->sal->set_callbacks(&linphone_sal_callbacks); - new(&lc->cppCore) Core(lc); + new(&lc->cppCore) std::shared_ptr(); + lc->cppCore = ObjectFactory::create(lc); #ifdef TUNNEL_ENABLED lc->tunnel=linphone_core_tunnel_new(lc); @@ -2266,7 +2268,6 @@ static void linphone_core_init(LinphoneCore * lc, LinphoneCoreCbs *cbs, LpConfig linphone_configuring_terminated(lc, LinphoneConfiguringSkipped, NULL); } // else linphone_core_start will be called after the remote provisioning (see linphone_core_iterate) lc->bw_controller = ms_bandwidth_controller_new(); - lc->group_chat_rooms = bctbx_mmap_cchar_new(); } #ifdef __ANDROID__ @@ -2280,7 +2281,7 @@ static void _linphone_core_set_system_context(LinphoneCore *lc, void *system_con } #else static void _linphone_core_set_system_context(LinphoneCore *lc, void *system_context){ - + } #endif @@ -5873,7 +5874,7 @@ void sip_config_uninit(LinphoneCore *lc) delete lc->sal; lc->sal=NULL; - lc->cppCore.~Core(); + lc->cppCore.~shared_ptr(); if (lc->sip_conf.guessed_contact) ms_free(lc->sip_conf.guessed_contact); @@ -6043,9 +6044,7 @@ static void linphone_core_uninit(LinphoneCore *lc) ms_usleep(10000); } - lc->chatrooms = bctbx_list_free_with_data(lc->chatrooms, (MSIterateFunc)linphone_chat_room_release); - if (lc->group_chat_rooms) - bctbx_mmap_cchar_delete_with_data(lc->group_chat_rooms, (void (*)(void *))linphone_chat_room_unref); + lc->chat_rooms = bctbx_list_free_with_data(lc->chat_rooms, (bctbx_list_free_func)linphone_chat_room_unref); linphone_core_set_state(lc,LinphoneGlobalShutdown,"Shutting down"); #ifdef VIDEO_ENABLED @@ -6143,11 +6142,11 @@ static void set_sip_network_reachable(LinphoneCore* lc,bool_t is_sip_reachable, if (lc->sip_network_reachable==is_sip_reachable) return; // no change, ignore. lc->network_reachable_to_be_notified=TRUE; - + if (is_sip_reachable){ getPlatformHelpers(lc)->setDnsServers(); } - + ms_message("SIP network reachability state is now [%s]",is_sip_reachable?"UP":"DOWN"); for(elem=linphone_core_get_proxy_config_list(lc);elem!=NULL;elem=elem->next){ LinphoneProxyConfig *cfg=(LinphoneProxyConfig*)elem->data; @@ -7149,53 +7148,6 @@ const char * linphone_core_get_conference_factory_uri(const LinphoneCore *lc) { return lp_config_get_string(linphone_core_get_config(lc), "misc", "conference_factory_uri", nullptr); } -bool_t _linphone_core_has_group_chat_room(const LinphoneCore *lc, const char *id) { - bool_t result; - bctbx_iterator_t *it = bctbx_map_cchar_find_key(lc->group_chat_rooms, id); - bctbx_iterator_t *endit = bctbx_map_cchar_end(lc->group_chat_rooms); - result = !bctbx_iterator_cchar_equals(it, endit); - bctbx_iterator_cchar_delete(endit); - bctbx_iterator_cchar_delete(it); - return result; -} - -void _linphone_core_add_group_chat_room(LinphoneCore *lc, const LinphonePrivate::Address &addr, LinphoneChatRoom *cr) { - Address cleanedAddr(addr); - cleanedAddr.clean(); - cleanedAddr.setPort(0); - bctbx_pair_t *pair = reinterpret_cast(bctbx_pair_cchar_new(cleanedAddr.asStringUriOnly().c_str(), linphone_chat_room_ref(cr))); - bctbx_map_cchar_insert_and_delete(lc->group_chat_rooms, pair); -} - -void _linphone_core_remove_group_chat_room(LinphoneCore *lc, LinphoneChatRoom *cr) { - const LinphoneAddress *confAddr = linphone_chat_room_get_conference_address(cr); - Address cleanedAddr(*L_GET_CPP_PTR_FROM_C_OBJECT(confAddr)); - cleanedAddr.clean(); - cleanedAddr.setPort(0); - bctbx_iterator_t *it = bctbx_map_cchar_find_key(lc->group_chat_rooms, cleanedAddr.asStringUriOnly().c_str()); - bctbx_iterator_t *endit = bctbx_map_cchar_end(lc->group_chat_rooms); - if (!bctbx_iterator_cchar_equals(it, endit)) { - bctbx_map_cchar_erase(lc->group_chat_rooms, it); - linphone_chat_room_unref(cr); - } - bctbx_iterator_cchar_delete(endit); - bctbx_iterator_cchar_delete(it); -} - -LinphoneChatRoom *_linphone_core_find_group_chat_room(const LinphoneCore *lc, const char *id) { - LinphoneChatRoom *result = nullptr; - Address cleanedAddr(id); - cleanedAddr.clean(); - cleanedAddr.setPort(0); - bctbx_iterator_t *it = bctbx_map_cchar_find_key(lc->group_chat_rooms, cleanedAddr.asStringUriOnly().c_str()); - bctbx_iterator_t *endit = bctbx_map_cchar_end(lc->group_chat_rooms); - if (!bctbx_iterator_cchar_equals(it, endit)) - result = reinterpret_cast(bctbx_pair_cchar_get_second(bctbx_iterator_cchar_get_pair(it))); - bctbx_iterator_cchar_delete(endit); - bctbx_iterator_cchar_delete(it); - return result; -} - void linphone_core_set_tls_cert(LinphoneCore *lc, const char *tls_cert) { if (lc->tls_cert) { ms_free(lc->tls_cert); diff --git a/coreapi/private.h b/coreapi/private.h index e00100c3f..be2eec3f1 100644 --- a/coreapi/private.h +++ b/coreapi/private.h @@ -25,6 +25,8 @@ #ifndef _PRIVATE_H #define _PRIVATE_H +#include + #include "linphone/core.h" #include "linphone/friend.h" #include "linphone/friendlist.h" @@ -34,7 +36,6 @@ #include "address/address.h" #include "c-wrapper/internal/c-sal.h" -#include "core/core.h" #include "sal/call-op.h" #include "sal/event-op.h" #include "sal/message-op.h" @@ -456,9 +457,6 @@ bool_t linphone_core_incompatible_security(LinphoneCore *lc, SalMediaDescription extern LinphonePrivate::Sal::Callbacks linphone_sal_callbacks; LINPHONE_PUBLIC bool_t linphone_core_rtcp_enabled(const LinphoneCore *lc); LINPHONE_PUBLIC bool_t linphone_core_symmetric_rtp_enabled(LinphoneCore*lc); -bool_t _linphone_core_has_group_chat_room (const LinphoneCore *lc, const char *id); -void _linphone_core_add_group_chat_room (LinphoneCore *lc, const LinphonePrivate::Address &addr, LinphoneChatRoom *cr); -void _linphone_core_remove_group_chat_room(LinphoneCore *lc, LinphoneChatRoom *cr); void linphone_core_queue_task(LinphoneCore *lc, belle_sip_source_func_t task_fun, void *data, const char *task_description); @@ -473,7 +471,6 @@ void _linphone_proxy_config_release_ops(LinphoneProxyConfig *obj); /*chat*/ LinphoneChatRoom * linphone_chat_room_new(LinphoneCore *core, const LinphoneAddress *addr); -LinphoneChatRoom *_linphone_core_join_client_group_chat_room (LinphoneCore *core, const LinphonePrivate::Address &addr); LinphoneChatRoom *_linphone_client_group_chat_room_new (LinphoneCore *core, const char *uri, const char *subject); void linphone_chat_room_release(LinphoneChatRoom *cr); void linphone_chat_room_set_call(LinphoneChatRoom *cr, LinphoneCall *call); @@ -810,6 +807,10 @@ typedef struct _LCCallbackObj { void *_user_data; } LCCallbackObj; +namespace LinphonePrivate { + class Core; +}; + struct _LinphoneCore { belle_sip_object_t base; @@ -819,7 +820,7 @@ struct _LinphoneCore LinphonePrivate::Sal *sal; // For migration purposes - LinphonePrivate::Core cppCore; + std::shared_ptr cppCore; void *platform_helper; /*is a LinphonePrivate::PlatformHelpers but cannot be used as is because private.h is compiled as C in testers.*/ @@ -847,8 +848,6 @@ struct _LinphoneCore MSList *calls; /* all the processed calls */ MSList *queued_calls; /* used by the autoreplier */ MSList *call_logs; - MSList *chatrooms; - bctbx_map_t *group_chat_rooms; int max_call_logs; int missed_calls; VideoPreview *previewstream; @@ -941,6 +940,9 @@ struct _LinphoneCore LinphoneImEncryptionEngine *im_encryption_engine; struct _LinphoneAccountCreatorService *default_ac_service; MSBandwidthController *bw_controller; + + // For migration purpose. + bctbx_list_t *chat_rooms; }; #ifdef __cplusplus diff --git a/coreapi/tester_utils.h b/coreapi/tester_utils.h index 0431d80de..12cf9b401 100644 --- a/coreapi/tester_utils.h +++ b/coreapi/tester_utils.h @@ -24,7 +24,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include "linphone/core.h" #include "linphone/tunnel.h" #include "c-wrapper/internal/c-sal.h" -#include +#include #include "quality_reporting.h" #include "vcard_private.h" @@ -94,7 +94,6 @@ LINPHONE_PUBLIC mblk_t *_linphone_call_stats_get_received_rtcp (const LinphoneCa LINPHONE_PUBLIC LinphoneQualityReporting *linphone_call_log_get_quality_reporting(LinphoneCallLog *call_log); LINPHONE_PUBLIC reporting_session_report_t **linphone_quality_reporting_get_reports(LinphoneQualityReporting *qreporting); -LINPHONE_PUBLIC LinphoneChatRoom *_linphone_core_find_group_chat_room(const LinphoneCore *lc, const char *id); LINPHONE_PUBLIC bctbx_list_t * linphone_chat_room_get_transient_messages(const LinphoneChatRoom *cr); LINPHONE_PUBLIC MSList* linphone_core_fetch_friends_from_db(LinphoneCore *lc, LinphoneFriendList *list); diff --git a/include/linphone/utils/utils.h b/include/linphone/utils/utils.h index 6f5569192..f8e8a0929 100644 --- a/include/linphone/utils/utils.h +++ b/include/linphone/utils/utils.h @@ -106,6 +106,7 @@ namespace Utils { } LINPHONE_PUBLIC std::tm getLongAsTm (long time); + LINPHONE_PUBLIC long getTmAsLong (const std::tm &time); } LINPHONE_END_NAMESPACE diff --git a/src/c-wrapper/api/c-chat-room.cpp b/src/c-wrapper/api/c-chat-room.cpp index e112f83c4..c86f16391 100644 --- a/src/c-wrapper/api/c-chat-room.cpp +++ b/src/c-wrapper/api/c-chat-room.cpp @@ -205,7 +205,7 @@ bctbx_list_t *linphone_chat_room_get_history (LinphoneChatRoom *cr, int nb_messa bctbx_list_t *linphone_chat_room_get_history_events (LinphoneChatRoom *cr, int nb_events) { return L_GET_RESOLVED_C_LIST_FROM_CPP_LIST( - L_GET_PRIVATE(L_GET_CPP_PTR_FROM_C_OBJECT(cr)->getCore()->cppCore)->mainDb.getHistory( + L_GET_PRIVATE(L_GET_CPP_PTR_FROM_C_OBJECT(cr)->getCore()->cppCore)->mainDb->getHistory( L_GET_CPP_PTR_FROM_C_OBJECT(cr)->getPeerAddress().asStringUriOnly(), nb_events ) @@ -214,7 +214,7 @@ bctbx_list_t *linphone_chat_room_get_history_events (LinphoneChatRoom *cr, int n bctbx_list_t *linphone_chat_room_get_history_range_events (LinphoneChatRoom *cr, int begin, int end) { return L_GET_RESOLVED_C_LIST_FROM_CPP_LIST( - L_GET_PRIVATE(L_GET_CPP_PTR_FROM_C_OBJECT(cr)->getCore()->cppCore)->mainDb.getHistory( + L_GET_PRIVATE(L_GET_CPP_PTR_FROM_C_OBJECT(cr)->getCore()->cppCore)->mainDb->getHistory( L_GET_CPP_PTR_FROM_C_OBJECT(cr)->getPeerAddress().asStringUriOnly(), begin, end @@ -330,14 +330,10 @@ void linphone_chat_room_set_user_data (LinphoneChatRoom *cr, void *ud) { // ============================================================================= LinphoneChatRoom *linphone_chat_room_new (LinphoneCore *core, const LinphoneAddress *addr) { - LinphoneChatRoom *cr = L_INIT(ChatRoom); - if (linphone_core_realtime_text_enabled(core)) - L_SET_CPP_PTR_FROM_C_OBJECT(cr, LinphonePrivate::ObjectFactory::create(core, *L_GET_CPP_PTR_FROM_C_OBJECT(addr))); - else - L_SET_CPP_PTR_FROM_C_OBJECT(cr, LinphonePrivate::ObjectFactory::create(core, *L_GET_CPP_PTR_FROM_C_OBJECT(addr))); - L_GET_PRIVATE_FROM_C_OBJECT(cr)->setState(LinphonePrivate::ChatRoom::State::Instantiated); - L_GET_PRIVATE_FROM_C_OBJECT(cr)->setState(LinphonePrivate::ChatRoom::State::Created); - return cr; + return L_GET_C_BACK_PTR(core->cppCore->getOrCreateBasicChatRoom( + *L_GET_CPP_PTR_FROM_C_OBJECT(addr), + linphone_core_realtime_text_enabled(core) + )); } LinphoneChatRoom *_linphone_client_group_chat_room_new (LinphoneCore *core, const char *uri, const char *subject) { diff --git a/src/chat/chat-room/basic-chat-room.cpp b/src/chat/chat-room/basic-chat-room.cpp index c439570fe..93d3ba079 100644 --- a/src/chat/chat-room/basic-chat-room.cpp +++ b/src/chat/chat-room/basic-chat-room.cpp @@ -35,7 +35,8 @@ BasicChatRoomPrivate::BasicChatRoomPrivate (LinphoneCore *core, const Address &p // ============================================================================= -BasicChatRoom::BasicChatRoom (LinphoneCore *core, const Address &peerAddress) : ChatRoom(*new BasicChatRoomPrivate(core, peerAddress)) {} +BasicChatRoom::BasicChatRoom (LinphoneCore *core, const Address &peerAddress) : + ChatRoom(*new BasicChatRoomPrivate(core, peerAddress)) {} // ----------------------------------------------------------------------------- diff --git a/src/chat/chat-room/chat-room.h b/src/chat/chat-room/chat-room.h index 562387c65..01a5108c8 100644 --- a/src/chat/chat-room/chat-room.h +++ b/src/chat/chat-room/chat-room.h @@ -30,6 +30,7 @@ LINPHONE_BEGIN_NAMESPACE class ChatRoomPrivate; class LINPHONE_PUBLIC ChatRoom : public Object, public ConferenceInterface { + friend class Core; friend class ChatMessage; friend class ChatMessagePrivate; diff --git a/src/chat/chat-room/client-group-chat-room.cpp b/src/chat/chat-room/client-group-chat-room.cpp index 6d8b6ede5..73ac9d595 100644 --- a/src/chat/chat-room/client-group-chat-room.cpp +++ b/src/chat/chat-room/client-group-chat-room.cpp @@ -251,7 +251,7 @@ void ClientGroupChatRoom::onConferenceCreated (const Address &addr) { dConference->conferenceAddress = addr; d->peerAddress = addr; d->setState(ChatRoom::State::Created); - _linphone_core_add_group_chat_room(d->core, addr, L_GET_C_BACK_PTR(this)); + d->core->cppCore->getPrivate()->insertChatRoomWithDb(getSharedFromThis()); } void ClientGroupChatRoom::onConferenceTerminated (const Address &addr) { @@ -283,7 +283,7 @@ void ClientGroupChatRoom::onParticipantAdded (time_t tm, const Address &addr) { dConference->eventHandler->getLastNotify(), addr ); - Conference::getCore()->cppCore.getPrivate()->mainDb.addEvent(event); + Conference::getCore()->cppCore->getPrivate()->mainDb->addEvent(event); if (cb) cb(cr, L_GET_C_BACK_PTR(event)); @@ -308,7 +308,7 @@ void ClientGroupChatRoom::onParticipantRemoved (time_t tm, const Address &addr) dConference->eventHandler->getLastNotify(), addr ); - Conference::getCore()->cppCore.getPrivate()->mainDb.addEvent(event); + Conference::getCore()->cppCore->getPrivate()->mainDb->addEvent(event); if (cb) cb(cr, L_GET_C_BACK_PTR(event)); @@ -339,7 +339,7 @@ void ClientGroupChatRoom::onParticipantSetAdmin (time_t tm, const Address &addr, dConference->eventHandler->getLastNotify(), addr ); - Conference::getCore()->cppCore.getPrivate()->mainDb.addEvent(event); + Conference::getCore()->cppCore->getPrivate()->mainDb->addEvent(event); if (cb) cb(cr, L_GET_C_BACK_PTR(event)); @@ -357,7 +357,7 @@ void ClientGroupChatRoom::onSubjectChanged (time_t tm, const std::string &subjec dConference->eventHandler->getLastNotify(), subject ); - Conference::getCore()->cppCore.getPrivate()->mainDb.addEvent(event); + Conference::getCore()->cppCore->getPrivate()->mainDb->addEvent(event); if (cb) cb(cr, L_GET_C_BACK_PTR(event)); @@ -386,7 +386,7 @@ void ClientGroupChatRoom::onParticipantDeviceAdded (time_t tm, const Address &ad addr, gruu ); - Conference::getCore()->cppCore.getPrivate()->mainDb.addEvent(event); + Conference::getCore()->cppCore->getPrivate()->mainDb->addEvent(event); if (cb) cb(cr, L_GET_C_BACK_PTR(event)); @@ -415,7 +415,7 @@ void ClientGroupChatRoom::onParticipantDeviceRemoved (time_t tm, const Address & addr, gruu ); - Conference::getCore()->cppCore.getPrivate()->mainDb.addEvent(event); + Conference::getCore()->cppCore->getPrivate()->mainDb->addEvent(event); if (cb) cb(cr, L_GET_C_BACK_PTR(event)); diff --git a/src/chat/chat-room/real-time-text-chat-room.cpp b/src/chat/chat-room/real-time-text-chat-room.cpp index 1c47fd0c1..388a0d313 100644 --- a/src/chat/chat-room/real-time-text-chat-room.cpp +++ b/src/chat/chat-room/real-time-text-chat-room.cpp @@ -69,7 +69,7 @@ void RealTimeTextChatRoomPrivate::realtimeTextReceived (uint32_t character, Linp pendingMessage->setToAddress( Address( linphone_call_get_dest_proxy(call) - ? linphone_address_as_string(linphone_call_get_dest_proxy(call)->identity_address) + ? linphone_address_as_string(linphone_call_get_dest_proxy(call)->identity_address) : linphone_core_get_identity(core) ) ); @@ -106,7 +106,8 @@ void RealTimeTextChatRoomPrivate::sendMessage (const std::shared_ptr(Capabilities::Basic) | static_cast(Capabilities::RealTimeText); diff --git a/src/core/core-p.h b/src/core/core-p.h index c402c6ccd..243fed9ac 100644 --- a/src/core/core-p.h +++ b/src/core/core-p.h @@ -30,12 +30,18 @@ LINPHONE_BEGIN_NAMESPACE class CorePrivate : public ObjectPrivate { public: - MainDb mainDb; - LinphoneCore *cCore; + std::unique_ptr mainDb; + LinphoneCore *cCore = nullptr; + + void insertChatRoomWithDb (const std::shared_ptr &chatRoom); + void deleteChatRoomWithDb (const std::string &peerAddress); private: + void insertChatRoom (const std::shared_ptr &chatRoom); + void deleteChatRoom (const std::string &peerAddress); + std::list> chatRooms; - std::unordered_map> chatRoomsMap; + std::unordered_map> chatRoomsByUri; L_DECLARE_PUBLIC(Core); }; diff --git a/src/core/core.cpp b/src/core/core.cpp index 3c34658c4..2bc55abf6 100644 --- a/src/core/core.cpp +++ b/src/core/core.cpp @@ -21,6 +21,8 @@ #include #include "chat/chat-room/basic-chat-room.h" +#include "chat/chat-room/chat-room-p.h" +#include "chat/chat-room/real-time-text-chat-room.h" #include "core-p.h" #include "db/main-db.h" #include "logger/logger.h" @@ -28,6 +30,7 @@ #include "paths/paths.h" // TODO: Remove me later. +#include "c-wrapper/c-wrapper.h" #include "private.h" #define LINPHONE_DB "linphone.db" @@ -39,41 +42,83 @@ using namespace std; LINPHONE_BEGIN_NAMESPACE // ----------------------------------------------------------------------------- +// Helpers. +// ----------------------------------------------------------------------------- + +static inline Address getCleanedPeerAddress (const Address &peerAddress) { + Address cleanedAddress = peerAddress; + cleanedAddress.clean(); + cleanedAddress.setPort(0); + return cleanedAddress; +} + +// ----------------------------------------------------------------------------- +// CorePrivate: ChatRoom. +// ----------------------------------------------------------------------------- + +void CorePrivate::insertChatRoom (const shared_ptr &chatRoom) { + L_ASSERT(chatRoom); + L_ASSERT(chatRoom->getState() == ChatRoom::State::Created); + + string peerAddress = getCleanedPeerAddress(chatRoom->getPeerAddress()).asStringUriOnly(); + deleteChatRoom(peerAddress); + + chatRooms.push_back(chatRoom); + chatRoomsByUri[peerAddress] = chatRoom; +} + +void CorePrivate::deleteChatRoom (const string &peerAddress) { + auto it = chatRoomsByUri.find(peerAddress); + if (it != chatRoomsByUri.end()) + chatRooms.erase( + find_if(chatRooms.begin(), chatRooms.end(), [&peerAddress](const shared_ptr &chatRoom) { + return peerAddress == chatRoom->getPeerAddress().asStringUriOnly(); + }) + ); +} + +void CorePrivate::insertChatRoomWithDb (const shared_ptr &chatRoom) { + insertChatRoom(chatRoom); + mainDb->insertChatRoom( + getCleanedPeerAddress(chatRoom->getPeerAddress()).asStringUriOnly(), + chatRoom->getCapabilities() + ); +} + +void CorePrivate::deleteChatRoomWithDb (const string &peerAddress) { + deleteChatRoom(peerAddress); + mainDb->deleteChatRoom(peerAddress); +} + +// ============================================================================= Core::Core (LinphoneCore *cCore) : Object(*new CorePrivate) { L_D(); d->cCore = cCore; - const char *uri = lp_config_get_string(linphone_core_get_config(d->cCore), "server", "db_uri", NULL); - if (uri) { - AbstractDb::Backend backend = - strcmp(lp_config_get_string(linphone_core_get_config(d->cCore), "server", "db_backend", NULL), "mysql") == 0 + d->mainDb.reset(new MainDb(this)); + + AbstractDb::Backend backend; + string uri = L_C_TO_STRING(lp_config_get_string(linphone_core_get_config(d->cCore), "server", "db_uri", NULL)); + if (!uri.empty()) + backend = strcmp(lp_config_get_string(linphone_core_get_config(d->cCore), "server", "db_backend", NULL), "mysql") == 0 ? MainDb::Mysql : MainDb::Sqlite3; - lInfo() << "Creating " LINPHONE_DB " at: " << uri; - d->mainDb.connect(backend, uri); - return; + else { + backend = AbstractDb::Sqlite3; + uri = getDataPath() + "/" LINPHONE_DB; } - static string path = getDataPath() + "/" LINPHONE_DB; - lInfo() << "Creating " LINPHONE_DB " at: " << path; - d->mainDb.connect(MainDb::Sqlite3, path); + lInfo() << "Opening " LINPHONE_DB " at: " << uri; + if (!d->mainDb->connect(backend, uri)) + lFatal() << "Unable to open linphone database."; + + for (auto &chatRoom : d->mainDb->getChatRooms()) + d->insertChatRoom(chatRoom); } // ----------------------------------------------------------------------------- - -shared_ptr Core::createClientGroupChatRoom (const string &subject) { - // TODO. - return shared_ptr(); -} - -shared_ptr Core::getOrCreateChatRoom (const string &peerAddress, bool isRtt) const { - return shared_ptr(); -} - -const list> &Core::getChatRooms () const { - L_D(); - return d->chatRooms; -} +// Paths. +// ----------------------------------------------------------------------------- string Core::getDataPath() const { L_D(); @@ -85,6 +130,78 @@ string Core::getConfigPath() const { return Paths::getPath(Paths::Config, static_cast(d->cCore->platform_helper)); } +// ----------------------------------------------------------------------------- +// ChatRoom. +// ----------------------------------------------------------------------------- + +const list> &Core::getChatRooms () const { + L_D(); + return d->chatRooms; +} + +shared_ptr Core::findChatRoom (const Address &peerAddress) const { + L_D(); + auto it = d->chatRoomsByUri.find(getCleanedPeerAddress(peerAddress).asStringUriOnly()); + return it == d->chatRoomsByUri.cend() ? shared_ptr() : it->second; +} + +shared_ptr Core::createClientGroupChatRoom (const string &subject) { + L_D(); + + const char *factoryUri = linphone_core_get_conference_factory_uri(d->cCore); + if (!factoryUri) + return nullptr; + + return L_GET_CPP_PTR_FROM_C_OBJECT( + _linphone_client_group_chat_room_new(d->cCore, factoryUri, L_STRING_TO_C(subject)) + ); +} + +shared_ptr Core::getOrCreateBasicChatRoom (const Address &peerAddress, bool isRtt) { + L_D(); + + if (!peerAddress.isValid()) { + lWarning() << "Cannot find get or create chat room with invalid peer address."; + return nullptr; + } + + shared_ptr chatRoom = findChatRoom(peerAddress); + if (chatRoom) + return chatRoom; + + if (isRtt) + chatRoom = ObjectFactory::create(d->cCore, peerAddress); + else + chatRoom = ObjectFactory::create(d->cCore, peerAddress); + + chatRoom->getPrivate()->setState(ChatRoom::State::Instantiated); + chatRoom->getPrivate()->setState(ChatRoom::State::Created); + + d->insertChatRoomWithDb(chatRoom); + + return chatRoom; +} + +shared_ptr Core::getOrCreateBasicChatRoom (const string &peerAddress, bool isRtt) { + L_D(); + + LinphoneAddress *address = linphone_core_interpret_url(d->cCore, L_STRING_TO_C(peerAddress)); + if (!address) { + lError() << "Cannot make a valid address with: `" << peerAddress << "`."; + return nullptr; + } + + shared_ptr chatRoom = getOrCreateBasicChatRoom(*L_GET_CPP_PTR_FROM_C_OBJECT(address), isRtt); + linphone_address_unref(address); + return chatRoom; +} + +void Core::deleteChatRoom (const shared_ptr &chatRoom) { + CorePrivate *d = chatRoom->getCore()->cppCore->getPrivate(); + string peerAddress = getCleanedPeerAddress(chatRoom->getPeerAddress()).asStringUriOnly(); + d->deleteChatRoomWithDb(peerAddress); +} + // ----------------------------------------------------------------------------- LINPHONE_END_NAMESPACE diff --git a/src/core/core.h b/src/core/core.h index 4e4cf5d04..207e3275b 100644 --- a/src/core/core.h +++ b/src/core/core.h @@ -30,21 +30,36 @@ L_DECL_C_STRUCT(LinphoneCore); LINPHONE_BEGIN_NAMESPACE +class Address; class ChatRoom; class CorePrivate; class LINPHONE_PUBLIC Core : public Object { -friend class ClientGroupChatRoom; + friend class ClientGroupChatRoom; + public: + L_OVERRIDE_SHARED_FROM_THIS(Core); + Core (LinphoneCore *cCore); + // --------------------------------------------------------------------------- + // Paths. + // --------------------------------------------------------------------------- + std::string getDataPath() const; std::string getConfigPath() const; - const std::list> &getChatRooms () const; + // --------------------------------------------------------------------------- + // ChatRoom. + // --------------------------------------------------------------------------- + const std::list> &getChatRooms () const; + std::shared_ptr findChatRoom (const Address &peerAddress) const; std::shared_ptr createClientGroupChatRoom (const std::string &subject); - std::shared_ptr getOrCreateChatRoom (const std::string &peerAddress, bool isRtt = false) const; + std::shared_ptr getOrCreateBasicChatRoom (const Address &peerAddress, bool isRtt = false); + std::shared_ptr getOrCreateBasicChatRoom (const std::string &peerAddress, bool isRtt = false); + + static void deleteChatRoom (const std::shared_ptr &chatRoom); private: L_DECLARE_PRIVATE(Core); diff --git a/src/core/paths/paths-android.cpp b/src/core/paths/paths-android.cpp index b53adbd22..275c4ef7c 100644 --- a/src/core/paths/paths-android.cpp +++ b/src/core/paths/paths-android.cpp @@ -19,26 +19,27 @@ #include -#include "core/platform-helpers/platform-helpers.h" #include "linphone/utils/utils.h" +#include "core/platform-helpers/platform-helpers.h" + #include "paths-android.h" // ============================================================================= +using namespace std; + LINPHONE_BEGIN_NAMESPACE -std::string SysPaths::getDataPath (PlatformHelpers *platformHelper) { - if (!platformHelper) { - return Utils::getEmptyConstRefObject(); - } +string SysPaths::getDataPath (PlatformHelpers *platformHelper) { + if (!platformHelper) + return Utils::getEmptyConstRefObject(); return platformHelper->getDataPath(); } -std::string SysPaths::getConfigPath (PlatformHelpers *platformHelper) { - if (!platformHelper) { - return Utils::getEmptyConstRefObject(); - } +string SysPaths::getConfigPath (PlatformHelpers *platformHelper) { + if (!platformHelper) + return Utils::getEmptyConstRefObject(); return platformHelper->getConfigPath(); } diff --git a/src/core/paths/paths-android.h b/src/core/paths/paths-android.h index 7661ebb59..f4ac148cc 100644 --- a/src/core/paths/paths-android.h +++ b/src/core/paths/paths-android.h @@ -29,6 +29,7 @@ LINPHONE_BEGIN_NAMESPACE class PlatformHelpers; + namespace SysPaths { LINPHONE_PUBLIC std::string getDataPath (PlatformHelpers *platformHelper); LINPHONE_PUBLIC std::string getConfigPath (PlatformHelpers *platformHelper); diff --git a/src/core/paths/paths-apple.h b/src/core/paths/paths-apple.h index acfe25ff0..f7942e646 100644 --- a/src/core/paths/paths-apple.h +++ b/src/core/paths/paths-apple.h @@ -29,6 +29,7 @@ LINPHONE_BEGIN_NAMESPACE class PlatformHelpers; + namespace SysPaths { LINPHONE_PUBLIC std::string getDataPath (PlatformHelpers *platformHelper); LINPHONE_PUBLIC std::string getConfigPath (PlatformHelpers *platformHelper); diff --git a/src/core/paths/paths-linux.cpp b/src/core/paths/paths-linux.cpp index 06cdab9fb..7261a326e 100644 --- a/src/core/paths/paths-linux.cpp +++ b/src/core/paths/paths-linux.cpp @@ -17,23 +17,37 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -#include "core/platform-helpers/platform-helpers.h" #include "linphone/utils/utils.h" +#include "core/platform-helpers/platform-helpers.h" +#include "logger/logger.h" + #include "paths-linux.h" // ============================================================================= +using namespace std; + LINPHONE_BEGIN_NAMESPACE -static std::string dataPath = "~/.local/share/linphone"; -static std::string configPath = "~/.config/linphone"; +static string getBaseDirectory () { + static string base; + if (base.empty()) { + char *dir = getenv("HOME"); + if (!dir) + lFatal() << "Unable to get home directory."; + base = dir; + } + return base; +} -std::string SysPaths::getDataPath (PlatformHelpers *platformHelper) { +string SysPaths::getDataPath (PlatformHelpers *platformHelper) { + static string dataPath = getBaseDirectory() + "/.local/share/linphone"; return dataPath; } -std::string SysPaths::getConfigPath (PlatformHelpers *platformHelper) { +string SysPaths::getConfigPath (PlatformHelpers *platformHelper) { + static string configPath = getBaseDirectory() + "/.config/linphone"; return configPath; } diff --git a/src/core/paths/paths-linux.h b/src/core/paths/paths-linux.h index cc652b833..c2fd0a117 100644 --- a/src/core/paths/paths-linux.h +++ b/src/core/paths/paths-linux.h @@ -29,6 +29,7 @@ LINPHONE_BEGIN_NAMESPACE class PlatformHelpers; + namespace SysPaths { LINPHONE_PUBLIC std::string getDataPath (PlatformHelpers *platformHelper); LINPHONE_PUBLIC std::string getConfigPath (PlatformHelpers *platformHelper); diff --git a/src/core/paths/paths-windows.cpp b/src/core/paths/paths-windows.cpp index 8af9d8a4a..fd2ba5d27 100644 --- a/src/core/paths/paths-windows.cpp +++ b/src/core/paths/paths-windows.cpp @@ -18,11 +18,13 @@ */ #include + #include "shlobj.h" -#include "core/platform-helpers/platform-helpers.h" #include "linphone/utils/utils.h" +#include "core/platform-helpers/platform-helpers.h" + #include "paths-windows.h" // ============================================================================= @@ -46,7 +48,7 @@ string SysPaths::getDataPath (PlatformHelpers *platformHelper) { } string SysPaths::getConfigPath (PlatformHelpers *platformHelper) { - // seems to be the same directory + // Seems to be the same directory. return getDataPath(platformHelper); } diff --git a/src/core/paths/paths-windows.h b/src/core/paths/paths-windows.h index 137be1f87..80b4aa5b3 100644 --- a/src/core/paths/paths-windows.h +++ b/src/core/paths/paths-windows.h @@ -29,6 +29,7 @@ LINPHONE_BEGIN_NAMESPACE class PlatformHelpers; + namespace SysPaths { LINPHONE_PUBLIC std::string getDataPath (PlatformHelpers *platformHelper); LINPHONE_PUBLIC std::string getConfigPath (PlatformHelpers *platformHelper); diff --git a/src/core/paths/paths.cpp b/src/core/paths/paths.cpp index 01987fec5..31b12289e 100644 --- a/src/core/paths/paths.cpp +++ b/src/core/paths/paths.cpp @@ -21,16 +21,16 @@ #include "paths.h" #ifdef __APPLE__ -#include "paths-apple.h" + #include "paths-apple.h" #elif defined(__ANDROID__) -#include "paths-android.h" + #include "paths-android.h" #elif defined(_WIN32) -#include "paths-windows.h" + #include "paths-windows.h" #elif defined(__linux) -#include "paths-linux.h" + #include "paths-linux.h" #else -#error "Unsupported system" -#endif + #error "Unsupported system." +#endif // ifdef __APPLE__ // ============================================================================= @@ -43,9 +43,11 @@ string Paths::getPath (Paths::Type type, PlatformHelpers *platformHelper) { case Data: return SysPaths::getDataPath(platformHelper); case Config: - default: return SysPaths::getConfigPath(platformHelper); } + + L_ASSERT(false); + return ""; } LINPHONE_END_NAMESPACE diff --git a/src/core/paths/paths.h b/src/core/paths/paths.h index e600a067e..c54a40893 100644 --- a/src/core/paths/paths.h +++ b/src/core/paths/paths.h @@ -35,7 +35,7 @@ namespace Paths { Config }; - LINPHONE_PUBLIC std::string getPath(Type type, PlatformHelpers *platformHelper); + LINPHONE_PUBLIC std::string getPath (Type type, PlatformHelpers *platformHelper); } LINPHONE_END_NAMESPACE diff --git a/src/core/platform-helpers/platform-helpers.cpp b/src/core/platform-helpers/platform-helpers.cpp index 624392072..15ed502dd 100644 --- a/src/core/platform-helpers/platform-helpers.cpp +++ b/src/core/platform-helpers/platform-helpers.cpp @@ -21,6 +21,8 @@ // ============================================================================= +using namespace std; + LINPHONE_BEGIN_NAMESPACE StubbedPlatformHelpers::StubbedPlatformHelpers (LinphoneCore *lc) : PlatformHelpers(lc) {} @@ -39,11 +41,11 @@ void StubbedPlatformHelpers::acquireCpuLock () {} void StubbedPlatformHelpers::releaseCpuLock () {} -std::string StubbedPlatformHelpers::getDataPath () { +string StubbedPlatformHelpers::getDataPath () { return ""; } -std::string StubbedPlatformHelpers::getConfigPath () { +string StubbedPlatformHelpers::getConfigPath () { return ""; } diff --git a/src/db/main-db-p.h b/src/db/main-db-p.h index 9f916d47c..01b9692b8 100644 --- a/src/db/main-db-p.h +++ b/src/db/main-db-p.h @@ -108,6 +108,8 @@ private: long long insertConferenceParticipantDeviceEvent (const std::shared_ptr &eventLog); long long insertConferenceSubjectEvent (const std::shared_ptr &eventLog); + Core *core; + L_DECLARE_PUBLIC(MainDb); }; diff --git a/src/db/main-db.cpp b/src/db/main-db.cpp index 6888d5487..a49285647 100644 --- a/src/db/main-db.cpp +++ b/src/db/main-db.cpp @@ -30,9 +30,10 @@ #include "conference/participant.h" #include "content/content-type.h" #include "content/content.h" +#include "core/core.h" #include "db/session/db-session-provider.h" -#include "event-log/events.h" #include "event-log/event-log-p.h" +#include "event-log/events.h" #include "logger/logger.h" #include "main-db-p.h" @@ -44,7 +45,10 @@ LINPHONE_BEGIN_NAMESPACE // ----------------------------------------------------------------------------- -MainDb::MainDb () : AbstractDb(*new MainDbPrivate) {} +MainDb::MainDb (Core *core) : AbstractDb(*new MainDbPrivate) { + L_D(); + d->core = core; +} #ifdef SOCI_ENABLED @@ -1003,53 +1007,72 @@ MainDb::MainDb () : AbstractDb(*new MainDbPrivate) {} // ----------------------------------------------------------------------------- -list> MainDb::getChatRooms () const { - static const string query = "SELECT value, creation_date, last_update_date, capabilities, subject, last_notify_id" - " FROM chat_room, sip_address" - " WHERE peer_sip_address_id = id"; + list> MainDb::getChatRooms () const { + static const string query = "SELECT value, creation_date, last_update_date, capabilities, subject, last_notify_id" + " FROM chat_room, sip_address" + " WHERE peer_sip_address_id = id"; - L_D(); + L_D(); - list> chatRooms; + list> chatRooms; - L_BEGIN_LOG_EXCEPTION + L_BEGIN_LOG_EXCEPTION - soci::session *session = d->dbSession.getBackendSession(); + soci::session *session = d->dbSession.getBackendSession(); - soci::rowset rows = (session->prepare << query); - for (const auto &row : rows) { - string sipAddress = row.get(0); - tm creationDate = row.get(1); - tm lastUpdateDate = row.get(2); - int capabilities = row.get(3); - string subject = row.get(4); - unsigned int lastNotifyId = row.get(5); + soci::rowset rows = (session->prepare << query); + for (const auto &row : rows) { + string sipAddress = row.get(0); + tm creationDate = row.get(1); + tm lastUpdateDate = row.get(2); + int capabilities = row.get(3); + string subject = row.get(4); + unsigned int lastNotifyId = row.get(5); - (void)sipAddress; - (void)creationDate; - (void)lastUpdateDate; - (void)capabilities; - (void)subject; - (void)lastNotifyId; + // TODO: Use me. + (void)creationDate; + (void)lastUpdateDate; + (void)subject; + (void)lastNotifyId; - if (capabilities & static_cast(ChatRoom::Capabilities::Basic)) { - if (capabilities & static_cast(ChatRoom::Capabilities::RealTimeText)) { - // TODO. - continue; + shared_ptr chatRoom; + if (capabilities & static_cast(ChatRoom::Capabilities::Basic)) { + chatRoom = d->core ? d->core->getOrCreateBasicChatRoom( + Address(sipAddress), + capabilities & static_cast(ChatRoom::Capabilities::RealTimeText) + ) : nullptr; + } else if (capabilities & static_cast(ChatRoom::Capabilities::Conference)) { + // TODO: Set sip address and participants. } - // TODO. - continue; + + if (!chatRoom) + continue; // Not fetched. + + chatRooms.push_back(chatRoom); } - if (capabilities & static_cast(ChatRoom::Capabilities::Conference)) { - // TODO. - } + L_END_LOG_EXCEPTION + + return chatRooms; } - L_END_LOG_EXCEPTION + void MainDb::insertChatRoom (const string &peerAddress, int capabilities) { + L_D(); + d->insertChatRoom(d->insertSipAddress(peerAddress), capabilities, Utils::getLongAsTm(0)); + } - return chatRooms; -} + void MainDb::deleteChatRoom (const std::string &peerAddress) { + L_D(); + + L_BEGIN_LOG_EXCEPTION + + soci::session *session = d->dbSession.getBackendSession(); + *session << "DELETE FROM chat_room WHERE peer_sip_address_id IN (" + " SELECT id FROM sip_address WHERE value = :peerAddress" + ")", soci::use(peerAddress); + + L_END_LOG_EXCEPTION + } // ----------------------------------------------------------------------------- diff --git a/src/db/main-db.h b/src/db/main-db.h index 86edc9ca1..af06cdcda 100644 --- a/src/db/main-db.h +++ b/src/db/main-db.h @@ -29,6 +29,7 @@ LINPHONE_BEGIN_NAMESPACE class ChatRoom; +class Core; class EventLog; class MainDbPrivate; @@ -45,7 +46,7 @@ public: typedef int FilterMask; - MainDb (); + MainDb (Core *core); // Generic. bool addEvent (const std::shared_ptr &eventLog); @@ -71,6 +72,8 @@ public: // ChatRooms. std::list> getChatRooms () const; + void insertChatRoom (const std::string &peerAddress, int capabilities); + void deleteChatRoom (const std::string &peerAddress); // Import legacy messages from old db. bool import (Backend backend, const std::string ¶meters) override; diff --git a/src/utils/utils.cpp b/src/utils/utils.cpp index 5a106897a..cfde5c7ac 100644 --- a/src/utils/utils.cpp +++ b/src/utils/utils.cpp @@ -178,4 +178,8 @@ tm Utils::getLongAsTm (long time) { return *gmtime_r(&static_cast(time), &result); } +long Utils::getTmAsLong (const tm &time) { + return timegm(&const_cast(time)); +} + LINPHONE_END_NAMESPACE diff --git a/tester/main-db-tester.cpp b/tester/main-db-tester.cpp index 8691b575c..2d4b6037a 100644 --- a/tester/main-db-tester.cpp +++ b/tester/main-db-tester.cpp @@ -36,12 +36,12 @@ static const string getDatabasePath () { // ----------------------------------------------------------------------------- static void open_database () { - MainDb mainDb; + MainDb mainDb(nullptr); BC_ASSERT_TRUE(mainDb.connect(MainDb::Sqlite3, getDatabasePath())); } static void get_events_count () { - MainDb mainDb; + MainDb mainDb(nullptr); BC_ASSERT_TRUE(mainDb.connect(MainDb::Sqlite3, getDatabasePath())); BC_ASSERT_EQUAL(mainDb.getEventsCount(), 4976, int, "%d"); BC_ASSERT_EQUAL(mainDb.getEventsCount(MainDb::ConferenceCallFilter), 0, int, "%d"); @@ -51,21 +51,21 @@ static void get_events_count () { } static void get_messages_count () { - MainDb mainDb; + MainDb mainDb(nullptr); BC_ASSERT_TRUE(mainDb.connect(MainDb::Sqlite3, getDatabasePath())); BC_ASSERT_EQUAL(mainDb.getMessagesCount(), 4976, int, "%d"); BC_ASSERT_EQUAL(mainDb.getMessagesCount("sip:test-39@sip.linphone.org"), 3, int, "%d"); } static void get_unread_messages_count () { - MainDb mainDb; + MainDb mainDb(nullptr); BC_ASSERT_TRUE(mainDb.connect(MainDb::Sqlite3, getDatabasePath())); BC_ASSERT_EQUAL(mainDb.getUnreadMessagesCount(), 2, int, "%d"); BC_ASSERT_EQUAL(mainDb.getUnreadMessagesCount("sip:test-39@sip.linphone.org"), 0, int, "%d"); } static void get_history () { - MainDb mainDb; + MainDb mainDb(nullptr); BC_ASSERT_TRUE(mainDb.connect(MainDb::Sqlite3, getDatabasePath())); BC_ASSERT_EQUAL( mainDb.getHistoryRange("sip:test-39@sip.linphone.org", 0, -1, MainDb::Filter::ConferenceChatMessageFilter).size(),