merge with master
[framework/pim/calendar-service.git] / dft / src / dft_main.c
1 /*
2  * Calendar Service
3  *
4  * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
5  *
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
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  */
19
20 #include <string.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <calendar2.h>
24
25 #include "dft_util.h"
26
27 #define MAX_COLS 32768
28
29 static void __dft_calendar_help(void)
30 {
31         printf("usage: dft-calendar path(string) count(int)\n"
32                         "eg. dft-calendar \"./calendar.txt\" 10\n");
33 }
34
35 // get type depending on file name.
36 static int __dft_get_file_type(char *path)
37 {
38         char *p, *q = NULL;
39         char dl = '/';
40
41         q = p = path;
42         while (*p)
43         {
44                 if (*p == dl)
45                 {
46                         q = p;
47                 }
48                 p++;
49         }
50         if (strstr(q, "event") || strstr(q, "schedule"))
51         {
52                 return CALENDAR_BOOK_TYPE_EVENT;
53         }
54         else if (strstr(q, "todo") || strstr(q, "task"))
55         {
56                 return CALENDAR_BOOK_TYPE_TODO;
57         }
58         else
59         {
60                 printf("set default type\n");
61                 return CALENDAR_BOOK_TYPE_EVENT;
62         }
63         return -1;
64 }
65
66 static int  __dft_event_set_1(calendar_record_h event, int book_id, char *subject, char *location)
67 {
68         int ret;
69
70         ret = calendar_record_set_int(event, _calendar_event.calendar_book_id, book_id);
71         if(CALENDAR_ERROR_NONE != ret)
72         {
73                 printf("failed to set _calendar_event.calendar_book_id\n");
74                 return -1;
75         }
76         ret = calendar_record_set_str(event, _calendar_event.summary, subject);
77         if(CALENDAR_ERROR_NONE != ret)
78         {
79                 printf("failed to set _calendar_event.summary\n");
80                 return -1;
81         }
82         ret = calendar_record_set_str(event, _calendar_event.location, location);
83         if(CALENDAR_ERROR_NONE != ret)
84         {
85                 printf("failed to set _calendar_event.location\n");
86                 return -1;
87         }
88         return 0;
89 }
90
91 static int __dft_event_set_2(calendar_record_h event, char *allday, char *sdate, char *stime, char *edate, char *etime)
92 {
93         int ret;
94         int y, m, d;
95         calendar_time_s st = {0}, et = {0};
96
97         if (!strncmp(allday, "Off", sizeof("Off")))
98         {
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)
103                 {
104                         printf("failed to set _calendar_event.start_tzid\n");
105                         return -1;
106                 }
107                 ret = calendar_record_set_caltime(event, _calendar_event.start_time, st);
108                 if(CALENDAR_ERROR_NONE != ret)
109                 {
110                         printf("failed to set _calendar_event.start_time\n");
111                         return -1;
112                 }
113
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)
118                 {
119                         printf("failed to set _calendar_event.end_tzid\n");
120                         return -1;
121                 }
122                 ret = calendar_record_set_caltime(event, _calendar_event.end_time, et);
123                 if(CALENDAR_ERROR_NONE != ret)
124                 {
125                         printf("failed to set _calendar_event.end_time\n");
126                         return -1;
127                 }
128         } else {
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)
136                 {
137                         printf("failed to set _calendar_event.start_tzid\n");
138                         return -1;
139                 }
140                 ret = calendar_record_set_caltime(event, _calendar_event.start_time, st);
141                 if(CALENDAR_ERROR_NONE != ret)
142                 {
143                         printf("failed to set _calendar_event.start_time\n");
144                         return -1;
145                 }
146
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)
154                 {
155                         printf("failed to set _calendar_event.end_tzid\n");
156                         return -1;
157                 }
158                 ret = calendar_record_set_caltime(event, _calendar_event.end_time, et);
159                 if(CALENDAR_ERROR_NONE != ret)
160                 {
161                         printf("failed to set _calendar_event.end_time\n");
162                         return -1;
163                 }
164         }
165         return 0;
166 }
167
168 static int __dft_event_set_3(calendar_record_h event, int occurence, int status, int sensitivity)
169 {
170         int ret;
171
172         switch (occurence)
173         {
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:
179             break;
180         default:
181                 printf("Out of range, so set CALENDAR_RECURRENCE_NONE\n");
182                 occurence = CALENDAR_RECURRENCE_NONE;
183                 break;
184         }
185         ret = calendar_record_set_int(event, _calendar_event.count, occurence);
186         if(CALENDAR_ERROR_NONE != ret)
187         {
188                 printf("failed to set _calendar_event.count\n");
189                 return -1;
190         }
191         switch (status)
192         {
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:
197                 break;
198         default:
199                 printf("Out of range, so set CALENDAR_EVENT_BUSY_STATUS_FREE\n");
200                 status = CALENDAR_EVENT_BUSY_STATUS_FREE;
201                 break;
202         }
203         ret = calendar_record_set_int(event, _calendar_event.busy_status, status);
204         if(CALENDAR_ERROR_NONE != ret)
205         {
206                 printf("failed to set _calendar_event.busy_status\n");
207                 return -1;
208         }
209
210         switch (sensitivity)
211         {
212         case CALENDAR_SENSITIVITY_PUBLIC:
213         case CALENDAR_SENSITIVITY_PRIVATE:
214         case CALENDAR_SENSITIVITY_CONFIDENTIAL:
215                 break;
216         default:
217                 printf("Out of range, so set CALENDAR_SENSITIVITY_PUBLIC\n");
218                 sensitivity = CALENDAR_SENSITIVITY_PUBLIC;
219                 break;
220         }
221         ret = calendar_record_set_int(event, _calendar_event.sensitivity, sensitivity);
222         if(CALENDAR_ERROR_NONE != ret)
223         {
224                 printf("failed to set _calendar_event.sensitivity\n");
225                 return -1;
226         }
227         return 0;
228 }
229
230 static int  __dft_todo_set_1(calendar_record_h todo, int book_id, char *subject, char *location)
231 {
232         int ret;
233
234         ret = calendar_record_set_int(todo, _calendar_todo.calendar_book_id, book_id);
235         if(CALENDAR_ERROR_NONE != ret)
236         {
237                 printf("failed to set _calendar_todo.calendar_book_id\n");
238                 return -1;
239         }
240         ret = calendar_record_set_str(todo, _calendar_todo.summary, subject);
241         if(CALENDAR_ERROR_NONE != ret)
242         {
243                 printf("failed to set _calendar_todo.summary\n");
244                 return -1;
245         }
246         ret = calendar_record_set_str(todo, _calendar_todo.location, location);
247         if(CALENDAR_ERROR_NONE != ret)
248         {
249                 printf("failed to set _calendar_todo.location\n");
250                 return -1;
251         }
252         return 0;
253 }
254
255 static int __dft_todo_set_2(calendar_record_h todo, char *allday, char *sdate, char *stime, char *edate, char *etime)
256 {
257         int ret;
258         int y, m, d;
259         calendar_time_s st = {0}, et = {0};
260
261         if (!strncmp(allday, "Off", sizeof("Off")))
262         {
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)
267                 {
268                         printf("failed to set _calendar_todo.start_tzid\n");
269                         return -1;
270                 }
271                 ret = calendar_record_set_caltime(todo, _calendar_todo.start_time, st);
272                 if(CALENDAR_ERROR_NONE != ret)
273                 {
274                         printf("failed to set _calendar_todo.start_time\n");
275                         return -1;
276                 }
277
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)
282                 {
283                         printf("failed to set _calendar_todo.due_tzid\n");
284                         return -1;
285                 }
286                 ret = calendar_record_set_caltime(todo, _calendar_todo.due_time, et);
287                 if(CALENDAR_ERROR_NONE != ret)
288                 {
289                         printf("failed to set _calendar_todo.due_time\n");
290                         return -1;
291                 }
292         } else {
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)
300                 {
301                         printf("failed to set _calendar_todo.start_tzid\n");
302                         return -1;
303                 }
304                 ret = calendar_record_set_caltime(todo, _calendar_todo.start_time, st);
305                 if(CALENDAR_ERROR_NONE != ret)
306                 {
307                         printf("failed to set _calendar_todo.start_time\n");
308                         return -1;
309                 }
310
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)
318                 {
319                         printf("failed to set _calendar_todo.due_tzid\n");
320                         return -1;
321                 }
322                 ret = calendar_record_set_caltime(todo, _calendar_todo.due_time, et);
323                 if(CALENDAR_ERROR_NONE != ret)
324                 {
325                         printf("failed to set _calendar_todo.due_time\n");
326                         return -1;
327                 }
328         }
329         return 0;
330 }
331
332 static int __dft_todo_set_3(calendar_record_h todo, int occurence, int status, int sensitivity)
333 {
334         int ret;
335
336         switch (occurence)
337         {
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:
343             break;
344         default:
345                 printf("Out of range, so set CALENDAR_RECURRENCE_NONE\n");
346                 occurence = CALENDAR_RECURRENCE_NONE;
347                 break;
348         }
349         ret = calendar_record_set_int(todo, _calendar_todo.count, occurence);
350         if(CALENDAR_ERROR_NONE != ret)
351         {
352                 printf("failed to set _calendar_todo.count\n");
353                 return -1;
354         }
355
356         switch (status)
357         {
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:
363                 break;
364         default:
365                 printf("Out of range, so set ALENDAR_TODO_STATUS_NEEDS_ACTION\n");
366                 status = CALENDAR_TODO_STATUS_NEEDS_ACTION;
367                 break;
368         }
369         ret = calendar_record_set_int(todo, _calendar_todo.todo_status, status);
370         if(CALENDAR_ERROR_NONE != ret)
371         {
372                 printf("failed to set _calendar_todo.todo_status\n");
373                 return -1;
374         }
375
376         switch (sensitivity)
377         {
378         case CALENDAR_SENSITIVITY_PUBLIC:
379         case CALENDAR_SENSITIVITY_PRIVATE:
380         case CALENDAR_SENSITIVITY_CONFIDENTIAL:
381                 break;
382         default:
383                 printf("Out of range, so set CALENDAR_SENSITIVITY_PUBLIC\n");
384                 sensitivity = CALENDAR_SENSITIVITY_PUBLIC;
385                 break;
386         }
387         ret = calendar_record_set_int(todo, _calendar_todo.sensitivity, sensitivity);
388         if(CALENDAR_ERROR_NONE != ret)
389         {
390                 printf("failed to set _calendar_todo.sensitivity\n");
391                 return -1;
392         }
393         return 0;
394 }
395
396 int main(int argc, char **argv)
397 {
398         FILE *file;
399         int ret;
400         int cnt;
401         int type;
402         char path[256] = {0};
403         char _cnt[16] = {0};
404
405         char subject[MAX_COLS];
406         char location[MAX_COLS];
407         char sdate[16];
408         char stime[16];
409         char edate[16];
410         char etime[16];
411         char allday[16];
412         int occurence;
413         char reminder[16];
414         int reminder_value;
415         int category;
416         int status;
417         int sensitivity;
418         char description[MAX_COLS];
419         calendar_record_h record = NULL;
420
421         printf("argc(%d)\n", argc);
422
423         if (argc != 3) {
424                 printf("Check parameters.\n");
425                 __dft_calendar_help();
426                 return -1;
427         }
428
429         snprintf(path, sizeof(path), "%s", argv[1]);
430         snprintf(_cnt, sizeof(_cnt), "%s", argv[2]);
431         cnt = atoi(_cnt);
432
433         type = __dft_get_file_type(argv[1]);
434         if (type != CALENDAR_BOOK_TYPE_EVENT && type != CALENDAR_BOOK_TYPE_TODO)
435         {
436                 printf("Invalid book type");
437                 return -1;
438         }
439
440         if (cnt <= 0) {
441                 printf("Invalid count(%d)\n", cnt);
442                 return -1;
443         }
444
445         file = fopen(path, "r");
446         if (file == NULL) {
447                 printf("Failed to open file(%s)\n", path);
448                 return -1;
449         }
450
451         ret = calendar_connect();
452         if (CALENDAR_ERROR_NONE != ret)
453         {
454                 printf("calendar_connect() failed\n");
455                 fclose(file);
456                 return -1;
457         }
458
459         while ( !feof(file) && fscanf(file,
460                                 "%32767[^\t]\t %32767[^\t]\t "
461                                 "%s %s %s %s"
462                                 "%s %d "
463                                 "%s %d "
464                                 "%d %d %d "
465                                 "%32767[^\0] ",
466                                 subject, location,
467                                 sdate, stime, edate, etime,
468                                 allday, &occurence,
469                                 reminder, &reminder_value,
470                                 &category, &status, &sensitivity,
471                                 description) > 1) {
472
473                 switch (type)
474                 {
475                 case CALENDAR_BOOK_TYPE_EVENT:
476                         ret = CALENDAR_ERROR_NONE;
477
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);
484
485                         if(CALENDAR_ERROR_NONE != ret)
486                         {
487                                 printf("__dft_work_event() failed\n");
488                                 return -1;
489                         }
490                         break;
491
492                 case CALENDAR_BOOK_TYPE_TODO:
493                         ret = CALENDAR_ERROR_NONE;
494
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);
501
502                         if(CALENDAR_ERROR_NONE != ret)
503                         {
504                                 printf("__dft_work_todo() failed\n");
505                                 return -1;
506                         }
507                         break;
508                 }
509
510                 cnt--;
511                 if (cnt <= 0) {
512                         break;
513                 }
514         }
515
516         fclose(file);
517         ret = calendar_disconnect();
518         if(CALENDAR_ERROR_NONE != ret)
519         {
520                 printf("calendar_disconnect() failed \n");
521                 return -1;
522         }
523         return 0;
524 }