/
dialog-preferences.c
1507 lines (1256 loc) · 50.5 KB
/
dialog-preferences.c
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
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/********************************************************************\
* dialog-preferences.c -- preferences dialog *
* *
* Copyright (C) 2005 David Hampton *
* Copyright (C) 2011 Robert Fewell *
* Copyright (C) 2013 Geert Janssens *
* *
* This program is free software; you can 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. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License*
* along with this program; if not, contact: *
* *
* Free Software Foundation Voice: +1-617-542-5942 *
* 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
* Boston, MA 02110-1301, USA gnu@gnu.org *
\********************************************************************/
/** @addtogroup Dialogs
@{ */
/** @addtogroup PrefDialog Preferences Dialog
@{ */
/** @file dialog-preferences.c
@brief Dialog for handling user preferences.
@author Copyright (c) 2005 David Hampton <hampton@employees.org>
These functions are the external API available for the user
preference dialog. This dialog allows a user to modify
several user preferences in the gnucash preferences database.
Any module may add a page (or partial page) of preferences
to the dialog. These additions are done by providing
the name of a glade file and the content to load from that
file along with a widget in that file. If a partial
page is added, the widget name provided must be that of
a GtkGrid containing four columns. If a full page is added,
the widget name provided to this code can be any kind of
widget, but for consistency it should probably be the same.
If a widget name is in the form pref/aaa.bbb/ccc... and it is a type
of widget this code knows how to handle, then the widget is bound
to the preference named ccc in group aaa.bbb. This means that if
the widget's value changes, the preference is automatically updated.
The same goes the other way around. This code currently knows about
font buttons, radio buttons, check buttons, spin boxes, combo boxes,
gnucash currency select widgets, gnucash accounting period widgets,
and a gnucash date edit widget. (Combo boxes should not be used for
less than five choices. Use a radio button group instead.)
The argument *is* a glade file, so if your code has special
requirements (e.g. make one widget insensitive until another is
selected) feel free to go ahead and add your own callbacks to the
glade file. This code will connect any callbacks that exist in
the glade file.
*/
#include <config.h>
#include <gtk/gtk.h>
#include <glib/gi18n.h>
#include "dialog-utils.h"
#include "gnc-currency-edit.h"
#include "gnc-date-edit.h"
#include "gnc-gobject-utils.h"
#include "gnc-period-select.h"
#include "gnc-engine.h"
#include "Account.h"
#include "gnc-prefs.h"
#include "gnc-ui.h"
#include "gnc-ui-util.h"
#include "gnc-component-manager.h"
#include "dialog-preferences.h"
#include "dialog-doclink-utils.h"
#define DIALOG_PREFERENCES_CM_CLASS "dialog-newpreferences"
#define GNC_PREFS_GROUP "dialogs.preferences"
#define PREF_PREFIX_LEN sizeof("pref/") - 1
#define PREFS_WIDGET_HASH "prefs_widget_hash"
#define NOTEBOOK "notebook"
/** The debugging module that this .o belongs to. */
static QofLogModule log_module = GNC_MOD_PREFS;
void gnc_preferences_response_cb(GtkDialog *dialog, gint response, GtkDialog *unused);
void gnc_account_separator_pref_changed_cb (GtkEntry *entry, GtkWidget *dialog);
gboolean gnc_account_separator_validate_cb (GtkEntry *entry, GdkEvent *event, GtkWidget *dialog);
void gnc_save_on_close_expires_cb (GtkToggleButton *button, GtkWidget *dialog);
/** This data structure holds the information for a single addition to
* the preferences dialog. */
typedef struct addition_t
{
/** The relative name of the file where the glade data for this
* addition can be found. */
gchar *filename;
/** The name of the widget within the glade data file that should be
* added to the preferences dialog. This should point to a
* GtkGrid widget that has four columns. */
gchar *widgetname;
/** The name of the tab within the preferences dialog where these
* widgets should be placed. */
gchar *tabname;
/** TRUE if this addition represents a full page in the preferences
* dialog. FALSE if this page may be combined with other pages. */
gboolean full_page;
} addition;
/** A list of all additions that have been made to the preferences
* dialog. The data fields for this list are ::addition data
* structures. */
GSList *add_ins = NULL;
static gchar *gnc_account_separator_is_valid (const gchar *separator,
gchar **normalized_separator)
{
QofBook *book = gnc_get_current_book();
GList *conflict_accts = NULL;
gchar *message = NULL;
*normalized_separator = gnc_normalize_account_separator (separator);
conflict_accts = gnc_account_list_name_violations (book, *normalized_separator);
if (conflict_accts)
message = gnc_account_name_violations_errmsg (*normalized_separator,
conflict_accts);
g_list_free (conflict_accts);
return message;
}
/** This function is called whenever the account separator is changed
* in the preferences dialog. It updates the example label in the
* "Account" page of the preferences dialog.
*
* @internal
*
* @param entry The text entry field for the account separator
*
* @param dialog A pointer to the preferences dialog.
*/
void
gnc_account_separator_pref_changed_cb (GtkEntry *entry, GtkWidget *dialog)
{
GtkWidget *label, *image;
gchar *sample;
gchar *separator;
gchar *conflict_msg = gnc_account_separator_is_valid (gtk_entry_get_text (entry), &separator);
label = g_object_get_data(G_OBJECT(dialog), "sample_account");
DEBUG("Sample Account pointer is %p", label );
/* Translators: Both %s will be the account separator character; the
resulting string is a demonstration how the account separator
character will look like. You can replace these three account
names with other account names that are more suitable for your
language - just keep in mind to have exactly two %s in your
translation. */
sample = g_strdup_printf(_("Income%sSalary%sTaxable"),
separator, separator);
PINFO(" Label set to '%s'", sample);
gtk_label_set_text(GTK_LABEL(label), sample);
g_free(sample);
/* Check if the new separator clashes with existing account names */
image = g_object_get_data(G_OBJECT(dialog), "separator_error");
DEBUG("Separator Error Image pointer is %p", image );
if (conflict_msg)
{
gtk_widget_set_tooltip_text(GTK_WIDGET(image), conflict_msg);
gtk_widget_show (GTK_WIDGET(image));
g_free ( conflict_msg );
}
else
gtk_widget_hide (GTK_WIDGET(image));
g_free (separator);
}
gboolean
gnc_account_separator_validate_cb (GtkEntry *entry, GdkEvent *event, GtkWidget *dialog)
{
gchar *separator;
gchar *conflict_msg = gnc_account_separator_is_valid (gtk_entry_get_text (entry), &separator);
/* Check if the new separator clashes with existing account names */
if (conflict_msg)
{
gnc_warning_dialog (GTK_WINDOW (dialog), "%s", conflict_msg);
g_free ( conflict_msg );
}
g_free (separator);
return FALSE;
}
/** Called when the save-on-close checkbutton is toggled.
* @internal
* @param button the toggle button.
* @param dialog the prefs dialog.
*/
void
gnc_save_on_close_expires_cb (GtkToggleButton *button, GtkWidget *dialog)
{
GtkWidget *spinner = g_object_get_data (G_OBJECT (dialog),
"save_on_close_wait_time");
gtk_widget_set_sensitive(spinner, gtk_toggle_button_get_active(button));
}
/** This function compares two add-ins to see if they specify the same
* tab name.
*
* @internal
*
* @param a A pointer to the first add-in.
*
* @param b A pointer to the second add-in.
*
* @return Zero if the tab name is the same in both add-ins. Non-zero otherwise.
*/
static gint
gnc_prefs_compare_addins (addition *a,
addition *b)
{
return g_utf8_collate(a->tabname, b->tabname);
}
/** This is the common function that adds any set of preferences to
* the preferences dialog. It allocates a data structure to remember
* the passed in data and queues it for later when the dialog is
* actually built. This code does check to ensure there aren't any
* conflicts, like multiple additions of the same tab name when the
* two pages being added aren't compatible.
*
* @internal
*
* @param filename The name of a glade file.
*
* @param widgetname A string of content to load, the last one being
* the name of the widget to add to the preferences dialog.
*
* @param tabname The name this page of preferences should have in
* the dialog notebook.
*
* @param full_page Is this a full page of preferences or a partial page.
*/
static void
gnc_preferences_add_page_internal (const gchar *filename,
const gchar *widgetname,
const gchar *tabname,
gboolean full_page)
{
addition *add_in, *preexisting;
gboolean error = FALSE;
GSList *ptr;
ENTER("file %s, widget %s, tab %s full page %d",
filename, widgetname, tabname, full_page);
add_in = g_malloc(sizeof(addition));
if (add_in == NULL)
{
g_critical("Unable to allocate memory.\n");
LEAVE("no memory");
return;
}
add_in->filename = g_strdup(filename);
add_in->widgetname = g_strdup(widgetname);
add_in->tabname = g_strdup(tabname);
add_in->full_page = full_page;
if (!add_in->filename || !add_in->widgetname || !add_in->tabname)
{
g_critical("Unable to allocate memory.\n");
g_free(add_in->filename);
g_free(add_in->widgetname);
g_free(add_in->tabname);
g_free(add_in);
LEAVE("no memory");
return;
}
ptr = g_slist_find_custom(add_ins, add_in, (GCompareFunc)gnc_prefs_compare_addins);
if (ptr)
{
/* problem? */
preexisting = ptr->data;
if (preexisting->full_page)
{
g_warning("New tab %s(%s/%s/%s) conflicts with existing tab %s(%s/%s/full)",
add_in->tabname, add_in->filename, add_in->widgetname,
add_in->full_page ? "full" : "partial",
preexisting->tabname, preexisting->filename, preexisting->widgetname);
error = TRUE;
}
else if (add_in->full_page)
{
g_warning("New tab %s(%s/%s/%s) conflicts with existing tab %s(%s/%s/partial)",
add_in->tabname, add_in->filename, add_in->widgetname,
add_in->full_page ? "full" : "partial",
preexisting->tabname, preexisting->filename, preexisting->widgetname);
error = TRUE;
}
}
if (error)
{
g_free(add_in->filename);
g_free(add_in->widgetname);
g_free(add_in->tabname);
g_free(add_in);
LEAVE("err");
return;
}
else
{
add_ins = g_slist_append(add_ins, add_in);
}
LEAVE("");
}
/* This function adds a full page of preferences to the preferences
* dialog. When the dialog is created, the specified content will be
* pulled from the specified glade file and added to the preferences
* dialog with the specified tab name. The tab name may not be
* duplicated. For example, the Business code might have a full page
* of its own preferences. */
void
gnc_preferences_add_page (const gchar *filename,
const gchar *widgetname,
const gchar *tabname)
{
gnc_preferences_add_page_internal(filename, widgetname, tabname, TRUE);
}
/* This function adds a partial page of preferences to the
* preferences dialog. When the dialog is created, the specified
* content will be pulled from the glade file and added to the
* preferences dialog with the specified tab name. The tab name
* may be duplicated. For example, the HBCI preferences may share a
* "Data Import" page with QIF and other methods. */
void
gnc_preferences_add_to_page (const gchar *filename,
const gchar *widgetname,
const gchar *tabname)
{
gnc_preferences_add_page_internal(filename, widgetname, tabname, FALSE);
}
/*******************************************************************/
/** This function builds a hash table of "interesting" widgets,
* i.e. widgets whose name starts with "pref/". This table is
* needed to perform name->widget lookups when binding the widgets
* to their matching preferences.
*
* @internal
*
* @param builder A pointer to builder glade file currently being
* added to the dialog.
*
* @param dialog A pointer to the dialog. The hash table is stored
* as a pointer off the dialog so that it can be found in the binding
* code. */
static void
gnc_prefs_build_widget_table (GtkBuilder *builder,
GtkWidget *dialog)
{
GHashTable *prefs_table;
GSList *interesting, *runner;
const gchar *name;
const gchar *wname;
GtkWidget *widget;
prefs_table = g_object_get_data(G_OBJECT(dialog), PREFS_WIDGET_HASH);
interesting = gtk_builder_get_objects(builder);
for (runner = interesting; runner; runner = g_slist_next(runner))
{
widget = runner->data;
if (GTK_IS_WIDGET(widget))
{
wname = gtk_widget_get_name(widget);
name = gtk_buildable_get_name(GTK_BUILDABLE(widget));
DEBUG("Widget type is %s and buildable get name is %s", wname, name);
if (g_str_has_prefix (name, "pref"))
g_hash_table_insert(prefs_table, (gchar *)name, widget);
}
}
g_slist_free(interesting);
}
/** This data structure is used while building the preferences dialog
* to copy a grid from a glade file to the dialog under construction.
* It maintains state information between invocations of the function
* gnc_prefs_move_grid_entry which is called via a foreach loop over
* each item in the grid. */
struct copy_data
{
/** The grid being copied from. */
GtkGrid *grid_from;
/** The grid being copied to. */
GtkGrid *grid_to;
/** The number of columns and rows in the grid. */
gint cols, rows;
};
static GtkWidget *
gnc_prefs_find_page (GtkNotebook *notebook, const gchar *name)
{
int n_pages, i;
GtkWidget *child;
const gchar *child_name;
g_return_val_if_fail (GTK_IS_NOTEBOOK (notebook), NULL);
g_return_val_if_fail (name, NULL);
ENTER("");
n_pages = gtk_notebook_get_n_pages (notebook);
for (i = 0; i < n_pages; i++)
{
child = gtk_notebook_get_nth_page (notebook, i);
g_return_val_if_fail (child, NULL);
child_name = gtk_notebook_get_tab_label_text (notebook, child);
g_return_val_if_fail (child_name, NULL);
if (g_utf8_collate (name, child_name) == 0)
{
LEAVE("found at index: %d", i);
return child;
}
}
LEAVE("not found");
return NULL;
}
/** This function finds the size of a GtkGrid and saves it to
* the data structure.
*
* @internal
*
* @param widget A pointer to the widget to move.
*
* @param data A pointer to a data structure passed in by the caller.
* This data structure contains pointers to the old and new grids
* plus the row offset into the new grid.
*/
static void
gnc_prefs_get_grid_size (GtkWidget *child, gpointer data)
{
struct copy_data *copydata = data;
gint top, left, height, width;
gtk_container_child_get(GTK_CONTAINER(copydata->grid_to), child,
"left-attach", &left,
"top-attach", &top,
"height", &height,
"width", &width,
NULL);
if (left + width >= copydata->cols)
copydata->cols = left + width;
if (top + height >= copydata->rows)
copydata->rows = top + height;
}
/** This function moves a GtkWidget from one GtkGrid to another,
* preserving its attachment data, etc. It is called when adding one
* partial preference page to another.
*
* @internal
*
* @param widget A pointer to the widget to move.
*
* @param data A pointer to a data structure passed in by the caller.
* This data structure contains pointers to the old and new grids
* plus the row offset into the new grid.
*/
static void
gnc_prefs_move_grid_entry (GtkWidget *child,
gpointer data)
{
struct copy_data *copydata = data;
gint top, left, height, width;
gboolean hexpand, vexpand;
GtkAlign halign, valign;
gint topm, bottomm, leftm, rightm;
ENTER("child %p, copy data %p", child, data);
gtk_container_child_get(GTK_CONTAINER(copydata->grid_from), child,
"left-attach", &left,
"top-attach", &top,
"height", &height,
"width", &width,
NULL);
hexpand = gtk_widget_get_hexpand (child);
vexpand = gtk_widget_get_vexpand (child);
halign = gtk_widget_get_halign (child);
valign = gtk_widget_get_valign (child);
g_object_get (child, "margin-top", &topm, "margin-bottom", &bottomm, NULL);
g_object_get (child, "margin-left", &leftm, "margin-right", &rightm, NULL);
g_object_ref(child);
gtk_container_remove(GTK_CONTAINER(copydata->grid_from), child);
gtk_grid_attach(copydata->grid_to, child, left, copydata->rows + top , width, height);
gtk_widget_set_hexpand (child, hexpand);
gtk_widget_set_vexpand (child, vexpand);
gtk_widget_set_halign (child, halign);
gtk_widget_set_valign (child, valign);
g_object_set (child, "margin-left", leftm, "margin-right", rightm, NULL);
g_object_set (child, "margin-top", topm, "margin-bottom", bottomm, NULL);
g_object_unref(child);
LEAVE(" ");
}
/** At dialog creation time, this function will be called once per
* adds-in. It performs the work of adding the page into the main
* dialog. It handles both the case of a full page being added to
* the dialog, and a partial page being added.
*
* @internal
*
* @param data A pointer to an addition data structure.
*
* @param user_data A pointer to the dialog.
*/
static void
gnc_preferences_build_page (gpointer data,
gpointer user_data)
{
GtkBuilder *builder;
GtkWidget *dialog, *existing_content, *new_content, *label;
GtkNotebook *notebook;
addition *add_in;
struct copy_data copydata = {NULL, NULL, 0, 0};
gchar **widgetname;
gint i;
ENTER("add_in %p, dialog %p", data, user_data);
add_in = (addition *)data;
dialog = user_data;
DEBUG("Opening %s to get %s", add_in->filename, add_in->widgetname);
builder = gtk_builder_new();
/* Adjustments etc... must come before dialog information */
widgetname = g_strsplit(add_in->widgetname, ",", -1);
for (i = 0; widgetname[i]; i++)
{
DEBUG("Opening %s to get content %s", add_in->filename, widgetname[i]);
gnc_builder_add_from_file (builder, add_in->filename, widgetname[i]);
}
DEBUG("Widget Content is %s", widgetname[i - 1]);
new_content = GTK_WIDGET(gtk_builder_get_object (builder, widgetname[i - 1]));
g_strfreev(widgetname);
DEBUG("done");
/* Add to the list of interesting widgets */
gnc_prefs_build_widget_table(builder, dialog);
/* Connect the signals in this glade file. The dialog is passed in
* so the callback can find "interesting" widgets from other
* glade files if necessary (via the GPREFS_WIDGET_HASH hash table). */
gtk_builder_connect_signals_full (builder, gnc_builder_connect_full_func, dialog);
/* Prepare for recursion */
notebook = g_object_get_data(G_OBJECT(dialog), NOTEBOOK);
if (add_in->full_page)
{
label = gtk_label_new(add_in->tabname);
gnc_label_set_alignment(label, 0.0, 0.5);
gtk_notebook_append_page(notebook, new_content, label);
g_object_unref(G_OBJECT(builder));
LEAVE("appended page");
return;
}
/* Copied grids must be grids */
if (!GTK_IS_GRID(new_content))
{
g_critical("The object name %s in file %s is not a GtkGrid. It cannot "
"be added to the preferences dialog.",
add_in->widgetname, add_in->filename);
g_object_unref(G_OBJECT(builder));
LEAVE("");
return;
}
/* Does the page exist or must we create it */
existing_content = gnc_prefs_find_page(notebook, add_in->tabname);
if (!existing_content)
{
/* No existing content with this name. Create a blank page */
existing_content = gtk_grid_new();
gtk_container_set_border_width(GTK_CONTAINER(existing_content), 6);
label = gtk_label_new(add_in->tabname);
gnc_label_set_alignment(label, 0.0, 0.5);
gtk_notebook_append_page(notebook, existing_content, label);
gtk_widget_show_all(existing_content);
DEBUG("created new page %s, appended it", add_in->tabname);
}
else
{
/* Lets get the size of the existing grid */
copydata.grid_to = GTK_GRID(existing_content);
gtk_container_foreach(GTK_CONTAINER(existing_content), gnc_prefs_get_grid_size, ©data);
DEBUG("found existing page %s, grid size is %d x %d", add_in->tabname, copydata.rows, copydata.cols);
}
/* Maybe add a spacer row */
if (copydata.rows > 0)
{
label = gtk_label_new("");
gtk_widget_show(label);
gtk_grid_attach (GTK_GRID(existing_content), label, 0, copydata.rows, 1, 1);
copydata.rows = copydata.rows + 1;
DEBUG("add spacer row");
}
/* Now copy all the entries in the grid */
copydata.grid_from = GTK_GRID(new_content);
copydata.grid_to = GTK_GRID(existing_content);
gtk_container_foreach(GTK_CONTAINER(new_content), gnc_prefs_move_grid_entry, ©data);
g_object_ref_sink(new_content);
g_object_unref(G_OBJECT(builder));
LEAVE("added content to page");
}
static gint
tab_cmp (GtkWidget *page_a, GtkWidget *page_b, GtkNotebook *notebook)
{
return g_utf8_collate (gtk_notebook_get_tab_label_text (notebook, page_a),
gtk_notebook_get_tab_label_text (notebook, page_b));
}
static void
gnc_prefs_sort_pages (GtkNotebook *notebook)
{
gint n_pages, i;
GList *tabs = NULL, *iter = NULL;
g_return_if_fail (GTK_IS_NOTEBOOK (notebook));
/* gather tabs */
n_pages = gtk_notebook_get_n_pages (notebook);
for (i = n_pages - 1; i >= 0; i--)
tabs = g_list_prepend (tabs, gtk_notebook_get_nth_page (notebook, i));
/* sort in local copy */
tabs = g_list_sort_with_data (tabs, (GCompareDataFunc) tab_cmp, notebook);
/* reorder tabs */
for (i = 0, iter = tabs; iter; i++, iter = iter->next)
gtk_notebook_reorder_child (notebook, GTK_WIDGET (iter->data), i);
g_list_free (tabs);
}
/*******************************/
/* Dynamically added Callbacks */
/*******************************/
static void
gnc_prefs_split_widget_name (const gchar *name, gchar **group, gchar **pref)
{
const gchar *group_with_pref = name + PREF_PREFIX_LEN;
gchar **splits = g_strsplit (group_with_pref, "/", 0);
*group = g_strdup (splits[0]);
*pref = g_strdup (splits[1]);
g_strfreev (splits);
}
/****************************************************************************/
/** Connect a GtkFontButton widget to its stored value in the preferences database.
*
* @internal
*
* @param fb A pointer to the font button that should be connected.
*/
static void
gnc_prefs_connect_font_button (GtkFontButton *fb)
{
gchar *group, *pref;
g_return_if_fail(GTK_IS_FONT_BUTTON(fb));
gnc_prefs_split_widget_name (gtk_buildable_get_name(GTK_BUILDABLE(fb)), &group, &pref);
gnc_prefs_bind (group, pref, G_OBJECT (fb), "font-name");
g_free (group);
g_free (pref);
gtk_widget_show_all(GTK_WIDGET(fb));
}
/****************************************************************************/
/** Callback for a GtkFileChooser widget to store a value in the preferences database.
*
* @internal
*
* @param fc A pointer to the file chooser widget emitting signal.
*/
static void
file_chooser_selected_cb (GtkFileChooser *fc, gpointer user_data)
{
GtkImage *image = g_object_get_data (G_OBJECT(fc), "path_head_error");
const gchar *group = g_object_get_data (G_OBJECT(fc), "group");
const gchar *pref = g_object_get_data (G_OBJECT(fc), "pref");
gchar *folder_uri = gtk_file_chooser_get_uri (fc);
gchar *old_path_head_uri = gnc_doclink_get_path_head ();
// make sure path_head ends with a trailing '/', 3.5 onwards
if (!g_str_has_suffix (folder_uri, "/"))
{
gchar *folder_with_slash = g_strconcat (folder_uri, "/", NULL);
g_free (folder_uri);
folder_uri = g_strdup (folder_with_slash);
g_free (folder_with_slash);
}
gtk_widget_hide (GTK_WIDGET(image));
if (!gnc_prefs_set_string (group, pref, folder_uri))
PINFO("Failed to save preference at %s, %s with %s", group, pref, folder_uri);
else
gnc_doclink_pref_path_head_changed (
GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (fc))),
old_path_head_uri);
g_free (old_path_head_uri);
g_free (folder_uri);
}
/** Connect a GtkFileChooserButton widget to its stored value in the preferences database.
*
* @internal
*
* @param fb A pointer to the file chooser button that should be connected.
*
* @param boxname The Hbox name that contains the GtkFileChooserButton and Clear button
*/
static void
gnc_prefs_connect_file_chooser_button (GtkFileChooserButton *fcb, const gchar *boxname)
{
GtkImage *image;
gchar *group, *pref;
gchar *uri;
gboolean folder_set = TRUE;
g_return_if_fail (GTK_FILE_CHOOSER_BUTTON(fcb));
if (boxname == NULL)
gnc_prefs_split_widget_name (gtk_buildable_get_name (GTK_BUILDABLE(fcb)), &group, &pref);
else
gnc_prefs_split_widget_name (boxname, &group, &pref);
uri = gnc_prefs_get_string (group, pref);
PINFO("Uri is %s", uri);
if (uri && *uri) // default entry
{
gchar *path_head = g_filename_from_uri (uri, NULL, NULL);
// test for current folder present and set chooser to it
if (g_file_test (path_head, G_FILE_TEST_IS_DIR))
gtk_file_chooser_set_current_folder_uri (GTK_FILE_CHOOSER(fcb), uri);
else
folder_set = FALSE;
g_free (path_head);
}
image = g_object_get_data (G_OBJECT(fcb), "path_head_error");
if (folder_set) // If current folder missing, display error and tt message
gtk_widget_hide (GTK_WIDGET(image));
else
{
gchar *path_head = gnc_doclink_get_unescape_uri (NULL, uri, "file");
gchar *ttip = g_strconcat (_("Path does not exist, "), path_head, NULL);
gtk_widget_set_tooltip_text (GTK_WIDGET(image), ttip);
gtk_widget_show (GTK_WIDGET(image));
g_free (ttip);
g_free (path_head);
}
g_signal_connect (GTK_FILE_CHOOSER(fcb), "selection-changed",
G_CALLBACK(file_chooser_selected_cb), NULL);
g_object_set_data_full (G_OBJECT(fcb),"group", g_strdup (group), (GDestroyNotify) g_free);
g_object_set_data_full (G_OBJECT(fcb),"pref", g_strdup (pref), (GDestroyNotify) g_free);
g_free (group);
g_free (pref);
g_free (uri);
gtk_widget_show_all (GTK_WIDGET(fcb));
}
/** Callback for a 'Clear' button for GtkFileChooserButton widget.
*
* @internal
*
* @param button A pointer to the button widget emitting signal.
*
* @param user_data A Pointer to the GtkFileChooserButton widget.
*/
static void
file_chooser_clear_cb (GtkButton *button, gpointer user_data)
{
GtkFileChooserButton *fcb = GTK_FILE_CHOOSER_BUTTON(user_data);
const gchar *group = g_object_get_data (G_OBJECT(fcb), "group");
const gchar *pref = g_object_get_data (G_OBJECT(fcb), "pref");
GtkImage *image = g_object_get_data (G_OBJECT(fcb), "path_head_error");
GtkWidget *box;
GtkWidget *fcb_new;
gchar *boxname;
gchar *old_path_head_uri = gnc_doclink_get_path_head ();
/* We need to destroy the GtkFileChooserButton and recreate as there
does not seem to be away of resetting the folder path to NONE */
box = gtk_widget_get_parent (GTK_WIDGET(fcb));
g_signal_handlers_disconnect_by_func (button, file_chooser_clear_cb, fcb);
if (!gnc_prefs_set_string (group, pref, ""))
PINFO("Failed to Clear preference at %s, %s", group, pref);
else
gnc_doclink_pref_path_head_changed (
GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (fcb))),
old_path_head_uri);
gtk_widget_destroy (GTK_WIDGET(fcb));
fcb_new = gtk_file_chooser_button_new (_("Select a folder"),
GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER);
g_object_set_data (G_OBJECT(fcb_new), "path_head_error", image);
g_object_set_data_full (G_OBJECT(fcb_new),"group", g_strdup (group), (GDestroyNotify) g_free);
g_object_set_data_full (G_OBJECT(fcb_new),"pref", g_strdup (pref), (GDestroyNotify) g_free);
gtk_box_pack_start (GTK_BOX(box), fcb_new, TRUE, TRUE, 0);
gtk_box_reorder_child (GTK_BOX(box), fcb_new, 0);
gtk_widget_show (fcb_new);
g_signal_connect (GTK_BUTTON(button), "clicked",
G_CALLBACK(file_chooser_clear_cb), fcb_new);
boxname = g_strconcat ("pref/", group, "/", pref, NULL);
gnc_prefs_connect_file_chooser_button (GTK_FILE_CHOOSER_BUTTON(fcb_new), boxname);
g_free (boxname);
g_free (old_path_head_uri);
}
/****************************************************************************/
/** Connect a GtkRadioButton widget to its stored value in the preferences database.
*
* @internal
*
* @param button A pointer to the radio button that should be
* connected.
*/
static void
gnc_prefs_connect_radio_button (GtkRadioButton *button)
{
gchar *group, *pref;
g_return_if_fail(GTK_IS_RADIO_BUTTON(button));
gnc_prefs_split_widget_name (gtk_buildable_get_name(GTK_BUILDABLE(button)), &group, &pref);
gnc_prefs_bind (group, pref, G_OBJECT (button), "active");
g_free (group);
g_free (pref);
}
/****************************************************************************/
/** Connect a GtkCheckButton widget to its stored value in the preferences database.
*
* @internal
*
* @param button A pointer to the check button that should be
* connected.
*/
static void
gnc_prefs_connect_check_button (GtkCheckButton *button)
{
gchar *group, *pref;
g_return_if_fail(GTK_IS_CHECK_BUTTON(button));
gnc_prefs_split_widget_name (gtk_buildable_get_name(GTK_BUILDABLE(button)), &group, &pref);
gnc_prefs_bind (group, pref, G_OBJECT (button), "active");
g_free (group);
g_free (pref);
}
/****************************************************************************/
/** Connect a GtkSpinButton widget to its stored value in the preferences database.
*
* @internal
*
* @param button A pointer to the spin button that should be
* connected.
*/
static void
gnc_prefs_connect_spin_button (GtkSpinButton *spin)
{
gchar *group, *pref;
g_return_if_fail(GTK_IS_SPIN_BUTTON(spin));
gnc_prefs_split_widget_name (gtk_buildable_get_name(GTK_BUILDABLE(spin)), &group, &pref);
gnc_prefs_bind (group, pref, G_OBJECT (spin), "value");
g_free (group);
g_free (pref);
}
/****************************************************************************/
/** Connect a GtkComboBox widget to its stored value in the preferences database.
*
* @internal
*
* @param box A pointer to the combo box that should be connected.
*/
static void
gnc_prefs_connect_combo_box (GtkComboBox *box)
{
gchar *group, *pref;
g_return_if_fail(GTK_IS_COMBO_BOX(box));
gnc_prefs_split_widget_name (gtk_buildable_get_name(GTK_BUILDABLE(box)), &group, &pref);
gnc_prefs_bind (group, pref, G_OBJECT (box), "active");
g_free (group);
g_free (pref);
}
/****************************************************************************/
/** Connect a GncCurrencyEdit widget to its stored value in the preferences database.