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
4 * Conky, a system monitor, based on torsmo
6 * Please see COPYING for details
8 * Copyright (c) 2007 Toni Spets
9 * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
11 * All rights reserved.
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.
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/>.
30 #include "text_object.h"
31 #include "ccurl_thread.h"
40 unsigned int nrspaces;
43 static ccurl_location_t *locations_head = 0;
45 void rss_free_info(void)
47 ccurl_location_t *tail = locations_head;
50 if (tail->result) prss_free((PRSS*)tail->result); /* clean up old data */
53 ccurl_free_locations(&locations_head);
56 static void rss_process_info(char *p, int p_max_size, char *uri, char *action, int
57 act_par, int interval, unsigned int nrspaces)
62 ccurl_location_t *curloc = ccurl_find_location(&locations_head, uri);
64 assert(act_par >= 0 && action);
66 if (!curloc->p_timed_thread) {
67 curloc->result = malloc(sizeof(PRSS));
68 memset(curloc->result, 0, sizeof(PRSS));
69 curloc->process_function = &prss_parse_data;
70 ccurl_init_thread(curloc, interval);
71 if (!curloc->p_timed_thread) {
72 NORM_ERR("error setting up RSS thread");
76 timed_thread_lock(curloc->p_timed_thread);
77 data = (PRSS*)curloc->result;
79 if (data == NULL || data->item_count < 1) {
83 * XXX: Refactor this so that we can retrieve any of the fields in the
84 * PRSS struct (in prss.h).
86 if (strcmp(action, "feed_title") == EQUAL) {
88 if (str && strlen(str) > 0) {
89 // remove trailing new line if one exists
90 if (str[strlen(str) - 1] == '\n') {
91 str[strlen(str) - 1] = 0;
93 snprintf(p, p_max_size, "%s", str);
95 } else if (strcmp(action, "item_title") == EQUAL) {
96 if (act_par < data->item_count) {
97 str = data->items[act_par].title;
98 // remove trailing new line if one exists
99 if (str && strlen(str) > 0) {
100 if (str[strlen(str) - 1] == '\n') {
101 str[strlen(str) - 1] = 0;
103 snprintf(p, p_max_size, "%s", str);
106 } else if (strcmp(action, "item_desc") == EQUAL) {
107 if (act_par < data->item_count) {
109 data->items[act_par].description;
110 // remove trailing new line if one exists
111 if (str && strlen(str) > 0) {
112 if (str[strlen(str) - 1] == '\n') {
113 str[strlen(str) - 1] = 0;
115 snprintf(p, p_max_size, "%s", str);
118 } else if (strcmp(action, "item_titles") == EQUAL) {
119 if (data->item_count > 0) {
122 //'tmpspaces' is a string with spaces too be placed in front of each title
123 char *tmpspaces = malloc(nrspaces + 1);
124 memset(tmpspaces, ' ', nrspaces);
125 tmpspaces[nrspaces]=0;
127 if (act_par > data->item_count) {
128 show = data->item_count;
132 for (itmp = 0; itmp < show; itmp++) {
133 PRSS_Item *item = &data->items[itmp];
137 // don't add new line before first item
139 strncat(p, "\n", p_max_size);
141 /* remove trailing new line if one exists,
143 if (strlen(str) > 0 && str[strlen(str) - 1] == '\n') {
144 str[strlen(str) - 1] = 0;
146 strncat(p, tmpspaces, p_max_size);
147 strncat(p, str, p_max_size);
153 NORM_ERR("rss: Invalid action '%s'", action);
156 timed_thread_unlock(curloc->p_timed_thread);
159 void rss_scan_arg(struct text_object *obj, const char *arg)
164 rd = malloc(sizeof(struct rss_data));
165 memset(rd, 0, sizeof(struct rss_data));
167 argc = sscanf(arg, "%127s %f %63s %d %u", rd->uri, &rd->interval, rd->action,
168 &rd->act_par, &rd->nrspaces);
170 NORM_ERR("wrong number of arguments for $rss");
174 obj->data.opaque = rd;
177 void rss_print_info(struct text_object *obj, char *p, int p_max_size)
179 struct rss_data *rd = obj->data.opaque;
182 NORM_ERR("error processing RSS data");
185 rss_process_info(p, p_max_size, rd->uri, rd->action,
186 rd->act_par, rd->interval, rd->nrspaces);
189 void rss_free_obj_info(struct text_object *obj)
191 if (obj->data.opaque) {
192 free(obj->data.opaque);
193 obj->data.opaque = NULL;