(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 "list.h"
25 #include "ttrss_model.h"
26
27 struct headline **headlines_add(struct headline **list, struct headline *h)
28 {
29         return (struct headline **)list_add((void **)list, (void *)h);
30 }
31
32 struct feed **feeds_add(struct feed **list, struct feed *f)
33 {
34         return (struct feed **)list_add((void **)list, (void *)f);
35 }
36
37 struct headline *feed_get_headline(struct feed *feed, int id)
38 {
39         struct headline **headlines;
40
41         headlines = feed->headlines;
42         if (headlines)
43                 while (*headlines) {
44                         if ((*headlines)->id == id)
45                                 return *headlines;
46                         headlines++;
47                 }
48
49         return NULL;
50 }
51
52 void headline_free(struct headline *headline)
53 {
54         if (headline) {
55                 free(headline->url);
56                 free(headline->title);
57                 free(headline->excerpt);
58                 free(headline->content);
59                 free(headline);
60         }
61 }
62
63 struct headline *headline_new(int id, const char *url, const char *title)
64 {
65         struct headline *h;
66
67         h = malloc(sizeof(struct headline));
68         h->id = id;
69         h->url = strdup(url);
70         h->title = strdup(title);
71         h->unread = -1;
72         h->excerpt = NULL;
73         h->content = NULL;
74
75         return h;
76 }
77
78 void headlines_free(struct headline **headlines)
79 {
80         struct headline **cur;
81
82         if (headlines) {
83                 cur = headlines;
84                 while (*cur) {
85                         headline_free(*cur);
86                         cur++;
87                 }
88                 free(headlines);
89         }
90 }
91
92 struct feed *feed_new(int id, const char *url, const char *title)
93 {
94         struct feed *f;
95
96         f = malloc(sizeof(struct feed));
97
98         f->id = id;
99         f->url = strdup(url);
100         f->title = strdup(title);
101         f->unread = -1;
102         f->headlines = NULL;
103
104         return f;
105 }
106
107 void feed_free(struct feed *feed)
108 {
109         if (feed) {
110                 if (feed->title)
111                         free(feed->title);
112                 if (feed->url)
113                         free(feed->url);
114                 headlines_free(feed->headlines);
115                 free(feed);
116         }
117 }
118
119 void feeds_free(struct feed **feeds)
120 {
121         struct feed **cur;
122
123         if (feeds) {
124                 cur = feeds;
125                 while (*cur) {
126                         feed_free(*cur);
127                         cur++;
128                 }
129                 free(feeds);
130         }
131 }
132
133 struct feed *feeds_get_feed(struct feed **feeds, int id)
134 {
135         struct feed **cur;
136
137         if (feeds)
138                 for (cur = feeds; *cur; cur++)
139                         if ((*cur)->id == id)
140                                 return *cur;
141
142         return NULL;
143 }
144
145 struct headline *feeds_get_headline(struct feed **feeds, int id)
146 {
147         struct headline *h;
148
149         if (!feeds)
150                 return NULL;
151
152         while (*feeds) {
153                 h = feed_get_headline(*feeds, id);
154
155                 if (h)
156                         return h;
157
158                 feeds++;
159         }
160
161         return NULL;
162 }