fixed code style
[prss.git] / src / ttrss_ws.c
index 267b129..e9680f4 100644 (file)
  * 02110-1301 USA
  */
 
+#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <stdio.h>
+
+#include <pthread.h>
 
 #include <json/json.h>
 
@@ -33,6 +35,9 @@ static char *session_url;
 static char *session_user;
 static char *session_pwd;
 
+static pthread_mutex_t lock;
+static struct http_session *session;
+
 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));
@@ -43,6 +48,11 @@ 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));
 }
 
+void ws_request_add_att_bool(json_object *rq, const char *k, int v)
+{
+       json_object_object_add(rq, k, json_object_new_boolean(v));
+}
+
 struct json_object *ws_request_new(const char *op)
 {
        struct json_object *rq;
@@ -57,7 +67,7 @@ struct json_object *ws_request_new(const char *op)
        return rq;
 }
 
-void ws_init(const char *url, const char *user, const char *pwd)
+void ws_set_config(const char *url, const char *user, const char *pwd)
 {
        char *tmp;
 
@@ -85,15 +95,34 @@ void ws_init(const char *url, const char *user, const char *pwd)
 
 struct json_object *ws_reply_get_content(struct json_object *rp)
 {
+       log_debug("ws_reply_get_content");
        return json_object_object_get(rp, "content");
 }
 
-static struct json_object *execute(struct json_object *rq, char **err)
+static const char *ws_reply_get_error(struct json_object *content)
 {
-       struct json_object *rp, *content, *jerror;
+       struct json_object *jerror;
+
+       log_debug("ws_reply_get_error");
+
+       if (json_object_get_type(content) != json_type_object)
+               return NULL;
+
+       jerror = json_object_object_get(content, "error");
+
+       if (!jerror)
+               return NULL;
+
+       return json_object_get_string(jerror);
+}
+
+static struct json_object *
+execute(struct http_session *sess, struct json_object *rq, char **err)
+{
+       struct json_object *rp, *content;
        const char *str;
 
-       rp = http_json_get(session_url, rq);
+       rp = http_json_get(sess, session_url, rq);
 
        content = NULL;
 
@@ -101,13 +130,14 @@ static struct json_object *execute(struct json_object *rq, char **err)
                content = ws_reply_get_content(rp);
 
                if (content) {
-                       jerror = json_object_object_get(content, "error");
-                       if (jerror) {
-                               if (err) {
-                                       str = json_object_get_string(jerror);
-                                       *err = strdup(str);
-                               }
+                       str = ws_reply_get_error(content);
+
+                       if (str) {
+                               log_debug("execute() err=%s", str);
                                content = NULL;
+
+                               if (err)
+                                       *err = strdup(str);
                        } else {
                                json_object_get(content);
                        }
@@ -116,6 +146,8 @@ static struct json_object *execute(struct json_object *rq, char **err)
                json_object_put(rp);
        }
 
+       log_debug("execute() done");
+
        return content;
 }
 
@@ -125,21 +157,28 @@ struct json_object *ws_execute(struct json_object *rq)
        struct json_object *result;
 
        log_debug("ws_execute()");
+       pthread_mutex_lock(&lock);
+       log_debug("ws_execute() lock");
 
        err = NULL;
-       result = execute(rq, &err);
+       result = execute(session, rq, &err);
 
        if (err) {
-               log_debug("ws_execute(): error=%s\n", err);
+               log_debug("ws_execute(): error=%s", err);
 
                if (!strcmp(err, "NOT_LOGGED_IN")) {
                        ws_open_session();
-                       result = execute(rq, NULL);
+                       result = execute(session, rq, NULL);
                }
 
                free(err);
        }
 
+       log_debug("ws_execute() unlock");
+       pthread_mutex_unlock(&lock);
+
+       log_debug("ws_execute()");
+
        return result;
 }
 
@@ -220,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)
@@ -280,6 +343,10 @@ int ws_update_headlines(struct feed *feed)
 
                                h = headline_new(hid, url, title);
 
+                               j = json_object_object_get(jheadline,
+                                                          "updated");
+                               h->date = json_object_get_int(j);
+
                                tmp = headlines_add(feed->headlines, h);
                                if (feed->headlines)
                                        free(feed->headlines);
@@ -289,6 +356,12 @@ int ws_update_headlines(struct feed *feed)
                        j = json_object_object_get(jheadline, "unread");
                        h->unread = json_object_get_boolean(j);
                }
+
+               if (!feed->headlines) {
+                       feed->headlines = malloc(sizeof(struct headline *));
+                       *(feed->headlines) = NULL;
+               }
+
                json_object_put(rp);
                return 1;
        } else {
@@ -296,25 +369,19 @@ int ws_update_headlines(struct feed *feed)
        }
 }
 
-struct feed **ws_update_feeds(struct feed **feeds)
+static struct feed **
+feeds_update(struct feed **feeds, struct json_object *jarray)
 {
-       struct json_object *rp, *rq, *jfeed, *j;
        int i, n, id;
+       struct json_object *jfeed, *j;
+       const char *url, *title;
        struct feed *feed, **tmp;
-       const char *title, *url;
 
-       log_debug("ws_update_feeds()");
-
-       rq = ws_request_new("getFeeds");
-
-       rp = ws_execute(rq);
-       json_object_put(rq);
-
-       if (rp) {
-               n = json_object_array_length(rp);
+       if (jarray) {
+               n = json_object_array_length(jarray);
 
                for (i = 0; i < n; i++) {
-                       jfeed = json_object_array_get_idx(rp, i);
+                       jfeed = json_object_array_get_idx(jarray, i);
 
                        j = json_object_object_get(jfeed, "id");
                        id = json_object_get_int(j);
@@ -337,11 +404,37 @@ struct feed **ws_update_feeds(struct feed **feeds)
 
                        j = json_object_object_get(jfeed, "unread");
                        feed->unread = json_object_get_int(j);
+
+                       ws_update_headlines(feed);
                }
+       }
+
+       return feeds;
+}
+
+struct feed **ws_update_feeds(struct feed **feeds)
+{
+       struct json_object *rp, *rq;
+
+       log_debug("ws_update_feeds()");
+
+       rq = ws_request_new("getFeeds");
+       ws_request_add_att_int(rq, "cat_id", 0);
+
+       rp = ws_execute(rq);
+
+       if (rp) {
+               feeds = feeds_update(feeds, rp);
+               json_object_put(rp);
+       }
+
+       ws_request_add_att_int(rq, "cat_id", -3);
+       rp = ws_execute(rq);
+       json_object_put(rq);
+
+       if (rp) {
+               feeds = feeds_update(feeds, rp);
                json_object_put(rp);
-       } else {
-               feeds_free(feeds);
-               feeds = NULL;
        }
 
        log_debug("ws_update_feeds() done");
@@ -378,3 +471,12 @@ void ws_set_article_unread(int id, int unread)
 
        log_debug("ws_set_article_unread(%d,%d) done", id, unread);
 }
+
+void ws_init()
+{
+       pthread_mutexattr_t attr;
+       pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
+       pthread_mutex_init(&lock, &attr);
+
+       session = http_session_new();
+}