File: rss.c

package info (click to toggle)
conky 1.9.0-2%2Bdeb7u1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 4,556 kB
  • sloc: ansic: 27,018; sh: 11,594; xml: 6,001; makefile: 259; python: 16
file content (195 lines) | stat: -rw-r--r-- 5,190 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
 * vim: ts=4 sw=4 noet ai cindent syntax=c
 *
 * Conky, a system monitor, based on torsmo
 *
 * Please see COPYING for details
 *
 * Copyright (c) 2007 Toni Spets
 * Copyright (c) 2005-2010 Brenden Matthews, Philip Kovacs, et. al.
 *	(see AUTHORS)
 * All rights reserved.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "conky.h"
#include "logging.h"
#include "prss.h"
#include "text_object.h"
#include "ccurl_thread.h"
#include <time.h>
#include <assert.h>

struct rss_data {
	char uri[128];
	char action[64];
	int act_par;
	float interval;
	unsigned int nrspaces;
};

static ccurl_location_t *locations_head = 0;

void rss_free_info(void)
{
	ccurl_location_t *tail = locations_head;

	while (tail) {
		if (tail->result) prss_free((PRSS*)tail->result);	/* clean up old data */
		tail = tail->next;
	}
	ccurl_free_locations(&locations_head);
}

static void rss_process_info(char *p, int p_max_size, char *uri, char *action, int
		act_par, int interval, unsigned int nrspaces)
{
	PRSS *data;
	char *str;

	ccurl_location_t *curloc = ccurl_find_location(&locations_head, uri);

	assert(act_par >= 0 && action);

	if (!curloc->p_timed_thread) {
		curloc->result = malloc(sizeof(PRSS));
		memset(curloc->result, 0, sizeof(PRSS));
		curloc->process_function = &prss_parse_data;
		ccurl_init_thread(curloc, interval * 60);
		if (!curloc->p_timed_thread) {
			NORM_ERR("error setting up RSS thread");
		}
	}

	timed_thread_lock(curloc->p_timed_thread);
	data = (PRSS*)curloc->result;

	if (data == NULL || data->item_count < 1) {
		*p = 0;
	} else {
		/*
		 * XXX: Refactor this so that we can retrieve any of the fields in the
		 * PRSS struct (in prss.h).
		 */
		if (strcmp(action, "feed_title") == EQUAL) {
			str = data->title;
			if (str && strlen(str) > 0) {
				// remove trailing new line if one exists
				if (str[strlen(str) - 1] == '\n') {
					str[strlen(str) - 1] = 0;
				}
				snprintf(p, p_max_size, "%s", str);
			}
		} else if (strcmp(action, "item_title") == EQUAL) {
			if (act_par < data->item_count) {
				str = data->items[act_par].title;
				// remove trailing new line if one exists
				if (str && strlen(str) > 0) {
					if (str[strlen(str) - 1] == '\n') {
						str[strlen(str) - 1] = 0;
					}
					snprintf(p, p_max_size, "%s", str);
				}
			}
		} else if (strcmp(action, "item_desc") == EQUAL) {
			if (act_par < data->item_count) {
				str =
					data->items[act_par].description;
				// remove trailing new line if one exists
				if (str && strlen(str) > 0) {
					if (str[strlen(str) - 1] == '\n') {
						str[strlen(str) - 1] = 0;
					}
					snprintf(p, p_max_size, "%s", str);
				}
			}
		} else if (strcmp(action, "item_titles") == EQUAL) {
			if (data->item_count > 0) {
				int itmp;
				int show;
				//'tmpspaces' is a string with spaces too be placed in front of each title
				char *tmpspaces = malloc(nrspaces + 1);
				memset(tmpspaces, ' ', nrspaces);
				tmpspaces[nrspaces]=0;

				if (act_par > data->item_count) {
					show = data->item_count;
				} else {
					show = act_par;
				}
				for (itmp = 0; itmp < show; itmp++) {
					PRSS_Item *item = &data->items[itmp];

					str = item->title;
					if (str) {
						// don't add new line before first item
						if (itmp > 0) {
							strncat(p, "\n", p_max_size);
						}
						/* remove trailing new line if one exists,
						 * we have our own */
						if (strlen(str) > 0 && str[strlen(str) - 1] == '\n') {
							str[strlen(str) - 1] = 0;
						}
						strncat(p, tmpspaces, p_max_size);
						strncat(p, str, p_max_size);
					}
				}
				free(tmpspaces);
			}
		} else {
			NORM_ERR("rss: Invalid action '%s'", action);
		}
	}
	timed_thread_unlock(curloc->p_timed_thread);
}

void rss_scan_arg(struct text_object *obj, const char *arg)
{
	int argc;
	struct rss_data *rd;

	rd = malloc(sizeof(struct rss_data));
	memset(rd, 0, sizeof(struct rss_data));

	argc = sscanf(arg, "%127s %f %63s %d %u", rd->uri, &rd->interval, rd->action,
			&rd->act_par, &rd->nrspaces);
	if (argc < 3) {
		NORM_ERR("wrong number of arguments for $rss");
		free(rd);
		return;
	}
	obj->data.opaque = rd;
}

void rss_print_info(struct text_object *obj, char *p, int p_max_size)
{
	struct rss_data *rd = obj->data.opaque;

	if (!rd) {
		NORM_ERR("error processing RSS data");
		return;
	}
	rss_process_info(p, p_max_size, rd->uri, rd->action,
			rd->act_par, rd->interval, rd->nrspaces);
}

void rss_free_obj_info(struct text_object *obj)
{
	if (obj->data.opaque) {
		free(obj->data.opaque);
		obj->data.opaque = NULL;
	}
}