54ee550c1c41527cab9275189a6f590b6c15362c
[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  * vim: ts=4 sw=4 noet ai cindent syntax=c
23  *
24  */
25
26 #include "conky.h"
27 #include "logging.h"
28 #include "prss.h"
29 #include "ccurl_thread.h"
30 #include <time.h>
31 #include <assert.h>
32
33 static ccurl_location_t *locations_head = 0;
34
35 void rss_free_info(void)
36 {
37         ccurl_location_t *tail = locations_head;
38
39         while (tail) {
40                 if (tail->result) prss_free((PRSS*)tail->result);       /* clean up old data */
41                 tail = tail->next;
42         }
43         ccurl_free_locations(&locations_head);
44 }
45
46 void rss_process_info(char *p, int p_max_size, char *uri, char *action, int
47                 act_par, int interval, unsigned int nrspaces)
48 {
49         PRSS *data;
50         char *str;
51
52         ccurl_location_t *curloc = ccurl_find_location(&locations_head, uri);
53         if (!curloc->p_timed_thread) {
54                 curloc->result = malloc(sizeof(PRSS));
55                 memset(curloc->result, 0, sizeof(PRSS));
56                 curloc->process_function = &prss_parse_data;
57                 ccurl_init_thread(curloc, interval);
58                 if (!curloc->p_timed_thread) {
59                         ERR("error setting up RSS thread");
60                 }
61         }
62
63         timed_thread_lock(curloc->p_timed_thread);
64         data = (PRSS*)curloc->result;
65
66         if (data == NULL) {
67                 snprintf(p, p_max_size, "prss: Error reading RSS data\n");
68         } else {
69                 if (strcmp(action, "feed_title") == EQUAL) {
70                         str = data->title;
71                         if (str != NULL) {
72                                 // remove trailing new line if one exists
73                                 if (str[strlen(str) - 1] == '\n') {
74                                         str[strlen(str) - 1] = 0;
75                                 }
76                                 snprintf(p, p_max_size, "%s", str);
77                         }
78                 } else if (strcmp(action, "item_title") == EQUAL) {
79                         if (act_par < data->item_count) {
80                                 str = data->items[act_par].title;
81                                 // remove trailing new line if one exists
82                                 if (str[strlen(str) - 1] == '\n') {
83                                         str[strlen(str) - 1] = 0;
84                                 }
85                                 snprintf(p, p_max_size, "%s", str);
86                         }
87                 } else if (strcmp(action, "item_desc") == EQUAL) {
88                         if (act_par < data->item_count) {
89                                 str =
90                                         data->items[act_par].description;
91                                 // remove trailing new line if one exists
92                                 if (str[strlen(str) - 1] == '\n') {
93                                         str[strlen(str) - 1] = 0;
94                                 }
95                                 snprintf(p, p_max_size, "%s", str);
96                         }
97                 } else if (strcmp(action, "item_titles") == EQUAL) {
98                         if (data->item_count > 0) {
99                                 int itmp;
100                                 int show;
101                                 //'tmpspaces' is a string with spaces too be placed in front of each title
102                                 char *tmpspaces = malloc(nrspaces + 1);
103                                 memset(tmpspaces, ' ', nrspaces);
104                                 tmpspaces[nrspaces]=0;
105
106                                 p[0] = 0;
107
108                                 if (act_par > data->item_count) {
109                                         show = data->item_count;
110                                 } else {
111                                         show = act_par;
112                                 }
113                                 for (itmp = 0; itmp < show; itmp++) {
114                                         PRSS_Item *item = &data->items[itmp];
115
116                                         str = item->title;
117                                         if (str) {
118                                                 // don't add new line before first item
119                                                 if (itmp > 0) {
120                                                         strncat(p, "\n", p_max_size);
121                                                 }
122                                                 /* remove trailing new line if one exists,
123                                                  * we have our own */
124                                                 if (str[strlen(str) - 1] == '\n') {
125                                                         str[strlen(str) - 1] = 0;
126                                                 }
127                                                 strncat(p, tmpspaces, p_max_size);
128                                                 strncat(p, str, p_max_size);
129                                         }
130                                 }
131                                 free(tmpspaces);
132                         }
133                 }
134         }
135         timed_thread_unlock(curloc->p_timed_thread);
136 }
137