(no commit message)
authorJean-Philippe Orsini <jeanfi@gmail.com>
Wed, 24 Apr 2013 12:24:58 +0000 (12:24 +0000)
committerJean-Philippe Orsini <jeanfi@gmail.com>
Wed, 24 Apr 2013 12:24:58 +0000 (12:24 +0000)
src/ttrss.c
src/ttrss.h

index 97f2803..3649b07 100644 (file)
@@ -31,6 +31,56 @@ static char *session_url;
 static char *session_user;
 static char *session_pwd;
 
 static char *session_user;
 static char *session_pwd;
 
+static int list_length(void **list)
+{
+       int n;
+
+       if (!list)
+               return 0;
+       
+       n = 0;
+       while(*list) {
+               n++;
+               list++;
+       }
+
+       return n;
+}
+
+static void **list_add(void **list, void *item)
+{
+       int n;
+       void **result;
+
+       n = list_length(list);
+
+       result = malloc((n + 1 + 1) * sizeof(void *));
+
+       if (list)
+               memcpy(result, list, n * sizeof(void *));
+
+       result[n] = item;
+       result[n + 1] = NULL;
+
+       return result;
+}
+
+
+struct headline *feed_get_headline(struct feed *feed, int id)
+{
+       struct headline **headlines;
+
+       headlines = feed->headlines;
+       if (headlines)
+               while (*headlines) {
+                       if ((*headlines)->id == id)
+                               return *headlines;
+                       headlines++;
+               }
+
+       return NULL;
+}
+
 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_str(json_object *rq, const char *k, const char *str)
 {
        json_object_object_add(rq, k, json_object_new_string(str));
@@ -179,68 +229,68 @@ int ws_open_session()
        return result;
 }
 
        return result;
 }
 
-const char *ttrss_get_headline_content(struct headline *h)
+char *ws_get_article_content(int id)
 {
 {
-       struct json_object *rp, *rq, *content, *array, *item;
-
-       printf("get_headlines %d\n", h->id);
-
-       if (!h->content) {
-               rq = ws_request_new("getArticle");
-               json_object_object_add(rq, "article_id",
-                                      json_object_new_int(h->id));
-               
-               rp = http_json_get(session_url, rq);
-               
-               json_object_put(rq);
-               
-               array = json_object_object_get(rp, "content");
-
-               if (!array)
-                       goto release;
-
-               item = json_object_array_get_idx(array, 0);
+       struct json_object *rp, *rq, *content, *item;
+       char *str;
 
 
-               if (!item)
-                       goto release;
+       rq = ws_request_new("getArticle");
+       ws_request_add_att_int(rq, "article_id", id);
+       
+       rp = ws_execute(rq);
+       
+       json_object_put(rq);
 
 
-               content = json_object_object_get(item, "content");
+       str = NULL;
 
 
-               h->content = strdup(json_object_get_string(content));
+       if (rp) {
+               item = json_object_array_get_idx(rp, 0);
+               
+               if (item) {
+                       content = json_object_object_get(item, "content");
                        
                        
-       release:
+                       str = strdup(json_object_get_string(content));
+               }
+               
                json_object_put(rp);
        }
                json_object_put(rp);
        }
-       return h->content;
+
+       return str;
 }
 
 }
 
-static struct headline **get_headlines(int feed_id)
+int ws_update_headlines(struct feed *feed)
 {
 {
-       struct json_object *rp, *rq, *content, *jheadline, *j;
-       int i, n;
-       struct headline **headlines, *h;
-
-       printf("get_headlines %d\n", feed_id);
+       struct json_object *rp, *rq, *jheadline, *j;
+       int i, n, err, hid;
+       struct headline *h, **tmp;
 
        rq = ws_request_new("getHeadlines");
 
        rq = ws_request_new("getHeadlines");
-       json_object_object_add(rq, "feed_id", json_object_new_int(feed_id));
+       ws_request_add_att_int(rq, "feed_id", feed->id);
 
 
-       rp = http_json_get(session_url, rq);
+       rp = ws_execute(rq);
 
        json_object_put(rq);
 
 
        json_object_put(rq);
 
-       content = json_object_object_get(rp, "content");
-
-       if (content) {
-               n = json_object_array_length(content);
-               headlines = malloc((n+1)*sizeof(struct headline *));
+       if (rp) {
+               n = json_object_array_length(rp);
                for (i = 0; i < n; i++) {
                for (i = 0; i < n; i++) {
-                       jheadline = json_object_array_get_idx(content, i);
-
-                       h = malloc(sizeof(struct headline));
+                       jheadline = json_object_array_get_idx(rp, i);
 
                        j = json_object_object_get(jheadline, "id");
 
                        j = json_object_object_get(jheadline, "id");
-                       h->id = json_object_get_int(j);
+                       hid = json_object_get_int(j);
+                       h = feed_get_headline(feed, hid);
+                       
+                       if (!h) {
+                               h = malloc(sizeof(struct headline));
+                               h->id = hid;
+                               h->excerpt = NULL;
+                               h->content = NULL;
+                               
+                               tmp = (struct headline **)list_add((void **)feed->headlines, h);
+                               if (feed->headlines)
+                                       free(feed->headlines);
+                               feed->headlines = tmp;
+                       }
 
                        j = json_object_object_get(jheadline, "title");
                        h->title = strdup(json_object_get_string(j));
 
                        j = json_object_object_get(jheadline, "title");
                        h->title = strdup(json_object_get_string(j));
@@ -248,59 +298,26 @@ static struct headline **get_headlines(int feed_id)
                        j = json_object_object_get(jheadline, "link");
                        h->url = strdup(json_object_get_string(j));
 
                        j = json_object_object_get(jheadline, "link");
                        h->url = strdup(json_object_get_string(j));
 
-                       h->excerpt = NULL;
-                       h->content = NULL;
-
                        j = json_object_object_get(jheadline, "unread");
                        h->unread = json_object_get_boolean(j);
                        j = json_object_object_get(jheadline, "unread");
                        h->unread = json_object_get_boolean(j);
-
-                       headlines[i] = h;
                }
                }
-               headlines[n] = NULL;
+               err = 0;
        } else {
        } else {
-               headlines = NULL;
+               err = 1;
        }
 
        json_object_put(rp);
        }
 
        json_object_put(rp);
-
-       printf("get_headlines %d end\n", feed_id);
-
-       return headlines;
+       return !err;
 }
 
 }
 
-static int feeds_length(struct feed **list)
+const char *ttrss_get_headline_content(struct headline *h)
 {
 {
-       int n;
+       if (!h->content)
+               h->content = ws_get_article_content(h->id);
 
 
-       if (!list)
-               return 0;
-       
-       n = 0;
-       while(*list) {
-               n++;
-               list++;
-       }
-
-       return n;
+       return h->content;
 }
 
 }
 
-static struct feed **feeds_add(struct feed **feeds, struct feed *feed)
-{
-       int n;
-       struct feed **result;
-
-       n = feeds_length(feeds);
-
-       result = malloc((n + 1 + 1) * sizeof(struct feed *));
-
-       if (feeds)
-               memcpy(result, feeds, n * sizeof(struct feed *));
-
-       result[n] = feed;
-       result[n + 1] = NULL;
-
-       return result;
-}
 
 struct feed **ttrss_get_feeds()
 {
 
 struct feed **ttrss_get_feeds()
 {
@@ -340,7 +357,7 @@ struct feed **ttrss_get_feeds()
 
                        feed->headlines = NULL;
 
 
                        feed->headlines = NULL;
 
-                       tmp = feeds_add(feeds, feed);
+                       tmp = (struct feed **)list_add((void **)feeds, feed);
                        free(feeds);
                        feeds = tmp;
                }
                        free(feeds);
                        feeds = tmp;
                }
@@ -358,7 +375,7 @@ struct feed **ttrss_get_feeds()
 struct headline **ttrss_get_headlines(struct feed *f)
 {
        if (!f->headlines)
 struct headline **ttrss_get_headlines(struct feed *f)
 {
        if (!f->headlines)
-               f->headlines = get_headlines(f->id);
+               ws_update_headlines(f);
 
        return f->headlines;
 }
 
        return f->headlines;
 }
index f200e59..78427eb 100644 (file)
@@ -42,10 +42,10 @@ struct feed {
 struct feed **ttrss_get_feeds();
 struct headline **ttrss_get_headlines(struct feed *);
 const char *ttrss_get_headline_content(struct headline *);
 struct feed **ttrss_get_feeds();
 struct headline **ttrss_get_headlines(struct feed *);
 const char *ttrss_get_headline_content(struct headline *);
-
 void ttrss_set_article_unread(int id, int unread);
 
 void ws_init(const char *url, const char *user, const char *pwd);
 int ws_open_session();
 
 void ttrss_set_article_unread(int id, int unread);
 
 void ws_init(const char *url, const char *user, const char *pwd);
 int ws_open_session();
 
+
 #endif
 #endif