asynchronous download of the article content
authorJean-Philippe Orsini <jeanfi@gmail.com>
Fri, 3 May 2013 00:16:39 +0000 (00:16 +0000)
committerJean-Philippe Orsini <jeanfi@gmail.com>
Fri, 3 May 2013 00:16:39 +0000 (00:16 +0000)
src/ttrss.c
src/ttrss_cache.c
src/ttrss_cache.h
src/ttrss_ws.c
src/ttrss_ws.h
src/ttrss_wsasync.c
src/ttrss_wsasync.h

index 2e50e08..d77007a 100644 (file)
@@ -46,7 +46,7 @@ char *ttrss_get_headline_content(struct headline *h)
        } else {
                log_debug("ttrss_get_headline_content: cache miss");
                content = ws_get_article_content(h->id);
-               cache_put(h, content);
+               cache_put(h->id, content);
        }
 
        return content;
@@ -93,6 +93,14 @@ struct headline *ttrss_get_headline(int id)
        return feeds_get_headline(data, id);
 }
 
+static void get_article_content_cbk(int id, const char *content)
+{
+       printf("get_article_content_cbk %d\n", id);
+
+       if (content)
+               cache_put(id, content);
+}
+
 void ttrs_download_headline_content(struct feed **feeds)
 {
        struct feed **fcur;
@@ -107,8 +115,10 @@ void ttrs_download_headline_content(struct feed **feeds)
                          (*fcur)->title);
 
                while (hcur && *hcur) {
-                       if (!cache_exists(*hcur))
-                               free(ttrss_get_headline_content(*hcur));
+                       if (!cache_exists(*hcur)) {
+                               ws_async_get_article_content
+                                       ((*hcur)->id, get_article_content_cbk);
+                       }
                        hcur++;
                }
        }
index d13b534..a2af0b0 100644 (file)
@@ -51,13 +51,13 @@ static const char *get_cache_dir()
        return cache_dir;
 }
 
-static gchar *content_get_path(const struct headline *h)
+static gchar *content_get_path(int id)
 {
        const char *cache_dir;
 
        cache_dir = get_cache_dir();
        if (cache_dir)
-               return g_strdup_printf("%s/%d", cache_dir, h->id);
+               return g_strdup_printf("%s/%d", cache_dir, id);
 
        return NULL;
 }
@@ -75,11 +75,11 @@ static void file_set_content(const char *path, const char *content)
        }
 }
 
-void cache_put(const struct headline *h, const char *content)
+void cache_put(int id, const char *content)
 {
        char *path;
 
-       path = content_get_path(h);
+       path = content_get_path(id);
 
        if (path) {
                pthread_mutex_lock(&lock);
@@ -96,7 +96,7 @@ int cache_exists(const struct headline *h)
        char *path;
        int result;
 
-       path = content_get_path(h);
+       path = content_get_path(h->id);
 
        pthread_mutex_lock(&lock);
        if (stat(path, &s) == -1)
@@ -115,7 +115,7 @@ char *cache_get(const struct headline *h)
        char *content, *path;
 
 
-       path = content_get_path(h);
+       path = content_get_path(h->id);
        if (path) {
                pthread_mutex_lock(&lock);
                content = file_get_content(path);
index caf4ca5..c63ad2d 100644 (file)
@@ -24,6 +24,6 @@
 
 int cache_exists(const struct headline *h);
 char *cache_get(const struct headline *h);
-void cache_put(const struct headline *h, const char *content);
+void cache_put(int id, const char *content);
 
 #endif
index 2c0da73..e9680f4 100644 (file)
@@ -259,32 +259,56 @@ int ws_open_session()
        return result;
 }
 
-char *ws_get_article_content(int id)
+struct json_object *ws_request_new_get_article_content(int id)
 {
-       struct json_object *rp, *rq, *content, *item;
-       char *str;
+       struct json_object *rq;
 
        rq = ws_request_new("getArticle");
        ws_request_add_att_int(rq, "article_id", id);
 
-       rp = ws_execute(rq);
-
-       json_object_put(rq);
+       return rq;
+}
 
-       str = NULL;
+const char *ws_reply_get_article_content(struct json_object *rp)
+{
+       struct json_object *item, *content;
 
        if (rp) {
                item = json_object_array_get_idx(rp, 0);
 
                if (item) {
                        content = json_object_object_get(item, "content");
-                       str = strdup(json_object_get_string(content));
+                       return json_object_get_string(content);
                }
+       }
 
-               json_object_put(rp);
+       return NULL;
+}
+
+char *ws_get_article_content(int id)
+{
+       struct json_object *rp, *rq;
+       const char *content;
+       char *str;
+
+       rq = ws_request_new("getArticle");
+       ws_request_add_att_int(rq, "article_id", id);
+
+       rp = ws_execute(rq);
+
+       json_object_put(rq);
+
+       if (rp) {
+               content = ws_reply_get_article_content(rp);
+
+               if (content) {
+                       str = strdup(content);
+                       json_object_put(rp);
+                       return str;
+               }
        }
 
-       return str;
+       return NULL;
 }
 
 int ws_update_headlines(struct feed *feed)
@@ -296,7 +320,6 @@ int ws_update_headlines(struct feed *feed)
 
        rq = ws_request_new("getHeadlines");
        ws_request_add_att_int(rq, "feed_id", feed->id);
-       ws_request_add_att_bool(rq, "show_excerpt", 1);
 
        rp = ws_execute(rq);
 
@@ -321,10 +344,6 @@ int ws_update_headlines(struct feed *feed)
                                h = headline_new(hid, url, title);
 
                                j = json_object_object_get(jheadline,
-                                                          "excerpt");
-                               h->excerpt = strdup(json_object_get_string(j));
-
-                               j = json_object_object_get(jheadline,
                                                           "updated");
                                h->date = json_object_get_int(j);
 
index cd09241..264bbfd 100644 (file)
@@ -33,6 +33,9 @@ struct feed **ws_update_feeds(struct feed **feeds);
 int ws_update_headlines(struct feed *feed);
 char *ws_get_article_content(int id);
 void ws_set_article_unread(int id, int unread);
+
 struct json_object *ws_request_new_set_article_unread(int id, int unread);
+struct json_object *ws_request_new_get_article_content(int id);
+const char *ws_reply_get_article_content(struct json_object *);
 
 #endif
index f3c3d84..35e42e7 100644 (file)
@@ -17,6 +17,7 @@
  * 02110-1301 USA
  */
 
+#include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
 
@@ -81,7 +82,7 @@ static void *loop()
                                        json_object_put(rp);
 
                                cur++;
-                               usleep(100000);
+                               usleep(500000);
                        }
                        free(tmp);
                }
@@ -134,3 +135,38 @@ void ws_async_set_article_unread(int id, int unread)
 
        log_debug("ws_async_set_article_unread(%d,%d) done", id, unread);
 }
+
+struct cbk_data {
+       int id;
+       void (*callback)(int id, const char *);
+};
+
+void async_article_content_cbk(struct json_object *rp, void *userdata)
+{
+       struct cbk_data *data;
+
+       data = userdata;
+
+       log_debug("async_article_content_cbk %d\n", data->id);
+
+       if (data->callback)
+               data->callback(data->id, ws_reply_get_article_content(rp));
+
+       free(data);
+}
+
+
+void
+ws_async_get_article_content(int id, void (*callback)(int id, const char *))
+{
+       struct json_object *rq;
+       struct cbk_data *data;
+
+       rq = ws_request_new_get_article_content(id);
+
+       data = malloc(sizeof(struct cbk_data));
+       data->id = id;
+       data->callback = callback;
+
+       add_async_request(rq, async_article_content_cbk, data);
+}
index 40835c7..ef6604e 100644 (file)
@@ -23,5 +23,7 @@
 #include "ttrss_ws.h"
 
 void ws_async_set_article_unread(int id, int unread);
+void
+ws_async_get_article_content(int id, void (*callback)(int id, const char *));
 
 #endif