[libgdata] Bug 589545 – *_download_document(): let the client decide where to download



commit 88159d6a2c37cf3edd1b80398a020968d6728c39
Author: Philip Withnall <philip tecnocode co uk>
Date:   Mon Jul 27 23:42:34 2009 +0100

    Bug 589545 â?? *_download_document(): let the client decide where to download
    
    Patch from Thibault Saunier <saunierthibault gmail com> to change the
    semantics of the *_download_document() functions. Closes: bgo#589545

 gdata/gdata-private.h                              |    2 +-
 gdata/services/documents/gdata-documents-entry.c   |   60 +++++++++++---------
 .../documents/gdata-documents-presentation.c       |   13 ++--
 .../documents/gdata-documents-presentation.h       |    2 +-
 .../documents/gdata-documents-spreadsheet.c        |   16 +++--
 .../documents/gdata-documents-spreadsheet.h        |    2 +-
 gdata/services/documents/gdata-documents-text.c    |   13 ++--
 gdata/services/documents/gdata-documents-text.h    |    2 +-
 gdata/tests/documents.c                            |   57 +++++++++++++++----
 9 files changed, 105 insertions(+), 62 deletions(-)
---
diff --git a/gdata/gdata-private.h b/gdata/gdata-private.h
index a978d3a..162f35d 100644
--- a/gdata/gdata-private.h
+++ b/gdata/gdata-private.h
@@ -57,7 +57,7 @@ void _gdata_feed_parse_data_free (gpointer data);
 void _gdata_feed_call_progress_callback (GDataFeed *self, gpointer user_data, GDataEntry *entry);
 
 #include "gdata/services/documents/gdata-documents-entry.h"
-GFile *_gdata_documents_entry_download_document (GDataDocumentsEntry *self, GDataService *service, gchar **content_type, gchar *download_uri,
+GFile *_gdata_documents_entry_download_document (GDataDocumentsEntry *self, GDataService *service, gchar **content_type, const gchar *download_uri,
 						 GFile *destination_directory, const gchar *file_extension, gboolean replace_file_if_exists,
 						 GCancellable *cancellable, GError **error) G_GNUC_WARN_UNUSED_RESULT;
 
diff --git a/gdata/services/documents/gdata-documents-entry.c b/gdata/services/documents/gdata-documents-entry.c
index efb9b29..7797a07 100644
--- a/gdata/services/documents/gdata-documents-entry.c
+++ b/gdata/services/documents/gdata-documents-entry.c
@@ -523,7 +523,7 @@ got_chunk_cb (SoupMessage *message, SoupBuffer *chunk, GOutputStream *output_str
  * @service: an authenticated #GDataDocumentsService
  * @content_type: return location for the document's content type, or %NULL; free with g_free()
  * @download_uri: the URI to download the document
- * @destination_directory: the directory into which the file should be downloaded
+ * @destination_file: the #GFile into which the document file should be saved
  * @file_extension: the extension with which to save the downloaded file
  * @replace_file_if_exists: %TRUE if you want to replace the file if it exists, %FALSE otherwise
  * @cancellable: optional #GCancellable object, or %NULL
@@ -542,29 +542,30 @@ got_chunk_cb (SoupMessage *message, SoupBuffer *chunk, GOutputStream *output_str
  *
  * If there is an error downloading the document, a %GDATA_SERVICE_ERROR_WITH_QUERY error will be returned.
  *
+ * If @destination_file is a directory, the file will be downloaded to this directory with the #GDataEntry:title and 
+ * the appropriate extension as its filename.
+ *
  * Return value: a #GFile pointing to the downloaded document, or %NULL; unref with g_object_unref()
  *
  * Since: 0.4.0
  */
 GFile *
-_gdata_documents_entry_download_document (GDataDocumentsEntry *self, GDataService *service, gchar **content_type, gchar *download_uri,
-					  GFile *destination_directory, const gchar *file_extension, gboolean replace_file_if_exists,
+_gdata_documents_entry_download_document (GDataDocumentsEntry *self, GDataService *service, gchar **content_type, const gchar *download_uri,
+					  GFile *destination_file, const gchar *file_extension, gboolean replace_file_if_exists,
 					  GCancellable *cancellable, GError **error)
 {
 	GDataServiceClass *klass;
 	GFileOutputStream *file_stream;
-	GFile *destination_file;
 	SoupMessage *message;
 	guint status;
-	const gchar *document_title;
-	gchar *filename;
 
 	/* TODO: async version */
 	g_return_val_if_fail (GDATA_IS_DOCUMENTS_ENTRY (self), NULL);
 	g_return_val_if_fail (GDATA_IS_SERVICE (service), NULL);
 	g_return_val_if_fail (download_uri != NULL, NULL);
-	g_return_val_if_fail (G_IS_FILE (destination_directory), NULL);
+	g_return_val_if_fail (G_IS_FILE (destination_file), NULL);
 	g_return_val_if_fail (file_extension != NULL, NULL);
+	g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), NULL);
 
 	/* Ensure we're authenticated first */
 	if (gdata_service_is_authenticated (GDATA_SERVICE (service)) == FALSE) {
@@ -573,25 +574,33 @@ _gdata_documents_entry_download_document (GDataDocumentsEntry *self, GDataServic
 		return NULL;
 	}
 
-	/* Prepare the GFile */
-	document_title = gdata_entry_get_title (GDATA_ENTRY (self));
-	filename = g_strdup_printf ("%s.%s", document_title, file_extension);
-	destination_file = g_file_get_child (destination_directory, filename);
-	g_free (filename);
-
-	/* Check if the file exists */
-	if (g_file_query_exists (destination_file, cancellable) == TRUE) {
+	/* Create a new file */
+	file_stream = g_file_create (destination_file, G_FILE_CREATE_NONE, cancellable, error);
+	if (g_error_matches (*error, G_IO_ERROR, G_IO_ERROR_EXISTS)) {
 		/* Replace a pre-existing file */
 		if (replace_file_if_exists == TRUE) {
-			file_stream = g_file_replace (destination_file, NULL, TRUE, G_FILE_CREATE_REPLACE_DESTINATION,
-						      cancellable, error);
-		} else {
-			g_set_error (error, G_IO_ERROR_EXISTS, 1, NULL);
-			return NULL;
+			g_clear_error (error);
+			file_stream = g_file_replace (destination_file, NULL, TRUE, G_FILE_CREATE_REPLACE_DESTINATION, cancellable, error);
+		}
+
+		if (g_error_matches (*error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY)) {
+			GFile *new_destination_file = NULL;
+			const gchar *document_title;
+			gchar *filename;
+
+			g_clear_error (error);
+
+			/* Prepare the GFile */
+			document_title = gdata_entry_get_title (GDATA_ENTRY (self));
+			filename = g_strdup_printf ("%s.%s", document_title, file_extension);
+			new_destination_file = g_file_get_child (destination_file, filename);
+			g_free (filename);
+
+			return _gdata_documents_entry_download_document (self, service, content_type, download_uri, new_destination_file, 
+									 file_extension, replace_file_if_exists, cancellable, error);
 		}
-	} else {
-		/* Create a new file */
-		file_stream = g_file_create (destination_file, G_FILE_CREATE_NONE, cancellable, error);
+
+		return NULL;
 	}
 
 	/* Get the document URI */
@@ -611,14 +620,12 @@ _gdata_documents_entry_download_document (GDataDocumentsEntry *self, GDataServic
 	g_object_unref (file_stream);
 	if (status == SOUP_STATUS_NONE) {
 		g_object_unref (message);
-		g_object_unref (destination_file);
 		return NULL;
 	}
 
 	/* Check for cancellation */
 	if (g_cancellable_set_error_if_cancelled (cancellable, error) == TRUE) {
 		g_object_unref (message);
-		g_object_unref (destination_file);
 		return NULL;
 	}
 
@@ -628,7 +635,6 @@ _gdata_documents_entry_download_document (GDataDocumentsEntry *self, GDataServic
 		klass->parse_error_response (GDATA_SERVICE (service), GDATA_SERVICE_ERROR_WITH_QUERY, status, message->reason_phrase,
 					     message->response_body->data, message->response_body->length, error);
 		g_object_unref (message);
-		g_object_unref (destination_file);
 		return NULL;
 	}
 
@@ -638,5 +644,5 @@ _gdata_documents_entry_download_document (GDataDocumentsEntry *self, GDataServic
 
 	g_object_unref (message);
 
-	return destination_file;
+	return g_object_ref (destination_file);
 }
diff --git a/gdata/services/documents/gdata-documents-presentation.c b/gdata/services/documents/gdata-documents-presentation.c
index 4bc840c..8d966d8 100644
--- a/gdata/services/documents/gdata-documents-presentation.c
+++ b/gdata/services/documents/gdata-documents-presentation.c
@@ -94,7 +94,7 @@ gdata_documents_presentation_new (const gchar *id)
  * @service: a #GDataDocumentsService
  * @content_type: return location for the document's content type, or %NULL; free with g_free()
  * @export_format: the format in which the presentation should be exported
- * @destination_directory: the directory into which the presentation file should be saved
+ * @destination_file: the #GFile into which the presentation file should be saved
  * @replace_file_if_exists: %TRUE if the file should be replaced if it already exists, %FALSE otherwise
  * @cancellable: optional #GCancellable object, or %NULL
  * @error: a #GError, or %NULL
@@ -107,16 +107,18 @@ gdata_documents_presentation_new (const gchar *id)
  *
  * If there is an error getting the document, a %GDATA_SERVICE_ERROR_WITH_QUERY error will be returned.
  *
+ * If @destination_file is a directory, then the file will be downloaded in this directory with the #GDataEntry:title with 
+ * the apropriate extension as name.
+ *
  * Return value: the document's data, or %NULL; unref with g_object_unref()
  *
  * Since: 0.4.0
  **/
 GFile *
 gdata_documents_presentation_download_document (GDataDocumentsPresentation *self, GDataDocumentsService *service, gchar **content_type,
-						GDataDocumentsPresentationFormat export_format, GFile *destination_directory,
+						GDataDocumentsPresentationFormat export_format, GFile *destination_file,
 						gboolean replace_file_if_exists, GCancellable *cancellable, GError **error)
 {
-	GFile *destination_file;
 	const gchar *document_id;
 	gchar *link_href;
 
@@ -131,7 +133,7 @@ gdata_documents_presentation_download_document (GDataDocumentsPresentation *self
 	/* TODO: async version */
 	g_return_val_if_fail (GDATA_IS_DOCUMENTS_PRESENTATION (self), NULL);
 	g_return_val_if_fail (GDATA_IS_DOCUMENTS_SERVICE (service), NULL);
-	g_return_val_if_fail (G_IS_FILE (destination_directory), NULL);
+	g_return_val_if_fail (G_IS_FILE (destination_file), NULL);
 	g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), NULL);
 	g_return_val_if_fail (export_format >= 0 && export_format < G_N_ELEMENTS (export_formats), NULL);
 
@@ -143,8 +145,7 @@ gdata_documents_presentation_download_document (GDataDocumentsPresentation *self
 
 	/* Call the common download method on the parent class */
 	destination_file = _gdata_documents_entry_download_document (GDATA_DOCUMENTS_ENTRY (self), GDATA_SERVICE (service), content_type,
-								     link_href, destination_directory, export_formats[export_format],
-								     replace_file_if_exists, cancellable, error);
+								     link_href, destination_file, export_formats[export_format], replace_file_if_exists, cancellable, error);
 	g_free (link_href);
 
 	return destination_file;
diff --git a/gdata/services/documents/gdata-documents-presentation.h b/gdata/services/documents/gdata-documents-presentation.h
index f8f02f6..211a459 100644
--- a/gdata/services/documents/gdata-documents-presentation.h
+++ b/gdata/services/documents/gdata-documents-presentation.h
@@ -87,7 +87,7 @@ GDataDocumentsPresentation *gdata_documents_presentation_new (const gchar *id) G
 
 #include <gdata/services/documents/gdata-documents-service.h>
 GFile *gdata_documents_presentation_download_document (GDataDocumentsPresentation *self, GDataDocumentsService *service, gchar **content_type,
-						       GDataDocumentsPresentationFormat export_format, GFile *destination_directory,
+						       GDataDocumentsPresentationFormat export_format, GFile *destination_file,
 						       gboolean replace_file_if_exists, GCancellable *cancellable,
 						       GError **error) G_GNUC_WARN_UNUSED_RESULT;
 
diff --git a/gdata/services/documents/gdata-documents-spreadsheet.c b/gdata/services/documents/gdata-documents-spreadsheet.c
index 45c494d..a7c305a 100644
--- a/gdata/services/documents/gdata-documents-spreadsheet.c
+++ b/gdata/services/documents/gdata-documents-spreadsheet.c
@@ -89,12 +89,12 @@ gdata_documents_spreadsheet_new (const gchar *id)
 
 /**
  * gdata_documents_spreadsheet_download_document:
- * @self: a #GDataDocumentsPresentation
+ * @self: a #GDataDocumentsSpreadsheet
  * @service: a #GDataDocumentsService
  * @content_type: return location for the document's content type, or %NULL; free with g_free()
- * @export_format: the format in which the presentation should be exported
+ * @export_format: the format in which the spreadsheet should be exported
  * @gid: the %0-based sheet ID to download, or %-1
- * @destination_directory: the directory into which the presentation file should be saved
+ * @destination_file: the #GFile into which the spreadsheet file should be saved
  * @replace_file_if_exists: %TRUE if the file should be replaced if it already exists, %FALSE otherwise
  * @cancellable: optional #GCancellable object, or %NULL
  * @error: a #GError, or %NULL
@@ -109,6 +109,9 @@ gdata_documents_spreadsheet_new (const gchar *id)
  * parameter called @gid which indicates which grid, or sheet, you wish to get (the index is %0-based, so gid %1 actually refers
  * to the second sheet sheet on a given spreadsheet).
  *
+ * If @destination_file is a directory, then the file will be downloaded in this directory with the #GDataEntry:title with 
+ * the apropriate extension as name.
+ *
  * If there is an error getting the document, a %GDATA_SERVICE_ERROR_WITH_QUERY error will be returned.
  *
  * Return value: the document's data, or %NULL; unref with g_object_unref()
@@ -117,11 +120,10 @@ gdata_documents_spreadsheet_new (const gchar *id)
  **/
 GFile *
 gdata_documents_spreadsheet_download_document (GDataDocumentsSpreadsheet *self, GDataDocumentsService *service, gchar **content_type,
-					       GDataDocumentsSpreadsheetFormat export_format, gint gid, GFile *destination_directory,
+					       GDataDocumentsSpreadsheetFormat export_format, gint gid, GFile *destination_file,
 					       gboolean replace_file_if_exists, GCancellable *cancellable, GError **error)
 {
 	gchar *link_href;
-	GFile *destination_file;
 	const gchar *document_id, *extension, *fmcmd;
 	GDataService *spreadsheet_service;
 
@@ -141,7 +143,7 @@ gdata_documents_spreadsheet_download_document (GDataDocumentsSpreadsheet *self,
 	g_return_val_if_fail (gid >= -1, NULL);
 	g_return_val_if_fail ((export_format != GDATA_DOCUMENTS_SPREADSHEET_CSV && export_format != GDATA_DOCUMENTS_SPREADSHEET_TSV) ||
 			      gid != -1, NULL);
-	g_return_val_if_fail (G_IS_FILE (destination_directory), NULL);
+	g_return_val_if_fail (G_IS_FILE (destination_file), NULL);
 	g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), NULL);
 
 	document_id = gdata_documents_entry_get_document_id (GDATA_DOCUMENTS_ENTRY (self));
@@ -164,7 +166,7 @@ gdata_documents_spreadsheet_download_document (GDataDocumentsSpreadsheet *self,
 
 	/* Download the document */
 	destination_file = _gdata_documents_entry_download_document (GDATA_DOCUMENTS_ENTRY (self), spreadsheet_service, content_type,
-								     link_href, destination_directory, extension, replace_file_if_exists,
+								     link_href, destination_file, extension, replace_file_if_exists,
 								     cancellable, error);
 	g_free (link_href);
 
diff --git a/gdata/services/documents/gdata-documents-spreadsheet.h b/gdata/services/documents/gdata-documents-spreadsheet.h
index 0f56493..30b2045 100644
--- a/gdata/services/documents/gdata-documents-spreadsheet.h
+++ b/gdata/services/documents/gdata-documents-spreadsheet.h
@@ -90,7 +90,7 @@ GDataDocumentsSpreadsheet *gdata_documents_spreadsheet_new (const gchar *id) G_G
 #include <gdata/services/documents/gdata-documents-service.h>
 
 GFile *gdata_documents_spreadsheet_download_document (GDataDocumentsSpreadsheet *self, GDataDocumentsService *service, gchar **content_type,
-						      GDataDocumentsSpreadsheetFormat export_format, gint gid, GFile *destination_directory,
+						      GDataDocumentsSpreadsheetFormat export_format, gint gid, GFile *destination_file,
 						      gboolean replace_file_if_exists, GCancellable *cancellable,
 						      GError **error) G_GNUC_WARN_UNUSED_RESULT;
 
diff --git a/gdata/services/documents/gdata-documents-text.c b/gdata/services/documents/gdata-documents-text.c
index 731ed24..933059a 100644
--- a/gdata/services/documents/gdata-documents-text.c
+++ b/gdata/services/documents/gdata-documents-text.c
@@ -93,7 +93,7 @@ gdata_documents_text_new (const gchar *id)
  * @service: a #GDataDocumentsService
  * @content_type: return location for the document's content type, or %NULL; free with g_free()
  * @export_format: the format in which the text document should be exported
- * @destination_directory: the directory into which the text document file should be saved
+ * @destination_file: the #GFile into which the text file should be saved
  * @replace_file_if_exists: %TRUE if the file should be replaced if it already exists, %FALSE otherwise
  * @cancellable: optional #GCancellable object, or %NULL
  * @error: a #GError, or %NULL
@@ -104,6 +104,9 @@ gdata_documents_text_new (const gchar *id)
  * If @cancellable is not %NULL, then the operation can be cancelled by triggering the @cancellable object from another thread.
  * If the operation was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
  *
+ * If @destination_file is a directory, then the file will be downloaded in this directory with the #GDataEntry:title with 
+ * the apropriate extension as name.
+ *
  * If there is an error getting the document, a %GDATA_SERVICE_ERROR_WITH_QUERY error will be returned.
  *
  * Return value: the document's data, or %NULL; unref with g_object_unref()
@@ -112,10 +115,9 @@ gdata_documents_text_new (const gchar *id)
  **/
 GFile *
 gdata_documents_text_download_document (GDataDocumentsText *self, GDataDocumentsService *service, gchar **content_type,
-					GDataDocumentsTextFormat export_format, GFile *destination_directory,
+					GDataDocumentsTextFormat export_format, GFile *destination_file,
 					gboolean replace_file_if_exists, GCancellable *cancellable, GError **error)
 {
-	GFile *destination_file;
 	const gchar *document_id;
 	gchar *link_href;
 
@@ -134,7 +136,7 @@ gdata_documents_text_download_document (GDataDocumentsText *self, GDataDocuments
 	g_return_val_if_fail (GDATA_IS_DOCUMENTS_TEXT (self), NULL);
 	g_return_val_if_fail (GDATA_IS_DOCUMENTS_SERVICE (service), NULL);
 	g_return_val_if_fail (export_format >= 0 && export_format < G_N_ELEMENTS (export_formats), NULL);
-	g_return_val_if_fail (G_IS_FILE (destination_directory), NULL);
+	g_return_val_if_fail (G_IS_FILE (destination_file), NULL);
 	g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), NULL);
 
 	document_id = gdata_documents_entry_get_document_id (GDATA_DOCUMENTS_ENTRY (self));
@@ -145,8 +147,7 @@ gdata_documents_text_download_document (GDataDocumentsText *self, GDataDocuments
 
 	/* Download the file */
 	destination_file = _gdata_documents_entry_download_document (GDATA_DOCUMENTS_ENTRY (self), GDATA_SERVICE (service),
-								     content_type, link_href, destination_directory,
-								     export_formats[export_format], replace_file_if_exists,
+								     content_type, link_href, destination_file, export_formats[export_format], replace_file_if_exists,
 								     cancellable, error);
 	g_free (link_href);
 
diff --git a/gdata/services/documents/gdata-documents-text.h b/gdata/services/documents/gdata-documents-text.h
index b7015da..f8526df 100644
--- a/gdata/services/documents/gdata-documents-text.h
+++ b/gdata/services/documents/gdata-documents-text.h
@@ -94,7 +94,7 @@ GDataDocumentsText *gdata_documents_text_new (const gchar *id) G_GNUC_WARN_UNUSE
 #include <gdata/services/documents/gdata-documents-service.h>
 
 GFile *gdata_documents_text_download_document (GDataDocumentsText *self, GDataDocumentsService *service, gchar **content_type,
-					       GDataDocumentsTextFormat export_format, GFile *destination_directory,
+					       GDataDocumentsTextFormat export_format, GFile *destination_file,
 					       gboolean replace_file_if_exists, GCancellable *cancellable,
 					       GError **error) G_GNUC_WARN_UNUSED_RESULT;
 
diff --git a/gdata/tests/documents.c b/gdata/tests/documents.c
index 39569d4..9872813 100644
--- a/gdata/tests/documents.c
+++ b/gdata/tests/documents.c
@@ -464,37 +464,71 @@ test_download_all_documents (GDataService *service)
 	GDataDocumentsFeed *feed;
 	GError *error = NULL;
 	gchar *content_type = NULL;
-	GFile *destination_directory;
+	GFile *destination_file;
+	gchar *destination_file_name;
+	GString *destination_display_name;
 	GList *i;
+	gint ppt_nb = 0, ods_nb = 0, odt_nb = 0;
 
-	destination_directory = g_file_new_for_path ("/tmp");
 	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) {
-		GFile *destination_file = NULL;
-
 		if (GDATA_IS_DOCUMENTS_PRESENTATION (i->data)) {
+			destination_file = g_file_new_for_path ("/tmp");
 			destination_file = gdata_documents_presentation_download_document (GDATA_DOCUMENTS_PRESENTATION (i->data), GDATA_DOCUMENTS_SERVICE (service),
-											   &content_type, GDATA_DOCUMENTS_PRESENTATION_PPT, destination_directory,
+											   &content_type, GDATA_DOCUMENTS_PRESENTATION_PPT, destination_file,
 											   TRUE, NULL, &error);
+
 		} else if (GDATA_IS_DOCUMENTS_SPREADSHEET (i->data)) {
+			GFile *destination_file;
+
+			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_spreadsheet_download_document (i->data, GDATA_DOCUMENTS_SERVICE (service),
-											  &content_type, GDATA_DOCUMENTS_SPREADSHEET_ODS, -1, destination_directory,
+											  &content_type, GDATA_DOCUMENTS_SPREADSHEET_ODS, -1, 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);
 		} else if (GDATA_IS_DOCUMENTS_TEXT (i->data)) {
+			GFile *destination_file;
+
+			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_text_download_document (i->data, GDATA_DOCUMENTS_SERVICE (service), &content_type, GDATA_DOCUMENTS_TEXT_ODT,
-										   destination_directory, TRUE, NULL, &error);
+										   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);
 		}
 
 		g_assert_no_error (error);
-		if (destination_file != NULL)
-			g_object_unref (destination_file);
 		g_free (content_type);
 	}
 
-	g_object_unref (destination_directory);
 	g_object_unref (feed);
 	g_clear_error (&error);
 }
@@ -560,9 +594,7 @@ main (int argc, char *argv[])
 	g_test_add_data_func ("/documents/upload/metadata_file", service, test_upload_metadata_file);
 	g_test_add_data_func ("/documents/upload/only_metadata", service, test_upload_metadata);
 	g_test_add_data_func ("/documents/upload/metadata_file_in_new_folder", service, test_upload_file_metadata_in_new_folder);
-
 	g_test_add_data_func ("/documents/download/download_all_documents", service, test_download_all_documents);
-
 	g_test_add_data_func ("/documents/update/only_metadata", service, test_update_metadata);
 	g_test_add_data_func ("/documents/update/only_file", service, test_update_file);
 	g_test_add_data_func ("/documents/update/metadata_file", service, test_update_metadata_file);
@@ -580,6 +612,7 @@ main (int argc, char *argv[])
 	g_test_add_data_func ("/documents/move/remove_from_folder", service, test_add_remove_file_from_folder);
 
 	g_test_add_data_func ("/documents/remove/all", service, test_remove_all_documents_and_folders);
+
 	retval = g_test_run ();
 
 	g_object_unref (service);



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