asynchronous download of the article content
[prss.git] / src / ttrss.c
index ae4a1c9..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 "ttrss.h"
+#include "io.h"
+#include "log.h"
+#include "ttrss_cache.h"
+#include "ttrss_wsasync.h"
 #include "url.h"
 
-static char *session_id;
-static char *session_url;
-static char *session_user;
-static char *session_pwd;
-
-void ws_request_add_att_str(json_object *rq, const char *k, const char *str)
-{
-       json_object_object_add(rq, k, json_object_new_string(str));
-}
-
-void ws_request_add_att_int(json_object *rq, const char *k, int v)
-{
-       json_object_object_add(rq, k, json_object_new_int(v));
-}
-
-struct json_object *ws_request_new(const char *op)
-{
-       struct json_object *rq;
-
-       rq = json_object_new_object();
-
-       ws_request_add_att_str(rq, "op", op);
-
-       if (session_id)
-               ws_request_add_att_str(rq, "sid", session_id);
-
-       return rq;
-}
-
-void ws_init(const char *url, const char *user, const char *pwd)
-{
-       char *tmp;
-
-       if (session_id)
-               session_id = NULL;
-
-       if (session_user)
-               free(session_user);
-       session_user = strdup(user);
-
-       if (session_pwd)
-               free(session_pwd);
-       session_pwd = strdup(pwd);
-
-       if (session_url)
-               free(session_url);
-
-       tmp = url_normalize(url);
-       session_url = malloc(strlen(tmp) + strlen("/api/") + 1);
-       strcpy(session_url, tmp);
-       strcat(session_url, "/api/");
-       free(tmp);
-}
-
-struct json_object *ws_reply_get_content(struct json_object *rp)
+static struct feed **data;
+char *ttrss_get_headline_content(struct headline *h)
 {
-       return json_object_object_get(rp, "content");
-}
+       char *content;
 
-struct json_object *ws_execute(struct json_object *rq)
-{
-       struct json_object *rp, *content;
-
-       rp = http_json_get(session_url, rq);
-
-       if (rp) {
-               content = ws_reply_get_content(rp);
-
-               if (content && !json_object_object_get(rp, "error"))
-                       return content;
+       content = cache_get(h);
 
-               json_object_put(rp);
+       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;
 }
 
-int ws_get_api_version()
+struct feed **ttrss_get_feeds()
 {
-       struct json_object *rp, *rq, *j;
-       int v;
+       data = ws_update_feeds(data);
 
-       rq = ws_request_new("getApiLevel");
-
-       rp = ws_execute(rq);
-
-       json_object_put(rq);
-
-       if (rp) {
-               j = json_object_object_get(rp, "level");
-
-               if (j)
-                       v = json_object_get_int(j);
-               else
-                       v = 0;
-
-               json_object_put(rp);
-       } else {
-               v = 0;
-       }
-
-       return v;
+       return data;
 }
 
-char *ws_login()
+struct headline **ttrss_feed_get_headlines(struct feed *f)
 {
-       struct json_object *rq, *rp, *j;
-       char *str;
-
-       rq = ws_request_new("login");
-       ws_request_add_att_str(rq, "user", session_user);
-       ws_request_add_att_str(rq, "password", session_pwd);
-
-       rp = ws_execute(rq);
-       json_object_put(rq);
-
-       if (rp) {
-               j = json_object_object_get(rp, "session_id");
-
-               if (j)
-                       str = strdup(json_object_get_string(j));
-               else
-                       str = NULL;
-
-               json_object_put(rp);
-       } else {
-               str = NULL;
-       }
+       if (!f->headlines)
+               ws_update_headlines(f);
 
-       return str;
+       return f->headlines;
 }
 
-int ws_open_session()
+void ttrss_set_article_unread(int id, int unread)
 {
-       int version, result;
-
-       if (session_id)
-               free(session_id);
+       log_debug("ttrss_set_article_unread(%d,%d)", id, unread);
 
-       session_id = ws_login();
-
-       if (session_id) {
-               version = ws_get_api_version();
-               printf("API version: %d\n", version);
-
-               if (version > 0) {
-                       result = 1;
-               } else {
-                       free(session_id);
-                       session_id = NULL;
-                       result = 0;
-               }
-       } else {
-               result =  0;
-       }
+       ws_async_set_article_unread(id, unread);
 
-       return result;
+       log_debug("ttrss_set_article_unread(%d,%d)", id, unread);
 }
 
-const char *ttrss_get_headline_content(struct headline *h)
+void ttrss_set_config(const char *url, const char *user, const char *pwd)
 {
-       struct json_object *rp, *rq, *content, *array, *item;
-
-       printf("get_headlines %d\n", h->id);
-
-       if (!h->content) {
-               rq = ws_request_new("getArticle");
-               json_object_object_add(rq, "article_id",
-                                      json_object_new_int(h->id));
-               
-               rp = http_json_get(session_url, rq);
-               
-               json_object_put(rq);
-               
-               array = json_object_object_get(rp, "content");
-
-               if (!array)
-                       goto release;
-
-               item = json_object_array_get_idx(array, 0);
-
-               if (!item)
-                       goto release;
-
-               content = json_object_object_get(item, "content");
-
-               h->content = strdup(json_object_get_string(content));
-                       
-       release:
-               json_object_put(rp);
-       }
-       return h->content;
+       feeds_free(data);
+       data = NULL;
+       ws_set_config(url, user, pwd);
 }
 
-static struct headline **get_headlines(int feed_id)
+struct feed *ttrss_get_feed(int id)
 {
-       struct json_object *rp, *rq, *content, *jheadline, *j;
-       int i, n;
-       struct headline **headlines, *h;
-
-       printf("get_headlines %d\n", feed_id);
-
-       rq = ws_request_new("getHeadlines");
-       json_object_object_add(rq, "feed_id", json_object_new_int(feed_id));
-
-       rp = http_json_get(session_url, rq);
-
-       json_object_put(rq);
-
-       content = json_object_object_get(rp, "content");
-
-       if (content) {
-               n = json_object_array_length(content);
-               headlines = malloc((n+1)*sizeof(struct headline *));
-               for (i = 0; i < n; i++) {
-                       jheadline = json_object_array_get_idx(content, i);
-
-                       h = malloc(sizeof(struct headline));
-
-                       j = json_object_object_get(jheadline, "id");
-                       h->id = json_object_get_int(j);
-
-                       j = json_object_object_get(jheadline, "title");
-                       h->title = strdup(json_object_get_string(j));
-
-                       j = json_object_object_get(jheadline, "link");
-                       h->url = strdup(json_object_get_string(j));
-
-                       h->excerpt = NULL;
-                       h->content = NULL;
-
-                       j = json_object_object_get(jheadline, "unread");
-                       h->unread = json_object_get_boolean(j);
-
-                       headlines[i] = h;
-               }
-               headlines[n] = NULL;
-       } else {
-               headlines = NULL;
-       }
-
-       json_object_put(rp);
-
-       printf("get_headlines %d end\n", feed_id);
-
-       return headlines;
+       return feeds_get_feed(data, id);
 }
 
-static int feeds_length(struct feed **list)
+struct headline *ttrss_get_headline(int id)
 {
-       int n;
-
-       if (!list)
-               return 0;
-       
-       n = 0;
-       while(*list) {
-               n++;
-               list++;
-       }
-
-       return n;
+       return feeds_get_headline(data, id);
 }
 
-static struct feed **feeds_add(struct feed **feeds, struct feed *feed)
+static void get_article_content_cbk(int id, const char *content)
 {
-       int n;
-       struct feed **result;
-
-       n = feeds_length(feeds);
-
-       result = malloc((n + 1 + 1) * sizeof(struct feed *));
-
-       if (feeds)
-               memcpy(result, feeds, n * sizeof(struct feed *));
+       printf("get_article_content_cbk %d\n", id);
 
-       result[n] = feed;
-       result[n + 1] = NULL;
-
-       return result;
+       if (content)
+               cache_put(id, content);
 }
 
-struct feed **ttrss_get_feeds()
+void ttrs_download_headline_content(struct feed **feeds)
 {
-       struct json_object *rp, *rq, *content, *jfeed, *j;
-       int i, n;
-       struct feed **feeds, *feed, **tmp;
-
-       printf("ttrss_get_feeds\n");
-
-       rq = ws_request_new("getFeeds");
-
-       rp = http_json_get(session_url, rq);
-       json_object_put(rq);
-
-       content = json_object_object_get(rp, "content");
-
-       if (content) {
-               n = json_object_array_length(content);
+       struct feed **fcur;
+       struct headline **hcur;
 
-               feeds = NULL;
-               for (i = 0; i < n; i++) {
-                       jfeed = json_object_array_get_idx(content, i);
+       log_debug("ttrs_download_headline_content");
 
-                       feed = malloc(sizeof(struct feed));
+       for (fcur = feeds; *fcur; fcur++) {
+               hcur = ttrss_feed_get_headlines(*fcur);
 
-                       j = json_object_object_get(jfeed, "title");
-                       feed->title = strdup(json_object_get_string(j));
+               log_debug("ttrs_download_headline_content(): %s",
+                         (*fcur)->title);
 
-                       j = json_object_object_get(jfeed, "feed_url");
-                       feed->url = strdup(json_object_get_string(j));
-
-                       j = json_object_object_get(jfeed, "id");
-                       feed->id = json_object_get_int(j);
-
-                       j = json_object_object_get(jfeed, "unread");
-                       feed->unread = json_object_get_int(j);
-
-                       feed->headlines = NULL;
-
-                       tmp = feeds_add(feeds, feed);
-                       free(feeds);
-                       feeds = tmp;
+               while (hcur && *hcur) {
+                       if (!cache_exists(*hcur)) {
+                               ws_async_get_article_content
+                                       ((*hcur)->id, get_article_content_cbk);
+                       }
+                       hcur++;
                }
-       } else {
-               feeds = NULL;
        }
-
-       json_object_put(rp);
-
-       printf("ttrss_get_feeds ended\n");
-
-       return feeds;
-}
-
-struct headline **ttrss_get_headlines(struct feed *f)
-{
-       if (!f->headlines)
-               f->headlines = get_headlines(f->id);
-
-       return f->headlines;
-}
-
-void ttrss_set_article_unread(int id, int unread)
-{
-       struct json_object *rp, *rq;
-
-       printf("ttrss_set_article_unread %d %d\n", 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 = http_json_get(session_url, rq);
-       
-       json_object_put(rq);
-       json_object_put(rp);
 }