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"
35 #define FONT_SERVICE_TAG "FONT_SERVICE"
36 #define DEBUG_LOG(frmt, args...) \
37 do { SLOG(LOG_DEBUG,FONT_SERVICE_TAG, "[font_service] %s: "frmt"\n",\
38 __func__, ##args);} while (0)
39 #define DEBUG_WARNING(frmt, args...) \
40 do { SLOG(LOG_WARN,FONT_SERVICE_TAG, "[font_service] %s: "frmt"\n",\
41 __func__, ##args);} while (0)
42 #define DEBUG_ERROR(frmt, args...) \
43 do { SLOG(LOG_ERROR,FONT_SERVICE_TAG, "[font_service] %s: "frmt"\n",\
44 __func__, ##args);} while (0)
46 static const char *PARENT_PATH = "/opt/share/fonts";
47 static const char *DOWNLOAD_PATH = "/opt/share/fonts/download";
48 static const char *PRELOADED_PATH = "/opt/share/fonts/preloaded";
50 static const char *ELM_PROFILE_CFG = "/opt/home/app/.elementary/config/profile.cfg";
51 #define MAX_FILE_LEN 4096
52 #define APP_OWNER_ID 5001
53 #define APP_GROUP_ID 100
55 static int make_dir(const char *path);
56 static int symbolic_link(const char *srcpath, const char *destpath);
57 static int do_install(const char *parent, const char *appid, const char *rootpath, pkgmgrinfo_pkginfo_h handle);
58 static int do_uninstall(const char *deletedir);
59 static int move_file(const char *srcpath, const char *destpath);
60 static const char* check_preloaded(const char *app_root_path);
61 #ifdef CHECK_FAMILY_NAME
62 static int check_family_name(const char *srcpath);
65 static const char* check_preloaded(const char *app_root_path)
67 char tpk_path[MAX_FILE_LEN];
68 char wgt_path[MAX_FILE_LEN];
70 sprintf(tpk_path, "%s/preloaded", app_root_path);
71 sprintf(wgt_path, "%s/res/wgt/preloaded", app_root_path);
73 if ((access(tpk_path, F_OK) == 0) || (access(wgt_path, F_OK) == 0))
75 return PRELOADED_PATH;
81 static int make_dir(const char *path)
85 if (access(path, F_OK) == 0)
91 ret = mkdir(path, 0755);
94 DEBUG_ERROR("make current directory %s is failed \n",path);
98 ret = chown(path, getuid(), APP_GROUP_ID);
101 DEBUG_ERROR("chown is failed %s\n",path);
105 ret = chmod(path, 0755);
108 DEBUG_ERROR("chmod is failed %s\n",path);
117 static int symbolic_link(const char *srcpath, const char *destpath)
123 d = opendir(srcpath);
124 while ((e = readdir (d)))
126 if (e->d_name[0] != '.' && strlen((char *)e->d_name) < MAX_FILE_LEN)
128 char srcdir[MAX_FILE_LEN];
129 char destdir[MAX_FILE_LEN];
132 if (strlen (srcpath) + strlen ((char *)e->d_name) + 2 >= MAX_FILE_LEN )
134 DEBUG_ERROR("srcdir length is not available \n");
138 if (strlen (destpath) + strlen ((char *)e->d_name) + 2 >= MAX_FILE_LEN )
140 DEBUG_ERROR("destdir length is not available \n");
144 sprintf(srcdir,"%s/%s",srcpath,(char *) e->d_name);
145 sprintf(destdir,"%s/%s",destpath,(char *) e->d_name);
146 if (stat (srcdir, &statb) == -1)
148 DEBUG_ERROR("stat %s is failed \n",srcdir);
152 if (S_ISDIR (statb.st_mode))
154 if (make_dir(destdir)<0)
156 DEBUG_ERROR("make current directory %s is failed \n",destdir);
159 ret = symbolic_link(srcdir,destdir);
162 DEBUG_ERROR("symlink is failed \n");
167 DEBUG_LOG("srcdir =%s\n",(char *) srcdir);
168 DEBUG_LOG("destdir =%s\n",(char *) destdir);
169 DEBUG_LOG("file name =%s\n",(char *) e->d_name);
171 ret = symlink((const char *)srcdir,(const char *)destdir);
174 DEBUG_ERROR("symlink is failed \n");
188 static int move_file(const char *srcpath, const char *destpath)
194 d = opendir (srcpath);
195 while ((e = readdir (d)))
197 if (e->d_name[0] != '.' && strlen((char *)e->d_name) < MAX_FILE_LEN)
199 char srcdir[MAX_FILE_LEN];
200 char destdir[MAX_FILE_LEN];
203 if (strlen (srcpath) + strlen ((char *)e->d_name) + 2 >= MAX_FILE_LEN )
205 DEBUG_ERROR("srcdir length is not available \n");
209 if (strlen (destpath) + strlen ((char *)e->d_name) + 2 >= MAX_FILE_LEN )
211 DEBUG_ERROR("destdir length is not available \n");
215 sprintf(srcdir,"%s/%s",srcpath,(char *) e->d_name);
216 sprintf(destdir,"%s/%s",destpath,(char *) e->d_name);
217 if (stat (srcdir, &statb) == -1)
219 DEBUG_ERROR("stat %s is failed \n",srcdir);
223 if (S_ISDIR (statb.st_mode))
225 if (make_dir(destdir)<0)
227 DEBUG_ERROR("make current directory %s is failed \n",destdir);
232 DEBUG_LOG("srcdir =%s\n",(char *) srcdir);
233 DEBUG_LOG("destdir =%s\n",(char *) destdir);
234 DEBUG_LOG("file name =%s\n",(char *) e->d_name);
236 ret = rename((const char *)srcdir,(const char *)destdir);
239 DEBUG_ERROR("symlink is failed \n");
252 #ifdef CHECK_FAMILY_NAME
253 static int check_family_name(const char *srcpath)
255 FcObjectSet *os = NULL;
256 FcFontSet* fs = NULL;
257 FcPattern* pat = NULL;
258 FcConfig* font_config = NULL;
259 bool is_only_one_family = false;
261 font_config = FcInitLoadConfigAndFonts();
263 if(font_config == NULL) {
264 DEBUG_ERROR("Failed: FcInitLoadConfigAndFonts");
268 pat = FcPatternCreate();
270 os = FcObjectSetBuild(FC_FAMILY, FC_FILE, (char *) 0);
273 fs = FcFontList(font_config, pat, os);
274 FcObjectSetDestroy(os);
279 FcPatternDestroy(pat);
286 char* only_one_family = NULL;
288 for (j = 0; j < fs->nfont; j++)
290 FcChar8 *family = NULL;
291 FcChar8 *file = NULL;
293 if (FcPatternGetString(fs->fonts[j], FC_FILE, 0, &file) == FcResultMatch)
295 int font_path_len = strlen(srcpath);
297 /* always shown for src path */
298 if (strncmp((const char*)file, srcpath, font_path_len) == 0)
300 if (FcPatternGetString(fs->fonts[j], FC_FAMILY, 0, &family) != FcResultMatch)
302 DEBUG_ERROR("Family name is invalid");
305 //DEBUG_ERROR("-------- FOUND FONT - family = %s", (char *)family);
307 // first found family name
308 if (only_one_family == NULL && strlen((char *)family) > 0)
310 is_only_one_family = true;
311 only_one_family = (char *)family;
312 //DEBUG_ERROR("--------First FOUND FONT - family = %s", only_one_family);
314 else if (only_one_family != NULL && strlen((char *)family) > 0)// after first
316 if (strcmp(only_one_family, (char *)family) != 0)
318 DEBUG_ERROR("Not supported various font famliy. only one font famliy. %s %s", only_one_family, (char *)family);
319 is_only_one_family = false;
325 DEBUG_ERROR("invalid fonts");
332 FcFontSetDestroy(fs);
336 FcConfigDestroy(font_config);
339 if (is_only_one_family == false)
341 //DEBUG_ERROR("Not supported various font famliy. only one font famliy");
349 static int do_install(const char *parent, const char *appid, const char *rootpath, pkgmgrinfo_pkginfo_h handle)
351 char destdir[MAX_FILE_LEN];
352 char path[MAX_FILE_LEN];
356 if (strlen (parent) + strlen (appid) + 2 >= MAX_FILE_LEN )
358 DEBUG_ERROR("appid length is not available \n");
362 sprintf(destdir,"%s/%s",parent,appid);
363 ret = make_dir(destdir);
366 DEBUG_ERROR("make current directory %s is failed \n", destdir);
370 if (strlen (rootpath) + 2 >= MAX_FILE_LEN )
372 DEBUG_ERROR("rootpath length is not available \n");
376 ret = pkgmgrinfo_pkginfo_get_type(handle, &type);
379 DEBUG_ERROR("pkgmgrinfo_pkginfo_get_type is failed\n");
383 sprintf(path,"%s/shared/res", rootpath);
385 if (!strcmp(type,"wgt"))
387 char srcpath[MAX_FILE_LEN];
388 sprintf(srcpath,"%s/res/wgt/shared/res", rootpath);
389 ret = move_file(srcpath, path);
394 DEBUG_ERROR("move_file is failed\n");
398 ret = symbolic_link(path, destdir);
402 DEBUG_ERROR("install is failed \n", destdir);
406 #ifdef CHECK_FAMILY_NAME
407 ret = check_family_name(destdir);
411 DEBUG_ERROR("Invaid font files\n");
419 do_uninstall(destdir);
424 static int do_uninstall(const char *deletedir)
429 d = opendir(deletedir);
430 while((e = readdir (d)))
432 if (e->d_name[0] != '.')
434 char destfile[MAX_FILE_LEN];
437 if (strlen (deletedir) + strlen ((char *) e->d_name) + 2 >= MAX_FILE_LEN )
439 DEBUG_ERROR("destfile length is not available \n");
443 sprintf(destfile,"%s/%s",deletedir,(char *) e->d_name);
444 if (lstat (destfile, &statb) == -1)
446 DEBUG_ERROR("lstat %s is failed \n",destfile);
450 if (S_ISDIR (statb.st_mode))
452 if (do_uninstall(destfile)<0)
455 else if (unlink(destfile) < 0)
457 DEBUG_ERROR("unlink is failed %s\n",destfile);
460 DEBUG_LOG("destfile =%s\n",destfile);
466 if (rmdir(deletedir) < 0)
468 DEBUG_ERROR("rmdir is failed \n");
472 DEBUG_LOG("rmdir =%s\n",deletedir);
481 int COMMON_PKGMGR_PLUGIN_INSTALL(const char *pkgid, const char *appid, GList *list)
484 pkgmgrinfo_pkginfo_h handle = NULL;
485 const char *app_root_path = NULL;
486 const char *dest_path = NULL;
489 pkgmgr_installer_info_get_target_uid(&uid);
490 ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid, uid, &handle);
494 DEBUG_ERROR("pkgid[%s] handle get fail", pkgid);
498 ret = pkgmgrinfo_pkginfo_get_root_path(handle, (char **)&app_root_path);
501 DEBUG_ERROR("pkgid[%s] path get fail", pkgid);
507 DEBUG_ERROR("appid is NULL \n");
511 ret = make_dir(PARENT_PATH);
515 DEBUG_ERROR("make current directory is failed \n");
519 dest_path = check_preloaded(app_root_path);
521 if (make_dir(dest_path)<0)
523 DEBUG_ERROR("make current directory is failed \n");
527 ret = do_install(dest_path,appid, app_root_path, handle);
530 DEBUG_ERROR("font install is failed \n");
534 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
539 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
544 int COMMON_PKGMGR_PLUGIN_UPGRADE(const char *pkgid, const char *appid, GList *list)
546 char deletedir[MAX_FILE_LEN];
548 pkgmgrinfo_pkginfo_h handle = NULL;
549 const char* app_root_path = NULL;
550 const char* dest_path = NULL;
553 pkgmgr_installer_info_get_target_uid(&uid);
554 ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid, uid, &handle);
558 DEBUG_ERROR("pkgid[%s] handle get fail", pkgid);
562 ret = pkgmgrinfo_pkginfo_get_root_path(handle, (char **)&app_root_path);
565 DEBUG_ERROR("pkgid[%s] path get fail", pkgid);
571 DEBUG_ERROR("appid is NULL \n");
575 dest_path = check_preloaded(app_root_path);
577 if (strlen (dest_path) + strlen (appid) + 2 >= MAX_FILE_LEN )
579 DEBUG_ERROR("appid length is not available \n");
583 sprintf(deletedir,"%s/%s", dest_path, appid);
585 if (access(deletedir, F_OK) == -1)
587 DEBUG_ERROR("dest directory(%s) is not exist: %s\n", deletedir, strerror(errno));
591 ret = do_uninstall(deletedir);
594 DEBUG_ERROR("do_uninstall is failed \n");
598 ret = make_dir(PARENT_PATH);
601 DEBUG_ERROR("make current directory(%s) is failed: %s\n", PARENT_PATH, strerror(errno));
605 ret = make_dir(dest_path);
608 DEBUG_ERROR("make current directory(%s) is failed: %s\n", dest_path, strerror(errno));
612 ret = do_install(dest_path, appid, app_root_path, handle);
615 DEBUG_ERROR("do_install is failed \n");
619 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
623 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
629 int COMMON_PKGMGR_PLUGIN_UNINSTALL(const char *pkgid, const char *appid, GList *list)
631 char deletedir[MAX_FILE_LEN];
632 FcObjectSet *os = NULL;
633 FcPattern *pat = NULL;
634 FcFontSet *fs = NULL;
635 pkgmgrinfo_pkginfo_h handle = NULL;
636 const char* app_root_path = NULL;
637 const char *dest_path = NULL;
645 DEBUG_ERROR("appid is NULL \n");
649 pkgmgr_installer_info_get_target_uid(&uid);
650 ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid, uid, &handle);
654 DEBUG_ERROR("pkgid[%s] handle get fail", pkgid);
658 ret = pkgmgrinfo_pkginfo_get_root_path(handle, (char **)&app_root_path);
661 DEBUG_ERROR("pkgid[%s] path get fail", pkgid);
665 dest_path = check_preloaded(app_root_path);
667 if (strlen (dest_path) + strlen (appid) + 2 >= MAX_FILE_LEN )
669 DEBUG_ERROR("appid length is not available \n");
673 sprintf(deletedir,"%s/%s", dest_path, appid);
675 //check if current using font is same with uninstall font
676 int 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);
706 for (j = 0; j < fs->nfont; j++)
708 FcChar8 *file = NULL;
709 if (FcPatternGetString(fs->fonts[j], FC_FILE, 0, &file) == FcResultMatch)
711 DEBUG_LOG("file =%s\n",file);
712 if (strncmp((const char*)file , deletedir , deletedir_len) == 0 )
714 DEBUG_LOG("change to default font\n");
715 char *default_font_name = NULL;
717 ret = system_settings_get_value_string(SYSTEM_SETTINGS_KEY_DEFAULT_FONT_TYPE, &default_font_name);
720 DEBUG_ERROR("Get default font is failed \n");
724 DEBUG_LOG("default_font_name = %s \n",default_font_name);
725 setenv("HOME", "/opt/home/app", 1);
727 ret = system_settings_set_value_string(SYSTEM_SETTINGS_KEY_FONT_TYPE, default_font_name);
730 DEBUG_ERROR("Set default font is failed ret=%d \n",ret);
731 free(default_font_name);
735 char *elm_profile_path = (char*)elm_config_profile_dir_get(elm_config_profile_get(), EINA_TRUE);
737 setenv("HOME", "/root", 1);
739 ret = chown(ELM_PROFILE_CFG, APP_OWNER_ID, APP_GROUP_ID);
742 chmod (ELM_PROFILE_CFG, 0777);
748 if (elm_profile_path)
749 d = opendir (elm_profile_path);
753 while ((e = readdir (d)))
755 if (e->d_name[0] != '.' && (strstr(e->d_name, ".cfg") != 0 || strstr(e->d_name, ".CFG") != 0))
757 char file_full_path[100];
759 sprintf(file_full_path, "%s/%s", elm_profile_path, e->d_name);
761 ret = chown(file_full_path, APP_OWNER_ID, APP_GROUP_ID);
764 DEBUG_LOG("chown is failed %s", file_full_path);
765 chmod (file_full_path, 0777);
773 free(default_font_name);
774 free(elm_profile_path);
775 DEBUG_LOG("success change to default font\n");
782 if (access(deletedir, F_OK) == -1)
784 DEBUG_ERROR("dest directory(%s) is not exist: %s\n", deletedir, strerror(errno));
789 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
791 return do_uninstall(deletedir);
795 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
801 int PKGMGR_MDPARSER_PLUGIN_INSTALL(const char *pkgid, const char *appid, GList *list)
803 return COMMON_PKGMGR_PLUGIN_INSTALL(pkgid, appid, list);
806 int PKGMGR_MDPARSER_PLUGIN_UPGRADE(const char *pkgid, const char *appid, GList *list)
808 return COMMON_PKGMGR_PLUGIN_UPGRADE(pkgid, appid, list);
811 int PKGMGR_MDPARSER_PLUGIN_UNINSTALL(const char *pkgid, const char *appid, GList *list)
813 return COMMON_PKGMGR_PLUGIN_UNINSTALL(pkgid, appid, list);
817 int PKGMGR_CATEGORY_PARSER_PLUGIN_INSTALL(const char *pkgid, const char *appid, GList *list)
819 return COMMON_PKGMGR_PLUGIN_INSTALL(pkgid, appid, list);
822 int PKGMGR_CATEGORY_PARSER_PLUGIN_UPGRADE(const char *pkgid, const char *appid, GList *list)
824 return COMMON_PKGMGR_PLUGIN_UPGRADE(pkgid, appid, list);
827 int PKGMGR_CATEGORY_PARSER_PLUGIN_UNINSTALL(const char *pkgid, const char *appid, GList *list)
829 return COMMON_PKGMGR_PLUGIN_UNINSTALL(pkgid, appid, list);