static char *session_user;
static char *session_pwd;
+static struct feed **data;
+
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));
if (rp) {
content = ws_reply_get_content(rp);
- if (content && !json_object_object_get(rp, "error"))
+ if (content && !json_object_object_get(rp, "error")) {
+ json_object_get(content);
+ json_object_put(rp);
return content;
+ }
json_object_put(rp);
}
int ws_get_api_version()
{
- struct json_object *rp, *rq, *j;
+ struct json_object *rp, *rq;
int v;
rq = ws_request_new("getApiLevel");
json_object_put(rq);
if (rp) {
- j = json_object_object_get(rp, "level");
-
- if (j)
- v = json_object_get_int(j);
- else
- v = 0;
+ v = json_object_get_int(json_object_object_get(rp, "level"));
json_object_put(rp);
} else {
if (rp) {
j = json_object_object_get(rp, "session_id");
-
- if (j)
- str = strdup(json_object_get_string(j));
- else
- str = NULL;
+ str = strdup(json_object_get_string(j));
json_object_put(rp);
} else {
return result;
}
-const char *ttrss_get_headline_content(struct headline *h)
+char *ws_get_article_content(int id)
{
- struct json_object *rp, *rq, *content, *array, *item;
+ struct json_object *rp, *rq, *content, *item;
+ char *str;
- printf("get_headlines %d\n", h->id);
+ rq = ws_request_new("getArticle");
+ ws_request_add_att_int(rq, "article_id", 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");
+ rp = ws_execute(rq);
- if (!array)
- goto release;
+ json_object_put(rq);
- item = json_object_array_get_idx(array, 0);
+ str = NULL;
- if (!item)
- goto release;
+ if (rp) {
+ item = json_object_array_get_idx(rp, 0);
- content = json_object_object_get(item, "content");
+ if (item) {
+ content = json_object_object_get(item, "content");
+ str = strdup(json_object_get_string(content));
+ }
- h->content = strdup(json_object_get_string(content));
-
- release:
json_object_put(rp);
}
- return h->content;
+
+ return str;
}
-static struct headline **get_headlines(int feed_id)
+int ws_update_headlines(struct feed *feed)
{
- struct json_object *rp, *rq, *content, *jheadline, *j;
- int i, n;
- struct headline **headlines, *h;
-
- printf("get_headlines %d\n", feed_id);
+ struct json_object *rp, *rq, *jheadline, *j;
+ int i, n, err, hid;
+ struct headline *h, **tmp;
rq = ws_request_new("getHeadlines");
- json_object_object_add(rq, "feed_id", json_object_new_int(feed_id));
+ ws_request_add_att_int(rq, "feed_id", feed->id);
- rp = http_json_get(session_url, rq);
+ rp = ws_execute(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 *));
+ if (rp) {
+ n = json_object_array_length(rp);
for (i = 0; i < n; i++) {
- jheadline = json_object_array_get_idx(content, i);
-
- h = malloc(sizeof(struct headline));
+ jheadline = json_object_array_get_idx(rp, i);
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;
+ hid = json_object_get_int(j);
+ h = feed_get_headline(feed, hid);
+
+ if (!h) {
+ h = malloc(sizeof(struct headline));
+ h->id = hid;
+ h->excerpt = NULL;
+ h->content = NULL;
+ h->title = NULL;
+ h->url = NULL;
+
+ tmp = headlines_add(feed->headlines, h);
+ if (feed->headlines)
+ free(feed->headlines);
+ feed->headlines = tmp;
+ }
+
+ if (!h->title) {
+ j = json_object_object_get(jheadline, "title");
+ h->title = strdup(json_object_get_string(j));
+ }
+
+ if (!h->url) {
+ j = json_object_object_get(jheadline, "link");
+ h->url = strdup(json_object_get_string(j));
+ }
j = json_object_object_get(jheadline, "unread");
h->unread = json_object_get_boolean(j);
-
- headlines[i] = h;
}
- headlines[n] = NULL;
+ err = 0;
} else {
- headlines = NULL;
+ err = 1;
}
json_object_put(rp);
-
- printf("get_headlines %d end\n", feed_id);
-
- return headlines;
-}
-
-static int feeds_length(struct feed **list)
-{
- int n;
-
- if (!list)
- return 0;
-
- n = 0;
- while(*list) {
- n++;
- list++;
- }
-
- return n;
+ return !err;
}
-static struct feed **feeds_add(struct feed **feeds, struct feed *feed)
+const char *ttrss_get_headline_content(struct headline *h)
{
- int n;
- struct feed **result;
-
- n = feeds_length(feeds);
-
- result = malloc((n + 1 + 1) * sizeof(struct feed *));
+ if (!h->content)
+ h->content = ws_get_article_content(h->id);
- if (feeds)
- memcpy(result, feeds, n * sizeof(struct feed *));
-
- result[n] = feed;
- result[n + 1] = NULL;
-
- return result;
+ return h->content;
}
struct feed **ttrss_get_feeds()
struct headline **ttrss_get_headlines(struct feed *f)
{
if (!f->headlines)
- f->headlines = get_headlines(f->id);
+ ws_update_headlines(f);
return f->headlines;
}
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);
}
+
+void ttrss_set_config(const char *url, const char *user, const char *pwd)
+{
+ feeds_free(data);
+ data = NULL;
+ ws_init(url, user, pwd);
+}