Add UTF8 conversion for chat messages

This commit is contained in:
Mickaël Turnel 2017-12-06 16:05:52 +01:00
parent 20efb4ad41
commit 8574752312
5 changed files with 24 additions and 118 deletions

View file

@ -28,6 +28,7 @@
#include "belle-sip/belle-sip.h"
#include "ortp/b64.h"
#include "linphone/wrapper_utils.h"
#include "bctoolbox/charconv.h"
#include <libxml/parser.h>
#include <libxml/tree.h>
@ -370,6 +371,14 @@ static void store_or_update_chat_message(LinphoneChatMessage *msg) {
}
}
void _linphone_chat_message_convert_to_utf8(LinphoneChatMessage *msg) {
if (msg && msg->message) {
char* tmp = msg->message;
msg->message = bctbx_locale_to_utf8(tmp);
ms_free(tmp);
}
}
void _linphone_chat_room_send_message(LinphoneChatRoom *cr, LinphoneChatMessage *msg) {
int retval = -1;
LinphoneCore *lc = cr->lc;
@ -406,6 +415,7 @@ void _linphone_chat_room_send_message(LinphoneChatRoom *cr, LinphoneChatMessage
char *clear_text_content_type = NULL;
if (msg->message) {
_linphone_chat_message_convert_to_utf8(msg);
clear_text_message = ms_strdup(msg->message);
}
if (msg->content_type) {
@ -952,6 +962,7 @@ LinphoneChatMessage *linphone_chat_room_create_message(LinphoneChatRoom *cr, con
msg->callbacks = linphone_chat_message_cbs_new();
msg->chat_room = (LinphoneChatRoom *)cr;
msg->message = message ? ms_strdup(message) : NULL;
msg->locale_message = NULL;
msg->content_type = ms_strdup("text/plain");
msg->file_transfer_information = NULL; /* this property is used only when transfering file */
msg->http_request = NULL;
@ -1606,8 +1617,12 @@ LinphoneChatMessageState linphone_chat_message_get_state(const LinphoneChatMessa
return msg->state;
}
const char *linphone_chat_message_get_text(const LinphoneChatMessage *msg) {
return msg->message;
const char *linphone_chat_message_get_text(LinphoneChatMessage *msg) {
// Convert into system locale is message is in UTF8
if (msg->message && !msg->locale_message) {
msg->locale_message = bctbx_utf8_to_locale(msg->message);
}
return msg->locale_message;
}
int linphone_chat_message_set_text(LinphoneChatMessage *msg, const char* text) {

View file

@ -19,60 +19,14 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include "private.h"
#include "linphone/core.h"
#include "bctoolbox/charconv.h"
#ifdef SQLITE_STORAGE_ENABLED
#ifndef _WIN32
#if !defined(__QNXNTO__) && !defined(__ANDROID__)
#include <ctype.h>
#include <langinfo.h>
#include <locale.h>
#include <iconv.h>
#include <string.h>
#endif
#else
#include <Windows.h>
#endif
#define MAX_PATH_SIZE 1024
#include "sqlite3.h"
#include <assert.h>
static char *utf8_convert(const char *filename){
char db_file_utf8[MAX_PATH_SIZE] = "";
#if defined(_WIN32)
wchar_t db_file_utf16[MAX_PATH_SIZE]={0};
MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, filename, -1, db_file_utf16, MAX_PATH_SIZE);
WideCharToMultiByte(CP_UTF8, 0, db_file_utf16, -1, db_file_utf8, sizeof(db_file_utf8), NULL, NULL);
#elif defined(__QNXNTO__) || defined(__ANDROID__)
strncpy(db_file_utf8, filename, MAX_PATH_SIZE - 1);
#else
char db_file_locale[MAX_PATH_SIZE] = {'\0'};
char *inbuf=db_file_locale, *outbuf=db_file_utf8;
size_t inbyteleft = MAX_PATH_SIZE, outbyteleft = MAX_PATH_SIZE;
iconv_t cb;
if (strcasecmp("UTF-8", nl_langinfo(CODESET)) == 0) {
strncpy(db_file_utf8, filename, MAX_PATH_SIZE - 1);
} else {
strncpy(db_file_locale, filename, MAX_PATH_SIZE-1);
cb = iconv_open("UTF-8", nl_langinfo(CODESET));
if (cb != (iconv_t)-1) {
int ret;
ret = iconv(cb, &inbuf, &inbyteleft, &outbuf, &outbyteleft);
if(ret == -1) db_file_utf8[0] = '\0';
iconv_close(cb);
}
}
#endif
return ms_strdup(db_file_utf8);
}
int _linphone_sqlite3_open(const char *db_file, sqlite3 **db) {
char* errmsg = NULL;
int ret;
@ -86,7 +40,7 @@ int _linphone_sqlite3_open(const char *db_file, sqlite3 **db) {
/*since we plug our vfs into sqlite, we convert to UTF-8.
* On Windows, the filename has to be converted back to windows native charset.*/
char *utf8_filename = utf8_convert(db_file);
char *utf8_filename = bctbx_locale_to_utf8(db_file);
ret = sqlite3_open_v2(utf8_filename, db, flags, LINPHONE_SQLITE3_VFS);
ms_free(utf8_filename);

View file

@ -245,6 +245,7 @@ struct _LinphoneChatMessage {
LinphoneErrorInfo *ei;
LinphoneChatMessageDir dir;
char* message;
char* locale_message;
void* message_state_changed_user_data;
void* message_userdata;
char* appdata;

View file

@ -18,7 +18,9 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifdef SQLITE_STORAGE_ENABLED
#include "private.h"
#include "bctoolbox/charconv.h"
#include "sqlite3_bctbx_vfs.h"
#include <sqlite3.h>
@ -28,17 +30,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#endif /*_WIN32_WCE*/
#ifndef _WIN32
#if !defined(__QNXNTO__) && !defined(__ANDROID__)
# include <langinfo.h>
# include <locale.h>
# include <iconv.h>
# include <string.h>
#endif
#endif
/**
* Closes the file whose file descriptor is stored in the file handle p.
* @param p sqlite3_file file handle pointer.
@ -252,61 +243,6 @@ static int sqlite3bctbx_Sync(sqlite3_file *p, int flags){
/************************ END OF PLACE HOLDER FUNCTIONS ***********************/
static char* ConvertFromUtf8Filename(const char* fName){
#if _WIN32
char* convertedFilename;
int nChar, nb_byte;
LPWSTR wideFilename;
nChar = MultiByteToWideChar(CP_UTF8, 0, fName, -1, NULL, 0);
if (nChar == 0) return NULL;
wideFilename = reinterpret_cast<LPWSTR>(bctbx_malloc(nChar*sizeof(wideFilename[0])));
if (wideFilename == NULL) return NULL;
nChar = MultiByteToWideChar(CP_UTF8, 0, fName, -1, wideFilename, nChar);
if (nChar == 0) {
bctbx_free(wideFilename);
wideFilename = 0;
}
nb_byte = WideCharToMultiByte(CP_ACP, 0, wideFilename, -1, 0, 0, 0, 0);
if (nb_byte == 0) return NULL;
convertedFilename = reinterpret_cast<char *>(bctbx_malloc(nb_byte));
if (convertedFilename == NULL) return NULL;
nb_byte = WideCharToMultiByte(CP_ACP, 0, wideFilename, -1, convertedFilename, nb_byte, 0, 0);
if (nb_byte == 0) {
bctbx_free(convertedFilename);
convertedFilename = 0;
}
bctbx_free(wideFilename);
return convertedFilename;
#elif defined(__QNXNTO__) || defined(__ANDROID__)
return bctbx_strdup(fName);
#else
#define MAX_PATH_SIZE 1024
char db_file_utf8[MAX_PATH_SIZE] = {'\0'};
char db_file_locale[MAX_PATH_SIZE] = "";
char *outbuf=db_file_locale, *inbuf=db_file_utf8;
size_t inbyteleft = MAX_PATH_SIZE, outbyteleft = MAX_PATH_SIZE;
iconv_t cb;
if (strcasecmp("UTF-8", nl_langinfo(CODESET)) == 0) {
strncpy(db_file_locale, fName, MAX_PATH_SIZE - 1);
} else {
strncpy(db_file_utf8, fName, MAX_PATH_SIZE-1);
cb = iconv_open(nl_langinfo(CODESET), "UTF-8");
if (cb != (iconv_t)-1) {
int ret;
ret = iconv(cb, &inbuf, &inbyteleft, &outbuf, &outbyteleft);
if(ret == -1) db_file_locale[0] = '\0';
iconv_close(cb);
}
}
return bctbx_strdup(db_file_locale);
#endif
}
/**
* Opens the file fName and populates the structure pointed by p
* with the necessary io_methods
@ -356,7 +292,7 @@ static int sqlite3bctbx_Open(sqlite3_vfs *pVfs, const char *fName, sqlite3_file
#if defined(_WIN32)
openFlags |= O_BINARY;
#endif
wFname = ConvertFromUtf8Filename(fName);
wFname = bctbx_utf8_to_locale(fName);
if (wFname != NULL) {
pFile->pbctbx_file = bctbx_file_open2(bctbx_vfs_get_default(), wFname, openFlags);
bctbx_free(wFname);

View file

@ -435,7 +435,7 @@ LINPHONE_PUBLIC void linphone_chat_message_set_appdata(LinphoneChatMessage* mess
* Get text part of this message
* @return text or NULL if no text.
*/
LINPHONE_PUBLIC const char* linphone_chat_message_get_text(const LinphoneChatMessage* message);
LINPHONE_PUBLIC const char* linphone_chat_message_get_text(LinphoneChatMessage* message);
/**
* Get the time the message was sent.