diff options
author | Philip Withnall <philip@tecnocode.co.uk> | 2010-12-08 15:06:38 +0000 |
---|---|---|
committer | Philip Withnall <philip@tecnocode.co.uk> | 2010-12-09 00:57:42 +0000 |
commit | 3f3fd8ca71d10bb24b5b3ee8b12325f46f17cf34 (patch) | |
tree | 011153d71d1aa8aa52c5e00d99be6dfe76b3be46 /gdata/tests/documents.c | |
parent | 0aa354ee8017e1a13f394c29e95ac5ba5b55363f (diff) | |
download | libgdata-3f3fd8ca71d10bb24b5b3ee8b12325f46f17cf34.tar.gz |
documents: Switch to a stream-based download API
Downloading to a file provided by its path was unnecessarily limiting,
forcing downloads to be made directly to disk instead of (e.g.) another
network stream, and subjecting clients to libgdata's idea of “Replace this
File?” logic. It's not much extra code for the clients to handle this
themselves, and offers much greater flexibility. We also get async support
for free, as creating the GDataDownloadStream is a cheap operation, and it
already has async support.
This updates the test suite appropriately.
This is an API break which affects:
• gdata_documents_document_download()
Diffstat (limited to 'gdata/tests/documents.c')
-rw-r--r-- | gdata/tests/documents.c | 101 |
1 files changed, 53 insertions, 48 deletions
diff --git a/gdata/tests/documents.c b/gdata/tests/documents.c index 6d6b2d9d..892fea8c 100644 --- a/gdata/tests/documents.c +++ b/gdata/tests/documents.c @@ -454,70 +454,75 @@ test_download_all_documents (gconstpointer service) { GDataDocumentsFeed *feed; GError *error = NULL; - gchar *content_type = NULL; - GFile *destination_file; - gchar *destination_file_name; - GString *destination_display_name; GList *i; - gint ods_nb = 0, odt_nb = 0; feed = gdata_documents_service_query_documents (GDATA_DOCUMENTS_SERVICE (service), NULL, NULL, NULL, NULL, &error); g_assert_no_error (error); g_assert (GDATA_IS_FEED (feed)); for (i = gdata_feed_get_entries (GDATA_FEED (feed)); i != NULL; i = i->next) { + GDataDownloadStream *download_stream; + GFileOutputStream *output_stream; + GFile *destination_file; + GFileInfo *file_info; + const gchar *destination_file_extension; + gchar *destination_file_name, *destination_file_path; + if (GDATA_IS_DOCUMENTS_PRESENTATION (i->data)) { - destination_file = g_file_new_for_path ("/tmp"); - destination_file = gdata_documents_document_download (GDATA_DOCUMENTS_DOCUMENT (i->data), GDATA_DOCUMENTS_SERVICE (service), - &content_type, GDATA_DOCUMENTS_PRESENTATION_PPT, destination_file, - TRUE, NULL, &error); + /* Presentation */ + destination_file_extension = "odp"; + download_stream = gdata_documents_document_download (GDATA_DOCUMENTS_DOCUMENT (i->data), GDATA_DOCUMENTS_SERVICE (service), + GDATA_DOCUMENTS_PRESENTATION_PPT, &error); } else if (GDATA_IS_DOCUMENTS_SPREADSHEET (i->data)) { - destination_file_name = g_strdup_printf ("/tmp/%s.%s", gdata_documents_entry_get_document_id (GDATA_DOCUMENTS_ENTRY (i->data)), "ods"); - destination_file = g_file_new_for_path (destination_file_name); - g_free (destination_file_name); - destination_file = gdata_documents_document_download (GDATA_DOCUMENTS_DOCUMENT (i->data), GDATA_DOCUMENTS_SERVICE (service), - &content_type, GDATA_DOCUMENTS_SPREADSHEET_ODS, destination_file, - TRUE, NULL, &error); - g_assert_no_error (error); - - destination_display_name = g_string_new (gdata_entry_get_title (GDATA_ENTRY(i->data))); - g_string_append (destination_display_name, ".ods"); - g_file_set_display_name (destination_file, destination_display_name->str, NULL, &error); - while (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_EXISTS)) { - g_clear_error (&error); - error = NULL; - g_string_printf (destination_display_name,"%s%d.%s", gdata_entry_get_title (GDATA_ENTRY(i->data)), ods_nb, "ods"); - g_file_set_display_name (destination_file, destination_display_name->str, NULL, &error); - ods_nb++; - } - g_string_free (destination_display_name, TRUE); + /* Spreadsheet */ + destination_file_extension = "ods"; + download_stream = gdata_documents_document_download (GDATA_DOCUMENTS_DOCUMENT (i->data), GDATA_DOCUMENTS_SERVICE (service), + GDATA_DOCUMENTS_SPREADSHEET_ODS, &error); } else if (GDATA_IS_DOCUMENTS_TEXT (i->data)) { - destination_file_name = g_strdup_printf ("/tmp/%s.%s", gdata_documents_entry_get_document_id (GDATA_DOCUMENTS_ENTRY (i->data)), "odt"); - destination_file = g_file_new_for_path (destination_file_name); - g_free (destination_file_name); - destination_file = gdata_documents_document_download (GDATA_DOCUMENTS_DOCUMENT (i->data), GDATA_DOCUMENTS_SERVICE (service), - &content_type, GDATA_DOCUMENTS_TEXT_ODT, destination_file, TRUE, NULL, - &error); - g_assert_no_error (error); - - destination_display_name = g_string_new (gdata_entry_get_title (GDATA_ENTRY(i->data))); - g_string_append (destination_display_name, ".odt"); - g_file_set_display_name (destination_file, destination_display_name->str, NULL, &error); - while (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_EXISTS)) { - g_clear_error (&error); - g_string_printf (destination_display_name,"%s%d.%s", gdata_entry_get_title (GDATA_ENTRY(i->data)), odt_nb, "odt"); - g_file_set_display_name (destination_file, destination_display_name->str, NULL, &error); - odt_nb++; - } - g_string_free (destination_display_name, TRUE); + /* Text document */ + destination_file_extension = "odt"; + download_stream = gdata_documents_document_download (GDATA_DOCUMENTS_DOCUMENT (i->data), GDATA_DOCUMENTS_SERVICE (service), + GDATA_DOCUMENTS_TEXT_ODT, &error); + } else { + /* Error! */ + g_assert_not_reached (); } g_assert_no_error (error); - g_free (content_type); + + /* Find a destination file */ + destination_file_name = g_strdup_printf ("%s.%s", gdata_documents_entry_get_document_id (GDATA_DOCUMENTS_ENTRY (i->data)), + destination_file_extension); + 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 */ + output_stream = g_file_replace (destination_file, NULL, FALSE, G_FILE_CREATE_REPLACE_DESTINATION, NULL, &error); + g_assert_no_error (error); + + g_output_stream_splice (G_OUTPUT_STREAM (output_stream), G_INPUT_STREAM (download_stream), + G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE | G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET, NULL, &error); + g_object_unref (output_stream); + g_assert_no_error (error); + + /* Check the filesize */ + file_info = g_file_query_info (destination_file, G_FILE_ATTRIBUTE_STANDARD_SIZE, G_FILE_QUERY_INFO_NONE, NULL, &error); + g_assert_no_error (error); + + g_assert (g_file_info_get_size (file_info) > 0); + /* Checking the content types turns out to be quite involved, and not worth doing as it depends on the local user's content type DB */ + + 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); } g_object_unref (feed); - g_clear_error (&error); } static void |