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 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933
|
/*
* Copyright © 2010 Codethink Limited
* Copyright © 2011 Canonical Limited
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include "glib-private.h"
#include "gsettingsschema-internal.h"
#include "gsettings.h"
#include "gvdb/gvdb-reader.h"
#include "strinfo.c"
#include <glibintl.h>
#include <locale.h>
#include <string.h>
#include <stdlib.h>
#ifdef HAVE_XLOCALE_H
/* Needed on macOS and FreeBSD for uselocale() */
#include <xlocale.h>
#endif
/**
* SECTION:gsettingsschema
* @short_description: Introspecting and controlling the loading
* of GSettings schemas
* @include: gio/gio.h
*
* The #GSettingsSchemaSource and #GSettingsSchema APIs provide a
* mechanism for advanced control over the loading of schemas and a
* mechanism for introspecting their content.
*
* Plugin loading systems that wish to provide plugins a way to access
* settings face the problem of how to make the schemas for these
* settings visible to GSettings. Typically, a plugin will want to ship
* the schema along with itself and it won't be installed into the
* standard system directories for schemas.
*
* #GSettingsSchemaSource provides a mechanism for dealing with this by
* allowing the creation of a new 'schema source' from which schemas can
* be acquired. This schema source can then become part of the metadata
* associated with the plugin and queried whenever the plugin requires
* access to some settings.
*
* Consider the following example:
*
* |[<!-- language="C" -->
* typedef struct
* {
* ...
* GSettingsSchemaSource *schema_source;
* ...
* } Plugin;
*
* Plugin *
* initialise_plugin (const gchar *dir)
* {
* Plugin *plugin;
*
* ...
*
* plugin->schema_source =
* g_settings_schema_source_new_from_directory (dir,
* g_settings_schema_source_get_default (), FALSE, NULL);
*
* ...
*
* return plugin;
* }
*
* ...
*
* GSettings *
* plugin_get_settings (Plugin *plugin,
* const gchar *schema_id)
* {
* GSettingsSchema *schema;
*
* if (schema_id == NULL)
* schema_id = plugin->identifier;
*
* schema = g_settings_schema_source_lookup (plugin->schema_source,
* schema_id, FALSE);
*
* if (schema == NULL)
* {
* ... disable the plugin or abort, etc ...
* }
*
* return g_settings_new_full (schema, NULL, NULL);
* }
* ]|
*
* The code above shows how hooks should be added to the code that
* initialises (or enables) the plugin to create the schema source and
* how an API can be added to the plugin system to provide a convenient
* way for the plugin to access its settings, using the schemas that it
* ships.
*
* From the standpoint of the plugin, it would need to ensure that it
* ships a gschemas.compiled file as part of itself, and then simply do
* the following:
*
* |[<!-- language="C" -->
* {
* GSettings *settings;
* gint some_value;
*
* settings = plugin_get_settings (self, NULL);
* some_value = g_settings_get_int (settings, "some-value");
* ...
* }
* ]|
*
* It's also possible that the plugin system expects the schema source
* files (ie: .gschema.xml files) instead of a gschemas.compiled file.
* In that case, the plugin loading system must compile the schemas for
* itself before attempting to create the settings source.
*
* Since: 2.32
**/
/**
* GSettingsSchemaKey:
*
* #GSettingsSchemaKey is an opaque data structure and can only be accessed
* using the following functions.
**/
/**
* GSettingsSchema:
*
* This is an opaque structure type. You may not access it directly.
*
* Since: 2.32
**/
struct _GSettingsSchema
{
GSettingsSchemaSource *source;
const gchar *gettext_domain;
const gchar *path;
GQuark *items;
gint n_items;
GvdbTable *table;
gchar *id;
GSettingsSchema *extends;
gint ref_count;
};
/**
* G_TYPE_SETTINGS_SCHEMA_SOURCE:
*
* A boxed #GType corresponding to #GSettingsSchemaSource.
*
* Since: 2.32
**/
G_DEFINE_BOXED_TYPE (GSettingsSchemaSource, g_settings_schema_source, g_settings_schema_source_ref, g_settings_schema_source_unref)
/**
* G_TYPE_SETTINGS_SCHEMA:
*
* A boxed #GType corresponding to #GSettingsSchema.
*
* Since: 2.32
**/
G_DEFINE_BOXED_TYPE (GSettingsSchema, g_settings_schema, g_settings_schema_ref, g_settings_schema_unref)
/**
* GSettingsSchemaSource:
*
* This is an opaque structure type. You may not access it directly.
*
* Since: 2.32
**/
struct _GSettingsSchemaSource
{
GSettingsSchemaSource *parent;
gchar *directory;
GvdbTable *table;
GHashTable **text_tables;
gint ref_count;
};
static GSettingsSchemaSource *schema_sources;
/**
* g_settings_schema_source_ref:
* @source: a #GSettingsSchemaSource
*
* Increase the reference count of @source, returning a new reference.
*
* Returns: (transfer full) (not nullable): a new reference to @source
*
* Since: 2.32
**/
GSettingsSchemaSource *
g_settings_schema_source_ref (GSettingsSchemaSource *source)
{
g_atomic_int_inc (&source->ref_count);
return source;
}
/**
* g_settings_schema_source_unref:
* @source: a #GSettingsSchemaSource
*
* Decrease the reference count of @source, possibly freeing it.
*
* Since: 2.32
**/
void
g_settings_schema_source_unref (GSettingsSchemaSource *source)
{
if (g_atomic_int_dec_and_test (&source->ref_count))
{
if (source == schema_sources)
g_error ("g_settings_schema_source_unref() called too many times on the default schema source");
if (source->parent)
g_settings_schema_source_unref (source->parent);
gvdb_table_free (source->table);
g_free (source->directory);
if (source->text_tables)
{
g_hash_table_unref (source->text_tables[0]);
g_hash_table_unref (source->text_tables[1]);
g_free (source->text_tables);
}
g_slice_free (GSettingsSchemaSource, source);
}
}
/**
* g_settings_schema_source_new_from_directory:
* @directory: (type filename): the filename of a directory
* @parent: (nullable): a #GSettingsSchemaSource, or %NULL
* @trusted: %TRUE, if the directory is trusted
* @error: a pointer to a #GError pointer set to %NULL, or %NULL
*
* Attempts to create a new schema source corresponding to the contents
* of the given directory.
*
* This function is not required for normal uses of #GSettings but it
* may be useful to authors of plugin management systems.
*
* The directory should contain a file called `gschemas.compiled` as
* produced by the [glib-compile-schemas][glib-compile-schemas] tool.
*
* If @trusted is %TRUE then `gschemas.compiled` is trusted not to be
* corrupted. This assumption has a performance advantage, but can result
* in crashes or inconsistent behaviour in the case of a corrupted file.
* Generally, you should set @trusted to %TRUE for files installed by the
* system and to %FALSE for files in the home directory.
*
* In either case, an empty file or some types of corruption in the file will
* result in %G_FILE_ERROR_INVAL being returned.
*
* If @parent is non-%NULL then there are two effects.
*
* First, if g_settings_schema_source_lookup() is called with the
* @recursive flag set to %TRUE and the schema can not be found in the
* source, the lookup will recurse to the parent.
*
* Second, any references to other schemas specified within this
* source (ie: `child` or `extends`) references may be resolved
* from the @parent.
*
* For this second reason, except in very unusual situations, the
* @parent should probably be given as the default schema source, as
* returned by g_settings_schema_source_get_default().
*
* Since: 2.32
**/
GSettingsSchemaSource *
g_settings_schema_source_new_from_directory (const gchar *directory,
GSettingsSchemaSource *parent,
gboolean trusted,
GError **error)
{
GSettingsSchemaSource *source;
GvdbTable *table;
gchar *filename;
filename = g_build_filename (directory, "gschemas.compiled", NULL);
table = gvdb_table_new (filename, trusted, error);
g_free (filename);
if (table == NULL)
return NULL;
source = g_slice_new (GSettingsSchemaSource);
source->directory = g_strdup (directory);
source->parent = parent ? g_settings_schema_source_ref (parent) : NULL;
source->text_tables = NULL;
source->table = table;
source->ref_count = 1;
return source;
}
static void
try_prepend_dir (const gchar *directory)
{
GSettingsSchemaSource *source;
source = g_settings_schema_source_new_from_directory (directory, schema_sources, TRUE, NULL);
/* If we successfully created it then prepend it to the global list */
if (source != NULL)
schema_sources = source;
}
static void
try_prepend_data_dir (const gchar *directory)
{
gchar *dirname = g_build_filename (directory, "glib-2.0", "schemas", NULL);
try_prepend_dir (dirname);
g_free (dirname);
}
static void
initialise_schema_sources (void)
{
static gsize initialised;
/* need a separate variable because 'schema_sources' may legitimately
* be null if we have zero valid schema sources
*/
if G_UNLIKELY (g_once_init_enter (&initialised))
{
gboolean is_setuid = GLIB_PRIVATE_CALL (g_check_setuid) ();
const gchar * const *dirs;
const gchar *path;
gchar **extra_schema_dirs;
gint i;
/* iterate in reverse: count up, then count down */
dirs = g_get_system_data_dirs ();
for (i = 0; dirs[i]; i++);
while (i--)
try_prepend_data_dir (dirs[i]);
try_prepend_data_dir (g_get_user_data_dir ());
/* Disallow loading extra schemas if running as setuid, as that could
* allow reading privileged files. */
if (!is_setuid && (path = g_getenv ("GSETTINGS_SCHEMA_DIR")) != NULL)
{
extra_schema_dirs = g_strsplit (path, G_SEARCHPATH_SEPARATOR_S, 0);
for (i = 0; extra_schema_dirs[i]; i++);
while (i--)
try_prepend_dir (extra_schema_dirs[i]);
g_strfreev (extra_schema_dirs);
}
g_once_init_leave (&initialised, TRUE);
}
}
/**
* g_settings_schema_source_get_default:
*
* Gets the default system schema source.
*
* This function is not required for normal uses of #GSettings but it
* may be useful to authors of plugin management systems or to those who
* want to introspect the content of schemas.
*
* If no schemas are installed, %NULL will be returned.
*
* The returned source may actually consist of multiple schema sources
* from different directories, depending on which directories were given
* in `XDG_DATA_DIRS` and `GSETTINGS_SCHEMA_DIR`. For this reason, all
* lookups performed against the default source should probably be done
* recursively.
*
* Returns: (transfer none) (nullable): the default schema source
*
* Since: 2.32
**/
GSettingsSchemaSource *
g_settings_schema_source_get_default (void)
{
initialise_schema_sources ();
return schema_sources;
}
/**
* g_settings_schema_source_lookup:
* @source: a #GSettingsSchemaSource
* @schema_id: a schema ID
* @recursive: %TRUE if the lookup should be recursive
*
* Looks up a schema with the identifier @schema_id in @source.
*
* This function is not required for normal uses of #GSettings but it
* may be useful to authors of plugin management systems or to those who
* want to introspect the content of schemas.
*
* If the schema isn't found directly in @source and @recursive is %TRUE
* then the parent sources will also be checked.
*
* If the schema isn't found, %NULL is returned.
*
* Returns: (nullable) (transfer full): a new #GSettingsSchema
*
* Since: 2.32
**/
GSettingsSchema *
g_settings_schema_source_lookup (GSettingsSchemaSource *source,
const gchar *schema_id,
gboolean recursive)
{
GSettingsSchema *schema;
GvdbTable *table;
const gchar *extends;
g_return_val_if_fail (source != NULL, NULL);
g_return_val_if_fail (schema_id != NULL, NULL);
table = gvdb_table_get_table (source->table, schema_id);
if (table == NULL && recursive)
for (source = source->parent; source; source = source->parent)
if ((table = gvdb_table_get_table (source->table, schema_id)))
break;
if (table == NULL)
return NULL;
schema = g_slice_new0 (GSettingsSchema);
schema->source = g_settings_schema_source_ref (source);
schema->ref_count = 1;
schema->id = g_strdup (schema_id);
schema->table = table;
schema->path = g_settings_schema_get_string (schema, ".path");
schema->gettext_domain = g_settings_schema_get_string (schema, ".gettext-domain");
if (schema->gettext_domain)
bind_textdomain_codeset (schema->gettext_domain, "UTF-8");
extends = g_settings_schema_get_string (schema, ".extends");
if (extends)
{
schema->extends = g_settings_schema_source_lookup (source, extends, TRUE);
if (schema->extends == NULL)
g_warning ("Schema '%s' extends schema '%s' but we could not find it", schema_id, extends);
}
return schema;
}
typedef struct
{
GHashTable *summaries;
GHashTable *descriptions;
GSList *gettext_domain;
GSList *schema_id;
GSList *key_name;
GString *string;
} TextTableParseInfo;
static const gchar *
get_attribute_value (GSList *list)
{
GSList *node;
for (node = list; node; node = node->next)
if (node->data)
return node->data;
return NULL;
}
static void
pop_attribute_value (GSList **list)
{
gchar *top;
top = (*list)->data;
*list = g_slist_remove (*list, top);
g_free (top);
}
static void
push_attribute_value (GSList **list,
const gchar *value)
{
*list = g_slist_prepend (*list, g_strdup (value));
}
static void
start_element (GMarkupParseContext *context,
const gchar *element_name,
const gchar **attribute_names,
const gchar **attribute_values,
gpointer user_data,
GError **error)
{
TextTableParseInfo *info = user_data;
const gchar *gettext_domain = NULL;
const gchar *schema_id = NULL;
const gchar *key_name = NULL;
gint i;
for (i = 0; attribute_names[i]; i++)
{
if (g_str_equal (attribute_names[i], "gettext-domain"))
gettext_domain = attribute_values[i];
else if (g_str_equal (attribute_names[i], "id"))
schema_id = attribute_values[i];
else if (g_str_equal (attribute_names[i], "name"))
key_name = attribute_values[i];
}
push_attribute_value (&info->gettext_domain, gettext_domain);
push_attribute_value (&info->schema_id, schema_id);
push_attribute_value (&info->key_name, key_name);
if (info->string)
{
g_string_free (info->string, TRUE);
info->string = NULL;
}
if (g_str_equal (element_name, "summary") || g_str_equal (element_name, "description"))
info->string = g_string_new (NULL);
}
static gchar *
normalise_whitespace (const gchar *orig)
{
/* We normalise by the same rules as in intltool:
*
* sub cleanup {
* s/^\s+//;
* s/\s+$//;
* s/\s+/ /g;
* return $_;
* }
*
* $message = join "\n\n", map &cleanup, split/\n\s*\n+/, $message;
*
* Where \s is an ascii space character.
*
* We aim for ease of implementation over efficiency -- this code is
* not run in normal applications.
*/
static GRegex *cleanup[3];
static GRegex *splitter;
gchar **lines;
gchar *result;
gint i;
if (g_once_init_enter (&splitter))
{
GRegex *s;
cleanup[0] = g_regex_new ("^\\s+", G_REGEX_DEFAULT,
G_REGEX_MATCH_DEFAULT, NULL);
cleanup[1] = g_regex_new ("\\s+$", G_REGEX_DEFAULT,
G_REGEX_MATCH_DEFAULT, NULL);
cleanup[2] = g_regex_new ("\\s+", G_REGEX_DEFAULT,
G_REGEX_MATCH_DEFAULT, NULL);
s = g_regex_new ("\\n\\s*\\n+", G_REGEX_DEFAULT,
G_REGEX_MATCH_DEFAULT, NULL);
g_once_init_leave (&splitter, s);
}
lines = g_regex_split (splitter, orig, 0);
for (i = 0; lines[i]; i++)
{
gchar *a, *b, *c;
a = g_regex_replace_literal (cleanup[0], lines[i], -1, 0, "", 0, 0);
b = g_regex_replace_literal (cleanup[1], a, -1, 0, "", 0, 0);
c = g_regex_replace_literal (cleanup[2], b, -1, 0, " ", 0, 0);
g_free (lines[i]);
g_free (a);
g_free (b);
lines[i] = c;
}
result = g_strjoinv ("\n\n", lines);
g_strfreev (lines);
return result;
}
static void
end_element (GMarkupParseContext *context,
const gchar *element_name,
gpointer user_data,
GError **error)
{
TextTableParseInfo *info = user_data;
pop_attribute_value (&info->gettext_domain);
pop_attribute_value (&info->schema_id);
pop_attribute_value (&info->key_name);
if (info->string)
{
GHashTable *source_table = NULL;
const gchar *gettext_domain;
const gchar *schema_id;
const gchar *key_name;
gettext_domain = get_attribute_value (info->gettext_domain);
schema_id = get_attribute_value (info->schema_id);
key_name = get_attribute_value (info->key_name);
if (g_str_equal (element_name, "summary"))
source_table = info->summaries;
else if (g_str_equal (element_name, "description"))
source_table = info->descriptions;
if (source_table && schema_id && key_name)
{
GHashTable *schema_table;
gchar *normalised;
schema_table = g_hash_table_lookup (source_table, schema_id);
if (schema_table == NULL)
{
schema_table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
g_hash_table_insert (source_table, g_strdup (schema_id), schema_table);
}
normalised = normalise_whitespace (info->string->str);
if (gettext_domain && normalised[0])
{
gchar *translated;
translated = g_strdup (g_dgettext (gettext_domain, normalised));
g_free (normalised);
normalised = translated;
}
g_hash_table_insert (schema_table, g_strdup (key_name), normalised);
}
g_string_free (info->string, TRUE);
info->string = NULL;
}
}
static void
text (GMarkupParseContext *context,
const gchar *text,
gsize text_len,
gpointer user_data,
GError **error)
{
TextTableParseInfo *info = user_data;
if (info->string)
g_string_append_len (info->string, text, text_len);
}
static void
parse_into_text_tables (const gchar *directory,
GHashTable *summaries,
GHashTable *descriptions)
{
GMarkupParser parser = { start_element, end_element, text, NULL, NULL };
TextTableParseInfo info = { summaries, descriptions, NULL, NULL, NULL, NULL };
const gchar *basename;
GDir *dir;
dir = g_dir_open (directory, 0, NULL);
while ((basename = g_dir_read_name (dir)))
{
gchar *filename;
gchar *contents;
gsize size;
filename = g_build_filename (directory, basename, NULL);
if (g_file_get_contents (filename, &contents, &size, NULL))
{
GMarkupParseContext *context;
context = g_markup_parse_context_new (&parser, G_MARKUP_TREAT_CDATA_AS_TEXT, &info, NULL);
/* Ignore errors here, this is best effort only. */
if (g_markup_parse_context_parse (context, contents, size, NULL))
(void) g_markup_parse_context_end_parse (context, NULL);
g_markup_parse_context_free (context);
/* Clean up dangling stuff in case there was an error. */
g_slist_free_full (info.gettext_domain, g_free);
g_slist_free_full (info.schema_id, g_free);
g_slist_free_full (info.key_name, g_free);
info.gettext_domain = NULL;
info.schema_id = NULL;
info.key_name = NULL;
if (info.string)
{
g_string_free (info.string, TRUE);
info.string = NULL;
}
g_free (contents);
}
g_free (filename);
}
g_dir_close (dir);
}
static GHashTable **
g_settings_schema_source_get_text_tables (GSettingsSchemaSource *source)
{
if (g_once_init_enter (&source->text_tables))
{
GHashTable **text_tables;
text_tables = g_new (GHashTable *, 2);
text_tables[0] = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify) g_hash_table_unref);
text_tables[1] = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify) g_hash_table_unref);
if (source->directory)
parse_into_text_tables (source->directory, text_tables[0], text_tables[1]);
g_once_init_leave (&source->text_tables, text_tables);
}
return source->text_tables;
}
/**
* g_settings_schema_source_list_schemas:
* @source: a #GSettingsSchemaSource
* @recursive: if we should recurse
* @non_relocatable: (out) (transfer full) (array zero-terminated=1): the
* list of non-relocatable schemas, in no defined order
* @relocatable: (out) (transfer full) (array zero-terminated=1): the list
* of relocatable schemas, in no defined order
*
* Lists the schemas in a given source.
*
* If @recursive is %TRUE then include parent sources. If %FALSE then
* only include the schemas from one source (ie: one directory). You
* probably want %TRUE.
*
* Non-relocatable schemas are those for which you can call
* g_settings_new(). Relocatable schemas are those for which you must
* use g_settings_new_with_path().
*
* Do not call this function from normal programs. This is designed for
* use by database editors, commandline tools, etc.
*
* Since: 2.40
**/
void
g_settings_schema_source_list_schemas (GSettingsSchemaSource *source,
gboolean recursive,
gchar ***non_relocatable,
gchar ***relocatable)
{
GHashTable *single, *reloc;
GSettingsSchemaSource *s;
/* We use hash tables to avoid duplicate listings for schemas that
* appear in more than one file.
*/
single = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
reloc = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
for (s = source; s; s = s->parent)
{
gchar **list;
gint i;
list = gvdb_table_list (s->table, "");
/* empty schema cache file? */
if (list == NULL)
continue;
for (i = 0; list[i]; i++)
{
if (!g_hash_table_contains (single, list[i]) &&
!g_hash_table_contains (reloc, list[i]))
{
gchar *schema;
GvdbTable *table;
schema = g_strdup (list[i]);
table = gvdb_table_get_table (s->table, list[i]);
g_assert (table != NULL);
if (gvdb_table_has_value (table, ".path"))
g_hash_table_add (single, schema);
else
g_hash_table_add (reloc, schema);
gvdb_table_free (table);
}
}
g_strfreev (list);
/* Only the first source if recursive not requested */
if (!recursive)
break;
}
if (non_relocatable)
{
*non_relocatable = (gchar **) g_hash_table_get_keys_as_array (single, NULL);
g_hash_table_steal_all (single);
}
if (relocatable)
{
*relocatable = (gchar **) g_hash_table_get_keys_as_array (reloc, NULL);
g_hash_table_steal_all (reloc);
}
g_hash_table_unref (single);
g_hash_table_unref (reloc);
}
static gchar **non_relocatable_schema_list;
static gchar **relocatable_schema_list;
static gsize schema_lists_initialised;
static void
ensure_schema_lists (void)
{
if (g_once_init_enter (&schema_lists_initialised))
{
initialise_schema_sources ();
g_settings_schema_source_list_schemas (schema_sources, TRUE,
&non_relocatable_schema_list,
&relocatable_schema_list);
g_once_init_leave (&schema_lists_initialised, TRUE);
}
}
/**
* g_settings_list_schemas:
*
* Deprecated.
*
* Returns: (element-type utf8) (transfer none) (not nullable): a list of
* #GSettings schemas that are available, in no defined order. The list
* must not be modified or freed.
*
* Since: 2.26
*
* Deprecated: 2.40: Use g_settings_schema_source_list_schemas() instead.
* If you used g_settings_list_schemas() to check for the presence of
* a particular schema, use g_settings_schema_source_lookup() instead
* of your whole loop.
**/
const gchar * const *
g_settings_list_schemas (void)
{
ensure_schema_lists ();
return (const gchar **) non_relocatable_schema_list;
}
/**
* g_settings_list_relocatable_schemas:
*
* Deprecated.
*
* Returns: (element-type utf8) (transfer none) (not nullable): a list of
* relocatable #GSettings schemas that are available, in no defined order.
* The list must not be modified or freed.
*
* Since: 2.28
*
* Deprecated: 2.40: Use g_settings_schema_source_list_schemas() instead
**/
const gchar * const *
g_settings_list_relocatable_schemas (void)
{
ensure_schema_lists ();
return (const gchar **) relocatable_schema_list;
}
/**
* g_settings_schema_ref:
* @schema: a #GSettingsSchema
*
* Increase the reference count of @schema, returning a new reference.
*
* Returns: (transfer full) (not nullable): a new reference to @schema
*
* Since: 2.32
**/
GSettingsSchema *
g_settings_schema_ref (GSettingsSchema *schema)
{
g_atomic_int_inc (&schema->ref_count);
return schema;
}
/**
* g_settings_schema_unref:
* @schema: a #GSettingsSchema
*
* Decrease the reference count of @schema, possibly freeing it.
*
* Since: 2.32
**/
void
g_settings_schema_unref (GSettingsSchema *schema)
{
if (g_atomic_int_dec_and_test (&schema->ref_count))
{
if (schema->extends)
g_settings_schema_unref (schema->extends);
g_settings_schema_source_unref (schema->source);
gvdb_table_free (schema->table);
g_free (schema->items);
g_free (schema->id);
g_slice_free (GSettingsSchema, schema);
}
}
const gchar *
g_settings_schema_get_string (GSettingsSchema *schema,
const gchar *key)
{
const gchar *result = NULL;
GVariant *value;
if ((value = gvdb_table_get_raw_value (schema->table, key)))
{
result = g_variant_get_string (value, NULL);
g_variant_unref (value);
}
return result;
}
GSettingsSchema *
g_settings_schema_get_child_schema (GSettingsSchema *schema,
const gchar *name)
{
const gchar *child_id;
gchar *child_name;
child_name = g_strconcat (name, "/", NULL);
child_id = g_settings_schema_get_string (schema, child_name);
g_free (child_name);
if (child_id == NULL)
return NULL;
return g_settings_schema_source_lookup (schema->source, child_id, TRUE);
}
GVariantIter *
g_settings_schema_get_value (GSettingsSchema *schema,
const gchar *key)
{
GSettingsSchema *s = schema;
GVariantIter *iter;
GVariant *value = NULL;
g_return_val_if_fail (schema != NULL, NULL);
for (s = schema; s; s = s->extends)
if ((value = gvdb_table_get_raw_value (s->table, key)))
break;
if G_UNLIKELY (value == NULL || !g_variant_is_of_type (value, G_VARIANT_TYPE_TUPLE))
g_error ("Settings schema '%s' does not contain a key named '%s'", schema->id, key);
iter = g_variant_iter_new (value);
g_variant_unref (value);
return iter;
}
/**
* g_settings_schema_get_path:
* @schema: a #GSettingsSchema
*
* Gets the path associated with @schema, or %NULL.
*
* Schemas may be single-instance or relocatable. Single-instance
* schemas correspond to exactly one set of keys in the backend
* database: those located at the path returned by this function.
*
* Relocatable schemas can be referenced by other schemas and can
* therefore describe multiple sets of keys at different locations. For
* relocatable schemas, this function will return %NULL.
*
* Returns: (nullable) (transfer none): the path of the schema, or %NULL
*
* Since: 2.32
**/
const gchar *
g_settings_schema_get_path (GSettingsSchema *schema)
{
return schema->path;
}
const gchar *
g_settings_schema_get_gettext_domain (GSettingsSchema *schema)
{
return schema->gettext_domain;
}
/**
* g_settings_schema_has_key:
* @schema: a #GSettingsSchema
* @name: the name of a key
*
* Checks if @schema has a key named @name.
*
* Returns: %TRUE if such a key exists
*
* Since: 2.40
**/
gboolean
g_settings_schema_has_key (GSettingsSchema *schema,
const gchar *key)
{
return gvdb_table_has_value (schema->table, key);
}
/**
* g_settings_schema_list_children:
* @schema: a #GSettingsSchema
*
* Gets the list of children in @schema.
*
* You should free the return value with g_strfreev() when you are done
* with it.
*
* Returns: (not nullable) (transfer full) (element-type utf8): a list of
* the children on @settings, in no defined order
*
* Since: 2.44
*/
gchar **
g_settings_schema_list_children (GSettingsSchema *schema)
{
const GQuark *keys;
gchar **strv;
gint n_keys;
gint i, j;
g_return_val_if_fail (schema != NULL, NULL);
keys = g_settings_schema_list (schema, &n_keys);
strv = g_new (gchar *, n_keys + 1);
for (i = j = 0; i < n_keys; i++)
{
const gchar *key = g_quark_to_string (keys[i]);
if (g_str_has_suffix (key, "/"))
{
gsize length = strlen (key);
strv[j] = g_memdup2 (key, length);
strv[j][length - 1] = '\0';
j++;
}
}
strv[j] = NULL;
return strv;
}
/**
* g_settings_schema_list_keys:
* @schema: a #GSettingsSchema
*
* Introspects the list of keys on @schema.
*
* You should probably not be calling this function from "normal" code
* (since you should already know what keys are in your schema). This
* function is intended for introspection reasons.
*
* Returns: (not nullable) (transfer full) (element-type utf8): a list
* of the keys on @schema, in no defined order
*
* Since: 2.46
*/
gchar **
g_settings_schema_list_keys (GSettingsSchema *schema)
{
const GQuark *keys;
gchar **strv;
gint n_keys;
gint i, j;
g_return_val_if_fail (schema != NULL, NULL);
keys = g_settings_schema_list (schema, &n_keys);
strv = g_new (gchar *, n_keys + 1);
for (i = j = 0; i < n_keys; i++)
{
const gchar *key = g_quark_to_string (keys[i]);
if (!g_str_has_suffix (key, "/"))
strv[j++] = g_strdup (key);
}
strv[j] = NULL;
return strv;
}
const GQuark *
g_settings_schema_list (GSettingsSchema *schema,
gint *n_items)
{
if (schema->items == NULL)
{
GSettingsSchema *s;
GHashTableIter iter;
GHashTable *items;
gpointer name;
gint len;
gint i;
items = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
for (s = schema; s; s = s->extends)
{
gchar **list;
list = gvdb_table_list (s->table, "");
if (list)
{
for (i = 0; list[i]; i++)
g_hash_table_add (items, list[i]); /* transfer ownership */
g_free (list); /* free container only */
}
}
/* Do a first pass to eliminate child items that do not map to
* valid schemas (ie: ones that would crash us if we actually
* tried to create them).
*/
g_hash_table_iter_init (&iter, items);
while (g_hash_table_iter_next (&iter, &name, NULL))
if (g_str_has_suffix (name, "/"))
{
GSettingsSchemaSource *source;
GVariant *child_schema;
GvdbTable *child_table;
child_schema = gvdb_table_get_raw_value (schema->table, name);
if (!child_schema)
continue;
child_table = NULL;
for (source = schema->source; source; source = source->parent)
if ((child_table = gvdb_table_get_table (source->table, g_variant_get_string (child_schema, NULL))))
break;
g_variant_unref (child_schema);
/* Schema is not found -> remove it from the list */
if (child_table == NULL)
{
g_hash_table_iter_remove (&iter);
continue;
}
/* Make sure the schema is relocatable or at the
* expected path
*/
if (gvdb_table_has_value (child_table, ".path"))
{
GVariant *path;
gchar *expected;
gboolean same;
path = gvdb_table_get_raw_value (child_table, ".path");
expected = g_strconcat (schema->path, name, NULL);
same = g_str_equal (expected, g_variant_get_string (path, NULL));
g_variant_unref (path);
g_free (expected);
/* Schema is non-relocatable and did not have the
* expected path -> remove it from the list
*/
if (!same)
g_hash_table_iter_remove (&iter);
}
gvdb_table_free (child_table);
}
/* Now create the list */
len = g_hash_table_size (items);
schema->items = g_new (GQuark, len);
i = 0;
g_hash_table_iter_init (&iter, items);
while (g_hash_table_iter_next (&iter, &name, NULL))
schema->items[i++] = g_quark_from_string (name);
schema->n_items = i;
g_assert (i == len);
g_hash_table_unref (items);
}
*n_items = schema->n_items;
return schema->items;
}
/**
* g_settings_schema_get_id:
* @schema: a #GSettingsSchema
*
* Get the ID of @schema.
*
* Returns: (not nullable) (transfer none): the ID
**/
const gchar *
g_settings_schema_get_id (GSettingsSchema *schema)
{
return schema->id;
}
static inline void
endian_fixup (GVariant **value)
{
#if G_BYTE_ORDER == G_BIG_ENDIAN
GVariant *tmp;
tmp = g_variant_byteswap (*value);
g_variant_unref (*value);
*value = tmp;
#endif
}
void
g_settings_schema_key_init (GSettingsSchemaKey *key,
GSettingsSchema *schema,
const gchar *name)
{
GVariantIter *iter;
GVariant *data;
guchar code;
memset (key, 0, sizeof *key);
iter = g_settings_schema_get_value (schema, name);
key->schema = g_settings_schema_ref (schema);
key->default_value = g_variant_iter_next_value (iter);
endian_fixup (&key->default_value);
key->type = g_variant_get_type (key->default_value);
key->name = g_intern_string (name);
while (g_variant_iter_next (iter, "(y*)", &code, &data))
{
switch (code)
{
case 'l':
/* translation requested */
g_variant_get (data, "(y&s)", &key->lc_char, &key->unparsed);
break;
case 'e':
/* enumerated types... */
key->is_enum = TRUE;
goto choice;
case 'f':
/* flags... */
key->is_flags = TRUE;
goto choice;
choice: case 'c':
/* ..., choices, aliases */
key->strinfo = g_variant_get_fixed_array (data, &key->strinfo_length, sizeof (guint32));
break;
case 'r':
g_variant_get (data, "(**)", &key->minimum, &key->maximum);
endian_fixup (&key->minimum);
endian_fixup (&key->maximum);
break;
case 'd':
g_variant_get (data, "@a{sv}", &key->desktop_overrides);
endian_fixup (&key->desktop_overrides);
break;
default:
g_warning ("unknown schema extension '%c'", code);
break;
}
g_variant_unref (data);
}
g_variant_iter_free (iter);
}
void
g_settings_schema_key_clear (GSettingsSchemaKey *key)
{
if (key->minimum)
g_variant_unref (key->minimum);
if (key->maximum)
g_variant_unref (key->maximum);
if (key->desktop_overrides)
g_variant_unref (key->desktop_overrides);
g_variant_unref (key->default_value);
g_settings_schema_unref (key->schema);
}
gboolean
g_settings_schema_key_type_check (GSettingsSchemaKey *key,
GVariant *value)
{
g_return_val_if_fail (value != NULL, FALSE);
return g_variant_is_of_type (value, key->type);
}
GVariant *
g_settings_schema_key_range_fixup (GSettingsSchemaKey *key,
GVariant *value)
{
const gchar *target;
if (g_settings_schema_key_range_check (key, value))
return g_variant_ref (value);
if (key->strinfo == NULL)
return NULL;
if (g_variant_is_container (value))
{
GVariantBuilder builder;
GVariantIter iter;
GVariant *child;
g_variant_iter_init (&iter, value);
g_variant_builder_init (&builder, g_variant_get_type (value));
while ((child = g_variant_iter_next_value (&iter)))
{
GVariant *fixed;
fixed = g_settings_schema_key_range_fixup (key, child);
g_variant_unref (child);
if (fixed == NULL)
{
g_variant_builder_clear (&builder);
return NULL;
}
g_variant_builder_add_value (&builder, fixed);
g_variant_unref (fixed);
}
return g_variant_ref_sink (g_variant_builder_end (&builder));
}
target = strinfo_string_from_alias (key->strinfo, key->strinfo_length,
g_variant_get_string (value, NULL));
return target ? g_variant_ref_sink (g_variant_new_string (target)) : NULL;
}
GVariant *
g_settings_schema_key_get_translated_default (GSettingsSchemaKey *key)
{
const gchar *translated = NULL;
GError *error = NULL;
const gchar *domain;
#ifdef HAVE_USELOCALE
const gchar *lc_time;
locale_t old_locale;
locale_t locale;
#endif
GVariant *value;
domain = g_settings_schema_get_gettext_domain (key->schema);
if (key->lc_char == '\0')
/* translation not requested for this key */
return NULL;
#ifdef HAVE_USELOCALE
if (key->lc_char == 't')
{
lc_time = setlocale (LC_TIME, NULL);
if (lc_time)
{
locale = newlocale (LC_MESSAGES_MASK, lc_time, (locale_t) 0);
if (locale != (locale_t) 0)
{
old_locale = uselocale (locale);
translated = g_dgettext (domain, key->unparsed);
uselocale (old_locale);
freelocale (locale);
}
}
}
#endif
if (translated == NULL)
translated = g_dgettext (domain, key->unparsed);
if (translated == key->unparsed)
/* the default value was not translated */
return NULL;
/* try to parse the translation of the unparsed default */
value = g_variant_parse (key->type, translated, NULL, NULL, &error);
if (value == NULL)
{
g_warning ("Failed to parse translated string '%s' for "
"key '%s' in schema '%s': %s", translated, key->name,
g_settings_schema_get_id (key->schema), error->message);
g_warning ("Using untranslated default instead.");
g_error_free (error);
}
else if (!g_settings_schema_key_range_check (key, value))
{
g_warning ("Translated default '%s' for key '%s' in schema '%s' "
"is outside of valid range", key->unparsed, key->name,
g_settings_schema_get_id (key->schema));
g_variant_unref (value);
value = NULL;
}
return value;
}
GVariant *
g_settings_schema_key_get_per_desktop_default (GSettingsSchemaKey *key)
{
static const gchar * const *current_desktops;
GVariant *value = NULL;
gint i;
if (!key->desktop_overrides)
return NULL;
if (g_once_init_enter (¤t_desktops))
{
const gchar *xdg_current_desktop = g_getenv ("XDG_CURRENT_DESKTOP");
gchar **tmp;
if (xdg_current_desktop != NULL && xdg_current_desktop[0] != '\0')
tmp = g_strsplit (xdg_current_desktop, G_SEARCHPATH_SEPARATOR_S, -1);
else
tmp = g_new0 (gchar *, 0 + 1);
g_once_init_leave (¤t_desktops, (const gchar **) tmp);
}
for (i = 0; value == NULL && current_desktops[i] != NULL; i++)
value = g_variant_lookup_value (key->desktop_overrides, current_desktops[i], NULL);
return value;
}
gint
g_settings_schema_key_to_enum (GSettingsSchemaKey *key,
GVariant *value)
{
gboolean it_worked G_GNUC_UNUSED /* when compiling with G_DISABLE_ASSERT */;
guint result;
it_worked = strinfo_enum_from_string (key->strinfo, key->strinfo_length,
g_variant_get_string (value, NULL),
&result);
/* 'value' can only come from the backend after being filtered for validity,
* from the translation after being filtered for validity, or from the schema
* itself (which the schema compiler checks for validity). If this assertion
* fails then it's really a bug in GSettings or the schema compiler...
*/
g_assert (it_worked);
return result;
}
/* Returns a new floating #GVariant. */
GVariant *
g_settings_schema_key_from_enum (GSettingsSchemaKey *key,
gint value)
{
const gchar *string;
string = strinfo_string_from_enum (key->strinfo, key->strinfo_length, value);
if (string == NULL)
return NULL;
return g_variant_new_string (string);
}
guint
g_settings_schema_key_to_flags (GSettingsSchemaKey *key,
GVariant *value)
{
GVariantIter iter;
const gchar *flag;
guint result;
result = 0;
g_variant_iter_init (&iter, value);
while (g_variant_iter_next (&iter, "&s", &flag))
{
gboolean it_worked G_GNUC_UNUSED /* when compiling with G_DISABLE_ASSERT */;
guint flag_value;
it_worked = strinfo_enum_from_string (key->strinfo, key->strinfo_length, flag, &flag_value);
/* as in g_settings_to_enum() */
g_assert (it_worked);
result |= flag_value;
}
return result;
}
/* Returns a new floating #GVariant. */
GVariant *
g_settings_schema_key_from_flags (GSettingsSchemaKey *key,
guint value)
{
GVariantBuilder builder;
gint i;
g_variant_builder_init (&builder, G_VARIANT_TYPE ("as"));
for (i = 0; i < 32; i++)
if (value & (1u << i))
{
const gchar *string;
string = strinfo_string_from_enum (key->strinfo, key->strinfo_length, 1u << i);
if (string == NULL)
{
g_variant_builder_clear (&builder);
return NULL;
}
g_variant_builder_add (&builder, "s", string);
}
return g_variant_builder_end (&builder);
}
G_DEFINE_BOXED_TYPE (GSettingsSchemaKey, g_settings_schema_key, g_settings_schema_key_ref, g_settings_schema_key_unref)
/**
* g_settings_schema_key_ref:
* @key: a #GSettingsSchemaKey
*
* Increase the reference count of @key, returning a new reference.
*
* Returns: (not nullable) (transfer full): a new reference to @key
*
* Since: 2.40
**/
GSettingsSchemaKey *
g_settings_schema_key_ref (GSettingsSchemaKey *key)
{
g_return_val_if_fail (key != NULL, NULL);
g_atomic_int_inc (&key->ref_count);
return key;
}
/**
* g_settings_schema_key_unref:
* @key: a #GSettingsSchemaKey
*
* Decrease the reference count of @key, possibly freeing it.
*
* Since: 2.40
**/
void
g_settings_schema_key_unref (GSettingsSchemaKey *key)
{
g_return_if_fail (key != NULL);
if (g_atomic_int_dec_and_test (&key->ref_count))
{
g_settings_schema_key_clear (key);
g_slice_free (GSettingsSchemaKey, key);
}
}
/**
* g_settings_schema_get_key:
* @schema: a #GSettingsSchema
* @name: the name of a key
*
* Gets the key named @name from @schema.
*
* It is a programmer error to request a key that does not exist. See
* g_settings_schema_list_keys().
*
* Returns: (not nullable) (transfer full): the #GSettingsSchemaKey for @name
*
* Since: 2.40
**/
GSettingsSchemaKey *
g_settings_schema_get_key (GSettingsSchema *schema,
const gchar *name)
{
GSettingsSchemaKey *key;
g_return_val_if_fail (schema != NULL, NULL);
g_return_val_if_fail (name != NULL, NULL);
key = g_slice_new (GSettingsSchemaKey);
g_settings_schema_key_init (key, schema, name);
key->ref_count = 1;
return key;
}
/**
* g_settings_schema_key_get_name:
* @key: a #GSettingsSchemaKey
*
* Gets the name of @key.
*
* Returns: (not nullable) (transfer none): the name of @key.
*
* Since: 2.44
*/
const gchar *
g_settings_schema_key_get_name (GSettingsSchemaKey *key)
{
g_return_val_if_fail (key != NULL, NULL);
return key->name;
}
/**
* g_settings_schema_key_get_summary:
* @key: a #GSettingsSchemaKey
*
* Gets the summary for @key.
*
* If no summary has been provided in the schema for @key, returns
* %NULL.
*
* The summary is a short description of the purpose of the key; usually
* one short sentence. Summaries can be translated and the value
* returned from this function is is the current locale.
*
* This function is slow. The summary and description information for
* the schemas is not stored in the compiled schema database so this
* function has to parse all of the source XML files in the schema
* directory.
*
* Returns: (nullable) (transfer none): the summary for @key, or %NULL
*
* Since: 2.34
**/
const gchar *
g_settings_schema_key_get_summary (GSettingsSchemaKey *key)
{
GHashTable **text_tables;
GHashTable *summaries;
text_tables = g_settings_schema_source_get_text_tables (key->schema->source);
summaries = g_hash_table_lookup (text_tables[0], key->schema->id);
return summaries ? g_hash_table_lookup (summaries, key->name) : NULL;
}
/**
* g_settings_schema_key_get_description:
* @key: a #GSettingsSchemaKey
*
* Gets the description for @key.
*
* If no description has been provided in the schema for @key, returns
* %NULL.
*
* The description can be one sentence to several paragraphs in length.
* Paragraphs are delimited with a double newline. Descriptions can be
* translated and the value returned from this function is is the
* current locale.
*
* This function is slow. The summary and description information for
* the schemas is not stored in the compiled schema database so this
* function has to parse all of the source XML files in the schema
* directory.
*
* Returns: (nullable) (transfer none): the description for @key, or %NULL
*
* Since: 2.34
**/
const gchar *
g_settings_schema_key_get_description (GSettingsSchemaKey *key)
{
GHashTable **text_tables;
GHashTable *descriptions;
text_tables = g_settings_schema_source_get_text_tables (key->schema->source);
descriptions = g_hash_table_lookup (text_tables[1], key->schema->id);
return descriptions ? g_hash_table_lookup (descriptions, key->name) : NULL;
}
/**
* g_settings_schema_key_get_value_type:
* @key: a #GSettingsSchemaKey
*
* Gets the #GVariantType of @key.
*
* Returns: (not nullable) (transfer none): the type of @key
*
* Since: 2.40
**/
const GVariantType *
g_settings_schema_key_get_value_type (GSettingsSchemaKey *key)
{
g_return_val_if_fail (key, NULL);
return key->type;
}
/**
* g_settings_schema_key_get_default_value:
* @key: a #GSettingsSchemaKey
*
* Gets the default value for @key.
*
* Note that this is the default value according to the schema. System
* administrator defaults and lockdown are not visible via this API.
*
* Returns: (not nullable) (transfer full): the default value for the key
*
* Since: 2.40
**/
GVariant *
g_settings_schema_key_get_default_value (GSettingsSchemaKey *key)
{
GVariant *value;
g_return_val_if_fail (key, NULL);
value = g_settings_schema_key_get_translated_default (key);
if (!value)
value = g_settings_schema_key_get_per_desktop_default (key);
if (!value)
value = g_variant_ref (key->default_value);
return value;
}
/**
* g_settings_schema_key_get_range:
* @key: a #GSettingsSchemaKey
*
* Queries the range of a key.
*
* This function will return a #GVariant that fully describes the range
* of values that are valid for @key.
*
* The type of #GVariant returned is `(sv)`. The string describes
* the type of range restriction in effect. The type and meaning of
* the value contained in the variant depends on the string.
*
* If the string is `'type'` then the variant contains an empty array.
* The element type of that empty array is the expected type of value
* and all values of that type are valid.
*
* If the string is `'enum'` then the variant contains an array
* enumerating the possible values. Each item in the array is
* a possible valid value and no other values are valid.
*
* If the string is `'flags'` then the variant contains an array. Each
* item in the array is a value that may appear zero or one times in an
* array to be used as the value for this key. For example, if the
* variant contained the array `['x', 'y']` then the valid values for
* the key would be `[]`, `['x']`, `['y']`, `['x', 'y']` and
* `['y', 'x']`.
*
* Finally, if the string is `'range'` then the variant contains a pair
* of like-typed values -- the minimum and maximum permissible values
* for this key.
*
* This information should not be used by normal programs. It is
* considered to be a hint for introspection purposes. Normal programs
* should already know what is permitted by their own schema. The
* format may change in any way in the future -- but particularly, new
* forms may be added to the possibilities described above.
*
* You should free the returned value with g_variant_unref() when it is
* no longer needed.
*
* Returns: (not nullable) (transfer full): a #GVariant describing the range
*
* Since: 2.40
**/
GVariant *
g_settings_schema_key_get_range (GSettingsSchemaKey *key)
{
const gchar *type;
GVariant *range;
if (key->minimum)
{
range = g_variant_new ("(**)", key->minimum, key->maximum);
type = "range";
}
else if (key->strinfo)
{
range = strinfo_enumerate (key->strinfo, key->strinfo_length);
type = key->is_flags ? "flags" : "enum";
}
else
{
range = g_variant_new_array (key->type, NULL, 0);
type = "type";
}
return g_variant_ref_sink (g_variant_new ("(sv)", type, range));
}
/**
* g_settings_schema_key_range_check:
* @key: a #GSettingsSchemaKey
* @value: the value to check
*
* Checks if the given @value is within the
* permitted range for @key.
*
* It is a programmer error if @value is not of the correct type — you
* must check for this first.
*
* Returns: %TRUE if @value is valid for @key
*
* Since: 2.40
**/
gboolean
g_settings_schema_key_range_check (GSettingsSchemaKey *key,
GVariant *value)
{
if (key->minimum == NULL && key->strinfo == NULL)
return TRUE;
if (g_variant_is_container (value))
{
gboolean ok = TRUE;
GVariantIter iter;
GVariant *child;
g_variant_iter_init (&iter, value);
while (ok && (child = g_variant_iter_next_value (&iter)))
{
ok = g_settings_schema_key_range_check (key, child);
g_variant_unref (child);
}
return ok;
}
if (key->minimum)
{
return g_variant_compare (key->minimum, value) <= 0 &&
g_variant_compare (value, key->maximum) <= 0;
}
return strinfo_is_string_valid (key->strinfo, key->strinfo_length,
g_variant_get_string (value, NULL));
}
|