Commit | Line | Data |
---|---|---|
7791f6cf RK |
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 | */ | |
e5cce183 RK |
18 | /** @file disobedience/globals.c |
19 | * @brief Track global preferences | |
7791f6cf RK |
20 | */ |
21 | ||
22 | #include "disobedience.h" | |
23 | ||
e5cce183 RK |
24 | static GtkWidget *globals_window; |
25 | static void globals_close(GtkButton attribute((unused)) *button, | |
4e6b1953 | 26 | gpointer attribute((unused)) userdata); |
7791f6cf | 27 | |
e5cce183 | 28 | static struct globals_row { |
7791f6cf RK |
29 | const char *label; |
30 | const char *pref; | |
31 | GtkWidget *entry; | |
e5cce183 | 32 | } globals_rows[] = { |
7791f6cf RK |
33 | { "Required tags", "required-tags", NULL }, |
34 | { "Prohibited tags", "prohibited-tags", NULL }, | |
e5cce183 RK |
35 | { "Plating", "playing", NULL }, |
36 | { "Random play", "random-play", NULL }, | |
7791f6cf | 37 | }; |
e5cce183 | 38 | #define NGLOBALS (sizeof globals_rows / sizeof *globals_rows) |
7791f6cf | 39 | |
e5cce183 RK |
40 | /** @brief Buttons for globals popup */ |
41 | static struct button globals_buttons[] = { | |
4e6b1953 RK |
42 | { |
43 | .stock = GTK_STOCK_CLOSE, | |
e5cce183 | 44 | .clicked = globals_close, |
4e6b1953 RK |
45 | .tip = "Close window", |
46 | .pack = gtk_box_pack_end, | |
47 | }, | |
48 | }; | |
e5cce183 | 49 | #define NGLOBALS_BUTTONS (sizeof globals_buttons / sizeof *globals_buttons) |
4e6b1953 | 50 | |
e5cce183 | 51 | static void globals_close(GtkButton attribute((unused)) *button, |
4e6b1953 | 52 | gpointer attribute((unused)) userdata) { |
e5cce183 | 53 | gtk_widget_destroy(globals_window); |
4e6b1953 | 54 | } |
7791f6cf RK |
55 | |
56 | /** @brief Called with the latest setting for a row */ | |
e5cce183 | 57 | static void globals_get_completed(void *v, const char *err, |
7791f6cf RK |
58 | const char *value) { |
59 | if(err) | |
60 | popup_protocol_error(0, err); | |
e5cce183 RK |
61 | else if(globals_window) { |
62 | struct globals_row *row = v; | |
7791f6cf RK |
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 */ | |
e5cce183 RK |
74 | static void globals_get(struct globals_row *row) { |
75 | disorder_eclient_get_global(client, globals_get_completed, row->pref, row); | |
7791f6cf RK |
76 | } |
77 | ||
78 | /** @brief Called when the user changes the contents of some entry */ | |
e5cce183 RK |
79 | static void globals_entry_changed(GtkEditable *editable, gpointer user_data) { |
80 | struct globals_row *row = user_data; | |
7791f6cf RK |
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 | ||
e5cce183 RK |
88 | /** @brief Display the globals window */ |
89 | void popup_globals(void) { | |
4e6b1953 | 90 | GtkWidget *label, *table, *hbox; |
7791f6cf | 91 | /* Pop up the window if it already exists */ |
e5cce183 RK |
92 | if(globals_window) { |
93 | gtk_window_present(GTK_WINDOW(globals_window)); | |
7791f6cf RK |
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? */ | |
e5cce183 RK |
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*/); | |
7791f6cf RK |
105 | gtk_widget_set_style(table, tool_style);\ |
106 | ||
e5cce183 RK |
107 | for(size_t n = 0; n < NGLOBALS; ++n) { |
108 | label = gtk_label_new(globals_rows[n].label); | |
7791f6cf RK |
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 */ | |
e5cce183 RK |
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, | |
7791f6cf RK |
119 | 1, 2, /* left/right_attach */ |
120 | n, n+1, /* top/bottom_attach */ | |
121 | GTK_FILL, 0, /* x/yoptions */ | |
122 | 1, 1); /* x/ypadding */ | |
e5cce183 RK |
123 | g_signal_connect(globals_rows[n].entry, "changed", |
124 | G_CALLBACK(globals_entry_changed), &globals_rows[n]); | |
125 | globals_get(&globals_rows[n]); | |
7791f6cf | 126 | } |
e5cce183 RK |
127 | hbox = create_buttons_box(globals_buttons, |
128 | NGLOBALS_BUTTONS, | |
4e6b1953 RK |
129 | gtk_hbox_new(FALSE, 1)); |
130 | gtk_table_attach_defaults(GTK_TABLE(table), hbox, | |
131 | 0, 2, /* left/right_attach */ | |
e5cce183 | 132 | NGLOBALS, NGLOBALS+1); /* top/bottom_attach */ |
7791f6cf | 133 | |
e5cce183 RK |
134 | gtk_container_add(GTK_CONTAINER(globals_window), frame_widget(table, NULL)); |
135 | gtk_widget_show_all(globals_window); | |
7791f6cf RK |
136 | } |
137 | ||
138 | /** @brief Called when any global pref changes */ | |
e5cce183 RK |
139 | static void globals_pref_changed(const char *event, |
140 | void *eventdata, | |
141 | void *callbackdata) { | |
7791f6cf | 142 | const char *pref = eventdata; |
e5cce183 | 143 | if(!globals_window) |
7791f6cf | 144 | return; /* not paying attention */ |
e5cce183 RK |
145 | for(size_t n = 0; n < NGLOBALS; ++n) { |
146 | if(!strcmp(pref, globals_rows[n].pref)) | |
147 | globals_get(&globals_rows[n]); | |
7791f6cf RK |
148 | } |
149 | } | |
150 | ||
e5cce183 RK |
151 | /** @brief Initialize globals infrastructure */ |
152 | void globals_init() { | |
153 | event_register("global-pref", globals_pref_changed, NULL); | |
7791f6cf | 154 | } |