Git init
[apps/home/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>
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(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_check_priv_id(notification_h noti, sqlite3 * db)
59 {
60         sqlite3_stmt *stmt = NULL;
61         char query[NOTIFICATION_QUERY_MAX] = { 0, };
62         int ret = NOTIFICATION_ERROR_NONE, result = 0;
63
64         /* Make query to check priv_id exist */
65         snprintf(query, sizeof(query),
66                  "select count(*) from noti_list where caller_pkgname = '%s' and priv_id = %d",
67                  noti->caller_pkgname, noti->priv_id);
68
69         ret = sqlite3_prepare(db, query, strlen(query), &stmt, NULL);
70         if (ret != SQLITE_OK) {
71                 NOTIFICATION_ERR("Get count DB err(%d) : %s", ret,
72                                  sqlite3_errmsg(db));
73                 return NOTIFICATION_ERROR_FROM_DB;
74         }
75
76         ret = sqlite3_step(stmt);
77         if (ret == SQLITE_ROW) {
78                 result = sqlite3_column_int(stmt, 0);
79         } else {
80                 result = 0;
81         }
82
83         sqlite3_finalize(stmt);
84
85         /* If result > 0, there is priv_id in DB */
86         if (result > 0) {
87                 return NOTIFICATION_ERROR_ALREADY_EXIST_ID;
88         }
89
90         return NOTIFICATION_ERROR_NONE;
91 }
92
93 static int _notification_noti_get_priv_id(notification_h noti, sqlite3 * db)
94 {
95         sqlite3_stmt *stmt = NULL;
96         char query[NOTIFICATION_QUERY_MAX] = { 0, };
97         int ret = NOTIFICATION_ERROR_NONE, result = 0;
98
99         /* Make query to get max priv_id */
100         snprintf(query, sizeof(query),
101                  "select max(priv_id) from noti_list where caller_pkgname = '%s'",
102                  noti->caller_pkgname);
103
104         ret = sqlite3_prepare(db, query, strlen(query), &stmt, NULL);
105         if (ret != SQLITE_OK) {
106                 NOTIFICATION_ERR("Get count DB err(%d) : %s", ret,
107                                  sqlite3_errmsg(db));
108                 return NOTIFICATION_ERROR_FROM_DB;
109         }
110
111         ret = sqlite3_step(stmt);
112         if (ret == SQLITE_ROW) {
113                 result = sqlite3_column_int(stmt, 0);
114         } else {
115                 result = 0;
116         }
117
118         sqlite3_finalize(stmt);
119
120         if (result < 0) {
121                 return NOTIFICATION_ERROR_FROM_DB;
122         }
123
124         /* Increase result(max priv_id value) for next priv_id */
125         noti->priv_id = result + 1;
126
127         return NOTIFICATION_ERROR_NONE;
128 }
129
130 static int _notification_noti_get_internal_group_id_by_priv_id(const char *pkgname,
131                                                                int priv_id,
132                                                                sqlite3 * db)
133 {
134         sqlite3_stmt *stmt = NULL;
135         char query[NOTIFICATION_QUERY_MAX] = { 0, };
136         int ret = NOTIFICATION_ERROR_NONE, result = 0;
137
138         snprintf(query, sizeof(query),
139                  "select internal_group_id from noti_list where caller_pkgname = '%s' and priv_id = %d",
140                  pkgname, priv_id);
141
142         ret = sqlite3_prepare(db, query, strlen(query), &stmt, NULL);
143         if (ret != SQLITE_OK) {
144                 NOTIFICATION_ERR("Get count DB err(%d) : %s", ret,
145                                  sqlite3_errmsg(db));
146                 return NOTIFICATION_ERROR_FROM_DB;
147         }
148
149         ret = sqlite3_step(stmt);
150         if (ret == SQLITE_ROW) {
151                 result = sqlite3_column_int(stmt, 0);
152         } else {
153                 result = 0;
154         }
155
156         sqlite3_finalize(stmt);
157
158         return result;
159 }
160
161 static int _notification_noti_get_max_internal_group_id(notification_h noti,
162                                                         sqlite3 * db)
163 {
164         sqlite3_stmt *stmt = NULL;
165         char query[NOTIFICATION_QUERY_MAX] = { 0, };
166         int ret = NOTIFICATION_ERROR_NONE, result = 0;
167
168         /* Get max internal group id */
169         snprintf(query, sizeof(query),
170                  "select max(internal_group_id) from noti_list");
171
172         ret = sqlite3_prepare(db, query, strlen(query), &stmt, NULL);
173         if (ret != SQLITE_OK) {
174                 NOTIFICATION_ERR("Get count DB err(%d) : %s", ret,
175                                  sqlite3_errmsg(db));
176                 return NOTIFICATION_ERROR_FROM_DB;
177         }
178
179         ret = sqlite3_step(stmt);
180         if (ret == SQLITE_ROW) {
181                 result = sqlite3_column_int(stmt, 0);
182         } else {
183                 result = 0;
184         }
185
186         sqlite3_finalize(stmt);
187
188         return result;
189 }
190
191 static int _notification_noti_get_internal_group_id(notification_h noti,
192                                                     sqlite3 * db)
193 {
194         sqlite3_stmt *stmt = NULL;
195         char query[NOTIFICATION_QUERY_MAX] = { 0, };
196         int ret = NOTIFICATION_ERROR_NONE, result = 0;
197         const char *ret_title = NULL;
198         char buf_key[32] = { 0, };
199
200         if (noti->group_id == NOTIFICATION_GROUP_ID_NONE) {
201                 /* If Group ID is NONE Get max internal group ID */
202                 result = _notification_noti_get_max_internal_group_id(noti, db);
203                 if (result < 0) {
204                         return NOTIFICATION_ERROR_FROM_DB;
205                 }
206
207                 /* Internal Group ID is max internal group ID + 1 */
208                 noti->internal_group_id = result + 1;
209
210                 return NOTIFICATION_ERROR_NONE;
211         } else if (noti->group_id == NOTIFICATION_GROUP_ID_DEFAULT) {
212                 /* If Group ID is DEFAULT, Get internal group id if it exist */
213                 if (noti->b_key != NULL) {
214                         snprintf(buf_key, sizeof(buf_key), "%d",
215                                  NOTIFICATION_TEXT_TYPE_TITLE);
216
217                         ret_title = bundle_get_val(noti->b_key, buf_key);
218                 }
219
220                 if (ret_title == NULL && noti->b_text != NULL) {
221                         snprintf(buf_key, sizeof(buf_key), "%d",
222                                  NOTIFICATION_TEXT_TYPE_TITLE);
223
224                         ret_title = bundle_get_val(noti->b_text, buf_key);
225                 }
226
227                 if (ret_title == NULL) {
228                         ret_title = noti->caller_pkgname;
229                 }
230
231                 snprintf(query, sizeof(query),
232                          "select internal_group_id from noti_list where title_key = $title_key and group_id = %d",
233                          NOTIFICATION_GROUP_ID_DEFAULT);
234         } else {
235                 /* If Group ID is > DEFAULT, Get internal group id if it exit */
236                 snprintf(query, sizeof(query),
237                          "select internal_group_id from noti_list where caller_pkgname = '%s' and group_id = %d",
238                          NOTIFICATION_CHECK_STR(noti->caller_pkgname),
239                          noti->group_id);
240         }
241
242         ret = sqlite3_prepare_v2(db, query, -1, &stmt, NULL);
243         if (ret != SQLITE_OK) {
244                 NOTIFICATION_ERR("Select Query : %s", query);
245                 NOTIFICATION_ERR("Select DB error(%d) : %s", ret,
246                                  sqlite3_errmsg(db));
247                 if (stmt) {
248                         sqlite3_finalize(stmt);
249                 }
250
251                 if (db) {
252                         notification_db_close(&db);
253                 }
254                 return NOTIFICATION_ERROR_FROM_DB;
255         }
256
257         /* Bind query */
258         if (ret_title != NULL) {
259                 ret =
260                     _notification_noti_bind_query(stmt, "$title_key",
261                                                   NOTIFICATION_CHECK_STR
262                                                   (ret_title));
263                 if (ret != NOTIFICATION_ERROR_NONE) {
264                         NOTIFICATION_ERR("Bind error : %s", sqlite3_errmsg(db));
265                         return ret;
266                 }
267         }
268
269         ret = sqlite3_step(stmt);
270         if (ret == SQLITE_ROW) {
271                 result = sqlite3_column_int(stmt, 0);
272         } else {
273                 /* If there is not internal_group_id, create new one */
274                 result = _notification_noti_get_max_internal_group_id(noti, db);
275                 result++;
276         }
277
278         sqlite3_finalize(stmt);
279
280         noti->internal_group_id = result;
281
282         return NOTIFICATION_ERROR_NONE;
283 }
284
285 static int _notification_noti_make_query(notification_h noti, char *query,
286                                          int query_size)
287 {
288         char *args = NULL;
289         char *group_args = NULL;
290         char *b_image_path = NULL;
291         char *b_execute_option = NULL;
292         char *b_service_responding = NULL;
293         char *b_service_single_launch = NULL;
294         char *b_service_multi_launch = NULL;
295         char *b_text = NULL;
296         char *b_key = NULL;
297         char *b_format_args = NULL;
298         int flag_simmode = 0;
299
300         /* Decode bundle to insert DB */
301         if (noti->args) {
302                 bundle_encode(noti->args, (bundle_raw **) & args, NULL);
303         }
304         if (noti->group_args) {
305                 bundle_encode(noti->group_args, (bundle_raw **) & group_args,
306                               NULL);
307         }
308
309         if (noti->b_execute_option) {
310                 bundle_encode(noti->b_execute_option,
311                               (bundle_raw **) & b_execute_option, NULL);
312         }
313         if (noti->b_service_responding) {
314                 bundle_encode(noti->b_service_responding,
315                               (bundle_raw **) & b_service_responding, NULL);
316         }
317         if (noti->b_service_single_launch) {
318                 bundle_encode(noti->b_service_single_launch,
319                               (bundle_raw **) & b_service_single_launch, NULL);
320         }
321         if (noti->b_service_multi_launch) {
322                 bundle_encode(noti->b_service_multi_launch,
323                               (bundle_raw **) & b_service_multi_launch, NULL);
324         }
325
326         if (noti->b_text) {
327                 bundle_encode(noti->b_text, (bundle_raw **) & b_text, NULL);
328         }
329         if (noti->b_key) {
330                 bundle_encode(noti->b_key, (bundle_raw **) & b_key, NULL);
331         }
332         if (noti->b_format_args) {
333                 bundle_encode(noti->b_format_args,
334                               (bundle_raw **) & b_format_args, NULL);
335         }
336
337         if (noti->b_image_path) {
338                 bundle_encode(noti->b_image_path,
339                               (bundle_raw **) & b_image_path, NULL);
340         }
341
342         /* Check only simmode property is enable */
343         if (noti->flags_for_property & NOTIFICATION_PROP_DISPLAY_ONLY_SIMMODE) {
344                 flag_simmode = 1;
345         }
346
347         /* Make query */
348         snprintf(query, query_size, "insert into noti_list ("
349                  "type, "
350                  "caller_pkgname, launch_pkgname, "
351                  "image_path, "
352                  "group_id, internal_group_id, priv_id, "
353                  "title_key, "
354                  "b_text, b_key, b_format_args, num_format_args, "
355                  "text_domain, text_dir, "
356                  "time, insert_time, "
357                  "args, group_args, "
358                  "b_execute_option, "
359                  "b_service_responding, b_service_single_launch, b_service_multi_launch, "
360                  "sound_type, sound_path, vibration_type, vibration_path, "
361                  "flags_for_property, flag_simmode, display_applist, "
362                  "progress_size, progress_percentage) values ("
363                  "%d, "
364                  "'%s', '%s', "
365                  "'%s', "
366                  "%d, %d, %d, "
367                  "$title_key, "
368                  "'%s', '%s', '%s', %d, "
369                  "'%s', '%s', "
370                  "%d, %d, "
371                  "'%s', '%s', "
372                  "'%s', "
373                  "'%s', '%s', '%s', "
374                  "%d, '%s', %d, '%s', "
375                  "%d, %d, %d, "
376                  "%f, %f)",
377                  noti->type,
378                  NOTIFICATION_CHECK_STR(noti->caller_pkgname),
379                  NOTIFICATION_CHECK_STR(noti->launch_pkgname),
380                  NOTIFICATION_CHECK_STR(b_image_path), noti->group_id,
381                  noti->internal_group_id, noti->priv_id,
382                  NOTIFICATION_CHECK_STR(b_text), NOTIFICATION_CHECK_STR(b_key),
383                  NOTIFICATION_CHECK_STR(b_format_args), noti->num_format_args,
384                  NOTIFICATION_CHECK_STR(noti->domain),
385                  NOTIFICATION_CHECK_STR(noti->dir), (int)noti->time,
386                  (int)noti->insert_time, NOTIFICATION_CHECK_STR(args),
387                  NOTIFICATION_CHECK_STR(group_args),
388                  NOTIFICATION_CHECK_STR(b_execute_option),
389                  NOTIFICATION_CHECK_STR(b_service_responding),
390                  NOTIFICATION_CHECK_STR(b_service_single_launch),
391                  NOTIFICATION_CHECK_STR(b_service_multi_launch),
392                  noti->sound_type, NOTIFICATION_CHECK_STR(noti->sound_path),
393                  noti->vibration_type,
394                  NOTIFICATION_CHECK_STR(noti->vibration_path),
395                  noti->flags_for_property, flag_simmode, noti->display_applist,
396                  noti->progress_size, noti->progress_percentage);
397
398         /* Free decoded data */
399         if (args) {
400                 free(args);
401         }
402         if (group_args) {
403                 free(group_args);
404         }
405
406         if (b_execute_option) {
407                 free(b_execute_option);
408         }
409         if (b_service_responding) {
410                 free(b_service_responding);
411         }
412         if (b_service_single_launch) {
413                 free(b_service_single_launch);
414         }
415         if (b_service_multi_launch) {
416                 free(b_service_multi_launch);
417         }
418
419         if (b_text) {
420                 free(b_text);
421         }
422         if (b_key) {
423                 free(b_key);
424         }
425         if (b_format_args) {
426                 free(b_format_args);
427         }
428
429         if (b_image_path) {
430                 free(b_image_path);
431         }
432
433         return NOTIFICATION_ERROR_NONE;
434 }
435
436 static notification_h _notification_noti_get_item(sqlite3_stmt * stmt)
437 {
438         notification_h noti = NULL;
439         int col = 0;
440
441         noti = malloc(sizeof(struct _notification));
442         if (noti == NULL) {
443                 return NULL;
444         }
445
446         noti->type = sqlite3_column_int(stmt, col++);
447         noti->caller_pkgname = notification_db_column_text(stmt, col++);
448         noti->launch_pkgname = notification_db_column_text(stmt, col++);
449         noti->b_image_path = notification_db_column_bundle(stmt, col++);
450         noti->group_id = sqlite3_column_int(stmt, col++);
451         noti->internal_group_id = 0;
452         noti->priv_id = sqlite3_column_int(stmt, col++);
453
454         noti->b_text = notification_db_column_bundle(stmt, col++);
455         noti->b_key = notification_db_column_bundle(stmt, col++);
456         noti->b_format_args = notification_db_column_bundle(stmt, col++);
457         noti->num_format_args = sqlite3_column_int(stmt, col++);
458
459         noti->domain = notification_db_column_text(stmt, col++);
460         noti->dir = notification_db_column_text(stmt, col++);
461         noti->time = sqlite3_column_int(stmt, col++);
462         noti->insert_time = sqlite3_column_int(stmt, col++);
463         noti->args = notification_db_column_bundle(stmt, col++);
464         noti->group_args = notification_db_column_bundle(stmt, col++);
465
466         noti->b_execute_option = notification_db_column_bundle(stmt, col++);
467         noti->b_service_responding = notification_db_column_bundle(stmt, col++);
468         noti->b_service_single_launch =
469             notification_db_column_bundle(stmt, col++);
470         noti->b_service_multi_launch =
471             notification_db_column_bundle(stmt, col++);
472
473         noti->sound_type = sqlite3_column_int(stmt, col++);
474         noti->sound_path = notification_db_column_text(stmt, col++);
475         noti->vibration_type = sqlite3_column_int(stmt, col++);
476         noti->vibration_path = notification_db_column_text(stmt, col++);
477
478         noti->flags_for_property = sqlite3_column_int(stmt, col++);
479         noti->display_applist = sqlite3_column_int(stmt, col++);
480         noti->progress_size = sqlite3_column_double(stmt, col++);
481         noti->progress_percentage = sqlite3_column_double(stmt, col++);
482
483         noti->app_icon_path = NULL;
484         noti->app_name = NULL;
485         noti->temp_title = NULL;
486         noti->temp_content = NULL;
487         return noti;
488 }
489
490 int notification_noti_insert(notification_h noti)
491 {
492         sqlite3 *db;
493         sqlite3_stmt *stmt = NULL;
494         char query[NOTIFICATION_QUERY_MAX] = { 0, };
495         int ret = 0, result = 0;
496         char buf_key[32] = { 0, };
497         const char *title_key = NULL;
498
499         /* Open DB */
500         db = notification_db_open(DBPATH);
501
502         /* Get private ID */
503         if (noti->priv_id == NOTIFICATION_PRIV_ID_NONE) {
504                 ret = _notification_noti_get_priv_id(noti, db);
505         } else {
506                 ret = _notification_noti_check_priv_id(noti, db);
507                 if (ret != NOTIFICATION_ERROR_NONE) {
508                         return ret;
509                 }
510         }
511
512         /* Get internal group ID */
513         ret = _notification_noti_get_internal_group_id(noti, db);
514         if (ret != NOTIFICATION_ERROR_NONE) {
515                 return ret;
516         }
517
518         /* make query */
519         ret = _notification_noti_make_query(noti, query, sizeof(query));
520         if (ret != NOTIFICATION_ERROR_NONE) {
521                 return ret;
522         }
523
524         ret = sqlite3_prepare_v2(db, query, -1, &stmt, NULL);
525         if (ret != SQLITE_OK) {
526                 NOTIFICATION_ERR("Insert Query : %s", query);
527                 NOTIFICATION_ERR("Insert DB error(%d) : %s", ret,
528                                  sqlite3_errmsg(db));
529                 if (stmt) {
530                         sqlite3_finalize(stmt);
531                 }
532
533                 if (db) {
534                         notification_db_close(&db);
535                 }
536                 return NOTIFICATION_ERROR_FROM_DB;
537         }
538
539         /* Get title key */
540         if (noti->b_key != NULL) {
541                 snprintf(buf_key, sizeof(buf_key), "%d",
542                          NOTIFICATION_TEXT_TYPE_TITLE);
543
544                 title_key = bundle_get_val(noti->b_key, buf_key);
545         }
546
547         if (title_key == NULL && noti->b_text != NULL) {
548                 snprintf(buf_key, sizeof(buf_key), "%d",
549                          NOTIFICATION_TEXT_TYPE_TITLE);
550
551                 title_key = bundle_get_val(noti->b_text, buf_key);
552         }
553
554         if (title_key == NULL) {
555                 title_key = noti->caller_pkgname;
556         }
557
558         /* Bind query */
559         ret = _notification_noti_bind_query(stmt, "$title_key", title_key);
560         if (ret != NOTIFICATION_ERROR_NONE) {
561                 NOTIFICATION_ERR("Bind error : %s", sqlite3_errmsg(db));
562                 return ret;
563         }
564
565         ret = sqlite3_step(stmt);
566         if (ret == SQLITE_OK || ret == SQLITE_DONE) {
567                 result = NOTIFICATION_ERROR_NONE;
568         } else {
569                 result = NOTIFICATION_ERROR_FROM_DB;
570         }
571
572         if (stmt) {
573                 sqlite3_finalize(stmt);
574         }
575
576         /* Close DB */
577         if (db) {
578                 notification_db_close(&db);
579         }
580
581         return result;
582 }
583
584 int notification_noti_delete_all(notification_type_e type, const char *pkgname)
585 {
586         sqlite3 *db;
587         char query[NOTIFICATION_QUERY_MAX] = { 0, };
588         char query_base[NOTIFICATION_QUERY_MAX] = { 0, };
589         char query_where[NOTIFICATION_QUERY_MAX] = { 0, };
590
591         /* Open DB */
592         db = notification_db_open(DBPATH);
593
594         /* Make query */
595         snprintf(query_base, sizeof(query_base), "delete from noti_list ");
596
597         if (pkgname == NULL) {
598                 if (type != NOTIFICATION_TYPE_NONE) {
599                         snprintf(query_where, sizeof(query_where),
600                                  "where type = %d ", type);
601                 }
602         } else {
603                 if (type == NOTIFICATION_TYPE_NONE) {
604                         snprintf(query_where, sizeof(query_where),
605                                  "where caller_pkgname = '%s' ", pkgname);
606                 } else {
607                         snprintf(query_where, sizeof(query_where),
608                                  "where caller_pkgname = '%s' and type = %d ",
609                                  pkgname, type);
610                 }
611         }
612
613         snprintf(query, sizeof(query), "%s %s", query_base, query_where);
614
615         //NOTIFICATION_INFO("Delete All : [%s]", query);
616
617         /* execute DB */
618         notification_db_exec(db, query);
619
620         /* Close DB */
621         if (db) {
622                 notification_db_close(&db);
623         }
624
625         return NOTIFICATION_ERROR_NONE;
626 }
627
628 int notification_noti_delete_group_by_group_id(const char *pkgname,
629                                                int group_id)
630 {
631         sqlite3 *db;
632         char query[NOTIFICATION_QUERY_MAX] = { 0, };
633
634         /* Check pkgname is valid */
635         if (pkgname == NULL) {
636                 return NOTIFICATION_ERROR_INVALID_DATA;
637         }
638
639         /* Open DB */
640         db = notification_db_open(DBPATH);
641
642         /* Make query */
643         snprintf(query, sizeof(query), "delete from noti_list "
644                  "where caller_pkgname = '%s' and group_id = %d", pkgname,
645                  group_id);
646
647         /* execute DB */
648         notification_db_exec(db, query);
649
650         /* Close DB */
651         if (db) {
652                 notification_db_close(&db);
653         }
654
655         return NOTIFICATION_ERROR_NONE;
656 }
657
658 int notification_noti_delete_group_by_priv_id(const char *pkgname, int priv_id)
659 {
660         sqlite3 *db;
661         char query[NOTIFICATION_QUERY_MAX] = { 0, };
662         int internal_group_id = 0;
663
664         /* Check pkgname is valid */
665         if (pkgname == NULL) {
666                 return NOTIFICATION_ERROR_INVALID_DATA;
667         }
668
669         /* Open DB */
670         db = notification_db_open(DBPATH);
671
672         /* Get internal group id using priv id */
673         internal_group_id =
674             _notification_noti_get_internal_group_id_by_priv_id(pkgname,
675                                                                 priv_id, db);
676
677         /* Make query */
678         snprintf(query, sizeof(query), "delete from noti_list "
679                  "where caller_pkgname = '%s' and internal_group_id = %d",
680                  pkgname, internal_group_id);
681
682         /* execute DB */
683         notification_db_exec(db, query);
684
685         /* Close DB */
686         if (db) {
687                 notification_db_close(&db);
688         }
689
690         return NOTIFICATION_ERROR_NONE;
691 }
692
693 int notification_noti_delete_by_priv_id(const char *pkgname, int priv_id)
694 {
695         sqlite3 *db;
696         char query[NOTIFICATION_QUERY_MAX] = { 0, };
697
698         /* Check pkgname is valid */
699         if (pkgname == NULL) {
700                 return NOTIFICATION_ERROR_INVALID_DATA;
701         }
702
703         /* Open DB */
704         db = notification_db_open(DBPATH);
705
706         /* Make query */
707         snprintf(query, sizeof(query), "delete from noti_list "
708                  "where caller_pkgname = '%s' and priv_id = %d", pkgname,
709                  priv_id);
710
711         /* execute DB */
712         notification_db_exec(db, query);
713
714         /* Close DB */
715         if (db) {
716                 notification_db_close(&db);
717         }
718
719         return NOTIFICATION_ERROR_NONE;
720 }
721
722 notification_error_e notification_noti_get_count(notification_type_e type,
723                                                  const char *pkgname,
724                                                  int group_id, int priv_id,
725                                                  int *count)
726 {
727         sqlite3 *db = NULL;
728         sqlite3_stmt *stmt = NULL;
729         char query[NOTIFICATION_QUERY_MAX] = { 0, };
730         char query_base[NOTIFICATION_QUERY_MAX] = { 0, };
731         char query_where[NOTIFICATION_QUERY_MAX] = { 0, };
732         char query_where_more[NOTIFICATION_QUERY_MAX] = { 0, };
733
734         int ret = 0, get_count = 0, internal_group_id = 0;
735         int status = VCONFKEY_TELEPHONY_SIM_UNKNOWN;
736         int flag_where = 0;
737         int flag_where_more = 0;
738
739         /* Open DB */
740         db = notification_db_open(DBPATH);
741
742         /* Check current sim status */
743         vconf_get_int(VCONFKEY_TELEPHONY_SIM_SLOT, &status);
744
745         /* Make query */
746         snprintf(query_base, sizeof(query_base),
747                  "select count(*) from noti_list ");
748
749         if (pkgname != NULL) {
750                 if (group_id == NOTIFICATION_GROUP_ID_NONE) {
751                         if (priv_id == NOTIFICATION_PRIV_ID_NONE) {
752                                 snprintf(query_where, sizeof(query_where),
753                                          "where caller_pkgname = '%s' ",
754                                          pkgname);
755                                 flag_where = 1;
756                         } else {
757                                 internal_group_id =
758                                     _notification_noti_get_internal_group_id_by_priv_id
759                                     (pkgname, priv_id, db);
760                                 snprintf(query_where, sizeof(query_where),
761                                          "where caller_pkgname = '%s' and internal_group_id = %d ",
762                                          pkgname, internal_group_id);
763                                 flag_where = 1;
764                         }
765                 } else {
766                         if (priv_id == NOTIFICATION_PRIV_ID_NONE) {
767                                 snprintf(query_where, sizeof(query_where),
768                                          "where caller_pkgname = '%s' and group_id = %d ",
769                                          pkgname, group_id);
770                                 flag_where = 1;
771                         } else {
772                                 internal_group_id =
773                                     _notification_noti_get_internal_group_id_by_priv_id
774                                     (pkgname, priv_id, db);
775                                 snprintf(query_where, sizeof(query_where),
776                                          "where caller_pkgname = '%s' and internal_group_id = %d ",
777                                          pkgname, internal_group_id);
778                                 flag_where = 1;
779                         }
780                 }
781
782         }
783
784         if (status == VCONFKEY_TELEPHONY_SIM_INSERTED) {
785                 if (type != NOTIFICATION_TYPE_NONE) {
786                         snprintf(query_where_more, sizeof(query_where_more),
787                                  "type = %d ", type);
788                         flag_where_more = 1;
789                 }
790         } else {
791                 if (type != NOTIFICATION_TYPE_NONE) {
792                         snprintf(query_where_more, sizeof(query_where_more),
793                                  "type = %d and flag_simmode = 0 ", type);
794                         flag_where_more = 1;
795                 } else {
796                         snprintf(query_where_more, sizeof(query_where_more),
797                                  "flag_simmode = 0 ");
798                         flag_where_more = 1;
799                 }
800         }
801
802         if (flag_where == 1) {
803                 if (flag_where_more == 1) {
804                         snprintf(query, sizeof(query), "%s %s and %s",
805                                  query_base, query_where, query_where_more);
806                 } else {
807                         snprintf(query, sizeof(query), "%s %s", query_base,
808                                  query_where);
809                 }
810
811         } else {
812                 if (flag_where_more == 1) {
813                         snprintf(query, sizeof(query), "%s where %s",
814                                  query_base, query_where_more);
815                 } else {
816                         snprintf(query, sizeof(query), "%s", query_base);
817                 }
818         }
819
820         ret = sqlite3_prepare(db, query, strlen(query), &stmt, NULL);
821         if (ret != SQLITE_OK) {
822                 NOTIFICATION_ERR("Select Query : %s", query);
823                 NOTIFICATION_ERR("Select DB error(%d) : %s", ret,
824                                  sqlite3_errmsg(db));
825
826                 return NOTIFICATION_ERROR_FROM_DB;
827         }
828
829         ret = sqlite3_step(stmt);
830         if (ret == SQLITE_ROW) {
831                 get_count = sqlite3_column_int(stmt, 0);
832         }
833
834         sqlite3_finalize(stmt);
835
836         /* Close DB */
837         if (db) {
838                 notification_db_close(&db);
839         }
840
841         *count = get_count;
842
843         return NOTIFICATION_ERROR_NONE;
844 }
845
846 notification_error_e notification_noti_get_grouping_list(notification_type_e type,
847                                                          int count,
848                                                          notification_list_h *
849                                                          list)
850 {
851         sqlite3 *db;
852         sqlite3_stmt *stmt = NULL;
853         char query[NOTIFICATION_QUERY_MAX] = { 0, };
854         char query_base[NOTIFICATION_QUERY_MAX] = { 0, };
855         char query_where[NOTIFICATION_QUERY_MAX] = { 0, };
856
857         int ret = 0;
858         notification_list_h get_list = NULL;
859         notification_h noti = NULL;
860         int internal_count = 0;
861         int status;
862
863         /* Open DB */
864         db = notification_db_open(DBPATH);
865
866         /* Check current sim status */
867         ret = vconf_get_int(VCONFKEY_TELEPHONY_SIM_SLOT, &status);
868
869         /* Make query */
870         snprintf(query_base, sizeof(query_base), "select "
871                  "type, 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, "
876                  "flags_for_property, display_applist, progress_size, progress_percentage "
877                  "from noti_list ");
878
879         if (status == VCONFKEY_TELEPHONY_SIM_INSERTED) {
880                 if (type != NOTIFICATION_TYPE_NONE) {
881                         snprintf(query_where, sizeof(query_where),
882                                  "where type = %d ", type);
883                 }
884         } else {
885                 if (type != NOTIFICATION_TYPE_NONE) {
886                         snprintf(query_where, sizeof(query_where),
887                                  "where type = %d and flag_simmode = 0 ", type);
888                 } else {
889                         snprintf(query_where, sizeof(query_where),
890                                  "where flag_simmode = 0 ");
891                 }
892         }
893
894         snprintf(query, sizeof(query),
895                  "%s %s "
896                  "group by internal_group_id "
897                  "order by rowid desc, time desc", query_base, query_where);
898
899         ret = sqlite3_prepare(db, query, strlen(query), &stmt, NULL);
900         if (ret != SQLITE_OK) {
901                 NOTIFICATION_ERR("Select Query : %s", query);
902                 NOTIFICATION_ERR("Select DB error(%d) : %s", ret,
903                                  sqlite3_errmsg(db));
904
905                 return NOTIFICATION_ERROR_FROM_DB;
906         }
907
908         ret = sqlite3_step(stmt);
909         while (ret == SQLITE_ROW) {
910                 /* Make notification list */
911                 noti = _notification_noti_get_item(stmt);
912                 if (noti != NULL) {
913                         internal_count++;
914
915                         get_list = notification_list_append(get_list, noti);
916
917                         if (count != -1 && internal_count >= count) {
918                                 NOTIFICATION_INFO
919                                     ("internal count %d >= count %d",
920                                      internal_count, count);
921                                 break;
922                         }
923                 }
924
925                 ret = sqlite3_step(stmt);
926         }
927
928         sqlite3_finalize(stmt);
929
930         /* Close DB */
931         if (db) {
932                 notification_db_close(&db);
933         }
934
935         if (get_list != NULL) {
936                 *list = notification_list_get_head(get_list);
937         }
938
939         return NOTIFICATION_ERROR_NONE;
940 }
941
942 notification_error_e notification_noti_get_detail_list(const char *pkgname,
943                                                        int group_id,
944                                                        int priv_id, int count,
945                                                        notification_list_h *list)
946 {
947         sqlite3 *db;
948         sqlite3_stmt *stmt = NULL;
949         char query_base[NOTIFICATION_QUERY_MAX] = { 0, };
950         char query_where[NOTIFICATION_QUERY_MAX] = { 0, };
951
952         char query[NOTIFICATION_QUERY_MAX] = { 0, };
953         int ret = 0;
954         notification_list_h get_list = NULL;
955         notification_h noti = NULL;
956         int internal_count = 0;
957         int internal_group_id = 0;
958         int status;
959
960         /* Open DB */
961         db = notification_db_open(DBPATH);
962
963         /* Check current sim status */
964         ret = vconf_get_int(VCONFKEY_TELEPHONY_SIM_SLOT, &status);
965
966         /* Make query */
967         snprintf(query_base, sizeof(query_base), "select "
968                  "type, caller_pkgname, launch_pkgname, image_path, group_id, priv_id, "
969                  "b_text, b_key, b_format_args, num_format_args, "
970                  "text_domain, text_dir, time, insert_time, args, group_args, "
971                  "b_execute_option, b_service_responding, b_service_single_launch, b_service_multi_launch, "
972                  "sound_type, sound_path, vibration_type, vibration_path, "
973                  "flags_for_property, display_applist, progress_size, progress_percentage "
974                  "from noti_list ");
975
976         internal_group_id =
977             _notification_noti_get_internal_group_id_by_priv_id(pkgname,
978                                                                 priv_id, db);
979
980         if (status == VCONFKEY_TELEPHONY_SIM_INSERTED) {
981                 snprintf(query_where, sizeof(query_where),
982                          "where  caller_pkgname = '%s' and internal_group_id = %d ",
983                          pkgname, internal_group_id);
984         } else {
985                 snprintf(query_where, sizeof(query_where),
986                          "where  caller_pkgname = '%s' and internal_group_id = %d and flag_simmode = 0 ",
987                          pkgname, internal_group_id);
988         }
989
990         snprintf(query, sizeof(query),
991                  "%s %s "
992                  "order by rowid desc, time desc", query_base, query_where);
993
994         ret = sqlite3_prepare(db, query, strlen(query), &stmt, NULL);
995         if (ret != SQLITE_OK) {
996                 NOTIFICATION_ERR("Select Query : %s", query);
997                 NOTIFICATION_ERR("Select DB error(%d) : %s", ret,
998                                  sqlite3_errmsg(db));
999
1000                 return NOTIFICATION_ERROR_FROM_DB;
1001         }
1002
1003         ret = sqlite3_step(stmt);
1004         while (ret == SQLITE_ROW) {
1005                 /* Make notification list */
1006                 noti = _notification_noti_get_item(stmt);
1007                 if (noti != NULL) {
1008                         internal_count++;
1009
1010                         get_list = notification_list_append(get_list, noti);
1011
1012                         if (count != -1 && internal_count >= count) {
1013                                 NOTIFICATION_INFO
1014                                     ("internal count %d >= count %d",
1015                                      internal_count, count);
1016                                 break;
1017                         }
1018                 }
1019
1020                 ret = sqlite3_step(stmt);
1021         }
1022
1023         sqlite3_finalize(stmt);
1024
1025         /* Close DB */
1026         if (db) {
1027                 notification_db_close(&db);
1028         }
1029
1030         if (get_list != NULL) {
1031                 *list = notification_list_get_head(get_list);
1032         }
1033
1034         return NOTIFICATION_ERROR_NONE;
1035 }