[evince] libdocument: Simplify ev_document_factory_get_document()



commit a082045890434e716c82a1580bc5f28112ef7d9c
Author: Murray Cumming <murrayc murrayc com>
Date:   Mon Jul 18 10:12:14 2011 +0200

    libdocument: Simplify ev_document_factory_get_document()
    
    * libdocument/ev-document-factory.c
    (ev_document_factory_get_document): This was two blocks of
    copy-pasted code, with a slight difference in the second block.
    Moved it into a new ev_document_factory_get_document_and_load()
    helper function, which is called twice, with the second call
    setting an error if it fails (when even slow mime detection
    fails.)

 libdocument/ev-document-factory.c |  137 ++++++++++++++++++-------------------
 1 files changed, 66 insertions(+), 71 deletions(-)
---
diff --git a/libdocument/ev-document-factory.c b/libdocument/ev-document-factory.c
index 1f1eb0d..3f679e8 100644
--- a/libdocument/ev-document-factory.c
+++ b/libdocument/ev-document-factory.c
@@ -135,21 +135,11 @@ free_uncompressed_uri (gchar *uri_unc)
 	g_free (uri_unc);
 }
 
-/**
- * ev_document_factory_get_document:
- * @uri: an URI
- * @error: a #GError location to store an error, or %NULL
- *
- * Creates a #EvDocument for the document at @uri; or, if no backend handling
- * the document's type is found, or an error occurred on opening the document,
- * returns %NULL and fills in @error.
- * If the document is encrypted, it is returned but also @error is set to
- * %EV_DOCUMENT_ERROR_ENCRYPTED.
- *
- * Returns: a new #EvDocument, or %NULL.
+/* Try to get and load the document from a file, dealing with errors 
+ * differently depending on whether we are using slow or fast mime detection.
  */
-EvDocument *
-ev_document_factory_get_document (const char *uri, GError **error)
+static EvDocument *
+ev_document_factory_load_uri (const char *uri, gboolean fast, GError **error)
 {
 	EvDocument *document;
 	int result;
@@ -157,50 +147,20 @@ ev_document_factory_get_document (const char *uri, GError **error)
 	gchar *uri_unc = NULL;
 	GError *err = NULL;
 
-	g_return_val_if_fail (uri != NULL, NULL);
-
-	document = get_document_from_uri (uri, TRUE, &compression, &err);
+	document = get_document_from_uri (uri, fast, &compression, &err);
 	g_assert (document != NULL || err != NULL);
 
-	if (document != NULL) {
-		uri_unc = ev_file_uncompress (uri, compression, &err);
-		if (uri_unc) {
-			g_object_set_data_full (G_OBJECT (document),
-						"uri-uncompressed",
-						uri_unc,
-						(GDestroyNotify) free_uncompressed_uri);
-		} else if (err != NULL) {
-			/* Error uncompressing file */
-			g_object_unref (document);
-			g_propagate_error (error, err);
-			return NULL;
-		}
-
-		result = ev_document_load (document, uri_unc ? uri_unc : uri, &err);
-
-		if (result == FALSE || err) {
-			if (err &&
-			    g_error_matches (err, EV_DOCUMENT_ERROR, EV_DOCUMENT_ERROR_ENCRYPTED)) {
-				g_propagate_error (error, err);
-				return document;
-			    }
-			/* else fall through to slow mime code section below */
+	if (document == NULL) {
+		if (fast) {
+			/* Try again with slow mime detection */
+			g_clear_error (&err);
 		} else {
-			return document;
+			/* This should have worked, and is usually the second try,
+			 * so set an error for the caller. */
+			g_assert (err != NULL);
+			g_propagate_error (error, err);
 		}
-
-		g_object_unref (document);
-		document = NULL;
-	}
-	
-	/* Try again with slow mime detection */
-	g_clear_error (&err);
-	uri_unc = NULL;
-
-	document = get_document_from_uri (uri, FALSE, &compression, &err);
-	if (document == NULL) {
-		g_assert (err != NULL);
-		g_propagate_error (error, err);
+		
 		return NULL;
 	}
 
@@ -212,33 +172,68 @@ ev_document_factory_get_document (const char *uri, GError **error)
 					(GDestroyNotify) free_uncompressed_uri);
 	} else if (err != NULL) {
 		/* Error uncompressing file */
+		g_object_unref (document);
 		g_propagate_error (error, err);
 
-		g_object_unref (document);
 		return NULL;
 	}
-	
+
 	result = ev_document_load (document, uri_unc ? uri_unc : uri, &err);
-	if (result == FALSE) {
-		if (err == NULL) {
-			/* FIXME: this really should not happen; the backend should
-			 * always return a meaningful error.
-			 */
-			g_set_error_literal (&err,
-                                             EV_DOCUMENT_ERROR,
-                                             EV_DOCUMENT_ERROR_INVALID,
-                                             _("Unknown MIME Type"));
-		} else if (g_error_matches (err, EV_DOCUMENT_ERROR, EV_DOCUMENT_ERROR_ENCRYPTED)) {
+	if (result)
+		return document;
+		
+	if (err) {
+		if (g_error_matches (err, EV_DOCUMENT_ERROR, EV_DOCUMENT_ERROR_ENCRYPTED)) {
 			g_propagate_error (error, err);
 			return document;
-		}
-
-		g_object_unref (document);
-		document = NULL;
 
+			/* else fall through to slow mime code detection. */
+		}
+	} else if (!fast) {
+		/* FIXME: this really should not happen; the backend should
+		 * always return a meaningful error.
+		 */
+		g_set_error_literal (&err,
+			EV_DOCUMENT_ERROR,
+			EV_DOCUMENT_ERROR_INVALID,
+			_("Unknown MIME Type"));
 		g_propagate_error (error, err);
+
+		/* else fall through to slow mime code detection. */
 	}
-	
+
+	g_object_unref (document);
+
+	return NULL;
+}
+
+/**
+ * ev_document_factory_get_document:
+ * @uri: an URI
+ * @error: a #GError location to store an error, or %NULL
+ *
+ * Creates a #EvDocument for the document at @uri; or, if no backend handling
+ * the document's type is found, or an error occurred on opening the document,
+ * returns %NULL and fills in @error.
+ * If the document is encrypted, it is returned but also @error is set to
+ * %EV_DOCUMENT_ERROR_ENCRYPTED.
+ *
+ * Returns: a new #EvDocument, or %NULL.
+ */
+EvDocument *
+ev_document_factory_get_document (const char *uri, GError **error)
+{
+	EvDocument *document;
+
+	g_return_val_if_fail (uri != NULL, NULL);
+
+	document = ev_document_factory_load_uri (uri, TRUE, error);
+	if (document)
+		return document;
+
+	/* Try again with slow mime detection */
+	g_clear_error (error); /* Though this should always be NULL here. */
+	document = ev_document_factory_load_uri (uri, FALSE, error);
 	return document;
 }
 



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