X-Git-Url: https://vcs.maemo.org/git/?a=blobdiff_plain;f=src%2Frss.c;h=2f06cc71bea5d96c38abd00cef67e1a5d6e0fc2b;hb=8264fbe05e119a10711d7377d18f38b2cf1f956a;hp=73254829f37eba7d5333f9b5f7f020d9ab50e400;hpb=ca04b9a3c7e354b36cd18f9b71c7f3e67a355d28;p=monky diff --git a/src/rss.c b/src/rss.c index 7325482..2f06cc7 100644 --- a/src/rss.c +++ b/src/rss.c @@ -1,9 +1,12 @@ -/* Conky, a system monitor, based on torsmo +/* -*- 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-2009 Brenden Matthews, Philip Kovacs, et. al. + * Copyright (c) 2005-2010 Brenden Matthews, Philip Kovacs, et. al. * (see AUTHORS) * All rights reserved. * @@ -24,10 +27,19 @@ #include "conky.h" #include "logging.h" #include "prss.h" +#include "text_object.h" #include "ccurl_thread.h" #include #include +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) @@ -41,35 +53,42 @@ void rss_free_info(void) ccurl_free_locations(&locations_head); } -void rss_process_info(char *p, int p_max_size, char *uri, char *action, int +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); if (!curloc->p_timed_thread) { - ERR("error setting up RSS thread"); + NORM_ERR("error setting up RSS thread"); } } timed_thread_lock(curloc->p_timed_thread); data = (PRSS*)curloc->result; - if (data == NULL) { - snprintf(p, p_max_size, "prss: Error reading RSS data\n"); + 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 != NULL) { - // remove trailing new line if one exists - if (str[strlen(str) - 1] == '\n') { - str[strlen(str) - 1] = 0; + 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); } @@ -77,20 +96,24 @@ void rss_process_info(char *p, int p_max_size, char *uri, char *action, int if (act_par < data->item_count) { str = data->items[act_par].title; // remove trailing new line if one exists - if (str[strlen(str) - 1] == '\n') { - str[strlen(str) - 1] = 0; + if (str && strlen(str) > 0) { + if (str[strlen(str) - 1] == '\n') { + str[strlen(str) - 1] = 0; + } + snprintf(p, p_max_size, "%s", str); } - 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) - 1] == '\n') { - str[strlen(str) - 1] = 0; + if (str && strlen(str) > 0) { + if (str[strlen(str) - 1] == '\n') { + str[strlen(str) - 1] = 0; + } + snprintf(p, p_max_size, "%s", str); } - snprintf(p, p_max_size, "%s", str); } } else if (strcmp(action, "item_titles") == EQUAL) { if (data->item_count > 0) { @@ -101,8 +124,6 @@ void rss_process_info(char *p, int p_max_size, char *uri, char *action, int memset(tmpspaces, ' ', nrspaces); tmpspaces[nrspaces]=0; - p[0] = 0; - if (act_par > data->item_count) { show = data->item_count; } else { @@ -119,7 +140,7 @@ void rss_process_info(char *p, int p_max_size, char *uri, char *action, int } /* remove trailing new line if one exists, * we have our own */ - if (str[strlen(str) - 1] == '\n') { + if (strlen(str) > 0 && str[strlen(str) - 1] == '\n') { str[strlen(str) - 1] = 0; } strncat(p, tmpspaces, p_max_size); @@ -128,8 +149,47 @@ void rss_process_info(char *p, int p_max_size, char *uri, char *action, int } 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; + } +}