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