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:
thunder2 2012-09-04 23:53:04 +00:00
parent 08904bf82f
commit c7ed9c6df7
23 changed files with 1595 additions and 430 deletions

View File

@ -65,6 +65,7 @@ const uint16_t RS_SERVICE_TYPE_STATUS = 0xf020;
const uint16_t RS_SERVICE_TYPE_PLUGIN_ARADO_ID = 0x0401; const uint16_t RS_SERVICE_TYPE_PLUGIN_ARADO_ID = 0x0401;
const uint16_t RS_SERVICE_TYPE_PLUGIN_QCHESS_ID = 0x0402; const uint16_t RS_SERVICE_TYPE_PLUGIN_QCHESS_ID = 0x0402;
const uint16_t RS_SERVICE_TYPE_PLUGIN_FEEDREADER = 0x0403;
/****************** BELOW ARE ONLY THEORETICAL (CAN BE CHANGED) *****/ /****************** BELOW ARE ONLY THEORETICAL (CAN BE CHANGED) *****/

View File

@ -14,7 +14,8 @@ SOURCES = FeedReaderPlugin.cpp \
gui/FeedReaderStringDefs.cpp \ gui/FeedReaderStringDefs.cpp \
util/CURLWrapper.cpp \ util/CURLWrapper.cpp \
util/XMLWrapper.cpp \ util/XMLWrapper.cpp \
util/HTMLWrapper.cpp util/HTMLWrapper.cpp \
util/XPathWrapper.cpp
HEADERS = FeedReaderPlugin.h \ HEADERS = FeedReaderPlugin.h \
interface/rsFeedReader.h \ interface/rsFeedReader.h \
@ -29,7 +30,8 @@ HEADERS = FeedReaderPlugin.h \
gui/FeedReaderStringDefs.h \ gui/FeedReaderStringDefs.h \
util/CURLWrapper.h \ util/CURLWrapper.h \
util/XMLWrapper.h \ util/XMLWrapper.h \
util/HTMLWrapper.h util/HTMLWrapper.h \
util/XPathWrapper.h
FORMS = gui/FeedReaderDialog.ui \ FORMS = gui/FeedReaderDialog.ui \
gui/AddFeedDialog.ui \ gui/AddFeedDialog.ui \

View File

@ -21,6 +21,7 @@
#pragma once #pragma once
#include <serialiser/rsserviceids.h>
#include <retroshare/rsplugin.h> #include <retroshare/rsplugin.h>
#include <retroshare-gui/mainpage.h> #include <retroshare-gui/mainpage.h>
#include "services/p3FeedReader.h" #include "services/p3FeedReader.h"
@ -33,7 +34,7 @@ class FeedReaderPlugin: public RsPlugin
public: public:
FeedReaderPlugin(); FeedReaderPlugin();
virtual uint16_t rs_service_id() const { return RS_PKT_TYPE_FEEDREADER_CONFIG; } virtual uint16_t rs_service_id() const { return RS_SERVICE_TYPE_PLUGIN_FEEDREADER; }
virtual RsPQIService *rs_pqi_service() const; virtual RsPQIService *rs_pqi_service() const;
virtual void stop(); virtual void stop();

View File

@ -27,6 +27,7 @@
#include "ui_AddFeedDialog.h" #include "ui_AddFeedDialog.h"
#include "PreviewFeedDialog.h" #include "PreviewFeedDialog.h"
#include "FeedReaderStringDefs.h" #include "FeedReaderStringDefs.h"
#include "gui/settings/rsharesettings.h"
#include "retroshare/rsforums.h" #include "retroshare/rsforums.h"
@ -36,7 +37,7 @@ bool sortForumInfo(const ForumInfo& info1, const ForumInfo& info2)
} }
AddFeedDialog::AddFeedDialog(RsFeedReader *feedReader, FeedReaderNotify *notify, QWidget *parent) AddFeedDialog::AddFeedDialog(RsFeedReader *feedReader, FeedReaderNotify *notify, QWidget *parent)
: QDialog(parent), mFeedReader(feedReader), mNotify(notify), ui(new Ui::AddFeedDialog) : QDialog(parent, Qt::Window), mFeedReader(feedReader), mNotify(notify), ui(new Ui::AddFeedDialog)
{ {
ui->setupUi(this); ui->setupUi(this);
@ -93,13 +94,37 @@ AddFeedDialog::AddFeedDialog(RsFeedReader *feedReader, FeedReaderNotify *notify,
validate(); validate();
ui->urlLineEdit->setFocus(); ui->urlLineEdit->setFocus();
/* load settings */
processSettings(true);
} }
AddFeedDialog::~AddFeedDialog() AddFeedDialog::~AddFeedDialog()
{ {
/* save settings */
processSettings(false);
delete ui; delete ui;
} }
void AddFeedDialog::processSettings(bool load)
{
Settings->beginGroup(QString("AddFeedDialog"));
if (load) {
// load settings
QByteArray geometry = Settings->value("Geometry").toByteArray();
if (!geometry.isEmpty()) {
restoreGeometry(geometry);
}
} else {
// save settings
Settings->setValue("Geometry", saveGeometry());
}
Settings->endGroup();
}
void AddFeedDialog::authenticationToggled() void AddFeedDialog::authenticationToggled()
{ {
bool checked = ui->useAuthenticationCheckBox->isChecked(); bool checked = ui->useAuthenticationCheckBox->isChecked();
@ -234,6 +259,9 @@ bool AddFeedDialog::fillFeed(const std::string &feedId)
ui->useStandardStorageTimeCheckBox->setChecked(feedInfo.flag.standardStorageTime); ui->useStandardStorageTimeCheckBox->setChecked(feedInfo.flag.standardStorageTime);
ui->storageTimeSpinBox->setValue(feedInfo.storageTime / (60 * 60 *24)); ui->storageTimeSpinBox->setValue(feedInfo.storageTime / (60 * 60 *24));
mXPathsToUse = feedInfo.xpathsToUse;
mXPathsToRemove = feedInfo.xpathsToRemove;
} }
return true; return true;
@ -274,6 +302,9 @@ void AddFeedDialog::getFeedInfo(FeedInfo &feedInfo)
feedInfo.flag.standardStorageTime = ui->useStandardStorageTimeCheckBox->isChecked(); feedInfo.flag.standardStorageTime = ui->useStandardStorageTimeCheckBox->isChecked();
feedInfo.storageTime = ui->storageTimeSpinBox->value() * 60 *60 * 24; feedInfo.storageTime = ui->storageTimeSpinBox->value() * 60 *60 * 24;
feedInfo.xpathsToUse = mXPathsToUse;
feedInfo.xpathsToRemove = mXPathsToRemove;
} }
void AddFeedDialog::createFeed() void AddFeedDialog::createFeed()
@ -310,5 +341,7 @@ void AddFeedDialog::preview()
getFeedInfo(feedInfo); getFeedInfo(feedInfo);
PreviewFeedDialog dialog(mFeedReader, mNotify, feedInfo, this); PreviewFeedDialog dialog(mFeedReader, mNotify, feedInfo, this);
dialog.exec(); if (dialog.exec() == QDialog::Accepted) {
dialog.getXPaths(mXPathsToUse, mXPathsToRemove);
}
} }

View File

@ -55,6 +55,7 @@ private slots:
void preview(); void preview();
private: private:
void processSettings(bool load);
void getFeedInfo(FeedInfo &feedInfo); void getFeedInfo(FeedInfo &feedInfo);
RsFeedReader *mFeedReader; RsFeedReader *mFeedReader;
@ -62,6 +63,9 @@ private:
std::string mFeedId; std::string mFeedId;
std::string mParentId; std::string mParentId;
std::list<std::string> mXPathsToUse;
std::list<std::string> mXPathsToRemove;
Ui::AddFeedDialog *ui; Ui::AddFeedDialog *ui;
}; };

View File

@ -76,55 +76,73 @@ QString FeedReaderStringDefs::workState(FeedInfo::WorkState state)
QString FeedReaderStringDefs::errorString(const FeedInfo &feedInfo) QString FeedReaderStringDefs::errorString(const FeedInfo &feedInfo)
{ {
QString errorState; return errorString(feedInfo.errorState, feedInfo.errorString);
switch (feedInfo.errorState) { }
QString FeedReaderStringDefs::errorString(RsFeedReaderErrorState errorState, const std::string &errorString)
{
QString errorText;
switch (errorState) {
case RS_FEED_ERRORSTATE_OK: case RS_FEED_ERRORSTATE_OK:
break; break;
/* download */ /* download */
case RS_FEED_ERRORSTATE_DOWNLOAD_INTERNAL_ERROR: case RS_FEED_ERRORSTATE_DOWNLOAD_INTERNAL_ERROR:
errorState = QApplication::translate("FeedReaderStringDefs", "Internal download error"); errorText = QApplication::translate("FeedReaderStringDefs", "Internal download error");
break; break;
case RS_FEED_ERRORSTATE_DOWNLOAD_ERROR: case RS_FEED_ERRORSTATE_DOWNLOAD_ERROR:
errorState = QApplication::translate("FeedReaderStringDefs", "Download error"); errorText = QApplication::translate("FeedReaderStringDefs", "Download error");
break; break;
case RS_FEED_ERRORSTATE_DOWNLOAD_UNKNOWN_CONTENT_TYPE: case RS_FEED_ERRORSTATE_DOWNLOAD_UNKNOWN_CONTENT_TYPE:
errorState = QApplication::translate("FeedReaderStringDefs", "Unknown content type"); errorText = QApplication::translate("FeedReaderStringDefs", "Unknown content type");
break; break;
case RS_FEED_ERRORSTATE_DOWNLOAD_NOT_FOUND: case RS_FEED_ERRORSTATE_DOWNLOAD_NOT_FOUND:
errorState = QApplication::translate("FeedReaderStringDefs", "Download not found"); errorText = QApplication::translate("FeedReaderStringDefs", "Download not found");
break; break;
case RS_FEED_ERRORSTATE_DOWNLOAD_UNKOWN_RESPONSE_CODE: case RS_FEED_ERRORSTATE_DOWNLOAD_UNKOWN_RESPONSE_CODE:
errorState = QApplication::translate("FeedReaderStringDefs", "Unknown response code"); errorText = QApplication::translate("FeedReaderStringDefs", "Unknown response code");
break; break;
/* process */ /* process */
case RS_FEED_ERRORSTATE_PROCESS_INTERNAL_ERROR: case RS_FEED_ERRORSTATE_PROCESS_INTERNAL_ERROR:
errorState = QApplication::translate("FeedReaderStringDefs", "Internal process error"); errorText = QApplication::translate("FeedReaderStringDefs", "Internal process error");
break; break;
case RS_FEED_ERRORSTATE_PROCESS_UNKNOWN_FORMAT: case RS_FEED_ERRORSTATE_PROCESS_UNKNOWN_FORMAT:
errorState = QApplication::translate("FeedReaderStringDefs", "Unknown XML format"); errorText = QApplication::translate("FeedReaderStringDefs", "Unknown XML format");
break; break;
case RS_FEED_ERRORSTATE_PROCESS_FORUM_CREATE: case RS_FEED_ERRORSTATE_PROCESS_FORUM_CREATE:
errorState = QApplication::translate("FeedReaderStringDefs", "Can't create forum"); errorText = QApplication::translate("FeedReaderStringDefs", "Can't create forum");
break; break;
case RS_FEED_ERRORSTATE_PROCESS_FORUM_NOT_FOUND: case RS_FEED_ERRORSTATE_PROCESS_FORUM_NOT_FOUND:
errorState = QApplication::translate("FeedReaderStringDefs", "Forum not found"); errorText = QApplication::translate("FeedReaderStringDefs", "Forum not found");
break; break;
case RS_FEED_ERRORSTATE_PROCESS_FORUM_NO_ADMIN: case RS_FEED_ERRORSTATE_PROCESS_FORUM_NO_ADMIN:
errorState = QApplication::translate("FeedReaderStringDefs", "You are not admin of the forum"); errorText = QApplication::translate("FeedReaderStringDefs", "You are not admin of the forum");
break; break;
case RS_FEED_ERRORSTATE_PROCESS_FORUM_NOT_ANONYMOUS: case RS_FEED_ERRORSTATE_PROCESS_FORUM_NOT_ANONYMOUS:
errorState = QApplication::translate("FeedReaderStringDefs", "The forum is no anonymous forum"); errorText = QApplication::translate("FeedReaderStringDefs", "The forum is no anonymous forum");
break;
case RS_FEED_ERRORSTATE_PROCESS_HTML_ERROR:
errorText = QApplication::translate("FeedReaderStringDefs", "Can't read html");
break;
case RS_FEED_ERRORSTATE_PROCESS_XPATH_INTERNAL_ERROR:
errorText = QApplication::translate("FeedReaderStringDefs", "Internal XPath error");
break;
case RS_FEED_ERRORSTATE_PROCESS_XPATH_WRONG_EXPRESSION:
errorText = QApplication::translate("FeedReaderStringDefs", "Wrong XPath expression");
break;
case RS_FEED_ERRORSTATE_PROCESS_XPATH_NO_RESULT:
errorText = QApplication::translate("FeedReaderStringDefs", "Empty XPath result");
break; break;
default: default:
errorState = QApplication::translate("FeedReaderStringDefs", "Unknown error"); errorText = QApplication::translate("FeedReaderStringDefs", "Unknown error");
} }
if (!feedInfo.errorString.empty()) { if (!errorString.empty()) {
errorState += QString(" (%1)").arg(QString::fromUtf8(feedInfo.errorString.c_str())); errorText += QString(" (%1)").arg(QString::fromUtf8(errorString.c_str()));
} }
return errorState; return errorText;
} }

View File

@ -34,6 +34,7 @@ public:
static bool showError(QWidget *parent, RsFeedAddResult result, const QString &title, const QString &text); static bool showError(QWidget *parent, RsFeedAddResult result, const QString &title, const QString &text);
static QString workState(FeedInfo::WorkState state); static QString workState(FeedInfo::WorkState state);
static QString errorString(const FeedInfo &feedInfo); static QString errorString(const FeedInfo &feedInfo);
static QString errorString(RsFeedReaderErrorState errorState, const std::string &errorString);
}; };
#endif #endif

View File

@ -19,13 +19,16 @@
* Boston, MA 02110-1301, USA. * Boston, MA 02110-1301, USA.
****************************************************************/ ****************************************************************/
#include <QPainter> //#include <QPainter>
#include <QMenu>
#include <QKeyEvent>
#include "PreviewFeedDialog.h" #include "PreviewFeedDialog.h"
#include "ui_PreviewFeedDialog.h" #include "ui_PreviewFeedDialog.h"
#include "FeedReaderNotify.h" #include "FeedReaderNotify.h"
#include "FeedReaderStringDefs.h" #include "FeedReaderStringDefs.h"
#include "util/HandleRichText.h" #include "util/HandleRichText.h"
#include "gui/settings/rsharesettings.h"
#include "interface/rsFeedReader.h" #include "interface/rsFeedReader.h"
#include "retroshare/rsiface.h" #include "retroshare/rsiface.h"
@ -138,33 +141,67 @@
//} //}
PreviewFeedDialog::PreviewFeedDialog(RsFeedReader *feedReader, FeedReaderNotify *notify, const FeedInfo &feedInfo, QWidget *parent) : PreviewFeedDialog::PreviewFeedDialog(RsFeedReader *feedReader, FeedReaderNotify *notify, const FeedInfo &feedInfo, QWidget *parent) :
QDialog(parent), mFeedReader(feedReader), mNotify(notify), ui(new Ui::PreviewFeedDialog) QDialog(parent, Qt::Window), mFeedReader(feedReader), mNotify(notify), ui(new Ui::PreviewFeedDialog)
{ {
ui->setupUi(this); ui->setupUi(this);
ui->feedNameLabel->clear(); ui->feedNameLabel->clear();
ui->feedInfoLabel->clear(); ui->useXPathCheckBox->setChecked(true);
/* connect signals */ /* connect signals */
connect(ui->previousPushButton, SIGNAL(clicked()), this, SLOT(previousMsg())); connect(ui->previousPushButton, SIGNAL(clicked()), this, SLOT(previousMsg()));
connect(ui->nextPushButton, SIGNAL(clicked()), this, SLOT(nextMsg())); connect(ui->nextPushButton, SIGNAL(clicked()), this, SLOT(nextMsg()));
connect(ui->documentButton, SIGNAL(toggled(bool)), this, SLOT(showDocumentFrame(bool))); connect(ui->closeStructureButton, SIGNAL(clicked()), this, SLOT(showStructureFrame()));
connect(ui->structureButton, SIGNAL(toggled(bool)), this, SLOT(showStructureFrame(bool)));
connect(ui->xpathPushButton, SIGNAL(toggled(bool)), this, SLOT(showXPathFrame(bool)));
connect(ui->useXPathCheckBox, SIGNAL(toggled(bool)), this, SLOT(fillStructureTree()));
connect(ui->xpathUseListWidget, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(xpathListCustomPopupMenu(QPoint)));
connect(ui->xpathRemoveListWidget, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(xpathListCustomPopupMenu(QPoint)));
connect(ui->xpathUseListWidget->itemDelegate(), SIGNAL(closeEditor(QWidget*,QAbstractItemDelegate::EndEditHint)), this, SLOT(xpathCloseEditor(QWidget*,QAbstractItemDelegate::EndEditHint)));
connect(ui->xpathRemoveListWidget->itemDelegate(), SIGNAL(closeEditor(QWidget*,QAbstractItemDelegate::EndEditHint)), this, SLOT(xpathCloseEditor(QWidget*,QAbstractItemDelegate::EndEditHint)));
connect(mNotify, SIGNAL(notifyFeedChanged(QString,int)), this, SLOT(feedChanged(QString,int))); connect(mNotify, SIGNAL(notifyFeedChanged(QString,int)), this, SLOT(feedChanged(QString,int)));
connect(mNotify, SIGNAL(notifyMsgChanged(QString,QString,int)), this, SLOT(msgChanged(QString,QString,int))); connect(mNotify, SIGNAL(notifyMsgChanged(QString,QString,int)), this, SLOT(msgChanged(QString,QString,int)));
// ui->documentTreeWidget->setItemDelegate(new PreviewItemDelegate(ui->documentTreeWidget)); // ui->documentTreeWidget->setItemDelegate(new PreviewItemDelegate(ui->documentTreeWidget));
showDocumentFrame(false); ui->structureFrame->hide();
if (!mFeedReader->addPreviewFeed(feedInfo, mFeedId)) { if (mFeedReader->addPreviewFeed(feedInfo, mFeedId)) {
setInfo(tr("Cannot create preview")); setFeedInfo("");
} else {
setFeedInfo(tr("Cannot create preview"));
}
setXPathInfo("");
showXPathFrame(true);
/* fill xpath expressions */
QListWidgetItem *item;
std::string xpath;
foreach(xpath, feedInfo.xpathsToUse){
item = new QListWidgetItem(QString::fromUtf8(xpath.c_str()));
item->setFlags(item->flags() | Qt::ItemIsEditable);
ui->xpathUseListWidget->addItem(item);
}
foreach(xpath, feedInfo.xpathsToRemove){
item = new QListWidgetItem(QString::fromUtf8(xpath.c_str()));
item->setFlags(item->flags() | Qt::ItemIsEditable);
ui->xpathRemoveListWidget->addItem(item);
} }
updateMsgCount(); updateMsgCount();
ui->xpathUseListWidget->installEventFilter(this);
ui->xpathRemoveListWidget->installEventFilter(this);
/* load settings */
processSettings(true);
} }
PreviewFeedDialog::~PreviewFeedDialog() PreviewFeedDialog::~PreviewFeedDialog()
{ {
/* save settings */
processSettings(false);
disconnect(mNotify); disconnect(mNotify);
disconnect(mNotify); disconnect(mNotify);
@ -175,6 +212,51 @@ PreviewFeedDialog::~PreviewFeedDialog()
delete ui; delete ui;
} }
void PreviewFeedDialog::processSettings(bool load)
{
Settings->beginGroup(QString("PreviewFeedDialog"));
if (load) {
// load settings
QByteArray geometry = Settings->value("Geometry").toByteArray();
if (!geometry.isEmpty()) {
restoreGeometry(geometry);
}
} else {
// save settings
Settings->setValue("Geometry", saveGeometry());
}
Settings->endGroup();
}
bool PreviewFeedDialog::eventFilter(QObject *obj, QEvent *event)
{
long todo_here;
if (event->type() == QEvent::KeyPress) {
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
if (keyEvent) {
if (keyEvent->key() == Qt::Key_Delete) {
/* Delete pressed */
if (obj == ui->xpathUseListWidget || obj == ui->xpathRemoveListWidget) {
QListWidget *listWidget = dynamic_cast<QListWidget*>(obj);
if (listWidget) {
QListWidgetItem *item = listWidget->currentItem();
if (item) {
delete(item);
processXPath();
}
return true; // eat event
}
}
}
}
}
/* pass the event on to the parent class */
return QDialog::eventFilter(obj, event);
}
void PreviewFeedDialog::feedChanged(const QString &feedId, int type) void PreviewFeedDialog::feedChanged(const QString &feedId, int type)
{ {
if (feedId.isEmpty()) { if (feedId.isEmpty()) {
@ -251,22 +333,139 @@ void PreviewFeedDialog::msgChanged(const QString &feedId, const QString &msgId,
updateMsgCount(); updateMsgCount();
} }
void PreviewFeedDialog::showDocumentFrame(bool show) void PreviewFeedDialog::showStructureFrame(bool show)
{ {
ui->documentFrame->setVisible(show); ui->structureButton->setChecked(show);
ui->documentButton->setChecked(show); ui->structureFrame->setVisible(show);
if (show) { if (show) {
ui->documentButton->setToolTip(tr("Hide tree")); fillStructureTree();
ui->documentButton->setIcon(QIcon(":images/hide_toolbox_frame.png"));
fillDocumentTree();
} else {
ui->documentButton->setToolTip(tr("Show tree"));
ui->documentButton->setIcon(QIcon(":images/show_toolbox_frame.png"));
} }
} }
void PreviewFeedDialog::showXPathFrame(bool show)
{
ui->xpathFrame->setVisible(show);
if (show) {
ui->xpathPushButton->setToolTip(tr("Hide XPath expressions"));
ui->xpathPushButton->setIcon(QIcon(":images/show_toolbox_frame.png"));
} else {
ui->xpathPushButton->setToolTip(tr("Show XPath expressions"));
ui->xpathPushButton->setIcon(QIcon(":images/hide_toolbox_frame.png"));
}
}
void PreviewFeedDialog::xpathListCustomPopupMenu(QPoint /*point*/)
{
QListWidgetItem *item = NULL;
if (sender() == ui->xpathUseListWidget) {
item = ui->xpathUseListWidget->currentItem();
} else if (sender() == ui->xpathRemoveListWidget) {
item = ui->xpathRemoveListWidget->currentItem();
} else {
return;
}
QMenu contextMnu(this);
QAction *action = contextMnu.addAction(QIcon(), tr("Add"), this, SLOT(addXPath()));
action->setData(QVariant::fromValue(sender()));
action = contextMnu.addAction(QIcon(), tr("Edit"), this, SLOT(editXPath()));
action->setData(QVariant::fromValue(sender()));
if (!item) {
action->setEnabled(false);
}
action = contextMnu.addAction(QIcon(), tr("Delete"), this, SLOT(removeXPath()));
action->setData(QVariant::fromValue(sender()));
if (!item) {
action->setEnabled(false);
}
contextMnu.exec(QCursor::pos());
}
void PreviewFeedDialog::xpathCloseEditor(QWidget */*editor*/, QAbstractItemDelegate::EndEditHint /*hint*/)
{
processXPath();
}
void PreviewFeedDialog::addXPath()
{
QAction *action = dynamic_cast<QAction*>(sender());
if (!action) {
return;
}
QObject *source = action->data().value<QObject*>();
QListWidget *listWidget;
if (source == ui->xpathUseListWidget) {
listWidget = ui->xpathUseListWidget;
} else if (source == ui->xpathRemoveListWidget) {
listWidget = ui->xpathRemoveListWidget;
} else {
return;
}
QListWidgetItem *item = new QListWidgetItem();
item->setFlags(item->flags() | Qt::ItemIsEditable);
listWidget->addItem(item);
listWidget->editItem(item);
}
void PreviewFeedDialog::editXPath()
{
QAction *action = dynamic_cast<QAction*>(sender());
if (!action) {
return;
}
QObject *source = action->data().value<QObject*>();
QListWidget *listWidget;
if (source == ui->xpathUseListWidget) {
listWidget = ui->xpathUseListWidget;
} else if (source == ui->xpathRemoveListWidget) {
listWidget = ui->xpathRemoveListWidget;
} else {
return;
}
listWidget->editItem(listWidget->currentItem());
}
void PreviewFeedDialog::removeXPath()
{
QAction *action = dynamic_cast<QAction*>(sender());
if (!action) {
return;
}
QObject *source = action->data().value<QObject*>();
QListWidget *listWidget;
if (source == ui->xpathUseListWidget) {
listWidget = ui->xpathUseListWidget;
} else if (source == ui->xpathRemoveListWidget) {
listWidget = ui->xpathRemoveListWidget;
} else {
return;
}
QListWidgetItem *item = listWidget->currentItem();
if (item) {
delete(item);
}
processXPath();
}
int PreviewFeedDialog::getMsgPos() int PreviewFeedDialog::getMsgPos()
{ {
int pos = -1; int pos = -1;
@ -281,14 +480,18 @@ int PreviewFeedDialog::getMsgPos()
return pos; return pos;
} }
void PreviewFeedDialog::setInfo(const QString &info) void PreviewFeedDialog::setFeedInfo(const QString &info)
{ {
ui->feedInfoLabel->setText(info); ui->feedInfoLabel->setText(info);
ui->infoLabel->setVisible(!info.isEmpty());
ui->feedInfoLabel->setVisible(!info.isEmpty()); ui->feedInfoLabel->setVisible(!info.isEmpty());
} }
void PreviewFeedDialog::setXPathInfo(const QString &info)
{
ui->xpathInfoLabel->setText(info);
ui->xpathInfoLabel->setVisible(!info.isEmpty());
}
void PreviewFeedDialog::fillFeedInfo(const FeedInfo &feedInfo) void PreviewFeedDialog::fillFeedInfo(const FeedInfo &feedInfo)
{ {
QString name = feedInfo.name.empty() ? tr("No name") : QString::fromUtf8(feedInfo.name.c_str()); QString name = feedInfo.name.empty() ? tr("No name") : QString::fromUtf8(feedInfo.name.c_str());
@ -299,7 +502,7 @@ void PreviewFeedDialog::fillFeedInfo(const FeedInfo &feedInfo)
} }
ui->feedNameLabel->setText(name); ui->feedNameLabel->setText(name);
setInfo(FeedReaderStringDefs::errorString(feedInfo)); setFeedInfo(FeedReaderStringDefs::errorString(feedInfo));
} }
void PreviewFeedDialog::previousMsg() void PreviewFeedDialog::previousMsg()
@ -344,24 +547,23 @@ void PreviewFeedDialog::updateMsg()
ui->msgTitle->clear(); ui->msgTitle->clear();
ui->msgText->clear(); ui->msgText->clear();
mDescription.clear(); mDescription.clear();
mDescriptionXPath.clear();
return; return;
} }
mDescription = msgInfo.description;
QString msgTxt = RsHtml().formatText(ui->msgText->document(), QString::fromUtf8(mDescription.c_str()), RSHTML_FORMATTEXT_EMBED_LINKS);
ui->msgText->setHtml(msgTxt);
ui->msgTitle->setText(QString::fromUtf8(msgInfo.title.c_str())); ui->msgTitle->setText(QString::fromUtf8(msgInfo.title.c_str()));
ui->documentTreeWidget->clear(); /* store description */
fillDocumentTree(); mDescription = msgInfo.description;
/* process xpath */
processXPath();
} }
static void examineChildElements(QTreeWidget *treeWidget, HTMLWrapper &html, xmlNodePtr node, QTreeWidgetItem *parentItem) static void buildNodeText(HTMLWrapper &html, xmlNodePtr node, QString &text)
{ {
QTreeWidgetItem *item = new QTreeWidgetItem; switch (node->type) {
QString text; case XML_ELEMENT_NODE:
if (node->type == XML_ELEMENT_NODE) {
text = QString("<%1 ").arg(QString::fromUtf8(html.nodeName(node).c_str())); text = QString("<%1 ").arg(QString::fromUtf8(html.nodeName(node).c_str()));
for (xmlAttrPtr attr = node->properties; attr; attr = attr->next) { for (xmlAttrPtr attr = node->properties; attr; attr = attr->next) {
@ -372,48 +574,148 @@ static void examineChildElements(QTreeWidget *treeWidget, HTMLWrapper &html, xml
text += QString("%1=\"%2\" ").arg(QString::fromUtf8(html.attrName(attr).c_str()), value); text += QString("%1=\"%2\" ").arg(QString::fromUtf8(html.attrName(attr).c_str()), value);
} }
text = text.trimmed() + ">"; text = text.trimmed() + ">";
} else {
std::string content; if (node->children && !node->children->next && node->children->type == XML_TEXT_NODE) {
if (html.getContent(node, content)) { /* only one text node as child */
text = QString::fromUtf8(content.c_str()); std::string content;
} else { if (html.getContent(node->children, content)) {
text = QApplication::translate("PreviewFeedDialog", "Error getting content"); text += QString::fromUtf8(content.c_str());
} else {
text += QApplication::translate("PreviewFeedDialog", "Error getting content");
}
text += QString("<%1>").arg(QString::fromUtf8(html.nodeName(node).c_str()));
xmlUnlinkNode(node->children);
xmlFreeNode(node->children);
} }
break;
case XML_TEXT_NODE:
case XML_COMMENT_NODE:
{
if (node->type == XML_COMMENT_NODE) {
text = "<!-- ";
}
std::string content;
if (html.getContent(node, content)) {
text += QString::fromUtf8(content.c_str());
} else {
text += QApplication::translate("PreviewFeedDialog", "Error getting content");
}
if (node->type == XML_COMMENT_NODE) {
text += " -->";
}
}
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;
}
}
static void examineChildElements(QTreeWidget *treeWidget, HTMLWrapper &html, QList<xmlNodePtr> &nodes, QTreeWidgetItem *parentItem)
{
int childIndex = 0;
int childCount;
QList<QPair<xmlNodePtr, QTreeWidgetItem*> > nodeItems;
foreach (xmlNodePtr node, nodes) {
QString text;
buildNodeText(html, node, text);
QList<QTreeWidgetItem*> itemsToDelete;
QTreeWidgetItem *item = NULL;
childCount = parentItem->childCount();
for (int index = childIndex; index < childCount; ++index) {
QTreeWidgetItem *childItem = parentItem->child(index);
if (childItem->text(0) == text) {
/* reuse item */
item = childItem;
break;
}
itemsToDelete.push_back(childItem);
}
if (item) {
/* delete old items */
foreach (QTreeWidgetItem *item, itemsToDelete) {
delete(item);
}
++childIndex;
} else {
item = new QTreeWidgetItem;
item->setText(0, text);
parentItem->insertChild(childIndex, item);
item->setExpanded(true);
++childIndex;
}
nodeItems.push_back(QPair<xmlNodePtr, QTreeWidgetItem*>(node, item));
}
/* delete not used items */
while (childIndex < parentItem->childCount()) {
delete(parentItem->child(childIndex));
}
QList<QPair<xmlNodePtr, QTreeWidgetItem*> >::iterator nodeItem;
for (nodeItem = nodeItems.begin(); nodeItem != nodeItems.end(); ++nodeItem) {
QList<xmlNodePtr> childNodes;
for (xmlNodePtr childNode = nodeItem->first->children; childNode; childNode = childNode->next) {
childNodes.push_back(childNode);
}
examineChildElements(treeWidget, html, childNodes, nodeItem->second);
} }
item->setText(0, text);
parentItem->addChild(item);
// QLabel *label = new QLabel(text); // QLabel *label = new QLabel(text);
// label->setTextFormat(Qt::PlainText); // label->setTextFormat(Qt::PlainText);
// label->setWordWrap(true); // label->setWordWrap(true);
// treeWidget->setItemWidget(item, 0, label); // treeWidget->setItemWidget(item, 0, label);
item->setExpanded(true);
for (xmlNodePtr child = node->children; child; child = child->next) {
examineChildElements(treeWidget, html, child, item);
}
} }
void PreviewFeedDialog::fillDocumentTree() void PreviewFeedDialog::fillStructureTree()
{ {
if (!ui->documentTreeWidget->isVisible()) { if (!ui->structureTreeWidget->isVisible()) {
return; return;
} }
if (ui->documentTreeWidget->topLevelItemCount() > 0) { // if (ui->structureTreeWidget->topLevelItemCount() > 0) {
// return;
// }
if (mDescriptionXPath.empty()) {
ui->structureTreeWidget->clear();
return; return;
} }
if (mDescription.empty()) { bool useXPath = ui->useXPathCheckBox->isChecked();
return;
}
HTMLWrapper html; HTMLWrapper html;
if (!html.readHTML(mDescription.c_str(), "")) { if (!html.readHTML(useXPath ? mDescriptionXPath.c_str() : mDescription.c_str(), "")) {
QTreeWidgetItem *item = new QTreeWidgetItem; QTreeWidgetItem *item = new QTreeWidgetItem;
item->setText(0, tr("Error parsing document")); item->setText(0, tr("Error parsing document"));
ui->documentTreeWidget->addTopLevelItem(item); ui->structureTreeWidget->addTopLevelItem(item);
return; return;
} }
@ -423,5 +725,42 @@ void PreviewFeedDialog::fillDocumentTree()
return; return;
} }
examineChildElements(ui->documentTreeWidget, html, root, ui->documentTreeWidget->invisibleRootItem()); QList<xmlNodePtr> nodes;
nodes.push_back(root);
examineChildElements(ui->structureTreeWidget, html, nodes, ui->structureTreeWidget->invisibleRootItem());
ui->structureTreeWidget->resizeColumnToContents(0);
}
void PreviewFeedDialog::getXPaths(std::list<std::string> &xpathsToUse, std::list<std::string> &xpathsToRemove)
{
int row;
int rowCount = ui->xpathUseListWidget->count();
for (row = 0; row < rowCount; ++row) {
xpathsToUse.push_back(ui->xpathUseListWidget->item(row)->text().toUtf8().constData());
}
rowCount = ui->xpathRemoveListWidget->count();
for (row = 0; row < rowCount; ++row) {
xpathsToRemove.push_back(ui->xpathRemoveListWidget->item(row)->text().toUtf8().constData());
}
}
void PreviewFeedDialog::processXPath()
{
std::list<std::string> xpathsToUse;
std::list<std::string> xpathsToRemove;
getXPaths(xpathsToUse, xpathsToRemove);
mDescriptionXPath = mDescription;
std::string errorString;
RsFeedReaderErrorState result = mFeedReader->processXPath(xpathsToUse, xpathsToRemove, mDescriptionXPath, errorString);
setXPathInfo(FeedReaderStringDefs::errorString(result, errorString));
/* fill message */
QString msgTxt = RsHtml().formatText(ui->msgText->document(), QString::fromUtf8(mDescriptionXPath.c_str()), RSHTML_FORMATTEXT_EMBED_LINKS);
ui->msgText->setHtml(msgTxt);
/* fill structure */
fillStructureTree();
} }

View File

@ -59,22 +59,36 @@ public:
PreviewFeedDialog(RsFeedReader *feedReader, FeedReaderNotify *notify, const FeedInfo &feedInfo, QWidget *parent = 0); PreviewFeedDialog(RsFeedReader *feedReader, FeedReaderNotify *notify, const FeedInfo &feedInfo, QWidget *parent = 0);
~PreviewFeedDialog(); ~PreviewFeedDialog();
void getXPaths(std::list<std::string> &xpathsToUse, std::list<std::string> &xpathsToRemove);
protected:
bool eventFilter(QObject *obj, QEvent *ev);
private slots: private slots:
void previousMsg(); void previousMsg();
void nextMsg(); void nextMsg();
void showDocumentFrame(bool show); void showStructureFrame(bool show = false);
void showXPathFrame(bool show);
void xpathListCustomPopupMenu(QPoint point);
void xpathCloseEditor(QWidget *editor, QAbstractItemDelegate::EndEditHint hint);
void addXPath();
void editXPath();
void removeXPath();
void fillStructureTree();
/* FeedReaderNotify */ /* FeedReaderNotify */
void feedChanged(const QString &feedId, int type); void feedChanged(const QString &feedId, int type);
void msgChanged(const QString &feedId, const QString &msgId, int type); void msgChanged(const QString &feedId, const QString &msgId, int type);
private: private:
void processSettings(bool load);
int getMsgPos(); int getMsgPos();
void setInfo(const QString &info); void setFeedInfo(const QString &info);
void setXPathInfo(const QString &info);
void fillFeedInfo(const FeedInfo &feedInfo); void fillFeedInfo(const FeedInfo &feedInfo);
void updateMsgCount(); void updateMsgCount();
void updateMsg(); void updateMsg();
void fillDocumentTree(); void processXPath();
RsFeedReader *mFeedReader; RsFeedReader *mFeedReader;
FeedReaderNotify *mNotify; FeedReaderNotify *mNotify;
@ -82,6 +96,7 @@ private:
std::string mMsgId; std::string mMsgId;
std::list<std::string> mMsgIds; std::list<std::string> mMsgIds;
std::string mDescription; std::string mDescription;
std::string mDescriptionXPath;
Ui::PreviewFeedDialog *ui; Ui::PreviewFeedDialog *ui;
}; };

View File

@ -6,8 +6,8 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>500</width> <width>800</width>
<height>350</height> <height>783</height>
</rect> </rect>
</property> </property>
<property name="windowTitle"> <property name="windowTitle">
@ -45,29 +45,103 @@
</property> </property>
</widget> </widget>
</item> </item>
<item row="1" column="0">
<widget class="QLabel" name="infoLabel">
<property name="text">
<string>Information:</string>
</property>
</widget>
</item>
<item row="1" column="1"> <item row="1" column="1">
<widget class="QLabel" name="feedInfoLabel"> <widget class="QLabel" name="feedInfoLabel">
<property name="palette">
<palette>
<active>
<colorrole role="WindowText">
<brush brushstyle="SolidPattern">
<color alpha="255">
<red>255</red>
<green>0</green>
<blue>0</blue>
</color>
</brush>
</colorrole>
</active>
<inactive>
<colorrole role="WindowText">
<brush brushstyle="SolidPattern">
<color alpha="255">
<red>255</red>
<green>0</green>
<blue>0</blue>
</color>
</brush>
</colorrole>
</inactive>
<disabled>
<colorrole role="WindowText">
<brush brushstyle="SolidPattern">
<color alpha="255">
<red>118</red>
<green>116</green>
<blue>108</blue>
</color>
</brush>
</colorrole>
</disabled>
</palette>
</property>
<property name="text"> <property name="text">
<string>Information</string> <string notr="true">Feed information</string>
</property> </property>
<property name="wordWrap"> <property name="wordWrap">
<bool>true</bool> <bool>true</bool>
</property> </property>
</widget> </widget>
</item> </item>
<item row="2" column="1">
<widget class="QLabel" name="xpathInfoLabel">
<property name="palette">
<palette>
<active>
<colorrole role="WindowText">
<brush brushstyle="SolidPattern">
<color alpha="255">
<red>255</red>
<green>0</green>
<blue>0</blue>
</color>
</brush>
</colorrole>
</active>
<inactive>
<colorrole role="WindowText">
<brush brushstyle="SolidPattern">
<color alpha="255">
<red>255</red>
<green>0</green>
<blue>0</blue>
</color>
</brush>
</colorrole>
</inactive>
<disabled>
<colorrole role="WindowText">
<brush brushstyle="SolidPattern">
<color alpha="255">
<red>118</red>
<green>116</green>
<blue>108</blue>
</color>
</brush>
</colorrole>
</disabled>
</palette>
</property>
<property name="text">
<string notr="true">XPath information</string>
</property>
</widget>
</item>
</layout> </layout>
</item> </item>
<item> <item>
<layout class="QHBoxLayout" name="navigateLayout"> <layout class="QHBoxLayout" name="buttonLayout">
<item> <item>
<spacer name="horizontalSpacer_1"> <spacer name="buttonSpacerLeft">
<property name="orientation"> <property name="orientation">
<enum>Qt::Horizontal</enum> <enum>Qt::Horizontal</enum>
</property> </property>
@ -119,7 +193,7 @@
</widget> </widget>
</item> </item>
<item> <item>
<spacer name="horizontalSpacer_2"> <spacer name="buttonSpacerRight">
<property name="orientation"> <property name="orientation">
<enum>Qt::Horizontal</enum> <enum>Qt::Horizontal</enum>
</property> </property>
@ -131,6 +205,16 @@
</property> </property>
</spacer> </spacer>
</item> </item>
<item>
<widget class="QPushButton" name="structureButton">
<property name="text">
<string>Structure</string>
</property>
<property name="checkable">
<bool>true</bool>
</property>
</widget>
</item>
</layout> </layout>
</item> </item>
<item> <item>
@ -173,92 +257,241 @@ background: white;}</string>
</layout> </layout>
</item> </item>
<item> <item>
<layout class="QHBoxLayout" name="msgLayout"> <widget class="QSplitter" name="messageSplitter">
<property name="spacing"> <property name="sizePolicy">
<number>0</number> <sizepolicy hsizetype="Preferred" vsizetype="MinimumExpanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property> </property>
<item> <property name="frameShape">
<layout class="QVBoxLayout" name="leftSidelLayout"> <enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="childrenCollapsible">
<bool>false</bool>
</property>
<widget class="QTextBrowser" name="msgText"/>
<widget class="QFrame" name="structureFrame">
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<property name="spacing">
<number>0</number>
</property>
<property name="margin">
<number>0</number>
</property>
<item> <item>
<widget class="QPushButton" name="documentButton"> <layout class="QHBoxLayout" name="structureLayout2">
<property name="minimumSize"> <property name="spacing">
<size> <number>0</number>
<width>14</width>
<height>31</height>
</size>
</property> </property>
<property name="maximumSize"> <item>
<size> <spacer name="structureSpacer">
<width>14</width> <property name="orientation">
<height>31</height> <enum>Qt::Horizontal</enum>
</size> </property>
</property> <property name="sizeHint" stdset="0">
<property name="text"> <size>
<string/> <width>40</width>
</property> <height>20</height>
<property name="iconSize"> </size>
<size> </property>
<width>16</width> </spacer>
<height>31</height> </item>
</size> <item>
</property> <widget class="QToolButton" name="closeStructureButton">
<property name="checkable"> <property name="maximumSize">
<bool>true</bool> <size>
</property> <width>16</width>
</widget> <height>16</height>
</size>
</property>
<property name="styleSheet">
<string notr="true">QToolButton
{
border-image: url(:/images/closenormal.png)
}
QToolButton:hover
{
border-image: url(:/images/closehover.png)
}
QToolButton:pressed {
border-image: url(:/images/closepressed.png)
}</string>
</property>
<property name="text">
<string>...</string>
</property>
<property name="autoRaise">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</item> </item>
<item> <item>
<spacer name="spacer"> <layout class="QHBoxLayout" name="structureLayout1">
<property name="orientation"> <item>
<enum>Qt::Vertical</enum> <widget class="QSplitter" name="structureSplitter">
</property> <property name="sizePolicy">
<property name="sizeHint" stdset="0"> <sizepolicy hsizetype="MinimumExpanding" vsizetype="Preferred">
<size> <horstretch>0</horstretch>
<width>12</width> <verstretch>0</verstretch>
<height>329</height> </sizepolicy>
</size> </property>
</property> <property name="frameShape">
</spacer> <enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="childrenCollapsible">
<bool>false</bool>
</property>
<widget class="QFrame" name="structureTreeFrame">
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<layout class="QVBoxLayout" name="verticalLayout_4">
<property name="spacing">
<number>2</number>
</property>
<property name="margin">
<number>0</number>
</property>
<item>
<widget class="QCheckBox" name="useXPathCheckBox">
<property name="text">
<string>Use XPath</string>
</property>
</widget>
</item>
<item>
<widget class="QTreeWidget" name="structureTreeWidget">
<property name="wordWrap">
<bool>true</bool>
</property>
<attribute name="headerVisible">
<bool>false</bool>
</attribute>
<attribute name="headerStretchLastSection">
<bool>false</bool>
</attribute>
<column>
<property name="text">
<string notr="true">1</string>
</property>
</column>
</widget>
</item>
</layout>
</widget>
<widget class="QFrame" name="xpathFrame">
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<layout class="QVBoxLayout" name="verticalLayout_3">
<property name="spacing">
<number>2</number>
</property>
<property name="margin">
<number>0</number>
</property>
<item>
<widget class="QLabel" name="xpathUseLabel">
<property name="text">
<string>XPath use</string>
</property>
</widget>
</item>
<item>
<widget class="QListWidget" name="xpathUseListWidget">
<property name="contextMenuPolicy">
<enum>Qt::CustomContextMenu</enum>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="xpathRemoveLabel">
<property name="text">
<string>XPath remove</string>
</property>
</widget>
</item>
<item>
<widget class="QListWidget" name="xpathRemoveListWidget">
<property name="contextMenuPolicy">
<enum>Qt::CustomContextMenu</enum>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
</item>
<item>
<layout class="QVBoxLayout" name="xpathLayout">
<item>
<widget class="QPushButton" name="xpathPushButton">
<property name="minimumSize">
<size>
<width>14</width>
<height>31</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>14</width>
<height>31</height>
</size>
</property>
<property name="checkable">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>13</width>
<height>13</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
</layout>
</item> </item>
</layout> </layout>
</item> </widget>
<item> </widget>
<widget class="QFrame" name="documentFrame">
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<property name="spacing">
<number>0</number>
</property>
<property name="margin">
<number>0</number>
</property>
<item>
<widget class="QTreeWidget" name="documentTreeWidget">
<property name="wordWrap">
<bool>true</bool>
</property>
<attribute name="headerVisible">
<bool>false</bool>
</attribute>
<column>
<property name="text">
<string notr="true">1</string>
</property>
</column>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QTextBrowser" name="msgText"/>
</item>
</layout>
</item> </item>
<item> <item>
<layout class="QVBoxLayout" name="previewLayout"> <layout class="QVBoxLayout" name="previewLayout">

View File

@ -27,22 +27,27 @@
#include <list> #include <list>
enum RsFeedReaderErrorState { enum RsFeedReaderErrorState {
RS_FEED_ERRORSTATE_OK = 0, RS_FEED_ERRORSTATE_OK = 0,
/* download */ /* download */
RS_FEED_ERRORSTATE_DOWNLOAD_INTERNAL_ERROR = 1, RS_FEED_ERRORSTATE_DOWNLOAD_INTERNAL_ERROR = 1,
RS_FEED_ERRORSTATE_DOWNLOAD_ERROR = 2, RS_FEED_ERRORSTATE_DOWNLOAD_ERROR = 2,
RS_FEED_ERRORSTATE_DOWNLOAD_UNKNOWN_CONTENT_TYPE = 3, RS_FEED_ERRORSTATE_DOWNLOAD_UNKNOWN_CONTENT_TYPE = 3,
RS_FEED_ERRORSTATE_DOWNLOAD_NOT_FOUND = 4, RS_FEED_ERRORSTATE_DOWNLOAD_NOT_FOUND = 4,
RS_FEED_ERRORSTATE_DOWNLOAD_UNKOWN_RESPONSE_CODE = 5, RS_FEED_ERRORSTATE_DOWNLOAD_UNKOWN_RESPONSE_CODE = 5,
/* process */ /* process */
RS_FEED_ERRORSTATE_PROCESS_INTERNAL_ERROR = 50, RS_FEED_ERRORSTATE_PROCESS_INTERNAL_ERROR = 50,
RS_FEED_ERRORSTATE_PROCESS_UNKNOWN_FORMAT = 51, RS_FEED_ERRORSTATE_PROCESS_UNKNOWN_FORMAT = 51,
RS_FEED_ERRORSTATE_PROCESS_FORUM_CREATE = 100, RS_FEED_ERRORSTATE_PROCESS_FORUM_CREATE = 100,
RS_FEED_ERRORSTATE_PROCESS_FORUM_NOT_FOUND = 101, RS_FEED_ERRORSTATE_PROCESS_FORUM_NOT_FOUND = 101,
RS_FEED_ERRORSTATE_PROCESS_FORUM_NO_ADMIN = 102, RS_FEED_ERRORSTATE_PROCESS_FORUM_NO_ADMIN = 102,
RS_FEED_ERRORSTATE_PROCESS_FORUM_NOT_ANONYMOUS = 103 RS_FEED_ERRORSTATE_PROCESS_FORUM_NOT_ANONYMOUS = 103,
RS_FEED_ERRORSTATE_PROCESS_HTML_ERROR = 150,
RS_FEED_ERRORSTATE_PROCESS_XPATH_INTERNAL_ERROR = 151,
RS_FEED_ERRORSTATE_PROCESS_XPATH_WRONG_EXPRESSION = 152,
RS_FEED_ERRORSTATE_PROCESS_XPATH_NO_RESULT = 153
}; };
@ -111,6 +116,9 @@ public:
RsFeedReaderErrorState errorState; RsFeedReaderErrorState errorState;
std::string errorString; std::string errorString;
std::list<std::string> xpathsToUse;
std::list<std::string> xpathsToRemove;
struct { struct {
bool folder : 1; bool folder : 1;
bool infoFromFeed : 1; bool infoFromFeed : 1;
@ -192,6 +200,8 @@ public:
virtual bool getFeedMsgIdList(const std::string &feedId, std::list<std::string> &msgIds) = 0; virtual bool getFeedMsgIdList(const std::string &feedId, std::list<std::string> &msgIds) = 0;
virtual bool processFeed(const std::string &feedId) = 0; virtual bool processFeed(const std::string &feedId) = 0;
virtual bool setMessageRead(const std::string &feedId, const std::string &msgId, bool read) = 0; virtual bool setMessageRead(const std::string &feedId, const std::string &msgId, bool read) = 0;
virtual RsFeedReaderErrorState processXPath(const std::list<std::string> &xpathsToUse, const std::list<std::string> &xpathsToRemove, std::string &description, std::string &errorString) = 0;
}; };
#endif #endif

View File

@ -37,7 +37,7 @@ RsFeedReader *rsFeedReader = NULL;
*********/ *********/
p3FeedReader::p3FeedReader(RsPluginHandler* pgHandler) 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") mFeedReaderMtx("p3FeedReader"), mDownloadMutex("p3FeedReaderDownload"), mProcessMutex("p3FeedReaderProcess"), mPreviewMutex("p3FeedReaderPreview")
{ {
mNextFeedId = 1; mNextFeedId = 1;
@ -88,6 +88,9 @@ static void feedToInfo(const RsFeedReaderFeed *feed, FeedInfo &info)
info.errorState = feed->errorState; info.errorState = feed->errorState;
info.errorString = feed->errorString; 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.folder = (feed->flag & RS_FEED_FLAG_FOLDER);
info.flag.infoFromFeed = (feed->flag & RS_FEED_FLAG_INFO_FROM_FEED); info.flag.infoFromFeed = (feed->flag & RS_FEED_FLAG_INFO_FROM_FEED);
info.flag.standardStorageTime = (feed->flag & RS_FEED_FLAG_STANDARD_STORAGE_TIME); 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->forumId = info.forumId;
} }
feed->xpathsToUse.ids = info.xpathsToUse;
feed->xpathsToRemove.ids = info.xpathsToRemove;
// feed->preview = info.flag.preview; // feed->preview = info.flag.preview;
uint32_t oldFlag = feed->flag; uint32_t oldFlag = feed->flag;
@ -512,11 +518,11 @@ void p3FeedReader::deleteAllMsgs_locked(RsFeedReaderFeed *fi)
} }
std::map<std::string, RsFeedReaderMsg*>::iterator msgIt; 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); delete(msgIt->second);
} }
fi->mMsgs.clear(); fi->msgs.clear();
} }
bool p3FeedReader::removeFeed(const std::string &feedId) 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; RsFeedReaderFeed *fi = feedIt->second;
std::map<std::string, RsFeedReaderMsg*>::iterator msgIt; std::map<std::string, RsFeedReaderMsg*>::iterator msgIt;
msgIt = fi->mMsgs.find(msgId); msgIt = fi->msgs.find(msgId);
if (msgIt == fi->mMsgs.end()) { if (msgIt == fi->msgs.end()) {
#ifdef FEEDREADER_DEBUG #ifdef FEEDREADER_DEBUG
std::cerr << "p3FeedReader::getMsgInfo - msg " << msgId << " not found" << std::endl; std::cerr << "p3FeedReader::getMsgInfo - msg " << msgId << " not found" << std::endl;
#endif #endif
@ -744,8 +750,8 @@ bool p3FeedReader::removeMsg(const std::string &feedId, const std::string &msgId
changed = !fi->preview; changed = !fi->preview;
std::map<std::string, RsFeedReaderMsg*>::iterator msgIt; std::map<std::string, RsFeedReaderMsg*>::iterator msgIt;
msgIt = fi->mMsgs.find(msgId); msgIt = fi->msgs.find(msgId);
if (msgIt == fi->mMsgs.end()) { if (msgIt == fi->msgs.end()) {
#ifdef FEEDREADER_DEBUG #ifdef FEEDREADER_DEBUG
std::cerr << "p3FeedReader::removeMsg - msg " << msgId << " not found" << std::endl; std::cerr << "p3FeedReader::removeMsg - msg " << msgId << " not found" << std::endl;
#endif #endif
@ -789,8 +795,8 @@ bool p3FeedReader::removeMsgs(const std::string &feedId, const std::list<std::st
std::list<std::string>::const_iterator idIt; std::list<std::string>::const_iterator idIt;
for (idIt = msgIds.begin(); idIt != msgIds.end(); ++idIt) { for (idIt = msgIds.begin(); idIt != msgIds.end(); ++idIt) {
std::map<std::string, RsFeedReaderMsg*>::iterator msgIt; std::map<std::string, RsFeedReaderMsg*>::iterator msgIt;
msgIt = fi->mMsgs.find(*idIt); msgIt = fi->msgs.find(*idIt);
if (msgIt == fi->mMsgs.end()) { if (msgIt == fi->msgs.end()) {
#ifdef FEEDREADER_DEBUG #ifdef FEEDREADER_DEBUG
std::cerr << "p3FeedReader::removeMsgs - msg " << *idIt << " not found" << std::endl; std::cerr << "p3FeedReader::removeMsgs - msg " << *idIt << " not found" << std::endl;
#endif #endif
@ -842,7 +848,7 @@ bool p3FeedReader::getMessageCount(const std::string &feedId, uint32_t *msgCount
RsFeedReaderFeed *fi = feedIt->second; RsFeedReaderFeed *fi = feedIt->second;
std::map<std::string, RsFeedReaderMsg*>::iterator msgIt; 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; RsFeedReaderMsg *mi = msgIt->second;
if (mi->flag & RS_FEEDMSG_FLAG_DELETED) { 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; RsFeedReaderFeed *fi = feedIt->second;
std::map<std::string, RsFeedReaderMsg*>::iterator msgIt; 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; RsFeedReaderMsg *mi = msgIt->second;
if (mi->flag & RS_FEEDMSG_FLAG_DELETED) { 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; RsFeedReaderFeed *fi = feedIt->second;
std::map<std::string, RsFeedReaderMsg*>::iterator msgIt; 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; RsFeedReaderMsg *mi = msgIt->second;
if (mi->flag & RS_FEEDMSG_FLAG_DELETED) { 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; RsFeedReaderFeed *fi = feedIt->second;
std::map<std::string, RsFeedReaderMsg*>::iterator msgIt; std::map<std::string, RsFeedReaderMsg*>::iterator msgIt;
msgIt = fi->mMsgs.find(msgId); msgIt = fi->msgs.find(msgId);
if (msgIt == fi->mMsgs.end()) { if (msgIt == fi->msgs.end()) {
#ifdef FEEDREADER_DEBUG #ifdef FEEDREADER_DEBUG
std::cerr << "p3FeedReader::setMessageRead - msg " << msgId << " not found" << std::endl; std::cerr << "p3FeedReader::setMessageRead - msg " << msgId << " not found" << std::endl;
#endif #endif
@ -1092,6 +1098,11 @@ bool p3FeedReader::setMessageRead(const std::string &feedId, const std::string &
return true; 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 **********************************/ /****************************** p3Service **********************************/
/***************************************************************************/ /***************************************************************************/
@ -1185,7 +1196,7 @@ void p3FeedReader::cleanFeeds()
uint32_t removedMsgs = 0; uint32_t removedMsgs = 0;
std::map<std::string, RsFeedReaderMsg*>::iterator msgIt; 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; RsFeedReaderMsg *mi = msgIt->second;
if (mi->flag & RS_FEEDMSG_FLAG_DELETED) { 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)); removedMsgIds.push_back(std::pair<std::string, std::string> (fi->feedId, mi->msgId));
delete(mi); delete(mi);
std::map<std::string, RsFeedReaderMsg*>::iterator deleteIt = msgIt++; std::map<std::string, RsFeedReaderMsg*>::iterator deleteIt = msgIt++;
fi->mMsgs.erase(deleteIt); fi->msgs.erase(deleteIt);
++removedMsgs; ++removedMsgs;
continue; continue;
} }
@ -1274,7 +1285,7 @@ bool p3FeedReader::saveList(bool &cleanup, std::list<RsItem *> & saveData)
saveData.push_back(fi); saveData.push_back(fi);
std::map<std::string, RsFeedReaderMsg*>::iterator it2; 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); saveData.push_back(it2->second);
} }
} }
@ -1376,7 +1387,7 @@ bool p3FeedReader::loadList(std::list<RsItem *>& load)
delete it1->second; delete it1->second;
continue; continue;
} }
it2->second->mMsgs[it1->first] = it1->second; it2->second->msgs[it1->first] = it1->second;
if (msgId + 1 > mNextMsgId) { if (msgId + 1 > mNextMsgId) {
mNextMsgId = msgId + 1; 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 *********/ RsStackMutex stack(mFeedReaderMtx); /******* LOCK STACK MUTEX *********/
@ -1516,28 +1527,7 @@ void p3FeedReader::onDownloadError(const std::string &feedId, p3FeedReaderThread
fi->lastUpdate = time(NULL); fi->lastUpdate = time(NULL);
fi->content.clear(); fi->content.clear();
switch (result) { fi->errorState = 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->errorString = errorString; fi->errorString = errorString;
#ifdef FEEDREADER_DEBUG #ifdef FEEDREADER_DEBUG
@ -1611,7 +1601,7 @@ bool p3FeedReader::getFeedToProcess(RsFeedReaderFeed &feed, const std::string &n
return true; 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 #ifdef FEEDREADER_DEBUG
std::cerr << "p3FeedReader::onProcessSuccess_filterMsg - feed " << feedId << " got " << msgs.size() << " messages" << std::endl; 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 #ifdef FEEDREADER_DEBUG
std::cerr << "p3FeedReader::onProcessSuccess_filterMsg - feed " << feedId << " not found" << std::endl; std::cerr << "p3FeedReader::onProcessSuccess_filterMsg - feed " << feedId << " not found" << std::endl;
#endif #endif
return false; return;
} }
RsFeedReaderFeed *fi = it->second; RsFeedReaderFeed *fi = it->second;
@ -1637,14 +1627,14 @@ bool p3FeedReader::onProcessSuccess_filterMsg(const std::string &feedId, std::li
RsFeedReaderMsg *miNew = *newMsgIt; RsFeedReaderMsg *miNew = *newMsgIt;
/* search for existing msg */ /* search for existing msg */
std::map<std::string, RsFeedReaderMsg*>::iterator msgIt; 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; RsFeedReaderMsg *mi = msgIt->second;
if (mi->title == miNew->title && mi->link == miNew->link && mi->author == miNew->author) { if (mi->title == miNew->title && mi->link == miNew->link && mi->author == miNew->author) {
/* msg exist */ /* msg exist */
break; break;
} }
} }
if (msgIt != fi->mMsgs.end()) { if (msgIt != fi->msgs.end()) {
/* msg exists */ /* msg exists */
delete(miNew); delete(miNew);
newMsgIt = msgs.erase(newMsgIt); newMsgIt = msgs.erase(newMsgIt);
@ -1660,11 +1650,9 @@ bool p3FeedReader::onProcessSuccess_filterMsg(const std::string &feedId, std::li
IndicateConfigChanged(); 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 #ifdef FEEDREADER_DEBUG
std::cerr << "p3FeedReader::onProcessSuccess_addMsgs - feed " << feedId << " got " << msgs.size() << " messages" << std::endl; 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; bool forum = (fi->flag & RS_FEED_FLAG_FORUM) && !fi->preview;
RsFeedReaderErrorState errorState = RS_FEED_ERRORSTATE_OK; RsFeedReaderErrorState errorState = RS_FEED_ERRORSTATE_OK;
if (result && forum && !msgs.empty()) { if (forum && !msgs.empty()) {
if (fi->forumId.empty()) { if (fi->forumId.empty()) {
/* create new forum */ /* create new forum */
std::wstring forumName; std::wstring forumName;
@ -1751,35 +1739,33 @@ void p3FeedReader::onProcessSuccess_addMsgs(const std::string &feedId, bool resu
uint32_t newMsgs = 0; uint32_t newMsgs = 0;
#endif #endif
if (result) { std::list<RsFeedReaderMsg*>::iterator newMsgIt;
std::list<RsFeedReaderMsg*>::iterator newMsgIt; for (newMsgIt = msgs.begin(); newMsgIt != msgs.end(); ) {
for (newMsgIt = msgs.begin(); newMsgIt != msgs.end(); ) { RsFeedReaderMsg *miNew = *newMsgIt;
RsFeedReaderMsg *miNew = *newMsgIt; /* add new msg */
/* add new msg */ if (fi->preview) {
if (fi->preview) { rs_sprintf(miNew->msgId, "preview%d", mNextPreviewMsgId--);
rs_sprintf(miNew->msgId, "preview%d", mNextPreviewMsgId--); } else {
} else { rs_sprintf(miNew->msgId, "%lu", mNextMsgId++);
rs_sprintf(miNew->msgId, "%lu", mNextMsgId++); }
} if (forum) {
if (forum) { miNew->flag = RS_FEEDMSG_FLAG_DELETED;
miNew->flag = RS_FEEDMSG_FLAG_DELETED; forumMsgs.push_back(*miNew);
forumMsgs.push_back(*miNew); // miNew->description.clear();
// miNew->description.clear(); } else {
} else { miNew->flag = RS_FEEDMSG_FLAG_NEW;
miNew->flag = RS_FEEDMSG_FLAG_NEW; addedMsgs.push_back(miNew->msgId);
addedMsgs.push_back(miNew->msgId); }
} fi->msgs[miNew->msgId] = miNew;
fi->mMsgs[miNew->msgId] = miNew; newMsgIt = msgs.erase(newMsgIt);
newMsgIt = msgs.erase(newMsgIt);
#ifdef FEEDREADER_DEBUG #ifdef FEEDREADER_DEBUG
++newMsgs; ++newMsgs;
#endif
}
#ifdef FEEDREADER_DEBUG
std::cerr << "p3FeedReader::onProcessSuccess_addMsgs - feed " << fi->feedId << " (" << fi->name << ") added " << newMsgs << "/" << msgs.size() << " messages" << std::endl;
#endif #endif
} }
#ifdef FEEDREADER_DEBUG
std::cerr << "p3FeedReader::onProcessSuccess_addMsgs - feed " << fi->feedId << " (" << fi->name << ") added " << newMsgs << "/" << msgs.size() << " messages" << std::endl;
#endif
} }
if (!single) { 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 *********/ RsStackMutex stack(mFeedReaderMtx); /******* LOCK STACK MUTEX *********/
@ -1853,23 +1839,7 @@ void p3FeedReader::onProcessError(const std::string &feedId, p3FeedReaderThread:
fi->lastUpdate = time(NULL); fi->lastUpdate = time(NULL);
fi->content.clear(); fi->content.clear();
long todo; // sort error codes fi->errorState = result;
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->errorString = errorString; fi->errorString = errorString;
#ifdef FEEDREADER_DEBUG #ifdef FEEDREADER_DEBUG

View File

@ -25,13 +25,10 @@
#include "retroshare/rsplugin.h" #include "retroshare/rsplugin.h"
#include "plugins/rspqiservice.h" #include "plugins/rspqiservice.h"
#include "interface/rsFeedReader.h" #include "interface/rsFeedReader.h"
#include "p3FeedReaderThread.h"
class RsFeedReaderFeed; class RsFeedReaderFeed;
class RsFeedReaderMsg;
//TODO: get new id's class p3FeedReaderThread;
const uint8_t RS_PKT_TYPE_FEEDREADER_CONFIG = 0xf0;
const uint32_t CONFIG_TYPE_FEEDREADER = 0x0001;
class p3FeedReader : public RsPQIService, public RsFeedReader class p3FeedReader : public RsPQIService, public RsFeedReader
{ {
@ -66,16 +63,18 @@ public:
virtual bool processFeed(const std::string &feedId); virtual bool processFeed(const std::string &feedId);
virtual bool setMessageRead(const std::string &feedId, const std::string &msgId, bool read); 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 ******************/ /****************** p3Service STUFF ******************/
virtual int tick(); virtual int tick();
/****************** internal STUFF *******************/ /****************** internal STUFF *******************/
bool getFeedToDownload(RsFeedReaderFeed &feed, const std::string &neededFeedId); bool getFeedToDownload(RsFeedReaderFeed &feed, const std::string &neededFeedId);
void onDownloadSuccess(const std::string &feedId, const std::string &content, std::string &icon); 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); void onDownloadError(const std::string &feedId, RsFeedReaderErrorState result, const std::string &errorString);
bool onProcessSuccess_filterMsg(const std::string &feedId, std::list<RsFeedReaderMsg*> &msgs); void 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 onProcessSuccess_addMsgs(const std::string &feedId, std::list<RsFeedReaderMsg*> &msgs, bool single);
void onProcessError(const std::string &feedId, p3FeedReaderThread::ProcessResult result, const std::string &errorString); void onProcessError(const std::string &feedId, RsFeedReaderErrorState result, const std::string &errorString);
bool getFeedToProcess(RsFeedReaderFeed &feed, const std::string &neededFeedId); bool getFeedToProcess(RsFeedReaderFeed &feed, const std::string &neededFeedId);

View File

@ -25,6 +25,7 @@
#include "util/CURLWrapper.h" #include "util/CURLWrapper.h"
#include "util/XMLWrapper.h" #include "util/XMLWrapper.h"
#include "util/HTMLWrapper.h" #include "util/HTMLWrapper.h"
#include "util/XPathWrapper.h"
#include <openssl/evp.h> #include <openssl/evp.h>
@ -67,8 +68,8 @@ void p3FeedReaderThread::run()
std::string icon; std::string icon;
std::string errorString; std::string errorString;
DownloadResult result = download(feed, content, icon, errorString); RsFeedReaderErrorState result = download(feed, content, icon, errorString);
if (result == DOWNLOAD_SUCCESS) { if (result == RS_FEED_ERRORSTATE_OK) {
mFeedReader->onDownloadSuccess(feed.feedId, content, icon); mFeedReader->onDownloadSuccess(feed.feedId, content, icon);
} else { } else {
mFeedReader->onDownloadError(feed.feedId, result, errorString); mFeedReader->onDownloadError(feed.feedId, result, errorString);
@ -84,37 +85,42 @@ void p3FeedReaderThread::run()
std::string errorString; std::string errorString;
std::list<RsFeedReaderMsg*>::iterator it; std::list<RsFeedReaderMsg*>::iterator it;
ProcessResult result = process(feed, msgs, errorString); RsFeedReaderErrorState result = process(feed, msgs, errorString);
if (result == PROCESS_SUCCESS) { if (result == RS_FEED_ERRORSTATE_OK) {
/* first, filter the messages */ /* first, filter the messages */
bool result = mFeedReader->onProcessSuccess_filterMsg(feed.feedId, msgs); mFeedReader->onProcessSuccess_filterMsg(feed.feedId, msgs);
if (isRunning()) { if (isRunning()) {
if (result) { /* second, process the descriptions */
/* second, process the descriptions */ for (it = msgs.begin(); it != msgs.end(); ) {
for (it = msgs.begin(); it != msgs.end(); ) { RsFeedReaderMsg *mi = *it;
RsFeedReaderMsg *mi = *it; result = processMsg(feed, mi, errorString);
processMsg(feed, mi); if (result != RS_FEED_ERRORSTATE_OK) {
break;
}
if (feed.preview) { if (feed.preview) {
/* add every message */ /* add every message */
it = msgs.erase(it); it = msgs.erase(it);
std::list<RsFeedReaderMsg*> msgSingle; std::list<RsFeedReaderMsg*> msgSingle;
msgSingle.push_back(mi); msgSingle.push_back(mi);
mFeedReader->onProcessSuccess_addMsgs(feed.feedId, result, msgSingle, true); mFeedReader->onProcessSuccess_addMsgs(feed.feedId, msgSingle, true);
/* delete not accepted message */ /* delete not accepted message */
std::list<RsFeedReaderMsg*>::iterator it1; std::list<RsFeedReaderMsg*>::iterator it1;
for (it1 = msgSingle.begin(); it1 != msgSingle.end(); ++it1) { for (it1 = msgSingle.begin(); it1 != msgSingle.end(); ++it1) {
delete (*it1); delete (*it1);
}
} else {
++it;
} }
} else {
++it;
} }
} }
/* third, add messages */ if (result == RS_FEED_ERRORSTATE_OK) {
mFeedReader->onProcessSuccess_addMsgs(feed.feedId, result, msgs, false); /* third, add messages */
mFeedReader->onProcessSuccess_addMsgs(feed.feedId, msgs, false);
} else {
mFeedReader->onProcessError(feed.feedId, result, errorString);
}
} }
} else { } else {
mFeedReader->onProcessError(feed.feedId, result, errorString); mFeedReader->onProcessError(feed.feedId, result, errorString);
@ -245,7 +251,7 @@ static bool getFavicon(CURLWrapper &CURL, const std::string &url, std::string &i
return result; 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 #ifdef FEEDREADER_DEBUG
std::cerr << "p3FeedReaderThread::download - feed " << feed.feedId << " (" << feed.name << ")" << std::endl; std::cerr << "p3FeedReaderThread::download - feed " << feed.feedId << " (" << feed.name << ")" << std::endl;
@ -254,7 +260,7 @@ p3FeedReaderThread::DownloadResult p3FeedReaderThread::download(const RsFeedRead
content.clear(); content.clear();
error.clear(); error.clear();
DownloadResult result; RsFeedReaderErrorState result;
std::string proxy = getProxyForFeed(feed); std::string proxy = getProxyForFeed(feed);
CURLWrapper CURL(proxy); CURLWrapper CURL(proxy);
@ -273,24 +279,24 @@ p3FeedReaderThread::DownloadResult p3FeedReaderThread::download(const RsFeedRead
isContentType(contentType, "application/xml") || isContentType(contentType, "application/xml") ||
isContentType(contentType, "application/xhtml+xml")) { isContentType(contentType, "application/xhtml+xml")) {
/* ok */ /* ok */
result = DOWNLOAD_SUCCESS; result = RS_FEED_ERRORSTATE_OK;
} else { } else {
result = DOWNLOAD_UNKNOWN_CONTENT_TYPE; result = RS_FEED_ERRORSTATE_DOWNLOAD_UNKNOWN_CONTENT_TYPE;
error = contentType; error = contentType;
} }
} }
break; break;
case 404: case 404:
result = DOWNLOAD_NOT_FOUND; result = RS_FEED_ERRORSTATE_DOWNLOAD_NOT_FOUND;
break; break;
default: default:
result = DOWNLOAD_UNKOWN_RESPONSE_CODE; result = RS_FEED_ERRORSTATE_DOWNLOAD_UNKOWN_RESPONSE_CODE;
rs_sprintf(error, "%ld", responseCode); rs_sprintf(error, "%ld", responseCode);
} }
getFavicon(CURL, feed.url, icon); getFavicon(CURL, feed.url, icon);
} else { } else {
result = DOWNLOAD_ERROR; result = RS_FEED_ERRORSTATE_DOWNLOAD_ERROR;
error = curl_easy_strerror(code); error = curl_easy_strerror(code);
} }
@ -326,7 +332,7 @@ static xmlNodePtr getNextItem(FeedFormat feedFormat, xmlNodePtr channel, xmlNode
item = item->next; item = item->next;
} }
for (; item; 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; break;
} }
} }
@ -789,29 +795,29 @@ static time_t parseISO8601Date(const std::string &pubDate)
return result; 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 #ifdef FEEDREADER_DEBUG
std::cerr << "p3FeedReaderThread::process - feed " << feed.feedId << " (" << feed.name << ")" << std::endl; std::cerr << "p3FeedReaderThread::process - feed " << feed.feedId << " (" << feed.name << ")" << std::endl;
#endif #endif
ProcessResult result = PROCESS_SUCCESS; RsFeedReaderErrorState result = RS_FEED_ERRORSTATE_OK;
XMLWrapper xml; XMLWrapper xml;
if (xml.readXML(feed.content.c_str())) { if (xml.readXML(feed.content.c_str())) {
xmlNodePtr root = xml.getRootElement(); xmlNodePtr root = xml.getRootElement();
if (root) { if (root) {
FeedFormat feedFormat; FeedFormat feedFormat;
if (xmlStrcasecmp(root->name, (const xmlChar*) "rss") == 0) { if (xmlStrEqual(root->name, BAD_CAST"rss")) {
feedFormat = FORMAT_RSS; feedFormat = FORMAT_RSS;
} else if (xmlStrcasecmp (root->name, (const xmlChar*) "rdf") != 0) { } else if (xmlStrEqual (root->name, BAD_CAST"rdf")) {
feedFormat = FORMAT_RDF; feedFormat = FORMAT_RDF;
} else { } else {
result = PROCESS_UNKNOWN_FORMAT; result = RS_FEED_ERRORSTATE_PROCESS_UNKNOWN_FORMAT;
error = "Only RSS or RDF supported"; error = "Only RSS or RDF supported";
} }
if (result == PROCESS_SUCCESS) { if (result == RS_FEED_ERRORSTATE_OK) {
xmlNodePtr channel = xml.findNode(root->children, "channel"); xmlNodePtr channel = xml.findNode(root->children, "channel");
if (channel) { if (channel) {
/* import header info */ /* import header info */
@ -900,16 +906,16 @@ p3FeedReaderThread::ProcessResult p3FeedReaderThread::process(const RsFeedReader
entries.push_back(item); entries.push_back(item);
} }
} else { } else {
result = PROCESS_UNKNOWN_FORMAT; result = RS_FEED_ERRORSTATE_PROCESS_UNKNOWN_FORMAT;
error = "Channel not found"; error = "Channel not found";
} }
} }
} else { } else {
result = PROCESS_UNKNOWN_FORMAT; result = RS_FEED_ERRORSTATE_PROCESS_UNKNOWN_FORMAT;
error = "Can't read document"; error = "Can't read document";
} }
} else { } else {
result = PROCESS_ERROR_INIT; result = RS_FEED_ERRORSTATE_PROCESS_INTERNAL_ERROR;
} }
#ifdef FEEDREADER_DEBUG #ifdef FEEDREADER_DEBUG
@ -936,12 +942,15 @@ std::string p3FeedReaderThread::getProxyForFeed(const RsFeedReaderFeed &feed)
return proxy; 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) { if (!msg) {
return false; return RS_FEED_ERRORSTATE_PROCESS_INTERNAL_ERROR;
} }
RsFeedReaderErrorState result = RS_FEED_ERRORSTATE_OK;
std::string proxy = getProxyForFeed(feed); std::string proxy = getProxyForFeed(feed);
std::string url; std::string url;
@ -953,14 +962,40 @@ bool p3FeedReaderThread::processMsg(const RsFeedReaderFeed &feed, RsFeedReaderMs
CURLWrapper CURL(proxy); CURLWrapper CURL(proxy);
CURLcode code = CURL.downloadText(msg->link, content); CURLcode code = CURL.downloadText(msg->link, content);
if (code == CURLE_OK && CURL.responseCode() == 200 && isContentType(CURL.contentType(), "text/html")) { if (code == CURLE_OK) {
/* ok */ long responseCode = CURL.responseCode();
msg->description = content;
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 { } else {
result = RS_FEED_ERRORSTATE_DOWNLOAD_ERROR;
errorString = curl_easy_strerror(code);
}
if (result != RS_FEED_ERRORSTATE_OK) {
#ifdef FEEDREADER_DEBUG #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 #endif
return false; return result;
} }
/* get effective url (moved location) */ /* 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) */ /* check if string contains xml chars (very simple test) */
if (msg->description.find('<') == std::string::npos) { if (msg->description.find('<') == std::string::npos) {
return true; return result;
} }
bool result = true;
/* process description */ /* process description */
long todo; // encoding long todo; // encoding
HTMLWrapper html; HTMLWrapper html;
if (html.readHTML(msg->description.c_str(), url.c_str())) { if (html.readHTML(msg->description.c_str(), url.c_str())) {
xmlNodePtr root = html.getRootElement(); xmlNodePtr root = html.getRootElement();
if (root) { if (root) {
/* process all children */ std::list<xmlNodePtr> nodesToDelete;
std::list<xmlNodePtr> parents;
parents.push_back(root);
while (!parents.empty()) { /* process all children */
std::list<xmlNodePtr> nodes;
nodes.push_back(root);
while (!nodes.empty()) {
if (!isRunning()) { if (!isRunning()) {
break; break;
} }
xmlNodePtr node = parents.front(); xmlNodePtr node = nodes.front();
parents.pop_front(); nodes.pop_front();
if (node->type == XML_ELEMENT_NODE) { switch (node->type) {
/* check for image */ case XML_ELEMENT_NODE:
if (xmlStrcasecmp(node->name, (const xmlChar*) "img") == 0) { if (xmlStrEqual(node->name, BAD_CAST"img")) {
bool removeImage = true; /* process images */
if (feed.flag & RS_FEED_FLAG_EMBED_IMAGES) { if ((feed.flag & RS_FEED_FLAG_EMBED_IMAGES) == 0) {
/* 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 */ /* remove image */
xmlUnlinkNode(node); xmlUnlinkNode(node);
xmlFreeNode(node); nodesToDelete.push_back(node);
continue; continue;
} }
} else if (xmlStrEqual(node->name, BAD_CAST"script")) {
/* 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) {
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()) { std::list<xmlNodePtr>::iterator nodeIt;
if (!html.saveHTML(msg->description)) { for (nodeIt = nodesToDelete.begin(); nodeIt != nodesToDelete.end(); ++nodeIt) {
#ifdef FEEDREADER_DEBUG xmlFreeNode(*nodeIt);
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;
} }
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; return result;

View File

@ -22,12 +22,16 @@
#ifndef P3_FEEDREADERTHREAD #ifndef P3_FEEDREADERTHREAD
#define P3_FEEDREADERTHREAD #define P3_FEEDREADERTHREAD
#include "interface/rsFeedReader.h"
#include "util/rsthreads.h" #include "util/rsthreads.h"
#include <list> #include <list>
class p3FeedReader; class p3FeedReader;
class RsFeedReaderFeed; class RsFeedReaderFeed;
class RsFeedReaderMsg; class RsFeedReaderMsg;
class HTMLWrapper;
class RsFeedReaderXPath;
class p3FeedReaderThread : public RsThread class p3FeedReaderThread : public RsThread
{ {
@ -37,20 +41,6 @@ public:
DOWNLOAD, DOWNLOAD,
PROCESS 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: public:
p3FeedReaderThread(p3FeedReader *feedReader, Type type, const std::string &feedId); p3FeedReaderThread(p3FeedReader *feedReader, Type type, const std::string &feedId);
@ -58,14 +48,17 @@ public:
std::string getFeedId() { return mFeedId; } 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: private:
virtual void run(); virtual void run();
DownloadResult download(const RsFeedReaderFeed &feed, std::string &content, std::string &icon, std::string &error); RsFeedReaderErrorState 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 process(const RsFeedReaderFeed &feed, std::list<RsFeedReaderMsg*> &entries, std::string &error);
std::string getProxyForFeed(const RsFeedReaderFeed &feed); 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; p3FeedReader *mFeedReader;
Type mType; Type mType;

View File

@ -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(); clear();
} }
@ -49,6 +51,8 @@ void RsFeedReaderFeed::clear()
icon.clear(); icon.clear();
errorState = RS_FEED_ERRORSTATE_OK; errorState = RS_FEED_ERRORSTATE_OK;
errorString.clear(); errorString.clear();
xpathsToUse.ids.clear();
xpathsToRemove.ids.clear();
preview = false; preview = false;
workstate = WAITING; workstate = WAITING;
@ -63,6 +67,7 @@ std::ostream &RsFeedReaderFeed::print(std::ostream &out, uint16_t /*indent*/)
uint32_t RsFeedReaderSerialiser::sizeFeed(RsFeedReaderFeed *item) uint32_t RsFeedReaderSerialiser::sizeFeed(RsFeedReaderFeed *item)
{ {
uint32_t s = 8; /* header */ uint32_t s = 8; /* header */
s += 2; /* version */
s += GetTlvStringSize(item->feedId); s += GetTlvStringSize(item->feedId);
s += GetTlvStringSize(item->parentId); s += GetTlvStringSize(item->parentId);
s += GetTlvStringSize(item->url); s += GetTlvStringSize(item->url);
@ -80,6 +85,8 @@ uint32_t RsFeedReaderSerialiser::sizeFeed(RsFeedReaderFeed *item)
s += GetTlvStringSize(item->forumId); s += GetTlvStringSize(item->forumId);
s += sizeof(uint32_t); /* errorstate */ s += sizeof(uint32_t); /* errorstate */
s += GetTlvStringSize(item->errorString); s += GetTlvStringSize(item->errorString);
s += item->xpathsToUse.TlvSize();
s += item->xpathsToRemove.TlvSize();
return s; return s;
} }
@ -103,6 +110,7 @@ bool RsFeedReaderSerialiser::serialiseFeed(RsFeedReaderFeed *item, void *data, u
offset += 8; offset += 8;
/* add values */ /* 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_GENID, item->feedId);
ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_VALUE, item->parentId); ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_VALUE, item->parentId);
ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_LINK, item->url); 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 &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_VALUE, item->forumId);
ok &= setRawUInt32(data, tlvsize, &offset, item->errorState); ok &= setRawUInt32(data, tlvsize, &offset, item->errorState);
ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_VALUE, item->errorString); 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) if (offset != tlvsize)
{ {
@ -138,9 +148,8 @@ RsFeedReaderFeed *RsFeedReaderSerialiser::deserialiseFeed(void *data, uint32_t *
uint32_t offset = 0; uint32_t offset = 0;
if ((RS_PKT_VERSION1 != getRsItemVersion(rstype)) || if ((RS_PKT_VERSION_SERVICE != getRsItemVersion(rstype)) ||
(RS_PKT_CLASS_CONFIG != getRsItemClass(rstype)) || (RS_SERVICE_TYPE_PLUGIN_FEEDREADER != getRsItemService(rstype)) ||
(RS_PKT_TYPE_FEEDREADER_CONFIG != getRsItemType(rstype)) ||
(RS_PKT_SUBTYPE_FEEDREADER_FEED != getRsItemSubType(rstype))) (RS_PKT_SUBTYPE_FEEDREADER_FEED != getRsItemSubType(rstype)))
{ {
return NULL; /* wrong type */ return NULL; /* wrong type */
@ -162,6 +171,8 @@ RsFeedReaderFeed *RsFeedReaderSerialiser::deserialiseFeed(void *data, uint32_t *
offset += 8; offset += 8;
/* get values */ /* 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_GENID, item->feedId);
ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_VALUE, item->parentId); ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_VALUE, item->parentId);
ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_LINK, item->url); 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); ok &= getRawUInt32(data, rssize, &offset, &errorState);
item->errorState = (RsFeedReaderErrorState) errorState; item->errorState = (RsFeedReaderErrorState) errorState;
ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_VALUE, item->errorString); 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) 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(); clear();
} }
@ -225,6 +238,7 @@ std::ostream &RsFeedReaderMsg::print(std::ostream &out, uint16_t /*indent*/)
uint32_t RsFeedReaderSerialiser::sizeMsg(RsFeedReaderMsg *item) uint32_t RsFeedReaderSerialiser::sizeMsg(RsFeedReaderMsg *item)
{ {
uint32_t s = 8; /* header */ uint32_t s = 8; /* header */
s += 2; /* version */
s += GetTlvStringSize(item->msgId); s += GetTlvStringSize(item->msgId);
s += GetTlvStringSize(item->feedId); s += GetTlvStringSize(item->feedId);
s += GetTlvStringSize(item->title); s += GetTlvStringSize(item->title);
@ -256,6 +270,7 @@ bool RsFeedReaderSerialiser::serialiseMsg(RsFeedReaderMsg *item, void *data, uin
offset += 8; offset += 8;
/* add values */ /* 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_GENID, item->msgId);
ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_VALUE, item->feedId); ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_VALUE, item->feedId);
ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_NAME, item->title); 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; uint32_t offset = 0;
if ((RS_PKT_VERSION1 != getRsItemVersion(rstype)) || if ((RS_PKT_VERSION_SERVICE != getRsItemVersion(rstype)) ||
(RS_PKT_CLASS_CONFIG != getRsItemClass(rstype)) || (RS_SERVICE_TYPE_PLUGIN_FEEDREADER != getRsItemService(rstype)) ||
(RS_PKT_TYPE_FEEDREADER_CONFIG != getRsItemType(rstype)) ||
(RS_PKT_SUBTYPE_FEEDREADER_MSG != getRsItemSubType(rstype))) (RS_PKT_SUBTYPE_FEEDREADER_MSG != getRsItemSubType(rstype)))
{ {
return NULL; /* wrong type */ return NULL; /* wrong type */
@ -306,6 +320,8 @@ RsFeedReaderMsg *RsFeedReaderSerialiser::deserialiseMsg(void *data, uint32_t *pk
offset += 8; offset += 8;
/* get values */ /* 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_GENID, item->msgId);
ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_VALUE, item->feedId); ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_VALUE, item->feedId);
ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_NAME, item->title); 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 */ /* get the type and size */
uint32_t rstype = getRsItemId(data); uint32_t rstype = getRsItemId(data);
if ((RS_PKT_VERSION1 != getRsItemVersion(rstype)) || if ((RS_PKT_VERSION_SERVICE != getRsItemVersion(rstype)) ||
(RS_PKT_CLASS_CONFIG != getRsItemClass(rstype)) || (RS_SERVICE_TYPE_PLUGIN_FEEDREADER != getRsItemService(rstype)))
(RS_PKT_TYPE_FEEDREADER_CONFIG != getRsItemType(rstype)))
{ {
return NULL; /* wrong type */ return NULL; /* wrong type */
} }

View File

@ -22,11 +22,14 @@
#ifndef RS_FEEDREADER_ITEMS_H #ifndef RS_FEEDREADER_ITEMS_H
#define RS_FEEDREADER_ITEMS_H #define RS_FEEDREADER_ITEMS_H
#include "serialiser/rsserviceids.h"
#include "serialiser/rsserial.h" #include "serialiser/rsserial.h"
#include "serialiser/rstlvtypes.h" #include "serialiser/rstlvtypes.h"
#include "p3FeedReader.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_FEED = 0x02;
const uint8_t RS_PKT_SUBTYPE_FEEDREADER_MSG = 0x03; const uint8_t RS_PKT_SUBTYPE_FEEDREADER_MSG = 0x03;
@ -80,12 +83,15 @@ public:
RsFeedReaderErrorState errorState; RsFeedReaderErrorState errorState;
std::string errorString; std::string errorString;
RsTlvStringSet xpathsToUse;
RsTlvStringSet xpathsToRemove;
/* Not Serialised */ /* Not Serialised */
bool preview; bool preview;
WorkState workstate; WorkState workstate;
std::string content; std::string content;
std::map<std::string, RsFeedReaderMsg*> mMsgs; std::map<std::string, RsFeedReaderMsg*> msgs;
}; };
#define RS_FEEDMSG_FLAG_DELETED 1 #define RS_FEEDMSG_FLAG_DELETED 1
@ -114,7 +120,7 @@ public:
class RsFeedReaderSerialiser: public RsSerialType class RsFeedReaderSerialiser: public RsSerialType
{ {
public: 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 ~RsFeedReaderSerialiser() {}
virtual uint32_t size(RsItem *item); virtual uint32_t size(RsItem *item);

View File

@ -32,7 +32,7 @@ bool HTMLWrapper::readHTML(const char *html, const char *url)
{ {
cleanup(); cleanup();
mDocument = htmlReadMemory(html, strlen(html), url, "", HTML_PARSE_NOERROR | HTML_PARSE_NOWARNING/* | HTML_PARSE_COMPACT*/); mDocument = htmlReadMemory(html, strlen(html), url, "", HTML_PARSE_NOERROR | HTML_PARSE_NOWARNING | HTML_PARSE_COMPACT | HTML_PARSE_NONET | HTML_PARSE_NOBLANKS);
if (mDocument) { if (mDocument) {
return true; return true;
} }
@ -58,3 +58,18 @@ bool HTMLWrapper::saveHTML(std::string &html)
return false; return false;
} }
bool HTMLWrapper::createHTML()
{
/* easy way */
return readHTML("<html><body></body></html>", "");
}
xmlNodePtr HTMLWrapper::getBody()
{
xmlNodePtr root = getRootElement();
if (!root) {
return NULL;
}
return findNode(root->children, "body", false);
}

View File

@ -31,6 +31,10 @@ public:
bool readHTML(const char *html, const char *url); bool readHTML(const char *html, const char *url);
bool saveHTML(std::string &html); bool saveHTML(std::string &html);
bool createHTML();
xmlNodePtr getBody();
}; };
#endif #endif

View File

@ -23,6 +23,7 @@
#include <string.h> #include <string.h>
#include "XMLWrapper.h" #include "XMLWrapper.h"
#include "XPathWrapper.h"
XMLWrapper::XMLWrapper() XMLWrapper::XMLWrapper()
{ {
@ -41,6 +42,18 @@ XMLWrapper::~XMLWrapper()
xmlCharEncCloseFunc(mCharEncodingHandler); xmlCharEncCloseFunc(mCharEncodingHandler);
} }
XMLWrapper &XMLWrapper::operator=(const XMLWrapper &xml)
{
cleanup();
const xmlDocPtr document = xml.getDocument();
if (document) {
mDocument = xmlCopyDoc(document, 1);
}
return *this;
}
void XMLWrapper::cleanup() void XMLWrapper::cleanup()
{ {
if (mDocument) { if (mDocument) {
@ -73,7 +86,7 @@ bool XMLWrapper::convertFromString(const char *text, xmlChar *&xmlText)
xmlBufferPtr in = xmlBufferCreateStatic((void*) text, strlen(text)); xmlBufferPtr in = xmlBufferCreateStatic((void*) text, strlen(text));
xmlBufferPtr out = xmlBufferCreate(); xmlBufferPtr out = xmlBufferCreate();
int ret = xmlCharEncOutFunc(mCharEncodingHandler, out, in); int ret = xmlCharEncInFunc(mCharEncodingHandler, out, in);
if (ret >= 0) { if (ret >= 0) {
result = true; result = true;
xmlText = xmlBufferDetach(out); xmlText = xmlBufferDetach(out);
@ -85,7 +98,12 @@ bool XMLWrapper::convertFromString(const char *text, xmlChar *&xmlText)
return result; return result;
} }
xmlNodePtr XMLWrapper::getRootElement() xmlDocPtr XMLWrapper::getDocument() const
{
return mDocument;
}
xmlNodePtr XMLWrapper::getRootElement() const
{ {
if (mDocument) { if (mDocument) {
return xmlDocGetRootElement(mDocument); return xmlDocGetRootElement(mDocument);
@ -98,7 +116,7 @@ bool XMLWrapper::readXML(const char *xml)
{ {
cleanup(); cleanup();
mDocument = xmlReadDoc((const xmlChar*) xml, "", NULL, XML_PARSE_NOERROR | XML_PARSE_NOWARNING/* | XML_PARSE_COMPACT | XML_PARSE_NOCDATA*/); mDocument = xmlReadDoc(BAD_CAST xml, "", NULL, XML_PARSE_NOERROR | XML_PARSE_NOWARNING | XML_PARSE_COMPACT | XML_PARSE_NOENT | XML_PARSE_NOCDATA);
if (mDocument) { if (mDocument) {
return true; return true;
} }
@ -114,7 +132,7 @@ bool XMLWrapper::getContent(xmlNodePtr node, std::string &content)
return false; return false;
} }
xmlChar *xmlContent = xmlNodeListGetString(mDocument, node, 1); xmlChar *xmlContent = xmlNodeGetContent(node);
if (!xmlContent) { if (!xmlContent) {
return true; return true;
} }
@ -125,6 +143,23 @@ bool XMLWrapper::getContent(xmlNodePtr node, std::string &content)
return result; return result;
} }
bool XMLWrapper::setContent(xmlNodePtr node, const char *content)
{
if (!node) {
return false;
}
xmlChar *xmlContent;
if (!convertFromString(content, xmlContent)) {
return false;
}
xmlNodeSetContent(node, xmlContent);
xmlFree(xmlContent);
return true;
}
std::string XMLWrapper::nodeName(xmlNodePtr node) std::string XMLWrapper::nodeName(xmlNodePtr node)
{ {
std::string name; std::string name;
@ -150,7 +185,7 @@ std::string XMLWrapper::attrName(xmlAttrPtr attr)
xmlNodePtr XMLWrapper::findNode(xmlNodePtr node, const char *name, bool children) xmlNodePtr XMLWrapper::findNode(xmlNodePtr node, const char *name, bool children)
{ {
if (node->name) { if (node->name) {
if (xmlStrcasecmp(node->name, (xmlChar*) name) == 0) { if (xmlStrEqual(node->name, BAD_CAST name)) {
return node; return node;
} }
} }
@ -218,7 +253,7 @@ std::string XMLWrapper::getAttr(xmlNodePtr node, const char *name)
std::string value; std::string value;
xmlChar *xmlValue = xmlGetProp(node, (const xmlChar*) name); xmlChar *xmlValue = xmlGetProp(node, BAD_CAST name);
if (xmlValue) { if (xmlValue) {
convertToString(xmlValue, value); convertToString(xmlValue, value);
xmlFree(xmlValue); xmlFree(xmlValue);
@ -238,8 +273,17 @@ bool XMLWrapper::setAttr(xmlNodePtr node, const char *name, const char *value)
return false; return false;
} }
xmlAttrPtr xmlAttr = xmlSetProp (node, (const xmlChar*) name, xmlValue); xmlAttrPtr xmlAttr = xmlSetProp (node, BAD_CAST name, xmlValue);
xmlFree(xmlValue); xmlFree(xmlValue);
return xmlAttr != NULL; return xmlAttr != NULL;
} }
XPathWrapper *XMLWrapper::createXPath()
{
if (mDocument) {
return new XPathWrapper(*this);
}
return NULL;
}

View File

@ -25,16 +25,23 @@
#include <string> #include <string>
#include <libxml/parser.h> #include <libxml/parser.h>
class XPathWrapper;
class XMLWrapper class XMLWrapper
{ {
public: public:
XMLWrapper(); XMLWrapper();
~XMLWrapper(); ~XMLWrapper();
XMLWrapper &operator=(const XMLWrapper &xml);
void cleanup(); void cleanup();
bool readXML(const char *xml); bool readXML(const char *xml);
xmlDocPtr getDocument() const;
xmlNodePtr getRootElement() const;
std::string nodeName(xmlNodePtr node); std::string nodeName(xmlNodePtr node);
std::string attrName(xmlAttrPtr attr); std::string attrName(xmlAttrPtr attr);
@ -42,19 +49,20 @@ public:
bool getChildText(xmlNodePtr node, const char *childName, std::string &text); bool getChildText(xmlNodePtr node, const char *childName, std::string &text);
bool getContent(xmlNodePtr node, std::string &content); bool getContent(xmlNodePtr node, std::string &content);
bool setContent(xmlNodePtr node, const char *content);
std::string getAttr(xmlNodePtr node, xmlAttrPtr attr); std::string getAttr(xmlNodePtr node, xmlAttrPtr attr);
std::string getAttr(xmlNodePtr node, const char *name); std::string getAttr(xmlNodePtr node, const char *name);
bool setAttr(xmlNodePtr node, const char *name, const char *value); bool setAttr(xmlNodePtr node, const char *name, const char *value);
xmlNodePtr getRootElement(); XPathWrapper *createXPath();
bool convertToString(const xmlChar *xmlText, std::string &text);
bool convertFromString(const char *text, xmlChar *&xmlText);
protected: protected:
xmlDocPtr mDocument; xmlDocPtr mDocument;
xmlCharEncodingHandlerPtr mCharEncodingHandler; xmlCharEncodingHandlerPtr mCharEncodingHandler;
bool convertToString(const xmlChar *xmlText, std::string &text);
bool convertFromString(const char *text, xmlChar *&xmlText);
}; };
#endif #endif

View File

@ -0,0 +1,102 @@
/****************************************************************
* RetroShare GUI is distributed under the following license:
*
* Copyright (C) 2012 by Thunder
*
* 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 2
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
****************************************************************/
#include "XPathWrapper.h"
#include "XMLWrapper.h"
XPathWrapper::XPathWrapper(XMLWrapper &xmlWrapper) : mXMLWrapper(xmlWrapper)
{
mContext = NULL;
mResult = NULL;
}
XPathWrapper::~XPathWrapper()
{
cleanup();
}
void XPathWrapper::cleanup()
{
if (mResult) {
xmlXPathFreeObject(mResult);
mResult = NULL;
}
if (mContext) {
xmlXPathFreeContext(mContext);
mContext = NULL;
}
}
bool XPathWrapper::compile(const char *expression)
{
cleanup();
xmlDocPtr document = mXMLWrapper.getDocument();
if (!document) {
return false;
}
mContext = xmlXPathNewContext(document);
if (!mContext) {
cleanup();
return false;
}
xmlChar *xmlExpression = NULL;
if (!mXMLWrapper.convertFromString(expression, xmlExpression)) {
cleanup();
return false;
}
mResult = xmlXPathEvalExpression(xmlExpression, mContext);
xmlFree(xmlExpression);
return true;
}
unsigned int XPathWrapper::count()
{
if (!mResult) {
return 0;
}
if (xmlXPathNodeSetIsEmpty(mResult->nodesetval)) {
return 0;
}
return mResult->nodesetval->nodeNr;
}
xmlNodePtr XPathWrapper::node(unsigned int index)
{
if (!mResult) {
return NULL;
}
if (xmlXPathNodeSetIsEmpty(mResult->nodesetval)) {
return NULL;
}
if (index >= (unsigned int) mResult->nodesetval->nodeNr) {
return NULL;
}
return mResult->nodesetval->nodeTab[index];
}

View File

@ -0,0 +1,51 @@
/****************************************************************
* RetroShare GUI is distributed under the following license:
*
* Copyright (C) 2012 by Thunder
*
* 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 2
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
****************************************************************/
#ifndef XPATHWRAPPER
#define XPATHWRAPPER
#include <libxml/xpath.h>
class XMLWrapper;
class XPathWrapper
{
friend class XMLWrapper;
public:
~XPathWrapper();
void cleanup();
bool compile(const char *expression);
unsigned int count();
xmlNodePtr node(unsigned int index);
protected:
XPathWrapper(XMLWrapper &xmlWrapper);
XMLWrapper &mXMLWrapper;
xmlXPathContextPtr mContext;
xmlXPathObjectPtr mResult;
};
#endif