summaryrefslogtreecommitdiff
path: root/src/plugins/media-export/rygel-media-export-node-query-container.vala
blob: c181fdb2a5c99188328d70791787e729cdade639 (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
/*
 * Copyright (C) 2011 Jens Georg <mail@jensge.org>.
 *
 * This file is part of Rygel.
 *
 * Rygel 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 of the License, or
 * (at your option) any later version.
 *
 * Rygel 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 program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

internal class Rygel.MediaExport.NodeQueryContainer : QueryContainer {
    private string template;
    private string attribute;

    public NodeQueryContainer (MediaCache       cache,
                               SearchExpression expression,
                               string           id,
                               string           name,
                               string           template,
                               string           attribute) {
        base (cache, expression, id, name);

        this.template = template;
        this.attribute = attribute;

        // base constructor does count_children but it depends on template and
        // attribute; so we have to call it again here after those two have
        // been set.
        try {
            this.child_count = this.count_children ();
        } catch (Error error) {};
    }

    // MediaContainer overrides

    public override async MediaObjects? get_children
                                        (uint         offset,
                                         uint         max_count,
                                         string?      sort_criteria,
                                         Cancellable? cancellable)
                                         throws GLib.Error {
        var children = new MediaObjects ();
        var data = this.media_db.get_object_attribute_by_search_expression
                                        (this.attribute,
                                         this.expression,
                                         // sort criteria
                                         offset,
                                         max_count);

        foreach (var meta_data in data) {
            var new_id = Uri.escape_string (meta_data, "", true);
            // template contains URL escaped text. This means it might
            // contain '%' chars which will makes sprintf crash
            new_id = this.template.replace ("%s", new_id);
            var factory = QueryContainerFactory.get_default ();
            var container = factory.create_from_description (this.media_db,
                                                             new_id,
                                                             meta_data);
            container.parent = this;
            children.add (container);
        }

        return children;
    }

    // QueryContainer overrides

    protected override int count_children () throws Error {
        // Happens during construction
        if (this.attribute == null || this.expression == null) {
            return 0;
        }

        var data = this.media_db.get_object_attribute_by_search_expression
                                        (this.attribute,
                                         this.expression,
                                         0,
                                         -1);
        return data.size;
    }
}