mirror of
https://github.com/RetroShare/RetroShare.git
synced 2025-05-02 14:16:16 -04:00
FeedReader plugin
- reserved service id - reworked error codes - added xpath manipulation and basic gui elements in preview dialog git-svn-id: http://svn.code.sf.net/p/retroshare/code/branches/v0.5-gxs-b1@5514 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
parent
08904bf82f
commit
c7ed9c6df7
23 changed files with 1595 additions and 430 deletions
|
@ -37,7 +37,7 @@ RsFeedReader *rsFeedReader = NULL;
|
|||
*********/
|
||||
|
||||
p3FeedReader::p3FeedReader(RsPluginHandler* pgHandler)
|
||||
: RsPQIService(RS_PKT_TYPE_FEEDREADER_CONFIG, CONFIG_TYPE_FEEDREADER, 5, pgHandler),
|
||||
: RsPQIService(RS_SERVICE_TYPE_PLUGIN_FEEDREADER, CONFIG_TYPE_FEEDREADER, 5, pgHandler),
|
||||
mFeedReaderMtx("p3FeedReader"), mDownloadMutex("p3FeedReaderDownload"), mProcessMutex("p3FeedReaderProcess"), mPreviewMutex("p3FeedReaderPreview")
|
||||
{
|
||||
mNextFeedId = 1;
|
||||
|
@ -88,6 +88,9 @@ static void feedToInfo(const RsFeedReaderFeed *feed, FeedInfo &info)
|
|||
info.errorState = feed->errorState;
|
||||
info.errorString = feed->errorString;
|
||||
|
||||
info.xpathsToUse = feed->xpathsToUse.ids;
|
||||
info.xpathsToRemove = feed->xpathsToRemove.ids;
|
||||
|
||||
info.flag.folder = (feed->flag & RS_FEED_FLAG_FOLDER);
|
||||
info.flag.infoFromFeed = (feed->flag & RS_FEED_FLAG_INFO_FROM_FEED);
|
||||
info.flag.standardStorageTime = (feed->flag & RS_FEED_FLAG_STANDARD_STORAGE_TIME);
|
||||
|
@ -141,6 +144,9 @@ static void infoToFeed(const FeedInfo &info, RsFeedReaderFeed *feed, bool add)
|
|||
feed->forumId = info.forumId;
|
||||
}
|
||||
|
||||
feed->xpathsToUse.ids = info.xpathsToUse;
|
||||
feed->xpathsToRemove.ids = info.xpathsToRemove;
|
||||
|
||||
// feed->preview = info.flag.preview;
|
||||
|
||||
uint32_t oldFlag = feed->flag;
|
||||
|
@ -512,11 +518,11 @@ void p3FeedReader::deleteAllMsgs_locked(RsFeedReaderFeed *fi)
|
|||
}
|
||||
|
||||
std::map<std::string, RsFeedReaderMsg*>::iterator msgIt;
|
||||
for (msgIt = fi->mMsgs.begin(); msgIt != fi->mMsgs.end(); ++msgIt) {
|
||||
for (msgIt = fi->msgs.begin(); msgIt != fi->msgs.end(); ++msgIt) {
|
||||
delete(msgIt->second);
|
||||
}
|
||||
|
||||
fi->mMsgs.clear();
|
||||
fi->msgs.clear();
|
||||
}
|
||||
|
||||
bool p3FeedReader::removeFeed(const std::string &feedId)
|
||||
|
@ -712,8 +718,8 @@ bool p3FeedReader::getMsgInfo(const std::string &feedId, const std::string &msgI
|
|||
RsFeedReaderFeed *fi = feedIt->second;
|
||||
|
||||
std::map<std::string, RsFeedReaderMsg*>::iterator msgIt;
|
||||
msgIt = fi->mMsgs.find(msgId);
|
||||
if (msgIt == fi->mMsgs.end()) {
|
||||
msgIt = fi->msgs.find(msgId);
|
||||
if (msgIt == fi->msgs.end()) {
|
||||
#ifdef FEEDREADER_DEBUG
|
||||
std::cerr << "p3FeedReader::getMsgInfo - msg " << msgId << " not found" << std::endl;
|
||||
#endif
|
||||
|
@ -744,8 +750,8 @@ bool p3FeedReader::removeMsg(const std::string &feedId, const std::string &msgId
|
|||
changed = !fi->preview;
|
||||
|
||||
std::map<std::string, RsFeedReaderMsg*>::iterator msgIt;
|
||||
msgIt = fi->mMsgs.find(msgId);
|
||||
if (msgIt == fi->mMsgs.end()) {
|
||||
msgIt = fi->msgs.find(msgId);
|
||||
if (msgIt == fi->msgs.end()) {
|
||||
#ifdef FEEDREADER_DEBUG
|
||||
std::cerr << "p3FeedReader::removeMsg - msg " << msgId << " not found" << std::endl;
|
||||
#endif
|
||||
|
@ -789,8 +795,8 @@ bool p3FeedReader::removeMsgs(const std::string &feedId, const std::list<std::st
|
|||
std::list<std::string>::const_iterator idIt;
|
||||
for (idIt = msgIds.begin(); idIt != msgIds.end(); ++idIt) {
|
||||
std::map<std::string, RsFeedReaderMsg*>::iterator msgIt;
|
||||
msgIt = fi->mMsgs.find(*idIt);
|
||||
if (msgIt == fi->mMsgs.end()) {
|
||||
msgIt = fi->msgs.find(*idIt);
|
||||
if (msgIt == fi->msgs.end()) {
|
||||
#ifdef FEEDREADER_DEBUG
|
||||
std::cerr << "p3FeedReader::removeMsgs - msg " << *idIt << " not found" << std::endl;
|
||||
#endif
|
||||
|
@ -842,7 +848,7 @@ bool p3FeedReader::getMessageCount(const std::string &feedId, uint32_t *msgCount
|
|||
RsFeedReaderFeed *fi = feedIt->second;
|
||||
|
||||
std::map<std::string, RsFeedReaderMsg*>::iterator msgIt;
|
||||
for (msgIt = fi->mMsgs.begin(); msgIt != fi->mMsgs.end(); ++msgIt) {
|
||||
for (msgIt = fi->msgs.begin(); msgIt != fi->msgs.end(); ++msgIt) {
|
||||
RsFeedReaderMsg *mi = msgIt->second;
|
||||
|
||||
if (mi->flag & RS_FEEDMSG_FLAG_DELETED) {
|
||||
|
@ -872,7 +878,7 @@ bool p3FeedReader::getFeedMsgList(const std::string &feedId, std::list<FeedMsgIn
|
|||
RsFeedReaderFeed *fi = feedIt->second;
|
||||
|
||||
std::map<std::string, RsFeedReaderMsg*>::iterator msgIt;
|
||||
for (msgIt = fi->mMsgs.begin(); msgIt != fi->mMsgs.end(); ++msgIt) {
|
||||
for (msgIt = fi->msgs.begin(); msgIt != fi->msgs.end(); ++msgIt) {
|
||||
RsFeedReaderMsg *mi = msgIt->second;
|
||||
|
||||
if (mi->flag & RS_FEEDMSG_FLAG_DELETED) {
|
||||
|
@ -902,7 +908,7 @@ bool p3FeedReader::getFeedMsgIdList(const std::string &feedId, std::list<std::st
|
|||
RsFeedReaderFeed *fi = feedIt->second;
|
||||
|
||||
std::map<std::string, RsFeedReaderMsg*>::iterator msgIt;
|
||||
for (msgIt = fi->mMsgs.begin(); msgIt != fi->mMsgs.end(); ++msgIt) {
|
||||
for (msgIt = fi->msgs.begin(); msgIt != fi->msgs.end(); ++msgIt) {
|
||||
RsFeedReaderMsg *mi = msgIt->second;
|
||||
|
||||
if (mi->flag & RS_FEEDMSG_FLAG_DELETED) {
|
||||
|
@ -1060,8 +1066,8 @@ bool p3FeedReader::setMessageRead(const std::string &feedId, const std::string &
|
|||
RsFeedReaderFeed *fi = feedIt->second;
|
||||
|
||||
std::map<std::string, RsFeedReaderMsg*>::iterator msgIt;
|
||||
msgIt = fi->mMsgs.find(msgId);
|
||||
if (msgIt == fi->mMsgs.end()) {
|
||||
msgIt = fi->msgs.find(msgId);
|
||||
if (msgIt == fi->msgs.end()) {
|
||||
#ifdef FEEDREADER_DEBUG
|
||||
std::cerr << "p3FeedReader::setMessageRead - msg " << msgId << " not found" << std::endl;
|
||||
#endif
|
||||
|
@ -1092,6 +1098,11 @@ bool p3FeedReader::setMessageRead(const std::string &feedId, const std::string &
|
|||
return true;
|
||||
}
|
||||
|
||||
RsFeedReaderErrorState p3FeedReader::processXPath(const std::list<std::string> &xpathsToUse, const std::list<std::string> &xpathsToRemove, std::string &description, std::string &errorString)
|
||||
{
|
||||
return p3FeedReaderThread::processXPath(xpathsToUse, xpathsToRemove, description, errorString);
|
||||
}
|
||||
|
||||
/***************************************************************************/
|
||||
/****************************** p3Service **********************************/
|
||||
/***************************************************************************/
|
||||
|
@ -1185,7 +1196,7 @@ void p3FeedReader::cleanFeeds()
|
|||
uint32_t removedMsgs = 0;
|
||||
|
||||
std::map<std::string, RsFeedReaderMsg*>::iterator msgIt;
|
||||
for (msgIt = fi->mMsgs.begin(); msgIt != fi->mMsgs.end(); ) {
|
||||
for (msgIt = fi->msgs.begin(); msgIt != fi->msgs.end(); ) {
|
||||
RsFeedReaderMsg *mi = msgIt->second;
|
||||
|
||||
if (mi->flag & RS_FEEDMSG_FLAG_DELETED) {
|
||||
|
@ -1193,7 +1204,7 @@ void p3FeedReader::cleanFeeds()
|
|||
removedMsgIds.push_back(std::pair<std::string, std::string> (fi->feedId, mi->msgId));
|
||||
delete(mi);
|
||||
std::map<std::string, RsFeedReaderMsg*>::iterator deleteIt = msgIt++;
|
||||
fi->mMsgs.erase(deleteIt);
|
||||
fi->msgs.erase(deleteIt);
|
||||
++removedMsgs;
|
||||
continue;
|
||||
}
|
||||
|
@ -1274,7 +1285,7 @@ bool p3FeedReader::saveList(bool &cleanup, std::list<RsItem *> & saveData)
|
|||
saveData.push_back(fi);
|
||||
|
||||
std::map<std::string, RsFeedReaderMsg*>::iterator it2;
|
||||
for (it2 = fi->mMsgs.begin(); it2 != fi->mMsgs.end(); ++it2) {
|
||||
for (it2 = fi->msgs.begin(); it2 != fi->msgs.end(); ++it2) {
|
||||
saveData.push_back(it2->second);
|
||||
}
|
||||
}
|
||||
|
@ -1376,7 +1387,7 @@ bool p3FeedReader::loadList(std::list<RsItem *>& load)
|
|||
delete it1->second;
|
||||
continue;
|
||||
}
|
||||
it2->second->mMsgs[it1->first] = it1->second;
|
||||
it2->second->msgs[it1->first] = it1->second;
|
||||
if (msgId + 1 > mNextMsgId) {
|
||||
mNextMsgId = msgId + 1;
|
||||
}
|
||||
|
@ -1496,7 +1507,7 @@ void p3FeedReader::onDownloadSuccess(const std::string &feedId, const std::strin
|
|||
}
|
||||
}
|
||||
|
||||
void p3FeedReader::onDownloadError(const std::string &feedId, p3FeedReaderThread::DownloadResult result, const std::string &errorString)
|
||||
void p3FeedReader::onDownloadError(const std::string &feedId, RsFeedReaderErrorState result, const std::string &errorString)
|
||||
{
|
||||
{
|
||||
RsStackMutex stack(mFeedReaderMtx); /******* LOCK STACK MUTEX *********/
|
||||
|
@ -1516,28 +1527,7 @@ void p3FeedReader::onDownloadError(const std::string &feedId, p3FeedReaderThread
|
|||
fi->lastUpdate = time(NULL);
|
||||
fi->content.clear();
|
||||
|
||||
switch (result) {
|
||||
case p3FeedReaderThread::DOWNLOAD_SUCCESS:
|
||||
/* this should not happen */
|
||||
std::cerr << "p3FeedReader::onDownloadError - success given as error" << std::endl;
|
||||
fi->errorState = RS_FEED_ERRORSTATE_DOWNLOAD_INTERNAL_ERROR;
|
||||
break;
|
||||
case p3FeedReaderThread::DOWNLOAD_ERROR:
|
||||
fi->errorState = RS_FEED_ERRORSTATE_DOWNLOAD_ERROR;
|
||||
break;
|
||||
case p3FeedReaderThread::DOWNLOAD_UNKNOWN_CONTENT_TYPE:
|
||||
fi->errorState = RS_FEED_ERRORSTATE_DOWNLOAD_UNKNOWN_CONTENT_TYPE;
|
||||
break;
|
||||
case p3FeedReaderThread::DOWNLOAD_NOT_FOUND:
|
||||
fi->errorState = RS_FEED_ERRORSTATE_DOWNLOAD_NOT_FOUND;
|
||||
break;
|
||||
case p3FeedReaderThread::DOWNLOAD_UNKOWN_RESPONSE_CODE:
|
||||
fi->errorState = RS_FEED_ERRORSTATE_DOWNLOAD_UNKOWN_RESPONSE_CODE;
|
||||
break;
|
||||
default:
|
||||
fi->errorState = RS_FEED_ERRORSTATE_DOWNLOAD_INTERNAL_ERROR;
|
||||
}
|
||||
|
||||
fi->errorState = result;
|
||||
fi->errorString = errorString;
|
||||
|
||||
#ifdef FEEDREADER_DEBUG
|
||||
|
@ -1611,7 +1601,7 @@ bool p3FeedReader::getFeedToProcess(RsFeedReaderFeed &feed, const std::string &n
|
|||
return true;
|
||||
}
|
||||
|
||||
bool p3FeedReader::onProcessSuccess_filterMsg(const std::string &feedId, std::list<RsFeedReaderMsg*> &msgs)
|
||||
void p3FeedReader::onProcessSuccess_filterMsg(const std::string &feedId, std::list<RsFeedReaderMsg*> &msgs)
|
||||
{
|
||||
#ifdef FEEDREADER_DEBUG
|
||||
std::cerr << "p3FeedReader::onProcessSuccess_filterMsg - feed " << feedId << " got " << msgs.size() << " messages" << std::endl;
|
||||
|
@ -1627,7 +1617,7 @@ bool p3FeedReader::onProcessSuccess_filterMsg(const std::string &feedId, std::li
|
|||
#ifdef FEEDREADER_DEBUG
|
||||
std::cerr << "p3FeedReader::onProcessSuccess_filterMsg - feed " << feedId << " not found" << std::endl;
|
||||
#endif
|
||||
return false;
|
||||
return;
|
||||
}
|
||||
|
||||
RsFeedReaderFeed *fi = it->second;
|
||||
|
@ -1637,14 +1627,14 @@ bool p3FeedReader::onProcessSuccess_filterMsg(const std::string &feedId, std::li
|
|||
RsFeedReaderMsg *miNew = *newMsgIt;
|
||||
/* search for existing msg */
|
||||
std::map<std::string, RsFeedReaderMsg*>::iterator msgIt;
|
||||
for (msgIt = fi->mMsgs.begin(); msgIt != fi->mMsgs.end(); ++msgIt) {
|
||||
for (msgIt = fi->msgs.begin(); msgIt != fi->msgs.end(); ++msgIt) {
|
||||
RsFeedReaderMsg *mi = msgIt->second;
|
||||
if (mi->title == miNew->title && mi->link == miNew->link && mi->author == miNew->author) {
|
||||
/* msg exist */
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (msgIt != fi->mMsgs.end()) {
|
||||
if (msgIt != fi->msgs.end()) {
|
||||
/* msg exists */
|
||||
delete(miNew);
|
||||
newMsgIt = msgs.erase(newMsgIt);
|
||||
|
@ -1660,11 +1650,9 @@ bool p3FeedReader::onProcessSuccess_filterMsg(const std::string &feedId, std::li
|
|||
IndicateConfigChanged();
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void p3FeedReader::onProcessSuccess_addMsgs(const std::string &feedId, bool result, std::list<RsFeedReaderMsg*> &msgs, bool single)
|
||||
void p3FeedReader::onProcessSuccess_addMsgs(const std::string &feedId, std::list<RsFeedReaderMsg*> &msgs, bool single)
|
||||
{
|
||||
#ifdef FEEDREADER_DEBUG
|
||||
std::cerr << "p3FeedReader::onProcessSuccess_addMsgs - feed " << feedId << " got " << msgs.size() << " messages" << std::endl;
|
||||
|
@ -1691,7 +1679,7 @@ void p3FeedReader::onProcessSuccess_addMsgs(const std::string &feedId, bool resu
|
|||
bool forum = (fi->flag & RS_FEED_FLAG_FORUM) && !fi->preview;
|
||||
RsFeedReaderErrorState errorState = RS_FEED_ERRORSTATE_OK;
|
||||
|
||||
if (result && forum && !msgs.empty()) {
|
||||
if (forum && !msgs.empty()) {
|
||||
if (fi->forumId.empty()) {
|
||||
/* create new forum */
|
||||
std::wstring forumName;
|
||||
|
@ -1751,35 +1739,33 @@ void p3FeedReader::onProcessSuccess_addMsgs(const std::string &feedId, bool resu
|
|||
uint32_t newMsgs = 0;
|
||||
#endif
|
||||
|
||||
if (result) {
|
||||
std::list<RsFeedReaderMsg*>::iterator newMsgIt;
|
||||
for (newMsgIt = msgs.begin(); newMsgIt != msgs.end(); ) {
|
||||
RsFeedReaderMsg *miNew = *newMsgIt;
|
||||
/* add new msg */
|
||||
if (fi->preview) {
|
||||
rs_sprintf(miNew->msgId, "preview%d", mNextPreviewMsgId--);
|
||||
} else {
|
||||
rs_sprintf(miNew->msgId, "%lu", mNextMsgId++);
|
||||
}
|
||||
if (forum) {
|
||||
miNew->flag = RS_FEEDMSG_FLAG_DELETED;
|
||||
forumMsgs.push_back(*miNew);
|
||||
// miNew->description.clear();
|
||||
} else {
|
||||
miNew->flag = RS_FEEDMSG_FLAG_NEW;
|
||||
addedMsgs.push_back(miNew->msgId);
|
||||
}
|
||||
fi->mMsgs[miNew->msgId] = miNew;
|
||||
newMsgIt = msgs.erase(newMsgIt);
|
||||
std::list<RsFeedReaderMsg*>::iterator newMsgIt;
|
||||
for (newMsgIt = msgs.begin(); newMsgIt != msgs.end(); ) {
|
||||
RsFeedReaderMsg *miNew = *newMsgIt;
|
||||
/* add new msg */
|
||||
if (fi->preview) {
|
||||
rs_sprintf(miNew->msgId, "preview%d", mNextPreviewMsgId--);
|
||||
} else {
|
||||
rs_sprintf(miNew->msgId, "%lu", mNextMsgId++);
|
||||
}
|
||||
if (forum) {
|
||||
miNew->flag = RS_FEEDMSG_FLAG_DELETED;
|
||||
forumMsgs.push_back(*miNew);
|
||||
// miNew->description.clear();
|
||||
} else {
|
||||
miNew->flag = RS_FEEDMSG_FLAG_NEW;
|
||||
addedMsgs.push_back(miNew->msgId);
|
||||
}
|
||||
fi->msgs[miNew->msgId] = miNew;
|
||||
newMsgIt = msgs.erase(newMsgIt);
|
||||
|
||||
#ifdef FEEDREADER_DEBUG
|
||||
++newMsgs;
|
||||
#endif
|
||||
}
|
||||
#ifdef FEEDREADER_DEBUG
|
||||
std::cerr << "p3FeedReader::onProcessSuccess_addMsgs - feed " << fi->feedId << " (" << fi->name << ") added " << newMsgs << "/" << msgs.size() << " messages" << std::endl;
|
||||
++newMsgs;
|
||||
#endif
|
||||
}
|
||||
#ifdef FEEDREADER_DEBUG
|
||||
std::cerr << "p3FeedReader::onProcessSuccess_addMsgs - feed " << fi->feedId << " (" << fi->name << ") added " << newMsgs << "/" << msgs.size() << " messages" << std::endl;
|
||||
#endif
|
||||
}
|
||||
|
||||
if (!single) {
|
||||
|
@ -1833,7 +1819,7 @@ void p3FeedReader::onProcessSuccess_addMsgs(const std::string &feedId, bool resu
|
|||
}
|
||||
}
|
||||
|
||||
void p3FeedReader::onProcessError(const std::string &feedId, p3FeedReaderThread::ProcessResult result, const std::string &errorString)
|
||||
void p3FeedReader::onProcessError(const std::string &feedId, RsFeedReaderErrorState result, const std::string &errorString)
|
||||
{
|
||||
{
|
||||
RsStackMutex stack(mFeedReaderMtx); /******* LOCK STACK MUTEX *********/
|
||||
|
@ -1853,23 +1839,7 @@ void p3FeedReader::onProcessError(const std::string &feedId, p3FeedReaderThread:
|
|||
fi->lastUpdate = time(NULL);
|
||||
fi->content.clear();
|
||||
|
||||
long todo; // sort error codes
|
||||
switch (result) {
|
||||
case p3FeedReaderThread::PROCESS_SUCCESS:
|
||||
/* this should not happen */
|
||||
std::cerr << "p3FeedReader::onProcessError - success given as error" << std::endl;
|
||||
fi->errorState = RS_FEED_ERRORSTATE_PROCESS_INTERNAL_ERROR;
|
||||
break;
|
||||
case p3FeedReaderThread::PROCESS_ERROR_INIT:
|
||||
fi->errorState = RS_FEED_ERRORSTATE_PROCESS_INTERNAL_ERROR;
|
||||
break;
|
||||
case p3FeedReaderThread::PROCESS_UNKNOWN_FORMAT:
|
||||
fi->errorState = RS_FEED_ERRORSTATE_PROCESS_UNKNOWN_FORMAT;
|
||||
break;
|
||||
default:
|
||||
fi->errorState = RS_FEED_ERRORSTATE_PROCESS_INTERNAL_ERROR;
|
||||
}
|
||||
|
||||
fi->errorState = result;
|
||||
fi->errorString = errorString;
|
||||
|
||||
#ifdef FEEDREADER_DEBUG
|
||||
|
|
|
@ -25,13 +25,10 @@
|
|||
#include "retroshare/rsplugin.h"
|
||||
#include "plugins/rspqiservice.h"
|
||||
#include "interface/rsFeedReader.h"
|
||||
#include "p3FeedReaderThread.h"
|
||||
|
||||
class RsFeedReaderFeed;
|
||||
|
||||
//TODO: get new id's
|
||||
const uint8_t RS_PKT_TYPE_FEEDREADER_CONFIG = 0xf0;
|
||||
const uint32_t CONFIG_TYPE_FEEDREADER = 0x0001;
|
||||
class RsFeedReaderMsg;
|
||||
class p3FeedReaderThread;
|
||||
|
||||
class p3FeedReader : public RsPQIService, public RsFeedReader
|
||||
{
|
||||
|
@ -66,16 +63,18 @@ public:
|
|||
virtual bool processFeed(const std::string &feedId);
|
||||
virtual bool setMessageRead(const std::string &feedId, const std::string &msgId, bool read);
|
||||
|
||||
virtual RsFeedReaderErrorState processXPath(const std::list<std::string> &xpathsToUse, const std::list<std::string> &xpathsToRemove, std::string &description, std::string &errorString);
|
||||
|
||||
/****************** p3Service STUFF ******************/
|
||||
virtual int tick();
|
||||
|
||||
/****************** internal STUFF *******************/
|
||||
bool getFeedToDownload(RsFeedReaderFeed &feed, const std::string &neededFeedId);
|
||||
void onDownloadSuccess(const std::string &feedId, const std::string &content, std::string &icon);
|
||||
void onDownloadError(const std::string &feedId, p3FeedReaderThread::DownloadResult result, const std::string &errorString);
|
||||
bool onProcessSuccess_filterMsg(const std::string &feedId, std::list<RsFeedReaderMsg*> &msgs);
|
||||
void onProcessSuccess_addMsgs(const std::string &feedId, bool result, std::list<RsFeedReaderMsg*> &msgs, bool single);
|
||||
void onProcessError(const std::string &feedId, p3FeedReaderThread::ProcessResult result, const std::string &errorString);
|
||||
void onDownloadError(const std::string &feedId, RsFeedReaderErrorState result, const std::string &errorString);
|
||||
void onProcessSuccess_filterMsg(const std::string &feedId, std::list<RsFeedReaderMsg*> &msgs);
|
||||
void onProcessSuccess_addMsgs(const std::string &feedId, std::list<RsFeedReaderMsg*> &msgs, bool single);
|
||||
void onProcessError(const std::string &feedId, RsFeedReaderErrorState result, const std::string &errorString);
|
||||
|
||||
bool getFeedToProcess(RsFeedReaderFeed &feed, const std::string &neededFeedId);
|
||||
|
||||
|
|
|
@ -25,6 +25,7 @@
|
|||
#include "util/CURLWrapper.h"
|
||||
#include "util/XMLWrapper.h"
|
||||
#include "util/HTMLWrapper.h"
|
||||
#include "util/XPathWrapper.h"
|
||||
|
||||
#include <openssl/evp.h>
|
||||
|
||||
|
@ -67,8 +68,8 @@ void p3FeedReaderThread::run()
|
|||
std::string icon;
|
||||
std::string errorString;
|
||||
|
||||
DownloadResult result = download(feed, content, icon, errorString);
|
||||
if (result == DOWNLOAD_SUCCESS) {
|
||||
RsFeedReaderErrorState result = download(feed, content, icon, errorString);
|
||||
if (result == RS_FEED_ERRORSTATE_OK) {
|
||||
mFeedReader->onDownloadSuccess(feed.feedId, content, icon);
|
||||
} else {
|
||||
mFeedReader->onDownloadError(feed.feedId, result, errorString);
|
||||
|
@ -84,37 +85,42 @@ void p3FeedReaderThread::run()
|
|||
std::string errorString;
|
||||
std::list<RsFeedReaderMsg*>::iterator it;
|
||||
|
||||
ProcessResult result = process(feed, msgs, errorString);
|
||||
if (result == PROCESS_SUCCESS) {
|
||||
RsFeedReaderErrorState result = process(feed, msgs, errorString);
|
||||
if (result == RS_FEED_ERRORSTATE_OK) {
|
||||
/* first, filter the messages */
|
||||
bool result = mFeedReader->onProcessSuccess_filterMsg(feed.feedId, msgs);
|
||||
mFeedReader->onProcessSuccess_filterMsg(feed.feedId, msgs);
|
||||
if (isRunning()) {
|
||||
if (result) {
|
||||
/* second, process the descriptions */
|
||||
for (it = msgs.begin(); it != msgs.end(); ) {
|
||||
RsFeedReaderMsg *mi = *it;
|
||||
processMsg(feed, mi);
|
||||
/* second, process the descriptions */
|
||||
for (it = msgs.begin(); it != msgs.end(); ) {
|
||||
RsFeedReaderMsg *mi = *it;
|
||||
result = processMsg(feed, mi, errorString);
|
||||
if (result != RS_FEED_ERRORSTATE_OK) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (feed.preview) {
|
||||
/* add every message */
|
||||
it = msgs.erase(it);
|
||||
if (feed.preview) {
|
||||
/* add every message */
|
||||
it = msgs.erase(it);
|
||||
|
||||
std::list<RsFeedReaderMsg*> msgSingle;
|
||||
msgSingle.push_back(mi);
|
||||
mFeedReader->onProcessSuccess_addMsgs(feed.feedId, result, msgSingle, true);
|
||||
std::list<RsFeedReaderMsg*> msgSingle;
|
||||
msgSingle.push_back(mi);
|
||||
mFeedReader->onProcessSuccess_addMsgs(feed.feedId, msgSingle, true);
|
||||
|
||||
/* delete not accepted message */
|
||||
std::list<RsFeedReaderMsg*>::iterator it1;
|
||||
for (it1 = msgSingle.begin(); it1 != msgSingle.end(); ++it1) {
|
||||
delete (*it1);
|
||||
}
|
||||
} else {
|
||||
++it;
|
||||
/* delete not accepted message */
|
||||
std::list<RsFeedReaderMsg*>::iterator it1;
|
||||
for (it1 = msgSingle.begin(); it1 != msgSingle.end(); ++it1) {
|
||||
delete (*it1);
|
||||
}
|
||||
} else {
|
||||
++it;
|
||||
}
|
||||
}
|
||||
/* third, add messages */
|
||||
mFeedReader->onProcessSuccess_addMsgs(feed.feedId, result, msgs, false);
|
||||
if (result == RS_FEED_ERRORSTATE_OK) {
|
||||
/* third, add messages */
|
||||
mFeedReader->onProcessSuccess_addMsgs(feed.feedId, msgs, false);
|
||||
} else {
|
||||
mFeedReader->onProcessError(feed.feedId, result, errorString);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
mFeedReader->onProcessError(feed.feedId, result, errorString);
|
||||
|
@ -245,7 +251,7 @@ static bool getFavicon(CURLWrapper &CURL, const std::string &url, std::string &i
|
|||
return result;
|
||||
}
|
||||
|
||||
p3FeedReaderThread::DownloadResult p3FeedReaderThread::download(const RsFeedReaderFeed &feed, std::string &content, std::string &icon, std::string &error)
|
||||
RsFeedReaderErrorState p3FeedReaderThread::download(const RsFeedReaderFeed &feed, std::string &content, std::string &icon, std::string &error)
|
||||
{
|
||||
#ifdef FEEDREADER_DEBUG
|
||||
std::cerr << "p3FeedReaderThread::download - feed " << feed.feedId << " (" << feed.name << ")" << std::endl;
|
||||
|
@ -254,7 +260,7 @@ p3FeedReaderThread::DownloadResult p3FeedReaderThread::download(const RsFeedRead
|
|||
content.clear();
|
||||
error.clear();
|
||||
|
||||
DownloadResult result;
|
||||
RsFeedReaderErrorState result;
|
||||
|
||||
std::string proxy = getProxyForFeed(feed);
|
||||
CURLWrapper CURL(proxy);
|
||||
|
@ -273,24 +279,24 @@ p3FeedReaderThread::DownloadResult p3FeedReaderThread::download(const RsFeedRead
|
|||
isContentType(contentType, "application/xml") ||
|
||||
isContentType(contentType, "application/xhtml+xml")) {
|
||||
/* ok */
|
||||
result = DOWNLOAD_SUCCESS;
|
||||
result = RS_FEED_ERRORSTATE_OK;
|
||||
} else {
|
||||
result = DOWNLOAD_UNKNOWN_CONTENT_TYPE;
|
||||
result = RS_FEED_ERRORSTATE_DOWNLOAD_UNKNOWN_CONTENT_TYPE;
|
||||
error = contentType;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 404:
|
||||
result = DOWNLOAD_NOT_FOUND;
|
||||
result = RS_FEED_ERRORSTATE_DOWNLOAD_NOT_FOUND;
|
||||
break;
|
||||
default:
|
||||
result = DOWNLOAD_UNKOWN_RESPONSE_CODE;
|
||||
result = RS_FEED_ERRORSTATE_DOWNLOAD_UNKOWN_RESPONSE_CODE;
|
||||
rs_sprintf(error, "%ld", responseCode);
|
||||
}
|
||||
|
||||
getFavicon(CURL, feed.url, icon);
|
||||
} else {
|
||||
result = DOWNLOAD_ERROR;
|
||||
result = RS_FEED_ERRORSTATE_DOWNLOAD_ERROR;
|
||||
error = curl_easy_strerror(code);
|
||||
}
|
||||
|
||||
|
@ -326,7 +332,7 @@ static xmlNodePtr getNextItem(FeedFormat feedFormat, xmlNodePtr channel, xmlNode
|
|||
item = item->next;
|
||||
}
|
||||
for (; item; item = item->next) {
|
||||
if (item->type == XML_ELEMENT_NODE && xmlStrcasecmp(item->name, (const xmlChar*) "item") == 0) {
|
||||
if (item->type == XML_ELEMENT_NODE && xmlStrEqual(item->name, BAD_CAST"item")) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -789,29 +795,29 @@ static time_t parseISO8601Date(const std::string &pubDate)
|
|||
return result;
|
||||
}
|
||||
|
||||
p3FeedReaderThread::ProcessResult p3FeedReaderThread::process(const RsFeedReaderFeed &feed, std::list<RsFeedReaderMsg*> &entries, std::string &error)
|
||||
RsFeedReaderErrorState p3FeedReaderThread::process(const RsFeedReaderFeed &feed, std::list<RsFeedReaderMsg*> &entries, std::string &error)
|
||||
{
|
||||
#ifdef FEEDREADER_DEBUG
|
||||
std::cerr << "p3FeedReaderThread::process - feed " << feed.feedId << " (" << feed.name << ")" << std::endl;
|
||||
#endif
|
||||
|
||||
ProcessResult result = PROCESS_SUCCESS;
|
||||
RsFeedReaderErrorState result = RS_FEED_ERRORSTATE_OK;
|
||||
|
||||
XMLWrapper xml;
|
||||
if (xml.readXML(feed.content.c_str())) {
|
||||
xmlNodePtr root = xml.getRootElement();
|
||||
if (root) {
|
||||
FeedFormat feedFormat;
|
||||
if (xmlStrcasecmp(root->name, (const xmlChar*) "rss") == 0) {
|
||||
if (xmlStrEqual(root->name, BAD_CAST"rss")) {
|
||||
feedFormat = FORMAT_RSS;
|
||||
} else if (xmlStrcasecmp (root->name, (const xmlChar*) "rdf") != 0) {
|
||||
} else if (xmlStrEqual (root->name, BAD_CAST"rdf")) {
|
||||
feedFormat = FORMAT_RDF;
|
||||
} else {
|
||||
result = PROCESS_UNKNOWN_FORMAT;
|
||||
result = RS_FEED_ERRORSTATE_PROCESS_UNKNOWN_FORMAT;
|
||||
error = "Only RSS or RDF supported";
|
||||
}
|
||||
|
||||
if (result == PROCESS_SUCCESS) {
|
||||
if (result == RS_FEED_ERRORSTATE_OK) {
|
||||
xmlNodePtr channel = xml.findNode(root->children, "channel");
|
||||
if (channel) {
|
||||
/* import header info */
|
||||
|
@ -900,16 +906,16 @@ p3FeedReaderThread::ProcessResult p3FeedReaderThread::process(const RsFeedReader
|
|||
entries.push_back(item);
|
||||
}
|
||||
} else {
|
||||
result = PROCESS_UNKNOWN_FORMAT;
|
||||
result = RS_FEED_ERRORSTATE_PROCESS_UNKNOWN_FORMAT;
|
||||
error = "Channel not found";
|
||||
}
|
||||
}
|
||||
} else {
|
||||
result = PROCESS_UNKNOWN_FORMAT;
|
||||
result = RS_FEED_ERRORSTATE_PROCESS_UNKNOWN_FORMAT;
|
||||
error = "Can't read document";
|
||||
}
|
||||
} else {
|
||||
result = PROCESS_ERROR_INIT;
|
||||
result = RS_FEED_ERRORSTATE_PROCESS_INTERNAL_ERROR;
|
||||
}
|
||||
|
||||
#ifdef FEEDREADER_DEBUG
|
||||
|
@ -936,12 +942,15 @@ std::string p3FeedReaderThread::getProxyForFeed(const RsFeedReaderFeed &feed)
|
|||
return proxy;
|
||||
}
|
||||
|
||||
bool p3FeedReaderThread::processMsg(const RsFeedReaderFeed &feed, RsFeedReaderMsg *msg)
|
||||
RsFeedReaderErrorState p3FeedReaderThread::processMsg(const RsFeedReaderFeed &feed, RsFeedReaderMsg *msg, std::string &errorString)
|
||||
{
|
||||
long todo_fill_errorString;
|
||||
|
||||
if (!msg) {
|
||||
return false;
|
||||
return RS_FEED_ERRORSTATE_PROCESS_INTERNAL_ERROR;
|
||||
}
|
||||
|
||||
RsFeedReaderErrorState result = RS_FEED_ERRORSTATE_OK;
|
||||
std::string proxy = getProxyForFeed(feed);
|
||||
|
||||
std::string url;
|
||||
|
@ -953,14 +962,40 @@ bool p3FeedReaderThread::processMsg(const RsFeedReaderFeed &feed, RsFeedReaderMs
|
|||
CURLWrapper CURL(proxy);
|
||||
CURLcode code = CURL.downloadText(msg->link, content);
|
||||
|
||||
if (code == CURLE_OK && CURL.responseCode() == 200 && isContentType(CURL.contentType(), "text/html")) {
|
||||
/* ok */
|
||||
msg->description = content;
|
||||
if (code == CURLE_OK) {
|
||||
long responseCode = CURL.responseCode();
|
||||
|
||||
switch (responseCode) {
|
||||
case 200:
|
||||
{
|
||||
std::string contentType = CURL.contentType();
|
||||
|
||||
if (isContentType(CURL.contentType(), "text/html")) {
|
||||
/* ok */
|
||||
msg->description = content;
|
||||
} else {
|
||||
result = RS_FEED_ERRORSTATE_DOWNLOAD_UNKNOWN_CONTENT_TYPE;
|
||||
errorString = contentType;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 404:
|
||||
result = RS_FEED_ERRORSTATE_DOWNLOAD_NOT_FOUND;
|
||||
break;
|
||||
default:
|
||||
result = RS_FEED_ERRORSTATE_DOWNLOAD_UNKOWN_RESPONSE_CODE;
|
||||
rs_sprintf(errorString, "%ld", responseCode);
|
||||
}
|
||||
} else {
|
||||
result = RS_FEED_ERRORSTATE_DOWNLOAD_ERROR;
|
||||
errorString = curl_easy_strerror(code);
|
||||
}
|
||||
|
||||
if (result != RS_FEED_ERRORSTATE_OK) {
|
||||
#ifdef FEEDREADER_DEBUG
|
||||
std::cerr << "p3FeedReaderThread::processHTML - feed " << feed.feedId << " (" << feed.name << ") cannot download page, CURLCode = " << code << ", responseCode = " << CURL.responseCode() << ", contentType = " << CURL.contentType() << std::endl;
|
||||
std::cerr << "p3FeedReaderThread::processHTML - feed " << feed.feedId << " (" << feed.name << ") cannot download page, CURLCode = " << code << ", error = " << errorString << std::endl;
|
||||
#endif
|
||||
return false;
|
||||
return result;
|
||||
}
|
||||
|
||||
/* get effective url (moved location) */
|
||||
|
@ -970,89 +1005,355 @@ bool p3FeedReaderThread::processMsg(const RsFeedReaderFeed &feed, RsFeedReaderMs
|
|||
|
||||
/* check if string contains xml chars (very simple test) */
|
||||
if (msg->description.find('<') == std::string::npos) {
|
||||
return true;
|
||||
return result;
|
||||
}
|
||||
|
||||
bool result = true;
|
||||
|
||||
/* process description */
|
||||
long todo; // encoding
|
||||
HTMLWrapper html;
|
||||
if (html.readHTML(msg->description.c_str(), url.c_str())) {
|
||||
xmlNodePtr root = html.getRootElement();
|
||||
if (root) {
|
||||
/* process all children */
|
||||
std::list<xmlNodePtr> parents;
|
||||
parents.push_back(root);
|
||||
std::list<xmlNodePtr> nodesToDelete;
|
||||
|
||||
while (!parents.empty()) {
|
||||
/* process all children */
|
||||
std::list<xmlNodePtr> nodes;
|
||||
nodes.push_back(root);
|
||||
|
||||
while (!nodes.empty()) {
|
||||
if (!isRunning()) {
|
||||
break;
|
||||
}
|
||||
xmlNodePtr node = parents.front();
|
||||
parents.pop_front();
|
||||
xmlNodePtr node = nodes.front();
|
||||
nodes.pop_front();
|
||||
|
||||
if (node->type == XML_ELEMENT_NODE) {
|
||||
/* check for image */
|
||||
if (xmlStrcasecmp(node->name, (const xmlChar*) "img") == 0) {
|
||||
bool removeImage = true;
|
||||
switch (node->type) {
|
||||
case XML_ELEMENT_NODE:
|
||||
if (xmlStrEqual(node->name, BAD_CAST"img")) {
|
||||
/* process images */
|
||||
|
||||
if (feed.flag & RS_FEED_FLAG_EMBED_IMAGES) {
|
||||
/* embed image */
|
||||
std::string src = html.getAttr(node, "src");
|
||||
if (!src.empty()) {
|
||||
/* download image */
|
||||
#ifdef FEEDREADER_DEBUG
|
||||
std::cerr << "p3FeedReaderThread::processHTML - feed " << feed.feedId << " (" << feed.name << ") download image " << src << std::endl;
|
||||
#endif
|
||||
std::vector<unsigned char> data;
|
||||
CURLWrapper CURL(proxy);
|
||||
CURLcode code = CURL.downloadBinary(calculateLink(url, src), data);
|
||||
if (code == CURLE_OK && CURL.responseCode() == 200) {
|
||||
std::string contentType = CURL.contentType();
|
||||
if (isContentType(contentType, "image/")) {
|
||||
std::string base64;
|
||||
if (toBase64(data, base64)) {
|
||||
std::string imageBase64;
|
||||
rs_sprintf(imageBase64, "data:%s;base64,%s", contentType.c_str(), base64.c_str());
|
||||
if (html.setAttr(node, "src", imageBase64.c_str())) {
|
||||
removeImage = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (removeImage) {
|
||||
if ((feed.flag & RS_FEED_FLAG_EMBED_IMAGES) == 0) {
|
||||
/* remove image */
|
||||
xmlUnlinkNode(node);
|
||||
xmlFreeNode(node);
|
||||
nodesToDelete.push_back(node);
|
||||
continue;
|
||||
}
|
||||
} else if (xmlStrEqual(node->name, BAD_CAST"script")) {
|
||||
/* remove script */
|
||||
xmlUnlinkNode(node);
|
||||
nodesToDelete.push_back(node);
|
||||
continue;
|
||||
}
|
||||
|
||||
xmlNodePtr child;
|
||||
for (child = node->children; child; child = child->next) {
|
||||
parents.push_back(child);
|
||||
nodes.push_back(child);
|
||||
}
|
||||
break;
|
||||
|
||||
case XML_TEXT_NODE:
|
||||
{
|
||||
/* check for only space */
|
||||
std::string content;
|
||||
if (html.getContent(node, content)) {
|
||||
std::string newContent = content;
|
||||
|
||||
/* trim left */
|
||||
std::string::size_type find = newContent.find_first_not_of(" \t\r\n");
|
||||
if (find != std::string::npos) {
|
||||
newContent.erase(0, find);
|
||||
|
||||
/* trim right */
|
||||
find = newContent.find_last_not_of(" \t\r\n");
|
||||
if (find != std::string::npos) {
|
||||
newContent.erase(find + 1);
|
||||
}
|
||||
} else {
|
||||
newContent.clear();
|
||||
}
|
||||
|
||||
if (newContent.empty()) {
|
||||
xmlUnlinkNode(node);
|
||||
nodesToDelete.push_back(node);
|
||||
} else {
|
||||
if (content != newContent) {
|
||||
html.setContent(node, newContent.c_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case XML_COMMENT_NODE:
|
||||
// xmlUnlinkNode(node);
|
||||
// nodesToDelete.push_back(node);
|
||||
break;
|
||||
|
||||
case XML_ATTRIBUTE_NODE:
|
||||
case XML_CDATA_SECTION_NODE:
|
||||
case XML_ENTITY_REF_NODE:
|
||||
case XML_ENTITY_NODE:
|
||||
case XML_PI_NODE:
|
||||
case XML_DOCUMENT_NODE:
|
||||
case XML_DOCUMENT_TYPE_NODE:
|
||||
case XML_DOCUMENT_FRAG_NODE:
|
||||
case XML_NOTATION_NODE:
|
||||
case XML_HTML_DOCUMENT_NODE:
|
||||
case XML_DTD_NODE:
|
||||
case XML_ELEMENT_DECL:
|
||||
case XML_ATTRIBUTE_DECL:
|
||||
case XML_ENTITY_DECL:
|
||||
case XML_NAMESPACE_DECL:
|
||||
case XML_XINCLUDE_START:
|
||||
case XML_XINCLUDE_END:
|
||||
#ifdef LIBXML_DOCB_ENABLED
|
||||
case XML_DOCB_DOCUMENT_NODE:
|
||||
#endif
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (isRunning()) {
|
||||
if (!html.saveHTML(msg->description)) {
|
||||
#ifdef FEEDREADER_DEBUG
|
||||
std::cerr << "p3FeedReaderThread::processHTML - feed " << feed.feedId << " (" << feed.name << ") cannot dump html" << std::endl;
|
||||
#endif
|
||||
result = false;
|
||||
}
|
||||
} else {
|
||||
#ifdef FEEDREADER_DEBUG
|
||||
std::cerr << "p3FeedReaderThread::processHTML - feed " << feed.feedId << " (" << feed.name << ") no root element" << std::endl;
|
||||
#endif
|
||||
result = false;
|
||||
std::list<xmlNodePtr>::iterator nodeIt;
|
||||
for (nodeIt = nodesToDelete.begin(); nodeIt != nodesToDelete.end(); ++nodeIt) {
|
||||
xmlFreeNode(*nodeIt);
|
||||
}
|
||||
nodesToDelete.clear();
|
||||
|
||||
if (!feed.preview) {
|
||||
result = processXPath(feed.xpathsToUse.ids, feed.xpathsToRemove.ids, html, errorString);
|
||||
}
|
||||
|
||||
if (result == RS_FEED_ERRORSTATE_OK) {
|
||||
unsigned int xpathCount;
|
||||
unsigned int xpathIndex;
|
||||
XPathWrapper *xpath = html.createXPath();
|
||||
if (xpath) {
|
||||
/* process images */
|
||||
if (xpath->compile("//img")) {
|
||||
xpathCount = xpath->count();
|
||||
for (xpathIndex = 0; xpathIndex < xpathCount; ++xpathIndex) {
|
||||
xmlNodePtr node = xpath->node(xpathIndex);
|
||||
|
||||
if (node->type == XML_ELEMENT_NODE) {
|
||||
bool removeImage = true;
|
||||
|
||||
if (feed.flag & RS_FEED_FLAG_EMBED_IMAGES) {
|
||||
/* embed image */
|
||||
std::string src = html.getAttr(node, "src");
|
||||
if (!src.empty()) {
|
||||
/* download image */
|
||||
#ifdef FEEDREADER_DEBUG
|
||||
std::cerr << "p3FeedReaderThread::processHTML - feed " << feed.feedId << " (" << feed.name << ") download image " << src << std::endl;
|
||||
#endif
|
||||
std::vector<unsigned char> data;
|
||||
CURLWrapper CURL(proxy);
|
||||
CURLcode code = CURL.downloadBinary(calculateLink(url, src), data);
|
||||
if (code == CURLE_OK && CURL.responseCode() == 200) {
|
||||
std::string contentType = CURL.contentType();
|
||||
if (isContentType(contentType, "image/")) {
|
||||
std::string base64;
|
||||
if (toBase64(data, base64)) {
|
||||
std::string imageBase64;
|
||||
rs_sprintf(imageBase64, "data:%s;base64,%s", contentType.c_str(), base64.c_str());
|
||||
if (html.setAttr(node, "src", imageBase64.c_str())) {
|
||||
removeImage = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (removeImage) {
|
||||
/* remove image */
|
||||
xmlUnlinkNode(node);
|
||||
nodesToDelete.push_back(node);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// unable to compile xpath expression
|
||||
result = RS_FEED_ERRORSTATE_PROCESS_XPATH_INTERNAL_ERROR;
|
||||
}
|
||||
delete(xpath);
|
||||
xpath = NULL;
|
||||
} else {
|
||||
// unable to create xpath object
|
||||
result = RS_FEED_ERRORSTATE_PROCESS_XPATH_INTERNAL_ERROR;
|
||||
std::cerr << "p3FeedReaderThread::process - feed " << feed.feedId << " (" << feed.name << "), unable to create xpath object" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
for (nodeIt = nodesToDelete.begin(); nodeIt != nodesToDelete.end(); ++nodeIt) {
|
||||
xmlFreeNode(*nodeIt);
|
||||
}
|
||||
nodesToDelete.clear();
|
||||
|
||||
if (result == RS_FEED_ERRORSTATE_OK) {
|
||||
if (isRunning()) {
|
||||
if (!html.saveHTML(msg->description)) {
|
||||
#ifdef FEEDREADER_DEBUG
|
||||
std::cerr << "p3FeedReaderThread::processHTML - feed " << feed.feedId << " (" << feed.name << ") cannot dump html" << std::endl;
|
||||
#endif
|
||||
result = RS_FEED_ERRORSTATE_PROCESS_INTERNAL_ERROR;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
#ifdef FEEDREADER_DEBUG
|
||||
std::cerr << "p3FeedReaderThread::processHTML - feed " << feed.feedId << " (" << feed.name << ") no root element" << std::endl;
|
||||
#endif
|
||||
result = RS_FEED_ERRORSTATE_PROCESS_HTML_ERROR;
|
||||
}
|
||||
} else {
|
||||
#ifdef FEEDREADER_DEBUG
|
||||
std::cerr << "p3FeedReaderThread::processHTML - feed " << feed.feedId << " (" << feed.name << ") cannot read html" << std::endl;
|
||||
#endif
|
||||
result = RS_FEED_ERRORSTATE_PROCESS_HTML_ERROR;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
RsFeedReaderErrorState p3FeedReaderThread::processXPath(const std::list<std::string> &xpathsToUse, const std::list<std::string> &xpathsToRemove, HTMLWrapper &html, std::string &errorString)
|
||||
{
|
||||
long todo_fill_errorString;
|
||||
|
||||
if (xpathsToUse.empty() && xpathsToRemove.empty()) {
|
||||
return RS_FEED_ERRORSTATE_OK;
|
||||
}
|
||||
|
||||
XPathWrapper *xpath = html.createXPath();
|
||||
if (xpath == NULL) {
|
||||
// unable to create xpath object
|
||||
std::cerr << "p3FeedReaderThread::processXPath - unable to create xpath object" << std::endl;
|
||||
return RS_FEED_ERRORSTATE_PROCESS_XPATH_INTERNAL_ERROR;
|
||||
}
|
||||
|
||||
RsFeedReaderErrorState result = RS_FEED_ERRORSTATE_OK;
|
||||
|
||||
unsigned int xpathCount;
|
||||
unsigned int xpathIndex;
|
||||
std::list<std::string>::const_iterator xpathIt;
|
||||
|
||||
if (!xpathsToUse.empty()) {
|
||||
HTMLWrapper htmlNew;
|
||||
if (htmlNew.createHTML()) {
|
||||
xmlNodePtr body = htmlNew.getBody();
|
||||
if (body) {
|
||||
/* process use list */
|
||||
for (xpathIt = xpathsToUse.begin(); xpathIt != xpathsToUse.end(); ++xpathIt) {
|
||||
if (xpath->compile(xpathIt->c_str())) {
|
||||
xpathCount = xpath->count();
|
||||
if (xpathCount) {
|
||||
for (xpathIndex = 0; xpathIndex < xpathCount; ++xpathIndex) {
|
||||
xmlNodePtr node = xpath->node(xpathIndex);
|
||||
xmlUnlinkNode(node);
|
||||
xmlAddChild(body, node);
|
||||
}
|
||||
} else {
|
||||
result = RS_FEED_ERRORSTATE_PROCESS_XPATH_NO_RESULT;
|
||||
errorString = *xpathIt;
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
// unable to process xpath expression
|
||||
#ifdef FEEDREADER_DEBUG
|
||||
std::cerr << "p3FeedReaderThread::processXPath - unable to process xpath expression" << std::endl;
|
||||
#endif
|
||||
errorString = *xpathIt;
|
||||
result = RS_FEED_ERRORSTATE_PROCESS_XPATH_WRONG_EXPRESSION;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
result = RS_FEED_ERRORSTATE_PROCESS_HTML_ERROR;
|
||||
}
|
||||
} else {
|
||||
result = RS_FEED_ERRORSTATE_PROCESS_HTML_ERROR;
|
||||
}
|
||||
|
||||
if (result == RS_FEED_ERRORSTATE_OK) {
|
||||
html = htmlNew;
|
||||
}
|
||||
}
|
||||
|
||||
if (result == RS_FEED_ERRORSTATE_OK) {
|
||||
std::list<xmlNodePtr> nodesToDelete;
|
||||
|
||||
/* process remove list */
|
||||
for (xpathIt = xpathsToRemove.begin(); xpathIt != xpathsToRemove.end(); ++xpathIt) {
|
||||
if (xpath->compile(xpathIt->c_str())) {
|
||||
xpathCount = xpath->count();
|
||||
if (xpathCount) {
|
||||
for (xpathIndex = 0; xpathIndex < xpathCount; ++xpathIndex) {
|
||||
xmlNodePtr node = xpath->node(xpathIndex);
|
||||
|
||||
xmlUnlinkNode(node);
|
||||
nodesToDelete.push_back(node);
|
||||
}
|
||||
} else {
|
||||
result = RS_FEED_ERRORSTATE_PROCESS_XPATH_NO_RESULT;
|
||||
errorString = *xpathIt;
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
// unable to process xpath expression
|
||||
#ifdef FEEDREADER_DEBUG
|
||||
std::cerr << "p3FeedReaderThread::processXPath - unable to process xpath expression" << std::endl;
|
||||
#endif
|
||||
errorString = *xpathIt;
|
||||
return RS_FEED_ERRORSTATE_PROCESS_XPATH_WRONG_EXPRESSION;
|
||||
}
|
||||
}
|
||||
|
||||
std::list<xmlNodePtr>::iterator nodeIt;
|
||||
for (nodeIt = nodesToDelete.begin(); nodeIt != nodesToDelete.end(); ++nodeIt) {
|
||||
xmlFreeNode(*nodeIt);
|
||||
}
|
||||
nodesToDelete.clear();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
RsFeedReaderErrorState p3FeedReaderThread::processXPath(const std::list<std::string> &xpathsToUse, const std::list<std::string> &xpathsToRemove, std::string &description, std::string &errorString)
|
||||
{
|
||||
if (xpathsToUse.empty() && xpathsToRemove.empty()) {
|
||||
return RS_FEED_ERRORSTATE_OK;
|
||||
}
|
||||
|
||||
RsFeedReaderErrorState result = RS_FEED_ERRORSTATE_OK;
|
||||
|
||||
long todo_fill_errorString;
|
||||
|
||||
/* process description */
|
||||
long todo; // encoding
|
||||
HTMLWrapper html;
|
||||
if (html.readHTML(description.c_str(), "")) {
|
||||
xmlNodePtr root = html.getRootElement();
|
||||
if (root) {
|
||||
result = processXPath(xpathsToUse, xpathsToRemove, html, errorString);
|
||||
|
||||
if (result == RS_FEED_ERRORSTATE_OK) {
|
||||
if (!html.saveHTML(description)) {
|
||||
#ifdef FEEDREADER_DEBUG
|
||||
std::cerr << "p3FeedReaderThread::processXPath - cannot dump html" << std::endl;
|
||||
#endif
|
||||
result = RS_FEED_ERRORSTATE_PROCESS_INTERNAL_ERROR;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
#ifdef FEEDREADER_DEBUG
|
||||
std::cerr << "p3FeedReaderThread::processXPath - no root element" << std::endl;
|
||||
#endif
|
||||
result = RS_FEED_ERRORSTATE_PROCESS_HTML_ERROR;
|
||||
}
|
||||
} else {
|
||||
#ifdef FEEDREADER_DEBUG
|
||||
std::cerr << "p3FeedReaderThread::processXPath - cannot read html" << std::endl;
|
||||
#endif
|
||||
result = RS_FEED_ERRORSTATE_PROCESS_HTML_ERROR;
|
||||
}
|
||||
|
||||
return result;
|
||||
|
|
|
@ -22,12 +22,16 @@
|
|||
#ifndef P3_FEEDREADERTHREAD
|
||||
#define P3_FEEDREADERTHREAD
|
||||
|
||||
#include "interface/rsFeedReader.h"
|
||||
|
||||
#include "util/rsthreads.h"
|
||||
#include <list>
|
||||
|
||||
class p3FeedReader;
|
||||
class RsFeedReaderFeed;
|
||||
class RsFeedReaderMsg;
|
||||
class HTMLWrapper;
|
||||
class RsFeedReaderXPath;
|
||||
|
||||
class p3FeedReaderThread : public RsThread
|
||||
{
|
||||
|
@ -37,20 +41,6 @@ public:
|
|||
DOWNLOAD,
|
||||
PROCESS
|
||||
};
|
||||
enum DownloadResult
|
||||
{
|
||||
DOWNLOAD_SUCCESS,
|
||||
DOWNLOAD_ERROR,
|
||||
DOWNLOAD_UNKNOWN_CONTENT_TYPE,
|
||||
DOWNLOAD_NOT_FOUND,
|
||||
DOWNLOAD_UNKOWN_RESPONSE_CODE
|
||||
};
|
||||
enum ProcessResult
|
||||
{
|
||||
PROCESS_SUCCESS,
|
||||
PROCESS_ERROR_INIT,
|
||||
PROCESS_UNKNOWN_FORMAT
|
||||
};
|
||||
|
||||
public:
|
||||
p3FeedReaderThread(p3FeedReader *feedReader, Type type, const std::string &feedId);
|
||||
|
@ -58,14 +48,17 @@ public:
|
|||
|
||||
std::string getFeedId() { return mFeedId; }
|
||||
|
||||
static RsFeedReaderErrorState processXPath(const std::list<std::string> &xpathsToUse, const std::list<std::string> &xpathsToRemove, std::string &description, std::string &errorString);
|
||||
static RsFeedReaderErrorState processXPath(const std::list<std::string> &xpathsToUse, const std::list<std::string> &xpathsToRemove, HTMLWrapper &html, std::string &errorString);
|
||||
|
||||
private:
|
||||
virtual void run();
|
||||
|
||||
DownloadResult download(const RsFeedReaderFeed &feed, std::string &content, std::string &icon, std::string &error);
|
||||
ProcessResult process(const RsFeedReaderFeed &feed, std::list<RsFeedReaderMsg*> &entries, std::string &error);
|
||||
RsFeedReaderErrorState download(const RsFeedReaderFeed &feed, std::string &content, std::string &icon, std::string &error);
|
||||
RsFeedReaderErrorState process(const RsFeedReaderFeed &feed, std::list<RsFeedReaderMsg*> &entries, std::string &error);
|
||||
|
||||
std::string getProxyForFeed(const RsFeedReaderFeed &feed);
|
||||
bool processMsg(const RsFeedReaderFeed &feed, RsFeedReaderMsg *msg);
|
||||
RsFeedReaderErrorState processMsg(const RsFeedReaderFeed &feed, RsFeedReaderMsg *msg, std::string &errorString);
|
||||
|
||||
p3FeedReader *mFeedReader;
|
||||
Type mType;
|
||||
|
|
|
@ -25,7 +25,9 @@
|
|||
|
||||
/*************************************************************************/
|
||||
|
||||
RsFeedReaderFeed::RsFeedReaderFeed() : RsItem(RS_PKT_VERSION1, RS_PKT_CLASS_CONFIG, RS_PKT_TYPE_FEEDREADER_CONFIG, RS_PKT_SUBTYPE_FEEDREADER_FEED)
|
||||
RsFeedReaderFeed::RsFeedReaderFeed()
|
||||
: RsItem(RS_PKT_VERSION_SERVICE, RS_SERVICE_TYPE_PLUGIN_FEEDREADER, RS_PKT_SUBTYPE_FEEDREADER_FEED),
|
||||
xpathsToUse(TLV_TYPE_STRINGSET), xpathsToRemove(TLV_TYPE_STRINGSET)
|
||||
{
|
||||
clear();
|
||||
}
|
||||
|
@ -49,6 +51,8 @@ void RsFeedReaderFeed::clear()
|
|||
icon.clear();
|
||||
errorState = RS_FEED_ERRORSTATE_OK;
|
||||
errorString.clear();
|
||||
xpathsToUse.ids.clear();
|
||||
xpathsToRemove.ids.clear();
|
||||
|
||||
preview = false;
|
||||
workstate = WAITING;
|
||||
|
@ -63,6 +67,7 @@ std::ostream &RsFeedReaderFeed::print(std::ostream &out, uint16_t /*indent*/)
|
|||
uint32_t RsFeedReaderSerialiser::sizeFeed(RsFeedReaderFeed *item)
|
||||
{
|
||||
uint32_t s = 8; /* header */
|
||||
s += 2; /* version */
|
||||
s += GetTlvStringSize(item->feedId);
|
||||
s += GetTlvStringSize(item->parentId);
|
||||
s += GetTlvStringSize(item->url);
|
||||
|
@ -80,6 +85,8 @@ uint32_t RsFeedReaderSerialiser::sizeFeed(RsFeedReaderFeed *item)
|
|||
s += GetTlvStringSize(item->forumId);
|
||||
s += sizeof(uint32_t); /* errorstate */
|
||||
s += GetTlvStringSize(item->errorString);
|
||||
s += item->xpathsToUse.TlvSize();
|
||||
s += item->xpathsToRemove.TlvSize();
|
||||
|
||||
return s;
|
||||
}
|
||||
|
@ -103,6 +110,7 @@ bool RsFeedReaderSerialiser::serialiseFeed(RsFeedReaderFeed *item, void *data, u
|
|||
offset += 8;
|
||||
|
||||
/* add values */
|
||||
ok &= setRawUInt16(data, tlvsize, &offset, 0); /* version */
|
||||
ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_GENID, item->feedId);
|
||||
ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_VALUE, item->parentId);
|
||||
ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_LINK, item->url);
|
||||
|
@ -120,6 +128,8 @@ bool RsFeedReaderSerialiser::serialiseFeed(RsFeedReaderFeed *item, void *data, u
|
|||
ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_VALUE, item->forumId);
|
||||
ok &= setRawUInt32(data, tlvsize, &offset, item->errorState);
|
||||
ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_VALUE, item->errorString);
|
||||
ok &= item->xpathsToUse.SetTlv(data, tlvsize, &offset);
|
||||
ok &= item->xpathsToRemove.SetTlv(data, tlvsize, &offset);
|
||||
|
||||
if (offset != tlvsize)
|
||||
{
|
||||
|
@ -138,9 +148,8 @@ RsFeedReaderFeed *RsFeedReaderSerialiser::deserialiseFeed(void *data, uint32_t *
|
|||
|
||||
uint32_t offset = 0;
|
||||
|
||||
if ((RS_PKT_VERSION1 != getRsItemVersion(rstype)) ||
|
||||
(RS_PKT_CLASS_CONFIG != getRsItemClass(rstype)) ||
|
||||
(RS_PKT_TYPE_FEEDREADER_CONFIG != getRsItemType(rstype)) ||
|
||||
if ((RS_PKT_VERSION_SERVICE != getRsItemVersion(rstype)) ||
|
||||
(RS_SERVICE_TYPE_PLUGIN_FEEDREADER != getRsItemService(rstype)) ||
|
||||
(RS_PKT_SUBTYPE_FEEDREADER_FEED != getRsItemSubType(rstype)))
|
||||
{
|
||||
return NULL; /* wrong type */
|
||||
|
@ -162,6 +171,8 @@ RsFeedReaderFeed *RsFeedReaderSerialiser::deserialiseFeed(void *data, uint32_t *
|
|||
offset += 8;
|
||||
|
||||
/* get values */
|
||||
uint16_t version = 0;
|
||||
ok &= getRawUInt16(data, rssize, &offset, &version);
|
||||
ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_GENID, item->feedId);
|
||||
ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_VALUE, item->parentId);
|
||||
ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_LINK, item->url);
|
||||
|
@ -181,6 +192,8 @@ RsFeedReaderFeed *RsFeedReaderSerialiser::deserialiseFeed(void *data, uint32_t *
|
|||
ok &= getRawUInt32(data, rssize, &offset, &errorState);
|
||||
item->errorState = (RsFeedReaderErrorState) errorState;
|
||||
ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_VALUE, item->errorString);
|
||||
ok &= item->xpathsToUse.GetTlv(data, rssize, &offset);
|
||||
ok &= item->xpathsToRemove.GetTlv(data, rssize, &offset);
|
||||
|
||||
if (offset != rssize)
|
||||
{
|
||||
|
@ -200,7 +213,7 @@ RsFeedReaderFeed *RsFeedReaderSerialiser::deserialiseFeed(void *data, uint32_t *
|
|||
|
||||
/*************************************************************************/
|
||||
|
||||
RsFeedReaderMsg::RsFeedReaderMsg() : RsItem(RS_PKT_VERSION1, RS_PKT_CLASS_CONFIG, RS_PKT_TYPE_FEEDREADER_CONFIG, RS_PKT_SUBTYPE_FEEDREADER_MSG)
|
||||
RsFeedReaderMsg::RsFeedReaderMsg() : RsItem(RS_PKT_VERSION_SERVICE, RS_SERVICE_TYPE_PLUGIN_FEEDREADER, RS_PKT_SUBTYPE_FEEDREADER_MSG)
|
||||
{
|
||||
clear();
|
||||
}
|
||||
|
@ -225,6 +238,7 @@ std::ostream &RsFeedReaderMsg::print(std::ostream &out, uint16_t /*indent*/)
|
|||
uint32_t RsFeedReaderSerialiser::sizeMsg(RsFeedReaderMsg *item)
|
||||
{
|
||||
uint32_t s = 8; /* header */
|
||||
s += 2; /* version */
|
||||
s += GetTlvStringSize(item->msgId);
|
||||
s += GetTlvStringSize(item->feedId);
|
||||
s += GetTlvStringSize(item->title);
|
||||
|
@ -256,6 +270,7 @@ bool RsFeedReaderSerialiser::serialiseMsg(RsFeedReaderMsg *item, void *data, uin
|
|||
offset += 8;
|
||||
|
||||
/* add values */
|
||||
ok &= setRawUInt16(data, tlvsize, &offset, 0); /* version */
|
||||
ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_GENID, item->msgId);
|
||||
ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_VALUE, item->feedId);
|
||||
ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_NAME, item->title);
|
||||
|
@ -282,9 +297,8 @@ RsFeedReaderMsg *RsFeedReaderSerialiser::deserialiseMsg(void *data, uint32_t *pk
|
|||
|
||||
uint32_t offset = 0;
|
||||
|
||||
if ((RS_PKT_VERSION1 != getRsItemVersion(rstype)) ||
|
||||
(RS_PKT_CLASS_CONFIG != getRsItemClass(rstype)) ||
|
||||
(RS_PKT_TYPE_FEEDREADER_CONFIG != getRsItemType(rstype)) ||
|
||||
if ((RS_PKT_VERSION_SERVICE != getRsItemVersion(rstype)) ||
|
||||
(RS_SERVICE_TYPE_PLUGIN_FEEDREADER != getRsItemService(rstype)) ||
|
||||
(RS_PKT_SUBTYPE_FEEDREADER_MSG != getRsItemSubType(rstype)))
|
||||
{
|
||||
return NULL; /* wrong type */
|
||||
|
@ -306,6 +320,8 @@ RsFeedReaderMsg *RsFeedReaderSerialiser::deserialiseMsg(void *data, uint32_t *pk
|
|||
offset += 8;
|
||||
|
||||
/* get values */
|
||||
uint16_t version = 0;
|
||||
ok &= getRawUInt16(data, rssize, &offset, &version);
|
||||
ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_GENID, item->msgId);
|
||||
ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_VALUE, item->feedId);
|
||||
ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_NAME, item->title);
|
||||
|
@ -372,9 +388,8 @@ RsItem *RsFeedReaderSerialiser::deserialise(void *data, uint32_t *pktsize)
|
|||
/* get the type and size */
|
||||
uint32_t rstype = getRsItemId(data);
|
||||
|
||||
if ((RS_PKT_VERSION1 != getRsItemVersion(rstype)) ||
|
||||
(RS_PKT_CLASS_CONFIG != getRsItemClass(rstype)) ||
|
||||
(RS_PKT_TYPE_FEEDREADER_CONFIG != getRsItemType(rstype)))
|
||||
if ((RS_PKT_VERSION_SERVICE != getRsItemVersion(rstype)) ||
|
||||
(RS_SERVICE_TYPE_PLUGIN_FEEDREADER != getRsItemService(rstype)))
|
||||
{
|
||||
return NULL; /* wrong type */
|
||||
}
|
||||
|
|
|
@ -22,11 +22,14 @@
|
|||
#ifndef RS_FEEDREADER_ITEMS_H
|
||||
#define RS_FEEDREADER_ITEMS_H
|
||||
|
||||
#include "serialiser/rsserviceids.h"
|
||||
#include "serialiser/rsserial.h"
|
||||
#include "serialiser/rstlvtypes.h"
|
||||
|
||||
#include "p3FeedReader.h"
|
||||
|
||||
const uint32_t CONFIG_TYPE_FEEDREADER = 0x0001; // is this correct?
|
||||
|
||||
const uint8_t RS_PKT_SUBTYPE_FEEDREADER_FEED = 0x02;
|
||||
const uint8_t RS_PKT_SUBTYPE_FEEDREADER_MSG = 0x03;
|
||||
|
||||
|
@ -80,12 +83,15 @@ public:
|
|||
RsFeedReaderErrorState errorState;
|
||||
std::string errorString;
|
||||
|
||||
RsTlvStringSet xpathsToUse;
|
||||
RsTlvStringSet xpathsToRemove;
|
||||
|
||||
/* Not Serialised */
|
||||
bool preview;
|
||||
WorkState workstate;
|
||||
std::string content;
|
||||
|
||||
std::map<std::string, RsFeedReaderMsg*> mMsgs;
|
||||
std::map<std::string, RsFeedReaderMsg*> msgs;
|
||||
};
|
||||
|
||||
#define RS_FEEDMSG_FLAG_DELETED 1
|
||||
|
@ -114,7 +120,7 @@ public:
|
|||
class RsFeedReaderSerialiser: public RsSerialType
|
||||
{
|
||||
public:
|
||||
RsFeedReaderSerialiser() : RsSerialType(RS_PKT_VERSION1, RS_PKT_CLASS_CONFIG, RS_PKT_TYPE_FEEDREADER_CONFIG) {}
|
||||
RsFeedReaderSerialiser() : RsSerialType(RS_PKT_VERSION_SERVICE, RS_SERVICE_TYPE_PLUGIN_FEEDREADER) {}
|
||||
virtual ~RsFeedReaderSerialiser() {}
|
||||
|
||||
virtual uint32_t size(RsItem *item);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue