use status bar widget
[prss.git] / src / ttrss.c
1 /*
2  * Copyright (C) 2010-2013 jeanfi@gmail.com
3  *
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.
8  *
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.
13  *
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
17  * 02110-1301 USA
18  */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <unistd.h>
26
27 #include <glib.h>
28 #include <json/json.h>
29
30 #include "http.h"
31 #include "io.h"
32 #include "log.h"
33 #include "ttrss_cache.h"
34 #include "ttrss_wsasync.h"
35 #include "url.h"
36
37 static struct feed **data;
38 char *ttrss_get_headline_content(struct headline *h)
39 {
40         char *content;
41
42         content = cache_get(h);
43
44         if (content) {
45                 log_debug("ttrss_get_headline_content: cache hit");
46         } else {
47                 log_debug("ttrss_get_headline_content: cache miss");
48                 content = ws_get_article_content(h->id);
49                 cache_put(h->id, content);
50         }
51
52         return content;
53 }
54
55 struct feed **ttrss_get_feeds()
56 {
57         data = ws_update_feeds(data);
58
59         return data;
60 }
61
62 struct headline **ttrss_feed_get_headlines(struct feed *f)
63 {
64         if (!f->headlines)
65                 ws_update_headlines(f);
66
67         return f->headlines;
68 }
69
70 void ttrss_set_article_unread(int id, int unread)
71 {
72         log_debug("ttrss_set_article_unread(%d,%d)", id, unread);
73
74         ws_async_set_article_unread(id, unread);
75
76         log_debug("ttrss_set_article_unread(%d,%d)", id, unread);
77 }
78
79 void ttrss_set_config(const char *url, const char *user, const char *pwd)
80 {
81         feeds_free(data);
82         data = NULL;
83         ws_set_config(url, user, pwd);
84 }
85
86 struct feed *ttrss_get_feed(int id)
87 {
88         return feeds_get_feed(data, id);
89 }
90
91 struct headline *ttrss_get_headline(int id)
92 {
93         return feeds_get_headline(data, id);
94 }
95
96 static void get_article_content_cbk(int id, const char *content)
97 {
98         printf("get_article_content_cbk %d\n", id);
99
100         if (content)
101                 cache_put(id, content);
102 }
103
104 void ttrs_download_headline_content(struct feed **feeds)
105 {
106         struct feed **fcur;
107         struct headline **hcur;
108
109         log_debug("ttrs_download_headline_content");
110
111         for (fcur = feeds; *fcur; fcur++) {
112                 hcur = ttrss_feed_get_headlines(*fcur);
113
114                 log_debug("ttrs_download_headline_content(): %s",
115                           (*fcur)->title);
116
117                 while (hcur && *hcur) {
118                         if (!cache_exists(*hcur)) {
119                                 ws_async_get_article_content
120                                         ((*hcur)->id, get_article_content_cbk);
121                         }
122                         hcur++;
123                 }
124         }
125 }