(no commit message)
[prss.git] / src / ttrss_model.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 <stdlib.h>
21 #include <stdio.h>
22 #include <string.h>
23
24 #include "ttrss_model.h"
25
26 static int list_length(void **list)
27 {
28         int n;
29
30         if (!list)
31                 return 0;
32
33         n = 0;
34         while (*list) {
35                 n++;
36                 list++;
37         }
38
39         return n;
40 }
41
42 static void **list_add(void **list, void *item)
43 {
44         int n;
45         void **result;
46
47         n = list_length(list);
48
49         result = malloc((n + 1 + 1) * sizeof(void *));
50
51         if (list)
52                 memcpy(result, list, n * sizeof(void *));
53
54         result[n] = item;
55         result[n + 1] = NULL;
56
57         return result;
58 }
59
60 struct headline **headlines_add(struct headline **list, struct headline *h)
61 {
62         return (struct headline **)list_add((void **)list, (void *)h);
63 }
64
65 struct feed **feeds_add(struct feed **list, struct feed *f)
66 {
67         return (struct feed **)list_add((void **)list, (void *)f);
68 }
69
70 struct headline *feed_get_headline(struct feed *feed, int id)
71 {
72         struct headline **headlines;
73
74         headlines = feed->headlines;
75         if (headlines)
76                 while (*headlines) {
77                         if ((*headlines)->id == id)
78                                 return *headlines;
79                         headlines++;
80                 }
81
82         return NULL;
83 }
84
85 void headline_free(struct headline *headline)
86 {
87         if (headline) {
88                 free(headline->url);
89                 free(headline->title);
90                 free(headline->excerpt);
91                 free(headline->content);
92                 free(headline);
93         }
94 }
95
96 struct headline *headline_new(int id, const char *url, const char *title)
97 {
98         struct headline *h;
99
100         h = malloc(sizeof(struct headline));
101         h->id = id;
102         h->url = strdup(url);
103         h->title = strdup(title);
104         h->unread = -1;
105         h->excerpt = NULL;
106         h->content = NULL;
107
108         return h;
109 }
110
111 void headlines_free(struct headline **headlines)
112 {
113         struct headline **cur;
114
115         if (headlines) {
116                 cur = headlines;
117                 while (*cur) {
118                         headline_free(*cur);
119                         cur++;
120                 }
121                 free(headlines);
122         }
123 }
124
125 struct feed *feed_new(int id, const char *url, const char *title)
126 {
127         struct feed *f;
128
129         f = malloc(sizeof(struct feed));
130
131         f->id = id;
132         f->url = strdup(url);
133         f->title = strdup(title);
134         f->unread = -1;
135         f->headlines = NULL;
136
137         return f;
138 }
139
140 void feed_free(struct feed *feed)
141 {
142         if (feed) {
143                 if (feed->title)
144                         free(feed->title);
145                 if (feed->url)
146                         free(feed->url);
147                 headlines_free(feed->headlines);
148                 free(feed);
149         }
150 }
151
152 void feeds_free(struct feed **feeds)
153 {
154         struct feed **cur;
155
156         if (feeds) {
157                 cur = feeds;
158                 while (*cur) {
159                         feed_free(*cur);
160                         cur++;
161                 }
162                 free(feeds);
163         }
164 }
165
166 struct feed *feeds_get_feed(struct feed **feeds, int id)
167 {
168         struct feed **cur;
169
170         if (feeds)
171                 for (cur = feeds; *cur; cur++)
172                         if ((*cur)->id == id)
173                                 return *cur;
174
175         return NULL;
176 }
177
178 struct headline *feeds_get_headline(struct feed **feeds, int id)
179 {
180         struct headline *h;
181
182         if (!feeds)
183                 return NULL;
184
185         while (*feeds) {
186                 h = feed_get_headline(*feeds, id);
187
188                 if (h)
189                         return h;
190
191                 feeds++;
192         }
193
194         return NULL;
195 }