1
0
mirror of https://github.com/Llewellynvdm/conky.git synced 2024-11-20 03:51:18 +00:00
conky/src/rss.cc

198 lines
5.0 KiB
C++
Raw Normal View History

2010-01-07 03:45:19 +00:00
/* -*- mode: c++; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
* vim: ts=4 sw=4 noet ai cindent syntax=cpp
*
* Conky, a system monitor, based on torsmo
*
* Please see COPYING for details
*
* Copyright (c) 2007 Toni Spets
2010-01-01 23:46:17 +00:00
* 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/>.
*
2008-12-09 23:35:49 +00:00
*/
#include "conky.h"
#include "logging.h"
#include "prss.h"
#include "text_object.h"
#include "ccurl_thread.h"
#include <time.h>
#include <assert.h>
2011-02-15 23:33:22 +00:00
#include <cmath>
#include <mutex>
2009-10-04 17:14:30 +00:00
struct rss_data {
char uri[128];
char action[64];
int act_par;
float interval;
unsigned int nrspaces;
};
2011-02-15 23:33:22 +00:00
namespace {
class rss_cb: public curl_callback<std::shared_ptr<PRSS>> {
typedef curl_callback<std::shared_ptr<PRSS>> Base;
2011-02-15 23:33:22 +00:00
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))
{}
};
}
2011-02-15 23:33:22 +00:00
2010-10-15 00:28:57 +00:00
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)
{
char *str;
2011-02-15 23:33:22 +00:00
uint32_t period = std::max(std::lround(interval/active_update_interval()), 1l);
2009-08-05 04:56:19 +00:00
2011-02-15 23:33:22 +00:00
auto cb = conky::register_cb<rss_cb>(period, uri);
2009-08-05 04:56:19 +00:00
2011-02-15 23:33:22 +00:00
assert(act_par >= 0 && action);
2011-02-15 23:33:22 +00:00
std::shared_ptr<PRSS> data = cb->get_result_copy();
2009-08-05 04:56:19 +00:00
if (data == NULL || data->item_count < 1) {
*p = 0;
} else {
2009-08-05 04:56:19 +00:00
/*
* 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) {
2009-08-05 04:56:19 +00:00
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
2009-08-05 04:56:19 +00:00
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
2009-08-05 04:56:19 +00:00
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 = (char*)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 */
2009-08-05 04:56:19 +00:00
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);
}
2009-08-05 04:56:19 +00:00
} else {
NORM_ERR("rss: Invalid action '%s'", action);
}
}
}
void rss_scan_arg(struct text_object *obj, const char *arg)
{
2009-10-04 17:14:30 +00:00
int argc;
struct rss_data *rd;
rd = (struct rss_data *)malloc(sizeof(struct rss_data));
2009-10-04 17:14:30 +00:00
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");
2009-10-04 17:14:30 +00:00
free(rd);
return;
}
2009-10-04 17:14:30 +00:00
obj->data.opaque = rd;
}
void rss_print_info(struct text_object *obj, char *p, int p_max_size)
{
struct rss_data *rd = (struct rss_data *)obj->data.opaque;
2009-10-04 17:14:30 +00:00
if (!rd) {
NORM_ERR("error processing RSS data");
return;
}
2009-10-04 17:14:30 +00:00
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)
{
free_and_zero(obj->data.opaque);
}