Debian Codegraph @

src/modules/frontend/gtk/choice_model.c

raw
1 /*****************************************************************************
2 *
3 * cdebconf - An implementation of the Debian Configuration Management
4 * System
5 *
6 * cdebconf is (c) 2000-2007 Randolph Chung and others under the following
7 * license.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 *****************************************************************************/
33
34 /** @file choice_model.c
35 * wrapper around GtkTreeModel for choice based question handlers
36 * @internal
37 */
38
39 #include "choice_model.h"
40
41 #include <string.h>
42 #include <gtk/gtk.h>
43
44 #include "frontend.h"
45 #include "question.h"
46 #include "strutl.h"
47
48 /** Internal data structure used by cdebconf_gtk_choice_model_find_value().
49 */
50 struct search_result {
51 /** value searched in the model */
52 const char * searched_value;
53
54 /** path to the value when found, NULL otherwise */
55 GtkTreePath * path;
56 };
57
58 /** GtkTreeModelForeachFunc used by cdebconf_gtk_choice_model_find_value().
59 *
60 * @param model searched model
61 * @param path path of the current value
62 * @param iter iter pointing to the current value
63 * @param result search result structure
64 * @return TRUE when the data has been found, FALSE otherwise
65 */
66 static gboolean is_searched_value(GtkTreeModel * model, GtkTreePath * path,
67 GtkTreeIter * iter,
68 struct search_result * result)
69 {
70 char * value;
71
72 gtk_tree_model_get(model, iter,
73 /* column: */ CHOICE_MODEL_VALUE, &value,
74 -1 /* end of list */);
75 if (0 == strcmp(value, result->searched_value)) {
76 result->path = gtk_tree_path_copy(path);
77 }
78 g_free(value);
79 return NULL != result->path;
80 }
81
82 /** Search for a given value in a model
83 *
84 * The allocated GtkTreePath must be freed by the caller.
85 * @param model model to be searched
86 * @param searched_value value to be found
87 * @return the path to the value when found, NULL otherwise
88 */
89 GtkTreePath * cdebconf_gtk_choice_model_find_value(GtkTreeModel * model,
90 const char * searched_value)
91 {
92 struct search_result result;
93
94 result.searched_value = searched_value;
95 result.path = NULL;
96
97 gtk_tree_model_foreach(model, (GtkTreeModelForeachFunc) is_searched_value,
98 &result);
99 return result.path;
100 }
101
102 /** Full interface to create a tree model from a given question.
103 *
104 * @param fe cdebconf frontend
105 * @param question select or multiselect question
106 * @param is_parent helper determining if a choice is the parent of the next
107 * choices
108 * @return the newly created model or NULL in case of errors
109 * @see parent_predicate
110 */
111 GtkTreeModel * cdebconf_gtk_choice_model_create_full(
112 struct frontend * fe, struct question * question,
113 parent_predicate is_parent)
114 {
115 GtkTreeStore * store;
116 int count;
117 int sorted_count;
118 char * raw_indices;
119 char * raw_choices;
120 char * raw_translated_choices;
121 int * sorted_indices;
122 char ** choice_values;
123 char ** choice_translated_values;
124 char ** default_values;
125 int default_count;
126 int i;
127 int sorted_index;
128 GtkTreeIter iter;
129 GtkTreeIter child;
130 GtkTreePath * path;
131
132 store = gtk_tree_store_new(CHOICE_MODEL_COLUMN_COUNT,
133 G_TYPE_BOOLEAN /* selected */,
134 G_TYPE_INT /* index */,
135 G_TYPE_STRING /* value */,
136 G_TYPE_STRING /* translated value */);
137 if (NULL == store) {
138 g_warning("gtk_tree_store_new failed.");
139 return NULL;
140 };
141
142 raw_indices = q_get_indices(fe, question);
143 raw_choices = q_get_choices_vals(fe, question);
144 raw_translated_choices = q_get_choices(fe, question);
145 count = strgetargc(raw_choices);
146 g_assert(0 < count);
147 sorted_indices = g_malloc0(count * sizeof (int));
148 choice_values = g_malloc0(count * sizeof (char *));
149 choice_translated_values = g_malloc0(count * sizeof (char *));
150 default_values = g_malloc0(count * sizeof (char *));
151
152 /* XXX: strchoicesplitsort interface is really nasty... :(
153 * Maybe it would be better to re-implement something with the nifty
154 * glib functions... */
155 sorted_count = strchoicesplitsort(raw_choices, raw_translated_choices,
156 raw_indices, choice_values,
157 choice_translated_values,
158 sorted_indices, count);
159 if (sorted_count != count) {
160 store = NULL;
161 goto exit;
162 }
163
164 default_count = strchoicesplit(question_getvalue(question, ""),
165 default_values, count);
166 g_assert(0 <= default_count);
167
168 /* Populate model */
169 for (i = 0; i < count; i++) {
170 sorted_index = sorted_indices[i];
171 g_assert(0 <= sorted_index && sorted_index < count);
172 if (NULL == is_parent ||
173 is_parent(sorted_indices[i], choice_values[sorted_index],
174 choice_translated_values[i])) {
175 gtk_tree_store_append(store, &iter, NULL /* no parent */);
176 gtk_tree_store_set(
177 store, &iter,
178 /* column: */ CHOICE_MODEL_SELECTED, FALSE,
179 /* column: */ CHOICE_MODEL_INDEX, sorted_indices[i],
180 /* column: */ CHOICE_MODEL_VALUE, choice_values[sorted_index],
181 /* column: */ CHOICE_MODEL_TRANSLATED_VALUE,
182 choice_translated_values[i],
183 -1 /* end of list */);
184 } else {
185 gtk_tree_store_append(store, &child, &iter);
186 gtk_tree_store_set(
187 store, &child,
188 /* column: */ CHOICE_MODEL_SELECTED, FALSE,
189 /* column: */ CHOICE_MODEL_INDEX, sorted_indices[i],
190 /* column: */ CHOICE_MODEL_VALUE, choice_values[sorted_index],
191 /* column: */ CHOICE_MODEL_TRANSLATED_VALUE,
192 choice_translated_values[i],
193 -1 /* end of list */);
194 }
195 }
196
197 /* Mark defaults as selected */
198 for (i = 0; i < default_count; i++) {
199 if (NULL != (path = cdebconf_gtk_choice_model_find_value(
200 GTK_TREE_MODEL(store), default_values[i]))) {
201 if (gtk_tree_model_get_iter(GTK_TREE_MODEL(store), &iter, path)) {
202 gtk_tree_store_set(store, &iter,
203 /* column: */ CHOICE_MODEL_SELECTED, TRUE,
204 -1 /* end of list */);
205 }
206 gtk_tree_path_free(path);
207 }
208 }
209
210 exit:
211 g_free(default_values);
212 g_free(sorted_indices);
213 g_free(choice_values);
214 g_free(choice_translated_values);
215 g_free(raw_translated_choices);
216 g_free(raw_choices);
217 g_free(raw_indices);
218 return GTK_TREE_MODEL(store);
219 }
220
221 /** Create a one level model for a given question.
222 *
223 * @param fe cdebconf frontend
224 * @param question a select or multiselect question
225 * @return a newly created model or NULL in case of errors
226 */
227 GtkTreeModel * cdebconf_gtk_choice_model_create(struct frontend * fe,
228 struct question * question)
229 {
230 return cdebconf_gtk_choice_model_create_full(
231 fe, question, NULL /* no parent predicate */);
232 }
233
234 /** GtkTreeModelForeachFunc used by cdebconf_gtk_choice_model_get_length().
235 *
236 * @param model traversed model
237 * @param path path of the current value
238 * @param iter iter pointing to the current value
239 * @param length current known size of the model
240 * @return FALSE in any case. :)
241 */
242 static gboolean increment_model_length(GtkTreeModel * model,
243 GtkTreePath * path,
244 GtkTreeIter * iter, guint * length)
245 {
246 *length = *length + 1;
247 return FALSE; /* foreach should go on */
248 }
249
250 /** Get the total number of choices in the given model.
251 *
252 * @param model measured model
253 * @return total number of choices in the given model
254 */
255 guint cdebconf_gtk_choice_model_get_length(GtkTreeModel * model)
256 {
257 guint length = 0;
258
259 g_assert(NULL != model);
260
261 gtk_tree_model_foreach(
262 model, (GtkTreeModelForeachFunc) increment_model_length, &length);
263 return length;
264 }
265
266 /** GtkTreeModelForeachFunc used by
267 * cdebconf_gtk_choice_model_get_first_selected().
268 *
269 * @param model searched model
270 * @param path path of the current value
271 * @param iter iter pointing to the current value
272 * @param result search result structure
273 * @return TRUE when a selected choice has been found, FALSE otherwise
274 */
275 static gboolean is_selected(GtkTreeModel * model, GtkTreePath * path,
276 GtkTreeIter * iter, GtkTreePath ** result)
277 {
278 gboolean selected;
279
280 gtk_tree_model_get(model, iter,
281 /* column: */ CHOICE_MODEL_SELECTED, &selected,
282 -1 /* end of list */);
283 if (selected) {
284 *result = gtk_tree_path_copy(path);
285 return TRUE /* stop here */;
286 }
287 return FALSE;
288 }
289
290 /** Get the first selected choice in the given model.
291 *
292 * The returned path must be free'd by the caller.
293 *
294 * @param model searched model
295 * @return path to the first selected value or NULL if no value is selected
296 */
297 GtkTreePath * cdebconf_gtk_choice_model_get_first_selected(
298 GtkTreeModel * model)
299 {
300 GtkTreePath * result = NULL;
301
302 gtk_tree_model_foreach(model, (GtkTreeModelForeachFunc) is_selected,
303 &result);
304 return result;
305 }
306
307 /** Set a given row in the model.
308 *
309 * This function has the same interface than gtk_tree_store_set(),
310 * and the same effect. It is just here to abstract over the GtkTreeModel
311 * implementation used for choice models.
312 *
313 * @param model the model to modify
314 * @param iter position in the model
315 */
316 void cdebconf_gtk_choice_model_set(GtkTreeModel * model,
317 GtkTreeIter * iter, ...)
318 {
319 va_list var_args;
320
321 va_start(var_args, iter);
322 gtk_tree_store_set_valist(GTK_TREE_STORE(model), iter, var_args);
323 va_end(var_args);
324 }
325
326 /* vim: et sw=4 si
327 */