164dcdd4818a91e5554b3f33546a9cf45f6e7086
[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 "ttrss_ws.h"
30 #include "url.h"
31
32 static struct feed **data;
33 static char *cache_dir;
34
35 static const char *get_cache_dir()
36 {
37         char *home;
38
39         if (!cache_dir) {
40                 home = getenv("HOME");
41
42                 if (!home)
43                         return NULL;
44
45                 cache_dir = path_append(home, ".prss/cache");
46                 mkdirs(cache_dir, 0777);
47         }
48
49         return cache_dir;
50 }
51
52 static void file_set_content(const char *path, const char *content)
53 {
54         FILE *fp;
55
56         fp = fopen(path, "w");
57         if (fp) {
58                 fwrite(content, 1, strlen(content), fp);
59                 fclose(fp);
60         }
61 }
62
63 const char *ttrss_get_headline_content(struct headline *h)
64 {
65         const char *cache_dir;
66         char *path, *content;
67
68         if (!h->content) {
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                                 h->content = content;
77                         } else {
78                                 h->content = ws_get_article_content(h->id);
79
80                                 if (h->content)
81                                         file_set_content(path, h->content);
82                         }
83
84                         g_free(path);
85                 }
86         }
87
88         return h->content;
89 }
90
91 struct feed **ttrss_get_feeds()
92 {
93         data = ws_update_feeds(data);
94
95         return data;
96 }
97
98 struct headline **ttrss_feed_get_headlines(struct feed *f)
99 {
100         if (!f->headlines)
101                 ws_update_headlines(f);
102
103         return f->headlines;
104 }
105
106 void ttrss_set_article_unread(int id, int unread)
107 {
108         struct json_object *rp, *rq;
109
110         printf("ttrss_set_article_unread %d %d\n", id, unread);
111
112         rq = ws_request_new("updateArticle");
113         json_object_object_add(rq, "article_ids", json_object_new_int(id));
114         json_object_object_add(rq, "field", json_object_new_int(2));
115         json_object_object_add(rq, "mode", json_object_new_int(unread));
116
117         rp = ws_execute(rq);
118
119         json_object_put(rq);
120         json_object_put(rp);
121 }
122
123 void ttrss_set_config(const char *url, const char *user, const char *pwd)
124 {
125         feeds_free(data);
126         data = NULL;
127         ws_init(url, user, pwd);
128 }
129
130 struct feed *ttrss_get_feed(int id)
131 {
132         return feeds_get_feed(data, id);
133 }
134
135 struct headline *ttrss_get_headline(int id)
136 {
137         return feeds_get_headline(data, id);
138 }