all repos — tint2 @ 690f30308fb252dc095fd3cb4a4a3d02408a9fd1

fork of the tint2 desktop panel for my custom setup - only minimized windows across all desktops for the taskbar

src/launcher/apps-common.c (raw)

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
294
295
296
297
298
299
300
301
302
303
304
305
306
/**************************************************************************
* Tint2 : .desktop file handling
*
* Copyright (C) 2015       (mrovi9000@gmail.com)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License version 2
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
**************************************************************************/

/* http://standards.freedesktop.org/desktop-entry-spec/ */

#include "apps-common.h"
#include "common.h"
#include "strnatcmp.h"

#include <glib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static gint compare_strings(gconstpointer a, gconstpointer b)
{
	return strnatcasecmp((const char *)a, (const char *)b);
}

int parse_dektop_line(char *line, char **key, char **value)
{
	char *p;
	int found = 0;
	*key = line;
	for (p = line; *p; p++) {
		if (*p == '=') {
			*value = p + 1;
			*p = 0;
			found = 1;
			break;
		}
	}
	if (!found)
		return 0;
	if (found && (strlen(*key) == 0 || strlen(*value) == 0))
		return 0;
	return 1;
}

void expand_exec(DesktopEntry *entry, const char *path)
{
	// Expand % in exec
	// %i -> --icon Icon
	// %c -> Name
	// %k -> path
	if (entry->exec) {
		char *exec2 = calloc(strlen(entry->exec) + (entry->name ? strlen(entry->name) : 1) +
		                         (entry->icon ? strlen(entry->icon) : 1) + 100,
		                     1);
		char *p, *q;
		// p will never point to an escaped char
		for (p = entry->exec, q = exec2; *p; p++, q++) {
			*q = *p; // Copy
			if (*p == '\\') {
				p++, q++;
				// Copy the escaped char
				if (*p == '%') // For % we delete the backslash, i.e. write % over it
					q--;
				*q = *p;
				if (!*p)
					break;
				continue;
			}
			if (*p == '%') {
				p++;
				if (!*p)
					break;
				if (*p == 'i' && entry->icon != NULL) {
					sprintf(q, "--icon '%s'", entry->icon);
					q += strlen("--icon ''");
					q += strlen(entry->icon);
					q--; // To balance the q++ in the for
				} else if (*p == 'c' && entry->name != NULL) {
					sprintf(q, "'%s'", entry->name);
					q += strlen("''");
					q += strlen(entry->name);
					q--; // To balance the q++ in the for
				} else if (*p == 'c') {
					sprintf(q, "'%s'", path);
					q += strlen("''");
					q += strlen(path);
					q--; // To balance the q++ in the for
				} else {
					// We don't care about other expansions
					q--; // Delete the last % from q
				}
				continue;
			}
		}
		*q = '\0';
		free(entry->exec);
		entry->exec = exec2;
	}
}

gboolean read_desktop_file_full_path(const char *path, DesktopEntry *entry)
{
	entry->name = entry->generic_name = entry->icon = entry->exec = NULL;
	entry->hidden_from_menus = FALSE;

	FILE *fp = fopen(path, "rt");
	if (fp == NULL) {
		fprintf(stderr, "Could not open file %s\n", path);
		return FALSE;
	}

	const gchar **languages = (const gchar **)g_get_language_names();
	// lang_index is the index of the language for the best Name key in the language vector
	// lang_index_default is a constant that encodes the Name key without a language
	int lang_index_default = 1;
#define LANG_DBG 0
	if (LANG_DBG)
		printf("Languages:");
	for (int i = 0; languages[i]; i++) {
		lang_index_default = i + 1;
		if (LANG_DBG)
			printf(" %s", languages[i]);
	}
	if (LANG_DBG)
		printf("\n");
	// we currently do not know about any Name key at all, so use an invalid index
	int lang_index_name = lang_index_default + 1;
	int lang_index_generic_name = lang_index_default + 1;

	gboolean inside_desktop_entry = 0;
	char *line = NULL;
	size_t line_size;
	while (getline(&line, &line_size, fp) >= 0) {
		int len = strlen(line);
		if (len == 0)
			continue;
		if (line[len - 1] == '\n')
			line[len - 1] = '\0';
		if (line[0] == '[') {
			inside_desktop_entry = (strcmp(line, "[Desktop Entry]") == 0);
		}
		char *key, *value;
		if (inside_desktop_entry && parse_dektop_line(line, &key, &value)) {
			if (strstr(key, "Name") == key) {
				if (strcmp(key, "Name") == 0 && lang_index_name > lang_index_default) {
					entry->name = strdup(value);
					lang_index_name = lang_index_default;
				} else {
					for (int i = 0; languages[i] && i < lang_index_name; i++) {
						gchar *localized_key = g_strdup_printf("Name[%s]", languages[i]);
						if (strcmp(key, localized_key) == 0) {
							if (entry->name)
								free(entry->name);
							entry->name = strdup(value);
							lang_index_name = i;
						}
						g_free(localized_key);
					}
				}
			} else if (strstr(key, "GenericName") == key) {
				if (strcmp(key, "GenericName") == 0 && lang_index_generic_name > lang_index_default) {
					entry->generic_name = strdup(value);
					lang_index_generic_name = lang_index_default;
				} else {
					for (int i = 0; languages[i] && i < lang_index_generic_name; i++) {
						gchar *localized_key = g_strdup_printf("GenericName[%s]", languages[i]);
						if (strcmp(key, localized_key) == 0) {
							if (entry->generic_name)
								free(entry->generic_name);
							entry->generic_name = strdup(value);
							lang_index_generic_name = i;
						}
						g_free(localized_key);
					}
				}
			} else if (!entry->exec && strcmp(key, "Exec") == 0) {
				entry->exec = strdup(value);
			} else if (!entry->icon && strcmp(key, "Icon") == 0) {
				entry->icon = strdup(value);
			} else if (strcmp(key, "NoDisplay") == 0) {
				entry->hidden_from_menus = strcasecmp(value, "true") == 0;
			}
		}
	}
	fclose(fp);
	// From this point:
	// entry->name, entry->generic_name, entry->icon, entry->exec will never be empty strings (can be NULL though)

	expand_exec(entry, entry->path);

	free(line);
	return entry->exec != NULL;
}

gboolean read_desktop_file_from_dir(const char *path, const char *file_name, DesktopEntry *entry)
{
	gchar *full_path = g_build_filename(path, file_name, NULL);
	if (read_desktop_file_full_path(full_path, entry)) {
		g_free(full_path);
		return TRUE;
	}
	free(entry->name);
	free(entry->generic_name);
	free(entry->icon);
	free(entry->exec);
	entry->name = entry->generic_name = entry->icon = entry->exec = NULL;

	GList *subdirs = NULL;

	GDir *d = g_dir_open(path, 0, NULL);
	if (d) {
		const gchar *name;
		while ((name = g_dir_read_name(d))) {
			gchar *child = g_build_filename(path, name, NULL);
			if (g_file_test(child, G_FILE_TEST_IS_DIR)) {
				subdirs = g_list_append(subdirs, child);
			} else {
				g_free(child);
			}
		}
		g_dir_close(d);
	}

	subdirs = g_list_sort(subdirs, compare_strings);
	gboolean found = FALSE;
	for (GList *l = subdirs; l; l = g_list_next(l)) {
		if (read_desktop_file_from_dir(l->data, file_name, entry)) {
			found = TRUE;
			break;
		}
	}

	for (GList *l = subdirs; l; l = g_list_next(l)) {
		g_free(l->data);
	}
	g_list_free(subdirs);
	g_free(full_path);

	return found;
}

gboolean read_desktop_file(const char *path, DesktopEntry *entry)
{
	entry->path = strdup(path);
	entry->name = entry->generic_name = entry->icon = entry->exec = NULL;

	if (strchr(path, '/'))
		return read_desktop_file_full_path(path, entry);
	for (const GSList *location = get_apps_locations(); location; location = g_slist_next(location)) {
		if (read_desktop_file_from_dir(location->data, path, entry))
			return TRUE;
	}
	return FALSE;
}

void free_desktop_entry(DesktopEntry *entry)
{
	free(entry->name);
	free(entry->generic_name);
	free(entry->icon);
	free(entry->exec);
	free(entry->path);
	entry->name = entry->generic_name = entry->icon = entry->exec = entry->path = NULL;
}

void test_read_desktop_file()
{
	fprintf(stdout, "\033[1;33m");
	DesktopEntry entry;
	read_desktop_file("/usr/share/applications/firefox.desktop", &entry);
	printf("Name:%s GenericName:%s Icon:%s Exec:%s\n", entry.name, entry.generic_name, entry.icon, entry.exec);
	fprintf(stdout, "\033[0m");
}

GSList *apps_locations = NULL;
// Do not free the result.
const GSList *get_apps_locations()
{
	if (apps_locations)
		return apps_locations;

	apps_locations = load_locations_from_env(apps_locations, "XDG_DATA_HOME", "applications", NULL);

	apps_locations =
	    g_slist_append(apps_locations, g_build_filename(g_get_home_dir(), ".local/share/applications", NULL));

	apps_locations = load_locations_from_env(apps_locations, "XDG_DATA_DIRS", "applications", NULL);

	apps_locations = g_slist_append(apps_locations, g_strdup("/usr/local/share/applications"));
	apps_locations = g_slist_append(apps_locations, g_strdup("/usr/share/applications"));
	apps_locations = g_slist_append(apps_locations, g_strdup("/opt/share/applications"));

	apps_locations = slist_remove_duplicates(apps_locations, g_str_equal, g_free);

	return apps_locations;
}