e03e98ac87d867f1f08b630bfaa5e15cc41c4bf9
[platform/core/system/sync-agent.git] / src / fw-plugins / common-public / vcalendar / src / vcalendar_string_util.c
1 /*
2  * sync-agent
3  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
4  *
5  * Licensed under the Apache License, Version 2.0 (the License);
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include "vcalendar_string_util.h"
19
20 #define DATE_TIME_LENGTH 16
21
22 #ifndef EXPORT_API
23 #define EXPORT_API __attribute__ ((visibility("default")))
24 #endif
25
26
27 #if 0
28 // usage / rrule converting
29 if (strstr(temp_agent_data, "\r\nRRULE:") != NULL) {
30         _DEBUG_INFO("rrule is founed");
31         temp_agent_data = _replace_rrule_data(temp_agent_data);
32 }
33
34 static char * _replace_rrule_data(char *origin)
35 {
36         _INNER_FUNC_ENTER;
37         retvm_if(origin == NULL, origin, "origin is NULL.");
38
39         char *str_new_rrule = NULL;
40         char *str_end_time = NULL;
41         char *freq_loc = NULL;
42         char *str_byday = NULL;
43         char *str_interval = NULL;
44         char *str_period_until = NULL;
45         char *str_period_count = NULL;
46         calendar_date_type_e cal_type = CALENDAR_DATE_TYPE_ONTIME;
47
48         if ((freq_loc = strstr(origin, RRULE_KEYWORD_FREQ_WEEKLY)) != NULL) {
49                 _DEBUG_INFO("this is weekly data");
50         }
51         else if ((freq_loc = strstr(origin, RRULE_KEYWORD_FREQ_MONTHLY)) != NULL) {
52                 _DEBUG_INFO("this is monthly data");
53         }
54         else if ((freq_loc = strstr(origin, RRULE_KEYWORD_FREQ_YEARLY)) != NULL) {
55                 _DEBUG_INFO("this is yearly data");
56         }
57         else if ((freq_loc = strstr(origin, RRULE_KEYWORD_FREQ_DAILY)) != NULL) {
58                 _DEBUG_INFO("this is daily data");
59         }
60         else {
61                 _DEBUG_ERROR("this is an undefined rrule type. origin = %s", origin);
62                 goto return_part;
63         }
64
65         char *temp_rrule = NULL;
66         if((temp_rrule = strstr(freq_loc, "RRULE:")) == NULL) {
67                 _DEBUG_ERROR("temp_rrule is NULL");
68                 goto return_part;
69         }
70
71         char *temp_rrule2 = NULL;
72         if((temp_rrule2 = strstr(temp_rrule, "\r\n")) == NULL) {
73                 _DEBUG_ERROR("temp_rrule2 is NULL");
74                 goto return_part;
75         }
76
77         char str_origin_rrule[100] = {0,};
78         int origin_rrule_length = strlen(temp_rrule) - strlen(temp_rrule2) + 2; // '2' means of '\r\n'
79         if (origin_rrule_length <= 0) {
80                 _DEBUG_ERROR("origin_rrule_length <= 0");
81                 goto return_part;
82         }
83
84         strncpy(str_origin_rrule, temp_rrule, origin_rrule_length);
85         _DEBUG_INFO("str_origin_rrule = %s", str_origin_rrule);
86
87         if (strstr(origin, RRULE_KEYWORD_FREQ_WEEKLY) != NULL) {
88                 str_byday = get_value_by_token(freq_loc, RRULE_KEYWORD_BYDAY, ";");
89                 cal_type = CALENDAR_DATE_TYPE_WEEK;
90         }
91         else if (strstr(origin, RRULE_KEYWORD_FREQ_MONTHLY) != NULL) {
92                 str_byday = get_value_by_token(freq_loc, RRULE_KEYWORD_BYMONTHDAY, ";");
93                 cal_type = CALENDAR_DATE_TYPE_MONTH;
94         }
95         else if (strstr(origin, RRULE_KEYWORD_FREQ_YEARLY) != NULL) {
96                 str_byday = get_value_by_token(freq_loc, RRULE_KEYWORD_BYMONTHDAY, ";");
97                 cal_type = CALENDAR_DATE_TYPE_YEAR;
98         }
99         else if (strstr(origin, RRULE_KEYWORD_FREQ_DAILY) != NULL) {
100                 cal_type = CALENDAR_DATE_TYPE_DAY;
101         }
102         else {
103                 _DEBUG_ERROR("this is an undefined rrule type. origin = %s", origin);
104                 goto return_part;
105         }
106
107         if ((cal_type != CALENDAR_DATE_TYPE_DAY) && (str_byday == NULL || strlen(str_byday) <= 0)) {
108                 _DEBUG_ERROR("str_byday is null");
109                 goto return_part;
110         }
111
112         str_interval = get_value_by_token(freq_loc, RRULE_KEYWORD_INTERVAL, ";");
113         str_period_until = get_value_by_token(freq_loc, RRULE_KEYWORD_UNTIL, ";");
114         str_period_count = get_value_by_token(freq_loc, RRULE_KEYWORD_COUNT, ";");
115
116         if (str_interval == NULL || strlen(str_interval) <= 0) {
117                 _DEBUG_ERROR("str_interval is null");
118                 goto return_part;
119         }
120
121         if (str_period_until == NULL && str_period_count == NULL) {
122                 str_end_time = (char *)calloc(2, sizeof(char));
123                 if (str_end_time == NULL) {
124                         _DEBUG_ERROR("str_end_time is null");
125                         goto return_part;
126                 }
127                 strncpy(str_end_time, "\r\n", 2);
128                 _DEBUG_INFO("endless = %s", str_end_time);
129         }
130         else if (str_period_until != NULL) {
131                 str_end_time = (char *)calloc(strlen(str_period_until), sizeof(char));
132                 if (str_end_time == NULL) {
133                         _DEBUG_ERROR("str_end_time is null");
134                         goto return_part;
135                 }
136                 strncpy(str_end_time, str_period_until, strlen(str_period_until));
137                 _DEBUG_INFO("until = %s", str_end_time);
138         }
139         else if (str_period_count != NULL) {
140                 str_end_time = (char *)calloc(strlen(str_period_count)+1, sizeof(char));
141                 if (str_end_time == NULL) {
142                         _DEBUG_ERROR("str_end_time is null");
143                         goto return_part;
144                 }
145                 snprintf(str_end_time, strlen(str_period_count)+1, "#%s", str_period_count);
146                 _DEBUG_INFO("count = %s", str_end_time);
147         }
148         else {
149                 _DEBUG_ERROR("there is an error");
150                 goto return_part;
151         }
152
153         int new_rrule_length = 0;
154         if (strstr(origin, RRULE_KEYWORD_FREQ_WEEKLY) != NULL) {
155                 new_rrule_length = strlen("RRULE:W") + strlen(str_interval) + strlen(" ") + strlen(str_byday) + strlen("  ") + strlen(str_end_time) + 1;
156         }
157         else if (strstr(origin, RRULE_KEYWORD_FREQ_MONTHLY) != NULL) {
158                 new_rrule_length = strlen("RRULE:MD") + strlen(str_interval) + strlen(" ") + strlen(str_byday) + strlen("  ") + strlen(str_end_time) + 1;
159         }
160         else if (strstr(origin, RRULE_KEYWORD_FREQ_YEARLY) != NULL) {
161                 new_rrule_length = strlen("RRULE:YD") + strlen(str_interval) + strlen(" ") + strlen(str_end_time) + 1;
162         }
163         else if (strstr(origin, RRULE_KEYWORD_FREQ_DAILY) != NULL) {
164                 new_rrule_length = strlen("RRULE:D") + strlen(str_interval) + strlen(" ") + strlen(str_end_time) + 1;
165         }
166         else {
167                 _DEBUG_ERROR("this is an undefined rrule type. origin = %s", origin);
168                 goto return_part;
169         }
170
171         if (new_rrule_length <= 0) {
172                 _DEBUG_ERROR("new_rrule_length(%d) <= 0", new_rrule_length);
173                 goto return_part;
174         }
175
176         str_new_rrule = (char *)calloc(new_rrule_length, sizeof(char));
177         if (str_new_rrule == NULL) {
178                 _DEBUG_ERROR("str_new_rrule calloc failed");
179                 goto return_part;
180         }
181
182         if (strstr(origin, RRULE_KEYWORD_FREQ_WEEKLY) != NULL) {
183                 snprintf(str_new_rrule, new_rrule_length, "RRULE:W%s %s  %s", str_interval, str_byday, str_end_time);
184         }
185         else if (strstr(origin, RRULE_KEYWORD_FREQ_MONTHLY) != NULL) {
186                 snprintf(str_new_rrule, new_rrule_length, "RRULE:MD%s %s  %s", str_interval, str_byday, str_end_time);
187         }
188         else if (strstr(origin, RRULE_KEYWORD_FREQ_YEARLY) != NULL) {
189                 snprintf(str_new_rrule, new_rrule_length, "RRULE:YD%s %s", str_interval, str_end_time);
190         }
191         else if (strstr(origin, RRULE_KEYWORD_FREQ_DAILY) != NULL) {
192                 snprintf(str_new_rrule, new_rrule_length, "RRULE:D%s %s", str_interval, str_end_time);
193         }
194         else {
195                 _DEBUG_ERROR("this is an undefined rrule type. origin = %s", origin);
196                 goto return_part;
197         }
198
199         _DEBUG_INFO("str_new_rrule = %s", str_new_rrule);
200         _DEBUG_INFO("str_origin_rrule = %s", str_origin_rrule);
201
202         char *new_agent_data = replace_string(origin, str_origin_rrule, str_new_rrule);
203         origin = NULL;
204         origin = new_agent_data;
205
206 return_part:
207
208         if (str_interval) {
209                 free(str_interval);
210                 str_interval = NULL;
211         }
212         if (str_byday) {
213                 free(str_byday);
214                 str_byday = NULL;
215         }
216         if (str_period_until) {
217                 free(str_period_until);
218                 str_period_until = NULL;
219         }
220         if (str_period_count) {
221                 free(str_period_count);
222                 str_period_count = NULL;
223         }
224         if (str_end_time) {
225                 free(str_end_time);
226                 str_end_time = NULL;
227         }
228         if (str_new_rrule) {
229                 free(str_new_rrule);
230                 str_new_rrule = NULL;
231         }
232
233         _DEBUG_INFO("origin = %s", origin);
234         _INNER_FUNC_EXIT;
235         return origin;
236 }
237
238 // usage / alarm converting
239 if (strstr(temp_agent_data, "\r\nBEGIN:VALARM") != NULL) {
240         _DEBUG_INFO("alarm is founed");
241         temp_agent_data = _replace_alarm_data(temp_agent_data);
242 }
243
244 static char * _replace_alarm_data(char *origin)
245 {
246         _INNER_FUNC_ENTER;
247         retvm_if(origin == NULL, origin, "origin is NULL.");
248
249         int interval = 0;
250         char *str_new_alarm = NULL;
251         char *freq_loc = NULL;
252         char *str_trigger = NULL;
253         char *str_customized_trigger = NULL;
254         char *str_interval = NULL;
255         calendar_date_type_e cal_type = CALENDAR_DATE_TYPE_ONTIME;
256
257         if ((freq_loc = strstr(origin, BEGIN_ALARM_KEYWORD)) == NULL) {
258                 _DEBUG_ERROR("there is not trigger data");
259                 goto return_part;
260         }
261
262         char *temp = NULL;
263         if((temp = strstr(freq_loc, "BEGIN:VALARM\r\n")) == NULL) {
264                 _DEBUG_ERROR("temp is NULL");
265                 goto return_part;
266         }
267
268         char *temp2 = NULL;
269         if((temp2 = strstr(temp, "END:VALARM\r\n")) == NULL) {
270                 _DEBUG_ERROR("temp2 is NULL");
271                 goto return_part;
272         }
273
274         char str_origin_alarm[100] = {0,};
275         int origin_alarm_length = strlen(temp) - strlen(temp2) + 12; // '12' means of "END:VALARM\r\n"
276         if (origin_alarm_length <= 0) {
277                 _DEBUG_ERROR("origin_alarm_length <= 0");
278                 goto return_part;
279         }
280
281         strncpy(str_origin_alarm, temp, origin_alarm_length);
282         _DEBUG_INFO("str_origin_alarm = %s", str_origin_alarm);
283
284         // case 1. "TRIGGER;VALUE=DATE-TIME:" is an alarm that created by user
285         str_customized_trigger = _get_value_by_token(freq_loc, "TRIGGER;VALUE=DATE-TIME:", "\r\n");
286         if (str_customized_trigger != NULL) {
287                 _DEBUG_INFO("str_customized_trigger is founded. str_customized_trigger = %s", str_customized_trigger);
288
289                 str_new_alarm = (char *)calloc(30, sizeof(char));
290                 if (str_new_alarm == NULL) {
291                         _DEBUG_ERROR("new_date_time is null");
292                         goto return_part;
293                 }
294
295                 snprintf(str_new_alarm, 30, "AALARM:%s\r\n", str_customized_trigger);
296                 _DEBUG_INFO("str_new_alarm = %s", str_new_alarm);
297                 char *new_agent_data = replace_string(origin, str_origin_alarm, str_new_alarm);
298                 origin = NULL;
299                 origin = new_agent_data;
300                 goto return_part;
301         }
302
303         // case 2. "TRIGGER:" is a normal alarm
304         str_trigger = _get_value_by_token(freq_loc, "TRIGGER:", "\r\n");
305         if (str_trigger == NULL || strlen(str_trigger) <= 0) {
306                 _DEBUG_ERROR("str_trigger is null");
307                 goto return_part;
308         }
309
310         char *temp_alarm = NULL;
311         if((temp_alarm = strstr(str_trigger, "P0W")) != NULL) {
312                 cal_type = CALENDAR_DATE_TYPE_ONTIME;
313                 interval = 0;
314         }
315         else if((temp_alarm = strstr(str_trigger, "PT")) != NULL) {
316                 if(strstr(temp_alarm, "H") != NULL) {
317                         cal_type = CALENDAR_DATE_TYPE_HOUR;
318                         str_interval = _get_value_by_token(str_trigger, "PT", "H");
319                         interval = atoi(str_interval);
320                 }
321                 else if(strstr(temp_alarm, "M") != NULL) {
322                         cal_type = CALENDAR_DATE_TYPE_MINUTE;
323                         str_interval = _get_value_by_token(str_trigger, "PT", "M");
324                         interval = atoi(str_interval);
325                 }
326                 else {
327                         _DEBUG_ERROR("wrong type, temp_alarm = %s", temp_alarm);
328                         goto return_part;
329                 }
330         }
331         else if((temp_alarm = strstr(str_trigger, "P")) != NULL) {
332                 if(strstr(temp_alarm, "D") != NULL) {
333                         cal_type = CALENDAR_DATE_TYPE_DAY;
334                         str_interval = _get_value_by_token(str_trigger, "P", "D");
335                         interval = atoi(str_interval);
336                 }
337                 else if(strstr(temp_alarm, "W") != NULL) {
338                         cal_type = CALENDAR_DATE_TYPE_WEEK;
339                         str_interval = _get_value_by_token(str_trigger, "P", "W");
340                         interval = atoi(str_interval);
341                 }
342                 else {
343                         _DEBUG_ERROR("wrong type, temp_alarm = %s", temp_alarm);
344                         goto return_part;
345                 }
346         }
347         else {
348                 _DEBUG_ERROR("wrong type, temp_alarm = %s", temp_alarm);
349                 goto return_part;
350         }
351
352         str_new_alarm = _calculate_date_time(origin, interval, cal_type);
353         if (str_new_alarm == NULL || strlen(str_new_alarm) <= 0) {
354                 _DEBUG_ERROR("str_new_alarm is null");
355                 goto return_part;
356         }
357
358         _DEBUG_INFO("str_new_alarm = %s", str_new_alarm);
359         _DEBUG_INFO("str_origin_alarm = %s", str_origin_alarm);
360
361         char *new_agent_data = replace_string(origin, str_origin_alarm, str_new_alarm);
362         origin = NULL;
363         origin = new_agent_data;
364
365 return_part:
366
367         if (str_trigger) {
368                 free(str_trigger);
369                 str_trigger = NULL;
370         }
371         if (str_customized_trigger) {
372                 free(str_customized_trigger);
373                 str_customized_trigger = NULL;
374         }
375         if (str_new_alarm) {
376                 free(str_new_alarm);
377                 str_new_alarm = NULL;
378         }
379         if (str_interval) {
380                 free(str_interval);
381                 str_interval = NULL;
382         }
383
384         _INNER_FUNC_EXIT;
385         _DEBUG_INFO("origin = %s", origin);
386         return origin;
387 }
388
389
390 static char * _calculate_date_time(char *origin, int value, calendar_date_type_e type)
391 {
392         _INNER_FUNC_ENTER;
393         retvm_if(origin == NULL, NULL, "origin is NULL");
394
395         _DEBUG_INFO("origin = %s", origin);
396         _DEBUG_INFO("value = %d", value);
397
398         UCalendar *ucal = NULL;
399         char *start_time = NULL;
400         char *new_date_time = NULL;
401         char *start_date_loc = NULL;
402         if ((start_date_loc = strstr(origin, DTSTART_KEYWORD)) == NULL) {
403                 _DEBUG_ERROR("start_date_loc is null");
404                 goto return_part;
405         }
406
407         char str_year[5] = {0,};
408         char str_month[3] = {0,};
409         char str_date[3] = {0,};
410         char str_hour[3] = {0,};
411         char str_minute[3] = {0,};
412         char str_second[3] = {0,};
413
414         /* example) 20130124T000000Z\r\n = 2013 01 24 T 00 00 00 Z \r\n */
415         strncpy(str_year, start_date_loc + strlen(DTSTART_KEYWORD), 4);
416         strncpy(str_month, start_date_loc + strlen(DTSTART_KEYWORD) + 4, 2);
417         strncpy(str_date, start_date_loc + strlen(DTSTART_KEYWORD) + 4 + 2, 2);
418         strncpy(str_hour, start_date_loc + strlen(DTSTART_KEYWORD) + 4 + 2 + 2 + 1, 2);
419         strncpy(str_minute, start_date_loc + strlen(DTSTART_KEYWORD) + 4 + 2 + 2 + 1 + 2, 2);
420         strncpy(str_second, start_date_loc + strlen(DTSTART_KEYWORD) + 4 + 2 + 2 + 1 + 2 + 2, 2);
421
422         int year = atoi(str_year);
423         int month = atoi(str_month);
424         int date = atoi(str_date);
425         int hour = atoi(str_hour);
426         int minute = atoi(str_minute);
427         int second = atoi(str_second);
428         long long int date_time = 0;
429
430         start_time = (char *)calloc(20, sizeof(char));
431         if (start_time == NULL) {
432                 _DEBUG_ERROR("start_time is null");
433                 goto return_part;
434         }
435
436         snprintf(start_time, 20, "%04d%02d%02dT%02d%02d%02dZ\r\n", year, month, date, hour, minute, second);
437         _DEBUG_INFO("start_time = %s", start_time);
438
439         UErrorCode status = U_ZERO_ERROR;
440         ucal = ucal_open(0, -1, uloc_getDefault(), UCAL_TRADITIONAL, &status);
441         if (U_FAILURE(status)) {
442                 _DEBUG_ERROR("ucal_open failed (%s)", u_errorName(status));
443                 goto return_part;
444         }
445
446         ucal_setAttribute(ucal, UCAL_LENIENT, 1);
447         ucal_setDateTime(ucal, year, month - 1, date, hour, minute, second, &status);
448         date_time = ucal_getMillis(ucal, &status);
449
450         switch (type) {
451         case CALENDAR_DATE_TYPE_ONTIME:
452                 _DEBUG_INFO("CALENDAR_DATE_TYPE_ONTIME");
453                 date_time = date_time / 1000;
454                 break;
455         case CALENDAR_DATE_TYPE_WEEK:
456                 _DEBUG_INFO("CALENDAR_DATE_TYPE_WEEK");
457                 date_time = date_time / 1000;
458                 date_time = date_time - (value*60*60*24*7);
459                 break;
460         case CALENDAR_DATE_TYPE_DAY:
461                 _DEBUG_INFO("CALENDAR_DATE_TYPE_DAY");
462                 date_time = date_time / 1000;
463                 date_time = date_time - (value*60*60*24);
464                 break;
465         case CALENDAR_DATE_TYPE_HOUR:
466                 _DEBUG_INFO("CALENDAR_DATE_TYPE_HOUR");
467                 date_time = date_time / 1000;
468                 date_time = date_time - (value*60*60);
469                 break;
470         case CALENDAR_DATE_TYPE_MINUTE:
471                 _DEBUG_INFO("CALENDAR_DATE_TYPE_MINUTE");
472                 date_time = date_time / 1000;
473                 date_time = date_time - (value*60);
474                 break;
475         default:
476                 _DEBUG_ERROR("%d is not supported type", type);
477                 goto return_part;
478                 break;
479         }
480
481         if (U_FAILURE(status)) {
482                 _DEBUG_ERROR("ucal_setDateTime failed (%s)", u_errorName(status));
483                 goto return_part;
484         }
485
486         ucal_setMillis(ucal, (date_time * 1000), &status);
487
488         year = month = date = hour = minute = second = 0;
489         year = ucal_get(ucal, UCAL_YEAR, &status);
490         month = ucal_get(ucal, UCAL_MONTH, &status) + 1;
491         date = ucal_get(ucal, UCAL_DATE, &status);
492         hour = ucal_get(ucal, UCAL_HOUR_OF_DAY, &status);
493         minute = ucal_get(ucal, UCAL_MINUTE, &status);
494         second = ucal_get(ucal, UCAL_SECOND, &status);
495
496         new_date_time = (char *)calloc(30, sizeof(char));
497         if (new_date_time == NULL) {
498                 _DEBUG_ERROR("new_date_time is null");
499                 goto return_part;
500         }
501
502         snprintf(new_date_time, 30, "AALARM:%04d%02d%02dT%02d%02d%02dZ\r\n", year, month, date, hour, minute, second);
503         _DEBUG_INFO("new_date_time = %s", new_date_time);
504
505 return_part:
506         _DEBUG_INFO("return_part");
507
508         if(start_time) {
509                 free(start_time);
510                 start_time = NULL;
511         }
512         if(ucal) {
513                 ucal_close(ucal);
514                 ucal = NULL;
515         }
516
517         _INNER_FUNC_EXIT;
518         return new_date_time;
519 }
520
521 #endif
522
523 char * get_value_by_token(const char *src, const char *token, const char *end_of_token)
524 {
525         _INNER_FUNC_ENTER;
526         if (src == NULL || strlen(src) <= 0) {
527                 _DEBUG_ERROR("src is null");
528                 return NULL;
529         }
530
531         char *token_loc = NULL;
532         char *str_output = NULL;
533
534         if ((token_loc = strstr(src, token)) != NULL) {
535                 char *str_temp = strstr(token_loc, end_of_token);
536                 if (str_temp != NULL) {
537                         int ret_str_length = strlen(token_loc) - strlen(token) - strlen(str_temp);
538                         if (ret_str_length <= 0) {
539                                 _DEBUG_ERROR("token_loc = %s", token_loc);
540                                 _DEBUG_ERROR("token = %s", token);
541                                 _DEBUG_ERROR("str_temp = %s", str_temp);
542                                 _DEBUG_ERROR("ret_str_length(%d) <= 0", ret_str_length);
543                                 goto return_part;
544                         }
545                         str_output = (char *)calloc(ret_str_length, sizeof(char));
546                         goto_if(str_output == NULL, "str_output is NULL");
547
548                         memcpy(str_output, token_loc + strlen(token), ret_str_length);
549                 }
550         }
551
552 return_part:
553         _INNER_FUNC_EXIT;
554         return str_output;
555 }
556
557 char * replace_string(char *origin, const char *src, const char *dest)
558 {
559         _INNER_FUNC_ENTER;
560
561         retvm_if((origin == NULL || strlen(origin) <= 0), NULL, "origin is NULL");
562         retvm_if((src == NULL || strlen(src) <= 0), NULL, "src is NULL");
563         retvm_if((dest == NULL || strlen(dest) <= 0), NULL, "dest is NULL");
564
565         char *str_top = NULL;
566         if ((str_top = strstr(origin, src)) == NULL) {
567                 _DEBUG_ERROR("%s is not founed", src);
568                 return origin;
569         }
570
571         int top_length = strlen(origin) - strlen(str_top);
572         int middle_length = strlen(dest);
573         int down_length = strlen(origin) - top_length - strlen(src);
574         int new_str_length = top_length + middle_length + down_length + 1;
575         if (new_str_length <= 0) {
576                 _DEBUG_ERROR("top_length = %d", top_length);
577                 _DEBUG_ERROR("middle_length = %d", middle_length);
578                 _DEBUG_ERROR("down_length = %d", down_length);
579                 _DEBUG_ERROR("new_str_length(%d) <= 0", new_str_length);
580                 return origin;
581         }
582         char *new_str = (char *)calloc(new_str_length, sizeof(char));
583         if (new_str == NULL) {
584                 _DEBUG_ERROR("calloc is failed.");
585                 return origin;
586         }
587
588         memset(new_str, 0, new_str_length);
589         memcpy(new_str, origin, top_length);
590         memcpy(new_str + top_length, dest, middle_length);
591         memcpy(new_str + top_length + middle_length, origin + top_length + strlen(src), down_length);
592
593         if (origin != NULL) {
594                 free(origin);
595                 origin = NULL;
596         }
597
598         _DEBUG_INFO("new_str = %s", new_str);
599         _INNER_FUNC_EXIT;
600         return new_str;
601 }
602
603 // PLM(P130408-0777) : In case of DTSTART / DTEND with zulu time ('Z'), GMT timezone should be set.
604 EXPORT_API void set_timezone(const char *origin, calendar_record_h cal_record)
605 {
606         _INNER_FUNC_ENTER;
607         retm_if(origin == NULL, "origin is NULL");
608         retm_if(cal_record == NULL, "cal_record is NULL");
609
610         int service_ret = CALENDAR_ERROR_NONE;
611         char *temp = NULL;
612         char *str_start = NULL;
613
614         temp = strstr(origin, DTSTART_KEYWORD);
615         goto_if(temp == NULL, "origin has not DTSTART. origin = %s", origin);
616
617         str_start = get_value_by_token(temp + 2, "DTSTART:", "\r\n");
618         goto_if(str_start == NULL, "str_start is NULL. temp = %s", temp);
619
620         if (strstr(str_start, "Z") != NULL) {
621                 _DEBUG_INFO("GMT timezone setting");
622                 service_ret = calendar_record_set_str(cal_record, _calendar_event.start_tzid, TIMEZONE_GMT);
623                 goto_if(service_ret != CALENDAR_ERROR_NONE, "[vcalendar] calendar_record_set_str() Fail! : err[%d]", service_ret);
624
625                 service_ret = calendar_record_set_str(cal_record, _calendar_event.end_tzid, TIMEZONE_GMT);
626                 goto_if(service_ret != CALENDAR_ERROR_NONE, "[vcalendar] calendar_record_set_str() Fail! : err[%d]", service_ret);
627         }
628
629 return_part:
630         if (str_start) {
631                 free(str_start);
632                 str_start = NULL;
633         }
634         _INNER_FUNC_EXIT;
635         return;
636 }
637
638 char * remove_uid(char *origin)
639 {
640         _INNER_FUNC_ENTER;
641         retvm_if((origin == NULL || strlen(origin) <= 0), NULL, "origin is NULL");
642
643         _DEBUG_INFO("origin = %s", origin);
644
645         char *str_uid = NULL;
646         char *str_uid_temp = NULL;
647         char *temp = NULL;
648
649         if ((temp = strstr(origin, EXTYPE_UID_KEYWORD)) == NULL) {
650                 _DEBUG_ERROR("origin has not UID string");
651                 goto return_part;
652         }
653
654         str_uid = get_value_by_token(temp+2, "UID:", "\r\n");
655         goto_if(str_uid == NULL, "str_uid is NULL");
656
657         str_uid_temp = g_strdup_printf("%s%s\r\n", EXTYPE_UID_KEYWORD, str_uid);
658         goto_if(str_uid_temp == NULL, "str_uid_temp is NULL");
659
660         origin = replace_string(origin, str_uid_temp, "\r\n");
661
662 return_part:
663         if (str_uid) {
664                 free(str_uid);
665                 str_uid = NULL;
666         }
667         if (str_uid_temp) {
668                 free(str_uid_temp);
669                 str_uid_temp = NULL;
670         }
671         _INNER_FUNC_EXIT;
672         return origin;
673 }
674
675 char * insert_exdate_data(char *origin, sync_agent_calendar_exdate_t *exdate_struct, int index)
676 {
677         _INNER_FUNC_ENTER;
678         retvm_if((origin == NULL || strlen(origin) <= 0), NULL, "origin is NULL");
679         retvm_if(exdate_struct == NULL, NULL, "exdate_struct is NULL");
680         retvm_if(index < 0, NULL, "index < 0");
681
682         char *old_str = NULL;
683         char *new_str = NULL;
684         // 1. copy from parent vcalendar string
685         char *str_child_vcalendar = strdup((char *)(origin));
686
687         if (exdate_struct->exdate_type <= CALENDAR_EXDATE_TYPE_NONE && exdate_struct->exdate_type >= CALENDAR_EXDATE_TYPE_MAX) {
688                 _DEBUG_ERROR("exdate_type(%d) is wrong type", exdate_struct->exdate_type);
689                 goto return_part_free;
690         }
691
692         _DEBUG_INFO("origin = %s", origin);
693         _DEBUG_INFO("cal_exdate_type = %d", exdate_struct->exdate_type);
694
695         if (exdate_struct->exdate_type == CALENDAR_EXDATE_TYPE_DELETED) {
696                 _DEBUG_INFO("delete");
697
698                 // 2. remove SUMMARY / RRULE / ALARM / EXDATE
699                 // 2-1. remove 'SUMMARY'
700                 if ((old_str = strstr(str_child_vcalendar, SUMMARY_KEYWORD)) != NULL) {
701                         _DEBUG_INFO("summary keyword");
702                         if ((new_str = strstr(old_str+2, "\r\n")) != NULL) {
703                                 str_child_vcalendar = replace_string(str_child_vcalendar, old_str, new_str);
704                         }
705                 }
706
707                 // 2-2. remove 'RRULE'
708                 if ((old_str = strstr(str_child_vcalendar, RRULE_KEYWORD)) != NULL) {
709                         _DEBUG_INFO("rrule keyword");
710                         if ((new_str = strstr(old_str+2, "\r\n")) != NULL) {
711                                 str_child_vcalendar = replace_string(str_child_vcalendar, old_str, new_str);
712                         }
713                 }
714
715                 // 2-3. remove 'ALARM'
716                 if ((old_str = strstr(str_child_vcalendar, AALARM_KEYWORD)) != NULL) {
717                         _DEBUG_INFO("alarm keyword");
718                         if ((new_str = strstr(old_str+2, "\r\n")) != NULL) {
719                                 str_child_vcalendar = replace_string(str_child_vcalendar, old_str, new_str);
720                         }
721                 }
722
723                 // 2-4. remove 'EXDATE' array
724                 if ((old_str = strstr(str_child_vcalendar, EXDATE_KEYWORD)) != NULL) {
725                         _DEBUG_INFO("exdate keyword");
726                         if ((new_str = strstr(old_str+2, "\r\nX-EXTYPE:1")) != NULL) {
727                                 str_child_vcalendar = replace_string(str_child_vcalendar, old_str, new_str);
728                         }
729                 }
730                 _DEBUG_INFO("after 2nd phase str_child_vcalendar = %s", str_child_vcalendar);
731
732                 // 3. change DTSTART / DTEND
733                 // example ) DTSTART:20130125T000000Z / DTEND:20130125T003000Z / EXDATE:20130201T000000Z => DTSTART:20130201T000000Z / DTEND:20130201T003000Z
734                 // 3-1. change DTSTART : DTSTART = EXDATE
735                 char *temp = NULL;
736                 if ((temp  = strstr(str_child_vcalendar, DTSTART_KEYWORD)) != NULL) {
737                         _DEBUG_INFO("start keyword");
738
739                         old_str = get_value_by_token(temp + 2, "DTSTART:", "\r\n");// '2' means strlen("\r\n")
740                         if (old_str == NULL) {
741                                 _DEBUG_ERROR("old_str is null");
742                                 goto return_part_free;
743                         }
744                         new_str = (char*)g_list_nth_data(exdate_struct->exdate_list, index);
745                         if (new_str == NULL) {
746                                 _DEBUG_ERROR("new_str is NULL");
747                                 goto return_part_free;
748                         }
749                         str_child_vcalendar = replace_string(str_child_vcalendar, old_str, new_str);
750                         if (old_str) {
751                                 free(old_str);
752                                 old_str = NULL;
753                         }
754                 }
755
756                 // 3-2. change DTEND : DTEND = date of EXDATE + time of origin DTEND
757                 temp = NULL;
758                 if ((temp  = strstr(str_child_vcalendar, DTEND_KEYWORD)) != NULL) {
759                         _DEBUG_INFO("end keyword");
760
761                         old_str = get_value_by_token(temp + 2, "DTEND:", "\r\n");// '2' means strlen("\r\n")
762                         if (old_str == NULL) {
763                                 _DEBUG_ERROR("old_str is null");
764                                 goto return_part_free;
765                         }
766                         char *exdate_str = (char*)g_list_nth_data(exdate_struct->exdate_list, index);
767                         if (exdate_str == NULL) {
768                                 _DEBUG_ERROR("exdate_str is NULL");
769                                 goto return_part_free;
770                         }
771                         _DEBUG_INFO("exdate_str = %s", exdate_str);
772                         new_str = (char *)calloc(strlen(exdate_str), sizeof(char));
773                         if (new_str == NULL) {
774                                 _DEBUG_ERROR("new_str calloc failed");
775                                 goto return_part_free;
776                         }
777
778                         memcpy(new_str, exdate_str, 8);
779                         memcpy(new_str + 8, old_str + 8, 8);
780                         str_child_vcalendar = replace_string(str_child_vcalendar, old_str, new_str);
781                         if (old_str) {
782                                 free(old_str);
783                                 old_str = NULL;
784                         }
785                         if (new_str) {
786                                 free(new_str);
787                                 new_str = NULL;
788                         }
789                 }
790                 _DEBUG_INFO("after 3rd phase str_child_vcalendar = %s", str_child_vcalendar);
791
792                 // 4. insert EXDATE
793                 if (strstr(str_child_vcalendar, END_VEVENT_KEYWORD) != NULL) {
794                         _DEBUG_INFO("EXTYPE keyword");
795
796                         char *str_end_vevent = "\r\nEND:VEVENT";
797                         char *exdate_str = (char*)g_list_nth_data(exdate_struct->exdate_list, index);
798                         if (exdate_str == NULL) {
799                                 _DEBUG_ERROR("exdate_str is NULL");
800                                 goto return_part_free;
801                         }
802
803                         new_str = g_strdup_printf("%s%s%s", EXDATE_KEYWORD, exdate_str, str_end_vevent);
804                         if (new_str == NULL) {
805                                 _DEBUG_ERROR("new_str is null");
806                                 goto return_part_free;
807                         }
808
809                         str_child_vcalendar = replace_string(str_child_vcalendar, str_end_vevent, new_str);
810                         if (new_str) {
811                                 free(new_str);
812                                 new_str = NULL;
813                         }
814                 }
815
816                 if (strstr(str_child_vcalendar, EXTYPE_DELETE_KEYWORD) != NULL) {
817                         str_child_vcalendar = replace_string(str_child_vcalendar, EXTYPE_DELETE_KEYWORD, "UID:000000000000000000000000000000000000000000000000\r\nX-EXTYPE:1");
818                 }
819                 goto return_part_free;
820         }
821         else if (exdate_struct->exdate_type == CALENDAR_EXDATE_TYPE_UPDATED_CHILD) {
822                 _DEBUG_INFO("update child");
823
824                 // 2. remove 'RRULE'
825                 if ((old_str = strstr(str_child_vcalendar, RRULE_KEYWORD)) != NULL) {
826                         _DEBUG_INFO("rrule keyword");
827                         if ((new_str = strstr(old_str+2, "\r\n")) != NULL) {
828                                 str_child_vcalendar = replace_string(str_child_vcalendar, old_str, new_str);
829                         }
830                 }
831
832                 // 3. insert EXDATE
833                 if (strstr(str_child_vcalendar, END_VEVENT_KEYWORD) != NULL) {
834                         _DEBUG_INFO("EXTYPE keyword");
835
836                         char *str_end_vevent = "\r\nEND:VEVENT";
837                         char *exdate_str = (char*)g_list_nth_data(exdate_struct->exdate_list, index);
838                         goto_if(exdate_str == NULL, "exdate_str is NULL");
839                         _DEBUG_INFO("exdate_str = %s", exdate_str);
840
841                         new_str = g_strdup_printf("%s%s%s", EXDATE_KEYWORD, exdate_str, str_end_vevent);
842                         goto_if(new_str == NULL, "new_str is NULL");
843
844                         str_child_vcalendar = replace_string(str_child_vcalendar, str_end_vevent, new_str);
845                         if (new_str) {
846                                 free(new_str);
847                                 new_str = NULL;
848                         }
849                 }
850         }
851
852 return_part:
853         _DEBUG_INFO("last str_child_vcalendar = %s", str_child_vcalendar);
854         _INNER_FUNC_EXIT;
855         return str_child_vcalendar;
856
857 return_part_free:
858         _DEBUG_INFO("last str_child_vcalendar = %s", str_child_vcalendar);
859         if (old_str) {
860                 free(old_str);
861                 old_str = NULL;
862         }
863         _INNER_FUNC_EXIT;
864         return str_child_vcalendar;
865 }
866
867 void insert_allday_data(const char *origin, calendar_record_h calendar_record)
868 {
869         _INNER_FUNC_ENTER;
870         retm_if((origin == NULL || strlen(origin) <= 0), "origin is NULL");
871         retm_if(calendar_record == NULL, "calendar_record is NULL");
872
873         int service_ret = CALENDAR_ERROR_NONE;
874         char *uri = NULL;
875
876         service_ret = calendar_record_get_uri_p(calendar_record, &uri);
877         goto_if(service_ret != CALENDAR_ERROR_NONE, "calendar_record_get_uri_p() Fail!: err[%d]", service_ret);
878
879         if (strncmp(uri, _calendar_event._uri, strlen(_calendar_event._uri))) {
880                 _DEBUG_ERROR("this is not event");
881                 goto return_part;
882         }
883
884         calendar_time_s cal_time_start = { 0, };
885         calendar_time_s cal_time_end = { 0, };
886         int year = 0;
887         int month = 0;
888         int day = 0;
889
890         service_ret = calendar_record_get_caltime(calendar_record, _calendar_event.start_time, &cal_time_start);
891         goto_if(service_ret != CALENDAR_ERROR_NONE, "calendar_record_get_caltime() Fail!: err[%d]", service_ret);
892
893         char *start_date_loc = NULL;
894         if ((start_date_loc = strstr(origin, DTSTART_KEYWORD)) == NULL) {
895                 _DEBUG_ERROR("start_date_loc is null");
896                 goto return_part;
897         }
898
899         char str_year_start[5] = {0,};
900         char str_month_start[3] = {0,};
901         char str_date_start[3] = {0,};
902
903         /* example) 20130124T000000Z\r\n = 2013 01 24 T 00 00 00 Z \r\n */
904         strncpy(str_year_start, start_date_loc + strlen(DTSTART_KEYWORD), 4);
905         strncpy(str_month_start, start_date_loc + strlen(DTSTART_KEYWORD) + 4, 2);
906         strncpy(str_date_start, start_date_loc + strlen(DTSTART_KEYWORD) + 4 + 2, 2);
907
908         year = atoi(str_year_start);
909         month = atoi(str_month_start);
910         day = atoi(str_date_start);
911
912         _DEBUG_INFO("start time : year = %d, month = %d, day = %d", year, month, day);
913         cal_time_start.type = CALENDAR_TIME_LOCALTIME;
914         cal_time_start.time.date.year = year;
915         cal_time_start.time.date.month = month;
916         cal_time_start.time.date.mday = day;
917
918         service_ret = calendar_record_set_caltime(calendar_record, _calendar_event.start_time, cal_time_start);
919         goto_if(service_ret != CALENDAR_ERROR_NONE, "calendar_record_set_caltime() Fail!: err[%d]", service_ret);
920
921         service_ret = calendar_record_get_caltime(calendar_record, _calendar_event.end_time, &cal_time_end);
922         goto_if(service_ret != CALENDAR_ERROR_NONE, "calendar_record_get_caltime() Fail!: err[%d]", service_ret);
923
924         char *end_date_loc = NULL;
925         if ((end_date_loc = strstr(origin, DTEND_KEYWORD)) == NULL) {
926                 _DEBUG_ERROR("end_date_loc is null");
927                 goto return_part;
928         }
929
930         char str_year_end[5] = {0,};
931         char str_month_end[3] = {0,};
932         char str_date_end[3] = {0,};
933
934         /* example) 20130124T000000Z\r\n = 2013 01 24 T 00 00 00 Z \r\n */
935         strncpy(str_year_end, end_date_loc + strlen(DTEND_KEYWORD), 4);
936         strncpy(str_month_end, end_date_loc + strlen(DTEND_KEYWORD) + 4, 2);
937         strncpy(str_date_end, end_date_loc + strlen(DTEND_KEYWORD) + 4 + 2, 2);
938
939         year = atoi(str_year_end);
940         month = atoi(str_month_end);
941         day = atoi(str_date_end);
942
943         _DEBUG_INFO("end time : year = %d, month = %d, day = %d", year, month, day);
944         cal_time_end.type = CALENDAR_TIME_LOCALTIME;
945         cal_time_end.time.date.year = year;
946         cal_time_end.time.date.month = month;
947         cal_time_end.time.date.mday = day;
948
949         service_ret = calendar_record_set_caltime(calendar_record, _calendar_event.end_time, cal_time_end);
950         goto_if(service_ret != CALENDAR_ERROR_NONE, "calendar_record_set_caltime() Fail!: err[%d]", service_ret);
951
952 return_part:
953         _INNER_FUNC_EXIT;
954         return;
955 }
956
957 EXPORT_API char * has_image_import(const char *origin)
958 {
959         _INNER_FUNC_ENTER;
960
961         char *temp_image_path = NULL;
962         char *image_path = NULL;
963         _DEBUG_INFO("origin = %s", origin);
964
965         if (strstr(origin, CR_IMAGE_KEYWORD_PREFIX_PHOTO) == NULL && strstr(origin, CR_IMAGE_KEYWORD_PREFIX_SNOTE) == NULL) {
966                 _DEBUG_INFO("this calendar has not an image");
967                 goto return_part;
968         }
969
970         temp_image_path = get_value_by_token(origin, IMAGE_KEYWORD_BEGIN_PATH, IMAGE_KEYWORD_END_PATH);
971         goto_if(temp_image_path == NULL, "temp_image_path is NULL");
972
973         if (strstr(temp_image_path, INTERNAL_MEMORY_CONVERTED) != NULL) {
974                 image_path = replace_string(temp_image_path, INTERNAL_MEMORY_CONVERTED, INTERNAL_MEMORY_ORIGIN);
975         }
976         else if (strstr(temp_image_path, EXTERNAL_MEMORY_CONVERTED) != NULL) {
977                 image_path = replace_string(temp_image_path, EXTERNAL_MEMORY_CONVERTED, EXTERNAL_MEMORY_ORIGIN);
978         }
979         else if (strstr(temp_image_path, SNOTE_MEMORY_ORIGIN) != NULL) {
980                 image_path = strdup(temp_image_path);
981         }
982         else {
983                 _DEBUG_ERROR("there is an error. origin = %s", origin);
984                 goto return_part;
985         }
986
987 return_part:
988         if (image_path)
989                 _DEBUG_INFO("image_path = %s", image_path);
990         _INNER_FUNC_EXIT;
991         return image_path;
992 }
993
994 EXPORT_API char * has_image_export(calendar_record_h cal_record)
995 {
996         _INNER_FUNC_ENTER;
997         retvm_if(cal_record == NULL, NULL, "cal_record is NULL");
998
999         calendar_record_h image_record = NULL;
1000         unsigned int child_record_count = 0;
1001         int i = 0;
1002         int service_ret = CALENDAR_ERROR_NONE;
1003         char *image_key = NULL;
1004         char *image_path = NULL;
1005
1006         char *uri = NULL;
1007         service_ret = calendar_record_get_uri_p(cal_record, &uri);
1008         goto_if(service_ret != CALENDAR_ERROR_NONE, "calendar_record_get_uri_p() Fail!: err[%d]", service_ret);
1009
1010         _DEBUG_INFO("uri = %s", uri);
1011
1012         service_ret = calendar_record_get_child_record_count(cal_record, _calendar_event.extended, &child_record_count);
1013         goto_if(service_ret != CALENDAR_ERROR_NONE, "calendar_record_get_child_record_count() Fail!: err[%d]", service_ret);
1014
1015         for (i = 0; i < child_record_count; i++) {
1016                 service_ret = calendar_record_get_child_record_at_p(cal_record, _calendar_event.extended, i, &image_record);
1017                 goto_if(service_ret != CALENDAR_ERROR_NONE, "calendar_record_get_child_record_at_p() Fail!: err[%d]", service_ret);
1018
1019                 service_ret = calendar_record_get_str_p(image_record, _calendar_extended_property.key, &image_key);
1020                 goto_if(service_ret != CALENDAR_ERROR_NONE || image_key == NULL || strlen(image_key) <= 0, "calendar_record_get_str_p() Fail!: err[%d]", service_ret);
1021
1022                 if (strcmp(image_key, CALENDAR_IMAGE_KEYWORD_SNOTE) == 0 || strcmp(image_key, CALENDAR_IMAGE_KEYWORD_PHOTO) == 0) {
1023                         _DEBUG_INFO("image is founded. image_key = %s", image_key);
1024                         service_ret = calendar_record_get_str(image_record, _calendar_extended_property.value, &image_path);
1025                         goto_if(service_ret != CALENDAR_ERROR_NONE || image_path == NULL || strlen(image_path) <= 0, "calendar_record_get_str() Fail!: err[%d]", service_ret);
1026                         _DEBUG_INFO("image_path = %s", image_path);
1027                         continue;
1028                 }
1029         }
1030
1031 return_part:
1032         _INNER_FUNC_EXIT;
1033         return image_path;
1034 }
1035
1036 // "EXTYPE" setting, '1' means deleted, '2' means updated : it should be excuted before calendar_vcalendar_make_from_records
1037 void get_exdate_data(calendar_record_h cal_record, sync_agent_calendar_exdate_t *exdate_struct)
1038 {
1039         _INNER_FUNC_ENTER;
1040         retm_if(cal_record == NULL, "cal_record is NULL");
1041         retm_if(exdate_struct == NULL, "exdate_struct is NULL");
1042
1043         calendar_record_h extended_exdate = NULL;
1044         int service_ret = CALENDAR_ERROR_NONE;
1045         int original_event_id = 0;
1046         char *exdate = NULL;
1047         char *recurrence_id = NULL;
1048
1049         exdate_struct->exdate_type = CALENDAR_EXDATE_TYPE_NONE;
1050         exdate_struct->exdate_count = 0;
1051         exdate_struct->exdate_list = NULL;
1052
1053         service_ret = calendar_record_get_int(cal_record, _calendar_event.original_event_id, &original_event_id);
1054         goto_if(service_ret != CALENDAR_ERROR_NONE, "calendar_record_get_int() Fail!: err[%d]", service_ret);
1055         _DEBUG_INFO("original_event_id = %d", original_event_id);
1056
1057         service_ret = calendar_record_get_str(cal_record, _calendar_event.recurrence_id, &recurrence_id);
1058         goto_if(service_ret != CALENDAR_ERROR_NONE, "calendar_record_get_str() Fail!: err[%d]", service_ret);
1059         _DEBUG_INFO("recurrence_id = %s", recurrence_id);
1060
1061         if (original_event_id >= 0 && recurrence_id != NULL && strlen(recurrence_id) > 0) {
1062                 service_ret = calendar_record_set_str(cal_record, _calendar_event.exdate, NULL);
1063                 goto_if(service_ret != CALENDAR_ERROR_NONE, "calendar_record_set_str() Fail!: err[%d]", service_ret);
1064         }
1065
1066         // 1. delete : if 'exdate' value is exist, there are deleted item.
1067         service_ret = calendar_record_get_str(cal_record, _calendar_event.exdate, &exdate);
1068         goto_if(service_ret != CALENDAR_ERROR_NONE, "calendar_record_get_str() Fail!: err[%d]", service_ret);
1069         _DEBUG_INFO("exdate = %s", exdate);
1070
1071         if (exdate != NULL && strlen(exdate) > 0) {
1072                 _DEBUG_INFO("EXTYPE:DELETE setting start");
1073                 service_ret = calendar_record_create(_calendar_extended_property._uri, &extended_exdate);
1074                 goto_if(service_ret != CALENDAR_ERROR_NONE, "calendar_record_create() Fail!: err[%d]", service_ret);
1075
1076                 service_ret = calendar_record_set_str(extended_exdate, _calendar_extended_property.key, "X-EXTYPE");
1077                 goto_if(service_ret != CALENDAR_ERROR_NONE, "calendar_record_set_str() Fail!: err[%d]", service_ret);
1078
1079                 service_ret = calendar_record_set_str(extended_exdate, _calendar_extended_property.value, ":1");
1080                 goto_if(service_ret != CALENDAR_ERROR_NONE, "calendar_record_set_str() Fail!: err[%d]", service_ret);
1081
1082                 service_ret = calendar_record_add_child_record(cal_record, _calendar_event.extended, extended_exdate);
1083                 goto_if(service_ret != CALENDAR_ERROR_NONE, "calendar_record_add_child_record() Fail!: err[%d]", service_ret);
1084
1085                 exdate_struct->exdate_type = CALENDAR_EXDATE_TYPE_DELETED;
1086                 char *str_exdate_loc = NULL;
1087                 int exdate_count = 0;
1088                 int exdate_loc = 0;
1089                 while (exdate_loc < strlen(exdate)) {
1090                         int one_exdate_length = 0;
1091                         if ((str_exdate_loc = strstr(exdate + exdate_loc, ",")) != NULL) {
1092                                 one_exdate_length = strlen(exdate) - strlen(str_exdate_loc) - (exdate_count*(DATE_TIME_LENGTH+1));
1093                         }
1094                         else {
1095                                 one_exdate_length = strlen(exdate) - exdate_loc;
1096                         }
1097                         _DEBUG_INFO("str_exdate_loc = %s", str_exdate_loc);
1098                         if (one_exdate_length <= 0) {
1099                                 _DEBUG_ERROR("one_exdate_length (%d) <= 0", one_exdate_length);
1100                                 continue;
1101                         }
1102                         char *str_one_exdate = (char *)calloc(one_exdate_length, sizeof(char));
1103                         if (str_one_exdate == NULL) {
1104                                 _DEBUG_ERROR("str_one_exdate calloc is failed.");
1105                                 continue;
1106                         }
1107                         memcpy(str_one_exdate, exdate + exdate_loc, one_exdate_length);
1108                         exdate_loc += (one_exdate_length+1); // '1' meaning of ',' length
1109                         exdate_struct->exdate_list = g_list_append(exdate_struct->exdate_list, str_one_exdate);
1110                         exdate_count++;
1111                         _DEBUG_INFO("str_one_exdate = %s", str_one_exdate);
1112                 }
1113
1114                 _DEBUG_INFO("exdate_count = %d", exdate_count);
1115                 exdate_struct->exdate_count = exdate_count;
1116                 _DEBUG_INFO("EXTYPE:DELETE setting end");
1117                 goto return_part;
1118         }
1119
1120         // 2. update : if 'origianl_event_id' and 'recurrence_id' value are exist, there are updated item.
1121         if (original_event_id < 0 && recurrence_id == NULL ) {
1122                 unsigned int count = 0;
1123                 service_ret = calendar_record_get_child_record_count(cal_record, _calendar_event.exception, &count);
1124                 goto_if(service_ret != CALENDAR_ERROR_NONE, "calendar_record_get_child_record_count() Fail! err[%d]\n", service_ret);
1125                 if (count <=0) {
1126                         _DEBUG_INFO("this is a normal calendar");
1127                         goto return_part;
1128                 }
1129
1130                 _DEBUG_INFO("parent EXTYPE:UPDATE setting start");
1131                 exdate_struct->exdate_type = CALENDAR_EXDATE_TYPE_UPDATED_PARENT;
1132                 service_ret = calendar_record_create(_calendar_extended_property._uri, &extended_exdate);
1133                 goto_if(service_ret != CALENDAR_ERROR_NONE, "calendar_record_create() Fail! err[%d]\n", service_ret);
1134
1135                 service_ret = calendar_record_set_str(extended_exdate, _calendar_extended_property.key, "X-EXTYPE");
1136                 goto_if(service_ret != CALENDAR_ERROR_NONE, "calendar_record_set_str() Fail! err[%d]\n", service_ret);
1137
1138                 service_ret = calendar_record_set_str(extended_exdate, _calendar_extended_property.value, ":2");
1139                 goto_if(service_ret != CALENDAR_ERROR_NONE, "calendar_record_set_str() Fail! err[%d]\n", service_ret);
1140
1141                 service_ret = calendar_record_add_child_record(cal_record, _calendar_event.extended, extended_exdate);
1142                 goto_if(service_ret != CALENDAR_ERROR_NONE, "calendar_record_add_child_record() Fail! err[%d]\n", service_ret);
1143         }
1144         else if (original_event_id >= 0 && recurrence_id != NULL && strlen(recurrence_id) > 0) {
1145                 _DEBUG_INFO("child EXTYPE:UPDATE setting start");
1146                 exdate_struct->exdate_type = CALENDAR_EXDATE_TYPE_UPDATED_CHILD;
1147                 exdate_struct->exdate_list = g_list_append(exdate_struct->exdate_list, recurrence_id);
1148                 exdate_struct->exdate_count = 1;
1149                 service_ret = calendar_record_create(_calendar_extended_property._uri, &extended_exdate);
1150                 goto_if(service_ret != CALENDAR_ERROR_NONE, "calendar_record_create() Fail!: err[%d]", service_ret);
1151
1152                 service_ret = calendar_record_set_str(extended_exdate, _calendar_extended_property.key, "X-EXTYPE");
1153                 goto_if(service_ret != CALENDAR_ERROR_NONE, "calendar_record_set_str() Fail! err[%d]\n", service_ret);
1154
1155                 service_ret = calendar_record_set_str(extended_exdate, _calendar_extended_property.value, ":2");
1156                 goto_if(service_ret != CALENDAR_ERROR_NONE, "calendar_record_set_str() Fail! err[%d]\n", service_ret);
1157
1158                 service_ret = calendar_record_add_child_record(cal_record, _calendar_event.extended, extended_exdate);
1159                 goto_if(service_ret != CALENDAR_ERROR_NONE, "calendar_record_add_child_record() Fail! err[%d]\n", service_ret);
1160         }
1161
1162 return_part:
1163         _INNER_FUNC_EXIT;
1164         return;
1165 }
1166
1167 char * replace_rrule_data(char *origin)
1168 {
1169         _INNER_FUNC_ENTER;
1170         retvm_if(origin == NULL, origin, "origin is NULL.");
1171
1172         char *temp = NULL;
1173         char *temp_rrule = NULL;
1174         char *str_old_rrule = NULL;
1175         char *str_new_rrule = NULL;
1176
1177         temp = strstr(origin, RRULE_KEYWORD);
1178         if (temp == NULL) {
1179                 _DEBUG_INFO("this vcalendar hasn't rrule data");
1180                 goto return_part;
1181         }
1182
1183         // case 1 : weekdays rrule converting
1184         if (strstr(origin, WEEKDAY_RRULE_ORIGIN_KEYWORD) != NULL) {
1185                 _DEBUG_INFO("this vcalendar has WEEKDAYS rrule data");
1186
1187                 origin = replace_string(origin, WEEKDAY_RRULE_ORIGIN_KEYWORD, WEEKDAY_RRULE_CONVERTED_KEYWORD);
1188                 _DEBUG_INFO("AFTER origin = %s", origin);
1189                 goto return_part;
1190         }
1191
1192         // case 2 : max rrule converting
1193         str_old_rrule = get_value_by_token(temp + 2, "RRULE:", "\r\n");// '2' means strlen("\r\n")
1194         goto_if(str_old_rrule == NULL, "str_old_rrule is null");
1195
1196         if (strstr(str_old_rrule, MAX_RRULE_DATE_KEYWORD) == NULL) {
1197                 _DEBUG_INFO("this vcalendar has rrule data, But it's not until=NONE rrule");
1198                 goto return_part;
1199         }
1200         _DEBUG_INFO("this vcalendar has until=NONE rrule");
1201
1202         temp_rrule = get_value_by_token(temp, RRULE_KEYWORD, MAX_RRULE_DATE_KEYWORD);
1203         goto_if(temp_rrule == NULL, "temp_rrule is null");
1204
1205         // every eternal rrule data must have '#0' at the end of string (Calendar-Svc recommend)
1206         str_new_rrule = g_strdup_printf("%s#0", temp_rrule);
1207         goto_if(str_new_rrule == NULL, "str_new_rrule is null");
1208         _DEBUG_INFO("str_new_rrule = %s", str_new_rrule);
1209
1210         origin = replace_string(origin, str_old_rrule, str_new_rrule);
1211
1212 return_part:
1213         if(str_old_rrule) {
1214                 free(str_old_rrule);
1215                 str_old_rrule = NULL;
1216         }
1217         if(str_new_rrule) {
1218                 free(str_new_rrule);
1219                 str_new_rrule = NULL;
1220         }
1221         if(temp_rrule) {
1222                 free(temp_rrule);
1223                 temp_rrule = NULL;
1224         }
1225         _INNER_FUNC_EXIT;
1226         return origin;
1227 }
1228
1229 char * replace_image_data(char *origin, char *image_path)
1230 {
1231         _INNER_FUNC_ENTER;
1232         retvm_if(origin == NULL, origin, "origin is NULL.");
1233         retvm_if(image_path == NULL, origin, "image_path is NULL.");
1234
1235         char *str_temp_image_info = NULL;
1236         char *str_image_info = NULL;
1237         char *str_image_tag = NULL;
1238
1239         if (strstr(image_path, INTERNAL_MEMORY_ORIGIN) != NULL) {
1240                 str_temp_image_info = replace_string(image_path, INTERNAL_MEMORY_ORIGIN, INTERNAL_MEMORY_CONVERTED);
1241                 str_image_tag = strdup(IMAGE_KEYWORD_PREFIX_PHOTO);
1242         }
1243         else if (strstr(image_path, EXTERNAL_MEMORY_ORIGIN) != NULL) {
1244                 str_temp_image_info = replace_string(image_path, EXTERNAL_MEMORY_ORIGIN, EXTERNAL_MEMORY_CONVERTED);
1245                 str_image_tag = strdup(IMAGE_KEYWORD_PREFIX_PHOTO);
1246         }
1247         else if (strstr(image_path, SNOTE_MEMORY_ORIGIN) != NULL) {
1248                 str_temp_image_info = strdup(image_path);
1249                 //str_image_tag = strdup(IMAGE_KEYWORD_PREFIX_SNOTE);
1250                 str_image_tag = strdup(IMAGE_KEYWORD_PREFIX_PHOTO);
1251         }
1252         else {
1253                 _DEBUG_ERROR("there is an error. image_path = %s", image_path);
1254                 goto return_part;
1255         }
1256
1257         goto_if(strstr(origin, END_VEVENT_KEYWORD) == NULL, "this data is wrong format. origin = %s", origin);
1258
1259         str_image_info = g_strdup_printf("\r\n%s;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:NAME=3Dnull=3BPATH=3D%s=3BDATE=3D%s", str_image_tag, str_temp_image_info, END_VEVENT_KEYWORD);
1260         goto_if(str_image_info == NULL, "str_image_info is null");
1261
1262         char *new_agent_data = replace_string(origin, END_VEVENT_KEYWORD, str_image_info);
1263         origin = NULL;
1264         origin = new_agent_data;
1265
1266 return_part:
1267
1268         if (str_image_info) {
1269                 free(str_image_info);
1270                 str_image_info = NULL;
1271         }
1272         if (str_image_tag) {
1273                 free(str_image_tag);
1274                 str_image_tag = NULL;
1275         }
1276         if (str_temp_image_info) {
1277                 free(str_temp_image_info);
1278                 str_temp_image_info = NULL;
1279         }
1280
1281         _INNER_FUNC_EXIT;
1282         return origin;
1283 }
1284
1285 // exdate string format like as "20130211T000000Z,20130212T000000Z,20130213T000000Z"
1286 void replace_deleted_exdate_data(const char *origin, calendar_record_h parent_record)
1287 {
1288         _INNER_FUNC_ENTER;
1289         retm_if(origin == NULL, "origin is NULL.");
1290         retm_if(parent_record == NULL, "parent_record is NULL");
1291
1292         char *str_exdata_loc = NULL;
1293         char *old_exdate_str = NULL;
1294         char *new_exdate_str = NULL;
1295         char *new_one_exdate_str = NULL;
1296         int exdate_length = 0;
1297         int service_ret = CALENDAR_ERROR_NONE;
1298
1299         str_exdata_loc = strstr(origin, IMPORT_EXTYPE_DELETE_KEYWORD);
1300         goto_if(str_exdata_loc == NULL, "this vcalendar has not exdate data");
1301
1302         new_one_exdate_str = get_value_by_token(str_exdata_loc + 2, "EXDATE:", "\r\n");
1303         _DEBUG_INFO("new_one_exdate_str = %s", new_one_exdate_str);
1304
1305         service_ret = calendar_record_get_str(parent_record, _calendar_event.exdate, &old_exdate_str);
1306         goto_if(service_ret != CALENDAR_ERROR_NONE, "calendar_record_set_str() Fail! err[%d]\n", service_ret);
1307
1308         _DEBUG_INFO("old_exdate_str = %s", old_exdate_str);
1309         int old_exdate_str_length = 0;
1310         if (old_exdate_str == NULL) {
1311                 old_exdate_str_length = 0;
1312         }
1313         else {
1314                 old_exdate_str_length = strlen(old_exdate_str);
1315                 if (strstr(old_exdate_str, new_one_exdate_str) != NULL) {
1316                         _DEBUG_ERROR("new_one_exdate_str is already exist in old_exdate_str");
1317                         goto return_part;
1318                 }
1319         }
1320
1321         exdate_length = old_exdate_str_length + strlen(new_one_exdate_str) + 2;
1322         new_exdate_str = (char *)calloc(exdate_length, sizeof(char));
1323         goto_if(new_exdate_str == NULL, "new_exdate_str calloc failed, exdate_length = %d", exdate_length);
1324
1325         if (old_exdate_str == NULL)
1326                 snprintf(new_exdate_str, exdate_length, "%s", new_one_exdate_str);
1327         else
1328                 snprintf(new_exdate_str, exdate_length, "%s,%s", old_exdate_str, new_one_exdate_str);
1329
1330         _DEBUG_INFO("new_exdate_str = %s", new_exdate_str);
1331         service_ret = calendar_record_set_str(parent_record, _calendar_event.exdate, new_exdate_str);
1332         goto_if(service_ret != CALENDAR_ERROR_NONE, "calendar_record_set_str() Fail! err[%d]\n", service_ret);
1333
1334 return_part:
1335         if(new_one_exdate_str) {
1336                 free(new_one_exdate_str);
1337                 new_one_exdate_str = NULL;
1338         }
1339         if(new_exdate_str) {
1340                 free(new_exdate_str);
1341                 new_exdate_str = NULL;
1342         }
1343         _INNER_FUNC_EXIT;
1344         return;
1345 }
1346
1347 int replace_updated_exdate_data(const char *origin, calendar_record_h parent_record)
1348 {
1349         _INNER_FUNC_ENTER;
1350         retvm_if(origin == NULL, -1, "origin is NULL.");
1351         retvm_if(parent_record == NULL, -1, "parent_record is NULL");
1352
1353         calendar_record_h child_record = NULL;
1354         char *str_exdata_loc = NULL;
1355         char *new_one_exdate_str = NULL;
1356         int parent_service_id = 0;
1357         int child_service_id = 0;
1358         int service_ret = CALENDAR_ERROR_NONE;
1359         int ret = -1;
1360
1361         str_exdata_loc = strstr(origin, IMPORT_EXTYPE_UPDATE_KEYWORD);
1362         goto_if(str_exdata_loc == NULL, "this vcalendar has not exdate data");
1363
1364         new_one_exdate_str = get_value_by_token(str_exdata_loc + 2, "EXDATE:", "\r\n");
1365         _DEBUG_INFO("new_one_exdate_str = %s", new_one_exdate_str);
1366
1367         service_ret = calendar_record_clone(parent_record, &child_record);
1368         goto_if(service_ret != CALENDAR_ERROR_NONE, "calendar_record_clone() Fail!: err[%d]", service_ret);
1369
1370         service_ret = calendar_record_get_int(parent_record, _calendar_event.id, &parent_service_id);
1371         goto_if(service_ret != CALENDAR_ERROR_NONE, "calendar_record_get_int() Fail!: err[%d]", service_ret);
1372         _DEBUG_INFO("parent_service_id = %d", parent_service_id);
1373
1374         service_ret = calendar_record_set_int(child_record, _calendar_event.original_event_id, parent_service_id);
1375         goto_if(service_ret != CALENDAR_ERROR_NONE, "calendar_record_set_int() Fail!: err[%d]", service_ret);
1376
1377         service_ret = calendar_record_set_str(child_record, _calendar_event.recurrence_id, new_one_exdate_str);
1378         goto_if(service_ret != CALENDAR_ERROR_NONE, "calendar_record_set_str() Fail!: err[%d]", service_ret);
1379
1380         service_ret = calendar_record_set_str(child_record, _calendar_event.exdate, NULL);
1381         goto_if(service_ret != CALENDAR_ERROR_NONE, "calendar_record_set_str() Fail!: err[%d]", service_ret);
1382
1383         service_ret = calendar_db_insert_record(child_record, &child_service_id);
1384         goto_if(service_ret != CALENDAR_ERROR_NONE, "calendar_db_insert_record() Fail!: err[%d]", service_ret);
1385
1386         service_ret = calendar_record_destroy(child_record, true);
1387         goto_if(service_ret != CALENDAR_ERROR_NONE, "calendar_record_destroy() Fail!: err[%d]", service_ret);
1388         _DEBUG_INFO("child_service_id = %d", child_service_id);
1389
1390         ret = child_service_id;
1391
1392 return_part:
1393         if(new_one_exdate_str) {
1394                 free(new_one_exdate_str);
1395                 new_one_exdate_str = NULL;
1396         }
1397         if (service_ret != CALENDAR_ERROR_NONE)
1398                 ret = -1;
1399         _INNER_FUNC_EXIT;
1400         return ret;
1401 }