Use list of string for datacontrol privilege
[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         char *priv;
711
712         if (datacontrol == NULL)
713                 return 0;
714
715         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
716         if (ret != SQLITE_OK) {
717                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
718                 return -1;
719         }
720
721         for (privileges = datacontrol->privileges; privileges;
722                         privileges = privileges->next) {
723                 priv = (char *)privileges->data;
724                 if (priv == NULL)
725                         continue;
726
727                 idx = 1;
728                 __BIND_TEXT(db, stmt, idx++, datacontrol->providerid);
729                 __BIND_TEXT(db, stmt, idx++, priv);
730                 __BIND_TEXT(db, stmt, idx++, datacontrol->type);
731
732                 ret = sqlite3_step(stmt);
733                 if (ret != SQLITE_DONE) {
734                         _LOGE("step failed: %s", sqlite3_errmsg(db));
735                         sqlite3_finalize(stmt);
736                         return -1;
737                 }
738
739                 sqlite3_reset(stmt);
740         }
741
742         sqlite3_finalize(stmt);
743         return 0;
744 }
745
746 static int __insert_datacontrol_info(sqlite3 *db, application_x *app)
747 {
748         static const char query[] =
749                 "INSERT INTO package_app_data_control (app_id, providerid,"
750                 "  access, type, trusted) VALUES (?, ?, ?, ?, ?)";
751         int ret;
752         sqlite3_stmt *stmt;
753         int idx;
754         GList *tmp;
755         datacontrol_x *dc;
756
757         if (app->datacontrol == NULL)
758                 return 0;
759
760         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
761         if (ret != SQLITE_OK) {
762                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
763                 return -1;
764         }
765
766         for (tmp = app->datacontrol; tmp; tmp = tmp->next) {
767                 dc = (datacontrol_x *)tmp->data;
768                 if (dc == NULL)
769                         continue;
770                 idx = 1;
771                 __BIND_TEXT(db, stmt, idx++, app->appid);
772                 __BIND_TEXT(db, stmt, idx++, dc->providerid);
773                 __BIND_TEXT(db, stmt, idx++, dc->access);
774                 __BIND_TEXT(db, stmt, idx++, dc->type);
775                 __BIND_TEXT(db, stmt, idx++, dc->trusted);
776
777                 ret = sqlite3_step(stmt);
778                 if (ret != SQLITE_DONE) {
779                         _LOGE("step failed: %s", sqlite3_errmsg(db));
780                         sqlite3_finalize(stmt);
781                         return -1;
782                 }
783
784                 if (dc->privileges &&
785                                 __insert_app_data_control_privilege_info(db, dc)) {
786                         sqlite3_finalize(stmt);
787                         return -1;
788                 }
789
790                 sqlite3_reset(stmt);
791         }
792
793         sqlite3_finalize(stmt);
794
795         return 0;
796 }
797
798 /* TODO: move to installer */
799 static int __check_dpi(const char *dpi_char, int dpi_int)
800 {
801         if (dpi_char == NULL)
802                 return -1;
803
804         if (strcasecmp(dpi_char, LDPI) == 0) {
805                 if (dpi_int >= LDPI_MIN && dpi_int <= LDPI_MAX)
806                         return 0;
807                 else
808                         return -1;
809         } else if (strcasecmp(dpi_char, MDPI) == 0) {
810                 if (dpi_int >= MDPI_MIN && dpi_int <= MDPI_MAX)
811                         return 0;
812                 else
813                         return -1;
814         } else if (strcasecmp(dpi_char, HDPI) == 0) {
815                 if (dpi_int >= HDPI_MIN && dpi_int <= HDPI_MAX)
816                         return 0;
817                 else
818                         return -1;
819         } else if (strcasecmp(dpi_char, XHDPI) == 0) {
820                 if (dpi_int >= XHDPI_MIN && dpi_int <= XHDPI_MAX)
821                         return 0;
822                 else
823                         return -1;
824         } else if (strcasecmp(dpi_char, XXHDPI) == 0) {
825                 if (dpi_int >= XXHDPI_MIN && dpi_int <= XXHDPI_MAX)
826                         return 0;
827                 else
828                         return -1;
829         } else
830                 return -1;
831 }
832
833 static gint __compare_splashscreen_with_orientation_dpi(gconstpointer a,
834                 gconstpointer b)
835 {
836         splashscreen_x *ss = (splashscreen_x *)a;
837         const char *orientation = (const char *)b;
838         int dpi = -1;
839         int ret;
840
841         if (ss->operation || ss->dpi == NULL)
842                 return -1;
843
844         ret = system_info_get_platform_int(
845                         "http://tizen.org/feature/screen.dpi", &dpi);
846         if (ret != SYSTEM_INFO_ERROR_NONE)
847                 return -1;
848
849         if (strcasecmp(ss->orientation, orientation) == 0 &&
850                         __check_dpi(ss->dpi, dpi) == 0)
851                 return 0;
852
853         return -1;
854 }
855
856 static gint __compare_splashscreen_with_orientation(gconstpointer a,
857                 gconstpointer b)
858 {
859         splashscreen_x *ss = (splashscreen_x *)a;
860         const char *orientation = (const char *)b;
861
862         if (ss->operation || ss->dpi)
863                 return -1;
864
865         if (strcasecmp(ss->orientation, orientation) == 0)
866                 return 0;
867
868         return -1;
869 }
870
871 static splashscreen_x *__find_default_splashscreen(GList *splashscreens,
872                 const char *orientation)
873 {
874         GList *tmp;
875
876         tmp = g_list_find_custom(splashscreens, orientation,
877                         (GCompareFunc)
878                         __compare_splashscreen_with_orientation_dpi);
879         if (tmp)
880                 return (splashscreen_x *)tmp->data;
881
882         tmp = g_list_find_custom(splashscreens, orientation,
883                         (GCompareFunc)__compare_splashscreen_with_orientation);
884         if (tmp)
885                 return (splashscreen_x *)tmp->data;
886
887         return NULL;
888 }
889
890 static void __find_appcontrol_splashscreen_with_dpi(gpointer data,
891                 gpointer user_data)
892 {
893         splashscreen_x *ss = (splashscreen_x *)data;
894         GList **list = (GList **)user_data;
895         int dpi = -1;
896         int ret;
897
898         if (ss->operation == NULL || ss->dpi == NULL)
899                 return;
900
901         ret = system_info_get_platform_int(
902                         "http://tizen.org/feature/screen.dpi", &dpi);
903         if (ret != SYSTEM_INFO_ERROR_NONE)
904                 return;
905
906         if (__check_dpi(ss->dpi, dpi) != 0)
907                 return;
908
909         *list = g_list_append(*list, ss);
910 }
911
912 static void __find_appcontrol_splashscreen(gpointer data, gpointer user_data)
913 {
914         splashscreen_x *ss = (splashscreen_x *)data;
915         GList **list = (GList **)user_data;
916         splashscreen_x *ss_tmp;
917         GList *tmp;
918
919         if (ss->operation == NULL || ss->dpi)
920                 return;
921
922         for (tmp = *list; tmp; tmp = tmp->next) {
923                 ss_tmp = (splashscreen_x *)tmp->data;
924                 if (ss_tmp->operation
925                         && strcmp(ss_tmp->operation, ss->operation) == 0
926                         && strcmp(ss_tmp->orientation, ss->orientation) == 0)
927                         return;
928         }
929
930         *list = g_list_append(*list, ss);
931 }
932
933 static GList *__find_splashscreens(GList *splashscreens)
934 {
935         GList *list = NULL;
936         splashscreen_x *ss;
937
938         g_list_foreach(splashscreens,
939                         __find_appcontrol_splashscreen_with_dpi, &list);
940         g_list_foreach(splashscreens,
941                         __find_appcontrol_splashscreen, &list);
942
943         ss = __find_default_splashscreen(splashscreens, "portrait");
944         if (ss)
945                 list = g_list_append(list, ss);
946         ss = __find_default_splashscreen(splashscreens, "landscape");
947         if (ss)
948                 list = g_list_append(list, ss);
949
950         return list;
951 }
952
953 static int __insert_splashscreen_info(sqlite3 *db, application_x *app)
954 {
955         static const char query[] =
956                 "INSERT INTO package_app_splash_screen (app_id, src, type,"
957                 "  orientation, indicatordisplay, operation, color_depth) "
958                 "VALUES (?, ?, ?, ?, ?, ?, ?)";
959         int ret;
960         sqlite3_stmt *stmt;
961         int idx;
962         GList *tmp;
963         splashscreen_x *ss;
964         GList *ss_list;
965
966         if (app->splashscreens == NULL)
967                 return 0;
968
969         ss_list = __find_splashscreens(app->splashscreens);
970         if (ss_list == NULL)
971                 return 0;
972
973         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
974         if (ret != SQLITE_OK) {
975                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
976                 return -1;
977         }
978
979         for (tmp = ss_list; tmp; tmp = tmp->next) {
980                 ss = (splashscreen_x *)tmp->data;
981                 if (ss == NULL)
982                         continue;
983                 idx = 1;
984                 __BIND_TEXT(db, stmt, idx++, app->appid);
985                 __BIND_TEXT(db, stmt, idx++, ss->src);
986                 __BIND_TEXT(db, stmt, idx++, ss->type);
987                 __BIND_TEXT(db, stmt, idx++, ss->orientation);
988                 __BIND_TEXT(db, stmt, idx++, ss->indicatordisplay);
989                 __BIND_TEXT(db, stmt, idx++, ss->operation);
990                 __BIND_TEXT(db, stmt, idx++, ss->color_depth);
991
992                 ret = sqlite3_step(stmt);
993                 if (ret != SQLITE_DONE) {
994                         _LOGE("step failed: %s", sqlite3_errmsg(db));
995                         sqlite3_finalize(stmt);
996                         return -1;
997                 }
998
999                 sqlite3_reset(stmt);
1000         }
1001
1002         sqlite3_finalize(stmt);
1003
1004         return 0;
1005 }
1006
1007 static void __trimfunc(GList *trim_list)
1008 {
1009         char *trim_data;
1010         char *prev = NULL;
1011         GList *list = g_list_first(trim_list);
1012
1013         while (list) {
1014                 trim_data = (char *)list->data;
1015                 if (trim_data) {
1016                         if (prev) {
1017                                 if (strcmp(trim_data, prev) == 0) {
1018                                         trim_list = g_list_remove(trim_list,
1019                                                         trim_data);
1020                                         list = g_list_first(trim_list);
1021                                         prev = NULL;
1022                                         continue;
1023                                 } else
1024                                         prev = trim_data;
1025                         } else {
1026                                 prev = trim_data;
1027                         }
1028                 }
1029                 list = g_list_next(list);
1030         }
1031 }
1032
1033 static gint __comparefunc(gconstpointer a, gconstpointer b, gpointer userdata)
1034 {
1035         if (a == NULL || b == NULL)
1036                 return 0;
1037         if (strcmp((char *)a, (char *)b) == 0)
1038                 return 0;
1039         if (strcmp((char *)a, (char *)b) < 0)
1040                 return -1;
1041         if (strcmp((char *)a, (char *)b) > 0)
1042                 return 1;
1043         return 0;
1044 }
1045
1046 /* TODO: refactor inserting localized info */
1047 static GList *__create_locale_list(GList *lbls, GList *lcns, GList *icns,
1048                 GList *dcns, GList *aths)
1049 {
1050         GList *locale = NULL;
1051         GList *tmp;
1052         label_x *lbl;
1053         license_x *lcn;
1054         icon_x *icn;
1055         description_x *dcn;
1056         author_x *ath;
1057
1058         for (tmp = lbls; tmp; tmp = tmp->next) {
1059                 lbl = (label_x *)tmp->data;
1060                 if (lbl == NULL)
1061                         continue;
1062                 if (lbl->lang)
1063                         locale = g_list_insert_sorted_with_data(
1064                                         locale, (gpointer)lbl->lang,
1065                                         __comparefunc, NULL);
1066         }
1067         for (tmp = lcns; tmp; tmp = tmp->next) {
1068                 lcn = (license_x *)tmp->data;
1069                 if (lcn == NULL)
1070                         continue;
1071                 if (lcn->lang)
1072                         locale = g_list_insert_sorted_with_data(
1073                                         locale, (gpointer)lcn->lang,
1074                                         __comparefunc, NULL);
1075         }
1076         for (tmp = icns; tmp; tmp = tmp->next) {
1077                 icn = (icon_x *)tmp->data;
1078                 if (icn == NULL)
1079                         continue;
1080                 if (icn->lang)
1081                         locale = g_list_insert_sorted_with_data(
1082                                         locale, (gpointer)icn->lang,
1083                                         __comparefunc, NULL);
1084         }
1085         for (tmp = dcns; tmp; tmp = tmp->next) {
1086                 dcn = (description_x *)tmp->data;
1087                 if (dcn == NULL)
1088                         continue;
1089                 if (dcn->lang)
1090                         locale = g_list_insert_sorted_with_data(
1091                                         locale, (gpointer)dcn->lang,
1092                                         __comparefunc, NULL);
1093         }
1094         for (tmp = aths; tmp; tmp = tmp->next) {
1095                 ath = (author_x *)tmp->data;
1096                 if (ath == NULL)
1097                         continue;
1098                 if (ath->lang)
1099                         locale = g_list_insert_sorted_with_data(
1100                                         locale, (gpointer)ath->lang,
1101                                         __comparefunc, NULL);
1102         }
1103         __trimfunc(locale);
1104         return locale;
1105 }
1106
1107 static gint __check_icon_resolution(const char *orig_icon_path,
1108                 char **new_icon_path)
1109 {
1110         int ret;
1111         char *dpi_path[2];
1112         char *icon_filename;
1113         char modified_iconpath[BUFSIZE];
1114         char icon_path[BUFSIZE];
1115         int i;
1116         int dpi = -1;
1117
1118         if (orig_icon_path == NULL)
1119                 return -1;
1120
1121         ret = system_info_get_platform_int(
1122                         "http://tizen.org/feature/screen.dpi", &dpi);
1123         if (ret != SYSTEM_INFO_ERROR_NONE)
1124                 return -1;
1125
1126         if (dpi >= LDPI_MIN && dpi <= LDPI_MAX) {
1127                 dpi_path[0] = "LDPI";
1128                 dpi_path[1] = "ldpi";
1129         } else if (dpi >= MDPI_MIN && dpi <= MDPI_MAX) {
1130                 dpi_path[0] = "MDPI";
1131                 dpi_path[1] = "mdpi";
1132         } else if (dpi >= HDPI_MIN && dpi <= HDPI_MAX) {
1133                 dpi_path[0] = "HDPI";
1134                 dpi_path[1] = "hdpi";
1135         } else if (dpi >= XHDPI_MIN && dpi <= XHDPI_MAX) {
1136                 dpi_path[0] = "XHDPI";
1137                 dpi_path[1] = "xhdpi";
1138         } else if (dpi >= XXHDPI_MIN && dpi <= XXHDPI_MAX) {
1139                 dpi_path[0] = "XXHDPI";
1140                 dpi_path[1] = "xxhdpi";
1141         } else {
1142                 _LOGE("Unidentified dpi[%d]", dpi);
1143                 return -1;
1144         }
1145
1146         icon_filename = strrchr(orig_icon_path, '/');
1147         if (icon_filename == NULL)
1148                 return -1;
1149
1150         snprintf(icon_path,
1151                         strlen(orig_icon_path) - (strlen(icon_filename) - 1),
1152                         "%s", orig_icon_path);
1153         for (i = 0; i < 2; i++) {
1154                 snprintf(modified_iconpath, BUFSIZE - 1, "%s/%s%s",
1155                                 icon_path, dpi_path[i], icon_filename);
1156                 if (access(modified_iconpath, F_OK) != -1) {
1157                         /* if exists, return modified icon path */
1158                         *new_icon_path = strdup(modified_iconpath);
1159                         return 0;
1160                 }
1161         }
1162
1163         return -1;
1164 }
1165
1166 static gint __compare_icon(gconstpointer a, gconstpointer b)
1167 {
1168         icon_x *icon = (icon_x *)a;
1169         char *icon_path;
1170
1171         if (icon->lang != NULL && strcasecmp(icon->lang, DEFAULT_LOCALE) != 0)
1172                 return -1;
1173
1174         if (icon->dpi != NULL)
1175                 return -1;
1176
1177         if (__check_icon_resolution(icon->text, &icon_path) == 0) {
1178                 free(icon->text);
1179                 icon->text = icon_path;
1180         }
1181
1182         return 0;
1183 }
1184
1185 static gint __compare_icon_with_dpi(gconstpointer a, gconstpointer b)
1186 {
1187         icon_x *icon = (icon_x *)a;
1188         int dpi = GPOINTER_TO_INT(b);
1189
1190         if (icon->lang != NULL && strcasecmp(icon->lang, DEFAULT_LOCALE) != 0)
1191                 return -1;
1192
1193         if (icon->dpi == NULL)
1194                 return -1;
1195
1196         if (__check_dpi(icon->dpi, dpi) == 0)
1197                 return 0;
1198
1199         return -1;
1200 }
1201
1202 static gint __compare_icon_with_lang(gconstpointer a, gconstpointer b)
1203 {
1204         icon_x *icon = (icon_x *)a;
1205         char *lang = (char *)b;
1206         char *icon_path;
1207
1208         if (icon->dpi != NULL)
1209                 return -1;
1210
1211         if (strcasecmp(icon->lang, lang) == 0) {
1212                 if (strcasecmp(icon->lang, DEFAULT_LOCALE) == 0) {
1213                         /* icon for no locale. check existance of
1214                          * folder-hierachied default icons
1215                          */
1216                         if (__check_icon_resolution(icon->text,
1217                                                 &icon_path) == 0) {
1218                                 free(icon->text);
1219                                 icon->text = icon_path;
1220                         }
1221                 }
1222                 return 0;
1223         }
1224
1225         return -1;
1226 }
1227
1228 static gint __compare_icon_with_lang_dpi(gconstpointer a, gconstpointer b)
1229 {
1230         int ret;
1231         icon_x *icon = (icon_x *)a;
1232         char *lang = (char *)b;
1233         int dpi = -1;
1234
1235         ret = system_info_get_platform_int(
1236                         "http://tizen.org/feature/screen.dpi", &dpi);
1237         if (ret != SYSTEM_INFO_ERROR_NONE)
1238                 return -1;
1239
1240         if (strcasecmp(icon->lang, lang) == 0 &&
1241                         __check_dpi(icon->dpi, dpi) == 0)
1242                 return 0;
1243
1244         return -1;
1245 }
1246
1247 static char *__find_icon(GList *icons, const char *lang)
1248 {
1249         GList *tmp;
1250         icon_x *icon;
1251         int dpi = 0;
1252         int ret;
1253
1254         /* first, find icon whose locale and dpi with given lang and
1255          * system's dpi has matched
1256          */
1257         tmp = g_list_find_custom(icons, lang,
1258                         (GCompareFunc)__compare_icon_with_lang_dpi);
1259         if (tmp != NULL) {
1260                 icon = (icon_x *)tmp->data;
1261                 return (char *)icon->text;
1262         }
1263
1264         /* if first has failed, find icon whose locale has matched */
1265         tmp = g_list_find_custom(icons, lang,
1266                         (GCompareFunc)__compare_icon_with_lang);
1267         if (tmp != NULL) {
1268                 icon = (icon_x *)tmp->data;
1269                 return (char *)icon->text;
1270         }
1271
1272         /* if second has failed, find icon whose dpi has matched with
1273          * system's dpi
1274          */
1275         ret = system_info_get_platform_int(
1276                         "http://tizen.org/feature/screen.dpi", &dpi);
1277         if (ret == SYSTEM_INFO_ERROR_NONE) {
1278                 tmp = g_list_find_custom(icons, GINT_TO_POINTER(dpi),
1279                                 (GCompareFunc)__compare_icon_with_dpi);
1280                 if (tmp != NULL) {
1281                         icon = (icon_x *)tmp->data;
1282                         return (char *)icon->text;
1283                 }
1284         }
1285
1286         /* last, find default icon marked as "No Locale" */
1287         tmp = g_list_find_custom(icons, NULL, (GCompareFunc)__compare_icon);
1288         if (tmp != NULL) {
1289                 icon = (icon_x *)tmp->data;
1290                 return (char *)icon->text;
1291         }
1292
1293         return NULL;
1294 }
1295
1296 static void __extract_data(const char *locale, GList *lbls, GList *lcns,
1297                 GList *icns, GList *dcns, GList *aths, char **label,
1298                 char **license, char **icon, char **description, char **author)
1299 {
1300         GList *tmp;
1301         label_x *lbl;
1302         license_x *lcn;
1303         description_x *dcn;
1304         author_x *ath;
1305
1306         for (tmp = lbls; tmp; tmp = tmp->next) {
1307                 lbl = (label_x *)tmp->data;
1308                 if (lbl == NULL)
1309                         continue;
1310                 if (lbl->lang) {
1311                         if (strcmp(lbl->lang, locale) == 0) {
1312                                 *label = (char *)lbl->text;
1313                                 break;
1314                         }
1315                 }
1316         }
1317         for (tmp = lcns; tmp; tmp = tmp->next) {
1318                 lcn = (license_x *)tmp->data;
1319                 if (lcn == NULL)
1320                         continue;
1321                 if (lcn->lang) {
1322                         if (strcmp(lcn->lang, locale) == 0) {
1323                                 *license = (char *)lcn->text;
1324                                 break;
1325                         }
1326                 }
1327         }
1328
1329         *icon = __find_icon(icns, locale);
1330
1331         for (tmp = dcns; tmp; tmp = tmp->next) {
1332                 dcn = (description_x *)tmp->data;
1333                 if (dcn == NULL)
1334                         continue;
1335                 if (dcn->lang) {
1336                         if (strcmp(dcn->lang, locale) == 0) {
1337                                 *description = (char *)dcn->text;
1338                                 break;
1339                         }
1340                 }
1341         }
1342         for (tmp = aths; tmp; tmp = tmp->next) {
1343                 ath = (author_x *)tmp->data;
1344                 if (ath == NULL)
1345                         continue;
1346                 if (ath->lang) {
1347                         if (strcmp(ath->lang, locale) == 0) {
1348                                 *author = (char *)ath->text;
1349                                 break;
1350                         }
1351                 }
1352         }
1353 }
1354
1355 static int __insert_mainapp_localized_info(sqlite3 *db, application_x *app,
1356                 const char *locale, const char *label, const char *icon)
1357 {
1358         static const char query[] =
1359                 "INSERT OR REPLACE INTO package_localized_info ("
1360                 "  package, package_locale, package_label, package_icon,"
1361                 "  package_description, package_license, package_author) "
1362                 "VALUES (?, ?,"
1363                 "  COALESCE((SELECT package_label FROM package_localized_info"
1364                 "            WHERE package=? AND package_locale=?), ?),"
1365                 "  COALESCE((SELECT package_icon FROM package_localized_info"
1366                 "            WHERE package=? AND package_icon=?), ?),"
1367                 "  (SELECT package_description FROM package_localized_info"
1368                 "   WHERE package=? AND package_locale=?),"
1369                 "  (SELECT package_description FROM package_localized_info"
1370                 "   WHERE package=? AND package_locale=?),"
1371                 "  (SELECT package_description FROM package_localized_info"
1372                 "   WHERE package=? AND package_locale=?))";
1373         int ret;
1374         sqlite3_stmt *stmt;
1375         int idx = 1;
1376
1377         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
1378         if (ret != SQLITE_OK) {
1379                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
1380                 return -1;
1381         }
1382
1383         __BIND_TEXT(db, stmt, idx++, app->package);
1384         __BIND_TEXT(db, stmt, idx++, locale);
1385         __BIND_TEXT(db, stmt, idx++, app->package);
1386         __BIND_TEXT(db, stmt, idx++, locale);
1387         __BIND_TEXT(db, stmt, idx++, label);
1388         __BIND_TEXT(db, stmt, idx++, app->package);
1389         __BIND_TEXT(db, stmt, idx++, locale);
1390         __BIND_TEXT(db, stmt, idx++, icon);
1391         __BIND_TEXT(db, stmt, idx++, app->package);
1392         __BIND_TEXT(db, stmt, idx++, locale);
1393         __BIND_TEXT(db, stmt, idx++, app->package);
1394         __BIND_TEXT(db, stmt, idx++, locale);
1395         __BIND_TEXT(db, stmt, idx++, app->package);
1396         __BIND_TEXT(db, stmt, idx++, locale);
1397
1398         ret = sqlite3_step(stmt);
1399         if (ret != SQLITE_DONE) {
1400                 _LOGE("step failed: %s", sqlite3_errmsg(db));
1401                 sqlite3_finalize(stmt);
1402                 return -1;
1403         }
1404
1405         sqlite3_finalize(stmt);
1406
1407         return 0;
1408 }
1409
1410 static int __insert_app_localized_info(sqlite3 *db, application_x *app)
1411 {
1412         static const char query[] =
1413                 "INSERT INTO package_app_localized_info (app_id, app_locale,"
1414                 "  app_label, app_icon) "
1415                 "VALUES (?, ?, ?, ?)";
1416         int ret;
1417         sqlite3_stmt *stmt;
1418         int idx;
1419         GList *tmp;
1420         GList *locales;
1421         const char *locale;
1422         char *label;
1423         char *icon;
1424
1425         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
1426         if (ret != SQLITE_OK) {
1427                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
1428                 return -1;
1429         }
1430
1431         locales = __create_locale_list(app->label, NULL, app->icon, NULL, NULL);
1432         for (tmp = locales; tmp; tmp = tmp->next) {
1433                 locale = (const char *)tmp->data;
1434                 label = NULL;
1435                 icon = NULL;
1436                 __extract_data(locale, app->label, NULL, app->icon, NULL, NULL,
1437                                 &label, NULL, &icon, NULL, NULL);
1438                 if (!label && !icon)
1439                         continue;
1440
1441                 idx = 1;
1442                 __BIND_TEXT(db, stmt, idx++, app->appid);
1443                 __BIND_TEXT(db, stmt, idx++, locale);
1444                 __BIND_TEXT(db, stmt, idx++, label);
1445                 __BIND_TEXT(db, stmt, idx++, icon);
1446
1447                 ret = sqlite3_step(stmt);
1448                 if (ret != SQLITE_DONE) {
1449                         _LOGE("step failed: %s", sqlite3_errmsg(db));
1450                         g_list_free(locales);
1451                         sqlite3_finalize(stmt);
1452                         return -1;
1453                 }
1454
1455                 sqlite3_reset(stmt);
1456
1457                 if (strcasecmp(app->mainapp, "true") == 0) {
1458                         if (__insert_mainapp_localized_info(db, app, locale,
1459                                                 label, icon))
1460                                 _LOGE("insert mainapp localized info failed");
1461                 }
1462         }
1463
1464         g_list_free(locales);
1465         sqlite3_finalize(stmt);
1466
1467         return 0;
1468 }
1469
1470 static int __insert_package_privilege_info(sqlite3 *db, manifest_x *mfx)
1471 {
1472         static const char query[] =
1473                 "INSERT INTO package_privilege_info (package, privilege, type) "
1474                 "VALUES (?, ?, ?)";
1475         int ret;
1476         sqlite3_stmt *stmt;
1477         int idx;
1478         GList *tmp;
1479         privilege_x *priv;
1480
1481         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
1482         if (ret != SQLITE_OK) {
1483                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
1484                 return -1;
1485         }
1486
1487         for (tmp = mfx->privileges; tmp; tmp = tmp->next) {
1488                 priv = (privilege_x *)tmp->data;
1489                 if (priv == NULL)
1490                         continue;
1491
1492                 idx = 1;
1493                 __BIND_TEXT(db, stmt, idx++, mfx->package);
1494                 __BIND_TEXT(db, stmt, idx++, priv->value);
1495                 __BIND_TEXT(db, stmt, idx++, priv->type);
1496
1497                 ret = sqlite3_step(stmt);
1498                 if (ret != SQLITE_DONE) {
1499                         _LOGE("step failed: %s", sqlite3_errmsg(db));
1500                         sqlite3_finalize(stmt);
1501                         return -1;
1502                 }
1503                 sqlite3_reset(stmt);
1504         }
1505
1506         sqlite3_finalize(stmt);
1507
1508         return 0;
1509 }
1510
1511 /* _PRODUCT_LAUNCHING_ENHANCED_
1512  *  app->indicatordisplay, app->portraitimg, app->landscapeimg,
1513  *  app->guestmode_appstatus
1514  */
1515 static int __insert_application_info(sqlite3 *db, manifest_x *mfx)
1516 {
1517         static const char query[] =
1518                 "INSERT INTO package_app_info (app_id, app_component,"
1519                 "  app_exec, app_nodisplay, app_type, app_onboot, app_multiple,"
1520                 "  app_autorestart, app_taskmanage, app_hwacceleration,"
1521                 "  app_screenreader, app_mainapp, app_recentimage,"
1522                 "  app_launchcondition, app_indicatordisplay, app_portraitimg,"
1523                 "  app_landscapeimg, app_guestmodevisibility,"
1524                 "  app_permissiontype, app_preload, app_submode,"
1525                 "  app_submode_mainid, app_installed_storage, app_process_pool,"
1526                 "  app_launch_mode, app_ui_gadget, app_support_mode,"
1527                 "  app_support_disable, component_type, package, app_tep_name,"
1528                 "  app_zip_mount_file, app_background_category,"
1529                 "  app_package_type, app_root_path, app_api_version,"
1530                 "  app_effective_appid, app_splash_screen_display,"
1531                 "  app_package_system, app_removable,"
1532                 "  app_package_installed_time, app_support_ambient,"
1533                 "  app_setup_appid) "
1534                 "VALUES (?, ?, "
1535                 "  ?, LOWER(?), ?, LOWER(?), LOWER(?),"
1536                 "  LOWER(?), LOWER(?), ?,"
1537                 "  ?, LOWER(?), ?,"
1538                 "  ?, LOWER(?), ?,"
1539                 "  ?, LOWER(?),"
1540                 "  ?, LOWER(?), LOWER(?),"
1541                 "  ?, ?, LOWER(?),"
1542                 "  COALESCE(?, 'single'), LOWER(?), ?,"
1543                 "  LOWER(?), ?, ?, ?,"
1544                 "  ?, ?,"
1545                 "  ?, ?, ?,"
1546                 "  ?, LOWER(?),"
1547                 "  LOWER(?), LOWER(?),"
1548                 "  ?, LOWER(?),"
1549                 "  ?)";
1550         int ret;
1551         sqlite3_stmt *stmt;
1552         int idx;
1553         GList *tmp;
1554         application_x *app;
1555         int bg_category;
1556         const char *effective_appid;
1557
1558         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
1559         if (ret != SQLITE_OK) {
1560                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
1561                 return -1;
1562         }
1563
1564         for (tmp = mfx->application; tmp; tmp = tmp->next) {
1565                 app = (application_x *)tmp->data;
1566                 if (app == NULL)
1567                         continue;
1568
1569                 bg_category = __convert_background_category(
1570                                 app->background_category);
1571                 effective_appid = __find_effective_appid(app->metadata);
1572
1573                 idx = 1;
1574                 __BIND_TEXT(db, stmt, idx++, app->appid);
1575                 __BIND_TEXT(db, stmt, idx++, app->component_type);
1576                 __BIND_TEXT(db, stmt, idx++, app->exec);
1577                 __BIND_TEXT(db, stmt, idx++, __get_bool(app->nodisplay, false));
1578                 __BIND_TEXT(db, stmt, idx++, app->type);
1579                 __BIND_TEXT(db, stmt, idx++, __get_bool(app->onboot, false));
1580                 __BIND_TEXT(db, stmt, idx++, __get_bool(app->multiple, false));
1581                 __BIND_TEXT(db, stmt, idx++,
1582                                 __get_bool(app->autorestart, false));
1583                 __BIND_TEXT(db, stmt, idx++,
1584                                 __get_bool(app->taskmanage, false));
1585                 __BIND_TEXT(db, stmt, idx++, app->hwacceleration);
1586                 __BIND_TEXT(db, stmt, idx++, app->screenreader);
1587                 __BIND_TEXT(db, stmt, idx++, __get_bool(app->mainapp, false));
1588                 __BIND_TEXT(db, stmt, idx++, app->recentimage);
1589                 __BIND_TEXT(db, stmt, idx++, app->launchcondition);
1590                 __BIND_TEXT(db, stmt, idx++,
1591                                 __get_bool(app->indicatordisplay, true));
1592                 __BIND_TEXT(db, stmt, idx++, app->portraitimg);
1593                 __BIND_TEXT(db, stmt, idx++, app->landscapeimg);
1594                 __BIND_TEXT(db, stmt, idx++,
1595                                 __get_bool(app->guestmode_visibility, true));
1596                 __BIND_TEXT(db, stmt, idx++, app->permission_type);
1597                 __BIND_TEXT(db, stmt, idx++, __get_bool(mfx->preload, false));
1598                 __BIND_TEXT(db, stmt, idx++, __get_bool(app->submode, false));
1599                 __BIND_TEXT(db, stmt, idx++, app->submode_mainid);
1600                 __BIND_TEXT(db, stmt, idx++, mfx->installed_storage);
1601                 __BIND_TEXT(db, stmt, idx++,
1602                                 __get_bool(app->process_pool, false));
1603                 __BIND_TEXT(db, stmt, idx++, app->launch_mode);
1604                 __BIND_TEXT(db, stmt, idx++, __get_bool(app->ui_gadget, false));
1605                 __BIND_TEXT(db, stmt, idx++, app->support_mode);
1606                 __BIND_TEXT(db, stmt, idx++,
1607                                 __get_bool(mfx->support_disable, false));
1608                 __BIND_TEXT(db, stmt, idx++, app->component_type);
1609                 __BIND_TEXT(db, stmt, idx++, mfx->package);
1610                 __BIND_TEXT(db, stmt, idx++, mfx->tep_name);
1611                 __BIND_TEXT(db, stmt, idx++, mfx->zip_mount_file);
1612                 __BIND_INT(db, stmt, idx++, bg_category);
1613                 __BIND_TEXT(db, stmt, idx++, mfx->type ? mfx->type : "tpk");
1614                 __BIND_TEXT(db, stmt, idx++, mfx->root_path);
1615                 __BIND_TEXT(db, stmt, idx++, mfx->api_version);
1616                 __BIND_TEXT(db, stmt, idx++, effective_appid);
1617                 __BIND_TEXT(db, stmt, idx++,
1618                                 __get_bool(app->splash_screen_display, true));
1619                 __BIND_TEXT(db, stmt, idx++, __get_bool(mfx->system, false));
1620                 __BIND_TEXT(db, stmt, idx++, __get_bool(mfx->removable, false));
1621                 __BIND_TEXT(db, stmt, idx++, mfx->installed_time);
1622                 __BIND_TEXT(db, stmt, idx++,
1623                                 __get_bool(app->support_ambient, false));
1624                 __BIND_TEXT(db, stmt, idx++, app->setup_appid);
1625
1626                 ret = sqlite3_step(stmt);
1627                 if (ret != SQLITE_DONE) {
1628                         _LOGE("step failed: %s", sqlite3_errmsg(db));
1629                         sqlite3_finalize(stmt);
1630                         return -1;
1631                 }
1632
1633                 sqlite3_reset(stmt);
1634
1635                 if (__insert_appcontrol_info(db, app)) {
1636                         sqlite3_finalize(stmt);
1637                         return -1;
1638                 }
1639                 if (__insert_category_info(db, app)) {
1640                         sqlite3_finalize(stmt);
1641                         return -1;
1642                 }
1643                 if (__insert_metadata_info(db, app)) {
1644                         sqlite3_finalize(stmt);
1645                         return -1;
1646                 }
1647                 if (__insert_datacontrol_info(db, app)) {
1648                         sqlite3_finalize(stmt);
1649                         return -1;
1650                 }
1651                 if (__insert_splashscreen_info(db, app)) {
1652                         sqlite3_finalize(stmt);
1653                         return -1;
1654                 }
1655                 if (__insert_app_localized_info(db, app)) {
1656                         sqlite3_finalize(stmt);
1657                         return -1;
1658                 }
1659         }
1660
1661         sqlite3_finalize(stmt);
1662
1663         return 0;
1664 }
1665
1666 static int __insert_package_update_info(sqlite3 *db, manifest_x *mfx)
1667 {
1668         static const char query[] =
1669                 "INSERT INTO package_update_info (package, update_version) "
1670                 "VALUES (?, ?)";
1671         int ret;
1672         int idx;
1673         sqlite3_stmt *stmt;
1674
1675         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
1676         if (ret != SQLITE_OK) {
1677                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
1678                 return -1;
1679         }
1680
1681         idx = 1;
1682         __BIND_TEXT(db, stmt, idx++, mfx->package);
1683         __BIND_TEXT(db, stmt, idx, mfx->version);
1684         ret = sqlite3_step(stmt);
1685         if (ret != SQLITE_DONE) {
1686                 _LOGE("step failed: %s", sqlite3_errmsg(db));
1687                 sqlite3_finalize(stmt);
1688                 return -1;
1689         }
1690         sqlite3_finalize(stmt);
1691
1692         return 0;
1693 }
1694
1695 static int __insert_package_localized_info(sqlite3 *db, manifest_x *mfx)
1696 {
1697         static const char query[] =
1698                 "INSERT INTO package_localized_info (package, package_locale,"
1699                 "  package_label, package_icon, package_description,"
1700                 "  package_license, package_author) "
1701                 "VALUES (?, ?, ?, ?, ?, ?, ?)";
1702         int ret;
1703         sqlite3_stmt *stmt;
1704         int idx;
1705         GList *tmp;
1706         GList *locales;
1707         const char *locale;
1708         char *label;
1709         char *icon;
1710         char *description;
1711         char *license;
1712         char *author;
1713
1714         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
1715         if (ret != SQLITE_OK) {
1716                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
1717                 return -1;
1718         }
1719
1720         locales = __create_locale_list(mfx->label, mfx->license, mfx->icon,
1721                         mfx->description, mfx->author);
1722         for (tmp = locales; tmp; tmp = tmp->next) {
1723                 locale = (const char *)tmp->data;
1724                 label = NULL;
1725                 icon = NULL;
1726                 description = NULL;
1727                 license = NULL;
1728                 author = NULL;
1729                 __extract_data(locale, mfx->label, mfx->license, mfx->icon,
1730                                 mfx->description, mfx->author,
1731                                 &label, &license, &icon, &description, &author);
1732                 if (!label && !license && !icon && !description && !author)
1733                         continue;
1734
1735                 idx = 1;
1736                 __BIND_TEXT(db, stmt, idx++, mfx->package);
1737                 __BIND_TEXT(db, stmt, idx++, locale);
1738                 __BIND_TEXT(db, stmt, idx++, label);
1739                 __BIND_TEXT(db, stmt, idx++, icon);
1740                 __BIND_TEXT(db, stmt, idx++, description);
1741                 __BIND_TEXT(db, stmt, idx++, license);
1742                 __BIND_TEXT(db, stmt, idx++, author);
1743
1744                 ret = sqlite3_step(stmt);
1745                 if (ret != SQLITE_DONE) {
1746                         _LOGE("step failed: %s", sqlite3_errmsg(db));
1747                         g_list_free(locales);
1748                         sqlite3_finalize(stmt);
1749                         return -1;
1750                 }
1751
1752                 sqlite3_reset(stmt);
1753         }
1754
1755         g_list_free(locales);
1756         sqlite3_finalize(stmt);
1757
1758         return 0;
1759 }
1760
1761 static int __insert_package_info(sqlite3 *db, manifest_x *mfx)
1762 {
1763         static const char query[] =
1764                 "INSERT INTO package_info (package, package_type,"
1765                 "  package_version, package_api_version, package_tep_name,"
1766                 "  package_zip_mount_file, install_location, package_size,"
1767                 "  package_removable, package_preload, package_readonly,"
1768                 "  package_update, package_appsetting, package_nodisplay,"
1769                 "  package_system, author_name, author_email, author_href,"
1770                 "  installed_time, installed_storage, storeclient_id,"
1771                 "  mainapp_id, package_url, root_path, external_path,"
1772                 "  csc_path, package_support_mode, package_support_disable) "
1773                 "VALUES (?, ?,"
1774                 "  ?, ?, ?,"
1775                 "  ?, ?, ?,"
1776                 "  LOWER(?), LOWER(?), LOWER(?),"
1777                 "  LOWER(?), LOWER(?), LOWER(?),"
1778                 "  LOWER(?), ?, ?, ?,"
1779                 "  ?, ?, ?,"
1780                 "  ?, ?, ?, ?,"
1781                 "  ?, ?, LOWER(?))";
1782         int ret;
1783         sqlite3_stmt *stmt;
1784         int idx = 1;
1785         const char *author_name = NULL;
1786         const char *author_email = NULL;
1787         const char *author_href = NULL;
1788
1789         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
1790         if (ret != SQLITE_OK) {
1791                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
1792                 return -1;
1793         }
1794
1795         if (mfx->author && mfx->author->data) {
1796                 author_name = ((author_x *)mfx->author->data)->text;
1797                 author_email = ((author_x *)mfx->author->data)->email;
1798                 author_href = ((author_x *)mfx->author->data)->href;
1799         }
1800
1801         __BIND_TEXT(db, stmt, idx++, mfx->package);
1802         __BIND_TEXT(db, stmt, idx++, mfx->type);
1803         __BIND_TEXT(db, stmt, idx++, mfx->version);
1804         __BIND_TEXT(db, stmt, idx++, mfx->api_version);
1805         __BIND_TEXT(db, stmt, idx++, mfx->tep_name);
1806         __BIND_TEXT(db, stmt, idx++, mfx->zip_mount_file);
1807         __BIND_TEXT(db, stmt, idx++, mfx->installlocation);
1808         __BIND_TEXT(db, stmt, idx++, mfx->package_size);
1809         __BIND_TEXT(db, stmt, idx++, __get_bool(mfx->removable, true));
1810         __BIND_TEXT(db, stmt, idx++, __get_bool(mfx->preload, false));
1811         __BIND_TEXT(db, stmt, idx++, __get_bool(mfx->readonly, false));
1812         __BIND_TEXT(db, stmt, idx++, __get_bool(mfx->update, false));
1813         __BIND_TEXT(db, stmt, idx++, __get_bool(mfx->appsetting, false));
1814         __BIND_TEXT(db, stmt, idx++, __get_bool(mfx->nodisplay_setting, false));
1815         __BIND_TEXT(db, stmt, idx++, __get_bool(mfx->system, false));
1816         __BIND_TEXT(db, stmt, idx++, author_name);
1817         __BIND_TEXT(db, stmt, idx++, author_email);
1818         __BIND_TEXT(db, stmt, idx++, author_href);
1819         __BIND_TEXT(db, stmt, idx++, mfx->installed_time);
1820         __BIND_TEXT(db, stmt, idx++, mfx->installed_storage);
1821         __BIND_TEXT(db, stmt, idx++, mfx->storeclient_id);
1822         __BIND_TEXT(db, stmt, idx++, mfx->mainapp_id);
1823         __BIND_TEXT(db, stmt, idx++, mfx->package_url);
1824         __BIND_TEXT(db, stmt, idx++, mfx->root_path);
1825         __BIND_TEXT(db, stmt, idx++, mfx->external_path);
1826         __BIND_TEXT(db, stmt, idx++, mfx->csc_path);
1827         __BIND_TEXT(db, stmt, idx++, mfx->support_mode);
1828         __BIND_TEXT(db, stmt, idx++, __get_bool(mfx->support_disable, false));
1829
1830         ret = sqlite3_step(stmt);
1831         if (ret != SQLITE_DONE) {
1832                 _LOGE("step failed: %s", sqlite3_errmsg(db));
1833                 sqlite3_finalize(stmt);
1834                 return -1;
1835         }
1836
1837         sqlite3_finalize(stmt);
1838
1839         if (__insert_package_update_info(db, mfx))
1840                 return -1;
1841         if (__insert_package_localized_info(db, mfx))
1842                 return -1;
1843         if (__insert_application_info(db, mfx))
1844                 return -1;
1845         if (__insert_package_privilege_info(db, mfx))
1846                 return -1;
1847
1848         return 0;
1849 }
1850
1851 API int pkgmgr_parser_insert_manifest_info_in_usr_db(manifest_x *mfx, uid_t uid)
1852 {
1853         int ret;
1854         const char *dbpath;
1855         sqlite3 *db;
1856
1857         if (mfx == NULL) {
1858                 _LOGE("invalid parameter");
1859                 return PM_PARSER_R_EINVAL;
1860         }
1861
1862         dbpath = __get_parser_db_path(uid);
1863
1864         ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE);
1865         if (ret != SQLITE_OK) {
1866                 _LOGE("open db failed: %d", ret);
1867                 return PM_PARSER_R_ERROR;
1868         }
1869
1870         __BEGIN_TRANSACTION(db);
1871         __DO_TRANSACTION(db, __insert_package_info(db, mfx));
1872         __END_TRANSACTION(db);
1873
1874         sqlite3_close_v2(db);
1875
1876         return PM_PARSER_R_OK;
1877 }
1878
1879 API int pkgmgr_parser_insert_manifest_info_in_db(manifest_x *mfx)
1880 {
1881         return pkgmgr_parser_insert_manifest_info_in_usr_db(mfx, __getuid());
1882 }
1883
1884 static int __delete_package_info(sqlite3 *db, const char *pkgid)
1885 {
1886         static const char query[] =
1887                 "DELETE FROM package_info WHERE package=?";
1888         int ret;
1889         sqlite3_stmt *stmt;
1890
1891         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
1892         if (ret != SQLITE_OK) {
1893                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
1894                 return -1;
1895         }
1896
1897         __BIND_TEXT(db, stmt, 1, pkgid);
1898
1899         ret = sqlite3_step(stmt);
1900         if (ret != SQLITE_DONE) {
1901                 _LOGE("step failed: %s", sqlite3_errmsg(db));
1902                 sqlite3_finalize(stmt);
1903                 return -1;
1904         }
1905
1906         sqlite3_finalize(stmt);
1907
1908         return 0;
1909 }
1910
1911 API int pkgmgr_parser_delete_manifest_info_from_usr_db(manifest_x *mfx,
1912         uid_t uid)
1913 {
1914         int ret;
1915         const char *dbpath;
1916         sqlite3 *db;
1917
1918         if (mfx == NULL) {
1919                 _LOGE("invalid parameter");
1920                 return PM_PARSER_R_EINVAL;
1921         }
1922
1923         dbpath = __get_parser_db_path(uid);
1924
1925         ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE);
1926         if (ret != SQLITE_OK) {
1927                 _LOGE("open db failed: %d", ret);
1928                 return PM_PARSER_R_ERROR;
1929         }
1930
1931         __BEGIN_TRANSACTION(db);
1932         __DO_TRANSACTION(db, __delete_package_info(db, mfx->package));
1933         __END_TRANSACTION(db);
1934
1935         sqlite3_close_v2(db);
1936
1937         return PM_PARSER_R_OK;
1938 }
1939
1940 API int pkgmgr_parser_delete_manifest_info_from_db(manifest_x *mfx)
1941 {
1942         return pkgmgr_parser_delete_manifest_info_from_usr_db(mfx, __getuid());
1943 }
1944
1945 API int pkgmgr_parser_update_manifest_info_in_usr_db(manifest_x *mfx, uid_t uid)
1946 {
1947         int ret;
1948         const char *dbpath;
1949         sqlite3 *db;
1950
1951         if (mfx == NULL) {
1952                 _LOGE("invalid parameter");
1953                 return PM_PARSER_R_EINVAL;
1954         }
1955
1956         dbpath = __get_parser_db_path(uid);
1957
1958         ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE);
1959         if (ret != SQLITE_OK) {
1960                 _LOGE("open db failed: %d", ret);
1961                 return PM_PARSER_R_ERROR;
1962         }
1963
1964         __BEGIN_TRANSACTION(db);
1965         __DO_TRANSACTION(db, __delete_package_info(db, mfx->package));
1966         __DO_TRANSACTION(db, __insert_package_info(db, mfx));
1967         __END_TRANSACTION(db);
1968
1969         sqlite3_close_v2(db);
1970
1971         return PM_PARSER_R_OK;
1972 }
1973
1974 API int pkgmgr_parser_update_manifest_info_in_db(manifest_x *mfx)
1975 {
1976         return pkgmgr_parser_update_manifest_info_in_usr_db(mfx, __getuid());
1977 }
1978
1979 static int __set_global_app_disable_for_uid(sqlite3 *db, const char *appid,
1980                 uid_t uid, bool is_disable)
1981 {
1982         static const char query[] =
1983                 "INSERT OR REPLACE INTO package_app_info_for_uid ("
1984                 "  app_id, uid, is_disabled, is_splash_screen_enabled) "
1985                 "VALUES (?, ?, ?,"
1986                 "  (SELECT app_splash_screen_display FROM package_app_info"
1987                 "   WHERE app_id=?))";
1988         int ret;
1989         sqlite3_stmt *stmt;
1990         int idx = 1;
1991
1992         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
1993         if (ret != SQLITE_OK) {
1994                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
1995                 return -1;
1996         }
1997
1998         __BIND_TEXT(db, stmt, idx++, appid);
1999         __BIND_INT(db, stmt, idx++, uid);
2000         __BIND_TEXT(db, stmt, idx++, is_disable ? "true" : "false");
2001         __BIND_TEXT(db, stmt, idx++, appid);
2002
2003         ret = sqlite3_step(stmt);
2004         if (ret != SQLITE_DONE) {
2005                 _LOGE("step failed: %s", sqlite3_errmsg(db));
2006                 sqlite3_finalize(stmt);
2007                 return -1;
2008         }
2009
2010         sqlite3_finalize(stmt);
2011
2012         return 0;
2013 }
2014
2015 API int pkgmgr_parser_update_global_app_disable_for_uid_info_in_db(
2016                 const char *appid, uid_t uid, int is_disable)
2017 {
2018         int ret;
2019         const char *dbpath;
2020         sqlite3 *db;
2021
2022         if (appid == NULL) {
2023                 _LOGE("invalid parameter");
2024                 return PM_PARSER_R_EINVAL;
2025         }
2026
2027         dbpath = __get_parser_db_path(GLOBAL_USER);
2028
2029         ret = __open_db(GLOBAL_USER, dbpath, &db, SQLITE_OPEN_READWRITE);
2030         if (ret != SQLITE_OK) {
2031                 _LOGE("open db failed: %d", ret);
2032                 return PM_PARSER_R_ERROR;
2033         }
2034
2035         __BEGIN_TRANSACTION(db);
2036         __DO_TRANSACTION(db, __set_global_app_disable_for_uid(db, appid,
2037                                 uid, (bool)is_disable));
2038         __END_TRANSACTION(db);
2039
2040         sqlite3_close_v2(db);
2041
2042         return PM_PARSER_R_OK;
2043 }
2044
2045 static int __set_app_disable(sqlite3 *db, const char *appid, uid_t uid,
2046                 bool is_disable)
2047 {
2048         static const char query[] =
2049                 "UPDATE package_app_info SET app_disable=? "
2050                 "WHERE app_id=?";
2051         int ret;
2052         sqlite3_stmt *stmt;
2053         int idx = 1;
2054
2055         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
2056         if (ret != SQLITE_OK) {
2057                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
2058                 return -1;
2059         }
2060
2061         __BIND_TEXT(db, stmt, idx++, is_disable ? "true" : "false");
2062         __BIND_TEXT(db, stmt, idx++, appid);
2063
2064         ret = sqlite3_step(stmt);
2065         if (ret != SQLITE_DONE) {
2066                 _LOGE("step failed: %s", sqlite3_errmsg(db));
2067                 sqlite3_finalize(stmt);
2068                 return -1;
2069         }
2070
2071         sqlite3_finalize(stmt);
2072
2073         return 0;
2074 }
2075
2076 API int pkgmgr_parser_update_app_disable_info_in_usr_db(const char *appid,
2077                 uid_t uid, int is_disable)
2078 {
2079         int ret;
2080         const char *dbpath;
2081         sqlite3 *db;
2082
2083         if (appid == NULL) {
2084                 _LOGE("invalid parameter");
2085                 return PM_PARSER_R_EINVAL;
2086         }
2087
2088         dbpath = __get_parser_db_path(uid);
2089
2090         ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE);
2091         if (ret != SQLITE_OK) {
2092                 _LOGE("open db failed: %d", ret);
2093                 return PM_PARSER_R_ERROR;
2094         }
2095
2096         __BEGIN_TRANSACTION(db);
2097         __DO_TRANSACTION(db, __set_app_disable(db, appid, uid,
2098                                 (bool)is_disable));
2099         __END_TRANSACTION(db);
2100
2101         sqlite3_close_v2(db);
2102
2103         return PM_PARSER_R_OK;
2104 }
2105
2106 API int pkgmgr_parser_update_app_disable_info_in_db(const char *appid,
2107                 int is_disable)
2108 {
2109         return pkgmgr_parser_update_app_disable_info_in_usr_db(appid,
2110                         __getuid(), is_disable);
2111 }
2112
2113 static int __set_pkg_disable(sqlite3 *db, const char *pkgid, uid_t uid,
2114                 bool is_disable)
2115 {
2116         static const char query[] =
2117                 "UPDATE package_info SET package_disable=? "
2118                 "WHERE package=?";
2119         int ret;
2120         sqlite3_stmt *stmt;
2121         int idx = 1;
2122
2123         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
2124         if (ret != SQLITE_OK) {
2125                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
2126                 return -1;
2127         }
2128
2129         __BIND_TEXT(db, stmt, idx++, is_disable ? "true" : "false");
2130         __BIND_TEXT(db, stmt, idx++, pkgid);
2131
2132         ret = sqlite3_step(stmt);
2133         if (ret != SQLITE_DONE) {
2134                 _LOGE("step failed: %s", sqlite3_errmsg(db));
2135                 sqlite3_finalize(stmt);
2136                 return -1;
2137         }
2138
2139         sqlite3_finalize(stmt);
2140
2141         return 0;
2142 }
2143
2144 API int pkgmgr_parser_update_pkg_disable_info_in_usr_db(const char *pkgid,
2145                 uid_t uid, int is_disable)
2146 {
2147         int ret;
2148         const char *dbpath;
2149         sqlite3 *db;
2150
2151         if (pkgid == NULL) {
2152                 _LOGE("invalid parameter");
2153                 return PM_PARSER_R_EINVAL;
2154         }
2155
2156         dbpath = __get_parser_db_path(uid);
2157
2158         ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE);
2159         if (ret != SQLITE_OK) {
2160                 _LOGE("open db failed: %d", ret);
2161                 return PM_PARSER_R_ERROR;
2162         }
2163
2164         __BEGIN_TRANSACTION(db);
2165         __DO_TRANSACTION(db, __set_pkg_disable(db, pkgid, uid,
2166                                 (bool)is_disable));
2167         __END_TRANSACTION(db);
2168
2169         sqlite3_close_v2(db);
2170
2171         return PM_PARSER_R_OK;
2172 }
2173
2174 API int pkgmgr_parser_update_pkg_disable_info_in_db(const char *pkgid,
2175                 int is_disable)
2176 {
2177         return pkgmgr_parser_update_pkg_disable_info_in_usr_db(pkgid,
2178                         __getuid(), is_disable);
2179 }
2180
2181 static int __set_global_app_splash_screen_for_uid(sqlite3 *db,
2182                 const char *appid, uid_t uid, bool is_enabled)
2183 {
2184         static const char query[] =
2185                 "INSERT OR REPLACE INTO package_app_info_for_uid("
2186                 "  appid, uid, is_splash_screen_enabled) "
2187                 "VALUES (?, ?, ?)";
2188         int ret;
2189         sqlite3_stmt *stmt;
2190         int idx = 1;
2191
2192         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
2193         if (ret != SQLITE_OK) {
2194                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
2195                 return -1;
2196         }
2197
2198         __BIND_TEXT(db, stmt, idx++, appid);
2199         __BIND_INT(db, stmt, idx++, uid);
2200         __BIND_TEXT(db, stmt, idx++, is_enabled ? "true" : "false");
2201
2202         ret = sqlite3_step(stmt);
2203         if (ret != SQLITE_DONE) {
2204                 _LOGE("step failed: %s", sqlite3_errmsg(db));
2205                 sqlite3_finalize(stmt);
2206                 return -1;
2207         }
2208
2209         sqlite3_finalize(stmt);
2210
2211         return 0;
2212 }
2213
2214 API int pkgmgr_parser_update_global_app_splash_screen_display_info_in_usr_db(
2215                 const char *appid, uid_t uid, int flag)
2216 {
2217         int ret;
2218         const char *dbpath;
2219         sqlite3 *db;
2220
2221         if (appid == NULL) {
2222                 _LOGE("invalid parameter");
2223                 return PM_PARSER_R_EINVAL;
2224         }
2225
2226         dbpath = __get_parser_db_path(GLOBAL_USER);
2227
2228         ret = __open_db(GLOBAL_USER, dbpath, &db, SQLITE_OPEN_READWRITE);
2229         if (ret != SQLITE_OK) {
2230                 _LOGE("open db failed: %d", ret);
2231                 return PM_PARSER_R_ERROR;
2232         }
2233
2234         __BEGIN_TRANSACTION(db);
2235         __DO_TRANSACTION(db, __set_global_app_splash_screen_for_uid(db,
2236                                 appid, uid, (bool)flag));
2237         __END_TRANSACTION(db);
2238
2239         sqlite3_close_v2(db);
2240
2241         return PM_PARSER_R_OK;
2242 }
2243
2244 static int __set_app_splash_screen(sqlite3 *db, const char *appid,
2245                 bool is_enabled)
2246 {
2247         static const char query[] =
2248                 "UPDATE package_app_info SET app_splash_screen_display=? "
2249                 "WHERE app_id=?";
2250         int ret;
2251         sqlite3_stmt *stmt;
2252         int idx = 1;
2253
2254         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
2255         if (ret != SQLITE_OK) {
2256                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
2257                 return -1;
2258         }
2259
2260         __BIND_TEXT(db, stmt, idx++, is_enabled ? "true" : "false");
2261         __BIND_TEXT(db, stmt, idx++, appid);
2262
2263         ret = sqlite3_step(stmt);
2264         if (ret != SQLITE_DONE) {
2265                 _LOGE("step failed: %s", sqlite3_errmsg(db));
2266                 sqlite3_finalize(stmt);
2267                 return -1;
2268         }
2269
2270         sqlite3_finalize(stmt);
2271
2272         return 0;
2273 }
2274
2275 API int pkgmgr_parser_update_app_splash_screen_display_info_in_usr_db(
2276                 const char *appid, uid_t uid, int flag)
2277 {
2278         int ret;
2279         const char *dbpath;
2280         sqlite3 *db;
2281
2282         if (appid == NULL) {
2283                 _LOGE("invalid parameter");
2284                 return PM_PARSER_R_EINVAL;
2285         }
2286
2287         dbpath = __get_parser_db_path(uid);
2288
2289         ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE);
2290         if (ret != SQLITE_OK) {
2291                 _LOGE("open db failed: %d", ret);
2292                 return PM_PARSER_R_ERROR;
2293         }
2294
2295         __BEGIN_TRANSACTION(db);
2296         __DO_TRANSACTION(db, __set_app_splash_screen(db, appid, (bool)flag));
2297         __END_TRANSACTION(db);
2298
2299         sqlite3_close_v2(db);
2300
2301         return PM_PARSER_R_OK;
2302 }
2303
2304 API int pkgmgr_parser_update_app_splash_screen_display_info_in_db(
2305                 const char *appid, int flag)
2306 {
2307         return pkgmgr_parser_update_app_splash_screen_display_info_in_usr_db(
2308                         appid, __getuid(), flag);
2309 }
2310
2311 static int __set_app_label(sqlite3 *db, const char *appid, const char *label)
2312 {
2313         static const char query[] =
2314                 "UPDATE package_app_localized_info SET app_label=? "
2315                 "WHERE app_id=? AND app_label IS NOT NULL";
2316         int ret;
2317         sqlite3_stmt *stmt;
2318         int idx = 1;
2319
2320         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
2321         if (ret != SQLITE_OK) {
2322                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
2323                 return -1;
2324         }
2325
2326         __BIND_TEXT(db, stmt, idx++, label);
2327         __BIND_TEXT(db, stmt, idx++, appid);
2328
2329         ret = sqlite3_step(stmt);
2330         if (ret != SQLITE_DONE) {
2331                 _LOGE("step failed: %s", sqlite3_errmsg(db));
2332                 sqlite3_finalize(stmt);
2333                 return -1;
2334         }
2335
2336         sqlite3_finalize(stmt);
2337
2338         return 0;
2339 }
2340
2341 API int pkgmgr_parser_update_app_label_info_in_usr_db(const char *appid,
2342                 uid_t uid, const char *label)
2343 {
2344         int ret;
2345         const char *dbpath;
2346         sqlite3 *db;
2347
2348         if (appid == NULL) {
2349                 _LOGE("invalid parameter");
2350                 return PM_PARSER_R_EINVAL;
2351         }
2352
2353         dbpath = __get_parser_db_path(uid);
2354
2355         ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE);
2356         if (ret != SQLITE_OK) {
2357                 _LOGE("open db failed: %d", ret);
2358                 return PM_PARSER_R_ERROR;
2359         }
2360
2361         __BEGIN_TRANSACTION(db);
2362         __DO_TRANSACTION(db, __set_app_label(db, appid, label));
2363         __END_TRANSACTION(db);
2364
2365         sqlite3_close_v2(db);
2366
2367         return PM_PARSER_R_OK;
2368 }
2369
2370 API int pkgmgr_parser_update_app_label_info_in_db(const char *appid,
2371                 const char *label)
2372 {
2373         return pkgmgr_parser_update_app_label_info_in_usr_db(appid, __getuid(),
2374                         label);
2375 }
2376
2377 static int __set_tep_path(sqlite3 *db, const char *pkgid, const char *tep_path)
2378 {
2379         static const char query[] =
2380                 "UPDATE package_info SET package_tep_name=? "
2381                 "WHERE package=?";
2382         int ret;
2383         sqlite3_stmt *stmt;
2384         int idx = 1;
2385
2386         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
2387         if (ret != SQLITE_OK) {
2388                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
2389                 return -1;
2390         }
2391
2392         __BIND_TEXT(db, stmt, idx++, tep_path);
2393         __BIND_TEXT(db, stmt, idx++, pkgid);
2394
2395         ret = sqlite3_step(stmt);
2396         if (ret != SQLITE_DONE) {
2397                 _LOGE("step failed: %s", sqlite3_errmsg(db));
2398                 sqlite3_finalize(stmt);
2399                 return -1;
2400         }
2401
2402         sqlite3_finalize(stmt);
2403
2404         return 0;
2405 }
2406
2407 API int pkgmgr_parser_update_tep_info_in_usr_db(const char *pkgid,
2408                 const char *tep_path, uid_t uid)
2409 {
2410         int ret;
2411         const char *dbpath;
2412         sqlite3 *db;
2413
2414         if (pkgid == NULL) {
2415                 _LOGE("invalid parameter");
2416                 return PM_PARSER_R_EINVAL;
2417         }
2418
2419         dbpath = __get_parser_db_path(uid);
2420
2421         ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE);
2422         if (ret != SQLITE_OK) {
2423                 _LOGE("open db failed: %d", ret);
2424                 return PM_PARSER_R_ERROR;
2425         }
2426
2427         __BEGIN_TRANSACTION(db);
2428         __DO_TRANSACTION(db, __set_tep_path(db, pkgid, tep_path));
2429         __END_TRANSACTION(db);
2430
2431         sqlite3_close_v2(db);
2432
2433         return PM_PARSER_R_OK;
2434 }
2435
2436 API int pkgmgr_parser_update_tep_info_in_db(const char *pkgid,
2437                 const char *tep_path)
2438 {
2439         return pkgmgr_parser_update_tep_info_in_usr_db(pkgid, tep_path,
2440                         __getuid());
2441 }
2442
2443 static int __convert_update_type(pkgmgrinfo_updateinfo_update_type type,
2444                 const char **update_type)
2445 {
2446         if (type == PMINFO_UPDATEINFO_NONE)
2447                 *update_type = PMINFO_UPDATEINFO_TYPE_NONE;
2448         else if (type == PMINFO_UPDATEINFO_FORCE)
2449                 *update_type = PMINFO_UPDATEINFO_TYPE_FORCE;
2450         else if (type == PMINFO_UPDATEINFO_OPTIONAL)
2451                 *update_type = PMINFO_UPDATEINFO_TYPE_OPTIONAL;
2452         else
2453                 return -1;
2454         return 0;
2455 }
2456
2457 static int __register_pkg_update_info(sqlite3 *db, updateinfo_x *info,
2458                 const char *update_type)
2459 {
2460         static const char query[] =
2461                 "UPDATE package_update_info "
2462                 "SET update_version=?, update_type=? "
2463                 "WHERE package=?";
2464         int ret;
2465         sqlite3_stmt *stmt;
2466         int idx = 1;
2467
2468         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
2469         if (ret != SQLITE_OK) {
2470                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
2471                 return -1;
2472         }
2473
2474         __BIND_TEXT(db, stmt, idx++, info->version);
2475         __BIND_TEXT(db, stmt, idx++, update_type);
2476         __BIND_TEXT(db, stmt, idx++, info->pkgid);
2477
2478         ret = sqlite3_step(stmt);
2479         if (ret != SQLITE_DONE) {
2480                 _LOGE("step failed: %s", sqlite3_errmsg(db));
2481                 sqlite3_finalize(stmt);
2482                 return -1;
2483         }
2484
2485         sqlite3_finalize(stmt);
2486
2487         return 0;
2488 }
2489
2490 API int pkgmgr_parser_register_pkg_update_info_in_usr_db(
2491                 pkgmgrinfo_updateinfo_h handle, uid_t uid)
2492 {
2493         int ret;
2494         updateinfo_x *update_info;
2495         updateinfo_x *prev_update_info;
2496         pkgmgrinfo_updateinfo_h prev_update_handle;
2497         pkgmgrinfo_pkginfo_h pkginfo;
2498         pkgmgrinfo_version_compare_type compare_result;
2499         bool is_global_pkg;
2500         const char *update_type;
2501         const char *dbpath;
2502         sqlite3 *db;
2503
2504         if (handle == NULL) {
2505                 _LOGE("invalid parameter");
2506                 return PM_PARSER_R_EINVAL;
2507         }
2508
2509         update_info = (updateinfo_x *)handle;
2510         if (update_info->pkgid == NULL || update_info->version == NULL)
2511                 return PM_PARSER_R_EINVAL;
2512         if (__convert_update_type(update_info->type, &update_type) != 0)
2513                 return PM_PARSER_R_EINVAL;
2514
2515         ret = pkgmgrinfo_updateinfo_get_usr_updateinfo(update_info->pkgid,
2516                         &prev_update_handle, uid);
2517         if (ret != PMINFO_R_OK)
2518                 return PM_PARSER_R_ERROR;
2519
2520         prev_update_info = (updateinfo_x *)prev_update_handle;
2521         ret = pkgmgrinfo_compare_package_version(update_info->version,
2522                         prev_update_info->version, &compare_result);
2523         if (ret != PMINFO_R_OK) {
2524                 pkgmgrinfo_updateinfo_destroy(prev_update_handle);
2525                 return PM_PARSER_R_ERROR;
2526         }
2527
2528         if (compare_result == PMINFO_VERSION_SAME &&
2529                         prev_update_info->type == PMINFO_UPDATEINFO_NONE) {
2530                 _LOGI("Given update info version[%s] of pkgid[%s] "
2531                                 "will be ignored",
2532                                 update_info->version, update_info->pkgid);
2533                 pkgmgrinfo_updateinfo_destroy(prev_update_handle);
2534                 return PM_PARSER_R_OK;
2535         }
2536         pkgmgrinfo_updateinfo_destroy(prev_update_handle);
2537
2538         ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(update_info->pkgid, uid,
2539                         &pkginfo);
2540         if (ret != PMINFO_R_OK)
2541                 return PM_PARSER_R_ERROR;
2542
2543         ret = pkgmgrinfo_pkginfo_is_global(pkginfo, &is_global_pkg);
2544         if (ret != PMINFO_R_OK) {
2545                 pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo);
2546                 return PM_PARSER_R_ERROR;
2547         }
2548         pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo);
2549
2550         dbpath = __get_parser_db_path(is_global_pkg ? GLOBAL_USER : uid);
2551
2552         ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE);
2553         if (ret != SQLITE_OK) {
2554                 _LOGE("open db failed: %d", ret);
2555                 return PM_PARSER_R_ERROR;
2556         }
2557
2558         __BEGIN_TRANSACTION(db);
2559         __DO_TRANSACTION(db, __register_pkg_update_info(db, update_info,
2560                                 update_type));
2561         __END_TRANSACTION(db);
2562
2563         sqlite3_close_v2(db);
2564
2565         return PM_PARSER_R_OK;
2566 }
2567
2568 API int pkgmgr_parser_register_pkg_update_info_in_db(
2569                 pkgmgrinfo_updateinfo_h handle)
2570 {
2571         return pkgmgr_parser_register_pkg_update_info_in_usr_db(handle,
2572                         __getuid());
2573 }
2574
2575 static int __unregister_pkg_update_info(sqlite3 *db, const char *pkgid)
2576 {
2577         static const char query[] =
2578                 "UPDATE package_update_info SET update_type='none' "
2579                 "WHERE package=?";
2580         int ret;
2581         sqlite3_stmt *stmt;
2582         int idx = 1;
2583
2584         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
2585         if (ret != SQLITE_OK) {
2586                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
2587                 return -1;
2588         }
2589
2590         __BIND_TEXT(db, stmt, idx++, pkgid);
2591
2592         ret = sqlite3_step(stmt);
2593         if (ret != SQLITE_DONE) {
2594                 _LOGE("step failed: %s", sqlite3_errmsg(db));
2595                 sqlite3_finalize(stmt);
2596                 return -1;
2597         }
2598
2599         sqlite3_finalize(stmt);
2600
2601         return 0;
2602 }
2603
2604 API int pkgmgr_parser_unregister_pkg_update_info_in_usr_db(const char *pkgid,
2605                 uid_t uid)
2606 {
2607         int ret;
2608         const char *dbpath;
2609         sqlite3 *db;
2610         pkgmgrinfo_pkginfo_h pkginfo;
2611         bool is_global_pkg;
2612
2613         if (pkgid == NULL) {
2614                 _LOGE("invalid parameter");
2615                 return PM_PARSER_R_EINVAL;
2616         }
2617
2618         ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid, uid, &pkginfo);
2619         if (ret != PMINFO_R_OK)
2620                 return PM_PARSER_R_EINVAL;
2621
2622         ret = pkgmgrinfo_pkginfo_is_global(pkginfo, &is_global_pkg);
2623         pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo);
2624         if (ret != PMINFO_R_OK)
2625                 return PM_PARSER_R_ERROR;
2626
2627         dbpath = __get_parser_db_path(is_global_pkg ? GLOBAL_USER : uid);
2628
2629         ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE);
2630         if (ret != SQLITE_OK) {
2631                 _LOGE("open db failed: %d", ret);
2632                 return PM_PARSER_R_ERROR;
2633         }
2634
2635         __BEGIN_TRANSACTION(db);
2636         __DO_TRANSACTION(db, __unregister_pkg_update_info(db, pkgid));
2637         __END_TRANSACTION(db);
2638
2639         sqlite3_close_v2(db);
2640
2641         return PM_PARSER_R_OK;
2642 }
2643
2644 API int pkgmgr_parser_unregister_pkg_update_info_in_db(const char *pkgid)
2645 {
2646         return pkgmgr_parser_unregister_pkg_update_info_in_usr_db(pkgid,
2647                         __getuid());
2648 }
2649
2650 static int __unregister_all_pkg_update_info(sqlite3 *db)
2651 {
2652         static const char query[] =
2653                 "UPDATE package_update_info SET update_type='none'";
2654         int ret;
2655         sqlite3_stmt *stmt;
2656
2657         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
2658         if (ret != SQLITE_OK) {
2659                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
2660                 return -1;
2661         }
2662
2663         ret = sqlite3_step(stmt);
2664         if (ret != SQLITE_DONE) {
2665                 _LOGE("step failed: %s", sqlite3_errmsg(db));
2666                 sqlite3_finalize(stmt);
2667                 return -1;
2668         }
2669
2670         sqlite3_finalize(stmt);
2671
2672         return 0;
2673 }
2674
2675 API int pkgmgr_parser_unregister_all_pkg_update_info_in_usr_db(uid_t uid)
2676 {
2677         int ret;
2678         const char *dbpath;
2679         sqlite3 *db;
2680
2681         dbpath = __get_parser_db_path(uid);
2682
2683         ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE);
2684         if (ret != SQLITE_OK) {
2685                 _LOGE("open db failed: %d", ret);
2686                 return PM_PARSER_R_ERROR;
2687         }
2688
2689         __BEGIN_TRANSACTION(db);
2690         __DO_TRANSACTION(db, __unregister_all_pkg_update_info(db));
2691         __END_TRANSACTION(db);
2692
2693         sqlite3_close_v2(db);
2694
2695         return PM_PARSER_R_OK;
2696 }
2697
2698 API int pkgmgr_parser_unregister_all_pkg_update_info_in_db(void)
2699 {
2700         return pkgmgr_parser_unregister_all_pkg_update_info_in_usr_db(
2701                         __getuid());
2702 }