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