2 * Copyright 2012 Samsung Electronics Co., Ltd
4 * Licensed under the Flora License, Version 1.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://floralicense.org/license
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 /*==================================================================================================
19 * ==================================================================================================*/
20 #include "msg-ui-viewer-utility.h"
21 #include <glib-object.h>
23 #include <msg-ui-util.h>
26 #include <Ecore_File.h>
27 #include <media_content.h>
29 /*==================================================================================================
30 * FUNCTION DECLARATIONS
31 *==================================================================================================*/
32 static void __msg_viewer_util_get_color_code(int color_dec);
34 /*==================================================================================================
35 * FUNCTION IMPLEMENTATIONS
36 *================================================================================================*/
37 /* Note: Caller should free returned string after using it*/
38 char *msg_viewer_util_get_display_name_by_index(int index)
40 MSG_UI_ENTER(MSG_UI_LEVEL_DEBUG);
41 MSG_UI_DEBUG(MSG_UI_LEVEL_DEBUG, "Label Contact Index [%d]", index);
43 contacts_record_h contact = NULL;
44 contacts_record_h record = NULL;
47 char *display_name = NULL;
51 ct_err = contacts_db_get_record(_contacts_contact._uri, index, &contact);
53 if (ct_err != CONTACTS_ERROR_NONE) {
54 MSG_UI_DEBUG(MSG_UI_LEVEL_ERR,"contacts_db_get_record is failed error_code = %d", ct_err);
58 /* get display name */
59 ct_err = contacts_record_get_str_p(contact, _contacts_contact.display_name, &main_name);
61 if (ct_err != CONTACTS_ERROR_NONE) {
62 MSG_UI_DEBUG(MSG_UI_LEVEL_ERR,"contacts_record_get_str_p is failed error_code = %d", ct_err);
63 contacts_record_destroy(contact, true);
68 display_name = g_strdup(main_name);
70 ct_err = contacts_record_get_child_record_at_p(contact, _contacts_contact.name, 0, &record);
72 if (ct_err != CONTACTS_ERROR_NONE) {
73 MSG_UI_DEBUG(MSG_UI_LEVEL_ERR,"contacts_record_get_child_record_at_p is failed error_code = %d", ct_err);
74 contacts_record_destroy(contact, true);
79 ct_err = contacts_record_get_str_p(record, _contacts_name.first, &first);
81 if (ct_err != CONTACTS_ERROR_NONE) {
82 MSG_UI_DEBUG(MSG_UI_LEVEL_ERR,"contacts_record_get_str_p is failed error_code = %d", ct_err);
83 contacts_record_destroy(contact, true);
88 ct_err = contacts_record_get_str_p(record, _contacts_name.last, &last);
90 if (ct_err != CONTACTS_ERROR_NONE) {
91 MSG_UI_DEBUG(MSG_UI_LEVEL_ERR,"contacts_record_get_str_p is failed error_code = %d", ct_err);
92 contacts_record_destroy(contact, true);
96 if (first != NULL && last != NULL) {
97 contacts_name_display_order_e name_display_order;
98 ct_err = contacts_setting_get_name_display_order(&name_display_order);
100 if (ct_err != CONTACTS_ERROR_NONE) {
101 MSG_UI_DEBUG(MSG_UI_LEVEL_ERR,"contacts_setting_get_name_display_order is failed error_code = %d", ct_err);
102 contacts_record_destroy(contact, true);
106 if (CONTACTS_NAME_DISPLAY_ORDER_FIRSTLAST == name_display_order) {
107 display_name = g_strconcat(first, " ", last, NULL);
109 display_name = g_strconcat(last, " ", first, NULL);
111 } else if (first != NULL || last != NULL) {
112 display_name = (first != NULL) ? g_strdup(first) : g_strdup(last);
114 MSG_UI_DEBUG(MSG_UI_LEVEL_ERR, "get display name has failed!");
118 contacts_record_destroy(contact, true);
123 MSG_BOOL msg_viewer_util_get_file_ext(const char *filepath, char *file_ext, int ext_len)
125 MSG_UI_RETV_IF(MSG_UI_LEVEL_ERR, filepath == NULL, FALSE);
126 MSG_UI_RETV_IF(MSG_UI_LEVEL_ERR, file_ext == NULL, FALSE);
127 const char *filename = NULL;
130 filename = (const char *)ecore_file_file_get(filepath);
131 MSG_UI_RETV_IF(MSG_UI_LEVEL_ERR, filename == NULL, FALSE);
133 pdot = strrchr(filename, '.');
137 } else if (pdot != filepath) {
138 strncpy(file_ext, pdot + 1, ext_len);
145 int msg_viewer_util_get_next_number(char *file_name_without_ext)
147 MSG_UI_RETV_IF(MSG_UI_LEVEL_ERR, file_name_without_ext == NULL, MSG_VIEWER_INVALID_FILE_NUMBER);
151 MSG_BOOL bAllDigits = TRUE;
154 /* check _02d format */
155 nLength = strlen(file_name_without_ext);
157 for (nUnderline = nLength - 1 ; nUnderline >= 0 ; nUnderline--) {
158 if (file_name_without_ext[nUnderline] == '_') {
163 if (nUnderline == 0 && file_name_without_ext[0] != '_') {
164 return MSG_VIEWER_DEFAULT_FILE_NUMBER; /*doesn't match*/
167 /* check the right characters are all digits */
168 for (i = nUnderline + 1 ; i < nLength ; i++) {
169 if (file_name_without_ext[i] < '0' || file_name_without_ext[i] > '9') {
176 for (i = nUnderline + 1 ; i < nLength ; i++) {
178 nCount += file_name_without_ext[i]-'0';
180 /* truncate the last '_dd' */
181 file_name_without_ext[nUnderline] = '\0';
184 /* increase nCount by 1*/
190 char hexa_data[MSG_VIEWER_COLOR_HEXA_BUFFER_LEN];
191 char hexa_value[MSG_VIEWER_HEXA_SIZE] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
192 static void __msg_viewer_util_get_color_code(int color_dec)
197 memset(hexa_data, 0x00, sizeof(hexa_data));
198 rem = col % MSG_VIEWER_HEXA_SIZE;
199 col = col / MSG_VIEWER_HEXA_SIZE;
200 hexa_data[0] = hexa_value[col];
201 hexa_data[1] = hexa_value[rem];
204 void msg_viewer_util_get_color_as_string(char *szcolor, int szcolor_len, int r, int g, int b)
206 MSG_UI_RET_IF(MSG_UI_LEVEL_ERR, szcolor == NULL);
207 MSG_UI_RET_IF(MSG_UI_LEVEL_ERR, szcolor_len < MSG_VIEWER_RGB_COLOR_BUF);
208 char col_hex[MSG_VIEWER_RGB_COLOR_BUF] = {0x0,};
210 __msg_viewer_util_get_color_code(r);
211 col_hex[0] = hexa_data[0];
212 col_hex[1] = hexa_data[1];
213 __msg_viewer_util_get_color_code(g);
214 col_hex[2] = hexa_data[0];
215 col_hex[3] = hexa_data[1];
216 __msg_viewer_util_get_color_code(b);
217 col_hex[4] = hexa_data[0];
218 col_hex[5] = hexa_data[1];
221 snprintf(szcolor, MSG_VIEWER_RGB_COLOR_BUF, "%s", col_hex);
224 MSG_BOOL msg_viewer_util_get_default_path(char *dir_path, int dir_path_len)
226 MSG_UI_RETV_IF(MSG_UI_LEVEL_ERR, dir_path == NULL, FALSE);
228 if (ecore_file_is_dir(MSG_VIEWER_DOWNLOAD_PATH) == EINA_FALSE) {
229 if (ecore_file_mkdir(MSG_VIEWER_DOWNLOAD_PATH) == EINA_FALSE) {
230 MSG_UI_DEBUG(MSG_UI_LEVEL_ERR, "ecore_file_mkdir is failed");
231 snprintf(dir_path, dir_path_len, "%s", MSG_VIEWER_CONTENT_PATH);
233 snprintf(dir_path, dir_path_len, "%s", MSG_VIEWER_DOWNLOAD_PATH);
236 snprintf(dir_path, dir_path_len, "%s", MSG_VIEWER_DOWNLOAD_PATH);
242 unsigned char *msg_ui_viewer_get_buffer_from_file(const char *filepath, const char *opt, unsigned int *buffer_size)
244 MSG_UI_ENTER(MSG_UI_LEVEL_DEBUG);
245 MSG_UI_RETV_IF(MSG_UI_LEVEL_ERR, filepath == NULL, NULL);
246 MSG_UI_RETV_IF(MSG_UI_LEVEL_ERR, buffer_size == NULL, NULL);
251 pFile = fopen(filepath, opt);
253 MSG_UI_DEBUG(MSG_UI_LEVEL_ERR, "File Open Error");
257 if (fseek(pFile, 0, SEEK_END) < 0) {
259 MSG_UI_DEBUG(MSG_UI_LEVEL_ERR, "File fseek Error: ");
263 int file_size = ftell(pFile);
265 if (file_size <= 0) {
266 MSG_UI_DEBUG(MSG_UI_LEVEL_ERR, "Filesize is error : %d", file_size);
271 if (fseek(pFile, 0, SEEK_SET) < 0) {
273 MSG_UI_DEBUG(MSG_UI_LEVEL_ERR, "File seek Error");
277 unsigned char *buffer = (unsigned char *)malloc(file_size);
280 MSG_UI_DEBUG(MSG_UI_LEVEL_ERR, "buffer is NULL");
285 if (fread(buffer, sizeof(char), file_size, pFile) != (size_t)file_size) {
288 MSG_UI_DEBUG(MSG_UI_LEVEL_ERR, "File Read Error");
292 *buffer_size = file_size;
298 char *msg_ui_viewer_util_copy_file(const char *src_path, char *dst_path)
300 MSG_UI_ENTER(MSG_UI_LEVEL_DEBUG);
301 MSG_UI_RETV_IF(MSG_UI_LEVEL_ERR, src_path == NULL, NULL);
302 MSG_UI_RETV_IF(MSG_UI_LEVEL_ERR, dst_path == NULL, NULL);
303 char file_ext[MSG_VIEWER_FILE_EXT_MAX_LEN + 1] = {0};
304 char file_name_without_ext[MSG_VIEWER_FILENAME_MAX_LEN + 1] = {0};
305 char new_file_name[MSG_VIEWER_FILENAME_MAX_LEN + 1] = {0};
306 MSG_BOOL result = FALSE;
308 Eina_Bool is_exists = FALSE;
310 snprintf(new_file_name, sizeof(new_file_name), "%s", dst_path);
312 /*check for same file*/
313 is_exists = ecore_file_exists(new_file_name);
314 if (is_exists == EINA_TRUE) {
315 strncpy(file_name_without_ext, dst_path, MSG_VIEWER_FILENAME_MAX_LEN);
316 result = msg_viewer_util_get_file_ext(dst_path, file_ext, MSG_VIEWER_FILE_EXT_MAX_LEN);
318 file_name_without_ext[strlen(file_name_without_ext) - strlen(file_ext) - 1] = '\0';
320 ncount = MSG_VIEWER_DEFAULT_FILE_NUMBER;
321 memset(new_file_name, 0x00, sizeof(new_file_name));
323 /*make new filename*/
325 if (strlen(file_ext) > 0)
326 snprintf(new_file_name, sizeof(new_file_name), "%s_%02d.%s", file_name_without_ext, ncount, file_ext);
328 snprintf(new_file_name, sizeof(new_file_name), "%s_%02d", file_name_without_ext, ncount);
329 /*check for same file*/
330 is_exists = ecore_file_exists(new_file_name);
331 if (is_exists == EINA_TRUE) {
332 memset(new_file_name, 0x00, sizeof(new_file_name));
340 MSG_UI_DEBUG(MSG_UI_LEVEL_DEBUG, "new_file_name = %s", new_file_name);
342 media_content_connect();
344 if (ecore_file_cp(src_path, new_file_name) == EINA_FALSE) {
345 MSG_UI_DEBUG(MSG_UI_LEVEL_ERR, "ecore_file_cp is failed");
346 media_content_disconnect();
350 if (media_content_scan_file(new_file_name) != MEDIA_CONTENT_ERROR_NONE) {
351 MSG_UI_DEBUG(MSG_UI_LEVEL_ERR, "media_content_scan_file() is failed");
352 media_content_disconnect();
356 media_content_disconnect();
358 return strdup(new_file_name);
361 char *msg_ui_viewer_util_create_working_dir(void)
364 char *base_dir = "/tmp/message";
365 char working_path[DEF_PATH_MAX] = {0,};
369 if (ecore_file_exists(base_dir) == EINA_FALSE) {
370 if(ecore_file_mkdir(base_dir) == EINA_FALSE) {
377 bzero(working_path, DEF_PATH_MAX);
378 snprintf(working_path, sizeof(working_path)-1, "%s/viewer_%d/", base_dir, i);
379 if (ecore_file_exists(working_path) == EINA_FALSE) {
380 if (ecore_file_mkdir(working_path) == EINA_FALSE) {
383 D_MSG("Success to create working path = %s", working_path);
384 return strdup(working_path);