Commit 0babed04 authored by Ghislain MARY's avatar Ghislain MARY Committed by Ronan

Use ChatRoom C++ class.

parent 95c781ec
This diff is collapsed.
...@@ -26,8 +26,12 @@ ...@@ -26,8 +26,12 @@
#include "private.h" #include "private.h"
#include "ortp/b64.h" #include "ortp/b64.h"
#include "chat/chat-room.h"
extern LinphonePrivate::ChatRoom& linphone_chat_room_get_cpp_obj(LinphoneChatRoom *cr);
static bool_t file_transfer_in_progress_and_valid(LinphoneChatMessage* msg) { static bool_t file_transfer_in_progress_and_valid(LinphoneChatMessage* msg) {
return (msg->chat_room && msg->chat_room->lc && msg->http_request && !belle_http_request_is_cancelled(msg->http_request)); return (msg->chat_room && linphone_chat_room_get_core(msg->chat_room) && msg->http_request && !belle_http_request_is_cancelled(msg->http_request));
} }
static void _release_http_request(LinphoneChatMessage* msg) { static void _release_http_request(LinphoneChatMessage* msg) {
...@@ -88,7 +92,7 @@ static void linphone_chat_message_file_transfer_on_progress(belle_sip_body_handl ...@@ -88,7 +92,7 @@ static void linphone_chat_message_file_transfer_on_progress(belle_sip_body_handl
msg, msg->file_transfer_information, offset, total); msg, msg->file_transfer_information, offset, total);
} else { } else {
/* Legacy: call back given by application level */ /* Legacy: call back given by application level */
linphone_core_notify_file_transfer_progress_indication(msg->chat_room->lc, msg, msg->file_transfer_information, linphone_core_notify_file_transfer_progress_indication(linphone_chat_room_get_core(msg->chat_room), msg, msg->file_transfer_information,
offset, total); offset, total);
} }
} }
...@@ -108,7 +112,7 @@ static int on_send_body(belle_sip_user_body_handler_t *bh, belle_sip_message_t * ...@@ -108,7 +112,7 @@ static int on_send_body(belle_sip_user_body_handler_t *bh, belle_sip_message_t *
return BELLE_SIP_STOP; return BELLE_SIP_STOP;
} }
lc = msg->chat_room->lc; lc = linphone_chat_room_get_core(msg->chat_room);
/* if we've not reach the end of file yet, ask for more data */ /* if we've not reach the end of file yet, ask for more data */
/* in case of file body handler, won't be called */ /* in case of file body handler, won't be called */
if (msg->file_transfer_filepath == NULL && offset < linphone_content_get_size(msg->file_transfer_information)) { if (msg->file_transfer_filepath == NULL && offset < linphone_content_get_size(msg->file_transfer_information)) {
...@@ -153,7 +157,7 @@ static int on_send_body(belle_sip_user_body_handler_t *bh, belle_sip_message_t * ...@@ -153,7 +157,7 @@ static int on_send_body(belle_sip_user_body_handler_t *bh, belle_sip_message_t *
static void on_send_end(belle_sip_user_body_handler_t *bh, void *data) { static void on_send_end(belle_sip_user_body_handler_t *bh, void *data) {
LinphoneChatMessage *msg = (LinphoneChatMessage *)data; LinphoneChatMessage *msg = (LinphoneChatMessage *)data;
LinphoneCore *lc = msg->chat_room->lc; LinphoneCore *lc = linphone_chat_room_get_core(msg->chat_room);
LinphoneImEncryptionEngine *imee = linphone_core_get_im_encryption_engine(lc); LinphoneImEncryptionEngine *imee = linphone_core_get_im_encryption_engine(lc);
if (imee) { if (imee) {
...@@ -185,8 +189,7 @@ static void file_upload_begin_background_task(LinphoneChatMessage *obj){ ...@@ -185,8 +189,7 @@ static void file_upload_begin_background_task(LinphoneChatMessage *obj){
} }
} }
static void linphone_chat_message_process_response_from_post_file(void *data, static void linphone_chat_message_process_response_from_post_file(void *data, const belle_http_response_event_t *event) {
const belle_http_response_event_t *event) {
LinphoneChatMessage *msg = (LinphoneChatMessage *)data; LinphoneChatMessage *msg = (LinphoneChatMessage *)data;
if (msg->http_request && !file_transfer_in_progress_and_valid(msg)) { if (msg->http_request && !file_transfer_in_progress_and_valid(msg)) {
...@@ -205,7 +208,7 @@ static void linphone_chat_message_process_response_from_post_file(void *data, ...@@ -205,7 +208,7 @@ static void linphone_chat_message_process_response_from_post_file(void *data,
belle_sip_body_handler_t *first_part_bh; belle_sip_body_handler_t *first_part_bh;
bool_t is_file_encryption_enabled = FALSE; bool_t is_file_encryption_enabled = FALSE;
LinphoneImEncryptionEngine *imee = linphone_core_get_im_encryption_engine(msg->chat_room->lc); LinphoneImEncryptionEngine *imee = linphone_core_get_im_encryption_engine(linphone_chat_room_get_core(msg->chat_room));
if (imee) { if (imee) {
LinphoneImEncryptionEngineCbs *imee_cbs = linphone_im_encryption_engine_get_callbacks(imee); LinphoneImEncryptionEngineCbs *imee_cbs = linphone_im_encryption_engine_get_callbacks(imee);
LinphoneImEncryptionEngineCbsIsEncryptionEnabledForFileTransferCb is_encryption_enabled_for_file_transfer_cb = LinphoneImEncryptionEngineCbsIsEncryptionEnabledForFileTransferCb is_encryption_enabled_for_file_transfer_cb =
...@@ -339,7 +342,7 @@ static void linphone_chat_message_process_response_from_post_file(void *data, ...@@ -339,7 +342,7 @@ static void linphone_chat_message_process_response_from_post_file(void *data,
linphone_chat_message_ref(msg); linphone_chat_message_ref(msg);
linphone_chat_message_set_state(msg, LinphoneChatMessageStateFileTransferDone); linphone_chat_message_set_state(msg, LinphoneChatMessageStateFileTransferDone);
_release_http_request(msg); _release_http_request(msg);
_linphone_chat_room_send_message(msg->chat_room, msg); linphone_chat_room_get_cpp_obj(msg->chat_room).sendMessage(msg);
file_upload_end_background_task(msg); file_upload_end_background_task(msg);
linphone_chat_message_unref(msg); linphone_chat_message_unref(msg);
} else { } else {
...@@ -374,7 +377,7 @@ static void on_recv_body(belle_sip_user_body_handler_t *bh, belle_sip_message_t ...@@ -374,7 +377,7 @@ static void on_recv_body(belle_sip_user_body_handler_t *bh, belle_sip_message_t
linphone_chat_message_cancel_file_transfer(msg); linphone_chat_message_cancel_file_transfer(msg);
return; return;
} }
lc = msg->chat_room->lc; lc = linphone_chat_room_get_core(msg->chat_room);
if (lc == NULL){ if (lc == NULL){
return; /*might happen during linphone_core_destroy()*/ return; /*might happen during linphone_core_destroy()*/
...@@ -425,7 +428,7 @@ static void on_recv_body(belle_sip_user_body_handler_t *bh, belle_sip_message_t ...@@ -425,7 +428,7 @@ static void on_recv_body(belle_sip_user_body_handler_t *bh, belle_sip_message_t
static void on_recv_end(belle_sip_user_body_handler_t *bh, void *data) { static void on_recv_end(belle_sip_user_body_handler_t *bh, void *data) {
LinphoneChatMessage *msg = (LinphoneChatMessage *)data; LinphoneChatMessage *msg = (LinphoneChatMessage *)data;
LinphoneCore *lc = msg->chat_room->lc; LinphoneCore *lc = linphone_chat_room_get_core(msg->chat_room);
LinphoneImEncryptionEngine *imee = linphone_core_get_im_encryption_engine(lc); LinphoneImEncryptionEngine *imee = linphone_core_get_im_encryption_engine(lc);
int retval = -1; int retval = -1;
...@@ -536,7 +539,7 @@ static void linphone_chat_process_response_from_get_file(void *data, const belle ...@@ -536,7 +539,7 @@ static void linphone_chat_process_response_from_get_file(void *data, const belle
int _linphone_chat_room_start_http_transfer(LinphoneChatMessage *msg, const char* url, const char* action, const belle_http_request_listener_callbacks_t *cbs) { int _linphone_chat_room_start_http_transfer(LinphoneChatMessage *msg, const char* url, const char* action, const belle_http_request_listener_callbacks_t *cbs) {
belle_generic_uri_t *uri = NULL; belle_generic_uri_t *uri = NULL;
const char* ua = linphone_core_get_user_agent(msg->chat_room->lc); const char* ua = linphone_core_get_user_agent(linphone_chat_room_get_core(msg->chat_room));
if (url == NULL) { if (url == NULL) {
ms_warning("Cannot process file transfer msg: no file remote URI configured."); ms_warning("Cannot process file transfer msg: no file remote URI configured.");
...@@ -559,7 +562,7 @@ int _linphone_chat_room_start_http_transfer(LinphoneChatMessage *msg, const char ...@@ -559,7 +562,7 @@ int _linphone_chat_room_start_http_transfer(LinphoneChatMessage *msg, const char
/* give msg to listener to be able to start the actual file upload when server answer a 204 No content */ /* give msg to listener to be able to start the actual file upload when server answer a 204 No content */
msg->http_listener = belle_http_request_listener_create_from_callbacks(cbs, linphone_chat_message_ref(msg)); msg->http_listener = belle_http_request_listener_create_from_callbacks(cbs, linphone_chat_message_ref(msg));
belle_http_provider_send_request(msg->chat_room->lc->http_provider, msg->http_request, msg->http_listener); belle_http_provider_send_request(linphone_chat_room_get_core(msg->chat_room)->http_provider, msg->http_request, msg->http_listener);
return 0; return 0;
error: error:
if (uri) { if (uri) {
...@@ -580,7 +583,7 @@ int linphone_chat_room_upload_file(LinphoneChatMessage *msg) { ...@@ -580,7 +583,7 @@ int linphone_chat_room_upload_file(LinphoneChatMessage *msg) {
cbs.process_response = linphone_chat_message_process_response_from_post_file; cbs.process_response = linphone_chat_message_process_response_from_post_file;
cbs.process_io_error = linphone_chat_message_process_io_error_upload; cbs.process_io_error = linphone_chat_message_process_io_error_upload;
cbs.process_auth_requested = linphone_chat_message_process_auth_requested_upload; cbs.process_auth_requested = linphone_chat_message_process_auth_requested_upload;
err = _linphone_chat_room_start_http_transfer(msg, linphone_core_get_file_transfer_server(msg->chat_room->lc), "POST", &cbs); err = _linphone_chat_room_start_http_transfer(msg, linphone_core_get_file_transfer_server(linphone_chat_room_get_core(msg->chat_room)), "POST", &cbs);
if (err == -1){ if (err == -1){
linphone_chat_message_set_state(msg, LinphoneChatMessageStateNotDelivered); linphone_chat_message_set_state(msg, LinphoneChatMessageStateNotDelivered);
} }
...@@ -621,10 +624,10 @@ void _linphone_chat_message_cancel_file_transfer(LinphoneChatMessage *msg, bool_ ...@@ -621,10 +624,10 @@ void _linphone_chat_message_cancel_file_transfer(LinphoneChatMessage *msg, bool_
if (!belle_http_request_is_cancelled(msg->http_request)) { if (!belle_http_request_is_cancelled(msg->http_request)) {
if (msg->chat_room) { if (msg->chat_room) {
ms_message("Canceling file transfer %s - msg [%p] chat room[%p]" ms_message("Canceling file transfer %s - msg [%p] chat room[%p]"
, (msg->external_body_url == NULL) ? linphone_core_get_file_transfer_server(msg->chat_room->lc) : msg->external_body_url , (msg->external_body_url == NULL) ? linphone_core_get_file_transfer_server(linphone_chat_room_get_core(msg->chat_room)) : msg->external_body_url
, msg , msg
, msg->chat_room); , msg->chat_room);
belle_http_provider_cancel_request(msg->chat_room->lc->http_provider, msg->http_request); belle_http_provider_cancel_request(linphone_chat_room_get_core(msg->chat_room)->http_provider, msg->http_request);
if ((msg->dir == LinphoneChatMessageOutgoing) && unref) { if ((msg->dir == LinphoneChatMessageOutgoing) && unref) {
// must release it // must release it
linphone_chat_message_unref(msg); linphone_chat_message_unref(msg);
...@@ -654,20 +657,6 @@ const char *linphone_chat_message_get_file_transfer_filepath(LinphoneChatMessage ...@@ -654,20 +657,6 @@ const char *linphone_chat_message_get_file_transfer_filepath(LinphoneChatMessage
return msg->file_transfer_filepath; return msg->file_transfer_filepath;
} }
LinphoneChatMessage *linphone_chat_room_create_file_transfer_message(LinphoneChatRoom *cr, LinphoneChatMessage *linphone_chat_room_create_file_transfer_message(LinphoneChatRoom *cr, const LinphoneContent *initial_content) {
const LinphoneContent *initial_content) { return linphone_chat_room_get_cpp_obj(cr).createFileTransferMessage(initial_content);
LinphoneChatMessage *msg = belle_sip_object_new(LinphoneChatMessage);
msg->callbacks = linphone_chat_message_cbs_new();
msg->chat_room = (LinphoneChatRoom *)cr;
msg->message = NULL;
msg->file_transfer_information = linphone_content_copy(initial_content);
msg->dir = LinphoneChatMessageOutgoing;
linphone_chat_message_set_to(msg, linphone_chat_room_get_peer_address(cr));
msg->from = linphone_address_new(linphone_core_get_identity(cr->lc)); /*direct assignment*/
/* this will be set to application/vnd.gsma.rcs-ft-http+xml when we will transfer the xml reply from server to the peers */
msg->content_type = NULL;
/* this will store the http request during file upload to the server */
msg->http_request = NULL;
msg->time = ms_time(0);
return msg;
} }
...@@ -749,11 +749,11 @@ error: ...@@ -749,11 +749,11 @@ error:
bool_t linphone_chat_room_lime_available(LinphoneChatRoom *cr) { bool_t linphone_chat_room_lime_available(LinphoneChatRoom *cr) {
if (cr) { if (cr) {
switch (linphone_core_lime_enabled(cr->lc)) { switch (linphone_core_lime_enabled(linphone_chat_room_get_core(cr))) {
case LinphoneLimeDisabled: return FALSE; case LinphoneLimeDisabled: return FALSE;
case LinphoneLimeMandatory: case LinphoneLimeMandatory:
case LinphoneLimePreferred: { case LinphoneLimePreferred: {
void *zrtp_cache_db = linphone_core_get_zrtp_cache_db(cr->lc); void *zrtp_cache_db = linphone_core_get_zrtp_cache_db(linphone_chat_room_get_core(cr));
if (zrtp_cache_db != NULL) { if (zrtp_cache_db != NULL) {
bool_t res; bool_t res;
limeURIKeys_t associatedKeys; limeURIKeys_t associatedKeys;
...@@ -832,7 +832,7 @@ int lime_im_encryption_engine_process_outgoing_message_cb(LinphoneImEncryptionEn ...@@ -832,7 +832,7 @@ int lime_im_encryption_engine_process_outgoing_message_cb(LinphoneImEncryptionEn
LinphoneCore *lc = linphone_im_encryption_engine_get_core(engine); LinphoneCore *lc = linphone_im_encryption_engine_get_core(engine);
int errcode = -1; int errcode = -1;
const char *new_content_type = "xml/cipher"; const char *new_content_type = "xml/cipher";
if(linphone_core_lime_enabled(room->lc)) { if(linphone_core_lime_enabled(linphone_chat_room_get_core(room))) {
if (linphone_chat_room_lime_available(room)) { if (linphone_chat_room_lime_available(room)) {
void *zrtp_cache_db = NULL; /* use a void * instead of sqlite3 * to avoid problems and ifdef when SQLITE is not available(the get function shall return NULL in that case) */ void *zrtp_cache_db = NULL; /* use a void * instead of sqlite3 * to avoid problems and ifdef when SQLITE is not available(the get function shall return NULL in that case) */
if (msg->content_type) { if (msg->content_type) {
...@@ -862,7 +862,7 @@ int lime_im_encryption_engine_process_outgoing_message_cb(LinphoneImEncryptionEn ...@@ -862,7 +862,7 @@ int lime_im_encryption_engine_process_outgoing_message_cb(LinphoneImEncryptionEn
retval = lime_createMultipartMessage(zrtp_cache_db, msg->content_type, (uint8_t *)msg->message, selfUri, peerUri, &crypted_body); retval = lime_createMultipartMessage(zrtp_cache_db, msg->content_type, (uint8_t *)msg->message, selfUri, peerUri, &crypted_body);
if (retval != 0) { /* fail to encrypt */ if (retval != 0) { /* fail to encrypt */
ms_warning("Unable to encrypt message for %s : %s", room->peer, lime_error_code_to_string(retval)); ms_warning("Unable to encrypt message for %s : %s", peerUri, lime_error_code_to_string(retval));
if (crypted_body) ms_free(crypted_body); if (crypted_body) ms_free(crypted_body);
errcode = 488; errcode = 488;
} else { /* encryption ok, swap plain text message body by encrypted one */ } else { /* encryption ok, swap plain text message body by encrypted one */
......
...@@ -1719,7 +1719,7 @@ static void linphone_call_set_terminated(LinphoneCall *call){ ...@@ -1719,7 +1719,7 @@ static void linphone_call_set_terminated(LinphoneCall *call){
call->ringing_beep=FALSE; call->ringing_beep=FALSE;
} }
if (call->chat_room){ if (call->chat_room){
call->chat_room->call = NULL; linphone_chat_room_set_call(call->chat_room, NULL);
} }
if (lc->calls == NULL){ if (lc->calls == NULL){
ms_bandwidth_controller_reset_state(lc->bw_controller); ms_bandwidth_controller_reset_state(lc->bw_controller);
......
...@@ -23,6 +23,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. ...@@ -23,6 +23,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#ifdef SQLITE_STORAGE_ENABLED #ifdef SQLITE_STORAGE_ENABLED
#include "chat/chat-room.h"
#ifndef _WIN32 #ifndef _WIN32
#if !defined(__QNXNTO__) && !defined(__ANDROID__) #if !defined(__QNXNTO__) && !defined(__ANDROID__)
...@@ -42,6 +43,9 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. ...@@ -42,6 +43,9 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include <assert.h> #include <assert.h>
extern LinphonePrivate::ChatRoom& linphone_chat_room_get_cpp_obj(LinphoneChatRoom *cr);
static char *utf8_convert(const char *filename){ static char *utf8_convert(const char *filename){
char db_file_utf8[MAX_PATH_SIZE] = ""; char db_file_utf8[MAX_PATH_SIZE] = "";
#if defined(_WIN32) #if defined(_WIN32)
...@@ -117,31 +121,6 @@ int _linphone_sqlite3_open(const char *db_file, sqlite3 **db) { ...@@ -117,31 +121,6 @@ int _linphone_sqlite3_open(const char *db_file, sqlite3 **db) {
#ifdef SQLITE_STORAGE_ENABLED #ifdef SQLITE_STORAGE_ENABLED
static LinphoneChatMessage * get_weak_message(LinphoneChatRoom *cr, unsigned int storage_id) {
LinphoneChatMessage *cm;
bctbx_list_t *item;
for (item = cr->weak_messages; item != NULL; item = bctbx_list_next(item)) {
cm = (LinphoneChatMessage *)bctbx_list_get_data(item);
if (linphone_chat_message_get_storage_id(cm) == storage_id)
return linphone_chat_message_ref(cm);
}
return NULL;
}
static ORTP_INLINE LinphoneChatMessage* get_transient_message(LinphoneChatRoom* cr, unsigned int storage_id){
bctbx_list_t* transients = cr->transient_messages;
LinphoneChatMessage* chat;
while( transients ){
chat = (LinphoneChatMessage*)transients->data;
if(chat->storage_id == storage_id){
return linphone_chat_message_ref(chat);
}
transients = transients->next;
}
return NULL;
}
/* DB layout: /* DB layout:
* | 0 | storage_id * | 0 | storage_id
* | 1 | type * | 1 | type
...@@ -172,7 +151,7 @@ static int callback_content(void *data, int argc, char **argv, char **colName) { ...@@ -172,7 +151,7 @@ static int callback_content(void *data, int argc, char **argv, char **colName) {
return 0; return 0;
} }
static void fetch_content_from_database(sqlite3 *db, LinphoneChatMessage *message, int content_id) { void linphone_chat_message_fetch_content_from_database(sqlite3 *db, LinphoneChatMessage *message, int content_id) {
char* errmsg = NULL; char* errmsg = NULL;
int ret; int ret;
char * buf; char * buf;
...@@ -186,8 +165,6 @@ static void fetch_content_from_database(sqlite3 *db, LinphoneChatMessage *messag ...@@ -186,8 +165,6 @@ static void fetch_content_from_database(sqlite3 *db, LinphoneChatMessage *messag
sqlite3_free(buf); sqlite3_free(buf);
} }
// Called when fetching all conversations from database // Called when fetching all conversations from database
static int callback_all(void *data, int argc, char **argv, char **colName){ static int callback_all(void *data, int argc, char **argv, char **colName){
LinphoneCore* lc = (LinphoneCore*) data; LinphoneCore* lc = (LinphoneCore*) data;
...@@ -200,93 +177,6 @@ static int callback_all(void *data, int argc, char **argv, char **colName){ ...@@ -200,93 +177,6 @@ static int callback_all(void *data, int argc, char **argv, char **colName){
return 0; return 0;
} }
/* DB layout:
* | 0 | storage_id
* | 1 | localContact
* | 2 | remoteContact
* | 3 | direction flag (LinphoneChatMessageDir)
* | 4 | message (text content of the message)
* | 5 | time (unused now, used to be string-based timestamp, replaced by the utc timestamp)
* | 6 | read flag (no longer used, replaced by the LinphoneChatMessageStateDisplayed state)
* | 7 | status (LinphoneChatMessageState)
* | 8 | external body url (deprecated file transfer system)
* | 9 | utc timestamp
* | 10 | app data text
* | 11 | linphone content id (LinphoneContent describing a file transfer)
* | 12 | message id (used for IMDN)
* | 13 | content type (of the message field [must be text representable])
* | 14 | secured flag
*/
static int create_chat_message(void *data, int argc, char **argv, char **colName){
LinphoneChatRoom *cr = (LinphoneChatRoom *)data;
unsigned int storage_id = (unsigned int)atoi(argv[0]);
LinphoneChatMessage* new_message;
/* Check if the message exists in the weak messages list, in which case we should return that one. */
new_message = get_weak_message(cr, storage_id);
if (new_message == NULL) {
/* Check if the message exists in the transient list, in which case we should return that one. */
new_message = get_transient_message(cr, storage_id);
}
if (new_message == NULL) {
new_message = linphone_chat_room_create_message(cr, argv[4]);
if(atoi(argv[3])==LinphoneChatMessageIncoming){
new_message->dir=LinphoneChatMessageIncoming;
linphone_chat_message_set_from(new_message,linphone_chat_room_get_peer_address(cr));
new_message->to = NULL; /*will be filled at the end */
} else {
new_message->dir=LinphoneChatMessageOutgoing;
new_message->from = NULL; /*will be filled at the end */
linphone_chat_message_set_to(new_message,linphone_chat_room_get_peer_address(cr));
}
new_message->time = (time_t)atol(argv[9]);
new_message->is_read=atoi(argv[6]);
new_message->state=static_cast<LinphoneChatMessageState>(atoi(argv[7]));
new_message->storage_id=storage_id;
new_message->external_body_url= ms_strdup(argv[8]);
new_message->appdata = ms_strdup(argv[10]);
new_message->message_id = ms_strdup(argv[12]);
linphone_chat_message_set_content_type(new_message, argv[13]);
new_message->is_secured = (bool_t)atoi(argv[14]);
if (argv[11] != NULL) {
int id = atoi(argv[11]);
if (id >= 0) {
fetch_content_from_database(cr->lc->db, new_message, id);
}
}
/* Fix content type for old messages that were stored without it */
if (new_message->content_type == NULL) {
if (new_message->file_transfer_information != NULL) {
new_message->content_type = ms_strdup("application/vnd.gsma.rcs-ft-http+xml");
} else if (new_message->external_body_url != NULL) {
new_message->content_type = ms_strdup("message/external-body");
} else {
new_message->content_type = ms_strdup("text/plain");
}
}
/* Add the new message to the weak messages list. */
linphone_chat_room_add_weak_message(cr, new_message);
}
cr->messages_hist=bctbx_list_prepend(cr->messages_hist,new_message);
return 0;
}
void linphone_sql_request_message(sqlite3 *db,const char *stmt,LinphoneChatRoom *cr){
char* errmsg=NULL;
int ret;
ret=sqlite3_exec(db,stmt,create_chat_message,cr,&errmsg);
if(ret != SQLITE_OK) {
ms_error("Error in creation: %s.", errmsg);
sqlite3_free(errmsg);
}
}
int linphone_sql_request(sqlite3* db,const char *stmt){ int linphone_sql_request(sqlite3* db,const char *stmt){
char* errmsg=NULL; char* errmsg=NULL;
int ret; int ret;
...@@ -407,7 +297,7 @@ void linphone_chat_message_store_update(LinphoneChatMessage *msg) { ...@@ -407,7 +297,7 @@ void linphone_chat_message_store_update(LinphoneChatMessage *msg) {
} }
void linphone_chat_message_store_state(LinphoneChatMessage *msg){ void linphone_chat_message_store_state(LinphoneChatMessage *msg){
LinphoneCore *lc=msg->chat_room->lc; LinphoneCore *lc=linphone_chat_room_get_core(msg->chat_room);
if (lc->db){ if (lc->db){
char *buf=sqlite3_mprintf("UPDATE history SET status=%i WHERE (id = %u);", char *buf=sqlite3_mprintf("UPDATE history SET status=%i WHERE (id = %u);",
msg->state,msg->storage_id); msg->state,msg->storage_id);
...@@ -417,7 +307,7 @@ void linphone_chat_message_store_state(LinphoneChatMessage *msg){ ...@@ -417,7 +307,7 @@ void linphone_chat_message_store_state(LinphoneChatMessage *msg){
} }
void linphone_chat_message_store_appdata(LinphoneChatMessage* msg){ void linphone_chat_message_store_appdata(LinphoneChatMessage* msg){
LinphoneCore *lc=msg->chat_room->lc; LinphoneCore *lc=linphone_chat_room_get_core(msg->chat_room);
if (lc->db){ if (lc->db){
char *buf=sqlite3_mprintf("UPDATE history SET appdata=%Q WHERE id=%u;", char *buf=sqlite3_mprintf("UPDATE history SET appdata=%Q WHERE id=%u;",
msg->appdata,msg->storage_id); msg->appdata,msg->storage_id);
...@@ -426,251 +316,44 @@ void linphone_chat_message_store_appdata(LinphoneChatMessage* msg){ ...@@ -426,251 +316,44 @@ void linphone_chat_message_store_appdata(LinphoneChatMessage* msg){
} }
} }
void linphone_chat_room_mark_as_read(LinphoneChatRoom *cr){ void linphone_chat_room_mark_as_read(LinphoneChatRoom *cr) {
LinphoneCore *lc=linphone_chat_room_get_core(cr); linphone_chat_room_get_cpp_obj(cr).markAsRead();
bctbx_list_t *item;
char *peer;
char *buf;
if (lc->db==NULL) return ;
// optimization: do not modify the database if no message is marked as unread
if(linphone_chat_room_get_unread_messages_count(cr) == 0) return;
peer=linphone_address_as_string_uri_only(linphone_chat_room_get_peer_address(cr));
buf = sqlite3_mprintf("SELECT * FROM history WHERE remoteContact = %Q AND direction = %i AND status != %i", peer, LinphoneChatMessageIncoming, LinphoneChatMessageStateDisplayed);
linphone_sql_request_message(lc->db, buf, cr);
sqlite3_free(buf);
for (item = cr->messages_hist; item != NULL; item = bctbx_list_next(item)) {
LinphoneChatMessage *cm = (LinphoneChatMessage *)bctbx_list_get_data(item);
linphone_chat_message_send_display_notification(cm);
}
bctbx_list_free_with_data(cr->messages_hist, (bctbx_list_free_func)linphone_chat_message_unref);
cr->messages_hist = NULL;
buf=sqlite3_mprintf("UPDATE history SET status=%i WHERE remoteContact=%Q AND direction=%i;",
LinphoneChatMessageStateDisplayed, peer, LinphoneChatMessageIncoming);
linphone_sql_request(lc->db,buf);
sqlite3_free(buf);
ms_free(peer);
if (cr->pending_message) {
linphone_chat_message_set_state(cr->pending_message, LinphoneChatMessageStateDisplayed);
linphone_chat_message_send_display_notification(cr->pending_message);
}
cr->unread_count = 0;
} }
void linphone_chat_room_update_url(LinphoneChatRoom *cr, LinphoneChatMessage *msg) { int linphone_chat_room_get_unread_messages_count(LinphoneChatRoom *cr) {
LinphoneCore *lc=linphone_chat_room_get_core(cr); return linphone_chat_room_get_cpp_obj(cr).getUnreadMessagesCount();
char *buf;
if (lc->db==NULL) return ;
buf=sqlite3_mprintf("UPDATE history SET url=%Q WHERE id=%u;",msg->external_body_url,msg->storage_id);
linphone_sql_request(lc->db,buf);
sqlite3_free(buf);
} }
static int linphone_chat_room_get_messages_count(LinphoneChatRoom *cr, bool_t unread_only){ int linphone_chat_room_get_history_size(LinphoneChatRoom *cr) {
LinphoneCore *lc=linphone_chat_room_get_core(cr); return linphone_chat_room_get_cpp_obj(cr).getHistorySize();
int numrows=0;
char *peer;
char *buf;
char *option = NULL;
sqlite3_stmt *selectStatement;
int returnValue;
if (lc->db==NULL) return 0;
// optimization: do not read database if the count is already available in memory
if(unread_only && cr->unread_count >= 0) return cr->unread_count;
peer=linphone_address_as_string_uri_only(linphone_chat_room_get_peer_address(cr));
if (unread_only) {
option = bctbx_strdup_printf("AND status!=%i AND direction=%i", LinphoneChatMessageStateDisplayed, LinphoneChatMessageIncoming);
}
buf=sqlite3_mprintf("SELECT count(*) FROM history WHERE remoteContact = %Q %s;",peer,unread_only?option:"");
returnValue = sqlite3_prepare_v2(lc->db,buf,-1,&selectStatement,NULL);
if (returnValue == SQLITE_OK){
if(sqlite3_step(selectStatement) == SQLITE_ROW){
numrows= sqlite3_column_int(selectStatement, 0);
}
}
sqlite3_finalize(selectStatement);
sqlite3_free(buf);
ms_free(peer);
/* no need to test the sign of cr->unread_count here
* because it has been tested above */
if(unread_only) {
cr->unread_count = numrows;
if (option) bctbx_free(option);
}
return numrows;
}
int linphone_chat_room_get_unread_messages_count(LinphoneChatRoom *cr){
return linphone_chat_room_get_messages_count(cr, TRUE);
}
int linphone_chat_room_get_history_size(LinphoneChatRoom *cr){
return linphone_chat_room_get_messages_count(cr, FALSE);
} }
void linphone_chat_room_delete_message(LinphoneChatRoom *cr, LinphoneChatMessage *msg) { void linphone_chat_room_delete_message(LinphoneChatRoom *cr, LinphoneChatMessage *msg) {
LinphoneCore *lc=cr->lc; linphone_chat_room_get_cpp_obj(cr).deleteMessage(msg);
char *buf;
if (lc->db==NULL) return ;