Skip to content

Commit 9af2b3d

Browse files
committed
Add option -i|--ignore <file>
...to specify a file listing .desktop files to exclude from the output.
1 parent f6a0c95 commit 9af2b3d

File tree

6 files changed

+76
-3
lines changed

6 files changed

+76
-3
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ PROG = labwc-menu-generator
1919

2020
all: $(PROG)
2121

22-
$(PROG): main.o desktop.o
22+
$(PROG): main.o desktop.o ignore.o
2323
$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)
2424

2525
clean:

desktop.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <dirent.h>
1515
#include <stdbool.h>
1616
#include "desktop.h"
17+
#include "ignore.h"
1718

1819
static GList *apps;
1920

@@ -223,6 +224,10 @@ add_app(FILE *fp, char *filename)
223224
char line[4096], *p;
224225
int is_desktop_entry;
225226

227+
if (should_ignore(filename)) {
228+
return NULL;
229+
}
230+
226231
struct app *app = calloc(1, sizeof(struct app));
227232
is_desktop_entry = 0;
228233
while (fgets(line, sizeof(line), fp)) {

ignore.c

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
/* Copyright (C) Johan Malm 2024 */
3+
#define _POSIX_C_SOURCE 200809L
4+
#include <glib.h>
5+
#include <stdio.h>
6+
#include <string.h>
7+
#include "ignore.h"
8+
9+
static GList *ignore_files;
10+
11+
void
12+
ignore_init(const char *filename)
13+
{
14+
if (!filename || !*filename) {
15+
return;
16+
}
17+
FILE *stream = fopen(filename, "r");
18+
if (!stream) {
19+
return;
20+
}
21+
char *line = NULL;
22+
size_t len = 0;
23+
while (getline(&line, &len, stream) != -1) {
24+
char *p = strrchr(line, '\n');
25+
if (p) {
26+
*p = '\0';
27+
}
28+
ignore_files = g_list_append(ignore_files,
29+
g_strdup(g_strstrip(line)));
30+
}
31+
free(line);
32+
fclose(stream);
33+
}
34+
35+
void
36+
ignore_finish(void)
37+
{
38+
g_list_free_full(ignore_files, g_free);
39+
}
40+
41+
bool
42+
should_ignore(const char *filename)
43+
{
44+
GList *iter;
45+
for (iter = ignore_files; iter; iter = iter->next) {
46+
if (!g_strcmp0((char *)iter->data, filename)) {
47+
return true;
48+
}
49+
}
50+
return false;
51+
}

ignore.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/* SPDX-License-Identifier: GPL-2.0-only */
2+
#ifndef IGNORE_H
3+
#define IGNORE_H
4+
#include <stdbool.h>
5+
6+
void ignore_init(const char *filename);
7+
void ignore_finish(void);
8+
bool should_ignore(const char *filename);
9+
10+
#endif /* IGNORE_H */

main.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <string.h>
1414
#include <stdbool.h>
1515
#include "desktop.h"
16+
#include "ignore.h"
1617
#include "schema.h"
1718

1819
static bool no_duplicates;
@@ -23,6 +24,7 @@ static bool pipemenu;
2324
static const struct option long_options[] = {
2425
{"bare", no_argument, NULL, 'b'},
2526
{"help", no_argument, NULL, 'h'},
27+
{"ignore", required_argument, NULL, 'i'},
2628
{"no-duplicates", no_argument, NULL, 'n'},
2729
{"pipemenu", no_argument, NULL, 'p'},
2830
{0, 0, 0, 0}
@@ -32,6 +34,7 @@ static const char labwc_menu_generator_usage[] =
3234
"Usage: labwc-menu-generator [options...]\n"
3335
" -b, --bare Show no header or footer\n"
3436
" -h, --help Show help message and quit\n"
37+
" -i, --ignore <file> Specify file listing .desktop files to ignore\n"
3538
" -n, --no-duplicates Limit desktop entries to one directory only\n"
3639
" -p, --pipemenu Output in pipemenu format\n";
3740

@@ -284,7 +287,7 @@ main(int argc, char **argv)
284287
int c;
285288
while (1) {
286289
int index = 0;
287-
c = getopt_long(argc, argv, "bhnp", long_options, &index);
290+
c = getopt_long(argc, argv, "bhi:np", long_options, &index);
288291
if (c == -1) {
289292
break;
290293
}
@@ -293,6 +296,9 @@ main(int argc, char **argv)
293296
no_footer = true;
294297
no_header = true;
295298
break;
299+
case 'i':
300+
ignore_init(optarg);
301+
break;
296302
case 'n':
297303
no_duplicates = true;
298304
break;
@@ -315,6 +321,7 @@ main(int argc, char **argv)
315321

316322
desktop_entries_destroy(apps);
317323
directory_entries_destroy(dirs);
324+
ignore_finish();
318325

319326
return 0;
320327
}

meson.build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ glib = dependency('glib-2.0')
1414

1515
executable(
1616
meson.project_name(),
17-
sources: files('main.c', 'desktop.c'),
17+
sources: files('main.c', 'desktop.c', 'ignore.c'),
1818
dependencies: [glib],
1919
install: true,
2020
)

0 commit comments

Comments
 (0)