2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
25 #include "launcher_info.h"
26 #include "launchpad_common.h"
28 #define TAG_LAUNCHER "[LAUNCHER]"
29 #define TAG_NAME "NAME"
31 #define TAG_APP_TYPE "APP_TYPE"
32 #define TAG_EXTRA_ARG "EXTRA_ARG"
34 struct launcher_info_s {
41 static struct launcher_info_s *__create_launcher_info(void)
43 struct launcher_info_s *info;
45 info = calloc(1, sizeof(struct launcher_info_s));
54 static void __destroy_launcher_info(gpointer data)
56 struct launcher_info_s *info = (struct launcher_info_s *)data;
62 g_list_free_full(info->extra_args, free);
64 g_list_free_full(info->app_types, free);
72 static void __parse_app_types(struct launcher_info_s *info, char *line)
77 token = strtok_r(line, " |\t\r\n", &saveptr);
79 info->app_types = g_list_append(info->app_types, strdup(token));
80 token = strtok_r(NULL, " |\t\r\n", &saveptr);
84 static GList *__parse_file(GList *list, const char *path)
90 struct launcher_info_s *info = NULL;
92 fp = fopen(path, "rt");
96 while (fgets(buf, sizeof(buf), fp) != NULL) {
99 sscanf(buf, "%ms %ms", &tok1, &tok2);
100 if (tok1 && strcasecmp(TAG_LAUNCHER, tok1) == 0) {
102 _D("name: %s, exe: %s", info->name, info->exe);
103 list = g_list_append(list, info);
106 info = __create_launcher_info();
115 if (tok1[0] == '\0' || tok2[0] == '\0' || tok1[0] == '#')
120 if (strcasecmp(TAG_NAME, tok1) == 0) {
121 info->name = strdup(tok2);
122 if (info->name == NULL) {
124 __destroy_launcher_info(info);
128 } else if (strcasecmp(TAG_EXE, tok1) == 0) {
129 info->exe = strdup(tok2);
130 if (info->exe == NULL) {
132 __destroy_launcher_info(info);
136 if (access(info->exe, F_OK | X_OK) != 0) {
137 _E("Failed to access %s", info->exe);
138 __destroy_launcher_info(info);
141 } else if (strcasecmp(TAG_APP_TYPE, tok1) == 0) {
142 __parse_app_types(info, &buf[strlen(tok1)]);
143 if (info->app_types == NULL) {
144 _E("app_types is NULL");
145 __destroy_launcher_info(info);
149 } else if (strcasecmp(TAG_EXTRA_ARG, tok1) == 0) {
150 info->extra_args = g_list_append(info->extra_args,
157 _D("name: %s, exe: %s", info->name, info->exe);
158 list = g_list_append(list, info);
169 GList *_launcher_info_load(const char *path)
172 struct dirent *dentry = NULL;
184 while ((dentry = readdir(dp)) != NULL) {
185 if (dentry->d_name[0] == '.')
188 ext = strrchr(dentry->d_name, '.');
189 if (ext && strcmp(ext, ".launcher") == 0) {
190 snprintf(buf, sizeof(buf), "%s/%s",
191 path, dentry->d_name);
192 list = __parse_file(list, buf);
200 void _launcher_info_unload(GList *info)
205 g_list_free_full(info, __destroy_launcher_info);
208 static int __comp_str(gconstpointer a, gconstpointer b)
210 if (a == NULL || b == NULL)
216 static int __comp_app_type(gconstpointer a, gconstpointer b)
218 struct launcher_info_s *info = (struct launcher_info_s *)a;
220 if (info == NULL || info->app_types == NULL || b == NULL)
223 if (g_list_find_custom(info->app_types, b, __comp_str))
229 launcher_info_h _launcher_info_find(GList *info_list, const char *app_type)
233 if (info_list == NULL || app_type == NULL)
236 list = g_list_find_custom(info_list, app_type, __comp_app_type);
240 return (launcher_info_h)list->data;
243 const char *_launcher_info_get_exe(launcher_info_h info)
251 GList *_launcher_info_get_extra_args(launcher_info_h info)
256 return info->extra_args;