Add initial source files for rygel-gst-0-10-plugins (1f5e770)
[profile/ivi/rygel-gst-0-10-plugins.git] / src / media-export / rygel-media-export-leaf-query-container.c
1 /*
2  * Copyright (C) 2011 Jens Georg <mail@jensge.org>.
3  * Copyright (C) 2013 Intel Corporation.
4  *
5  * This file is part of Rygel.
6  *
7  * Rygel is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * Rygel is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this program; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  */
21
22 #include "rygel-media-export-leaf-query-container.h"
23
24 G_DEFINE_TYPE (RygelMediaExportLeafQueryContainer,
25                rygel_media_export_leaf_query_container,
26                RYGEL_MEDIA_EXPORT_TYPE_QUERY_CONTAINER);
27
28 RygelMediaExportLeafQueryContainer*
29 rygel_media_export_leaf_query_container_new (RygelSearchExpression *expression,
30                                              const gchar           *id,
31                                              const gchar           *name) {
32   g_return_val_if_fail (RYGEL_IS_SEARCH_EXPRESSION (expression), NULL);
33   g_return_val_if_fail (id != NULL, NULL);
34   g_return_val_if_fail (name != NULL, NULL);
35
36   return RYGEL_MEDIA_EXPORT_LEAF_QUERY_CONTAINER (g_object_new (RYGEL_MEDIA_EXPORT_TYPE_LEAF_QUERY_CONTAINER,
37                                                                 "id", id,
38                                                                 "title", name,
39                                                                 "child-count", 0,
40                                                                 "expression", expression,
41                                                                 "parent", NULL,
42                                                                 NULL));
43 }
44
45 static void
46 rygel_media_export_leaf_query_container_get_children_ready (GObject      *source_object,
47                                                             GAsyncResult *res,
48                                                             gpointer      user_data) {
49   RygelSearchableContainer *container = RYGEL_SEARCHABLE_CONTAINER (source_object);
50   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);
51   GError *error = NULL;
52   RygelMediaObjects *children = rygel_searchable_container_search_finish (container, res, NULL, &error);
53
54   if (error) {
55     g_simple_async_result_take_error (simple, error);
56   } else {
57     gint iter;
58     gint size = gee_abstract_collection_get_size (GEE_ABSTRACT_COLLECTION (children));
59     RygelMediaContainer *media_container = RYGEL_MEDIA_CONTAINER (source_object);
60
61     for (iter = 0; iter < size; ++iter) {
62       RygelMediaObject *child = RYGEL_MEDIA_OBJECT (gee_abstract_list_get (GEE_ABSTRACT_LIST (children), iter));
63
64       rygel_media_object_set_parent (child, media_container);
65       g_object_unref (child);
66     }
67
68     g_simple_async_result_set_op_res_gpointer (simple, children, g_object_unref);
69   }
70   g_simple_async_result_complete (simple);
71   g_object_unref (simple);
72 }
73
74 static void
75 rygel_media_export_leaf_query_container_real_get_children (RygelMediaContainer *base,
76                                                            guint                offset,
77                                                            guint                max_count,
78                                                            const gchar         *sort_criteria,
79                                                            GCancellable        *cancellable,
80                                                            GAsyncReadyCallback  callback,
81                                                            gpointer             user_data) {
82   GSimpleAsyncResult *simple = g_simple_async_result_new (G_OBJECT (base),
83                                                           callback,
84                                                           user_data,
85                                                           rygel_media_export_leaf_query_container_real_get_children);
86   RygelSearchableContainer *searchable = RYGEL_SEARCHABLE_CONTAINER (base);
87
88   rygel_searchable_container_search (searchable,
89                                      NULL,
90                                      offset,
91                                      max_count,
92                                      sort_criteria,
93                                      cancellable,
94                                      rygel_media_export_leaf_query_container_get_children_ready,
95                                      simple);
96 }
97
98 static RygelMediaObjects *
99 rygel_media_export_leaf_query_container_real_get_children_finish (RygelMediaContainer  *base G_GNUC_UNUSED,
100                                                                   GAsyncResult         *res,
101                                                                   GError              **error) {
102   RygelMediaObjects* result;
103   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
104   if (g_simple_async_result_propagate_error (simple, error)) {
105     return NULL;
106   }
107   result = RYGEL_MEDIA_OBJECTS (g_simple_async_result_get_op_res_gpointer (simple));
108   if (result) {
109     g_object_ref (result);
110   }
111
112   return result;
113 }
114
115 static gint
116 rygel_media_export_leaf_query_container_real_count_children (RygelMediaExportDBContainer  *base) {
117   GError *error = NULL;
118   RygelMediaExportQueryContainer *query_container = RYGEL_MEDIA_EXPORT_QUERY_CONTAINER (base);
119   RygelMediaExportMediaCache *cache = rygel_media_export_db_container_get_media_db (base);
120   RygelSearchExpression *expression = rygel_media_export_query_container_get_expression (query_container);
121   gint result = (gint) rygel_media_export_media_cache_get_object_count_by_search_expression (cache,
122                                                                                              expression,
123                                                                                              NULL,
124                                                                                              &error);
125
126   if (error) {
127     g_error_free (error);
128     return 0;
129   }
130
131   return result;
132 }
133
134 static void
135 rygel_media_export_leaf_query_container_class_init (RygelMediaExportLeafQueryContainerClass *leaf_class) {
136   RygelMediaContainerClass *container_class = RYGEL_MEDIA_CONTAINER_CLASS (leaf_class);
137   RygelMediaExportDBContainerClass *db_container_class = RYGEL_MEDIA_EXPORT_DB_CONTAINER_CLASS (leaf_class);
138
139   container_class->get_children = rygel_media_export_leaf_query_container_real_get_children;
140   container_class->get_children_finish = rygel_media_export_leaf_query_container_real_get_children_finish;
141   db_container_class->count_children = rygel_media_export_leaf_query_container_real_count_children;
142 }
143
144 static void
145 rygel_media_export_leaf_query_container_init (RygelMediaExportLeafQueryContainer *self G_GNUC_UNUSED) {
146 }