[libgrss] Improved error reporting for FeedChannel Minor corrections in documentation



commit b07ea26002bf8f2144f1ba16ae6138afbcb02feb
Author: Roberto Guido <bob4job gmail com>
Date:   Sat Mar 3 13:32:56 2012 +0100

    Improved error reporting for FeedChannel
    Minor corrections in documentation

 .gitignore               |   11 +++++
 examples/async-fetcher.c |    4 +-
 examples/subscriber.c    |    6 ++-
 src/feed-channel.c       |  112 ++++++++++++++++++++++++++++------------------
 src/feed-channel.h       |    8 ++--
 src/feed-item.c          |    4 +-
 src/feeds-group.c        |    2 +-
 src/feeds-pool.c         |    6 +--
 src/feeds-publisher.c    |    2 +-
 src/feeds-subscriber.c   |    2 +-
 src/utils.c              |    2 +-
 src/utils.h              |    2 +
 12 files changed, 100 insertions(+), 61 deletions(-)
---
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..c57b311
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,11 @@
+*.lo
+*.la
+*.make
+*.stamp
+.deps
+.libs
+Makefile
+Makefile.in
+config.*
+feed-marshal.h
+feed-marshal.c
diff --git a/examples/async-fetcher.c b/examples/async-fetcher.c
index 351422b..077cecc 100644
--- a/examples/async-fetcher.c
+++ b/examples/async-fetcher.c
@@ -45,7 +45,7 @@ print_items (GObject *source, GAsyncResult *res, gpointer useless)
 	error = NULL;
 	channel = GRSS_FEED_CHANNEL (source);
 	items = grss_feed_channel_fetch_all_finish (channel, res, &error);
-	
+
 	if (items == NULL) {
 		printf ("%s\n", error->message);
 	}
@@ -64,7 +64,7 @@ static gboolean do_work (gpointer useless)
 	i = 0;
 	g_timeout_add (100, mark_time, NULL);
 
-	feed = grss_feed_channel_new_from_source ("http://rss.slashdot.org/Slashdot/slashdot";);
+	feed = grss_feed_channel_new_with_source ("http://rss.slashdot.org/Slashdot/slashdot";);
 	grss_feed_channel_fetch_all_async (feed, print_items, NULL);
 
 	return FALSE;
diff --git a/examples/subscriber.c b/examples/subscriber.c
index 29789f9..feca3ae 100644
--- a/examples/subscriber.c
+++ b/examples/subscriber.c
@@ -48,6 +48,7 @@ main ()
 		"http://techcrunch.com/feed/";,
 		NULL
 	};
+	GError *error;
 	GList *iter;
 	GList *list;
 	GrssFeedChannel *feed;
@@ -67,11 +68,12 @@ main ()
 	for (i = 0; example_feeds [i] != NULL; i++) {
 		feed = grss_feed_channel_new ();
 		grss_feed_channel_set_source (feed, example_feeds [i]);
-		ok = grss_feed_channel_fetch (feed);
+		ok = grss_feed_channel_fetch (feed, &error);
 
 		if (ok == FALSE) {
-			g_warning ("Unable to fetch feed at %s", example_feeds [i]);
+			g_warning ("Unable to fetch feed at %s: %s", example_feeds [i], error->message);
 			g_object_unref (feed);
+			g_error_free (error);
 		}
 		else {
 			list = g_list_prepend (list, feed);
diff --git a/src/feed-channel.c b/src/feed-channel.c
index b4789a8..c370bc7 100644
--- a/src/feed-channel.c
+++ b/src/feed-channel.c
@@ -22,7 +22,8 @@
 #include "feed-channel.h"
 #include "feed-parser.h"
 
-#define FEED_CHANNEL_GET_PRIVATE(obj)     (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GRSS_FEED_CHANNEL_TYPE, GrssFeedChannelPrivate))
+#define FEED_CHANNEL_GET_PRIVATE(obj)	(G_TYPE_INSTANCE_GET_PRIVATE ((obj), GRSS_FEED_CHANNEL_TYPE, GrssFeedChannelPrivate))
+#define FEED_CHANNEL_ERROR		feed_channel_error_quark()
 
 /**
  * SECTION: feed-channel
@@ -31,8 +32,6 @@
  * #GrssFeedChannel rappresents a single feed which may be fetched and parsed
  */
 
-#define FEEDS_CHANNEL_ERROR		feeds_channel_error_quark()
-
 typedef struct {
 	gchar	*hub;
 } PubSub;
@@ -68,16 +67,17 @@ struct _GrssFeedChannelPrivate {
 };
 
 enum {
-	FEEDS_CHANNEL_FETCH_ERROR,
-	FEEDS_CHANNEL_PARSE_ERROR
+	FEED_CHANNEL_FETCH_ERROR,
+	FEED_CHANNEL_PARSE_ERROR,
+	FEED_CHANNEL_FILE_ERROR,
 };
 
 G_DEFINE_TYPE (GrssFeedChannel, grss_feed_channel, G_TYPE_OBJECT);
 
 static GQuark
-feeds_channel_error_quark ()
+feed_channel_error_quark ()
 {
-	return g_quark_from_static_string ("feeds_channel_error");
+	return g_quark_from_static_string ("feed_channel_error");
 }
 
 static void
@@ -141,7 +141,7 @@ grss_feed_channel_new ()
 }
 
 /**
- * grss_feed_channel_new_from_source:
+ * grss_feed_channel_new_with_source:
  * @source: URL of the feed
  *
  * Allocates a new #GrssFeedChannel and assign it the given remote source
@@ -149,7 +149,7 @@ grss_feed_channel_new ()
  * Return value: a #GrssFeedChannel
  */
 GrssFeedChannel*
-grss_feed_channel_new_from_source (gchar *source)
+grss_feed_channel_new_with_source (gchar *source)
 {
 	GrssFeedChannel *ret;
 
@@ -161,47 +161,58 @@ grss_feed_channel_new_from_source (gchar *source)
 /**
  * grss_feed_channel_new_from_file:
  * @path: path of the file to parse
+ * @error: if an error occourred, %NULL is returned and this is filled with the
+ *         message
  *
  * Allocates a new #GrssFeedChannel and init it with contents found in specified
  * file
  *
- * Return value: a #GrssFeedChannel, or NULL if the file in @path is not a valid
- * document
+ * Return value: a #GrssFeedChannel, or %NULL if the file in @path is not a
+ * valid document
  */
 GrssFeedChannel*
-grss_feed_channel_new_from_file (const gchar *path)
+grss_feed_channel_new_from_file (const gchar *path, GError **error)
 {
+	struct stat sbuf;
 	GList *items;
 	GList *iter;
 	xmlDocPtr doc;
 	GrssFeedParser *parser;
 	GrssFeedChannel *ret;
 
+	ret = NULL;
+
+	if (stat (path, &sbuf) == -1) {
+		g_set_error (error, FEED_CHANNEL_ERROR, FEED_CHANNEL_FILE_ERROR, "Unable to open file: %s", strerror (errno));
+		return NULL;
+	}
+
+	doc = file_to_xml (path);
+	if (doc == NULL) {
+		g_set_error (error, FEED_CHANNEL_ERROR, FEED_CHANNEL_PARSE_ERROR, "Unable to parse file");
+		return NULL;
+	}
+
+	ret = g_object_new (GRSS_FEED_CHANNEL_TYPE, NULL);
+	parser = grss_feed_parser_new ();
+
 	/*
 		TODO	This function is quite inefficent because parses all
 			the feed with a GrssFeedParser and then trash obtained
 			GrssFeedItems. Perhaps a more aimed function in
 			GrssFeedParser would help...
 	*/
+	items = grss_feed_parser_parse (parser, ret, doc, NULL);
 
-	ret = NULL;
-	doc = file_to_xml (path);
-
-	if (doc != NULL) {
-		ret = g_object_new (GRSS_FEED_CHANNEL_TYPE, NULL);
-		parser = grss_feed_parser_new ();
-		items = grss_feed_parser_parse (parser, ret, doc, NULL);
-
-		if (items != NULL) {
-			for (iter = items; iter; iter = g_list_next (iter))
-				g_object_unref (iter->data);
-			g_list_free (items);
-		}
-
-		g_object_unref (parser);
-		xmlFreeDoc (doc);
+	if (items != NULL) {
+		for (iter = items; iter; iter = g_list_next (iter))
+			g_object_unref (iter->data);
+		g_list_free (items);
 	}
 
+	g_object_unref (parser);
+	xmlFreeDoc (doc);
+
 	return ret;
 }
 
@@ -815,6 +826,8 @@ quick_and_dirty_parse (GrssFeedChannel *channel, SoupMessage *msg, GList **save_
 /**
  * grss_feed_channel_fetch:
  * @channel: a #GrssFeedChannel
+ * @error: if an error occourred, %FALSE is returned and this is filled with the
+ *         message
  *
  * Utility to fetch and populate a #GrssFeedChannel for the first time, and init
  * all his internal values. Only the source URL has to be set in @channel
@@ -825,23 +838,27 @@ quick_and_dirty_parse (GrssFeedChannel *channel, SoupMessage *msg, GList **save_
  * otherwise
  */
 gboolean
-grss_feed_channel_fetch (GrssFeedChannel *channel)
+grss_feed_channel_fetch (GrssFeedChannel *channel, GError **error)
 {
 	gboolean ret;
 	guint status;
 	SoupMessage *msg;
 	SoupSession *session;
 
+	ret = FALSE;
+
 	session = soup_session_sync_new ();
 	msg = soup_message_new ("GET", grss_feed_channel_get_source (channel));
 	status = soup_session_send_message (session, msg);
 
 	if (status >= 200 && status <= 299) {
 		ret = quick_and_dirty_parse (channel, msg, NULL);
+		if (ret == FALSE)
+			g_set_error (error, FEED_CHANNEL_ERROR, FEED_CHANNEL_PARSE_ERROR, "Unable to parse file");
 	}
 	else {
-		g_warning ("Unable to fetch feed from %s: %s", grss_feed_channel_get_source (channel), soup_status_get_phrase (status));
-		ret = FALSE;
+		g_set_error (error, FEED_CHANNEL_ERROR, FEED_CHANNEL_FETCH_ERROR,
+		             "Unable to download from %s", grss_feed_channel_get_source (channel));
 	}
 
 	g_object_unref (session);
@@ -861,11 +878,11 @@ feed_downloaded (SoupSession *session, SoupMessage *msg, gpointer user_data) {
 
 	if (status >= 200 && status <= 299) {
 		if (quick_and_dirty_parse (channel, msg, NULL) == FALSE)
-			g_simple_async_result_set_error (result, FEEDS_CHANNEL_ERROR, FEEDS_CHANNEL_PARSE_ERROR,
+			g_simple_async_result_set_error (result, FEED_CHANNEL_ERROR, FEED_CHANNEL_PARSE_ERROR,
 						 "Unable to parse feed from %s", grss_feed_channel_get_source (channel));
 	}
 	else {
-		g_simple_async_result_set_error (result, FEEDS_CHANNEL_ERROR, FEEDS_CHANNEL_FETCH_ERROR,
+		g_simple_async_result_set_error (result, FEED_CHANNEL_ERROR, FEED_CHANNEL_FETCH_ERROR,
 						 "Unable to download from %s", grss_feed_channel_get_source (channel));
 	}
 
@@ -877,11 +894,14 @@ feed_downloaded (SoupSession *session, SoupMessage *msg, gpointer user_data) {
  * grss_feed_channel_fetch_finish:
  * @channel: a #GrssFeedChannel
  * @res: the #GAsyncResult passed to the callback
- * @error: if an error occourred, FALSE is returned and this is filled with the message
+ * @error: if an error occourred, %FALSE is returned and this is filled with the
+ *         message
  *
- * Finalizes an asyncronous operation started with grss_feed_channel_fetch_async()
+ * Finalizes an asyncronous operation started with
+ * grss_feed_channel_fetch_async()
  *
- * Return value: TRUE if @channel informations have been successfully fetched, FALSE otherwise
+ * Return value: %TRUE if @channel informations have been successfully fetched,
+ * %FALSE otherwise
  */
 gboolean
 grss_feed_channel_fetch_finish (GrssFeedChannel *channel, GAsyncResult *res, GError **error)
@@ -917,6 +937,8 @@ grss_feed_channel_fetch_async (GrssFeedChannel *channel, GAsyncReadyCallback cal
 /**
  * grss_feed_channel_fetch_all:
  * @channel: a #GrssFeedChannel
+ * @error: if an error occourred, %NULL is returned and this is filled with the
+ *         message
  *
  * Utility to fetch and populate a #GrssFeedChannel, and retrieve all its
  * items
@@ -925,7 +947,7 @@ grss_feed_channel_fetch_async (GrssFeedChannel *channel, GAsyncReadyCallback cal
  * freed when no longer in use, or %NULL if an error occurs
  */
 GList*
-grss_feed_channel_fetch_all (GrssFeedChannel *channel)
+grss_feed_channel_fetch_all (GrssFeedChannel *channel, GError **error)
 {
 	guint status;
 	GList *items;
@@ -937,10 +959,14 @@ grss_feed_channel_fetch_all (GrssFeedChannel *channel)
 	status = soup_session_send_message (session, msg);
 	items = NULL;
 
-	if (status >= 200 && status <= 299)
-		quick_and_dirty_parse (channel, msg, &items);
-	else
-		g_warning ("Unable to fetch feed from %s: %s", grss_feed_channel_get_source (channel), soup_status_get_phrase (status));
+	if (status >= 200 && status <= 299) {
+		if (quick_and_dirty_parse (channel, msg, &items) == FALSE)
+			g_set_error (error, FEED_CHANNEL_ERROR, FEED_CHANNEL_PARSE_ERROR, "Unable to parse file");
+	}
+	else {
+		g_set_error (error, FEED_CHANNEL_ERROR, FEED_CHANNEL_FETCH_ERROR,
+		             "Unable to download from %s", grss_feed_channel_get_source (channel));
+	}
 
 	g_object_unref (session);
 	g_object_unref (msg);
@@ -979,11 +1005,11 @@ feed_downloaded_return_items (SoupSession *session, SoupMessage *msg, gpointer u
 		if (quick_and_dirty_parse (channel, msg, &items) == TRUE)
 			g_simple_async_result_set_op_res_gpointer (result, items, free_items_list);
 		else
-			g_simple_async_result_set_error (result, FEEDS_CHANNEL_ERROR, FEEDS_CHANNEL_PARSE_ERROR,
+			g_simple_async_result_set_error (result, FEED_CHANNEL_ERROR, FEED_CHANNEL_PARSE_ERROR,
 						 "Unable to parse feed from %s", grss_feed_channel_get_source (channel));
 	}
 	else {
-		g_simple_async_result_set_error (result, FEEDS_CHANNEL_ERROR, FEEDS_CHANNEL_FETCH_ERROR,
+		g_simple_async_result_set_error (result, FEED_CHANNEL_ERROR, FEED_CHANNEL_FETCH_ERROR,
 						 "Unable to download from %s", grss_feed_channel_get_source (channel));
 	}
 
diff --git a/src/feed-channel.h b/src/feed-channel.h
index abf6007..ec8ebfe 100644
--- a/src/feed-channel.h
+++ b/src/feed-channel.h
@@ -45,8 +45,8 @@ typedef struct {
 GType			grss_feed_channel_get_type		(void) G_GNUC_CONST;
 
 GrssFeedChannel*	grss_feed_channel_new			();
-GrssFeedChannel*	grss_feed_channel_new_from_source	(gchar *source);
-GrssFeedChannel*	grss_feed_channel_new_from_file		(const gchar *path);
+GrssFeedChannel*	grss_feed_channel_new_with_source	(gchar *source);
+GrssFeedChannel*	grss_feed_channel_new_from_file		(const gchar *path, GError **error);
 
 void			grss_feed_channel_set_format		(GrssFeedChannel *channel, gchar *format);
 const gchar*		grss_feed_channel_get_format		(GrssFeedChannel *channel);
@@ -89,10 +89,10 @@ time_t			grss_feed_channel_get_update_time	(GrssFeedChannel *channel);
 void			grss_feed_channel_set_update_interval	(GrssFeedChannel *channel, int minutes);
 int			grss_feed_channel_get_update_interval	(GrssFeedChannel *channel);
 
-gboolean		grss_feed_channel_fetch			(GrssFeedChannel *channel);
+gboolean		grss_feed_channel_fetch			(GrssFeedChannel *channel, GError **error);
 void			grss_feed_channel_fetch_async		(GrssFeedChannel *channel, GAsyncReadyCallback callback, gpointer user_data);
 gboolean		grss_feed_channel_fetch_finish		(GrssFeedChannel *channel, GAsyncResult *res, GError **error);
-GList*			grss_feed_channel_fetch_all		(GrssFeedChannel *channel);
+GList*			grss_feed_channel_fetch_all		(GrssFeedChannel *channel, GError **error);
 void			grss_feed_channel_fetch_all_async	(GrssFeedChannel *channel, GAsyncReadyCallback callback, gpointer user_data);
 GList*			grss_feed_channel_fetch_all_finish	(GrssFeedChannel *channel, GAsyncResult *res, GError **error);
 
diff --git a/src/feed-item.c b/src/feed-item.c
index 902003a..9723183 100644
--- a/src/feed-item.c
+++ b/src/feed-item.c
@@ -534,8 +534,8 @@ grss_feed_item_set_geo_point (GrssFeedItem *item, double latitude, double longit
  *
  * Retrieves the geo reference of the @item
  *
- * Return value: TRUE if @item has geographic coordinates assigned and
- * @latitude and @longitude have been set, FALSE if @item has not geo
+ * Return value: %TRUE if @item has geographic coordinates assigned and
+ * @latitude and @longitude have been set, %FALSE if @item has not geo
  * reference
  */
 gboolean
diff --git a/src/feeds-group.c b/src/feeds-group.c
index c2da403..58e89fe 100644
--- a/src/feeds-group.c
+++ b/src/feeds-group.c
@@ -225,7 +225,7 @@ grss_feeds_group_parse_file (GrssFeedsGroup *group, const gchar *path, GError **
  * @uri:
  * @error:
  *
- * Return value: FALSE
+ * Return value: %FALSE
  */
 gboolean
 grss_feeds_group_export_file (GrssFeedsGroup *group, GList *channels, const gchar *format, const gchar *uri, GError **error)
diff --git a/src/feeds-pool.c b/src/feeds-pool.c
index 190cb5a..968b56c 100644
--- a/src/feeds-pool.c
+++ b/src/feeds-pool.c
@@ -260,10 +260,8 @@ grss_feeds_pool_get_listened_num (GrssFeedsPool *pool)
 static void
 feed_downloaded (GObject *source, GAsyncResult *res, gpointer user_data)
 {
-	guint status;
 	GList *items;
 	GError *error;
-	xmlDocPtr doc;
 	GrssFeedChannelWrap *feed;
 
 	feed = (GrssFeedChannelWrap*) user_data;
@@ -343,9 +341,9 @@ run_scheduler (GrssFeedsPool *pool)
 /**
  * grss_feeds_pool_switch:
  * @pool: a #GrssFeedsPool
- * @run: TRUE to run the pool, FALSE to pause it
+ * @run: %TRUE to run the pool, %FALSE to pause it
  *
- * Permits to pause or resume the @pool fetching feeds. If @run is #TRUE, the
+ * Permits to pause or resume the @pool fetching feeds. If @run is %TRUE, the
  * @pool starts immediately
  */
 void
diff --git a/src/feeds-publisher.c b/src/feeds-publisher.c
index 718a620..0720729 100644
--- a/src/feeds-publisher.c
+++ b/src/feeds-publisher.c
@@ -970,7 +970,7 @@ remove_refresh_handler (GrssFeedsPublisher *pub)
 /**
  * grss_feeds_publisher_hub_switch:
  * @pub: a #GrssFeedsPublisher
- * @run: TRUE to run the local server, FALSE to stop it
+ * @run: %TRUE to run the local server, %FALSE to stop it
  *
  * Permits to start and stop the webserver implemented by this object
  */
diff --git a/src/feeds-subscriber.c b/src/feeds-subscriber.c
index b0cb1ec..a336a86 100644
--- a/src/feeds-subscriber.c
+++ b/src/feeds-subscriber.c
@@ -593,7 +593,7 @@ grss_feeds_subscriber_set_port (GrssFeedsSubscriber *sub, int port)
 /**
  * grss_feeds_subscriber_switch:
  * @sub: a #GrssFeedsSubscriber
- * @run: TRUE to run the subscriber, FALSE to pause it
+ * @run: %TRUE to run the subscriber, %FALSE to pause it
  *
  * Permits to pause or resume @sub listening for events
  */
diff --git a/src/utils.c b/src/utils.c
index d103467..1c13793 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -554,7 +554,7 @@ date_to_ISO8601 (time_t date)
 	struct tm broken;
 
 	localtime_r (&date, &broken);
-	strftime (text, 100, "%Y-%m-%dT%H:%M%t", &broken);
+	strftime (text, 100, "%Y-%m-%dT%H:%M", &broken);
 	return g_strdup (text);
 }
 
diff --git a/src/utils.h b/src/utils.h
index cfa270d..f71b166 100644
--- a/src/utils.h
+++ b/src/utils.h
@@ -32,6 +32,8 @@
 #include <ctype.h>
 #include <unistd.h>
 #include <errno.h>
+#include <sys/types.h>
+#include <sys/stat.h>
 
 #include "libgrss.h"
 



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