1 /*
2 * libEtPan! -- a mail stuff library
3 *
4 * Copyright (C) 2001, 2005 - DINH Viet Hoa
5 * Copyright (C) 2006 Andrej Kacian <andrej@kacian.sk>
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the libEtPan! project nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33 #include "newsfeed_item.h"
34
35 #include "newsfeed.h"
36 #include "newsfeed_item_enclosure.h"
37
38 #include <stdlib.h>
39 #include <string.h>
40
41 /* newsfeed_item_new()
42 * Initializes a new empty newsfeed_item struct, setting its parent feed,
43 * if supplied. */
newsfeed_item_new(struct newsfeed * feed)44 struct newsfeed_item * newsfeed_item_new(struct newsfeed * feed)
45 {
46 struct newsfeed_item * item;
47
48 item = malloc(sizeof(* item));
49 if (item == NULL)
50 return NULL;
51
52 item->fi_url = NULL;
53 item->fi_title = NULL;
54 item->fi_summary = NULL;
55 item->fi_text = NULL;
56 item->fi_author = NULL;
57 item->fi_id = NULL;
58
59 item->fi_date_published = 0;
60 item->fi_date_modified = 0;
61 item->fi_enclosure = NULL;
62
63 item->fi_feed = feed;
64
65 return item;
66 }
67
newsfeed_item_free(struct newsfeed_item * item)68 void newsfeed_item_free(struct newsfeed_item * item)
69 {
70 if (item->fi_enclosure != NULL)
71 newsfeed_item_enclosure_free(item->fi_enclosure);
72 free(item->fi_id);
73 free(item->fi_author);
74 free(item->fi_text);
75 free(item->fi_summary);
76 free(item->fi_title);
77 free(item->fi_url);
78
79 free(item);
80 }
81
newsfeed_item_get_feed(struct newsfeed_item * item)82 struct newsfeed * newsfeed_item_get_feed(struct newsfeed_item * item)
83 {
84 return item->fi_feed;
85 }
86
newsfeed_item_get_url(struct newsfeed_item * item)87 const char * newsfeed_item_get_url(struct newsfeed_item * item)
88 {
89 return item->fi_url;
90 }
91
newsfeed_item_set_url(struct newsfeed_item * item,const char * url)92 int newsfeed_item_set_url(struct newsfeed_item *item, const char * url)
93 {
94 if (url != item->fi_url) {
95 char * dup_url;
96
97 if (url == NULL) {
98 dup_url = NULL;
99 }
100 else {
101 dup_url = strdup(url);
102 if (dup_url == NULL)
103 return -1;
104 }
105
106 free(item->fi_url);
107 item->fi_url = dup_url;
108 }
109
110 return 0;
111 }
112
newsfeed_item_get_title(struct newsfeed_item * item)113 const char * newsfeed_item_get_title(struct newsfeed_item * item)
114 {
115 return item->fi_title;
116 }
117
newsfeed_item_set_title(struct newsfeed_item * item,const char * title)118 int newsfeed_item_set_title(struct newsfeed_item * item, const char * title)
119 {
120 if (title != item->fi_title) {
121 char * dup_title;
122
123 if (title == NULL) {
124 dup_title = NULL;
125 }
126 else {
127 dup_title = strdup(title);
128 if (dup_title == NULL)
129 return -1;
130 }
131
132 free(item->fi_title);
133 item->fi_title = dup_title;
134 }
135
136 return 0;
137 }
138
newsfeed_item_get_summary(struct newsfeed_item * item)139 const char * newsfeed_item_get_summary(struct newsfeed_item * item)
140 {
141 return item->fi_summary;
142 }
143
newsfeed_item_set_summary(struct newsfeed_item * item,const char * summary)144 int newsfeed_item_set_summary(struct newsfeed_item * item, const char * summary)
145 {
146 if (summary != item->fi_summary) {
147 char * dup_summary;
148
149 if (summary == NULL) {
150 dup_summary = NULL;
151 }
152 else {
153 dup_summary = strdup(summary);
154 if (dup_summary == NULL)
155 return -1;
156 }
157
158 free(item->fi_summary);
159 item->fi_summary = dup_summary;
160 }
161
162 return 0;
163 }
164
newsfeed_item_get_text(struct newsfeed_item * item)165 const char * newsfeed_item_get_text(struct newsfeed_item * item)
166 {
167 return item->fi_text;
168 }
169
newsfeed_item_set_text(struct newsfeed_item * item,const char * text)170 int newsfeed_item_set_text(struct newsfeed_item * item, const char * text)
171 {
172 if (text != item->fi_text) {
173 char * dup_text;
174
175 if (text == NULL) {
176 dup_text = NULL;
177 }
178 else {
179 dup_text = strdup(text);
180 if (dup_text == NULL)
181 return -1;
182 }
183
184 free(item->fi_text);
185 item->fi_text = dup_text;
186 }
187
188 return 0;
189 }
190
newsfeed_item_get_author(struct newsfeed_item * item)191 const char * newsfeed_item_get_author(struct newsfeed_item * item)
192 {
193 return item->fi_author;
194 }
195
newsfeed_item_set_author(struct newsfeed_item * item,const char * author)196 int newsfeed_item_set_author(struct newsfeed_item * item, const char * author)
197 {
198 if (author != item->fi_author) {
199 char * dup_author;
200
201 if (author == NULL) {
202 dup_author = NULL;
203 }
204 else {
205 dup_author = strdup(author);
206 if (dup_author == NULL)
207 return -1;
208 }
209
210 free(item->fi_author);
211 item->fi_author = dup_author;
212 }
213
214 return 0;
215 }
216
newsfeed_item_get_id(struct newsfeed_item * item)217 const char * newsfeed_item_get_id(struct newsfeed_item * item)
218 {
219 return item->fi_id;
220 }
221
newsfeed_item_set_id(struct newsfeed_item * item,const char * id)222 int newsfeed_item_set_id(struct newsfeed_item * item, const char * id)
223 {
224 if (id != item->fi_id) {
225 char * dup_id;
226
227 if (id == NULL) {
228 dup_id = NULL;
229 }
230 else {
231 dup_id = strdup(id);
232 if (dup_id == NULL)
233 return -1;
234 }
235
236 free(item->fi_id);
237 item->fi_id = dup_id;
238 }
239
240 return 0;
241 }
242
newsfeed_item_get_date_published(struct newsfeed_item * item)243 time_t newsfeed_item_get_date_published(struct newsfeed_item * item)
244 {
245 return item->fi_date_published;
246 }
247
newsfeed_item_set_date_published(struct newsfeed_item * item,time_t date)248 void newsfeed_item_set_date_published(struct newsfeed_item * item, time_t date)
249 {
250 item->fi_date_published = date;
251 }
252
newsfeed_item_get_date_modified(struct newsfeed_item * item)253 time_t newsfeed_item_get_date_modified(struct newsfeed_item * item)
254 {
255 return item->fi_date_modified;
256 }
257
newsfeed_item_set_date_modified(struct newsfeed_item * item,time_t date)258 void newsfeed_item_set_date_modified(struct newsfeed_item * item, time_t date)
259 {
260 item->fi_date_modified = date;
261 }
262
newsfeed_item_get_enclosure(struct newsfeed_item * item)263 struct newsfeed_item_enclosure * newsfeed_item_get_enclosure(struct newsfeed_item * item)
264 {
265 return item->fi_enclosure;
266 }
267
newsfeed_item_set_enclosure(struct newsfeed_item * item,struct newsfeed_item_enclosure * enclosure)268 void newsfeed_item_set_enclosure(struct newsfeed_item * item,
269 struct newsfeed_item_enclosure * enclosure)
270 {
271 item->fi_enclosure = enclosure;
272 }
273