summaryrefslogtreecommitdiff
path: root/src/plugins/tracker/rygel-tracker-category-all-container.vala
blob: 460c1d1781884cc11944d345dd6cab93cde7ce76 (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
/*
 * Copyright (C) 2008 Zeeshan Ali <zeenix@gmail.com>.
 * Copyright (C) 2008-2012 Nokia Corporation.
 *
 * Author: Zeeshan Ali (Khattak) <zeeshanak@gnome.org>
 *                               <zeeshan.ali@nokia.com>
 *         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 Gee;
using Tracker;

/**
 * A search container that contains all the items in a category.
 */
public class Rygel.Tracker.CategoryAllContainer : SearchContainer,
                                                  WritableContainer,
                                                  SearchableContainer {
    /* class-wide constants */
    private const string TRACKER_SERVICE = "org.freedesktop.Tracker1";
    private const string RESOURCES_PATH = "/org/freedesktop/Tracker1/Resources";

    public ArrayList<string> create_classes { get; set; }
    public ArrayList<string> search_classes { get; set; }

    private Sparql.Connection resources;

    public CategoryAllContainer (CategoryContainer parent) {
        base ("All" + parent.id, parent, "All", parent.item_factory);

        this.create_classes = new ArrayList<string> ();
        this.create_classes.add (item_factory.upnp_class);
        this.search_classes = new ArrayList<string> ();

        try {
            this.resources = Sparql.Connection.get ();
        } catch (Error io_error) {
            critical (_("Failed to create a Tracker connection: %s"),
                      io_error.message);
        }

        if (item_factory.upload_dir != null) {
            try {
                var uri = Filename.to_uri (item_factory.upload_dir, null);
                this.add_uri (uri);
            } catch (ConvertError error) {
                warning (_("Failed to construct URI for folder ā€œ%sā€: %s"),
                        item_factory.upload_dir,
                        error.message);
            }
        }

        try {
            var connection = Bus.get_sync (BusType.SESSION);
            connection.signal_subscribe (TRACKER_SERVICE,
                                         TRACKER_SERVICE + ".Resources",
                                         "GraphUpdated",
                                         RESOURCES_PATH,
                                         this.item_factory.category_iri,
                                         DBusSignalFlags.NONE,
                                         this.on_graph_updated);
        } catch (Error error) {
            critical (_("Could not subscribe to Tracker signals: %s"),
                      error.message);
        }

        var cleanup_query = new CleanupQuery (this.item_factory.category);
        cleanup_query.execute.begin (this.resources);
    }

    public async void add_item (MediaFileItem item, Cancellable? cancellable)
                                throws Error {
        var urn = yield this.create_entry_in_store (item);

        item.id = this.create_child_id_for_urn (urn);
        item.parent = this;
    }

    public async void add_container (MediaContainer container,
                                     Cancellable? cancellable) throws Error {
        throw new WritableContainerError.NOT_IMPLEMENTED (_("Not supported"));
    }

    public async void remove_item (string id, Cancellable? cancellable)
                                   throws Error {
        string parent_id;

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

        yield this.remove_entry_from_store (urn);
    }

    public async void remove_container (string id, Cancellable? cancellable)
                                        throws Error {
        throw new WritableContainerError.NOT_IMPLEMENTED ("Not supported");
    }

    public async MediaObjects? search (SearchExpression? expression,
                                       uint              offset,
                                       uint              max_count,
                                       string            sort_criteria,
                                       Cancellable?      cancellable,
                                       out uint          total_matches)
                                       throws Error {
        return yield this.simple_search (expression,
                                         offset,
                                         max_count,
                                         sort_criteria,
                                         cancellable,
                                         out total_matches);
    }

    private void on_graph_updated (DBusConnection connection,
                                   string         sender,
                                   string         object_path,
                                   string         interface_name,
                                   string         signal_path,
                                   Variant        parameters) {
        this.get_children_count.begin ();
    }

    private async string create_entry_in_store (MediaFileItem item)
                                                throws Error {
        var category = this.item_factory.category;
        var query = new InsertionQuery (item, category);

        yield query.execute (this.resources);

        return query.id;
    }

    private async void remove_entry_from_store (string id) throws Error {
        var query = new DeletionQuery (id);

        yield query.execute (this.resources);
    }
}