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 "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 static const char *ELM_PROFILE_CFG = "/opt/home/app/.elementary/config/profile.cfg";
50 #define MAX_FILE_LEN 4096
51 #define APP_OWNER_ID 5000
52 #define APP_GROUP_ID 5000
54 static int make_dir(const char *path);
55 static int symbolic_link(const char *srcpath, const char *destpath);
56 static int do_install(const char *parent, const char *appid, const char *rootpath, pkgmgrinfo_pkginfo_h handle);
57 static int do_uninstall(const char *deletedir);
58 static int move_file(const char *srcpath, const char *destpath);
59 static const char* check_preloaded(const char *app_root_path);
60 #ifdef CHECK_FAMILY_NAME
61 static int check_family_name(const char *srcpath);
64 static const char* check_preloaded(const char *app_root_path)
66 char tpk_path[MAX_FILE_LEN];
67 char wgt_path[MAX_FILE_LEN];
69 sprintf(tpk_path, "%s/preloaded", app_root_path);
70 sprintf(wgt_path, "%s/res/wgt/preloaded", app_root_path);
72 if ((access(tpk_path, F_OK) == 0) || (access(wgt_path, F_OK) == 0))
74 return PRELOADED_PATH;
80 static int make_dir(const char *path)
84 if (access(path, F_OK) == 0)
90 ret = mkdir(path, 0755);
93 DEBUG_ERROR("make current directory %s is failed \n",path);
97 ret = chmod (path, 0755);
100 DEBUG_ERROR("chmod is failed %s\n",path);
105 ret = lsetxattr(path, "security.SMACK64", "_", 1, 0);
109 DEBUG_ERROR("lsetxattr is failed %s\n",path);
116 static int symbolic_link(const char *srcpath, const char *destpath)
122 d = opendir(srcpath);
123 while ((e = readdir (d)))
125 if (e->d_name[0] != '.' && strlen((char *)e->d_name) < MAX_FILE_LEN)
127 char srcdir[MAX_FILE_LEN];
128 char destdir[MAX_FILE_LEN];
131 if (strlen (srcpath) + strlen ((char *)e->d_name) + 2 >= MAX_FILE_LEN )
133 DEBUG_ERROR("srcdir length is not available \n");
137 if (strlen (destpath) + strlen ((char *)e->d_name) + 2 >= MAX_FILE_LEN )
139 DEBUG_ERROR("destdir length is not available \n");
143 sprintf(srcdir,"%s/%s",srcpath,(char *) e->d_name);
144 sprintf(destdir,"%s/%s",destpath,(char *) e->d_name);
145 if (stat (srcdir, &statb) == -1)
147 DEBUG_ERROR("stat %s is failed \n",srcdir);
151 if (S_ISDIR (statb.st_mode))
153 if (make_dir(destdir)<0)
155 DEBUG_ERROR("make current directory %s is failed \n",destdir);
158 ret = symbolic_link(srcdir,destdir);
161 DEBUG_ERROR("symlink is failed \n");
166 DEBUG_LOG("srcdir =%s\n",(char *) srcdir);
167 DEBUG_LOG("destdir =%s\n",(char *) destdir);
168 DEBUG_LOG("file name =%s\n",(char *) e->d_name);
170 ret = symlink((const char *)srcdir,(const char *)destdir);
173 DEBUG_ERROR("symlink is failed \n");
177 ret = lsetxattr(destdir, "security.SMACK64", "_", 1, 0);
181 DEBUG_ERROR("lsetxattr is failed %s\n",destdir);
195 static int move_file(const char *srcpath, const char *destpath)
201 d = opendir (srcpath);
202 while ((e = readdir (d)))
204 if (e->d_name[0] != '.' && strlen((char *)e->d_name) < MAX_FILE_LEN)
206 char srcdir[MAX_FILE_LEN];
207 char destdir[MAX_FILE_LEN];
210 if (strlen (srcpath) + strlen ((char *)e->d_name) + 2 >= MAX_FILE_LEN )
212 DEBUG_ERROR("srcdir length is not available \n");
216 if (strlen (destpath) + strlen ((char *)e->d_name) + 2 >= MAX_FILE_LEN )
218 DEBUG_ERROR("destdir length is not available \n");
222 sprintf(srcdir,"%s/%s",srcpath,(char *) e->d_name);
223 sprintf(destdir,"%s/%s",destpath,(char *) e->d_name);
224 if (stat (srcdir, &statb) == -1)
226 DEBUG_ERROR("stat %s is failed \n",srcdir);
230 if (S_ISDIR (statb.st_mode))
232 if (make_dir(destdir)<0)
234 DEBUG_ERROR("make current directory %s is failed \n",destdir);
239 DEBUG_LOG("srcdir =%s\n",(char *) srcdir);
240 DEBUG_LOG("destdir =%s\n",(char *) destdir);
241 DEBUG_LOG("file name =%s\n",(char *) e->d_name);
243 ret = rename((const char *)srcdir,(const char *)destdir);
246 DEBUG_ERROR("symlink is failed \n");
250 ret = lsetxattr(destdir, "security.SMACK64", "_", 1, 0);
254 DEBUG_ERROR("lsetxattr is failed %s\n",destdir);
267 #ifdef CHECK_FAMILY_NAME
268 static int check_family_name(const char *srcpath)
270 FcObjectSet *os = NULL;
271 FcFontSet* fs = NULL;
272 FcPattern* pat = NULL;
273 FcConfig* font_config = NULL;
274 bool is_only_one_family = false;
276 font_config = FcInitLoadConfigAndFonts();
278 if(font_config == NULL) {
279 DEBUG_ERROR("Failed: FcInitLoadConfigAndFonts");
283 pat = FcPatternCreate();
285 os = FcObjectSetBuild(FC_FAMILY, FC_FILE, (char *) 0);
288 fs = FcFontList(font_config, pat, os);
289 FcObjectSetDestroy(os);
294 FcPatternDestroy(pat);
301 char* only_one_family = NULL;
303 for (j = 0; j < fs->nfont; j++)
305 FcChar8 *family = NULL;
306 FcChar8 *file = NULL;
308 if (FcPatternGetString(fs->fonts[j], FC_FILE, 0, &file) == FcResultMatch)
310 int font_path_len = strlen(srcpath);
312 /* always shown for src path */
313 if (strncmp((const char*)file, srcpath, font_path_len) == 0)
315 if (FcPatternGetString(fs->fonts[j], FC_FAMILY, 0, &family) != FcResultMatch)
317 DEBUG_ERROR("Family name is invalid");
320 //DEBUG_ERROR("-------- FOUND FONT - family = %s", (char *)family);
322 // first found family name
323 if (only_one_family == NULL && strlen((char *)family) > 0)
325 is_only_one_family = true;
326 only_one_family = (char *)family;
327 //DEBUG_ERROR("--------First FOUND FONT - family = %s", only_one_family);
329 else if (only_one_family != NULL && strlen((char *)family) > 0)// after first
331 if (strcmp(only_one_family, (char *)family) != 0)
333 DEBUG_ERROR("Not supported various font famliy. only one font famliy. %s %s", only_one_family, (char *)family);
334 is_only_one_family = false;
340 DEBUG_ERROR("invalid fonts");
347 FcFontSetDestroy(fs);
351 FcConfigDestroy(font_config);
354 if (is_only_one_family == false)
356 //DEBUG_ERROR("Not supported various font famliy. only one font famliy");
364 static int do_install(const char *parent, const char *appid, const char *rootpath, pkgmgrinfo_pkginfo_h handle)
366 char destdir[MAX_FILE_LEN];
367 char path[MAX_FILE_LEN];
371 if (strlen (parent) + strlen (appid) + 2 >= MAX_FILE_LEN )
373 DEBUG_ERROR("appid length is not available \n");
377 sprintf(destdir,"%s/%s",parent,appid);
378 ret = make_dir(destdir);
381 DEBUG_ERROR("make current directory %s is failed \n", destdir);
385 if (strlen (rootpath) + 2 >= MAX_FILE_LEN )
387 DEBUG_ERROR("rootpath length is not available \n");
391 ret = pkgmgrinfo_pkginfo_get_type(handle, &type);
394 DEBUG_ERROR("pkgmgrinfo_pkginfo_get_type is failed\n");
398 sprintf(path,"%s/shared/res", rootpath);
400 if (!strcmp(type,"wgt"))
402 char srcpath[MAX_FILE_LEN];
403 sprintf(srcpath,"%s/res/wgt/shared/res", rootpath);
404 ret = move_file(srcpath, path);
409 DEBUG_ERROR("move_file is failed\n");
413 ret = symbolic_link(path, destdir);
417 DEBUG_ERROR("install is failed \n", destdir);
421 #ifdef CHECK_FAMILY_NAME
422 ret = check_family_name(destdir);
426 DEBUG_ERROR("Invaid font files\n");
434 do_uninstall(destdir);
439 static int do_uninstall(const char *deletedir)
444 d = opendir(deletedir);
445 while((e = readdir (d)))
447 if (e->d_name[0] != '.')
449 char destfile[MAX_FILE_LEN];
452 if (strlen (deletedir) + strlen ((char *) e->d_name) + 2 >= MAX_FILE_LEN )
454 DEBUG_ERROR("destfile length is not available \n");
458 sprintf(destfile,"%s/%s",deletedir,(char *) e->d_name);
459 if (lstat (destfile, &statb) == -1)
461 DEBUG_ERROR("lstat %s is failed \n",destfile);
465 if (S_ISDIR (statb.st_mode))
467 if (do_uninstall(destfile)<0)
470 else if (unlink(destfile) < 0)
472 DEBUG_ERROR("unlink is failed %s\n",destfile);
475 DEBUG_LOG("destfile =%s\n",destfile);
481 if (rmdir(deletedir) < 0)
483 DEBUG_ERROR("rmdir is failed \n");
487 DEBUG_LOG("rmdir =%s\n",deletedir);
496 int COMMON_PKGMGR_PLUGIN_INSTALL(const char *pkgid, const char *appid, GList *list)
499 pkgmgrinfo_pkginfo_h handle = NULL;
500 const char *app_root_path = NULL;
501 const char *dest_path = NULL;
503 ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &handle);
506 DEBUG_ERROR("pkgid[%s] handle get fail", pkgid);
510 ret = pkgmgrinfo_pkginfo_get_root_path(handle, (char **)&app_root_path);
513 DEBUG_ERROR("pkgid[%s] path get fail", pkgid);
519 DEBUG_ERROR("appid is NULL \n");
523 ret = make_dir(PARENT_PATH);
527 DEBUG_ERROR("make current directory is failed \n");
531 dest_path = check_preloaded(app_root_path);
533 if (make_dir(dest_path)<0)
535 DEBUG_ERROR("make current directory is failed \n");
539 ret = do_install(dest_path,appid, app_root_path, handle);
542 DEBUG_ERROR("font install is failed \n");
546 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
551 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
556 int COMMON_PKGMGR_PLUGIN_UPGRADE(const char *pkgid, const char *appid, GList *list)
558 char deletedir[MAX_FILE_LEN];
560 pkgmgrinfo_pkginfo_h handle = NULL;
561 const char* app_root_path = NULL;
562 const char* dest_path = NULL;
564 ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &handle);
567 DEBUG_ERROR("pkgid[%s] handle get fail", pkgid);
571 ret = pkgmgrinfo_pkginfo_get_root_path(handle, (char **)&app_root_path);
574 DEBUG_ERROR("pkgid[%s] path get fail", pkgid);
580 DEBUG_ERROR("appid is NULL \n");
584 dest_path = check_preloaded(app_root_path);
586 if (strlen (dest_path) + strlen (appid) + 2 >= MAX_FILE_LEN )
588 DEBUG_ERROR("appid length is not available \n");
592 sprintf(deletedir,"%s/%s", dest_path, appid);
594 if (access(deletedir, F_OK) == -1)
596 DEBUG_ERROR("dest directory is not exist \n");
600 ret = do_uninstall(deletedir);
603 DEBUG_ERROR("do_uninstall is failed \n");
607 ret = make_dir(PARENT_PATH);
611 DEBUG_ERROR("make current directory is failed \n");
615 ret = make_dir(dest_path);
619 DEBUG_ERROR("make current directory is failed \n");
623 ret = do_install(dest_path, appid, app_root_path, handle);
626 DEBUG_ERROR("do_install is failed \n");
630 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
634 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
640 int COMMON_PKGMGR_PLUGIN_UNINSTALL(const char *pkgid, const char *appid, GList *list)
642 char deletedir[MAX_FILE_LEN];
643 FcObjectSet *os = NULL;
644 FcPattern *pat = NULL;
645 FcFontSet *fs = NULL;
646 const char* app_root_path = NULL;
647 const char *dest_path = NULL;
654 DEBUG_ERROR("appid is NULL \n");
658 pkgmgrinfo_pkginfo_h handle = NULL;
661 ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &handle);
664 DEBUG_ERROR("pkgid[%s] handle get fail", pkgid);
668 ret = pkgmgrinfo_pkginfo_get_root_path(handle, (char **)&app_root_path);
671 DEBUG_ERROR("pkgid[%s] path get fail", pkgid);
675 dest_path = check_preloaded(app_root_path);
677 if (strlen (dest_path) + strlen (appid) + 2 >= MAX_FILE_LEN )
679 DEBUG_ERROR("appid length is not available \n");
683 sprintf(deletedir,"%s/%s", dest_path, appid);
685 //check if current using font is same with uninstall font
686 int deletedir_len = strlen(deletedir);
688 pat = FcPatternCreate();
691 DEBUG_ERROR("FcPatternCreate is NULL \n");
695 char *current_font_name = NULL;
696 ret = system_settings_get_value_string(SYSTEM_SETTINGS_KEY_FONT_TYPE, ¤t_font_name);
699 DEBUG_ERROR("Get current font is failed \n");
703 DEBUG_LOG("current_font_name =%s\n",current_font_name);
705 FcInitLoadConfigAndFonts();
706 FcPatternAddString (pat, FC_FAMILY,(FcChar8*)current_font_name);
707 os = FcObjectSetBuild(FC_FAMILY, FC_FILE, (char *) 0);
708 fs = FcFontList(NULL, pat, os);
710 FcPatternDestroy(pat);
711 free(current_font_name);
716 for (j = 0; j < fs->nfont; j++)
718 FcChar8 *file = NULL;
719 if (FcPatternGetString(fs->fonts[j], FC_FILE, 0, &file) == FcResultMatch)
721 DEBUG_LOG("file =%s\n",file);
722 if (strncmp((const char*)file , deletedir , deletedir_len) == 0 )
724 DEBUG_LOG("change to default font\n");
725 char *default_font_name = NULL;
727 ret = system_settings_get_value_string(SYSTEM_SETTINGS_KEY_DEFAULT_FONT_TYPE, &default_font_name);
730 DEBUG_ERROR("Get default font is failed \n");
734 DEBUG_LOG("default_font_name = %s \n",default_font_name);
735 setenv("HOME", "/opt/home/app", 1);
737 ret = system_settings_set_value_string(SYSTEM_SETTINGS_KEY_FONT_TYPE, default_font_name);
740 DEBUG_ERROR("Set default font is failed ret=%d \n",ret);
741 free(default_font_name);
745 char *elm_profile_path = (char*)elm_config_profile_dir_get(elm_config_profile_get(), EINA_TRUE);
747 setenv("HOME", "/root", 1);
749 ret = chown(ELM_PROFILE_CFG, APP_OWNER_ID, APP_GROUP_ID);
752 chmod (ELM_PROFILE_CFG, 0777);
755 ret = lsetxattr(ELM_PROFILE_CFG, "security.SMACK64", "system::homedir", 15, 0);
758 chmod (ELM_PROFILE_CFG, 0777);
764 d = opendir (elm_profile_path);
766 while ((e = readdir (d)))
768 if (e->d_name[0] != '.' && (strstr(e->d_name, ".cfg") != 0 || strstr(e->d_name, ".CFG") != 0))
770 char file_full_path[100];
772 sprintf(file_full_path, "%s/%s", elm_profile_path, e->d_name);
774 ret = chown(file_full_path, APP_OWNER_ID, APP_GROUP_ID);
777 DEBUG_LOG("chown is failed %s", file_full_path);
778 chmod (file_full_path, 0777);
781 ret = lsetxattr(file_full_path, "security.SMACK64", "system::homedir", 15, 0);
784 DEBUG_LOG("chsmack is failed %s", file_full_path);
785 chmod (file_full_path, 0777);
792 free(default_font_name);
793 free(elm_profile_path);
794 DEBUG_LOG("success change to default font\n");
801 if (access(deletedir, F_OK) == -1)
803 DEBUG_ERROR("dest directory(%s) is not exist \n", deletedir);
808 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
810 return do_uninstall(deletedir);
814 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
820 int PKGMGR_MDPARSER_PLUGIN_INSTALL(const char *pkgid, const char *appid, GList *list)
822 return COMMON_PKGMGR_PLUGIN_INSTALL(pkgid, appid, list);
825 int PKGMGR_MDPARSER_PLUGIN_UPGRADE(const char *pkgid, const char *appid, GList *list)
827 return COMMON_PKGMGR_PLUGIN_UPGRADE(pkgid, appid, list);
830 int PKGMGR_MDPARSER_PLUGIN_UNINSTALL(const char *pkgid, const char *appid, GList *list)
832 return COMMON_PKGMGR_PLUGIN_UNINSTALL(pkgid, appid, list);
836 int PKGMGR_CATEGORY_PARSER_PLUGIN_INSTALL(const char *pkgid, const char *appid, GList *list)
838 return COMMON_PKGMGR_PLUGIN_INSTALL(pkgid, appid, list);
841 int PKGMGR_CATEGORY_PARSER_PLUGIN_UPGRADE(const char *pkgid, const char *appid, GList *list)
843 return COMMON_PKGMGR_PLUGIN_UPGRADE(pkgid, appid, list);
846 int PKGMGR_CATEGORY_PARSER_PLUGIN_UNINSTALL(const char *pkgid, const char *appid, GList *list)
848 return COMMON_PKGMGR_PLUGIN_UNINSTALL(pkgid, appid, list);