merge with master
[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 #if 0
27 // usage / rrule converting
28 if (strstr(temp_agent_data, "\r\nRRULE:") != NULL) {
29         _DEBUG_INFO("rrule is founed");
30         temp_agent_data = _replace_rrule_data(temp_agent_data);
31 }
32
33 static char * _replace_rrule_data(char *origin)
34 {
35         _INNER_FUNC_ENTER;
36         retvm_if(origin == NULL, origin, "origin is NULL.");
37
38         char *str_new_rrule = NULL;
39         char *str_end_time = NULL;
40         char *freq_loc = NULL;
41         char *str_byday = NULL;
42         char *str_interval = NULL;
43         char *str_period_until = NULL;
44         char *str_period_count = NULL;
45         calendar_date_type_e cal_type = CALENDAR_DATE_TYPE_ONTIME;
46
47         if ((freq_loc = strstr(origin, RRULE_KEYWORD_FREQ_WEEKLY)) != NULL) {
48                 _DEBUG_INFO("this is weekly data");
49         }
50         else if ((freq_loc = strstr(origin, RRULE_KEYWORD_FREQ_MONTHLY)) != NULL) {
51                 _DEBUG_INFO("this is monthly data");
52         }
53         else if ((freq_loc = strstr(origin, RRULE_KEYWORD_FREQ_YEARLY)) != NULL) {
54                 _DEBUG_INFO("this is yearly data");
55         }
56         else if ((freq_loc = strstr(origin, RRULE_KEYWORD_FREQ_DAILY)) != NULL) {
57                 _DEBUG_INFO("this is daily data");
58         }
59         else {
60                 _DEBUG_ERROR("this is an undefined rrule type. origin = %s", origin);
61                 goto return_part;
62         }
63
64         char *temp_rrule = NULL;
65         if((temp_rrule = strstr(freq_loc, "RRULE:")) == NULL) {
66                 _DEBUG_ERROR("temp_rrule is NULL");
67                 goto return_part;
68         }
69
70         char *temp_rrule2 = NULL;
71         if((temp_rrule2 = strstr(temp_rrule, "\r\n")) == NULL) {
72                 _DEBUG_ERROR("temp_rrule2 is NULL");
73                 goto return_part;
74         }
75
76         char str_origin_rrule[100] = {0,};
77         int origin_rrule_length = strlen(temp_rrule) - strlen(temp_rrule2) + 2; // '2' means of '\r\n'
78         if (origin_rrule_length <= 0) {
79                 _DEBUG_ERROR("origin_rrule_length <= 0");
80                 goto return_part;
81         }
82
83         strncpy(str_origin_rrule, temp_rrule, origin_rrule_length);
84         _DEBUG_INFO("str_origin_rrule = %s", str_origin_rrule);
85
86         if (strstr(origin, RRULE_KEYWORD_FREQ_WEEKLY) != NULL) {
87                 str_byday = get_value_by_token(freq_loc, RRULE_KEYWORD_BYDAY, ";");
88                 cal_type = CALENDAR_DATE_TYPE_WEEK;
89         }
90         else if (strstr(origin, RRULE_KEYWORD_FREQ_MONTHLY) != NULL) {
91                 str_byday = get_value_by_token(freq_loc, RRULE_KEYWORD_BYMONTHDAY, ";");
92                 cal_type = CALENDAR_DATE_TYPE_MONTH;
93         }
94         else if (strstr(origin, RRULE_KEYWORD_FREQ_YEARLY) != NULL) {
95                 str_byday = get_value_by_token(freq_loc, RRULE_KEYWORD_BYMONTHDAY, ";");
96                 cal_type = CALENDAR_DATE_TYPE_YEAR;
97         }
98         else if (strstr(origin, RRULE_KEYWORD_FREQ_DAILY) != NULL) {
99                 cal_type = CALENDAR_DATE_TYPE_DAY;
100         }
101         else {
102                 _DEBUG_ERROR("this is an undefined rrule type. origin = %s", origin);
103                 goto return_part;
104         }
105
106         if ((cal_type != CALENDAR_DATE_TYPE_DAY) && (str_byday == NULL || strlen(str_byday) <= 0)) {
107                 _DEBUG_ERROR("str_byday is null");
108                 goto return_part;
109         }
110
111         str_interval = get_value_by_token(freq_loc, RRULE_KEYWORD_INTERVAL, ";");
112         str_period_until = get_value_by_token(freq_loc, RRULE_KEYWORD_UNTIL, ";");
113         str_period_count = get_value_by_token(freq_loc, RRULE_KEYWORD_COUNT, ";");
114
115         if (str_interval == NULL || strlen(str_interval) <= 0) {
116                 _DEBUG_ERROR("str_interval is null");
117                 goto return_part;
118         }
119
120         if (str_period_until == NULL && str_period_count == NULL) {
121                 str_end_time = (char *)calloc(2, sizeof(char));
122                 if (str_end_time == NULL) {
123                         _DEBUG_ERROR("str_end_time is null");
124                         goto return_part;
125                 }
126                 strncpy(str_end_time, "\r\n", 2);
127                 _DEBUG_INFO("endless = %s", str_end_time);
128         }
129         else if (str_period_until != NULL) {
130                 str_end_time = (char *)calloc(strlen(str_period_until), sizeof(char));
131                 if (str_end_time == NULL) {
132                         _DEBUG_ERROR("str_end_time is null");
133                         goto return_part;
134                 }
135                 strncpy(str_end_time, str_period_until, strlen(str_period_until));
136                 _DEBUG_INFO("until = %s", str_end_time);
137         }
138         else if (str_period_count != NULL) {
139                 str_end_time = (char *)calloc(strlen(str_period_count)+1, sizeof(char));
140                 if (str_end_time == NULL) {
141                         _DEBUG_ERROR("str_end_time is null");
142                         goto return_part;
143                 }
144                 snprintf(str_end_time, strlen(str_period_count)+1, "#%s", str_period_count);
145                 _DEBUG_INFO("count = %s", str_end_time);
146         }
147         else {
148                 _DEBUG_ERROR("there is an error");
149                 goto return_part;
150         }
151
152         int new_rrule_length = 0;
153         if (strstr(origin, RRULE_KEYWORD_FREQ_WEEKLY) != NULL) {
154                 new_rrule_length = strlen("RRULE:W") + strlen(str_interval) + strlen(" ") + strlen(str_byday) + strlen("  ") + strlen(str_end_time) + 1;
155         }
156         else if (strstr(origin, RRULE_KEYWORD_FREQ_MONTHLY) != NULL) {
157                 new_rrule_length = strlen("RRULE:MD") + strlen(str_interval) + strlen(" ") + strlen(str_byday) + strlen("  ") + strlen(str_end_time) + 1;
158         }
159         else if (strstr(origin, RRULE_KEYWORD_FREQ_YEARLY) != NULL) {
160                 new_rrule_length = strlen("RRULE:YD") + strlen(str_interval) + strlen(" ") + strlen(str_end_time) + 1;
161         }
162         else if (strstr(origin, RRULE_KEYWORD_FREQ_DAILY) != NULL) {
163                 new_rrule_length = strlen("RRULE:D") + strlen(str_interval) + strlen(" ") + strlen(str_end_time) + 1;
164         }
165         else {
166                 _DEBUG_ERROR("this is an undefined rrule type. origin = %s", origin);
167                 goto return_part;
168         }
169
170         if (new_rrule_length <= 0) {
171                 _DEBUG_ERROR("new_rrule_length(%d) <= 0", new_rrule_length);
172                 goto return_part;
173         }
174
175         str_new_rrule = (char *)calloc(new_rrule_length, sizeof(char));
176         if (str_new_rrule == NULL) {
177                 _DEBUG_ERROR("str_new_rrule calloc failed");
178                 goto return_part;
179         }
180
181         if (strstr(origin, RRULE_KEYWORD_FREQ_WEEKLY) != NULL) {
182                 snprintf(str_new_rrule, new_rrule_length, "RRULE:W%s %s  %s", str_interval, str_byday, str_end_time);
183         }
184         else if (strstr(origin, RRULE_KEYWORD_FREQ_MONTHLY) != NULL) {
185                 snprintf(str_new_rrule, new_rrule_length, "RRULE:MD%s %s  %s", str_interval, str_byday, str_end_time);
186         }
187         else if (strstr(origin, RRULE_KEYWORD_FREQ_YEARLY) != NULL) {
188                 snprintf(str_new_rrule, new_rrule_length, "RRULE:YD%s %s", str_interval, str_end_time);
189         }
190         else if (strstr(origin, RRULE_KEYWORD_FREQ_DAILY) != NULL) {
191                 snprintf(str_new_rrule, new_rrule_length, "RRULE:D%s %s", str_interval, str_end_time);
192         }
193         else {
194                 _DEBUG_ERROR("this is an undefined rrule type. origin = %s", origin);
195                 goto return_part;
196         }
197
198         _DEBUG_INFO("str_new_rrule = %s", str_new_rrule);
199         _DEBUG_INFO("str_origin_rrule = %s", str_origin_rrule);
200
201         char *new_agent_data = replace_string(origin, str_origin_rrule, str_new_rrule);
202         origin = NULL;
203         origin = new_agent_data;
204
205 return_part:
206
207         if (str_interval) {
208                 free(str_interval);
209                 str_interval = NULL;
210         }
211         if (str_byday) {
212                 free(str_byday);
213                 str_byday = NULL;
214         }
215         if (str_period_until) {
216                 free(str_period_until);
217                 str_period_until = NULL;
218         }
219         if (str_period_count) {
220                 free(str_period_count);
221                 str_period_count = NULL;
222         }
223         if (str_end_time) {
224                 free(str_end_time);
225                 str_end_time = NULL;
226         }
227         if (str_new_rrule) {
228                 free(str_new_rrule);
229                 str_new_rrule = NULL;
230         }
231
232         _DEBUG_INFO("origin = %s", origin);
233         _INNER_FUNC_EXIT;
234         return origin;
235 }
236
237 // usage / alarm converting
238 if (strstr(temp_agent_data, "\r\nBEGIN:VALARM") != NULL) {
239         _DEBUG_INFO("alarm is founed");
240         temp_agent_data = _replace_alarm_data(temp_agent_data);
241 }
242
243 static char * _replace_alarm_data(char *origin)
244 {
245         _INNER_FUNC_ENTER;
246         retvm_if(origin == NULL, origin, "origin is NULL.");
247
248         int interval = 0;
249         char *str_new_alarm = NULL;
250         char *freq_loc = NULL;
251         char *str_trigger = NULL;
252         char *str_customized_trigger = NULL;
253         char *str_interval = NULL;
254         calendar_date_type_e cal_type = CALENDAR_DATE_TYPE_ONTIME;
255
256         if ((freq_loc = strstr(origin, BEGIN_ALARM_KEYWORD)) == NULL) {
257                 _DEBUG_ERROR("there is not trigger data");
258                 goto return_part;
259         }
260
261         char *temp = NULL;
262         if((temp = strstr(freq_loc, "BEGIN:VALARM\r\n")) == NULL) {
263                 _DEBUG_ERROR("temp is NULL");
264                 goto return_part;
265         }
266
267         char *temp2 = NULL;
268         if((temp2 = strstr(temp, "END:VALARM\r\n")) == NULL) {
269                 _DEBUG_ERROR("temp2 is NULL");
270                 goto return_part;
271         }
272
273         char str_origin_alarm[100] = {0,};
274         int origin_alarm_length = strlen(temp) - strlen(temp2) + 12; // '12' means of "END:VALARM\r\n"
275         if (origin_alarm_length <= 0) {
276                 _DEBUG_ERROR("origin_alarm_length <= 0");
277                 goto return_part;
278         }
279
280         strncpy(str_origin_alarm, temp, origin_alarm_length);
281         _DEBUG_INFO("str_origin_alarm = %s", str_origin_alarm);
282
283         // case 1. "TRIGGER;VALUE=DATE-TIME:" is an alarm that created by user
284         str_customized_trigger = _get_value_by_token(freq_loc, "TRIGGER;VALUE=DATE-TIME:", "\r\n");
285         if (str_customized_trigger != NULL) {
286                 _DEBUG_INFO("str_customized_trigger is founded. str_customized_trigger = %s", str_customized_trigger);
287
288                 str_new_alarm = (char *)calloc(30, sizeof(char));
289                 if (str_new_alarm == NULL) {
290                         _DEBUG_ERROR("new_date_time is null");
291                         goto return_part;
292                 }
293
294                 snprintf(str_new_alarm, 30, "AALARM:%s\r\n", str_customized_trigger);
295                 _DEBUG_INFO("str_new_alarm = %s", str_new_alarm);
296                 char *new_agent_data = _replace_string(origin, str_origin_alarm, str_new_alarm);
297                 origin = NULL;
298                 origin = new_agent_data;
299                 goto return_part;
300         }
301
302         // case 2. "TRIGGER:" is a normal alarm
303         str_trigger = _get_value_by_token(freq_loc, "TRIGGER:", "\r\n");
304         if (str_trigger == NULL || strlen(str_trigger) <= 0) {
305                 _DEBUG_ERROR("str_trigger is null");
306                 goto return_part;
307         }
308
309         char *temp_alarm = NULL;
310         char *temp_alarm2 = 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((temp_alarm2 = 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((temp_alarm2 = 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((temp_alarm2 = 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((temp_alarm2 = 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                         _DEBUG_INFO("token_loc = %s", token_loc);
538                         _DEBUG_INFO("token = %s", token);
539                         _DEBUG_INFO("str_temp = %s", str_temp);
540                         int ret_str_length = strlen(token_loc) - strlen(token) - strlen(str_temp);
541                         if (ret_str_length <= 0) {
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                         if (str_output == NULL) {
547                                 _DEBUG_ERROR("calloc failed");
548                                 goto return_part;
549                         }
550                         memcpy(str_output, token_loc + strlen(token), ret_str_length);
551                 }
552         }
553
554 return_part:
555         _INNER_FUNC_EXIT;
556         return str_output;
557 }
558
559 char * replace_string(char *origin, const char *src, const char *dest)
560 {
561         _INNER_FUNC_ENTER;
562
563         retvm_if((origin == NULL || strlen(origin) <= 0), NULL, "origin is NULL");
564         retvm_if((src == NULL || strlen(src) <= 0), NULL, "src is NULL");
565         retvm_if((dest == NULL || strlen(dest) <= 0), NULL, "dest is NULL");
566
567         _DEBUG_INFO("origin = %s", origin);
568         _DEBUG_INFO("src = %s", src);
569         _DEBUG_INFO("dest = %s", dest);
570
571         char *str_top = NULL;
572
573         if ((str_top = strstr(origin, src)) == NULL) {
574                 _DEBUG_INFO("%s is not founed", src);
575                 return origin;
576         }
577
578         int top_length = strlen(origin) - strlen(str_top);
579         int middle_length = strlen(dest);
580         int down_length = strlen(origin) - top_length - strlen(src);
581         int new_str_length = top_length + middle_length + down_length + 1;
582         if (new_str_length <= 0) {
583                 _DEBUG_ERROR("new_str_length(%d) <= 0", new_str_length);
584                 return origin;
585         }
586         char *new_str = (char *)calloc(new_str_length, sizeof(char));
587         if (new_str == NULL) {
588                 _DEBUG_ERROR("calloc is failed.");
589                 return origin;
590         }
591
592         memset(new_str, 0, new_str_length);
593         memcpy(new_str, origin, top_length);
594         memcpy(new_str + top_length, dest, middle_length);
595         memcpy(new_str + top_length + middle_length, origin + top_length + strlen(src), down_length);
596
597         if (origin != NULL) {
598                 free(origin);
599                 origin = NULL;
600         }
601
602         _DEBUG_INFO("new_str = %s", new_str);
603         _INNER_FUNC_EXIT;
604         return new_str;
605 }
606
607 char * remove_timezone(char *origin)
608 {
609         _INNER_FUNC_ENTER;
610         retvm_if((origin == NULL || strlen(origin) <= 0), NULL, "origin is NULL");
611
612         _DEBUG_INFO("origin = %s", origin);
613
614         char *str_timezone = NULL;
615         char *str_temp = NULL;
616
617         if ((str_temp = strstr(origin, "\r\nDTSTART;TZID=")) != NULL) {
618                 _DEBUG_INFO("str_temp = %s", str_temp);
619                 str_timezone = get_value_by_token(str_temp, "\r\nDTSTART", ":");
620                 _DEBUG_INFO("str_timezone = %s", str_timezone);
621                 strcat(str_timezone, ":");
622                 _DEBUG_INFO("str_timezone = %s", str_timezone);
623                 origin = replace_string(origin, str_timezone, ":");
624
625                 if (str_timezone)
626                         free(str_timezone);
627                 str_timezone = NULL;
628         }
629
630         if ((str_temp = strstr(origin, "\r\nDTEND;TZID=")) != NULL) {
631                 _DEBUG_INFO("str_temp = %s", str_temp);
632                 str_timezone = get_value_by_token(str_temp, "\r\nDTEND", ":");
633                 _DEBUG_INFO("str_timezone = %s", str_timezone);
634                 strcat(str_timezone, ":");
635                 _DEBUG_INFO("str_timezone = %s", str_timezone);
636                 origin = replace_string(origin, str_timezone, ":");
637
638                 if (str_timezone)
639                         free(str_timezone);
640                 str_timezone = NULL;
641         }
642
643         _DEBUG_INFO("origin = %s", origin);
644         _INNER_FUNC_EXIT;
645         return origin;
646 }
647
648 char * insert_exdate_data(const char *origin, sync_agent_calendar_exdate_t *exdate_struct, int index)
649 {
650         _INNER_FUNC_ENTER;
651         retvm_if((origin == NULL || strlen(origin) <= 0), NULL, "origin is NULL");
652         retvm_if(exdate_struct == NULL, NULL, "exdate_struct is NULL");
653         retvm_if(index < 0, NULL, "index < 0");
654
655         char *old_str = NULL;
656         char *new_str = NULL;
657         // 1. copy from parent vcalendar string
658         char *str_child_vcalendar = strdup((char *)(origin));
659
660         if (exdate_struct->exdate_type != CALENDAR_EXDATE_TYPE_DELETED && exdate_struct->exdate_type != CALENDAR_EXDATE_TYPE_UPDATED) {
661                 _DEBUG_ERROR("exdate_type(%d) is wrong", exdate_struct->exdate_type);
662                 goto return_part;
663         }
664
665         _DEBUG_INFO("origin = %s", origin);
666         _DEBUG_INFO("cal_exdate_type = %d", exdate_struct->exdate_type);
667
668         if (exdate_struct->exdate_type == CALENDAR_EXDATE_TYPE_DELETED) {
669                 _DEBUG_INFO("delete");
670                 // 2. remove SUMMARY / RRULE / ALARM / EXDATE
671                 // 2-1. remove 'SUMMARY'
672                 if ((old_str = strstr(str_child_vcalendar, SUMMARY_KEYWORD)) != NULL) {
673                         _DEBUG_INFO("summary keyword");
674                         _DEBUG_INFO("old_str = %s", old_str);
675                         if ((new_str = strstr(old_str+2, "\r\n")) != NULL) {
676                                 _DEBUG_INFO("new_str = %s", new_str);
677                                 str_child_vcalendar = replace_string(str_child_vcalendar, old_str, new_str);
678                                 _DEBUG_INFO("str_child_vcalendar = %s", str_child_vcalendar);
679                         }
680                 }
681
682                 // 2-2. remove 'RRULE'
683                 if ((old_str = strstr(str_child_vcalendar, RRULE_KEYWORD)) != NULL) {
684                         _DEBUG_INFO("rrule keyword");
685                         _DEBUG_INFO("old_str = %s", old_str);
686                         if ((new_str = strstr(old_str+2, "\r\n")) != NULL) {
687                                 _DEBUG_INFO("new_str = %s", new_str);
688                                 str_child_vcalendar = replace_string(str_child_vcalendar, old_str, new_str);
689                                 _DEBUG_INFO("str_child_vcalendar = %s", str_child_vcalendar);
690                         }
691                 }
692
693                 // 2-3. remove 'ALARM'
694                 if ((old_str = strstr(str_child_vcalendar, AALARM_KEYWORD)) != NULL) {
695                         _DEBUG_INFO("alarm keyword");
696                         _DEBUG_INFO("old_str = %s", old_str);
697                         if ((new_str = strstr(old_str+2, "\r\n")) != NULL) {
698                                 _DEBUG_INFO("new_str = %s", new_str);
699                                 str_child_vcalendar = replace_string(str_child_vcalendar, old_str, new_str);
700                                 _DEBUG_INFO("str_child_vcalendar = %s", str_child_vcalendar);
701                         }
702                 }
703
704                 // 2-4. remove 'EXDATE' array
705                 if ((old_str = strstr(str_child_vcalendar, EXDATE_KEYWORD)) != NULL) {
706                         _DEBUG_INFO("exdate keyword");
707                         _DEBUG_INFO("old_str = %s", old_str);
708                         if ((new_str = strstr(old_str+2, "\r\nX-EXTYPE:1")) != NULL) {
709                                 _DEBUG_INFO("new_str = %s", new_str);
710                                 str_child_vcalendar = replace_string(str_child_vcalendar, old_str, new_str);
711                                 _DEBUG_INFO("str_child_vcalendar = %s", str_child_vcalendar);
712                         }
713                 }
714
715                 // 3. change DTSTART / DTEND
716                 // example ) DTSTART:20130125T000000Z / DTEND:20130125T003000Z / EXDATE:20130201T000000Z => DTSTART:20130201T000000Z / DTEND:20130201T003000Z
717                 // 3-1. change DTSTART : DTSTART = EXDATE
718                 char *temp = NULL;
719                 if ((temp  = strstr(str_child_vcalendar, DTSTART_KEYWORD)) != NULL) {
720                         _DEBUG_INFO("start keyword");
721                         _DEBUG_INFO("temp  = %s", temp );
722
723                         old_str = get_value_by_token(temp + 2, "DTSTART:", "\r\n");// '2' means strlen("\r\n")
724                         if (old_str == NULL) {
725                                 _DEBUG_ERROR("old_str is null");
726                                 goto return_part;
727                         }
728                         _DEBUG_INFO("old_str = %s", old_str);
729                         new_str = (char*)g_list_nth_data(exdate_struct->exdate_list, index);
730                         if (new_str == NULL) {
731                                 _DEBUG_ERROR("new_str is NULL");
732                                 goto return_part;
733                         }
734                         _DEBUG_INFO("new_str = %s", new_str);
735                         str_child_vcalendar = replace_string(str_child_vcalendar, old_str, new_str);
736                         _DEBUG_INFO("str_child_vcalendar = %s", str_child_vcalendar);
737                 }
738
739                 // 3-2. change DTEND : DTEND = date of EXDATE + time of origin DTEND
740                 temp = NULL;
741                 if ((temp  = strstr(str_child_vcalendar, DTEND_KEYWORD)) != NULL) {
742                         _DEBUG_INFO("end keyword");
743                         _DEBUG_INFO("temp  = %s", temp );
744
745                         old_str = get_value_by_token(temp + 2, "DTEND:", "\r\n");// '2' means strlen("\r\n")
746                         if (old_str == NULL) {
747                                 _DEBUG_ERROR("old_str is null");
748                                 goto return_part;
749                         }
750                         _DEBUG_INFO("old_str = %s", old_str);
751                         char *exdate_str = (char*)g_list_nth_data(exdate_struct->exdate_list, index);
752                         if (exdate_str == NULL) {
753                                 _DEBUG_ERROR("exdate_str is NULL");
754                                 goto return_part;
755                         }
756                         _DEBUG_INFO("exdate_str = %s", exdate_str);
757                         new_str = (char *)calloc(strlen(exdate_str), sizeof(char));
758                         if (new_str == NULL) {
759                                 _DEBUG_ERROR("new_str calloc failed");
760                                 goto return_part;
761                         }
762
763                         memcpy(new_str, exdate_str, 8);
764                         memcpy(new_str + 8, old_str + 8, 8);
765                         _DEBUG_INFO("new_str = %s", new_str);
766                         str_child_vcalendar = replace_string(str_child_vcalendar, old_str, new_str);
767                         _DEBUG_INFO("str_child_vcalendar = %s", str_child_vcalendar);
768                         if (new_str) {
769                                 free(new_str);
770                                 new_str = NULL;
771                         }
772                         _DEBUG_INFO("str_child_vcalendar = %s", str_child_vcalendar);
773                 }
774
775                 // 4. insert EXDATE
776                 if ((temp  = strstr(str_child_vcalendar, END_VEVENT_KEYWORD)) != NULL) {
777                         _DEBUG_INFO("EXTYPE keyword");
778                         _DEBUG_INFO("temp = %s", temp);
779
780                         char *exdate_str = (char*)g_list_nth_data(exdate_struct->exdate_list, index);
781                         if (exdate_str == NULL) {
782                                 _DEBUG_ERROR("exdate_str is NULL");
783                                 goto return_part;
784                         }
785                         _DEBUG_INFO("exdate_str = %s", exdate_str);
786
787                         new_str = (char *)calloc(100, sizeof(char));
788                         if (new_str == NULL) {
789                                 _DEBUG_ERROR("new_str calloc failed");
790                                 goto return_part;
791                         }
792                         _DEBUG_INFO("str_child_vcalendar = %s", str_child_vcalendar);
793                         char *str_end_vevent = "\r\nEND:VEVENT";
794                         snprintf(new_str, 100, "%s%s%s", EXDATE_KEYWORD, exdate_str, str_end_vevent);
795                         _DEBUG_INFO("new_str = %s", new_str);
796                         str_child_vcalendar = replace_string(str_child_vcalendar, str_end_vevent, new_str);
797                         _DEBUG_INFO("str_child_vcalendar = %s", str_child_vcalendar);
798                         if (new_str) {
799                                 free(new_str);
800                                 new_str = NULL;
801                         }
802                         _DEBUG_INFO("str_child_vcalendar = %s", str_child_vcalendar);
803                 }
804
805                 if ((temp  = strstr(str_child_vcalendar, EXTYPE_DELETE_KEYWORD)) != NULL) {
806                         str_child_vcalendar = replace_string(str_child_vcalendar, EXTYPE_DELETE_KEYWORD, "UID:000000000000000000000000000000000000000000000000\r\nX-EXTYPE:1");
807                         _DEBUG_INFO("str_child_vcalendar = %s", str_child_vcalendar);
808                 }
809         }
810         else if (exdate_struct->exdate_type == CALENDAR_EXDATE_TYPE_UPDATED) {
811                 _DEBUG_INFO("update");
812                 // 2. remove RRULE / EXDATE
813                 // 2-1. remove 'RRULE'
814                 if ((old_str = strstr(str_child_vcalendar, RRULE_KEYWORD)) != NULL) {
815                         _DEBUG_INFO("rrule keyword");
816                         _DEBUG_INFO("old_str = %s", old_str);
817                         if ((new_str = strstr(old_str+2, "\r\n")) != NULL) {
818                                 _DEBUG_INFO("new_str = %s", new_str);
819                                 str_child_vcalendar = replace_string(str_child_vcalendar, old_str, new_str);
820                                 _DEBUG_INFO("str_child_vcalendar = %s", str_child_vcalendar);
821                         }
822                 }
823
824                 // 2-2. remove 'EXDATE' array
825                 if ((old_str = strstr(str_child_vcalendar, EXDATE_KEYWORD)) != NULL) {
826                         _DEBUG_INFO("exdate keyword");
827                         _DEBUG_INFO("old_str = %s", old_str);
828                         if ((new_str = strstr(old_str+2, "\r\n")) != NULL) {
829                                 _DEBUG_INFO("new_str = %s", new_str);
830                                 str_child_vcalendar = replace_string(str_child_vcalendar, old_str, new_str);
831                                 _DEBUG_INFO("str_child_vcalendar = %s", str_child_vcalendar);
832                         }
833                 }
834
835                 // 3. change DTSTART / DTEND
836                 // example ) DTSTART:20130125T000000Z / DTEND:20130125T003000Z / EXDATE:20130201T000000Z => DTSTART:20130201T000000Z / DTEND:20130201T003000Z
837                 // 3-1. change DTSTART : DTSTART = EXDATE
838                 char *temp = NULL;
839                 if ((temp  = strstr(str_child_vcalendar, DTSTART_KEYWORD)) != NULL) {
840                         _DEBUG_INFO("start keyword");
841                         _DEBUG_INFO("temp  = %s", temp );
842
843                         old_str = get_value_by_token(temp + 2, "DTSTART:", "\r\n");// '2' means strlen("\r\n")
844                         if (old_str == NULL) {
845                                 _DEBUG_ERROR("old_str is null");
846                                 goto return_part;
847                         }
848                         _DEBUG_INFO("old_str = %s", old_str);
849                         new_str = (char*)g_list_nth_data(exdate_struct->exdate_list, index);
850                         if (new_str == NULL) {
851                                 _DEBUG_ERROR("new_str is NULL");
852                                 goto return_part;
853                         }
854                         _DEBUG_INFO("new_str = %s", new_str);
855                         str_child_vcalendar = replace_string(str_child_vcalendar, old_str, new_str);
856                         _DEBUG_INFO("str_child_vcalendar = %s", str_child_vcalendar);
857                 }
858
859                 // 3-2. change DTEND : DTEND = date of EXDATE + time of origin DTEND
860                 temp = NULL;
861                 if ((temp  = strstr(str_child_vcalendar, DTEND_KEYWORD)) != NULL) {
862                         _DEBUG_INFO("end keyword");
863                         _DEBUG_INFO("temp  = %s", temp );
864
865                         old_str = get_value_by_token(temp + 2, "DTEND:", "\r\n");// '2' means strlen("\r\n")
866                         if (old_str == NULL) {
867                                 _DEBUG_ERROR("old_str is null");
868                                 goto return_part;
869                         }
870                         _DEBUG_INFO("old_str = %s", old_str);
871                         char *exdate_str = (char*)g_list_nth_data(exdate_struct->exdate_list, index);
872                         if (exdate_str == NULL) {
873                                 _DEBUG_ERROR("exdate_str is NULL");
874                                 goto return_part;
875                         }
876                         _DEBUG_INFO("exdate_str = %s", exdate_str);
877                         new_str = (char *)calloc(strlen(exdate_str), sizeof(char));
878                         if (new_str == NULL) {
879                                 _DEBUG_ERROR("new_str calloc failed");
880                                 goto return_part;
881                         }
882
883                         memcpy(new_str, exdate_str, 8);
884                         memcpy(new_str + 8, old_str + 8, 8);
885                         _DEBUG_INFO("new_str = %s", new_str);
886                         str_child_vcalendar = replace_string(str_child_vcalendar, old_str, new_str);
887                         _DEBUG_INFO("str_child_vcalendar = %s", str_child_vcalendar);
888                         if (new_str) {
889                                 free(new_str);
890                                 new_str = NULL;
891                         }
892                         _DEBUG_INFO("str_child_vcalendar = %s", str_child_vcalendar);
893                 }
894
895                 // 4. insert EXDATE
896                 if ((temp  = strstr(str_child_vcalendar, END_VEVENT_KEYWORD)) != NULL) {
897                         _DEBUG_INFO("EXTYPE keyword");
898                         _DEBUG_INFO("temp = %s", temp);
899
900                         char *exdate_str = (char*)g_list_nth_data(exdate_struct->exdate_list, index);
901                         if (exdate_str == NULL) {
902                                 _DEBUG_ERROR("exdate_str is NULL");
903                                 goto return_part;
904                         }
905                         _DEBUG_INFO("exdate_str = %s", exdate_str);
906
907                         new_str = (char *)calloc(100, sizeof(char));
908                         if (new_str == NULL) {
909                                 _DEBUG_ERROR("new_str calloc failed");
910                                 goto return_part;
911                         }
912                         _DEBUG_INFO("str_child_vcalendar = %s", str_child_vcalendar);
913                         char *str_end_vevent = "\r\nEND:VEVENT";
914                         snprintf(new_str, 100, "%s%s%s", EXDATE_KEYWORD, exdate_str, str_end_vevent);
915                         _DEBUG_INFO("new_str = %s", new_str);
916                         str_child_vcalendar = replace_string(str_child_vcalendar, str_end_vevent, new_str);
917                         _DEBUG_INFO("str_child_vcalendar = %s", str_child_vcalendar);
918                         if (new_str) {
919                                 free(new_str);
920                                 new_str = NULL;
921                         }
922                         _DEBUG_INFO("str_child_vcalendar = %s", str_child_vcalendar);
923                 }
924
925                 if ((temp  = strstr(str_child_vcalendar, EXTYPE_UPDATE_KEYWORD)) != NULL) {
926                         str_child_vcalendar = replace_string(str_child_vcalendar, EXTYPE_UPDATE_KEYWORD, "UID:000000000000000000000000000000000000000000000000\r\nX-EXTYPE:2");
927                         _DEBUG_INFO("str_child_vcalendar = %s", str_child_vcalendar);
928                 }
929         }
930         else {
931                 _DEBUG_INFO("cal_exdate_type(%d) is wrong type", exdate_struct->exdate_type);
932         }
933
934 return_part:
935         _INNER_FUNC_EXIT;
936         _DEBUG_INFO("last str_child_vcalendar = %s", str_child_vcalendar);
937         return str_child_vcalendar;
938 }
939
940 void insert_allday_data(const char *origin, calendar_record_h calendar_record)
941 {
942         _INNER_FUNC_ENTER;
943         retm_if((origin == NULL || strlen(origin) <= 0), "origin is NULL");
944         retm_if(calendar_record == NULL, "calendar_record is NULL");
945
946         int service_ret = CALENDAR_ERROR_NONE;
947         char *uri = NULL;
948
949         service_ret = calendar_record_get_uri_p(calendar_record, &uri);
950         if (service_ret != CALENDAR_ERROR_NONE) {
951                 _DEBUG_ERROR("[dc_vcalendar_plugIn] calendar_record_get_uri_p() Fail! : err[%d]", service_ret);
952                 goto return_part;
953         }
954
955         if (strncmp(uri, _calendar_event._uri, strlen(_calendar_event._uri))) {
956                 _DEBUG_ERROR("this is not event");
957                 goto return_part;
958         }
959
960         calendar_time_s cal_time_start = { 0, };
961         calendar_time_s cal_time_end = { 0, };
962         int year = 0;
963         int month = 0;
964         int day = 0;
965
966         service_ret = calendar_record_get_caltime(calendar_record, _calendar_event.start_time, &cal_time_start);
967         if (service_ret != CALENDAR_ERROR_NONE) {
968                 _DEBUG_ERROR("calendar_record_get_caltime() Fail: err[%d]", service_ret);
969                 goto return_part;
970         }
971
972         char *start_date_loc = NULL;
973         if ((start_date_loc = strstr(origin, DTSTART_KEYWORD)) == NULL) {
974                 _DEBUG_ERROR("start_date_loc is null");
975                 goto return_part;
976         }
977
978         char str_year_start[5] = {0,};
979         char str_month_start[3] = {0,};
980         char str_date_start[3] = {0,};
981
982         /* example) 20130124T000000Z\r\n = 2013 01 24 T 00 00 00 Z \r\n */
983         strncpy(str_year_start, start_date_loc + strlen(DTSTART_KEYWORD), 4);
984         strncpy(str_month_start, start_date_loc + strlen(DTSTART_KEYWORD) + 4, 2);
985         strncpy(str_date_start, start_date_loc + strlen(DTSTART_KEYWORD) + 4 + 2, 2);
986
987         year = atoi(str_year_start);
988         month = atoi(str_month_start);
989         day = atoi(str_date_start);
990
991         _DEBUG_INFO("start time : year = %d, month = %d, day = %d", year, month, day);
992         cal_time_start.type = CALENDAR_TIME_LOCALTIME;
993         cal_time_start.time.date.year = year;
994         cal_time_start.time.date.month = month;
995         cal_time_start.time.date.mday = day;
996
997         service_ret = calendar_record_set_caltime(calendar_record, _calendar_event.start_time, cal_time_start);
998         if (service_ret != CALENDAR_ERROR_NONE) {
999                 _DEBUG_ERROR("calendar_record_set_caltime() Fail: err[%d]", service_ret);
1000                 goto return_part;
1001         }
1002
1003         service_ret = calendar_record_get_caltime(calendar_record, _calendar_event.end_time, &cal_time_end);
1004         if (service_ret != CALENDAR_ERROR_NONE) {
1005                 _DEBUG_ERROR("calendar_record_get_caltime() Fail: err[%d]", service_ret);
1006                 goto return_part;
1007         }
1008
1009         char *end_date_loc = NULL;
1010         if ((end_date_loc = strstr(origin, DTEND_KEYWORD)) == NULL) {
1011                 _DEBUG_ERROR("end_date_loc is null");
1012                 goto return_part;
1013         }
1014
1015         char str_year_end[5] = {0,};
1016         char str_month_end[3] = {0,};
1017         char str_date_end[3] = {0,};
1018
1019         /* example) 20130124T000000Z\r\n = 2013 01 24 T 00 00 00 Z \r\n */
1020         strncpy(str_year_end, end_date_loc + strlen(DTEND_KEYWORD), 4);
1021         strncpy(str_month_end, end_date_loc + strlen(DTEND_KEYWORD) + 4, 2);
1022         strncpy(str_date_end, end_date_loc + strlen(DTEND_KEYWORD) + 4 + 2, 2);
1023
1024         year = atoi(str_year_end);
1025         month = atoi(str_month_end);
1026         day = atoi(str_date_end);
1027
1028         _DEBUG_INFO("end time : year = %d, month = %d, day = %d", year, month, day);
1029         cal_time_end.type = CALENDAR_TIME_LOCALTIME;
1030         cal_time_end.time.date.year = year;
1031         cal_time_end.time.date.month = month;
1032         cal_time_end.time.date.mday = day;
1033
1034         service_ret = calendar_record_set_caltime(calendar_record, _calendar_event.end_time, cal_time_end);
1035         if (service_ret != CALENDAR_ERROR_NONE) {
1036                 _DEBUG_ERROR("calendar_record_set_caltime() Fail: err[%d]", service_ret);
1037                 goto return_part;
1038         }
1039
1040 return_part:
1041         _INNER_FUNC_EXIT;
1042         return;
1043 }
1044
1045 EXPORT_API char * has_image_import(const char *origin)
1046 {
1047         _INNER_FUNC_ENTER;
1048
1049         char *temp_image_path = NULL;
1050         char *image_path = NULL;
1051
1052         if (strstr(origin, IMAGE_KEYWORD_PREFIX_PHOTO) == NULL && strstr(origin, IMAGE_KEYWORD_PREFIX_SNOTE) == NULL) {
1053                 _DEBUG_INFO("this calendar has not an image");
1054                 goto return_part;
1055         }
1056
1057         temp_image_path = get_value_by_token(origin, IMAGE_KEYWORD_BEGIN_PATH, IMAGE_KEYWORD_END_PATH);
1058
1059         if (strstr(temp_image_path, INTERNAL_MEMORY_CONVERTED) != NULL) {
1060                 image_path = replace_string(temp_image_path, INTERNAL_MEMORY_CONVERTED, INTERNAL_MEMORY_ORIGIN);
1061         }
1062         else if (strstr(temp_image_path, EXTERNAL_MEMORY_CONVERTED) != NULL) {
1063                 image_path = replace_string(temp_image_path, EXTERNAL_MEMORY_CONVERTED, EXTERNAL_MEMORY_ORIGIN);
1064         }
1065         else if (strstr(temp_image_path, SNOTE_MEMORY_ORIGIN) != NULL) {
1066                 image_path = strdup(temp_image_path);
1067         }
1068         else {
1069                 _DEBUG_ERROR("there is an error. origin = %s", origin);
1070                 goto return_part;
1071         }
1072
1073 return_part:
1074         if (image_path)
1075                 _DEBUG_INFO("image_path = %s", image_path);
1076         _INNER_FUNC_EXIT;
1077         return image_path;
1078 }
1079
1080 EXPORT_API char * has_image_export(calendar_record_h cal_record)
1081 {
1082         _INNER_FUNC_ENTER;
1083         retvm_if(cal_record == NULL, NULL, "cal_record is NULL");
1084
1085         calendar_record_h image_record = NULL;
1086         unsigned int child_record_count = 0;
1087         int i = 0;
1088         int service_ret = CALENDAR_ERROR_NONE;
1089         char *image_key = NULL;
1090         char *image_path = NULL;
1091
1092         char *uri = NULL;
1093         service_ret = calendar_record_get_uri_p(cal_record, &uri);
1094         if (service_ret != CALENDAR_ERROR_NONE) {
1095                 _DEBUG_ERROR("[dc_vcalendar-task_plugIn] calendar_record_get_uri_p() Fail! : err[%d]", service_ret);
1096                 goto return_part;
1097         }
1098         _DEBUG_INFO("uri = %s", uri);
1099         _DEBUG_INFO("_calendar_event.uri = %s", _calendar_event._uri);
1100
1101         service_ret = calendar_record_get_child_record_count(cal_record, _calendar_event.extended, &child_record_count);
1102         if (service_ret != CALENDAR_ERROR_NONE) {
1103                 _DEBUG_ERROR("calendar_record_get_child_record_count() Fail: err[%d]", service_ret);
1104                 goto return_part;
1105         }
1106
1107         for (i = 0; i < child_record_count; i++) {
1108                 service_ret = calendar_record_get_child_record_at_p(cal_record, _calendar_event.extended, i, &image_record);
1109                 if (service_ret != CALENDAR_ERROR_NONE) {
1110                         _DEBUG_ERROR("calendar_record_get_child_record_at_p() Fail: err[%d]", service_ret);
1111                         goto return_part;
1112                 }
1113
1114                 service_ret = calendar_record_get_str_p(image_record, _calendar_extended_property.key, &image_key);
1115                 if (service_ret != CALENDAR_ERROR_NONE || image_key == NULL || strlen(image_key) <= 0) {
1116                         _DEBUG_ERROR("calendar_record_get_str_p is failed(%d)", service_ret);
1117                         goto return_part;
1118                 }
1119
1120                 if (strcmp(image_key, CALENDAR_IMAGE_KEYWORD_SNOTE) == 0 || strcmp(image_key, CALENDAR_IMAGE_KEYWORD_PHOTO) == 0) {
1121                         _DEBUG_INFO("image is founded. image_key = %s", image_key);
1122                         service_ret = calendar_record_get_str(image_record, _calendar_extended_property.value, &image_path);
1123                         if (service_ret != CALENDAR_ERROR_NONE || image_path == NULL || strlen(image_path) <= 0) {
1124                                 _DEBUG_ERROR("calendar_record_get_str_p is failed(%d)", service_ret);
1125                                 image_path = NULL;
1126                                 goto return_part;
1127                         }
1128                         _DEBUG_INFO("image_path = %s", image_path);
1129                         continue;
1130                 }
1131         }
1132
1133 return_part:
1134         _INNER_FUNC_EXIT;
1135         return image_path;
1136 }
1137
1138 // "EXTYPE" setting, '1' means deleted, '2' means updated : it should be excuted before calendar_vcalendar_make_from_records
1139 void get_exdate_data(calendar_record_h cal_record, sync_agent_calendar_exdate_t *exdate_struct)
1140 {
1141         _INNER_FUNC_ENTER;
1142         retm_if(cal_record == NULL, "cal_record is NULL");
1143         retm_if(exdate_struct == NULL, "exdate_struct is NULL");
1144
1145         calendar_record_h extended_exdate = NULL;
1146         char *exdate = NULL;
1147         char *recurrence_id = NULL;
1148         int service_ret = CALENDAR_ERROR_NONE;
1149         int original_event_id = 0;
1150
1151         exdate_struct->exdate_type = CALENDAR_EXDATE_TYPE_NONE;
1152
1153
1154         char *uri = NULL;
1155         service_ret = calendar_record_get_uri_p(cal_record, &uri);
1156         if (service_ret != CALENDAR_ERROR_NONE) {
1157                 _DEBUG_ERROR("[dc_vcalendar_plugIn] calendar_record_get_uri_p() Fail! : err[%d]", service_ret);
1158                 goto return_part;
1159         }
1160         _DEBUG_INFO("uri = %s", uri);
1161
1162         // 1. delete : if 'exdate' value is exist, there are deleted item.
1163         service_ret = calendar_record_get_str(cal_record, _calendar_event.exdate, &exdate);
1164         if (service_ret != CALENDAR_ERROR_NONE) {
1165                 _DEBUG_ERROR("calendar_list_create() Fail!: err[%d]", service_ret);
1166                 goto return_part;
1167         }
1168         _DEBUG_INFO("exdate = %s", exdate);
1169
1170         if (exdate != NULL && strlen(exdate) > 0) {
1171                 _DEBUG_INFO("EXTYPE:DELETE setting start");
1172                 service_ret = calendar_record_create(_calendar_extended_property._uri, &extended_exdate);
1173                 if (service_ret != CALENDAR_ERROR_NONE) {
1174                         _DEBUG_ERROR("calendar_record_create() Fail!: err[%d]", service_ret);
1175                         goto return_part;
1176                 }
1177                 service_ret = calendar_record_set_str(extended_exdate, _calendar_extended_property.key, "X-EXTYPE");
1178                 if (service_ret != CALENDAR_ERROR_NONE) {
1179                         _DEBUG_ERROR("calendar_record_set_str() Fail!: err[%d]", service_ret);
1180                         goto return_part;
1181                 }
1182                 service_ret = calendar_record_set_str(extended_exdate, _calendar_extended_property.value, ":1");
1183                 if (service_ret != CALENDAR_ERROR_NONE) {
1184                         _DEBUG_ERROR("calendar_record_set_str() Fail!: err[%d]", service_ret);
1185                         goto return_part;
1186                 }
1187                 service_ret = calendar_record_add_child_record(cal_record, _calendar_event.extended, extended_exdate);
1188                 if (service_ret != CALENDAR_ERROR_NONE) {
1189                         _DEBUG_ERROR("calendar_record_add_child_record() Fail!: err[%d]", service_ret);
1190                         goto return_part;
1191                 }
1192
1193                 exdate_struct->exdate_type = CALENDAR_EXDATE_TYPE_DELETED;
1194                 char *str_exdate_loc = NULL;
1195                 int exdate_count = 0;
1196                 int exdate_loc = 0;
1197                 while (exdate_loc < strlen(exdate)) {
1198                         _DEBUG_INFO("111111111111 exdate_loc = %d", exdate_loc);
1199                         _DEBUG_INFO("111111111111 strlen(exdate) = %d", strlen(exdate));
1200                         int one_exdate_length = 0;
1201                         if ((str_exdate_loc = strstr(exdate + exdate_loc, ",")) != NULL) {
1202                                 one_exdate_length = strlen(exdate) - strlen(str_exdate_loc) - (exdate_count*(DATE_TIME_LENGTH+1));
1203                         }
1204                         else {
1205                                 one_exdate_length = strlen(exdate) - exdate_loc;
1206                         }
1207                         _DEBUG_INFO("str_exdate_loc = %s", str_exdate_loc);
1208                         if (one_exdate_length <= 0) {
1209                                 _DEBUG_ERROR("one_exdate_length (%d) <= 0", one_exdate_length);
1210                                 continue;
1211                         }
1212                         char *str_one_exdate = (char *)calloc(one_exdate_length, sizeof(char));
1213                         if (str_one_exdate == NULL) {
1214                                 _DEBUG_ERROR("str_one_exdate calloc is failed.");
1215                                 continue;
1216                         }
1217                         memcpy(str_one_exdate, exdate + exdate_loc, one_exdate_length);
1218                         exdate_loc += (one_exdate_length+1); // '1' meaning of ',' length
1219                         exdate_struct->exdate_list = g_list_append(exdate_struct->exdate_list, str_one_exdate);
1220                         exdate_count++;
1221                         _DEBUG_INFO("str_one_exdate = %s", str_one_exdate);
1222                         _DEBUG_INFO("one_exdate_length = %d", one_exdate_length);
1223                         _DEBUG_INFO("exdate_loc = %d", exdate_loc);
1224                         _DEBUG_INFO("exdate_count = %d\n\n", exdate_count);
1225                 }
1226
1227                 _DEBUG_INFO("exdate_loc = %d", exdate_loc);
1228                 _DEBUG_INFO("exdate_count = %d", exdate_count);
1229                 exdate_struct->exdate_count = exdate_count;
1230                 _DEBUG_INFO("EXTYPE:DELETE setting end");
1231         }
1232
1233         _DEBUG_INFO("222222222");
1234         // 2. update : if 'origianl_event_id' and 'recurrence_id' value are exist, there are updated item.
1235         service_ret = calendar_record_get_int(cal_record, _calendar_event.original_event_id, &original_event_id);
1236         if (service_ret != CALENDAR_ERROR_NONE) {
1237                 _DEBUG_ERROR("calendar_list_create() Fail!: err[%d]", service_ret);
1238                 goto return_part;
1239         }
1240         _DEBUG_INFO("original_event_id = %d", original_event_id);
1241
1242         service_ret = calendar_record_get_str(cal_record, _calendar_event.recurrence_id, &recurrence_id);
1243         if (service_ret != CALENDAR_ERROR_NONE) {
1244                 _DEBUG_ERROR("calendar_list_create() Fail!: err[%d]", service_ret);
1245                 goto return_part;
1246         }
1247         _DEBUG_INFO("recurrence_id = %s", recurrence_id);
1248
1249         if (original_event_id > 0 && recurrence_id != NULL && strlen(recurrence_id) > 0) {
1250                 _DEBUG_INFO("EXTYPE:UPDATE setting start");
1251                 service_ret = calendar_record_create(_calendar_extended_property._uri, &extended_exdate);
1252                 if (service_ret != CALENDAR_ERROR_NONE) {
1253                         _DEBUG_ERROR("calendar_record_create() Fail!: err[%d]", service_ret);
1254                         goto return_part;
1255                 }
1256                 service_ret = calendar_record_set_str(extended_exdate, _calendar_extended_property.key, "X-EXTYPE");
1257                 if (service_ret != CALENDAR_ERROR_NONE) {
1258                         _DEBUG_ERROR("calendar_record_set_str() Fail!: err[%d]", service_ret);
1259                         goto return_part;
1260                 }
1261                 service_ret = calendar_record_set_str(extended_exdate, _calendar_extended_property.value, ":2");
1262                 if (service_ret != CALENDAR_ERROR_NONE) {
1263                         _DEBUG_ERROR("calendar_record_set_str() Fail!: err[%d]", service_ret);
1264                         goto return_part;
1265                 }
1266                 service_ret = calendar_record_add_child_record(cal_record, _calendar_event.extended, extended_exdate);
1267                 if (service_ret != CALENDAR_ERROR_NONE) {
1268                         _DEBUG_ERROR("calendar_record_add_child_record() Fail!: err[%d]", service_ret);
1269                         goto return_part;
1270                 }
1271                 exdate_struct->exdate_type = CALENDAR_EXDATE_TYPE_UPDATED;
1272                 _DEBUG_INFO("EXTYPE:UPDATE setting end");
1273         }
1274
1275 return_part:
1276         _INNER_FUNC_EXIT;
1277         return;
1278 }
1279
1280 char * replace_max_rrule_data(char *origin)
1281 {
1282         _INNER_FUNC_ENTER;
1283         retvm_if(origin == NULL, origin, "origin is NULL.");
1284
1285         char *temp = NULL;
1286         char *temp_rrule = NULL;
1287         char *str_old_rrule = NULL;
1288         char *str_new_rrule = NULL;
1289
1290         if ((temp = strstr(origin, RRULE_KEYWORD)) == NULL) {
1291                 _DEBUG_INFO("this vcalendar hasn't rrule data");
1292                 goto return_part;
1293         }
1294
1295         str_old_rrule = get_value_by_token(temp + 2, "RRULE:", "\r\n");// '2' means strlen("\r\n")
1296         if (str_old_rrule == NULL) {
1297                 _DEBUG_ERROR("str_old_rrule is null");
1298                 goto return_part;
1299         }
1300
1301         if (strstr(str_old_rrule, MAX_RRULE_DATE_KEYWORD) == NULL) {
1302                 _DEBUG_INFO("this vcalendar has rrule data, But it's not until=NONE rrule");
1303                 goto return_part;
1304         }
1305         _DEBUG_INFO("this vcalendar has until=NONE rrule");
1306
1307         temp_rrule = get_value_by_token(temp, RRULE_KEYWORD, MAX_RRULE_DATE_KEYWORD);
1308         if (temp_rrule == NULL) {
1309                 _DEBUG_ERROR("temp_rrule is null");
1310                 goto return_part;
1311         }
1312
1313         str_new_rrule = (char *)calloc(strlen(temp_rrule) + 2, sizeof(char));
1314         if (str_new_rrule == NULL) {
1315                 _DEBUG_ERROR("str_new_rrule calloc failed");
1316                 goto return_part;
1317         }
1318
1319         // every eternal rrule data must have '#0' at the end of string (Calendar-Svc recommend)
1320         snprintf(str_new_rrule, strlen(temp_rrule) + 2, "%s#0", temp_rrule);
1321         origin = replace_string(origin, str_old_rrule, str_new_rrule);
1322
1323 return_part:
1324         if(str_old_rrule) {
1325                 free(str_old_rrule);
1326                 str_old_rrule = NULL;
1327         }
1328         if(str_new_rrule) {
1329                 free(str_new_rrule);
1330                 str_new_rrule = NULL;
1331         }
1332         if(temp_rrule) {
1333                 free(temp_rrule);
1334                 temp_rrule = NULL;
1335         }
1336         _INNER_FUNC_EXIT;
1337         _DEBUG_INFO("origin = %s", origin);
1338         return origin;
1339 }
1340
1341 char * replace_image_data(char *origin, char *image_path)
1342 {
1343         _INNER_FUNC_ENTER;
1344         retvm_if(origin == NULL, origin, "origin is NULL.");
1345         retvm_if(image_path == NULL, origin, "image_path is NULL.");
1346
1347         int image_path_length = 500;
1348         char *str_temp_image_info = NULL;
1349         char *str_image_info = NULL;
1350         char *str_image_tag = NULL;
1351
1352         _DEBUG_INFO("origin = %s", origin);
1353         _DEBUG_INFO("image_path = %s", image_path);
1354
1355         if (strstr(image_path, INTERNAL_MEMORY_ORIGIN) != NULL) {
1356                 str_temp_image_info = replace_string(image_path, INTERNAL_MEMORY_ORIGIN, INTERNAL_MEMORY_CONVERTED);
1357                 str_image_tag = strdup("X-SS-ATTACH-IMAGE");
1358         }
1359         else if (strstr(image_path, EXTERNAL_MEMORY_ORIGIN) != NULL) {
1360                 str_temp_image_info = replace_string(image_path, EXTERNAL_MEMORY_ORIGIN, EXTERNAL_MEMORY_CONVERTED);
1361                 str_image_tag = strdup("X-SS-ATTACH-IMAGE");
1362         }
1363         else if (strstr(image_path, SNOTE_MEMORY_ORIGIN) != NULL) {
1364                 str_temp_image_info = strdup(image_path);
1365                 //str_image_tag = strdup("X-SS-ATTACH-SNOTE");
1366                 str_image_tag = strdup("X-SS-ATTACH-IMAGE");
1367         }
1368         else {
1369                 _DEBUG_ERROR("there is an error. image_path = %s", image_path);
1370                 goto return_part;
1371         }
1372
1373         if (strstr(origin, END_VEVENT_KEYWORD) == NULL) {
1374                 _DEBUG_ERROR("this data is wrong format. origin = %s", origin);
1375                 goto return_part;
1376         }
1377
1378         if (image_path_length <= 0) {
1379                 _DEBUG_ERROR("image_path_length(%d) <= 0", image_path_length);
1380                 goto return_part;
1381         }
1382         str_image_info = (char *)calloc(image_path_length, sizeof(char));
1383         if (str_image_info == NULL) {
1384                 _DEBUG_ERROR("str_image_info calloc failed");
1385                 goto return_part;
1386         }
1387
1388         snprintf(str_image_info, image_path_length, "\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);
1389
1390         char *new_agent_data = replace_string(origin, END_VEVENT_KEYWORD, str_image_info);
1391         origin = NULL;
1392         origin = new_agent_data;
1393
1394 return_part:
1395
1396         if (str_image_info) {
1397                 free(str_image_info);
1398                 str_image_info = NULL;
1399         }
1400         if (str_image_tag) {
1401                 free(str_image_tag);
1402                 str_image_tag = NULL;
1403         }
1404         if (str_temp_image_info) {
1405                 free(str_temp_image_info);
1406                 str_temp_image_info = NULL;
1407         }
1408
1409         _INNER_FUNC_EXIT;
1410         _DEBUG_INFO("origin = %s", origin);
1411         return origin;
1412 }
1413
1414 // exdate string format like as "20130211T000000Z,20130211T000000Z,20130211T000000Z"
1415 sync_agent_da_return_e replace_exdate_data(const char *origin, calendar_record_h calendar_record)
1416 {
1417         _INNER_FUNC_ENTER;
1418         retvm_if(origin == NULL, SYNC_AGENT_DA_ERRORS, "origin is NULL.");
1419         retvm_if(calendar_record == NULL, SYNC_AGENT_DA_ERRORS, "calendar_record is NULL");
1420
1421         char *str_exdata_loc = NULL;
1422         char *old_exdate_str = NULL;
1423         char *new_exdate_str = NULL;
1424         char *new_one_exdate_str = NULL;
1425         int exdate_length = 0;
1426         int service_ret = CALENDAR_ERROR_NONE;
1427         sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
1428
1429         if ((str_exdata_loc = strstr(origin, IMPORT_EXTYPE_DELETE_KEYWORD)) == NULL) {
1430                 _DEBUG_ERROR("this vcalendar has not exdate data");
1431                 ret = SYNC_AGENT_DA_ERRORS;
1432                 goto return_part;
1433         }
1434
1435         new_one_exdate_str = get_value_by_token(str_exdata_loc + 2, "EXDATE:", "\r\n");
1436         _DEBUG_INFO("new_one_exdate_str = %s", new_one_exdate_str);
1437
1438         service_ret = calendar_record_get_str(calendar_record, _calendar_event.exdate, &old_exdate_str);
1439         if (service_ret != CALENDAR_ERROR_NONE) {
1440                 _DEBUG_ERROR("calendar_record_set_str() Fail! err[%d]\n", service_ret);
1441                 goto return_part;
1442         }
1443
1444         _DEBUG_INFO("old_exdate_str = %s", old_exdate_str);
1445         int old_exdate_str_length = 0;
1446         if (old_exdate_str == NULL)
1447                 old_exdate_str_length = 0;
1448         else
1449                 old_exdate_str_length = strlen(old_exdate_str);
1450
1451         exdate_length = old_exdate_str_length + strlen(new_one_exdate_str) + 2;
1452         _DEBUG_INFO("exdate_length = %d", exdate_length);
1453         new_exdate_str = (char *)calloc(exdate_length, sizeof(char));
1454         if (new_exdate_str == NULL) {
1455                 _DEBUG_ERROR("new_exdate_str calloc failed");
1456                 goto return_part;
1457         }
1458
1459         if (old_exdate_str == NULL)
1460                 snprintf(new_exdate_str, exdate_length, "%s", new_one_exdate_str);
1461         else
1462                 snprintf(new_exdate_str, exdate_length, "%s,%s", old_exdate_str, new_one_exdate_str);
1463
1464         _DEBUG_INFO("new_exdate_str = %s", new_exdate_str);
1465
1466         service_ret = calendar_record_set_str(calendar_record, _calendar_event.exdate, new_exdate_str);
1467         if (service_ret != CALENDAR_ERROR_NONE) {
1468                 _DEBUG_ERROR("calendar_record_set_str() Fail! err[%d]\n", service_ret);
1469                 goto return_part;
1470         }
1471
1472 return_part:
1473         if(new_one_exdate_str) {
1474                 free(new_one_exdate_str);
1475                 new_one_exdate_str = NULL;
1476         }
1477         if(new_exdate_str) {
1478                 free(new_exdate_str);
1479                 new_exdate_str = NULL;
1480         }
1481         _INNER_FUNC_EXIT;
1482         return ret;
1483 }