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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
|
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* nautilus-preferences.c - Preference peek/poke/notify implementation.
Copyright (C) 1999, 2000 Eazel, Inc.
The Gnome Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The Gnome Library 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the Gnome Library; see the file COPYING.LIB. If not,
write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
Authors: Ramiro Estrugo <ramiro@eazel.com>
*/
#include <config.h>
#include "nautilus-preferences.h"
#include "nautilus-gconf-extensions.h"
#include "nautilus-lib-self-check-functions.h"
#include <eel/eel-enumeration.h>
#include <eel/eel-glib-extensions.h>
#include <eel/eel-string-list.h>
#include <eel/eel-string.h>
#include <gconf/gconf-client.h>
#include <gconf/gconf.h>
#include <gtk/gtksignal.h>
#include <libgnome/gnome-defs.h>
#include <libgnome/gnome-i18n.h>
#include <libgnomeui/gnome-dialog.h>
#include <libgnomeui/gnome-dialog-util.h>
#define DEFAULT_USER_LEVEL NAUTILUS_USER_LEVEL_INTERMEDIATE
/* An enumeration used for updating auto-storage variables in a type-specific way.
* FIXME: there is another enumeration like this in nautilus-global-preferences.c,
* used for different purposes but in a related way. Should we combine them?
*/
typedef enum
{
PREFERENCE_BOOLEAN = 1,
PREFERENCE_INTEGER,
PREFERENCE_STRING
} PreferenceType;
/*
* PreferencesEntry:
*
* A structure to manage preference hash table nodes.
* Preferences are hash tables. The hash key is the preference name
* (a string). The hash value is a pointer of the following struct:
*/
typedef struct {
char *name;
char *description;
PreferenceType type;
GList *callback_list;
GList *auto_storage_list;
int gconf_connection_id;
EelEnumeration *enumeration;
GConfValue *cached_value;
} PreferencesEntry;
/*
* PreferencesCallbackEntry:
*
* A structure to manage callback lists. A callback list is a GList.
* The callback_data in each list node is a pointer to the following
* struct:
*/
typedef struct {
NautilusPreferencesCallback callback;
gpointer callback_data;
} PreferencesCallbackEntry;
static const char *user_level_names_for_display[] =
{
N_("Beginner"),
N_("Intermediate"),
N_("Advanced")
};
static const char *user_level_names_for_storage[] =
{
"novice",
"intermediate",
"advanced"
};
static char * preferences_get_path (void);
static char * preferences_get_defaults_path (void);
static char * preferences_get_visibility_path (void);
static char * preferences_get_user_level_key (void);
static GConfClient *preferences_global_client_get (void);
static void preferences_global_client_remove_notification (void);
static gboolean preferences_preference_is_internal (const char *name);
static gboolean preferences_preference_is_user_level (const char *name);
static gboolean preferences_preference_is_default (const char *name);
static char * preferences_key_make (const char *name);
static char * preferences_key_make_for_getter (const char *name);
static char * preferences_key_make_for_default (const char *name,
int user_level);
static char * preferences_key_make_for_default_getter (const char *name,
int user_level);
static char * preferences_key_make_for_visibility (const char *name);
static void preferences_user_level_changed_notice (GConfClient *client,
guint connection_id,
GConfEntry *gconf_entry,
gpointer user_data);
static void preferences_something_changed_notice (GConfClient *client,
guint connection_id,
GConfEntry *gconf_entry,
gpointer user_data);
static void preferences_global_table_check_changes_function (gpointer key,
gpointer value,
gpointer callback_data);
static GHashTable *preferences_global_table_get_global (void);
static void preferences_callback_entry_free (PreferencesCallbackEntry *callback_entry);
static int preferences_user_level_check_range (int user_level);
static void preferences_entry_update_auto_storage (PreferencesEntry *entry);
static int user_level_changed_connection_id = -1;
static GHashTable *global_table = NULL;
static char *
preferences_get_path (void)
{
return g_strdup ("/apps/nautilus");
}
static char *
preferences_get_defaults_path (void)
{
char *defaults_path;
char *path;
path = preferences_get_path ();
defaults_path = g_strdup_printf ("%s/defaults", path);
g_free (path);
return defaults_path;
}
static char *
preferences_get_visibility_path (void)
{
char *visibility_path;
char *path;
path = preferences_get_path ();
visibility_path = g_strdup_printf ("%s/visibility", path);
g_free (path);
return visibility_path;
}
static char *
preferences_get_user_level_key (void)
{
char *user_level_key;
char *path;
path = preferences_get_path ();
user_level_key = g_strdup_printf ("%s/user_level", path);
g_free (path);
return user_level_key;
}
/* If the preference name begind with a "/", we interpret
* it as a straight gconf key. */
static gboolean
preferences_preference_is_internal (const char *name)
{
g_return_val_if_fail (name != NULL, FALSE);
if (eel_str_has_prefix (name, "/")) {
return FALSE;
}
return TRUE;
}
static gboolean
preferences_preference_is_user_level (const char *name)
{
gboolean result;
char *user_level_key;
g_return_val_if_fail (name != NULL, FALSE);
user_level_key = preferences_get_user_level_key ();
result = eel_str_is_equal (name, user_level_key)
|| eel_str_is_equal (name, "user_level");
g_free (user_level_key);
return result;
}
static char *
preferences_key_make (const char *name)
{
char *key;
char *path;
g_return_val_if_fail (name != NULL, NULL);
if (!preferences_preference_is_internal (name)) {
return g_strdup (name);
}
/* Otherwise, we prefix it with the path */
path = preferences_get_path ();
key = g_strdup_printf ("%s/%s", path, name);
g_free (path);
return key;
}
static char *
preferences_key_make_for_default (const char *name,
int user_level)
{
char *key;
char *default_key = NULL;
char *defaults_path;
char *storage_name;
g_return_val_if_fail (name != NULL, NULL);
user_level = preferences_user_level_check_range (user_level);
key = preferences_key_make (name);
defaults_path = preferences_get_defaults_path ();
storage_name = nautilus_preferences_get_user_level_name_for_storage (user_level);
default_key = g_strdup_printf ("%s/%s%s",
defaults_path,
storage_name,
key);
g_free (storage_name);
g_free (key);
g_free (defaults_path);
return default_key;
}
static char *
preferences_key_make_for_default_getter (const char *name,
int user_level)
{
char *default_key_for_getter = NULL;
gboolean done;
g_return_val_if_fail (name != NULL, NULL);
user_level = preferences_user_level_check_range (user_level);
done = FALSE;
while (!done) {
default_key_for_getter = preferences_key_make_for_default (name, user_level);
done = (user_level == 0) || (!nautilus_gconf_is_default (default_key_for_getter));
if (!done) {
g_free (default_key_for_getter);
user_level--;
}
}
return default_key_for_getter;
}
static char *
preferences_key_make_for_visibility (const char *name)
{
char *default_key;
char *key;
char *visibility_path;
g_return_val_if_fail (name != NULL, NULL);
key = preferences_key_make (name);
visibility_path = preferences_get_visibility_path ();
default_key = g_strdup_printf ("%s%s", visibility_path, key);
g_free (key);
g_free (visibility_path);
return default_key;
}
static void
preferences_global_client_remove_notification (void)
{
GConfClient *client;
client = preferences_global_client_get ();
g_return_if_fail (client != NULL);
gconf_client_notify_remove (client, user_level_changed_connection_id);
user_level_changed_connection_id = -1;
}
static GConfClient *
preferences_global_client_get (void)
{
static GConfClient *global_gconf_client = NULL;
GError *error = NULL;
char *path;
char *user_level_key;
if (global_gconf_client != NULL) {
return global_gconf_client;
}
global_gconf_client = nautilus_gconf_client_get_global ();
g_return_val_if_fail (global_gconf_client != NULL, NULL);
user_level_key = preferences_get_user_level_key ();
error = NULL;
user_level_changed_connection_id = gconf_client_notify_add (global_gconf_client,
user_level_key,
preferences_user_level_changed_notice,
NULL,
NULL,
&error);
g_free (user_level_key);
if (nautilus_gconf_handle_error (&error)) {
global_gconf_client = NULL;
return NULL;
}
path = preferences_get_path ();
nautilus_gconf_monitor_directory (path);
g_free (path);
g_atexit (preferences_global_client_remove_notification);
return global_gconf_client;
}
static gboolean
preferences_preference_is_default (const char *name)
{
gboolean result;
char *key;
g_return_val_if_fail (name != NULL, FALSE);
key = preferences_key_make (name);
result = nautilus_gconf_is_default (key);
g_free (key);
return result;
}
static char *
preferences_make_user_level_filtered_key (const char *name)
{
char *key;
g_return_val_if_fail (name != NULL, NULL);
if (nautilus_preferences_is_visible (name)) {
key = preferences_key_make (name);
} else {
key = preferences_key_make_for_default (name, nautilus_preferences_get_user_level ());
}
return key;
}
/* Public preferences functions */
int
nautilus_preferences_get_visible_user_level (const char *name)
{
int result;
char *visible_key;
g_return_val_if_fail (name != NULL, FALSE);
visible_key = preferences_key_make_for_visibility (name);
result = nautilus_gconf_get_integer (visible_key);
g_free (visible_key);
return result;
}
void
nautilus_preferences_set_visible_user_level (const char *name,
int visible_user_level)
{
char *visible_key;
g_return_if_fail (name != NULL);
visible_key = preferences_key_make_for_visibility (name);
nautilus_gconf_set_integer (visible_key, visible_user_level);
g_free (visible_key);
}
void
nautilus_preferences_set_boolean (const char *name,
gboolean boolean_value)
{
char *key;
g_return_if_fail (name != NULL);
key = preferences_key_make (name);
nautilus_gconf_set_boolean (key, boolean_value);
g_free (key);
nautilus_gconf_suggest_sync ();
}
static char *
preferences_key_make_for_getter (const char *name)
{
char *key;
g_return_val_if_fail (name != NULL, NULL);
if (preferences_preference_is_default (name) || !nautilus_preferences_is_visible (name)) {
key = preferences_key_make_for_default_getter (name, nautilus_preferences_get_user_level ());
} else {
key = preferences_make_user_level_filtered_key (name);
}
return key;
}
gboolean
nautilus_preferences_get_boolean (const char *name)
{
gboolean result;
char *key;
g_return_val_if_fail (name != NULL, FALSE);
key = preferences_key_make_for_getter (name);
result = nautilus_gconf_get_boolean (key);
g_free (key);
return result;
}
void
nautilus_preferences_set_integer (const char *name,
int int_value)
{
char *key;
int old_value;
g_return_if_fail (name != NULL);
key = preferences_key_make (name);
old_value = nautilus_preferences_get_integer (name);
if (int_value != old_value) {
nautilus_gconf_set_integer (key, int_value);
nautilus_gconf_suggest_sync ();
}
g_free (key);
}
int
nautilus_preferences_get_integer (const char *name)
{
int result;
char *key;
g_return_val_if_fail (name != NULL, 0);
key = preferences_key_make_for_getter (name);
result = nautilus_gconf_get_integer (key);
g_free (key);
return result;
}
void
nautilus_preferences_set (const char *name,
const char *string_value)
{
char *key;
char *old_value;
g_return_if_fail (name != NULL);
key = preferences_key_make (name);
old_value = nautilus_preferences_get (name);
if (strcmp (string_value, old_value) != 0) {
nautilus_gconf_set_string (key, string_value);
nautilus_gconf_suggest_sync ();
}
g_free (key);
}
char *
nautilus_preferences_get (const char *name)
{
char *result;
char *key;
g_return_val_if_fail (name != NULL, NULL);
key = preferences_key_make_for_getter (name);
result = nautilus_gconf_get_string (key);
g_free (key);
if (result == NULL) {
result = g_strdup ("");
}
return result;
}
void
nautilus_preferences_set_string_list (const char *name,
GList *string_list_value)
{
char *key;
g_return_if_fail (name != NULL);
key = preferences_key_make (name);
nautilus_gconf_set_string_list (key, string_list_value);
g_free (key);
nautilus_gconf_suggest_sync ();
}
GList *
nautilus_preferences_get_string_list (const char *name)
{
GList *result;
char *key;
g_return_val_if_fail (name != NULL, NULL);
key = preferences_key_make_for_getter (name);
result = nautilus_gconf_get_string_list (key);
g_free (key);
return result;
}
int
nautilus_preferences_get_user_level (void)
{
char *key;
char *user_level;
int result;
/* This is a little silly, but it is technically possible
* to have different user_level defaults in each user level.
*
* This is a consequence of using gconf to store the user
* level itself. So, we special case the "user_level" setting
* to always return the default for the first user level.
*/
if (preferences_preference_is_default ("user_level")) {
key = preferences_key_make_for_default ("user_level", 0);
} else {
key = preferences_key_make ("user_level");
}
user_level = nautilus_gconf_get_string (key);
g_free (key);
if (eel_str_is_equal (user_level, "advanced")) {
result = NAUTILUS_USER_LEVEL_ADVANCED;
} else if (eel_str_is_equal (user_level, "intermediate")) {
result = NAUTILUS_USER_LEVEL_INTERMEDIATE;
} else if (eel_str_is_equal (user_level, "novice")) {
result = NAUTILUS_USER_LEVEL_NOVICE;
} else {
result = DEFAULT_USER_LEVEL;
}
g_free (user_level);
return result;
}
void
nautilus_preferences_set_user_level (int user_level)
{
char *user_level_key;
user_level = preferences_user_level_check_range (user_level);
user_level_key = preferences_get_user_level_key ();
nautilus_gconf_set_string (user_level_key, user_level_names_for_storage[user_level]);
g_free (user_level_key);
nautilus_gconf_suggest_sync ();
}
void
nautilus_preferences_default_set_integer (const char *name,
int user_level,
int int_value)
{
char *default_key;
g_return_if_fail (name != NULL);
default_key = preferences_key_make_for_default (name, user_level);
nautilus_gconf_set_integer (default_key, int_value);
g_free (default_key);
}
int
nautilus_preferences_default_get_integer (const char *name,
int user_level)
{
int result;
char *default_key;
g_return_val_if_fail (name != NULL, 0);
default_key = preferences_key_make_for_default (name, user_level);
result = nautilus_gconf_get_integer (default_key);
g_free (default_key);
return result;
}
void
nautilus_preferences_default_set_boolean (const char *name,
int user_level,
gboolean boolean_value)
{
char *default_key;
g_return_if_fail (name != NULL);
default_key = preferences_key_make_for_default (name, user_level);
nautilus_gconf_set_boolean (default_key, boolean_value);
g_free (default_key);
}
gboolean
nautilus_preferences_default_get_boolean (const char *name,
int user_level)
{
gboolean result;
char *default_key;
g_return_val_if_fail (name != NULL, FALSE);
default_key = preferences_key_make_for_default (name, user_level);
result = nautilus_gconf_get_boolean (default_key);
g_free (default_key);
return result;
}
void
nautilus_preferences_default_set_string (const char *name,
int user_level,
const char *string_value)
{
char *default_key;
g_return_if_fail (name != NULL);
default_key = preferences_key_make_for_default (name, user_level);
nautilus_gconf_set_string (default_key, string_value);
g_free (default_key);
}
char *
nautilus_preferences_default_get_string (const char *name,
int user_level)
{
char *result;
char *default_key;
g_return_val_if_fail (name != NULL, NULL);
default_key = preferences_key_make_for_default (name, user_level);
result = nautilus_gconf_get_string (default_key);
g_free (default_key);
return result;
}
void
nautilus_preferences_default_set_string_list (const char *name,
int user_level,
GList *string_list_value)
{
char *default_key;
g_return_if_fail (name != NULL);
default_key = preferences_key_make_for_default (name, user_level);
nautilus_gconf_set_string_list (default_key, string_list_value);
g_free (default_key);
}
GList *
nautilus_preferences_default_get_string_list (const char *name,
int user_level)
{
GList *result;
char *default_key;
g_return_val_if_fail (name != NULL, NULL);
default_key = preferences_key_make_for_default (name, user_level);
result = nautilus_gconf_get_string_list (default_key);
g_free (default_key);
return result;
}
/**
* preferences_callback_entry_invoke_function
*
* A function that invokes a callback from the given struct. It is meant to be fed to
* g_list_foreach ()
* @data: The list data privately maintained by the GList.
* @callback_data: The callback_data privately maintained by the GList.
**/
static void
preferences_callback_entry_invoke_function (gpointer data,
gpointer callback_data)
{
PreferencesCallbackEntry *callback_entry;
g_return_if_fail (data != NULL);
callback_entry = data;
(* callback_entry->callback) (callback_entry->callback_data);
}
/**
* preferences_entry_invoke_callbacks_if_needed
*
* @entry: A PreferencesEntry
*
* This function checks the cached value in the entry with the current
* value of the preference. If the value has changed, then callbacks
* are invoked and auto storage updated.
*
* We need this check because even though the GConf value of a preference
* could indeed have changed, its representation on the Nautilus side
* of things could still be the same. The best example of this is
* user level changes, where the value of the preference on the Nautilus
* end of things is determined by visibility.
**/
static void
preferences_entry_invoke_callbacks_if_needed (PreferencesEntry *entry)
{
GConfValue *new_value;
char *getter_key;
g_return_if_fail (entry != NULL);
getter_key = preferences_key_make_for_getter (entry->name);
new_value = nautilus_gconf_get_value (getter_key);
g_free (getter_key);
/* If the values are the same, then we dont need to invoke any callbacks */
if (nautilus_gconf_value_is_equal (entry->cached_value, new_value)) {
nautilus_gconf_value_free (new_value);
return;
}
/* Update the auto storage preferences */
if (entry->auto_storage_list != NULL) {
preferences_entry_update_auto_storage (entry);
}
/* Store the new cached value */
nautilus_gconf_value_free (entry->cached_value);
entry->cached_value = new_value;
/* Invoke callbacks for this entry if any */
if (entry->callback_list != NULL) {
g_list_foreach (entry->callback_list,
preferences_callback_entry_invoke_function,
NULL);
}
}
static void
update_auto_string (gpointer data, gpointer callback_data)
{
char **storage;
const char *value;
g_return_if_fail (data != NULL);
g_return_if_fail (callback_data != NULL);
storage = (char **)data;
value = (const char *)callback_data;
g_free (*storage);
*(char **)storage = g_strdup (value);
}
static void
update_auto_integer_or_boolean (gpointer data, gpointer callback_data)
{
g_return_if_fail (data != NULL);
*(int *)data = GPOINTER_TO_INT (callback_data);
}
static void
preferences_entry_update_auto_storage (PreferencesEntry *entry)
{
char *new_string_value;
int new_int_value;
gboolean new_boolean_value;
switch (entry->type) {
case PREFERENCE_STRING:
new_string_value = nautilus_preferences_get (entry->name);
g_list_foreach (entry->auto_storage_list,
update_auto_string,
new_string_value);
g_free (new_string_value);
break;
case PREFERENCE_INTEGER:
new_int_value = nautilus_preferences_get_integer (entry->name);
g_list_foreach (entry->auto_storage_list,
update_auto_integer_or_boolean,
GINT_TO_POINTER (new_int_value));
break;
case PREFERENCE_BOOLEAN:
new_boolean_value = nautilus_preferences_get_boolean (entry->name);
g_list_foreach (entry->auto_storage_list,
update_auto_integer_or_boolean,
GINT_TO_POINTER (new_boolean_value));
break;
default:
g_warning ("unexpected preferences type %d in preferences_entry_update_auto_storage", entry->type);
}
}
static void
preferences_something_changed_notice (GConfClient *client,
guint connection_id,
GConfEntry *entry,
gpointer notice_data)
{
g_return_if_fail (entry != NULL);
g_return_if_fail (entry->key != NULL);
g_return_if_fail (notice_data != NULL);
preferences_entry_invoke_callbacks_if_needed (notice_data);
}
static void
preferences_global_table_check_changes_function (gpointer key,
gpointer value,
gpointer user_data)
{
PreferencesEntry *entry;
g_return_if_fail (key != NULL);
g_return_if_fail (value != NULL);
entry = value;
g_return_if_fail (entry->name != NULL);
/* We dont worry about the 'user_level' itself for recursive reasons */
if (preferences_preference_is_user_level (entry->name)) {
return;
}
preferences_entry_invoke_callbacks_if_needed (entry);
}
static void
preferences_entry_update_cached_value (PreferencesEntry *entry)
{
char *getter_key;
g_return_if_fail (entry != NULL);
nautilus_gconf_value_free (entry->cached_value);
getter_key = preferences_key_make_for_getter (entry->name);
entry->cached_value = nautilus_gconf_get_value (getter_key);
g_free (getter_key);
}
static void
preferences_user_level_changed_notice (GConfClient *client,
guint connection_id,
GConfEntry *gconf_entry,
gpointer user_data)
{
g_return_if_fail (gconf_entry != NULL);
g_return_if_fail (gconf_entry->key != NULL);
g_return_if_fail (eel_str_has_suffix (gconf_entry->key, "user_level"));
g_hash_table_foreach (preferences_global_table_get_global (),
preferences_global_table_check_changes_function,
NULL);
}
static void
preferences_entry_ensure_gconf_connection (PreferencesEntry *entry)
{
GError *error;
GConfClient *client;
char *key;
/*
* We install only one gconf notification for each preference entry.
* Otherwise, we would invoke the installed callbacks more than once
* per registered callback.
*/
if (entry->gconf_connection_id != 0) {
return;
}
g_return_if_fail (entry->name != NULL);
client = preferences_global_client_get ();
g_return_if_fail (client != NULL);
key = preferences_key_make (entry->name);
error = NULL;
entry->gconf_connection_id = gconf_client_notify_add (client,
key,
preferences_something_changed_notice,
entry,
NULL,
&error);
if (nautilus_gconf_handle_error (&error)) {
entry->gconf_connection_id = 0;
}
g_free (key);
/* Update the cached value.
* From now onwards the cached value will be updated
* each time preferences_something_changed_notice() triggers
* so that it can be later compared with new values to
* determine if the gconf value is different from the
* Nautilus value.
*/
preferences_entry_update_cached_value (entry);
}
/**
* preferences_entry_add_callback
*
* Add a callback to a pref node. Callbacks are fired whenever
* the pref value changes.
* @preferences_entry: The hash node.
* @callback: The user-supplied callback.
* @callback_data: The user-supplied closure.
**/
static void
preferences_entry_add_callback (PreferencesEntry *entry,
NautilusPreferencesCallback callback,
gpointer callback_data)
{
PreferencesCallbackEntry *callback_entry;
g_return_if_fail (entry != NULL);
g_return_if_fail (callback != NULL);
callback_entry = g_new0 (PreferencesCallbackEntry, 1);
callback_entry->callback = callback;
callback_entry->callback_data = callback_data;
g_return_if_fail (callback_entry != NULL);
entry->callback_list = g_list_append (entry->callback_list, callback_entry);
preferences_entry_ensure_gconf_connection (entry);
}
/**
* preferences_entry_add_auto_storage
*
* Add an auto-storage variable to a pref node. The variable will
* be updated to match the pref value whenever the pref
* the pref value changes.
* @preferences_entry: The hash node.
* @storage: The user-supplied location at which to store the value.
* @type: Which type of variable this is.
**/
static void
preferences_entry_add_auto_storage (PreferencesEntry *entry,
gpointer storage,
PreferenceType type)
{
g_return_if_fail (entry != NULL);
g_return_if_fail (storage != NULL);
g_return_if_fail (entry->type == 0 || entry->type == type);
g_return_if_fail (g_list_find (entry->auto_storage_list, storage) == NULL);
entry->type = type;
entry->auto_storage_list = g_list_append (entry->auto_storage_list, storage);
preferences_entry_ensure_gconf_connection (entry);
}
static void
preferences_entry_check_remove_connection (PreferencesEntry *entry)
{
GConfClient *client;
/*
* If there are no callbacks or auto-storage variables left in the entry,
* remove the gconf notification.
*/
if (entry->callback_list != NULL || entry->auto_storage_list != NULL) {
return;
}
client = preferences_global_client_get ();
if (entry->gconf_connection_id != 0) {
gconf_client_notify_remove (client, entry->gconf_connection_id);
}
entry->gconf_connection_id = 0;
}
/**
* preferences_entry_remove_callback
*
* remove a callback from a pref entry. Both the callback and the callback_data must
* match in order for a callback to be removed from the entry.
* @preferences_entry: The hash entry.
* @callback: The user-supplied callback.
* @callback_data: The user-supplied closure.
**/
static void
preferences_entry_remove_callback (PreferencesEntry *entry,
NautilusPreferencesCallback callback,
gpointer callback_data)
{
GList *new_list;
GList *iterator;
g_return_if_fail (entry != NULL);
g_return_if_fail (callback != NULL);
g_return_if_fail (entry->callback_list != NULL);
new_list = g_list_copy (entry->callback_list);
for (iterator = new_list; iterator != NULL; iterator = iterator->next) {
PreferencesCallbackEntry *callback_entry = iterator->data;
g_return_if_fail (callback_entry != NULL);
if (callback_entry->callback == callback &&
callback_entry->callback_data == callback_data) {
entry->callback_list = g_list_remove (entry->callback_list,
callback_entry);
preferences_callback_entry_free (callback_entry);
}
}
g_list_free (new_list);
preferences_entry_check_remove_connection (entry);
}
/**
* preferences_entry_remove_auto_storage
*
* remove an auto-storage variable from a pref entry.
* @preferences_entry: The hash entry.
* @storage: The user-supplied location.
**/
static void
preferences_entry_remove_auto_storage (PreferencesEntry *entry,
gpointer storage)
{
GList *new_list;
GList *iterator;
gpointer storage_in_entry;
g_return_if_fail (entry != NULL);
g_return_if_fail (storage != NULL);
g_return_if_fail (entry->auto_storage_list != NULL);
new_list = g_list_copy (entry->auto_storage_list);
for (iterator = new_list; iterator != NULL; iterator = iterator->next) {
storage_in_entry = iterator->data;
g_return_if_fail (storage_in_entry != NULL);
if (storage_in_entry == storage) {
entry->auto_storage_list = g_list_remove (entry->auto_storage_list,
storage);
switch (entry->type) {
case PREFERENCE_STRING:
update_auto_string (storage, NULL);
break;
case PREFERENCE_BOOLEAN:
case PREFERENCE_INTEGER:
update_auto_integer_or_boolean (storage, 0);
break;
default:
g_warning ("unexpected preference type %d in preferences_entry_remove_auto_storage", entry->type);
}
}
}
g_list_free (new_list);
preferences_entry_check_remove_connection (entry);
}
/**
* preferences_callback_entry_free
*
* Free a callback info struct.
* @preferences_callback_entry: The struct to free.
**/
static void
preferences_callback_entry_free (PreferencesCallbackEntry *callback_entry)
{
g_return_if_fail (callback_entry != NULL);
callback_entry->callback = NULL;
callback_entry->callback_data = NULL;
g_free (callback_entry);
}
/**
* preferences_callback_entry_free_func
*
* A function that frees a callback info struct. It is meant to be fed to
* g_list_foreach ()
* @data: The list data privately maintained by the GList.
* @callback_data: The callback_data privately maintained by the GList.
**/
static void
preferences_callback_entry_free_func (gpointer data,
gpointer callback_data)
{
g_return_if_fail (data != NULL);
preferences_callback_entry_free (data);
}
/**
* preferences_entry_free
*
* Free a preference hash node's members along with the node itself.
* @preferences_hash_node: The node to free.
**/
static void
preferences_entry_free (PreferencesEntry *entry)
{
g_return_if_fail (entry != NULL);
if (entry->gconf_connection_id != 0) {
GConfClient *client;
client = preferences_global_client_get ();
g_assert (client != NULL);
gconf_client_notify_remove (client, entry->gconf_connection_id);
entry->gconf_connection_id = 0;
}
g_list_free (entry->auto_storage_list);
eel_g_list_free_deep_custom (entry->callback_list,
preferences_callback_entry_free_func,
NULL);
entry->auto_storage_list = NULL;
entry->callback_list = NULL;
g_free (entry->name);
g_free (entry->description);
nautilus_gconf_value_free (entry->cached_value);
eel_enumeration_free (entry->enumeration);
g_free (entry);
}
/**
* preferences_entry_free_func
*
* A function that frees a pref hash node. It is meant to be fed to
* g_hash_table_foreach ()
* @key: The hash key privately maintained by the GHashTable.
* @value: The hash value privately maintained by the GHashTable.
* @callback_data: The callback_data privately maintained by the GHashTable.
**/
static void
preferences_entry_free_func (gpointer key,
gpointer value,
gpointer callback_data)
{
g_assert (value != NULL);
preferences_entry_free (value);
}
static void
preferences_global_table_free (void)
{
if (global_table == NULL) {
return;
}
g_hash_table_foreach (global_table, preferences_entry_free_func, NULL);
g_hash_table_destroy (global_table);
global_table = NULL;
}
static GHashTable *
preferences_global_table_get_global (void)
{
if (global_table == NULL) {
global_table = g_hash_table_new (g_str_hash, g_str_equal);
g_atexit (preferences_global_table_free);
}
return global_table;
}
static PreferencesEntry *
preferences_global_table_lookup (const char *name)
{
g_return_val_if_fail (name != NULL, NULL);
g_return_val_if_fail (preferences_global_table_get_global () != NULL, NULL);
return g_hash_table_lookup (preferences_global_table_get_global (), name);
}
static PreferencesEntry *
preferences_global_table_insert (const char *name)
{
PreferencesEntry *entry;
g_return_val_if_fail (name != NULL, NULL);
g_return_val_if_fail (preferences_global_table_get_global () != NULL, NULL);
g_return_val_if_fail (preferences_global_table_lookup (name) == NULL, NULL);
entry = g_new0 (PreferencesEntry, 1);
entry->name = g_strdup (name);
g_hash_table_insert (preferences_global_table_get_global (), entry->name, entry);
g_return_val_if_fail (entry == preferences_global_table_lookup (name), NULL);
/* Update the cached value for the first time.
*
* We need to do this because checks for value changes
* happen not only as a result of callbacks triggering, but
* also as a result of user_level changes. When a user level
* changes, all the preferences entries are iterated to invoke
* callbacks for those that changed as a result.
*
* See preferences_global_table_check_changes_function().
*/
preferences_entry_update_cached_value (entry);
return entry;
}
static PreferencesEntry *
preferences_global_table_lookup_or_insert (const char *name)
{
PreferencesEntry *entry;
g_return_val_if_fail (name != NULL, NULL);
entry = preferences_global_table_lookup (name);
if (entry != NULL) {
return entry;
}
entry = preferences_global_table_insert (name);
g_assert (entry != NULL);
return entry;
}
void
nautilus_preferences_add_callback (const char *name,
NautilusPreferencesCallback callback,
gpointer callback_data)
{
PreferencesEntry *entry;
g_return_if_fail (name != NULL);
g_return_if_fail (callback != NULL);
entry = preferences_global_table_lookup_or_insert (name);
g_assert (entry != NULL);
preferences_entry_add_callback (entry, callback, callback_data);
}
void
nautilus_preferences_add_auto_string (const char *name,
const char **storage)
{
PreferencesEntry *entry;
char *value;
g_return_if_fail (name != NULL);
g_return_if_fail (storage != NULL);
entry = preferences_global_table_lookup_or_insert (name);
g_assert (entry != NULL);
preferences_entry_add_auto_storage (entry, storage, PREFERENCE_STRING);
value = nautilus_preferences_get (entry->name);
update_auto_string (storage, value);
g_free (value);
}
void
nautilus_preferences_add_auto_integer (const char *name,
int *storage)
{
PreferencesEntry *entry;
int value;
g_return_if_fail (name != NULL);
g_return_if_fail (storage != NULL);
entry = preferences_global_table_lookup_or_insert (name);
g_assert (entry != NULL);
preferences_entry_add_auto_storage (entry, storage, PREFERENCE_INTEGER);
value = nautilus_preferences_get_integer (entry->name);
update_auto_integer_or_boolean (storage, GINT_TO_POINTER (value));
}
void
nautilus_preferences_add_auto_boolean (const char *name,
gboolean *storage)
{
PreferencesEntry *entry;
gboolean value;
g_return_if_fail (name != NULL);
g_return_if_fail (storage != NULL);
entry = preferences_global_table_lookup_or_insert (name);
g_assert (entry != NULL);
preferences_entry_add_auto_storage (entry, storage, PREFERENCE_BOOLEAN);
value = nautilus_preferences_get_boolean (entry->name);
update_auto_integer_or_boolean (storage, GINT_TO_POINTER (value));
}
void
nautilus_preferences_remove_auto_string (const char *name,
const char **storage)
{
PreferencesEntry *entry;
g_return_if_fail (name != NULL);
g_return_if_fail (storage != NULL);
entry = preferences_global_table_lookup (name);
if (entry == NULL) {
g_warning ("Trying to remove auto-string for %s without adding it first.", name);
return;
}
preferences_entry_remove_auto_storage (entry, storage);
}
void
nautilus_preferences_remove_auto_integer (const char *name,
int *storage)
{
PreferencesEntry *entry;
g_return_if_fail (name != NULL);
g_return_if_fail (storage != NULL);
entry = preferences_global_table_lookup (name);
if (entry == NULL) {
g_warning ("Trying to remove auto-integer for %s without adding it first.", name);
return;
}
preferences_entry_remove_auto_storage (entry, storage);
}
void
nautilus_preferences_remove_auto_boolean (const char *name,
gboolean *storage)
{
PreferencesEntry *entry;
g_return_if_fail (name != NULL);
g_return_if_fail (storage != NULL);
entry = preferences_global_table_lookup (name);
if (entry == NULL) {
g_warning ("Trying to remove auto-boolean for %s without adding it first.", name);
return;
}
preferences_entry_remove_auto_storage (entry, storage);
}
typedef struct
{
char *name;
NautilusPreferencesCallback callback;
gpointer callback_data;
} WhileAliveData;
static void
preferences_while_alive_disconnector (GtkObject *object, gpointer callback_data)
{
WhileAliveData *data;
g_return_if_fail (GTK_IS_OBJECT (object));
g_return_if_fail (callback_data != NULL);
data = callback_data;
nautilus_preferences_remove_callback (data->name,
data->callback,
data->callback_data);
g_free (data->name);
g_free (data);
}
void
nautilus_preferences_add_callback_while_alive (const char *name,
NautilusPreferencesCallback callback,
gpointer callback_data,
GtkObject *alive_object)
{
WhileAliveData *data;
g_return_if_fail (name != NULL);
g_return_if_fail (callback != NULL);
g_return_if_fail (GTK_IS_OBJECT (alive_object));
data = g_new (WhileAliveData, 1);
data->name = g_strdup (name);
data->callback = callback;
data->callback_data = callback_data;
nautilus_preferences_add_callback (name, callback, callback_data);
gtk_signal_connect (alive_object,
"destroy",
GTK_SIGNAL_FUNC (preferences_while_alive_disconnector),
data);
}
void
nautilus_preferences_remove_callback (const char *name,
NautilusPreferencesCallback callback,
gpointer callback_data)
{
PreferencesEntry *entry;
g_return_if_fail (name != NULL);
g_return_if_fail (callback != NULL);
entry = preferences_global_table_lookup (name);
if (entry == NULL) {
g_warning ("Trying to remove a callback for %s without adding it first.", name);
return;
}
preferences_entry_remove_callback (entry, callback, callback_data);
}
void
nautilus_preferences_enumeration_insert (const char *name,
const char *entry,
const char *description,
int value)
{
PreferencesEntry *preferences_entry;
g_return_if_fail (name != NULL);
g_return_if_fail (entry != NULL);
preferences_entry = preferences_global_table_lookup_or_insert (name);
g_assert (preferences_entry != NULL);
if (preferences_entry->enumeration == NULL) {
preferences_entry->enumeration = eel_enumeration_new ();
}
eel_enumeration_insert (preferences_entry->enumeration, entry, description, value);
}
char *
nautilus_preferences_enumeration_get_nth_entry (const char *name,
guint n)
{
PreferencesEntry *preferences_entry;
g_return_val_if_fail (name != NULL, NULL);
preferences_entry = preferences_global_table_lookup_or_insert (name);
g_assert (preferences_entry != NULL);
return eel_enumeration_get_nth_entry (preferences_entry->enumeration, n);
}
char *
nautilus_preferences_enumeration_get_nth_description (const char *name,
guint n)
{
PreferencesEntry *preferences_entry;
g_return_val_if_fail (name != NULL, NULL);
preferences_entry = preferences_global_table_lookup_or_insert (name);
g_assert (preferences_entry != NULL);
return eel_enumeration_get_nth_description (preferences_entry->enumeration, n);
}
int
nautilus_preferences_enumeration_get_nth_value (const char *name,
guint n)
{
PreferencesEntry *preferences_entry;
g_return_val_if_fail (name != NULL, 0);
preferences_entry = preferences_global_table_lookup_or_insert (name);
g_assert (preferences_entry != NULL);
return eel_enumeration_get_nth_value (preferences_entry->enumeration, n);
}
guint
nautilus_preferences_enumeration_get_num_entries (const char *name)
{
PreferencesEntry *preferences_entry;
g_return_val_if_fail (name != NULL, 0);
preferences_entry = preferences_global_table_lookup_or_insert (name);
g_assert (preferences_entry != NULL);
return eel_enumeration_get_num_entries (preferences_entry->enumeration);
}
void
nautilus_preferences_set_description (const char *name,
const char *description)
{
PreferencesEntry *entry;
g_return_if_fail (name != NULL);
g_return_if_fail (description != NULL);
entry = preferences_global_table_lookup_or_insert (name);
g_assert (entry != NULL);
g_free (entry->description);
entry->description = g_strdup (description);
}
char *
nautilus_preferences_get_description (const char *name)
{
PreferencesEntry *entry;
g_return_val_if_fail (name != NULL, NULL);
entry = preferences_global_table_lookup_or_insert (name);
return g_strdup (entry->description ? entry->description : "");
}
char *
nautilus_preferences_get_user_level_name_for_display (int user_level)
{
user_level = preferences_user_level_check_range (user_level);
return g_strdup (gettext (user_level_names_for_display[user_level]));
}
char *
nautilus_preferences_get_user_level_name_for_storage (int user_level)
{
user_level = preferences_user_level_check_range (user_level);
return g_strdup (user_level_names_for_storage[user_level]);
}
static int
preferences_user_level_check_range (int user_level)
{
user_level = MAX (user_level, 0);
user_level = MIN (user_level, 2);
return user_level;
}
gboolean
nautilus_preferences_monitor_directory (const char *directory)
{
return nautilus_gconf_monitor_directory (directory);
}
gboolean
nautilus_preferences_is_visible (const char *name)
{
int user_level;
int visible_user_level;
g_return_val_if_fail (name != NULL, FALSE);
user_level = nautilus_preferences_get_user_level ();
visible_user_level = nautilus_preferences_get_visible_user_level (name);
return visible_user_level <= user_level;
}
#if !defined (NAUTILUS_OMIT_SELF_CHECK)
void
nautilus_self_check_preferences (void)
{
}
#endif /* !NAUTILUS_OMIT_SELF_CHECK */
|