=?utf-8?q?=5Blibgdata=5D_Bug_656970_=E2=80=94_Add_thumbnail_support?=



commit 39b6ac0d4c79730bbaa62f36cd9aa343559b07ee
Author: Philip Withnall <philip tecnocode co uk>
Date:   Sat Jul 21 11:07:45 2012 +0100

    Bug 656970 â Add thumbnail support
    
    Add support for downloading thumbnails of Google Documents documents. This
    includes a test case, but it isnât very useful because I canât find a single
    document which has a thumbnail expressed in its metadata. Your mileage may
    vary.
    
    This also includes an example of how to download thumbnails, but thatâs
    similarly (unfortunately) untestable. This leaves me with a sour taste in
    my mouth.
    
    New API:
     â gdata_documents_document_get_download_uri()
    
    Closes: https://bugzilla.gnome.org/show_bug.cgi?id=656970

 docs/reference/gdata-sections.txt                  |    1 +
 gdata/gdata.symbols                                |    1 +
 .../services/documents/gdata-documents-document.c  |   65 ++++++++++++++++
 .../services/documents/gdata-documents-document.h  |    2 +
 gdata/tests/documents.c                            |   81 ++++++++++++++++++++
 5 files changed, 150 insertions(+), 0 deletions(-)
---
diff --git a/docs/reference/gdata-sections.txt b/docs/reference/gdata-sections.txt
index 49ddfb3..211b67e 100644
--- a/docs/reference/gdata-sections.txt
+++ b/docs/reference/gdata-sections.txt
@@ -1622,6 +1622,7 @@ GDataDocumentsDocumentClass
 gdata_documents_document_new
 gdata_documents_document_download
 gdata_documents_document_get_download_uri
+gdata_documents_document_get_thumbnail_uri
 <SUBSECTION Standard>
 gdata_documents_document_get_type
 GDATA_DOCUMENTS_DOCUMENT
diff --git a/gdata/gdata.symbols b/gdata/gdata.symbols
index 29151f1..5d8c7ba 100644
--- a/gdata/gdata.symbols
+++ b/gdata/gdata.symbols
@@ -958,3 +958,4 @@ gdata_documents_service_copy_document_finish
 gdata_goa_authorizer_get_type
 gdata_goa_authorizer_new
 gdata_goa_authorizer_get_goa_object
+gdata_documents_document_get_thumbnail_uri
diff --git a/gdata/services/documents/gdata-documents-document.c b/gdata/services/documents/gdata-documents-document.c
index d8649f4..4810955 100644
--- a/gdata/services/documents/gdata-documents-document.c
+++ b/gdata/services/documents/gdata-documents-document.c
@@ -333,3 +333,68 @@ gdata_documents_document_get_download_uri (GDataDocumentsDocument *self, const g
 
 	return _gdata_service_build_uri ("%p&exportFormat=%s", gdata_entry_get_content_uri (GDATA_ENTRY (self)), export_format);
 }
+
+/**
+ * gdata_documents_document_download_thumbnail:
+ * @self: a #GDataDocumentsDocument
+ * @service: the #GDataDocumentsService
+ * @cancellable: (allow-none): a #GCancellable for the entire download stream, or %NULL
+ * @error: a #GError, or %NULL
+ *
+ * Gets the URI of the thumbnail for the #GDataDocumentsDocument. If no thumbnail exists for the document, %NULL will be returned.
+ *
+ * The thumbnail may then be downloaded using a #GDataDownloadStream.
+ *
+ * <example>
+ *	<title></title>
+ *	<programlisting>
+ *	GDataDocumentsService *service;
+ *	const gchar *thumbnail_uri;
+ *	GCancellable *cancellable;
+ *	GdkPixbuf *pixbuf;
+ *	GError *error = NULL;
+ *
+ *	service = get_my_documents_service ();
+ *	thumbnail_uri = gdata_documents_document_get_thumbnail_uri (my_document);
+ *	cancellable = g_cancellable_new ();
+ *
+ *	/<!-- -->* Prepare a download stream *<!-- -->/
+ *	download_stream = GDATA_DOWNLOAD_STREAM (gdata_download_stream_new (GDATA_SERVICE (service), NULL, thumbnail_uri, cancellable));
+ *
+ *	/<!-- -->* Download into a new GdkPixbuf. This can be cancelled using 'cancellable'. *<!-- -->/
+ *	pixbuf = gdk_pixbuf_new_from_stream (G_INPUT_STREAM (download_stream), NULL, &error);
+ *
+ *	if (error != NULL) {
+ *		/<!-- -->* Handle the error. *<!-- -->/
+ *		g_error_free (error);
+ *	}
+ *
+ *	g_object_unref (download_stream);
+ *	g_object_unref (cancellable);
+ *
+ *	/<!-- -->* Do something with the GdkPixbuf. *<!-- -->/
+ *
+ *	g_object_unref (pixbuf);
+ *	</programlisting>
+ * </example>
+ *
+ * Return value: (allow-none): the URI of the document's thumbnail, or %NULL
+ *
+ * Since: 0.13.1
+ */
+const gchar *
+gdata_documents_document_get_thumbnail_uri (GDataDocumentsDocument *self)
+{
+	GDataLink *thumbnail_link;
+
+	g_return_val_if_fail (GDATA_IS_DOCUMENTS_DOCUMENT (self), NULL);
+
+	/* Get the thumbnail URI (if it exists). */
+	thumbnail_link = gdata_entry_look_up_link (GDATA_ENTRY (self), "http://schemas.google.com/docs/2007/thumbnail";);
+
+	if (thumbnail_link == NULL) {
+		return NULL;
+	}
+
+	return gdata_link_get_uri (thumbnail_link);
+}
diff --git a/gdata/services/documents/gdata-documents-document.h b/gdata/services/documents/gdata-documents-document.h
index 5b27cb2..b87992f 100644
--- a/gdata/services/documents/gdata-documents-document.h
+++ b/gdata/services/documents/gdata-documents-document.h
@@ -71,6 +71,8 @@ GDataDownloadStream *gdata_documents_document_download (GDataDocumentsDocument *
                                                         GCancellable *cancellable, GError **error) G_GNUC_WARN_UNUSED_RESULT G_GNUC_MALLOC;
 gchar *gdata_documents_document_get_download_uri (GDataDocumentsDocument *self, const gchar *export_format) G_GNUC_WARN_UNUSED_RESULT G_GNUC_MALLOC;
 
+const gchar *gdata_documents_document_get_thumbnail_uri (GDataDocumentsDocument *self) G_GNUC_PURE;
+
 G_END_DECLS
 
 #endif /* !GDATA_DOCUMENTS_DOCUMENT_H */
diff --git a/gdata/tests/documents.c b/gdata/tests/documents.c
index 8438371..8a91fcb 100644
--- a/gdata/tests/documents.c
+++ b/gdata/tests/documents.c
@@ -22,6 +22,11 @@
 #include <unistd.h>
 #include <string.h>
 
+/* For the thumbnail size tests in test_download_thumbnail() */
+#ifdef HAVE_GDK_PIXBUF
+#include <gdk-pixbuf/gdk-pixbuf.h>
+#endif
+
 #include "gdata.h"
 #include "common.h"
 
@@ -1325,6 +1330,80 @@ test_download_document (TempDocumentsData *data, gconstpointer service)
 }
 
 static void
+test_download_thumbnail (TempDocumentData *data, gconstpointer service)
+{
+	const gchar *thumbnail_uri;
+	GDataDownloadStream *download_stream;
+	gchar *destination_file_name, *destination_file_path;
+	GFile *destination_file;
+	GFileOutputStream *file_stream;
+	gssize transfer_size;
+	GError *error = NULL;
+
+	thumbnail_uri = gdata_documents_document_get_thumbnail_uri (data->document);
+
+	/* Google takes many minutes to generate thumbnails for uploaded documents, so with our current testing strategy of creating fresh documents
+	 * for each test, this particular test is fairly useless. */
+	if (thumbnail_uri == NULL) {
+		g_test_message ("Skipping thumbnail download test because document â%sâ doesnât have a thumbnail.",
+		                gdata_documents_entry_get_resource_id (GDATA_DOCUMENTS_ENTRY (data->document)));
+		return;
+	}
+
+	/* Download the thumbnail to a file for testing (in case we weren't compiled with GdkPixbuf support) */
+	download_stream = GDATA_DOWNLOAD_STREAM (gdata_download_stream_new (GDATA_SERVICE (service), NULL, thumbnail_uri, NULL));
+	g_assert (GDATA_IS_DOWNLOAD_STREAM (download_stream));
+
+	/* Prepare a file to write the data to */
+	destination_file_name = g_strdup_printf ("%s_thumbnail.jpg", gdata_documents_entry_get_resource_id (GDATA_DOCUMENTS_ENTRY (data->document)));
+	destination_file_path = g_build_filename (g_get_tmp_dir (), destination_file_name, NULL);
+	g_free (destination_file_name);
+	destination_file = g_file_new_for_path (destination_file_path);
+	g_free (destination_file_path);
+
+	/* Download the file */
+	file_stream = g_file_replace (destination_file, NULL, FALSE, G_FILE_CREATE_REPLACE_DESTINATION, NULL, &error);
+	g_assert_no_error (error);
+	g_assert (G_IS_FILE_OUTPUT_STREAM (file_stream));
+
+	transfer_size = g_output_stream_splice (G_OUTPUT_STREAM (file_stream), G_INPUT_STREAM (download_stream),
+	                                        G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE | G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET, NULL, &error);
+	g_assert_no_error (error);
+	g_assert_cmpint (transfer_size, >, 0);
+
+	g_object_unref (file_stream);
+	g_object_unref (download_stream);
+
+	/* Delete the file (shouldn't cause the test to fail if this fails) */
+	g_file_delete (destination_file, NULL, NULL);
+	g_object_unref (destination_file);
+
+#ifdef HAVE_GDK_PIXBUF
+	/* Test downloading all thumbnails directly into GdkPixbufs, and check that they're all the correct size */
+	{
+		GdkPixbuf *pixbuf;
+
+		/* Prepare a new download stream */
+		download_stream = GDATA_DOWNLOAD_STREAM (gdata_download_stream_new (GDATA_SERVICE (service), NULL, thumbnail_uri, NULL));
+		g_assert (GDATA_IS_DOWNLOAD_STREAM (download_stream));
+
+		/* Download into a new GdkPixbuf */
+		pixbuf = gdk_pixbuf_new_from_stream (G_INPUT_STREAM (download_stream), NULL, &error);
+		g_assert_no_error (error);
+		g_assert (GDK_IS_PIXBUF (pixbuf));
+
+		g_object_unref (download_stream);
+
+		/* Check the dimensions are as expected. */
+		g_assert_cmpint (gdk_pixbuf_get_width (pixbuf), ==, 10);
+		g_assert_cmpint (gdk_pixbuf_get_height (pixbuf), ==, 10);
+
+		g_object_unref (pixbuf);
+	}
+#endif /* HAVE_GDK_PIXBUF */
+}
+
+static void
 test_access_rule_insert (TempDocumentData *data, gconstpointer service)
 {
 	GDataAccessRule *access_rule, *new_access_rule;
@@ -1781,6 +1860,8 @@ main (int argc, char *argv[])
 
 		g_test_add ("/documents/download/document", TempDocumentsData, service, set_up_temp_documents, test_download_document,
 		            tear_down_temp_documents);
+		g_test_add ("/documents/download/thumbnail", TempDocumentData, service, set_up_temp_document_spreadsheet, test_download_thumbnail,
+		            tear_down_temp_document);
 
 		/* Test all possible combinations of conditions for resumable updates. */
 		{



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