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)
174 case CALENDAR_RECURRENCE_NONE:
175 case CALENDAR_RECURRENCE_DAILY:
176 case CALENDAR_RECURRENCE_WEEKLY:
177 case CALENDAR_RECURRENCE_MONTHLY:
178 case CALENDAR_RECURRENCE_YEARLY:
181 printf("Out of range, so set CALENDAR_RECURRENCE_NONE\n");
182 occurence = CALENDAR_RECURRENCE_NONE;
185 ret = calendar_record_set_int(event, _calendar_event.count, occurence);
186 if(CALENDAR_ERROR_NONE != ret)
188 printf("failed to set _calendar_event.count\n");
193 case CALENDAR_EVENT_BUSY_STATUS_FREE:
194 case CALENDAR_EVENT_BUSY_STATUS_BUSY:
195 case CALENDAR_EVENT_BUSY_STATUS_UNAVAILABLE:
196 case CALENDAR_EVENT_BUSY_STATUS_TENTATIVE:
199 printf("Out of range, so set CALENDAR_EVENT_BUSY_STATUS_FREE\n");
200 status = CALENDAR_EVENT_BUSY_STATUS_FREE;
203 ret = calendar_record_set_int(event, _calendar_event.busy_status, status);
204 if(CALENDAR_ERROR_NONE != ret)
206 printf("failed to set _calendar_event.busy_status\n");
212 case CALENDAR_SENSITIVITY_PUBLIC:
213 case CALENDAR_SENSITIVITY_PRIVATE:
214 case CALENDAR_SENSITIVITY_CONFIDENTIAL:
217 printf("Out of range, so set CALENDAR_SENSITIVITY_PUBLIC\n");
218 sensitivity = CALENDAR_SENSITIVITY_PUBLIC;
221 ret = calendar_record_set_int(event, _calendar_event.sensitivity, sensitivity);
222 if(CALENDAR_ERROR_NONE != ret)
224 printf("failed to set _calendar_event.sensitivity\n");
230 static int __dft_todo_set_1(calendar_record_h todo, int book_id, char *subject, char *location)
234 ret = calendar_record_set_int(todo, _calendar_todo.calendar_book_id, book_id);
235 if(CALENDAR_ERROR_NONE != ret)
237 printf("failed to set _calendar_todo.calendar_book_id\n");
240 ret = calendar_record_set_str(todo, _calendar_todo.summary, subject);
241 if(CALENDAR_ERROR_NONE != ret)
243 printf("failed to set _calendar_todo.summary\n");
246 ret = calendar_record_set_str(todo, _calendar_todo.location, location);
247 if(CALENDAR_ERROR_NONE != ret)
249 printf("failed to set _calendar_todo.location\n");
255 static int __dft_todo_set_2(calendar_record_h todo, char *allday, char *sdate, char *stime, char *edate, char *etime)
259 calendar_time_s st = {0}, et = {0};
261 if (!strncmp(allday, "Off", sizeof("Off")))
263 st.type = CALENDAR_TIME_UTIME;
264 st.time.utime = _convert_to_utime(sdate, stime);
265 ret = calendar_record_set_str(todo, _calendar_todo.start_tzid, "Asia/Seoul");
266 if(CALENDAR_ERROR_NONE != ret)
268 printf("failed to set _calendar_todo.start_tzid\n");
271 ret = calendar_record_set_caltime(todo, _calendar_todo.start_time, st);
272 if(CALENDAR_ERROR_NONE != ret)
274 printf("failed to set _calendar_todo.start_time\n");
278 et.type = CALENDAR_TIME_UTIME;
279 et.time.utime = _convert_to_utime(edate, etime);
280 ret = calendar_record_set_str(todo, _calendar_todo.due_tzid, "Asia/Seoul");
281 if(CALENDAR_ERROR_NONE != ret)
283 printf("failed to set _calendar_todo.due_tzid\n");
286 ret = calendar_record_set_caltime(todo, _calendar_todo.due_time, et);
287 if(CALENDAR_ERROR_NONE != ret)
289 printf("failed to set _calendar_todo.due_time\n");
293 _convert_to_datetime(sdate, &y, &m, &d);
294 st.type = CALENDAR_TIME_LOCALTIME;
295 st.time.date.year = y;
296 st.time.date.month = m;
297 st.time.date.mday = d;
298 ret = calendar_record_set_str(todo, _calendar_todo.start_tzid, "Asia/Seoul");
299 if(CALENDAR_ERROR_NONE != ret)
301 printf("failed to set _calendar_todo.start_tzid\n");
304 ret = calendar_record_set_caltime(todo, _calendar_todo.start_time, st);
305 if(CALENDAR_ERROR_NONE != ret)
307 printf("failed to set _calendar_todo.start_time\n");
311 _convert_to_datetime(edate, &y, &m, &d);
312 et.type = CALENDAR_TIME_LOCALTIME;
313 et.time.date.year = y;
314 et.time.date.month = m;
315 et.time.date.mday = d;
316 ret = calendar_record_set_str(todo, _calendar_todo.due_tzid, "Asia/Seoul");
317 if(CALENDAR_ERROR_NONE != ret)
319 printf("failed to set _calendar_todo.due_tzid\n");
322 ret = calendar_record_set_caltime(todo, _calendar_todo.due_time, et);
323 if(CALENDAR_ERROR_NONE != ret)
325 printf("failed to set _calendar_todo.due_time\n");
332 static int __dft_todo_set_3(calendar_record_h todo, int occurence, int status, int sensitivity)
338 case CALENDAR_RECURRENCE_NONE:
339 case CALENDAR_RECURRENCE_DAILY:
340 case CALENDAR_RECURRENCE_WEEKLY:
341 case CALENDAR_RECURRENCE_MONTHLY:
342 case CALENDAR_RECURRENCE_YEARLY:
345 printf("Out of range, so set CALENDAR_RECURRENCE_NONE\n");
346 occurence = CALENDAR_RECURRENCE_NONE;
349 ret = calendar_record_set_int(todo, _calendar_todo.count, occurence);
350 if(CALENDAR_ERROR_NONE != ret)
352 printf("failed to set _calendar_todo.count\n");
358 case CALENDAR_TODO_STATUS_NONE:
359 case CALENDAR_TODO_STATUS_NEEDS_ACTION:
360 case CALENDAR_TODO_STATUS_COMPLETED:
361 case CALENDAR_TODO_STATUS_IN_PROCESS:
362 case CALENDAR_TODO_STATUS_CANCELED:
365 printf("Out of range, so set ALENDAR_TODO_STATUS_NEEDS_ACTION\n");
366 status = CALENDAR_TODO_STATUS_NEEDS_ACTION;
369 ret = calendar_record_set_int(todo, _calendar_todo.todo_status, status);
370 if(CALENDAR_ERROR_NONE != ret)
372 printf("failed to set _calendar_todo.todo_status\n");
378 case CALENDAR_SENSITIVITY_PUBLIC:
379 case CALENDAR_SENSITIVITY_PRIVATE:
380 case CALENDAR_SENSITIVITY_CONFIDENTIAL:
383 printf("Out of range, so set CALENDAR_SENSITIVITY_PUBLIC\n");
384 sensitivity = CALENDAR_SENSITIVITY_PUBLIC;
387 ret = calendar_record_set_int(todo, _calendar_todo.sensitivity, sensitivity);
388 if(CALENDAR_ERROR_NONE != ret)
390 printf("failed to set _calendar_todo.sensitivity\n");
396 int main(int argc, char **argv)
402 char path[256] = {0};
405 char subject[MAX_COLS];
406 char location[MAX_COLS];
418 char description[MAX_COLS];
419 calendar_record_h record = NULL;
421 printf("argc(%d)\n", argc);
424 printf("Check parameters.\n");
425 __dft_calendar_help();
429 snprintf(path, sizeof(path), "%s", argv[1]);
430 snprintf(_cnt, sizeof(_cnt), "%s", argv[2]);
433 type = __dft_get_file_type(argv[1]);
434 if (type != CALENDAR_BOOK_TYPE_EVENT && type != CALENDAR_BOOK_TYPE_TODO)
436 printf("Invalid book type");
441 printf("Invalid count(%d)\n", cnt);
445 file = fopen(path, "r");
447 printf("Failed to open file(%s)\n", path);
451 ret = calendar_connect();
452 if (CALENDAR_ERROR_NONE != ret)
454 printf("calendar_connect() failed\n");
459 while ( !feof(file) && fscanf(file,
460 "%32767[^\t]\t %32767[^\t]\t "
467 sdate, stime, edate, etime,
469 reminder, &reminder_value,
470 &category, &status, &sensitivity,
475 case CALENDAR_BOOK_TYPE_EVENT:
476 ret = CALENDAR_ERROR_NONE;
478 ret |= calendar_record_create(_calendar_event._uri, &record);
479 ret |= __dft_event_set_1(record, 1, subject, location);
480 ret |= __dft_event_set_2(record, allday, sdate, stime, edate, etime);
481 ret |= __dft_event_set_3(record, occurence, status, sensitivity);
482 ret |= calendar_db_insert_record(record, NULL);
483 ret |= calendar_record_destroy(record, true);
485 if(CALENDAR_ERROR_NONE != ret)
487 printf("__dft_work_event() failed\n");
492 case CALENDAR_BOOK_TYPE_TODO:
493 ret = CALENDAR_ERROR_NONE;
495 ret |= calendar_record_create(_calendar_todo._uri, &record);
496 ret |= __dft_todo_set_1(record, 1, subject, location);
497 ret |= __dft_todo_set_2(record, allday, sdate, stime, edate, etime);
498 ret |= __dft_todo_set_3(record, occurence, status, sensitivity);
499 ret |= calendar_db_insert_record(record, NULL);
500 ret |= calendar_record_destroy(record, true);
502 if(CALENDAR_ERROR_NONE != ret)
504 printf("__dft_work_todo() failed\n");
517 ret = calendar_disconnect();
518 if(CALENDAR_ERROR_NONE != ret)
520 printf("calendar_disconnect() failed \n");