Commit | Line | Data |
---|---|---|
7791f6cf RK |
1 | /* |
2 | * This file is part of DisOrder. | |
3 | * Copyright (C) 2011 Richard Kettlewell | |
4 | * | |
5 | * This program is free software: you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation, either version 3 of the License, or | |
8 | * (at your option) any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
17 | */ | |
18 | /** @file disobedience/filter.c | |
19 | * @brief Track filtering | |
20 | */ | |
21 | ||
22 | #include "disobedience.h" | |
23 | ||
24 | static GtkWidget *filtering_window; | |
4e6b1953 RK |
25 | static void filter_close(GtkButton attribute((unused)) *button, |
26 | gpointer attribute((unused)) userdata); | |
7791f6cf RK |
27 | |
28 | static struct filter_row { | |
29 | const char *label; | |
30 | const char *pref; | |
31 | GtkWidget *entry; | |
32 | } filter_rows[] = { | |
33 | { "Required tags", "required-tags", NULL }, | |
34 | { "Prohibited tags", "prohibited-tags", NULL }, | |
35 | }; | |
36 | #define NFILTER (sizeof filter_rows / sizeof *filter_rows) | |
37 | ||
4e6b1953 RK |
38 | /** @brief Buttons for filtering popup */ |
39 | static struct button filter_buttons[] = { | |
40 | { | |
41 | .stock = GTK_STOCK_CLOSE, | |
42 | .clicked = filter_close, | |
43 | .tip = "Close window", | |
44 | .pack = gtk_box_pack_end, | |
45 | }, | |
46 | }; | |
47 | #define NFILTER_BUTTONS (sizeof filter_buttons / sizeof *filter_buttons) | |
48 | ||
49 | static void filter_close(GtkButton attribute((unused)) *button, | |
50 | gpointer attribute((unused)) userdata) { | |
51 | gtk_widget_destroy(filtering_window); | |
52 | } | |
7791f6cf RK |
53 | |
54 | /** @brief Called with the latest setting for a row */ | |
55 | static void filter_get_completed(void *v, const char *err, | |
56 | const char *value) { | |
57 | if(err) | |
58 | popup_protocol_error(0, err); | |
59 | else if(filtering_window) { | |
60 | struct filter_row *row = v; | |
61 | /* Identify unset and empty lists */ | |
62 | if(!value) | |
63 | value = ""; | |
64 | /* Skip trivial updates (we'll see one as a consequence of each | |
65 | * update we make...) */ | |
66 | if(strcmp(gtk_entry_get_text(GTK_ENTRY(row->entry)), value)) | |
67 | gtk_entry_set_text(GTK_ENTRY(row->entry), value); | |
68 | } | |
69 | } | |
70 | ||
71 | /** @brief Retrieve the latest setting for @p row */ | |
72 | static void filter_get(struct filter_row *row) { | |
73 | disorder_eclient_get_global(client, filter_get_completed, row->pref, row); | |
74 | } | |
75 | ||
76 | /** @brief Called when the user changes the contents of some entry */ | |
77 | static void filter_entry_changed(GtkEditable *editable, gpointer user_data) { | |
78 | struct filter_row *row = user_data; | |
79 | const char *new_value = gtk_entry_get_text(GTK_ENTRY(editable)); | |
80 | if(*new_value) | |
81 | disorder_eclient_set_global(client, NULL, row->pref, new_value, row); | |
82 | else | |
83 | disorder_eclient_unset_global(client, NULL, row->pref, row); | |
84 | } | |
85 | ||
86 | /** @brief Display the filtering window */ | |
87 | void popup_filtering(void) { | |
4e6b1953 | 88 | GtkWidget *label, *table, *hbox; |
7791f6cf RK |
89 | /* Pop up the window if it already exists */ |
90 | if(filtering_window) { | |
91 | gtk_window_present(GTK_WINDOW(filtering_window)); | |
92 | return; | |
93 | } | |
94 | /* Create the window */ | |
95 | /* TODO loads of this is very similar to (copied from!) users.c - can we | |
96 | * de-dupe? */ | |
97 | filtering_window = gtk_window_new(GTK_WINDOW_TOPLEVEL); | |
98 | gtk_widget_set_style(filtering_window, tool_style); | |
99 | gtk_window_set_title(GTK_WINDOW(filtering_window), "Filtering"); | |
100 | g_signal_connect(filtering_window, "destroy", | |
101 | G_CALLBACK(gtk_widget_destroyed), &filtering_window); | |
4e6b1953 | 102 | table = gtk_table_new(NFILTER + 1/*rows*/, 2/*cols*/, FALSE/*homogeneous*/); |
7791f6cf RK |
103 | gtk_widget_set_style(table, tool_style);\ |
104 | ||
105 | for(size_t n = 0; n < NFILTER; ++n) { | |
106 | label = gtk_label_new(filter_rows[n].label); | |
107 | gtk_widget_set_style(label, tool_style); | |
108 | gtk_misc_set_alignment(GTK_MISC(label), 1/*right*/, 0/*bottom*/); | |
109 | gtk_table_attach(GTK_TABLE(table), label, | |
110 | 0, 1, /* left/right_attach */ | |
111 | n, n+1, /* top/bottom_attach */ | |
112 | GTK_FILL, 0, /* x/yoptions */ | |
113 | 1, 1); /* x/ypadding */ | |
114 | filter_rows[n].entry = gtk_entry_new(); | |
115 | gtk_widget_set_style(filter_rows[n].entry, tool_style); | |
116 | gtk_table_attach(GTK_TABLE(table), filter_rows[n].entry, | |
117 | 1, 2, /* left/right_attach */ | |
118 | n, n+1, /* top/bottom_attach */ | |
119 | GTK_FILL, 0, /* x/yoptions */ | |
120 | 1, 1); /* x/ypadding */ | |
121 | g_signal_connect(filter_rows[n].entry, "changed", | |
122 | G_CALLBACK(filter_entry_changed), &filter_rows[n]); | |
123 | filter_get(&filter_rows[n]); | |
124 | } | |
4e6b1953 RK |
125 | hbox = create_buttons_box(filter_buttons, |
126 | NFILTER_BUTTONS, | |
127 | gtk_hbox_new(FALSE, 1)); | |
128 | gtk_table_attach_defaults(GTK_TABLE(table), hbox, | |
129 | 0, 2, /* left/right_attach */ | |
130 | NFILTER, NFILTER+1); /* top/bottom_attach */ | |
7791f6cf RK |
131 | |
132 | gtk_container_add(GTK_CONTAINER(filtering_window), frame_widget(table, NULL)); | |
133 | gtk_widget_show_all(filtering_window); | |
134 | } | |
135 | ||
136 | /** @brief Called when any global pref changes */ | |
137 | static void filtering_global_pref_changed(const char *event, | |
138 | void *eventdata, | |
139 | void *callbackdata) { | |
140 | const char *pref = eventdata; | |
141 | if(!filtering_window) | |
142 | return; /* not paying attention */ | |
143 | for(size_t n = 0; n < NFILTER; ++n) { | |
144 | if(!strcmp(pref, filter_rows[n].pref)) | |
145 | filter_get(&filter_rows[n]); | |
146 | } | |
147 | } | |
148 | ||
149 | /** @brief Initialize filtering infrastructure */ | |
722a71bf RK |
150 | void filtering_init() { |
151 | event_register("global-pref", filtering_global_pref_changed, NULL); | |
7791f6cf | 152 | } |