2 * This file is part of DisOrder.
3 * Copyright (C) 2011 Richard Kettlewell
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.
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.
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/>.
18 /** @file disobedience/filter.c
19 * @brief Track filtering
22 #include "disobedience.h"
24 static GtkWidget *filtering_window;
25 static void filter_close(GtkButton attribute((unused)) *button,
26 gpointer attribute((unused)) userdata);
28 static struct filter_row {
33 { "Required tags", "required-tags", NULL },
34 { "Prohibited tags", "prohibited-tags", NULL },
36 #define NFILTER (sizeof filter_rows / sizeof *filter_rows)
38 /** @brief Buttons for filtering popup */
39 static struct button filter_buttons[] = {
41 .stock = GTK_STOCK_CLOSE,
42 .clicked = filter_close,
43 .tip = "Close window",
44 .pack = gtk_box_pack_end,
47 #define NFILTER_BUTTONS (sizeof filter_buttons / sizeof *filter_buttons)
49 static void filter_close(GtkButton attribute((unused)) *button,
50 gpointer attribute((unused)) userdata) {
51 gtk_widget_destroy(filtering_window);
54 /** @brief Called with the latest setting for a row */
55 static void filter_get_completed(void *v, const char *err,
58 popup_protocol_error(0, err);
59 else if(filtering_window) {
60 struct filter_row *row = v;
61 /* Identify unset and empty lists */
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);
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);
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));
81 disorder_eclient_set_global(client, NULL, row->pref, new_value, row);
83 disorder_eclient_unset_global(client, NULL, row->pref, row);
86 /** @brief Display the filtering window */
87 void popup_filtering(void) {
88 GtkWidget *label, *table, *hbox;
89 /* Pop up the window if it already exists */
90 if(filtering_window) {
91 gtk_window_present(GTK_WINDOW(filtering_window));
94 /* Create the window */
95 /* TODO loads of this is very similar to (copied from!) users.c - can we
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);
102 table = gtk_table_new(NFILTER + 1/*rows*/, 2/*cols*/, FALSE/*homogeneous*/);
103 gtk_widget_set_style(table, tool_style);\
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]);
125 hbox = create_buttons_box(filter_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 */
132 gtk_container_add(GTK_CONTAINER(filtering_window), frame_widget(table, NULL));
133 gtk_widget_show_all(filtering_window);
136 /** @brief Called when any global pref changes */
137 static void filtering_global_pref_changed(const char *event,
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]);
149 /** @brief Initialize filtering infrastructure */
150 void filtering_init() {
151 event_register("global-pref", filtering_global_pref_changed, NULL);