=?utf-8?q?=5Blibgdata=5D_Bug_607616_=E2=80=94_Permit_the_uploading_of_doc?= =?utf-8?q?uments_of_any_type?=



commit 8fd2cf3963d25eb97014ddb3f234c2bf8cad6261
Author: Philip Withnall <philip tecnocode co uk>
Date:   Fri Apr 13 13:14:54 2012 +0100

    Bug 607616 â Permit the uploading of documents of any type
    
    Add support to GDataDocumentsUploadQuery for specifying that documents
    shouldnât be converted to common formats on being uploaded. This permits
    arbitrary files to be uploaded.
    
    This includes test cases.
    
    API additions:
     â GDataDocumentsUploadQuery:convert,
       gdata_documents_upload_query_get_convert and
       gdata_documents_upload_query_set_convert
    
    Closes: https://bugzilla.gnome.org/show_bug.cgi?id=607616

 docs/reference/gdata-sections.txt                  |    2 +
 gdata/gdata.symbols                                |    2 +
 gdata/services/documents/gdata-documents-service.c |   54 +++---
 .../documents/gdata-documents-upload-query.c       |  189 +++++++++++++++++++-
 .../documents/gdata-documents-upload-query.h       |    3 +
 gdata/tests/documents.c                            |  152 ++++++++++++----
 6 files changed, 341 insertions(+), 61 deletions(-)
---
diff --git a/docs/reference/gdata-sections.txt b/docs/reference/gdata-sections.txt
index c98edc0..a791926 100644
--- a/docs/reference/gdata-sections.txt
+++ b/docs/reference/gdata-sections.txt
@@ -2382,6 +2382,8 @@ gdata_documents_upload_query_new
 gdata_documents_upload_query_build_uri
 gdata_documents_upload_query_get_folder
 gdata_documents_upload_query_set_folder
+gdata_documents_upload_query_get_convert
+gdata_documents_upload_query_set_convert
 <SUBSECTION Standard>
 gdata_documents_upload_query_get_type
 GDATA_DOCUMENTS_UPLOAD_QUERY
diff --git a/gdata/gdata.symbols b/gdata/gdata.symbols
index bfe9e54..dceb4ad 100644
--- a/gdata/gdata.symbols
+++ b/gdata/gdata.symbols
@@ -947,3 +947,5 @@ gdata_documents_upload_query_build_uri
 gdata_documents_upload_query_get_folder
 gdata_documents_upload_query_set_folder
 gdata_documents_document_new
+gdata_documents_upload_query_get_convert
+gdata_documents_upload_query_set_convert
diff --git a/gdata/services/documents/gdata-documents-service.c b/gdata/services/documents/gdata-documents-service.c
index b0bfe36..d1836d3 100644
--- a/gdata/services/documents/gdata-documents-service.c
+++ b/gdata/services/documents/gdata-documents-service.c
@@ -649,6 +649,9 @@ gdata_documents_service_upload_document (GDataDocumentsService *self, GDataDocum
  * the document as an opaque file, or convert it to a standard format. If @query is %NULL, the document will be uploaded into the root folder, and
  * automatically converted to a standard format. No OCR or automatic language translation will be performed by default.
  *
+ * If @query is non-%NULL and #GDataDocumentsUploadQuery:convert is %FALSE, @document must be an instance of #GDataDocumentsDocument. Otherwise,
+ * @document must be a subclass of it, such as #GDataDocumentsPresentation.
+ *
  * The stream returned by this function should be written to using the standard #GOutputStream methods, asychronously or synchronously. Once the stream
  * is closed (using g_output_stream_close()), gdata_documents_service_finish_upload() should be called on it to parse and return the updated
  * #GDataDocumentsDocument for the document. This must be done, as @document isn't updated in-place.
@@ -840,44 +843,45 @@ gdata_documents_service_update_document_resumable (GDataDocumentsService *self,
 GDataDocumentsDocument *
 gdata_documents_service_finish_upload (GDataDocumentsService *self, GDataUploadStream *upload_stream, GError **error)
 {
-	const gchar *response_body, *content_type;
+	const gchar *response_body, *term_pos;
 	gssize response_length;
-	GDataEntry *entry;
 	GType new_document_type = G_TYPE_INVALID;
 
-	/* Determine the type of the document we've uploaded */
-	entry = gdata_upload_stream_get_entry (upload_stream);
-	content_type = gdata_upload_stream_get_content_type (upload_stream);
+	/* Get and parse the response from the server */
+	response_body = gdata_upload_stream_get_response (upload_stream, &response_length);
+	if (response_body == NULL || response_length == 0) {
+		/* Error will have been set by the upload stream. */
+		return NULL;
+	}
+
+	/* Hackily determine the document format the server chose, and then parse the XML accordingly. The full parse will pick up any errors in
+	 * our choice of format. */
+	#define TERM_MARKER "<category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/docs/2007#";
+	term_pos = g_strstr_len (response_body, response_length, TERM_MARKER);
+	if (term_pos == NULL) {
+		goto done;
+	}
+
+	term_pos += strlen (TERM_MARKER);
 
-	if (entry != NULL) {
-		new_document_type = G_OBJECT_TYPE (entry);
-	} else if (strcmp (content_type, "application/x-vnd.oasis.opendocument.spreadsheet") == 0 ||
-	           strcmp (content_type, "text/tab-separated-values") == 0 ||
-	           strcmp (content_type, "application/x-vnd.oasis.opendocument.spreadsheet") == 0 ||
-	           strcmp (content_type, "application/vnd.ms-excel") == 0) {
+	if (g_str_has_prefix (term_pos, "file'") == TRUE) {
+		new_document_type = GDATA_TYPE_DOCUMENTS_DOCUMENT;
+	} else if (g_str_has_prefix (term_pos, "spreadsheet'") == TRUE) {
 		new_document_type = GDATA_TYPE_DOCUMENTS_SPREADSHEET;
-	} else if (strcmp (content_type, "application/msword") == 0 ||
-	           strcmp (content_type, "application/vnd.oasis.opendocument.text") == 0 ||
-	           strcmp (content_type, "application/rtf") == 0 ||
-	           strcmp (content_type, "text/html") == 0 ||
-	           strcmp (content_type, "application/vnd.sun.xml.writer") == 0 ||
-	           strcmp (content_type, "text/plain") == 0) {
-		new_document_type = GDATA_TYPE_DOCUMENTS_TEXT;
-	} else if (strcmp (content_type, "application/vnd.ms-powerpoint") == 0) {
+	} else if (g_str_has_prefix (term_pos, "presentation'") == TRUE) {
 		new_document_type = GDATA_TYPE_DOCUMENTS_PRESENTATION;
+	} else if (g_str_has_prefix (term_pos, "document'") == TRUE) {
+		new_document_type = GDATA_TYPE_DOCUMENTS_TEXT;
 	}
 
+done:
 	if (g_type_is_a (new_document_type, GDATA_TYPE_DOCUMENTS_DOCUMENT) == FALSE) {
 		g_set_error (error, GDATA_DOCUMENTS_SERVICE_ERROR, GDATA_DOCUMENTS_SERVICE_ERROR_INVALID_CONTENT_TYPE,
-		             _("The content type of the supplied document ('%s') could not be recognized."), content_type);
+		             _("The content type of the supplied document ('%s') could not be recognized."),
+		             gdata_upload_stream_get_content_type (upload_stream));
 		return NULL;
 	}
 
-	/* Get and parse the response from the server */
-	response_body = gdata_upload_stream_get_response (upload_stream, &response_length);
-	if (response_body == NULL || response_length == 0)
-		return NULL;
-
 	return GDATA_DOCUMENTS_DOCUMENT (gdata_parsable_new_from_xml (new_document_type, response_body, (gint) response_length, error));
 }
 
diff --git a/gdata/services/documents/gdata-documents-upload-query.c b/gdata/services/documents/gdata-documents-upload-query.c
index 5c3bc53..30bce76 100644
--- a/gdata/services/documents/gdata-documents-upload-query.c
+++ b/gdata/services/documents/gdata-documents-upload-query.c
@@ -30,6 +30,110 @@
  * #GDataDocumentsUploadQuery is designed as an object (rather than a fixed struct or set of function arguments) to allow for easy additions of new
  * Google Documents features in the future.
  *
+ * <example>
+ * 	<title>Uploading an Arbitrary File from Disk</title>
+ * 	<programlisting>
+ *	GDataDocumentsService *service;
+ *	GDataDocumentsDocument *document, *uploaded_document;
+ *	GFile *arbitrary_file;
+ *	GFileInfo *file_info;
+ *	const gchar *slug, *content_type;
+ *	goffset file_size;
+ *	GDataDocumentsUploadQuery *upload_query;
+ *	GFileInputStream *file_stream;
+ *	GDataUploadStream *upload_stream;
+ *	GError *error = NULL;
+ *
+ *	/<!-- -->* Create a service. *<!-- -->/
+ *	service = create_documents_service ();
+ *
+ *	/<!-- -->* Get the file to upload. *<!-- -->/
+ *	arbitrary_file = g_file_new_for_path ("arbitrary-file.bin");
+ *
+ *	/<!-- -->* Get the file's display name, content type and size. *<!-- -->/
+ *	file_info = g_file_query_info (arbitrary_file, G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME "," G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE ","
+ *	                               G_FILE_ATTRIBUTE_STANDARD_SIZE, G_FILE_QUERY_INFO_NONE, NULL, &error);
+ *
+ *	if (error != NULL) {
+ *		g_error ("Error getting arbitrary file information: %s", error->message);
+ *		g_error_free (error);
+ *		g_object_unref (arbitrary_file);
+ *		g_object_unref (service);
+ *		return;
+ *	}
+ *
+ *	slug = g_file_info_get_display_name (file_info);
+ *	content_type = g_file_info_get_content_type (file_info);
+ *	file_size = g_file_info_get_size (file_info);
+ *
+ *	/<!-- -->* Get an input stream for the file. *<!-- -->/
+ *	file_stream = g_file_read (arbitrary_file, NULL, &error);
+ *
+ *	g_object_unref (arbitrary_file);
+ *
+ *	if (error != NULL) {
+ *		g_error ("Error getting arbitrary file stream: %s", error->message);
+ *		g_error_free (error);
+ *		g_object_unref (file_info);
+ *		g_object_unref (service);
+ *		return;
+ *	}
+ *
+ *	/<!-- -->* Create the file metadata to upload. *<!-- -->/
+ *	document = gdata_documents_document_new (NULL);
+ *	gdata_entry_set_title (GDATA_ENTRY (document), "Title for My Arbitrary File");
+ *
+ *	/<!-- -->* Build the upload query and set the upload to not be converted to a standard format. *<!-- -->/
+ *	upload_query = gdata_documents_upload_query_new ();
+ *	gdata_documents_upload_query_set_convert (upload_query, FALSE);
+ *
+ *	/<!-- -->* Get an upload stream for the file. *<!-- -->/
+ *	upload_stream = gdata_documents_service_upload_document_resumable (service, document, slug, content_type, file_size, upload_query, NULL, &error);
+ *
+ *	g_object_unref (upload_query);
+ *	g_object_unref (document);
+ *	g_object_unref (file_info);
+ *
+ *	if (error != NULL) {
+ *		g_error ("Error getting upload stream: %s", error->message);
+ *		g_error_free (error);
+ *		g_object_unref (file_stream);
+ *		g_object_unref (service);
+ *		return;
+ *	}
+ *
+ *	/<!-- -->* Upload the document. This is a blocking operation, and should normally be done asynchronously. *<!-- -->/
+ *	g_output_stream_splice (G_OUTPUT_STREAM (upload_stream), G_INPUT_STREAM (file_stream),
+ *	                        G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE | G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET, NULL, &error);
+ *
+ *	g_object_unref (file_stream);
+ *
+ *	if (error != NULL) {
+ *		g_error ("Error splicing streams: %s", error->message);
+ *		g_error_free (error);
+ *		g_object_unref (upload_stream);
+ *		g_object_unref (service);
+ *		return;
+ *	}
+ *
+ *	/<!-- -->* Finish off the upload by parsing the returned updated document metadata entry. *<!-- -->/
+ *	uploaded_document = gdata_documents_service_finish_upload (service, upload_stream, &error);
+ *
+ *	g_object_unref (upload_stream);
+ *	g_object_unref (service);
+ *
+ *	if (error != NULL) {
+ *		g_error ("Error uploading file: %s", error->message);
+ *		g_error_free (error);
+ *		return;
+ *	}
+ *
+ *	/<!-- -->* Do something with the uploaded document. *<!-- -->/
+ *
+ *	g_object_unref (uploaded_document);
+ * 	</programlisting>
+ * </example>
+ *
  * Since: 0.13.0
  */
 
@@ -45,10 +149,12 @@ static void gdata_documents_upload_query_set_property (GObject *object, guint pr
 
 struct _GDataDocumentsUploadQueryPrivate {
 	GDataDocumentsFolder *folder;
+	gboolean convert;
 };
 
 enum {
 	PROP_FOLDER = 1,
+	PROP_CONVERT,
 };
 
 G_DEFINE_TYPE (GDataDocumentsUploadQuery, gdata_documents_upload_query, G_TYPE_OBJECT)
@@ -76,6 +182,28 @@ gdata_documents_upload_query_class_init (GDataDocumentsUploadQueryClass *klass)
 	                                                      "Folder", "Folder to upload the document into.",
 	                                                      GDATA_TYPE_DOCUMENTS_FOLDER,
 	                                                      G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+
+	/**
+	 * GDataDocumentsUploadQuery:convert:
+	 *
+	 * %TRUE to automatically convert the uploaded document into a standard format (such as a text document, spreadsheet, presentation, etc.).
+	 * %FALSE to upload the document without converting it; this allows for arbitrary files to be uploaded to Google Documents.
+	 *
+	 * For more information, see the
+	 * <ulink type="http" url="https://developers.google.com/google-apps/documents-list/#creating_or_uploading_files";>online documentation</ulink>.
+	 *
+	 * Note that uploading with this property set to %FALSE will only have an effect when using gdata_documents_service_update_document_resumable()
+	 * and not gdata_documents_service_update_document(). Additionally, the #GDataDocumentsDocument passed to
+	 * gdata_documents_service_update_document_resumable() must be a #GDataDocumentsDocument if this property is %FALSE, and a subclass of it
+	 * otherwise.
+	 *
+	 * Since: 0.13.0
+	 */
+	g_object_class_install_property (gobject_class, PROP_CONVERT,
+	                                 g_param_spec_boolean ("convert",
+	                                                       "Convert?", "Whether to automatically convert uploaded documents into a standard format.",
+	                                                       TRUE,
+	                                                       G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
 }
 
 static void
@@ -104,6 +232,9 @@ gdata_documents_upload_query_get_property (GObject *object, guint property_id, G
 		case PROP_FOLDER:
 			g_value_set_object (value, priv->folder);
 			break;
+		case PROP_CONVERT:
+			g_value_set_boolean (value, priv->convert);
+			break;
 		default:
 			/* We don't have any other property... */
 			G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
@@ -120,6 +251,9 @@ gdata_documents_upload_query_set_property (GObject *object, guint property_id, c
 		case PROP_FOLDER:
 			gdata_documents_upload_query_set_folder (self, g_value_get_object (value));
 			break;
+		case PROP_CONVERT:
+			gdata_documents_upload_query_set_convert (self, g_value_get_boolean (value));
+			break;
 		default:
 			/* We don't have any other property... */
 			G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
@@ -159,6 +293,7 @@ gchar *
 gdata_documents_upload_query_build_uri (GDataDocumentsUploadQuery *self)
 {
 	gchar *base_uri;
+	GString *upload_uri;
 	GDataDocumentsUploadQueryPrivate *priv;
 
 	g_return_val_if_fail (GDATA_IS_DOCUMENTS_UPLOAD_QUERY (self), NULL);
@@ -184,7 +319,19 @@ gdata_documents_upload_query_build_uri (GDataDocumentsUploadQuery *self)
 		base_uri = g_strconcat (_gdata_service_get_scheme (), "://docs.google.com/feeds/upload/create-session/default/private/full", NULL);
 	}
 
-	return base_uri;
+	/* Document format conversion. See: https://developers.google.com/google-apps/documents-list/#creating_or_uploading_files */
+	upload_uri = g_string_new (base_uri);
+	g_free (base_uri);
+
+	if (priv->convert == TRUE) {
+		/* Convert documents to standard formats on upload. */
+		g_string_append (upload_uri, "?convert=true");
+	} else {
+		/* Don't convert them â this permits uploading of arbitrary files. */
+		g_string_append (upload_uri, "?convert=false");
+	}
+
+	return g_string_free (upload_uri, FALSE);
 }
 
 /**
@@ -233,3 +380,43 @@ gdata_documents_upload_query_set_folder (GDataDocumentsUploadQuery *self, GDataD
 
 	g_object_notify (G_OBJECT (self), "folder");
 }
+
+/**
+ * gdata_documents_upload_query_get_convert:
+ * @self: a #GDataDocumentsUploadQuery
+ *
+ * Gets #GDataDocumentsUploadQuery:convert.
+ *
+ * Return value: %TRUE to convert documents to common formats, %FALSE to upload them unmodified
+ *
+ * Since: 0.13.0
+ */
+gboolean
+gdata_documents_upload_query_get_convert (GDataDocumentsUploadQuery *self)
+{
+	g_return_val_if_fail (GDATA_IS_DOCUMENTS_UPLOAD_QUERY (self), TRUE);
+
+	return self->priv->convert;
+}
+
+/**
+ * gdata_documents_upload_query_set_convert:
+ * @self: a #GDataDocumentsUploadQuery
+ * @convert: %TRUE to convert documents to common formats, %FALSE to upload them unmodified
+ *
+ * Sets #GDataDocumentsUploadQuery:convert to @convert.
+ *
+ * Since: 0.13.0
+ */
+void
+gdata_documents_upload_query_set_convert (GDataDocumentsUploadQuery *self, gboolean convert)
+{
+	g_return_if_fail (GDATA_IS_DOCUMENTS_UPLOAD_QUERY (self));
+
+	if (convert == self->priv->convert) {
+		return;
+	}
+
+	self->priv->convert = convert;
+	g_object_notify (G_OBJECT (self), "convert");
+}
diff --git a/gdata/services/documents/gdata-documents-upload-query.h b/gdata/services/documents/gdata-documents-upload-query.h
index 92a1959..eb35a14 100644
--- a/gdata/services/documents/gdata-documents-upload-query.h
+++ b/gdata/services/documents/gdata-documents-upload-query.h
@@ -70,6 +70,9 @@ gchar *gdata_documents_upload_query_build_uri (GDataDocumentsUploadQuery *self)
 GDataDocumentsFolder *gdata_documents_upload_query_get_folder (GDataDocumentsUploadQuery *self) G_GNUC_PURE;
 void gdata_documents_upload_query_set_folder (GDataDocumentsUploadQuery *self, GDataDocumentsFolder *folder);
 
+gboolean gdata_documents_upload_query_get_convert (GDataDocumentsUploadQuery *self) G_GNUC_PURE;
+void gdata_documents_upload_query_set_convert (GDataDocumentsUploadQuery *self, gboolean convert);
+
 G_END_DECLS
 
 #endif /* !GDATA_DOCUMENTS_UPLOAD_QUERY_H */
diff --git a/gdata/tests/documents.c b/gdata/tests/documents.c
index ff5bf47..c6e8042 100644
--- a/gdata/tests/documents.c
+++ b/gdata/tests/documents.c
@@ -503,10 +503,24 @@ const gchar *resumable_type_names[] = {
 	"non-resumable",
 };
 
+typedef enum {
+	UPLOAD_ODT_CONVERT,
+	UPLOAD_ODT_NO_CONVERT,
+	UPLOAD_BIN_NO_CONVERT,
+} DocumentType;
+#define UPLOAD_DOCUMENT_TYPE_MAX UPLOAD_BIN_NO_CONVERT
+
+const gchar *document_type_names[] = {
+	"odt-convert",
+	"odt-no-convert",
+	"bin-no-convert",
+};
+
 typedef struct {
 	PayloadType payload_type;
 	FolderType folder_type;
 	ResumableType resumable_type;
+	DocumentType document_type;
 	gchar *test_name;
 
 	GDataDocumentsService *service;
@@ -564,24 +578,56 @@ test_upload (UploadDocumentData *data, gconstpointer _test_params)
 	const UploadDocumentTestParams *test_params = _test_params;
 
 	GDataDocumentsDocument *document = NULL;
+	const gchar *document_filename = NULL;
 	GFile *document_file = NULL;
 	GFileInfo *file_info = NULL;
+	GDataDocumentsUploadQuery *upload_query = NULL;
+	GDataLink *edit_media_link;
 	GError *error = NULL;
 
+	upload_query = gdata_documents_upload_query_new ();
+
+	/* File to upload. (Ignored if we're doing a metadata-only upload.) Also set the conversion type (ignored for non-resumable uploads). */
+	switch (test_params->document_type) {
+		case UPLOAD_ODT_CONVERT:
+			/* ODT file. */
+			document_filename = "test.odt";
+			gdata_documents_upload_query_set_convert (upload_query, TRUE);
+			break;
+		case UPLOAD_ODT_NO_CONVERT:
+			/* ODT file. */
+			document_filename = "test.odt";
+			gdata_documents_upload_query_set_convert (upload_query, FALSE);
+			break;
+		case UPLOAD_BIN_NO_CONVERT:
+			/* Arbitrary binary file. */
+			document_filename = "sample.ogg";
+			gdata_documents_upload_query_set_convert (upload_query, FALSE);
+			break;
+		default:
+			g_assert_not_reached ();
+	}
+
 	/* Upload content? */
 	switch (test_params->payload_type) {
 		case UPLOAD_METADATA_ONLY:
+			document_filename = NULL;
 			document_file = NULL;
 			file_info = NULL;
 			break;
 		case UPLOAD_CONTENT_ONLY:
-		case UPLOAD_CONTENT_AND_METADATA:
-			document_file = g_file_new_for_path (TEST_FILE_DIR "test.odt");
+		case UPLOAD_CONTENT_AND_METADATA: {
+			gchar *document_file_path = g_strconcat (TEST_FILE_DIR, document_filename, NULL);
+			document_file = g_file_new_for_path (document_file_path);
+			g_free (document_file_path);
+
 			file_info = g_file_query_info (document_file,
 			                               G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME "," G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE ","
 			                               G_FILE_ATTRIBUTE_STANDARD_SIZE, G_FILE_QUERY_INFO_NONE, NULL, &error);
 			g_assert_no_error (error);
+
 			break;
+		}
 		default:
 			g_assert_not_reached ();
 	}
@@ -595,7 +641,17 @@ test_upload (UploadDocumentData *data, gconstpointer _test_params)
 		case UPLOAD_CONTENT_AND_METADATA: {
 			gchar *title;
 
-			document = GDATA_DOCUMENTS_DOCUMENT (gdata_documents_text_new (NULL));
+			switch (test_params->document_type) {
+				case UPLOAD_ODT_CONVERT:
+					document = GDATA_DOCUMENTS_DOCUMENT (gdata_documents_text_new (NULL));
+					break;
+				case UPLOAD_ODT_NO_CONVERT:
+				case UPLOAD_BIN_NO_CONVERT:
+					document = GDATA_DOCUMENTS_DOCUMENT (gdata_documents_document_new (NULL));
+					break;
+				default:
+					g_assert_not_reached ();
+			}
 
 			/* Build a title including the test details. */
 			title = g_strdup_printf ("Test Upload file (%s)", test_params->test_name);
@@ -631,10 +687,7 @@ test_upload (UploadDocumentData *data, gconstpointer _test_params)
 				                                                         g_file_info_get_content_type (file_info), data->folder,
 				                                                         NULL, &error);
 				break;
-			case UPLOAD_RESUMABLE: {
-				GDataDocumentsUploadQuery *upload_query;
-
-				upload_query = gdata_documents_upload_query_new ();
+			case UPLOAD_RESUMABLE:
 				gdata_documents_upload_query_set_folder (upload_query, data->folder);
 
 				upload_stream = gdata_documents_service_upload_document_resumable (test_params->service, document,
@@ -643,10 +696,7 @@ test_upload (UploadDocumentData *data, gconstpointer _test_params)
 				                                                                   g_file_info_get_size (file_info), upload_query,
 				                                                                   NULL, &error);
 
-				g_object_unref (upload_query);
-
 				break;
-			}
 			default:
 				g_assert_not_reached ();
 		}
@@ -673,7 +723,7 @@ test_upload (UploadDocumentData *data, gconstpointer _test_params)
 		g_object_unref (file_stream);
 	}
 
-	g_assert (GDATA_IS_DOCUMENTS_TEXT (data->new_document));
+	g_assert (GDATA_IS_DOCUMENTS_DOCUMENT (data->new_document)); /* note that this isn't entirely specific */
 
 	/* Verify the uploaded document is the same as the original */
 	switch (test_params->payload_type) {
@@ -684,7 +734,7 @@ test_upload (UploadDocumentData *data, gconstpointer _test_params)
 		case UPLOAD_CONTENT_ONLY:
 			/* HACK: The title returned by the server varies depending on how we uploaded the document. */
 			if (test_params->resumable_type == UPLOAD_NON_RESUMABLE) {
-				g_assert_cmpstr (gdata_entry_get_title (GDATA_ENTRY (data->new_document)), ==, "test.odt");
+				g_assert_cmpstr (gdata_entry_get_title (GDATA_ENTRY (data->new_document)), ==, document_filename);
 			} else {
 				g_assert_cmpstr (gdata_entry_get_title (GDATA_ENTRY (data->new_document)), ==, "Untitled");
 			}
@@ -694,6 +744,29 @@ test_upload (UploadDocumentData *data, gconstpointer _test_params)
 			g_assert_not_reached ();
 	}
 
+	/* Check it's been correctly converted/not converted and is of the right document type. */
+	edit_media_link = gdata_entry_look_up_link (GDATA_ENTRY (data->new_document), GDATA_LINK_EDIT_MEDIA);
+
+	switch (test_params->document_type) {
+		case UPLOAD_ODT_CONVERT:
+			g_assert (GDATA_IS_DOCUMENTS_TEXT (data->new_document));
+			g_assert (g_str_has_prefix (gdata_documents_entry_get_resource_id (GDATA_DOCUMENTS_ENTRY (data->new_document)), "document:"));
+			g_assert_cmpstr (gdata_link_get_content_type (edit_media_link), ==, "text/html");
+			break;
+		case UPLOAD_ODT_NO_CONVERT:
+			g_assert (GDATA_IS_DOCUMENTS_DOCUMENT (data->new_document));
+			g_assert (g_str_has_prefix (gdata_documents_entry_get_resource_id (GDATA_DOCUMENTS_ENTRY (data->new_document)), "file:"));
+			g_assert_cmpstr (gdata_link_get_content_type (edit_media_link), ==, "application/vnd.oasis.opendocument.text");
+			break;
+		case UPLOAD_BIN_NO_CONVERT:
+			g_assert (GDATA_IS_DOCUMENTS_DOCUMENT (data->new_document));
+			g_assert (g_str_has_prefix (gdata_documents_entry_get_resource_id (GDATA_DOCUMENTS_ENTRY (data->new_document)), "file:"));
+			g_assert_cmpstr (gdata_link_get_content_type (edit_media_link), ==, "video/x-theora+ogg");
+			break;
+		default:
+			g_assert_not_reached ();
+	}
+
 	/* Check it's in the right folder. */
 	switch (test_params->folder_type) {
 		case UPLOAD_IN_FOLDER:
@@ -712,6 +785,7 @@ test_upload (UploadDocumentData *data, gconstpointer _test_params)
 	}
 
 	g_clear_error (&error);
+	g_clear_object (&upload_query);
 	g_clear_object (&document_file);
 	g_clear_object (&document);
 }
@@ -1497,34 +1571,42 @@ main (int argc, char *argv[])
 			PayloadType i;
 			FolderType j;
 			ResumableType k;
+			DocumentType l;
 
 			for (i = 0; i < UPLOAD_PAYLOAD_TYPE_MAX + 1; i++) {
 				for (j = 0; j < UPLOAD_FOLDER_TYPE_MAX + 1; j++) {
 					for (k = 0; k < UPLOAD_RESUMABLE_TYPE_MAX + 1; k++) {
-						UploadDocumentTestParams *test_params;
-						gchar *test_name;
-
-						/* Resumable metadata-only uploads don't make sense. */
-						if (i == UPLOAD_METADATA_ONLY && k == UPLOAD_RESUMABLE) {
-							continue;
+						for (l = 0; l < UPLOAD_DOCUMENT_TYPE_MAX + 1; l++) {
+							UploadDocumentTestParams *test_params;
+							gchar *test_name;
+
+							/* Resumable metadata-only uploads don't make sense. */
+							if (i == UPLOAD_METADATA_ONLY && k == UPLOAD_RESUMABLE) {
+								continue;
+							}
+							/* Non-resumable non-conversion uploads don't make sense. */
+							if (k == UPLOAD_NON_RESUMABLE && l != UPLOAD_ODT_CONVERT) {
+								continue;
+							}
+
+							test_name = g_strdup_printf ("/documents/upload/%s/%s/%s/%s",
+							                             payload_type_names[i], folder_type_names[j],
+							                             resumable_type_names[k], document_type_names[l]);
+
+							/* Allocate a new struct. We leak this. */
+							test_params = g_slice_new0 (UploadDocumentTestParams);
+							test_params->payload_type = i;
+							test_params->folder_type = j;
+							test_params->resumable_type = k;
+							test_params->document_type = l;
+							test_params->test_name = g_strdup (test_name);
+							test_params->service = GDATA_DOCUMENTS_SERVICE (service);
+
+							g_test_add (test_name, UploadDocumentData, test_params, set_up_upload_document, test_upload,
+							            tear_down_upload_document);
+
+							g_free (test_name);
 						}
-
-						test_name = g_strdup_printf ("/documents/upload/%s/%s/%s",
-						                             payload_type_names[i], folder_type_names[j],
-						                             resumable_type_names[k]);
-
-						/* Allocate a new struct. We leak this. */
-						test_params = g_slice_new0 (UploadDocumentTestParams);
-						test_params->payload_type = i;
-						test_params->folder_type = j;
-						test_params->resumable_type = k;
-						test_params->test_name = g_strdup (test_name);
-						test_params->service = GDATA_DOCUMENTS_SERVICE (service);
-
-						g_test_add (test_name, UploadDocumentData, test_params, set_up_upload_document, test_upload,
-						            tear_down_upload_document);
-
-						g_free (test_name);
 					}
 				}
 			}



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