forked from jgmenu/jgmenu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxdgapps.c
168 lines (134 loc) · 3.63 KB
/
xdgapps.c
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
/*
* xdgapps.c creates cache for .desktop- and .directory-files
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include "xdgapps.h"
#include "xdgdirs.h"
#include "util.h"
#include "sbuf.h"
#include "list.h"
struct list_head desktop_files_all;
struct list_head desktop_files_filtered;
struct list_head directory_files;
/* "${HOME}/.local/share", "/usr/share", etc. */
static struct list_head xdg_data_dirs;
static void parse_directory_file(FILE *fp, const char *filename)
{
char line[8192];
char *p;
struct directory_file_data *tmp;
tmp = xcalloc(1, sizeof(struct directory_file_data));
tmp->filename = strdup(filename);
while (fgets(line, sizeof(line), fp)) {
p = strchr(line, '\n');
if (p)
*p = '\0';
if (!strncmp("Name=", line, 5))
tmp->name = strdup(line + 5);
else if (!strncmp("Icon=", line, 5))
tmp->icon = strdup(line + 5);
}
list_add_tail(&tmp->list, &directory_files);
}
static void parse_desktop_file(FILE *fp)
{
char line[8192];
char *p;
struct desktop_file_data *tmp;
tmp = xcalloc(1, sizeof(struct desktop_file_data));
while (fgets(line, sizeof(line), fp)) {
p = strchr(line, '\n');
if (p)
*p = '\0';
/* A bit crude, but prevents loading other Exec values */
if ((line[0] == '[') && strcmp(line, "[Desktop Entry]"))
break;
if (!strncmp("Name=", line, 5))
tmp->name = strdup(line + 5);
else if (!strncmp("Exec=", line, 5))
tmp->exec = strdup(line + 5);
else if (!strncmp("Icon=", line, 5))
tmp->icon = strdup(line + 5);
else if (!strncmp("Categories=", line, 11))
tmp->categories = strdup(line + 11);
}
/* Remove %U, %f, etc at the end of Exec cmd */
if (tmp->exec) {
p = strchr(tmp->exec, '%');
if (p)
*p = '\0';
}
list_add_tail(&tmp->full_list, &desktop_files_all);
}
static void process_file(char *filename, const char *path, int isdir)
{
FILE *fp;
char fullname[8192];
strncpy(fullname, path, strlen(path));
strncpy(fullname + strlen(path), filename, strlen(filename) + 1);
fp = fopen(fullname, "r");
if (!fp)
die("could not open file %s", filename);
if (isdir)
parse_directory_file(fp, filename);
else
parse_desktop_file(fp);
fclose(fp);
}
/*
* isdir=0 will process .desktop files
* isdir=1 will process .directory files
*/
static void populate_desktop_and_directory_lists(const char *path, int isdir)
{
struct dirent *entry;
DIR *dp;
dp = opendir(path);
if (!dp)
goto close_dir;
while ((entry = readdir(dp))) {
if (!strncmp(entry->d_name, ".", 1) ||
!strncmp(entry->d_name, "..", 2))
continue;
process_file(entry->d_name, path, isdir);
}
close_dir:
closedir(dp);
}
void xdgapps_init_lists(void)
{
struct sbuf *dir;
struct sbuf s;
INIT_LIST_HEAD(&desktop_files_all);
INIT_LIST_HEAD(&desktop_files_filtered);
INIT_LIST_HEAD(&directory_files);
INIT_LIST_HEAD(&xdg_data_dirs);
xdgdirs_get_basedirs(&xdg_data_dirs);
sbuf_init(&s);
list_for_each_entry(dir, &xdg_data_dirs, list) {
sbuf_cpy(&s, dir->buf);
sbuf_addstr(&s, "/applications/");
populate_desktop_and_directory_lists(s.buf, 0);
}
list_for_each_entry(dir, &xdg_data_dirs, list) {
sbuf_cpy(&s, dir->buf);
sbuf_addstr(&s, "/desktop-directories/");
populate_desktop_and_directory_lists(s.buf, 1);
}
}
void xdgapps_filter_desktop_files_on_category(const char *category)
{
struct desktop_file_data *a, *pos;
list_for_each_entry_safe(pos, a, &desktop_files_filtered, filtered_list)
list_del(&pos->filtered_list);
list_for_each_entry(a, &desktop_files_all, full_list) {
if (!a->name || !a->categories)
continue;
if (strstr(a->categories, category))
list_add_tail(&a->filtered_list,
&desktop_files_filtered);
}
}