(no commit message)
[prss.git] / src / ttrss.c
index f955cee..1f97155 100644 (file)
@@ -31,60 +31,7 @@ static char *session_url;
 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 **
-headlines_add(struct headline **list, struct headline *h)
-{
-       return (struct headline **)list_add((void **)list, (void *)h);
-}
-
-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;
-}
+static struct feed **data;
 
 void ws_request_add_att_str(json_object *rq, const char *k, const char *str)
 {
@@ -241,22 +188,21 @@ char *ws_get_article_content(int id)
 
        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);
        }
 
@@ -268,6 +214,7 @@ 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);
@@ -284,31 +231,22 @@ int ws_update_headlines(struct feed *feed)
                        j = json_object_object_get(jheadline, "id");
                        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;
-                               h->title = NULL;
-                               h->url = NULL;
+                               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;
                        }
 
-                       if (!h->title) {
-                               j = json_object_object_get(jheadline, "title");
-                               h->title = strdup(json_object_get_string(j));
-                       }
-
-                       if (!h->url) {
-                               j = json_object_object_get(jheadline, "link");
-                               h->url = strdup(json_object_get_string(j));
-                       }
-
                        j = json_object_object_get(jheadline, "unread");
                        h->unread = json_object_get_boolean(j);
                }
@@ -329,7 +267,6 @@ const char *ttrss_get_headline_content(struct headline *h)
        return h->content;
 }
 
-
 struct feed **ttrss_get_feeds()
 {
        struct json_object *rp, *rq, *content, *jfeed, *j;
@@ -368,7 +305,7 @@ struct feed **ttrss_get_feeds()
 
                        feed->headlines = NULL;
 
-                       tmp = (struct feed **)list_add((void **)feeds, feed);
+                       tmp = feeds_add(feeds, feed);
                        free(feeds);
                        feeds = tmp;
                }
@@ -403,7 +340,14 @@ void ttrss_set_article_unread(int id, int unread)
        json_object_object_add(rq, "mode", json_object_new_int(unread));
 
        rp = http_json_get(session_url, rq);
-       
+
        json_object_put(rq);
        json_object_put(rp);
 }
+
+void ttrss_set_config(const char *url, const char *user, const char *pwd)
+{
+       feeds_free(data);
+       data = NULL;
+       ws_init(url, user, pwd);
+}