From b93130ac77415ae292a445e3e8f5b8f3c7980cad Mon Sep 17 00:00:00 2001 From: csoler Date: Thu, 9 Feb 2017 16:15:35 +0100 Subject: [PATCH] fixed a few bugs in post version collecting --- .../gui/gxsforums/GxsForumThreadWidget.cpp | 35 +++++++-- .../src/gui/gxsforums/GxsForumsFillThread.cpp | 72 +++++++++++-------- 2 files changed, 73 insertions(+), 34 deletions(-) diff --git a/retroshare-gui/src/gui/gxsforums/GxsForumThreadWidget.cpp b/retroshare-gui/src/gui/gxsforums/GxsForumThreadWidget.cpp index d143a70ba..9e731a0c7 100644 --- a/retroshare-gui/src/gui/gxsforums/GxsForumThreadWidget.cpp +++ b/retroshare-gui/src/gui/gxsforums/GxsForumThreadWidget.cpp @@ -200,7 +200,7 @@ GxsForumThreadWidget::GxsForumThreadWidget(const RsGxsGroupId &forumId, QWidget ui->threadTreeWidget->setItemDelegateForColumn(COLUMN_THREAD_DISTRIBUTION,new DistributionItemDelegate()) ; - connect(ui->versions_CB, SIGNAL(currentItemChanged(int)), this, SLOT(updateCurrentPostVersion(int))); + connect(ui->versions_CB, SIGNAL(currentIndexChanged(int)), this, SLOT(changedThread())); connect(ui->threadTreeWidget, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(threadListCustomPopupMenu(QPoint))); connect(ui->postText, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(contextMenuTextBrowser(QPoint))); @@ -698,7 +698,13 @@ void GxsForumThreadWidget::changedThread() if (!item || !item->isSelected()) { mThreadId.clear(); } else { - mThreadId = RsGxsMessageId(item->data(COLUMN_THREAD_MSGID, Qt::DisplayRole).toString().toStdString()); + mThreadId.clear(); + + if(ui->versions_CB->count() > 0) + mThreadId = RsGxsMessageId(ui->versions_CB->itemData(ui->versions_CB->currentIndex()).toString().toStdString()) ; + + if(mThreadId.isNull()) + mThreadId = RsGxsMessageId(item->data(COLUMN_THREAD_MSGID, Qt::DisplayRole).toString().toStdString()); } if (mFillThread) { @@ -1024,13 +1030,13 @@ void GxsForumThreadWidget::fillThreadFinished() mLastViewType = thread->mViewType; mLastForumID = groupId(); ui->threadTreeWidget->insertTopLevelItems(0, thread->mItems); + mPostVersions = thread->mPostVersions; // clear list thread->mItems.clear(); } else { - fillThreads(thread->mItems, thread->mExpandNewMessages, thread->mItemToExpand); - mPostVersions = thread->mPostVersions; + fillThreads(thread->mItems, thread->mExpandNewMessages, thread->mItemToExpand); // cleanup list cleanupItems(thread->mItems); @@ -1520,6 +1526,7 @@ void GxsForumThreadWidget::insertMessage() mStateHelper->clear(mTokenTypeMessageData); ui->versions_CB->hide(); + ui->time_label->show(); ui->postText->clear(); //ui->threadTitle->clear(); @@ -1532,6 +1539,7 @@ void GxsForumThreadWidget::insertMessage() mStateHelper->clear(mTokenTypeMessageData); ui->versions_CB->hide(); + ui->time_label->show(); //ui->threadTitle->setText(tr("Forum Description")); ui->postText->setText(mForumDescription); @@ -1552,6 +1560,7 @@ void GxsForumThreadWidget::insertMessage() mStateHelper->setWidgetEnabled(ui->previousButton, false); mStateHelper->setWidgetEnabled(ui->nextButton, false); ui->versions_CB->hide(); + ui->time_label->show(); return; } @@ -1568,19 +1577,33 @@ void GxsForumThreadWidget::insertMessage() // add/show combobox for versions, if applicable, and enable it. If no older versions of the post available, hide the combobox. + std::cerr << "Looking into existing versions for post " << mThreadId << ", thread history: " << mPostVersions.size() << std::endl; + QMap > >::const_iterator it = mPostVersions.find(mThreadId) ; ui->versions_CB->clear(); if(it != mPostVersions.end()) { + std::cerr << (*it).size() << " versions found " << std::endl; + ui->versions_CB->setVisible(true) ; + ui->time_label->hide(); + + ui->versions_CB->blockSignals(true) ; for(uint32_t i=0;i<(*it).size();++i) - ui->versions_CB->insertItem(i,QDateTime::fromTime_t( (*it)[i].first).toString()) ; + { + ui->versions_CB->insertItem(i,DateTime::formatLongDateTime( (*it)[i].first)); + ui->versions_CB->setItemData(i,QString::fromStdString((*it)[i].second.toStdString())); + + std::cerr << " added new post version " << (*it)[i].first << " " << (*it)[i].second << std::endl; + } + ui->versions_CB->blockSignals(false) ; } else { ui->versions_CB->hide(); + ui->time_label->show(); } /* request Post */ @@ -2137,7 +2160,7 @@ void GxsForumThreadWidget::editForumMessageData(const RsGxsForumMsg& msg) if (!msg.mMeta.mAuthorId.isNull()) { - CreateGxsForumMsg *cfm = new CreateGxsForumMsg(groupId(), mThreadId, msg.mMeta.mMsgId); + CreateGxsForumMsg *cfm = new CreateGxsForumMsg(groupId(), msg.mMeta.mParentId, msg.mMeta.mMsgId); cfm->insertPastedText(QString::fromUtf8(msg.mMsg.c_str())) ; cfm->show(); diff --git a/retroshare-gui/src/gui/gxsforums/GxsForumsFillThread.cpp b/retroshare-gui/src/gui/gxsforums/GxsForumsFillThread.cpp index 9b8e0d6b0..fee9ff5f5 100644 --- a/retroshare-gui/src/gui/gxsforums/GxsForumsFillThread.cpp +++ b/retroshare-gui/src/gui/gxsforums/GxsForumsFillThread.cpp @@ -89,6 +89,8 @@ void GxsForumsFillThread::calculateExpand(const RsGxsForumMsg &msg, QTreeWidgetI } } +static bool decreasing_time_comp(const QPair& e1,const QPair& e2) { return e2.first < e1.first ; } + void GxsForumsFillThread::run() { RsTokenService *service = rsGxsForums->getTokenService(); @@ -200,61 +202,75 @@ void GxsForumsFillThread::run() #ifdef DEBUG_FORUMS std::cerr << " Post " << msgIt->second.mMeta.mMsgId << " is a new version of " << msgIt->second.mMeta.mOrigMsgId << std::endl; #endif - mPostVersions[msgIt->second.mMeta.mOrigMsgId].push_back(QPair(msgIt->second.mMeta.mPublishTs,msgIt->second.mMeta.mMsgId)) ; + std::map::iterator msgIt2 = msgs.find(msgIt->second.mMeta.mOrigMsgId); + + // always add the post a a self version + + if(msgIt2 != msgs.end()) + { + // Ensuring that the post exists allows to only collect the existing data. + + mPostVersions[msgIt->second.mMeta.mOrigMsgId].push_back(QPair(msgIt2->second.mMeta.mPublishTs,msgIt2->second.mMeta.mMsgId)) ; + mPostVersions[msgIt->second.mMeta.mOrigMsgId].push_back(QPair(msgIt->second.mMeta.mPublishTs,msgIt->second.mMeta.mMsgId)) ; + } } + // The following code assembles all new versions of a given post into the same array, indexed by the oldest version of the post. + for(QMap > >::iterator it(mPostVersions.begin());it!=mPostVersions.end();++it) { QVector >& v(*it) ; for(int32_t i=0;i > >::iterator it2 = mPostVersions.find(sub_msg_id); - if(v[0].first < v[i].first) // works if i==0 - { - QPair tmp(v[0]) ; - v[0] = v[i] ; - v[i] = tmp ; - } + if(it2 != mPostVersions.end()) + { + for(uint32_t j=0;j<(*it2).size();++j) + if((*it2)[j].second != sub_msg_id) // dont copy it, since it is already present at slot i + v.append((*it2)[j]) ; - QMap > >::iterator it2 = mPostVersions.find(sub_msg_id); - - if(it2 != mPostVersions.end()) - { - for(uint32_t j=0;j<(*it2).size();++j) - v.append((*it2)[j]) ; - - mPostVersions.erase(it2) ; // it2 is never equal to it - } - } + mPostVersions.erase(it2) ; // it2 is never equal to it + } + } } - // Now remove from msg ids, all posts except the most recent one. + // Now remove from msg ids, all posts except the most recent one. And make the mPostVersion be indexed by the most recent version of the post, + // which corresponds to the item in the tree widget. #ifdef DEBUG_FORUMS std::cerr << "Final post versions: " << std::endl; #endif + QMap > > mTmp; + for(QMap > >::iterator it(mPostVersions.begin());it!=mPostVersions.end();++it) { #ifdef DEBUG_FORUMS std::cerr << "Original post: " << it.key() << std::endl; #endif - if(!(*it).empty()) - msgs.erase(it.key()) ; + // Finally, sort the posts from newer to older - for(uint32_t i=0;i<(*it).size();++i) - { - if(i > 0) - msgs.erase((*it)[i].second) ; + qSort((*it).begin(),(*it).end(),decreasing_time_comp) ; #ifdef DEBUG_FORUMS - std::cerr << " new version " << (*it)[i].first << " " << (*it)[i].second << std::endl; + std::cerr << " most recent version " << (*it)[0].first << " " << (*it)[0].second << std::endl; +#endif + for(uint32_t i=1;i<(*it).size();++i) + { + msgs.erase((*it)[i].second) ; + +#ifdef DEBUG_FORUMS + std::cerr << " older version " << (*it)[i].first << " " << (*it)[i].second << std::endl; #endif } + + mTmp[(*it)[0].second] = *it ; // index the versions map by the ID of the most recent post. } + mPostVersions = mTmp ; // The first step is to find the top level thread messages. These are defined as the messages without // any parent message ID.