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
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include <sal/config.h>

#include <cassert>
#include <cstddef>
#include <cstring>
#include <forward_list>
#include <limits>
#include <vector>

extern "C" {
    // <https://bugzilla.gnome.org/show_bug.cgi?id=754245>
    // "common/dconf-changeset.h etc. lack extern "C" wrapper for C++"
#include <dconf/dconf.h>
}

#include <com/sun/star/uno/Sequence.hxx>
#include <o3tl/safeint.hxx>
#include <rtl/ustrbuf.hxx>
#include <sal/log.hxx>

#include "data.hxx"
#include "dconf.hxx"
#include "groupnode.hxx"
#include "localizedpropertynode.hxx"
#include "localizedvaluenode.hxx"
#include "nodemap.hxx"
#include "propertynode.hxx"
#include "setnode.hxx"

// component-data is encoded in dconf as follows:
//
// * The node hierarchy (starting at component nodes with names like
//   "org.openoffice.Setup") maps to dconf paths underneath
//   "/org/libreoffice/registry/".
//
// * Component, group, set, and localized property nodes map to dconf dirs,
//   while property and localized value nodes map to dconf keys.
//
// * The names of nodes that are not set elements are used directly as dconf
//   path segments.  (The syntax for node names is any non-empty sequences of
//   any Unicode scalar values except U+0000--0008, U+000B--000C, U+000E--001F,
//   U+002F SOLIDUS, and U+FFFE--FFFF.  TODO: "<aruiz> sberg, in general I think
//   it'd be nice if you used path separators instead of dots though, they have
//   meaning in dconf/gvdb world :-)"?)
//
// * The names of set element nodes are encoded as dconf path segments as
//   follows: each occurrence of U+0000 NULL is replace by the three characters
//   "\00", each occurrence of U+002F SOLIDUS is replaced by the three
//   characters "\2F", and each occurrence of U+005C REVERSE SOLIDUS is replaced
//   by the three characters "\5C".
//
// * Set elements (which must themselves be either sets or groups) map to
//   "indirection" dconf dirs as follows:
//
// ** The dir must contain a key named "op" of string type, with a value of
//    "fuse", "replace", or "remove".
//
// ** If "op" is "fuse" or "replace", the dir must contain exactly the following
//    further keys and dirs:
//
// *** The dir must contain a key named "template" of string type, containing
//     the full template name, encoded as follows: each occurrence of U+0000
//     NULL is replace by the three characters "\00" and each occurrence of
//     U+005C REVERSE SOLIDUS is replaced by the three characters "\5C".
//
// *** The dir must contain a dir named "content" that contains the set
//     element's (i.e., set or group node's) real content.
//
// ** If "op" is "remove", the dir must contain no further keys or dirs.
//
// * Property and localized property value "fuse" operations map to GVariant
//   instances as follows:
//
// ** Non-nillable boolean values map to GVariant boolean instances.
//
// ** Non-nillable short values map to GVariant int16 instances.
//
// ** Non-nillable int values map to GVariant int32 instances.
//
// ** Non-nillable long values map to GVariant int64 instances.
//
// ** Non-nillable double values map to GVariant double instances.
//
// ** Non-nillable string values map to GVariant string instances, with the
//    following encoding: each occurrence of U+0000 NULL is replace by the three
//    characters "\00" and each occurrence of U+005C REVERSE SOLIDUS is replaced
//    by the three characters "\5C".
//
// ** Non-nillable hexbinary values map to GVariant byte array instances.
//
// ** Non-nillable list values recursively map to GVariant array instances.
//
// ** Nillable values recursively map to GVariant maybe instances.
//
// * Property "remove" operations map to GVariant instances of empty tuple type.
//
// Finalization:  The component-update.dtd allows for finalization of
// oor:component-data, node, and prop elements, while dconf allows for locking
// of individual keys.  That does not match, but just mark the individual Node
// instances that correspond to individual dconf keys as finalized for
// non-writable dconf keys.
//
// TODO: support "mandatory" and "external"?

namespace configmgr::dconf {

namespace {

template<typename T> class GObjectHolder {
public:
    explicit GObjectHolder(T * object): object_(object) {}

    ~GObjectHolder() {
        if (object_ != nullptr) {
            g_object_unref(object_);
        }
    }

    T * get() const { return object_; }

private:
    GObjectHolder(GObjectHolder const &) = delete;
    GObjectHolder& operator =(GObjectHolder const &) = delete;

    T * object_;
};

class GVariantHolder {
public:
    explicit GVariantHolder(GVariant * variant = nullptr): variant_(variant) {}

    ~GVariantHolder() { unref(); }

    void reset(GVariant * variant) {
        unref();
        variant_ = variant;
    }

    void release() { variant_ = nullptr; }

    GVariant * get() const { return variant_; }

private:
    GVariantHolder(GVariantHolder const &) = delete;
    GVariantHolder& operator =(GVariantHolder const &) = delete;

    void unref() {
        if (variant_ != nullptr) {
            g_variant_unref(variant_);
        }
    }

    GVariant * variant_;
};

class GVariantTypeHolder {
public:
    explicit GVariantTypeHolder(GVariantType * type): type_(type) {}

    ~GVariantTypeHolder() {
        if (type_ != nullptr) {
            g_variant_type_free(type_);
        }
    }

    GVariantType * get() const { return type_; }

private:
    GVariantTypeHolder(GVariantTypeHolder const &) = delete;
    GVariantTypeHolder& operator =(GVariantTypeHolder const &) = delete;

    GVariantType * type_;
};

class StringArrayHolder {
public:
    explicit StringArrayHolder(gchar ** array): array_(array) {}

    ~StringArrayHolder() { g_strfreev(array_); }

    gchar ** get() const { return array_; }

private:
    StringArrayHolder(StringArrayHolder const &) = delete;
    StringArrayHolder& operator =(StringArrayHolder const &) = delete;

    gchar ** array_;
};

class ChangesetHolder {
public:
    explicit ChangesetHolder(DConfChangeset * changeset):
        changeset_(changeset)
    {}

    ~ChangesetHolder() {
        if (changeset_ != nullptr) {
            dconf_changeset_unref(changeset_);
        }
    }

    DConfChangeset * get() const { return changeset_; }

private:
    ChangesetHolder(ChangesetHolder const &) = delete;
    ChangesetHolder& operator =(ChangesetHolder const &) = delete;

    DConfChangeset * changeset_;
};

OString getRoot() {
    return "/org/libreoffice/registry";
}

bool decode(OUString * string, bool slash) {
    for (sal_Int32 i = 0;; ++i) {
        i = string->indexOf('\\', i);
        if (i == -1) {
            return true;
        }
        if (string->match("00", i + 1)) {
            *string = string->replaceAt(i, 3, OUString(u'\0'));
        } else if (slash && string->match("2F", i + 1)) {
            *string = string->replaceAt(i, 3, "/");
        } else if (string->match("5C", i + 1)) {
            *string = string->replaceAt(i + 1, 2, "");
        } else {
            SAL_WARN("configmgr.dconf", "bad escape in " << *string);
            return false;
        }
    }
}

bool getBoolean(
    OString const & key, GVariantHolder const & variant, css::uno::Any * value)
{
    assert(value != nullptr);
    if (!g_variant_is_of_type(variant.get(), G_VARIANT_TYPE_BOOLEAN)) {
        SAL_WARN(
            "configmgr.dconf",
            "bad key " << key << " does not match boolean property");
        return false;
    }
    *value <<= bool(g_variant_get_boolean(variant.get()));
    return true;
}

bool getShort(
    OString const & key, GVariantHolder const & variant, css::uno::Any * value)
{
    assert(value != nullptr);
    if (!g_variant_is_of_type(variant.get(), G_VARIANT_TYPE_INT16)) {
        SAL_WARN(
            "configmgr.dconf",
            "bad key " << key << " does not match short property");
        return false;
    }
    *value <<= sal_Int16(g_variant_get_int16(variant.get()));
    return true;
}

bool getInt(
    OString const & key, GVariantHolder const & variant, css::uno::Any * value)
{
    assert(value != nullptr);
    if (!g_variant_is_of_type(variant.get(), G_VARIANT_TYPE_INT32)) {
        SAL_WARN(
            "configmgr.dconf",
            "bad key " << key << " does not match int property");
        return false;
    }
    *value <<= sal_Int32(g_variant_get_int32(variant.get()));
    return true;
}

bool getLong(
    OString const & key, GVariantHolder const & variant, css::uno::Any * value)
{
    assert(value != nullptr);
    if (!g_variant_is_of_type(variant.get(), G_VARIANT_TYPE_INT64)) {
        SAL_WARN(
            "configmgr.dconf",
            "bad key " << key << " does not match long property");
        return false;
    }
    *value <<= sal_Int64(g_variant_get_int64(variant.get()));
    return true;
}

bool getDouble(
    OString const & key, GVariantHolder const & variant, css::uno::Any * value)
{
    assert(value != nullptr);
    if (!g_variant_is_of_type(variant.get(), G_VARIANT_TYPE_DOUBLE)) {
        SAL_WARN(
            "configmgr.dconf",
            "bad key " << key << " does not match double property");
        return false;
    }
    *value <<= double(g_variant_get_double(variant.get()));
    return true;
}

bool getStringValue(
    OString const & key, GVariantHolder const & variant, OUString * value)
{
    assert(value != nullptr);
    if (!g_variant_is_of_type(variant.get(), G_VARIANT_TYPE_STRING)) {
        SAL_WARN(
            "configmgr.dconf",
            "bad key " << key << " does not match string property");
        return false;
    }
    gsize n;
    char const * p = g_variant_get_string(variant.get(), &n);
    if (n > o3tl::make_unsigned(
            std::numeric_limits<sal_Int32>::max()))
    {
        SAL_WARN("configmgr.dconf", "too long string value for key " << key);
        return false;
    }
    if (!rtl_convertStringToUString(
            &value->pData, p, static_cast<sal_Int32>(n), RTL_TEXTENCODING_UTF8,
            (RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_ERROR
             | RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_ERROR
             | RTL_TEXTTOUNICODE_FLAGS_INVALID_ERROR)))
    {
        SAL_WARN("configmgr.dconf", "non--UTF-8 string value for key " << key);
        return false;
    }
    return decode(value, false);
}

bool getString(
    OString const & key, GVariantHolder const & variant, css::uno::Any * value)
{
    assert(value != nullptr);
    OUString v;
    if (!getStringValue(key, variant, &v)) {
        return false;
    }
    *value <<= v;
    return true;
}

bool getHexbinaryValue(
    OString const & key, GVariantHolder const & variant,
    css::uno::Sequence<sal_Int8> * value)
{
    assert(value != nullptr);
    if (std::strcmp(g_variant_get_type_string(variant.get()), "ay") != 0) {
        SAL_WARN(
            "configmgr.dconf",
            "bad key " << key << " does not match hexbinary property");
        return false;
    }
    gsize n;
    gconstpointer p = g_variant_get_fixed_array(
        variant.get(), &n, sizeof (guchar));
    if (n > o3tl::make_unsigned(
            std::numeric_limits<sal_Int32>::max()))
    {
        SAL_WARN("configmgr.dconf", "too long hexbinary value for key " << key);
        return false;
    }
    value->realloc(static_cast<sal_Int32>(n));
    static_assert(sizeof (sal_Int8) == sizeof (guchar), "size mismatch");
    std::memcpy(value->getArray(), p, n * sizeof (guchar));
        // assuming that n * sizeof (guchar) is small enough for std::size_t
    return true;
}

bool getHexbinary(
    OString const & key, GVariantHolder const & variant, css::uno::Any * value)
{
    assert(value != nullptr);
    css::uno::Sequence<sal_Int8> v;
    if (!getHexbinaryValue(key, variant, &v)) {
        return false;
    }
    *value <<= v;
    return true;
}

bool getBooleanList(
    OString const & key, GVariantHolder const & variant, css::uno::Any * value)
{
    assert(value != nullptr);
    if (std::strcmp(g_variant_get_type_string(variant.get()), "ab") != 0) {
        SAL_WARN(
            "configmgr.dconf",
            "bad key " << key << " does not match boolean list property");
        return false;
    }
    gsize n;
    gconstpointer p = g_variant_get_fixed_array(
        variant.get(), &n, sizeof (guchar));
    if (n > o3tl::make_unsigned(
            std::numeric_limits<sal_Int32>::max()))
    {
        SAL_WARN("configmgr.dconf", "too long boolean list for key " << key);
        return false;
    }
    css::uno::Sequence<sal_Bool> v(static_cast<sal_Int32>(n));
    static_assert(sizeof (sal_Bool) == sizeof (guchar), "size mismatch");
    std::memcpy(v.getArray(), p, n * sizeof (guchar));
        // assuming that n * sizeof (guchar) is small enough for std::size_t
    *value <<= v;
    return true;
}

bool getShortList(
    OString const & key, GVariantHolder const & variant, css::uno::Any * value)
{
    assert(value != nullptr);
    if (std::strcmp(g_variant_get_type_string(variant.get()), "an") != 0) {
        SAL_WARN(
            "configmgr.dconf",
            "bad key " << key << " does not match short list property");
        return false;
    }
    gsize n;
    gconstpointer p = g_variant_get_fixed_array(
        variant.get(), &n, sizeof (gint16));
    if (n > o3tl::make_unsigned(
            std::numeric_limits<sal_Int32>::max()))
    {
        SAL_WARN("configmgr.dconf", "too long short list for key " << key);
        return false;
    }
    css::uno::Sequence<sal_Int16> v(static_cast<sal_Int32>(n));
    static_assert(sizeof (sal_Int16) == sizeof (gint16), "size mismatch");
    std::memcpy(v.getArray(), p, n * sizeof (gint16));
        // assuming that n * sizeof (gint16) is small enough for std::size_t
    *value <<= v;
    return true;
}

bool getIntList(
    OString const & key, GVariantHolder const & variant, css::uno::Any * value)
{
    assert(value != nullptr);
    if (std::strcmp(g_variant_get_type_string(variant.get()), "ai") != 0) {
        SAL_WARN(
            "configmgr.dconf",
            "bad key " << key << " does not match int list property");
        return false;
    }
    gsize n;
    gconstpointer p = g_variant_get_fixed_array(
        variant.get(), &n, sizeof (gint32));
    if (n > o3tl::make_unsigned(
            std::numeric_limits<sal_Int32>::max()))
    {
        SAL_WARN("configmgr.dconf", "too long int list for key " << key);
        return false;
    }
    css::uno::Sequence<sal_Int32> v(static_cast<sal_Int32>(n));
    static_assert(sizeof (sal_Int32) == sizeof (gint32), "size mismatch");
    std::memcpy(v.getArray(), p, n * sizeof (gint32));
        // assuming that n * sizeof (gint32) is small enough for std::size_t
    *value <<= v;
    return true;
}

bool getLongList(
    OString const & key, GVariantHolder const & variant, css::uno::Any * value)
{
    assert(value != nullptr);
    if (std::strcmp(g_variant_get_type_string(variant.get()), "ax") != 0) {
        SAL_WARN(
            "configmgr.dconf",
            "bad key " << key << " does not match long list property");
        return false;
    }
    gsize n;
    gconstpointer p = g_variant_get_fixed_array(
        variant.get(), &n, sizeof (gint64));
    if (n > o3tl::make_unsigned(
            std::numeric_limits<sal_Int32>::max()))
    {
        SAL_WARN("configmgr.dconf", "too long long list for key " << key);
        return false;
    }
    css::uno::Sequence<sal_Int64> v(static_cast<sal_Int32>(n));
    static_assert(sizeof (sal_Int64) == sizeof (gint64), "size mismatch");
    std::memcpy(v.getArray(), p, n * sizeof (gint64));
        // assuming that n * sizeof (gint64) is small enough for std::size_t
    *value <<= v;
    return true;
}

bool getDoubleList(
    OString const & key, GVariantHolder const & variant, css::uno::Any * value)
{
    assert(value != nullptr);
    if (std::strcmp(g_variant_get_type_string(variant.get()), "ad") != 0) {
        SAL_WARN(
            "configmgr.dconf",
            "bad key " << key << " does not match double list property");
        return false;
    }
    gsize n;
    gconstpointer p = g_variant_get_fixed_array(
        variant.get(), &n, sizeof (gdouble));
    if (n > o3tl::make_unsigned(
            std::numeric_limits<sal_Int32>::max()))
    {
        SAL_WARN("configmgr.dconf", "too long double list for key " << key);
        return false;
    }
    css::uno::Sequence<double> v(static_cast<sal_Int32>(n));
    static_assert(std::is_same<double, gdouble>::value, "type mismatch");
    std::memcpy(v.getArray(), p, n * sizeof (gdouble));
        // assuming that n * sizeof (gdouble) is small enough for std::size_t
    *value <<= v;
    return true;
}

bool getStringList(
    OString const & key, GVariantHolder const & variant, css::uno::Any * value)
{
    assert(value != nullptr);
    if (std::strcmp(g_variant_get_type_string(variant.get()), "as") != 0) {
        SAL_WARN(
            "configmgr.dconf",
            "bad key " << key << " does not match string list property");
        return false;
    }
    gsize n = g_variant_n_children(variant.get());
    if (n > o3tl::make_unsigned(
            std::numeric_limits<sal_Int32>::max()))
    {
        SAL_WARN("configmgr.dconf", "too long string list for key " << key);
        return false;
    }
    css::uno::Sequence<OUString> v(static_cast<sal_Int32>(n));
    for (gsize i = 0; i != n; ++i) {
        GVariantHolder c(g_variant_get_child_value(variant.get(), i));
        if (!getStringValue(key, c, v.getArray() + i)) {
            return false;
        }
    }
    *value <<= v;
    return true;
}

bool getHexbinaryList(
    OString const & key, GVariantHolder const & variant, css::uno::Any * value)
{
    assert(value != nullptr);
    if (std::strcmp(g_variant_get_type_string(variant.get()), "aay") != 0) {
        SAL_WARN(
            "configmgr.dconf",
            "bad key " << key << " does not match hexbinary list property");
        return false;
    }
    gsize n = g_variant_n_children(variant.get());
    if (n > o3tl::make_unsigned(
            std::numeric_limits<sal_Int32>::max()))
    {
        SAL_WARN("configmgr.dconf", "too long hexbinary list for key " << key);
        return false;
    }
    css::uno::Sequence<css::uno::Sequence<sal_Int8>> v(
        static_cast<sal_Int32>(n));
    for (gsize i = 0; i != n; ++i) {
        GVariantHolder c(g_variant_get_child_value(variant.get(), i));
        if (!getHexbinaryValue(key, c, v.getArray() + i)) {
            return false;
        }
    }
    *value <<= v;
    return true;
}

bool getAny(
    OString const & key, GVariantHolder const & variant, css::uno::Any * value)
{
    char const * t = g_variant_get_type_string(variant.get());
    if (std::strcmp(t, "b") == 0) {
        return getBoolean(key, variant, value);
    }
    if (std::strcmp(t, "n") == 0) {
        return getShort(key, variant, value);
    }
    if (std::strcmp(t, "i") == 0) {
        return getInt(key, variant, value);
    }
    if (std::strcmp(t, "x") == 0) {
        return getLong(key, variant, value);
    }
    if (std::strcmp(t, "d") == 0) {
        return getDouble(key, variant, value);
    }
    if (std::strcmp(t, "s") == 0) {
        return getString(key, variant, value);
    }
    if (std::strcmp(t, "ay") == 0) {
        return getHexbinary(key, variant, value);
    }
    if (std::strcmp(t, "ab") == 0) {
        return getBooleanList(key, variant, value);
    }
    if (std::strcmp(t, "an") == 0) {
        return getShortList(key, variant, value);
    }
    if (std::strcmp(t, "ai") == 0) {
        return getIntList(key, variant, value);
    }
    if (std::strcmp(t, "ax") == 0) {
        return getLongList(key, variant, value);
    }
    if (std::strcmp(t, "ad") == 0) {
        return getDoubleList(key, variant, value);
    }
    if (std::strcmp(t, "as") == 0) {
        return getStringList(key, variant, value);
    }
    if (std::strcmp(t, "aay") == 0) {
        return getHexbinaryList(key, variant, value);
    }
    SAL_WARN(
        "configmgr.dconf", "bad key " << key << " does not match any property");
    return false;
}

enum class ReadValue { Error, Value, Remove };

ReadValue readValue(
    GObjectHolder<DConfClient> const & client, OString const & path, Type type,
    bool nillable, bool removable, css::uno::Any * value)
{
    assert(value != nullptr);
    assert(!value->hasValue());
    assert(!path.endsWith("/"));
    GVariantHolder v(dconf_client_read(client.get(), path.getStr()));
    if (v.get() == nullptr) {
        SAL_WARN("configmgr.dconf", "cannot read key " << path);
        return ReadValue::Error;
    }
    if (removable && std::strcmp(g_variant_get_type_string(v.get()), "()") == 0)
    {
        return ReadValue::Remove;
    }
    bool nil;
    if (nillable) {
        if (g_variant_classify(v.get()) != G_VARIANT_CLASS_MAYBE) {
            SAL_WARN(
                "configmgr.dconf",
                "bad key " << path << " does not match nillable property");
        }
        v.reset(g_variant_get_maybe(v.get()));
        nil = v.get() == nullptr;
    } else {
        nil = false;
    }
    if (!nil) {
        switch (type) {
        case TYPE_ANY:
            if (!getAny(path, v, value)) {
                return ReadValue::Error;
            }
            break;
        case TYPE_BOOLEAN:
            if (!getBoolean(path, v, value)) {
                return ReadValue::Error;
            }
            break;
        case TYPE_SHORT:
            if (!getShort(path, v, value)) {
                return ReadValue::Error;
            }
            break;
        case TYPE_INT:
            if (!getInt(path, v, value)) {
                return ReadValue::Error;
            }
            break;
        case TYPE_LONG:
            if (!getLong(path, v, value)) {
                return ReadValue::Error;
            }
            break;
        case TYPE_DOUBLE:
            if (!getDouble(path, v, value)) {
                return ReadValue::Error;
            }
            break;
        case TYPE_STRING:
            if (!getString(path, v, value)) {
                return ReadValue::Error;
            }
            break;
        case TYPE_HEXBINARY:
            if (!getHexbinary(path, v, value)) {
                return ReadValue::Error;
            }
            break;
        case TYPE_BOOLEAN_LIST:
            if (!getBooleanList(path, v, value)) {
                return ReadValue::Error;
            }
            break;
        case TYPE_SHORT_LIST:
            if (!getShortList(path, v, value)) {
                return ReadValue::Error;
            }
            break;
        case TYPE_INT_LIST:
            if (!getIntList(path, v, value)) {
                return ReadValue::Error;
            }
            break;
        case TYPE_LONG_LIST:
            if (!getLongList(path, v, value)) {
                return ReadValue::Error;
            }
            break;
        case TYPE_DOUBLE_LIST:
            if (!getDoubleList(path, v, value)) {
                return ReadValue::Error;
            }
            break;
        case TYPE_STRING_LIST:
            if (!getStringList(path, v, value)) {
                return ReadValue::Error;
            }
            break;
        case TYPE_HEXBINARY_LIST:
            if (!getHexbinaryList(path, v, value)) {
                return ReadValue::Error;
            }
            break;
        default:
            assert(false); // cannot happen
        }
    }
    return ReadValue::Value;
}

void finalize(
    GObjectHolder<DConfClient> const & client, OString const & path,
    rtl::Reference<Node> const & node, int layer)
{
    if (!dconf_client_is_writable(client.get(), path.getStr())) {
        node->setFinalized(layer);
    }
}

void readDir(
    Data & data, int layer, rtl::Reference<Node> const & node,
    NodeMap & members, GObjectHolder<DConfClient> const & client,
    OString const & dir)
{
    StringArrayHolder a(dconf_client_list(client.get(), dir.getStr(), nullptr));
    for (char const * const * p = a.get(); *p != nullptr; ++p) {
        std::size_t n = std::strlen(*p);
        if (n > o3tl::make_unsigned(
                std::numeric_limits<sal_Int32>::max()))
        {
            SAL_WARN("configmgr.dconf", "too long dir/key in dir " << dir);
            continue;
        }
        OString s(*p, static_cast<sal_Int32>(n));
        OString path(dir + s);
        OUString name;
        if (!rtl_convertStringToUString(
                &name.pData, s.getStr(), s.getLength(), RTL_TEXTENCODING_UTF8,
                (RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_ERROR
                 | RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_ERROR
                 | RTL_TEXTTOUNICODE_FLAGS_INVALID_ERROR)))
        {
            SAL_WARN("configmgr.dconf", "non--UTF-8 dir/key in dir " << dir);
            continue;
        }
        bool isDir = name.endsWith("/", &name);
        OUString templ;
        bool remove;
        bool replace;
        if (node.is() && node->kind() == Node::KIND_SET) {
            if (!isDir) {
                SAL_WARN(
                    "configmgr.dconf",
                    "bad key " << path << " does not match set element");
                continue;
            }
            if (!decode(&name, true)) {
                continue;
            }
            enum class Op { None, Fuse, Replace, Remove };
            Op op = Op::None;
            bool content = false;
            bool bad = false;
            StringArrayHolder a2(
                dconf_client_list(client.get(), path.getStr(), nullptr));
            for (char const * const * p2 = a2.get(); *p2 != nullptr; ++p2) {
                if (std::strcmp(*p2, "op") == 0) {
                    OString path2(path + "op");
                    GVariantHolder v(
                        dconf_client_read(client.get(), path2.getStr()));
                    if (v.get() == nullptr) {
                        SAL_WARN(
                            "configmgr.dconf", "cannot read key " << path2);
                        bad = true;
                        break;
                    }
                    OUString ops;
                    if (!getStringValue(path2, v, &ops)) {
                        bad = true;
                        break;
                    }
                    if (ops == "fuse") {
                        op = Op::Fuse;
                    } else if (ops == "replace") {
                        op = Op::Replace;
                    } else if (ops == "remove") {
                        op = Op::Remove;
                    } else {
                        SAL_WARN(
                            "configmgr.dconf",
                            "bad key " << path2 << " value " << ops);
                        bad = true;
                        break;
                    }
                } else if (std::strcmp(*p2, "template") == 0) {
                    OString path2(path + "template");
                    GVariantHolder v(
                        dconf_client_read(client.get(), path2.getStr()));
                    if (v.get() == nullptr) {
                        SAL_WARN(
                            "configmgr.dconf", "cannot read key " << path2);
                        bad = true;
                        break;
                    }
                    if (!getStringValue(path2, v, &templ)) {
                        bad = true;
                        break;
                    }
                    if (!static_cast<SetNode *>(node.get())->
                        isValidTemplate(templ))
                    {
                        SAL_WARN(
                            "configmgr.dconf",
                            "bad key " << path2 << " value " << templ
                                << " denotes unsupported set element template");
                        bad = true;
                        break;
                    }
                } else if (std::strcmp(*p2, "content/") == 0) {
                    content = true;
                } else {
                    SAL_WARN(
                        "configmgr.dconf",
                        "bad dir/key " << p2
                            << " in set element indirection dir " << path);
                    bad = true;
                    break;
                }
            }
            if (bad) {
                continue;
            }
            switch (op) {
            default: // case Op::None:
                SAL_WARN(
                    "configmgr.dconf",
                    "bad set element indirection dir " << path
                        << " missing \"op\" key");
                continue;
            case Op::Fuse:
            case Op::Replace:
                if (templ.isEmpty() || !content) {
                    SAL_WARN(
                        "configmgr.dconf",
                        "missing \"content\" and/or \"template\" dir/key in "
                            "\"op\" = \"fuse\"/\"remove\" set element"
                            " indirection dir " << path);
                    continue;
                }
                path += "content/";
                remove = false;
                replace = op == Op::Replace;
                break;
            case Op::Remove:
                if (!templ.isEmpty() || content) {
                    SAL_WARN(
                        "configmgr.dconf",
                        "bad \"content\" and/or \"template\" dir/key in \"op\" "
                            "= \"remove\" set element indirection dir "
                            << path);
                    continue;
                }
                remove = true;
                replace = false;
                break;
            }
        } else {
            remove = false;
            replace = false;
        }
        rtl::Reference<Node> member(members.findNode(layer, name));
        bool insert = !member.is();
        if (!remove) {
            if (replace || insert) {
                if (!node.is()) {
                    SAL_WARN("configmgr.dconf", "bad unmatched " << path);
                    continue;
                }
                switch (node->kind()) {
                case Node::KIND_LOCALIZED_PROPERTY:
                    member.set(new LocalizedValueNode(layer));
                    break;
                case Node::KIND_GROUP:
                    if (!static_cast<GroupNode *>(node.get())->isExtensible()) {
                        SAL_WARN("configmgr.dconf", "bad unmatched " << path);
                        continue;
                    }
                    member.set(
                        new PropertyNode(
                            layer, TYPE_ANY, true, css::uno::Any(), true));
                    break;
                case Node::KIND_SET:
                    assert(!templ.isEmpty());
                    member = data.getTemplate(layer, templ);
                    if (!member.is()) {
                        SAL_WARN(
                            "configmgr.dconf",
                            "bad " << path << " denoting undefined template "
                                << templ);
                        continue;
                    }
                    member = member->clone(true);
                    break;
                default:
                    assert(false); // cannot happen
                }
            } else if (!templ.isEmpty() && templ != member->getTemplateName()) {
                SAL_WARN(
                    "configmgr.dconf",
                    "bad " << path
                        << " denoting set element of non-matching template "
                        << member->getTemplateName());
                continue;
            }
        }
        if (member.is()) {
            if (member->getFinalized() < layer) {
                continue;
            }
            switch (member->kind()) {
            case Node::KIND_PROPERTY:
                {
                    if (isDir) {
                        SAL_WARN(
                            "configmgr.dconf",
                            "bad dir " << path << " does not match property");
                        continue;
                    }
                    rtl::Reference<PropertyNode> prop(
                        static_cast<PropertyNode *>(member.get()));
                    css::uno::Any value;
                    switch (readValue(
                                client, path, prop->getStaticType(),
                                prop->isNillable(), prop->isExtension(),
                                &value))
                    {
                    case ReadValue::Error:
                        continue;
                    case ReadValue::Value:
                        prop->setValue(layer, value);
                        finalize(client, path, member, layer);
                        break;
                    case ReadValue::Remove:
                        remove = true;
                        break;
                    }
                    break;
                }
            case Node::KIND_LOCALIZED_VALUE:
                {
                    if (isDir) {
                        SAL_WARN(
                            "configmgr.dconf",
                            "bad dir " << path
                                << " does not match localized value");
                        continue;
                    }
                    assert(
                        node.is()
                        && node->kind() == Node::KIND_LOCALIZED_PROPERTY);
                    rtl::Reference<LocalizedPropertyNode> locProp(
                        static_cast<LocalizedPropertyNode *>(node.get()));
                    css::uno::Any value;
                    if (readValue(
                            client, path, locProp->getStaticType(),
                            locProp->isNillable(), false, &value)
                        == ReadValue::Error)
                    {
                        continue;
                    }
                    static_cast<LocalizedValueNode *>(member.get())->setValue(
                        layer, value);
                    finalize(client, path, member, layer);
                    break;
                }
            case Node::KIND_LOCALIZED_PROPERTY:
            case Node::KIND_GROUP:
            case Node::KIND_SET:
                if (!isDir) {
                    SAL_WARN(
                        "configmgr.dconf",
                        "bad key " << path
                            << " does not match localized property, group, or"
                            " set, respectively");
                    continue;
                }
                assert(path.endsWith("/"));
                readDir(
                    data, layer, member, member->getMembers(), client, path);
                break;
            default:
                assert(false); // cannot happen
            }
        }
        if (remove) {
            if (!(member.is() && member->getMandatory())) {
                members.erase(name);
            }
        } else if (replace) {
            members.erase(name);
            members.insert(NodeMap::value_type(name, member));
        } else if (insert) {
            members.insert(NodeMap::value_type(name, member));
        }
    }
}

OString encodeSegment(OUString const & name, bool setElement) {
    if (!setElement) {
        return name.toUtf8();
    }
    OUStringBuffer buf;
    for (sal_Int32 i = 0; i != name.getLength(); ++i) {
        sal_Unicode c = name[i];
        switch (c) {
        case '\0':
            buf.append("\\00");
            break;
        case '/':
            buf.append("\\2F");
            break;
        case '\\':
            buf.append("\\5C");
            break;
        default:
            buf.append(c);
        }
    }
    return buf.makeStringAndClear().toUtf8();
}

OString encodeString(OUString const & value) {
    OUStringBuffer buf;
    for (sal_Int32 i = 0; i != value.getLength(); ++i) {
        sal_Unicode c = value[i];
        switch (c) {
        case '\0':
            buf.append("\\00");
            break;
        case '\\':
            buf.append("\\5C");
            break;
        default:
            buf.append(c);
        }
    }
    return buf.makeStringAndClear().toUtf8();
}

bool addProperty(
    ChangesetHolder const & changeset, OString const & pathRepresentation,
    Type type, bool nillable, css::uno::Any const & value)
{
    Type dynType = getDynamicType(value);
    assert(dynType != TYPE_ERROR);
    if (type == TYPE_ANY) {
        type = dynType;
    }
    GVariantHolder v;
    std::forward_list<GVariantHolder> children;
    if (dynType == TYPE_NIL) {
        switch (type) {
        case TYPE_BOOLEAN:
            v.reset(g_variant_new_maybe(G_VARIANT_TYPE_BOOLEAN, nullptr));
            break;
        case TYPE_SHORT:
            v.reset(g_variant_new_maybe(G_VARIANT_TYPE_INT16, nullptr));
            break;
        case TYPE_INT:
            v.reset(g_variant_new_maybe(G_VARIANT_TYPE_INT32, nullptr));
            break;
        case TYPE_LONG:
            v.reset(g_variant_new_maybe(G_VARIANT_TYPE_INT64, nullptr));
            break;
        case TYPE_DOUBLE:
            v.reset(g_variant_new_maybe(G_VARIANT_TYPE_DOUBLE, nullptr));
            break;
        case TYPE_STRING:
            v.reset(g_variant_new_maybe(G_VARIANT_TYPE_STRING, nullptr));
            break;
        case TYPE_HEXBINARY:
        case TYPE_BOOLEAN_LIST:
        case TYPE_SHORT_LIST:
        case TYPE_INT_LIST:
        case TYPE_LONG_LIST:
        case TYPE_DOUBLE_LIST:
        case TYPE_STRING_LIST:
        case TYPE_HEXBINARY_LIST:
            {
                static char const * const typeString[
                    TYPE_HEXBINARY_LIST - TYPE_HEXBINARY + 1]
                    = { "ay", "ab", "an", "ai", "ax", "ad", "as", "aay" };
                GVariantTypeHolder ty(
                    g_variant_type_new(typeString[type - TYPE_HEXBINARY]));
                if (ty.get() == nullptr) {
                    SAL_WARN("configmgr.dconf", "g_variant_type_new failed");
                    return false;
                }
                v.reset(g_variant_new_maybe(ty.get(), nullptr));
                break;
            }
        default:
            assert(false); // this cannot happen
            break;
        }
        if (v.get() == nullptr) {
            SAL_WARN("configmgr.dconf", "g_variant_new_maybe failed");
            return false;
        }
    } else {
        switch (type) {
        case TYPE_BOOLEAN:
            v.reset(g_variant_new_boolean(value.get<bool>()));
            break;
        case TYPE_SHORT:
            v.reset(g_variant_new_int16(value.get<sal_Int16>()));
            break;
        case TYPE_INT:
            v.reset(g_variant_new_int32(value.get<sal_Int32>()));
            break;
        case TYPE_LONG:
            v.reset(g_variant_new_int64(value.get<sal_Int64>()));
            break;
        case TYPE_DOUBLE:
            v.reset(g_variant_new_double(value.get<double>()));
            break;
        case TYPE_STRING:
            v.reset(
                g_variant_new_string(
                    encodeString(value.get<OUString>()).getStr()));
            break;
        case TYPE_HEXBINARY:
            {
                css::uno::Sequence<sal_Int8> seq(
                    value.get<css::uno::Sequence<sal_Int8>>());
                static_assert(
                    sizeof(sal_Int32) <= sizeof(gsize),
                    "G_MAXSIZE too small");
                static_assert(
                    sizeof (sal_Int8) == sizeof (guchar), "size mismatch");
                v.reset(
                    g_variant_new_fixed_array(
                        G_VARIANT_TYPE_BYTE, seq.getConstArray(),
                        seq.getLength(), sizeof (sal_Int8)));
                break;
            }
        case TYPE_BOOLEAN_LIST:
            {
                css::uno::Sequence<sal_Bool> seq(
                    value.get<css::uno::Sequence<sal_Bool>>());
                static_assert(
                    sizeof(sal_Int32) <= sizeof(gsize),
                    "G_MAXSIZE too small");
                static_assert(sizeof (sal_Bool) == 1, "size mismatch");
                v.reset(
                    g_variant_new_fixed_array(
                        G_VARIANT_TYPE_BOOLEAN, seq.getConstArray(),
                        seq.getLength(), sizeof (sal_Bool)));
                break;
            }
        case TYPE_SHORT_LIST:
            {
                css::uno::Sequence<sal_Int16> seq(
                    value.get<css::uno::Sequence<sal_Int16>>());
                static_assert(
                    sizeof(sal_Int32) <= sizeof(gsize),
                    "G_MAXSIZE too small");
                static_assert(
                    sizeof (sal_Int16) == sizeof (gint16), "size mismatch");
                v.reset(
                    g_variant_new_fixed_array(
                        G_VARIANT_TYPE_INT16, seq.getConstArray(),
                        seq.getLength(), sizeof (sal_Int16)));
                    //TODO: endian-ness?
                break;
            }
        case TYPE_INT_LIST:
            {
                css::uno::Sequence<sal_Int32> seq(
                    value.get<css::uno::Sequence<sal_Int32>>());
                static_assert(
                    sizeof(sal_Int32) <= sizeof(gsize),
                    "G_MAXSIZE too small");
                static_assert(
                    sizeof (sal_Int32) == sizeof (gint32), "size mismatch");
                v.reset(
                    g_variant_new_fixed_array(
                        G_VARIANT_TYPE_INT32, seq.getConstArray(),
                        seq.getLength(), sizeof (sal_Int32)));
                    //TODO: endian-ness?
                break;
            }
        case TYPE_LONG_LIST:
            {
                css::uno::Sequence<sal_Int64> seq(
                    value.get<css::uno::Sequence<sal_Int64>>());
                static_assert(
                    sizeof(sal_Int32) <= sizeof(gsize),
                    "G_MAXSIZE too small");
                static_assert(
                    sizeof (sal_Int64) == sizeof (gint64), "size mismatch");
                v.reset(
                    g_variant_new_fixed_array(
                        G_VARIANT_TYPE_INT64, seq.getConstArray(),
                        seq.getLength(), sizeof (sal_Int64)));
                    //TODO: endian-ness?
                break;
            }
        case TYPE_DOUBLE_LIST:
            {
                css::uno::Sequence<double> seq(
                    value.get<css::uno::Sequence<double>>());
                static_assert(
                    sizeof(sal_Int32) <= sizeof(gsize),
                    "G_MAXSIZE too small");
                static_assert(
                    sizeof (double) == sizeof (gdouble), "size mismatch");
                v.reset(
                    g_variant_new_fixed_array(
                        G_VARIANT_TYPE_DOUBLE, seq.getConstArray(),
                        seq.getLength(), sizeof (double)));
                    //TODO: endian-ness?
                break;
            }
        case TYPE_STRING_LIST:
            {
                const css::uno::Sequence<OUString> seq(
                    value.get<css::uno::Sequence<OUString>>());
                std::vector<GVariant *> vs;
                for (OUString const & s : seq) {
                    children.emplace_front(
                        g_variant_new_string(encodeString(s).getStr()));
                    if (children.front().get() == nullptr) {
                        SAL_WARN(
                            "configmgr.dconf", "g_variant_new_string failed");
                        return false;
                    }
                    vs.push_back(children.front().get());
                }
                static_assert(
                    sizeof(sal_Int32) <= sizeof(gsize),
                    "G_MAXSIZE too small");
                v.reset(
                    g_variant_new_array(
                        G_VARIANT_TYPE_STRING, vs.data(), seq.getLength()));
                break;
            }
        case TYPE_HEXBINARY_LIST:
            {
                const css::uno::Sequence<css::uno::Sequence<sal_Int8>> seqSeq(
                    value.get<
                        css::uno::Sequence<css::uno::Sequence<sal_Int8>>>());
                std::vector<GVariant *> vs;
                for (css::uno::Sequence<sal_Int8> const & seq : seqSeq) {
                    static_assert(
                        sizeof(sal_Int32) <= sizeof(gsize),
                        "G_MAXSIZE too small");
                    static_assert(
                        sizeof (sal_Int8) == sizeof (guchar), "size mismatch");
                    children.emplace_front(
                        g_variant_new_fixed_array(
                            G_VARIANT_TYPE_BYTE, seq.getConstArray(),
                            seq.getLength(), sizeof (sal_Int8)));
                    if (children.front().get() == nullptr) {
                        SAL_WARN(
                            "configmgr.dconf",
                            "g_variant_new_fixed_array failed");
                        return false;
                    }
                    vs.push_back(children.front().get());
                }
                GVariantTypeHolder ty(g_variant_type_new("aay"));
                if (ty.get() == nullptr) {
                    SAL_WARN("configmgr.dconf", "g_variant_type_new failed");
                    return false;
                }
                static_assert(
                    sizeof(sal_Int32) <= sizeof(gsize),
                    "G_MAXSIZE too small");
                v.reset(
                    g_variant_new_array(ty.get(), vs.data(), seqSeq.getLength()));
                break;
            }
        default:
            assert(false); // this cannot happen
            break;
        }
        if (v.get() == nullptr) {
            SAL_WARN("configmgr.dconf", "GVariant creation failed");
            return false;
        }
        if (nillable) {
            GVariantHolder v1(g_variant_new_maybe(nullptr, v.get()));
            if (v1.get() == nullptr) {
                SAL_WARN("configmgr.dconf", "g_variant_new_maybe failed");
                return false;
            }
            v.release();
            v.reset(v1.get());
            v1.release();
        }
    }
    dconf_changeset_set(
        changeset.get(), pathRepresentation.getStr(), v.get());
    for (auto & i: children) {
        i.release();
    }
    v.release();
    return true;
}

bool addNode(
    Components & components, ChangesetHolder const & changeset,
    rtl::Reference<Node> const & parent, OString const & pathRepresentation,
    rtl::Reference<Node> const & node)
{
    switch (node->kind()) {
    case Node::KIND_PROPERTY:
        {
            PropertyNode * prop = static_cast<PropertyNode *>(node.get());
            if (!addProperty(
                    changeset, pathRepresentation, prop->getStaticType(),
                    prop->isNillable(), prop->getValue(components)))
            {
                return false;
            }
            break;
        }
    case Node::KIND_LOCALIZED_VALUE:
        {
            //TODO: name.isEmpty()?
            LocalizedPropertyNode * locprop
                = static_cast<LocalizedPropertyNode *>(parent.get());
            if (!addProperty(
                    changeset, pathRepresentation,
                    locprop->getStaticType(), locprop->isNillable(),
                    static_cast<LocalizedValueNode *>(node.get())->getValue()))
            {
                return false;
            }
            break;
        }
    case Node::KIND_LOCALIZED_PROPERTY:
    case Node::KIND_GROUP:
    case Node::KIND_SET:
        for (auto const & i: node->getMembers()) {
            OUString templ(i.second->getTemplateName());
            OString path(
                pathRepresentation + "/"
                + encodeSegment(i.first, !templ.isEmpty()));
            if (!templ.isEmpty()) {
                path += "/";
                GVariantHolder v(g_variant_new_string("replace"));
                if (v.get() == nullptr) {
                    SAL_WARN("configmgr.dconf", "g_variant_new_string failed");
                    return false;
                }
                dconf_changeset_set(
                    changeset.get(), OString(path + "op").getStr(), v.get());
                v.release();
                v.reset(g_variant_new_string(encodeString(templ).getStr()));
                if (v.get() == nullptr) {
                    SAL_WARN("configmgr.dconf", "g_variant_new_string failed");
                    return false;
                }
                dconf_changeset_set(
                    changeset.get(), OString(path + "template").getStr(),
                    v.get());
                v.release();
                path += "content";
            }
            if (!addNode(components, changeset, parent, path, i.second)) {
                return false;
            }
        }
        break;
    case Node::KIND_ROOT:
        assert(false); // this cannot happen
        break;
    }
    return true;
}

bool addModifications(
    Components & components, ChangesetHolder const & changeset,
    OString const & parentPathRepresentation,
    rtl::Reference<Node> const & parent, OUString const & nodeName,
    rtl::Reference<Node> const & node,
    Modifications::Node const & modifications)
{
    // It is never necessary to write oor:finalized or oor:mandatory attributes,
    // as they cannot be set via the UNO API.
    if (modifications.children.empty()) {
        assert(parent.is());
            // components themselves have no parent but must have children
        if (node.is()) {
            OUString templ(node->getTemplateName());
            OString path(
                parentPathRepresentation + "/"
                + encodeSegment(nodeName, !templ.isEmpty()));
            if (!templ.isEmpty()) {
                path += "/";
                GVariantHolder v(g_variant_new_string("replace"));
                if (v.get() == nullptr) {
                    SAL_WARN("configmgr.dconf", "g_variant_new_string failed");
                    return false;
                }
                dconf_changeset_set(
                    changeset.get(), OString(path + "op").getStr(), v.get());
                v.release();
                v.reset(g_variant_new_string(encodeString(templ).getStr()));
                if (v.get() == nullptr) {
                    SAL_WARN("configmgr.dconf", "g_variant_new_string failed");
                    return false;
                }
                dconf_changeset_set(
                    changeset.get(), OString(path + "template").getStr(),
                    v.get());
                v.release();
                path += "content";
            }
            if (!addNode(components, changeset, parent, path, node)) {
                return false;
            }
        } else {
            switch (parent->kind()) {
            case Node::KIND_LOCALIZED_PROPERTY:
            case Node::KIND_GROUP:
                {
                    GVariantHolder v(g_variant_new_tuple(nullptr, 0));
                    if (v.get() == nullptr) {
                        SAL_WARN(
                            "configmgr.dconf", "g_variant_new_tuple failed");
                        return false;
                    }
                    OString path(parentPathRepresentation);
                    if (!nodeName.isEmpty()) { // KIND_LOCALIZED_PROPERTY
                        path += "/" + encodeSegment(nodeName, false);
                    }
                    dconf_changeset_set(
                        changeset.get(), path.getStr(), v.get());
                    v.release();
                    break;
                }
            case Node::KIND_SET:
                {
                    OString path(
                        parentPathRepresentation + "/"
                        + encodeSegment(nodeName, true) + "/");
                    GVariantHolder v(g_variant_new_string("remove"));
                    if (v.get() == nullptr) {
                        SAL_WARN(
                            "configmgr.dconf", "g_variant_new_string failed");
                        return false;
                    }
                    dconf_changeset_set(
                        changeset.get(), OString(path + "op").getStr(),
                        v.get());
                    v.release();
                    dconf_changeset_set(
                        changeset.get(), OString(path + "template").getStr(),
                        nullptr);
                    dconf_changeset_set(
                        changeset.get(), OString(path + "content/").getStr(),
                        nullptr);
                    break;
                }
            default:
                assert(false); // this cannot happen
                break;
            }
        }
    } else {
        assert(node.is());
        OUString templ(node->getTemplateName());
        OString path(
            parentPathRepresentation + "/"
            + encodeSegment(nodeName, !templ.isEmpty()));
        if (!templ.isEmpty()) {
            path += "/";
            GVariantHolder v(g_variant_new_string("fuse"));
            if (v.get() == nullptr) {
                SAL_WARN("configmgr.dconf", "g_variant_new_string failed");
                return false;
            }
            dconf_changeset_set(
                changeset.get(), OString(path + "op").getStr(), v.get());
            v.release();
            v.reset(g_variant_new_string(encodeString(templ).getStr()));
            if (v.get() == nullptr) {
                SAL_WARN("configmgr.dconf", "g_variant_new_string failed");
                return false;
            }
            dconf_changeset_set(
                changeset.get(), OString(path + "template").getStr(), v.get());
            v.release();
            path += "content";
        }
        for (auto const & i: modifications.children) {
            if (!addModifications(
                    components, changeset, path, node, i.first,
                    node->getMember(i.first), i.second))
            {
                return false;
            }
        }
    }
    return true;
}

}

void readLayer(Data & data, int layer) {<--- The function 'readLayer' is never used.
    GObjectHolder<DConfClient> client(dconf_client_new());
    if (client.get() == nullptr) {
        SAL_WARN("configmgr.dconf", "dconf_client_new failed");
        return;
    }
    readDir(
        data, layer, rtl::Reference<Node>(), data.getComponents(), client,
        getRoot() + "/");
}

void writeModifications(Components & components, Data & data) {
    GObjectHolder<DConfClient> client(dconf_client_new());
    if (client.get() == nullptr) {
        SAL_WARN("configmgr.dconf", "dconf_client_new failed");
    }
    ChangesetHolder cs(dconf_changeset_new());
    if (cs.get() == nullptr) {
        SAL_WARN("configmgr.dconf", "dconf_changeset_new failed");
        return;
    }
    for (auto const & i: data.modifications.getRoot().children) {
        if (!addModifications(
                components, cs, getRoot(), rtl::Reference<Node>(), i.first,
                data.getComponents().findNode(Data::NO_LAYER, i.first),
                i.second))
        {
            return;
        }
    }
    if (!dconf_client_change_sync(
            client.get(), cs.get(), nullptr, nullptr, nullptr))
    {
        //TODO: GError
        SAL_WARN("configmgr.dconf", "dconf_client_change_sync failed");
        return;
    }
    data.modifications.clear();
}

}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */