From: Jean-Philippe Orsini Date: Wed, 24 Apr 2013 16:30:32 +0000 (+0000) Subject: (no commit message) X-Git-Url: https://git.wpitchoune.net/gitweb/?p=prss.git;a=commitdiff_plain;h=4e8c884a282bcf16f486d06164ee187faa8a2610 --- diff --git a/src/Makefile.am b/src/Makefile.am index e2393da..042d0f2 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -18,6 +18,8 @@ prss_SOURCES = main.c \ ttrss.h \ ttrss_model.c \ ttrss_model.h \ + ttrss_ws.c \ + ttrss_ws.h \ url.c \ url.h \ webbrowser.c \ diff --git a/src/Makefile.in b/src/Makefile.in index 888be55..41e5e30 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -65,7 +65,8 @@ CONFIG_CLEAN_VPATH_FILES = am__installdirs = "$(DESTDIR)$(bindir)" "$(DESTDIR)$(man1dir)" PROGRAMS = $(bin_PROGRAMS) am_prss_OBJECTS = main.$(OBJEXT) http.$(OBJEXT) ttrss.$(OBJEXT) \ - ttrss_model.$(OBJEXT) url.$(OBJEXT) webbrowser.$(OBJEXT) + ttrss_model.$(OBJEXT) ttrss_ws.$(OBJEXT) url.$(OBJEXT) \ + webbrowser.$(OBJEXT) prss_OBJECTS = $(am_prss_OBJECTS) prss_LDADD = $(LDADD) DEFAULT_INCLUDES = -I.@am__isrc@ @@ -298,6 +299,8 @@ prss_SOURCES = main.c \ ttrss.h \ ttrss_model.c \ ttrss_model.h \ + ttrss_ws.c \ + ttrss_ws.h \ url.c \ url.h \ webbrowser.c \ @@ -393,6 +396,7 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/main.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ttrss.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ttrss_model.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ttrss_ws.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/url.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/webbrowser.Po@am__quote@ diff --git a/src/http.c b/src/http.c index a9cf301..99afa01 100644 --- a/src/http.c +++ b/src/http.c @@ -28,7 +28,7 @@ #include "http.h" -static int debug = 1; +static int debug = 0; struct ucontent { char *data; diff --git a/src/main.c b/src/main.c index 934b852..d3f134b 100644 --- a/src/main.c +++ b/src/main.c @@ -91,6 +91,7 @@ void update() printf("update(): clear feed tree\n"); model_state = 1; gtk_list_store_clear(GTK_LIST_STORE(model)); + gtk_list_store_clear(GTK_LIST_STORE(gtk_tree_view_get_model(w_headlineview))); printf("update(): clear feed tree done\n"); feeds = ttrss_get_feeds(); while (feeds && *feeds) { @@ -179,6 +180,9 @@ int feed_cursor_changed_cbk(GtkTreeView *treeview, gpointer data) struct headline **headlines; char *title; + if (model_state) + return TRUE; + printf("feed_cursor_changed_cbk\n"); gtk_tree_view_get_cursor(treeview, &path, &cols); @@ -193,7 +197,7 @@ int feed_cursor_changed_cbk(GtkTreeView *treeview, gpointer data) model_state = 1; gtk_list_store_clear(headline_store); - headlines = ttrss_get_headlines(feed); + headlines = ttrss_feed_get_headlines(feed); while (headlines && *headlines) { gtk_list_store_append(headline_store, &iter); diff --git a/src/ttrss.c b/src/ttrss.c index 1f97155..a9fb0f8 100644 --- a/src/ttrss.c +++ b/src/ttrss.c @@ -23,242 +23,11 @@ #include #include "http.h" -#include "ttrss.h" +#include "ttrss_ws.h" #include "url.h" -static char *session_id; -static char *session_url; -static char *session_user; -static char *session_pwd; - static struct feed **data; -void ws_request_add_att_str(json_object *rq, const char *k, const char *str) -{ - json_object_object_add(rq, k, json_object_new_string(str)); -} - -void ws_request_add_att_int(json_object *rq, const char *k, int v) -{ - json_object_object_add(rq, k, json_object_new_int(v)); -} - -struct json_object *ws_request_new(const char *op) -{ - struct json_object *rq; - - rq = json_object_new_object(); - - ws_request_add_att_str(rq, "op", op); - - if (session_id) - ws_request_add_att_str(rq, "sid", session_id); - - return rq; -} - -void ws_init(const char *url, const char *user, const char *pwd) -{ - char *tmp; - - if (session_id) - session_id = NULL; - - if (session_user) - free(session_user); - session_user = strdup(user); - - if (session_pwd) - free(session_pwd); - session_pwd = strdup(pwd); - - if (session_url) - free(session_url); - - tmp = url_normalize(url); - session_url = malloc(strlen(tmp) + strlen("/api/") + 1); - strcpy(session_url, tmp); - strcat(session_url, "/api/"); - free(tmp); -} - -struct json_object *ws_reply_get_content(struct json_object *rp) -{ - return json_object_object_get(rp, "content"); -} - -struct json_object *ws_execute(struct json_object *rq) -{ - struct json_object *rp, *content; - - rp = http_json_get(session_url, rq); - - if (rp) { - content = ws_reply_get_content(rp); - - if (content && !json_object_object_get(rp, "error")) { - json_object_get(content); - json_object_put(rp); - return content; - } - - json_object_put(rp); - } - - return NULL; -} - -int ws_get_api_version() -{ - struct json_object *rp, *rq; - int v; - - rq = ws_request_new("getApiLevel"); - - rp = ws_execute(rq); - - json_object_put(rq); - - if (rp) { - v = json_object_get_int(json_object_object_get(rp, "level")); - - json_object_put(rp); - } else { - v = 0; - } - - return v; -} - -char *ws_login() -{ - struct json_object *rq, *rp, *j; - char *str; - - rq = ws_request_new("login"); - ws_request_add_att_str(rq, "user", session_user); - ws_request_add_att_str(rq, "password", session_pwd); - - rp = ws_execute(rq); - json_object_put(rq); - - if (rp) { - j = json_object_object_get(rp, "session_id"); - str = strdup(json_object_get_string(j)); - - json_object_put(rp); - } else { - str = NULL; - } - - return str; -} - -int ws_open_session() -{ - int version, result; - - if (session_id) - free(session_id); - - session_id = ws_login(); - - if (session_id) { - version = ws_get_api_version(); - printf("API version: %d\n", version); - - if (version > 0) { - result = 1; - } else { - free(session_id); - session_id = NULL; - result = 0; - } - } else { - result = 0; - } - - return result; -} - -char *ws_get_article_content(int id) -{ - struct json_object *rp, *rq, *content, *item; - char *str; - - rq = ws_request_new("getArticle"); - ws_request_add_att_int(rq, "article_id", id); - - rp = ws_execute(rq); - - json_object_put(rq); - - str = NULL; - - if (rp) { - item = json_object_array_get_idx(rp, 0); - - if (item) { - content = json_object_object_get(item, "content"); - str = strdup(json_object_get_string(content)); - } - - json_object_put(rp); - } - - return str; -} - -int ws_update_headlines(struct feed *feed) -{ - struct json_object *rp, *rq, *jheadline, *j; - int i, n, err, hid; - struct headline *h, **tmp; - const char *title, *url; - - rq = ws_request_new("getHeadlines"); - ws_request_add_att_int(rq, "feed_id", feed->id); - - rp = ws_execute(rq); - - json_object_put(rq); - - if (rp) { - n = json_object_array_length(rp); - for (i = 0; i < n; i++) { - jheadline = json_object_array_get_idx(rp, i); - - j = json_object_object_get(jheadline, "id"); - hid = json_object_get_int(j); - h = feed_get_headline(feed, hid); - - if (!h) { - j = json_object_object_get(jheadline, "title"); - title = json_object_get_string(j); - - j = json_object_object_get(jheadline, "link"); - url = json_object_get_string(j); - - h = headline_new(hid, url, title); - - tmp = headlines_add(feed->headlines, h); - if (feed->headlines) - free(feed->headlines); - feed->headlines = tmp; - } - - j = json_object_object_get(jheadline, "unread"); - h->unread = json_object_get_boolean(j); - } - err = 0; - } else { - err = 1; - } - - json_object_put(rp); - return !err; -} - const char *ttrss_get_headline_content(struct headline *h) { if (!h->content) @@ -269,58 +38,16 @@ const char *ttrss_get_headline_content(struct headline *h) struct feed **ttrss_get_feeds() { - struct json_object *rp, *rq, *content, *jfeed, *j; - int i, n; - struct feed **feeds, *feed, **tmp; - - printf("ttrss_get_feeds\n"); - - rq = ws_request_new("getFeeds"); - - rp = http_json_get(session_url, rq); - json_object_put(rq); - - content = json_object_object_get(rp, "content"); + struct feed **tmp; - if (content) { - n = json_object_array_length(content); - - feeds = NULL; - for (i = 0; i < n; i++) { - jfeed = json_object_array_get_idx(content, i); - - feed = malloc(sizeof(struct feed)); - - j = json_object_object_get(jfeed, "title"); - feed->title = strdup(json_object_get_string(j)); - - j = json_object_object_get(jfeed, "feed_url"); - feed->url = strdup(json_object_get_string(j)); - - j = json_object_object_get(jfeed, "id"); - feed->id = json_object_get_int(j); - - j = json_object_object_get(jfeed, "unread"); - feed->unread = json_object_get_int(j); - - feed->headlines = NULL; - - tmp = feeds_add(feeds, feed); - free(feeds); - feeds = tmp; - } - } else { - feeds = NULL; - } - - json_object_put(rp); - - printf("ttrss_get_feeds ended\n"); + tmp = ws_update_feeds(data); + feeds_free(data); + data = tmp; - return feeds; + return data; } -struct headline **ttrss_get_headlines(struct feed *f) +struct headline **ttrss_feed_get_headlines(struct feed *f) { if (!f->headlines) ws_update_headlines(f); @@ -339,7 +66,7 @@ void ttrss_set_article_unread(int id, int unread) json_object_object_add(rq, "field", json_object_new_int(2)); json_object_object_add(rq, "mode", json_object_new_int(unread)); - rp = http_json_get(session_url, rq); + rp = ws_execute(rq); json_object_put(rq); json_object_put(rp); diff --git a/src/ttrss.h b/src/ttrss.h index 71b3cef..d23c6e2 100644 --- a/src/ttrss.h +++ b/src/ttrss.h @@ -24,7 +24,7 @@ void ttrss_set_config(const char *url, const char *user, const char *pwd); struct feed **ttrss_get_feeds(); -struct headline **ttrss_get_headlines(struct feed *); +struct headline **ttrss_feed_get_headlines(struct feed *); const char *ttrss_get_headline_content(struct headline *); void ttrss_set_article_unread(int id, int unread); diff --git a/src/ttrss_model.c b/src/ttrss_model.c index c65a710..2c80c9e 100644 --- a/src/ttrss_model.c +++ b/src/ttrss_model.c @@ -100,6 +100,7 @@ struct headline *headline_new(int id, const char *url, const char *title) h->id = id; h->url = strdup(url); h->title = strdup(title); + h->unread = -1; h->excerpt = NULL; h->content = NULL; @@ -120,6 +121,21 @@ void headlines_free(struct headline **headlines) } } +struct feed *feed_new(int id, const char *url, const char *title) +{ + struct feed *f; + + f = malloc(sizeof(struct feed)); + + f->id = id; + f->url = strdup(url); + f->title = strdup(title); + f->unread = -1; + f->headlines = NULL; + + return f; +} + void feed_free(struct feed *feed) { if (feed) { diff --git a/src/ttrss_model.h b/src/ttrss_model.h index 6f1a4c1..338d941 100644 --- a/src/ttrss_model.h +++ b/src/ttrss_model.h @@ -39,6 +39,7 @@ struct feed { struct headline **headlines; }; +struct feed *feed_new(int id, const char *url, const char *title); void feed_free(struct feed *feed); void feeds_free(struct feed **feed);