Move vi modelines closer to the beginning, so they're more likely to be actually...
[monky] / src / rss.c
1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
2  * vim: ts=4 sw=4 noet ai cindent syntax=c
3  *
4  * Conky, a system monitor, based on torsmo
5  *
6  * Please see COPYING for details
7  *
8  * Copyright (c) 2007 Toni Spets
9  * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
10  *      (see AUTHORS)
11  * All rights reserved.
12  *
13  * This program is free software: you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation, either version 3 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  * You should have received a copy of the GNU General Public License
23  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24  *
25  */
26
27 #include "conky.h"
28 #include "logging.h"
29 #include "prss.h"
30 #include "ccurl_thread.h"
31 #include <time.h>
32 #include <assert.h>
33
34 static ccurl_location_t *locations_head = 0;
35
36 void rss_free_info(void)
37 {
38         ccurl_location_t *tail = locations_head;
39
40         while (tail) {
41                 if (tail->result) prss_free((PRSS*)tail->result);       /* clean up old data */
42                 tail = tail->next;
43         }
44         ccurl_free_locations(&locations_head);
45 }
46
47 void rss_process_info(char *p, int p_max_size, char *uri, char *action, int
48                 act_par, int interval, unsigned int nrspaces)
49 {
50         PRSS *data;
51         char *str;
52
53         ccurl_location_t *curloc = ccurl_find_location(&locations_head, uri);
54
55         assert(act_par >= 0 && action);
56
57         if (!curloc->p_timed_thread) {
58                 curloc->result = malloc(sizeof(PRSS));
59                 memset(curloc->result, 0, sizeof(PRSS));
60                 curloc->process_function = &prss_parse_data;
61                 ccurl_init_thread(curloc, interval);
62                 if (!curloc->p_timed_thread) {
63                         NORM_ERR("error setting up RSS thread");
64                 }
65         }
66
67         timed_thread_lock(curloc->p_timed_thread);
68         data = (PRSS*)curloc->result;
69
70         if (data == NULL || data->item_count < 1) {
71                 *p = 0;
72         } else {
73                 /*
74                  * XXX: Refactor this so that we can retrieve any of the fields in the
75                  * PRSS struct (in prss.h).
76                  */
77                 if (strcmp(action, "feed_title") == EQUAL) {
78                         str = data->title;
79                         if (str && strlen(str) > 0) {
80                                 // remove trailing new line if one exists
81                                 if (str[strlen(str) - 1] == '\n') {
82                                         str[strlen(str) - 1] = 0;
83                                 }
84                                 snprintf(p, p_max_size, "%s", str);
85                         }
86                 } else if (strcmp(action, "item_title") == EQUAL) {
87                         if (act_par < data->item_count) {
88                                 str = data->items[act_par].title;
89                                 // remove trailing new line if one exists
90                                 if (str && strlen(str) > 0) {
91                                         if (str[strlen(str) - 1] == '\n') {
92                                                 str[strlen(str) - 1] = 0;
93                                         }
94                                         snprintf(p, p_max_size, "%s", str);
95                                 }
96                         }
97                 } else if (strcmp(action, "item_desc") == EQUAL) {
98                         if (act_par < data->item_count) {
99                                 str =
100                                         data->items[act_par].description;
101                                 // remove trailing new line if one exists
102                                 if (str && strlen(str) > 0) {
103                                         if (str[strlen(str) - 1] == '\n') {
104                                                 str[strlen(str) - 1] = 0;
105                                         }
106                                         snprintf(p, p_max_size, "%s", str);
107                                 }
108                         }
109                 } else if (strcmp(action, "item_titles") == EQUAL) {
110                         if (data->item_count > 0) {
111                                 int itmp;
112                                 int show;
113                                 //'tmpspaces' is a string with spaces too be placed in front of each title
114                                 char *tmpspaces = malloc(nrspaces + 1);
115                                 memset(tmpspaces, ' ', nrspaces);
116                                 tmpspaces[nrspaces]=0;
117
118                                 if (act_par > data->item_count) {
119                                         show = data->item_count;
120                                 } else {
121                                         show = act_par;
122                                 }
123                                 for (itmp = 0; itmp < show; itmp++) {
124                                         PRSS_Item *item = &data->items[itmp];
125
126                                         str = item->title;
127                                         if (str) {
128                                                 // don't add new line before first item
129                                                 if (itmp > 0) {
130                                                         strncat(p, "\n", p_max_size);
131                                                 }
132                                                 /* remove trailing new line if one exists,
133                                                  * we have our own */
134                                                 if (strlen(str) > 0 && str[strlen(str) - 1] == '\n') {
135                                                         str[strlen(str) - 1] = 0;
136                                                 }
137                                                 strncat(p, tmpspaces, p_max_size);
138                                                 strncat(p, str, p_max_size);
139                                         }
140                                 }
141                                 free(tmpspaces);
142                         }
143                 } else {
144                         NORM_ERR("rss: Invalid action '%s'", action);
145                 }
146         }
147         timed_thread_unlock(curloc->p_timed_thread);
148 }
149