4 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
23 #include <calendar2.h>
27 #define MAX_COLS 32768
29 static void __dft_calendar_help(void)
31 printf("usage: dft-calendar path(string) count(int)\n"
32 "eg. dft-calendar \"./calendar.txt\" 10\n");
35 // get type depending on file name.
36 static int __dft_get_file_type(char *path)
50 if (strstr(q, "event") || strstr(q, "schedule"))
52 return CALENDAR_BOOK_TYPE_EVENT;
54 else if (strstr(q, "todo") || strstr(q, "task"))
56 return CALENDAR_BOOK_TYPE_TODO;
60 printf("set default type\n");
61 return CALENDAR_BOOK_TYPE_EVENT;
66 static int __dft_event_set_1(calendar_record_h event, int book_id, char *subject, char *location)
70 ret = calendar_record_set_int(event, _calendar_event.calendar_book_id, book_id);
71 if(CALENDAR_ERROR_NONE != ret)
73 printf("failed to set _calendar_event.calendar_book_id\n");
76 ret = calendar_record_set_str(event, _calendar_event.summary, subject);
77 if(CALENDAR_ERROR_NONE != ret)
79 printf("failed to set _calendar_event.summary\n");
82 ret = calendar_record_set_str(event, _calendar_event.location, location);
83 if(CALENDAR_ERROR_NONE != ret)
85 printf("failed to set _calendar_event.location\n");
91 static int __dft_event_set_2(calendar_record_h event, char *allday, char *sdate, char *stime, char *edate, char *etime)
95 calendar_time_s st = {0}, et = {0};
97 if (!strncmp(allday, "Off", sizeof("Off")))
99 st.type = CALENDAR_TIME_UTIME;
100 st.time.utime = _convert_to_utime(sdate, stime);
101 ret = calendar_record_set_str(event, _calendar_event.start_tzid, "Asia/Seoul");
102 if(CALENDAR_ERROR_NONE != ret)
104 printf("failed to set _calendar_event.start_tzid\n");
107 ret = calendar_record_set_caltime(event, _calendar_event.start_time, st);
108 if(CALENDAR_ERROR_NONE != ret)
110 printf("failed to set _calendar_event.start_time\n");
114 et.type = CALENDAR_TIME_UTIME;
115 et.time.utime = _convert_to_utime(edate, etime);
116 ret = calendar_record_set_str(event, _calendar_event.end_tzid, "Asia/Seoul");
117 if(CALENDAR_ERROR_NONE != ret)
119 printf("failed to set _calendar_event.end_tzid\n");
122 ret = calendar_record_set_caltime(event, _calendar_event.end_time, et);
123 if(CALENDAR_ERROR_NONE != ret)
125 printf("failed to set _calendar_event.end_time\n");
129 _convert_to_datetime(sdate, &y, &m, &d);
130 st.type = CALENDAR_TIME_LOCALTIME;
131 st.time.date.year = y;
132 st.time.date.month = m;
133 st.time.date.mday = d;
134 ret = calendar_record_set_str(event, _calendar_event.start_tzid, "Asia/Seoul");
135 if(CALENDAR_ERROR_NONE != ret)
137 printf("failed to set _calendar_event.start_tzid\n");
140 ret = calendar_record_set_caltime(event, _calendar_event.start_time, st);
141 if(CALENDAR_ERROR_NONE != ret)
143 printf("failed to set _calendar_event.start_time\n");
147 _convert_to_datetime(edate, &y, &m, &d);
148 et.type = CALENDAR_TIME_LOCALTIME;
149 et.time.date.year = y;
150 et.time.date.month = m;
151 et.time.date.mday = d;
152 ret = calendar_record_set_str(event, _calendar_event.end_tzid, "Asia/Seoul");
153 if(CALENDAR_ERROR_NONE != ret)
155 printf("failed to set _calendar_event.end_tzid\n");
158 ret = calendar_record_set_caltime(event, _calendar_event.end_time, et);
159 if(CALENDAR_ERROR_NONE != ret)
161 printf("failed to set _calendar_event.end_time\n");
168 static int __dft_event_set_3(calendar_record_h event, int occurence, int status, int sensitivity)
172 ret = calendar_record_set_int(event, _calendar_event.count, occurence);
173 if(CALENDAR_ERROR_NONE != ret)
175 printf("failed to set _calendar_event.count\n");
178 ret = calendar_record_set_int(event, _calendar_event.busy_status, status);
179 if(CALENDAR_ERROR_NONE != ret)
181 printf("failed to set _calendar_event.busy_status\n");
184 ret = calendar_record_set_int(event, _calendar_event.sensitivity, sensitivity);
185 if(CALENDAR_ERROR_NONE != ret)
187 printf("failed to set _calendar_event.sensitivity\n");
193 static int __dft_todo_set_1(calendar_record_h todo, int book_id, char *subject, char *location)
197 ret = calendar_record_set_int(todo, _calendar_todo.calendar_book_id, book_id);
198 if(CALENDAR_ERROR_NONE != ret)
200 printf("failed to set _calendar_todo.calendar_book_id\n");
203 ret = calendar_record_set_str(todo, _calendar_todo.summary, subject);
204 if(CALENDAR_ERROR_NONE != ret)
206 printf("failed to set _calendar_todo.summary\n");
209 ret = calendar_record_set_str(todo, _calendar_todo.location, location);
210 if(CALENDAR_ERROR_NONE != ret)
212 printf("failed to set _calendar_todo.location\n");
218 static int __dft_todo_set_2(calendar_record_h todo, char *allday, char *sdate, char *stime, char *edate, char *etime)
222 calendar_time_s st = {0}, et = {0};
224 if (!strncmp(allday, "Off", sizeof("Off")))
226 st.type = CALENDAR_TIME_UTIME;
227 st.time.utime = _convert_to_utime(sdate, stime);
228 ret = calendar_record_set_str(todo, _calendar_todo.start_tzid, "Asia/Seoul");
229 if(CALENDAR_ERROR_NONE != ret)
231 printf("failed to set _calendar_todo.start_tzid\n");
234 ret = calendar_record_set_caltime(todo, _calendar_todo.start_time, st);
235 if(CALENDAR_ERROR_NONE != ret)
237 printf("failed to set _calendar_todo.start_time\n");
241 et.type = CALENDAR_TIME_UTIME;
242 et.time.utime = _convert_to_utime(edate, etime);
243 ret = calendar_record_set_str(todo, _calendar_todo.due_tzid, "Asia/Seoul");
244 if(CALENDAR_ERROR_NONE != ret)
246 printf("failed to set _calendar_todo.due_tzid\n");
249 ret = calendar_record_set_caltime(todo, _calendar_todo.due_time, et);
250 if(CALENDAR_ERROR_NONE != ret)
252 printf("failed to set _calendar_todo.due_time\n");
256 _convert_to_datetime(sdate, &y, &m, &d);
257 st.type = CALENDAR_TIME_LOCALTIME;
258 st.time.date.year = y;
259 st.time.date.month = m;
260 st.time.date.mday = d;
261 ret = calendar_record_set_str(todo, _calendar_todo.start_tzid, "Asia/Seoul");
262 if(CALENDAR_ERROR_NONE != ret)
264 printf("failed to set _calendar_todo.start_tzid\n");
267 ret = calendar_record_set_caltime(todo, _calendar_todo.start_time, st);
268 if(CALENDAR_ERROR_NONE != ret)
270 printf("failed to set _calendar_todo.start_time\n");
274 _convert_to_datetime(edate, &y, &m, &d);
275 et.type = CALENDAR_TIME_LOCALTIME;
276 et.time.date.year = y;
277 et.time.date.month = m;
278 et.time.date.mday = d;
279 ret = calendar_record_set_str(todo, _calendar_todo.due_tzid, "Asia/Seoul");
280 if(CALENDAR_ERROR_NONE != ret)
282 printf("failed to set _calendar_todo.due_tzid\n");
285 ret = calendar_record_set_caltime(todo, _calendar_todo.due_time, et);
286 if(CALENDAR_ERROR_NONE != ret)
288 printf("failed to set _calendar_todo.due_time\n");
295 static int __dft_todo_set_3(calendar_record_h todo, int occurence, int status, int sensitivity)
299 ret = calendar_record_set_int(todo, _calendar_todo.count, occurence);
300 if(CALENDAR_ERROR_NONE != ret)
302 printf("failed to set _calendar_todo.count\n");
305 ret = calendar_record_set_int(todo, _calendar_todo.todo_status, status);
306 if(CALENDAR_ERROR_NONE != ret)
308 printf("failed to set _calendar_todo.todo_status\n");
311 ret = calendar_record_set_int(todo, _calendar_todo.sensitivity, sensitivity);
312 if(CALENDAR_ERROR_NONE != ret)
314 printf("failed to set _calendar_todo.sensitivity\n");
320 int main(int argc, char **argv)
326 char path[256] = {0};
329 char subject[MAX_COLS];
330 char location[MAX_COLS];
342 char description[MAX_COLS];
343 calendar_record_h record = NULL;
345 printf("argc(%d)\n", argc);
348 printf("Check parameters.\n");
349 __dft_calendar_help();
353 snprintf(path, sizeof(path), "%s", argv[1]);
354 snprintf(_cnt, sizeof(_cnt), "%s", argv[2]);
357 type = __dft_get_file_type(argv[1]);
358 if (type != CALENDAR_BOOK_TYPE_EVENT && type != CALENDAR_BOOK_TYPE_TODO)
360 printf("Invalid book type");
365 printf("Invalid count(%d)\n", cnt);
369 file = fopen(path, "r");
371 printf("Failed to open file(%s)\n", path);
375 ret = calendar_connect();
376 if (CALENDAR_ERROR_NONE != ret)
378 printf("calendar_connect() failed\n");
383 while ( !feof(file) && fscanf(file,
384 "%32767[^\t]\t %32767[^\t]\t "
391 sdate, stime, edate, etime,
393 reminder, &reminder_value,
394 &category, &status, &sensitivity,
399 case CALENDAR_BOOK_TYPE_EVENT:
400 ret = CALENDAR_ERROR_NONE;
402 ret |= calendar_record_create(_calendar_event._uri, &record);
403 ret |= __dft_event_set_1(record, 1, subject, location);
404 ret |= __dft_event_set_2(record, allday, sdate, stime, edate, etime);
405 ret |= __dft_event_set_3(record, occurence, status, sensitivity);
406 ret |= calendar_db_insert_record(record, NULL);
407 ret |= calendar_record_destroy(record, true);
409 if(CALENDAR_ERROR_NONE != ret)
411 printf("__dft_work_event() failed\n");
416 case CALENDAR_BOOK_TYPE_TODO:
417 ret = CALENDAR_ERROR_NONE;
419 ret |= calendar_record_create(_calendar_todo._uri, &record);
420 ret |= __dft_todo_set_1(record, 1, subject, location);
421 ret |= __dft_todo_set_2(record, allday, sdate, stime, edate, etime);
422 ret |= __dft_todo_set_3(record, occurence, status, sensitivity);
423 ret |= calendar_db_insert_record(record, NULL);
424 ret |= calendar_record_destroy(record, true);
426 if(CALENDAR_ERROR_NONE != ret)
428 printf("__dft_work_todo() failed\n");
441 ret = calendar_disconnect();
442 if(CALENDAR_ERROR_NONE != ret)
444 printf("calendar_disconnect() failed \n");