1
0
mirror of https://github.com/Llewellynvdm/conky.git synced 2024-09-29 13:39:10 +00:00

Make $rss use the new callback system

This commit is contained in:
Pavel Labath 2011-02-16 00:33:22 +01:00
parent fb81e3a9b3
commit 7cf37f7de0
7 changed files with 62 additions and 75 deletions

View File

@ -2571,9 +2571,6 @@ void clean_up_without_threads(void *memtofree1, void* memtofree2)
#ifdef BUILD_PORT_MONITORS
tcp_portmon_clear();
#endif
#ifdef BUILD_RSS
rss_free_info();
#endif
llua_shutdown_hook();
#if defined BUILD_WEATHER_XOAP || defined BUILD_RSS

View File

@ -98,10 +98,6 @@ struct text_object;
#include "audacious.h"
#endif /* BUILD_AUDACIOUS */
#ifdef BUILD_RSS
#include "rss.h"
#endif /* BUILD_RSS */
#ifdef BUILD_WEATHER_XOAP
#ifndef BUILD_WEATHER_METAR
#error "BUILD_WEATHER_METAR needs to be defined if XOAP is defined"

View File

@ -90,6 +90,9 @@
#ifdef BUILD_WEATHER_METAR
#include "weather.h"
#endif /* BUILD_WEATHER_METAR */
#ifdef BUILD_RSS
#include "rss.h"
#endif /* BUILD_RSS */
/* check for OS and include appropriate headers */
#if defined(__linux__)

View File

@ -30,19 +30,19 @@
void prss_parse_doc(PRSS *result, xmlDocPtr doc);
void prss_parse_data(void *result, const char *xml_data)
PRSS::PRSS(const std::string &xml_data)
: version(NULL), title(NULL), link(NULL), description(NULL), language(NULL),
generator(NULL), managingEditor(NULL), webMaster(NULL), docs(NULL), lastBuildDate(NULL),
pubDate(NULL), copyright(NULL), ttl(NULL), items(NULL), item_count(0)
{
PRSS *data = (PRSS*)result;
std::unique_ptr<xmlDoc, void (*)(xmlDoc *)> doc(
xmlReadMemory(xml_data.c_str(), xml_data.length(), "", NULL, PARSE_OPTIONS),
xmlFreeDoc);
xmlDocPtr doc = xmlReadMemory(xml_data, strlen(xml_data), "", NULL,
PARSE_OPTIONS);
if (!doc)
throw std::runtime_error("Unable to parse rss data");
if (!doc) {
return;
}
prss_parse_doc(data, doc);
xmlFreeDoc(doc);
prss_parse_doc(this, doc.get());
}
void free_rss_items(PRSS *data)
@ -65,28 +65,22 @@ void free_rss_items(PRSS *data)
}
}
void prss_free(PRSS *data)
PRSS::~PRSS()
{
if (!data) {
return;
}
free_and_zero(data->version);
free_rss_items(data);
data->version = 0;
#define CLEAR(a) free_and_zero(data->a);
CLEAR(title);
CLEAR(link);
CLEAR(description);
CLEAR(language);
CLEAR(pubDate);
CLEAR(lastBuildDate);
CLEAR(generator);
CLEAR(docs);
CLEAR(managingEditor);
CLEAR(webMaster);
CLEAR(copyright);
CLEAR(ttl);
#undef CLEAR
free_rss_items(this);
free(version);
free(title);
free(link);
free(description);
free(language);
free(pubDate);
free(lastBuildDate);
free(generator);
free(docs);
free(managingEditor);
free(webMaster);
free(copyright);
free(ttl);
}
static inline void prss_null_item(PRSS_Item *i)

View File

@ -1,4 +1,5 @@
/* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
* vim: ts=4 sw=4 noet ai cindent syntax=cpp
*
* Copyright (c) 2007 Mikko Sysikaski <mikko.sysikaski@gmail.com>
* Toni Spets <toni.spets@gmail.com>
@ -29,7 +30,7 @@ typedef struct PRSS_Item_ {
char *guid;
} PRSS_Item;
typedef struct PRSS_ {
struct PRSS {
char *version;
char *title;
@ -47,17 +48,9 @@ typedef struct PRSS_ {
PRSS_Item *items;
int item_count;
} PRSS;
/* Functions for parsing RSS-data */
void prss_parse_data(void *result, const char *xml_data);
/* // Works wrong currently when called from application!
PRSS *prss_parse_doc(xmlDocPtr doc); */
/* Frees the PRSS-stucture returned by prss_parse_*.
* The memory area pointed by data becomes invalid
* after call to this function. */
void prss_free(PRSS *data);
PRSS(const std::string &xml_data);
~PRSS();
};
#endif /* PRSS_H */

View File

@ -31,6 +31,7 @@
#include "ccurl_thread.h"
#include <time.h>
#include <assert.h>
#include <cmath>
#include <mutex>
struct rss_data {
@ -41,40 +42,45 @@ struct rss_data {
unsigned int nrspaces;
};
static ccurl_location_list locations_rss;
namespace {
class rss_cb: public curl_callback<std::shared_ptr<PRSS>> {
typedef curl_callback<std::shared_ptr<PRSS>> Base;
void rss_free_info(void)
{
for (ccurl_location_list::iterator i = locations_rss.begin();
i != locations_rss.end(); i++) {
prss_free((PRSS*) (*i)->result);
}
ccurl_free_locations(locations_rss);
protected:
virtual void process_data()
{
try {
std::shared_ptr<PRSS> tmp(new PRSS(data));
std::unique_lock<std::mutex> lock(Base::result_mutex);
Base::result = tmp;
}
catch(std::runtime_error &e) {
NORM_ERR("%s", e.what());
}
}
public:
rss_cb(uint32_t period, const std::string &uri)
: Base(period, Base::Tuple(uri))
{}
};
}
static void rss_process_info(char *p, int p_max_size, const std::string &uri, char *action, int
act_par, int interval, unsigned int nrspaces)
{
PRSS *data;
char *str;
ccurl_location_ptr curloc = ccurl_find_location(locations_rss, uri);
uint32_t period = std::max(std::lround(interval/active_update_interval()), 1l);
auto cb = conky::register_cb<rss_cb>(period, uri);
assert(act_par >= 0 && action);
if (!curloc->p_timed_thread) {
curloc->result = (char*)malloc(sizeof(PRSS));
memset(curloc->result, 0, sizeof(PRSS));
curloc->process_function = std::bind(prss_parse_data,
std::placeholders::_1, std::placeholders::_2);
ccurl_init_thread(curloc, interval * 60);
if (!curloc->p_timed_thread) {
NORM_ERR("error setting up RSS thread");
}
}
std::lock_guard<std::mutex> lock(curloc->p_timed_thread->mutex());
data = (PRSS*)curloc->result;
std::shared_ptr<PRSS> data = cb->get_result_copy();
if (data == NULL || data->item_count < 1) {
*p = 0;

View File

@ -30,6 +30,4 @@ void rss_scan_arg(struct text_object *, const char *);
void rss_print_info(struct text_object *, char *, int);
void rss_free_obj_info(struct text_object *);
void rss_free_info(void);
#endif /*RSS_H_*/