(no commit message)
[prss.git] / src / ttrss.c
index 2c57b4e..db57397 100644 (file)
  */
 
 #include <stdio.h>
+#include <stlid.h>
 #include <string.h>
 
+#include <glib.h>
 #include <json/json.h>
 
-#include "phttp.h"
-#include "ttrss.h"
+#include "http.h"
+#include "io.h"
+#include "ttrss_ws.h"
 #include "url.h"
 
-static char *session_id;
-static char *session_url;
+static struct feed **data;
+static char *cache_dir;
 
-static struct json_object *create_op(const char *op)
+static const char *get_cache_dir()
 {
-       struct json_object *j;
+       char *home;
 
-       j = json_object_new_object();
-       json_object_object_add(j, "op", json_object_new_string(op));
+       if (!cache_dir) {
+               home = getenv("HOME");
 
-       if (session_id && strcmp(op, "login"))
-               json_object_object_add(j,
-                                      "sid",
-                                      json_object_new_string(session_id));
+               if (!home)
+                       return NULL;
 
-       return j;
-}
-
-void ttrss_login(const char *url, const char *user, const char *password)
-{
-       struct json_object *content, *rp, *error, *sid, *rq;
-       char *tmp;
-
-       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);
-
-
-       rq = create_op("login");
-       json_object_object_add(rq, "user", json_object_new_string(user));
-       json_object_object_add(rq,
-                              "password",
-                              json_object_new_string(password));
-
-       rp = post_json_object(session_url, rq);
-       json_object_put(rq);
-
-       content = json_object_object_get(rp, "content");
-       if (!content) {
-               fprintf(stderr, "Login failed: no content\n");
-               return ;
+               cache_dir = path_append(home, ".prss/cache");
+               mkdirs(cache_dir, 0777);
        }
 
-       error = json_object_object_get(content, "error");
-       if (error) {
-               fprintf(stderr, "Login failed\n");
-               return ;
-       }
-
-       sid = json_object_object_get(content, "session_id");
-
-       if (session_id) {
-               free(session_id);
-               session_id = NULL;
-       }
-
-       session_id = strdup(json_object_get_string(sid));
-
-       printf("Session id: %s\n", session_id);
-
-       json_object_put(rp);
+       return cache_dir;
 }
 
-const char *ttrss_get_headline_content(struct headline *h)
+static void file_set_content(const char *path, const char *content)
 {
-       struct json_object *rp, *rq, *content, *array, *item;
+       FILE *fp;
 
-       printf("get_headlines %d\n", h->id);
-
-       if (!h->content) {
-               rq = create_op("getArticle");
-               json_object_object_add(rq, "article_id",
-                                      json_object_new_int(h->id));
-               
-               rp = post_json_object(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);
+       fp = fopen(path, "w");
+       if (fp) {
+               fwrite(content, 1, strlen(content), fp);
+               fclose(fp);
        }
-       return h->content;
 }
 
-static struct headline **get_headlines(int feed_id)
+const char *ttrss_get_headline_content(struct headline *h)
 {
-       struct json_object *rp, *rq, *content, *jheadline, *j;
-       int i, n;
-       struct headline **headlines, *h;
-
-       printf("get_headlines %d\n", feed_id);
-
-       rq = create_op("getHeadlines");
-       json_object_object_add(rq, "feed_id", json_object_new_int(feed_id));
-
-       rp = post_json_object(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);
+       const char *cache_dir;
+       char *path, *content;
 
-                       j = json_object_object_get(jheadline, "title");
-                       h->title = strdup(json_object_get_string(j));
+       if (!h->content) {
+               cache_dir = get_cache_dir();
+               if (cache_dir) {
+                       path = g_strdup_printf("%s/%d", cache_dir, h->id);
 
-                       j = json_object_object_get(jheadline, "link");
-                       h->url = strdup(json_object_get_string(j));
+                       content = file_get_content(path);
 
-                       h->excerpt = NULL;
-                       h->content = NULL;
+                       if (content) {
+                               h->content = content;
+                       } else {
+                               h->content = ws_get_article_content(h->id);
 
-                       j = json_object_object_get(jheadline, "unread");
-                       h->unread = json_object_get_boolean(j);
+                               if (h->content)
+                                       file_set_content(path, h->content);
+                       }
 
-                       headlines[i] = h;
+                       g_free(path);
                }
-               headlines[n] = NULL;
-       } else {
-               headlines = NULL;
        }
 
-       json_object_put(rp);
-
-       printf("get_headlines %d end\n", feed_id);
-
-       return headlines;
+       return h->content;
 }
 
-static int feeds_length(struct feed **list)
+struct feed **ttrss_get_feeds()
 {
-       int n;
-
-       if (!list)
-               return 0;
-       
-       n = 0;
-       while(*list) {
-               n++;
-               list++;
-       }
+       data = ws_update_feeds(data);
 
-       return n;
+       return data;
 }
 
-static struct feed **feeds_add(struct feed **feeds, struct feed *feed)
+struct headline **ttrss_feed_get_headlines(struct feed *f)
 {
-       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 *));
-
-       result[n] = feed;
-       result[n + 1] = NULL;
+       if (!f->headlines)
+               ws_update_headlines(f);
 
-       return result;
+       return f->headlines;
 }
 
-struct feed **ttrss_get_feeds()
+void ttrss_set_article_unread(int id, int unread)
 {
-       struct json_object *rp, *rq, *content, *jfeed, *j;
-       int i, n;
-       struct feed **feeds, *feed, **tmp;
-
-       printf("ttrss_get_feeds\n");
-
-       rq = create_op("getFeeds");
-
-       rp = post_json_object(session_url, rq);
-       json_object_put(rq);
-
-       content = json_object_object_get(rp, "content");
-
-       if (content) {
-               n = json_object_array_length(content);
-
-               feeds = NULL;
-               for (i = 0; i < n; i++) {
-                       jfeed = json_object_array_get_idx(content, i);
-
-                       feed = malloc(sizeof(struct feed));
+       struct json_object *rp, *rq;
 
-                       j = json_object_object_get(jfeed, "title");
-                       feed->title = strdup(json_object_get_string(j));
+       printf("ttrss_set_article_unread %d %d\n", id, unread);
 
-                       j = json_object_object_get(jfeed, "feed_url");
-                       feed->url = strdup(json_object_get_string(j));
+       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));
 
-                       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;
-               }
-       } else {
-               feeds = NULL;
-       }
+       rp = ws_execute(rq);
 
+       json_object_put(rq);
        json_object_put(rp);
+}
 
-       printf("ttrss_get_feeds ended\n");
-
-       return feeds;
+void ttrss_set_config(const char *url, const char *user, const char *pwd)
+{
+       feeds_free(data);
+       data = NULL;
+       ws_init(url, user, pwd);
 }
 
-struct headline **ttrss_get_headlines(struct feed *f)
+struct feed *ttrss_get_feed(int id)
 {
-       if (!f->headlines)
-               f->headlines = get_headlines(f->id);
+       return feeds_get_feed(data, id);
+}
 
-       return f->headlines;
+struct headline *ttrss_get_headline(int id)
+{
+       return feeds_get_headline(data, id);
 }