summaryrefslogtreecommitdiff
path: root/src/plugins/tracker3/rygel-tracker-search-container.vala
blob: bf66d2dca6af3e1e30cd7e30b690eda33ec3ae77 (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
/*
 * Copyright (C) 2008 Zeeshan Ali <zeenix@gmail.com>.
 * Copyright (C) 2008-2012 Nokia Corporation.
 * Copyright (C) 2010 MediaNet Inh.
 *
 * Authors: Zeeshan Ali <zeenix@gmail.com>
 *          Sunil Mohan Adapa <sunil@medhas.org>
 *         Jens Georg <jensg@openismus.com>
 *
 * 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.1 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 library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

using GUPnP;
using Gee;
using Tracker;

/**
 * A container listing a Tracker search result.
 */
public class Rygel.Tracker.SearchContainer : SimpleContainer {
    /* class-wide constants */
    private const string MODIFIED_PROPERTY = "nfo:fileLastModified";

    public SelectionQuery query;
    public ItemFactory item_factory;

    static construct {
        update_id_hash = new HashMap<string, uint> ();
    }

    private static HashMap<string, uint> update_id_hash;

    public SearchContainer (string             id,
                            MediaContainer     parent,
                            string             title,
                            ItemFactory        item_factory,
                            QueryTriplets?     triplets = null,
                            ArrayList<string>? filters = null) {
        base (id, parent, title);

        if (update_id_hash.has_key (this.id)) {
            this.update_id = update_id_hash[this.id];
        }

        this.container_updated.connect ( (container, origin) => {
            if (origin == this) {
                update_id_hash[this.id] = this.update_id;
            }
        });

        this.item_factory = item_factory;

        var variables = new ArrayList<string> ();
        variables.add (SelectionQuery.ITEM_VARIABLE);

        QueryTriplets our_triplets;
        if (triplets != null) {
            our_triplets = triplets;
        } else {
            our_triplets = new QueryTriplets ();
        }

        our_triplets.add (new QueryTriplet
                                (SelectionQuery.ITEM_VARIABLE,
                                 "a",
                                 item_factory.category));
        our_triplets.add (new QueryTriplet
                                (SelectionQuery.ITEM_VARIABLE,
                                 "nie:isStoredAs",
                                SelectionQuery.STORAGE_VARIABLE));

        var property_map = UPnPPropertyMap.get_property_map ();
        foreach (var property in this.item_factory.properties) {
            variables.add (property_map[property]);
        }

        var order_by = MODIFIED_PROPERTY +
                       "(" +
                       SelectionQuery.STORAGE_VARIABLE +
                       ")";

        this.query = new SelectionQuery (variables,
                                         our_triplets,
                                         filters,
                                         this.item_factory.graph,
                                         order_by);

        this.get_children_count.begin ();
    }

    public override async MediaObjects? get_children (uint       offset,
                                                      uint       max_count,
                                                      string     sort_criteria,
                                                      Cancellable? cancellable)
                                                      throws GLib.Error {
        var expression = new RelationalExpression ();
        expression.op = SearchCriteriaOp.EQ;
        expression.operand1 = "@parentID";
        expression.operand2 = this.id;

        uint total_matches;

        return yield this.execute_query (expression,
                                         sort_criteria,
                                         offset,
                                         max_count,
                                         cancellable,
                                         out total_matches);
    }

    public async MediaObjects? execute_query (SearchExpression? expression,
                                              string            sort_criteria,
                                              uint              offset,
                                              uint              max_count,
                                              Cancellable?      cancellable,
                                              out uint          total_matches)
                                              throws GLib.Error {
        var results = new MediaObjects ();

        var query = this.create_query (expression as RelationalExpression,
                                       (int) offset,
                                       (int) max_count,
                                       sort_criteria);

        if (query != null) {
            yield query.execute (RootContainer.connection);

            /* Iterate through all items */
            while (yield query.result.next_async ()) {
                var id = query.result.get_string (0);
                id = this.create_child_id_for_urn (id);
                var uri = query.result.get_string (1);

                var item = this.item_factory.create (id,
                                                     uri,
                                                     this,
                                                     query.result);
                results.add (item);
            }

            query.result.close ();
        }

        total_matches = results.size;

        return results;
    }

    public override async MediaObject? find_object (string       id,
                                                    Cancellable? cancellable)
                                                    throws GLib.Error {
        if (!this.is_our_child (id)) {
            return null;
        }

        var expression = new RelationalExpression ();
        expression.op = SearchCriteriaOp.EQ;
        expression.operand1 = "@id";
        expression.operand2 = id;

        uint total_matches;
        var results = yield this.execute_query (expression,
                                                "",
                                                0,
                                                1,
                                                cancellable,
                                                out total_matches);
        if (results.size > 0) {
            return results[0];
        } else {
            return null;
        }
    }

    public string create_child_id_for_urn (string urn) {
        return this.id + "," + urn;
    }

    // Returns the URN and the ID of the parent this item belongs to, or null
    // if item_id is invalid
    protected string? get_item_info (string     item_id,
                                     out string parent_id) {
        var tokens = item_id.split (",", 2);

        if (tokens[0] != null && tokens[1] != null) {
            parent_id = tokens[0];

            return tokens[1];
        } else {
            parent_id = null;

            return null;
        }
    }

    internal async void get_children_count () {
        try {
            var query = new SelectionQuery.clone (this.query);

            query.variables = new ArrayList<string> ();
            query.variables.add ("COUNT(" +
                                 SelectionQuery.ITEM_VARIABLE +
                                 ") AS ?x");

            yield query.execute (RootContainer.connection);

            if (query.result.next ()) {
                this.child_count = int.parse (query.result.get_string (0));
                this.updated ();
            }

            query.result.close ();
        } catch (GLib.Error error) {
            critical (_("Error getting item count under category ā€œ%sā€: %s"),
                      this.item_factory.category,
                      error.message);

            return;
        }
    }

    private bool is_our_child (string id) {
        return id.has_prefix (this.id + ",");
    }

    private SelectionQuery? create_query (RelationalExpression? expression,
                                          int offset,
                                          int max_count,
                                          string sort_criteria = "") {
        if (expression.operand1 == "upnp:class" &&
            !this.item_factory.upnp_class.has_prefix (expression.operand2)) {
            return null;
        }

        SelectionQuery query;
        if (sort_criteria == null || sort_criteria == "") {
            query = new SelectionQuery.clone (this.query);
        } else {
            query = create_sorted_query (sort_criteria);
        }

        if (expression.operand1 == "@parentID") {
            if (!expression.compare_string (this.id)) {
                return null;
            }
        } else if (expression.operand1 != "upnp:class") {
            var filter = create_filter_for_child (expression);
            if (filter != null) {
                query.filters.insert (0, filter);
            } else {
                return null;
            }
        }

        query.offset = offset;
        query.max_count = max_count;

        return query;
    }

    private SelectionQuery? create_sorted_query (string sort_criteria) {
         var key_chain_map = UPnPPropertyMap.get_property_map ();
         var sort_props = sort_criteria.split (",");
         string order = "";
         ArrayList<string> variables = new ArrayList<string> ();
         ArrayList<string> filters = new ArrayList<string> ();

         variables.add_all (this.query.variables);
         filters.add_all (this.query.filters);

         foreach (string s in sort_props) {
             var key = key_chain_map[s.substring(1)];
             if (key.index_of (SelectionQuery.ITEM_VARIABLE) == 0 ||
                 key.index_of (SelectionQuery.STORAGE_VARIABLE) == 0) {
                 continue;
             }


             if (s.has_prefix("-")) {
                 order += "DESC (" +
                           key + ") ";
             } else {
                 order += key + " ";
             }
         }

         if (order == "") {
             order = this.query.order_by;
         }

         return new SelectionQuery (
                                variables,
                                new QueryTriplets.clone(this.query.triplets),
                                filters,
                                this.item_factory.graph,
                                order);
    }

    private string? urn_to_utf8 (string urn) {
        var urn_builder = new StringBuilder ();
        unowned string s = urn;

        for (; s.get_char () != 0; s = s.next_char ()) {
            unichar character = s.get_char ();
            if (!(character.iscntrl () || !character.validate ())) {
                urn_builder.append_unichar (character);
            }
        }

        return urn_builder.str;
    }

    private string? create_filter_for_child (RelationalExpression expression) {
        string filter = null;
        string variable = null;
        string value = null;

        if (expression.operand1 == "@id") {
            variable = SelectionQuery.ITEM_VARIABLE;

            string parent_id;

            var urn = this.get_item_info (expression.operand2, out parent_id);

            if (!urn.validate ()) {
                urn = urn_to_utf8 (urn);
            }

            if (urn == null || parent_id == null || parent_id != this.id) {
                return null;
            }

            urn = Query.escape_string (urn);

            switch (expression.op) {
                case SearchCriteriaOp.EQ:
                    value = "<" + urn + ">";
                    break;
                case SearchCriteriaOp.CONTAINS:
                    value = expression.operand2;
                    break;
            }
        }

        if (variable == null || value == null) {
            return null;
        }

        switch (expression.op) {
            case SearchCriteriaOp.EQ:
                filter = variable + " = " + value;
                break;
            case SearchCriteriaOp.CONTAINS:
                // We need to escape this twice for Tracker
                var regex = Query.escape_regex (value);

                filter = "regex(" + variable + ", \"" + regex + "\", \"i\")";
                break;
        }

        return filter;
    }
}