From b6d14656358cea807f836fafa74177dae276421d Mon Sep 17 00:00:00 2001 From: Toni Spets Date: Sat, 2 Jun 2007 08:17:33 +0000 Subject: [PATCH] RSS updates: more flexible output and updated prss code git-svn-id: https://conky.svn.sourceforge.net/svnroot/conky/trunk/conky1@873 7f574dfc-610e-0410-a909-a81674777703 --- src/conky.c | 59 ++++++++++++++++++++++++++++++++--------------------- src/prss.c | 18 ++++++++-------- src/prss.h | 1 + src/rss.c | 5 ++++- 4 files changed, 49 insertions(+), 34 deletions(-) diff --git a/src/conky.c b/src/conky.c index 12b4c898..2e3b9fa6 100644 --- a/src/conky.c +++ b/src/conky.c @@ -95,7 +95,7 @@ static void print_version() #endif /* TCP_PORT_MONITOR */ #ifdef RSS " * rss\n" -#endif +#endif /* RSS */ "\n"); exit(0); @@ -1286,7 +1286,8 @@ struct text_object { #ifdef RSS struct { char *uri; - int count; + char *action; + int act_par; int delay; } rss; #endif @@ -2047,6 +2048,7 @@ static void free_text_objects(unsigned int count, struct text_object *objs) #ifdef RSS case OBJ_rss: free(objs[i].data.rss.uri); + free(objs[i].data.rss.action); #endif case OBJ_pre_exec: case OBJ_battery: @@ -3120,15 +3122,17 @@ static struct text_object *construct_text_object(const char *s, const char *arg, #ifdef RSS OBJ(rss, 0) if (arg) { - int argc, count, delay; - char *uri = (char *)malloc(64 * sizeof(char *)); + int argc, delay, act_par; + char *uri = (char *)malloc(128 * sizeof(char *)); + char *action = (char *)malloc(64 * sizeof(char *)); - argc = sscanf(arg, "%63s %d %d", uri, &count, &delay); + argc = sscanf(arg, "%127s %d %63s %d", uri, &delay, action, &act_par); obj->data.rss.uri = uri; - obj->data.rss.count = count; obj->data.rss.delay = delay; + obj->data.rss.action = action; + obj->data.rss.act_par = act_par; } else - CRIT_ERR("rss: needs arguments (uri, item count, delay in minutes)"); + CRIT_ERR("rss needs arguments: [act_par]"); END #endif @@ -4292,23 +4296,32 @@ static void generate_text_internal(char *p, int p_max_size, struct text_object * if(data == NULL) snprintf(p, p_max_size, "prss: Error reading RSS data\n"); else { - if(data->item_count > 0) { - int itmp; - char ctmp[64]; - p[0] = 0; - int show; - if(obj->data.rss.count > data->item_count) - show = data->item_count; - else show = obj->data.rss.count; - for(itmp = 0; itmp < show; itmp++) { - PRSS_Item *item = &data->items[itmp]; - if(i>0) - strncat(p, "\n", p_max_size); - snprintf(ctmp, 64, "%s", item->title); - strncat(p, ctmp, p_max_size); + if(!strcmp(obj->data.rss.action, "feed_title")) { + snprintf(p, p_max_size, "%s", data->title); + } else if(!strcmp(obj->data.rss.action, "item_title")) { + if(obj->data.rss.act_par < data->item_count) { + snprintf(p, p_max_size, "%s", data->items[obj->data.rss.act_par].title); } - } else - snprintf(p, p_max_size, "prss: Empty feed"); + } else if(!strcmp(obj->data.rss.action, "item_desc")) { + if(obj->data.rss.act_par < data->item_count) { + snprintf(p, p_max_size, "%s", data->items[obj->data.rss.act_par].description); + } + } else if(!strcmp(obj->data.rss.action, "item_titles")) { + if(data->item_count > 0) { + int itmp; + p[0] = 0; + int show; + if(obj->data.rss.act_par > data->item_count) + show = data->item_count; + else show = obj->data.rss.act_par; + for(itmp = 0; itmp < show; itmp++) { + PRSS_Item *item = &data->items[itmp]; + if(i>0) + strncat(p, "\n", p_max_size); + strncat(p, item->title, p_max_size); + } + } + } } } #endif diff --git a/src/prss.c b/src/prss.c index 571fb266..cab706b0 100644 --- a/src/prss.c +++ b/src/prss.c @@ -26,15 +26,13 @@ #define PARSE_OPTIONS 0 #endif -static PRSS* get_data(xmlDocPtr doc); - PRSS* prss_parse_data(const char* xml_data) { xmlDocPtr doc = xmlReadMemory(xml_data, strlen(xml_data), "", NULL, PARSE_OPTIONS); if (!doc) return NULL; - return get_data(doc); + return prss_parse_doc(doc); } PRSS* prss_parse_file(const char* xml_file) { @@ -42,7 +40,7 @@ PRSS* prss_parse_file(const char* xml_file) if (!doc) return NULL; - return get_data(doc); + return prss_parse_doc(doc); } void prss_free(PRSS* data) { @@ -55,13 +53,11 @@ void prss_free(PRSS* data) static inline void prss_null(PRSS* p) { - p->title = p->link = p->description = p->language = NULL; - p->items = NULL; - p->item_count = 0; + memset(p, 0, sizeof(PRSS)); } static inline void prss_null_item(PRSS_Item* i) { - i->title = i->link = i->description = i->category = i->pubdate = NULL; + memset(i, 0, sizeof(PRSS_Item)); } static inline void read_item(PRSS_Item* res, xmlNodePtr data) @@ -127,7 +123,9 @@ static inline void read_element(PRSS* res, xmlNodePtr n) static inline int parse_rss_2_0(PRSS* res, xmlNodePtr root) { xmlNodePtr channel = root->children; - while(channel && (channel->type!=XML_ELEMENT_NODE || strcmp((char*)channel->name, "channel"))) + while(channel + && (channel->type!=XML_ELEMENT_NODE + || strcmp((char*)channel->name, "channel"))) channel = channel->next; if (!channel) return 0; @@ -180,7 +178,7 @@ static inline int parse_rss_0_9x(PRSS* res, xmlNodePtr root) return parse_rss_2_0(res, root); } -PRSS* get_data(xmlDocPtr doc) +PRSS* prss_parse_doc(xmlDocPtr doc) { xmlNodePtr root = xmlDocGetRootElement(doc); PRSS* result = malloc(sizeof(PRSS)); diff --git a/src/prss.h b/src/prss.h index 4a9e06fc..89fa7718 100644 --- a/src/prss.h +++ b/src/prss.h @@ -50,6 +50,7 @@ typedef struct PRSS_ { /* Functions for parsing RSS-data */ PRSS* prss_parse_data(const char *xml_data); PRSS* prss_parse_file(const char *xml_file); +PRSS* prss_parse_doc(xmlDocPtr doc); /* Frees the PRSS-stucture returned by prss_parse_*. * The memory area pointed by data becomes invalid diff --git a/src/rss.c b/src/rss.c index 0e1df808..d15b2585 100644 --- a/src/rss.c +++ b/src/rss.c @@ -15,6 +15,8 @@ #include #include +#include "conky.h" + #define MAX_FEEDS 16 struct MemoryStruct { @@ -125,7 +127,8 @@ get_rss_info(char *uri, int delay) if(chunk.size) { curdata = prss_parse_data(chunk.memory); free(chunk.memory); - } + } else + ERR("No data from server"); curfeed->data = curdata;