5868799ae76daa1805673d48b42cc35bb6a9dc77
[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         ret = calendar_record_set_int(event, _calendar_event.count, occurence);
173         if(CALENDAR_ERROR_NONE != ret)
174         {
175                 printf("failed to set _calendar_event.count\n");
176                 return -1;
177         }
178         ret = calendar_record_set_int(event, _calendar_event.busy_status, status);
179         if(CALENDAR_ERROR_NONE != ret)
180         {
181                 printf("failed to set _calendar_event.busy_status\n");
182                 return -1;
183         }
184         ret = calendar_record_set_int(event, _calendar_event.sensitivity, sensitivity);
185         if(CALENDAR_ERROR_NONE != ret)
186         {
187                 printf("failed to set _calendar_event.sensitivity\n");
188                 return -1;
189         }
190         return 0;
191 }
192
193 static int  __dft_todo_set_1(calendar_record_h todo, int book_id, char *subject, char *location)
194 {
195         int ret;
196
197         ret = calendar_record_set_int(todo, _calendar_todo.calendar_book_id, book_id);
198         if(CALENDAR_ERROR_NONE != ret)
199         {
200                 printf("failed to set _calendar_todo.calendar_book_id\n");
201                 return -1;
202         }
203         ret = calendar_record_set_str(todo, _calendar_todo.summary, subject);
204         if(CALENDAR_ERROR_NONE != ret)
205         {
206                 printf("failed to set _calendar_todo.summary\n");
207                 return -1;
208         }
209         ret = calendar_record_set_str(todo, _calendar_todo.location, location);
210         if(CALENDAR_ERROR_NONE != ret)
211         {
212                 printf("failed to set _calendar_todo.location\n");
213                 return -1;
214         }
215         return 0;
216 }
217
218 static int __dft_todo_set_2(calendar_record_h todo, char *allday, char *sdate, char *stime, char *edate, char *etime)
219 {
220         int ret;
221         int y, m, d;
222         calendar_time_s st = {0}, et = {0};
223
224         if (!strncmp(allday, "Off", sizeof("Off")))
225         {
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)
230                 {
231                         printf("failed to set _calendar_todo.start_tzid\n");
232                         return -1;
233                 }
234                 ret = calendar_record_set_caltime(todo, _calendar_todo.start_time, st);
235                 if(CALENDAR_ERROR_NONE != ret)
236                 {
237                         printf("failed to set _calendar_todo.start_time\n");
238                         return -1;
239                 }
240
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)
245                 {
246                         printf("failed to set _calendar_todo.due_tzid\n");
247                         return -1;
248                 }
249                 ret = calendar_record_set_caltime(todo, _calendar_todo.due_time, et);
250                 if(CALENDAR_ERROR_NONE != ret)
251                 {
252                         printf("failed to set _calendar_todo.due_time\n");
253                         return -1;
254                 }
255         } else {
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)
263                 {
264                         printf("failed to set _calendar_todo.start_tzid\n");
265                         return -1;
266                 }
267                 ret = calendar_record_set_caltime(todo, _calendar_todo.start_time, st);
268                 if(CALENDAR_ERROR_NONE != ret)
269                 {
270                         printf("failed to set _calendar_todo.start_time\n");
271                         return -1;
272                 }
273
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)
281                 {
282                         printf("failed to set _calendar_todo.due_tzid\n");
283                         return -1;
284                 }
285                 ret = calendar_record_set_caltime(todo, _calendar_todo.due_time, et);
286                 if(CALENDAR_ERROR_NONE != ret)
287                 {
288                         printf("failed to set _calendar_todo.due_time\n");
289                         return -1;
290                 }
291         }
292         return 0;
293 }
294
295 static int __dft_todo_set_3(calendar_record_h todo, int occurence, int status, int sensitivity)
296 {
297         int ret;
298
299         ret = calendar_record_set_int(todo, _calendar_todo.count, occurence);
300         if(CALENDAR_ERROR_NONE != ret)
301         {
302                 printf("failed to set _calendar_todo.count\n");
303                 return -1;
304         }
305         ret = calendar_record_set_int(todo, _calendar_todo.todo_status, status);
306         if(CALENDAR_ERROR_NONE != ret)
307         {
308                 printf("failed to set _calendar_todo.todo_status\n");
309                 return -1;
310         }
311         ret = calendar_record_set_int(todo, _calendar_todo.sensitivity, sensitivity);
312         if(CALENDAR_ERROR_NONE != ret)
313         {
314                 printf("failed to set _calendar_todo.sensitivity\n");
315                 return -1;
316         }
317         return 0;
318 }
319
320 int main(int argc, char **argv)
321 {
322         FILE *file;
323         int ret;
324         int cnt;
325         int type;
326         char path[256] = {0};
327         char _cnt[16] = {0};
328
329         char subject[MAX_COLS];
330         char location[MAX_COLS];
331         char sdate[16];
332         char stime[16];
333         char edate[16];
334         char etime[16];
335         char allday[16];
336         int occurence;
337         char reminder[16];
338         int reminder_value;
339         int category;
340         int status;
341         int sensitivity;
342         char description[MAX_COLS];
343         calendar_record_h record = NULL;
344
345         printf("argc(%d)\n", argc);
346
347         if (argc != 3) {
348                 printf("Check parameters.\n");
349                 __dft_calendar_help();
350                 return -1;
351         }
352
353         snprintf(path, sizeof(path), "%s", argv[1]);
354         snprintf(_cnt, sizeof(_cnt), "%s", argv[2]);
355         cnt = atoi(_cnt);
356
357         type = __dft_get_file_type(argv[1]);
358         if (type != CALENDAR_BOOK_TYPE_EVENT && type != CALENDAR_BOOK_TYPE_TODO)
359         {
360                 printf("Invalid book type");
361                 return -1;
362         }
363
364         if (cnt <= 0) {
365                 printf("Invalid count(%d)\n", cnt);
366                 return -1;
367         }
368
369         file = fopen(path, "r");
370         if (file == NULL) {
371                 printf("Failed to open file(%s)\n", path);
372                 return -1;
373         }
374
375         ret = calendar_connect();
376         if (CALENDAR_ERROR_NONE != ret)
377         {
378                 printf("calendar_connect() failed\n");
379                 fclose(file);
380                 return -1;
381         }
382
383         while ( !feof(file) && fscanf(file,
384                                 "%32767[^\t]\t %32767[^\t]\t "
385                                 "%s %s %s %s"
386                                 "%s %d "
387                                 "%s %d "
388                                 "%d %d %d "
389                                 "%32767[^\0] ",
390                                 subject, location,
391                                 sdate, stime, edate, etime,
392                                 allday, &occurence,
393                                 reminder, &reminder_value,
394                                 &category, &status, &sensitivity,
395                                 description) > 1) {
396
397                 switch (type)
398                 {
399                 case CALENDAR_BOOK_TYPE_EVENT:
400                         ret = CALENDAR_ERROR_NONE;
401
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);
408
409                         if(CALENDAR_ERROR_NONE != ret)
410                         {
411                                 printf("__dft_work_event() failed\n");
412                                 return -1;
413                         }
414                         break;
415
416                 case CALENDAR_BOOK_TYPE_TODO:
417                         ret = CALENDAR_ERROR_NONE;
418
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);
425
426                         if(CALENDAR_ERROR_NONE != ret)
427                         {
428                                 printf("__dft_work_todo() failed\n");
429                                 return -1;
430                         }
431                         break;
432                 }
433
434                 cnt--;
435                 if (cnt <= 0) {
436                         break;
437                 }
438         }
439
440         fclose(file);
441         ret = calendar_disconnect();
442         if(CALENDAR_ERROR_NONE != ret)
443         {
444                 printf("calendar_disconnect() failed \n");
445                 return -1;
446         }
447         return 0;
448 }