| 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 | |
| 26 | struct globals_row; |
| 27 | |
| 28 | /** @brief Handler for the presentation form of a global preference */ |
| 29 | struct global_handler { |
| 30 | /** @brief Initialize */ |
| 31 | void (*init)(struct globals_row *row); |
| 32 | |
| 33 | /** @brief Convert presentation form to string */ |
| 34 | const char *(*get)(struct globals_row *row); |
| 35 | |
| 36 | /** @brief Convert string to presentation form */ |
| 37 | void (*set)(struct globals_row *row, const char *value); |
| 38 | }; |
| 39 | |
| 40 | /** @brief Definition of a global preference */ |
| 41 | struct globals_row { |
| 42 | const char *label; |
| 43 | const char *pref; |
| 44 | GtkWidget *widget; |
| 45 | const struct global_handler *handler; |
| 46 | int initialized; |
| 47 | }; |
| 48 | |
| 49 | static void globals_close(GtkButton attribute((unused)) *button, |
| 50 | gpointer attribute((unused)) userdata); |
| 51 | static void globals_row_changed(struct globals_row *row); |
| 52 | |
| 53 | /** @brief Called when the user changes the contents of a string entry */ |
| 54 | static void global_string_entry_changed(GtkEditable attribute((unused)) *editable, |
| 55 | gpointer user_data) { |
| 56 | struct globals_row *row = user_data; |
| 57 | globals_row_changed(row); |
| 58 | } |
| 59 | |
| 60 | /** @brief Initialize a global presented as a string */ |
| 61 | static void global_string_init(struct globals_row *row) { |
| 62 | row->widget = gtk_entry_new(); |
| 63 | g_signal_connect(row->widget, "changed", |
| 64 | G_CALLBACK(global_string_entry_changed), row); |
| 65 | } |
| 66 | |
| 67 | static const char *global_string_get(struct globals_row *row) { |
| 68 | return gtk_entry_get_text(GTK_ENTRY(row->widget)); |
| 69 | } |
| 70 | |
| 71 | static void global_string_set(struct globals_row *row, const char *value) { |
| 72 | /* Identify unset and empty lists */ |
| 73 | if(!value) |
| 74 | value = ""; |
| 75 | /* Skip trivial updates (we'll see one as a consequence of each |
| 76 | * update we make...) */ |
| 77 | if(strcmp(gtk_entry_get_text(GTK_ENTRY(row->widget)), value)) |
| 78 | gtk_entry_set_text(GTK_ENTRY(row->widget), value); |
| 79 | } |
| 80 | |
| 81 | /** @brief String global preference */ |
| 82 | static const struct global_handler global_string = { |
| 83 | global_string_init, |
| 84 | global_string_get, |
| 85 | global_string_set, |
| 86 | }; |
| 87 | |
| 88 | /** @brief Called when the user changes the contents of a string entry */ |
| 89 | static void global_boolean_toggled(GtkToggleButton attribute((unused)) *button, |
| 90 | gpointer user_data) { |
| 91 | struct globals_row *row = user_data; |
| 92 | globals_row_changed(row); |
| 93 | } |
| 94 | |
| 95 | static void global_boolean_init(struct globals_row *row) { |
| 96 | row->widget = gtk_check_button_new(); |
| 97 | g_signal_connect(row->widget, "toggled", |
| 98 | G_CALLBACK(global_boolean_toggled), row); |
| 99 | } |
| 100 | |
| 101 | static const char *global_boolean_get(struct globals_row *row) { |
| 102 | return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(row->widget)) ? "yes" : "no"; |
| 103 | } |
| 104 | |
| 105 | static void global_boolean_set(struct globals_row *row, const char *value) { |
| 106 | gboolean new_state = !(value && strcmp(value, "yes")); |
| 107 | if(new_state != gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(row->widget))) |
| 108 | gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(row->widget), new_state); |
| 109 | } |
| 110 | |
| 111 | /** @brief Boolean global preference */ |
| 112 | static const struct global_handler global_boolean = { |
| 113 | global_boolean_init, |
| 114 | global_boolean_get, |
| 115 | global_boolean_set, |
| 116 | }; |
| 117 | |
| 118 | /** @brief Table of global preferences */ |
| 119 | static struct globals_row globals_rows[] = { |
| 120 | { "Required tags", "required-tags", NULL, &global_string, 0 }, |
| 121 | { "Prohibited tags", "prohibited-tags", NULL, &global_string, 0 }, |
| 122 | { "Playing", "playing", NULL, &global_boolean, 0 }, |
| 123 | { "Random play", "random-play", NULL, &global_boolean, 0 }, |
| 124 | }; |
| 125 | #define NGLOBALS (sizeof globals_rows / sizeof *globals_rows) |
| 126 | |
| 127 | /** @brief Buttons for globals popup */ |
| 128 | static struct button globals_buttons[] = { |
| 129 | { |
| 130 | .stock = GTK_STOCK_CLOSE, |
| 131 | .clicked = globals_close, |
| 132 | .tip = "Close window", |
| 133 | .pack = gtk_box_pack_end, |
| 134 | }, |
| 135 | }; |
| 136 | #define NGLOBALS_BUTTONS (sizeof globals_buttons / sizeof *globals_buttons) |
| 137 | |
| 138 | static void globals_close(GtkButton attribute((unused)) *button, |
| 139 | gpointer attribute((unused)) userdata) { |
| 140 | gtk_widget_destroy(globals_window); |
| 141 | } |
| 142 | |
| 143 | /** @brief Called with the latest setting for a row */ |
| 144 | static void globals_get_completed(void *v, const char *err, |
| 145 | const char *value) { |
| 146 | if(err) |
| 147 | popup_protocol_error(0, err); |
| 148 | else if(globals_window) { |
| 149 | struct globals_row *row = v; |
| 150 | row->handler->set(row, value); |
| 151 | row->initialized = 1; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | /** @brief Retrieve the latest setting for @p row */ |
| 156 | static void globals_get(struct globals_row *row) { |
| 157 | disorder_eclient_get_global(client, globals_get_completed, row->pref, row); |
| 158 | } |
| 159 | |
| 160 | static void globals_row_changed(struct globals_row *row) { |
| 161 | if(row->initialized) { |
| 162 | const char *new_value = row->handler->get(row); |
| 163 | if(new_value) |
| 164 | disorder_eclient_set_global(client, NULL, row->pref, new_value, row); |
| 165 | else |
| 166 | disorder_eclient_unset_global(client, NULL, row->pref, row); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | /** @brief Display the globals window */ |
| 171 | void popup_globals(void) { |
| 172 | GtkWidget *label, *table, *hbox; |
| 173 | /* Pop up the window if it already exists */ |
| 174 | if(globals_window) { |
| 175 | gtk_window_present(GTK_WINDOW(globals_window)); |
| 176 | return; |
| 177 | } |
| 178 | /* Create the window */ |
| 179 | /* TODO loads of this is very similar to (copied from!) users.c - can we |
| 180 | * de-dupe? */ |
| 181 | globals_window = gtk_window_new(GTK_WINDOW_TOPLEVEL); |
| 182 | gtk_widget_set_style(globals_window, tool_style); |
| 183 | gtk_window_set_title(GTK_WINDOW(globals_window), "Globals"); |
| 184 | g_signal_connect(globals_window, "destroy", |
| 185 | G_CALLBACK(gtk_widget_destroyed), &globals_window); |
| 186 | table = gtk_table_new(NGLOBALS + 1/*rows*/, 2/*cols*/, FALSE/*homogeneous*/); |
| 187 | gtk_widget_set_style(table, tool_style);\ |
| 188 | |
| 189 | for(size_t n = 0; n < NGLOBALS; ++n) { |
| 190 | globals_rows[n].initialized = 0; |
| 191 | label = gtk_label_new(globals_rows[n].label); |
| 192 | gtk_widget_set_style(label, tool_style); |
| 193 | gtk_misc_set_alignment(GTK_MISC(label), 1/*right*/, 0/*bottom*/); |
| 194 | gtk_table_attach(GTK_TABLE(table), label, |
| 195 | 0, 1, /* left/right_attach */ |
| 196 | n, n+1, /* top/bottom_attach */ |
| 197 | GTK_FILL, 0, /* x/yoptions */ |
| 198 | 1, 1); /* x/ypadding */ |
| 199 | globals_rows[n].handler->init(&globals_rows[n]); |
| 200 | gtk_widget_set_style(globals_rows[n].widget, tool_style); |
| 201 | gtk_table_attach(GTK_TABLE(table), globals_rows[n].widget, |
| 202 | 1, 2, /* left/right_attach */ |
| 203 | n, n+1, /* top/bottom_attach */ |
| 204 | GTK_FILL, 0, /* x/yoptions */ |
| 205 | 1, 1); /* x/ypadding */ |
| 206 | globals_get(&globals_rows[n]); |
| 207 | } |
| 208 | hbox = create_buttons_box(globals_buttons, |
| 209 | NGLOBALS_BUTTONS, |
| 210 | gtk_hbox_new(FALSE, 1)); |
| 211 | gtk_table_attach_defaults(GTK_TABLE(table), hbox, |
| 212 | 0, 2, /* left/right_attach */ |
| 213 | NGLOBALS, NGLOBALS+1); /* top/bottom_attach */ |
| 214 | |
| 215 | gtk_container_add(GTK_CONTAINER(globals_window), frame_widget(table, NULL)); |
| 216 | gtk_widget_show_all(globals_window); |
| 217 | } |
| 218 | |
| 219 | /** @brief Called when any global pref changes */ |
| 220 | static void globals_pref_changed(const char attribute((unused)) *event, |
| 221 | void *eventdata, |
| 222 | void attribute((unused)) *callbackdata) { |
| 223 | const char *pref = eventdata; |
| 224 | if(!globals_window) |
| 225 | return; /* not paying attention */ |
| 226 | for(size_t n = 0; n < NGLOBALS; ++n) { |
| 227 | if(!strcmp(pref, globals_rows[n].pref)) |
| 228 | globals_get(&globals_rows[n]); |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | /** @brief Initialize globals infrastructure */ |
| 233 | void globals_init() { |
| 234 | event_register("global-pref", globals_pref_changed, NULL); |
| 235 | } |