1 /*
2 * Evolution internal utilities - Glade dialog widget utilities
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
11 * for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program; if not, see <http://www.gnu.org/licenses/>.
15 *
16 *
17 * Authors:
18 * Federico Mena-Quintero <federico@ximian.com>
19 *
20 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
21 *
22 */
23
24 #include "evolution-config.h"
25
26 #include <math.h>
27 #include <string.h>
28 #include <time.h>
29 #include <gtk/gtk.h>
30 #include <glib/gi18n-lib.h>
31
32 #include <libedataserver/libedataserver.h>
33
34 #include "e-dialog-widgets.h"
35
36 /* Converts a mapped value to the appropriate index in an item group. The
37 * values for the items are provided as a -1-terminated array.
38 */
39 static gint
value_to_index(const gint * value_map,gint value)40 value_to_index (const gint *value_map,
41 gint value)
42 {
43 gint i;
44
45 for (i = 0; value_map[i] != -1; i++)
46 if (value_map[i] == value)
47 return i;
48
49 return -1;
50 }
51
52 /* Converts an index in an item group to the appropriate mapped value. See the
53 * function above.
54 */
55 static gint
index_to_value(const gint * value_map,gint index)56 index_to_value (const gint *value_map,
57 gint index)
58 {
59 gint i;
60
61 /* We do this the hard way, i.e. not as a simple array reference, to
62 * check for correctness.
63 */
64
65 for (i = 0; value_map[i] != -1; i++)
66 if (i == index)
67 return value_map[i];
68
69 return -1;
70 }
71
72 /**
73 * e_dialog_combo_box_set:
74 * @widget: A #GtkComboBox.
75 * @value: Enumerated value.
76 * @value_map: Map from enumeration values to array indices.
77 *
78 * Sets the selected item in a #GtkComboBox. Please read the description of
79 * e_dialog_radio_set() to see how @value_map maps enumeration values to item
80 * indices.
81 **/
82 void
e_dialog_combo_box_set(GtkWidget * widget,gint value,const gint * value_map)83 e_dialog_combo_box_set (GtkWidget *widget,
84 gint value,
85 const gint *value_map)
86 {
87 gint i;
88
89 g_return_if_fail (GTK_IS_COMBO_BOX (widget));
90 g_return_if_fail (value_map != NULL);
91
92 i = value_to_index (value_map, value);
93
94 if (i != -1)
95 gtk_combo_box_set_active (GTK_COMBO_BOX (widget), i);
96 else
97 g_message (
98 "e_dialog_combo_box_set(): could not "
99 "find value %d in value map!", value);
100 }
101
102 /**
103 * e_dialog_combo_box_get:
104 * @widget: A #GtkComboBox.
105 * @value_map: Map from enumeration values to array indices.
106 *
107 * Queries the selected item in a #GtkComboBox. Please read the description
108 * of e_dialog_radio_set() to see how @value_map maps enumeration values to item
109 * indices.
110 *
111 * Return value: Enumeration value which corresponds to the selected item in the
112 * combo box.
113 **/
114 gint
e_dialog_combo_box_get(GtkWidget * widget,const gint * value_map)115 e_dialog_combo_box_get (GtkWidget *widget,
116 const gint *value_map)
117 {
118 gint active;
119 gint i;
120
121 g_return_val_if_fail (GTK_IS_COMBO_BOX (widget), -1);
122 g_return_val_if_fail (value_map != NULL, -1);
123
124 active = gtk_combo_box_get_active (GTK_COMBO_BOX (widget));
125 i = index_to_value (value_map, active);
126
127 if (i == -1) {
128 g_message (
129 "e_dialog_combo_box_get(): could not "
130 "find index %d in value map!", i);
131 return -1;
132 }
133
134 return i;
135 }
136
137 /**
138 * e_dialog_button_new_with_icon:
139 * @icon_name: Icon's name to use; can be %NULL
140 * @label: Button label to set, with mnemonics
141 *
142 * Creates a new #GtkButton with preset @label and image set
143 * to @icon_name.
144 *
145 * Returns: (transfer-full): A new #GtkButton
146 *
147 * Since: 3.12
148 **/
149 GtkWidget *
e_dialog_button_new_with_icon(const gchar * icon_name,const gchar * label)150 e_dialog_button_new_with_icon (const gchar *icon_name,
151 const gchar *label)
152 {
153 GtkIconSize icon_size = GTK_ICON_SIZE_BUTTON;
154 GtkWidget *button;
155
156 if (label && *label) {
157 button = gtk_button_new_with_mnemonic (label);
158 } else {
159 button = gtk_button_new ();
160 icon_size = GTK_ICON_SIZE_MENU;
161 }
162
163 if (icon_name)
164 gtk_button_set_image (
165 GTK_BUTTON (button),
166 gtk_image_new_from_icon_name (icon_name, icon_size));
167
168 gtk_widget_show (button);
169
170 return button;
171 }
172
173 static GtkWidget *
dialog_widgets_construct_time_units_combo(void)174 dialog_widgets_construct_time_units_combo (void)
175 {
176 struct _units {
177 const gchar *nick;
178 const gchar *caption;
179 } units[4] = {
180 /* Translators: This is part of: "Do not synchronize locally mails older than [ xxx ] [ days ]" */
181 { "days", NC_("time-unit", "days") },
182 /* Translators: This is part of: "Do not synchronize locally mails older than [ xxx ] [ weeks ]" */
183 { "weeks", NC_("time-unit", "weeks") },
184 /* Translators: This is part of: "Do not synchronize locally mails older than [ xxx ] [ months ]" */
185 { "months", NC_("time-unit", "months") },
186 /* Translators: This is part of: "Do not synchronize locally mails older than [ xxx ] [ years ]" */
187 { "years", NC_("time-unit", "years") }
188 };
189 gint ii;
190 GtkCellRenderer *renderer;
191 GtkListStore *store;
192 GtkWidget *combo;
193
194 /* 0: 'nick', 1: caption */
195 store = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_STRING);
196
197 for (ii = 0; ii < G_N_ELEMENTS (units); ii++) {
198 GtkTreeIter iter;
199 const gchar *caption;
200
201 /* Localize the caption. */
202 caption = g_dpgettext2 (GETTEXT_PACKAGE, "time-unit", units[ii].caption);
203
204 gtk_list_store_append (store, &iter);
205 gtk_list_store_set (store, &iter, 0, units[ii].nick, 1, caption, -1);
206 }
207
208 combo = gtk_combo_box_new_with_model (GTK_TREE_MODEL (store));
209 gtk_combo_box_set_id_column (GTK_COMBO_BOX (combo), 0);
210
211 renderer = gtk_cell_renderer_text_new ();
212 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), renderer, TRUE);
213 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), renderer, "text", 1, NULL);
214
215 g_object_unref (store);
216
217 return combo;
218 }
219
220 /**
221 * e_dialog_offline_settings_new_limit_box:
222 * @offline_settings: a #CamelOfflineSettings
223 *
224 * Creates a new horizontal #GtkBox, which contains widgets
225 * to configure @offline_settings properties limit-by-age,
226 * limit-unit and limit-value.
227 *
228 * Returns: (transfer full): a new #GtkBox
229 *
230 * Since: 3.24
231 **/
232 GtkWidget *
e_dialog_offline_settings_new_limit_box(CamelOfflineSettings * offline_settings)233 e_dialog_offline_settings_new_limit_box (CamelOfflineSettings *offline_settings)
234 {
235 GtkAdjustment *adjustment;
236 GtkWidget *hbox, *spin, *combo;
237 GtkWidget *prefix;
238
239 g_return_val_if_fail (CAMEL_IS_OFFLINE_SETTINGS (offline_settings), NULL);
240
241 hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 3);
242 gtk_widget_show (hbox);
243
244 /* Translators: This is part of: "Do not synchronize locally mails older than [ xxx ] [ days ]" */
245 prefix = gtk_check_button_new_with_mnemonic (_("Do not synchronize locally mails older than"));
246 gtk_box_pack_start (GTK_BOX (hbox), prefix, FALSE, TRUE, 0);
247 gtk_widget_show (prefix);
248
249 e_binding_bind_property (
250 offline_settings, "limit-by-age",
251 prefix, "active",
252 G_BINDING_BIDIRECTIONAL |
253 G_BINDING_SYNC_CREATE);
254
255 adjustment = gtk_adjustment_new (1.0, 1.0, 999.0, 1.0, 1.0, 0.0);
256
257 spin = gtk_spin_button_new (adjustment, 1.0, 0);
258 gtk_box_pack_start (GTK_BOX (hbox), spin, FALSE, TRUE, 0);
259 gtk_widget_show (spin);
260
261 e_binding_bind_property (
262 offline_settings, "limit-value",
263 spin, "value",
264 G_BINDING_BIDIRECTIONAL |
265 G_BINDING_SYNC_CREATE);
266
267 e_binding_bind_property (
268 prefix, "active",
269 spin, "sensitive",
270 G_BINDING_SYNC_CREATE);
271
272 combo = dialog_widgets_construct_time_units_combo ();
273 gtk_box_pack_start (GTK_BOX (hbox), combo, FALSE, FALSE, 0);
274 gtk_widget_show (combo);
275
276 e_binding_bind_property_full (
277 offline_settings, "limit-unit",
278 combo, "active-id",
279 G_BINDING_BIDIRECTIONAL |
280 G_BINDING_SYNC_CREATE,
281 e_binding_transform_enum_value_to_nick,
282 e_binding_transform_enum_nick_to_value,
283 NULL, NULL);
284
285 e_binding_bind_property (
286 prefix, "active",
287 combo, "sensitive",
288 G_BINDING_SYNC_CREATE);
289
290 return hbox;
291 }
292
293 static CamelThreeState
edw_three_state_source_to_camel(EThreeState value)294 edw_three_state_source_to_camel (EThreeState value)
295 {
296 switch (value) {
297 case E_THREE_STATE_OFF:
298 return CAMEL_THREE_STATE_OFF;
299 case E_THREE_STATE_ON:
300 return CAMEL_THREE_STATE_ON;
301 case E_THREE_STATE_INCONSISTENT:
302 return CAMEL_THREE_STATE_INCONSISTENT;
303 }
304
305 return CAMEL_THREE_STATE_INCONSISTENT;
306 }
307
308 static EThreeState
edw_three_state_camel_to_source(CamelThreeState value)309 edw_three_state_camel_to_source (CamelThreeState value)
310 {
311 switch (value) {
312 case CAMEL_THREE_STATE_OFF:
313 return E_THREE_STATE_OFF;
314 case CAMEL_THREE_STATE_ON:
315 return E_THREE_STATE_ON;
316 case CAMEL_THREE_STATE_INCONSISTENT:
317 return E_THREE_STATE_INCONSISTENT;
318 }
319
320 return E_THREE_STATE_INCONSISTENT;
321 }
322
323 static gboolean
edw_three_state_to_sensitive_cb(GBinding * binding,const GValue * from_value,GValue * to_value,gpointer user_data)324 edw_three_state_to_sensitive_cb (GBinding *binding,
325 const GValue *from_value,
326 GValue *to_value,
327 gpointer user_data)
328 {
329 CamelThreeState value;
330
331 if (CAMEL_IS_FOLDER (g_binding_get_source (binding))) {
332 value = g_value_get_enum (from_value);
333 } else {
334 value = edw_three_state_source_to_camel (g_value_get_enum (from_value));
335 }
336
337 g_value_set_boolean (to_value, value == CAMEL_THREE_STATE_ON);
338
339 return TRUE;
340 }
341
342 static gboolean
edw_mark_seen_timeout_to_double_cb(GBinding * binding,const GValue * from_value,GValue * to_value,gpointer user_data)343 edw_mark_seen_timeout_to_double_cb (GBinding *binding,
344 const GValue *from_value,
345 GValue *to_value,
346 gpointer user_data)
347 {
348 gint value;
349
350 value = g_value_get_int (from_value);
351 g_value_set_double (to_value, ((gdouble) value) / 1000.0);
352
353 return TRUE;
354 }
355
356 static gboolean
edw_double_to_mark_seen_timeout_cb(GBinding * binding,const GValue * from_value,GValue * to_value,gpointer user_data)357 edw_double_to_mark_seen_timeout_cb (GBinding *binding,
358 const GValue *from_value,
359 GValue *to_value,
360 gpointer user_data)
361 {
362 gdouble value;
363
364 value = g_value_get_double (from_value);
365 g_value_set_int (to_value, value * 1000);
366
367 return TRUE;
368 }
369
370 typedef struct _ThreeStateData {
371 GObject *object;
372 gulong handler_id;
373 } ThreeStateData;
374
375 static void
three_state_data_free(gpointer data,GClosure * closure)376 three_state_data_free (gpointer data,
377 GClosure *closure)
378 {
379 ThreeStateData *tsd = data;
380
381 if (tsd) {
382 g_clear_object (&tsd->object);
383 g_free (tsd);
384 }
385 }
386
387 static void
edw_three_state_toggled_cb(GtkToggleButton * widget,gpointer user_data)388 edw_three_state_toggled_cb (GtkToggleButton *widget,
389 gpointer user_data)
390 {
391 ThreeStateData *tsd = user_data;
392 CamelThreeState set_to;
393
394 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (widget));
395 g_return_if_fail (tsd != NULL);
396
397 g_signal_handler_block (widget, tsd->handler_id);
398
399 if (gtk_toggle_button_get_inconsistent (widget) &&
400 gtk_toggle_button_get_active (widget)) {
401 gtk_toggle_button_set_active (widget, FALSE);
402 gtk_toggle_button_set_inconsistent (widget, FALSE);
403 set_to = CAMEL_THREE_STATE_OFF;
404 } else if (!gtk_toggle_button_get_active (widget)) {
405 gtk_toggle_button_set_inconsistent (widget, TRUE);
406 gtk_toggle_button_set_active (widget, FALSE);
407 set_to = CAMEL_THREE_STATE_INCONSISTENT;
408 } else {
409 set_to = CAMEL_THREE_STATE_ON;
410 }
411
412 if (CAMEL_IS_FOLDER (tsd->object)) {
413 g_object_set (G_OBJECT (tsd->object), "mark-seen", set_to, NULL);
414 } else {
415 g_object_set (G_OBJECT (tsd->object),
416 "mark-seen", edw_three_state_camel_to_source (set_to),
417 NULL);
418 }
419
420 g_signal_handler_unblock (widget, tsd->handler_id);
421 }
422
423 /**
424 * e_dialog_new_mark_seen_box:
425 * @object: either #CamelFolder or #ESourceMailAccount
426 *
427 * Returns: (transfer full): a new #GtkBox containing widgets to
428 * setup "[x] Mark messages as read after [spin button] seconds"
429 * option using @object properties "mark-seen" and "mark-seen-timeout".
430 *
431 * Since: 3.32
432 **/
433 GtkWidget *
e_dialog_new_mark_seen_box(gpointer object)434 e_dialog_new_mark_seen_box (gpointer object)
435 {
436 /* Translators: The %s is replaced with a spin button; always keep it in the string at the right position */
437 const gchar *blurb = _("Mark messages as read after %s seconds");
438 GtkWidget *hbox, *widget;
439 CamelThreeState three_state = CAMEL_THREE_STATE_INCONSISTENT;
440 ThreeStateData *tsd;
441 gboolean set_inconsistent = FALSE, set_active = FALSE;
442 gchar **strv;
443
444 g_return_val_if_fail (CAMEL_IS_FOLDER (object) || E_IS_SOURCE_MAIL_ACCOUNT (object), NULL);
445
446 if (CAMEL_IS_FOLDER (object)) {
447 three_state = camel_folder_get_mark_seen (object);
448 } else {
449 three_state = edw_three_state_source_to_camel (e_source_mail_account_get_mark_seen (object));
450 }
451
452 switch (three_state) {
453 case CAMEL_THREE_STATE_ON:
454 set_inconsistent = FALSE;
455 set_active = TRUE;
456 break;
457 case CAMEL_THREE_STATE_OFF:
458 set_inconsistent = FALSE;
459 set_active = FALSE;
460 break;
461 case CAMEL_THREE_STATE_INCONSISTENT:
462 set_inconsistent = TRUE;
463 set_active = FALSE;
464 break;
465 }
466
467 hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 2);
468 gtk_widget_show (hbox);
469
470 strv = g_strsplit (blurb, "%s", -1);
471 g_warn_if_fail (strv && strv[0] && strv[1] && !strv[2]);
472
473 widget = gtk_check_button_new_with_mnemonic (strv && strv[0] ? strv[0] : "Mark messages as read after ");
474
475 g_object_set (G_OBJECT (widget),
476 "inconsistent", set_inconsistent,
477 "active", set_active,
478 NULL);
479
480 tsd = g_new0 (ThreeStateData, 1);
481 tsd->object = g_object_ref (object);
482 tsd->handler_id = g_signal_connect_data (widget, "toggled",
483 G_CALLBACK (edw_three_state_toggled_cb),
484 tsd, three_state_data_free, 0);
485
486 gtk_widget_show (widget);
487
488 gtk_box_pack_start (GTK_BOX (hbox), widget, FALSE, FALSE, 0);
489
490 widget = gtk_spin_button_new_with_range (0.0, 10.0, 1.0);
491 gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (widget), TRUE);
492 gtk_spin_button_set_digits (GTK_SPIN_BUTTON (widget), 1);
493 e_binding_bind_property_full (object, "mark-seen",
494 widget, "sensitive",
495 G_BINDING_DEFAULT | G_BINDING_SYNC_CREATE,
496 edw_three_state_to_sensitive_cb,
497 NULL, NULL, NULL);
498 e_binding_bind_property_full (object, "mark-seen-timeout",
499 widget, "value",
500 G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE,
501 edw_mark_seen_timeout_to_double_cb,
502 edw_double_to_mark_seen_timeout_cb,
503 NULL, NULL);
504 gtk_widget_show (widget);
505
506 gtk_box_pack_start (GTK_BOX (hbox), widget, FALSE, FALSE, 0);
507
508 widget = gtk_label_new (strv && strv[0] && strv[1] ? strv[1] : " seconds");
509 gtk_widget_show (widget);
510
511 gtk_box_pack_start (GTK_BOX (hbox), widget, FALSE, FALSE, 0);
512
513 g_strfreev (strv);
514
515 return hbox;
516 }
517