1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
|
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
* settings.c
* Copyright (C) John Stebbins 2008-2013 <stebbins@stebbins>
*
* settings.c is free software.
*
* You may redistribute it and/or modify it under the terms of the
* GNU General Public License, as published by the Free Software
* Foundation; either version 2 of the License, or (at your option)
* any later version.
*
*/
#include <fcntl.h>
#include <unistd.h>
#include <glib.h>
#include <glib/gstdio.h>
#include <string.h>
#include "ghbcompat.h"
#include "settings.h"
#include "hb-backend.h"
#include "values.h"
void dump_settings(GValue *settings);
void ghb_pref_audio_init(signal_user_data_t *ud);
GObject*
debug_get_object(GtkBuilder* b, const gchar *n)
{
g_message("name %s\n", n);
return gtk_builder_get_object(b, n);
}
GValue*
ghb_settings_new()
{
return ghb_dict_value_new();
}
void
ghb_settings_set_value(
GValue *settings,
const gchar *key,
const GValue *value)
{
if (key == NULL || value == NULL)
return;
ghb_dict_insert(settings, g_strdup(key), ghb_value_dup(value));
}
void
ghb_settings_take_value(GValue *settings, const gchar *key, GValue *value)
{
ghb_dict_insert(settings, g_strdup(key), value);
}
void
ghb_settings_set_string(
GValue *settings,
const gchar *key,
const gchar *sval)
{
GValue *value;
value = ghb_string_value_new(sval);
ghb_dict_insert(settings, g_strdup(key), value);
}
void
ghb_settings_set_double(GValue *settings, const gchar *key, gdouble dval)
{
GValue *value;
value = ghb_double_value_new(dval);
ghb_dict_insert(settings, g_strdup(key), value);
}
void
ghb_settings_set_int64(GValue *settings, const gchar *key, gint64 ival)
{
GValue *value;
value = ghb_int64_value_new(ival);
ghb_dict_insert(settings, g_strdup(key), value);
}
void
ghb_settings_set_int(GValue *settings, const gchar *key, gint ival)
{
GValue *value;
value = ghb_int64_value_new((gint64)ival);
ghb_dict_insert(settings, g_strdup(key), value);
}
void
ghb_settings_set_boolean(GValue *settings, const gchar *key, gboolean bval)
{
GValue *value;
value = ghb_boolean_value_new(bval);
ghb_dict_insert(settings, g_strdup(key), value);
}
GValue*
ghb_settings_get_value(const GValue *settings, const gchar *key)
{
GValue *value;
value = ghb_dict_lookup(settings, key);
if (value == NULL)
g_warning("returning null (%s)", key);
return value;
}
gboolean
ghb_settings_get_boolean(const GValue *settings, const gchar *key)
{
const GValue* value;
value = ghb_settings_get_value(settings, key);
if (value == NULL) return FALSE;
return ghb_value_boolean(value);
}
gint64
ghb_settings_get_int64(const GValue *settings, const gchar *key)
{
const GValue* value;
value = ghb_settings_get_value(settings, key);
if (value == NULL) return 0;
return ghb_value_int64(value);
}
gint
ghb_settings_get_int(const GValue *settings, const gchar *key)
{
const GValue* value;
value = ghb_settings_get_value(settings, key);
if (value == NULL) return 0;
return ghb_value_int(value);
}
gdouble
ghb_settings_get_double(const GValue *settings, const gchar *key)
{
const GValue* value;
value = ghb_settings_get_value(settings, key);
if (value == NULL) return 0;
return ghb_value_double(value);
}
gchar*
ghb_settings_get_string(const GValue *settings, const gchar *key)
{
const GValue* value;
value = ghb_settings_get_value(settings, key);
if (value == NULL) return g_strdup("");
return ghb_value_string(value);
}
gint
ghb_settings_combo_int(const GValue *settings, const gchar *key)
{
return ghb_lookup_combo_int(key, ghb_settings_get_value(settings, key));
}
gdouble
ghb_settings_combo_double(const GValue *settings, const gchar *key)
{
return ghb_lookup_combo_double(key, ghb_settings_get_value(settings, key));
}
const gchar*
ghb_settings_combo_option(const GValue *settings, const gchar *key)
{
return ghb_lookup_combo_option(key, ghb_settings_get_value(settings, key));
}
const gchar*
ghb_settings_combo_string(const GValue *settings, const gchar *key)
{
return ghb_lookup_combo_string(key, ghb_settings_get_value(settings, key));
}
// Map widget names to setting keys
// Widgets that map to settings have names
// of this format: s_<setting key>
const gchar*
ghb_get_setting_key(GtkWidget *widget)
{
const gchar *name;
g_debug("get_setting_key ()\n");
if (widget == NULL) return NULL;
name = gtk_buildable_get_name(GTK_BUILDABLE(widget));
if (name == NULL)
{
// Bad widget pointer? Should never happen.
g_debug("Bad widget\n");
return NULL;
}
return name;
}
GValue*
ghb_widget_value(GtkWidget *widget)
{
GValue *value = NULL;
const gchar *name;
GType type;
if (widget == NULL)
{
g_debug("NULL widget\n");
return NULL;
}
type = G_OBJECT_TYPE(widget);
name = ghb_get_setting_key(widget);
g_debug("ghb_widget_value widget (%s)\n", name);
if (type == GTK_TYPE_ENTRY)
{
const gchar *str = gtk_entry_get_text(GTK_ENTRY(widget));
value = ghb_string_value_new(str);
}
else if (type == GTK_TYPE_RADIO_BUTTON)
{
g_debug("\tradio_button");
gboolean bval;
bval = gtk_toggle_button_get_inconsistent(GTK_TOGGLE_BUTTON(widget));
if (bval)
{
value = ghb_boolean_value_new(FALSE);
}
else
{
bval = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
value = ghb_boolean_value_new(bval);
}
}
else if (type == GTK_TYPE_CHECK_BUTTON)
{
g_debug("\tcheck_button");
gboolean bval;
bval = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
value = ghb_boolean_value_new(bval);
}
else if (type == GTK_TYPE_TOGGLE_TOOL_BUTTON)
{
g_debug("\ttoggle_tool_button");
gboolean bval;
bval = gtk_toggle_tool_button_get_active(GTK_TOGGLE_TOOL_BUTTON(widget));
value = ghb_boolean_value_new(bval);
}
else if (type == GTK_TYPE_TOGGLE_BUTTON)
{
g_debug("\ttoggle_button");
gboolean bval;
bval = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
value = ghb_boolean_value_new(bval);
}
else if (type == GTK_TYPE_TOGGLE_ACTION)
{
g_debug("\ttoggle action");
gboolean bval;
bval = gtk_toggle_action_get_active(GTK_TOGGLE_ACTION(widget));
value = ghb_boolean_value_new(bval);
}
else if (type == GTK_TYPE_CHECK_MENU_ITEM)
{
g_debug("\tcheck_menu_item");
gboolean bval;
bval = gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget));
value = ghb_boolean_value_new(bval);
}
else if (type == GTK_TYPE_COMBO_BOX)
{
g_debug("\tcombo_box");
GtkTreeModel *store;
GtkTreeIter iter;
gchar *shortOpt;
store = gtk_combo_box_get_model(GTK_COMBO_BOX(widget));
if (gtk_combo_box_get_active_iter(GTK_COMBO_BOX(widget), &iter))
{
gtk_tree_model_get(store, &iter, 2, &shortOpt, -1);
value = ghb_string_value_new(shortOpt);
g_free(shortOpt);
}
else
{
value = ghb_string_value_new("");
}
}
else if (type == GTK_TYPE_COMBO_BOX_ENTRY)
{
GtkTreeModel *store;
GtkTreeIter iter;
gchar *shortOpt;
g_debug("\tcombo_box_entry");
store = gtk_combo_box_get_model(GTK_COMBO_BOX(widget));
if (gtk_combo_box_get_active_iter(GTK_COMBO_BOX(widget), &iter))
{
gtk_tree_model_get(store, &iter, 2, &shortOpt, -1);
value = ghb_string_value_new(shortOpt);
g_free(shortOpt);
}
else
{
const gchar *str;
str = gtk_combo_box_get_active_text(GTK_COMBO_BOX(widget));
if (str == NULL) str = "";
value = ghb_string_value_new(str);
}
}
else if (type == GTK_TYPE_SPIN_BUTTON)
{
gint ival;
ival = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(widget));
value = ghb_int64_value_new(ival);
}
else if (type == GTK_TYPE_HSCALE)
{
gdouble dval;
gint digits;
digits = gtk_scale_get_digits(GTK_SCALE(widget));
dval = gtk_range_get_value(GTK_RANGE(widget));
if (digits)
{
value = ghb_double_value_new(dval);
}
else
{
value = ghb_int_value_new(dval);
}
}
else if (type == GTK_TYPE_SCALE_BUTTON)
{
gdouble dval;
dval = gtk_scale_button_get_value(GTK_SCALE_BUTTON(widget));
value = ghb_double_value_new(dval);
}
else if (type == GTK_TYPE_TEXT_VIEW)
{
GtkTextBuffer *buffer;
GtkTextIter start, end;
gchar *str;
buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(widget));
gtk_text_buffer_get_bounds(buffer, &start, &end);
str = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
value = ghb_string_value_new(str);
g_free(str);
}
else if (type == GTK_TYPE_LABEL)
{
const gchar *str;
str = gtk_label_get_text (GTK_LABEL(widget));
value = ghb_string_value_new(str);
}
else if (type == GTK_TYPE_FILE_CHOOSER_BUTTON)
{
gchar *str;
str = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER(widget));
if (str == NULL)
str = gtk_file_chooser_get_current_folder(GTK_FILE_CHOOSER(widget));
value = ghb_string_value_new(str);
if (str != NULL)
g_free(str);
}
else
{
g_debug("Attempt to set unknown widget type: %s\n", name);
g_free(value);
value = NULL;
}
return value;
}
gchar*
ghb_widget_string(GtkWidget *widget)
{
GValue *value;
gchar *sval;
value = ghb_widget_value(widget);
sval = ghb_value_string(value);
ghb_value_free(value);
return sval;
}
gdouble
ghb_widget_double(GtkWidget *widget)
{
GValue *value;
gdouble dval;
value = ghb_widget_value(widget);
dval = ghb_value_double(value);
ghb_value_free(value);
return dval;
}
gint64
ghb_widget_int64(GtkWidget *widget)
{
GValue *value;
gint64 ival;
value = ghb_widget_value(widget);
ival = ghb_value_int64(value);
ghb_value_free(value);
return ival;
}
gint
ghb_widget_int(GtkWidget *widget)
{
GValue *value;
gint ival;
value = ghb_widget_value(widget);
ival = (gint)ghb_value_int64(value);
ghb_value_free(value);
return ival;
}
gint
ghb_widget_boolean(GtkWidget *widget)
{
GValue *value;
gboolean bval;
value = ghb_widget_value(widget);
bval = ghb_value_boolean(value);
ghb_value_free(value);
return bval;
}
static void check_radio_consistency(GValue *settings, GtkWidget *widget)
{
const gchar *key = NULL;
GValue *value;
if (widget == NULL) return;
if (G_OBJECT_TYPE(widget) == GTK_TYPE_RADIO_BUTTON)
{
// Find corresponding setting
key = ghb_get_setting_key(widget);
if (key == NULL) return;
value = ghb_widget_value(widget);
if (value == NULL) return;
if (ghb_value_boolean(value) == ghb_settings_get_boolean(settings, key))
{
gtk_toggle_button_set_inconsistent(GTK_TOGGLE_BUTTON(widget), FALSE);
}
}
}
void
ghb_widget_to_setting(GValue *settings, GtkWidget *widget)
{
const gchar *key = NULL;
GValue *value;
if (widget == NULL) return;
g_debug("ghb_widget_to_setting");
// Find corresponding setting
key = ghb_get_setting_key(widget);
if (key == NULL) return;
value = ghb_widget_value(widget);
if (value != NULL)
{
check_radio_consistency(settings, widget);
ghb_settings_take_value(settings, key, value);
}
else
{
g_debug("No value found for %s\n", key);
}
}
static void
update_widget(GtkWidget *widget, const GValue *value)
{
GType type;
gchar *str;
gint ival;
gdouble dval;
g_debug("update_widget");
type = G_VALUE_TYPE(value);
if (type == ghb_array_get_type() || type == ghb_dict_get_type())
return;
if (value == NULL) return;
str = ghb_value_string(value);
ival = ghb_value_int(value);
dval = ghb_value_double(value);
type = G_OBJECT_TYPE(widget);
if (type == GTK_TYPE_ENTRY)
{
g_debug("entry");
gtk_entry_set_text((GtkEntry*)widget, str);
}
else if (type == GTK_TYPE_RADIO_BUTTON)
{
g_debug("radio button");
int cur_val = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
if (cur_val && !ival)
{
gtk_toggle_button_set_inconsistent(GTK_TOGGLE_BUTTON(widget), TRUE);
}
else
{
gtk_toggle_button_set_inconsistent(GTK_TOGGLE_BUTTON(widget), FALSE);
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget), ival);
}
}
else if (type == GTK_TYPE_CHECK_BUTTON)
{
g_debug("check button");
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget), ival);
}
else if (type == GTK_TYPE_TOGGLE_TOOL_BUTTON)
{
g_debug("toggle button");
gtk_toggle_tool_button_set_active(GTK_TOGGLE_TOOL_BUTTON(widget), ival);
}
else if (type == GTK_TYPE_TOGGLE_BUTTON)
{
g_debug("toggle button");
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget), ival);
}
else if (type == GTK_TYPE_TOGGLE_ACTION)
{
g_debug("toggle action");
gtk_toggle_action_set_active(GTK_TOGGLE_ACTION(widget), ival);
}
else if (type == GTK_TYPE_CHECK_MENU_ITEM)
{
g_debug("check menu item");
gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(widget), ival);
}
else if (type == GTK_TYPE_COMBO_BOX)
{
GtkTreeModel *store;
GtkTreeIter iter;
gchar *shortOpt;
gdouble ivalue;
gboolean foundit = FALSE;
g_debug("combo (%s)", str);
store = gtk_combo_box_get_model(GTK_COMBO_BOX(widget));
if (gtk_tree_model_get_iter_first (store, &iter))
{
do
{
gtk_tree_model_get(store, &iter, 2, &shortOpt, -1);
if (strcasecmp(shortOpt, str) == 0)
{
gtk_combo_box_set_active_iter (
GTK_COMBO_BOX(widget), &iter);
g_free(shortOpt);
foundit = TRUE;
break;
}
g_free(shortOpt);
} while (gtk_tree_model_iter_next (store, &iter));
}
if (!foundit && gtk_tree_model_get_iter_first (store, &iter))
{
do
{
gtk_tree_model_get(store, &iter, 3, &ivalue, -1);
if ((gint)ivalue == ival || ivalue == dval)
{
gtk_combo_box_set_active_iter (
GTK_COMBO_BOX(widget), &iter);
foundit = TRUE;
break;
}
} while (gtk_tree_model_iter_next (store, &iter));
}
if (!foundit)
{
gtk_combo_box_set_active (GTK_COMBO_BOX(widget), 0);
}
}
else if (type == GTK_TYPE_COMBO_BOX_ENTRY)
{
GtkTreeModel *store;
GtkTreeIter iter;
gchar *shortOpt;
gdouble ivalue;
gboolean foundit = FALSE;
g_debug("GTK_COMBO_BOX_ENTRY");
store = gtk_combo_box_get_model(GTK_COMBO_BOX(widget));
if (gtk_tree_model_get_iter_first (store, &iter))
{
do
{
gtk_tree_model_get(store, &iter, 2, &shortOpt, -1);
if (strcmp(shortOpt, str) == 0)
{
gtk_combo_box_set_active_iter (
GTK_COMBO_BOX(widget), &iter);
g_free(shortOpt);
foundit = TRUE;
break;
}
g_free(shortOpt);
} while (gtk_tree_model_iter_next (store, &iter));
}
if (!foundit && gtk_tree_model_get_iter_first (store, &iter))
{
do
{
gtk_tree_model_get(store, &iter, 3, &ivalue, -1);
if ((gint)ivalue == ival || ivalue == dval)
{
gtk_combo_box_set_active_iter (
GTK_COMBO_BOX(widget), &iter);
foundit = TRUE;
break;
}
} while (gtk_tree_model_iter_next (store, &iter));
}
if (!foundit)
{
GtkEntry *entry = GTK_ENTRY(gtk_bin_get_child(GTK_BIN(widget)));
if (entry)
{
gtk_entry_set_text (entry, str);
}
}
}
else if (type == GTK_TYPE_SPIN_BUTTON)
{
g_debug("spin (%s)", str);
gtk_spin_button_set_value(GTK_SPIN_BUTTON(widget), dval);
}
else if (type == GTK_TYPE_HSCALE)
{
g_debug("hscale");
gtk_range_set_value(GTK_RANGE(widget), dval);
}
else if (type == GTK_TYPE_SCALE_BUTTON)
{
g_debug("scale_button");
gtk_scale_button_set_value(GTK_SCALE_BUTTON(widget), dval);
}
else if (type == GTK_TYPE_TEXT_VIEW)
{
g_debug("textview (%s)", str);
GtkTextBuffer *buffer = gtk_text_view_get_buffer(
GTK_TEXT_VIEW(widget));
gtk_text_buffer_set_text (buffer, str, -1);
}
else if (type == GTK_TYPE_LABEL)
{
gtk_label_set_markup (GTK_LABEL(widget), str);
}
else if (type == GTK_TYPE_FILE_CHOOSER_BUTTON)
{
GtkFileChooserAction act;
act = gtk_file_chooser_get_action(GTK_FILE_CHOOSER(widget));
if (str[0] == 0)
{
// Do nothing
;
}
else if (act == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
act == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
{
gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(widget), str);
}
else if (act == GTK_FILE_CHOOSER_ACTION_SAVE)
{
gtk_file_chooser_set_filename (GTK_FILE_CHOOSER(widget), str);
}
else
{
if (g_file_test(str, G_FILE_TEST_IS_DIR))
{
gtk_file_chooser_set_current_folder(
GTK_FILE_CHOOSER(widget), str);
}
else if (g_file_test(str, G_FILE_TEST_EXISTS))
{
gtk_file_chooser_set_filename (GTK_FILE_CHOOSER(widget), str);
}
else
{
gchar *dirname;
dirname = g_path_get_dirname(str);
gtk_file_chooser_set_current_folder(
GTK_FILE_CHOOSER(widget), dirname);
g_free(dirname);
}
}
}
else
{
g_debug("Attempt to set unknown widget type");
}
g_free(str);
}
int
ghb_ui_update_from_settings(GtkBuilder *builder, const gchar *name, const GValue *settings)
{
GObject *object;
GValue * value;
g_debug("ghb_ui_update_from_settings() %s", name);
if (name == NULL)
return 0;
value = ghb_settings_get_value(settings, name);
if (value == NULL)
return 0;
object = GHB_OBJECT(builder, name);
if (object == NULL)
{
g_debug("Failed to find widget for key: %s\n", name);
return -1;
}
update_widget((GtkWidget*)object, value);
return 0;
}
int
ghb_ui_update(signal_user_data_t *ud, const gchar *name, const GValue *value)
{
GObject *object;
g_debug("ghb_ui_update() %s", name);
if (name == NULL || value == NULL)
return 0;
object = GHB_OBJECT(ud->builder, name);
if (object == NULL)
{
g_debug("Failed to find widget for key: %s\n", name);
return -1;
}
update_widget((GtkWidget*)object, value);
// Its possible the value hasn't changed. Since settings are only
// updated when the value changes, I'm initializing settings here as well.
ghb_widget_to_setting(ud->settings, (GtkWidget*)object);
return 0;
}
|