summaryrefslogtreecommitdiff
path: root/editor/dconf-model.vala
blob: 46e8f6c890dac99e1ee814b9f54f6e27da2e3750 (plain)
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
public class Key : GLib.Object
{
    private SettingsModel model;

    public Directory? parent;

    public string name;
    public string full_name;

    public SchemaKey? schema;
    
    public bool has_schema
    {
        get { return schema != null; }
    }

    public int index
    {
        get { return parent.keys.index (this); }
    }

    public string type_string
    {
       private set {}
       public get
       {
           if (value != null)
           {
               if (value.is_of_type(VariantType.STRING) && has_schema && schema.enum_name != null)
                   return "<enum>";
               else
                   return value.get_type_string();
           }
           else
               return schema.type;
       }
    }

    private Variant? _value;
    public Variant value
    {
        get
        {
            update_value();
            if (_value != null)
                return _value;
            else
                return schema.default_value;
        }
        set
        {
            _value = value;
            try
            {
                model.client.write(full_name, value);
            }
            catch (GLib.Error e)
            {
            }
            value_changed();
        }
    }

    public Variant? get_min()
    {
        switch (value.classify ())
        {
        case Variant.Class.BYTE:
            return new Variant.byte(0);
        case Variant.Class.INT16:
            return new Variant.int16(int16.MIN);
        case Variant.Class.UINT16:
            return new Variant.uint16(uint16.MIN);
        case Variant.Class.INT32:
            return new Variant.int32(int32.MIN);
        case Variant.Class.UINT32:
            return new Variant.uint32(uint32.MIN);
        case Variant.Class.INT64:
            return new Variant.int64(int64.MIN);
        case Variant.Class.UINT64:
            return new Variant.uint64(uint64.MIN);
        case Variant.Class.DOUBLE:
            return new Variant.double(double.MIN);
        default:
            return null;
        }
    }

    public Variant? get_max()
    {
        switch (value.classify ())
        {
        case Variant.Class.BYTE:
            return new Variant.byte(255);
        case Variant.Class.INT16:
            return new Variant.int16(int16.MAX);
        case Variant.Class.UINT16:
            return new Variant.uint16(uint16.MAX);
        case Variant.Class.INT32:
            return new Variant.int32(int32.MAX);
        case Variant.Class.UINT32:
            return new Variant.uint32(uint32.MAX);
        case Variant.Class.INT64:
            return new Variant.int64(int64.MAX);
        case Variant.Class.UINT64:
            return new Variant.uint64(uint64.MAX);
        case Variant.Class.DOUBLE:
            return new Variant.double(double.MAX);
        default:
            return null;
        }
    }

    public bool is_default
    {
        get { update_value(); return _value == null; }
    }

    public signal void value_changed();

    public Key(SettingsModel model, Directory parent, string name, string full_name)
    {
        this.model = model;
        this.parent = parent;
        this.name = name;
        this.full_name = full_name;
        this.schema = model.schemas.keys.lookup(full_name);
    }

    public void set_to_default()
    {
        if (!has_schema)
            return;

        _value = null;
        try
        {
            model.client.write(full_name, null);
        }
        catch (GLib.Error e)
        {
        }
        value_changed();
    }

    private void update_value()
    {
        _value = model.client.read(full_name);
    }
}

public class Directory : GLib.Object
{
    private SettingsModel model;

    public string name;
    public string full_name;

    public Directory? parent;

    private KeyModel _key_model;
    public KeyModel key_model
    {
        get { update_children(); if (_key_model == null) _key_model = new KeyModel(this); return _key_model; }
        private set {}
    }

    public int index
    {
       get { return parent.children.index (this); }
    }

    public GLib.HashTable<string, Directory> _child_map = new GLib.HashTable<string, Directory>(str_hash, str_equal);
    public GLib.List<Directory> _children = new GLib.List<Directory>();
    public GLib.List<Directory> children
    {
        get { update_children(); return _children; }
        private set { }
    }

    public GLib.HashTable<string, Key> _key_map = new GLib.HashTable<string, Key>(str_hash, str_equal);
    private GLib.List<Key> _keys = new GLib.List<Key>();
    public GLib.List<Key> keys
    {
        get { update_children(); return _keys; }
        private set { }
    }

    private bool have_children;
    
    public Directory(SettingsModel model, Directory? parent, string name, string full_name)
    {
        this.model = model;
        this.parent = parent;
        this.name = name;
        this.full_name = full_name;
    }
    
    public Directory get_child(string name)
    {
        Directory? directory = _child_map.lookup(name);

        if (directory == null)
        {
            directory = new Directory(model, this, name, full_name + name + "/");
            _children.insert_sorted(directory, compare_directories);
            _child_map.insert(name, directory);
        }

        return directory;
    }

    private static int compare_directories(Directory a, Directory b)
    {
        return strcmp(a.name, b.name);
    }

    public Key get_key(string name)
    {
        Key? key = _key_map.lookup(name);

        if (key == null)
        {
            key = new Key(model, this, name, full_name + name);
            _keys.insert_sorted(key, compare_keys);
            _key_map.insert(name, key);
        }

        return key;
    }

    public static int compare_keys(Key a, Key b)
    {
        return strcmp(a.name, b.name);
    }

    public void load_schema(Schema schema, string path)
    {
        if (path == "")
        {
            foreach (var schema_key in schema.keys.get_values())
                get_key(schema_key.name);
        }
        else
        {
            string[] tokens = path.split("/", 2);
            string name = tokens[0];

            Directory directory = get_child(name);
            directory.load_schema(schema, tokens[1]);
        }
    }

    private void update_children()
    {
        if (have_children)
            return;
        have_children = true;

        string[] items = model.client.list(full_name);
        for (int i = 0; i < items.length; i++)
        {
            string item_name = full_name + items[i];

            if (DConf.is_dir(item_name))
            {
                string dir_name = items[i][0:-1];
                get_child(dir_name);
            }
            else
            {
                get_key(items[i]);
            }
        }
    }
}

public class KeyModel: GLib.Object, Gtk.TreeModel
{
    private Directory directory;

    public KeyModel(Directory directory)
    {
        this.directory = directory;
        foreach (var key in directory.keys)
            key.value_changed.connect(key_changed_cb); // FIXME: Need to delete this callbacks
    }

    private void key_changed_cb(Key key)
    {
        Gtk.TreeIter iter;
        if (!get_iter_first(out iter))
            return;

        do
        {
            if(get_key(iter) == key)
            {
                row_changed(get_path(iter), iter);
                return;
            }
        } while(iter_next(ref iter));
    }

    public Gtk.TreeModelFlags get_flags()
    {
        return Gtk.TreeModelFlags.LIST_ONLY;
    }

    public int get_n_columns()
    {
        return 3;
    }

    public Type get_column_type(int index)
    {
        if (index == 0)
            return typeof(Key);
        else
            return typeof(string);
    }
    
    private void set_iter(out Gtk.TreeIter iter, Key key)
    {
        iter.stamp = 0;
        iter.user_data = key;
        iter.user_data2 = key;
        iter.user_data3 = key;
    }

    public Key get_key(Gtk.TreeIter iter)
    {
        return (Key)iter.user_data;
    }

    public bool get_iter(out Gtk.TreeIter iter, Gtk.TreePath path)
    {
        if (path.get_depth() != 1)
            return false;

        return iter_nth_child(out iter, null, path.get_indices()[0]);
    }

    public Gtk.TreePath get_path(Gtk.TreeIter iter)
    {
        var path = new Gtk.TreePath();
        path.append_index(get_key(iter).index);
        return path;
    }

    public void get_value(Gtk.TreeIter iter, int column, out Value value)
    {
        Key key = get_key(iter);

        if (column == 0)
            value = key;
        else if (column == 1)
            value = key.name;
        else if (column == 2)
        {
            if (key.value != null)
                value = key.value.print(false);
            else
                value = "";
        }
        else if (column == 4)
        {
            if (key.is_default)
                value = Pango.Weight.NORMAL;            
            else
                value = Pango.Weight.BOLD;
        }
    }

    public bool iter_next(ref Gtk.TreeIter iter)
    {
        int index = get_key(iter).index;
        if (index >= directory.keys.length() - 1)
            return false;
        set_iter(out iter, directory.keys.nth_data(index+1));
        return true;
    }

    public bool iter_children(out Gtk.TreeIter iter, Gtk.TreeIter? parent)
    {
        if (parent != null || directory.keys.length() == 0)
            return false;
        set_iter(out iter, directory.keys.nth_data(0));
        return true;
    }

    public bool iter_has_child(Gtk.TreeIter iter)
    {
        return false;
    }

    public int iter_n_children(Gtk.TreeIter? iter)
    {
        if (iter == null)
            return (int)directory.keys.length();
        else
            return 0;
    }

    public bool iter_nth_child(out Gtk.TreeIter iter, Gtk.TreeIter? parent, int n)
    {
        if (parent != null)
            return false;

        if (n >= directory.keys.length())
            return false;
        set_iter(out iter, directory.keys.nth_data(n));
        return true;
    }

    public bool iter_parent(out Gtk.TreeIter iter, Gtk.TreeIter child)
    {
        return false;
    }

    public void ref_node(Gtk.TreeIter iter)
    {
        get_key(iter).ref();
    }

    public void unref_node(Gtk.TreeIter iter)
    {
        get_key(iter).unref();
    }
}

public class EnumModel: GLib.Object, Gtk.TreeModel
{
    private SchemaEnum schema_enum;

    public EnumModel(SchemaEnum schema_enum)
    {
        this.schema_enum = schema_enum;
    }

    public Gtk.TreeModelFlags get_flags()
    {
        return Gtk.TreeModelFlags.LIST_ONLY;
    }

    public int get_n_columns()
    {
        return 2;
    }

    public Type get_column_type(int index)
    {
        if (index == 0)
            return typeof(string);
        else
            return typeof(int);
    }
    
    private void set_iter(out Gtk.TreeIter iter, SchemaEnumValue value)
    {
        iter.stamp = 0;
        iter.user_data = value;
        iter.user_data2 = value;
        iter.user_data3 = value;
    }

    public SchemaEnumValue get_enum_value(Gtk.TreeIter iter)
    {
        return (SchemaEnumValue)iter.user_data;
    }

    public bool get_iter(out Gtk.TreeIter iter, Gtk.TreePath path)
    {
        if (path.get_depth() != 1)
            return false;

        return iter_nth_child(out iter, null, path.get_indices()[0]);
    }

    public Gtk.TreePath get_path(Gtk.TreeIter iter)
    {
        var path = new Gtk.TreePath();
        path.append_index((int)get_enum_value(iter).index);
        return path;
    }

    public void get_value(Gtk.TreeIter iter, int column, out Value value)
    {
        if (column == 0)
            value = get_enum_value(iter).nick;
        else if (column == 1)
            value = get_enum_value(iter).value;
    }

    public bool iter_next(ref Gtk.TreeIter iter)
    {
        uint index = get_enum_value(iter).index;
        if (index >= schema_enum.values.length () - 1)
            return false;
        set_iter(out iter, schema_enum.values.nth_data(index + 1));
        return true;
    }

    public bool iter_children(out Gtk.TreeIter iter, Gtk.TreeIter? parent)
    {
        if (parent != null || schema_enum.values.length() == 0)
            return false;
        set_iter(out iter, schema_enum.values.nth_data(0));
        return true;
    }

    public bool iter_has_child(Gtk.TreeIter iter)
    {
        return false;
    }

    public int iter_n_children(Gtk.TreeIter? iter)
    {
        if (iter == null)
            return (int) schema_enum.values.length();
        else
            return 0;
    }

    public bool iter_nth_child(out Gtk.TreeIter iter, Gtk.TreeIter? parent, int n)
    {
        if (parent != null)
            return false;

        if (n >= schema_enum.values.length())
            return false;
        set_iter(out iter, schema_enum.values.nth_data(n));
        return true;
    }

    public bool iter_parent(out Gtk.TreeIter iter, Gtk.TreeIter child)
    {
        return false;
    }

    public void ref_node(Gtk.TreeIter iter)
    {
        get_enum_value(iter).ref();
    }

    public void unref_node(Gtk.TreeIter iter)
    {
        get_enum_value(iter).unref();
    }
}

public class SettingsModel: GLib.Object, Gtk.TreeModel
{
    public SchemaList schemas;

    public DConf.Client client;
    private Directory root;

    public SettingsModel()
    {
        client = new DConf.Client ();
        root = new Directory(this, null, "/", "/");

        schemas = new SchemaList();
        try
        {
            foreach (var dir in GLib.Environment.get_system_data_dirs())
            {
                var path = Path.build_filename (dir, "glib-2.0", "schemas", null);
                if (File.new_for_path (path).query_exists ())
                    schemas.load_directory (path);
            }

            var dir = GLib.Environment.get_variable ("GSETTINGS_SCHEMA_DIR");
            if (dir != null)
                schemas.load_directory(dir);
        } catch (Error e) {
            warning("Failed to parse schemas: %s", e.message);
        }

        /* Add keys for the values in the schemas */
        foreach (var schema in schemas.schemas)
            root.load_schema(schema, schema.path[1:schema.path.length]);
    }

    public Gtk.TreeModelFlags get_flags()
    {
        return 0;
    }

    public int get_n_columns()
    {
        return 2;
    }

    public Type get_column_type(int index)
    {
        if (index == 0)
            return typeof(Directory);
        else
            return typeof(string);
    }
    
    private void set_iter(out Gtk.TreeIter iter, Directory directory)
    {
        iter.stamp = 0;
        iter.user_data = directory;
        iter.user_data2 = directory;
        iter.user_data3 = directory;
    }

    public Directory get_directory(Gtk.TreeIter? iter)
    {
        if (iter == null)
            return root;
        else
            return (Directory)iter.user_data;
    }

    public bool get_iter(out Gtk.TreeIter iter, Gtk.TreePath path)
    {
        if (!iter_nth_child(out iter, null, path.get_indices()[0]))
            return false;

        for (int i = 1; i < path.get_depth(); i++)
        {
            Gtk.TreeIter parent = iter;
            if (!iter_nth_child(out iter, parent, path.get_indices()[i]))
                return false;
        }

        return true;
    }

    public Gtk.TreePath get_path(Gtk.TreeIter iter)
    {
        var path = new Gtk.TreePath();
        for (var d = get_directory(iter); d != root; d = d.parent)
            path.prepend_index((int)d.index);
        return path;
    }

    public void get_value(Gtk.TreeIter iter, int column, out Value value)
    {
        if (column == 0)
            value = get_directory(iter);
        else
            value = get_directory(iter).name;
    }

    public bool iter_next(ref Gtk.TreeIter iter)
    {
        Directory directory = get_directory(iter);
        if (directory.index >= directory.parent.children.length() - 1)
            return false;
        set_iter(out iter, directory.parent.children.nth_data(directory.index+1));
        return true;
    }

    public bool iter_children(out Gtk.TreeIter iter, Gtk.TreeIter? parent)
    {
        Directory directory = get_directory(parent);
        if (directory.children.length() == 0)
            return false;
        set_iter(out iter, directory.children.nth_data(0));
        return true;
    }

    public bool iter_has_child(Gtk.TreeIter iter)
    {
        return get_directory(iter).children.length() > 0;
    }

    public int iter_n_children(Gtk.TreeIter? iter)
    {
        return (int) get_directory(iter).children.length();
    }

    public bool iter_nth_child(out Gtk.TreeIter iter, Gtk.TreeIter? parent, int n)
    {
        Directory directory = get_directory(parent);
        if (n >= directory.children.length())
            return false;
        set_iter(out iter, directory.children.nth_data(n));
        return true;
    }

    public bool iter_parent(out Gtk.TreeIter iter, Gtk.TreeIter child)
    {
        Directory directory = get_directory(child);
        if (directory.parent == root)
            return false;
        set_iter(out iter, directory.parent);
        return true;
    }

    public void ref_node(Gtk.TreeIter iter)
    {
        get_directory(iter).ref();
    }

    public void unref_node(Gtk.TreeIter iter)
    {
        get_directory(iter).unref();
    }
}