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
;
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
);
33 /** @brief Convert presentation form to string */
34 const char *(*get
)(struct globals_row
*row
);
36 /** @brief Convert string to presentation form */
37 void (*set
)(struct globals_row
*row
, const char *value
);
40 /** @brief Definition of a global preference */
45 const struct global_handler
*handler
;
49 static void globals_close(GtkButton
attribute((unused
)) *button
,
50 gpointer
attribute((unused
)) userdata
);
51 static void globals_row_changed(struct globals_row
*row
);
53 /** @brief Called when the user changes the contents of a string entry */
54 static void global_string_entry_changed(GtkEditable
attribute((unused
)) *editable
,
56 struct globals_row
*row
= user_data
;
57 globals_row_changed(row
);
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
);
67 static const char *global_string_get(struct globals_row
*row
) {
68 return gtk_entry_get_text(GTK_ENTRY(row
->widget
));
71 static void global_string_set(struct globals_row
*row
, const char *value
) {
72 /* Identify unset and empty lists */
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
);
81 /** @brief String global preference */
82 static const struct global_handler global_string
= {
88 /** @brief Called when the user changes the contents of a string entry */
89 static void global_boolean_toggled(GtkToggleButton
attribute((unused
)) *button
,
91 struct globals_row
*row
= user_data
;
92 globals_row_changed(row
);
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
);
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";
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
);
111 /** @brief Boolean global preference */
112 static const struct global_handler global_boolean
= {
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 },
125 #define NGLOBALS (sizeof globals_rows / sizeof *globals_rows)
127 /** @brief Buttons for globals popup */
128 static struct button globals_buttons
[] = {
130 .stock
= GTK_STOCK_CLOSE
,
131 .clicked
= globals_close
,
132 .tip
= "Close window",
133 .pack
= gtk_box_pack_end
,
136 #define NGLOBALS_BUTTONS (sizeof globals_buttons / sizeof *globals_buttons)
138 static void globals_close(GtkButton
attribute((unused
)) *button
,
139 gpointer
attribute((unused
)) userdata
) {
140 gtk_widget_destroy(globals_window
);
143 /** @brief Called with the latest setting for a row */
144 static void globals_get_completed(void *v
, const char *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;
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
);
160 static void globals_row_changed(struct globals_row
*row
) {
161 if(row
->initialized
) {
162 const char *new_value
= row
->handler
->get(row
);
164 disorder_eclient_set_global(client
, NULL
, row
->pref
, new_value
, row
);
166 disorder_eclient_unset_global(client
, NULL
, row
->pref
, row
);
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 */
175 gtk_window_present(GTK_WINDOW(globals_window
));
178 /* Create the window */
179 /* TODO loads of this is very similar to (copied from!) users.c - can we
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
);\
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
]);
208 hbox
= create_buttons_box(globals_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 */
215 gtk_container_add(GTK_CONTAINER(globals_window
), frame_widget(table
, NULL
));
216 gtk_widget_show_all(globals_window
);
219 /** @brief Called when any global pref changes */
220 static void globals_pref_changed(const char attribute((unused
)) *event
,
222 void attribute((unused
)) *callbackdata
) {
223 const char *pref
= eventdata
;
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
]);
232 /** @brief Initialize globals infrastructure */
233 void globals_init() {
234 event_register("global-pref", globals_pref_changed
, NULL
);