~ubuntu-branches/ubuntu/precise/rygel/precise

« back to all changes in this revision

Viewing changes to src/plugins/tracker/rygel-tracker-search-container.vala

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Henriksson
  • Date: 2009-09-26 14:04:05 UTC
  • Revision ID: james.westby@ubuntu.com-20090926140405-d5x3j13k10psa1gu
Tags: upstream-0.4.1
ImportĀ upstreamĀ versionĀ 0.4.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2008 Zeeshan Ali <zeenix@gmail.com>.
 
3
 * Copyright (C) 2008 Nokia Corporation.
 
4
 *
 
5
 * Author: Zeeshan Ali <zeenix@gmail.com>
 
6
 *
 
7
 * This file is part of Rygel.
 
8
 *
 
9
 * Rygel is free software; you can redistribute it and/or modify
 
10
 * it under the terms of the GNU Lesser General Public License as published by
 
11
 * the Free Software Foundation; either version 2 of the License, or
 
12
 * (at your option) any later version.
 
13
 *
 
14
 * Rygel is distributed in the hope that it will be useful,
 
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
17
 * GNU Lesser General Public License for more details.
 
18
 *
 
19
 * You should have received a copy of the GNU Lesser General Public License
 
20
 * along with this program; if not, write to the Free Software Foundation,
 
21
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 
22
 */
 
23
 
 
24
using GUPnP;
 
25
using DBus;
 
26
using Gee;
 
27
 
 
28
/**
 
29
 * A container listing a Tracker search result.
 
30
 */
 
31
public class Rygel.TrackerSearchContainer : Rygel.MediaContainer {
 
32
    /* class-wide constants */
 
33
    private const string TRACKER_SERVICE = "org.freedesktop.Tracker";
 
34
    private const string TRACKER_PATH = "/org/freedesktop/Tracker";
 
35
    private const string TRACKER_IFACE = "org.freedesktop.Tracker";
 
36
    private const string SEARCH_PATH = "/org/freedesktop/Tracker/Search";
 
37
    private const string SEARCH_IFACE = "org.freedesktop.Tracker.Search";
 
38
    private const string METADATA_PATH = "/org/freedesktop/Tracker/Metadata";
 
39
    private const string METADATA_IFACE = "org.freedesktop.Tracker.Metadata";
 
40
 
 
41
    public dynamic DBus.Object metadata;
 
42
    public dynamic DBus.Object search;
 
43
    public dynamic DBus.Object tracker;
 
44
 
 
45
    public string service;
 
46
 
 
47
    public string query_condition;
 
48
 
 
49
    public string[] keywords;
 
50
 
 
51
    Gee.List<AsyncResult> results;
 
52
 
 
53
    public TrackerSearchContainer (string         id,
 
54
                                   MediaContainer parent,
 
55
                                   string         title,
 
56
                                   string         service,
 
57
                                   string         query_condition = "",
 
58
                                   string[]       keywords = new string[0]) {
 
59
        base (id, parent, title, 0);
 
60
 
 
61
        this.service = service;
 
62
        this.keywords = keywords;
 
63
        this.query_condition = query_condition;
 
64
 
 
65
        try {
 
66
            this.create_proxies ();
 
67
 
 
68
            /* FIXME: We need to hook to some tracker signals to keep
 
69
             *        this field up2date at all times
 
70
             */
 
71
            this.get_children_count ();
 
72
 
 
73
            this.results = new Gee.ArrayList<AsyncResult>();
 
74
        } catch (DBus.Error error) {
 
75
            critical ("Failed to connect to session bus: %s\n", error.message);
 
76
        }
 
77
    }
 
78
 
 
79
    private void get_children_count () {
 
80
        try {
 
81
            // We are performing actual search (though an optimized one) to get
 
82
            // the hitcount rather than GetHitCount because GetHitCount only
 
83
            // allows us to get hit count for Text searches.
 
84
            this.search.Query (0,
 
85
                               this.service,
 
86
                               new string[0],
 
87
                               "",
 
88
                               this.keywords,
 
89
                               this.query_condition,
 
90
                               false,
 
91
                               new string[0],
 
92
                               false,
 
93
                               0,
 
94
                               -1,
 
95
                               on_search_query_cb);
 
96
        } catch (GLib.Error error) {
 
97
            critical ("error getting items under service '%s': %s",
 
98
                      this.service,
 
99
                      error.message);
 
100
 
 
101
            return;
 
102
        }
 
103
    }
 
104
 
 
105
    private void on_search_query_cb (string[][] search_result,
 
106
                                     GLib.Error error) {
 
107
        if (error != null) {
 
108
            critical ("error getting items under service '%s': %s",
 
109
                      this.service,
 
110
                      error.message);
 
111
 
 
112
            return;
 
113
        }
 
114
 
 
115
        this.child_count = search_result.length;
 
116
        this.updated ();
 
117
    }
 
118
 
 
119
    public override void get_children (uint               offset,
 
120
                                       uint               max_count,
 
121
                                       Cancellable?       cancellable,
 
122
                                       AsyncReadyCallback callback) {
 
123
        var res = new TrackerSearchResult (this, callback);
 
124
 
 
125
        this.results.add (res);
 
126
 
 
127
        try {
 
128
            this.search.Query (0,
 
129
                               this.service,
 
130
                               TrackerItem.get_metadata_keys (),
 
131
                               "",
 
132
                               this.keywords,
 
133
                               this.query_condition,
 
134
                               false,
 
135
                               new string[0],
 
136
                               false,
 
137
                               (int) offset,
 
138
                               (int) max_count,
 
139
                               res.ready);
 
140
        } catch (GLib.Error error) {
 
141
            res.error = error;
 
142
 
 
143
            res.complete_in_idle ();
 
144
        }
 
145
    }
 
146
 
 
147
    public override Gee.List<MediaObject>? get_children_finish (
 
148
                                                         AsyncResult res)
 
149
                                                         throws GLib.Error {
 
150
        var search_res = (Rygel.TrackerSearchResult) res;
 
151
 
 
152
        this.results.remove (search_res);
 
153
 
 
154
        if (search_res.error != null) {
 
155
            throw search_res.error;
 
156
        } else {
 
157
            return search_res.data;
 
158
        }
 
159
    }
 
160
 
 
161
    public override void find_object (string             id,
 
162
                                      Cancellable?       cancellable,
 
163
                                      AsyncReadyCallback callback) {
 
164
        var res = new TrackerGetMetadataResult (this, callback, id);
 
165
 
 
166
        this.results.add (res);
 
167
 
 
168
        try {
 
169
            string parent_id;
 
170
 
 
171
            res.item_path = this.get_item_info (id,
 
172
                                                out parent_id,
 
173
                                                out res.item_service);
 
174
            if (res.item_path == null) {
 
175
                res.complete_in_idle ();
 
176
 
 
177
                return;
 
178
            }
 
179
 
 
180
            string[] keys = TrackerItem.get_metadata_keys ();
 
181
 
 
182
            this.metadata.Get (res.item_service,
 
183
                               res.item_path,
 
184
                               keys,
 
185
                               res.ready);
 
186
        } catch (GLib.Error error) {
 
187
            res.error = error;
 
188
 
 
189
            res.complete_in_idle ();
 
190
        }
 
191
    }
 
192
 
 
193
    public override MediaObject? find_object_finish (AsyncResult res)
 
194
                                                     throws GLib.Error {
 
195
        var metadata_res = (TrackerGetMetadataResult) res;
 
196
 
 
197
        if (metadata_res.error != null) {
 
198
            throw metadata_res.error;
 
199
        } else {
 
200
            return metadata_res.data;
 
201
        }
 
202
    }
 
203
 
 
204
    public bool is_thy_child (string item_id) {
 
205
        string parent_id = null;
 
206
        this.get_item_info (id, out parent_id, out service);
 
207
 
 
208
        if (parent_id != null && parent_id == this.id) {
 
209
            return true;
 
210
        } else {
 
211
            return false;
 
212
        }
 
213
    }
 
214
 
 
215
    public MediaItem? create_item (string   service,
 
216
                                   string   path,
 
217
                                   string[] metadata) {
 
218
        var id = service + ":" + this.id + ":" + path;
 
219
 
 
220
        if (service == TrackerVideoItem.SERVICE) {
 
221
            return new TrackerVideoItem (id,
 
222
                                         path,
 
223
                                         this,
 
224
                                         metadata);
 
225
        } else if (service == TrackerImageItem.SERVICE) {
 
226
            return new TrackerImageItem (id,
 
227
                                         path,
 
228
                                         this,
 
229
                                         metadata);
 
230
        } else if (service == TrackerMusicItem.SERVICE) {
 
231
            return new TrackerMusicItem (id,
 
232
                                         path,
 
233
                                         this,
 
234
                                         metadata);
 
235
        } else {
 
236
            return null;
 
237
        }
 
238
    }
 
239
 
 
240
    // Returns the path, ID of the parent and service this item belongs to, or
 
241
    // null item_id is invalid
 
242
    public string? get_item_info (string     item_id,
 
243
                                  out string parent_id,
 
244
                                  out string service) {
 
245
        var tokens = item_id.split (":", 3);
 
246
 
 
247
        if (tokens[0] != null && tokens[1] != null && tokens[2] != null) {
 
248
            service = tokens[0];
 
249
            parent_id = tokens[1];
 
250
 
 
251
            return tokens[2];
 
252
        } else {
 
253
            return null;
 
254
        }
 
255
    }
 
256
 
 
257
    private void create_proxies () throws DBus.Error {
 
258
        DBus.Connection connection = DBus.Bus.get (DBus.BusType.SESSION);
 
259
 
 
260
        this.metadata = connection.get_object (TRACKER_SERVICE,
 
261
                                               METADATA_PATH,
 
262
                                               METADATA_IFACE);
 
263
        this.search = connection.get_object (TRACKER_SERVICE,
 
264
                                             SEARCH_PATH,
 
265
                                             SEARCH_IFACE);
 
266
        this.tracker = connection.get_object (TRACKER_SERVICE,
 
267
                                              TRACKER_PATH,
 
268
                                              TRACKER_IFACE);
 
269
    }
 
270
}
 
271