2 * Copyright (C) 2010-2013 jeanfi@gmail.com
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 #include <json/json.h>
29 static char *session_id;
30 static char *session_url;
31 static char *session_user;
32 static char *session_pwd;
34 static int list_length(void **list)
50 static void **list_add(void **list, void *item)
55 n = list_length(list);
57 result = malloc((n + 1 + 1) * sizeof(void *));
60 memcpy(result, list, n * sizeof(void *));
69 struct headline *feed_get_headline(struct feed *feed, int id)
71 struct headline **headlines;
73 headlines = feed->headlines;
76 if ((*headlines)->id == id)
84 void ws_request_add_att_str(json_object *rq, const char *k, const char *str)
86 json_object_object_add(rq, k, json_object_new_string(str));
89 void ws_request_add_att_int(json_object *rq, const char *k, int v)
91 json_object_object_add(rq, k, json_object_new_int(v));
94 struct json_object *ws_request_new(const char *op)
96 struct json_object *rq;
98 rq = json_object_new_object();
100 ws_request_add_att_str(rq, "op", op);
103 ws_request_add_att_str(rq, "sid", session_id);
108 void ws_init(const char *url, const char *user, const char *pwd)
117 session_user = strdup(user);
121 session_pwd = strdup(pwd);
126 tmp = url_normalize(url);
127 session_url = malloc(strlen(tmp) + strlen("/api/") + 1);
128 strcpy(session_url, tmp);
129 strcat(session_url, "/api/");
133 struct json_object *ws_reply_get_content(struct json_object *rp)
135 return json_object_object_get(rp, "content");
138 struct json_object *ws_execute(struct json_object *rq)
140 struct json_object *rp, *content;
142 rp = http_json_get(session_url, rq);
145 content = ws_reply_get_content(rp);
147 if (content && !json_object_object_get(rp, "error")) {
148 json_object_get(content);
159 int ws_get_api_version()
161 struct json_object *rp, *rq;
164 rq = ws_request_new("getApiLevel");
171 v = json_object_get_int(json_object_object_get(rp, "level"));
183 struct json_object *rq, *rp, *j;
186 rq = ws_request_new("login");
187 ws_request_add_att_str(rq, "user", session_user);
188 ws_request_add_att_str(rq, "password", session_pwd);
194 j = json_object_object_get(rp, "session_id");
195 str = strdup(json_object_get_string(j));
205 int ws_open_session()
212 session_id = ws_login();
215 version = ws_get_api_version();
216 printf("API version: %d\n", version);
232 char *ws_get_article_content(int id)
234 struct json_object *rp, *rq, *content, *item;
237 rq = ws_request_new("getArticle");
238 ws_request_add_att_int(rq, "article_id", id);
247 item = json_object_array_get_idx(rp, 0);
250 content = json_object_object_get(item, "content");
252 str = strdup(json_object_get_string(content));
261 int ws_update_headlines(struct feed *feed)
263 struct json_object *rp, *rq, *jheadline, *j;
265 struct headline *h, **tmp;
267 rq = ws_request_new("getHeadlines");
268 ws_request_add_att_int(rq, "feed_id", feed->id);
275 n = json_object_array_length(rp);
276 for (i = 0; i < n; i++) {
277 jheadline = json_object_array_get_idx(rp, i);
279 j = json_object_object_get(jheadline, "id");
280 hid = json_object_get_int(j);
281 h = feed_get_headline(feed, hid);
284 h = malloc(sizeof(struct headline));
289 tmp = (struct headline **)list_add((void **)feed->headlines, h);
291 free(feed->headlines);
292 feed->headlines = tmp;
295 j = json_object_object_get(jheadline, "title");
296 h->title = strdup(json_object_get_string(j));
298 j = json_object_object_get(jheadline, "link");
299 h->url = strdup(json_object_get_string(j));
301 j = json_object_object_get(jheadline, "unread");
302 h->unread = json_object_get_boolean(j);
313 const char *ttrss_get_headline_content(struct headline *h)
316 h->content = ws_get_article_content(h->id);
322 struct feed **ttrss_get_feeds()
324 struct json_object *rp, *rq, *content, *jfeed, *j;
326 struct feed **feeds, *feed, **tmp;
328 printf("ttrss_get_feeds\n");
330 rq = ws_request_new("getFeeds");
332 rp = http_json_get(session_url, rq);
335 content = json_object_object_get(rp, "content");
338 n = json_object_array_length(content);
341 for (i = 0; i < n; i++) {
342 jfeed = json_object_array_get_idx(content, i);
344 feed = malloc(sizeof(struct feed));
346 j = json_object_object_get(jfeed, "title");
347 feed->title = strdup(json_object_get_string(j));
349 j = json_object_object_get(jfeed, "feed_url");
350 feed->url = strdup(json_object_get_string(j));
352 j = json_object_object_get(jfeed, "id");
353 feed->id = json_object_get_int(j);
355 j = json_object_object_get(jfeed, "unread");
356 feed->unread = json_object_get_int(j);
358 feed->headlines = NULL;
360 tmp = (struct feed **)list_add((void **)feeds, feed);
370 printf("ttrss_get_feeds ended\n");
375 struct headline **ttrss_get_headlines(struct feed *f)
378 ws_update_headlines(f);
383 void ttrss_set_article_unread(int id, int unread)
385 struct json_object *rp, *rq;
387 printf("ttrss_set_article_unread %d %d\n", id, unread);
389 rq = ws_request_new("updateArticle");
390 json_object_object_add(rq, "article_ids", json_object_new_int(id));
391 json_object_object_add(rq, "field", json_object_new_int(2));
392 json_object_object_add(rq, "mode", json_object_new_int(unread));
394 rp = http_json_get(session_url, rq);