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