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