Add the manifest
[profile/ivi/alarm-manager.git] / alarm-manager-registry.c
1 /*
2  *  alarm-manager
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Venkatesha Sarpangala <sarpangala.v@samsung.com>, Jayoun Lee <airjany@samsung.com>,
7  * Sewook Park <sewook7.park@samsung.com>, Jaeho Lee <jaeho81.lee@samsung.com>
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  */
22
23
24
25
26 #include<stdio.h>
27 #include<stdlib.h>
28 #include<time.h>
29 #include<signal.h>
30 #include<string.h>
31 #include<sys/types.h>
32
33 #include<dbus/dbus.h>
34 #include<glib.h>
35 #include <db-util.h>
36 #if !GLIB_CHECK_VERSION (2, 31, 0)
37 #include <glib/gmacros.h>
38 #else
39 #endif
40
41 #include"alarm.h"
42 #include"alarm-internal.h"
43
44 #define SIG_TIMER 0x32
45
46 #define MAX_GCONF_PATH_LEN 256
47 #define MAX_QUERY_LEN 4096
48
49 extern __alarm_server_context_t alarm_context;
50 extern sqlite3 *alarmmgr_db;
51
52 #ifdef __ALARM_BOOT
53 extern bool enable_power_on_alarm;
54 #endif
55
56 bool _save_alarms(__alarm_info_t *__alarm_info);
57 bool _update_alarms(__alarm_info_t *__alarm_info);
58 bool _delete_alarms(alarm_id_t alarm_id);
59 #ifdef __ALARM_BOOT
60 bool _update_power_on(bool on_off);
61 #endif
62 bool _load_alarms_from_registry(void);
63
64 bool _save_alarms(__alarm_info_t *__alarm_info)
65 {
66         char query[MAX_QUERY_LEN] = {0,};
67         char *error_message = NULL;
68         alarm_info_t *alarm_info =
69             (alarm_info_t *) &(__alarm_info->alarm_info);
70         alarm_date_t *start = &alarm_info->start;
71         alarm_mode_t *mode = &alarm_info->mode;
72
73         snprintf(query, MAX_QUERY_LEN, "insert into alarmmgr( alarm_id, start,\
74                         end, pid, app_unique_name, app_service_name, app_service_name_mod, bundle, year,\
75                         month, day, hour, min, sec, day_of_week, repeat,\
76                         alarm_type, reserved_info, dst_service_name, dst_service_name_mod)\
77                         values (%d,%d,%d,%d,'%s','%s','%s','%s',%d,%d,%d,%d,%d,%d,%d,%d,\
78                         %d,%d,'%s','%s')",\
79                         __alarm_info->alarm_id,
80                         (int)__alarm_info->start,
81                         (int)__alarm_info->end,
82                         __alarm_info->pid,
83                         (char *)g_quark_to_string(
84                                 __alarm_info->quark_app_unique_name),
85                         (char *)g_quark_to_string(
86                                 __alarm_info->quark_app_service_name),
87                         (char *)g_quark_to_string(
88                                 __alarm_info->quark_app_service_name_mod),
89                         (char *)g_quark_to_string(
90                                 __alarm_info->quark_bundle),
91                         start->year,
92                         start->month,
93                         start->day,
94                         start->hour,
95                         start->min,
96                         start->sec,
97                         mode->u_interval.day_of_week,
98                         mode->repeat,
99                         alarm_info->alarm_type,
100                         alarm_info->reserved_info,
101                         (char *)g_quark_to_string(
102                         __alarm_info->quark_dst_service_name),
103                         (char *)g_quark_to_string(
104                         __alarm_info->quark_dst_service_name_mod));
105
106         if (SQLITE_OK !=
107             sqlite3_exec(alarmmgr_db, query, NULL, NULL, &error_message)) {
108                 ALARM_MGR_EXCEPTION_PRINT(
109                     "Don't execute query = %s, error message = %s\n", query,
110                      error_message);
111                 return false;
112         }
113
114         return true;
115 }
116
117 bool _update_alarms(__alarm_info_t *__alarm_info)
118 {
119         char query[MAX_QUERY_LEN] = {0,};
120         char *error_message = NULL;
121         alarm_info_t *alarm_info =
122             (alarm_info_t *) &(__alarm_info->alarm_info);
123         alarm_date_t *start = &alarm_info->start;
124         alarm_mode_t *mode = &alarm_info->mode;
125
126         snprintf(query, MAX_QUERY_LEN, "update alarmmgr set start=%d, end=%d,\
127                         pid=%d, app_unique_name='%s', app_service_name='%s', app_service_name_mod='%s',\
128                         bundle='%s', year=%d, month=%d, day=%d, hour=%d, min=%d, sec=%d,\
129                         day_of_week=%d, repeat=%d, alarm_type=%d,\
130                         reserved_info=%d, dst_service_name='%s', dst_service_name_mod='%s'\
131                         where alarm_id=%d",\
132                         (int)__alarm_info->start,
133                         (int)__alarm_info->end,
134                         __alarm_info->pid,
135                         (char *)g_quark_to_string(
136                                 __alarm_info->quark_app_unique_name),
137                         (char *)g_quark_to_string(
138                                 __alarm_info->quark_app_service_name),
139                         (char *)g_quark_to_string(
140                                 __alarm_info->quark_app_service_name_mod),
141                         (char *)g_quark_to_string(
142                                 __alarm_info->quark_bundle),
143                         start->year,
144                         start->month,
145                         start->day,
146                         start->hour,
147                         start->min,
148                         start->sec,
149                         mode->u_interval.day_of_week,
150                         mode->repeat,
151                         alarm_info->alarm_type,
152                         alarm_info->reserved_info,
153                         (char *)g_quark_to_string(
154                                 __alarm_info->quark_dst_service_name),
155                         (char *)g_quark_to_string(
156                                 __alarm_info->quark_dst_service_name_mod),
157                         __alarm_info->alarm_id);
158
159         if (SQLITE_OK !=
160             sqlite3_exec(alarmmgr_db, query, NULL, NULL, &error_message)) {
161                 ALARM_MGR_EXCEPTION_PRINT(
162                     "Don't execute query = %s, error message = %s\n", query,
163                      error_message);
164                 return false;
165         }
166
167         return true;
168
169 }
170
171 bool _delete_alarms(alarm_id_t alarm_id)
172 {
173         char query[MAX_QUERY_LEN] = {0};
174         char *error_message = NULL;
175
176         snprintf(query, MAX_QUERY_LEN, "delete from alarmmgr where alarm_id=%d",
177                  alarm_id);
178
179         if (SQLITE_OK !=
180             sqlite3_exec(alarmmgr_db, query, NULL, NULL, &error_message)) {
181                 ALARM_MGR_EXCEPTION_PRINT(
182                     "Don't execute query = %s, error message = %s\n", query,
183                      error_message);
184                 return false;
185         }
186
187         return true;
188
189 }
190
191 #ifdef __ALARM_BOOT
192 bool _update_power_on(bool on_off)
193 {
194 /*      GConfClient* pGCC;
195         char key[MAX_GCONF_PATH_LEN];
196         GError* error = NULL;
197         
198         g_type_init();
199         pGCC = gconf_client_get_default();
200
201         if(!pGCC)
202         {
203                 ALARM_MGR_EXCEPTION_PRINT(" gconf get failed.. \n");
204                 return false;
205         }
206
207         sprintf(key,"/Services/AlarmMgr/Auto_poweron"); 
208
209         
210         if(!gconf_client_set_bool(pGCC, key, on_off, &error))
211         {
212                 ALARM_MGR_EXCEPTION_PRINT("set string has failed...\n");
213                 return false;
214         }
215
216         gconf_client_suggest_sync(pGCC, NULL);
217         g_object_unref(pGCC);
218 */
219         return false;
220 }
221 #endif
222
223 bool _load_alarms_from_registry()
224 {
225         int i = 0;
226         char query[MAX_QUERY_LEN] = {0,};
227         sqlite3_stmt *stmt = NULL;
228         const char *tail = NULL;
229         alarm_info_t *alarm_info = NULL;
230         __alarm_info_t *__alarm_info = NULL;
231         alarm_date_t *start = NULL;
232         alarm_mode_t *mode = NULL;
233         char app_unique_name[MAX_SERVICE_NAME_LEN] = {0,};
234         char app_service_name[MAX_SERVICE_NAME_LEN] = {0,};
235         char app_service_name_mod[MAX_SERVICE_NAME_LEN] = {0,};
236         char dst_service_name[MAX_SERVICE_NAME_LEN] = {0,};
237         char dst_service_name_mod[MAX_SERVICE_NAME_LEN] = {0,};
238         char bundle[MAX_BUNDLE_NAME_LEN] = {0,};
239
240 #ifdef __ALARM_BOOT
241         /*sprintf(path, "/Services/AlarmMgr/Auto_poweron"); */
242
243         enable_power_on_alarm = 0;
244         /*gconf_client_get_bool(pGCC, path, NULL); */
245 #endif
246
247         snprintf(query, MAX_QUERY_LEN, "select * from alarmmgr");
248
249         if (SQLITE_OK !=
250             sqlite3_prepare(alarmmgr_db, query, strlen(query), &stmt, &tail)) {
251                 ALARM_MGR_EXCEPTION_PRINT("sqlite3_prepare error\n");
252                 return false;
253         }
254
255         for (i = 0; SQLITE_ROW == sqlite3_step(stmt); i++) {
256                 __alarm_info = malloc(sizeof(__alarm_info_t));
257
258                 if (G_UNLIKELY(__alarm_info == NULL)){
259                         ALARM_MGR_EXCEPTION_PRINT("[alarm-server]:Malloc failed\n");
260                         return false;
261                 }
262                 alarm_info = (alarm_info_t *) &(__alarm_info->alarm_info);
263                 start = &alarm_info->start;
264                 mode = &alarm_info->mode;
265
266                 memset(app_service_name, 0, MAX_SERVICE_NAME_LEN);
267                 memset(app_service_name_mod, 0, MAX_SERVICE_NAME_LEN);
268                 memset(dst_service_name, 0, MAX_SERVICE_NAME_LEN);
269                 memset(dst_service_name_mod, 0, MAX_SERVICE_NAME_LEN);
270
271                 __alarm_info->alarm_id = sqlite3_column_int(stmt, 0);
272                 __alarm_info->start = sqlite3_column_int(stmt, 1);
273                 __alarm_info->end = sqlite3_column_int(stmt, 2);
274                 __alarm_info->pid = sqlite3_column_int(stmt, 3);
275                 strncpy(app_unique_name, (const char *)sqlite3_column_text(stmt, 4),
276                         MAX_SERVICE_NAME_LEN - 1);
277                 strncpy(app_service_name, (const char *)sqlite3_column_text(stmt, 5),
278                         MAX_SERVICE_NAME_LEN - 1);
279                 strncpy(app_service_name_mod, (const char *)sqlite3_column_text(stmt, 6),
280                         MAX_SERVICE_NAME_LEN - 1);
281                 strncpy(bundle, (const char *)sqlite3_column_text(stmt, 7),
282                         MAX_BUNDLE_NAME_LEN - 1);               
283                 start->year = sqlite3_column_int(stmt, 8);
284                 start->month = sqlite3_column_int(stmt, 9);
285                 start->day = sqlite3_column_int(stmt, 10);
286                 start->hour = sqlite3_column_int(stmt, 11);
287                 start->min = sqlite3_column_int(stmt, 12);
288                 start->sec = sqlite3_column_int(stmt, 13);
289                 mode->u_interval.day_of_week = sqlite3_column_int(stmt, 14);
290                 mode->repeat = sqlite3_column_int(stmt, 15);
291                 alarm_info->alarm_type = sqlite3_column_int(stmt, 16);
292                 alarm_info->reserved_info = sqlite3_column_int(stmt, 17);
293                 strncpy(dst_service_name, (const char *)sqlite3_column_text(stmt, 18),
294                         MAX_SERVICE_NAME_LEN - 1);
295                 strncpy(dst_service_name_mod, (const char *)sqlite3_column_text(stmt, 19),
296                         MAX_SERVICE_NAME_LEN - 1);
297
298                 __alarm_info->quark_app_unique_name =
299                     g_quark_from_string(app_unique_name);
300                 __alarm_info->quark_app_service_name =
301                     g_quark_from_string(app_service_name);
302                 __alarm_info->quark_app_service_name_mod=
303                     g_quark_from_string(app_service_name_mod);
304                 __alarm_info->quark_dst_service_name =
305                     g_quark_from_string(dst_service_name);
306                 __alarm_info->quark_dst_service_name_mod=
307                     g_quark_from_string(dst_service_name_mod);
308                 __alarm_info->quark_bundle = g_quark_from_string(bundle);
309
310                 _alarm_next_duetime(__alarm_info);
311                 alarm_context.alarms =
312                     g_slist_append(alarm_context.alarms, __alarm_info);
313         }
314
315         if (SQLITE_OK != sqlite3_finalize(stmt)) {
316                 ALARM_MGR_EXCEPTION_PRINT("error : sqlite3_finalize\n");
317                 return false;
318         }
319
320         _alarm_schedule();
321
322         return true;
323 }