summaryrefslogtreecommitdiff
path: root/src/launcher_info.c
blob: bff7a1208584c8afced42f6ee6e952f37d28e4e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the License);
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an AS IS BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#define _GNU_SOURCE

#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <dirent.h>
#include <string.h>
#include <unistd.h>

#include "launcher_info.h"
#include "launchpad_common.h"

#define TAG_LAUNCHER	"[LAUNCHER]"
#define TAG_NAME	"NAME"
#define TAG_EXE		"EXE"
#define TAG_APP_TYPE	"APP_TYPE"
#define TAG_EXTRA_ARG	"EXTRA_ARG"

struct launcher_info_s {
	char *name;
	char *exe;
	GList *app_types;
	GList *extra_args;
};

static struct launcher_info_s *__create_launcher_info(void)
{
	struct launcher_info_s *info;

	info = calloc(1, sizeof(struct launcher_info_s));
	if (info == NULL) {
		_E("out of memory");
		return NULL;
	}

	return info;
}

static void __destroy_launcher_info(gpointer data)
{
	struct launcher_info_s *info = (struct launcher_info_s *)data;

	if (info == NULL)
		return;

	if (info->extra_args)
		g_list_free_full(info->extra_args, free);
	if (info->app_types)
		g_list_free_full(info->app_types, free);
	if (info->exe)
		free(info->exe);
	if (info->name)
		free(info->name);
	free(info);
}

static void __parse_app_types(struct launcher_info_s *info, char *line)
{
	char *token;
	char *saveptr = NULL;
	char tok[LINE_MAX];

	token = strtok_r(line, "|", &saveptr);
	while (token) {
		tok[0] = '\0';
		sscanf(token, "%s", tok);
		if (tok[0] != '\0' && strcasecmp(tok, "null") != 0) {
			info->app_types = g_list_append(info->app_types,
					strdup(tok));
		}
		token = strtok_r(NULL, "|", &saveptr);
	}
}

static GList *__parse_file(GList *list, const char *path)
{
	FILE *fp;
	char buf[LINE_MAX];
	char tok1[LINE_MAX];
	char tok2[LINE_MAX];
	struct launcher_info_s *info = NULL;

	fp = fopen(path, "rt");
	if (fp == NULL)
		return list;

	while (fgets(buf, sizeof(buf), fp) != NULL) {
		tok1[0] = '\0';
		tok2[0] = '\0';
		sscanf(buf, "%s %s", tok1, tok2);

		if (strcasecmp(TAG_LAUNCHER, tok1) == 0) {
			if (info) {
				_D("name: %s, exe: %s", info->name, info->exe);
				list = g_list_append(list, info);
			}

			info = __create_launcher_info();
			if (info == NULL)
				break;

			continue;
		}

		if (tok1[0] == '\0' || tok2[0] == '\0' || tok1[0] == '#')
			continue;
		if (info == NULL)
			continue;

		if (strcasecmp(TAG_NAME, tok1) == 0) {
			info->name = strdup(tok2);
			if (info->name == NULL) {
				_E("out of memory");
				__destroy_launcher_info(info);
				info = NULL;
				break;
			}
		} else if (strcasecmp(TAG_EXE, tok1) == 0) {
			info->exe = strdup(tok2);
			if (info->exe == NULL) {
				_E("out of memory");
				__destroy_launcher_info(info);
				info = NULL;
				break;
			}
			if (access(info->exe, F_OK | X_OK) != 0) {
				_E("Failed to access %s", info->exe);
				__destroy_launcher_info(info);
				info = NULL;
			}
		} else if (strcasecmp(TAG_APP_TYPE, tok1) == 0) {
			__parse_app_types(info, &buf[strlen(tok1)]);
			if (info->app_types == NULL) {
				_E("app_types is NULL");
				__destroy_launcher_info(info);
				info = NULL;
				break;
			}
		} else if (strcasecmp(TAG_EXTRA_ARG, tok1) == 0) {
			info->extra_args = g_list_append(info->extra_args,
					strdup(tok2));
		}
	}
	fclose(fp);

	if (info) {
		_D("name: %s, exe: %s", info->name, info->exe);
		list = g_list_append(list, info);
	}

	return list;
}

GList *_launcher_info_load(const char *path)
{
	DIR *dp;
	struct dirent *dentry = NULL;
	GList *list = NULL;
	char buf[PATH_MAX];
	char *ext;

	if (path == NULL)
		return NULL;

	dp = opendir(path);
	if (dp == NULL)
		return NULL;

	while ((dentry = readdir(dp)) != NULL) {
		if (dentry->d_name[0] == '.')
			continue;

		ext = strrchr(dentry->d_name, '.');
		if (ext && strcmp(ext, ".launcher") == 0) {
			snprintf(buf, sizeof(buf), "%s/%s",
					path, dentry->d_name);
			list = __parse_file(list, buf);
		}
	}
	closedir(dp);

	return list;
}

void _launcher_info_unload(GList *info)
{
	if (info == NULL)
		return;

	g_list_free_full(info, __destroy_launcher_info);
}

static int __comp_str(gconstpointer a, gconstpointer b)
{
	if (a == NULL || b == NULL)
		return -1;

	return strcmp(a, b);
}

static int __comp_app_type(gconstpointer a, gconstpointer b)
{
	struct launcher_info_s *info = (struct launcher_info_s *)a;

	if (info == NULL || info->app_types == NULL || b == NULL)
		return -1;

	if (g_list_find_custom(info->app_types, b, __comp_str))
		return 0;

	return -1;
}

launcher_info_h _launcher_info_find(GList *info_list, const char *app_type)
{
	GList *list;

	if (info_list == NULL || app_type == NULL)
		return NULL;

	list = g_list_find_custom(info_list, app_type, __comp_app_type);
	if (list == NULL)
		return NULL;

	return (launcher_info_h)list->data;
}

const char *_launcher_info_get_exe(launcher_info_h info)
{
	if (info == NULL)
		return NULL;

	return info->exe;
}

GList *_launcher_info_get_extra_args(launcher_info_h info)
{
	if (info == NULL)
		return NULL;

	return info->extra_args;
}