Tizen 2.1 base
[apps/home/libslp-alarm.git] / test / test_alarmdb.c
1 /*
2 *
3 * Copyright 2012  Samsung Electronics Co., Ltd
4 *
5 * Licensed under the Flora License, Version 1.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://floralicense.org/license/
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 <stdio.h>
19 #include <stdlib.h>
20 #include <getopt.h>
21 #include <time.h>
22 #include <string.h>
23
24 #include "../include/alarm-engine.h"
25
26 void usage(char *name)
27 {
28         fprintf(stderr,
29                 "Usage: %s [-l] [-L author] [-a add] [-d id] [-s id] [-e id] [-v id]\n",
30                 name);
31         fprintf(stderr, "\t -l, --datalist  list alarm data\n");
32         fprintf(stderr, "\t -L, --author    author list alarm data\n");
33         fprintf(stderr, "\t -a, --add       add alarm data\n");
34         fprintf(stderr, "\t -d, --del       delete alarm data\n");
35         fprintf(stderr, "\t -s, --snooze    set alarm enable \n");
36         fprintf(stderr, "\t -e, --enable    set alarm snooze\n");
37         fprintf(stderr, "\t -v, --view      view alarm data\n");
38         exit(EXIT_FAILURE);
39 }
40
41 void prt_data_list(void)
42 {
43         printf("%s is called\n", __func__);
44         struct alarm_data_list *adl = NULL;
45         struct alarm_data_list *t = NULL;
46
47         adl = alarmdb_get_data_list_all();
48         if (!adl) {
49                 printf("Empty\n");
50         }
51         t = adl;
52         while (t) {
53                 printf("[%d] %d| %d| %d| %s| %d| %d| %d| %d|"
54                        "%d| %d| %d| [%d] %d| %d| %d|"
55                        "%d| %s| %d\n",
56                        t->ad.id, t->ad.alarm_mgr_id, t->ad.enable, t->ad.author,
57                        t->ad.name, (int)t->ad.stime, (int)t->ad.etime,
58                        (int)t->ad.sdate, (int)t->ad.edate, t->ad.repeat_once,
59                        t->ad.repeat_every, t->ad.repeat_weekly,
60                        t->ad.snooze_enable, t->ad.snooze_min,
61                        t->ad.snooze_times, t->ad.count, t->ad.type, t->ad.tone,
62                        t->ad.volume);
63                 t = t->next;
64         }
65         alarmdb_free_data_list(adl);
66 }
67
68 void prt_data_list_by_author(char author)
69 {
70         printf("%s is called\n", __func__);
71         struct alarm_data_list *adl = NULL;
72         struct alarm_data_list *t = NULL;
73
74         adl = alarmdb_get_data_list_by_author(author);
75         if (!adl) {
76                 printf("Empty\n");
77         }
78         t = adl;
79         while (t) {
80                 printf("[%d] %d| %d| %d| %s| %d| %d| %d| %d|"
81                        "%d| %d| %d| [%d] %d| %d| %d|"
82                        "%d| %s| %d\n",
83                        t->ad.id, t->ad.alarm_mgr_id, t->ad.enable, t->ad.author,
84                        t->ad.name, (int)t->ad.stime, (int)t->ad.etime,
85                        (int)t->ad.sdate, (int)t->ad.edate, t->ad.repeat_once,
86                        t->ad.repeat_every, t->ad.repeat_weekly,
87                        t->ad.snooze_enable, t->ad.snooze_min,
88                        t->ad.snooze_times, t->ad.count, t->ad.type, t->ad.tone,
89                        t->ad.volume);
90                 t = t->next;
91         }
92         alarmdb_free_data_list(adl);
93 }
94
95 void prt_data(struct alarm_data *ad)
96 {
97         printf("id:     [%d]\n", ad->id);
98         printf("alarm manager id:       [%d]\n", ad->alarm_mgr_id);
99         printf("enable: [%d]\n", ad->enable);
100         printf("author: [%d]\n", ad->author);
101         printf("name:   [%s]\n", ad->name);
102         printf("stime:  [%d]\n", (int)ad->stime);
103         printf("etime:  [%d]\n", (int)ad->etime);
104         printf("sdate:  [%d]\n", (int)ad->sdate);
105         printf("edate:  [%d]\n", (int)ad->edate);
106         printf("repeat_once:    [%d]\n", ad->repeat_once);
107         printf("repeat_every:   [%d]\n", ad->repeat_every);
108         printf("repeat_weekly:  [%d]\n", ad->repeat_weekly);
109         printf("snooze_enable:  [%d]\n", ad->snooze_enable);
110         printf("snooze_min:     [%d]\n", ad->snooze_min);
111         printf("snooze_times:   [%d]\n", ad->snooze_times);
112         printf("count:  [%d]\n", ad->count);
113         printf("type:   [%d]\n", ad->type);
114         printf("tone:   [%s]\n", ad->tone);
115         printf("volume: [%d]\n", ad->volume);
116 }
117
118 void add_data(time_t tm)
119 {
120         struct alarm_data *ad = NULL;
121         ad = alarmdb_create_data();
122         if (!ad) {
123                 return;
124         }
125
126         ad->enable = 1;
127         ad->author = 1;
128         snprintf(ad->name, sizeof(ad->name), "%s", "New name");
129         ad->stime = tm;
130         ad->etime = tm + 100;
131         ad->sdate = 0;
132         ad->edate = 0;
133         ad->repeat_once = 0;
134         ad->repeat_every = 1;
135         ad->repeat_weekly = 0;
136         ad->snooze_enable = 0;
137         ad->snooze_min = 5;
138         ad->snooze_times = 2;
139         ad->count = 0;
140         ad->type = 0;
141         snprintf(ad->tone, sizeof(ad->tone), "%s", "/opt/x1");
142         ad->volume = 4;
143
144         alarmdb_add_data(ad);
145         alarmdb_free_data(ad);
146 }
147
148 void set_snooze(int id)
149 {
150         alarmdb_set_snooze(id, 1);
151 }
152
153 void set_enable(int id)
154 {
155         alarmdb_set_enable(id, 1);
156 }
157
158 void del_data(int id)
159 {
160         alarmdb_del_data(id);
161 }
162
163 void view_data(int id)
164 {
165         struct alarm_data *ad = NULL;
166
167         ad = alarmdb_create_data();
168         if (ad) {
169                 prt_data(ad);
170         } else {
171                 printf("Not found %d data\n", id);
172         }
173         alarmdb_free_data(ad);
174 }
175
176 int main(int argc, char *argv[])
177 {
178         int c, r;
179         // struct alarm_data ad = {0, };
180
181         r = alarmdb_init(NULL);
182         if (r < 0)
183                 exit(EXIT_FAILURE);
184
185         while (1) {
186                 int opt_idx = 0;
187                 static struct option long_options[] = {
188                         {"datalist", no_argument, NULL, 'l'},
189                         {"author", no_argument, NULL, 'L'},
190                         {"add", no_argument, NULL, 'a'},
191                         {"delete", no_argument, NULL, 'd'},
192                         {"enable", no_argument, NULL, 'e'},
193                         {"snooze", no_argument, NULL, 's'},
194                         {"view", no_argument, NULL, 'v'},
195                         {0, 0, 0, 0}
196                 };
197
198                 c = getopt_long(argc, argv, "lL:a:d:e:s:v:c:", long_options,
199                                 &opt_idx);
200                 if (c == -1)
201                         break;
202
203                 switch (c) {
204                 case 'l':
205                         prt_data_list();
206                         break;
207                 case 'L':
208                         prt_data_list_by_author(atoi(optarg));
209                         break;
210                 case 'a':
211                         add_data(atoi(optarg));
212                         break;
213                 case 'd':
214                         del_data(atoi(optarg));
215                         break;
216                 case 'e':
217                         set_enable(atoi(optarg));
218                         break;
219                 case 's':
220                         set_snooze(atoi(optarg));
221                         break;
222                 case 'v':
223                         view_data(atoi(optarg));
224                         break;
225                 default:
226                         usage(argv[0]);
227                         break;
228                 }
229         }
230         if (optind < argc)
231                 usage(argv[0]);
232
233         alarmdb_fini();
234
235         return 0;
236 }