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