From 857475231283b510489a9e16c5e1b5d56232168f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20Turnel?= Date: Wed, 6 Dec 2017 16:05:52 +0100 Subject: [PATCH] Add UTF8 conversion for chat messages --- coreapi/chat.c | 19 ++++++++-- coreapi/message_storage.c | 50 ++------------------------ coreapi/private.h | 1 + coreapi/sqlite3_bctbx_vfs.c | 70 ++----------------------------------- include/linphone/chat.h | 2 +- 5 files changed, 24 insertions(+), 118 deletions(-) diff --git a/coreapi/chat.c b/coreapi/chat.c index 035520f96..16367cc87 100644 --- a/coreapi/chat.c +++ b/coreapi/chat.c @@ -28,6 +28,7 @@ #include "belle-sip/belle-sip.h" #include "ortp/b64.h" #include "linphone/wrapper_utils.h" +#include "bctoolbox/charconv.h" #include #include @@ -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) { diff --git a/coreapi/message_storage.c b/coreapi/message_storage.c index 11a2d8a6e..c81845d4e 100644 --- a/coreapi/message_storage.c +++ b/coreapi/message_storage.c @@ -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 -#include -#include -#include -#include -#endif -#else -#include -#endif - -#define MAX_PATH_SIZE 1024 - #include "sqlite3.h" #include -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); diff --git a/coreapi/private.h b/coreapi/private.h index 9b511de63..1537834d5 100644 --- a/coreapi/private.h +++ b/coreapi/private.h @@ -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; diff --git a/coreapi/sqlite3_bctbx_vfs.c b/coreapi/sqlite3_bctbx_vfs.c index 882ab7368..1c1a5aeb3 100755 --- a/coreapi/sqlite3_bctbx_vfs.c +++ b/coreapi/sqlite3_bctbx_vfs.c @@ -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 @@ -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 -# include -# include -# include -#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(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(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); diff --git a/include/linphone/chat.h b/include/linphone/chat.h index c3d6ab006..0195e7cba 100644 --- a/include/linphone/chat.h +++ b/include/linphone/chat.h @@ -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.