merge with master
[platform/core/system/sync-agent.git] / src / fw-plugins / common-public / vcalendar-task / 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 #ifndef EXPORT_API
21 #define EXPORT_API __attribute__ ((visibility("default")))
22 #endif
23
24 #if 0
25 // usage / rrule converting
26 if (strstr(temp_agent_data, "\r\nRRULE:") != NULL) {
27         _DEBUG_INFO("rrule is founed");
28         temp_agent_data = _replace_rrule_data(temp_agent_data);
29 }
30
31 static char * _replace_rrule_data(char *origin)
32 {
33         _INNER_FUNC_ENTER;
34         retvm_if(origin == NULL, origin, "origin is NULL.");
35
36         char *str_new_rrule = NULL;
37         char *str_end_time = NULL;
38         char *freq_loc = NULL;
39         char *str_byday = NULL;
40         char *str_interval = NULL;
41         char *str_period_until = NULL;
42         char *str_period_count = NULL;
43         calendar_date_type_e cal_type = CALENDAR_DATE_TYPE_ONTIME;
44
45         if ((freq_loc = strstr(origin, RRULE_KEYWORD_FREQ_WEEKLY)) != NULL) {
46                 _DEBUG_INFO("this is weekly data");
47         }
48         else if ((freq_loc = strstr(origin, RRULE_KEYWORD_FREQ_MONTHLY)) != NULL) {
49                 _DEBUG_INFO("this is monthly data");
50         }
51         else if ((freq_loc = strstr(origin, RRULE_KEYWORD_FREQ_YEARLY)) != NULL) {
52                 _DEBUG_INFO("this is yearly data");
53         }
54         else if ((freq_loc = strstr(origin, RRULE_KEYWORD_FREQ_DAILY)) != NULL) {
55                 _DEBUG_INFO("this is daily data");
56         }
57         else {
58                 _DEBUG_ERROR("this is an undefined rrule type. origin = %s", origin);
59                 goto return_part;
60         }
61
62         char *temp_rrule = NULL;
63         if((temp_rrule = strstr(freq_loc, "RRULE:")) == NULL) {
64                 _DEBUG_ERROR("temp_rrule is NULL");
65                 goto return_part;
66         }
67
68         char *temp_rrule2 = NULL;
69         if((temp_rrule2 = strstr(temp_rrule, "\r\n")) == NULL) {
70                 _DEBUG_ERROR("temp_rrule2 is NULL");
71                 goto return_part;
72         }
73
74         char str_origin_rrule[100] = {0,};
75         int origin_rrule_length = strlen(temp_rrule) - strlen(temp_rrule2) + 2; // '2' means of '\r\n'
76         if (origin_rrule_length <= 0) {
77                 _DEBUG_ERROR("origin_rrule_length <= 0");
78                 goto return_part;
79         }
80
81         strncpy(str_origin_rrule, temp_rrule, origin_rrule_length);
82         _DEBUG_INFO("str_origin_rrule = %s", str_origin_rrule);
83
84         if (strstr(origin, RRULE_KEYWORD_FREQ_WEEKLY) != NULL) {
85                 str_byday = get_value_by_token(freq_loc, RRULE_KEYWORD_BYDAY, ";");
86                 cal_type = CALENDAR_DATE_TYPE_WEEK;
87         }
88         else if (strstr(origin, RRULE_KEYWORD_FREQ_MONTHLY) != NULL) {
89                 str_byday = get_value_by_token(freq_loc, RRULE_KEYWORD_BYMONTHDAY, ";");
90                 cal_type = CALENDAR_DATE_TYPE_MONTH;
91         }
92         else if (strstr(origin, RRULE_KEYWORD_FREQ_YEARLY) != NULL) {
93                 str_byday = get_value_by_token(freq_loc, RRULE_KEYWORD_BYMONTHDAY, ";");
94                 cal_type = CALENDAR_DATE_TYPE_YEAR;
95         }
96         else if (strstr(origin, RRULE_KEYWORD_FREQ_DAILY) != NULL) {
97                 cal_type = CALENDAR_DATE_TYPE_DAY;
98         }
99         else {
100                 _DEBUG_ERROR("this is an undefined rrule type. origin = %s", origin);
101                 goto return_part;
102         }
103
104         if ((cal_type != CALENDAR_DATE_TYPE_DAY) && (str_byday == NULL || strlen(str_byday) <= 0)) {
105                 _DEBUG_ERROR("str_byday is null");
106                 goto return_part;
107         }
108
109         str_interval = get_value_by_token(freq_loc, RRULE_KEYWORD_INTERVAL, ";");
110         str_period_until = get_value_by_token(freq_loc, RRULE_KEYWORD_UNTIL, ";");
111         str_period_count = get_value_by_token(freq_loc, RRULE_KEYWORD_COUNT, ";");
112
113         if (str_interval == NULL || strlen(str_interval) <= 0) {
114                 _DEBUG_ERROR("str_interval is null");
115                 goto return_part;
116         }
117
118         if (str_period_until == NULL && str_period_count == NULL) {
119                 str_end_time = (char *)calloc(2, sizeof(char));
120                 if (str_end_time == NULL) {
121                         _DEBUG_ERROR("str_end_time is null");
122                         goto return_part;
123                 }
124                 strncpy(str_end_time, "\r\n", 2);
125                 _DEBUG_INFO("endless = %s", str_end_time);
126         }
127         else if (str_period_until != NULL) {
128                 str_end_time = (char *)calloc(strlen(str_period_until), sizeof(char));
129                 if (str_end_time == NULL) {
130                         _DEBUG_ERROR("str_end_time is null");
131                         goto return_part;
132                 }
133                 strncpy(str_end_time, str_period_until, strlen(str_period_until));
134                 _DEBUG_INFO("until = %s", str_end_time);
135         }
136         else if (str_period_count != NULL) {
137                 str_end_time = (char *)calloc(strlen(str_period_count)+1, sizeof(char));
138                 if (str_end_time == NULL) {
139                         _DEBUG_ERROR("str_end_time is null");
140                         goto return_part;
141                 }
142                 snprintf(str_end_time, strlen(str_period_count)+1, "#%s", str_period_count);
143                 _DEBUG_INFO("count = %s", str_end_time);
144         }
145         else {
146                 _DEBUG_ERROR("there is an error");
147                 goto return_part;
148         }
149
150         int new_rrule_length = 0;
151         if (strstr(origin, RRULE_KEYWORD_FREQ_WEEKLY) != NULL) {
152                 new_rrule_length = strlen("RRULE:W") + strlen(str_interval) + strlen(" ") + strlen(str_byday) + strlen("  ") + strlen(str_end_time) + 1;
153         }
154         else if (strstr(origin, RRULE_KEYWORD_FREQ_MONTHLY) != NULL) {
155                 new_rrule_length = strlen("RRULE:MD") + strlen(str_interval) + strlen(" ") + strlen(str_byday) + strlen("  ") + strlen(str_end_time) + 1;
156         }
157         else if (strstr(origin, RRULE_KEYWORD_FREQ_YEARLY) != NULL) {
158                 new_rrule_length = strlen("RRULE:YD") + strlen(str_interval) + strlen(" ") + strlen(str_end_time) + 1;
159         }
160         else if (strstr(origin, RRULE_KEYWORD_FREQ_DAILY) != NULL) {
161                 new_rrule_length = strlen("RRULE:D") + strlen(str_interval) + strlen(" ") + strlen(str_end_time) + 1;
162         }
163         else {
164                 _DEBUG_ERROR("this is an undefined rrule type. origin = %s", origin);
165                 goto return_part;
166         }
167
168         if (new_rrule_length <= 0) {
169                 _DEBUG_ERROR("new_rrule_length(%d) <= 0", new_rrule_length);
170                 goto return_part;
171         }
172
173         str_new_rrule = (char *)calloc(new_rrule_length, sizeof(char));
174         if (str_new_rrule == NULL) {
175                 _DEBUG_ERROR("str_new_rrule calloc failed");
176                 goto return_part;
177         }
178
179         if (strstr(origin, RRULE_KEYWORD_FREQ_WEEKLY) != NULL) {
180                 snprintf(str_new_rrule, new_rrule_length, "RRULE:W%s %s  %s", str_interval, str_byday, str_end_time);
181         }
182         else if (strstr(origin, RRULE_KEYWORD_FREQ_MONTHLY) != NULL) {
183                 snprintf(str_new_rrule, new_rrule_length, "RRULE:MD%s %s  %s", str_interval, str_byday, str_end_time);
184         }
185         else if (strstr(origin, RRULE_KEYWORD_FREQ_YEARLY) != NULL) {
186                 snprintf(str_new_rrule, new_rrule_length, "RRULE:YD%s %s", str_interval, str_end_time);
187         }
188         else if (strstr(origin, RRULE_KEYWORD_FREQ_DAILY) != NULL) {
189                 snprintf(str_new_rrule, new_rrule_length, "RRULE:D%s %s", str_interval, str_end_time);
190         }
191         else {
192                 _DEBUG_ERROR("this is an undefined rrule type. origin = %s", origin);
193                 goto return_part;
194         }
195
196         _DEBUG_INFO("str_new_rrule = %s", str_new_rrule);
197         _DEBUG_INFO("str_origin_rrule = %s", str_origin_rrule);
198
199         char *new_agent_data = replace_string(origin, str_origin_rrule, str_new_rrule);
200         origin = NULL;
201         origin = new_agent_data;
202
203 return_part:
204
205         if (str_interval) {
206                 free(str_interval);
207                 str_interval = NULL;
208         }
209         if (str_byday) {
210                 free(str_byday);
211                 str_byday = NULL;
212         }
213         if (str_period_until) {
214                 free(str_period_until);
215                 str_period_until = NULL;
216         }
217         if (str_period_count) {
218                 free(str_period_count);
219                 str_period_count = NULL;
220         }
221         if (str_end_time) {
222                 free(str_end_time);
223                 str_end_time = NULL;
224         }
225         if (str_new_rrule) {
226                 free(str_new_rrule);
227                 str_new_rrule = NULL;
228         }
229
230         _DEBUG_INFO("origin = %s", origin);
231         _INNER_FUNC_EXIT;
232         return origin;
233 }
234
235 // usage / alarm converting
236 if (strstr(temp_agent_data, "\r\nBEGIN:VALARM") != NULL) {
237         _DEBUG_INFO("alarm is founed");
238         temp_agent_data = _replace_alarm_data(temp_agent_data);
239 }
240
241 static char * _replace_alarm_data(char *origin)
242 {
243         _INNER_FUNC_ENTER;
244         retvm_if(origin == NULL, origin, "origin is NULL.");
245
246         int interval = 0;
247         char *str_new_alarm = NULL;
248         char *freq_loc = NULL;
249         char *str_trigger = NULL;
250         char *str_customized_trigger = NULL;
251         char *str_interval = NULL;
252         calendar_date_type_e cal_type = CALENDAR_DATE_TYPE_ONTIME;
253
254         if ((freq_loc = strstr(origin, BEGIN_ALARM_KEYWORD)) == NULL) {
255                 _DEBUG_ERROR("there is not trigger data");
256                 goto return_part;
257         }
258
259         char *temp = NULL;
260         if((temp = strstr(freq_loc, "BEGIN:VALARM\r\n")) == NULL) {
261                 _DEBUG_ERROR("temp is NULL");
262                 goto return_part;
263         }
264
265         char *temp2 = NULL;
266         if((temp2 = strstr(temp, "END:VALARM\r\n")) == NULL) {
267                 _DEBUG_ERROR("temp2 is NULL");
268                 goto return_part;
269         }
270
271         char str_origin_alarm[100] = {0,};
272         int origin_alarm_length = strlen(temp) - strlen(temp2) + 12; // '12' means of "END:VALARM\r\n"
273         if (origin_alarm_length <= 0) {
274                 _DEBUG_ERROR("origin_alarm_length <= 0");
275                 goto return_part;
276         }
277
278         strncpy(str_origin_alarm, temp, origin_alarm_length);
279         _DEBUG_INFO("str_origin_alarm = %s", str_origin_alarm);
280
281         // case 1. "TRIGGER;VALUE=DATE-TIME:" is an alarm that created by user
282         str_customized_trigger = _get_value_by_token(freq_loc, "TRIGGER;VALUE=DATE-TIME:", "\r\n");
283         if (str_customized_trigger != NULL) {
284                 _DEBUG_INFO("str_customized_trigger is founded. str_customized_trigger = %s", str_customized_trigger);
285
286                 str_new_alarm = (char *)calloc(30, sizeof(char));
287                 if (str_new_alarm == NULL) {
288                         _DEBUG_ERROR("new_date_time is null");
289                         goto return_part;
290                 }
291
292                 snprintf(str_new_alarm, 30, "AALARM:%s\r\n", str_customized_trigger);
293                 _DEBUG_INFO("str_new_alarm = %s", str_new_alarm);
294                 char *new_agent_data = _replace_string(origin, str_origin_alarm, str_new_alarm);
295                 origin = NULL;
296                 origin = new_agent_data;
297                 goto return_part;
298         }
299
300         // case 2. "TRIGGER:" is a normal alarm
301         str_trigger = _get_value_by_token(freq_loc, "TRIGGER:", "\r\n");
302         if (str_trigger == NULL || strlen(str_trigger) <= 0) {
303                 _DEBUG_ERROR("str_trigger is null");
304                 goto return_part;
305         }
306
307         char *temp_alarm = NULL;
308         char *temp_alarm2 = NULL;
309         if((temp_alarm = strstr(str_trigger, "P0W")) != NULL) {
310                 cal_type = CALENDAR_DATE_TYPE_ONTIME;
311                 interval = 0;
312         }
313         else if((temp_alarm = strstr(str_trigger, "PT")) != NULL) {
314                 if((temp_alarm2 = strstr(temp_alarm, "H")) != NULL) {
315                         cal_type = CALENDAR_DATE_TYPE_HOUR;
316                         str_interval = _get_value_by_token(str_trigger, "PT", "H");
317                         interval = atoi(str_interval);
318                 }
319                 else if((temp_alarm2 = strstr(temp_alarm, "M")) != NULL) {
320                         cal_type = CALENDAR_DATE_TYPE_MINUTE;
321                         str_interval = _get_value_by_token(str_trigger, "PT", "M");
322                         interval = atoi(str_interval);
323                 }
324                 else {
325                         _DEBUG_ERROR("wrong type, temp_alarm = %s", temp_alarm);
326                         goto return_part;
327                 }
328         }
329         else if((temp_alarm = strstr(str_trigger, "P")) != NULL) {
330                 if((temp_alarm2 = strstr(temp_alarm, "D")) != NULL) {
331                         cal_type = CALENDAR_DATE_TYPE_DAY;
332                         str_interval = _get_value_by_token(str_trigger, "P", "D");
333                         interval = atoi(str_interval);
334                 }
335                 else if((temp_alarm2 = strstr(temp_alarm, "W")) != NULL) {
336                         cal_type = CALENDAR_DATE_TYPE_WEEK;
337                         str_interval = _get_value_by_token(str_trigger, "P", "W");
338                         interval = atoi(str_interval);
339                 }
340                 else {
341                         _DEBUG_ERROR("wrong type, temp_alarm = %s", temp_alarm);
342                         goto return_part;
343                 }
344         }
345         else {
346                 _DEBUG_ERROR("wrong type, temp_alarm = %s", temp_alarm);
347                 goto return_part;
348         }
349
350         str_new_alarm = _calculate_date_time(origin, interval, cal_type);
351         if (str_new_alarm == NULL || strlen(str_new_alarm) <= 0) {
352                 _DEBUG_ERROR("str_new_alarm is null");
353                 goto return_part;
354         }
355
356         _DEBUG_INFO("str_new_alarm = %s", str_new_alarm);
357         _DEBUG_INFO("str_origin_alarm = %s", str_origin_alarm);
358
359         char *new_agent_data = _replace_string(origin, str_origin_alarm, str_new_alarm);
360         origin = NULL;
361         origin = new_agent_data;
362
363 return_part:
364
365         if (str_trigger) {
366                 free(str_trigger);
367                 str_trigger = NULL;
368         }
369         if (str_customized_trigger) {
370                 free(str_customized_trigger);
371                 str_customized_trigger = NULL;
372         }
373         if (str_new_alarm) {
374                 free(str_new_alarm);
375                 str_new_alarm = NULL;
376         }
377         if (str_interval) {
378                 free(str_interval);
379                 str_interval = NULL;
380         }
381
382         _INNER_FUNC_EXIT;
383         _DEBUG_INFO("origin = %s", origin);
384         return origin;
385 }
386
387
388 static char * _calculate_date_time(char *origin, int value, calendar_date_type_e type)
389 {
390         _INNER_FUNC_ENTER;
391         retvm_if(origin == NULL, NULL, "origin is NULL");
392
393         _DEBUG_INFO("origin = %s", origin);
394         _DEBUG_INFO("value = %d", value);
395
396         UCalendar *ucal = NULL;
397         char *start_time = NULL;
398         char *new_date_time = NULL;
399         char *start_date_loc = NULL;
400         if ((start_date_loc = strstr(origin, DTSTART_KEYWORD)) == NULL) {
401                 _DEBUG_ERROR("start_date_loc is null");
402                 goto return_part;
403         }
404
405         char str_year[5] = {0,};
406         char str_month[3] = {0,};
407         char str_date[3] = {0,};
408         char str_hour[3] = {0,};
409         char str_minute[3] = {0,};
410         char str_second[3] = {0,};
411
412         /* example) 20130124T000000Z\r\n = 2013 01 24 T 00 00 00 Z \r\n */
413         strncpy(str_year, start_date_loc + strlen(DTSTART_KEYWORD), 4);
414         strncpy(str_month, start_date_loc + strlen(DTSTART_KEYWORD) + 4, 2);
415         strncpy(str_date, start_date_loc + strlen(DTSTART_KEYWORD) + 4 + 2, 2);
416         strncpy(str_hour, start_date_loc + strlen(DTSTART_KEYWORD) + 4 + 2 + 2 + 1, 2);
417         strncpy(str_minute, start_date_loc + strlen(DTSTART_KEYWORD) + 4 + 2 + 2 + 1 + 2, 2);
418         strncpy(str_second, start_date_loc + strlen(DTSTART_KEYWORD) + 4 + 2 + 2 + 1 + 2 + 2, 2);
419
420         int year = atoi(str_year);
421         int month = atoi(str_month);
422         int date = atoi(str_date);
423         int hour = atoi(str_hour);
424         int minute = atoi(str_minute);
425         int second = atoi(str_second);
426         long long int date_time = 0;
427
428         start_time = (char *)calloc(20, sizeof(char));
429         if (start_time == NULL) {
430                 _DEBUG_ERROR("start_time is null");
431                 goto return_part;
432         }
433
434         snprintf(start_time, 20, "%04d%02d%02dT%02d%02d%02dZ\r\n", year, month, date, hour, minute, second);
435         _DEBUG_INFO("start_time = %s", start_time);
436
437         UErrorCode status = U_ZERO_ERROR;
438         ucal = ucal_open(0, -1, uloc_getDefault(), UCAL_TRADITIONAL, &status);
439         if (U_FAILURE(status)) {
440                 _DEBUG_ERROR("ucal_open failed (%s)", u_errorName(status));
441                 goto return_part;
442         }
443
444         ucal_setAttribute(ucal, UCAL_LENIENT, 1);
445         ucal_setDateTime(ucal, year, month - 1, date, hour, minute, second, &status);
446         date_time = ucal_getMillis(ucal, &status);
447
448         switch (type) {
449         case CALENDAR_DATE_TYPE_ONTIME:
450                 _DEBUG_INFO("CALENDAR_DATE_TYPE_ONTIME");
451                 date_time = date_time / 1000;
452                 break;
453         case CALENDAR_DATE_TYPE_WEEK:
454                 _DEBUG_INFO("CALENDAR_DATE_TYPE_WEEK");
455                 date_time = date_time / 1000;
456                 date_time = date_time - (value*60*60*24*7);
457                 break;
458         case CALENDAR_DATE_TYPE_DAY:
459                 _DEBUG_INFO("CALENDAR_DATE_TYPE_DAY");
460                 date_time = date_time / 1000;
461                 date_time = date_time - (value*60*60*24);
462                 break;
463         case CALENDAR_DATE_TYPE_HOUR:
464                 _DEBUG_INFO("CALENDAR_DATE_TYPE_HOUR");
465                 date_time = date_time / 1000;
466                 date_time = date_time - (value*60*60);
467                 break;
468         case CALENDAR_DATE_TYPE_MINUTE:
469                 _DEBUG_INFO("CALENDAR_DATE_TYPE_MINUTE");
470                 date_time = date_time / 1000;
471                 date_time = date_time - (value*60);
472                 break;
473         default:
474                 _DEBUG_ERROR("%d is not supported type", type);
475                 goto return_part;
476                 break;
477         }
478
479         if (U_FAILURE(status)) {
480                 _DEBUG_ERROR("ucal_setDateTime failed (%s)", u_errorName(status));
481                 goto return_part;
482         }
483
484         ucal_setMillis(ucal, (date_time * 1000), &status);
485
486         year = month = date = hour = minute = second = 0;
487         year = ucal_get(ucal, UCAL_YEAR, &status);
488         month = ucal_get(ucal, UCAL_MONTH, &status) + 1;
489         date = ucal_get(ucal, UCAL_DATE, &status);
490         hour = ucal_get(ucal, UCAL_HOUR_OF_DAY, &status);
491         minute = ucal_get(ucal, UCAL_MINUTE, &status);
492         second = ucal_get(ucal, UCAL_SECOND, &status);
493
494         new_date_time = (char *)calloc(30, sizeof(char));
495         if (new_date_time == NULL) {
496                 _DEBUG_ERROR("new_date_time is null");
497                 goto return_part;
498         }
499
500         snprintf(new_date_time, 30, "AALARM:%04d%02d%02dT%02d%02d%02dZ\r\n", year, month, date, hour, minute, second);
501         _DEBUG_INFO("new_date_time = %s", new_date_time);
502
503 return_part:
504         _DEBUG_INFO("return_part");
505
506         if(start_time) {
507                 free(start_time);
508                 start_time = NULL;
509         }
510         if(ucal) {
511                 ucal_close(ucal);
512                 ucal = NULL;
513         }
514
515         _INNER_FUNC_EXIT;
516         return new_date_time;
517 }
518
519 #endif
520
521 char * get_value_by_token(const char *src, const char *token, const char *end_of_token)
522 {
523         _INNER_FUNC_ENTER;
524         if (src == NULL || strlen(src) <= 0) {
525                 _DEBUG_ERROR("src is null");
526                 return NULL;
527         }
528
529         char *token_loc = NULL;
530         char *str_output = NULL;
531
532         if ((token_loc = strstr(src, token)) != NULL) {
533                 char *str_temp = strstr(token_loc, end_of_token);
534                 if (str_temp != NULL) {
535                         _DEBUG_INFO("token = %s", token);
536                         _DEBUG_INFO("str_temp = %s", str_temp);
537                         int ret_str_length = strlen(token_loc) - strlen(token) - strlen(str_temp);
538                         if (ret_str_length <= 0) {
539                                 _DEBUG_ERROR("ret_str_length(%d) <= 0", ret_str_length);
540                                 goto return_part;
541                         }
542                         str_output = (char *)calloc(ret_str_length, sizeof(char));
543                         if (str_output == NULL) {
544                                 _DEBUG_ERROR("calloc failed");
545                                 goto return_part;
546                         }
547                         memcpy(str_output, token_loc + strlen(token), ret_str_length);
548                 }
549         }
550
551 return_part:
552         _INNER_FUNC_EXIT;
553         return str_output;
554 }
555
556 char * replace_string(char *origin, const char *src, const char *dest)
557 {
558         _INNER_FUNC_ENTER;
559
560         retvm_if((origin == NULL || strlen(origin) <= 0), NULL, "origin is NULL");
561         retvm_if((src == NULL || strlen(src) <= 0), NULL, "src is NULL");
562         retvm_if((dest == NULL || strlen(dest) <= 0), NULL, "dest is NULL");
563
564         _DEBUG_INFO("origin = %s", origin);
565         _DEBUG_INFO("src = %s", src);
566         _DEBUG_INFO("dest = %s", dest);
567
568         char *str_top = NULL;
569
570         if ((str_top = strstr(origin, src)) == NULL) {
571                 _DEBUG_INFO("%s is not founed", src);
572                 return origin;
573         }
574
575         int top_length = strlen(origin) - strlen(str_top);
576         int middle_length = strlen(dest);
577         int down_length = strlen(origin) - top_length - strlen(src);
578         int new_str_length = top_length + middle_length + down_length + 1;
579         if (new_str_length <= 0) {
580                 _DEBUG_ERROR("new_str_length(%d) <= 0", new_str_length);
581                 return origin;
582         }
583         char *new_str = (char *)calloc(new_str_length, sizeof(char));
584         if (new_str == NULL) {
585                 _DEBUG_ERROR("calloc is failed.");
586                 return origin;
587         }
588
589         memset(new_str, 0, new_str_length);
590         memcpy(new_str, origin, top_length);
591         memcpy(new_str + top_length, dest, middle_length);
592         memcpy(new_str + top_length + middle_length, origin + top_length + strlen(src), down_length);
593
594         if (origin != NULL) {
595                 free(origin);
596                 origin = NULL;
597         }
598
599         _DEBUG_INFO("new_str = %s", new_str);
600         _INNER_FUNC_EXIT;
601         return new_str;
602 }
603
604 char * remove_timezone(char *origin)
605 {
606         _INNER_FUNC_ENTER;
607         retvm_if((origin == NULL || strlen(origin) <= 0), NULL, "origin is NULL");
608
609         _DEBUG_INFO("origin = %s", origin);
610
611         char *str_timezone = NULL;
612         char *str_temp = NULL;
613
614         if ((str_temp = strstr(origin, "\r\nDTSTART;TZID=")) != NULL) {
615                 _DEBUG_INFO("str_temp = %s", str_temp);
616                 str_timezone = get_value_by_token(str_temp, "\r\nDTSTART", ":");
617                 _DEBUG_INFO("str_timezone = %s", str_timezone);
618                 strcat(str_timezone, ":");
619                 _DEBUG_INFO("str_timezone = %s", str_timezone);
620                 origin = replace_string(origin, str_timezone, ":");
621
622                 if (str_timezone)
623                         free(str_timezone);
624                 str_timezone = NULL;
625         }
626
627         if ((str_temp = strstr(origin, "\r\nDUE;TZID=")) != NULL) {
628                 _DEBUG_INFO("str_temp = %s", str_temp);
629                 str_timezone = get_value_by_token(str_temp, "\r\nDUE", ":");
630                 _DEBUG_INFO("str_timezone = %s", str_timezone);
631                 strcat(str_timezone, ":");
632                 _DEBUG_INFO("str_timezone = %s", str_timezone);
633                 origin = replace_string(origin, str_timezone, ":");
634
635                 if (str_timezone)
636                         free(str_timezone);
637                 str_timezone = NULL;
638         }
639
640         _DEBUG_INFO("origin = %s", origin);
641         _INNER_FUNC_EXIT;
642         return origin;
643 }
644
645 void insert_allday_data(const char *origin, calendar_record_h calendar_record)
646 {
647         _INNER_FUNC_ENTER;
648         retm_if((origin == NULL || strlen(origin) <= 0), "origin is NULL");
649         retm_if(calendar_record == NULL, "calendar_record is NULL");
650
651         int service_ret = CALENDAR_ERROR_NONE;
652         char *uri = NULL;
653
654         service_ret = calendar_record_get_uri_p(calendar_record, &uri);
655         if (service_ret != CALENDAR_ERROR_NONE) {
656                 _DEBUG_ERROR("calendar_record_get_uri_p() Fail! : err[%d]", service_ret);
657                 goto return_part;
658         }
659
660         if (strncmp(uri, _calendar_todo._uri, strlen(_calendar_todo._uri))) {
661                 _DEBUG_ERROR("this is not todo");
662                 goto return_part;
663         }
664
665         calendar_time_s cal_time_start = { 0, };
666         calendar_time_s cal_time_end = { 0, };
667         int year = 0;
668         int month = 0;
669         int day = 0;
670
671         service_ret = calendar_record_get_caltime(calendar_record, _calendar_todo.start_time, &cal_time_start);
672         if (service_ret != CALENDAR_ERROR_NONE) {
673                 _DEBUG_ERROR("calendar_record_get_caltime() Fail: err[%d]", service_ret);
674                 goto return_part;
675         }
676
677         char *start_date_loc = NULL;
678         if ((start_date_loc = strstr(origin, DTSTART_KEYWORD)) == NULL) {
679                 _DEBUG_ERROR("start_date_loc is null");
680                 goto return_part;
681         }
682
683         char str_year_start[5] = {0,};
684         char str_month_start[3] = {0,};
685         char str_date_start[3] = {0,};
686
687         /* example) 20130124T000000Z\r\n = 2013 01 24 T 00 00 00 Z \r\n */
688         strncpy(str_year_start, start_date_loc + strlen(DTSTART_KEYWORD), 4);
689         strncpy(str_month_start, start_date_loc + strlen(DTSTART_KEYWORD) + 4, 2);
690         strncpy(str_date_start, start_date_loc + strlen(DTSTART_KEYWORD) + 4 + 2, 2);
691
692         year = atoi(str_year_start);
693         month = atoi(str_month_start);
694         day = atoi(str_date_start);
695
696         _DEBUG_INFO("start time : year = %d, month = %d, day = %d", year, month, day);
697         cal_time_start.type = CALENDAR_TIME_LOCALTIME;
698         cal_time_start.time.date.year = year;
699         cal_time_start.time.date.month = month;
700         cal_time_start.time.date.mday = day;
701
702         service_ret = calendar_record_set_caltime(calendar_record, _calendar_todo.start_time, cal_time_start);
703         if (service_ret != CALENDAR_ERROR_NONE) {
704                 _DEBUG_ERROR("calendar_record_set_caltime() Fail: err[%d]", service_ret);
705                 goto return_part;
706         }
707
708         service_ret = calendar_record_get_caltime(calendar_record, _calendar_todo.due_time, &cal_time_end);
709         if (service_ret != CALENDAR_ERROR_NONE) {
710                 _DEBUG_ERROR("calendar_record_get_caltime() Fail: err[%d]", service_ret);
711                 goto return_part;
712         }
713
714         char *end_date_loc = NULL;
715         if ((end_date_loc = strstr(origin, DTEND_KEYWORD)) == NULL) {
716                 _DEBUG_ERROR("end_date_loc is null");
717                 goto return_part;
718         }
719
720         char str_year_end[5] = {0,};
721         char str_month_end[3] = {0,};
722         char str_date_end[3] = {0,};
723
724         /* example) 20130124T000000Z\r\n = 2013 01 24 T 00 00 00 Z \r\n */
725         strncpy(str_year_end, end_date_loc + strlen(DTEND_KEYWORD), 4);
726         strncpy(str_month_end, end_date_loc + strlen(DTEND_KEYWORD) + 4, 2);
727         strncpy(str_date_end, end_date_loc + strlen(DTEND_KEYWORD) + 4 + 2, 2);
728
729         year = atoi(str_year_end);
730         month = atoi(str_month_end);
731         day = atoi(str_date_end);
732
733         _DEBUG_INFO("end time : year = %d, month = %d, day = %d", year, month, day);
734         cal_time_end.type = CALENDAR_TIME_LOCALTIME;
735         cal_time_end.time.date.year = year;
736         cal_time_end.time.date.month = month;
737         cal_time_end.time.date.mday = day;
738
739         service_ret = calendar_record_set_caltime(calendar_record, _calendar_todo.due_time, cal_time_end);
740         if (service_ret != CALENDAR_ERROR_NONE) {
741                 _DEBUG_ERROR("calendar_record_set_caltime() Fail: err[%d]", service_ret);
742                 goto return_part;
743         }
744
745 return_part:
746         _INNER_FUNC_EXIT;
747         return;
748 }
749
750 EXPORT_API char * has_image_import(const char *origin)
751 {
752         _INNER_FUNC_ENTER;
753
754         char *temp_image_path = NULL;
755         char *image_path = NULL;
756
757         if (strstr(origin, IMAGE_KEYWORD_PREFIX_PHOTO) == NULL && strstr(origin, IMAGE_KEYWORD_PREFIX_SNOTE) == NULL) {
758                 _DEBUG_INFO("this calendar has not an image");
759                 goto return_part;
760         }
761
762         temp_image_path = get_value_by_token(origin, IMAGE_KEYWORD_BEGIN_PATH, IMAGE_KEYWORD_END_PATH);
763
764         if (strstr(temp_image_path, INTERNAL_MEMORY_CONVERTED) != NULL) {
765                 image_path = replace_string(temp_image_path, INTERNAL_MEMORY_CONVERTED, INTERNAL_MEMORY_ORIGIN);
766         }
767         else if (strstr(temp_image_path, EXTERNAL_MEMORY_CONVERTED) != NULL) {
768                 image_path = replace_string(temp_image_path, EXTERNAL_MEMORY_CONVERTED, EXTERNAL_MEMORY_ORIGIN);
769         }
770         else if (strstr(temp_image_path, SNOTE_MEMORY_ORIGIN) != NULL) {
771                 image_path = strdup(temp_image_path);
772         }
773         else {
774                 _DEBUG_ERROR("there is an error. origin = %s", origin);
775                 goto return_part;
776         }
777
778 return_part:
779         if (image_path)
780                 _DEBUG_INFO("image_path = %s", image_path);
781         _INNER_FUNC_EXIT;
782         return image_path;
783 }
784
785 EXPORT_API char * has_image_export(calendar_record_h cal_record)
786 {
787         _INNER_FUNC_ENTER;
788
789         calendar_record_h image_record = NULL;
790         unsigned int child_record_count = 0;
791         int i = 0;
792         int service_ret = CALENDAR_ERROR_NONE;
793         char *image_key = NULL;
794         char *image_path = NULL;
795
796         char *uri = NULL;
797         service_ret = calendar_record_get_uri_p(cal_record, &uri);
798         if (service_ret != CALENDAR_ERROR_NONE) {
799                 _DEBUG_ERROR("[dc_vcalendar-task_plugIn] calendar_record_get_uri_p() Fail! : err[%d]", service_ret);
800                 goto return_part;
801         }
802         _DEBUG_INFO("uri = %s", uri);
803         _DEBUG_INFO("_calendar_todo.uri = %s", _calendar_todo._uri);
804
805         service_ret = calendar_record_get_child_record_count(cal_record, _calendar_todo.extended, &child_record_count);
806         if (service_ret != CALENDAR_ERROR_NONE) {
807                 _DEBUG_ERROR("calendar_record_get_child_record_count() Fail: err[%d]", service_ret);
808                 goto return_part;
809         }
810
811         for (i = 0; i < child_record_count; i++) {
812                 service_ret = calendar_record_get_child_record_at_p(cal_record, _calendar_todo.extended, i, &image_record);
813                 if (service_ret != CALENDAR_ERROR_NONE) {
814                         _DEBUG_ERROR("calendar_record_get_child_record_at_p() Fail: err[%d]", service_ret);
815                         goto return_part;
816                 }
817
818                 service_ret = calendar_record_get_str_p(image_record, _calendar_extended_property.key, &image_key);
819                 if (service_ret != CALENDAR_ERROR_NONE || image_key == NULL || strlen(image_key) <= 0) {
820                         _DEBUG_ERROR("calendar_record_get_str_p is failed(%d)", service_ret);
821                         goto return_part;
822                 }
823
824                 if (strcmp(image_key, CALENDAR_IMAGE_KEYWORD_SNOTE) == 0 || strcmp(image_key, CALENDAR_IMAGE_KEYWORD_PHOTO) == 0) {
825                         _DEBUG_INFO("image is founded. image_key = %s", image_key);
826                         service_ret = calendar_record_get_str(image_record, _calendar_extended_property.value, &image_path);
827                         if (service_ret != CALENDAR_ERROR_NONE || image_path == NULL || strlen(image_path) <= 0) {
828                                 _DEBUG_ERROR("calendar_record_get_str_p is failed(%d)", service_ret);
829                                 image_path = NULL;
830                                 goto return_part;
831                         }
832                         _DEBUG_INFO("image_path = %s", image_path);
833                         continue;
834                 }
835         }
836
837 return_part:
838         _INNER_FUNC_EXIT;
839         return image_path;
840 }
841
842 char * replace_max_rrule_data(char *origin)
843 {
844         _INNER_FUNC_ENTER;
845         retvm_if(origin == NULL, origin, "origin is NULL.");
846
847         char *temp = NULL;
848         char *temp_rrule = NULL;
849         char *str_old_rrule = NULL;
850         char *str_new_rrule = NULL;
851
852         if ((temp = strstr(origin, RRULE_KEYWORD)) == NULL) {
853                 _DEBUG_INFO("this vcalendar hasn't rrule data");
854                 goto return_part;
855         }
856
857         str_old_rrule = get_value_by_token(temp+2, "RRULE:", "\r\n");// '2' means strlen("\r\n")
858         if (str_old_rrule == NULL) {
859                 _DEBUG_ERROR("str_old_rrule is null");
860                 goto return_part;
861         }
862
863         if (strstr(str_old_rrule, MAX_RRULE_DATE_KEYWORD) == NULL) {
864                 _DEBUG_INFO("this vcalendar has rrule data, But it's not until=NONE rrule");
865                 goto return_part;
866         }
867         _DEBUG_INFO("this vcalendar has until=NONE rrule");
868
869         temp_rrule = get_value_by_token(temp, RRULE_KEYWORD, MAX_RRULE_DATE_KEYWORD);
870         if (temp_rrule == NULL) {
871                 _DEBUG_ERROR("temp_rrule is null");
872                 goto return_part;
873         }
874
875         str_new_rrule = (char *)calloc(strlen(temp_rrule)+2, sizeof(char));
876         if (str_new_rrule == NULL) {
877                 _DEBUG_ERROR("str_new_rrule calloc failed");
878                 goto return_part;
879         }
880
881         // every eternal rrule data must have '#0' at the end of string (Calendar-Svc recommend)
882         snprintf(str_new_rrule, strlen(temp_rrule)+2, "%s#0", temp_rrule);
883         origin = replace_string(origin, str_old_rrule, str_new_rrule);
884
885 return_part:
886         if(str_old_rrule) {
887                 free(str_old_rrule);
888                 str_old_rrule = NULL;
889         }
890         if(str_new_rrule) {
891                 free(str_new_rrule);
892                 str_new_rrule = NULL;
893         }
894         _INNER_FUNC_EXIT;
895         _DEBUG_INFO("origin = %s", origin);
896         return origin;
897 }
898
899 char * replace_image_data(char *origin, char *image_path)
900 {
901         _INNER_FUNC_ENTER;
902         retvm_if(origin == NULL, origin, "origin is NULL.");
903         retvm_if(image_path == NULL, origin, "image_path is NULL.");
904
905         int image_path_length = 500;
906         char *str_temp_image_info = NULL;
907         char *str_image_info = NULL;
908         char *str_image_tag = NULL;
909
910         _DEBUG_INFO("origin = %s", origin);
911         _DEBUG_INFO("image_path = %s", image_path);
912
913         if (strstr(image_path, INTERNAL_MEMORY_ORIGIN) != NULL) {
914                 str_temp_image_info = replace_string(image_path, INTERNAL_MEMORY_ORIGIN, INTERNAL_MEMORY_CONVERTED);
915                 str_image_tag = strdup("X-SS-ATTACH-IMAGE");
916         }
917         else if (strstr(image_path, EXTERNAL_MEMORY_ORIGIN) != NULL) {
918                 str_temp_image_info = replace_string(image_path, EXTERNAL_MEMORY_ORIGIN, EXTERNAL_MEMORY_CONVERTED);
919                 str_image_tag = strdup("X-SS-ATTACH-IMAGE");
920         }
921         else if (strstr(image_path, SNOTE_MEMORY_ORIGIN) != NULL) {
922                 str_temp_image_info = strdup(image_path);
923                 //str_image_tag = strdup("X-SS-ATTACH-SNOTE");
924                 str_image_tag = strdup("X-SS-ATTACH-IMAGE");
925         }
926         else {
927                 _DEBUG_ERROR("there is an error. image_path = %s", image_path);
928                 goto return_part;
929         }
930
931         if (strstr(origin, END_VTODO_KEYWORD) == NULL) {
932                 _DEBUG_ERROR("this data is wrong format. origin = %s", origin);
933                 goto return_part;
934         }
935
936         if (image_path_length <= 0) {
937                 _DEBUG_ERROR("image_path_length(%d) <= 0", image_path_length);
938                 goto return_part;
939         }
940         str_image_info = (char *)calloc(image_path_length, sizeof(char));
941         if (str_image_info == NULL) {
942                 _DEBUG_ERROR("str_image_info calloc failed");
943                 goto return_part;
944         }
945
946         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_VTODO_KEYWORD);
947
948         char *new_agent_data = replace_string(origin, END_VTODO_KEYWORD, str_image_info);
949         origin = NULL;
950         origin = new_agent_data;
951
952 return_part:
953
954         if (str_image_info) {
955                 free(str_image_info);
956                 str_image_info = NULL;
957         }
958         if (str_image_tag) {
959                 free(str_image_tag);
960                 str_image_tag = NULL;
961         }
962         if (str_temp_image_info) {
963                 free(str_temp_image_info);
964                 str_temp_image_info = NULL;
965         }
966
967         _INNER_FUNC_EXIT;
968         _DEBUG_INFO("origin = %s", origin);
969         return origin;
970 }
971
972 // SLP Phone = high:2, normal:4 low:8 NONE:1
973 // Kies = high : 1 normal : 2 low : 3
974 char * replace_priority_data(char *origin, bool is_phone_to_kies)
975 {
976         _INNER_FUNC_ENTER;
977         retvm_if(origin == NULL, origin, "origin is NULL.");
978         _DEBUG_INFO("origin = %s", origin);
979
980         if (is_phone_to_kies) {
981                 _DEBUG_INFO("phone to kies converting");
982                 if (strstr(origin, "\r\nPRIORITY:2") != NULL) {
983                         // high priority
984                         origin = replace_string(origin, "\r\nPRIORITY:2", "\r\nPRIORITY:1");
985                 }
986                 else if (strstr(origin, "\r\nPRIORITY:4") != NULL) {
987                         // normal priority
988                         origin = replace_string(origin, "\r\nPRIORITY:4", "\r\nPRIORITY:2");
989                 }
990                 else if (strstr(origin, "\r\nPRIORITY:8") != NULL) {
991                         // low priority
992                         origin = replace_string(origin, "\r\nPRIORITY:8", "\r\nPRIORITY:3");
993                 }
994                 else {
995                         _DEBUG_ERROR("there is an error. origin = %s", origin);
996                         goto return_part;
997                 }
998         }
999         else {
1000                 _DEBUG_INFO("kies to phone converting");
1001                 if (strstr(origin, "\r\nPRIORITY:1") != NULL) {
1002                         // high priority
1003                         origin = replace_string(origin, "\r\nPRIORITY:1", "\r\nPRIORITY:2");
1004                 }
1005                 else if (strstr(origin, "\r\nPRIORITY:2") != NULL) {
1006                         // normal priority
1007                         origin = replace_string(origin, "\r\nPRIORITY:2", "\r\nPRIORITY:4");
1008                 }
1009                 else if (strstr(origin, "\r\nPRIORITY:3") != NULL) {
1010                         // low priority
1011                         origin = replace_string(origin, "\r\nPRIORITY:3", "\r\nPRIORITY:8");
1012                 }
1013                 else {
1014                         _DEBUG_ERROR("there is an error. origin = %s", origin);
1015                         goto return_part;
1016                 }
1017         }
1018
1019 return_part:
1020
1021         _INNER_FUNC_EXIT;
1022         _DEBUG_INFO("origin = %s", origin);
1023         return origin;
1024 }
1025