simplified the addIfUnique mechanism in NewsFeed by using a data-based identifier for items

This commit is contained in:
csoler 2019-12-03 22:30:13 +01:00
parent fcbecbaa16
commit b85be7cc8e
No known key found for this signature in database
GPG Key ID: 7BCA522266C0804C
21 changed files with 62 additions and 165 deletions

View File

@ -213,15 +213,8 @@ void NewsFeed::handleConnectionEvent(std::shared_ptr<const RsEvent> event)
switch(e.mConnectionType)
{
case RsConnectionEvent::PEER_CONNECTED:
addFeedItemIfUnique(new PeerItem(this, NEWSFEED_PEERLIST, e.mSslId, PEER_TYPE_CONNECT, false),
PEER_TYPE_CONNECT,
e.mSslId.toStdString(),
std::string(),
std::string(),
std::string(),
true);
break;
case RsConnectionEvent::PEER_CONNECTED: addFeedItemIfUnique(new PeerItem(this, NEWSFEED_PEERLIST, e.mSslId, PEER_TYPE_CONNECT, false), true);
break;
case RsConnectionEvent::PEER_DISCONNECTED: break;// not handled yet
case RsConnectionEvent::PEER_REFUSED_CONNECTION:
@ -230,20 +223,9 @@ void NewsFeed::handleConnectionEvent(std::shared_ptr<const RsEvent> event)
if(!rsPeers->getPeerDetails(e.mSslId,det))
return;
addFeedItemIfUnique(new SecurityItem(this,
NEWSFEED_SECLIST,
det.gpg_id, e.mSslId,
det.location,
std::string(),
RS_FEED_ITEM_SEC_AUTH_DENIED,
false),
RS_FEED_ITEM_SEC_AUTH_DENIED,
e.mSslId.toStdString(),
std::string(),
std::string(),
std::string(),
true );
} break;
addFeedItemIfUnique(new SecurityItem(this, NEWSFEED_SECLIST, det.gpg_id, e.mSslId, det.location, std::string(), RS_FEED_ITEM_SEC_AUTH_DENIED, false), true );
}
break;
default: break;
}
@ -280,19 +262,7 @@ void NewsFeed::handleSecurityEvent(std::shared_ptr<const RsEvent> event)
RsPeerDetails det;
rsPeers->getPeerDetails(e.mSslId,det) || rsPeers->getGPGDetails(e.mPgpId,det);
addFeedItemIfUnique(new SecurityItem(this,
NEWSFEED_SECLIST,
det.gpg_id, det.id,
det.location,
e.mLocator.toString(),
FeedItemType,
true),
FeedItemType,
e.mSslId.toStdString(),
std::string(),
std::string(),
std::string(),
true );
addFeedItemIfUnique(new SecurityItem(this, NEWSFEED_SECLIST, det.gpg_id, det.id, det.location, e.mLocator.toString(), FeedItemType, true), true );
if (Settings->getMessageFlags() & RS_MESSAGE_CONNECT_ATTEMPT)
MessageComposer::addConnectAttemptMsg(e.mPgpId, e.mSslId, QString::fromStdString(det.name + "(" + det.location + ")"));
@ -1146,80 +1116,14 @@ void NewsFeed::addFeedItem(FeedItem *item)
ui->feedWidget->addFeedItem(item, ROLE_RECEIVED, QDateTime::currentDateTime());
}
struct AddFeedItemIfUniqueData
void NewsFeed::addFeedItemIfUnique(FeedItem *item, bool replace)
{
AddFeedItemIfUniqueData(FeedItem *feedItem, int type
, const std::string& id1, const std::string& id2
, const std::string& id3, const std::string& id4)
: mType(type), mId1(id1), mId2(id2), mId3(id3), mId4(id4)
{
mGxsCircleItem = dynamic_cast<GxsCircleItem*>(feedItem);
mPeerItem = dynamic_cast<PeerItem*>(feedItem);
mSecItem = dynamic_cast<SecurityItem*>(feedItem);
mSecurityIpItem = dynamic_cast<SecurityIpItem*>(feedItem);
}
FeedItem *feedItem = ui->feedWidget->findFeedItem(item->uniqueIdentifier());
int mType;
const std::string& mId1;
const std::string& mId2;
const std::string& mId3;
const std::string& mId4;
GxsCircleItem *mGxsCircleItem;
PeerItem *mPeerItem;
SecurityItem *mSecItem;
SecurityIpItem *mSecurityIpItem;
};
static bool addFeedItemIfUniqueCallback(FeedItem *feedItem, void *data)
{
AddFeedItemIfUniqueData *findData = (AddFeedItemIfUniqueData*) data;
if (!findData || findData->mId1.empty()) {
return false;
}
if (findData->mGxsCircleItem) {
GxsCircleItem *gxsCircleItem = dynamic_cast<GxsCircleItem*>(feedItem);
if (gxsCircleItem && gxsCircleItem->isSame(RsGxsCircleId(findData->mId1), RsGxsId(findData->mId2), findData->mType)) {
return true;
}
return false;
}
if (findData->mPeerItem) {
PeerItem *peerItem = dynamic_cast<PeerItem*>(feedItem);
if (peerItem && peerItem->isSame(RsPeerId(findData->mId1), findData->mType)) {
return true;
}
return false;
}
if (findData->mSecItem) {
SecurityItem *secitem = dynamic_cast<SecurityItem*>(feedItem);
if (secitem && secitem->isSame(RsPeerId(findData->mId1), findData->mType)) {
return true;
}
return false;
}
if (findData->mSecurityIpItem) {
SecurityIpItem *securityIpItem = dynamic_cast<SecurityIpItem*>(feedItem);
if (securityIpItem && securityIpItem->isSame(RsPeerId(findData->mId1), findData->mId2, findData->mId3, findData->mType)) {
return true;
}
return false;
}
return false;
}
void NewsFeed::addFeedItemIfUnique(FeedItem *item, int itemType, const std::string& id1, const std::string& id2, const std::string& id3, const std::string& id4, bool replace)
{
AddFeedItemIfUniqueData data(item, itemType, id1, id2, id3, id4);
FeedItem *feedItem = ui->feedWidget->findFeedItem(addFeedItemIfUniqueCallback, &data);
if (feedItem) {
if (!replace) {
if (feedItem)
{
if (!replace)
{
delete item;
return;
}
@ -1230,12 +1134,12 @@ void NewsFeed::addFeedItemIfUnique(FeedItem *item, int itemType, const std::stri
addFeedItem(item);
}
void NewsFeed::remUniqueFeedItem(FeedItem *item, int itemType, const std::string &id1, const std::string &id2, const std::string &id3, const std::string &id4)
void NewsFeed::remUniqueFeedItem(FeedItem *item)
{
AddFeedItemIfUniqueData data(item, itemType, id1, id2, id3, id4);
FeedItem *feedItem = ui->feedWidget->findFeedItem(addFeedItemIfUniqueCallback, &data);
FeedItem *feedItem = ui->feedWidget->findFeedItem(item->uniqueIdentifier());
if (feedItem) {
if (feedItem)
{
delete item;
ui->feedWidget->removeFeedItem(feedItem);
@ -1304,7 +1208,7 @@ void NewsFeed::addFeedItemPeerOffset(const RsFeedItem &fi)
PeerItem *pi = new PeerItem(this, NEWSFEED_PEERLIST, RsPeerId(fi.mId1), PEER_TYPE_OFFSET, false);
/* add to layout */
addFeedItemIfUnique(pi, PEER_TYPE_OFFSET, fi.mId1, fi.mId2, fi.mId3, fi.mId4, false);
addFeedItemIfUnique(pi, false);
#ifdef NEWS_DEBUG
std::cerr << "NewsFeed::addFeedItemPeerOffset()";
@ -1318,7 +1222,7 @@ void NewsFeed::addFeedItemSecurityConnectAttempt(const RsFeedItem &fi)
SecurityItem *pi = new SecurityItem(this, NEWSFEED_SECLIST, RsPgpId(fi.mId1), RsPeerId(fi.mId2), fi.mId3, fi.mId4, fi.mType, false);
/* add to layout */
addFeedItemIfUnique(pi, fi.mType, fi.mId2, "", "", "", false);
addFeedItemIfUnique(pi, false);
#ifdef NEWS_DEBUG
std::cerr << "NewsFeed::addFeedItemSecurityConnectAttempt()";
@ -1332,7 +1236,7 @@ void NewsFeed::addFeedItemSecurityAuthDenied(const RsFeedItem &fi)
SecurityItem *pi = new SecurityItem(this, NEWSFEED_SECLIST, RsPgpId(fi.mId1), RsPeerId(fi.mId2), fi.mId3, fi.mId4, fi.mType, false);
/* add to layout */
addFeedItemIfUnique(pi, RS_FEED_ITEM_SEC_AUTH_DENIED, fi.mId2, "", "", "", false);
addFeedItemIfUnique(pi, false);
#ifdef NEWS_DEBUG
std::cerr << "NewsFeed::addFeedItemSecurityAuthDenied()";
@ -1346,7 +1250,7 @@ void NewsFeed::addFeedItemSecurityUnknownIn(const RsFeedItem &fi)
SecurityItem *pi = new SecurityItem(this, NEWSFEED_SECLIST, RsPgpId(fi.mId1), RsPeerId(fi.mId2), fi.mId3, fi.mId4, RS_FEED_ITEM_SEC_UNKNOWN_IN, false);
/* add to layout */
addFeedItemIfUnique(pi, RS_FEED_ITEM_SEC_UNKNOWN_IN, fi.mId2, "", "", "", false);
addFeedItemIfUnique(pi, false);
#ifdef NEWS_DEBUG
std::cerr << "NewsFeed::addFeedItemSecurityUnknownIn()";
@ -1360,7 +1264,7 @@ void NewsFeed::addFeedItemSecurityUnknownOut(const RsFeedItem &fi)
SecurityItem *pi = new SecurityItem(this, NEWSFEED_SECLIST, RsPgpId(fi.mId1), RsPeerId(fi.mId2), fi.mId3, fi.mId4, RS_FEED_ITEM_SEC_UNKNOWN_OUT, false);
/* add to layout */
addFeedItemIfUnique(pi, RS_FEED_ITEM_SEC_UNKNOWN_OUT, fi.mId2, "", "", "", false);
addFeedItemIfUnique(pi, false);
#ifdef NEWS_DEBUG
std::cerr << "NewsFeed::addFeedItemSecurityUnknownOut()";
@ -1374,7 +1278,7 @@ void NewsFeed::addFeedItemSecurityIpBlacklisted(const RsFeedItem &fi, bool isTes
SecurityIpItem *pi = new SecurityIpItem(this, RsPeerId(fi.mId1), fi.mId2, fi.mResult1, RS_FEED_ITEM_SEC_IP_BLACKLISTED, isTest);
/* add to layout */
addFeedItemIfUnique(pi, RS_FEED_ITEM_SEC_IP_BLACKLISTED, fi.mId1, fi.mId2, fi.mId3, fi.mId4, false);
addFeedItemIfUnique(pi, false);
#ifdef NEWS_DEBUG
std::cerr << "NewsFeed::addFeedItemSecurityIpBlacklisted()";
@ -1388,7 +1292,7 @@ void NewsFeed::addFeedItemSecurityWrongExternalIpReported(const RsFeedItem &fi,
SecurityIpItem *pi = new SecurityIpItem(this, RsPeerId(fi.mId1), fi.mId2, fi.mId3, RS_FEED_ITEM_SEC_IP_WRONG_EXTERNAL_IP_REPORTED, isTest);
/* add to layout */
addFeedItemIfUnique(pi, RS_FEED_ITEM_SEC_IP_WRONG_EXTERNAL_IP_REPORTED, fi.mId1, fi.mId2, fi.mId3, fi.mId4, false);
addFeedItemIfUnique(pi, false);
#ifdef NEWS_DEBUG
std::cerr << "NewsFeed::addFeedItemSecurityWrongExternalIpReported()";
@ -1656,7 +1560,7 @@ void NewsFeed::addFeedItemCircleMembReq(const RsFeedItem &fi)
GxsCircleItem *item = new GxsCircleItem(this, NEWSFEED_CIRCLELIST, circleId, gxsId, RS_FEED_ITEM_CIRCLE_MEMB_REQ);
/* add to layout */
addFeedItemIfUnique(item, RS_FEED_ITEM_CIRCLE_MEMB_REQ, fi.mId1, fi.mId2, fi.mId3, fi.mId4, false);
addFeedItemIfUnique(item, false);
#ifdef NEWS_DEBUG
std::cerr << "NewsFeed::addFeedItemCircleMembReq()" << std::endl;
@ -1676,7 +1580,7 @@ void NewsFeed::remFeedItemCircleMembReq(const RsFeedItem &fi)
GxsCircleItem *item = new GxsCircleItem(this, NEWSFEED_CIRCLELIST, circleId, gxsId, RS_FEED_ITEM_CIRCLE_MEMB_REQ);
/* add to layout */
remUniqueFeedItem(item, RS_FEED_ITEM_CIRCLE_MEMB_REQ, fi.mId1, fi.mId2, fi.mId3, fi.mId4);
remUniqueFeedItem(item);
#ifdef NEWS_DEBUG
std::cerr << "NewsFeed::remFeedItemCircleMembReq()" << std::endl;

View File

@ -106,8 +106,8 @@ private:
void handleConnectionEvent(std::shared_ptr<const RsEvent> event);
void addFeedItem(FeedItem *item);
void addFeedItemIfUnique(FeedItem *item, int itemType, const std::string& id1, const std::string& id2, const std::string& id3, const std::string& id4, bool replace);
void remUniqueFeedItem(FeedItem *item, int itemType, const std::string& id1, const std::string& id2, const std::string& id3, const std::string& id4);
void addFeedItemIfUnique(FeedItem *item, bool replace);
void remUniqueFeedItem(FeedItem *item);
void addFeedItemPeerConnect(const RsFeedItem &fi);
void addFeedItemPeerDisconnect(const RsFeedItem &fi);

View File

@ -49,6 +49,7 @@ public:
const RsPostedPost &getPost() const;
RsPostedPost &post();
QString uniqueIdentifier() const override { return "PostedItem " + QString::fromStdString(mMessageId.toStdString()); }
protected:
/* FeedItem */
virtual void doExpand(bool open);

View File

@ -476,14 +476,13 @@ void RSFeedWidget::withAll(RSFeedWidgetCallbackFunction callback, void *data)
}
}
FeedItem *RSFeedWidget::findFeedItem(RSFeedWidgetFindCallbackFunction callback, void *data)
FeedItem *RSFeedWidget::findFeedItem(const QString& identifier)
{
if (!callback) {
return NULL;
}
QTreeWidgetItemIterator it(ui->treeWidget);
QTreeWidgetItem *treeItem;
// this search could probably be automatised by giving the tree items the identifier as data for some specific role, then calling QTreeWidget::findItems()
#warning TODO
while ((treeItem = *it) != NULL) {
++it;
@ -492,9 +491,8 @@ FeedItem *RSFeedWidget::findFeedItem(RSFeedWidgetFindCallbackFunction callback,
continue;
}
if (callback(feedItem, data)) {
if (feedItem->uniqueIdentifier() == identifier)
return feedItem;
}
}
return NULL;

View File

@ -71,7 +71,7 @@ public:
bool scrollTo(FeedItem *feedItem, bool focus);
void withAll(RSFeedWidgetCallbackFunction callback, void *data);
FeedItem *findFeedItem(RSFeedWidgetFindCallbackFunction callback, void *data);
FeedItem *findFeedItem(const QString &identifier);
void selectedFeedItems(QList<FeedItem*> &feedItems);

View File

@ -37,6 +37,7 @@ public:
void updateItemStatic();
virtual QString uniqueIdentifier() const override { return "ChatMsgItem " + QString::fromStdString(mPeerId.toStdString()) ;}
protected:
/* FeedItem */
virtual void doExpand(bool /*open*/) {}

View File

@ -36,6 +36,12 @@ public:
bool wasExpanded() { return mWasExpanded; }
void expand(bool open);
/*!
* \brief uniqueIdentifier
* \return returns a string that is unique to this specific item. The string will be used to search for an existing item that
* would contain the same information. It should therefore sumarise the data represented by the item.
*/
virtual QString uniqueIdentifier() const =0;
protected:
virtual void doExpand(bool open) = 0;
virtual void expandFill(bool /*first*/) {}

View File

@ -42,6 +42,7 @@ public:
bool setGroup(const RsGxsChannelGroup &group);
QString uniqueIdentifier() const override { return "GxsChannelGroupItem " + QString::fromStdString(mGroup.mMeta.mGroupId.toStdString()) ; }
protected:
/* FeedItem */
virtual void doExpand(bool open);

View File

@ -53,6 +53,8 @@ public:
//GxsChannelPostItem(FeedHolder *feedHolder, uint32_t feedId, const RsGxsChannelPost &post, bool isHome, bool autoUpdate);
virtual ~GxsChannelPostItem();
virtual QString uniqueIdentifier() const override { "GxsChannelPostItem " + QString::fromStdString(mPost.mMeta.mMsgId.toStdString()) ; }
bool setGroup(const RsGxsChannelGroup &group, bool doFill = true);
bool setPost(const RsGxsChannelPost &post, bool doFill = true);

View File

@ -125,14 +125,9 @@ void GxsCircleItem::setup()
}
bool GxsCircleItem::isSame(const RsGxsCircleId &circleId, const RsGxsId &gxsId, uint32_t type)
QString GxsCircleItem::uniqueIdentifier() const
{
if ((mCircleId == circleId) && (mGxsId == gxsId) && (mType == type))
{
return true;
}
return false;
return "GxsCircle " + QString::fromStdString(mCircleId.toStdString()) + " " + QString::fromStdString(mGxsId.toStdString()) + " " + QString::number(mType);
}
void GxsCircleItem::removeItem()

View File

@ -52,8 +52,7 @@ public:
GxsCircleItem(FeedHolder *feedHolder, uint32_t feedId, const RsGxsCircleId &circleId, const RsGxsId &gxsId, const uint32_t type);
virtual ~GxsCircleItem();
bool isSame(const RsGxsCircleId &circleId, const RsGxsId &gxsId, uint32_t type);
QString uniqueIdentifier() const override;
void loadRequest(const TokenQueue *queue, const TokenRequest &req);

View File

@ -42,6 +42,7 @@ public:
bool setGroup(const RsGxsForumGroup &group);
virtual QString uniqueIdentifier() const override { return "GxsForumGroupItem " + QString::fromStdString(mGroup.mMeta.mGroupId.toStdString()) ; }
protected:
/* FeedItem */
virtual void doExpand(bool open);

View File

@ -43,6 +43,7 @@ public:
bool setGroup(const RsGxsForumGroup &group, bool doFill = true);
bool setMessage(const RsGxsForumMsg &msg, bool doFill = true);
QString uniqueIdentifier() const override { return "GxsForumMsgItem " + QString::fromStdString(mMessage.mMeta.mMsgId.toStdString()) ; }
protected:
/* FeedItem */
virtual void doExpand(bool open);

View File

@ -38,6 +38,7 @@ public:
void updateItemStatic();
virtual QString uniqueIdentifier() const override { return "MsgItem " + QString::fromStdString(mMsgId) ; }
protected:
/* FeedItem */
virtual void doExpand(bool open);

View File

@ -67,17 +67,11 @@ PeerItem::PeerItem(FeedHolder *parent, uint32_t feedId, const RsPeerId &peerId,
updateItem();
}
bool PeerItem::isSame(const RsPeerId &peerId, uint32_t type)
QString PeerItem::uniqueIdentifier() const
{
if ((mPeerId == peerId) && (mType == type))
{
return true;
}
return false;
return "PeerItem " + QString::fromStdString(mPeerId.toStdString()) + " " + QString::number(mType) ;
}
void PeerItem::updateItemStatic()
{
if (!rsPeers)

View File

@ -43,7 +43,7 @@ public:
void updateItemStatic();
bool isSame(const RsPeerId &peerId, uint32_t type);
virtual QString uniqueIdentifier() const;
protected:
/* FeedItem */

View File

@ -42,6 +42,8 @@ public:
bool setGroup(const RsPostedGroup &group);
virtual QString uniqueIdentifier() const override { return "PostedGroupItem " + QString::fromStdString(mGroup.mMeta.mGroupId.toStdString()) ; }
protected:
/* FeedItem */
virtual void doExpand(bool open);

View File

@ -78,13 +78,10 @@ void SecurityIpItem::setup()
updateItem();
}
bool SecurityIpItem::isSame(const RsPeerId &sslId, const std::string& ipAddr, const std::string& ipAddrReported, uint32_t type)
QString SecurityIpItem::uniqueIdentifier() const
{
if (mType == type && mSslId==sslId && mIpAddr == ipAddr && mIpAddrReported == ipAddrReported) {
return true;
}
return false;
return "SecurityItem " + QString::number(mType) + " " + QString::fromStdString(mSslId.toStdString())
+ " " + QString::fromStdString(mIpAddr) + " " + QString::fromStdString(mIpAddrReported) ;
}
void SecurityIpItem::updateItemStatic()

View File

@ -43,7 +43,7 @@ public:
void updateItemStatic();
bool isSame(const RsPeerId &sslId, const std::string& ipAddr, const std::string& ipAddrReported, uint32_t type);
QString uniqueIdentifier() const override;
protected:
/* FeedItem */

View File

@ -81,17 +81,11 @@ SecurityItem::SecurityItem(FeedHolder *parent, uint32_t feedId, const RsPgpId &g
updateItem();
}
bool SecurityItem::isSame(const RsPeerId &sslId, uint32_t type)
QString SecurityItem::uniqueIdentifier() const
{
if ((mSslId == sslId) && (mType == type))
{
return true;
}
return false;
return "SecurityItem " + QString::number(mType) + " " + QString::fromStdString(mSslId.toStdString());
}
void SecurityItem::updateItemStatic()
{
if (!rsPeers)

View File

@ -42,7 +42,7 @@ public:
void updateItemStatic();
bool isSame(const RsPeerId &sslId, uint32_t type);
QString uniqueIdentifier() const override;
protected:
/* FeedItem */