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 *http_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 return json_object_object_get(rp, "content");
101 static struct json_object *execute(struct json_object *rq, char **err)
103 struct json_object *rp, *content, *jerror;
106 rp = http_json_get(http_session, session_url, rq);
111 content = ws_reply_get_content(rp);
114 jerror = json_object_object_get(content, "error");
117 str = json_object_get_string(jerror);
122 json_object_get(content);
132 struct json_object *ws_execute(struct json_object *rq)
135 struct json_object *result;
137 log_debug("ws_execute()");
138 pthread_mutex_lock(&lock);
139 log_debug("ws_execute() lock");
142 result = execute(rq, &err);
145 log_debug("ws_execute(): error=%s\n", err);
147 if (!strcmp(err, "NOT_LOGGED_IN")) {
149 result = execute(rq, NULL);
155 log_debug("ws_execute() unlock");
156 pthread_mutex_unlock(&lock);
158 log_debug("ws_execute()");
163 int ws_get_api_version()
165 struct json_object *rp, *rq;
168 rq = ws_request_new("getApiLevel");
175 v = json_object_get_int(json_object_object_get(rp, "level"));
187 struct json_object *rq, *rp, *j;
190 rq = ws_request_new("login");
191 ws_request_add_att_str(rq, "user", session_user);
192 ws_request_add_att_str(rq, "password", session_pwd);
199 j = json_object_object_get(rp, "session_id");
202 str = strdup(json_object_get_string(j));
210 int ws_open_session()
214 log_debug("ws_open_session()");
220 session_id = ws_login();
223 version = ws_get_api_version();
224 log_debug("API version= %d", version);
241 char *ws_get_article_content(int id)
243 struct json_object *rp, *rq, *content, *item;
246 rq = ws_request_new("getArticle");
247 ws_request_add_att_int(rq, "article_id", id);
256 item = json_object_array_get_idx(rp, 0);
259 content = json_object_object_get(item, "content");
260 str = strdup(json_object_get_string(content));
269 int ws_update_headlines(struct feed *feed)
271 struct json_object *rp, *rq, *jheadline, *j;
273 struct headline *h, **tmp;
274 const char *title, *url;
276 rq = ws_request_new("getHeadlines");
277 ws_request_add_att_int(rq, "feed_id", feed->id);
278 ws_request_add_att_bool(rq, "show_excerpt", 1);
285 n = json_object_array_length(rp);
286 for (i = 0; i < n; i++) {
287 jheadline = json_object_array_get_idx(rp, i);
289 j = json_object_object_get(jheadline, "id");
290 hid = json_object_get_int(j);
291 h = feed_get_headline(feed, hid);
294 j = json_object_object_get(jheadline, "title");
295 title = json_object_get_string(j);
297 j = json_object_object_get(jheadline, "link");
298 url = json_object_get_string(j);
300 h = headline_new(hid, url, title);
302 j = json_object_object_get(jheadline,
304 h->excerpt = strdup(json_object_get_string(j));
306 j = json_object_object_get(jheadline,
308 h->date = json_object_get_int(j);
310 tmp = headlines_add(feed->headlines, h);
312 free(feed->headlines);
313 feed->headlines = tmp;
316 j = json_object_object_get(jheadline, "unread");
317 h->unread = json_object_get_boolean(j);
326 struct feed **ws_update_feeds(struct feed **feeds)
328 struct json_object *rp, *rq, *jfeed, *j;
330 struct feed *feed, **tmp;
331 const char *title, *url;
333 log_debug("ws_update_feeds()");
335 rq = ws_request_new("getFeeds");
341 n = json_object_array_length(rp);
343 for (i = 0; i < n; i++) {
344 jfeed = json_object_array_get_idx(rp, i);
346 j = json_object_object_get(jfeed, "id");
347 id = json_object_get_int(j);
349 feed = feeds_get_feed(feeds, id);
352 j = json_object_object_get(jfeed, "title");
353 title = json_object_get_string(j);
355 j = json_object_object_get(jfeed, "feed_url");
356 url = json_object_get_string(j);
358 feed = feed_new(id, url, title);
360 tmp = feeds_add(feeds, feed);
365 j = json_object_object_get(jfeed, "unread");
366 feed->unread = json_object_get_int(j);
374 log_debug("ws_update_feeds() done");
379 struct json_object *ws_request_new_set_article_unread(int id, int unread)
381 struct json_object *rq;
383 rq = ws_request_new("updateArticle");
384 json_object_object_add(rq, "article_ids", json_object_new_int(id));
385 json_object_object_add(rq, "field", json_object_new_int(2));
386 json_object_object_add(rq, "mode", json_object_new_int(unread));
391 void ws_set_article_unread(int id, int unread)
393 struct json_object *rp, *rq;
395 log_debug("ws_set_article_unread(%d,%d)", id, unread);
397 rq = ws_request_new_set_article_unread(id, unread);
406 log_debug("ws_set_article_unread(%d,%d) done", id, unread);
411 pthread_mutexattr_t attr;
412 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
413 pthread_mutex_init(&lock, &attr);
415 http_session = http_session_new();