2 * Copyright (C) 2010-2013 jeanfi@gmail.com
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.
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.
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
23 #include <sys/types.h>
29 #include <json/json.h>
34 #include "ttrss_wsasync.h"
37 static struct feed **data;
38 static char *cache_dir;
40 static const char *get_cache_dir()
45 home = getenv("HOME");
50 cache_dir = path_append(home, ".prss/cache");
51 mkdirs(cache_dir, 0777);
57 static void file_set_content(const char *path, const char *content)
61 log_debug("file_set_content(): path=%s", path);
63 fp = fopen(path, "w");
65 fwrite(content, 1, strlen(content), fp);
70 static gchar *content_get_path(const struct headline *h)
72 const char *cache_dir;
74 cache_dir = get_cache_dir();
76 return g_strdup_printf("%s/%d", cache_dir, h->id);
81 static int is_content_cached(const struct headline *h)
87 path = content_get_path(h);
89 if (stat(path, &s) == -1)
99 char *ttrss_get_headline_content(struct headline *h)
101 char *path, *content;
103 path = content_get_path(h);
105 content = file_get_content(path);
110 content = ws_get_article_content(h->id);
111 file_set_content(path, content);
119 struct feed **ttrss_get_feeds()
121 data = ws_update_feeds(data);
126 struct headline **ttrss_feed_get_headlines(struct feed *f)
129 ws_update_headlines(f);
134 void ttrss_set_article_unread(int id, int unread)
136 log_debug("ttrss_set_article_unread(%d,%d)", id, unread);
138 ws_async_set_article_unread(id, unread);
140 log_debug("ttrss_set_article_unread(%d,%d)", id, unread);
143 void ttrss_set_config(const char *url, const char *user, const char *pwd)
147 ws_set_config(url, user, pwd);
150 struct feed *ttrss_get_feed(int id)
152 return feeds_get_feed(data, id);
155 struct headline *ttrss_get_headline(int id)
157 return feeds_get_headline(data, id);
160 void ttrs_download_headline_content(struct feed **feeds)
163 struct headline **hcur;
165 for (fcur = feeds; *fcur; fcur++) {
166 hcur = ttrss_feed_get_headlines(*fcur);
168 while (hcur && *hcur) {
169 if (!is_content_cached(*hcur))
170 free(ttrss_get_headline_content(*hcur));