2012-07-31 21:47:53 -04:00
|
|
|
/****************************************************************
|
|
|
|
* RetroShare is distributed under the following license:
|
|
|
|
*
|
|
|
|
* Copyright (C) 2008 Robert Fernie
|
|
|
|
*
|
|
|
|
* 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 <QMimeData>
|
|
|
|
#include <QDateTime>
|
2012-11-25 16:50:45 -05:00
|
|
|
#include <QMenu>
|
2012-07-31 21:47:53 -04:00
|
|
|
|
|
|
|
#include "gui/gxs/GxsCommentTreeWidget.h"
|
2013-03-11 16:53:15 -04:00
|
|
|
#include "gui/gxs/GxsCreateCommentDialog.h"
|
2012-07-31 21:47:53 -04:00
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
2012-11-25 16:50:45 -05:00
|
|
|
#define PCITEM_COLUMN_COMMENT 0
|
|
|
|
#define PCITEM_COLUMN_AUTHOR 1
|
|
|
|
#define PCITEM_COLUMN_DATE 2
|
2012-07-31 21:47:53 -04:00
|
|
|
#define PCITEM_COLUMN_SERVSTRING 3
|
|
|
|
#define PCITEM_COLUMN_MSGID 4
|
|
|
|
#define PCITEM_COLUMN_PARENTID 5
|
|
|
|
|
|
|
|
#define GXSCOMMENTS_LOADTHREAD 1
|
|
|
|
|
|
|
|
|
2012-11-25 16:50:45 -05:00
|
|
|
/* Images for context menu icons */
|
|
|
|
#define IMAGE_MESSAGE ":/images/folder-draft.png"
|
2012-07-31 21:47:53 -04:00
|
|
|
|
|
|
|
GxsCommentTreeWidget::GxsCommentTreeWidget(QWidget *parent)
|
2013-03-11 16:53:15 -04:00
|
|
|
:QTreeWidget(parent), mRsTokenService(NULL), mCommentService(NULL), mTokenQueue(NULL)
|
2012-07-31 21:47:53 -04:00
|
|
|
{
|
2012-11-25 16:50:45 -05:00
|
|
|
// QTreeWidget* widget = this;
|
2012-07-31 21:47:53 -04:00
|
|
|
|
2012-11-30 13:54:25 -05:00
|
|
|
setContextMenuPolicy(Qt::CustomContextMenu);
|
2012-11-25 16:50:45 -05:00
|
|
|
// QFont font = QFont("ARIAL", 10);
|
|
|
|
// font.setBold(true);
|
|
|
|
|
|
|
|
// QString name("test");
|
|
|
|
// QTreeWidgetItem *item = new QTreeWidgetItem();
|
|
|
|
// item->setText(0, name);
|
|
|
|
// item->setFont(0, font);
|
|
|
|
// item->setSizeHint(0, QSize(18, 18));
|
|
|
|
// item->setForeground(0, QBrush(QColor(79, 79, 79)));
|
|
|
|
|
|
|
|
// addTopLevelItem(item);
|
|
|
|
// item->setExpanded(true);
|
|
|
|
|
2012-11-22 17:40:31 -05:00
|
|
|
return;
|
2012-07-31 21:47:53 -04:00
|
|
|
}
|
|
|
|
|
2012-11-25 16:50:45 -05:00
|
|
|
void GxsCommentTreeWidget::setCurrentMsgId(QTreeWidgetItem *current, QTreeWidgetItem *previous)
|
|
|
|
{
|
|
|
|
|
|
|
|
Q_UNUSED(previous);
|
|
|
|
|
|
|
|
if(current)
|
|
|
|
{
|
|
|
|
mCurrentMsgId = current->text(PCITEM_COLUMN_MSGID).toStdString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GxsCommentTreeWidget::customPopUpMenu(const QPoint& point)
|
|
|
|
{
|
|
|
|
QMenu contextMnu( this );
|
2012-11-30 13:54:25 -05:00
|
|
|
QAction* action = contextMnu.addAction(QIcon(IMAGE_MESSAGE), tr("Reply to Comment"), this, SLOT(replyToComment()));
|
|
|
|
action->setDisabled(mCurrentMsgId.empty());
|
|
|
|
action = contextMnu.addAction(QIcon(IMAGE_MESSAGE), tr("Submit Comment"), this, SLOT(makeComment()));
|
|
|
|
action->setDisabled(mThreadId.first.empty());
|
2012-11-25 16:50:45 -05:00
|
|
|
contextMnu.exec(QCursor::pos());
|
|
|
|
}
|
|
|
|
|
|
|
|
void GxsCommentTreeWidget::makeComment()
|
|
|
|
{
|
2013-03-11 16:53:15 -04:00
|
|
|
GxsCreateCommentDialog pcc(mTokenQueue, mCommentService, mThreadId, mThreadId.second, this);
|
2012-11-30 13:54:25 -05:00
|
|
|
pcc.exec();
|
|
|
|
}
|
2012-11-25 16:50:45 -05:00
|
|
|
|
2012-11-30 13:54:25 -05:00
|
|
|
void GxsCommentTreeWidget::replyToComment()
|
|
|
|
{
|
|
|
|
RsGxsGrpMsgIdPair msgId;
|
|
|
|
msgId.first = mThreadId.first;
|
|
|
|
msgId.second = mCurrentMsgId;
|
2013-03-11 16:53:15 -04:00
|
|
|
GxsCreateCommentDialog pcc(mTokenQueue, mCommentService, msgId, mThreadId.second, this);
|
2012-11-30 13:54:25 -05:00
|
|
|
pcc.exec();
|
2012-11-25 16:50:45 -05:00
|
|
|
}
|
|
|
|
|
2013-03-11 16:53:15 -04:00
|
|
|
void GxsCommentTreeWidget::setup(RsTokenService *token_service, RsGxsCommentService *comment_service)
|
2012-07-31 21:47:53 -04:00
|
|
|
{
|
2013-03-11 16:53:15 -04:00
|
|
|
mRsTokenService = token_service;
|
|
|
|
mCommentService = comment_service;
|
|
|
|
mTokenQueue = new TokenQueue(token_service, this);
|
2012-11-25 16:50:45 -05:00
|
|
|
connect(this, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(customPopUpMenu(QPoint)));
|
|
|
|
connect(this, SIGNAL(currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)), this, SLOT(setCurrentMsgId(QTreeWidgetItem*, QTreeWidgetItem*)));
|
2012-07-31 21:47:53 -04:00
|
|
|
|
2012-11-25 16:50:45 -05:00
|
|
|
return;
|
2012-07-31 21:47:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Load Comments */
|
2012-11-25 16:50:45 -05:00
|
|
|
void GxsCommentTreeWidget::requestComments(const RsGxsGrpMsgIdPair& threadId)
|
2012-07-31 21:47:53 -04:00
|
|
|
{
|
2012-11-25 16:50:45 -05:00
|
|
|
/* request comments */
|
2012-07-31 21:47:53 -04:00
|
|
|
|
2012-11-25 16:50:45 -05:00
|
|
|
mThreadId = threadId;
|
|
|
|
service_requestComments(threadId);
|
2012-07-31 21:47:53 -04:00
|
|
|
}
|
|
|
|
|
2012-11-25 16:50:45 -05:00
|
|
|
void GxsCommentTreeWidget::service_requestComments(const RsGxsGrpMsgIdPair& threadId)
|
2012-07-31 21:47:53 -04:00
|
|
|
{
|
2012-11-30 13:54:25 -05:00
|
|
|
/* request comments */
|
|
|
|
std::cerr << "GxsCommentTreeWidget::service_requestComments(" << threadId.second << ")";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
RsTokReqOptions opts;
|
|
|
|
opts.mReqType = GXS_REQUEST_TYPE_MSG_RELATED_DATA;
|
|
|
|
opts.mOptions = RS_TOKREQOPT_MSG_THREAD | RS_TOKREQOPT_MSG_LATEST;
|
|
|
|
|
|
|
|
std::vector<RsGxsGrpMsgIdPair> msgIds;
|
|
|
|
msgIds.push_back(threadId);
|
|
|
|
|
|
|
|
uint32_t token;
|
|
|
|
mTokenQueue->requestMsgRelatedInfo(token, RS_TOKREQ_ANSTYPE_DATA, opts, msgIds, GXSCOMMENTS_LOADTHREAD);
|
2012-07-31 21:47:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Generic Handling */
|
|
|
|
void GxsCommentTreeWidget::clearItems()
|
|
|
|
{
|
2012-11-30 13:54:25 -05:00
|
|
|
mPendingInsertMap.clear();
|
|
|
|
mLoadingMap.clear();
|
2012-07-31 21:47:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GxsCommentTreeWidget::completeItems()
|
|
|
|
{
|
2012-11-30 13:54:25 -05:00
|
|
|
/* handle pending items */
|
|
|
|
std::string parentId;
|
|
|
|
QTreeWidgetItem *parent = NULL;
|
|
|
|
QList<QTreeWidgetItem *> topLevelItems;
|
2012-07-31 21:47:53 -04:00
|
|
|
|
2012-11-30 13:54:25 -05:00
|
|
|
std::map<std::string, QTreeWidgetItem *>::iterator lit;
|
|
|
|
std::multimap<std::string, QTreeWidgetItem *>::iterator pit;
|
2012-07-31 21:47:53 -04:00
|
|
|
|
2012-11-30 13:54:25 -05:00
|
|
|
std::cerr << "GxsCommentTreeWidget::completeItems() " << mPendingInsertMap.size();
|
|
|
|
std::cerr << " PendingItems";
|
|
|
|
std::cerr << std::endl;
|
2012-07-31 21:47:53 -04:00
|
|
|
|
2012-11-30 13:54:25 -05:00
|
|
|
for(pit = mPendingInsertMap.begin(); pit != mPendingInsertMap.end(); pit++)
|
|
|
|
{
|
|
|
|
std::cerr << "GxsCommentTreeWidget::completeItems() item->parent: " << pit->first;
|
|
|
|
std::cerr << std::endl;
|
2012-07-31 21:47:53 -04:00
|
|
|
|
2012-11-30 13:54:25 -05:00
|
|
|
if (pit->first != parentId)
|
|
|
|
{
|
|
|
|
/* find parent */
|
|
|
|
parentId = pit->first;
|
|
|
|
lit = mLoadingMap.find(pit->first);
|
|
|
|
if (lit != mLoadingMap.end())
|
|
|
|
{
|
|
|
|
parent = lit->second;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
parent = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parent)
|
|
|
|
{
|
|
|
|
std::cerr << "GxsCommentTreeWidget::completeItems() Added to Parent";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
parent->addChild(pit->second);
|
|
|
|
}
|
|
|
|
else if (parentId == mThreadId.second)
|
|
|
|
{
|
|
|
|
std::cerr << "GxsCommentTreeWidget::completeItems() Added to topLevelItems";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
topLevelItems.append(pit->second);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
|
|
|
|
/* missing parent -> insert At Top Level */
|
|
|
|
QTreeWidgetItem *missingItem = service_createMissingItem(pit->first);
|
|
|
|
|
|
|
|
std::cerr << "GxsCommentTreeWidget::completeItems() Added MissingItem";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
parent = missingItem;
|
|
|
|
parent->addChild(pit->second);
|
|
|
|
topLevelItems.append(parent);
|
|
|
|
}
|
|
|
|
}
|
2012-07-31 21:47:53 -04:00
|
|
|
|
2012-11-30 13:54:25 -05:00
|
|
|
/* now push final tree into Tree */
|
|
|
|
clear();
|
|
|
|
insertTopLevelItems(0, topLevelItems);
|
2012-07-31 21:47:53 -04:00
|
|
|
|
2012-11-30 13:54:25 -05:00
|
|
|
/* cleanup temp stuff */
|
|
|
|
mLoadingMap.clear();
|
|
|
|
mPendingInsertMap.clear();
|
2012-07-31 21:47:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GxsCommentTreeWidget::addItem(std::string itemId, std::string parentId, QTreeWidgetItem *item)
|
|
|
|
{
|
|
|
|
std::cerr << "GxsCommentTreeWidget::addItem() Id: " << itemId;
|
|
|
|
std::cerr << " ParentId: " << parentId;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
/* store in map -> for children */
|
|
|
|
mLoadingMap[itemId] = item;
|
|
|
|
|
|
|
|
std::map<std::string, QTreeWidgetItem *>::iterator it;
|
|
|
|
it = mLoadingMap.find(parentId);
|
|
|
|
if (it != mLoadingMap.end())
|
|
|
|
{
|
|
|
|
std::cerr << "GxsCommentTreeWidget::addItem() Added to Parent";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
it->second->addChild(item);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::cerr << "GxsCommentTreeWidget::addItem() Added to Pending List";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
mPendingInsertMap.insert(std::make_pair(parentId, item));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GxsCommentTreeWidget::loadThread(const uint32_t &token)
|
|
|
|
{
|
|
|
|
clearItems();
|
|
|
|
|
|
|
|
service_loadThread(token);
|
|
|
|
|
|
|
|
completeItems();
|
|
|
|
}
|
|
|
|
|
2012-11-25 16:50:45 -05:00
|
|
|
void GxsCommentTreeWidget::acknowledgeComment(const uint32_t &token)
|
|
|
|
{
|
|
|
|
RsGxsGrpMsgIdPair msgId;
|
2013-03-11 16:53:15 -04:00
|
|
|
mCommentService->acknowledgeComment(token, msgId);
|
2012-11-25 16:50:45 -05:00
|
|
|
|
|
|
|
// simply reload data
|
|
|
|
service_requestComments(mThreadId);
|
|
|
|
}
|
|
|
|
|
2012-07-31 21:47:53 -04:00
|
|
|
|
|
|
|
void GxsCommentTreeWidget::service_loadThread(const uint32_t &token)
|
|
|
|
{
|
2012-11-25 16:50:45 -05:00
|
|
|
std::cerr << "GxsCommentTreeWidget::service_loadThread() ERROR must be overloaded!";
|
|
|
|
std::cerr << std::endl;
|
2012-07-31 21:47:53 -04:00
|
|
|
|
2013-03-11 16:53:15 -04:00
|
|
|
std::vector<RsGxsComment> comments;
|
|
|
|
mCommentService->getRelatedComments(token, comments);
|
2012-07-31 21:47:53 -04:00
|
|
|
|
2013-03-11 16:53:15 -04:00
|
|
|
std::vector<RsGxsComment>::iterator vit;
|
2012-11-25 16:50:45 -05:00
|
|
|
|
2013-03-11 16:53:15 -04:00
|
|
|
for(vit = comments.begin(); vit != comments.end(); vit++)
|
2012-11-25 16:50:45 -05:00
|
|
|
{
|
2013-03-11 16:53:15 -04:00
|
|
|
RsGxsComment &comment = *vit;
|
2012-11-25 16:50:45 -05:00
|
|
|
/* convert to a QTreeWidgetItem */
|
|
|
|
std::cerr << "GxsCommentTreeWidget::service_loadThread() Got Comment: " << comment.mMeta.mMsgId;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
QTreeWidgetItem *item = new QTreeWidgetItem();
|
|
|
|
QString text;
|
2012-07-31 21:47:53 -04:00
|
|
|
|
2012-11-25 16:50:45 -05:00
|
|
|
{
|
|
|
|
QDateTime qtime;
|
|
|
|
qtime.setTime_t(comment.mMeta.mPublishTs);
|
2012-07-31 21:47:53 -04:00
|
|
|
|
2012-11-25 16:50:45 -05:00
|
|
|
text = qtime.toString("yyyy-MM-dd hh:mm:ss");
|
|
|
|
item->setText(PCITEM_COLUMN_DATE, text);
|
|
|
|
}
|
2012-07-31 21:47:53 -04:00
|
|
|
|
2012-11-25 16:50:45 -05:00
|
|
|
text = QString::fromUtf8(comment.mComment.c_str());
|
|
|
|
item->setText(PCITEM_COLUMN_COMMENT, text);
|
2012-07-31 21:47:53 -04:00
|
|
|
|
2012-11-25 16:50:45 -05:00
|
|
|
text = QString::fromUtf8(comment.mMeta.mAuthorId.c_str());
|
|
|
|
if (text.isEmpty())
|
|
|
|
{
|
|
|
|
item->setText(PCITEM_COLUMN_AUTHOR, tr("Anonymous"));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
item->setText(PCITEM_COLUMN_AUTHOR, text);
|
|
|
|
}
|
2012-07-31 21:47:53 -04:00
|
|
|
|
2012-11-25 16:50:45 -05:00
|
|
|
text = QString::fromUtf8(comment.mMeta.mMsgId.c_str());
|
|
|
|
item->setText(PCITEM_COLUMN_MSGID, text);
|
|
|
|
|
|
|
|
text = QString::fromUtf8(comment.mMeta.mParentId.c_str());
|
|
|
|
item->setText(PCITEM_COLUMN_PARENTID, text);
|
|
|
|
|
2012-11-30 13:54:25 -05:00
|
|
|
text = QString::fromUtf8("0");
|
|
|
|
//text = QString::fromUtf8(comment.mMeta.mServiceString.c_str());
|
2012-11-25 16:50:45 -05:00
|
|
|
item->setText(PCITEM_COLUMN_SERVSTRING, text);
|
|
|
|
|
|
|
|
addItem(comment.mMeta.mMsgId, comment.mMeta.mParentId, item);
|
|
|
|
}
|
2012-07-31 21:47:53 -04:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-11-25 16:50:45 -05:00
|
|
|
QTreeWidgetItem *GxsCommentTreeWidget::service_createMissingItem(const RsGxsMessageId& parent)
|
2012-07-31 21:47:53 -04:00
|
|
|
{
|
|
|
|
std::cerr << "GxsCommentTreeWidget::service_createMissingItem()";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
QTreeWidgetItem *item = new QTreeWidgetItem();
|
|
|
|
QString text("Unknown");
|
|
|
|
|
|
|
|
item->setText(PCITEM_COLUMN_DATE, text);
|
|
|
|
|
|
|
|
item->setText(PCITEM_COLUMN_COMMENT, text);
|
|
|
|
|
|
|
|
item->setText(PCITEM_COLUMN_AUTHOR, text);
|
|
|
|
|
|
|
|
item->setText(PCITEM_COLUMN_MSGID, text);
|
|
|
|
|
|
|
|
item->setText(PCITEM_COLUMN_SERVSTRING, text);
|
|
|
|
|
2012-11-25 16:50:45 -05:00
|
|
|
text = QString::fromUtf8(parent.c_str());
|
2012-07-31 21:47:53 -04:00
|
|
|
item->setText(PCITEM_COLUMN_PARENTID, text);
|
|
|
|
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-11-25 16:50:45 -05:00
|
|
|
|
2012-10-21 15:45:35 -04:00
|
|
|
void GxsCommentTreeWidget::loadRequest(const TokenQueue *queue, const TokenRequest &req)
|
2012-07-31 21:47:53 -04:00
|
|
|
{
|
|
|
|
std::cerr << "GxsCommentTreeWidget::loadRequest() UserType: " << req.mUserType;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
if (queue != mTokenQueue)
|
|
|
|
{
|
|
|
|
std::cerr << "GxsCommentTreeWidget::loadRequest() Queue ERROR";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* now switch on req */
|
2012-11-25 16:50:45 -05:00
|
|
|
switch(req.mType)
|
2012-07-31 21:47:53 -04:00
|
|
|
{
|
|
|
|
|
2012-11-25 16:50:45 -05:00
|
|
|
case TOKENREQ_MSGINFO:
|
|
|
|
{
|
|
|
|
switch(req.mAnsType)
|
|
|
|
{
|
|
|
|
case RS_TOKREQ_ANSTYPE_ACK:
|
|
|
|
acknowledgeComment(req.mToken);
|
|
|
|
break;
|
|
|
|
case RS_TOKREQ_ANSTYPE_DATA:
|
|
|
|
loadThread(req.mToken);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
std::cerr << "GxsCommentTreeWidget::loadRequest() UNKNOWN UserType ";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
break;
|
2012-07-31 21:47:53 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|