f0f1325c5d8f79582744da84b2aa791e8db1c4c7
[apps/core/preloaded/message-app.git] / viewer / src / msg-ui-viewer-utility.c
1 /*
2  * Copyright 2012  Samsung Electronics Co., Ltd
3  *
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
7  *
8  *    http://floralicense.org/license
9  *
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.
15  */
16
17 /*==================================================================================================
18  *                                                              INCLUDE HEADERS
19  * ==================================================================================================*/
20 #include "msg-ui-viewer-utility.h"
21 #include <glib-object.h>
22 #include <glib.h>
23 #include <msg-ui-util.h>
24 #include <contacts.h>
25 #include <ctype.h>
26 #include <Ecore_File.h>
27 #include <media_content.h>
28
29 /*==================================================================================================
30  *                                                                      FUNCTION DECLARATIONS
31  *==================================================================================================*/
32 static void __msg_viewer_util_get_color_code(int color_dec);
33
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)
39 {
40         MSG_UI_ENTER(MSG_UI_LEVEL_DEBUG);
41         MSG_UI_DEBUG(MSG_UI_LEVEL_DEBUG, "Label Contact Index [%d]", index);
42
43         contacts_record_h contact = NULL;
44         contacts_record_h record = NULL;
45         char *first = NULL;
46         char *last = NULL;
47         char *display_name = NULL;
48         char *main_name;
49         int ct_err;
50
51         ct_err = contacts_db_get_record(_contacts_contact._uri, index, &contact);
52
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);
55                 return NULL;
56         }
57
58         /* get display name */
59         ct_err = contacts_record_get_str_p(contact, _contacts_contact.display_name, &main_name);
60
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);
64                 return NULL;
65         }
66
67         if (main_name) {
68                 display_name = g_strdup(main_name);
69         } else {
70                 ct_err = contacts_record_get_child_record_at_p(contact, _contacts_contact.name, 0, &record);
71
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);
75                         return NULL;
76                 }
77
78                 /* get first name */
79                 ct_err = contacts_record_get_str_p(record, _contacts_name.first, &first);
80
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);
84                         return NULL;
85                 }
86
87                 /* get last name */
88                 ct_err = contacts_record_get_str_p(record, _contacts_name.last, &last);
89
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);
93                         return NULL;
94                 }
95
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);
99
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);
103                                 return NULL;
104                         }
105
106                         if (CONTACTS_NAME_DISPLAY_ORDER_FIRSTLAST == name_display_order) {
107                                 display_name = g_strconcat(first, " ", last, NULL);
108                         } else {
109                                 display_name = g_strconcat(last, " ", first, NULL);
110                         }
111                 } else if (first != NULL || last != NULL) {
112                         display_name = (first != NULL) ? g_strdup(first) : g_strdup(last);
113                 } else {
114                         MSG_UI_DEBUG(MSG_UI_LEVEL_ERR, "get display name has failed!");
115                 }
116         }
117
118         contacts_record_destroy(contact, true);
119
120         return display_name;
121 }
122
123 MSG_BOOL msg_viewer_util_get_file_ext(const char *filepath, char *file_ext, int ext_len)
124 {
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;
128         char *pdot = NULL;
129
130         filename = (const char *)ecore_file_file_get(filepath);
131         MSG_UI_RETV_IF(MSG_UI_LEVEL_ERR, filename == NULL, FALSE);
132
133         pdot = strrchr(filename, '.');
134
135         if (!pdot) {
136                 return FALSE;
137         } else if (pdot != filepath) {
138                 strncpy(file_ext, pdot + 1, ext_len);
139                 return TRUE;
140         } else {
141                 return FALSE;
142         }
143 }
144
145 int msg_viewer_util_get_next_number(char *file_name_without_ext)
146 {
147         MSG_UI_RETV_IF(MSG_UI_LEVEL_ERR, file_name_without_ext == NULL, MSG_VIEWER_INVALID_FILE_NUMBER);
148         int nCount = 0;
149         int nLength = 0;
150         int nUnderline = 0;
151         MSG_BOOL bAllDigits = TRUE;
152         int i = 0;
153
154         /* check _02d format */
155         nLength = strlen(file_name_without_ext);
156
157         for (nUnderline = nLength - 1 ; nUnderline >= 0 ; nUnderline--) {
158                 if (file_name_without_ext[nUnderline] == '_') {
159                         break;
160                 }
161         }
162
163         if (nUnderline == 0 && file_name_without_ext[0] != '_') {
164                 return MSG_VIEWER_DEFAULT_FILE_NUMBER; /*doesn't match*/
165         }
166
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') {
170                         bAllDigits = FALSE;
171                         break;
172                 }
173         }
174
175         if (bAllDigits) {
176                 for (i = nUnderline + 1 ; i < nLength ; i++) {
177                         nCount *= 10;
178                         nCount += file_name_without_ext[i]-'0';
179                 }
180                 /* truncate the last '_dd' */
181                 file_name_without_ext[nUnderline] = '\0';
182         }
183
184         /* increase nCount by 1*/
185         nCount++;
186
187         return nCount;
188 }
189
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)
193 {
194         int col = color_dec;
195         int rem = 0;
196
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];
202 }
203
204 void msg_viewer_util_get_color_as_string(char *szcolor, int szcolor_len, int r, int g, int b)
205 {
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,};
209         /* Get color*/
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];
219         col_hex[6] = '\0';
220
221         snprintf(szcolor, MSG_VIEWER_RGB_COLOR_BUF, "%s", col_hex);
222 }
223
224 MSG_BOOL msg_viewer_util_get_default_path(char *dir_path, int dir_path_len)
225 {
226         MSG_UI_RETV_IF(MSG_UI_LEVEL_ERR, dir_path == NULL, FALSE);
227
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);
232                 } else {
233                         snprintf(dir_path, dir_path_len, "%s", MSG_VIEWER_DOWNLOAD_PATH);
234                 }
235         } else {
236                 snprintf(dir_path, dir_path_len, "%s", MSG_VIEWER_DOWNLOAD_PATH);
237         }
238
239         return TRUE;
240 }
241
242 unsigned char *msg_ui_viewer_get_buffer_from_file(const char *filepath, const char *opt, unsigned int *buffer_size)
243 {
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);
247
248         *buffer_size = 0;
249         FILE *pFile = NULL;
250
251         pFile = fopen(filepath, opt);
252         if (pFile == NULL) {
253                 MSG_UI_DEBUG(MSG_UI_LEVEL_ERR, "File Open Error");
254                 return NULL;
255         }
256
257         if (fseek(pFile, 0, SEEK_END) < 0) {
258                 fclose(pFile);
259                 MSG_UI_DEBUG(MSG_UI_LEVEL_ERR, "File fseek Error: ");
260                 return NULL;
261         }
262
263         int file_size = ftell(pFile);
264
265         if (file_size <= 0) {
266                 MSG_UI_DEBUG(MSG_UI_LEVEL_ERR, "Filesize is error : %d", file_size);
267                 fclose(pFile);
268                 return NULL;
269         }
270
271         if (fseek(pFile, 0, SEEK_SET) < 0) {
272                 fclose(pFile);
273                 MSG_UI_DEBUG(MSG_UI_LEVEL_ERR, "File seek Error");
274                 return NULL;
275         }
276
277         unsigned char *buffer = (unsigned char *)malloc(file_size);
278
279         if (!buffer) {
280                 MSG_UI_DEBUG(MSG_UI_LEVEL_ERR, "buffer is NULL");
281                 fclose(pFile);
282                 return NULL;
283         }
284
285         if (fread(buffer, sizeof(char), file_size, pFile) != (size_t)file_size) {
286                 fclose(pFile);
287                 free(buffer);
288                 MSG_UI_DEBUG(MSG_UI_LEVEL_ERR, "File Read Error");
289                 return NULL;
290         }
291
292         *buffer_size = file_size;
293         fclose(pFile);
294
295         return buffer;
296 }
297
298 char *msg_ui_viewer_util_copy_file(const char *src_path, char *dst_path)
299 {
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;
307         int ncount = 0;
308         Eina_Bool is_exists = FALSE;
309
310         snprintf(new_file_name, sizeof(new_file_name), "%s", dst_path);
311
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);
317                 if (result)
318                         file_name_without_ext[strlen(file_name_without_ext) - strlen(file_ext) - 1] = '\0';
319
320                 ncount = MSG_VIEWER_DEFAULT_FILE_NUMBER;
321                 memset(new_file_name, 0x00, sizeof(new_file_name));
322
323                 /*make new filename*/
324                 do {
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);
327                         else
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));
333                                 ncount++;
334                         } else {
335                                 break;
336                         }
337                 } while (is_exists);
338         }
339
340         MSG_UI_DEBUG(MSG_UI_LEVEL_DEBUG, "new_file_name = %s", new_file_name);
341
342         media_content_connect();
343
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();
347                 return NULL;
348         }
349
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();
353                 return NULL;
354         }
355
356         media_content_disconnect();
357
358         return strdup(new_file_name);
359 }
360
361 char *msg_ui_viewer_util_create_working_dir(void)
362 {
363         D_ENTER;
364         char *base_dir = "/tmp/message";
365         char working_path[DEF_PATH_MAX] = {0,};
366         int i = 0;
367
368         /*Create base dir*/
369         if (ecore_file_exists(base_dir) == EINA_FALSE) {
370                 if(ecore_file_mkdir(base_dir) == EINA_FALSE) {
371                         return NULL;
372                 }
373         }
374
375         /*Create work dir*/
376         do {
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) {
381                                 return NULL;
382                         } else {
383                                 D_MSG("Success to create working path = %s", working_path);
384                                 return strdup(working_path);
385                         }
386                         break;
387                 }
388                 i++;
389         } while(i < 255);
390
391         return NULL;
392 }