mirror of
https://github.com/RetroShare/RetroShare.git
synced 2025-01-14 17:07:17 -05:00
simplified loginc in deleting duplicate items
This commit is contained in:
parent
f8d07a3e92
commit
87e92e5229
@ -515,6 +515,68 @@ void IdDialog::updateCircles()
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static QTreeWidgetItem *setChildItem(QTreeWidgetItem *item, const RsGroupMetaData& circle_group)
|
||||||
|
{
|
||||||
|
QString test_str = QString::fromStdString(circle_group.mGroupId.toStdString());
|
||||||
|
|
||||||
|
// 1 - check if the item already exists and remove possible duplicates
|
||||||
|
|
||||||
|
std::vector<uint32_t> found_indices;
|
||||||
|
|
||||||
|
for(uint32_t k=0; k < (uint32_t)item->childCount(); ++k)
|
||||||
|
if( item->child(k)->data(CIRCLEGROUP_CIRCLE_COL_GROUPID,Qt::UserRole).toString() == test_str)
|
||||||
|
found_indices.push_back(k);
|
||||||
|
|
||||||
|
while(found_indices.size() > 1) // delete duplicates, starting from the end in order that deletion preserves indices
|
||||||
|
{
|
||||||
|
delete item->takeChild(found_indices.back());
|
||||||
|
found_indices.pop_back();
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!found_indices.empty())
|
||||||
|
{
|
||||||
|
QTreeWidgetItem *subitem = item->child(found_indices[0]);
|
||||||
|
|
||||||
|
if(subitem->text(CIRCLEGROUP_CIRCLE_COL_GROUPNAME) != QString::fromUtf8(circle_group.mGroupName.c_str()))
|
||||||
|
{
|
||||||
|
#ifdef ID_DEBUG
|
||||||
|
std::cerr << " Existing circle has a new name. Updating it in the tree." << std::endl;
|
||||||
|
#endif
|
||||||
|
subitem->setText(CIRCLEGROUP_CIRCLE_COL_GROUPNAME, QString::fromUtf8(circle_group.mGroupName.c_str()));
|
||||||
|
}
|
||||||
|
|
||||||
|
return subitem;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2 - if not, create
|
||||||
|
|
||||||
|
QTreeWidgetItem *subitem = new QTreeWidgetItem();
|
||||||
|
|
||||||
|
subitem->setText(CIRCLEGROUP_CIRCLE_COL_GROUPNAME, QString::fromUtf8(circle_group.mGroupName.c_str()));
|
||||||
|
subitem->setData(CIRCLEGROUP_CIRCLE_COL_GROUPID,Qt::UserRole, QString::fromStdString(circle_group.mGroupId.toStdString()));
|
||||||
|
subitem->setData(CIRCLEGROUP_CIRCLE_COL_GROUPFLAGS, Qt::UserRole, QVariant(circle_group.mSubscribeFlags));
|
||||||
|
|
||||||
|
item->addChild(subitem);
|
||||||
|
|
||||||
|
return subitem;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void removeChildItem(QTreeWidgetItem *item, const RsGroupMetaData& circle_group)
|
||||||
|
{
|
||||||
|
QString test_str = QString::fromStdString(circle_group.mGroupId.toStdString());
|
||||||
|
|
||||||
|
// 1 - check if the item already exists and remove possible duplicates
|
||||||
|
|
||||||
|
std::list<uint32_t> found_indices;
|
||||||
|
|
||||||
|
for(uint32_t k=0; k < (uint32_t)item->childCount(); ++k)
|
||||||
|
if( item->child(k)->data(CIRCLEGROUP_CIRCLE_COL_GROUPID,Qt::UserRole).toString() == test_str)
|
||||||
|
found_indices.push_front(k);
|
||||||
|
|
||||||
|
for(auto k:found_indices)
|
||||||
|
delete item->takeChild(k); // delete items in the reverse order (because of the push_front()), so that indices are preserved
|
||||||
|
}
|
||||||
|
|
||||||
void IdDialog::loadCircles(const std::list<RsGroupMetaData>& groupInfo)
|
void IdDialog::loadCircles(const std::list<RsGroupMetaData>& groupInfo)
|
||||||
{
|
{
|
||||||
#ifdef ID_DEBUG
|
#ifdef ID_DEBUG
|
||||||
@ -561,102 +623,27 @@ void IdDialog::loadCircles(const std::list<RsGroupMetaData>& groupInfo)
|
|||||||
bool am_I_in_circle = details.mAmIAllowed ;
|
bool am_I_in_circle = details.mAmIAllowed ;
|
||||||
bool am_I_admin (vit->mSubscribeFlags & GXS_SERV::GROUP_SUBSCRIBE_ADMIN) ;
|
bool am_I_admin (vit->mSubscribeFlags & GXS_SERV::GROUP_SUBSCRIBE_ADMIN) ;
|
||||||
bool am_I_subscribed (vit->mSubscribeFlags & GXS_SERV::GROUP_SUBSCRIBE_SUBSCRIBED) ;
|
bool am_I_subscribed (vit->mSubscribeFlags & GXS_SERV::GROUP_SUBSCRIBE_SUBSCRIBED) ;
|
||||||
QTreeWidgetItem *item = NULL ;
|
|
||||||
|
|
||||||
#ifdef ID_DEBUG
|
#ifdef ID_DEBUG
|
||||||
std::cerr << "Loaded info for circle " << vit->mGroupId << ". am_I_in_circle=" << am_I_in_circle << std::endl;
|
std::cerr << "Loaded info for circle " << vit->mGroupId << ". am_I_in_circle=" << am_I_in_circle << std::endl;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// find already existing items for this circle
|
// Find already existing items for this circle, or create one.
|
||||||
|
QTreeWidgetItem *item = NULL ;
|
||||||
|
|
||||||
// implement the search manually, because there's no find based on user role.
|
|
||||||
//QList<QTreeWidgetItem*> clist = ui->treeWidget_membership->findItems( QString::fromStdString(vit->mGroupId.toStdString()), Qt::MatchExactly|Qt::MatchRecursive, CIRCLEGROUP_CIRCLE_COL_GROUPID);
|
|
||||||
QList<QTreeWidgetItem*> clist ;
|
|
||||||
QString test_str = QString::fromStdString(vit->mGroupId.toStdString()) ;
|
|
||||||
for(QTreeWidgetItemIterator itt(ui->treeWidget_membership);*itt;++itt)
|
|
||||||
if( (*itt)->data(CIRCLEGROUP_CIRCLE_COL_GROUPID,Qt::UserRole).toString() == test_str)
|
|
||||||
clist.push_back(*itt) ;
|
|
||||||
|
|
||||||
if(!clist.empty())
|
|
||||||
{
|
|
||||||
// delete all duplicate items. This should not happen, but just in case it does.
|
|
||||||
|
|
||||||
while(clist.size() > 1)
|
|
||||||
{
|
|
||||||
#ifdef ID_DEBUG
|
|
||||||
std::cerr << " more than 1 item correspond to this ID. Removing!" << std::endl;
|
|
||||||
#endif
|
|
||||||
delete clist.front() ;
|
|
||||||
clist.pop_front();
|
|
||||||
}
|
|
||||||
|
|
||||||
item = clist.front() ;
|
|
||||||
|
|
||||||
#ifdef CIRCLE_MEMBERSHIP_CATEGORIES
|
|
||||||
if(am_I_in_circle && item->parent() != mExternalBelongingCircleItem)
|
|
||||||
{
|
|
||||||
#ifdef ID_DEBUG
|
|
||||||
std::cerr << " Existing circle is not in subscribed items although it is subscribed. Removing." << std::endl;
|
|
||||||
#endif
|
|
||||||
delete item ;
|
|
||||||
item = NULL ;
|
|
||||||
}
|
|
||||||
else if(!am_I_in_circle && item->parent() != mExternalOtherCircleItem)
|
|
||||||
{
|
|
||||||
#ifdef ID_DEBUG
|
|
||||||
std::cerr << " Existing circle is not in subscribed items although it is subscribed. Removing." << std::endl;
|
|
||||||
#endif
|
|
||||||
delete item ;
|
|
||||||
item = NULL ;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
#endif
|
|
||||||
should_re_add = false ; // item already exists
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Add Widget, and request Pages */
|
|
||||||
|
|
||||||
if(should_re_add)
|
|
||||||
{
|
|
||||||
item = new QTreeWidgetItem();
|
|
||||||
|
|
||||||
item->setText(CIRCLEGROUP_CIRCLE_COL_GROUPNAME, QString::fromUtf8(vit->mGroupName.c_str()));
|
|
||||||
item->setData(CIRCLEGROUP_CIRCLE_COL_GROUPID,Qt::UserRole, QString::fromStdString(vit->mGroupId.toStdString()));
|
|
||||||
item->setData(CIRCLEGROUP_CIRCLE_COL_GROUPFLAGS, Qt::UserRole, QVariant(vit->mSubscribeFlags));
|
|
||||||
|
|
||||||
#ifdef CIRCLE_MEMBERSHIP_CATEGORIES
|
|
||||||
if(am_I_in_circle)
|
if(am_I_in_circle)
|
||||||
{
|
{
|
||||||
#ifdef ID_DEBUG
|
item = setChildItem(mExternalBelongingCircleItem,*vit);
|
||||||
std::cerr << " adding item for circle " << vit->mGroupId << " to own circles"<< std::endl;
|
removeChildItem(mExternalOtherCircleItem,*vit);
|
||||||
#endif
|
|
||||||
mExternalBelongingCircleItem->addChild(item);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
#ifdef ID_DEBUG
|
item = setChildItem(mExternalOtherCircleItem,*vit);
|
||||||
std::cerr << " adding item for circle " << vit->mGroupId << " to others"<< std::endl;
|
removeChildItem(mExternalBelongingCircleItem,*vit);
|
||||||
#endif
|
|
||||||
mExternalOtherCircleItem->addChild(item);
|
|
||||||
}
|
}
|
||||||
#else
|
|
||||||
ui->treeWidget_membership->addTopLevelItem(item) ;
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
else if(item->text(CIRCLEGROUP_CIRCLE_COL_GROUPNAME) != QString::fromUtf8(vit->mGroupName.c_str()))
|
|
||||||
{
|
|
||||||
#ifdef ID_DEBUG
|
|
||||||
std::cerr << " Existing circle has a new name. Updating it in the tree." << std::endl;
|
|
||||||
#endif
|
|
||||||
item->setText(CIRCLEGROUP_CIRCLE_COL_GROUPNAME, QString::fromUtf8(vit->mGroupName.c_str()));
|
|
||||||
}
|
|
||||||
// just in case.
|
|
||||||
|
|
||||||
item->setData(CIRCLEGROUP_CIRCLE_COL_GROUPFLAGS, Qt::UserRole, QVariant(vit->mSubscribeFlags));
|
item->setData(CIRCLEGROUP_CIRCLE_COL_GROUPFLAGS, Qt::UserRole, QVariant(vit->mSubscribeFlags));
|
||||||
|
|
||||||
QString tooltip ;
|
QString tooltip ;
|
||||||
tooltip += tr("Circle ID: ")+QString::fromStdString(vit->mGroupId.toStdString()) ;
|
tooltip += tr("Circle ID: ")+QString::fromStdString(vit->mGroupId.toStdString()) ;
|
||||||
|
|
||||||
tooltip += "\n"+tr("Visibility: ");
|
tooltip += "\n"+tr("Visibility: ");
|
||||||
|
|
||||||
if(details.mRestrictedCircleId == details.mCircleId)
|
if(details.mRestrictedCircleId == details.mCircleId)
|
||||||
|
Loading…
Reference in New Issue
Block a user