b3b555ed78dcb1d33543979ddf900a480303f98c
[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
28 #include <glib.h>
29 #include <json/json.h>
30
31 #include "http.h"
32 #include "io.h"
33 #include "log.h"
34 #include "ttrss_wsasync.h"
35 #include "url.h"
36
37 static struct feed **data;
38 static char *cache_dir;
39
40 static const char *get_cache_dir()
41 {
42         char *home;
43
44         if (!cache_dir) {
45                 home = getenv("HOME");
46
47                 if (!home)
48                         return NULL;
49
50                 cache_dir = path_append(home, ".prss/cache");
51                 mkdirs(cache_dir, 0777);
52         }
53
54         return cache_dir;
55 }
56
57 static void file_set_content(const char *path, const char *content)
58 {
59         FILE *fp;
60
61         log_debug("file_set_content(): path=%s", path);
62
63         fp = fopen(path, "w");
64         if (fp) {
65                 fwrite(content, 1, strlen(content), fp);
66                 fclose(fp);
67         }
68 }
69
70 static char *content_get_path(const struct headline *h)
71 {
72         const char *cache_dir;
73
74         cache_dir = get_cache_dir();
75         if (cache_dir)
76                 return g_strdup_printf("%s/%d", cache_dir, h->id);
77
78         return NULL;
79 }
80
81 static int is_content_cached(const struct headline *h)
82 {
83         struct stat s;
84         char *path;
85         int result;
86
87         path = content_get_path(h);
88
89         if (stat(path, &s) == -1)
90                 result = 0;
91         else
92                 result = 1;
93
94         free(path);
95
96         return result;
97 }
98
99 char *ttrss_get_headline_content(struct headline *h)
100 {
101         char *path, *content;
102
103         path = content_get_path(h);
104         if (path)
105                 content = file_get_content(path);
106         else
107                 content = NULL;
108
109         if (!content) {
110                 content = ws_get_article_content(h->id);
111                 file_set_content(path, content);
112         }
113
114         free(path);
115
116         return content;
117 }
118
119 struct feed **ttrss_get_feeds()
120 {
121         data = ws_update_feeds(data);
122
123         return data;
124 }
125
126 struct headline **ttrss_feed_get_headlines(struct feed *f)
127 {
128         if (!f->headlines)
129                 ws_update_headlines(f);
130
131         return f->headlines;
132 }
133
134 void ttrss_set_article_unread(int id, int unread)
135 {
136         log_debug("ttrss_set_article_unread(%d,%d)", id, unread);
137
138         ws_async_set_article_unread(id, unread);
139
140         log_debug("ttrss_set_article_unread(%d,%d)", id, unread);
141 }
142
143 void ttrss_set_config(const char *url, const char *user, const char *pwd)
144 {
145         feeds_free(data);
146         data = NULL;
147         ws_set_config(url, user, pwd);
148 }
149
150 struct feed *ttrss_get_feed(int id)
151 {
152         return feeds_get_feed(data, id);
153 }
154
155 struct headline *ttrss_get_headline(int id)
156 {
157         return feeds_get_headline(data, id);
158 }
159
160 void ttrs_download_headline_content(struct feed **feeds)
161 {
162         struct feed **fcur;
163         struct headline **hcur;
164
165         for (fcur = feeds; *fcur; fcur++) {
166                 hcur = ttrss_feed_get_headlines(*fcur);
167
168                 while (hcur && *hcur) {
169                         if (!is_content_cached(*hcur))
170                                 free(ttrss_get_headline_content(*hcur));
171                         hcur++;
172                 }
173         }
174 }