4ac5fdf2d4a2ad820d8ea40715dd3ab0a00c3518
[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         cache_dir = get_cache_dir();
69         if (cache_dir) {
70                 path = g_strdup_printf("%s/%d", cache_dir, h->id);
71                 
72                 content = file_get_content(path);
73                 
74                 if (content)
75                         file_set_content(path, content);
76                 else
77                         content = ws_get_article_content(h->id);
78                 
79                 g_free(path);
80
81                 return content;
82         } 
83
84         return NULL;
85 }
86
87 struct feed **ttrss_get_feeds()
88 {
89         data = ws_update_feeds(data);
90
91         return data;
92 }
93
94 struct headline **ttrss_feed_get_headlines(struct feed *f)
95 {
96         if (!f->headlines)
97                 ws_update_headlines(f);
98
99         return f->headlines;
100 }
101
102 void ttrss_set_article_unread(int id, int unread)
103 {
104         struct json_object *rp, *rq;
105
106         printf("ttrss_set_article_unread %d %d\n", id, unread);
107
108         rq = ws_request_new("updateArticle");
109         json_object_object_add(rq, "article_ids", json_object_new_int(id));
110         json_object_object_add(rq, "field", json_object_new_int(2));
111         json_object_object_add(rq, "mode", json_object_new_int(unread));
112
113         rp = ws_execute(rq);
114
115         json_object_put(rq);
116         json_object_put(rp);
117 }
118
119 void ttrss_set_config(const char *url, const char *user, const char *pwd)
120 {
121         feeds_free(data);
122         data = NULL;
123         ws_init(url, user, pwd);
124 }
125
126 struct feed *ttrss_get_feed(int id)
127 {
128         return feeds_get_feed(data, id);
129 }
130
131 struct headline *ttrss_get_headline(int id)
132 {
133         return feeds_get_headline(data, id);
134 }