(no commit message)
[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
24 #include <glib.h>
25 #include <json/json.h>
26
27 #include "http.h"
28 #include "io.h"
29 #include "log.h"
30 #include "ttrss_ws.h"
31 #include "url.h"
32
33 static struct feed **data;
34 static char *cache_dir;
35
36 static const char *get_cache_dir()
37 {
38         char *home;
39
40         if (!cache_dir) {
41                 home = getenv("HOME");
42
43                 if (!home)
44                         return NULL;
45
46                 cache_dir = path_append(home, ".prss/cache");
47                 mkdirs(cache_dir, 0777);
48         }
49
50         return cache_dir;
51 }
52
53 static void file_set_content(const char *path, const char *content)
54 {
55         FILE *fp;
56
57         fp = fopen(path, "w");
58         if (fp) {
59                 fwrite(content, 1, strlen(content), fp);
60                 fclose(fp);
61         }
62 }
63
64 char *ttrss_get_headline_content(struct headline *h)
65 {
66         const char *cache_dir;
67         char *path, *content;
68
69         cache_dir = get_cache_dir();
70         if (cache_dir) {
71                 path = g_strdup_printf("%s/%d", cache_dir, h->id);
72
73                 content = file_get_content(path);
74
75                 if (!content) {
76                         content = ws_get_article_content(h->id);
77                         file_set_content(path, content);
78                 }
79
80                 g_free(path);
81
82                 return content;
83         }
84
85         return NULL;
86 }
87
88 struct feed **ttrss_get_feeds()
89 {
90         data = ws_update_feeds(data);
91
92         return data;
93 }
94
95 struct headline **ttrss_feed_get_headlines(struct feed *f)
96 {
97         if (!f->headlines)
98                 ws_update_headlines(f);
99
100         return f->headlines;
101 }
102
103 void ttrss_set_article_unread(int id, int unread)
104 {
105         log_debug("ttrss_set_article_unread(%d,%d)", id, unread);
106
107         ws_set_article_unread(id, unread);
108
109         log_debug("ttrss_set_article_unread(%d,%d)", id, unread);
110 }
111
112 void ttrss_set_config(const char *url, const char *user, const char *pwd)
113 {
114         feeds_free(data);
115         data = NULL;
116         ws_init(url, user, pwd);
117 }
118
119 struct feed *ttrss_get_feed(int id)
120 {
121         return feeds_get_feed(data, id);
122 }
123
124 struct headline *ttrss_get_headline(int id)
125 {
126         return feeds_get_headline(data, id);
127 }