summaryrefslogtreecommitdiff
path: root/tz-launcher/tz-launcher.c
blob: 7a50fc203800c04268c2d83edb1060e129583e85 (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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/* Copyright © 2014 Manuel Bachmann */

#include <glib.h>
#include <gio/gio.h>

#define MAX_DESKTOPFILES 100

int desktopfiles;
gchar **desktoptable[MAX_DESKTOPFILES];

int global_argc;
char **global_argv;


gboolean
file_is_parsable (GFile *file)
{
	gboolean result;

	GKeyFile *keyfile = g_key_file_new ();
	if (g_key_file_load_from_file (keyfile, g_file_get_path(file), G_KEY_FILE_NONE, NULL))
		result = TRUE;
	else
		result = FALSE;

	g_key_file_free (keyfile);
	return result;
}

gboolean
file_is_desktop_file (GFile *file)
{
	gboolean result;

	GKeyFile *keyfile = g_key_file_new ();
	g_key_file_load_from_file (keyfile, g_file_get_path(file), G_KEY_FILE_NONE, NULL);

	if (g_key_file_has_group (keyfile, "Desktop Entry"))
		return TRUE;
	else
		return FALSE;

	g_key_file_free (keyfile);
	return result;
}

gboolean
file_is_desktop_file_for_application (GFile *file, gboolean store_values, gchar ***table)
{
	gboolean result;

	GKeyFile *keyfile = g_key_file_new ();
	g_key_file_load_from_file (keyfile, g_file_get_path(file), G_KEY_FILE_NONE, NULL);

	GError *error = NULL;
	gchar **values = g_new (gchar*, 7);
	values[0] = g_key_file_get_value (keyfile, "Desktop Entry", "Name", &error);
	values[1] = g_key_file_get_value (keyfile, "Desktop Entry", "Comment", NULL);
	values[2] = g_key_file_get_value (keyfile, "Desktop Entry", "Exec", &error);
	values[3] = g_key_file_get_value (keyfile, "Desktop Entry", "Icon", NULL);
	values[4] = g_key_file_get_value (keyfile, "Desktop Entry", "Terminal", NULL);
	values[5] = g_key_file_get_value (keyfile, "Desktop Entry", "Type", &error);
	values[6] = NULL;

	if (!values[1]) values[1] = g_strdup (values[0]);
	if (!values[3]) values[3] = g_strdup (values[0]);
	if (!values[4]) values[4] = g_strdup ("false");

	if (error) {
		result = FALSE;
		g_error_free (error);
		g_strfreev(values);
	} else {
		result = TRUE;
		if (store_values)
			*table = values;
		else
			g_strfreev(values);
	}

	g_key_file_free (keyfile);
	return result;
}


void
tz_launcher_parse_file (GFile *file)
{
	if (!file_is_parsable (file)) {
		g_printerr ("File \"%s\" is not parsable !\n",
		            g_file_get_path(file));
		return;
	}

	if (!file_is_desktop_file (file)) {
		g_printerr ("File \"%s\" is not a .desktop file !\n",
		            g_file_get_path(file));
		return;
	}

	gchar **table;
	if (!file_is_desktop_file_for_application (file, 1, &table)) {
		g_printerr ("File \"%s\" is not a .desktop file for an application !\n",
		            g_file_get_path(file));
		return;
	}

	desktoptable[desktopfiles] = g_strdupv(table);
	desktopfiles++;

	g_strfreev(table);
}


void
tz_launcher_parse_directory (GFile *directory)
{
	GError *error = NULL;
	GFileInfo *info = NULL;

	GFileEnumerator *filelist = g_file_enumerate_children (directory,
	                                                       G_FILE_ATTRIBUTE_STANDARD_NAME,
	                                                       0, NULL, &error);
	if (error) {
		g_printerr ("Failed to enumerate content of directory \"%s\" : %s\n",
		            g_file_get_path(directory), error->message);
		g_error_free (error);
		g_object_unref (filelist);
		return;
	}

	while ((info = g_file_enumerator_next_file (filelist, NULL, &error)) != NULL) {
		if (error) {
			g_printerr ("Failed to get info about element of directory \"%s\" : %s\n",
			            g_file_get_path(directory), error->message);
			g_error_free (error);
			g_object_unref (info);
			continue;
		}

		gchar *path = g_strconcat (g_file_get_path(directory), G_DIR_SEPARATOR_S,
			                       g_file_info_get_name (info), NULL);
		GFile *file = g_file_new_for_path (path);
		g_free (path);

		GFileType type = g_file_info_get_file_type (info);
		switch (type) {
			case G_FILE_TYPE_REGULAR:
			case G_FILE_TYPE_SYMBOLIC_LINK:
			case G_FILE_TYPE_UNKNOWN:
				tz_launcher_parse_file (file);
				break;
			case G_FILE_TYPE_DIRECTORY:
				tz_launcher_parse_directory (file);
				break;
		}
		g_object_unref (file);
		g_object_unref (info);
	}

	g_object_unref (filelist);
}

void
tz_launcher_parse_config_file (GFile *file)
{
	GFileInputStream *istream;
	GDataInputStream *stream;
	char *line;
	GFile *linefile;
	GFileType type;

	istream = g_file_read (file, NULL, NULL);
	stream = g_data_input_stream_new (G_INPUT_STREAM(istream));
	while (line = g_data_input_stream_read_line (stream, NULL, NULL, NULL)) {
		line = g_strstrip (line);
		if ((strcmp (line, "")) && (!g_str_has_prefix (line, "#"))) {
			linefile = g_file_new_for_path (line);
			if (g_file_query_exists (linefile, NULL)) {

				type = g_file_query_file_type (linefile, 0, NULL);
				switch (type) {
				case G_FILE_TYPE_SPECIAL:
					g_printerr ("File \"%s\" is special !\n", line);
					break;
				case G_FILE_TYPE_REGULAR:
				case G_FILE_TYPE_SYMBOLIC_LINK:
				case G_FILE_TYPE_UNKNOWN:
					tz_launcher_parse_file (linefile);
					break;
				case G_FILE_TYPE_DIRECTORY:
					tz_launcher_parse_directory (linefile);
					break;
				}

			}
			g_object_unref (linefile);
		}
		g_free (line);
	}
	g_object_unref (stream);
	g_object_unref (istream);
}


void
parse_args (int argc, char **argv)
{

	int i;
	desktopfiles = 0;
	GFile *desktopfile;

	for (i = 1; i < argc ; i++) {

		if (!strcmp (argv[i], "-c")) {
			desktopfile = g_file_new_for_path (argv[i+1]);
			if (!g_file_query_exists (desktopfile, NULL)) {
				g_printerr ("Config file \"%s\" does not exist !\n", argv[i+1]);
				i++; continue;
			}
			tz_launcher_parse_config_file (desktopfile);
			g_object_unref (desktopfile);
			i++; continue;
		}

		desktopfile = g_file_new_for_path (argv[i]);

		if (!g_file_query_exists (desktopfile, NULL)) {
			g_printerr ("File \"%s\" does not exist !\n", argv[i]);
			continue;
		}

		GFileType type = g_file_query_file_type (desktopfile, 0, NULL);
		switch (type) {
			case G_FILE_TYPE_SPECIAL:
				g_printerr ("File \"%s\" is special !\n", argv[i]);
				break;
			case G_FILE_TYPE_REGULAR:
			case G_FILE_TYPE_SYMBOLIC_LINK:
			case G_FILE_TYPE_UNKNOWN:
				tz_launcher_parse_file (desktopfile);
				break;
			case G_FILE_TYPE_DIRECTORY:
				tz_launcher_parse_directory (desktopfile);
				break;
		}

		g_object_unref (desktopfile);

		if (desktopfiles == MAX_DESKTOPFILES)
			break;
	}
}

static void
sigreload_handler (int s)
{
	parse_args (global_argc, global_argv);

	if (desktopfiles > 0)
		tz_launcher_wl_reload (desktopfiles, desktoptable);
}

int
main (int argc, char **argv)
{
	if (argc < 2) {
		g_print ("Usage : tz-launcher <file1>.desktop <file2>.desktop <directory>...\n"
                         "         or\n"
                         "        tz-launcher -c <list-of-.desktop-files>.conf\n");
		return 0;
	}

	g_type_init ();


	signal (SIGUSR1, sigreload_handler);
	global_argc = argc;
	global_argv = argv;


	parse_args (argc, argv);

	if (desktopfiles > 0)
		tz_launcher_wl_run (desktopfiles, desktoptable);
	else
		g_printerr ("No .desktop files found !\n");


	return 0;
}