[libgrss] added grss_feed_channel_fetch_cancel()



commit d258c1ff3e0f5606a6e8a895c6acf85c9143e6f8
Author: Roberto Guido <rguido src gnome org>
Date:   Wed Jan 14 02:12:54 2015 +0100

    added grss_feed_channel_fetch_cancel()

 src/feed-channel.c    |   43 +++++++++++++++++++++++++++++++++++++++++++
 src/feed-channel.h    |    1 +
 src/feeds-pool.c      |   16 ++++++++++++++++
 src/feeds-publisher.c |    2 ++
 4 files changed, 62 insertions(+), 0 deletions(-)
---
diff --git a/src/feed-channel.c b/src/feed-channel.c
index 9789564..dd8d8e0 100644
--- a/src/feed-channel.c
+++ b/src/feed-channel.c
@@ -66,6 +66,8 @@ struct _GrssFeedChannelPrivate {
        time_t          pub_time;
        time_t          update_time;
        int             update_interval;
+
+       GCancellable    *fetchcancel;
 };
 
 enum {
@@ -1047,6 +1049,7 @@ feed_downloaded (SoupSession *session, SoupMessage *msg, gpointer user_data) {
        }
 
        g_simple_async_result_complete_in_idle (result);
+       g_object_unref (channel->priv->fetchcancel);
        g_object_unref (result);
 }
 
@@ -1072,6 +1075,13 @@ grss_feed_channel_fetch_finish (GrssFeedChannel *channel, GAsyncResult *res, GEr
                return TRUE;
 }
 
+static void
+do_prefetch (GrssFeedChannel *channel)
+{
+       grss_feed_channel_fetch_cancel (channel);
+       channel->priv->fetchcancel = g_cancellable_new ();
+}
+
 /**
  * grss_feed_channel_fetch_async:
  * @channel: a #GrssFeedChannel.
@@ -1087,7 +1097,13 @@ grss_feed_channel_fetch_async (GrssFeedChannel *channel, GAsyncReadyCallback cal
        SoupMessage *msg;
        SoupSession *session;
 
+       /*
+               TODO: if the source is not valid, call anyway the callback with an error
+       */
+
+       do_prefetch (channel);
        result = g_simple_async_result_new (G_OBJECT (channel), callback, user_data, 
grss_feed_channel_fetch_async);
+       g_simple_async_result_set_check_cancellable (result, channel->priv->fetchcancel);
 
        session = soup_session_async_new ();
        init_soup_session (session, channel);
@@ -1183,6 +1199,7 @@ feed_downloaded_return_items (SoupSession *session, SoupMessage *msg, gpointer u
        }
 
        g_simple_async_result_complete_in_idle (result);
+       g_object_unref (channel->priv->fetchcancel);
        g_object_unref (result);
 }
 
@@ -1201,7 +1218,9 @@ grss_feed_channel_fetch_all_async (GrssFeedChannel *channel, GAsyncReadyCallback
        SoupMessage *msg;
        SoupSession *session;
 
+       do_prefetch (channel);
        result = g_simple_async_result_new (G_OBJECT (channel), callback, user_data, 
grss_feed_channel_fetch_async);
+       g_simple_async_result_set_check_cancellable (result, channel->priv->fetchcancel);
 
        session = soup_session_async_new ();
        init_soup_session (session, channel);
@@ -1235,3 +1254,27 @@ grss_feed_channel_fetch_all_finish (GrssFeedChannel *channel, GAsyncResult *res,
        else
                return (GList*) g_simple_async_result_get_op_res_gpointer (G_SIMPLE_ASYNC_RESULT (res));
 }
+
+/**
+ * grss_feed_channel_fetch_cancel:
+ * @channel: a #GrssFeedChannel.
+ *
+ * If a fetch operation was scheduled with grss_feed_channel_fetch_async() or
+ * grss_feed_channel_fetch_all_async(), cancel it.
+ * 
+ * Return value: %TRUE if a fetch was scheduled (and now cancelled), %FALSE if
+ * this function had nothing to do
+ */
+gboolean
+grss_feed_channel_fetch_cancel (GrssFeedChannel *channel)
+{
+       if (channel->priv->fetchcancel != NULL) {
+               g_cancellable_cancel (channel->priv->fetchcancel);
+               g_object_unref (channel->priv->fetchcancel);
+               return TRUE;
+       }
+       else {
+               return FALSE;
+       }
+}
+
diff --git a/src/feed-channel.h b/src/feed-channel.h
index d609888..3b8dd38 100644
--- a/src/feed-channel.h
+++ b/src/feed-channel.h
@@ -101,5 +101,6 @@ gboolean            grss_feed_channel_fetch_finish          (GrssFeedChannel *channel, 
GAsyncResul
 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);
+gboolean               grss_feed_channel_fetch_cancel          (GrssFeedChannel *channel);
 
 #endif /* __FEED_CHANNEL_H__ */
diff --git a/src/feeds-pool.c b/src/feeds-pool.c
index e15bf82..a6b76a5 100644
--- a/src/feeds-pool.c
+++ b/src/feeds-pool.c
@@ -61,12 +61,27 @@ static guint signals [LAST_SIGNAL] = {0};
 G_DEFINE_TYPE (GrssFeedsPool, grss_feeds_pool, G_TYPE_OBJECT);
 
 static void
+cancel_all_pending (GrssFeedsPool *pool)
+{
+       GList *iter;
+       GrssFeedChannelWrap *wrap;
+
+       if (pool->priv->feeds_list != NULL) {
+               for (iter = pool->priv->feeds_list; iter; iter = g_list_next (iter)) {
+                       wrap = (GrssFeedChannelWrap*) iter->data;
+                       grss_feed_channel_fetch_cancel (wrap->channel);
+               }
+       }
+}
+
+static void
 remove_currently_listened (GrssFeedsPool *pool)
 {
        GList *iter;
        GrssFeedChannelWrap *wrap;
 
        soup_session_abort (pool->priv->soupsession);
+       cancel_all_pending (pool);
 
        if (pool->priv->feeds_list != NULL) {
                for (iter = pool->priv->feeds_list; iter; iter = g_list_next (iter)) {
@@ -368,6 +383,7 @@ grss_feeds_pool_switch (GrssFeedsPool *pool, gboolean run)
                else {
                        if (pool->priv->scheduler != 0)
                                g_source_remove (pool->priv->scheduler);
+                       cancel_all_pending (pool);
                }
        }
 }
diff --git a/src/feeds-publisher.c b/src/feeds-publisher.c
index e37bdf8..2df0534 100644
--- a/src/feeds-publisher.c
+++ b/src/feeds-publisher.c
@@ -236,6 +236,8 @@ grss_feeds_publisher_new ()
  * 
  * Return value: a newly allocated string holding the formatted feed, to be
  * freed when no longer in use.
+ *
+ * Deprecated: 0.6: Use grss_feed_formatter_format() instead.
  */
 gchar*
 grss_feeds_publisher_format_content (GrssFeedsPublisher *pub, GrssFeedChannel *channel, GList *items, GError 
**error)


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