Add multi-user feature
[platform/core/appfw/librua.git] / src / rua_stat.c
1 /*
2  * Copyright (c) 2000 - 2016 Samsung Electronics Co., Ltd. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /*
18  * @file    rua_stat.c
19  * @author  Hyunho Kang (hhstark.kang@samsung.com)
20  * @version 0.1
21  */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <sys/types.h>
28
29 #include <db-util.h>
30 #include <aul.h>
31
32 #include "rua_stat_internal.h"
33 #include "rua_stat.h"
34
35 int rua_stat_update_for_uid(char *caller, char *tag, uid_t uid)
36 {
37         int r;
38         bundle *b = NULL;
39
40         r = _rua_util_check_uid(uid);
41         if (r == -1)
42                 return r;
43
44         if (caller == NULL || tag == NULL) {
45                 LOGE("invalid param");
46                 return -1;
47         }
48
49         b = bundle_create();
50         if (b == NULL) {
51                 LOGE("bundle_create fail out of memory.");
52                 return -1;
53         }
54         bundle_add_str(b, AUL_SVC_K_RUA_STAT_CALLER, caller);
55         bundle_add_str(b, AUL_SVC_K_RUA_STAT_TAG, tag);
56         r = aul_update_rua_stat_for_uid(b, uid);
57         LOGI("rua_add_history_for_uid result : %d ", r);
58         bundle_free(b);
59         return r;
60 }
61
62 int rua_stat_get_stat_tags(char *caller,
63                 int (*rua_stat_tag_iter_fn)(const char *rua_stat_tag, void *data),
64                 void *data)
65 {
66         return rua_stat_get_stat_tags_for_uid(caller, rua_stat_tag_iter_fn, data, getuid());
67 }
68
69 int rua_stat_get_stat_tags_for_uid(char *caller,
70                 int (*rua_stat_tag_iter_fn)(const char *rua_stat_tag, void *data),
71                 void *data, uid_t uid)
72 {
73         int r;
74         sqlite3_stmt *stmt;
75         char query[QUERY_MAXLEN];
76         const unsigned char *ct;
77         sqlite3 *db = NULL;
78
79         r = _rua_util_check_uid(uid);
80         if (r == -1)
81                 return r;
82
83         r = _rua_stat_init(&db, RUA_STAT_DB_NAME, SQLITE_OPEN_READONLY, uid);
84         if (r == -1) {
85                 LOGE("__rua_stat_init fail");
86                 return -1;
87         }
88
89         sqlite3_snprintf(QUERY_MAXLEN, query,
90                 "SELECT rua_stat_tag FROM rua_panel_stat WHERE caller_panel = ? ORDER BY score DESC");
91
92         r = sqlite3_prepare(db, query, sizeof(query), &stmt, NULL);
93         if (r != SQLITE_OK) {
94                 LOGE("sqlite3_prepare error(%d , %d, %s)", r, sqlite3_extended_errcode(db), sqlite3_errmsg(db));
95                 goto out;
96         }
97
98         r = sqlite3_bind_text(stmt, 1, caller, strlen(caller), SQLITE_STATIC);
99         if (r != SQLITE_OK) {
100                 LOGE("caller bind error(%d) \n", r);
101                 goto out;
102         }
103
104         while (sqlite3_step(stmt) == SQLITE_ROW) {
105                 ct = sqlite3_column_text(stmt, 0);
106                 if (ct == NULL || ct[0] == '\0')
107                         LOGW("sqlite3_column_text null");
108
109                 rua_stat_tag_iter_fn((const char *)ct, data);
110         }
111
112 out:
113         if (stmt)
114                 sqlite3_finalize(stmt);
115
116         if (db)
117                 db_util_close(db);
118
119         return r;
120 }