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
24 #include <json/json.h>
31 static char *session_id;
32 static char *session_url;
33 static char *session_user;
34 static char *session_pwd;
36 void ws_request_add_att_str(json_object *rq, const char *k, const char *str)
38 json_object_object_add(rq, k, json_object_new_string(str));
41 void ws_request_add_att_int(json_object *rq, const char *k, int v)
43 json_object_object_add(rq, k, json_object_new_int(v));
46 struct json_object *ws_request_new(const char *op)
48 struct json_object *rq;
50 rq = json_object_new_object();
52 ws_request_add_att_str(rq, "op", op);
55 ws_request_add_att_str(rq, "sid", session_id);
60 void ws_init(const char *url, const char *user, const char *pwd)
64 if (session_url && !strcmp(session_url, url)
65 && session_user && !strcmp(session_user, user)
66 && session_pwd && !strcmp(session_pwd, pwd))
73 session_user = strdup(user);
76 session_pwd = strdup(pwd);
78 tmp = url_normalize(url);
80 session_url = malloc(strlen(tmp) + strlen("/api/") + 1);
81 strcpy(session_url, tmp);
82 strcat(session_url, "/api/");
86 struct json_object *ws_reply_get_content(struct json_object *rp)
88 return json_object_object_get(rp, "content");
91 static struct json_object *execute(struct json_object *rq, char **err)
93 struct json_object *rp, *content, *jerror;
96 rp = http_json_get(session_url, rq);
101 content = ws_reply_get_content(rp);
104 jerror = json_object_object_get(content, "error");
107 str = json_object_get_string(jerror);
112 json_object_get(content);
122 struct json_object *ws_execute(struct json_object *rq)
125 struct json_object *result;
127 log_debug("ws_execute()");
130 result = execute(rq, &err);
133 log_debug("ws_execute(): error=%s\n", err);
135 if (!strcmp(err, "NOT_LOGGED_IN")) {
137 result = execute(rq, NULL);
146 int ws_get_api_version()
148 struct json_object *rp, *rq;
151 rq = ws_request_new("getApiLevel");
158 v = json_object_get_int(json_object_object_get(rp, "level"));
170 struct json_object *rq, *rp, *j;
173 rq = ws_request_new("login");
174 ws_request_add_att_str(rq, "user", session_user);
175 ws_request_add_att_str(rq, "password", session_pwd);
182 j = json_object_object_get(rp, "session_id");
185 str = strdup(json_object_get_string(j));
193 int ws_open_session()
197 log_debug("ws_open_session()");
202 session_id = ws_login();
205 version = ws_get_api_version();
206 log_debug("API version= %d", version);
222 char *ws_get_article_content(int id)
224 struct json_object *rp, *rq, *content, *item;
227 rq = ws_request_new("getArticle");
228 ws_request_add_att_int(rq, "article_id", id);
237 item = json_object_array_get_idx(rp, 0);
240 content = json_object_object_get(item, "content");
241 str = strdup(json_object_get_string(content));
250 int ws_update_headlines(struct feed *feed)
252 struct json_object *rp, *rq, *jheadline, *j;
254 struct headline *h, **tmp;
255 const char *title, *url;
257 rq = ws_request_new("getHeadlines");
258 ws_request_add_att_int(rq, "feed_id", feed->id);
265 n = json_object_array_length(rp);
266 for (i = 0; i < n; i++) {
267 jheadline = json_object_array_get_idx(rp, i);
269 j = json_object_object_get(jheadline, "id");
270 hid = json_object_get_int(j);
271 h = feed_get_headline(feed, hid);
274 j = json_object_object_get(jheadline, "title");
275 title = json_object_get_string(j);
277 j = json_object_object_get(jheadline, "link");
278 url = json_object_get_string(j);
280 h = headline_new(hid, url, title);
282 tmp = headlines_add(feed->headlines, h);
284 free(feed->headlines);
285 feed->headlines = tmp;
288 j = json_object_object_get(jheadline, "unread");
289 h->unread = json_object_get_boolean(j);
298 struct feed **ws_update_feeds(struct feed **feeds)
300 struct json_object *rp, *rq, *jfeed, *j;
302 struct feed *feed, **tmp;
303 const char *title, *url;
305 log_debug("ws_update_feeds()");
307 rq = ws_request_new("getFeeds");
313 n = json_object_array_length(rp);
315 for (i = 0; i < n; i++) {
316 jfeed = json_object_array_get_idx(rp, i);
318 j = json_object_object_get(jfeed, "id");
319 id = json_object_get_int(j);
321 feed = feeds_get_feed(feeds, id);
324 j = json_object_object_get(jfeed, "title");
325 title = json_object_get_string(j);
327 j = json_object_object_get(jfeed, "feed_url");
328 url = json_object_get_string(j);
330 feed = feed_new(id, url, title);
332 tmp = feeds_add(feeds, feed);
337 j = json_object_object_get(jfeed, "unread");
338 feed->unread = json_object_get_int(j);
346 log_debug("ws_update_feeds() done");
351 void ws_set_article_unread(int id, int unread)
353 struct json_object *rp, *rq;
356 log_debug("ws_set_article_unread(%d,%d)", id, unread);
358 rq = ws_request_new("updateArticle");
359 json_object_object_add(rq, "article_ids", json_object_new_int(id));
360 json_object_object_add(rq, "field", json_object_new_int(2));
361 json_object_object_add(rq, "mode", json_object_new_int(unread));
370 log_debug("ws_set_article_unread(%d,%d) done", id, unread);