4912479885d57c630d5ab1962bc2e1a4c79b45cf
[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                         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                 } else if (strcmp(action, "item_title") == EQUAL) {
75                         if (act_par < data->item_count) {
76                                 str = data->items[act_par].title;
77                                 // remove trailing new line if one exists
78                                 if (str[strlen(str) - 1] == '\n') {
79                                         str[strlen(str) - 1] = 0;
80                                 }
81                                 snprintf(p, p_max_size, "%s", str);
82                         }
83                 } else if (strcmp(action, "item_desc") == EQUAL) {
84                         if (act_par < data->item_count) {
85                                 str =
86                                         data->items[act_par].description;
87                                 // remove trailing new line if one exists
88                                 if (str[strlen(str) - 1] == '\n') {
89                                         str[strlen(str) - 1] = 0;
90                                 }
91                                 snprintf(p, p_max_size, "%s", str);
92                         }
93                 } else if (strcmp(action, "item_titles") == EQUAL) {
94                         if (data->item_count > 0) {
95                                 int itmp;
96                                 int show;
97                                 //'tmpspaces' is a string with spaces too be placed in front of each title
98                                 char *tmpspaces = malloc(nrspaces + 1);
99                                 memset(tmpspaces, ' ', nrspaces);
100                                 tmpspaces[nrspaces]=0;
101
102                                 p[0] = 0;
103
104                                 if (act_par > data->item_count) {
105                                         show = data->item_count;
106                                 } else {
107                                         show = act_par;
108                                 }
109                                 for (itmp = 0; itmp < show; itmp++) {
110                                         PRSS_Item *item = &data->items[itmp];
111
112                                         str = item->title;
113                                         if (str) {
114                                                 // don't add new line before first item
115                                                 if (itmp > 0) {
116                                                         strncat(p, "\n", p_max_size);
117                                                 }
118                                                 /* remove trailing new line if one exists,
119                                                  * we have our own */
120                                                 if (str[strlen(str) - 1] == '\n') {
121                                                         str[strlen(str) - 1] = 0;
122                                                 }
123                                                 strncat(p, tmpspaces, p_max_size);
124                                                 strncat(p, str, p_max_size);
125                                         }
126                                 }
127                                 free(tmpspaces);
128                         }
129                 }
130         }
131         timed_thread_unlock(curloc->p_timed_thread);
132 }
133