[libgdata] freebase: Add helper function to retrieve images



commit 901a0aec17339ebdcb3d726ef400470a7ca3e886
Author: Carlos Garnacho <carlosg gnome org>
Date:   Mon Mar 17 02:45:34 2014 +0100

    freebase: Add helper function to retrieve images
    
    This helper function can be used together with the topic API,
    if a returned element is an image, this function can be used to
    retrieve a GInputStream to the image at a requested maximum size.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=726486

 docs/reference/gdata-sections.txt                |    1 +
 gdata/gdata.symbols                              |    1 +
 gdata/services/freebase/gdata-freebase-service.c |   76 ++++++++++++++++++++++
 gdata/services/freebase/gdata-freebase-service.h |    3 +
 po/POTFILES.in                                   |    1 +
 5 files changed, 82 insertions(+), 0 deletions(-)
---
diff --git a/docs/reference/gdata-sections.txt b/docs/reference/gdata-sections.txt
index 2e9dbc6..5426402 100644
--- a/docs/reference/gdata-sections.txt
+++ b/docs/reference/gdata-sections.txt
@@ -2579,6 +2579,7 @@ gdata_freebase_service_get_topic
 gdata_freebase_service_get_topic_async
 gdata_freebase_service_search
 gdata_freebase_service_search_async
+gdata_freebase_service_get_image
 <SUBSECTION Standard>
 gdata_freebase_service_get_type
 GDATA_FREEBASE_SERVICE
diff --git a/gdata/gdata.symbols b/gdata/gdata.symbols
index 5fd4815..3f3c1f0 100644
--- a/gdata/gdata.symbols
+++ b/gdata/gdata.symbols
@@ -1028,6 +1028,7 @@ gdata_freebase_service_get_topic
 gdata_freebase_service_get_topic_async
 gdata_freebase_service_search
 gdata_freebase_service_search_async
+gdata_freebase_service_get_image
 gdata_freebase_query_get_type
 gdata_freebase_query_new
 gdata_freebase_query_new_from_variant
diff --git a/gdata/services/freebase/gdata-freebase-service.c 
b/gdata/services/freebase/gdata-freebase-service.c
index 165ec14..0509429 100644
--- a/gdata/services/freebase/gdata-freebase-service.c
+++ b/gdata/services/freebase/gdata-freebase-service.c
@@ -42,13 +42,16 @@
 
 #include "gdata-freebase-service.h"
 #include "gdata-freebase-result.h"
+#include "gdata-freebase-search-result.h"
 #include "gdata-service.h"
 #include "gdata-private.h"
 #include "gdata-query.h"
+#include "gdata-feed.h"
 
 /* Standards reference at https://developers.google.com/freebase/v1/ */
 
 #define URLBASE "://www.googleapis.com/freebase/v1"
+#define IMAGE_URI_PREFIX "https://usercontent.googleapis.com/freebase/v1/image";
 
 enum {
        PROP_DEVELOPER_KEY = 1
@@ -377,3 +380,76 @@ gdata_freebase_service_search_async (GDataFreebaseService *self, GDataFreebaseSe
        gdata_service_query_single_entry_async (GDATA_SERVICE (self), get_freebase_authorization_domain (), 
"search",
                                                GDATA_QUERY (query), GDATA_TYPE_FREEBASE_SEARCH_RESULT, 
cancellable, callback, user_data);
 }
+
+static gchar *
+compose_image_uri (GDataFreebaseTopicValue *value, guint max_width, guint max_height)
+{
+       GString *uri = g_string_new (IMAGE_URI_PREFIX);
+       const GDataFreebaseTopicObject *object;
+       gboolean first = TRUE;
+
+       object = gdata_freebase_topic_value_get_object (value);
+       g_assert (object != NULL);
+
+       g_string_append (uri, gdata_freebase_topic_object_get_id (object));
+
+#define APPEND_SEP g_string_append_c (uri, first ? '?' : '&'); first = FALSE;
+
+       if (max_width > 0) {
+               APPEND_SEP;
+               g_string_append_printf (uri, "maxwidth=%d", max_width);
+       }
+
+       if (max_height > 0) {
+               APPEND_SEP;
+               g_string_append_printf (uri, "maxheight=%d", max_height);
+       }
+#undef APPEND_SEP
+
+       return g_string_free (uri, FALSE);
+}
+
+/**
+ * gdata_freebase_service_get_image:
+ * @self: a #GDataFreebaseService
+ * @value: a #GDataFreebaseTopicValue from a topic result
+ * @cancellable: (allow-none): optional #GCancellable object, or %NULL
+ * @max_width: maximum width of the image returned, or 0
+ * @max_height: maximum height of the image returned, or 0
+ * @error: (allow-none): a #GError, or %NULL
+ *
+ * Creates an input stream to an image object returned in a topic query. If @max_width and @max_height
+ * are unspecified (i.e. set to 0), the image returned will be the smallest available.
+ *
+ * Return value: (transfer full): a #GInputStream opened to the image; unref with g_object_unref()
+ *
+ * Since: UNRELEASED
+ **/
+GInputStream *
+gdata_freebase_service_get_image (GDataFreebaseService *self, GDataFreebaseTopicValue *value,
+                                 GCancellable *cancellable, guint max_width, guint max_height, GError 
**error)
+{
+       GInputStream *stream;
+       gchar *uri;
+
+       g_return_val_if_fail (GDATA_IS_FREEBASE_SERVICE (self), NULL);
+       g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), NULL);
+       g_return_val_if_fail (value != NULL, NULL);
+       g_return_val_if_fail (!error || !*error, NULL);
+       g_return_val_if_fail (max_width < 4096 && max_height < 4096, NULL);
+
+       if (!gdata_freebase_topic_value_is_image (value)) {
+               g_set_error (error,
+                            GDATA_SERVICE_ERROR,
+                            GDATA_SERVICE_ERROR_BAD_QUERY_PARAMETER,
+                            _("Property '%s' does not hold an image"),
+                            gdata_freebase_topic_value_get_property (value));
+               return NULL;
+       }
+
+       uri = compose_image_uri (value, max_width, max_height);
+       stream = gdata_download_stream_new (GDATA_SERVICE (self), get_freebase_authorization_domain (), uri, 
cancellable);
+       g_free (uri);
+
+       return stream;
+}
diff --git a/gdata/services/freebase/gdata-freebase-service.h 
b/gdata/services/freebase/gdata-freebase-service.h
index 609a38c..ad6eab6 100644
--- a/gdata/services/freebase/gdata-freebase-service.h
+++ b/gdata/services/freebase/gdata-freebase-service.h
@@ -90,6 +90,9 @@ GDataFreebaseSearchResult *gdata_freebase_service_search (GDataFreebaseService *
 void gdata_freebase_service_search_async (GDataFreebaseService *self, GDataFreebaseSearchQuery *query,
                                          GCancellable *cancellable, GAsyncReadyCallback callback, gpointer 
user_data);
 
+GInputStream *gdata_freebase_service_get_image (GDataFreebaseService *self, GDataFreebaseTopicValue *value,
+                                               GCancellable *cancellable, guint max_width, guint max_height, 
GError **error) G_GNUC_WARN_UNUSED_RESULT G_GNUC_MALLOC;
+
 G_END_DECLS
 
 #endif /* !GDATA_FREEBASE_SERVICE_H */
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 5a0820b..1d0a3d9 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -19,6 +19,7 @@ gdata/services/contacts/gdata-contacts-service.c
 gdata/services/documents/gdata-documents-document.c
 gdata/services/documents/gdata-documents-entry.c
 gdata/services/documents/gdata-documents-service.c
+gdata/services/freebase/gdata-freebase-service.c
 gdata/services/picasaweb/gdata-picasaweb-service.c
 gdata/services/tasks/gdata-tasks-service.c
 gdata/services/youtube/gdata-youtube-service.c


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]