asynchronous download of the article content
[prss.git] / src / ttrss.c
index 6e32cc0..d77007a 100644 (file)
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
 
 #include <glib.h>
 #include <json/json.h>
 
 #include "http.h"
 #include "io.h"
-#include "ttrss_ws.h"
+#include "log.h"
+#include "ttrss_cache.h"
+#include "ttrss_wsasync.h"
 #include "url.h"
 
 static struct feed **data;
-static char *cache_dir;
-
-static const char *get_cache_dir()
-{
-       char *home;
-
-       if (!cache_dir) {
-               home = getenv("HOME");
-
-               if (!home)
-                       return NULL;
-
-               cache_dir = path_append(home, ".prss/cache");
-               mkdirs(cache_dir, 0777);
-       }
-
-       return cache_dir;
-}
-
-static void file_set_content(const char *path, const char *content)
-{
-       FILE *fp;
-
-       fp = fopen(path, "w");
-       if (fp) {
-               fwrite(content, 1, strlen(content), fp);
-               fclose(fp);
-       }
-}
-
 char *ttrss_get_headline_content(struct headline *h)
 {
-       const char *cache_dir;
-       char *path, *content;
-
-       cache_dir = get_cache_dir();
-       if (cache_dir) {
-               path = g_strdup_printf("%s/%d", cache_dir, h->id);
-               
-               content = file_get_content(path);
-               
-               if (!content) {
-                       content = ws_get_article_content(h->id);
-                       file_set_content(path, content);
-               }
+       char *content;
 
-               g_free(path);
+       content = cache_get(h);
 
-               return content;
-       } 
+       if (content) {
+               log_debug("ttrss_get_headline_content: cache hit");
+       } else {
+               log_debug("ttrss_get_headline_content: cache miss");
+               content = ws_get_article_content(h->id);
+               cache_put(h->id, content);
+       }
 
-       return NULL;
+       return content;
 }
 
 struct feed **ttrss_get_feeds()
@@ -101,26 +69,18 @@ struct headline **ttrss_feed_get_headlines(struct feed *f)
 
 void ttrss_set_article_unread(int id, int unread)
 {
-       struct json_object *rp, *rq;
+       log_debug("ttrss_set_article_unread(%d,%d)", id, unread);
 
-       printf("ttrss_set_article_unread %d %d\n", id, unread);
+       ws_async_set_article_unread(id, unread);
 
-       rq = ws_request_new("updateArticle");
-       json_object_object_add(rq, "article_ids", json_object_new_int(id));
-       json_object_object_add(rq, "field", json_object_new_int(2));
-       json_object_object_add(rq, "mode", json_object_new_int(unread));
-
-       rp = ws_execute(rq);
-
-       json_object_put(rq);
-       json_object_put(rp);
+       log_debug("ttrss_set_article_unread(%d,%d)", id, unread);
 }
 
 void ttrss_set_config(const char *url, const char *user, const char *pwd)
 {
        feeds_free(data);
        data = NULL;
-       ws_init(url, user, pwd);
+       ws_set_config(url, user, pwd);
 }
 
 struct feed *ttrss_get_feed(int id)
@@ -132,3 +92,34 @@ 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;
+       struct headline **hcur;
+
+       log_debug("ttrs_download_headline_content");
+
+       for (fcur = feeds; *fcur; fcur++) {
+               hcur = ttrss_feed_get_headlines(*fcur);
+
+               log_debug("ttrs_download_headline_content(): %s",
+                         (*fcur)->title);
+
+               while (hcur && *hcur) {
+                       if (!cache_exists(*hcur)) {
+                               ws_async_get_article_content
+                                       ((*hcur)->id, get_article_content_cbk);
+                       }
+                       hcur++;
+               }
+       }
+}