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

@ -19,13 +19,16 @@
* Boston, MA 02110-1301, USA.
****************************************************************/
#include <QPainter>
//#include <QPainter>
#include <QMenu>
#include <QKeyEvent>
#include "PreviewFeedDialog.h"
#include "ui_PreviewFeedDialog.h"
#include "FeedReaderNotify.h"
#include "FeedReaderStringDefs.h"
#include "util/HandleRichText.h"
#include "gui/settings/rsharesettings.h"
#include "interface/rsFeedReader.h"
#include "retroshare/rsiface.h"
@ -138,33 +141,67 @@
//}
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->feedNameLabel->clear();
ui->feedInfoLabel->clear();
ui->useXPathCheckBox->setChecked(true);
/* connect signals */
connect(ui->previousPushButton, SIGNAL(clicked()), this, SLOT(previousMsg()));
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(notifyMsgChanged(QString,QString,int)), this, SLOT(msgChanged(QString,QString,int)));
// ui->documentTreeWidget->setItemDelegate(new PreviewItemDelegate(ui->documentTreeWidget));
showDocumentFrame(false);
ui->structureFrame->hide();
if (!mFeedReader->addPreviewFeed(feedInfo, mFeedId)) {
setInfo(tr("Cannot create preview"));
if (mFeedReader->addPreviewFeed(feedInfo, mFeedId)) {
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();
ui->xpathUseListWidget->installEventFilter(this);
ui->xpathRemoveListWidget->installEventFilter(this);
/* load settings */
processSettings(true);
}
PreviewFeedDialog::~PreviewFeedDialog()
{
/* save settings */
processSettings(false);
disconnect(mNotify);
disconnect(mNotify);
@ -175,6 +212,51 @@ PreviewFeedDialog::~PreviewFeedDialog()
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)
{
if (feedId.isEmpty()) {
@ -251,22 +333,139 @@ void PreviewFeedDialog::msgChanged(const QString &feedId, const QString &msgId,
updateMsgCount();
}
void PreviewFeedDialog::showDocumentFrame(bool show)
void PreviewFeedDialog::showStructureFrame(bool show)
{
ui->documentFrame->setVisible(show);
ui->documentButton->setChecked(show);
ui->structureButton->setChecked(show);
ui->structureFrame->setVisible(show);
if (show) {
ui->documentButton->setToolTip(tr("Hide tree"));
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"));
fillStructureTree();
}
}
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 pos = -1;
@ -281,14 +480,18 @@ int PreviewFeedDialog::getMsgPos()
return pos;
}
void PreviewFeedDialog::setInfo(const QString &info)
void PreviewFeedDialog::setFeedInfo(const QString &info)
{
ui->feedInfoLabel->setText(info);
ui->infoLabel->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)
{
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);
setInfo(FeedReaderStringDefs::errorString(feedInfo));
setFeedInfo(FeedReaderStringDefs::errorString(feedInfo));
}
void PreviewFeedDialog::previousMsg()
@ -344,24 +547,23 @@ void PreviewFeedDialog::updateMsg()
ui->msgTitle->clear();
ui->msgText->clear();
mDescription.clear();
mDescriptionXPath.clear();
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->documentTreeWidget->clear();
fillDocumentTree();
/* store description */
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;
QString text;
if (node->type == XML_ELEMENT_NODE) {
switch (node->type) {
case XML_ELEMENT_NODE:
text = QString("<%1 ").arg(QString::fromUtf8(html.nodeName(node).c_str()));
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 = text.trimmed() + ">";
} else {
std::string content;
if (html.getContent(node, content)) {
text = QString::fromUtf8(content.c_str());
} else {
text = QApplication::translate("PreviewFeedDialog", "Error getting content");
if (node->children && !node->children->next && node->children->type == XML_TEXT_NODE) {
/* only one text node as child */
std::string content;
if (html.getContent(node->children, 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);
// label->setTextFormat(Qt::PlainText);
// label->setWordWrap(true);
// 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;
}
if (ui->documentTreeWidget->topLevelItemCount() > 0) {
// if (ui->structureTreeWidget->topLevelItemCount() > 0) {
// return;
// }
if (mDescriptionXPath.empty()) {
ui->structureTreeWidget->clear();
return;
}
if (mDescription.empty()) {
return;
}
bool useXPath = ui->useXPathCheckBox->isChecked();
HTMLWrapper html;
if (!html.readHTML(mDescription.c_str(), "")) {
if (!html.readHTML(useXPath ? mDescriptionXPath.c_str() : mDescription.c_str(), "")) {
QTreeWidgetItem *item = new QTreeWidgetItem;
item->setText(0, tr("Error parsing document"));
ui->documentTreeWidget->addTopLevelItem(item);
ui->structureTreeWidget->addTopLevelItem(item);
return;
}
@ -423,5 +725,42 @@ void PreviewFeedDialog::fillDocumentTree()
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();
}