Add apis for datacontrol privileges info
[platform/core/appfw/pkgmgr-info.git] / parser / src / pkgmgr_parser_db.c
1 /*
2  * Copyright (c) 2000 - 2017 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 #include <fcntl.h>
19 #include <stdio.h>
20 #include <stdbool.h>
21 #include <string.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <sys/smack.h>
25 #include <linux/limits.h>
26 #include <unistd.h>
27 #include <pwd.h>
28
29 #include <glib.h>
30 #include <sqlite3.h>
31
32 #include <tzplatform_config.h>
33 #include <system_info.h>
34
35 #include "pkgmgr-info.h"
36 #include "pkgmgrinfo_basic.h"
37 #include "pkgmgr_parser.h"
38 #include "pkgmgr_parser_db_queries.h"
39 #include "pkgmgr_parser_debug.h"
40 #include "pkgmgr_parser_internal.h"
41
42 #ifndef OWNER_ROOT
43 #define OWNER_ROOT 0
44 #endif
45 #ifndef GLOBAL_USER
46 #define GLOBAL_USER tzplatform_getuid(TZ_SYS_GLOBALAPP_USER)
47 #endif
48 #ifndef APPFW_USER
49 #define APPFW_USER "app_fw"
50 #endif
51
52 #define BUFSIZE 4096
53
54 #define LDPI "ldpi"
55 #define MDPI "mdpi"
56 #define HDPI "hdpi"
57 #define XHDPI "xhdpi"
58 #define XXHDPI "xxhdpi"
59
60 #define LDPI_MIN 0
61 #define LDPI_MAX 240
62 #define MDPI_MIN 241
63 #define MDPI_MAX 300
64 #define HDPI_MIN 301
65 #define HDPI_MAX 380
66 #define XHDPI_MIN 381
67 #define XHDPI_MAX 480
68 #define XXHDPI_MIN 481
69 #define XXHDPI_MAX 600
70
71 /* app background category value */
72 #define APP_BG_CATEGORY_USER_DISABLE_FALSE_VAL 0x00000
73 #define APP_BG_CATEGORY_USER_DISABLE_TRUE_VAL  0x00001
74 #define APP_BG_CATEGORY_MEDIA_VAL              0x00002
75 #define APP_BG_CATEGORY_DOWNLOAD_VAL           0x00004
76 #define APP_BG_CATEGORY_BGNETWORK_VAL          0x00008
77 #define APP_BG_CATEGORY_LOCATION_VAL           0x00010
78 #define APP_BG_CATEGORY_SENSOR_VAL             0x00020
79 #define APP_BG_CATEGORY_IOTCOMM_VAL            0x00040
80 #define APP_BG_CATEGORY_SYSTEM_VAL             0x00080
81
82 #define APP_BG_CATEGORY_USER_DISABLE_FALSE_STR "enable"
83 #define APP_BG_CATEGORY_USER_DISABLE_TRUE_STR  "disable"
84 #define APP_BG_CATEGORY_MEDIA_STR              "media"
85 #define APP_BG_CATEGORY_DOWNLOAD_STR           "download"
86 #define APP_BG_CATEGORY_BGNETWORK_STR          "background-network"
87 #define APP_BG_CATEGORY_LOCATION_STR           "location"
88 #define APP_BG_CATEGORY_SENSOR_STR             "sensor"
89 #define APP_BG_CATEGORY_IOTCOMM_STR            "iot-communication"
90 #define APP_BG_CATEGORY_SYSTEM                 "system"
91
92 #define REGULAR_USER 5000
93 static inline uid_t __getuid(void)
94 {
95         uid_t uid = getuid();
96
97         if (uid < REGULAR_USER)
98                 return tzplatform_getuid(TZ_SYS_GLOBALAPP_USER);
99         else
100                 return uid;
101 }
102
103 static const char *__get_bool(char *value, bool is_true)
104 {
105         if (value != NULL) {
106                 if (!strcmp(value, ""))
107                         return (is_true) ? "true" : "false";
108                 return value;
109         }
110
111         return (is_true) ? "true" : "false";
112 }
113
114 #define __BEGIN_TRANSACTION(db)                                                \
115 do {                                                                           \
116         if (sqlite3_exec(db, "BEGIN EXCLUSIVE", NULL, NULL, NULL) !=           \
117                         SQLITE_OK) {                                           \
118                 _LOGE("begin transaction failed: %s", sqlite3_errmsg(db));     \
119                 sqlite3_close_v2(db);                                          \
120                 return PM_PARSER_R_ERROR;                                      \
121         }                                                                      \
122 } while (0)                                                                    \
123
124 #define __DO_TRANSACTION(db, func)                                             \
125 do {                                                                           \
126         if (func) {                                                            \
127                 _LOGE("transaction failed: %s, rollback", sqlite3_errmsg(db)); \
128                 if (sqlite3_exec(db, "ROLLBACK", NULL, NULL, NULL) !=          \
129                                 SQLITE_OK)                                     \
130                         _LOGE("roll back transaction failed: %s",              \
131                                         sqlite3_errmsg(db));                   \
132                 sqlite3_close_v2(db);                                          \
133                 return PM_PARSER_R_ERROR;                                      \
134         }                                                                      \
135 } while (0)                                                                    \
136
137 #define __END_TRANSACTION(db)                                                  \
138 do {                                                                           \
139         if (sqlite3_exec(db, "COMMIT", NULL, NULL, NULL) !=                    \
140                         SQLITE_OK) {                                           \
141                 _LOGE("commit failed: %s, rollback", sqlite3_errmsg(db));      \
142                 if (sqlite3_exec(db, "ROLLBACK", NULL, NULL, NULL) !=          \
143                                 SQLITE_OK)                                     \
144                         _LOGE("roll back transaction failed: %s",              \
145                                         sqlite3_errmsg(db));                   \
146                 sqlite3_close_v2(db);                                          \
147                 return PM_PARSER_R_ERROR;                                      \
148         }                                                                      \
149 } while (0)                                                                    \
150
151 #define __BIND_TEXT(db, stmt, i, text)                                         \
152 do {                                                                           \
153         if (sqlite3_bind_text(stmt, i, text, -1, SQLITE_STATIC) != SQLITE_OK) {\
154                 _LOGE("bind error(index %d): %s", i, sqlite3_errmsg(db));      \
155                 sqlite3_finalize(stmt);                                        \
156                 return -1;                                                     \
157         }                                                                      \
158 } while (0)
159
160 #define __BIND_INT(db, stmt, i, int)                                           \
161 do {                                                                           \
162         if (sqlite3_bind_int(stmt, i, int) != SQLITE_OK) {                     \
163                 _LOGE("bind error(index %d): %s", i, sqlite3_errmsg(db));      \
164                 sqlite3_finalize(stmt);                                        \
165                 return -1;                                                     \
166         }                                                                      \
167 } while (0)
168
169 static const char *__get_parser_db_path(uid_t uid)
170 {
171         char buf[PATH_MAX];
172         const char *path;
173
174         if (uid == GLOBAL_USER || uid == OWNER_ROOT) {
175                 path = tzplatform_mkpath(TZ_SYS_DB, ".pkgmgr_parser.db");
176         } else {
177                 snprintf(buf, sizeof(buf), "user/%d/.pkgmgr_parser.db", uid);
178                 path = tzplatform_mkpath(TZ_SYS_DB, buf);
179         }
180
181         return path;
182 }
183
184 static const char *__get_cert_db_path(void)
185 {
186         return tzplatform_mkpath(TZ_SYS_DB, ".pkgmgr_cert.db");
187 }
188
189 static int __set_db_version(sqlite3 *db)
190 {
191         static const char query_raw[] = "PRAGMA user_version=";
192         char query[BUFSIZE];
193         int ret;
194         int version;
195
196         version = atoi(TIZEN_MAJOR_VER) * 10000 + atoi(TIZEN_MINOR_VER) * 100 +
197                 atoi(TIZEN_PATCH_VER);
198         snprintf(query, sizeof(query), "%s%d", query_raw, version);
199
200         ret = sqlite3_exec(db, query, NULL, NULL, NULL);
201         if (ret != SQLITE_OK) {
202                 _LOGE("exec failed: %s", sqlite3_errmsg(db));
203                 return -1;
204         }
205
206         return 0;
207 }
208
209 /* TODO: Do not labeling directly */
210 #define DB_LABEL "User::Home"
211 #define SET_SMACK_LABEL(x)                                                     \
212 do {                                                                           \
213         if (smack_setlabel((x), DB_LABEL, SMACK_LABEL_ACCESS))                 \
214                 _LOGE("failed chsmack -a %s %s", DB_LABEL, x);                 \
215         else                                                                   \
216                 _LOGD("chsmack -a %s %s", DB_LABEL, x);                        \
217 } while (0)
218
219 static int __set_db_permission(const char *path, uid_t uid)
220 {
221         int fd;
222         const char *files[2];
223         char journal_file[BUFSIZE];
224         struct stat sb;
225         mode_t mode;
226         struct passwd pwd;
227         struct passwd *result;
228         char buf[BUFSIZE];
229         int ret;
230         int i;
231
232         if (getuid() != OWNER_ROOT)
233                 return 0;
234
235         if (uid == OWNER_ROOT || uid == GLOBAL_USER) {
236                 ret = getpwnam_r(APPFW_USER, &pwd, buf, sizeof(buf), &result);
237                 if (result == NULL) {
238                         if (ret == 0)
239                                 _LOGE("no such user: %d", uid);
240                         else
241                                 _LOGE("getpwuid_r failed: %d", errno);
242                         return -1;
243                 }
244                 uid = pwd.pw_uid;
245         }
246
247         snprintf(journal_file, sizeof(journal_file), "%s-journal", path);
248         files[0] = path;
249         files[1] = journal_file;
250
251         ret = getpwuid_r(uid, &pwd, buf, sizeof(buf), &result);
252         if (result == NULL) {
253                 if (ret == 0)
254                         _LOGE("no such user: %d", uid);
255                 else
256                         _LOGE("getpwuid_r failed: %d", errno);
257                 return -1;
258         }
259
260         for (i = 0; i < 2; i++) {
261                 fd = open(files[i], O_RDONLY);
262                 if (fd == -1) {
263                         _LOGE("open %s failed: %d", files[i], errno);
264                         return -1;
265                 }
266                 ret = fstat(fd, &sb);
267                 if (ret == -1) {
268                         _LOGE("stat %s failed: %d", files[i], errno);
269                         close(fd);
270                         return -1;
271                 }
272                 if (S_ISLNK(sb.st_mode)) {
273                         _LOGE("%s is symlink!", files[i]);
274                         close(fd);
275                         return -1;
276                 }
277                 ret = fchown(fd, uid, pwd.pw_gid);
278                 if (ret == -1) {
279                         _LOGE("fchown %s failed: %d", files[i], errno);
280                         close(fd);
281                         return -1;
282                 }
283
284                 mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH;
285                 if (!strcmp(path, __get_cert_db_path()))
286                         mode |= S_IWOTH;
287                 ret = fchmod(fd, mode);
288                 if (ret == -1) {
289                         _LOGD("fchmod %s failed: %d", files[i], errno);
290                         close(fd);
291                         return -1;
292                 }
293                 close(fd);
294                 SET_SMACK_LABEL(files[i]);
295         }
296
297         return 0;
298 }
299
300 static const char *parser_init_queries[] = {
301         QUERY_CREATE_TABLE_PACKAGE_INFO,
302         QUERY_CREATE_TABLE_PACKAGE_LOCALIZED_INFO,
303         QUERY_CREATE_TABLE_PACKAGE_PRIVILEGE_INFO,
304         QUERY_CREATE_TABLE_PACKAGE_UPDATE_INFO,
305         QUERY_CREATE_TABLE_PACKAGE_APP_INFO,
306         QUERY_CREATE_TABLE_PACKAGE_APP_LOCALIZED_INFO,
307         QUERY_CREATE_TABLE_PACKAGE_APP_ICON_SECTION_INFO, /* ? */
308         QUERY_CREATE_TABLE_PACKAGE_APP_IMAGE_INFO, /* ? */
309         QUERY_CREATE_TABLE_PACKAGE_APP_APP_CONTROL,
310         QUERY_CREATE_TABLE_PACKAGE_APP_APP_CATEGORY,
311         QUERY_CREATE_TABLE_PACKAGE_APP_APP_METADATA,
312         QUERY_CREATE_TABLE_PACKAGE_APP_APP_PERMISSION, /* ? */
313         QUERY_CREATE_TABLE_PACKAGE_APP_SHARE_ALLOWED, /* ? */
314         QUERY_CREATE_TABLE_PACKAGE_APP_SHARE_REQUEST, /* ? */
315         QUERY_CREATE_TABLE_PACKAGE_APP_DATA_CONTROL,
316         QUERY_CREATE_TABLE_PACKAGE_APP_DATA_CONTROL_PRIVILEGE,
317         QUERY_CREATE_TABLE_PACKAGE_APP_INFO_FOR_UID,
318         QUERY_CREATE_TRIGGER_UPDATE_PACKAGE_APP_INFO_FOR_UID,
319         QUERY_CREATE_TABLE_PACKAGE_APP_SPLASH_SCREEN,
320         NULL,
321 };
322
323 static const char *cert_init_queries[] = {
324         QUERY_CREATE_TABLE_PACKAGE_CERT_INFO,
325         QUERY_CREATE_TABLE_PACKAGE_CERT_INDEX_INFO,
326         QUERY_CREATE_TRIGGER_UPDATE_CERT_INFO,
327         QUERY_CREATE_TRIGGER_UPDATE_CERT_INFO2,
328         QUERY_CREATE_TRIGGER_DELETE_CERT_INFO,
329         QUERY_CREATE_TRIGGER_UPDATE_CERT_INDEX_INFO,
330         NULL
331 };
332
333 static int __initialize_db(sqlite3 *db, const char *dbpath, uid_t uid)
334 {
335         int ret;
336         const char **queries;
337         int i;
338
339         if (__set_db_version(db))
340                 return -1;
341
342         if (strstr(dbpath, ".pkgmgr_parser.db")) {
343                 queries = parser_init_queries;
344         } else if (strstr(dbpath, ".pkgmgr_cert.db")) {
345                 queries = cert_init_queries;
346         } else {
347                 _LOGE("unexpected dbpath: %s", dbpath);
348                 return -1;
349         }
350
351         for (i = 0; queries[i] != NULL; i++) {
352                 ret = sqlite3_exec(db, queries[i], NULL, NULL, NULL);
353                 if (ret != SQLITE_OK) {
354                         _LOGE("exec failed: %s", sqlite3_errmsg(db));
355                         return -1;
356                 }
357         }
358
359         if (__set_db_permission(dbpath, uid))
360                 _LOGE("failed to set db permission");
361
362         return 0;
363 }
364
365 API int pkgmgr_parser_initialize_parser_db(uid_t uid)
366 {
367         int ret;
368         const char *dbpath;
369         sqlite3 *db;
370
371         dbpath = __get_parser_db_path(uid);
372         if (access(dbpath, F_OK) != -1) {
373                 _LOGE("Manifest db for user %d is already exists", uid);
374                 return PM_PARSER_R_ERROR;
375         }
376
377         ret = sqlite3_open_v2(dbpath, &db,
378                         SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
379         if (ret != SQLITE_OK) {
380                 _LOGE("open db failed: %d", ret);
381                 return PM_PARSER_R_ERROR;
382         }
383
384         if (__initialize_db(db, dbpath, uid)) {
385                 sqlite3_close_v2(db);
386                 return PM_PARSER_R_ERROR;
387         }
388         sqlite3_close_v2(db);
389
390         return PM_PARSER_R_OK;
391 }
392
393 API int pkgmgr_parser_initialize_cert_db(void)
394 {
395         int ret;
396         const char *dbpath;
397         sqlite3 *db;
398
399         dbpath = __get_cert_db_path();
400         if (access(dbpath, F_OK) != -1) {
401                 _LOGE("Cert db is already exists");
402                 return PM_PARSER_R_ERROR;
403         }
404
405         ret = sqlite3_open_v2(dbpath, &db,
406                         SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
407         if (ret != SQLITE_OK) {
408                 _LOGE("open db failed: %d", ret);
409                 return PM_PARSER_R_ERROR;
410         }
411
412         if (__initialize_db(db, dbpath, GLOBAL_USER)) {
413                 sqlite3_close_v2(db);
414                 return PM_PARSER_R_ERROR;
415         }
416         sqlite3_close_v2(db);
417
418         return PM_PARSER_R_OK;
419 }
420
421 API int pkgmgr_parser_create_and_initialize_db(uid_t uid)
422 {
423         int ret;
424         struct passwd pwd;
425         struct passwd *result;
426         char buf[BUFSIZE];
427
428         ret = getpwnam_r(APPFW_USER, &pwd, buf, sizeof(buf), &result);
429         if (result == NULL) {
430                 if (ret == 0)
431                         _LOGE("no such user: %s", APPFW_USER);
432                 else
433                         _LOGE("getpwnam_r failed: %d", errno);
434                 return PM_PARSER_R_ERROR;
435         }
436
437         if (getuid() != OWNER_ROOT && getuid() != pwd.pw_uid) {
438                 _LOGE("Only root or app_fw user is allowed");
439                 return PM_PARSER_R_EINVAL;
440         }
441
442         if (pkgmgr_parser_initialize_parser_db(uid))
443                 return PM_PARSER_R_ERROR;
444
445         if (pkgmgr_parser_initialize_cert_db())
446                 return PM_PARSER_R_ERROR;
447
448         return PM_PARSER_R_OK;
449 }
450
451 #define BUSY_WAITING_USEC (1000000 / 10 / 2) /* 0.05 sec */
452 #define BUSY_WAITING_MAX 20 /* wait for max 1 sec */
453 static int __db_busy_handler(void *data, int count)
454 {
455         if (count < BUSY_WAITING_MAX) {
456                 usleep(BUSY_WAITING_USEC);
457                 return 1;
458         } else {
459                 /* sqlite3_prepare_v2 will return SQLITE_BUSY */
460                 return 0;
461         }
462 }
463
464 static int __open_db(uid_t uid, const char *path, sqlite3 **db, int flags)
465 {
466         int ret;
467
468         /* FIXME: always open with OPEN_CREATE flag for keeping previous
469          * implementation
470          */
471         if (flags & SQLITE_OPEN_READWRITE)
472                 flags = flags | SQLITE_OPEN_CREATE;
473
474         ret = sqlite3_open_v2(path, db, flags, NULL);
475         if (ret != SQLITE_OK)
476                 return ret;
477
478         ret = sqlite3_busy_handler(*db, __db_busy_handler, NULL);
479         if (ret != SQLITE_OK) {
480                 _LOGE("failed to register busy handler: %s",
481                                 sqlite3_errmsg(*db));
482                 sqlite3_close_v2(*db);
483                 return ret;
484         }
485
486         if (flags & SQLITE_OPEN_CREATE) {
487                 ret = __initialize_db(*db, path, uid);
488                 if (ret) {
489                         _LOGE("failed to initialize db: %s\n");
490                         sqlite3_close_v2(*db);
491                         return -1;
492                 }
493         }
494
495         ret = sqlite3_exec(*db, "PRAGMA foreign_keys=ON", NULL, NULL, NULL);
496         if (ret != SQLITE_OK) {
497                 _LOGE("failed to enable foreign key support: %s",
498                                 sqlite3_errmsg(*db));
499                 sqlite3_close_v2(*db);
500                 return ret;
501         }
502
503         return ret;
504 }
505
506
507 static int __convert_background_category(GList *category_list)
508 {
509         int ret = 0;
510         GList *tmp;
511         char *category_data;
512
513         if (category_list == NULL)
514                 return 0;
515
516         for (tmp = category_list; tmp; tmp = tmp->next) {
517                 category_data = (char *)tmp->data;
518                 if (category_data == NULL)
519                         continue;
520                 if (!strcmp(category_data, APP_BG_CATEGORY_MEDIA_STR))
521                         ret |= APP_BG_CATEGORY_MEDIA_VAL;
522                 else if (!strcmp(category_data, APP_BG_CATEGORY_DOWNLOAD_STR))
523                         ret |= APP_BG_CATEGORY_DOWNLOAD_VAL;
524                 else if (!strcmp(category_data, APP_BG_CATEGORY_BGNETWORK_STR))
525                         ret |= APP_BG_CATEGORY_BGNETWORK_VAL;
526                 else if (!strcmp(category_data, APP_BG_CATEGORY_LOCATION_STR))
527                         ret |= APP_BG_CATEGORY_LOCATION_VAL;
528                 else if (!strcmp(category_data, APP_BG_CATEGORY_SENSOR_STR))
529                         ret |= APP_BG_CATEGORY_SENSOR_VAL;
530                 else if (!strcmp(category_data, APP_BG_CATEGORY_IOTCOMM_STR))
531                         ret |= APP_BG_CATEGORY_IOTCOMM_VAL;
532                 else if (!strcmp(category_data, APP_BG_CATEGORY_SYSTEM))
533                         ret |= APP_BG_CATEGORY_SYSTEM_VAL;
534                 else
535                         _LOGE("Unidentified category [%s]", category_data);
536         }
537
538         return ret;
539 }
540
541 #define EFFECTIVE_APPID_KEY "http://tizen.org/metadata/effective-appid"
542 static const char *__find_effective_appid(GList *metadata_list)
543 {
544         GList *tmp;
545         metadata_x *md;
546
547         for (tmp = metadata_list; tmp; tmp = tmp->next) {
548                 md = (metadata_x *)tmp->data;
549                 if (md == NULL || md->key == NULL)
550                         continue;
551
552                 if (strcmp(md->key, EFFECTIVE_APPID_KEY) == 0) {
553                         if (md->value)
554                                 return md->value;
555                 }
556         }
557
558         return NULL;
559 }
560
561 static int __insert_appcontrol_info(sqlite3 *db, application_x *app)
562 {
563         static const char query[] =
564                 "INSERT INTO package_app_app_control (app_id, app_control) "
565                 "VALUES (?, ?)";
566         int ret;
567         sqlite3_stmt *stmt;
568         int idx;
569         char app_control[BUFSIZE];
570         GList *tmp;
571         appcontrol_x *ac;
572
573         if (app->appcontrol == NULL)
574                 return 0;
575
576         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
577         if (ret != SQLITE_OK) {
578                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
579                 return -1;
580         }
581
582         for (tmp = app->appcontrol; tmp; tmp = tmp->next) {
583                 ac = (appcontrol_x *)tmp->data;
584                 if (ac == NULL)
585                         continue;
586                 idx = 1;
587                 snprintf(app_control, sizeof(app_control), "%s|%s|%s",
588                                 ac->operation ? (strlen(ac->operation) > 0 ?
589                                         ac->operation : "NULL") : "NULL",
590                                 ac->uri ? (strlen(ac->uri) > 0 ?
591                                         ac->uri : "NULL") : "NULL",
592                                 ac->mime ? (strlen(ac->mime) > 0 ?
593                                         ac->mime : "NULL") : "NULL");
594                 __BIND_TEXT(db, stmt, idx++, app->appid);
595                 __BIND_TEXT(db, stmt, idx++, app_control);
596
597                 ret = sqlite3_step(stmt);
598                 if (ret != SQLITE_DONE) {
599                         _LOGE("step failed: %s", sqlite3_errmsg(db));
600                         sqlite3_finalize(stmt);
601                         return -1;
602                 }
603
604                 sqlite3_reset(stmt);
605         }
606
607         sqlite3_finalize(stmt);
608
609         return 0;
610 }
611
612 static int __insert_category_info(sqlite3 *db, application_x *app)
613 {
614         static const char query[] =
615                 "INSERT INTO package_app_app_category (app_id, category) "
616                 "VALUES (?, ?)";
617         int ret;
618         sqlite3_stmt *stmt;
619         int idx;
620         GList *tmp;
621         const char *category;
622
623         if (app->category == NULL)
624                 return 0;
625
626         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
627         if (ret != SQLITE_OK) {
628                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
629                 return -1;
630         }
631
632         for (tmp = app->category; tmp; tmp = tmp->next) {
633                 category = (const char *)tmp->data;
634                 if (category == NULL)
635                         continue;
636                 idx = 1;
637                 __BIND_TEXT(db, stmt, idx++, app->appid);
638                 __BIND_TEXT(db, stmt, idx++, category);
639
640                 ret = sqlite3_step(stmt);
641                 if (ret != SQLITE_DONE) {
642                         _LOGE("step failed: %s", sqlite3_errmsg(db));
643                         sqlite3_finalize(stmt);
644                         return -1;
645                 }
646
647                 sqlite3_reset(stmt);
648         }
649
650         sqlite3_finalize(stmt);
651
652         return 0;
653 }
654
655 static int __insert_metadata_info(sqlite3 *db, application_x *app)
656 {
657         static const char query[] =
658                 "INSERT INTO package_app_app_metadata (app_id,"
659                 "  md_key, md_value) VALUES (?, ?, ?)";
660         int ret;
661         sqlite3_stmt *stmt;
662         int idx;
663         GList *tmp;
664         metadata_x *md;
665
666         if (app->metadata == NULL)
667                 return 0;
668
669         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
670         if (ret != SQLITE_OK) {
671                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
672                 return -1;
673         }
674
675         for (tmp = app->metadata; tmp; tmp = tmp->next) {
676                 md = (metadata_x *)tmp->data;
677                 if (md == NULL)
678                         continue;
679                 idx = 1;
680                 __BIND_TEXT(db, stmt, idx++, app->appid);
681                 __BIND_TEXT(db, stmt, idx++, md->key);
682                 __BIND_TEXT(db, stmt, idx++, md->value);
683
684                 ret = sqlite3_step(stmt);
685                 if (ret != SQLITE_DONE) {
686                         _LOGE("step failed: %s", sqlite3_errmsg(db));
687                         sqlite3_finalize(stmt);
688                         return -1;
689                 }
690
691                 sqlite3_reset(stmt);
692         }
693
694         sqlite3_finalize(stmt);
695
696         return 0;
697 }
698
699 static int __insert_app_data_control_privilege_info(sqlite3 *db,
700                 datacontrol_x *datacontrol)
701 {
702         static const char query[] =
703                 "INSERT INTO package_app_data_control_privilege (providerid,"
704                 "  privilege, type) VALUES (?, ?, ?)";
705
706         int ret;
707         sqlite3_stmt *stmt;
708         int idx;
709         GList *privileges;
710
711         if (datacontrol == NULL)
712                 return 0;
713
714         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
715         if (ret != SQLITE_OK) {
716                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
717                 return -1;
718         }
719
720         for (privileges = datacontrol->privileges; privileges;
721                         privileges = privileges->next) {
722                 privilege_x *priv = (privilege_x *)privileges->data;
723                 if (priv == NULL)
724                         continue;
725
726                 idx = 1;
727                 __BIND_TEXT(db, stmt, idx++, datacontrol->providerid);
728                 __BIND_TEXT(db, stmt, idx++, priv->value);
729                 __BIND_TEXT(db, stmt, idx++, datacontrol->type);
730
731                 ret = sqlite3_step(stmt);
732                 if (ret != SQLITE_DONE) {
733                         _LOGE("step failed: %s", sqlite3_errmsg(db));
734                         sqlite3_finalize(stmt);
735                         return -1;
736                 }
737
738                 sqlite3_reset(stmt);
739         }
740
741         sqlite3_finalize(stmt);
742         return 0;
743 }
744
745 static int __insert_datacontrol_info(sqlite3 *db, application_x *app)
746 {
747         static const char query[] =
748                 "INSERT INTO package_app_data_control (app_id, providerid,"
749                 "  access, type, trusted) VALUES (?, ?, ?, ?, ?)";
750         int ret;
751         sqlite3_stmt *stmt;
752         int idx;
753         GList *tmp;
754         datacontrol_x *dc;
755
756         if (app->datacontrol == NULL)
757                 return 0;
758
759         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
760         if (ret != SQLITE_OK) {
761                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
762                 return -1;
763         }
764
765         for (tmp = app->datacontrol; tmp; tmp = tmp->next) {
766                 dc = (datacontrol_x *)tmp->data;
767                 if (dc == NULL)
768                         continue;
769                 idx = 1;
770                 __BIND_TEXT(db, stmt, idx++, app->appid);
771                 __BIND_TEXT(db, stmt, idx++, dc->providerid);
772                 __BIND_TEXT(db, stmt, idx++, dc->access);
773                 __BIND_TEXT(db, stmt, idx++, dc->type);
774                 __BIND_TEXT(db, stmt, idx++, dc->trusted);
775
776                 ret = sqlite3_step(stmt);
777                 if (ret != SQLITE_DONE) {
778                         _LOGE("step failed: %s", sqlite3_errmsg(db));
779                         sqlite3_finalize(stmt);
780                         return -1;
781                 }
782
783                 if (dc->privileges &&
784                                 __insert_app_data_control_privilege_info(db, dc)) {
785                         sqlite3_finalize(stmt);
786                         return -1;
787                 }
788
789                 sqlite3_reset(stmt);
790         }
791
792         sqlite3_finalize(stmt);
793
794         return 0;
795 }
796
797 /* TODO: move to installer */
798 static int __check_dpi(const char *dpi_char, int dpi_int)
799 {
800         if (dpi_char == NULL)
801                 return -1;
802
803         if (strcasecmp(dpi_char, LDPI) == 0) {
804                 if (dpi_int >= LDPI_MIN && dpi_int <= LDPI_MAX)
805                         return 0;
806                 else
807                         return -1;
808         } else if (strcasecmp(dpi_char, MDPI) == 0) {
809                 if (dpi_int >= MDPI_MIN && dpi_int <= MDPI_MAX)
810                         return 0;
811                 else
812                         return -1;
813         } else if (strcasecmp(dpi_char, HDPI) == 0) {
814                 if (dpi_int >= HDPI_MIN && dpi_int <= HDPI_MAX)
815                         return 0;
816                 else
817                         return -1;
818         } else if (strcasecmp(dpi_char, XHDPI) == 0) {
819                 if (dpi_int >= XHDPI_MIN && dpi_int <= XHDPI_MAX)
820                         return 0;
821                 else
822                         return -1;
823         } else if (strcasecmp(dpi_char, XXHDPI) == 0) {
824                 if (dpi_int >= XXHDPI_MIN && dpi_int <= XXHDPI_MAX)
825                         return 0;
826                 else
827                         return -1;
828         } else
829                 return -1;
830 }
831
832 static gint __compare_splashscreen_with_orientation_dpi(gconstpointer a,
833                 gconstpointer b)
834 {
835         splashscreen_x *ss = (splashscreen_x *)a;
836         const char *orientation = (const char *)b;
837         int dpi = -1;
838         int ret;
839
840         if (ss->operation || ss->dpi == NULL)
841                 return -1;
842
843         ret = system_info_get_platform_int(
844                         "http://tizen.org/feature/screen.dpi", &dpi);
845         if (ret != SYSTEM_INFO_ERROR_NONE)
846                 return -1;
847
848         if (strcasecmp(ss->orientation, orientation) == 0 &&
849                         __check_dpi(ss->dpi, dpi) == 0)
850                 return 0;
851
852         return -1;
853 }
854
855 static gint __compare_splashscreen_with_orientation(gconstpointer a,
856                 gconstpointer b)
857 {
858         splashscreen_x *ss = (splashscreen_x *)a;
859         const char *orientation = (const char *)b;
860
861         if (ss->operation || ss->dpi)
862                 return -1;
863
864         if (strcasecmp(ss->orientation, orientation) == 0)
865                 return 0;
866
867         return -1;
868 }
869
870 static splashscreen_x *__find_default_splashscreen(GList *splashscreens,
871                 const char *orientation)
872 {
873         GList *tmp;
874
875         tmp = g_list_find_custom(splashscreens, orientation,
876                         (GCompareFunc)
877                         __compare_splashscreen_with_orientation_dpi);
878         if (tmp)
879                 return (splashscreen_x *)tmp->data;
880
881         tmp = g_list_find_custom(splashscreens, orientation,
882                         (GCompareFunc)__compare_splashscreen_with_orientation);
883         if (tmp)
884                 return (splashscreen_x *)tmp->data;
885
886         return NULL;
887 }
888
889 static void __find_appcontrol_splashscreen_with_dpi(gpointer data,
890                 gpointer user_data)
891 {
892         splashscreen_x *ss = (splashscreen_x *)data;
893         GList **list = (GList **)user_data;
894         int dpi = -1;
895         int ret;
896
897         if (ss->operation == NULL || ss->dpi == NULL)
898                 return;
899
900         ret = system_info_get_platform_int(
901                         "http://tizen.org/feature/screen.dpi", &dpi);
902         if (ret != SYSTEM_INFO_ERROR_NONE)
903                 return;
904
905         if (__check_dpi(ss->dpi, dpi) != 0)
906                 return;
907
908         *list = g_list_append(*list, ss);
909 }
910
911 static void __find_appcontrol_splashscreen(gpointer data, gpointer user_data)
912 {
913         splashscreen_x *ss = (splashscreen_x *)data;
914         GList **list = (GList **)user_data;
915         splashscreen_x *ss_tmp;
916         GList *tmp;
917
918         if (ss->operation == NULL || ss->dpi)
919                 return;
920
921         for (tmp = *list; tmp; tmp = tmp->next) {
922                 ss_tmp = (splashscreen_x *)tmp->data;
923                 if (ss_tmp->operation
924                         && strcmp(ss_tmp->operation, ss->operation) == 0
925                         && strcmp(ss_tmp->orientation, ss->orientation) == 0)
926                         return;
927         }
928
929         *list = g_list_append(*list, ss);
930 }
931
932 static GList *__find_splashscreens(GList *splashscreens)
933 {
934         GList *list = NULL;
935         splashscreen_x *ss;
936
937         g_list_foreach(splashscreens,
938                         __find_appcontrol_splashscreen_with_dpi, &list);
939         g_list_foreach(splashscreens,
940                         __find_appcontrol_splashscreen, &list);
941
942         ss = __find_default_splashscreen(splashscreens, "portrait");
943         if (ss)
944                 list = g_list_append(list, ss);
945         ss = __find_default_splashscreen(splashscreens, "landscape");
946         if (ss)
947                 list = g_list_append(list, ss);
948
949         return list;
950 }
951
952 static int __insert_splashscreen_info(sqlite3 *db, application_x *app)
953 {
954         static const char query[] =
955                 "INSERT INTO package_app_splash_screen (app_id, src, type,"
956                 "  orientation, indicatordisplay, operation, color_depth) "
957                 "VALUES (?, ?, ?, ?, ?, ?, ?)";
958         int ret;
959         sqlite3_stmt *stmt;
960         int idx;
961         GList *tmp;
962         splashscreen_x *ss;
963         GList *ss_list;
964
965         if (app->splashscreens == NULL)
966                 return 0;
967
968         ss_list = __find_splashscreens(app->splashscreens);
969         if (ss_list == NULL)
970                 return 0;
971
972         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
973         if (ret != SQLITE_OK) {
974                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
975                 return -1;
976         }
977
978         for (tmp = ss_list; tmp; tmp = tmp->next) {
979                 ss = (splashscreen_x *)tmp->data;
980                 if (ss == NULL)
981                         continue;
982                 idx = 1;
983                 __BIND_TEXT(db, stmt, idx++, app->appid);
984                 __BIND_TEXT(db, stmt, idx++, ss->src);
985                 __BIND_TEXT(db, stmt, idx++, ss->type);
986                 __BIND_TEXT(db, stmt, idx++, ss->orientation);
987                 __BIND_TEXT(db, stmt, idx++, ss->indicatordisplay);
988                 __BIND_TEXT(db, stmt, idx++, ss->operation);
989                 __BIND_TEXT(db, stmt, idx++, ss->color_depth);
990
991                 ret = sqlite3_step(stmt);
992                 if (ret != SQLITE_DONE) {
993                         _LOGE("step failed: %s", sqlite3_errmsg(db));
994                         sqlite3_finalize(stmt);
995                         return -1;
996                 }
997
998                 sqlite3_reset(stmt);
999         }
1000
1001         sqlite3_finalize(stmt);
1002
1003         return 0;
1004 }
1005
1006 static void __trimfunc(GList *trim_list)
1007 {
1008         char *trim_data;
1009         char *prev = NULL;
1010         GList *list = g_list_first(trim_list);
1011
1012         while (list) {
1013                 trim_data = (char *)list->data;
1014                 if (trim_data) {
1015                         if (prev) {
1016                                 if (strcmp(trim_data, prev) == 0) {
1017                                         trim_list = g_list_remove(trim_list,
1018                                                         trim_data);
1019                                         list = g_list_first(trim_list);
1020                                         prev = NULL;
1021                                         continue;
1022                                 } else
1023                                         prev = trim_data;
1024                         } else {
1025                                 prev = trim_data;
1026                         }
1027                 }
1028                 list = g_list_next(list);
1029         }
1030 }
1031
1032 static gint __comparefunc(gconstpointer a, gconstpointer b, gpointer userdata)
1033 {
1034         if (a == NULL || b == NULL)
1035                 return 0;
1036         if (strcmp((char *)a, (char *)b) == 0)
1037                 return 0;
1038         if (strcmp((char *)a, (char *)b) < 0)
1039                 return -1;
1040         if (strcmp((char *)a, (char *)b) > 0)
1041                 return 1;
1042         return 0;
1043 }
1044
1045 /* TODO: refactor inserting localized info */
1046 static GList *__create_locale_list(GList *lbls, GList *lcns, GList *icns,
1047                 GList *dcns, GList *aths)
1048 {
1049         GList *locale = NULL;
1050         GList *tmp;
1051         label_x *lbl;
1052         license_x *lcn;
1053         icon_x *icn;
1054         description_x *dcn;
1055         author_x *ath;
1056
1057         for (tmp = lbls; tmp; tmp = tmp->next) {
1058                 lbl = (label_x *)tmp->data;
1059                 if (lbl == NULL)
1060                         continue;
1061                 if (lbl->lang)
1062                         locale = g_list_insert_sorted_with_data(
1063                                         locale, (gpointer)lbl->lang,
1064                                         __comparefunc, NULL);
1065         }
1066         for (tmp = lcns; tmp; tmp = tmp->next) {
1067                 lcn = (license_x *)tmp->data;
1068                 if (lcn == NULL)
1069                         continue;
1070                 if (lcn->lang)
1071                         locale = g_list_insert_sorted_with_data(
1072                                         locale, (gpointer)lcn->lang,
1073                                         __comparefunc, NULL);
1074         }
1075         for (tmp = icns; tmp; tmp = tmp->next) {
1076                 icn = (icon_x *)tmp->data;
1077                 if (icn == NULL)
1078                         continue;
1079                 if (icn->lang)
1080                         locale = g_list_insert_sorted_with_data(
1081                                         locale, (gpointer)icn->lang,
1082                                         __comparefunc, NULL);
1083         }
1084         for (tmp = dcns; tmp; tmp = tmp->next) {
1085                 dcn = (description_x *)tmp->data;
1086                 if (dcn == NULL)
1087                         continue;
1088                 if (dcn->lang)
1089                         locale = g_list_insert_sorted_with_data(
1090                                         locale, (gpointer)dcn->lang,
1091                                         __comparefunc, NULL);
1092         }
1093         for (tmp = aths; tmp; tmp = tmp->next) {
1094                 ath = (author_x *)tmp->data;
1095                 if (ath == NULL)
1096                         continue;
1097                 if (ath->lang)
1098                         locale = g_list_insert_sorted_with_data(
1099                                         locale, (gpointer)ath->lang,
1100                                         __comparefunc, NULL);
1101         }
1102         __trimfunc(locale);
1103         return locale;
1104 }
1105
1106 static gint __check_icon_resolution(const char *orig_icon_path,
1107                 char **new_icon_path)
1108 {
1109         int ret;
1110         char *dpi_path[2];
1111         char *icon_filename;
1112         char modified_iconpath[BUFSIZE];
1113         char icon_path[BUFSIZE];
1114         int i;
1115         int dpi = -1;
1116
1117         if (orig_icon_path == NULL)
1118                 return -1;
1119
1120         ret = system_info_get_platform_int(
1121                         "http://tizen.org/feature/screen.dpi", &dpi);
1122         if (ret != SYSTEM_INFO_ERROR_NONE)
1123                 return -1;
1124
1125         if (dpi >= LDPI_MIN && dpi <= LDPI_MAX) {
1126                 dpi_path[0] = "LDPI";
1127                 dpi_path[1] = "ldpi";
1128         } else if (dpi >= MDPI_MIN && dpi <= MDPI_MAX) {
1129                 dpi_path[0] = "MDPI";
1130                 dpi_path[1] = "mdpi";
1131         } else if (dpi >= HDPI_MIN && dpi <= HDPI_MAX) {
1132                 dpi_path[0] = "HDPI";
1133                 dpi_path[1] = "hdpi";
1134         } else if (dpi >= XHDPI_MIN && dpi <= XHDPI_MAX) {
1135                 dpi_path[0] = "XHDPI";
1136                 dpi_path[1] = "xhdpi";
1137         } else if (dpi >= XXHDPI_MIN && dpi <= XXHDPI_MAX) {
1138                 dpi_path[0] = "XXHDPI";
1139                 dpi_path[1] = "xxhdpi";
1140         } else {
1141                 _LOGE("Unidentified dpi[%d]", dpi);
1142                 return -1;
1143         }
1144
1145         icon_filename = strrchr(orig_icon_path, '/');
1146         if (icon_filename == NULL)
1147                 return -1;
1148
1149         snprintf(icon_path,
1150                         strlen(orig_icon_path) - (strlen(icon_filename) - 1),
1151                         "%s", orig_icon_path);
1152         for (i = 0; i < 2; i++) {
1153                 snprintf(modified_iconpath, BUFSIZE - 1, "%s/%s%s",
1154                                 icon_path, dpi_path[i], icon_filename);
1155                 if (access(modified_iconpath, F_OK) != -1) {
1156                         /* if exists, return modified icon path */
1157                         *new_icon_path = strdup(modified_iconpath);
1158                         return 0;
1159                 }
1160         }
1161
1162         return -1;
1163 }
1164
1165 static gint __compare_icon(gconstpointer a, gconstpointer b)
1166 {
1167         icon_x *icon = (icon_x *)a;
1168         char *icon_path;
1169
1170         if (icon->lang != NULL && strcasecmp(icon->lang, DEFAULT_LOCALE) != 0)
1171                 return -1;
1172
1173         if (icon->dpi != NULL)
1174                 return -1;
1175
1176         if (__check_icon_resolution(icon->text, &icon_path) == 0) {
1177                 free(icon->text);
1178                 icon->text = icon_path;
1179         }
1180
1181         return 0;
1182 }
1183
1184 static gint __compare_icon_with_dpi(gconstpointer a, gconstpointer b)
1185 {
1186         icon_x *icon = (icon_x *)a;
1187         int dpi = GPOINTER_TO_INT(b);
1188
1189         if (icon->lang != NULL && strcasecmp(icon->lang, DEFAULT_LOCALE) != 0)
1190                 return -1;
1191
1192         if (icon->dpi == NULL)
1193                 return -1;
1194
1195         if (__check_dpi(icon->dpi, dpi) == 0)
1196                 return 0;
1197
1198         return -1;
1199 }
1200
1201 static gint __compare_icon_with_lang(gconstpointer a, gconstpointer b)
1202 {
1203         icon_x *icon = (icon_x *)a;
1204         char *lang = (char *)b;
1205         char *icon_path;
1206
1207         if (icon->dpi != NULL)
1208                 return -1;
1209
1210         if (strcasecmp(icon->lang, lang) == 0) {
1211                 if (strcasecmp(icon->lang, DEFAULT_LOCALE) == 0) {
1212                         /* icon for no locale. check existance of
1213                          * folder-hierachied default icons
1214                          */
1215                         if (__check_icon_resolution(icon->text,
1216                                                 &icon_path) == 0) {
1217                                 free(icon->text);
1218                                 icon->text = icon_path;
1219                         }
1220                 }
1221                 return 0;
1222         }
1223
1224         return -1;
1225 }
1226
1227 static gint __compare_icon_with_lang_dpi(gconstpointer a, gconstpointer b)
1228 {
1229         int ret;
1230         icon_x *icon = (icon_x *)a;
1231         char *lang = (char *)b;
1232         int dpi = -1;
1233
1234         ret = system_info_get_platform_int(
1235                         "http://tizen.org/feature/screen.dpi", &dpi);
1236         if (ret != SYSTEM_INFO_ERROR_NONE)
1237                 return -1;
1238
1239         if (strcasecmp(icon->lang, lang) == 0 &&
1240                         __check_dpi(icon->dpi, dpi) == 0)
1241                 return 0;
1242
1243         return -1;
1244 }
1245
1246 static char *__find_icon(GList *icons, const char *lang)
1247 {
1248         GList *tmp;
1249         icon_x *icon;
1250         int dpi = 0;
1251         int ret;
1252
1253         /* first, find icon whose locale and dpi with given lang and
1254          * system's dpi has matched
1255          */
1256         tmp = g_list_find_custom(icons, lang,
1257                         (GCompareFunc)__compare_icon_with_lang_dpi);
1258         if (tmp != NULL) {
1259                 icon = (icon_x *)tmp->data;
1260                 return (char *)icon->text;
1261         }
1262
1263         /* if first has failed, find icon whose locale has matched */
1264         tmp = g_list_find_custom(icons, lang,
1265                         (GCompareFunc)__compare_icon_with_lang);
1266         if (tmp != NULL) {
1267                 icon = (icon_x *)tmp->data;
1268                 return (char *)icon->text;
1269         }
1270
1271         /* if second has failed, find icon whose dpi has matched with
1272          * system's dpi
1273          */
1274         ret = system_info_get_platform_int(
1275                         "http://tizen.org/feature/screen.dpi", &dpi);
1276         if (ret == SYSTEM_INFO_ERROR_NONE) {
1277                 tmp = g_list_find_custom(icons, GINT_TO_POINTER(dpi),
1278                                 (GCompareFunc)__compare_icon_with_dpi);
1279                 if (tmp != NULL) {
1280                         icon = (icon_x *)tmp->data;
1281                         return (char *)icon->text;
1282                 }
1283         }
1284
1285         /* last, find default icon marked as "No Locale" */
1286         tmp = g_list_find_custom(icons, NULL, (GCompareFunc)__compare_icon);
1287         if (tmp != NULL) {
1288                 icon = (icon_x *)tmp->data;
1289                 return (char *)icon->text;
1290         }
1291
1292         return NULL;
1293 }
1294
1295 static void __extract_data(const char *locale, GList *lbls, GList *lcns,
1296                 GList *icns, GList *dcns, GList *aths, char **label,
1297                 char **license, char **icon, char **description, char **author)
1298 {
1299         GList *tmp;
1300         label_x *lbl;
1301         license_x *lcn;
1302         description_x *dcn;
1303         author_x *ath;
1304
1305         for (tmp = lbls; tmp; tmp = tmp->next) {
1306                 lbl = (label_x *)tmp->data;
1307                 if (lbl == NULL)
1308                         continue;
1309                 if (lbl->lang) {
1310                         if (strcmp(lbl->lang, locale) == 0) {
1311                                 *label = (char *)lbl->text;
1312                                 break;
1313                         }
1314                 }
1315         }
1316         for (tmp = lcns; tmp; tmp = tmp->next) {
1317                 lcn = (license_x *)tmp->data;
1318                 if (lcn == NULL)
1319                         continue;
1320                 if (lcn->lang) {
1321                         if (strcmp(lcn->lang, locale) == 0) {
1322                                 *license = (char *)lcn->text;
1323                                 break;
1324                         }
1325                 }
1326         }
1327
1328         *icon = __find_icon(icns, locale);
1329
1330         for (tmp = dcns; tmp; tmp = tmp->next) {
1331                 dcn = (description_x *)tmp->data;
1332                 if (dcn == NULL)
1333                         continue;
1334                 if (dcn->lang) {
1335                         if (strcmp(dcn->lang, locale) == 0) {
1336                                 *description = (char *)dcn->text;
1337                                 break;
1338                         }
1339                 }
1340         }
1341         for (tmp = aths; tmp; tmp = tmp->next) {
1342                 ath = (author_x *)tmp->data;
1343                 if (ath == NULL)
1344                         continue;
1345                 if (ath->lang) {
1346                         if (strcmp(ath->lang, locale) == 0) {
1347                                 *author = (char *)ath->text;
1348                                 break;
1349                         }
1350                 }
1351         }
1352 }
1353
1354 static int __insert_mainapp_localized_info(sqlite3 *db, application_x *app,
1355                 const char *locale, const char *label, const char *icon)
1356 {
1357         static const char query[] =
1358                 "INSERT OR REPLACE INTO package_localized_info ("
1359                 "  package, package_locale, package_label, package_icon,"
1360                 "  package_description, package_license, package_author) "
1361                 "VALUES (?, ?,"
1362                 "  COALESCE((SELECT package_label FROM package_localized_info"
1363                 "            WHERE package=? AND package_locale=?), ?),"
1364                 "  COALESCE((SELECT package_icon FROM package_localized_info"
1365                 "            WHERE package=? AND package_icon=?), ?),"
1366                 "  (SELECT package_description FROM package_localized_info"
1367                 "   WHERE package=? AND package_locale=?),"
1368                 "  (SELECT package_description FROM package_localized_info"
1369                 "   WHERE package=? AND package_locale=?),"
1370                 "  (SELECT package_description FROM package_localized_info"
1371                 "   WHERE package=? AND package_locale=?))";
1372         int ret;
1373         sqlite3_stmt *stmt;
1374         int idx = 1;
1375
1376         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
1377         if (ret != SQLITE_OK) {
1378                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
1379                 return -1;
1380         }
1381
1382         __BIND_TEXT(db, stmt, idx++, app->package);
1383         __BIND_TEXT(db, stmt, idx++, locale);
1384         __BIND_TEXT(db, stmt, idx++, app->package);
1385         __BIND_TEXT(db, stmt, idx++, locale);
1386         __BIND_TEXT(db, stmt, idx++, label);
1387         __BIND_TEXT(db, stmt, idx++, app->package);
1388         __BIND_TEXT(db, stmt, idx++, locale);
1389         __BIND_TEXT(db, stmt, idx++, icon);
1390         __BIND_TEXT(db, stmt, idx++, app->package);
1391         __BIND_TEXT(db, stmt, idx++, locale);
1392         __BIND_TEXT(db, stmt, idx++, app->package);
1393         __BIND_TEXT(db, stmt, idx++, locale);
1394         __BIND_TEXT(db, stmt, idx++, app->package);
1395         __BIND_TEXT(db, stmt, idx++, locale);
1396
1397         ret = sqlite3_step(stmt);
1398         if (ret != SQLITE_DONE) {
1399                 _LOGE("step failed: %s", sqlite3_errmsg(db));
1400                 sqlite3_finalize(stmt);
1401                 return -1;
1402         }
1403
1404         sqlite3_finalize(stmt);
1405
1406         return 0;
1407 }
1408
1409 static int __insert_app_localized_info(sqlite3 *db, application_x *app)
1410 {
1411         static const char query[] =
1412                 "INSERT INTO package_app_localized_info (app_id, app_locale,"
1413                 "  app_label, app_icon) "
1414                 "VALUES (?, ?, ?, ?)";
1415         int ret;
1416         sqlite3_stmt *stmt;
1417         int idx;
1418         GList *tmp;
1419         GList *locales;
1420         const char *locale;
1421         char *label;
1422         char *icon;
1423
1424         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
1425         if (ret != SQLITE_OK) {
1426                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
1427                 return -1;
1428         }
1429
1430         locales = __create_locale_list(app->label, NULL, app->icon, NULL, NULL);
1431         for (tmp = locales; tmp; tmp = tmp->next) {
1432                 locale = (const char *)tmp->data;
1433                 label = NULL;
1434                 icon = NULL;
1435                 __extract_data(locale, app->label, NULL, app->icon, NULL, NULL,
1436                                 &label, NULL, &icon, NULL, NULL);
1437                 if (!label && !icon)
1438                         continue;
1439
1440                 idx = 1;
1441                 __BIND_TEXT(db, stmt, idx++, app->appid);
1442                 __BIND_TEXT(db, stmt, idx++, locale);
1443                 __BIND_TEXT(db, stmt, idx++, label);
1444                 __BIND_TEXT(db, stmt, idx++, icon);
1445
1446                 ret = sqlite3_step(stmt);
1447                 if (ret != SQLITE_DONE) {
1448                         _LOGE("step failed: %s", sqlite3_errmsg(db));
1449                         g_list_free(locales);
1450                         sqlite3_finalize(stmt);
1451                         return -1;
1452                 }
1453
1454                 sqlite3_reset(stmt);
1455
1456                 if (strcasecmp(app->mainapp, "true") == 0) {
1457                         if (__insert_mainapp_localized_info(db, app, locale,
1458                                                 label, icon))
1459                                 _LOGE("insert mainapp localized info failed");
1460                 }
1461         }
1462
1463         g_list_free(locales);
1464         sqlite3_finalize(stmt);
1465
1466         return 0;
1467 }
1468
1469 static int __insert_package_privilege_info(sqlite3 *db, manifest_x *mfx)
1470 {
1471         static const char query[] =
1472                 "INSERT INTO package_privilege_info (package, privilege, type) "
1473                 "VALUES (?, ?, ?)";
1474         int ret;
1475         sqlite3_stmt *stmt;
1476         int idx;
1477         GList *tmp;
1478         privilege_x *priv;
1479
1480         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
1481         if (ret != SQLITE_OK) {
1482                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
1483                 return -1;
1484         }
1485
1486         for (tmp = mfx->privileges; tmp; tmp = tmp->next) {
1487                 priv = (privilege_x *)tmp->data;
1488                 if (priv == NULL)
1489                         continue;
1490
1491                 idx = 1;
1492                 __BIND_TEXT(db, stmt, idx++, mfx->package);
1493                 __BIND_TEXT(db, stmt, idx++, priv->value);
1494                 __BIND_TEXT(db, stmt, idx++, priv->type);
1495
1496                 ret = sqlite3_step(stmt);
1497                 if (ret != SQLITE_DONE) {
1498                         _LOGE("step failed: %s", sqlite3_errmsg(db));
1499                         sqlite3_finalize(stmt);
1500                         return -1;
1501                 }
1502                 sqlite3_reset(stmt);
1503         }
1504
1505         sqlite3_finalize(stmt);
1506
1507         return 0;
1508 }
1509
1510 /* _PRODUCT_LAUNCHING_ENHANCED_
1511  *  app->indicatordisplay, app->portraitimg, app->landscapeimg,
1512  *  app->guestmode_appstatus
1513  */
1514 static int __insert_application_info(sqlite3 *db, manifest_x *mfx)
1515 {
1516         static const char query[] =
1517                 "INSERT INTO package_app_info (app_id, app_component,"
1518                 "  app_exec, app_nodisplay, app_type, app_onboot, app_multiple,"
1519                 "  app_autorestart, app_taskmanage, app_hwacceleration,"
1520                 "  app_screenreader, app_mainapp, app_recentimage,"
1521                 "  app_launchcondition, app_indicatordisplay, app_portraitimg,"
1522                 "  app_landscapeimg, app_guestmodevisibility,"
1523                 "  app_permissiontype, app_preload, app_submode,"
1524                 "  app_submode_mainid, app_installed_storage, app_process_pool,"
1525                 "  app_launch_mode, app_ui_gadget, app_support_mode,"
1526                 "  app_support_disable, component_type, package, app_tep_name,"
1527                 "  app_zip_mount_file, app_background_category,"
1528                 "  app_package_type, app_root_path, app_api_version,"
1529                 "  app_effective_appid, app_splash_screen_display,"
1530                 "  app_package_system, app_removable,"
1531                 "  app_package_installed_time, app_support_ambient,"
1532                 "  app_setup_appid) "
1533                 "VALUES (?, ?, "
1534                 "  ?, LOWER(?), ?, LOWER(?), LOWER(?),"
1535                 "  LOWER(?), LOWER(?), ?,"
1536                 "  ?, LOWER(?), ?,"
1537                 "  ?, LOWER(?), ?,"
1538                 "  ?, LOWER(?),"
1539                 "  ?, LOWER(?), LOWER(?),"
1540                 "  ?, ?, LOWER(?),"
1541                 "  COALESCE(?, 'single'), LOWER(?), ?,"
1542                 "  LOWER(?), ?, ?, ?,"
1543                 "  ?, ?,"
1544                 "  ?, ?, ?,"
1545                 "  ?, LOWER(?),"
1546                 "  LOWER(?), LOWER(?),"
1547                 "  ?, LOWER(?),"
1548                 "  ?)";
1549         int ret;
1550         sqlite3_stmt *stmt;
1551         int idx;
1552         GList *tmp;
1553         application_x *app;
1554         int bg_category;
1555         const char *effective_appid;
1556
1557         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
1558         if (ret != SQLITE_OK) {
1559                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
1560                 return -1;
1561         }
1562
1563         for (tmp = mfx->application; tmp; tmp = tmp->next) {
1564                 app = (application_x *)tmp->data;
1565                 if (app == NULL)
1566                         continue;
1567
1568                 bg_category = __convert_background_category(
1569                                 app->background_category);
1570                 effective_appid = __find_effective_appid(app->metadata);
1571
1572                 idx = 1;
1573                 __BIND_TEXT(db, stmt, idx++, app->appid);
1574                 __BIND_TEXT(db, stmt, idx++, app->component_type);
1575                 __BIND_TEXT(db, stmt, idx++, app->exec);
1576                 __BIND_TEXT(db, stmt, idx++, __get_bool(app->nodisplay, false));
1577                 __BIND_TEXT(db, stmt, idx++, app->type);
1578                 __BIND_TEXT(db, stmt, idx++, __get_bool(app->onboot, false));
1579                 __BIND_TEXT(db, stmt, idx++, __get_bool(app->multiple, false));
1580                 __BIND_TEXT(db, stmt, idx++,
1581                                 __get_bool(app->autorestart, false));
1582                 __BIND_TEXT(db, stmt, idx++,
1583                                 __get_bool(app->taskmanage, false));
1584                 __BIND_TEXT(db, stmt, idx++, app->hwacceleration);
1585                 __BIND_TEXT(db, stmt, idx++, app->screenreader);
1586                 __BIND_TEXT(db, stmt, idx++, __get_bool(app->mainapp, false));
1587                 __BIND_TEXT(db, stmt, idx++, app->recentimage);
1588                 __BIND_TEXT(db, stmt, idx++, app->launchcondition);
1589                 __BIND_TEXT(db, stmt, idx++,
1590                                 __get_bool(app->indicatordisplay, true));
1591                 __BIND_TEXT(db, stmt, idx++, app->portraitimg);
1592                 __BIND_TEXT(db, stmt, idx++, app->landscapeimg);
1593                 __BIND_TEXT(db, stmt, idx++,
1594                                 __get_bool(app->guestmode_visibility, true));
1595                 __BIND_TEXT(db, stmt, idx++, app->permission_type);
1596                 __BIND_TEXT(db, stmt, idx++, __get_bool(mfx->preload, false));
1597                 __BIND_TEXT(db, stmt, idx++, __get_bool(app->submode, false));
1598                 __BIND_TEXT(db, stmt, idx++, app->submode_mainid);
1599                 __BIND_TEXT(db, stmt, idx++, mfx->installed_storage);
1600                 __BIND_TEXT(db, stmt, idx++,
1601                                 __get_bool(app->process_pool, false));
1602                 __BIND_TEXT(db, stmt, idx++, app->launch_mode);
1603                 __BIND_TEXT(db, stmt, idx++, __get_bool(app->ui_gadget, false));
1604                 __BIND_TEXT(db, stmt, idx++, app->support_mode);
1605                 __BIND_TEXT(db, stmt, idx++,
1606                                 __get_bool(mfx->support_disable, false));
1607                 __BIND_TEXT(db, stmt, idx++, app->component_type);
1608                 __BIND_TEXT(db, stmt, idx++, mfx->package);
1609                 __BIND_TEXT(db, stmt, idx++, mfx->tep_name);
1610                 __BIND_TEXT(db, stmt, idx++, mfx->zip_mount_file);
1611                 __BIND_INT(db, stmt, idx++, bg_category);
1612                 __BIND_TEXT(db, stmt, idx++, mfx->type ? mfx->type : "tpk");
1613                 __BIND_TEXT(db, stmt, idx++, mfx->root_path);
1614                 __BIND_TEXT(db, stmt, idx++, mfx->api_version);
1615                 __BIND_TEXT(db, stmt, idx++, effective_appid);
1616                 __BIND_TEXT(db, stmt, idx++,
1617                                 __get_bool(app->splash_screen_display, false));
1618                 __BIND_TEXT(db, stmt, idx++, __get_bool(mfx->system, false));
1619                 __BIND_TEXT(db, stmt, idx++, __get_bool(mfx->removable, false));
1620                 __BIND_TEXT(db, stmt, idx++, mfx->installed_time);
1621                 __BIND_TEXT(db, stmt, idx++,
1622                                 __get_bool(app->support_ambient, false));
1623                 __BIND_TEXT(db, stmt, idx++, app->setup_appid);
1624
1625                 ret = sqlite3_step(stmt);
1626                 if (ret != SQLITE_DONE) {
1627                         _LOGE("step failed: %s", sqlite3_errmsg(db));
1628                         sqlite3_finalize(stmt);
1629                         return -1;
1630                 }
1631
1632                 sqlite3_reset(stmt);
1633
1634                 if (__insert_appcontrol_info(db, app)) {
1635                         sqlite3_finalize(stmt);
1636                         return -1;
1637                 }
1638                 if (__insert_category_info(db, app)) {
1639                         sqlite3_finalize(stmt);
1640                         return -1;
1641                 }
1642                 if (__insert_metadata_info(db, app)) {
1643                         sqlite3_finalize(stmt);
1644                         return -1;
1645                 }
1646                 if (__insert_datacontrol_info(db, app)) {
1647                         sqlite3_finalize(stmt);
1648                         return -1;
1649                 }
1650                 if (__insert_splashscreen_info(db, app)) {
1651                         sqlite3_finalize(stmt);
1652                         return -1;
1653                 }
1654                 if (__insert_app_localized_info(db, app)) {
1655                         sqlite3_finalize(stmt);
1656                         return -1;
1657                 }
1658         }
1659
1660         sqlite3_finalize(stmt);
1661
1662         return 0;
1663 }
1664
1665 static int __insert_package_update_info(sqlite3 *db, manifest_x *mfx)
1666 {
1667         static const char query[] =
1668                 "INSERT INTO package_update_info (package, update_version) "
1669                 "VALUES (?, ?)";
1670         int ret;
1671         int idx;
1672         sqlite3_stmt *stmt;
1673
1674         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
1675         if (ret != SQLITE_OK) {
1676                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
1677                 return -1;
1678         }
1679
1680         idx = 1;
1681         __BIND_TEXT(db, stmt, idx++, mfx->package);
1682         __BIND_TEXT(db, stmt, idx, mfx->version);
1683         ret = sqlite3_step(stmt);
1684         if (ret != SQLITE_DONE) {
1685                 _LOGE("step failed: %s", sqlite3_errmsg(db));
1686                 sqlite3_finalize(stmt);
1687                 return -1;
1688         }
1689         sqlite3_finalize(stmt);
1690
1691         return 0;
1692 }
1693
1694 static int __insert_package_localized_info(sqlite3 *db, manifest_x *mfx)
1695 {
1696         static const char query[] =
1697                 "INSERT INTO package_localized_info (package, package_locale,"
1698                 "  package_label, package_icon, package_description,"
1699                 "  package_license, package_author) "
1700                 "VALUES (?, ?, ?, ?, ?, ?, ?)";
1701         int ret;
1702         sqlite3_stmt *stmt;
1703         int idx;
1704         GList *tmp;
1705         GList *locales;
1706         const char *locale;
1707         char *label;
1708         char *icon;
1709         char *description;
1710         char *license;
1711         char *author;
1712
1713         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
1714         if (ret != SQLITE_OK) {
1715                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
1716                 return -1;
1717         }
1718
1719         locales = __create_locale_list(mfx->label, mfx->license, mfx->icon,
1720                         mfx->description, mfx->author);
1721         for (tmp = locales; tmp; tmp = tmp->next) {
1722                 locale = (const char *)tmp->data;
1723                 label = NULL;
1724                 icon = NULL;
1725                 description = NULL;
1726                 license = NULL;
1727                 author = NULL;
1728                 __extract_data(locale, mfx->label, mfx->license, mfx->icon,
1729                                 mfx->description, mfx->author,
1730                                 &label, &license, &icon, &description, &author);
1731                 if (!label && !license && !icon && !description && !author)
1732                         continue;
1733
1734                 idx = 1;
1735                 __BIND_TEXT(db, stmt, idx++, mfx->package);
1736                 __BIND_TEXT(db, stmt, idx++, locale);
1737                 __BIND_TEXT(db, stmt, idx++, label);
1738                 __BIND_TEXT(db, stmt, idx++, icon);
1739                 __BIND_TEXT(db, stmt, idx++, description);
1740                 __BIND_TEXT(db, stmt, idx++, license);
1741                 __BIND_TEXT(db, stmt, idx++, author);
1742
1743                 ret = sqlite3_step(stmt);
1744                 if (ret != SQLITE_DONE) {
1745                         _LOGE("step failed: %s", sqlite3_errmsg(db));
1746                         g_list_free(locales);
1747                         sqlite3_finalize(stmt);
1748                         return -1;
1749                 }
1750
1751                 sqlite3_reset(stmt);
1752         }
1753
1754         g_list_free(locales);
1755         sqlite3_finalize(stmt);
1756
1757         return 0;
1758 }
1759
1760 static int __insert_package_info(sqlite3 *db, manifest_x *mfx)
1761 {
1762         static const char query[] =
1763                 "INSERT INTO package_info (package, package_type,"
1764                 "  package_version, package_api_version, package_tep_name,"
1765                 "  package_zip_mount_file, install_location, package_size,"
1766                 "  package_removable, package_preload, package_readonly,"
1767                 "  package_update, package_appsetting, package_nodisplay,"
1768                 "  package_system, author_name, author_email, author_href,"
1769                 "  installed_time, installed_storage, storeclient_id,"
1770                 "  mainapp_id, package_url, root_path, external_path,"
1771                 "  csc_path, package_support_mode, package_support_disable) "
1772                 "VALUES (?, ?,"
1773                 "  ?, ?, ?,"
1774                 "  ?, ?, ?,"
1775                 "  LOWER(?), LOWER(?), LOWER(?),"
1776                 "  LOWER(?), LOWER(?), LOWER(?),"
1777                 "  LOWER(?), ?, ?, ?,"
1778                 "  ?, ?, ?,"
1779                 "  ?, ?, ?, ?,"
1780                 "  ?, ?, LOWER(?))";
1781         int ret;
1782         sqlite3_stmt *stmt;
1783         int idx = 1;
1784         const char *author_name = NULL;
1785         const char *author_email = NULL;
1786         const char *author_href = NULL;
1787
1788         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
1789         if (ret != SQLITE_OK) {
1790                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
1791                 return -1;
1792         }
1793
1794         if (mfx->author && mfx->author->data) {
1795                 author_name = ((author_x *)mfx->author->data)->text;
1796                 author_email = ((author_x *)mfx->author->data)->email;
1797                 author_href = ((author_x *)mfx->author->data)->href;
1798         }
1799
1800         __BIND_TEXT(db, stmt, idx++, mfx->package);
1801         __BIND_TEXT(db, stmt, idx++, mfx->type);
1802         __BIND_TEXT(db, stmt, idx++, mfx->version);
1803         __BIND_TEXT(db, stmt, idx++, mfx->api_version);
1804         __BIND_TEXT(db, stmt, idx++, mfx->tep_name);
1805         __BIND_TEXT(db, stmt, idx++, mfx->zip_mount_file);
1806         __BIND_TEXT(db, stmt, idx++, mfx->installlocation);
1807         __BIND_TEXT(db, stmt, idx++, mfx->package_size);
1808         __BIND_TEXT(db, stmt, idx++, __get_bool(mfx->removable, true));
1809         __BIND_TEXT(db, stmt, idx++, __get_bool(mfx->preload, false));
1810         __BIND_TEXT(db, stmt, idx++, __get_bool(mfx->readonly, false));
1811         __BIND_TEXT(db, stmt, idx++, __get_bool(mfx->update, false));
1812         __BIND_TEXT(db, stmt, idx++, __get_bool(mfx->appsetting, false));
1813         __BIND_TEXT(db, stmt, idx++, __get_bool(mfx->nodisplay_setting, false));
1814         __BIND_TEXT(db, stmt, idx++, __get_bool(mfx->system, false));
1815         __BIND_TEXT(db, stmt, idx++, author_name);
1816         __BIND_TEXT(db, stmt, idx++, author_email);
1817         __BIND_TEXT(db, stmt, idx++, author_href);
1818         __BIND_TEXT(db, stmt, idx++, mfx->installed_time);
1819         __BIND_TEXT(db, stmt, idx++, mfx->installed_storage);
1820         __BIND_TEXT(db, stmt, idx++, mfx->storeclient_id);
1821         __BIND_TEXT(db, stmt, idx++, mfx->mainapp_id);
1822         __BIND_TEXT(db, stmt, idx++, mfx->package_url);
1823         __BIND_TEXT(db, stmt, idx++, mfx->root_path);
1824         __BIND_TEXT(db, stmt, idx++, mfx->external_path);
1825         __BIND_TEXT(db, stmt, idx++, mfx->csc_path);
1826         __BIND_TEXT(db, stmt, idx++, mfx->support_mode);
1827         __BIND_TEXT(db, stmt, idx++, __get_bool(mfx->support_disable, false));
1828
1829         ret = sqlite3_step(stmt);
1830         if (ret != SQLITE_DONE) {
1831                 _LOGE("step failed: %s", sqlite3_errmsg(db));
1832                 sqlite3_finalize(stmt);
1833                 return -1;
1834         }
1835
1836         sqlite3_finalize(stmt);
1837
1838         if (__insert_package_update_info(db, mfx))
1839                 return -1;
1840         if (__insert_package_localized_info(db, mfx))
1841                 return -1;
1842         if (__insert_application_info(db, mfx))
1843                 return -1;
1844         if (__insert_package_privilege_info(db, mfx))
1845                 return -1;
1846
1847         return 0;
1848 }
1849
1850 API int pkgmgr_parser_insert_manifest_info_in_usr_db(manifest_x *mfx, uid_t uid)
1851 {
1852         int ret;
1853         const char *dbpath;
1854         sqlite3 *db;
1855
1856         if (mfx == NULL) {
1857                 _LOGE("invalid parameter");
1858                 return PM_PARSER_R_EINVAL;
1859         }
1860
1861         dbpath = __get_parser_db_path(uid);
1862
1863         ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE);
1864         if (ret != SQLITE_OK) {
1865                 _LOGE("open db failed: %d", ret);
1866                 return PM_PARSER_R_ERROR;
1867         }
1868
1869         __BEGIN_TRANSACTION(db);
1870         __DO_TRANSACTION(db, __insert_package_info(db, mfx));
1871         __END_TRANSACTION(db);
1872
1873         sqlite3_close_v2(db);
1874
1875         return PM_PARSER_R_OK;
1876 }
1877
1878 API int pkgmgr_parser_insert_manifest_info_in_db(manifest_x *mfx)
1879 {
1880         return pkgmgr_parser_insert_manifest_info_in_usr_db(mfx, __getuid());
1881 }
1882
1883 static int __delete_package_info(sqlite3 *db, const char *pkgid)
1884 {
1885         static const char query[] =
1886                 "DELETE FROM package_info WHERE package=?";
1887         int ret;
1888         sqlite3_stmt *stmt;
1889
1890         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
1891         if (ret != SQLITE_OK) {
1892                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
1893                 return -1;
1894         }
1895
1896         __BIND_TEXT(db, stmt, 1, pkgid);
1897
1898         ret = sqlite3_step(stmt);
1899         if (ret != SQLITE_DONE) {
1900                 _LOGE("step failed: %s", sqlite3_errmsg(db));
1901                 sqlite3_finalize(stmt);
1902                 return -1;
1903         }
1904
1905         sqlite3_finalize(stmt);
1906
1907         return 0;
1908 }
1909
1910 API int pkgmgr_parser_delete_manifest_info_from_usr_db(manifest_x *mfx,
1911         uid_t uid)
1912 {
1913         int ret;
1914         const char *dbpath;
1915         sqlite3 *db;
1916
1917         if (mfx == NULL) {
1918                 _LOGE("invalid parameter");
1919                 return PM_PARSER_R_EINVAL;
1920         }
1921
1922         dbpath = __get_parser_db_path(uid);
1923
1924         ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE);
1925         if (ret != SQLITE_OK) {
1926                 _LOGE("open db failed: %d", ret);
1927                 return PM_PARSER_R_ERROR;
1928         }
1929
1930         __BEGIN_TRANSACTION(db);
1931         __DO_TRANSACTION(db, __delete_package_info(db, mfx->package));
1932         __END_TRANSACTION(db);
1933
1934         sqlite3_close_v2(db);
1935
1936         return PM_PARSER_R_OK;
1937 }
1938
1939 API int pkgmgr_parser_delete_manifest_info_from_db(manifest_x *mfx)
1940 {
1941         return pkgmgr_parser_delete_manifest_info_from_usr_db(mfx, __getuid());
1942 }
1943
1944 API int pkgmgr_parser_update_manifest_info_in_usr_db(manifest_x *mfx, uid_t uid)
1945 {
1946         int ret;
1947         const char *dbpath;
1948         sqlite3 *db;
1949
1950         if (mfx == NULL) {
1951                 _LOGE("invalid parameter");
1952                 return PM_PARSER_R_EINVAL;
1953         }
1954
1955         dbpath = __get_parser_db_path(uid);
1956
1957         ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE);
1958         if (ret != SQLITE_OK) {
1959                 _LOGE("open db failed: %d", ret);
1960                 return PM_PARSER_R_ERROR;
1961         }
1962
1963         __BEGIN_TRANSACTION(db);
1964         __DO_TRANSACTION(db, __delete_package_info(db, mfx->package));
1965         __DO_TRANSACTION(db, __insert_package_info(db, mfx));
1966         __END_TRANSACTION(db);
1967
1968         sqlite3_close_v2(db);
1969
1970         return PM_PARSER_R_OK;
1971 }
1972
1973 API int pkgmgr_parser_update_manifest_info_in_db(manifest_x *mfx)
1974 {
1975         return pkgmgr_parser_update_manifest_info_in_usr_db(mfx, __getuid());
1976 }
1977
1978 static int __set_global_app_disable_for_uid(sqlite3 *db, const char *appid,
1979                 uid_t uid, bool is_disable)
1980 {
1981         static const char query[] =
1982                 "INSERT OR REPLACE INTO package_app_info_for_uid ("
1983                 "  app_id, uid, is_disabled, is_splash_screen_enabled) "
1984                 "VALUES (?, ?, ?,"
1985                 "  (SELECT app_splash_screen_display FROM package_app_info"
1986                 "   WHERE app_id=?))";
1987         int ret;
1988         sqlite3_stmt *stmt;
1989         int idx = 1;
1990
1991         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
1992         if (ret != SQLITE_OK) {
1993                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
1994                 return -1;
1995         }
1996
1997         __BIND_TEXT(db, stmt, idx++, appid);
1998         __BIND_INT(db, stmt, idx++, uid);
1999         __BIND_TEXT(db, stmt, idx++, is_disable ? "true" : "false");
2000         __BIND_TEXT(db, stmt, idx++, appid);
2001
2002         ret = sqlite3_step(stmt);
2003         if (ret != SQLITE_DONE) {
2004                 _LOGE("step failed: %s", sqlite3_errmsg(db));
2005                 sqlite3_finalize(stmt);
2006                 return -1;
2007         }
2008
2009         sqlite3_finalize(stmt);
2010
2011         return 0;
2012 }
2013
2014 API int pkgmgr_parser_update_global_app_disable_for_uid_info_in_db(
2015                 const char *appid, uid_t uid, int is_disable)
2016 {
2017         int ret;
2018         const char *dbpath;
2019         sqlite3 *db;
2020
2021         if (appid == NULL) {
2022                 _LOGE("invalid parameter");
2023                 return PM_PARSER_R_EINVAL;
2024         }
2025
2026         dbpath = __get_parser_db_path(GLOBAL_USER);
2027
2028         ret = __open_db(GLOBAL_USER, dbpath, &db, SQLITE_OPEN_READWRITE);
2029         if (ret != SQLITE_OK) {
2030                 _LOGE("open db failed: %d", ret);
2031                 return PM_PARSER_R_ERROR;
2032         }
2033
2034         __BEGIN_TRANSACTION(db);
2035         __DO_TRANSACTION(db, __set_global_app_disable_for_uid(db, appid,
2036                                 uid, (bool)is_disable));
2037         __END_TRANSACTION(db);
2038
2039         sqlite3_close_v2(db);
2040
2041         return PM_PARSER_R_OK;
2042 }
2043
2044 static int __set_app_disable(sqlite3 *db, const char *appid, uid_t uid,
2045                 bool is_disable)
2046 {
2047         static const char query[] =
2048                 "UPDATE package_app_info SET app_disable=? "
2049                 "WHERE app_id=?";
2050         int ret;
2051         sqlite3_stmt *stmt;
2052         int idx = 1;
2053
2054         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
2055         if (ret != SQLITE_OK) {
2056                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
2057                 return -1;
2058         }
2059
2060         __BIND_TEXT(db, stmt, idx++, is_disable ? "true" : "false");
2061         __BIND_TEXT(db, stmt, idx++, appid);
2062
2063         ret = sqlite3_step(stmt);
2064         if (ret != SQLITE_DONE) {
2065                 _LOGE("step failed: %s", sqlite3_errmsg(db));
2066                 sqlite3_finalize(stmt);
2067                 return -1;
2068         }
2069
2070         sqlite3_finalize(stmt);
2071
2072         return 0;
2073 }
2074
2075 API int pkgmgr_parser_update_app_disable_info_in_usr_db(const char *appid,
2076                 uid_t uid, int is_disable)
2077 {
2078         int ret;
2079         const char *dbpath;
2080         sqlite3 *db;
2081
2082         if (appid == NULL) {
2083                 _LOGE("invalid parameter");
2084                 return PM_PARSER_R_EINVAL;
2085         }
2086
2087         dbpath = __get_parser_db_path(uid);
2088
2089         ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE);
2090         if (ret != SQLITE_OK) {
2091                 _LOGE("open db failed: %d", ret);
2092                 return PM_PARSER_R_ERROR;
2093         }
2094
2095         __BEGIN_TRANSACTION(db);
2096         __DO_TRANSACTION(db, __set_app_disable(db, appid, uid,
2097                                 (bool)is_disable));
2098         __END_TRANSACTION(db);
2099
2100         sqlite3_close_v2(db);
2101
2102         return PM_PARSER_R_OK;
2103 }
2104
2105 API int pkgmgr_parser_update_app_disable_info_in_db(const char *appid,
2106                 int is_disable)
2107 {
2108         return pkgmgr_parser_update_app_disable_info_in_usr_db(appid,
2109                         __getuid(), is_disable);
2110 }
2111
2112 static int __set_pkg_disable(sqlite3 *db, const char *pkgid, uid_t uid,
2113                 bool is_disable)
2114 {
2115         static const char query[] =
2116                 "UPDATE package_info SET package_disable=? "
2117                 "WHERE package=?";
2118         int ret;
2119         sqlite3_stmt *stmt;
2120         int idx = 1;
2121
2122         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
2123         if (ret != SQLITE_OK) {
2124                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
2125                 return -1;
2126         }
2127
2128         __BIND_TEXT(db, stmt, idx++, is_disable ? "true" : "false");
2129         __BIND_TEXT(db, stmt, idx++, pkgid);
2130
2131         ret = sqlite3_step(stmt);
2132         if (ret != SQLITE_DONE) {
2133                 _LOGE("step failed: %s", sqlite3_errmsg(db));
2134                 sqlite3_finalize(stmt);
2135                 return -1;
2136         }
2137
2138         sqlite3_finalize(stmt);
2139
2140         return 0;
2141 }
2142
2143 API int pkgmgr_parser_update_pkg_disable_info_in_usr_db(const char *pkgid,
2144                 uid_t uid, int is_disable)
2145 {
2146         int ret;
2147         const char *dbpath;
2148         sqlite3 *db;
2149
2150         if (pkgid == NULL) {
2151                 _LOGE("invalid parameter");
2152                 return PM_PARSER_R_EINVAL;
2153         }
2154
2155         dbpath = __get_parser_db_path(uid);
2156
2157         ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE);
2158         if (ret != SQLITE_OK) {
2159                 _LOGE("open db failed: %d", ret);
2160                 return PM_PARSER_R_ERROR;
2161         }
2162
2163         __BEGIN_TRANSACTION(db);
2164         __DO_TRANSACTION(db, __set_pkg_disable(db, pkgid, uid,
2165                                 (bool)is_disable));
2166         __END_TRANSACTION(db);
2167
2168         sqlite3_close_v2(db);
2169
2170         return PM_PARSER_R_OK;
2171 }
2172
2173 API int pkgmgr_parser_update_pkg_disable_info_in_db(const char *pkgid,
2174                 int is_disable)
2175 {
2176         return pkgmgr_parser_update_pkg_disable_info_in_usr_db(pkgid,
2177                         __getuid(), is_disable);
2178 }
2179
2180 static int __set_global_app_splash_screen_for_uid(sqlite3 *db,
2181                 const char *appid, uid_t uid, bool is_enabled)
2182 {
2183         static const char query[] =
2184                 "INSERT OR REPLACE INTO package_app_info_for_uid("
2185                 "  appid, uid, is_splash_screen_enabled) "
2186                 "VALUES (?, ?, ?)";
2187         int ret;
2188         sqlite3_stmt *stmt;
2189         int idx = 1;
2190
2191         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
2192         if (ret != SQLITE_OK) {
2193                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
2194                 return -1;
2195         }
2196
2197         __BIND_TEXT(db, stmt, idx++, appid);
2198         __BIND_INT(db, stmt, idx++, uid);
2199         __BIND_TEXT(db, stmt, idx++, is_enabled ? "true" : "false");
2200
2201         ret = sqlite3_step(stmt);
2202         if (ret != SQLITE_DONE) {
2203                 _LOGE("step failed: %s", sqlite3_errmsg(db));
2204                 sqlite3_finalize(stmt);
2205                 return -1;
2206         }
2207
2208         sqlite3_finalize(stmt);
2209
2210         return 0;
2211 }
2212
2213 API int pkgmgr_parser_update_global_app_splash_screen_display_info_in_usr_db(
2214                 const char *appid, uid_t uid, int flag)
2215 {
2216         int ret;
2217         const char *dbpath;
2218         sqlite3 *db;
2219
2220         if (appid == NULL) {
2221                 _LOGE("invalid parameter");
2222                 return PM_PARSER_R_EINVAL;
2223         }
2224
2225         dbpath = __get_parser_db_path(GLOBAL_USER);
2226
2227         ret = __open_db(GLOBAL_USER, dbpath, &db, SQLITE_OPEN_READWRITE);
2228         if (ret != SQLITE_OK) {
2229                 _LOGE("open db failed: %d", ret);
2230                 return PM_PARSER_R_ERROR;
2231         }
2232
2233         __BEGIN_TRANSACTION(db);
2234         __DO_TRANSACTION(db, __set_global_app_splash_screen_for_uid(db,
2235                                 appid, uid, (bool)flag));
2236         __END_TRANSACTION(db);
2237
2238         sqlite3_close_v2(db);
2239
2240         return PM_PARSER_R_OK;
2241 }
2242
2243 static int __set_app_splash_screen(sqlite3 *db, const char *appid,
2244                 bool is_enabled)
2245 {
2246         static const char query[] =
2247                 "UPDATE package_app_info SET app_splash_screen_display=? "
2248                 "WHERE app_id=?";
2249         int ret;
2250         sqlite3_stmt *stmt;
2251         int idx = 1;
2252
2253         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
2254         if (ret != SQLITE_OK) {
2255                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
2256                 return -1;
2257         }
2258
2259         __BIND_TEXT(db, stmt, idx++, is_enabled ? "true" : "false");
2260         __BIND_TEXT(db, stmt, idx++, appid);
2261
2262         ret = sqlite3_step(stmt);
2263         if (ret != SQLITE_DONE) {
2264                 _LOGE("step failed: %s", sqlite3_errmsg(db));
2265                 sqlite3_finalize(stmt);
2266                 return -1;
2267         }
2268
2269         sqlite3_finalize(stmt);
2270
2271         return 0;
2272 }
2273
2274 API int pkgmgr_parser_update_app_splash_screen_display_info_in_usr_db(
2275                 const char *appid, uid_t uid, int flag)
2276 {
2277         int ret;
2278         const char *dbpath;
2279         sqlite3 *db;
2280
2281         if (appid == NULL) {
2282                 _LOGE("invalid parameter");
2283                 return PM_PARSER_R_EINVAL;
2284         }
2285
2286         dbpath = __get_parser_db_path(uid);
2287
2288         ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE);
2289         if (ret != SQLITE_OK) {
2290                 _LOGE("open db failed: %d", ret);
2291                 return PM_PARSER_R_ERROR;
2292         }
2293
2294         __BEGIN_TRANSACTION(db);
2295         __DO_TRANSACTION(db, __set_app_splash_screen(db, appid, (bool)flag));
2296         __END_TRANSACTION(db);
2297
2298         sqlite3_close_v2(db);
2299
2300         return PM_PARSER_R_OK;
2301 }
2302
2303 API int pkgmgr_parser_update_app_splash_screen_display_info_in_db(
2304                 const char *appid, int flag)
2305 {
2306         return pkgmgr_parser_update_app_splash_screen_display_info_in_usr_db(
2307                         appid, __getuid(), flag);
2308 }
2309
2310 static int __set_app_label(sqlite3 *db, const char *appid, const char *label)
2311 {
2312         static const char query[] =
2313                 "UPDATE package_app_localized_info SET app_label=? "
2314                 "WHERE app_id=? AND app_label IS NOT NULL";
2315         int ret;
2316         sqlite3_stmt *stmt;
2317         int idx = 1;
2318
2319         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
2320         if (ret != SQLITE_OK) {
2321                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
2322                 return -1;
2323         }
2324
2325         __BIND_TEXT(db, stmt, idx++, label);
2326         __BIND_TEXT(db, stmt, idx++, appid);
2327
2328         ret = sqlite3_step(stmt);
2329         if (ret != SQLITE_DONE) {
2330                 _LOGE("step failed: %s", sqlite3_errmsg(db));
2331                 sqlite3_finalize(stmt);
2332                 return -1;
2333         }
2334
2335         sqlite3_finalize(stmt);
2336
2337         return 0;
2338 }
2339
2340 API int pkgmgr_parser_update_app_label_info_in_usr_db(const char *appid,
2341                 uid_t uid, const char *label)
2342 {
2343         int ret;
2344         const char *dbpath;
2345         sqlite3 *db;
2346
2347         if (appid == NULL) {
2348                 _LOGE("invalid parameter");
2349                 return PM_PARSER_R_EINVAL;
2350         }
2351
2352         dbpath = __get_parser_db_path(uid);
2353
2354         ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE);
2355         if (ret != SQLITE_OK) {
2356                 _LOGE("open db failed: %d", ret);
2357                 return PM_PARSER_R_ERROR;
2358         }
2359
2360         __BEGIN_TRANSACTION(db);
2361         __DO_TRANSACTION(db, __set_app_label(db, appid, label));
2362         __END_TRANSACTION(db);
2363
2364         sqlite3_close_v2(db);
2365
2366         return PM_PARSER_R_OK;
2367 }
2368
2369 API int pkgmgr_parser_update_app_label_info_in_db(const char *appid,
2370                 const char *label)
2371 {
2372         return pkgmgr_parser_update_app_label_info_in_usr_db(appid, __getuid(),
2373                         label);
2374 }
2375
2376 static int __set_tep_path(sqlite3 *db, const char *pkgid, const char *tep_path)
2377 {
2378         static const char query[] =
2379                 "UPDATE package_info SET package_tep_name=? "
2380                 "WHERE package=?";
2381         int ret;
2382         sqlite3_stmt *stmt;
2383         int idx = 1;
2384
2385         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
2386         if (ret != SQLITE_OK) {
2387                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
2388                 return -1;
2389         }
2390
2391         __BIND_TEXT(db, stmt, idx++, tep_path);
2392         __BIND_TEXT(db, stmt, idx++, pkgid);
2393
2394         ret = sqlite3_step(stmt);
2395         if (ret != SQLITE_DONE) {
2396                 _LOGE("step failed: %s", sqlite3_errmsg(db));
2397                 sqlite3_finalize(stmt);
2398                 return -1;
2399         }
2400
2401         sqlite3_finalize(stmt);
2402
2403         return 0;
2404 }
2405
2406 API int pkgmgr_parser_update_tep_info_in_usr_db(const char *pkgid,
2407                 const char *tep_path, uid_t uid)
2408 {
2409         int ret;
2410         const char *dbpath;
2411         sqlite3 *db;
2412
2413         if (pkgid == NULL) {
2414                 _LOGE("invalid parameter");
2415                 return PM_PARSER_R_EINVAL;
2416         }
2417
2418         dbpath = __get_parser_db_path(uid);
2419
2420         ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE);
2421         if (ret != SQLITE_OK) {
2422                 _LOGE("open db failed: %d", ret);
2423                 return PM_PARSER_R_ERROR;
2424         }
2425
2426         __BEGIN_TRANSACTION(db);
2427         __DO_TRANSACTION(db, __set_tep_path(db, pkgid, tep_path));
2428         __END_TRANSACTION(db);
2429
2430         sqlite3_close_v2(db);
2431
2432         return PM_PARSER_R_OK;
2433 }
2434
2435 API int pkgmgr_parser_update_tep_info_in_db(const char *pkgid,
2436                 const char *tep_path)
2437 {
2438         return pkgmgr_parser_update_tep_info_in_usr_db(pkgid, tep_path,
2439                         __getuid());
2440 }
2441
2442 static int __convert_update_type(pkgmgrinfo_updateinfo_update_type type,
2443                 const char **update_type)
2444 {
2445         if (type == PMINFO_UPDATEINFO_NONE)
2446                 *update_type = PMINFO_UPDATEINFO_TYPE_NONE;
2447         else if (type == PMINFO_UPDATEINFO_FORCE)
2448                 *update_type = PMINFO_UPDATEINFO_TYPE_FORCE;
2449         else if (type == PMINFO_UPDATEINFO_OPTIONAL)
2450                 *update_type = PMINFO_UPDATEINFO_TYPE_OPTIONAL;
2451         else
2452                 return -1;
2453         return 0;
2454 }
2455
2456 static int __register_pkg_update_info(sqlite3 *db, updateinfo_x *info,
2457                 const char *update_type)
2458 {
2459         static const char query[] =
2460                 "UPDATE package_update_info "
2461                 "SET update_version=?, update_type=? "
2462                 "WHERE package=?";
2463         int ret;
2464         sqlite3_stmt *stmt;
2465         int idx = 1;
2466
2467         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
2468         if (ret != SQLITE_OK) {
2469                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
2470                 return -1;
2471         }
2472
2473         __BIND_TEXT(db, stmt, idx++, info->version);
2474         __BIND_TEXT(db, stmt, idx++, update_type);
2475         __BIND_TEXT(db, stmt, idx++, info->pkgid);
2476
2477         ret = sqlite3_step(stmt);
2478         if (ret != SQLITE_DONE) {
2479                 _LOGE("step failed: %s", sqlite3_errmsg(db));
2480                 sqlite3_finalize(stmt);
2481                 return -1;
2482         }
2483
2484         sqlite3_finalize(stmt);
2485
2486         return 0;
2487 }
2488
2489 API int pkgmgr_parser_register_pkg_update_info_in_usr_db(
2490                 pkgmgrinfo_updateinfo_h handle, uid_t uid)
2491 {
2492         int ret;
2493         updateinfo_x *update_info;
2494         updateinfo_x *prev_update_info;
2495         pkgmgrinfo_updateinfo_h prev_update_handle;
2496         pkgmgrinfo_pkginfo_h pkginfo;
2497         pkgmgrinfo_version_compare_type compare_result;
2498         bool is_global_pkg;
2499         const char *update_type;
2500         const char *dbpath;
2501         sqlite3 *db;
2502
2503         if (handle == NULL) {
2504                 _LOGE("invalid parameter");
2505                 return PM_PARSER_R_EINVAL;
2506         }
2507
2508         update_info = (updateinfo_x *)handle;
2509         if (update_info->pkgid == NULL || update_info->version == NULL)
2510                 return PM_PARSER_R_EINVAL;
2511         if (__convert_update_type(update_info->type, &update_type) != 0)
2512                 return PM_PARSER_R_EINVAL;
2513
2514         ret = pkgmgrinfo_updateinfo_get_usr_updateinfo(update_info->pkgid,
2515                         &prev_update_handle, uid);
2516         if (ret != PMINFO_R_OK)
2517                 return PM_PARSER_R_ERROR;
2518
2519         prev_update_info = (updateinfo_x *)prev_update_handle;
2520         ret = pkgmgrinfo_compare_package_version(update_info->version,
2521                         prev_update_info->version, &compare_result);
2522         if (ret != PMINFO_R_OK) {
2523                 pkgmgrinfo_updateinfo_destroy(prev_update_handle);
2524                 return PM_PARSER_R_ERROR;
2525         }
2526
2527         if (compare_result == PMINFO_VERSION_SAME &&
2528                         prev_update_info->type == PMINFO_UPDATEINFO_NONE) {
2529                 _LOGI("Given update info version[%s] of pkgid[%s] "
2530                                 "will be ignored",
2531                                 update_info->version, update_info->pkgid);
2532                 pkgmgrinfo_updateinfo_destroy(prev_update_handle);
2533                 return PM_PARSER_R_OK;
2534         }
2535         pkgmgrinfo_updateinfo_destroy(prev_update_handle);
2536
2537         ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(update_info->pkgid, uid,
2538                         &pkginfo);
2539         if (ret != PMINFO_R_OK)
2540                 return PM_PARSER_R_ERROR;
2541
2542         ret = pkgmgrinfo_pkginfo_is_global(pkginfo, &is_global_pkg);
2543         if (ret != PMINFO_R_OK) {
2544                 pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo);
2545                 return PM_PARSER_R_ERROR;
2546         }
2547         pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo);
2548
2549         dbpath = __get_parser_db_path(is_global_pkg ? GLOBAL_USER : uid);
2550
2551         ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE);
2552         if (ret != SQLITE_OK) {
2553                 _LOGE("open db failed: %d", ret);
2554                 return PM_PARSER_R_ERROR;
2555         }
2556
2557         __BEGIN_TRANSACTION(db);
2558         __DO_TRANSACTION(db, __register_pkg_update_info(db, update_info,
2559                                 update_type));
2560         __END_TRANSACTION(db);
2561
2562         sqlite3_close_v2(db);
2563
2564         return PM_PARSER_R_OK;
2565 }
2566
2567 API int pkgmgr_parser_register_pkg_update_info_in_db(
2568                 pkgmgrinfo_updateinfo_h handle)
2569 {
2570         return pkgmgr_parser_register_pkg_update_info_in_usr_db(handle,
2571                         __getuid());
2572 }
2573
2574 static int __unregister_pkg_update_info(sqlite3 *db, const char *pkgid)
2575 {
2576         static const char query[] =
2577                 "UPDATE package_update_info SET update_type='none' "
2578                 "WHERE package=?";
2579         int ret;
2580         sqlite3_stmt *stmt;
2581         int idx = 1;
2582
2583         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
2584         if (ret != SQLITE_OK) {
2585                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
2586                 return -1;
2587         }
2588
2589         __BIND_TEXT(db, stmt, idx++, pkgid);
2590
2591         ret = sqlite3_step(stmt);
2592         if (ret != SQLITE_DONE) {
2593                 _LOGE("step failed: %s", sqlite3_errmsg(db));
2594                 sqlite3_finalize(stmt);
2595                 return -1;
2596         }
2597
2598         sqlite3_finalize(stmt);
2599
2600         return 0;
2601 }
2602
2603 API int pkgmgr_parser_unregister_pkg_update_info_in_usr_db(const char *pkgid,
2604                 uid_t uid)
2605 {
2606         int ret;
2607         const char *dbpath;
2608         sqlite3 *db;
2609         pkgmgrinfo_pkginfo_h pkginfo;
2610         bool is_global_pkg;
2611
2612         if (pkgid == NULL) {
2613                 _LOGE("invalid parameter");
2614                 return PM_PARSER_R_EINVAL;
2615         }
2616
2617         ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid, uid, &pkginfo);
2618         if (ret != PMINFO_R_OK)
2619                 return PM_PARSER_R_EINVAL;
2620
2621         ret = pkgmgrinfo_pkginfo_is_global(pkginfo, &is_global_pkg);
2622         pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo);
2623         if (ret != PMINFO_R_OK)
2624                 return PM_PARSER_R_ERROR;
2625
2626         dbpath = __get_parser_db_path(is_global_pkg ? GLOBAL_USER : uid);
2627
2628         ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE);
2629         if (ret != SQLITE_OK) {
2630                 _LOGE("open db failed: %d", ret);
2631                 return PM_PARSER_R_ERROR;
2632         }
2633
2634         __BEGIN_TRANSACTION(db);
2635         __DO_TRANSACTION(db, __unregister_pkg_update_info(db, pkgid));
2636         __END_TRANSACTION(db);
2637
2638         sqlite3_close_v2(db);
2639
2640         return PM_PARSER_R_OK;
2641 }
2642
2643 API int pkgmgr_parser_unregister_pkg_update_info_in_db(const char *pkgid)
2644 {
2645         return pkgmgr_parser_unregister_pkg_update_info_in_usr_db(pkgid,
2646                         __getuid());
2647 }
2648
2649 static int __unregister_all_pkg_update_info(sqlite3 *db)
2650 {
2651         static const char query[] =
2652                 "UPDATE package_update_info SET update_type='none'";
2653         int ret;
2654         sqlite3_stmt *stmt;
2655
2656         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
2657         if (ret != SQLITE_OK) {
2658                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
2659                 return -1;
2660         }
2661
2662         ret = sqlite3_step(stmt);
2663         if (ret != SQLITE_DONE) {
2664                 _LOGE("step failed: %s", sqlite3_errmsg(db));
2665                 sqlite3_finalize(stmt);
2666                 return -1;
2667         }
2668
2669         sqlite3_finalize(stmt);
2670
2671         return 0;
2672 }
2673
2674 API int pkgmgr_parser_unregister_all_pkg_update_info_in_usr_db(uid_t uid)
2675 {
2676         int ret;
2677         const char *dbpath;
2678         sqlite3 *db;
2679
2680         dbpath = __get_parser_db_path(uid);
2681
2682         ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE);
2683         if (ret != SQLITE_OK) {
2684                 _LOGE("open db failed: %d", ret);
2685                 return PM_PARSER_R_ERROR;
2686         }
2687
2688         __BEGIN_TRANSACTION(db);
2689         __DO_TRANSACTION(db, __unregister_all_pkg_update_info(db));
2690         __END_TRANSACTION(db);
2691
2692         sqlite3_close_v2(db);
2693
2694         return PM_PARSER_R_OK;
2695 }
2696
2697 API int pkgmgr_parser_unregister_all_pkg_update_info_in_db(void)
2698 {
2699         return pkgmgr_parser_unregister_all_pkg_update_info_in_usr_db(
2700                         __getuid());
2701 }