Disobedience: change filtering window to be a more general-purpose
[disorder] / disobedience / globals.c
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/globals.c
19 * @brief Track global preferences
20 */
21
22 #include "disobedience.h"
23
24 static GtkWidget *globals_window;
25 static void globals_close(GtkButton attribute((unused)) *button,
26 gpointer attribute((unused)) userdata);
27
28 static struct globals_row {
29 const char *label;
30 const char *pref;
31 GtkWidget *entry;
32 } globals_rows[] = {
33 { "Required tags", "required-tags", NULL },
34 { "Prohibited tags", "prohibited-tags", NULL },
35 { "Plating", "playing", NULL },
36 { "Random play", "random-play", NULL },
37 };
38 #define NGLOBALS (sizeof globals_rows / sizeof *globals_rows)
39
40 /** @brief Buttons for globals popup */
41 static struct button globals_buttons[] = {
42 {
43 .stock = GTK_STOCK_CLOSE,
44 .clicked = globals_close,
45 .tip = "Close window",
46 .pack = gtk_box_pack_end,
47 },
48 };
49 #define NGLOBALS_BUTTONS (sizeof globals_buttons / sizeof *globals_buttons)
50
51 static void globals_close(GtkButton attribute((unused)) *button,
52 gpointer attribute((unused)) userdata) {
53 gtk_widget_destroy(globals_window);
54 }
55
56 /** @brief Called with the latest setting for a row */
57 static void globals_get_completed(void *v, const char *err,
58 const char *value) {
59 if(err)
60 popup_protocol_error(0, err);
61 else if(globals_window) {
62 struct globals_row *row = v;
63 /* Identify unset and empty lists */
64 if(!value)
65 value = "";
66 /* Skip trivial updates (we'll see one as a consequence of each
67 * update we make...) */
68 if(strcmp(gtk_entry_get_text(GTK_ENTRY(row->entry)), value))
69 gtk_entry_set_text(GTK_ENTRY(row->entry), value);
70 }
71 }
72
73 /** @brief Retrieve the latest setting for @p row */
74 static void globals_get(struct globals_row *row) {
75 disorder_eclient_get_global(client, globals_get_completed, row->pref, row);
76 }
77
78 /** @brief Called when the user changes the contents of some entry */
79 static void globals_entry_changed(GtkEditable *editable, gpointer user_data) {
80 struct globals_row *row = user_data;
81 const char *new_value = gtk_entry_get_text(GTK_ENTRY(editable));
82 if(*new_value)
83 disorder_eclient_set_global(client, NULL, row->pref, new_value, row);
84 else
85 disorder_eclient_unset_global(client, NULL, row->pref, row);
86 }
87
88 /** @brief Display the globals window */
89 void popup_globals(void) {
90 GtkWidget *label, *table, *hbox;
91 /* Pop up the window if it already exists */
92 if(globals_window) {
93 gtk_window_present(GTK_WINDOW(globals_window));
94 return;
95 }
96 /* Create the window */
97 /* TODO loads of this is very similar to (copied from!) users.c - can we
98 * de-dupe? */
99 globals_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
100 gtk_widget_set_style(globals_window, tool_style);
101 gtk_window_set_title(GTK_WINDOW(globals_window), "Globals");
102 g_signal_connect(globals_window, "destroy",
103 G_CALLBACK(gtk_widget_destroyed), &globals_window);
104 table = gtk_table_new(NGLOBALS + 1/*rows*/, 2/*cols*/, FALSE/*homogeneous*/);
105 gtk_widget_set_style(table, tool_style);\
106
107 for(size_t n = 0; n < NGLOBALS; ++n) {
108 label = gtk_label_new(globals_rows[n].label);
109 gtk_widget_set_style(label, tool_style);
110 gtk_misc_set_alignment(GTK_MISC(label), 1/*right*/, 0/*bottom*/);
111 gtk_table_attach(GTK_TABLE(table), label,
112 0, 1, /* left/right_attach */
113 n, n+1, /* top/bottom_attach */
114 GTK_FILL, 0, /* x/yoptions */
115 1, 1); /* x/ypadding */
116 globals_rows[n].entry = gtk_entry_new();
117 gtk_widget_set_style(globals_rows[n].entry, tool_style);
118 gtk_table_attach(GTK_TABLE(table), globals_rows[n].entry,
119 1, 2, /* left/right_attach */
120 n, n+1, /* top/bottom_attach */
121 GTK_FILL, 0, /* x/yoptions */
122 1, 1); /* x/ypadding */
123 g_signal_connect(globals_rows[n].entry, "changed",
124 G_CALLBACK(globals_entry_changed), &globals_rows[n]);
125 globals_get(&globals_rows[n]);
126 }
127 hbox = create_buttons_box(globals_buttons,
128 NGLOBALS_BUTTONS,
129 gtk_hbox_new(FALSE, 1));
130 gtk_table_attach_defaults(GTK_TABLE(table), hbox,
131 0, 2, /* left/right_attach */
132 NGLOBALS, NGLOBALS+1); /* top/bottom_attach */
133
134 gtk_container_add(GTK_CONTAINER(globals_window), frame_widget(table, NULL));
135 gtk_widget_show_all(globals_window);
136 }
137
138 /** @brief Called when any global pref changes */
139 static void globals_pref_changed(const char *event,
140 void *eventdata,
141 void *callbackdata) {
142 const char *pref = eventdata;
143 if(!globals_window)
144 return; /* not paying attention */
145 for(size_t n = 0; n < NGLOBALS; ++n) {
146 if(!strcmp(pref, globals_rows[n].pref))
147 globals_get(&globals_rows[n]);
148 }
149 }
150
151 /** @brief Initialize globals infrastructure */
152 void globals_init() {
153 event_register("global-pref", globals_pref_changed, NULL);
154 }