1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 /*
3  * brasero
4  * Copyright (C) Philippe Rouquier 2007-2008 <bonfire-app@wanadoo.fr>
5  *
6  *  Brasero is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  * brasero is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with brasero.  If not, write to:
18  * 	The Free Software Foundation, Inc.,
19  * 	51 Franklin Street, Fifth Floor
20  * 	Boston, MA  02110-1301, USA.
21  */
22 
23 #ifdef HAVE_CONFIG_H
24 #  include <config.h>
25 #endif
26 
27 #include <glib.h>
28 #include <glib-object.h>
29 #include <glib/gi18n-lib.h>
30 #include <glib/gstdio.h>
31 
32 #include <gtk/gtk.h>
33 
34 #include "brasero-plugin.h"
35 #include "brasero-plugin-information.h"
36 #include "brasero-plugin-option.h"
37 
38 enum {
39 	STRING_COL,
40 	VALUE_COL,
41 	COL_NUM
42 };
43 
44 typedef struct _BraseroPluginOptionPrivate BraseroPluginOptionPrivate;
45 struct _BraseroPluginOptionPrivate
46 {
47 	GtkWidget *title;
48 	GtkWidget *vbox;
49 
50 	GSettings *settings;
51 };
52 
53 #define BRASERO_PLUGIN_OPTION_PRIVATE(o)  (G_TYPE_INSTANCE_GET_PRIVATE ((o), BRASERO_TYPE_PLUGIN_OPTION, BraseroPluginOptionPrivate))
54 
55 G_DEFINE_TYPE (BraseroPluginOption, brasero_plugin_option, GTK_TYPE_DIALOG);
56 #define BRASERO_SCHEMA_CONFIG		"org.gnome.brasero.config"
57 
58 static GtkWidget *
brasero_plugin_option_add_conf_widget(BraseroPluginOption * self,BraseroPluginConfOption * option,GtkBox * container)59 brasero_plugin_option_add_conf_widget (BraseroPluginOption *self,
60 				       BraseroPluginConfOption *option,
61 				       GtkBox *container)
62 {
63 	BraseroPluginOptionPrivate *priv;
64 	BraseroPluginConfOptionType type;
65 	GtkCellRenderer *renderer;
66 	GtkListStore *model;
67 	gchar *description;
68 	GSList *suboptions;
69 	GtkWidget *widget;
70 	GtkWidget *label;
71 	GtkTreeIter iter;
72 	GtkWidget *hbox;
73 	GtkWidget *box;
74 	gchar *key;
75 
76 	priv = BRASERO_PLUGIN_OPTION_PRIVATE (self);
77 
78 	brasero_plugin_conf_option_get_info (option,
79 					     &key,
80 					     &description,
81 					     &type);
82 
83 	hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
84 	gtk_widget_show (hbox);
85 	gtk_box_pack_start (container, hbox, FALSE, FALSE, 0);
86 
87 	label = gtk_label_new ("\t");
88 	gtk_widget_show (label);
89 	gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
90 
91 	box = NULL;
92 	switch (type) {
93 	case BRASERO_PLUGIN_OPTION_BOOL:
94 		widget = gtk_check_button_new_with_label (description);
95 		g_settings_bind (priv->settings, key,
96 			         widget, "active",
97 			         G_SETTINGS_BIND_DEFAULT);
98 
99 		gtk_widget_show (widget);
100 
101 		suboptions = brasero_plugin_conf_option_bool_get_suboptions (option);
102 		if (suboptions) {
103 			box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
104 			gtk_widget_show (box);
105 
106 			gtk_box_pack_start (GTK_BOX (box),
107 					    widget,
108 					    FALSE,
109 					    FALSE,
110 					    0);
111 			gtk_box_pack_start (GTK_BOX (hbox),
112 					    box,
113 					    FALSE,
114 					    FALSE,
115 					    0);
116 
117 			for (; suboptions; suboptions = suboptions->next) {
118 				GtkWidget *child;
119 				BraseroPluginConfOption *suboption;
120 
121 				suboption = suboptions->data;
122 
123 				/* first create the slaves then set state */
124 				child = brasero_plugin_option_add_conf_widget (self, suboption, GTK_BOX (box));
125 				g_settings_bind (priv->settings, key,
126 				                 child, "sensitive",
127 				                 G_SETTINGS_BIND_DEFAULT);
128 			}
129 		}
130 		else
131 			gtk_box_pack_start (GTK_BOX (hbox),
132 					    widget,
133 					    FALSE,
134 					    FALSE,
135 					    0);
136 		break;
137 
138 	case BRASERO_PLUGIN_OPTION_INT:
139 		box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
140 
141 		label = gtk_label_new (description);
142 		gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
143 		gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
144 
145 		widget = gtk_spin_button_new_with_range (brasero_plugin_conf_option_int_get_min (option),
146 		                                         brasero_plugin_conf_option_int_get_max (option),
147 		                                         1.0);
148 		gtk_box_pack_start (GTK_BOX (box), widget, FALSE, FALSE, 0);
149 
150 		gtk_widget_show_all (box);
151 		gtk_box_pack_start (GTK_BOX (hbox),
152 				    box,
153 				    FALSE,
154 				    FALSE,
155 				    0);
156 
157 		g_settings_bind (priv->settings, key,
158 			         widget, "value",
159 			         G_SETTINGS_BIND_DEFAULT);
160 		break;
161 
162 	case BRASERO_PLUGIN_OPTION_STRING:
163 		box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
164 
165 		label = gtk_label_new (description);
166 		gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
167 		gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
168 
169 		widget = gtk_entry_new ();
170 		gtk_box_pack_start (GTK_BOX (box), widget, FALSE, FALSE, 0);
171 
172 		gtk_widget_show_all (box);
173 		gtk_box_pack_start (GTK_BOX (hbox),
174 				    box,
175 				    FALSE,
176 				    FALSE,
177 				    0);
178 
179 		g_settings_bind (priv->settings, key,
180 			         widget, "text",
181 			         G_SETTINGS_BIND_DEFAULT);
182 		break;
183 
184 	case BRASERO_PLUGIN_OPTION_CHOICE:
185 		box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
186 
187 		label = gtk_label_new (description);
188 		gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
189 		gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
190 
191 		model = gtk_list_store_new (COL_NUM,
192 					    G_TYPE_STRING,
193 					    G_TYPE_INT);
194 		widget = gtk_combo_box_new_with_model (GTK_TREE_MODEL (model));
195 		g_object_unref (model);
196 		gtk_widget_show (widget);
197 		gtk_box_pack_start (GTK_BOX (box), widget, FALSE, TRUE, 0);
198 
199 		renderer = gtk_cell_renderer_text_new ();
200 		gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (widget), renderer, TRUE);
201 		gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (widget), renderer,
202 						"text", STRING_COL,
203 						NULL);
204 
205 		suboptions = brasero_plugin_conf_option_choice_get (option);
206 		for (; suboptions; suboptions = suboptions->next) {
207 			BraseroPluginChoicePair *pair;
208 
209 			pair = suboptions->data;
210 			gtk_list_store_append (GTK_LIST_STORE (model), &iter);
211 			gtk_list_store_set (GTK_LIST_STORE (model), &iter,
212 					    STRING_COL, pair->string,
213 					    VALUE_COL, pair->value,
214 					    -1);
215 		}
216 
217 		g_settings_bind (priv->settings, key,
218 			         widget, "active",
219 			         G_SETTINGS_BIND_DEFAULT);
220 
221 		if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (widget), &iter)) {
222 			if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (model), &iter))
223 				gtk_combo_box_set_active_iter (GTK_COMBO_BOX (widget), &iter);
224 		}
225 
226 		gtk_widget_show_all (box);
227 		gtk_box_pack_start (GTK_BOX (hbox),
228 				    box,
229 				    FALSE,
230 				    FALSE,
231 				    0);
232 		break;
233 
234 
235 	default:
236 		widget = NULL;
237 		break;
238 	}
239 
240 	g_free (key);
241 	g_free (description);
242 
243 	return widget;
244 }
245 
246 void
brasero_plugin_option_set_plugin(BraseroPluginOption * self,BraseroPlugin * plugin)247 brasero_plugin_option_set_plugin (BraseroPluginOption *self,
248 				  BraseroPlugin *plugin)
249 {
250 	BraseroPluginOptionPrivate *priv;
251 	BraseroPluginConfOption *option;
252 	gchar *string;
253 	gchar *tmp;
254 
255 	priv = BRASERO_PLUGIN_OPTION_PRIVATE (self);
256 
257 	/* Use the translated name for the plugin. */
258 	tmp = g_strdup_printf (_("Options for plugin %s"), _(brasero_plugin_get_display_name (plugin)));
259 	string = g_strdup_printf ("<b>%s</b>", tmp);
260 	g_free (tmp);
261 
262 	gtk_label_set_markup (GTK_LABEL (priv->title), string);
263 	g_free (string);
264 
265 	option = brasero_plugin_get_next_conf_option (plugin, NULL);
266 	for (; option; option = brasero_plugin_get_next_conf_option (plugin, option))
267 		brasero_plugin_option_add_conf_widget (self,
268 						       option,
269 						       GTK_BOX (priv->vbox));
270 }
271 
272 static void
brasero_plugin_option_init(BraseroPluginOption * object)273 brasero_plugin_option_init (BraseroPluginOption *object)
274 {
275 	BraseroPluginOptionPrivate *priv;
276 	GtkWidget *frame;
277 
278 	priv = BRASERO_PLUGIN_OPTION_PRIVATE (object);
279 
280 	frame = gtk_frame_new ("");
281 	gtk_container_set_border_width (GTK_CONTAINER (frame), 8);
282 	gtk_widget_show (frame);
283 	gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_NONE);
284 
285 	priv->title = gtk_frame_get_label_widget (GTK_FRAME (frame));
286 	gtk_label_set_use_markup (GTK_LABEL (priv->title), TRUE);
287 
288 	priv->vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
289 	gtk_widget_show (priv->vbox);
290 	gtk_container_set_border_width (GTK_CONTAINER (priv->vbox), 8);
291 	gtk_container_add (GTK_CONTAINER (frame), priv->vbox);
292 
293 	gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (object))),
294 			    frame,
295 			    FALSE,
296 			    FALSE,
297 			    0);
298 
299 	gtk_dialog_add_button (GTK_DIALOG (object),
300 			       GTK_STOCK_CLOSE, GTK_RESPONSE_OK);
301 
302 	priv->settings = g_settings_new (BRASERO_SCHEMA_CONFIG);
303 }
304 
305 static void
brasero_plugin_option_finalize(GObject * object)306 brasero_plugin_option_finalize (GObject *object)
307 {
308 	BraseroPluginOptionPrivate *priv;
309 
310 	priv = BRASERO_PLUGIN_OPTION_PRIVATE (object);
311 
312 	if (priv->settings) {
313 		g_object_unref (priv->settings);
314 		priv->settings = NULL;
315 	}
316 
317 	G_OBJECT_CLASS (brasero_plugin_option_parent_class)->finalize (object);
318 }
319 
320 static void
brasero_plugin_option_class_init(BraseroPluginOptionClass * klass)321 brasero_plugin_option_class_init (BraseroPluginOptionClass *klass)
322 {
323 	GObjectClass* object_class = G_OBJECT_CLASS (klass);
324 
325 	g_type_class_add_private (klass, sizeof (BraseroPluginOptionPrivate));
326 	object_class->finalize = brasero_plugin_option_finalize;
327 }
328 
329 GtkWidget *
brasero_plugin_option_new(void)330 brasero_plugin_option_new (void)
331 {
332 	return g_object_new (BRASERO_TYPE_PLUGIN_OPTION, NULL);
333 }
334