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 pthread_mutex_t lock;
35 static char *session_id;
36 static char *session_url;
37 static char *session_user;
38 static char *session_pwd;
40 void ws_request_add_att_str(json_object *rq, const char *k, const char *str)
42 json_object_object_add(rq, k, json_object_new_string(str));
45 void ws_request_add_att_int(json_object *rq, const char *k, int v)
47 json_object_object_add(rq, k, json_object_new_int(v));
50 void ws_request_add_att_bool(json_object *rq, const char *k, int v)
52 json_object_object_add(rq, k, json_object_new_boolean(v));
55 struct json_object *ws_request_new(const char *op)
57 struct json_object *rq;
59 rq = json_object_new_object();
61 ws_request_add_att_str(rq, "op", op);
64 ws_request_add_att_str(rq, "sid", session_id);
69 void ws_set_config(const char *url, const char *user, const char *pwd)
73 if (session_url && !strcmp(session_url, url)
74 && session_user && !strcmp(session_user, user)
75 && session_pwd && !strcmp(session_pwd, pwd))
82 session_user = strdup(user);
85 session_pwd = strdup(pwd);
87 tmp = url_normalize(url);
89 session_url = malloc(strlen(tmp) + strlen("/api/") + 1);
90 strcpy(session_url, tmp);
91 strcat(session_url, "/api/");
95 struct json_object *ws_reply_get_content(struct json_object *rp)
97 return json_object_object_get(rp, "content");
100 static struct json_object *execute(struct json_object *rq, char **err)
102 struct json_object *rp, *content, *jerror;
105 rp = http_json_get(session_url, rq);
110 content = ws_reply_get_content(rp);
113 jerror = json_object_object_get(content, "error");
116 str = json_object_get_string(jerror);
121 json_object_get(content);
131 struct json_object *ws_execute(struct json_object *rq)
134 struct json_object *result;
136 log_debug("ws_execute()");
137 pthread_mutex_lock(&lock);
138 log_debug("ws_execute() lock");
141 result = execute(rq, &err);
144 log_debug("ws_execute(): error=%s\n", err);
146 if (!strcmp(err, "NOT_LOGGED_IN")) {
148 result = execute(rq, NULL);
154 log_debug("ws_execute() unlock");
155 pthread_mutex_unlock(&lock);
157 log_debug("ws_execute()");
162 int ws_get_api_version()
164 struct json_object *rp, *rq;
167 rq = ws_request_new("getApiLevel");
174 v = json_object_get_int(json_object_object_get(rp, "level"));
186 struct json_object *rq, *rp, *j;
189 rq = ws_request_new("login");
190 ws_request_add_att_str(rq, "user", session_user);
191 ws_request_add_att_str(rq, "password", session_pwd);
198 j = json_object_object_get(rp, "session_id");
201 str = strdup(json_object_get_string(j));
209 int ws_open_session()
213 log_debug("ws_open_session()");
219 session_id = ws_login();
222 version = ws_get_api_version();
223 log_debug("API version= %d", version);
240 char *ws_get_article_content(int id)
242 struct json_object *rp, *rq, *content, *item;
245 rq = ws_request_new("getArticle");
246 ws_request_add_att_int(rq, "article_id", id);
255 item = json_object_array_get_idx(rp, 0);
258 content = json_object_object_get(item, "content");
259 str = strdup(json_object_get_string(content));
268 int ws_update_headlines(struct feed *feed)
270 struct json_object *rp, *rq, *jheadline, *j;
272 struct headline *h, **tmp;
273 const char *title, *url;
275 rq = ws_request_new("getHeadlines");
276 ws_request_add_att_int(rq, "feed_id", feed->id);
277 ws_request_add_att_bool(rq, "show_excerpt", 1);
284 n = json_object_array_length(rp);
285 for (i = 0; i < n; i++) {
286 jheadline = json_object_array_get_idx(rp, i);
288 j = json_object_object_get(jheadline, "id");
289 hid = json_object_get_int(j);
290 h = feed_get_headline(feed, hid);
293 j = json_object_object_get(jheadline, "title");
294 title = json_object_get_string(j);
296 j = json_object_object_get(jheadline, "link");
297 url = json_object_get_string(j);
299 h = headline_new(hid, url, title);
301 j = json_object_object_get(jheadline,
303 h->excerpt = strdup(json_object_get_string(j));
305 tmp = headlines_add(feed->headlines, h);
307 free(feed->headlines);
308 feed->headlines = tmp;
311 j = json_object_object_get(jheadline, "unread");
312 h->unread = json_object_get_boolean(j);
321 struct feed **ws_update_feeds(struct feed **feeds)
323 struct json_object *rp, *rq, *jfeed, *j;
325 struct feed *feed, **tmp;
326 const char *title, *url;
328 log_debug("ws_update_feeds()");
330 rq = ws_request_new("getFeeds");
336 n = json_object_array_length(rp);
338 for (i = 0; i < n; i++) {
339 jfeed = json_object_array_get_idx(rp, i);
341 j = json_object_object_get(jfeed, "id");
342 id = json_object_get_int(j);
344 feed = feeds_get_feed(feeds, id);
347 j = json_object_object_get(jfeed, "title");
348 title = json_object_get_string(j);
350 j = json_object_object_get(jfeed, "feed_url");
351 url = json_object_get_string(j);
353 feed = feed_new(id, url, title);
355 tmp = feeds_add(feeds, feed);
360 j = json_object_object_get(jfeed, "unread");
361 feed->unread = json_object_get_int(j);
369 log_debug("ws_update_feeds() done");
374 struct json_object *ws_request_new_set_article_unread(int id, int unread)
376 struct json_object *rq;
378 rq = ws_request_new("updateArticle");
379 json_object_object_add(rq, "article_ids", json_object_new_int(id));
380 json_object_object_add(rq, "field", json_object_new_int(2));
381 json_object_object_add(rq, "mode", json_object_new_int(unread));
386 void ws_set_article_unread(int id, int unread)
388 struct json_object *rp, *rq;
390 log_debug("ws_set_article_unread(%d,%d)", id, unread);
392 rq = ws_request_new_set_article_unread(id, unread);
401 log_debug("ws_set_article_unread(%d,%d) done", id, unread);
406 pthread_mutexattr_t attr;
407 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
408 pthread_mutex_init(&lock, &attr);