From 253378d1670ab13be5bf50dd48c7214dc0c579f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20Turnel?= Date: Fri, 15 Dec 2017 10:37:37 +0100 Subject: [PATCH] Add conversion mechanism in Content so it is always UTF-8 --- include/linphone/utils/utils.h | 4 ++++ src/content/content.cpp | 14 +++++++++++++- src/content/content.h | 2 ++ src/utils/utils.cpp | 18 ++++++++++++++++++ 4 files changed, 37 insertions(+), 1 deletion(-) diff --git a/include/linphone/utils/utils.h b/include/linphone/utils/utils.h index b11154255..fa0a55db8 100644 --- a/include/linphone/utils/utils.h +++ b/include/linphone/utils/utils.h @@ -107,6 +107,10 @@ namespace Utils { LINPHONE_PUBLIC std::tm getTimeTAsTm (time_t time); LINPHONE_PUBLIC time_t getTmAsTimeT (const std::tm &time); + + LINPHONE_PUBLIC std::string localeToUtf8 (const std::string &str); + LINPHONE_PUBLIC std::string utf8ToLocale (const std::string &str); + LINPHONE_PUBLIC std::string convertString (const std::string &str, const std::string &from, const std::string &to); } LINPHONE_END_NAMESPACE diff --git a/src/content/content.cpp b/src/content/content.cpp index 8b309431d..9c8fa4e09 100644 --- a/src/content/content.cpp +++ b/src/content/content.cpp @@ -19,6 +19,7 @@ // TODO: Remove me later. #include "linphone/core.h" +#include "linphone/utils/utils.h" #include "content-p.h" #include "content-type.h" @@ -117,6 +118,11 @@ const vector &Content::getBody () const { } string Content::getBodyAsString () const { + L_D(); + return Utils::utf8ToLocale(string(d->body.begin(), d->body.end())); +} + +string Content::getBodyAsUtf8String () const { L_D(); return string(d->body.begin(), d->body.end()); } @@ -133,7 +139,8 @@ void Content::setBody (vector &&body) { void Content::setBody (const string &body) { L_D(); - d->body = vector(body.cbegin(), body.cend()); + string toUtf8 = Utils::localeToUtf8(body); + d->body = vector(toUtf8.cbegin(), toUtf8.cend()); } void Content::setBody (const void *buffer, size_t size) { @@ -142,6 +149,11 @@ void Content::setBody (const void *buffer, size_t size) { d->body = vector(start, start + size); } +void Content::setBodyFromUtf8 (const string &body) { + L_D(); + d->body = vector(body.cbegin(), body.cend()); +} + size_t Content::getSize () const { L_D(); return d->body.size(); diff --git a/src/content/content.h b/src/content/content.h index 2cdaa4d73..8e38b22c2 100644 --- a/src/content/content.h +++ b/src/content/content.h @@ -55,11 +55,13 @@ public: const std::vector &getBody () const; std::string getBodyAsString () const; + std::string getBodyAsUtf8String () const; void setBody (const std::vector &body); void setBody (std::vector &&body); void setBody (const std::string &body); void setBody (const void *buffer, size_t size); + void setBodyFromUtf8 (const std::string &body); size_t getSize () const; diff --git a/src/utils/utils.cpp b/src/utils/utils.cpp index e3819de39..c60a8141c 100644 --- a/src/utils/utils.cpp +++ b/src/utils/utils.cpp @@ -22,6 +22,7 @@ #include #include +#include #include "linphone/utils/utils.h" @@ -184,4 +185,21 @@ long Utils::getTmAsTimeT (const tm &time) { return timegm(&const_cast(time)); } +// ----------------------------------------------------------------------------- + +string Utils::localeToUtf8 (const string &str) { + char *ret = bctbx_locale_to_utf8(str.c_str()); + return string(ret != NULL ? ret : ""); +} + +string Utils::utf8ToLocale (const string &str) { + char *ret = bctbx_utf8_to_locale(str.c_str()); + return string(ret != NULL ? ret : ""); +} + +string Utils::convertString (const string &str, const string &from, const string &to) { + char *ret = bctbx_convert_from_to(str.c_str(), from.c_str(), to.c_str()); + return string(ret != NULL ? ret : ""); +} + LINPHONE_END_NAMESPACE