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