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 struct json_object *ws_request_new(const char *op)
52 struct json_object *rq;
54 rq = json_object_new_object();
56 ws_request_add_att_str(rq, "op", op);
59 ws_request_add_att_str(rq, "sid", session_id);
64 void ws_set_config(const char *url, const char *user, const char *pwd)
68 if (session_url && !strcmp(session_url, url)
69 && session_user && !strcmp(session_user, user)
70 && session_pwd && !strcmp(session_pwd, pwd))
77 session_user = strdup(user);
80 session_pwd = strdup(pwd);
82 tmp = url_normalize(url);
84 session_url = malloc(strlen(tmp) + strlen("/api/") + 1);
85 strcpy(session_url, tmp);
86 strcat(session_url, "/api/");
90 struct json_object *ws_reply_get_content(struct json_object *rp)
92 return json_object_object_get(rp, "content");
95 static struct json_object *execute(struct json_object *rq, char **err)
97 struct json_object *rp, *content, *jerror;
100 rp = http_json_get(session_url, rq);
105 content = ws_reply_get_content(rp);
108 jerror = json_object_object_get(content, "error");
111 str = json_object_get_string(jerror);
116 json_object_get(content);
126 struct json_object *ws_execute(struct json_object *rq)
129 struct json_object *result;
131 log_debug("ws_execute()");
132 pthread_mutex_lock(lock);
133 log_debug("ws_execute() lock");
136 result = execute(rq, &err);
139 log_debug("ws_execute(): error=%s\n", err);
141 if (!strcmp(err, "NOT_LOGGED_IN")) {
143 result = execute(rq, NULL);
149 log_debug("ws_execute() unlock");
150 pthread_mutex_unlock(lock);
152 log_debug("ws_execute()");
157 int ws_get_api_version()
159 struct json_object *rp, *rq;
162 rq = ws_request_new("getApiLevel");
169 v = json_object_get_int(json_object_object_get(rp, "level"));
181 struct json_object *rq, *rp, *j;
184 rq = ws_request_new("login");
185 ws_request_add_att_str(rq, "user", session_user);
186 ws_request_add_att_str(rq, "password", session_pwd);
193 j = json_object_object_get(rp, "session_id");
196 str = strdup(json_object_get_string(j));
204 int ws_open_session()
206 int /*version, */result;
208 log_debug("ws_open_session()");
214 session_id = ws_login();
217 /*version = ws_get_api_version();
218 log_debug("API version= %d", version);
235 char *ws_get_article_content(int id)
237 struct json_object *rp, *rq, *content, *item;
240 rq = ws_request_new("getArticle");
241 ws_request_add_att_int(rq, "article_id", id);
250 item = json_object_array_get_idx(rp, 0);
253 content = json_object_object_get(item, "content");
254 str = strdup(json_object_get_string(content));
263 int ws_update_headlines(struct feed *feed)
265 struct json_object *rp, *rq, *jheadline, *j;
267 struct headline *h, **tmp;
268 const char *title, *url;
270 rq = ws_request_new("getHeadlines");
271 ws_request_add_att_int(rq, "feed_id", feed->id);
278 n = json_object_array_length(rp);
279 for (i = 0; i < n; i++) {
280 jheadline = json_object_array_get_idx(rp, i);
282 j = json_object_object_get(jheadline, "id");
283 hid = json_object_get_int(j);
284 h = feed_get_headline(feed, hid);
287 j = json_object_object_get(jheadline, "title");
288 title = json_object_get_string(j);
290 j = json_object_object_get(jheadline, "link");
291 url = json_object_get_string(j);
293 h = headline_new(hid, url, title);
295 tmp = headlines_add(feed->headlines, h);
297 free(feed->headlines);
298 feed->headlines = tmp;
301 j = json_object_object_get(jheadline, "unread");
302 h->unread = json_object_get_boolean(j);
311 struct feed **ws_update_feeds(struct feed **feeds)
313 struct json_object *rp, *rq, *jfeed, *j;
315 struct feed *feed, **tmp;
316 const char *title, *url;
318 log_debug("ws_update_feeds()");
320 rq = ws_request_new("getFeeds");
326 n = json_object_array_length(rp);
328 for (i = 0; i < n; i++) {
329 jfeed = json_object_array_get_idx(rp, i);
331 j = json_object_object_get(jfeed, "id");
332 id = json_object_get_int(j);
334 feed = feeds_get_feed(feeds, id);
337 j = json_object_object_get(jfeed, "title");
338 title = json_object_get_string(j);
340 j = json_object_object_get(jfeed, "feed_url");
341 url = json_object_get_string(j);
343 feed = feed_new(id, url, title);
345 tmp = feeds_add(feeds, feed);
350 j = json_object_object_get(jfeed, "unread");
351 feed->unread = json_object_get_int(j);
359 log_debug("ws_update_feeds() done");
364 struct json_object *ws_request_new_set_article_unread(int id, int unread)
366 struct json_object *rq;
368 rq = ws_request_new("updateArticle");
369 json_object_object_add(rq, "article_ids", json_object_new_int(id));
370 json_object_object_add(rq, "field", json_object_new_int(2));
371 json_object_object_add(rq, "mode", json_object_new_int(unread));
376 void ws_set_article_unread(int id, int unread)
378 struct json_object *rp, *rq;
380 log_debug("ws_set_article_unread(%d,%d)", id, unread);
382 rq = ws_request_new_set_article_unread(id, unread);
391 log_debug("ws_set_article_unread(%d,%d) done", id, unread);
396 pthread_mutexattr_t attr;
398 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
399 lock = malloc(sizeof(pthread_mutex_t));
400 pthread_mutex_init(lock, &attr);