FeedReeder:

- Fixed memory leak in xpath processing
- Stop long loops when closing the preview dialog or shutdown
- Added drag and drop to xpath lists in preview dialog
- Fixed save of xpaths lists

git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@6074 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
thunder2 2013-01-16 23:20:47 +00:00
parent 66c4a3d71b
commit 13f2863569
5 changed files with 254 additions and 149 deletions

View File

@ -191,7 +191,9 @@ PreviewFeedDialog::PreviewFeedDialog(RsFeedReader *feedReader, FeedReaderNotify
updateMsgCount(); updateMsgCount();
ui->xpathUseListWidget->installEventFilter(this); ui->xpathUseListWidget->installEventFilter(this);
ui->xpathUseListWidget->viewport()->installEventFilter(this);
ui->xpathRemoveListWidget->installEventFilter(this); ui->xpathRemoveListWidget->installEventFilter(this);
ui->xpathRemoveListWidget->viewport()->installEventFilter(this);
/* load settings */ /* load settings */
processSettings(true); processSettings(true);
@ -253,6 +255,9 @@ bool PreviewFeedDialog::eventFilter(QObject *obj, QEvent *event)
} }
} }
} }
if (event->type() == QEvent::Drop) {
processXPath();
}
/* pass the event on to the parent class */ /* pass the event on to the parent class */
return QDialog::eventFilter(obj, event); return QDialog::eventFilter(obj, event);
} }
@ -733,6 +738,9 @@ void PreviewFeedDialog::fillStructureTree()
void PreviewFeedDialog::getXPaths(std::list<std::string> &xpathsToUse, std::list<std::string> &xpathsToRemove) void PreviewFeedDialog::getXPaths(std::list<std::string> &xpathsToUse, std::list<std::string> &xpathsToRemove)
{ {
xpathsToUse.clear();
xpathsToRemove.clear();
int row; int row;
int rowCount = ui->xpathUseListWidget->count(); int rowCount = ui->xpathUseListWidget->count();
for (row = 0; row < rowCount; ++row) { for (row = 0; row < rowCount; ++row) {

View File

@ -166,6 +166,22 @@
</property> </property>
</widget> </widget>
</item> </item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::Fixed</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>10</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item> <item>
<widget class="QLabel" name="messageCountLabel"> <widget class="QLabel" name="messageCountLabel">
<property name="sizePolicy"> <property name="sizePolicy">
@ -179,6 +195,22 @@
</property> </property>
</widget> </widget>
</item> </item>
<item>
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::Fixed</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>10</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item> <item>
<widget class="QPushButton" name="nextPushButton"> <widget class="QPushButton" name="nextPushButton">
<property name="sizePolicy"> <property name="sizePolicy">
@ -282,7 +314,16 @@
<property name="spacing"> <property name="spacing">
<number>0</number> <number>0</number>
</property> </property>
<property name="margin"> <property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number> <number>0</number>
</property> </property>
<item> <item>
@ -369,7 +410,16 @@ border-image: url(:/images/closepressed.png)
<property name="spacing"> <property name="spacing">
<number>2</number> <number>2</number>
</property> </property>
<property name="margin"> <property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number> <number>0</number>
</property> </property>
<item> <item>
@ -410,7 +460,16 @@ border-image: url(:/images/closepressed.png)
<property name="spacing"> <property name="spacing">
<number>2</number> <number>2</number>
</property> </property>
<property name="margin"> <property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number> <number>0</number>
</property> </property>
<item> <item>
@ -425,6 +484,9 @@ border-image: url(:/images/closepressed.png)
<property name="contextMenuPolicy"> <property name="contextMenuPolicy">
<enum>Qt::CustomContextMenu</enum> <enum>Qt::CustomContextMenu</enum>
</property> </property>
<property name="dragDropMode">
<enum>QAbstractItemView::InternalMove</enum>
</property>
</widget> </widget>
</item> </item>
<item> <item>
@ -439,6 +501,9 @@ border-image: url(:/images/closepressed.png)
<property name="contextMenuPolicy"> <property name="contextMenuPolicy">
<enum>Qt::CustomContextMenu</enum> <enum>Qt::CustomContextMenu</enum>
</property> </property>
<property name="dragDropMode">
<enum>QAbstractItemView::InternalMove</enum>
</property>
</widget> </widget>
</item> </item>
</layout> </layout>

View File

@ -93,6 +93,10 @@ void p3FeedReaderThread::run()
if (isRunning()) { if (isRunning()) {
/* second, process the descriptions */ /* second, process the descriptions */
for (it = msgs.begin(); it != msgs.end(); ) { for (it = msgs.begin(); it != msgs.end(); ) {
if (!isRunning()) {
break;
}
RsFeedReaderMsg *mi = *it; RsFeedReaderMsg *mi = *it;
result = processMsg(feed, mi, errorString); result = processMsg(feed, mi, errorString);
if (result != RS_FEED_ERRORSTATE_OK) { if (result != RS_FEED_ERRORSTATE_OK) {
@ -116,11 +120,13 @@ void p3FeedReaderThread::run()
++it; ++it;
} }
} }
if (result == RS_FEED_ERRORSTATE_OK) { if (isRunning()) {
/* third, add messages */ if (result == RS_FEED_ERRORSTATE_OK) {
mFeedReader->onProcessSuccess_addMsgs(feed.feedId, msgs, false); /* third, add messages */
} else { mFeedReader->onProcessSuccess_addMsgs(feed.feedId, msgs, false);
mFeedReader->onProcessError(feed.feedId, result, errorString); } else {
mFeedReader->onProcessError(feed.feedId, result, errorString);
}
} }
} }
} else { } else {
@ -1057,198 +1063,203 @@ RsFeedReaderErrorState p3FeedReaderThread::processMsg(const RsFeedReaderFeed &fe
return result; return result;
} }
/* process description */ if (isRunning()) {
long todo; // encoding /* process description */
HTMLWrapper html; long todo; // encoding
if (html.readHTML(msg->description.c_str(), url.c_str())) { HTMLWrapper html;
xmlNodePtr root = html.getRootElement(); if (html.readHTML(msg->description.c_str(), url.c_str())) {
if (root) { xmlNodePtr root = html.getRootElement();
std::list<xmlNodePtr> nodesToDelete; if (root) {
std::list<xmlNodePtr> nodesToDelete;
/* process all children */ /* process all children */
std::list<xmlNodePtr> nodes; std::list<xmlNodePtr> nodes;
nodes.push_back(root); nodes.push_back(root);
while (!nodes.empty()) { while (!nodes.empty()) {
if (!isRunning()) { if (!isRunning()) {
break; break;
} }
xmlNodePtr node = nodes.front(); xmlNodePtr node = nodes.front();
nodes.pop_front(); nodes.pop_front();
switch (node->type) { switch (node->type) {
case XML_ELEMENT_NODE: case XML_ELEMENT_NODE:
if (xmlStrcasecmp(node->name, BAD_CAST"img") == 0) { if (xmlStrcasecmp(node->name, BAD_CAST"img") == 0) {
/* process images */ /* process images */
if ((feed.flag & RS_FEED_FLAG_EMBED_IMAGES) == 0) { if ((feed.flag & RS_FEED_FLAG_EMBED_IMAGES) == 0) {
/* remove image */ /* remove image */
xmlUnlinkNode(node);
nodesToDelete.push_back(node);
continue;
}
} else if (xmlStrcasecmp(node->name, BAD_CAST"script") == 0) {
/* remove script */
xmlUnlinkNode(node); xmlUnlinkNode(node);
nodesToDelete.push_back(node); nodesToDelete.push_back(node);
continue; continue;
} }
} else if (xmlStrcasecmp(node->name, BAD_CAST"script") == 0) {
/* remove script */
xmlUnlinkNode(node);
nodesToDelete.push_back(node);
continue;
}
xmlNodePtr child; xmlNodePtr child;
for (child = node->children; child; child = child->next) { for (child = node->children; child; child = child->next) {
nodes.push_back(child); nodes.push_back(child);
} }
break; break;
case XML_TEXT_NODE: case XML_TEXT_NODE:
{ {
/* check for only space */ /* check for only space */
std::string content; std::string content;
if (html.getContent(node, content, false)) { if (html.getContent(node, content, false)) {
std::string newContent = content; std::string newContent = content;
/* trim */ /* trim */
XMLWrapper::trimString(newContent); XMLWrapper::trimString(newContent);
if (newContent.empty()) { if (newContent.empty()) {
xmlUnlinkNode(node); xmlUnlinkNode(node);
nodesToDelete.push_back(node); nodesToDelete.push_back(node);
} else { } else {
if (content != newContent) { if (content != newContent) {
html.setContent(node, newContent.c_str()); html.setContent(node, newContent.c_str());
}
} }
} }
} }
} break;
break;
case XML_COMMENT_NODE: case XML_COMMENT_NODE:
// xmlUnlinkNode(node); // xmlUnlinkNode(node);
// nodesToDelete.push_back(node); // nodesToDelete.push_back(node);
break; break;
case XML_ATTRIBUTE_NODE: case XML_ATTRIBUTE_NODE:
case XML_CDATA_SECTION_NODE: case XML_CDATA_SECTION_NODE:
case XML_ENTITY_REF_NODE: case XML_ENTITY_REF_NODE:
case XML_ENTITY_NODE: case XML_ENTITY_NODE:
case XML_PI_NODE: case XML_PI_NODE:
case XML_DOCUMENT_NODE: case XML_DOCUMENT_NODE:
case XML_DOCUMENT_TYPE_NODE: case XML_DOCUMENT_TYPE_NODE:
case XML_DOCUMENT_FRAG_NODE: case XML_DOCUMENT_FRAG_NODE:
case XML_NOTATION_NODE: case XML_NOTATION_NODE:
case XML_HTML_DOCUMENT_NODE: case XML_HTML_DOCUMENT_NODE:
case XML_DTD_NODE: case XML_DTD_NODE:
case XML_ELEMENT_DECL: case XML_ELEMENT_DECL:
case XML_ATTRIBUTE_DECL: case XML_ATTRIBUTE_DECL:
case XML_ENTITY_DECL: case XML_ENTITY_DECL:
case XML_NAMESPACE_DECL: case XML_NAMESPACE_DECL:
case XML_XINCLUDE_START: case XML_XINCLUDE_START:
case XML_XINCLUDE_END: case XML_XINCLUDE_END:
#ifdef LIBXML_DOCB_ENABLED #ifdef LIBXML_DOCB_ENABLED
case XML_DOCB_DOCUMENT_NODE: case XML_DOCB_DOCUMENT_NODE:
#endif #endif
break; break;
}
} }
}
std::list<xmlNodePtr>::iterator nodeIt; std::list<xmlNodePtr>::iterator nodeIt;
for (nodeIt = nodesToDelete.begin(); nodeIt != nodesToDelete.end(); ++nodeIt) { for (nodeIt = nodesToDelete.begin(); nodeIt != nodesToDelete.end(); ++nodeIt) {
xmlFreeNode(*nodeIt); xmlFreeNode(*nodeIt);
} }
nodesToDelete.clear(); nodesToDelete.clear();
if (!feed.preview) { if (isRunning() && !feed.preview) {
result = processXPath(feed.xpathsToUse.ids, feed.xpathsToRemove.ids, html, errorString); result = processXPath(feed.xpathsToUse.ids, feed.xpathsToRemove.ids, html, errorString);
} }
if (result == RS_FEED_ERRORSTATE_OK) { if (isRunning() && result == RS_FEED_ERRORSTATE_OK) {
unsigned int xpathCount; unsigned int xpathCount;
unsigned int xpathIndex; unsigned int xpathIndex;
XPathWrapper *xpath = html.createXPath(); XPathWrapper *xpath = html.createXPath();
if (xpath) { if (xpath) {
/* process images */ /* process images */
if (xpath->compile("//img")) { if (xpath->compile("//img")) {
xpathCount = xpath->count(); xpathCount = xpath->count();
for (xpathIndex = 0; xpathIndex < xpathCount; ++xpathIndex) { for (xpathIndex = 0; xpathIndex < xpathCount; ++xpathIndex) {
xmlNodePtr node = xpath->node(xpathIndex); if (!isRunning()) {
break;
}
xmlNodePtr node = xpath->node(xpathIndex);
if (node->type == XML_ELEMENT_NODE) { if (node->type == XML_ELEMENT_NODE) {
bool removeImage = true; bool removeImage = true;
if (feed.flag & RS_FEED_FLAG_EMBED_IMAGES) { if (feed.flag & RS_FEED_FLAG_EMBED_IMAGES) {
/* embed image */ /* embed image */
std::string src = html.getAttr(node, "src"); std::string src = html.getAttr(node, "src");
if (!src.empty()) { if (!src.empty()) {
/* download image */ /* download image */
#ifdef FEEDREADER_DEBUG #ifdef FEEDREADER_DEBUG
std::cerr << "p3FeedReaderThread::processHTML - feed " << feed.feedId << " (" << feed.name << ") download image " << src << std::endl; std::cerr << "p3FeedReaderThread::processHTML - feed " << feed.feedId << " (" << feed.name << ") download image " << src << std::endl;
#endif #endif
std::vector<unsigned char> data; std::vector<unsigned char> data;
CURLWrapper CURL(proxy); CURLWrapper CURL(proxy);
CURLcode code = CURL.downloadBinary(calculateLink(url, src), data); CURLcode code = CURL.downloadBinary(calculateLink(url, src), data);
if (code == CURLE_OK && CURL.responseCode() == 200) { if (code == CURLE_OK && CURL.responseCode() == 200) {
std::string contentType = CURL.contentType(); std::string contentType = CURL.contentType();
if (isContentType(contentType, "image/")) { if (isContentType(contentType, "image/")) {
std::string base64; std::string base64;
if (toBase64(data, base64)) { if (toBase64(data, base64)) {
std::string imageBase64; std::string imageBase64;
rs_sprintf(imageBase64, "data:%s;base64,%s", contentType.c_str(), base64.c_str()); rs_sprintf(imageBase64, "data:%s;base64,%s", contentType.c_str(), base64.c_str());
if (html.setAttr(node, "src", imageBase64.c_str())) { if (html.setAttr(node, "src", imageBase64.c_str())) {
removeImage = false; removeImage = false;
}
} }
} }
} }
} }
} }
}
if (removeImage) { if (removeImage) {
/* remove image */ /* remove image */
xmlUnlinkNode(node); xmlUnlinkNode(node);
nodesToDelete.push_back(node); nodesToDelete.push_back(node);
continue; continue;
}
} }
} }
} else {
// unable to compile xpath expression
result = RS_FEED_ERRORSTATE_PROCESS_XPATH_INTERNAL_ERROR;
} }
delete(xpath);
xpath = NULL;
} else { } else {
// unable to compile xpath expression // unable to create xpath object
result = RS_FEED_ERRORSTATE_PROCESS_XPATH_INTERNAL_ERROR; result = RS_FEED_ERRORSTATE_PROCESS_XPATH_INTERNAL_ERROR;
std::cerr << "p3FeedReaderThread::process - feed " << feed.feedId << " (" << feed.name << "), unable to create xpath object" << std::endl;
} }
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) { for (nodeIt = nodesToDelete.begin(); nodeIt != nodesToDelete.end(); ++nodeIt) {
xmlFreeNode(*nodeIt); xmlFreeNode(*nodeIt);
} }
nodesToDelete.clear(); nodesToDelete.clear();
if (result == RS_FEED_ERRORSTATE_OK) { if (result == RS_FEED_ERRORSTATE_OK) {
if (isRunning()) { if (isRunning()) {
if (!html.saveHTML(msg->description)) { if (!html.saveHTML(msg->description)) {
#ifdef FEEDREADER_DEBUG #ifdef FEEDREADER_DEBUG
std::cerr << "p3FeedReaderThread::processHTML - feed " << feed.feedId << " (" << feed.name << ") cannot dump html" << std::endl; std::cerr << "p3FeedReaderThread::processHTML - feed " << feed.feedId << " (" << feed.name << ") cannot dump html" << std::endl;
#endif #endif
result = RS_FEED_ERRORSTATE_PROCESS_INTERNAL_ERROR; 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 { } else {
#ifdef FEEDREADER_DEBUG #ifdef FEEDREADER_DEBUG
std::cerr << "p3FeedReaderThread::processHTML - feed " << feed.feedId << " (" << feed.name << ") no root element" << std::endl; std::cerr << "p3FeedReaderThread::processHTML - feed " << feed.feedId << " (" << feed.name << ") cannot read html" << std::endl;
#endif #endif
result = RS_FEED_ERRORSTATE_PROCESS_HTML_ERROR; 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; return result;
@ -1352,6 +1363,8 @@ RsFeedReaderErrorState p3FeedReaderThread::processXPath(const std::list<std::str
nodesToDelete.clear(); nodesToDelete.clear();
} }
delete(xpath);
return result; return result;
} }

View File

@ -71,12 +71,25 @@ bool XPathWrapper::compile(const char *expression)
return true; return true;
} }
xmlXPathObjectType XPathWrapper::type()
{
if (mResult) {
return mResult->type;
}
return XPATH_UNDEFINED;
}
unsigned int XPathWrapper::count() unsigned int XPathWrapper::count()
{ {
if (!mResult) { if (!mResult) {
return 0; return 0;
} }
if (mResult->type != XPATH_NODESET) {
return 0;
}
if (xmlXPathNodeSetIsEmpty(mResult->nodesetval)) { if (xmlXPathNodeSetIsEmpty(mResult->nodesetval)) {
return 0; return 0;
} }
@ -90,6 +103,10 @@ xmlNodePtr XPathWrapper::node(unsigned int index)
return NULL; return NULL;
} }
if (mResult->type != XPATH_NODESET) {
return NULL;
}
if (xmlXPathNodeSetIsEmpty(mResult->nodesetval)) { if (xmlXPathNodeSetIsEmpty(mResult->nodesetval)) {
return NULL; return NULL;
} }

View File

@ -37,6 +37,8 @@ public:
bool compile(const char *expression); bool compile(const char *expression);
xmlXPathObjectType type();
unsigned int count(); unsigned int count();
xmlNodePtr node(unsigned int index); xmlNodePtr node(unsigned int index);
@ -48,4 +50,4 @@ protected:
xmlXPathObjectPtr mResult; xmlXPathObjectPtr mResult;
}; };
#endif #endif