summaryrefslogtreecommitdiff
path: root/common/src/email-html-converter.c
diff options
context:
space:
mode:
Diffstat (limited to 'common/src/email-html-converter.c')
-rwxr-xr-xcommon/src/email-html-converter.c215
1 files changed, 215 insertions, 0 deletions
diff --git a/common/src/email-html-converter.c b/common/src/email-html-converter.c
new file mode 100755
index 0000000..2d79e20
--- /dev/null
+++ b/common/src/email-html-converter.c
@@ -0,0 +1,215 @@
+/*
+ * Copyright 2012 Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.tizenopensource.org/license
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <sys/types.h>
+
+#include <glib.h>
+#include <glib/gprintf.h>
+
+#include "email-common-types.h"
+#include "email-debug.h"
+#include "email-utils.h"
+#include "email-html-converter.h"
+
+#define _USE_HTML_DUMP_ 0
+
+static const gchar *_get_html_tag(const gchar char_val)
+{
+ const gchar *tag = NULL;
+
+ switch (char_val) {
+ case '\n':
+ tag = "<br>";
+ break;
+ case '\r':
+ tag = ""; /* skip. */
+ break;
+ case '<':
+ tag = "&lt";
+ break;
+ case '>':
+ tag = "&gt";
+ break;
+ default:
+ break;
+ }
+ return tag;
+}
+
+/* g_free(). */
+static gchar *_get_parse_plain_text(const gchar *text, gint size)
+{
+ RETURN_VAL_IF_FAIL(STR_VALID(text), NULL);
+ RETURN_VAL_IF_FAIL(size > 0, NULL);
+
+ gchar *html = NULL;
+ gchar *buff = NULL;
+ gchar *temp = NULL;
+ guint i = 0;
+ guint old_offset = 0, offset = 0;
+
+ for (i = 0; i < size; ++i) {
+
+ /* Get html tag. */
+ const gchar *tag = _get_html_tag(text[i]);
+
+ /* If need, convert text to html format. */
+ if (tag) {
+
+ temp = html;
+
+ if (i > old_offset) {
+ offset = i - old_offset;
+ buff = g_strndup(&text[old_offset], offset);
+ if (temp) {
+ html = g_strconcat(temp, buff, tag, NULL);
+ } else {
+ html = g_strconcat(buff, tag, NULL);
+ }
+ if (buff) {
+ g_free(buff);
+ buff = NULL;
+ }
+ } else {
+ if (temp) {
+ html = g_strconcat(temp, tag, NULL);
+ } else {
+ html = g_strconcat(tag, NULL);
+ }
+ }
+
+ if (temp) {
+ g_free(temp);
+ temp = NULL;
+ }
+
+ old_offset = i + 1;
+ }
+ }
+
+ /* If not terminated. */
+ if (old_offset < size) {
+ if (html) {
+ temp = html;
+ buff = g_strndup(&text[old_offset], size - old_offset);
+ html = g_strconcat(temp, buff, NULL);
+ if (buff) {
+ g_free(buff);
+ }
+ g_free(temp);
+ }
+ }
+
+ return html ? html : g_strndup(text, size);
+}
+
+static void _dump_buff(const gchar *buff)
+{
+ RETURN_IF_FAIL(STR_VALID(buff));
+
+#if _USE_HTML_DUMP_ == 1
+ tzset(); /* MUST BE. */
+ time_t now = time(NULL);
+ struct tm *t = localtime(&now);
+
+ gchar buff_path[MAX_STR_LEN] = "\0";
+
+ g_sprintf(buff_path, "%s/%04d.%02d.%02d[%02dH.%02dM].html", DIR_SRV_PREFIX, t->tm_year + 1900, t->tm_mon, t->tm_mday, t->tm_hour, t->tm_min);
+
+ debug_log("path [%s]", buff_path);
+
+ email_save_file(buff_path, buff, STR_LEN((gchar *)buff));
+#endif /* _USE_HTML_DUMP_ */
+}
+
+/* Exported API.
+ */
+gchar *email_html_converter(const gchar *plain_text, const gchar *charset)
+{
+ /*debug_time();*/
+ char meta_charset[MAX_STR_LEN] = { 0, };
+ g_snprintf(meta_charset, sizeof(meta_charset), "<html><head><meta name=\"viewport\" content=\"width=device-width, "
+ "initial-scale=0.8, maximum-scale=2.0, minimum-scale=0.5, user-scalable=yes, target-densitydpi=medium-dpi\"/>"
+ "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=%s\">", charset);
+ debug_log("meta charset:%s", meta_charset);
+
+ /*const char *text_css = "<STYLE type=\"text/css\"> \
+body {background-color: #1a1a1a; \
+color: #ffffff; \
+font-size: 32px; \
+margin-left: 18px; \
+margin-right: 18px; \
+margin-top: 8px; \
+margin-bottom: 8px;} \
+p {font-size: 32px;} \
+table {font-size: 32px;} \
+a:link {color: white} \
+a:visited {color: red} \
+a:active {color: red; background: #1a1a1a} \
+</STYLE></head><body>";*/
+
+ const char *text_css = "<STYLE type=\"text/css\"> \
+body {font-size: 16px; \
+margin-left: 18px; \
+margin-right: 18px; \
+margin-top: 8px; \
+margin-bottom: 8px;} \
+p {font-size: 16px;} \
+table {font-size: 16px;} \
+</STYLE></head><body>";
+
+ const gchar *html_etag = "</body></html>";
+ gchar *html_stag = NULL;
+ gchar *html = NULL;
+
+ html_stag = g_strconcat(meta_charset, text_css, NULL);
+
+ if (STR_INVALID(plain_text)) {
+ debug_log("empty file detected");
+ html = g_strconcat(html_stag, html_etag, NULL);
+ g_free(html_stag);
+ return html;
+ }
+
+ /*
+ debug_log("plain-text [%s]", plain_text);
+ */
+
+ gchar *text = NULL;
+ gchar *buff = NULL;
+
+ if (STR_INVALID(html)) {
+ debug_log("not found cut-link - convert plain/text to html");
+ text = _get_parse_plain_text(plain_text, STR_LEN((gchar *)plain_text));
+ buff = g_strconcat(html_stag, text, NULL);
+ if (text)
+ g_free(text);
+ html = g_strconcat(buff, html_etag, NULL);
+ g_free(buff);
+ }
+ /*debug_time();*/
+
+ _dump_buff(html);
+
+ if (html_stag)
+ g_free(html_stag);
+
+ return html;
+}
+
+/* EOF */