Release version 0.5.14
[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 <sqlite3.h>
30 #include <aul.h>
31 #include <dlog.h>
32
33 #include "rua_util.h"
34 #include "rua_private.h"
35
36 API int rua_stat_update_for_uid(char *caller, char *tag, uid_t uid)
37 {
38         int r;
39         bundle *b = NULL;
40
41         r = _rua_util_check_uid(uid);
42         if (r == -1)
43                 return r;
44
45         if (caller == NULL || tag == NULL) {
46                 LOGE("invalid param");
47                 return -1;
48         }
49
50         b = bundle_create();
51         if (b == NULL) {
52                 LOGE("bundle_create fail out of memory.");
53                 return -1;
54         }
55         bundle_add_str(b, AUL_SVC_K_RUA_STAT_CALLER, caller);
56         bundle_add_str(b, AUL_SVC_K_RUA_STAT_TAG, tag);
57         r = aul_update_rua_stat_for_uid(b, uid);
58         LOGI("rua_add_history_for_uid result : %d ", r);
59         bundle_free(b);
60         return r;
61 }
62
63 static int __get_stat_tags_for_uid(sqlite3 *db, const char *caller,
64                 int (*rua_stat_tag_iter_fn)(
65                         const char *rua_stat_tag, void *data),
66                 void *data, uid_t uid)
67 {
68         static const char query[] =
69                 "SELECT rua_stat_tag FROM rua_panel_stat "
70                 "WHERE caller_panel=? ORDER BY score DESC";
71         int r;
72         sqlite3_stmt *stmt;
73         const unsigned char *ct;
74
75         r = sqlite3_prepare(db, query, strlen(query), &stmt, NULL);
76         if (r != SQLITE_OK) {
77                 LOGE("prepare failed: %s", sqlite3_errmsg(db));
78                 return -1;
79         }
80
81         __BIND_TEXT(db, stmt, 1, caller);
82
83         while (sqlite3_step(stmt) == SQLITE_ROW) {
84                 ct = sqlite3_column_text(stmt, 0);
85                 if (ct == NULL || ct[0] == '\0')
86                         LOGW("sqlite3_column_text null");
87
88                 rua_stat_tag_iter_fn((const char *)ct, data);
89         }
90
91         sqlite3_finalize(stmt);
92
93         return r;
94 }
95
96 API int rua_stat_get_stat_tags_for_uid(char *caller,
97                 int (*rua_stat_tag_iter_fn)(const char *rua_stat_tag, void *data),
98                 void *data, uid_t uid)
99 {
100         int r;
101         sqlite3 *db;
102
103         r = _rua_util_check_uid(uid);
104         if (r == -1)
105                 return r;
106
107         r = _rua_util_open_db(&db, SQLITE_OPEN_READONLY, uid, RUA_STAT_DB_NAME);
108         if (r == -1) {
109                 LOGE("open rua stat db failed");
110                 return -1;
111         }
112
113         r = __get_stat_tags_for_uid(db, caller, rua_stat_tag_iter_fn, data,
114                         uid);
115
116         sqlite3_close_v2(db);
117
118         return r;
119 }
120
121 API int rua_stat_get_stat_tags(char *caller,
122                 int (*rua_stat_tag_iter_fn)(const char *rua_stat_tag, void *data),
123                 void *data)
124 {
125         return rua_stat_get_stat_tags_for_uid(caller, rua_stat_tag_iter_fn, data, getuid());
126 }