sync with private git
[platform/core/api/notification.git] / src / notification_noti.c
1 /*
2  *  libnotification
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Seungtaek Chung <seungtaek.chung@samsung.com>, Mi-Ju Lee <miju52.lee@samsung.com>, Xi Zhichan <zhichan.xi@samsung.com>, Youngsub Ko <ys4610.ko@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdlib.h>
25
26 #include <vconf.h>
27
28 #include <notification.h>
29 #include <notification_db.h>
30 #include <notification_noti.h>
31 #include <notification_debug.h>
32 #include <notification_internal.h>
33
34 static int _notification_noti_bind_query_text(sqlite3_stmt * stmt, const char *name,
35                                          const char *str)
36 {
37         int ret = 0;
38         int index = 0;
39
40         index = sqlite3_bind_parameter_index(stmt, name);
41         if (index == 0) {
42                 NOTIFICATION_ERR("Insert : invalid column name");
43                 return NOTIFICATION_ERROR_FROM_DB;
44         }
45
46         ret =
47             sqlite3_bind_text(stmt, index, NOTIFICATION_CHECK_STR(str), -1,
48                               SQLITE_STATIC);
49         if (ret != SQLITE_OK) {
50                 NOTIFICATION_ERR("Insert text : %s",
51                                  NOTIFICATION_CHECK_STR(str));
52                 return NOTIFICATION_ERROR_FROM_DB;
53         }
54
55         return NOTIFICATION_ERROR_NONE;
56 }
57
58 static int _notification_noti_bind_query_double(sqlite3_stmt * stmt, const char *name,
59                                          double val)
60 {
61         int ret = 0;
62         int index = 0;
63
64         index = sqlite3_bind_parameter_index(stmt, name);
65         if (index == 0) {
66                 NOTIFICATION_ERR("Insert : invalid column name");
67                 return NOTIFICATION_ERROR_FROM_DB;
68         }
69
70         ret = sqlite3_bind_double(stmt, index, val);
71         if (ret != SQLITE_OK) {
72                 NOTIFICATION_ERR("Insert double : %f", val);
73                 return NOTIFICATION_ERROR_FROM_DB;
74         }
75
76         return NOTIFICATION_ERROR_NONE;
77 }
78
79 static int _notification_noti_check_priv_id(notification_h noti, sqlite3 * db)
80 {
81         sqlite3_stmt *stmt = NULL;
82         char query[NOTIFICATION_QUERY_MAX] = { 0, };
83         int ret = NOTIFICATION_ERROR_NONE, result = 0;
84
85         /* Make query to check priv_id exist */
86         snprintf(query, sizeof(query),
87                  "select count(*) from noti_list where caller_pkgname = '%s' and priv_id = %d",
88                  noti->caller_pkgname, noti->priv_id);
89
90         ret = sqlite3_prepare(db, query, strlen(query), &stmt, NULL);
91         if (ret != SQLITE_OK) {
92                 NOTIFICATION_ERR("Get count DB err(%d) : %s", ret,
93                                  sqlite3_errmsg(db));
94                 return NOTIFICATION_ERROR_FROM_DB;
95         }
96
97         ret = sqlite3_step(stmt);
98         if (ret == SQLITE_ROW) {
99                 result = sqlite3_column_int(stmt, 0);
100         } else {
101                 result = 0;
102         }
103
104         sqlite3_finalize(stmt);
105
106         /* If result > 0, there is priv_id in DB */
107         if (result > 0) {
108                 return NOTIFICATION_ERROR_ALREADY_EXIST_ID;
109         }
110
111         return NOTIFICATION_ERROR_NONE;
112 }
113
114 static int _notification_noti_get_priv_id(notification_h noti, sqlite3 * db)
115 {
116         sqlite3_stmt *stmt = NULL;
117         char query[NOTIFICATION_QUERY_MAX] = { 0, };
118         int ret = NOTIFICATION_ERROR_NONE, result = 0;
119
120         /* Make query to get max priv_id */
121         snprintf(query, sizeof(query),
122                  "select max(priv_id) from noti_list where caller_pkgname = '%s'",
123                  noti->caller_pkgname);
124
125         ret = sqlite3_prepare(db, query, strlen(query), &stmt, NULL);
126         if (ret != SQLITE_OK) {
127                 NOTIFICATION_ERR("Get count DB err(%d) : %s", ret,
128                                  sqlite3_errmsg(db));
129                 return NOTIFICATION_ERROR_FROM_DB;
130         }
131
132         ret = sqlite3_step(stmt);
133         if (ret == SQLITE_ROW) {
134                 result = sqlite3_column_int(stmt, 0);
135         } else {
136                 result = 0;
137         }
138
139         sqlite3_finalize(stmt);
140
141         if (result < 0) {
142                 return NOTIFICATION_ERROR_FROM_DB;
143         }
144
145         /* Increase result(max priv_id value) for next priv_id */
146         noti->priv_id = result + 1;
147
148         return NOTIFICATION_ERROR_NONE;
149 }
150
151 static int _notification_noti_get_internal_group_id_by_priv_id(const char *pkgname,
152                                                                int priv_id,
153                                                                sqlite3 * db)
154 {
155         sqlite3_stmt *stmt = NULL;
156         char query[NOTIFICATION_QUERY_MAX] = { 0, };
157         int ret = NOTIFICATION_ERROR_NONE, result = 0;
158
159         snprintf(query, sizeof(query),
160                  "select internal_group_id from noti_list where caller_pkgname = '%s' and priv_id = %d",
161                  pkgname, priv_id);
162
163         ret = sqlite3_prepare(db, query, strlen(query), &stmt, NULL);
164         if (ret != SQLITE_OK) {
165                 NOTIFICATION_ERR("Get count DB err(%d) : %s", ret,
166                                  sqlite3_errmsg(db));
167                 return NOTIFICATION_ERROR_FROM_DB;
168         }
169
170         ret = sqlite3_step(stmt);
171         if (ret == SQLITE_ROW) {
172                 result = sqlite3_column_int(stmt, 0);
173         } else {
174                 result = 0;
175         }
176
177         sqlite3_finalize(stmt);
178
179         return result;
180 }
181
182 static int _notification_noti_get_max_internal_group_id(notification_h noti,
183                                                         sqlite3 * db)
184 {
185         sqlite3_stmt *stmt = NULL;
186         char query[NOTIFICATION_QUERY_MAX] = { 0, };
187         int ret = NOTIFICATION_ERROR_NONE, result = 0;
188
189         /* Get max internal group id */
190         snprintf(query, sizeof(query),
191                  "select max(internal_group_id) from noti_list");
192
193         ret = sqlite3_prepare(db, query, strlen(query), &stmt, NULL);
194         if (ret != SQLITE_OK) {
195                 NOTIFICATION_ERR("Get count DB err(%d) : %s", ret,
196                                  sqlite3_errmsg(db));
197                 return NOTIFICATION_ERROR_FROM_DB;
198         }
199
200         ret = sqlite3_step(stmt);
201         if (ret == SQLITE_ROW) {
202                 result = sqlite3_column_int(stmt, 0);
203         } else {
204                 result = 0;
205         }
206
207         sqlite3_finalize(stmt);
208
209         return result;
210 }
211
212 static int _notification_noti_get_internal_group_id(notification_h noti,
213                                                     sqlite3 * db)
214 {
215         sqlite3_stmt *stmt = NULL;
216         char query[NOTIFICATION_QUERY_MAX] = { 0, };
217         int ret = NOTIFICATION_ERROR_NONE, result = 0;
218         const char *ret_title = NULL;
219         char buf_key[32] = { 0, };
220
221         if (noti->group_id == NOTIFICATION_GROUP_ID_NONE) {
222                 /* If Group ID is NONE Get max internal group ID */
223                 result = _notification_noti_get_max_internal_group_id(noti, db);
224                 if (result < 0) {
225                         return NOTIFICATION_ERROR_FROM_DB;
226                 }
227
228                 /* Internal Group ID is max internal group ID + 1 */
229                 noti->internal_group_id = result + 1;
230
231                 return NOTIFICATION_ERROR_NONE;
232         } else if (noti->group_id == NOTIFICATION_GROUP_ID_DEFAULT) {
233                 /* If Group ID is DEFAULT, Get internal group id if it exist */
234                 if (noti->b_key != NULL) {
235                         snprintf(buf_key, sizeof(buf_key), "%d",
236                                  NOTIFICATION_TEXT_TYPE_TITLE);
237
238                         ret_title = bundle_get_val(noti->b_key, buf_key);
239                 }
240
241                 if (ret_title == NULL && noti->b_text != NULL) {
242                         snprintf(buf_key, sizeof(buf_key), "%d",
243                                  NOTIFICATION_TEXT_TYPE_TITLE);
244
245                         ret_title = bundle_get_val(noti->b_text, buf_key);
246                 }
247
248                 if (ret_title == NULL) {
249                         ret_title = noti->caller_pkgname;
250                 }
251
252                 snprintf(query, sizeof(query),
253                          "select internal_group_id from noti_list where title_key = $title_key and group_id = %d",
254                          NOTIFICATION_GROUP_ID_DEFAULT);
255         } else {
256                 /* If Group ID is > DEFAULT, Get internal group id if it exit */
257                 snprintf(query, sizeof(query),
258                          "select internal_group_id from noti_list where caller_pkgname = '%s' and group_id = %d",
259                          NOTIFICATION_CHECK_STR(noti->caller_pkgname),
260                          noti->group_id);
261         }
262
263         ret = sqlite3_prepare_v2(db, query, -1, &stmt, NULL);
264         if (ret != SQLITE_OK) {
265                 NOTIFICATION_ERR("Select Query : %s", query);
266                 NOTIFICATION_ERR("Select DB error(%d) : %s", ret,
267                                  sqlite3_errmsg(db));
268                 if (stmt) {
269                         sqlite3_finalize(stmt);
270                 }
271                 return NOTIFICATION_ERROR_FROM_DB;
272         }
273
274         /* Bind query */
275         if (ret_title != NULL) {
276                 ret =
277                     _notification_noti_bind_query_text(stmt, "$title_key",
278                                                   NOTIFICATION_CHECK_STR
279                                                   (ret_title));
280                 if (ret != NOTIFICATION_ERROR_NONE) {
281                         NOTIFICATION_ERR("Bind error : %s", sqlite3_errmsg(db));
282                         if (stmt) {
283                                 sqlite3_finalize(stmt);
284                         }
285                         return ret;
286                 }
287         }
288
289         ret = sqlite3_step(stmt);
290         if (ret == SQLITE_ROW) {
291                 result = sqlite3_column_int(stmt, 0);
292         } else {
293                 /* If there is not internal_group_id, create new one */
294                 result = _notification_noti_get_max_internal_group_id(noti, db);
295                 result++;
296         }
297
298         sqlite3_finalize(stmt);
299
300         noti->internal_group_id = result;
301
302         return NOTIFICATION_ERROR_NONE;
303 }
304
305 static int _notification_noti_make_query(notification_h noti, char *query,
306                                          int query_size)
307 {
308         char *args = NULL;
309         char *group_args = NULL;
310         char *b_image_path = NULL;
311         char *b_execute_option = NULL;
312         char *b_service_responding = NULL;
313         char *b_service_single_launch = NULL;
314         char *b_service_multi_launch = NULL;
315         char *b_text = NULL;
316         char *b_key = NULL;
317         char *b_format_args = NULL;
318         int flag_simmode = 0;
319
320         /* Decode bundle to insert DB */
321         if (noti->args) {
322                 bundle_encode(noti->args, (bundle_raw **) & args, NULL);
323         }
324         if (noti->group_args) {
325                 bundle_encode(noti->group_args, (bundle_raw **) & group_args,
326                               NULL);
327         }
328
329         if (noti->b_execute_option) {
330                 bundle_encode(noti->b_execute_option,
331                               (bundle_raw **) & b_execute_option, NULL);
332         }
333         if (noti->b_service_responding) {
334                 bundle_encode(noti->b_service_responding,
335                               (bundle_raw **) & b_service_responding, NULL);
336         }
337         if (noti->b_service_single_launch) {
338                 bundle_encode(noti->b_service_single_launch,
339                               (bundle_raw **) & b_service_single_launch, NULL);
340         }
341         if (noti->b_service_multi_launch) {
342                 bundle_encode(noti->b_service_multi_launch,
343                               (bundle_raw **) & b_service_multi_launch, NULL);
344         }
345
346         if (noti->b_text) {
347                 bundle_encode(noti->b_text, (bundle_raw **) & b_text, NULL);
348         }
349         if (noti->b_key) {
350                 bundle_encode(noti->b_key, (bundle_raw **) & b_key, NULL);
351         }
352         if (noti->b_format_args) {
353                 bundle_encode(noti->b_format_args,
354                               (bundle_raw **) & b_format_args, NULL);
355         }
356
357         if (noti->b_image_path) {
358                 bundle_encode(noti->b_image_path,
359                               (bundle_raw **) & b_image_path, NULL);
360         }
361
362         /* Check only simmode property is enable */
363         if (noti->flags_for_property & NOTIFICATION_PROP_DISPLAY_ONLY_SIMMODE) {
364                 flag_simmode = 1;
365         }
366
367         /* Make query */
368         snprintf(query, query_size, "insert into noti_list ("
369                  "type, "
370                  "layout, "
371                  "caller_pkgname, launch_pkgname, "
372                  "image_path, "
373                  "group_id, internal_group_id, priv_id, "
374                  "title_key, "
375                  "b_text, b_key, b_format_args, num_format_args, "
376                  "text_domain, text_dir, "
377                  "time, insert_time, "
378                  "args, group_args, "
379                  "b_execute_option, "
380                  "b_service_responding, b_service_single_launch, b_service_multi_launch, "
381                  "sound_type, sound_path, vibration_type, vibration_path, led_operation, led_argb, led_on_ms, led_off_ms, "
382                  "flags_for_property, flag_simmode, display_applist, "
383                  "progress_size, progress_percentage) values ("
384                  "%d, "
385                  "%d, "
386                  "'%s', '%s', "
387                  "'%s', "
388                  "%d, %d, %d, "
389                  "$title_key, "
390                  "'%s', '%s', '%s', %d, "
391                  "'%s', '%s', "
392                  "%d, %d, "
393                  "'%s', '%s', "
394                  "'%s', "
395                  "'%s', '%s', '%s', "
396                  "%d, '%s', %d, '%s', %d, %d, %d, %d,"
397                  "%d, %d, %d, "
398                  "$progress_size, $progress_percentage)",
399                  noti->type,
400                  noti->layout,
401                  NOTIFICATION_CHECK_STR(noti->caller_pkgname),
402                  NOTIFICATION_CHECK_STR(noti->launch_pkgname),
403                  NOTIFICATION_CHECK_STR(b_image_path), noti->group_id,
404                  noti->internal_group_id, noti->priv_id,
405                  NOTIFICATION_CHECK_STR(b_text), NOTIFICATION_CHECK_STR(b_key),
406                  NOTIFICATION_CHECK_STR(b_format_args), noti->num_format_args,
407                  NOTIFICATION_CHECK_STR(noti->domain),
408                  NOTIFICATION_CHECK_STR(noti->dir), (int)noti->time,
409                  (int)noti->insert_time, NOTIFICATION_CHECK_STR(args),
410                  NOTIFICATION_CHECK_STR(group_args),
411                  NOTIFICATION_CHECK_STR(b_execute_option),
412                  NOTIFICATION_CHECK_STR(b_service_responding),
413                  NOTIFICATION_CHECK_STR(b_service_single_launch),
414                  NOTIFICATION_CHECK_STR(b_service_multi_launch),
415                  noti->sound_type, NOTIFICATION_CHECK_STR(noti->sound_path),
416                  noti->vibration_type,
417                  NOTIFICATION_CHECK_STR(noti->vibration_path),
418                  noti->led_operation,
419                  noti->led_argb,
420                  noti->led_on_ms,
421                  noti->led_off_ms,
422                  noti->flags_for_property, flag_simmode, noti->display_applist);
423
424         /* Free decoded data */
425         if (args) {
426                 free(args);
427         }
428         if (group_args) {
429                 free(group_args);
430         }
431
432         if (b_execute_option) {
433                 free(b_execute_option);
434         }
435         if (b_service_responding) {
436                 free(b_service_responding);
437         }
438         if (b_service_single_launch) {
439                 free(b_service_single_launch);
440         }
441         if (b_service_multi_launch) {
442                 free(b_service_multi_launch);
443         }
444
445         if (b_text) {
446                 free(b_text);
447         }
448         if (b_key) {
449                 free(b_key);
450         }
451         if (b_format_args) {
452                 free(b_format_args);
453         }
454
455         if (b_image_path) {
456                 free(b_image_path);
457         }
458
459         return NOTIFICATION_ERROR_NONE;
460 }
461
462
463 static int _notification_noti_make_update_query(notification_h noti, char *query,
464                                          int query_size)
465 {
466         char *args = NULL;
467         char *group_args = NULL;
468         char *b_image_path = NULL;
469         char *b_execute_option = NULL;
470         char *b_service_responding = NULL;
471         char *b_service_single_launch = NULL;
472         char *b_service_multi_launch = NULL;
473         char *b_text = NULL;
474         char *b_key = NULL;
475         char *b_format_args = NULL;
476         int flag_simmode = 0;
477
478         /* Decode bundle to update DB */
479         if (noti->args) {
480                 bundle_encode(noti->args, (bundle_raw **) & args, NULL);
481         }
482         if (noti->group_args) {
483                 bundle_encode(noti->group_args, (bundle_raw **) & group_args,
484                               NULL);
485         }
486
487         if (noti->b_execute_option) {
488                 bundle_encode(noti->b_execute_option,
489                               (bundle_raw **) & b_execute_option, NULL);
490         }
491         if (noti->b_service_responding) {
492                 bundle_encode(noti->b_service_responding,
493                               (bundle_raw **) & b_service_responding, NULL);
494         }
495         if (noti->b_service_single_launch) {
496                 bundle_encode(noti->b_service_single_launch,
497                               (bundle_raw **) & b_service_single_launch, NULL);
498         }
499         if (noti->b_service_multi_launch) {
500                 bundle_encode(noti->b_service_multi_launch,
501                               (bundle_raw **) & b_service_multi_launch, NULL);
502         }
503
504         if (noti->b_text) {
505                 bundle_encode(noti->b_text, (bundle_raw **) & b_text, NULL);
506         }
507         if (noti->b_key) {
508                 bundle_encode(noti->b_key, (bundle_raw **) & b_key, NULL);
509         }
510         if (noti->b_format_args) {
511                 bundle_encode(noti->b_format_args,
512                               (bundle_raw **) & b_format_args, NULL);
513         }
514
515         if (noti->b_image_path) {
516                 bundle_encode(noti->b_image_path,
517                               (bundle_raw **) & b_image_path, NULL);
518         }
519
520         /* Check only simmode property is enable */
521         if (noti->flags_for_property & NOTIFICATION_PROP_DISPLAY_ONLY_SIMMODE) {
522                 flag_simmode = 1;
523         }
524
525         /* Make query */
526         snprintf(query, query_size, "update noti_list set "
527                  "type = %d, "
528                  "layout = %d, "
529                  "launch_pkgname = '%s', "
530                  "image_path = '%s', "
531                  "b_text = '%s', b_key = '%s', "
532                  "b_format_args = '%s', num_format_args = %d, "
533                  "text_domain = '%s', text_dir = '%s', "
534                  "time = %d, insert_time = %d, "
535                  "args = '%s', group_args = '%s', "
536                  "b_execute_option = '%s', "
537                  "b_service_responding = '%s', "
538                  "b_service_single_launch = '%s', "
539                  "b_service_multi_launch = '%s', "
540                  "sound_type = %d, sound_path = '%s', "
541                  "vibration_type = %d, vibration_path = '%s', "
542                  "led_operation = %d, led_argb = %d, "
543                  "led_on_ms = %d, led_off_ms = %d, "
544                  "flags_for_property = %d, flag_simmode = %d, "
545                  "display_applist = %d, "
546                  "progress_size = $progress_size, progress_percentage = $progress_percentage "
547                  "where priv_id = %d ",
548                  noti->type,
549                  noti->layout,
550                  NOTIFICATION_CHECK_STR(noti->launch_pkgname),
551                  NOTIFICATION_CHECK_STR(b_image_path),
552                  NOTIFICATION_CHECK_STR(b_text), NOTIFICATION_CHECK_STR(b_key),
553                  NOTIFICATION_CHECK_STR(b_format_args), noti->num_format_args,
554                  NOTIFICATION_CHECK_STR(noti->domain),
555                  NOTIFICATION_CHECK_STR(noti->dir),
556                  (int)noti->time, (int)noti->insert_time,
557                  NOTIFICATION_CHECK_STR(args), NOTIFICATION_CHECK_STR(group_args),
558                  NOTIFICATION_CHECK_STR(b_execute_option),
559                  NOTIFICATION_CHECK_STR(b_service_responding),
560                  NOTIFICATION_CHECK_STR(b_service_single_launch),
561                  NOTIFICATION_CHECK_STR(b_service_multi_launch),
562                  noti->sound_type, NOTIFICATION_CHECK_STR(noti->sound_path),
563                  noti->vibration_type,
564                  NOTIFICATION_CHECK_STR(noti->vibration_path),
565                  noti->led_operation,
566                  noti->led_argb,
567                  noti->led_on_ms,
568                  noti->led_off_ms,
569                  noti->flags_for_property, flag_simmode, noti->display_applist,
570                  noti->priv_id);
571
572         /* Free decoded data */
573         if (args) {
574                 free(args);
575         }
576         if (group_args) {
577                 free(group_args);
578         }
579
580         if (b_execute_option) {
581                 free(b_execute_option);
582         }
583         if (b_service_responding) {
584                 free(b_service_responding);
585         }
586         if (b_service_single_launch) {
587                 free(b_service_single_launch);
588         }
589         if (b_service_multi_launch) {
590                 free(b_service_multi_launch);
591         }
592
593         if (b_text) {
594                 free(b_text);
595         }
596         if (b_key) {
597                 free(b_key);
598         }
599         if (b_format_args) {
600                 free(b_format_args);
601         }
602
603         if (b_image_path) {
604                 free(b_image_path);
605         }
606
607         return NOTIFICATION_ERROR_NONE;
608 }
609
610 static void _notification_noti_populate_from_stmt(sqlite3_stmt * stmt, notification_h noti) {
611         int col = 0;
612
613         if (stmt == NULL || noti == NULL) {
614                 return ;
615         }
616
617         noti->type = sqlite3_column_int(stmt, col++);
618         noti->layout = sqlite3_column_int(stmt, col++);
619         noti->caller_pkgname = notification_db_column_text(stmt, col++);
620         noti->launch_pkgname = notification_db_column_text(stmt, col++);
621         noti->b_image_path = notification_db_column_bundle(stmt, col++);
622         noti->group_id = sqlite3_column_int(stmt, col++);
623         noti->internal_group_id = 0;
624         noti->priv_id = sqlite3_column_int(stmt, col++);
625
626         noti->b_text = notification_db_column_bundle(stmt, col++);
627         noti->b_key = notification_db_column_bundle(stmt, col++);
628         noti->b_format_args = notification_db_column_bundle(stmt, col++);
629         noti->num_format_args = sqlite3_column_int(stmt, col++);
630
631         noti->domain = notification_db_column_text(stmt, col++);
632         noti->dir = notification_db_column_text(stmt, col++);
633         noti->time = sqlite3_column_int(stmt, col++);
634         noti->insert_time = sqlite3_column_int(stmt, col++);
635         noti->args = notification_db_column_bundle(stmt, col++);
636         noti->group_args = notification_db_column_bundle(stmt, col++);
637
638         noti->b_execute_option = notification_db_column_bundle(stmt, col++);
639         noti->b_service_responding = notification_db_column_bundle(stmt, col++);
640         noti->b_service_single_launch =
641             notification_db_column_bundle(stmt, col++);
642         noti->b_service_multi_launch =
643             notification_db_column_bundle(stmt, col++);
644
645         noti->sound_type = sqlite3_column_int(stmt, col++);
646         noti->sound_path = notification_db_column_text(stmt, col++);
647         noti->vibration_type = sqlite3_column_int(stmt, col++);
648         noti->vibration_path = notification_db_column_text(stmt, col++);
649         noti->led_operation = sqlite3_column_int(stmt, col++);
650         noti->led_argb = sqlite3_column_int(stmt, col++);
651         noti->led_on_ms = sqlite3_column_int(stmt, col++);
652         noti->led_off_ms = sqlite3_column_int(stmt, col++);
653
654         noti->flags_for_property = sqlite3_column_int(stmt, col++);
655         noti->display_applist = sqlite3_column_int(stmt, col++);
656         noti->progress_size = sqlite3_column_double(stmt, col++);
657         noti->progress_percentage = sqlite3_column_double(stmt, col++);
658
659         noti->app_icon_path = NULL;
660         noti->app_name = NULL;
661         noti->temp_title = NULL;
662         noti->temp_content = NULL;
663 }
664
665 static notification_h _notification_noti_get_item(sqlite3_stmt * stmt)
666 {
667         notification_h noti = NULL;
668
669         noti = malloc(sizeof(struct _notification));
670         if (noti == NULL) {
671                 return NULL;
672         }
673
674         _notification_noti_populate_from_stmt(stmt, noti);
675
676         return noti;
677 }
678
679 int notification_noti_set_tag(const char *tag, char *value, char *buf, int buf_len)
680 {
681         int len_total = 0;
682
683         len_total += (strlen(tag) * 2) + 5 + strlen(value) + 1;
684
685         if (buf_len <= len_total)
686                 return NOTIFICATION_ERROR_INVALID_DATA;
687
688         snprintf(buf, buf_len, "<%s>%s</%s>", tag, value, tag);
689
690         return NOTIFICATION_ERROR_NONE;
691 }
692
693 char *notification_noti_strip_tag(const char *tagged_str)
694 {
695         if (tagged_str == NULL)
696                 return NULL;
697
698         int len_total = strlen(tagged_str);
699
700         if (len_total == 0)
701                 return NULL;
702
703         char *b_f_e = strstr(tagged_str, ">");
704         char *b_e_s = strstr(tagged_str, "</");
705
706         if (b_f_e == NULL || b_e_s == NULL || (b_e_s - b_f_e - 1) <= 0)
707                 return NULL;
708
709         return strndup(b_f_e + 1, b_e_s - b_f_e - 1);
710 }
711
712 int notification_noti_get_tag_type(const char *tagged_str)
713 {
714         if (tagged_str == NULL)
715                 return TAG_TYPE_INVALID;
716
717         if (strlen(tagged_str)== 0)
718                 return TAG_TYPE_INVALID;
719
720         char *b_f_s = strstr(tagged_str, "<");
721         char *b_f_e = strstr(tagged_str, ">");
722
723         if (b_f_s == NULL || b_f_e == NULL || (b_f_e - b_f_s - 1) <= 0)
724                 return TAG_TYPE_INVALID;
725
726         char *start = b_f_s + 1;
727         int len_tag = b_f_e - b_f_s - 1;
728
729         if (strncmp(start,TAG_TIME,len_tag) == 0) {
730                 return TAG_TYPE_TIME;
731         }
732
733         return TAG_TYPE_INVALID;
734 }
735
736 static int _notification_noti_update_priv_id(sqlite3 * db, int rowid)
737 {
738         char query[128] = { 0, };
739
740         if (db == NULL) {
741                 return NOTIFICATION_ERROR_INVALID_DATA;
742         }
743
744         snprintf(query, sizeof(query), "update noti_list set "
745                         "priv_id = %d, internal_group_id = %d where rowid = %d",
746                         rowid, rowid, rowid);
747
748         return notification_db_exec(db, query, NULL);
749 }
750
751 EXPORT_API int notification_noti_insert(notification_h noti)
752 {
753         sqlite3 *db = NULL;
754         sqlite3_stmt *stmt = NULL;
755         char query[NOTIFICATION_QUERY_MAX] = { 0, };
756         int ret = 0;
757         char buf_key[32] = { 0, };
758         const char *title_key = NULL;
759
760         /* Open DB */
761         db = notification_db_open(DBPATH);
762         if (!db) {
763                 return NOTIFICATION_ERROR_FROM_DB;
764         }
765
766         /* Initialize private ID */
767         noti->priv_id = NOTIFICATION_PRIV_ID_NONE;
768         noti->group_id = NOTIFICATION_GROUP_ID_NONE;
769         noti->internal_group_id = NOTIFICATION_GROUP_ID_NONE;
770
771         /* make query */
772         ret = _notification_noti_make_query(noti, query, sizeof(query));
773         if (ret != NOTIFICATION_ERROR_NONE) {
774                 goto err;
775         }
776
777         ret = sqlite3_prepare_v2(db, query, -1, &stmt, NULL);
778         if (ret != SQLITE_OK) {
779                 NOTIFICATION_ERR("Insert Query : %s", query);
780                 NOTIFICATION_ERR("Insert DB error(%d) : %s", ret,
781                                  sqlite3_errmsg(db));
782                 ret = NOTIFICATION_ERROR_FROM_DB;
783                 goto err;
784         }
785
786         /* Get title key */
787         if (noti->b_key != NULL) {
788                 snprintf(buf_key, sizeof(buf_key), "%d",
789                          NOTIFICATION_TEXT_TYPE_TITLE);
790
791                 title_key = bundle_get_val(noti->b_key, buf_key);
792         }
793
794         if (title_key == NULL && noti->b_text != NULL) {
795                 snprintf(buf_key, sizeof(buf_key), "%d",
796                          NOTIFICATION_TEXT_TYPE_TITLE);
797
798                 title_key = bundle_get_val(noti->b_text, buf_key);
799         }
800
801         if (title_key == NULL) {
802                 title_key = noti->caller_pkgname;
803         }
804
805         /* Bind query */
806         ret = _notification_noti_bind_query_text(stmt, "$title_key", title_key);
807         if (ret != NOTIFICATION_ERROR_NONE) {
808                 NOTIFICATION_ERR("Bind error : %s", sqlite3_errmsg(db));
809                 goto err;
810         }
811         ret = _notification_noti_bind_query_double(stmt, "$progress_size",noti->progress_size);
812         if (ret != NOTIFICATION_ERROR_NONE) {
813                 NOTIFICATION_ERR("Bind error : %s", sqlite3_errmsg(db));
814                 if (stmt) {
815                         sqlite3_finalize(stmt);
816                 }
817                 return ret;
818         }
819         ret = _notification_noti_bind_query_double(stmt, "$progress_percentage",noti->progress_percentage);
820         if (ret != NOTIFICATION_ERROR_NONE) {
821                 NOTIFICATION_ERR("Bind error : %s", sqlite3_errmsg(db));
822                 if (stmt) {
823                         sqlite3_finalize(stmt);
824                 }
825                 return ret;
826         }
827
828         ret = sqlite3_step(stmt);
829         if (ret == SQLITE_OK || ret == SQLITE_DONE) {
830                 noti->priv_id = (int)sqlite3_last_insert_rowid(db);
831                 if (_notification_noti_update_priv_id(db, noti->priv_id) == 0) {
832                         ret = NOTIFICATION_ERROR_NONE;
833                 } else {
834                         ret = NOTIFICATION_ERROR_FROM_DB;
835                 }
836         } else {
837                 ret = NOTIFICATION_ERROR_FROM_DB;
838         }
839 err:
840         if (stmt) {
841                 sqlite3_finalize(stmt);
842         }
843
844         /* Close DB */
845         if (db) {
846                 notification_db_close(&db);
847         }
848
849         return ret;
850 }
851
852 int notification_noti_get_by_priv_id(notification_h noti, char *pkgname, int priv_id)
853 {
854         sqlite3 *db = NULL;
855         sqlite3_stmt *stmt = NULL;
856         char query[NOTIFICATION_QUERY_MAX] = { 0, };
857         int ret = 0;
858
859         if (priv_id < 0 || noti == NULL) {
860                 ret = NOTIFICATION_ERROR_INVALID_DATA;
861                 goto err;
862         }
863
864         /* Open DB */
865         db = notification_db_open(DBPATH);
866         if (!db) {
867                 return NOTIFICATION_ERROR_FROM_DB;
868         }
869
870         char *base_query = "select "
871                          "type, layout, caller_pkgname, launch_pkgname, image_path, group_id, priv_id, "
872                          "b_text, b_key, b_format_args, num_format_args, "
873                          "text_domain, text_dir, time, insert_time, args, group_args, "
874                          "b_execute_option, b_service_responding, b_service_single_launch, b_service_multi_launch, "
875                          "sound_type, sound_path, vibration_type, vibration_path, led_operation, led_argb, led_on_ms, led_off_ms, "
876                          "flags_for_property, display_applist, progress_size, progress_percentage "
877                          "from noti_list ";
878
879         if (pkgname != NULL) {
880                 snprintf(query, sizeof(query), "%s where caller_pkgname = '%s' and priv_id = %d",
881                                 base_query ,pkgname, priv_id);
882         } else {
883                 snprintf(query, sizeof(query), "%s where priv_id = %d", base_query,  priv_id);
884         }
885
886         ret = sqlite3_prepare_v2(db, query, -1, &stmt, NULL);
887         if (ret != SQLITE_OK) {
888                 NOTIFICATION_ERR("select Query : %s", query);
889                 NOTIFICATION_ERR("select DB error(%d) : %s", ret,
890                                  sqlite3_errmsg(db));
891                 ret = NOTIFICATION_ERROR_FROM_DB;
892                 goto err;
893         }
894
895         ret = sqlite3_step(stmt);
896         if (ret == SQLITE_ROW) {
897                 _notification_noti_populate_from_stmt(stmt, noti);
898                 ret = NOTIFICATION_ERROR_NONE;
899         } else {
900                 ret = NOTIFICATION_ERROR_FROM_DB;
901         }
902 err:
903         if (stmt) {
904                 sqlite3_finalize(stmt);
905         }
906
907         /* Close DB */
908         if (db != NULL) {
909                 notification_db_close(&db);
910         }
911
912         return ret;
913 }
914
915 EXPORT_API int notification_noti_update(notification_h noti)
916 {
917         sqlite3 *db;
918         sqlite3_stmt *stmt = NULL;
919         char query[NOTIFICATION_QUERY_MAX] = { 0, };
920         int ret = 0;
921
922         /* Open DB */
923         db = notification_db_open(DBPATH);
924         if (!db) {
925                 return NOTIFICATION_ERROR_FROM_DB;
926         }
927
928         /* Check private ID is exist */
929         ret = _notification_noti_check_priv_id(noti, db);
930         if (ret != NOTIFICATION_ERROR_ALREADY_EXIST_ID) {
931                 ret = NOTIFICATION_ERROR_NOT_EXIST_ID;
932                 goto err;
933         }
934
935         /* make update query */
936         ret = _notification_noti_make_update_query(noti, query, sizeof(query));
937         if (ret != NOTIFICATION_ERROR_NONE) {
938                 goto err;
939         }
940
941         ret = sqlite3_prepare_v2(db, query, -1, &stmt, NULL);
942         if (ret != SQLITE_OK) {
943                 NOTIFICATION_ERR("Insert Query : %s", query);
944                 NOTIFICATION_ERR("Insert DB error(%d) : %s", ret,
945                                  sqlite3_errmsg(db));
946                 ret = NOTIFICATION_ERROR_FROM_DB;
947                 goto err;
948         }
949
950         ret = _notification_noti_bind_query_double(stmt, "$progress_size",noti->progress_size);
951         if (ret != NOTIFICATION_ERROR_NONE) {
952                 NOTIFICATION_ERR("Bind error : %s", sqlite3_errmsg(db));
953                 goto err;
954         }
955         ret = _notification_noti_bind_query_double(stmt, "$progress_percentage",noti->progress_percentage);
956         if (ret != NOTIFICATION_ERROR_NONE) {
957                 NOTIFICATION_ERR("Bind error : %s", sqlite3_errmsg(db));
958                 goto err;
959         }
960
961         ret = sqlite3_step(stmt);
962         if (ret == SQLITE_OK || ret == SQLITE_DONE) {
963                 ret = NOTIFICATION_ERROR_NONE;
964         } else {
965                 ret = NOTIFICATION_ERROR_FROM_DB;
966         }
967 err:
968         if (stmt) {
969                 sqlite3_finalize(stmt);
970         }
971
972         /* Close DB */
973         if (db) {
974                 notification_db_close(&db);
975         }
976
977         return ret;
978 }
979
980 EXPORT_API int notification_noti_delete_all(notification_type_e type, const char *pkgname, int *num_deleted, int **list_deleted_rowid)
981 {
982         int ret = NOTIFICATION_ERROR_NONE;
983         int i = 0, data_cnt = 0;
984         sqlite3 *db = NULL;
985         sqlite3_stmt *stmt = NULL;
986         char buf[128] = { 0, };
987         char query[NOTIFICATION_QUERY_MAX] = { 0, };
988         char query_base[NOTIFICATION_QUERY_MAX] = { 0, };
989         char query_where[NOTIFICATION_QUERY_MAX] = { 0, };
990
991         /* Open DB */
992         db = notification_db_open(DBPATH);
993         if (!db) {
994                 return NOTIFICATION_ERROR_FROM_DB;
995         }
996
997         if (pkgname == NULL) {
998                 if (type != NOTIFICATION_TYPE_NONE) {
999                         snprintf(query_where, sizeof(query_where),
1000                                  "where type = %d ", type);
1001                 }
1002         } else {
1003                 if (type == NOTIFICATION_TYPE_NONE) {
1004                         snprintf(query_where, sizeof(query_where),
1005                                  "where caller_pkgname = '%s' ", pkgname);
1006                 } else {
1007                         snprintf(query_where, sizeof(query_where),
1008                                  "where caller_pkgname = '%s' and type = %d ",
1009                                  pkgname, type);
1010                 }
1011         }
1012
1013         if (num_deleted != NULL) {
1014                 *num_deleted = 0;
1015         }
1016         if (list_deleted_rowid != NULL) {
1017                 *list_deleted_rowid = NULL;
1018                 snprintf(query, sizeof(query),
1019                                 "select priv_id from noti_list %s ", query_where);
1020
1021                 ret = sqlite3_prepare(db, query, strlen(query), &stmt, NULL);
1022                 if (ret != SQLITE_OK) {
1023                         NOTIFICATION_ERR("Select Query : %s", query);
1024                         NOTIFICATION_ERR("Select DB error(%d) : %s", ret,
1025                                          sqlite3_errmsg(db));
1026
1027                         ret = NOTIFICATION_ERROR_FROM_DB;
1028                         goto err;
1029                 }
1030
1031                 while(sqlite3_step(stmt) == SQLITE_ROW) {
1032                         if (data_cnt % 8 == 0) {
1033                                 int *tmp;
1034
1035                                 tmp = (int *)realloc(*list_deleted_rowid, sizeof(int) * (data_cnt + 8 + 1));
1036                                 if (tmp) {
1037                                         *list_deleted_rowid = tmp;
1038                                 } else {
1039                                         NOTIFICATION_ERR("Heap: %s\n", strerror(errno));
1040                                         /*!
1041                                          * \TODO
1042                                          * How can I handle this?
1043                                          */
1044                                         free(*list_deleted_rowid);
1045                                         *list_deleted_rowid = NULL;
1046                                         ret = NOTIFICATION_ERROR_NO_MEMORY;
1047                                         goto err;
1048                                 }
1049                         }
1050                         *((*list_deleted_rowid) + data_cnt) = sqlite3_column_int(stmt, 0);
1051                         data_cnt++;
1052                 }
1053
1054                 if (stmt) {
1055                         sqlite3_finalize(stmt);
1056                         stmt = NULL;
1057                 }
1058
1059                 if (data_cnt > 0) {
1060                         query_where[0] = '\0';
1061                         for (i = 0; i < data_cnt ; i++) {
1062                                 snprintf(buf, sizeof(buf), "%s%d", (i == 0) ? "" : ",", *((*list_deleted_rowid) + i));
1063                                 strncat(query_where, buf,sizeof(query_where) - strlen(query_where) - 1);
1064                         }
1065                         snprintf(query_base, sizeof(query_base), "delete from noti_list");
1066                         snprintf(query, sizeof(query), "%s where priv_id in (%s)", query_base, query_where);
1067
1068                         NOTIFICATION_ERR("check : %s", query);
1069                         ret = notification_db_exec(db, query, NULL);
1070                 } else {
1071                         free(*list_deleted_rowid);
1072                         *list_deleted_rowid = NULL;
1073                 }
1074
1075                 if (num_deleted != NULL) {
1076                         *num_deleted = data_cnt;
1077                 }
1078         } else {
1079                 /* Make main query */
1080                 snprintf(query_base, sizeof(query_base), "delete from noti_list ");
1081                 snprintf(query, sizeof(query), "%s %s", query_base, query_where);
1082
1083                 ret = notification_db_exec(db, query, NULL);
1084
1085                 if (num_deleted != NULL) {
1086                         *num_deleted = sqlite3_changes(db);
1087                 }
1088         }
1089
1090 err:
1091         if (stmt) {
1092                 sqlite3_finalize(stmt);
1093         }
1094         /* Close DB */
1095         if (db) {
1096                 notification_db_close(&db);
1097         }
1098
1099         return ret;
1100 }
1101
1102 int notification_noti_delete_group_by_group_id(const char *pkgname,
1103                                                int group_id, int *num_deleted, int **list_deleted_rowid)
1104 {
1105         int ret = NOTIFICATION_ERROR_NONE;
1106         sqlite3 *db = NULL;
1107         int i = 0, data_cnt = 0;
1108         sqlite3_stmt *stmt = NULL;
1109         char buf[128] = { 0, };
1110         char query[NOTIFICATION_QUERY_MAX] = { 0, };
1111         char query_base[NOTIFICATION_QUERY_MAX] = { 0, };
1112         char query_where[NOTIFICATION_QUERY_MAX] = { 0, };
1113
1114         /* Check pkgname is valid */
1115         if (pkgname == NULL) {
1116                 return NOTIFICATION_ERROR_INVALID_DATA;
1117         }
1118
1119         snprintf(query_where, sizeof(query_where),
1120                         "where caller_pkgname = '%s' and group_id = %d", pkgname, group_id);
1121
1122         /* Open DB */
1123         db = notification_db_open(DBPATH);
1124         if (!db) {
1125                 return NOTIFICATION_ERROR_FROM_DB;
1126         }
1127
1128         if (num_deleted != NULL) {
1129                 *num_deleted = 0;
1130         }
1131         if (list_deleted_rowid != NULL) {
1132                 *list_deleted_rowid = NULL;
1133                 snprintf(query, sizeof(query),
1134                                 "select priv_id from noti_list %s ", query_where);
1135
1136                 ret = sqlite3_prepare(db, query, strlen(query), &stmt, NULL);
1137                 if (ret != SQLITE_OK) {
1138                         NOTIFICATION_ERR("Select Query : %s", query);
1139                         NOTIFICATION_ERR("Select DB error(%d) : %s", ret,
1140                                          sqlite3_errmsg(db));
1141
1142                         ret = NOTIFICATION_ERROR_FROM_DB;
1143                         goto err;
1144                 }
1145
1146                 while(sqlite3_step(stmt) == SQLITE_ROW) {
1147                         if (data_cnt % 8 == 0) {
1148                                 int *tmp;
1149                                 tmp = (int *)realloc(*list_deleted_rowid, sizeof(int) * (data_cnt + 8 + 1));
1150                                 if (tmp) {
1151                                         *list_deleted_rowid = tmp;
1152                                 } else {
1153                                         free(*list_deleted_rowid);
1154                                         *list_deleted_rowid = NULL;
1155                                         ret = NOTIFICATION_ERROR_NO_MEMORY;
1156                                         goto err;
1157                                 }
1158                         }
1159                         *((*list_deleted_rowid) + data_cnt) = sqlite3_column_int(stmt, 0);
1160                         data_cnt++;
1161                 }
1162
1163                 if (stmt) {
1164                         sqlite3_finalize(stmt);
1165                         stmt = NULL;
1166                 }
1167
1168                 if (data_cnt > 0) {
1169                         query_where[0] = '\0';
1170                         for (i = 0; i < data_cnt ; i++) {
1171                                 snprintf(buf, sizeof(buf), "%s%d", (i == 0) ? "" : ",", *((*list_deleted_rowid) + i));
1172                                 strncat(query_where, buf,sizeof(query_where) - strlen(query_where) - 1);
1173                         }
1174                         snprintf(query_base, sizeof(query_base), "delete from noti_list");
1175                         snprintf(query, sizeof(query), "%s where priv_id in (%s)", query_base, query_where);
1176
1177                         NOTIFICATION_ERR("check : %s", query);
1178                         ret = notification_db_exec(db, query, NULL);
1179                 } else {
1180                         free(*list_deleted_rowid);
1181                         *list_deleted_rowid = NULL;
1182                 }
1183
1184                 if (num_deleted != NULL) {
1185                         *num_deleted = data_cnt;
1186                 }
1187         } else {
1188                 /* Make query */
1189                 snprintf(query, sizeof(query), "delete from noti_list %s", query_where);
1190
1191                 /* execute DB */
1192                 ret = notification_db_exec(db, query, NULL);
1193         }
1194
1195 err:
1196         if (stmt) {
1197                 sqlite3_finalize(stmt);
1198         }
1199         /* Close DB */
1200         if (db) {
1201                 notification_db_close(&db);
1202         }
1203
1204         return ret;
1205 }
1206
1207 int notification_noti_delete_group_by_priv_id(const char *pkgname, int priv_id)
1208 {
1209         sqlite3 *db = NULL;
1210         char query[NOTIFICATION_QUERY_MAX] = { 0, };
1211         int internal_group_id = 0;
1212         int ret;
1213
1214         /* Check pkgname is valid */
1215         if (pkgname == NULL) {
1216                 return NOTIFICATION_ERROR_INVALID_DATA;
1217         }
1218
1219         /* Open DB */
1220         db = notification_db_open(DBPATH);
1221         if (!db) {
1222                 return NOTIFICATION_ERROR_FROM_DB;
1223         }
1224
1225         /* Get internal group id using priv id */
1226         internal_group_id =
1227             _notification_noti_get_internal_group_id_by_priv_id(pkgname,
1228                                                                 priv_id, db);
1229
1230         /* Make query */
1231         snprintf(query, sizeof(query), "delete from noti_list "
1232                  "where caller_pkgname = '%s' and internal_group_id = %d",
1233                  pkgname, internal_group_id);
1234
1235         /* execute DB */
1236         ret = notification_db_exec(db, query, NULL);
1237
1238         /* Close DB */
1239         notification_db_close(&db);
1240
1241         return ret;
1242 }
1243
1244 EXPORT_API int notification_noti_delete_by_priv_id(const char *pkgname, int priv_id)
1245 {
1246         sqlite3 *db = NULL;
1247         char query[NOTIFICATION_QUERY_MAX] = { 0, };
1248         int ret;
1249
1250         /* Check pkgname is valid */
1251         if (pkgname == NULL) {
1252                 return NOTIFICATION_ERROR_INVALID_DATA;
1253         }
1254
1255         /* Open DB */
1256         db = notification_db_open(DBPATH);
1257         if (!db) {
1258                 return NOTIFICATION_ERROR_FROM_DB;
1259         }
1260
1261         /* Make query */
1262         snprintf(query, sizeof(query), "delete from noti_list "
1263                  "where caller_pkgname = '%s' and priv_id = %d", pkgname,
1264                  priv_id);
1265
1266         /* execute DB */
1267         ret = notification_db_exec(db, query, NULL);
1268
1269         /* Close DB */
1270         if (db) {
1271                 notification_db_close(&db);
1272         }
1273
1274         return ret;
1275 }
1276
1277 EXPORT_API int notification_noti_delete_by_priv_id_get_changes(const char *pkgname, int priv_id, int *num_changes)
1278 {
1279         sqlite3 *db = NULL;
1280         char query[NOTIFICATION_QUERY_MAX] = { 0, };
1281         int ret;
1282
1283         /* Check pkgname is valid */
1284         if (pkgname == NULL) {
1285                 return NOTIFICATION_ERROR_INVALID_DATA;
1286         }
1287
1288         /* Open DB */
1289         db = notification_db_open(DBPATH);
1290         if (!db) {
1291                 return NOTIFICATION_ERROR_FROM_DB;
1292         }
1293
1294         /* Make query */
1295         snprintf(query, sizeof(query), "delete from noti_list "
1296                  "where caller_pkgname = '%s' and priv_id = %d", pkgname,
1297                  priv_id);
1298
1299         /* execute DB */
1300         ret = notification_db_exec(db, query, num_changes);
1301
1302         if (num_changes != NULL) {
1303                 NOTIFICATION_DBG("deleted num:%d", *num_changes);
1304         }
1305
1306         /* Close DB */
1307         if (db) {
1308                 notification_db_close(&db);
1309         }
1310
1311         return ret;
1312 }
1313
1314 notification_error_e notification_noti_get_count(notification_type_e type,
1315                                                  const char *pkgname,
1316                                                  int group_id, int priv_id,
1317                                                  int *count)
1318 {
1319         sqlite3 *db = NULL;
1320         sqlite3_stmt *stmt = NULL;
1321         char query[NOTIFICATION_QUERY_MAX] = { 0, };
1322         char query_base[NOTIFICATION_QUERY_MAX] = { 0, };
1323         char query_where[NOTIFICATION_QUERY_MAX] = { 0, };
1324         char query_where_more[NOTIFICATION_QUERY_MAX] = { 0, };
1325
1326         int ret = 0, get_count = 0, internal_group_id = 0;
1327         int status = VCONFKEY_TELEPHONY_SIM_UNKNOWN;
1328         int flag_where = 0;
1329         int flag_where_more = 0;
1330         int ret_vconf = 0;
1331
1332         /* Open DB */
1333         db = notification_db_open(DBPATH);
1334         if (!db) {
1335                 return NOTIFICATION_ERROR_FROM_DB;
1336         }
1337
1338         /* Check current sim status */
1339         ret_vconf = vconf_get_int(VCONFKEY_TELEPHONY_SIM_SLOT, &status);
1340
1341         /* Make query */
1342         snprintf(query_base, sizeof(query_base),
1343                  "select count(*) from noti_list ");
1344
1345         if (pkgname != NULL) {
1346                 if (group_id == NOTIFICATION_GROUP_ID_NONE) {
1347                         if (priv_id == NOTIFICATION_PRIV_ID_NONE) {
1348                                 snprintf(query_where, sizeof(query_where),
1349                                          "where caller_pkgname = '%s' ",
1350                                          pkgname);
1351                                 flag_where = 1;
1352                         } else {
1353                                 internal_group_id =
1354                                     _notification_noti_get_internal_group_id_by_priv_id
1355                                     (pkgname, priv_id, db);
1356                                 snprintf(query_where, sizeof(query_where),
1357                                          "where caller_pkgname = '%s' and internal_group_id = %d ",
1358                                          pkgname, internal_group_id);
1359                                 flag_where = 1;
1360                         }
1361                 } else {
1362                         if (priv_id == NOTIFICATION_PRIV_ID_NONE) {
1363                                 snprintf(query_where, sizeof(query_where),
1364                                          "where caller_pkgname = '%s' and group_id = %d ",
1365                                          pkgname, group_id);
1366                                 flag_where = 1;
1367                         } else {
1368                                 internal_group_id =
1369                                     _notification_noti_get_internal_group_id_by_priv_id
1370                                     (pkgname, priv_id, db);
1371                                 snprintf(query_where, sizeof(query_where),
1372                                          "where caller_pkgname = '%s' and internal_group_id = %d ",
1373                                          pkgname, internal_group_id);
1374                                 flag_where = 1;
1375                         }
1376                 }
1377
1378         }
1379
1380         if (ret_vconf == 0 && status == VCONFKEY_TELEPHONY_SIM_INSERTED) {
1381                 if (type != NOTIFICATION_TYPE_NONE) {
1382                         snprintf(query_where_more, sizeof(query_where_more),
1383                                  "type = %d ", type);
1384                         flag_where_more = 1;
1385                 }
1386         } else {
1387                 if (type != NOTIFICATION_TYPE_NONE) {
1388                         snprintf(query_where_more, sizeof(query_where_more),
1389                                  "type = %d and flag_simmode = 0 ", type);
1390                         flag_where_more = 1;
1391                 } else {
1392                         snprintf(query_where_more, sizeof(query_where_more),
1393                                  "flag_simmode = 0 ");
1394                         flag_where_more = 1;
1395                 }
1396         }
1397
1398         if (flag_where == 1) {
1399                 if (flag_where_more == 1) {
1400                         snprintf(query, sizeof(query), "%s %s and %s",
1401                                  query_base, query_where, query_where_more);
1402                 } else {
1403                         snprintf(query, sizeof(query), "%s %s", query_base,
1404                                  query_where);
1405                 }
1406
1407         } else {
1408                 if (flag_where_more == 1) {
1409                         snprintf(query, sizeof(query), "%s where %s",
1410                                  query_base, query_where_more);
1411                 } else {
1412                         snprintf(query, sizeof(query), "%s", query_base);
1413                 }
1414         }
1415
1416         ret = sqlite3_prepare(db, query, strlen(query), &stmt, NULL);
1417         if (ret != SQLITE_OK) {
1418                 NOTIFICATION_ERR("Select Query : %s", query);
1419                 NOTIFICATION_ERR("Select DB error(%d) : %s", ret,
1420                                  sqlite3_errmsg(db));
1421
1422                 ret = NOTIFICATION_ERROR_FROM_DB;
1423                 goto err;
1424         }
1425
1426         ret = sqlite3_step(stmt);
1427         if (ret == SQLITE_ROW) {
1428                 get_count = sqlite3_column_int(stmt, 0);
1429         }
1430
1431         ret = NOTIFICATION_ERROR_NONE;
1432
1433 err:
1434         if (stmt) {
1435                 sqlite3_finalize(stmt);
1436         }
1437
1438         /* Close DB */
1439         if (db) {
1440                 notification_db_close(&db);
1441         }
1442
1443         *count = get_count;
1444
1445         return ret;
1446 }
1447
1448 notification_error_e notification_noti_get_grouping_list(notification_type_e type,
1449                                                          int count,
1450                                                          notification_list_h *
1451                                                          list)
1452 {
1453         sqlite3 *db = NULL;
1454         sqlite3_stmt *stmt = NULL;
1455         char query[NOTIFICATION_QUERY_MAX] = { 0, };
1456         char query_base[NOTIFICATION_QUERY_MAX] = { 0, };
1457         char query_where[NOTIFICATION_QUERY_MAX] = { 0, };
1458
1459         int ret = 0;
1460         notification_list_h get_list = NULL;
1461         notification_h noti = NULL;
1462         int internal_count = 0;
1463         int status;
1464
1465         /* Open DB */
1466         db = notification_db_open(DBPATH);
1467         if (!db) {
1468                 return NOTIFICATION_ERROR_FROM_DB;
1469         }
1470
1471         /* Check current sim status */
1472         ret = vconf_get_int(VCONFKEY_TELEPHONY_SIM_SLOT, &status);
1473
1474         /* Make query */
1475         snprintf(query_base, sizeof(query_base), "select "
1476                  "type, layout, caller_pkgname, launch_pkgname, image_path, group_id, priv_id, "
1477                  "b_text, b_key, b_format_args, num_format_args, "
1478                  "text_domain, text_dir, time, insert_time, args, group_args, "
1479                  "b_execute_option, b_service_responding, b_service_single_launch, b_service_multi_launch, "
1480                  "sound_type, sound_path, vibration_type, vibration_path, led_operation, led_argb, led_on_ms, led_off_ms, "
1481                  "flags_for_property, display_applist, progress_size, progress_percentage "
1482                  "from noti_list ");
1483
1484         if (status == VCONFKEY_TELEPHONY_SIM_INSERTED) {
1485                 if (type != NOTIFICATION_TYPE_NONE) {
1486                         snprintf(query_where, sizeof(query_where),
1487                                  "where type = %d ", type);
1488                 }
1489         } else {
1490                 if (type != NOTIFICATION_TYPE_NONE) {
1491                         snprintf(query_where, sizeof(query_where),
1492                                  "where type = %d and flag_simmode = 0 ", type);
1493                 } else {
1494                         snprintf(query_where, sizeof(query_where),
1495                                  "where flag_simmode = 0 ");
1496                 }
1497         }
1498
1499         snprintf(query, sizeof(query),
1500                  "%s %s "
1501                  "group by internal_group_id "
1502                  "order by rowid desc, time desc", query_base, query_where);
1503
1504         ret = sqlite3_prepare(db, query, strlen(query), &stmt, NULL);
1505         if (ret != SQLITE_OK) {
1506                 NOTIFICATION_ERR("Select Query : %s", query);
1507                 NOTIFICATION_ERR("Select DB error(%d) : %s", ret,
1508                                  sqlite3_errmsg(db));
1509
1510                 ret = NOTIFICATION_ERROR_FROM_DB;
1511                 goto err;
1512         }
1513
1514         while (sqlite3_step(stmt) == SQLITE_ROW) {
1515                 /* Make notification list */
1516                 noti = _notification_noti_get_item(stmt);
1517                 if (noti != NULL) {
1518                         internal_count++;
1519
1520                         get_list = notification_list_append(get_list, noti);
1521
1522                         if (count != -1 && internal_count >= count) {
1523                                 NOTIFICATION_INFO
1524                                     ("internal count %d >= count %d",
1525                                      internal_count, count);
1526                                 break;
1527                         }
1528                 }
1529         }
1530
1531         ret = NOTIFICATION_ERROR_NONE;
1532
1533 err:
1534         if (stmt) {
1535                 sqlite3_finalize(stmt);
1536         }
1537
1538         /* Close DB */
1539         if (db) {
1540                 notification_db_close(&db);
1541         }
1542
1543         if (get_list != NULL) {
1544                 *list = notification_list_get_head(get_list);
1545         }
1546
1547         return ret;
1548 }
1549
1550 notification_error_e notification_noti_get_detail_list(const char *pkgname,
1551                                                        int group_id,
1552                                                        int priv_id, int count,
1553                                                        notification_list_h *list)
1554 {
1555         sqlite3 *db = NULL;
1556         sqlite3_stmt *stmt = NULL;
1557         char query_base[NOTIFICATION_QUERY_MAX] = { 0, };
1558         char query_where[NOTIFICATION_QUERY_MAX] = { 0, };
1559
1560         char query[NOTIFICATION_QUERY_MAX] = { 0, };
1561         int ret = 0;
1562         notification_list_h get_list = NULL;
1563         notification_h noti = NULL;
1564         int internal_count = 0;
1565         int internal_group_id = 0;
1566         int status = 0; /* If the vconf_get_int failed, the status will be the garbage value */
1567
1568         /* Open DB */
1569         db = notification_db_open(DBPATH);
1570         if (!db) {
1571                 return NOTIFICATION_ERROR_FROM_DB;
1572         }
1573
1574         /* Check current sim status */
1575         ret = vconf_get_int(VCONFKEY_TELEPHONY_SIM_SLOT, &status);
1576
1577         /* Make query */
1578         snprintf(query_base, sizeof(query_base), "select "
1579                  "type, layout, caller_pkgname, launch_pkgname, image_path, group_id, priv_id, "
1580                  "b_text, b_key, b_format_args, num_format_args, "
1581                  "text_domain, text_dir, time, insert_time, args, group_args, "
1582                  "b_execute_option, b_service_responding, b_service_single_launch, b_service_multi_launch, "
1583                  "sound_type, sound_path, vibration_type, vibration_path, led_operation, led_argb, led_on_ms, led_off_ms, "
1584                  "flags_for_property, display_applist, progress_size, progress_percentage "
1585                  "from noti_list ");
1586
1587         if (priv_id == NOTIFICATION_PRIV_ID_NONE && group_id == NOTIFICATION_GROUP_ID_NONE) {
1588                 if (status == VCONFKEY_TELEPHONY_SIM_INSERTED) {
1589                         snprintf(query_where, sizeof(query_where),
1590                                  "where  caller_pkgname = '%s' ",
1591                                  pkgname, internal_group_id);
1592                 } else {
1593                         snprintf(query_where, sizeof(query_where),
1594                                  "where  caller_pkgname = '%s' and flag_simmode = 0 ",
1595                                  pkgname, internal_group_id);
1596                 }
1597         } else {
1598                 internal_group_id =
1599                     _notification_noti_get_internal_group_id_by_priv_id(pkgname,
1600                                                                         priv_id, db);
1601
1602                 if (status == VCONFKEY_TELEPHONY_SIM_INSERTED) {
1603                         snprintf(query_where, sizeof(query_where),
1604                                  "where  caller_pkgname = '%s' and internal_group_id = %d ",
1605                                  pkgname, internal_group_id);
1606                 } else {
1607                         snprintf(query_where, sizeof(query_where),
1608                                  "where  caller_pkgname = '%s' and internal_group_id = %d and flag_simmode = 0 ",
1609                                  pkgname, internal_group_id);
1610                 }
1611         }
1612
1613         snprintf(query, sizeof(query),
1614                  "%s %s "
1615                  "order by rowid desc, time desc", query_base, query_where);
1616
1617         ret = sqlite3_prepare(db, query, strlen(query), &stmt, NULL);
1618         if (ret != SQLITE_OK) {
1619                 NOTIFICATION_ERR("Select Query : %s", query);
1620                 NOTIFICATION_ERR("Select DB error(%d) : %s", ret,
1621                                  sqlite3_errmsg(db));
1622
1623                 ret = NOTIFICATION_ERROR_FROM_DB;
1624                 goto err;
1625         }
1626
1627         while (sqlite3_step(stmt) == SQLITE_ROW) {
1628                 /* Make notification list */
1629                 noti = _notification_noti_get_item(stmt);
1630                 if (noti != NULL) {
1631                         internal_count++;
1632
1633                         get_list = notification_list_append(get_list, noti);
1634
1635                         if (count != -1 && internal_count >= count) {
1636                                 NOTIFICATION_INFO
1637                                     ("internal count %d >= count %d",
1638                                      internal_count, count);
1639                                 break;
1640                         }
1641                 }
1642         }
1643
1644         ret = NOTIFICATION_ERROR_NONE;
1645
1646 err:
1647         if (stmt) {
1648                 sqlite3_finalize(stmt);
1649         }
1650
1651         /* Close DB */
1652         if (db) {
1653                 notification_db_close(&db);
1654         }
1655
1656         if (get_list != NULL) {
1657                 *list = notification_list_get_head(get_list);
1658         }
1659
1660         return ret;
1661 }