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/globals.c
19 * @brief Track global preferences
22 #include "disobedience.h"
24 static GtkWidget *globals_window;
25 static void globals_close(GtkButton attribute((unused)) *button,
26 gpointer attribute((unused)) userdata);
28 static struct globals_row {
33 { "Required tags", "required-tags", NULL },
34 { "Prohibited tags", "prohibited-tags", NULL },
35 { "Plating", "playing", NULL },
36 { "Random play", "random-play", NULL },
38 #define NGLOBALS (sizeof globals_rows / sizeof *globals_rows)
40 /** @brief Buttons for globals popup */
41 static struct button globals_buttons[] = {
43 .stock = GTK_STOCK_CLOSE,
44 .clicked = globals_close,
45 .tip = "Close window",
46 .pack = gtk_box_pack_end,
49 #define NGLOBALS_BUTTONS (sizeof globals_buttons / sizeof *globals_buttons)
51 static void globals_close(GtkButton attribute((unused)) *button,
52 gpointer attribute((unused)) userdata) {
53 gtk_widget_destroy(globals_window);
56 /** @brief Called with the latest setting for a row */
57 static void globals_get_completed(void *v, const char *err,
60 popup_protocol_error(0, err);
61 else if(globals_window) {
62 struct globals_row *row = v;
63 /* Identify unset and empty lists */
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);
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);
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));
83 disorder_eclient_set_global(client, NULL, row->pref, new_value, row);
85 disorder_eclient_unset_global(client, NULL, row->pref, row);
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 */
93 gtk_window_present(GTK_WINDOW(globals_window));
96 /* Create the window */
97 /* TODO loads of this is very similar to (copied from!) users.c - can we
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);\
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]);
127 hbox = create_buttons_box(globals_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 */
134 gtk_container_add(GTK_CONTAINER(globals_window), frame_widget(table, NULL));
135 gtk_widget_show_all(globals_window);
138 /** @brief Called when any global pref changes */
139 static void globals_pref_changed(const char *event,
141 void *callbackdata) {
142 const char *pref = eventdata;
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]);
151 /** @brief Initialize globals infrastructure */
152 void globals_init() {
153 event_register("global-pref", globals_pref_changed, NULL);