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
26 #include <json/json.h>
33 static char *session_id;
34 static char *session_url;
35 static char *session_user;
36 static char *session_pwd;
38 static pthread_mutex_t lock;
39 static struct http_session *session;
41 void ws_request_add_att_str(json_object *rq, const char *k, const char *str)
43 json_object_object_add(rq, k, json_object_new_string(str));
46 void ws_request_add_att_int(json_object *rq, const char *k, int v)
48 json_object_object_add(rq, k, json_object_new_int(v));
51 void ws_request_add_att_bool(json_object *rq, const char *k, int v)
53 json_object_object_add(rq, k, json_object_new_boolean(v));
56 struct json_object *ws_request_new(const char *op)
58 struct json_object *rq;
60 rq = json_object_new_object();
62 ws_request_add_att_str(rq, "op", op);
65 ws_request_add_att_str(rq, "sid", session_id);
70 void ws_set_config(const char *url, const char *user, const char *pwd)
74 if (session_url && !strcmp(session_url, url)
75 && session_user && !strcmp(session_user, user)
76 && session_pwd && !strcmp(session_pwd, pwd))
83 session_user = strdup(user);
86 session_pwd = strdup(pwd);
88 tmp = url_normalize(url);
90 session_url = malloc(strlen(tmp) + strlen("/api/") + 1);
91 strcpy(session_url, tmp);
92 strcat(session_url, "/api/");
96 struct json_object *ws_reply_get_content(struct json_object *rp)
98 log_debug("ws_reply_get_content");
99 return json_object_object_get(rp, "content");
102 static const char *ws_reply_get_error(struct json_object *content)
104 struct json_object *jerror;
106 if (json_object_get_type(content) != json_type_object)
109 jerror = json_object_object_get(content, "error");
114 return json_object_get_string(jerror);
117 static struct json_object *
118 execute(struct http_session *sess, struct json_object *rq, char **err)
120 struct json_object *rp, *content;
123 rp = http_json_get(sess, session_url, rq);
128 content = ws_reply_get_content(rp);
131 str = ws_reply_get_error(content);
137 json_object_get(content);
147 struct json_object *ws_execute(struct json_object *rq)
150 struct json_object *result;
152 log_debug("ws_execute()");
153 pthread_mutex_lock(&lock);
154 log_debug("ws_execute() lock");
157 result = execute(session, rq, &err);
160 log_debug("ws_execute(): error=%s", err);
162 if (!strcmp(err, "NOT_LOGGED_IN")) {
164 result = execute(session, rq, NULL);
170 log_debug("ws_execute() unlock");
171 pthread_mutex_unlock(&lock);
173 log_debug("ws_execute()");
178 int ws_get_api_version()
180 struct json_object *rp, *rq;
183 rq = ws_request_new("getApiLevel");
190 v = json_object_get_int(json_object_object_get(rp, "level"));
202 struct json_object *rq, *rp, *j;
205 rq = ws_request_new("login");
206 ws_request_add_att_str(rq, "user", session_user);
207 ws_request_add_att_str(rq, "password", session_pwd);
214 j = json_object_object_get(rp, "session_id");
217 str = strdup(json_object_get_string(j));
225 int ws_open_session()
229 log_debug("ws_open_session()");
235 session_id = ws_login();
238 version = ws_get_api_version();
239 log_debug("API version= %d", version);
256 char *ws_get_article_content(int id)
258 struct json_object *rp, *rq, *content, *item;
261 rq = ws_request_new("getArticle");
262 ws_request_add_att_int(rq, "article_id", id);
271 item = json_object_array_get_idx(rp, 0);
274 content = json_object_object_get(item, "content");
275 str = strdup(json_object_get_string(content));
284 int ws_update_headlines(struct feed *feed)
286 struct json_object *rp, *rq, *jheadline, *j;
288 struct headline *h, **tmp;
289 const char *title, *url;
291 rq = ws_request_new("getHeadlines");
292 ws_request_add_att_int(rq, "feed_id", feed->id);
293 ws_request_add_att_bool(rq, "show_excerpt", 1);
300 n = json_object_array_length(rp);
301 for (i = 0; i < n; i++) {
302 jheadline = json_object_array_get_idx(rp, i);
304 j = json_object_object_get(jheadline, "id");
305 hid = json_object_get_int(j);
306 h = feed_get_headline(feed, hid);
309 j = json_object_object_get(jheadline, "title");
310 title = json_object_get_string(j);
312 j = json_object_object_get(jheadline, "link");
313 url = json_object_get_string(j);
315 h = headline_new(hid, url, title);
317 j = json_object_object_get(jheadline,
319 h->excerpt = strdup(json_object_get_string(j));
321 j = json_object_object_get(jheadline,
323 h->date = json_object_get_int(j);
325 tmp = headlines_add(feed->headlines, h);
327 free(feed->headlines);
328 feed->headlines = tmp;
331 j = json_object_object_get(jheadline, "unread");
332 h->unread = json_object_get_boolean(j);
341 struct feed **ws_update_feeds(struct feed **feeds)
343 struct json_object *rp, *rq, *jfeed, *j;
345 struct feed *feed, **tmp;
346 const char *title, *url;
348 log_debug("ws_update_feeds()");
350 rq = ws_request_new("getFeeds");
357 n = json_object_array_length(rp);
359 for (i = 0; i < n; i++) {
360 jfeed = json_object_array_get_idx(rp, i);
362 j = json_object_object_get(jfeed, "id");
363 id = json_object_get_int(j);
365 feed = feeds_get_feed(feeds, id);
368 j = json_object_object_get(jfeed, "title");
369 title = json_object_get_string(j);
371 j = json_object_object_get(jfeed, "feed_url");
372 url = json_object_get_string(j);
374 feed = feed_new(id, url, title);
376 tmp = feeds_add(feeds, feed);
381 j = json_object_object_get(jfeed, "unread");
382 feed->unread = json_object_get_int(j);
384 ws_update_headlines(feed);
392 log_debug("ws_update_feeds() done");
397 struct json_object *ws_request_new_set_article_unread(int id, int unread)
399 struct json_object *rq;
401 rq = ws_request_new("updateArticle");
402 json_object_object_add(rq, "article_ids", json_object_new_int(id));
403 json_object_object_add(rq, "field", json_object_new_int(2));
404 json_object_object_add(rq, "mode", json_object_new_int(unread));
409 void ws_set_article_unread(int id, int unread)
411 struct json_object *rp, *rq;
413 log_debug("ws_set_article_unread(%d,%d)", id, unread);
415 rq = ws_request_new_set_article_unread(id, unread);
424 log_debug("ws_set_article_unread(%d,%d) done", id, unread);
429 pthread_mutexattr_t attr;
430 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
431 pthread_mutex_init(&lock, &attr);
433 session = http_session_new();