mirror of
https://github.com/Llewellynvdm/conky.git
synced 2024-11-17 18:45:10 +00:00
Make $rss use the new callback system
This commit is contained in:
parent
fb81e3a9b3
commit
7cf37f7de0
@ -2571,9 +2571,6 @@ void clean_up_without_threads(void *memtofree1, void* memtofree2)
|
|||||||
|
|
||||||
#ifdef BUILD_PORT_MONITORS
|
#ifdef BUILD_PORT_MONITORS
|
||||||
tcp_portmon_clear();
|
tcp_portmon_clear();
|
||||||
#endif
|
|
||||||
#ifdef BUILD_RSS
|
|
||||||
rss_free_info();
|
|
||||||
#endif
|
#endif
|
||||||
llua_shutdown_hook();
|
llua_shutdown_hook();
|
||||||
#if defined BUILD_WEATHER_XOAP || defined BUILD_RSS
|
#if defined BUILD_WEATHER_XOAP || defined BUILD_RSS
|
||||||
|
@ -98,10 +98,6 @@ struct text_object;
|
|||||||
#include "audacious.h"
|
#include "audacious.h"
|
||||||
#endif /* BUILD_AUDACIOUS */
|
#endif /* BUILD_AUDACIOUS */
|
||||||
|
|
||||||
#ifdef BUILD_RSS
|
|
||||||
#include "rss.h"
|
|
||||||
#endif /* BUILD_RSS */
|
|
||||||
|
|
||||||
#ifdef BUILD_WEATHER_XOAP
|
#ifdef BUILD_WEATHER_XOAP
|
||||||
#ifndef BUILD_WEATHER_METAR
|
#ifndef BUILD_WEATHER_METAR
|
||||||
#error "BUILD_WEATHER_METAR needs to be defined if XOAP is defined"
|
#error "BUILD_WEATHER_METAR needs to be defined if XOAP is defined"
|
||||||
|
@ -90,6 +90,9 @@
|
|||||||
#ifdef BUILD_WEATHER_METAR
|
#ifdef BUILD_WEATHER_METAR
|
||||||
#include "weather.h"
|
#include "weather.h"
|
||||||
#endif /* BUILD_WEATHER_METAR */
|
#endif /* BUILD_WEATHER_METAR */
|
||||||
|
#ifdef BUILD_RSS
|
||||||
|
#include "rss.h"
|
||||||
|
#endif /* BUILD_RSS */
|
||||||
|
|
||||||
/* check for OS and include appropriate headers */
|
/* check for OS and include appropriate headers */
|
||||||
#if defined(__linux__)
|
#if defined(__linux__)
|
||||||
|
56
src/prss.cc
56
src/prss.cc
@ -30,19 +30,19 @@
|
|||||||
|
|
||||||
void prss_parse_doc(PRSS *result, xmlDocPtr doc);
|
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,
|
if (!doc)
|
||||||
PARSE_OPTIONS);
|
throw std::runtime_error("Unable to parse rss data");
|
||||||
|
|
||||||
if (!doc) {
|
prss_parse_doc(this, doc.get());
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
prss_parse_doc(data, doc);
|
|
||||||
xmlFreeDoc(doc);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void free_rss_items(PRSS *data)
|
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) {
|
free_rss_items(this);
|
||||||
return;
|
free(version);
|
||||||
}
|
free(title);
|
||||||
free_and_zero(data->version);
|
free(link);
|
||||||
free_rss_items(data);
|
free(description);
|
||||||
data->version = 0;
|
free(language);
|
||||||
#define CLEAR(a) free_and_zero(data->a);
|
free(pubDate);
|
||||||
CLEAR(title);
|
free(lastBuildDate);
|
||||||
CLEAR(link);
|
free(generator);
|
||||||
CLEAR(description);
|
free(docs);
|
||||||
CLEAR(language);
|
free(managingEditor);
|
||||||
CLEAR(pubDate);
|
free(webMaster);
|
||||||
CLEAR(lastBuildDate);
|
free(copyright);
|
||||||
CLEAR(generator);
|
free(ttl);
|
||||||
CLEAR(docs);
|
|
||||||
CLEAR(managingEditor);
|
|
||||||
CLEAR(webMaster);
|
|
||||||
CLEAR(copyright);
|
|
||||||
CLEAR(ttl);
|
|
||||||
#undef CLEAR
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline void prss_null_item(PRSS_Item *i)
|
static inline void prss_null_item(PRSS_Item *i)
|
||||||
|
17
src/prss.h
17
src/prss.h
@ -1,4 +1,5 @@
|
|||||||
/* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
|
/* -*- 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>
|
* Copyright (c) 2007 Mikko Sysikaski <mikko.sysikaski@gmail.com>
|
||||||
* Toni Spets <toni.spets@gmail.com>
|
* Toni Spets <toni.spets@gmail.com>
|
||||||
@ -29,7 +30,7 @@ typedef struct PRSS_Item_ {
|
|||||||
char *guid;
|
char *guid;
|
||||||
} PRSS_Item;
|
} PRSS_Item;
|
||||||
|
|
||||||
typedef struct PRSS_ {
|
struct PRSS {
|
||||||
char *version;
|
char *version;
|
||||||
|
|
||||||
char *title;
|
char *title;
|
||||||
@ -47,17 +48,9 @@ typedef struct PRSS_ {
|
|||||||
|
|
||||||
PRSS_Item *items;
|
PRSS_Item *items;
|
||||||
int item_count;
|
int item_count;
|
||||||
} PRSS;
|
|
||||||
|
|
||||||
/* Functions for parsing RSS-data */
|
PRSS(const std::string &xml_data);
|
||||||
void prss_parse_data(void *result, const char *xml_data);
|
~PRSS();
|
||||||
|
};
|
||||||
/* // 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);
|
|
||||||
|
|
||||||
#endif /* PRSS_H */
|
#endif /* PRSS_H */
|
||||||
|
48
src/rss.cc
48
src/rss.cc
@ -31,6 +31,7 @@
|
|||||||
#include "ccurl_thread.h"
|
#include "ccurl_thread.h"
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
#include <cmath>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
|
|
||||||
struct rss_data {
|
struct rss_data {
|
||||||
@ -41,40 +42,45 @@ struct rss_data {
|
|||||||
unsigned int nrspaces;
|
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)
|
protected:
|
||||||
|
virtual void process_data()
|
||||||
{
|
{
|
||||||
for (ccurl_location_list::iterator i = locations_rss.begin();
|
try {
|
||||||
i != locations_rss.end(); i++) {
|
std::shared_ptr<PRSS> tmp(new PRSS(data));
|
||||||
prss_free((PRSS*) (*i)->result);
|
|
||||||
|
std::unique_lock<std::mutex> lock(Base::result_mutex);
|
||||||
|
Base::result = tmp;
|
||||||
}
|
}
|
||||||
ccurl_free_locations(locations_rss);
|
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
|
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)
|
act_par, int interval, unsigned int nrspaces)
|
||||||
{
|
{
|
||||||
PRSS *data;
|
|
||||||
char *str;
|
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);
|
assert(act_par >= 0 && action);
|
||||||
|
|
||||||
if (!curloc->p_timed_thread) {
|
std::shared_ptr<PRSS> data = cb->get_result_copy();
|
||||||
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;
|
|
||||||
|
|
||||||
if (data == NULL || data->item_count < 1) {
|
if (data == NULL || data->item_count < 1) {
|
||||||
*p = 0;
|
*p = 0;
|
||||||
|
@ -30,6 +30,4 @@ void rss_scan_arg(struct text_object *, const char *);
|
|||||||
void rss_print_info(struct text_object *, char *, int);
|
void rss_print_info(struct text_object *, char *, int);
|
||||||
void rss_free_obj_info(struct text_object *);
|
void rss_free_obj_info(struct text_object *);
|
||||||
|
|
||||||
void rss_free_info(void);
|
|
||||||
|
|
||||||
#endif /*RSS_H_*/
|
#endif /*RSS_H_*/
|
||||||
|
Loading…
Reference in New Issue
Block a user