2 * Copyright 2013 Samsung Electronics Co., Ltd
5 * Contact: Minsu Seo <minsu15.seo@samsung.com>
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 #include <sys/types.h>
22 #include <sys/xattr.h>
28 #include <fontconfig/fontconfig.h>
29 #include <Elementary.h>
30 #include <pkgmgr-info.h>
31 #include <pkgmgr_installer_info.h>
32 #include "system_settings.h"
34 #define FONT_SERVICE_TAG "FONT_SERVICE"
35 #define DEBUG_LOG(frmt, args...) \
36 do { SLOG(LOG_DEBUG,FONT_SERVICE_TAG, "[font_service] %s: "frmt"\n",\
37 __func__, ##args);} while (0)
38 #define DEBUG_WARNING(frmt, args...) \
39 do { SLOG(LOG_WARN,FONT_SERVICE_TAG, "[font_service] %s: "frmt"\n",\
40 __func__, ##args);} while (0)
41 #define DEBUG_ERROR(frmt, args...) \
42 do { SLOG(LOG_ERROR,FONT_SERVICE_TAG, "[font_service] %s: "frmt"\n",\
43 __func__, ##args);} while (0)
45 static const char *PARENT_PATH = "/opt/share/fonts";
46 static const char *DOWNLOAD_PATH = "/opt/share/fonts/download";
47 static const char *PRELOADED_PATH = "/opt/share/fonts/preloaded";
49 #define MAX_FILE_LEN 4096
50 #define APP_OWNER_ID 5001
51 #define APP_GROUP_ID 100
53 static int make_dir(const char *path);
54 static int symbolic_link(const char *srcpath, const char *destpath);
55 static int do_install(const char *parent, const char *appid, const char *rootpath, pkgmgrinfo_pkginfo_h handle);
56 static int do_uninstall(const char *deletedir);
57 static int move_file(const char *srcpath, const char *destpath);
58 static const char* check_preloaded(const char *app_root_path);
59 #ifdef CHECK_FAMILY_NAME
60 static int check_family_name(const char *srcpath);
63 static const char* check_preloaded(const char *app_root_path)
65 char tpk_path[MAX_FILE_LEN];
66 char wgt_path[MAX_FILE_LEN];
68 sprintf(tpk_path, "%s/preloaded", app_root_path);
69 sprintf(wgt_path, "%s/res/wgt/preloaded", app_root_path);
71 if ((access(tpk_path, F_OK) == 0) || (access(wgt_path, F_OK) == 0))
73 return PRELOADED_PATH;
79 static int make_dir(const char *path)
83 if (access(path, F_OK) == 0)
89 ret = mkdir(path, 0755);
92 DEBUG_ERROR("make current directory %s is failed \n",path);
96 ret = chown(path, getuid(), APP_GROUP_ID);
99 DEBUG_ERROR("chown is failed %s\n",path);
103 ret = chmod(path, 0755);
106 DEBUG_ERROR("chmod is failed %s\n",path);
115 static int symbolic_link(const char *srcpath, const char *destpath)
121 d = opendir(srcpath);
122 while ((e = readdir (d)))
124 if (e->d_name[0] != '.' && strlen((char *)e->d_name) < MAX_FILE_LEN)
126 char srcdir[MAX_FILE_LEN];
127 char destdir[MAX_FILE_LEN];
130 if (strlen (srcpath) + strlen ((char *)e->d_name) + 2 >= MAX_FILE_LEN )
132 DEBUG_ERROR("srcdir length is not available \n");
136 if (strlen (destpath) + strlen ((char *)e->d_name) + 2 >= MAX_FILE_LEN )
138 DEBUG_ERROR("destdir length is not available \n");
142 sprintf(srcdir,"%s/%s",srcpath,(char *) e->d_name);
143 sprintf(destdir,"%s/%s",destpath,(char *) e->d_name);
144 if (stat (srcdir, &statb) == -1)
146 DEBUG_ERROR("stat %s is failed \n",srcdir);
150 if (S_ISDIR (statb.st_mode))
152 if (make_dir(destdir)<0)
154 DEBUG_ERROR("make current directory %s is failed \n",destdir);
157 ret = symbolic_link(srcdir,destdir);
160 DEBUG_ERROR("symlink is failed \n");
165 DEBUG_LOG("srcdir =%s\n",(char *) srcdir);
166 DEBUG_LOG("destdir =%s\n",(char *) destdir);
167 DEBUG_LOG("file name =%s\n",(char *) e->d_name);
169 ret = symlink((const char *)srcdir,(const char *)destdir);
172 DEBUG_ERROR("symlink is failed \n");
186 static int move_file(const char *srcpath, const char *destpath)
192 d = opendir (srcpath);
193 while ((e = readdir (d)))
195 if (e->d_name[0] != '.' && strlen((char *)e->d_name) < MAX_FILE_LEN)
197 char srcdir[MAX_FILE_LEN];
198 char destdir[MAX_FILE_LEN];
201 if (strlen (srcpath) + strlen ((char *)e->d_name) + 2 >= MAX_FILE_LEN )
203 DEBUG_ERROR("srcdir length is not available \n");
207 if (strlen (destpath) + strlen ((char *)e->d_name) + 2 >= MAX_FILE_LEN )
209 DEBUG_ERROR("destdir length is not available \n");
213 sprintf(srcdir,"%s/%s",srcpath,(char *) e->d_name);
214 sprintf(destdir,"%s/%s",destpath,(char *) e->d_name);
215 if (stat (srcdir, &statb) == -1)
217 DEBUG_ERROR("stat %s is failed \n",srcdir);
221 if (S_ISDIR (statb.st_mode))
223 if (make_dir(destdir)<0)
225 DEBUG_ERROR("make current directory %s is failed \n",destdir);
230 DEBUG_LOG("srcdir =%s\n",(char *) srcdir);
231 DEBUG_LOG("destdir =%s\n",(char *) destdir);
232 DEBUG_LOG("file name =%s\n",(char *) e->d_name);
234 ret = rename((const char *)srcdir,(const char *)destdir);
237 DEBUG_ERROR("symlink is failed \n");
250 #ifdef CHECK_FAMILY_NAME
251 static int check_family_name(const char *srcpath)
253 FcObjectSet *os = NULL;
254 FcFontSet* fs = NULL;
255 FcPattern* pat = NULL;
256 FcConfig* font_config = NULL;
257 bool is_only_one_family = false;
259 font_config = FcInitLoadConfigAndFonts();
261 if(font_config == NULL) {
262 DEBUG_ERROR("Failed: FcInitLoadConfigAndFonts");
266 pat = FcPatternCreate();
268 os = FcObjectSetBuild(FC_FAMILY, FC_FILE, (char *) 0);
271 fs = FcFontList(font_config, pat, os);
272 FcObjectSetDestroy(os);
277 FcPatternDestroy(pat);
284 char* only_one_family = NULL;
286 for (j = 0; j < fs->nfont; j++)
288 FcChar8 *family = NULL;
289 FcChar8 *file = NULL;
291 if (FcPatternGetString(fs->fonts[j], FC_FILE, 0, &file) == FcResultMatch)
293 int font_path_len = strlen(srcpath);
295 /* always shown for src path */
296 if (strncmp((const char*)file, srcpath, font_path_len) == 0)
298 if (FcPatternGetString(fs->fonts[j], FC_FAMILY, 0, &family) != FcResultMatch)
300 DEBUG_ERROR("Family name is invalid");
303 //DEBUG_ERROR("-------- FOUND FONT - family = %s", (char *)family);
305 // first found family name
306 if (only_one_family == NULL && strlen((char *)family) > 0)
308 is_only_one_family = true;
309 only_one_family = (char *)family;
310 //DEBUG_ERROR("--------First FOUND FONT - family = %s", only_one_family);
312 else if (only_one_family != NULL && strlen((char *)family) > 0)// after first
314 if (strcmp(only_one_family, (char *)family) != 0)
316 DEBUG_ERROR("Not supported various font famliy. only one font famliy. %s %s", only_one_family, (char *)family);
317 is_only_one_family = false;
323 DEBUG_ERROR("invalid fonts");
330 FcFontSetDestroy(fs);
334 FcConfigDestroy(font_config);
337 if (is_only_one_family == false)
339 //DEBUG_ERROR("Not supported various font famliy. only one font famliy");
347 static int do_install(const char *parent, const char *appid, const char *rootpath, pkgmgrinfo_pkginfo_h handle)
349 char destdir[MAX_FILE_LEN];
350 char path[MAX_FILE_LEN];
354 if (strlen (parent) + strlen (appid) + 2 >= MAX_FILE_LEN )
356 DEBUG_ERROR("appid length is not available \n");
360 sprintf(destdir,"%s/%s",parent,appid);
361 ret = make_dir(destdir);
364 DEBUG_ERROR("make current directory %s is failed \n", destdir);
368 if (strlen (rootpath) + 2 >= MAX_FILE_LEN )
370 DEBUG_ERROR("rootpath length is not available \n");
374 ret = pkgmgrinfo_pkginfo_get_type(handle, &type);
377 DEBUG_ERROR("pkgmgrinfo_pkginfo_get_type is failed\n");
381 sprintf(path,"%s/shared/res", rootpath);
383 if (!strcmp(type,"wgt"))
385 char srcpath[MAX_FILE_LEN];
386 sprintf(srcpath,"%s/res/wgt/shared/res", rootpath);
387 ret = move_file(srcpath, path);
392 DEBUG_ERROR("move_file is failed\n");
396 ret = symbolic_link(path, destdir);
400 DEBUG_ERROR("install is failed \n", destdir);
404 #ifdef CHECK_FAMILY_NAME
405 ret = check_family_name(destdir);
409 DEBUG_ERROR("Invaid font files\n");
417 do_uninstall(destdir);
422 static int do_uninstall(const char *deletedir)
427 d = opendir(deletedir);
428 while((e = readdir (d)))
430 if (e->d_name[0] != '.')
432 char destfile[MAX_FILE_LEN];
435 if (strlen (deletedir) + strlen ((char *) e->d_name) + 2 >= MAX_FILE_LEN )
437 DEBUG_ERROR("destfile length is not available \n");
441 sprintf(destfile,"%s/%s",deletedir,(char *) e->d_name);
442 if (lstat (destfile, &statb) == -1)
444 DEBUG_ERROR("lstat %s is failed \n",destfile);
448 if (S_ISDIR (statb.st_mode))
450 if (do_uninstall(destfile)<0)
453 else if (unlink(destfile) < 0)
455 DEBUG_ERROR("unlink is failed %s\n",destfile);
458 DEBUG_LOG("destfile =%s\n",destfile);
464 if (rmdir(deletedir) < 0)
466 DEBUG_ERROR("rmdir is failed \n");
470 DEBUG_LOG("rmdir =%s\n",deletedir);
479 int COMMON_PKGMGR_PLUGIN_INSTALL(const char *pkgid, const char *appid, GList *list)
482 pkgmgrinfo_pkginfo_h handle = NULL;
483 const char *app_root_path = NULL;
484 const char *dest_path = NULL;
487 pkgmgr_installer_info_get_target_uid(&uid);
488 ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid, uid, &handle);
492 DEBUG_ERROR("pkgid[%s] handle get fail", pkgid);
496 ret = pkgmgrinfo_pkginfo_get_root_path(handle, (char **)&app_root_path);
499 DEBUG_ERROR("pkgid[%s] path get fail", pkgid);
505 DEBUG_ERROR("appid is NULL \n");
509 ret = make_dir(PARENT_PATH);
513 DEBUG_ERROR("make current directory is failed \n");
517 dest_path = check_preloaded(app_root_path);
519 if (make_dir(dest_path)<0)
521 DEBUG_ERROR("make current directory is failed \n");
525 ret = do_install(dest_path,appid, app_root_path, handle);
528 DEBUG_ERROR("font install is failed \n");
532 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
537 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
542 int COMMON_PKGMGR_PLUGIN_UPGRADE(const char *pkgid, const char *appid, GList *list)
544 char deletedir[MAX_FILE_LEN];
546 pkgmgrinfo_pkginfo_h handle = NULL;
547 const char* app_root_path = NULL;
548 const char* dest_path = NULL;
551 pkgmgr_installer_info_get_target_uid(&uid);
552 ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid, uid, &handle);
556 DEBUG_ERROR("pkgid[%s] handle get fail", pkgid);
560 ret = pkgmgrinfo_pkginfo_get_root_path(handle, (char **)&app_root_path);
563 DEBUG_ERROR("pkgid[%s] path get fail", pkgid);
569 DEBUG_ERROR("appid is NULL \n");
573 dest_path = check_preloaded(app_root_path);
575 if (strlen (dest_path) + strlen (appid) + 2 >= MAX_FILE_LEN )
577 DEBUG_ERROR("appid length is not available \n");
581 sprintf(deletedir,"%s/%s", dest_path, appid);
583 if (access(deletedir, F_OK) == -1)
585 DEBUG_ERROR("dest directory(%s) is not exist: %s\n", deletedir, strerror(errno));
589 ret = do_uninstall(deletedir);
592 DEBUG_ERROR("do_uninstall is failed \n");
596 ret = make_dir(PARENT_PATH);
599 DEBUG_ERROR("make current directory(%s) is failed: %s\n", PARENT_PATH, strerror(errno));
603 ret = make_dir(dest_path);
606 DEBUG_ERROR("make current directory(%s) is failed: %s\n", dest_path, strerror(errno));
610 ret = do_install(dest_path, appid, app_root_path, handle);
613 DEBUG_ERROR("do_install is failed \n");
617 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
621 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
627 int COMMON_PKGMGR_PLUGIN_UNINSTALL(const char *pkgid, const char *appid, GList *list)
629 char deletedir[MAX_FILE_LEN];
630 FcObjectSet *os = NULL;
631 FcPattern *pat = NULL;
632 FcFontSet *fs = NULL;
633 pkgmgrinfo_pkginfo_h handle = NULL;
634 const char* app_root_path = NULL;
635 const char *dest_path = NULL;
636 int ret, deletedir_len;
643 DEBUG_ERROR("appid is NULL \n");
647 pkgmgr_installer_info_get_target_uid(&uid);
648 ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid, uid, &handle);
652 DEBUG_ERROR("pkgid[%s] handle get fail", pkgid);
656 ret = pkgmgrinfo_pkginfo_get_root_path(handle, (char **)&app_root_path);
659 DEBUG_ERROR("pkgid[%s] path get fail", pkgid);
663 dest_path = check_preloaded(app_root_path);
665 if (strlen (dest_path) + strlen (appid) + 2 >= MAX_FILE_LEN )
667 DEBUG_ERROR("appid length is not available \n");
671 /* It must contain "/" character at end of the delete dir path.
672 * It prevents file path comparing issues when there are many similar path. */
673 sprintf(deletedir,"%s/%s/", dest_path, appid);
675 //check if current using font is same with uninstall font
676 deletedir_len = strlen(deletedir);
678 pat = FcPatternCreate();
681 DEBUG_ERROR("FcPatternCreate is NULL \n");
685 char *current_font_name = NULL;
686 ret = system_settings_get_value_string(SYSTEM_SETTINGS_KEY_FONT_TYPE, ¤t_font_name);
689 DEBUG_ERROR("Get current font is failed \n");
693 DEBUG_LOG("current_font_name =%s\n",current_font_name);
695 FcInitLoadConfigAndFonts();
696 FcPatternAddString (pat, FC_FAMILY,(FcChar8*)current_font_name);
697 os = FcObjectSetBuild(FC_FAMILY, FC_FILE, (char *) 0);
698 fs = FcFontList(NULL, pat, os);
700 FcPatternDestroy(pat);
701 free(current_font_name);
707 for (j = 0; j < fs->nfont; j++)
709 FcChar8 *file = NULL;
710 if (FcPatternGetString(fs->fonts[j], FC_FILE, 0, &file) == FcResultMatch)
712 DEBUG_LOG("file =%s, deletedir =%s\n", file, deletedir);
713 if (strncmp((const char*)file, deletedir, deletedir_len) == 0)
715 DEBUG_LOG("change to default font\n");
716 char *default_font_name = NULL;
718 ret = system_settings_get_value_string(SYSTEM_SETTINGS_KEY_DEFAULT_FONT_TYPE, &default_font_name);
721 DEBUG_ERROR("Get default font is failed \n");
725 DEBUG_LOG("default_font_name = %s \n",default_font_name);
726 ret = system_settings_set_value_string(SYSTEM_SETTINGS_KEY_FONT_TYPE, default_font_name);
729 DEBUG_ERROR("Set default font is failed ret=%d \n",ret);
730 free(default_font_name);
734 free(default_font_name);
735 DEBUG_LOG("success change to default font\n");
742 if (access(deletedir, F_OK) == -1)
744 DEBUG_ERROR("dest directory(%s) is not exist: %s\n", deletedir, strerror(errno));
749 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
751 return do_uninstall(deletedir);
755 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
761 int PKGMGR_MDPARSER_PLUGIN_INSTALL(const char *pkgid, const char *appid, GList *list)
763 return COMMON_PKGMGR_PLUGIN_INSTALL(pkgid, appid, list);
766 int PKGMGR_MDPARSER_PLUGIN_UPGRADE(const char *pkgid, const char *appid, GList *list)
768 return COMMON_PKGMGR_PLUGIN_UPGRADE(pkgid, appid, list);
771 int PKGMGR_MDPARSER_PLUGIN_UNINSTALL(const char *pkgid, const char *appid, GList *list)
773 return COMMON_PKGMGR_PLUGIN_UNINSTALL(pkgid, appid, list);
777 int PKGMGR_CATEGORY_PARSER_PLUGIN_INSTALL(const char *pkgid, const char *appid, GList *list)
779 return COMMON_PKGMGR_PLUGIN_INSTALL(pkgid, appid, list);
782 int PKGMGR_CATEGORY_PARSER_PLUGIN_UPGRADE(const char *pkgid, const char *appid, GList *list)
784 return COMMON_PKGMGR_PLUGIN_UPGRADE(pkgid, appid, list);
787 int PKGMGR_CATEGORY_PARSER_PLUGIN_UNINSTALL(const char *pkgid, const char *appid, GList *list)
789 return COMMON_PKGMGR_PLUGIN_UNINSTALL(pkgid, appid, list);