加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
UIGroupManager.cpp 34.81 KB
一键复制 编辑 原始数据 按行查看 历史
chaocc.wang 提交于 2020-05-28 18:45 . 版本迭代 - 2020-05-28
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068
#include "UIGroupManager.h"
#include <QVBoxLayout>
#include <QTreeWidget>
#include <QLabel>
#include <QListWidget>
#include <QPushButton>
#include <QHeaderView>
#include <QFile>
#include <QFileInfo>
#include <QStandardItemModel>
#include <thread>
#include <QDateTime>
#include <QSplitter>
#include <QJsonDocument>
#include <QApplication>
#include <QtConcurrent>
#include "../CustomUi/QtMessageBox.h"
#include "../UICom/uicom.h"
#include "../Platform/Platform.h"
#include "../QtUtil/Utils/utils.h"
#include "SearchWgt.h"
#include "ChoseMemberWgt.h"
#include "TreeItemDelegate.h"
#include "../CustomUi/HeadPhotoLab.h"
#include "../Platform/dbPlatForm.h"
#include "BatchAddMemberWnd.h"
#include "../include/Line.h"
#include "../QtUtil/Entity/JID.h"
UIGroupManager::UIGroupManager()
: UShadowDialog(nullptr, true)
, _pSearchWgt(nullptr)
, _pLstWgt(nullptr)
, _plstModel(nullptr)
, _pTipLabel(nullptr)
, _pTitleLabel(nullptr)
, _pGroupNameEdit(nullptr)
, _pGroupNaemFrm(nullptr)
, _pTreeWgt(nullptr)
, _pTreeModel(nullptr)
, _batchAddMemberBtn(nullptr)
, _batchAddMemberWnd(nullptr)
, _type(EM_TYPE_INVALID)
, _pItemDelegate(nullptr)
, _pRecentItem(nullptr){
#ifndef _LINUX
auto flags = Qt::X11BypassWindowManagerHint | Qt::WindowStaysOnTopHint | Qt::Tool;
setWindowFlags(this->windowFlags() | flags);
#endif
initUi();
//
_pMsgManager = new GroupManagerMsgManager;
_pMsgListener = new GroupMsgListener(this);
connect(this, &UIGroupManager::initDataFinish, this, &UIGroupManager::updateUi, Qt::QueuedConnection);
// init data
initData();
}
UIGroupManager::~UIGroupManager() = default;
//
void UIGroupManager::onCreatGroup(const QString &memberId) {
resetUi(memberId);
_pTitleLabel->setText(tr("创建群组"));
_type = EM_TYPE_CREATGROUP;
_pGroupNaemFrm->setVisible(true);
_pTreeWgt->setVisible(true);
_batchAddMemberBtn->setVisible(true);
//
std::shared_ptr<QTalk::Entity::ImUserInfo> info = dbPlatForm::instance().getUserInfo(memberId.toStdString());
if (info) {
QString headrSrc = QString(QTalk::GetHeadPathByUrl(info->HeaderSrc).c_str());
if (QFileInfo(headrSrc).isDir() || !QFile::exists(headrSrc)) {
#ifdef _STARTALK
headrSrc = ":/QTalk/image1/StarTalk_defaultHead.png";
#else
headrSrc = ":/QTalk/image1/headPortrait.png";
#endif
}
addGroupMember(memberId, headrSrc);
//
// QWidget *wgt = UICom::getInstance()->getAcltiveMainWnd();
this->setVisible(false);
this->setVisible(true);
}
}
//
void UIGroupManager::onAddGroupMember(const QString &groupId) {
resetUi();
_pTitleLabel->setText(tr("加人进群"));
_type = EM_TYPE_ADDGROUPMEMBER;
_strGroupId = groupId;
_pGroupNaemFrm->setVisible(false);
_pTreeWgt->setVisible(true);
_batchAddMemberBtn->setVisible(true);
// QWidget *wgt = UICom::getInstance()->getAcltiveMainWnd();
// showCenter(true, wgt);
this->setVisible(false);
this->setVisible(true);
}
//
void UIGroupManager::initUi() {
setFixedSize(650, 570);
setObjectName("UIGroupManager");
//
auto *titleFrm = new QFrame();
titleFrm->setFixedHeight(50);
titleFrm->setObjectName("titleFrm");
auto *titleLay = new QHBoxLayout(titleFrm);
titleLay->setContentsMargins(20, 0, 20, 0);
_pTitleLabel = new QLabel();
_pTitleLabel->setAlignment(Qt::AlignVCenter);
_pTitleLabel->setObjectName("TitleLabel");
titleLay->addWidget(_pTitleLabel);
titleLay->addItem(new QSpacerItem(10, 10, QSizePolicy::Expanding));
setMoverAble(true, titleFrm);
//
_pSearchWgt = new SearchWgt(this);
// tree
_pTreeModel = new QStandardItemModel(this);
_pTreeWgt = new QTreeView(this);
_pTreeWgt->setFrameShape(QFrame::NoFrame);
_pTreeWgt->setEditTriggers(QAbstractItemView::NoEditTriggers);
_pTreeWgt->setModel(_pTreeModel);
_pTreeWgt->header()->setVisible(false);
_pTreeWgt->setFrameShape(QFrame::NoFrame);
_pTreeWgt->setSortingEnabled(true);
_pTreeWgt->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
_pTreeWgt->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
_pItemDelegate = new TreeItemDelegate(this);
_pTreeWgt->setItemDelegate(_pItemDelegate);
_pTreeWgt->setSelectionMode(QAbstractItemView::NoSelection);
_pTreeWgt->setRootIsDecorated(false);
_pTreeWgt->setExpandsOnDoubleClick(false);
// search view
_pSearchView = new QListView(this);
_pSearchSrcModel = new QStandardItemModel(this);
_pSearchModel = new SearchItemSortModel(this);
_pSearchItemDelegate = new SearchItemDelegate(this);
_pSearchModel->setSourceModel(_pSearchSrcModel);
_pSearchView->setModel(_pSearchModel);
_pSearchView->setEditTriggers(QAbstractItemView::NoEditTriggers);
_pSearchView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
_pSearchView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
_pSearchView->setItemDelegate(_pSearchItemDelegate);
_pSearchView->setFrameShape(QFrame::NoFrame);
// group
// auto *item = new QSpacerItem(1, 1, QSizePolicy::Fixed, QSizePolicy::Expanding);
//
QFrame *leftFrm = new QFrame(this);
auto *leftLayout = new QVBoxLayout(leftFrm);
leftLayout->setMargin(0);
leftLayout->addWidget(_pSearchWgt);
leftLayout->addWidget(_pTreeWgt);
leftLayout->addWidget(_pSearchView);
leftLayout->setStretch(0, 0);
//
_pSearchView->setVisible(false);
//
QFrame *rightFrm = new QFrame(this);
auto *rightLayout = new QVBoxLayout(rightFrm);
auto *btnlayout = new QHBoxLayout;
btnlayout->setContentsMargins(12, 10, 18, 10);
btnlayout->setSpacing(15);
_pTipLabel = new QLabel(QString(tr("已选择了 %1 个联系人")).arg(0), this);
_pTipLabel->setObjectName("TipLabel");
_pTipLabel->setContentsMargins(6, 6, 6, 10);
auto *listItemDelegate = new ChoseItemDelegate(this);
_pLstWgt = new QListView(this);
_pLstWgt->setEditTriggers(QAbstractItemView::NoEditTriggers);
_plstModel = new QStandardItemModel(this);
_pLstWgt->setModel(_plstModel);
_pLstWgt->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
_pLstWgt->setItemDelegate(listItemDelegate);
_pLstWgt->setFrameShape(QFrame::NoFrame);
// group name
_pGroupNaemFrm = new QFrame();
_pGroupNaemFrm->setObjectName("groupNaemFrm");
auto *groupNameLay = new QHBoxLayout(_pGroupNaemFrm);
groupNameLay->setContentsMargins(20, 20, 5, 20);
groupNameLay->setSpacing(10);
auto *groupNameLab = new QLabel(tr("群名称"), this);
groupNameLab->setObjectName("GroupNameLabel");
_pGroupNameEdit = new QLineEdit(this);
_pGroupNameEdit->setObjectName("GroupNameEdit");
_pGroupNameEdit->setPlaceholderText(tr("取个群名方便后续搜索"));
_pGroupNameEdit->setFixedHeight(30);
groupNameLay->addWidget(groupNameLab);
groupNameLay->addWidget(_pGroupNameEdit);
//
auto *cancelBtn = new QPushButton(tr("取消"), this);
auto *okBtn = new QPushButton(tr("确定"), this);
_batchAddMemberBtn = new QPushButton(tr("高级"), this);
cancelBtn->setObjectName("cancelButton");
_batchAddMemberBtn->setObjectName("cancelButton");
okBtn->setObjectName("makeSureButton");
cancelBtn->setFixedSize(72, 32);
_batchAddMemberBtn->setFixedSize(72, 32);
okBtn->setFixedSize(72, 32);
cancelBtn->setFocusPolicy(Qt::NoFocus);
_batchAddMemberBtn->setFocusPolicy(Qt::NoFocus);
okBtn->setFocusPolicy(Qt::NoFocus);
btnlayout->addWidget(_batchAddMemberBtn);
btnlayout->addSpacerItem(new QSpacerItem(10, 10, QSizePolicy::Expanding));
btnlayout->addWidget(cancelBtn);
btnlayout->addWidget(okBtn);
rightLayout->addWidget(_pTipLabel);
rightLayout->addWidget(_pLstWgt);
rightLayout->addWidget(_pGroupNaemFrm);
//
auto *splitter = new QSplitter;
splitter->setContentsMargins(20, 10, 10, 10);
splitter->setObjectName("groupManagerSplitter");
splitter->setHandleWidth(1);
splitter->addWidget(leftFrm);
splitter->addWidget(rightFrm);
splitter->setStretchFactor(0, 1);
splitter->setStretchFactor(1, 1);
splitter->setCollapsible(0, false);
splitter->setCollapsible(1, false);
//
QFrame* mainFrm = new QFrame(this);
mainFrm->setObjectName("mainFrm");
auto* mainLay = new QVBoxLayout(mainFrm);
mainLay->setMargin(6);
mainLay->setSpacing(0);
mainLay->addWidget(splitter, 1);
mainLay->addWidget(new Line, 0);
mainLay->addLayout(btnlayout, 0);
//
auto *layout = new QVBoxLayout(_pCenternWgt);
layout->setMargin(0);
layout->setSpacing(0);
layout->addWidget(titleFrm, 0);
layout->addWidget(mainFrm);
_batchAddMemberWnd = new BatchAddMemberWnd(this);
connect(cancelBtn, &QPushButton::clicked, [this]() {
this->setVisible(false);
});
connect(splitter, &QSplitter::splitterMoved, [this](int pos, int index) {
qreal maxW = _pTreeWgt->width();
});
connect(okBtn, &QPushButton::clicked, [this]() {
switch (_type) {
case EM_TYPE_ADDGROUPMEMBER: {
if (!_mapLstWgtItem.empty()) {
sendAddGroupMemberMessage(_strGroupId, _mapLstWgtItem.keys());
}
break;
}
case EM_TYPE_CREATGROUP:
creatGroup();
break;
default:
break;
}
this->setVisible(false);
});
connect(_pSearchItemDelegate, &SearchItemDelegate::itemChanged, [this](QModelIndex index) {
QString memberId = index.data(EM_SEARCH_DATATYPE_XMPPID).toString();
QString headSrc = index.data(EM_SEARCH_DATATYPE_ICONPATH).toString();
bool check = index.data(EM_SEARCH_DATATYPE_CHECKSTATE).toBool();
std::string id = memberId.toStdString();
if(!id.empty())
{
if(check)
addGroupMember(memberId, headSrc);
else
removeGroupMeber(memberId);
}
});
connect(_pSearchItemDelegate, &SearchItemDelegate::sgItemDbClicked, [this](QModelIndex index) {
QString id = index.data(EM_SEARCH_DATATYPE_XMPPID).toString();
QString name = index.data(EM_SEARCH_DATATYPE_TEXT).toString();
onItemDoubleClick(id, name);
});
connect(_pItemDelegate, &TreeItemDelegate::sgItemDbClicked, [this](QModelIndex index) {
int type = index.data(EM_STAFF_DATATYPE_ROW_TYPE).toInt();
if(EM_ROW_TYPE_ITEM == type)
{
QString id = index.data(EM_STAFF_DATATYPE_XMPPID).toString();
QString name = index.data(EM_STAFF_DATATYPE_TEXT).toString();
onItemDoubleClick(id, name);
}
});
connect(_pSearchWgt, &SearchWgt::textChanged, [this](const QString& text) {
_pSearchModel->setFilterRegExp(text.toLower());
//
bool isEmpty = text.isEmpty();
_pSearchView->setVisible(!isEmpty);
_pTreeWgt->setVisible(isEmpty);
});
connect(_batchAddMemberBtn, &QPushButton::clicked, [this](){
QPoint pos = this->geometry().topRight();
_batchAddMemberWnd->clear();
_batchAddMemberWnd->move(pos.x() + 10, pos.y());
_batchAddMemberWnd->showModel();
});
connect(_batchAddMemberWnd, &BatchAddMemberWnd::sgBatchAddGroupMember, this, &UIGroupManager::onBatchAddMember);
connect(listItemDelegate, &ChoseItemDelegate::removeItem, [this](const QString& id){
removeGroupMeber(id);
});
connect(_pItemDelegate, &TreeItemDelegate::itemChanged, [this](QModelIndex index) {
int rowType = index.data(EM_STAFF_DATATYPE_ROW_TYPE).toInt();
if(rowType == EM_ROW_TYPE_ITEM)
{
QString memberId = index.data(EM_STAFF_DATATYPE_XMPPID).toString();
QString headSrc = index.data(EM_STAFF_DATATYPE_ICONPATH).toString();
bool check = index.data(EM_DATATYPE_CHECKSTATE).toBool();
std::string id = memberId.toStdString();
if(!id.empty())
{
if(check)
addGroupMember(memberId, headSrc);
else
removeGroupMeber(memberId);
}
}
else
{
bool extend = _pTreeModel->data(index, EM_STAFF_DATATYPE_EXTEND).toBool();
_pTreeWgt->setExpanded(index, extend);
}
});
qRegisterMetaType<std::vector<QTalk::StShareSession>>("std::vector<QTalk::StShareSession>");
connect(this, &UIGroupManager::sgUpdateSession, this, &UIGroupManager::onUpdateSession);
}
//
void UIGroupManager::addGroupMember(const QString &memberId, const QString &headSrc)
{
if (_mapLstWgtItem.contains(memberId)) return;
auto *item = new QStandardItem();
item->setData(memberId, EM_ITEM_DATA_XMPPID);
QString name = _mapUserName[memberId];
std::string head = headSrc.toStdString();
item->setData(headSrc, EM_ITEM_DATA_HEAD);
item->setData(name, EM_ITEM_DATA_NAME);
_plstModel->appendRow(item);
_mapLstWgtItem[memberId] = item;
_pTipLabel->setText(QString(tr("已选择了 %1 个联系人")).arg(_mapLstWgtItem.size()));
//
std::string id = memberId.toStdString();
if(!id.empty())
{
const auto &mapItems = _mapItems[id];
for(const auto& it : mapItems)
{
auto *tmpItem = it.second;
if(EM_STRUCTURE == it.first)
tmpItem->setData(true, EM_SEARCH_DATATYPE_CHECKSTATE);
else
tmpItem->setData(true, EM_DATATYPE_CHECKSTATE);
}
}
}
//
void UIGroupManager::removeGroupMeber(const QString &memberId) {
auto itFind = _mapLstWgtItem.find(memberId);
if (itFind != _mapLstWgtItem.end()) {
_plstModel->removeRow((*itFind)->row());
_mapLstWgtItem.erase(itFind);
_pTipLabel->setText(QString(tr("已选择了 %1 个联系人")).arg(_mapLstWgtItem.size()));
}
//
std::string id = memberId.toStdString();
if(!id.empty())
{
const auto &mapItems = _mapItems[id];
for(const auto& it : mapItems)
{
auto *tmpItem = it.second;
if(EM_STRUCTURE == it.first)
tmpItem->setData(false, EM_SEARCH_DATATYPE_CHECKSTATE);
else
tmpItem->setData(false, EM_DATATYPE_CHECKSTATE);
}
}
}
void UIGroupManager::resetUi(const QString &memberId) {
initRecentSessionData();
_pGroupNameEdit->setText("");
_pSearchWgt->clearText();
//
_plstModel->clear();
_mapLstWgtItem.clear();
_pTipLabel->setText(QString(tr("已选择了 %1 个联系人")).arg(_mapLstWgtItem.size()));
//
for(const auto& mapItem : _mapItems)
{
for(const auto& item : mapItem.second)
{
if(item.first == EM_RECENT)
continue;
auto *tmpItem = item.second;
if(item.first == EM_STRUCTURE)
tmpItem->setData(false, EM_SEARCH_DATATYPE_CHECKSTATE);
else
tmpItem->setData(false, EM_DATATYPE_CHECKSTATE);
}
}
//
std::string id = memberId.toStdString();
if(!id.empty() && _structure.find(id) != _structure.end())
{
auto info =_structure[id];
addGroupMember(memberId,
QTalk::GetHeadPathByUrl(info->HeaderSrc).data());
}
//
_pRecentItem->setData(true, EM_STAFF_DATATYPE_EXTEND);
_pFriendItem->setData(false, EM_STAFF_DATATYPE_EXTEND);
_pStarItem->setData(false, EM_STAFF_DATATYPE_EXTEND);
_pTreeWgt->setExpanded(_pRecentItem->index(), true);
_pTreeWgt->setExpanded(_pFriendItem->index(), false);
_pTreeWgt->setExpanded(_pStarItem->index(), false);
_pTreeWgt->scrollTo(_pTreeModel->index(0, 0), QAbstractItemView::PositionAtTop);
}
void UIGroupManager::sendAddGroupMemberMessage(const QString &groupId, const QList<QString> &members, bool showTip) {
if (nullptr == _pMsgManager)
return;
if(showTip && members.size() > 50)
{
int ret = QtMessageBox::question(this, tr("提示"), QString(tr("选择的群成员已超过50人,是否继续?")));
if(ret == QtMessageBox::EM_BUTTON_NO)
return;
}
std::vector<std::string> member;
for(const QString &id : members) {
member.push_back(id.toStdString());
}
_mapGroupMembers.remove(groupId);
_pMsgManager->addGroupMember(member, groupId.toStdString());
}
void UIGroupManager::creatGroup() {
if (nullptr == _pMsgManager)
return;
if(_mapLstWgtItem.size() > 50)
{
int ret = QtMessageBox::question(this, tr("提示"), QString(tr("选择的群成员已超过50人,是否继续?")));
if(ret == QtMessageBox::EM_BUTTON_NO)
{
return;
}
}
QString groupId = QString("%1@conference.%2").arg(QTalk::utils::getMessageId().data()).arg(Platform::instance().getSelfDomain().c_str());
_mapGroupMembers[groupId] = _mapLstWgtItem.keys();
QString groupName = _pGroupNameEdit->text();
if(groupName.isEmpty())
{
auto members = _mapGroupMembers[groupId];
QString selfId = Platform::instance().getSelfXmppId().data();
if(!members.contains(selfId))
members.push_front(selfId);
QFontMetricsF nameF(_pGroupNameEdit->font());
for(const auto& mem : members)
{
groupName.append(_mapUserName[mem]);
if(nameF.width(groupName) > 200)
{
groupName.append("..");
break;
}
else
groupName.append(",");
}
if(groupName.right(1) == ",")
groupName.chop(1);
}
//
_pMsgManager->creatGroup(groupId.toStdString(), groupName.toStdString());
}
void UIGroupManager::onCreatGroupRet(bool ret, const std::string &groupId) {
QString gId = QString::fromStdString(groupId);
if (ret && _mapGroupMembers.contains(gId)) {
sendAddGroupMemberMessage(gId, _mapGroupMembers.value(gId), false);
}
}
/**
*
* @param ids
*/
void UIGroupManager::onBatchAddMember(const QString &ids)
{
QString tmpIds = ids;
tmpIds.replace("\n", "");
tmpIds.replace(" ", "");
QStringList lstItems = tmpIds.split(";");
QString domain = QString::fromStdString(Platform::instance().getSelfDomain());
std::vector<std::string> arMembers;
for(const auto& item : lstItems)
{
if(item.isEmpty()) continue;
QString id = item
.section("@", 0, 0)
.append("@")
.append(domain);
std::string t = id.toStdString();
arMembers.push_back(id.toStdString());
}
auto infos = dbPlatForm::instance().getGroupMemberInfo(arMembers);
for(const auto& info : infos)
{
QString icon = QString::fromStdString(info.headerSrc);
if(!icon.isEmpty())
{
icon = QString::fromStdString(QTalk::GetHeadPathByUrl(info.headerSrc));
}
if(icon.isEmpty() || !QFile::exists(icon) || QFileInfo(icon).isDir())
{
#ifdef _STARTALK
icon = ":/QTalk/image1/StarTalk_defaultHead.png";
#else
icon = ":/QTalk/image1/headPortrait.png";
#endif
}
addGroupMember(QString::fromStdString(info.xmppId), icon);
}
}
QStandardItem* UIGroupManager::creatGroupItem(QStandardItem* item, const std::string& groupId)
{
auto* subTitleItem = new QStandardItem;
subTitleItem->setData(EM_ROW_TYPE_SUBTITLE, EM_STAFF_DATATYPE_ROW_TYPE);
subTitleItem->setData(groupId.data(), EM_STAFF_DATATYPE_TEXT);
subTitleItem->setData(groupId.data(), EM_STAFF_DATATYPE_XMPPID);
subTitleItem->setData(false, EM_STAFF_DATATYPE_EXTEND);
if(_groupInfos.find(groupId) != _groupInfos.end())
{
const QTalk::Entity::ImGroupInfo& info = _groupInfos[groupId];
#ifdef _STARTALK
QString defaultGroupHead = ":/QTalk/image1/StarTalk_defaultGroup.png";
#else
QString defaultGroupHead = ":/QTalk/image1/defaultGroupHead.png";
#endif
QString iconPath = info.HeaderSrc.empty() ? defaultGroupHead :
QString::fromStdString(QTalk::GetHeadPathByUrl(info.HeaderSrc));
if (!QFile::exists(iconPath) || QFileInfo(iconPath).isDir())
{
iconPath = defaultGroupHead;
}
subTitleItem->setData(info.Name.data(), EM_STAFF_DATATYPE_TEXT);
subTitleItem->setData(iconPath, EM_STAFF_DATATYPE_ICONPATH);
}
item->appendRow(subTitleItem);
return subTitleItem;
}
QStandardItem* UIGroupManager::creatItem(QStandardItem *item, const std::string &xmppId) {
auto* mainItem = new QStandardItem;
mainItem->setData(EM_ROW_TYPE_ITEM, EM_STAFF_DATATYPE_ROW_TYPE);
mainItem->setData(QString(xmppId.data()).section("@", 0, 0), EM_STAFF_DATATYPE_TEXT);
mainItem->setData(xmppId.data(), EM_STAFF_DATATYPE_XMPPID);
mainItem->setData(false, EM_DATATYPE_CHECKSTATE);
std::shared_ptr<QTalk::Entity::ImUserInfo> userinfo;
if(_structure.find(xmppId) == _structure.end() || nullptr == _structure[xmppId])
{
// userinfo = std::make_shared<QTalk::Entity::ImUserInfo>();
// userinfo->XmppId = xmppId;
// if(_pMsgManager)
// _pMsgManager->getUserInfo(userinfo);
//
// if(userinfo)
// {
// _mapUserName[xmppId.data()] = QTalk::getUserName(userinfo).data();
// _structure[xmppId] = userinfo;
// }
}
else
userinfo = _structure[xmppId];
QString defaultPath;
#ifdef _STARTALK
defaultPath = ":/QTalk/image1/StarTalk_defaultHead.png";
#else
defaultPath = ":/QTalk/image1/headPortrait.png";
#endif
QString iconPath = defaultPath;
if(nullptr != userinfo)
{
iconPath = userinfo->HeaderSrc.empty() ? defaultPath :
QString::fromStdString(QTalk::GetHeadPathByUrl(userinfo->HeaderSrc));
if (!QFile::exists(iconPath) || QFileInfo(iconPath).isDir())
iconPath = defaultPath;
std::string name = _mapUserName[xmppId.data()].toStdString();
mainItem->setData(_mapUserName[xmppId.data()], EM_STAFF_DATATYPE_TEXT);
}
mainItem->setData(iconPath, EM_STAFF_DATATYPE_ICONPATH);
item->appendRow(mainItem);
return mainItem;
}
/**
*
*/
void UIGroupManager::initGroupMembers()
{
// title
auto* titleItem = new QStandardItem;
titleItem->setData(tr("从群组选择"), EM_STAFF_DATATYPE_TEXT);
titleItem->setData(":/GroupManager/image1/groupList.png", EM_STAFF_DATATYPE_ICONPATH);
titleItem->setData(EM_ROW_TYPE_TITLE, EM_STAFF_DATATYPE_ROW_TYPE);
titleItem->setData(false, EM_STAFF_DATATYPE_EXTEND);
_pTreeModel->appendRow(titleItem);
// init all group
for(const auto& group : _groupmemebers)
{
//
std::string groupId = group.first;
QStandardItem* groupItem = creatGroupItem(titleItem, groupId);
//
for(const auto& member : group.second)
{
_mapItems[member][EM_GROUPMEMEBER] = creatItem(groupItem, member);
}
}
}
void UIGroupManager::onRecvFriends(const std::vector<QTalk::Entity::IMFriendList> &friends)
{
std::lock_guard<QTalk::util::spin_mutex> lock(sm);
_friends = friends;
flags = (flags << 1);
if(EM_ALL == flags)
emit initDataFinish();
}
//
void UIGroupManager::initData()
{
auto func = [this]() {
#ifdef _MACOS
pthread_setname_np("UIGroupManager::getStructure");
#endif
if (_pMsgManager) {
{
std::lock_guard<QTalk::util::spin_mutex> lock(sm);
// 组织架构
std::vector<std::shared_ptr<QTalk::Entity::ImUserInfo>> structure;
_pMsgManager->getStructure(structure);
std::for_each(structure.begin(), structure.end(), [this](std::shared_ptr<QTalk::Entity::ImUserInfo> info) {
if(info)
{
_structure[info->XmppId] = info;
std::string name = QTalk::getUserNameNoMask(info);
_mapUserName[info->XmppId.data()] = name.data();
}
});
flags = (flags << 1);
// 所有群以及群成员
std::map<std::string, std::set<std::string>> mapGroupMembers;
_pMsgManager->getGroupMembers(mapGroupMembers);
if(!mapGroupMembers.empty())
_groupmemebers = mapGroupMembers;
flags = (flags << 1);
if(flags == EM_ALL )
emit initDataFinish();
}
}
};
//
std::thread t(func);
t.detach();
}
/**
*
* @param friends
*/
void UIGroupManager::onRecvGroupList(const std::vector<QTalk::Entity::ImGroupInfo>& groupInfos)
{
std::lock_guard<QTalk::util::spin_mutex> lock(sm);
for(const auto& info : groupInfos)
{
_groupInfos[info.GroupId] = info;
}
flags = (flags << 1);
if(EM_ALL == flags)
emit initDataFinish();
}
void UIGroupManager::updateUi()
{
//
// initGroupMembers();
//
initFriends();
QApplication::processEvents(QEventLoop::AllEvents, 100);
//
initStarUser();
QApplication::processEvents(QEventLoop::AllEvents, 100);
//
initRecentSession();
QApplication::processEvents(QEventLoop::AllEvents, 100);
//
initStructure();
}
void UIGroupManager::onUpdateSession(const std::vector<QTalk::StShareSession> & ss) {
std::set<std::string> recent = _arTopUsers;
for(const auto& s : ss)
{
if(s.chatType == QTalk::Enum::TwoPersonChat && recent.find(s.xmppId) == recent.end())
recent.insert(s.xmppId);
}
for(const auto& id : recent)
{
auto* item = creatItem(_pRecentItem, id);
_mapItems[id][EM_RECENT] = item;
recentItems.push_back(item);
}
}
void UIGroupManager::initFriends()
{
_pFriendItem = new QStandardItem;
_pFriendItem->setData(tr("从好友选择"), EM_STAFF_DATATYPE_TEXT);
_pFriendItem->setData(":/GroupManager/image1/friendList.png", EM_STAFF_DATATYPE_ICONPATH);
_pFriendItem->setData(EM_ROW_TYPE_TITLE, EM_STAFF_DATATYPE_ROW_TYPE);
_pFriendItem->setData(false, EM_STAFF_DATATYPE_EXTEND);
//
_pTreeModel->appendRow(_pFriendItem);
//
for(const auto& frie : _friends)
{
_mapItems[frie.XmppId][EM_FRIEND] = creatItem(_pFriendItem, frie.XmppId);
}
}
/**
* @函数名
* @功能描述
* @参数
* @author cc
* @date 2018/12/16
*/
void UIGroupManager::updateUserConfig(const std::vector<QTalk::Entity::ImConfig> &arConfigs) {
std::lock_guard<QTalk::util::spin_mutex> lock(sm);
_arStarContact.clear();
_arTopUsers.clear();
auto it = arConfigs.begin();
for (; it != arConfigs.end(); it++) {
std::string subKey = it->ConfigSubKey;
if (it->ConfigKey == "kStarContact") {
_arStarContact.insert(subKey);
}
else if(it->ConfigKey == "kStickJidDic")
{
QString xmppId = QString::fromStdString(it->ConfigSubKey).section("<>", 1, 1);
QJsonDocument jsonDocument = QJsonDocument::fromJson(it->ConfigValue.data());
if (!jsonDocument.isNull()) {
QJsonObject jsonObject = jsonDocument.object();
int type = jsonObject.value("chatType").toInt();
if(0 == type)
_arTopUsers.insert(xmppId.toStdString());
}
}
}
if(flags < EM_ALL)
{
flags = (flags << 1);
if(flags == EM_ALL)
emit initDataFinish();
}
else
{
// emit ;
}
}
void UIGroupManager::updateUserConfig(const std::map<std::string, std::string> &deleteData,
const std::vector<QTalk::Entity::ImConfig> &arImConfig) {
std::lock_guard<QTalk::util::spin_mutex> lock(sm);
for(const auto& it : deleteData)
{
if (it.second == "kStarContact") {
if(_arStarContact.find(it.first) != _arStarContact.end())
_arStarContact.erase(it.first);
}
else if(it.second == "kStickJidDic")
{
std::string xmppId = QString::fromStdString(it.first).section("<>", 1, 1).toStdString();
if(_arTopUsers.find(xmppId) != _arTopUsers.end())
_arTopUsers.erase(xmppId);
}
}
}
void UIGroupManager::initStarUser()
{
_pStarItem = new QStandardItem;
_pStarItem->setData(tr("从星标联系人选择"), EM_STAFF_DATATYPE_TEXT);
_pStarItem->setData(":/GroupManager/image1/star.png", EM_STAFF_DATATYPE_ICONPATH);
_pStarItem->setData(EM_ROW_TYPE_TITLE, EM_STAFF_DATATYPE_ROW_TYPE);
_pStarItem->setData(false, EM_STAFF_DATATYPE_EXTEND);
_pTreeModel->appendRow(_pStarItem);
//
for(const auto& star : _arStarContact)
{
_mapItems[star][EM_STAR] = creatItem(_pStarItem, star);
}
}
void UIGroupManager::initRecentSession()
{
_pRecentItem = new QStandardItem;
_pRecentItem->setData(tr("从最近联系人选择"), EM_STAFF_DATATYPE_TEXT);
_pRecentItem->setData(":/GroupManager/image1/Recent.png", EM_STAFF_DATATYPE_ICONPATH);
_pRecentItem->setData(EM_ROW_TYPE_TITLE, EM_STAFF_DATATYPE_ROW_TYPE);
_pRecentItem->setData(false, EM_STAFF_DATATYPE_EXTEND);
//
_pTreeModel->appendRow(_pRecentItem);
}
void UIGroupManager::initRecentSessionData()
{
if(_pRecentItem)
{
for(auto *item : recentItems)
{
// 移除缓冲数据
if(item)
{
std::string id = item->data(EM_STAFF_DATATYPE_XMPPID).toString().toStdString();
if(!id.empty() && _mapItems.find(id) != _mapItems.end())
{
_mapItems[id].erase(EM_RECENT);
}
_pTreeModel->removeRow(item->row(), _pRecentItem->index());
}
}
recentItems.clear();
//
// if(_pMsgManager)
{
auto result = QtConcurrent::run([this](){
std::vector<QTalk::StShareSession> ss;
GroupManagerMsgManager::getRecentSession(ss);
emit sgUpdateSession(ss);
});
}
}
}
/**
*
*/
void UIGroupManager::initStructure()
{
static int index = 0;
for(const auto& it : _structure)
{
if(++index == 10)
{
QApplication::processEvents(QEventLoop::AllEvents, 100);
index = 0;
}
//
const auto& info = it.second;
if(info)
{
std::string xmppId = info->XmppId;
auto* mainItem = new QStandardItem;
mainItem->setData(xmppId.data(), EM_SEARCH_DATATYPE_XMPPID);
mainItem->setData(QTalk::Entity::JID(xmppId).username().data(),
EM_SEARCH_DATATYPE_USERID);
mainItem->setData(false, EM_SEARCH_DATATYPE_CHECKSTATE);
mainItem->setData(info->SearchIndex.data(), EM_SEARCH_DATATYPE_INDEX);
QString defaultPath;
#ifdef _STARTALK
defaultPath = ":/QTalk/image1/StarTalk_defaultHead.png";
#else
defaultPath = ":/QTalk/image1/headPortrait.png";
#endif
QString iconPath = info->HeaderSrc.empty() ? defaultPath :
QString::fromStdString(QTalk::GetHeadPathByUrl(info->HeaderSrc));
if (!QFile::exists(iconPath) || QFileInfo(iconPath).isDir())
{
iconPath = defaultPath;
}
mainItem->setData(_mapUserName[xmppId.data()], EM_SEARCH_DATATYPE_TEXT);
mainItem->setData(iconPath, EM_SEARCH_DATATYPE_ICONPATH);
mainItem->setData(info->DescInfo.data(), EM_SEARCH_DATATYPE_STAFF);
mainItem->setData(info->DescInfo.data(), Qt::ToolTipRole);
_mapItems[xmppId][EM_STRUCTURE] = mainItem;
_pSearchSrcModel->appendRow(mainItem);
}
}
}
/**
*
* @param memberId
*/
void UIGroupManager::onItemDoubleClick(const QString& memberId, const QString& name)
{
if(EM_TYPE_ADDGROUPMEMBER == _type)
{
int ret =QtMessageBox::question(this, tr("提示"), QString(tr("确认仅邀请<b>%1</b>进群?")).arg(name));
if(ret == QtMessageBox::EM_BUTTON_YES)
{
_mapLstWgtItem.clear();
sendAddGroupMemberMessage(_strGroupId, QList<QString>() << memberId);
this->setVisible(false);
}
}
}
void UIGroupManager::gotIncrementUser(const std::vector<QTalk::Entity::ImUserInfo> &arUserInfo,
const std::vector<std::string> &arDeletes)
{
std::lock_guard<QTalk::util::spin_mutex> lock(sm);
// delete
for(const auto& id : arDeletes)
{
if(_structure.find(id) != _structure.end())
_structure.erase(id);
//
if(_mapItems.find(id) != _mapItems.end() && _mapItems[id].find(EM_STRUCTURE) != _mapItems[id].end())
{
auto* item = _mapItems[id][EM_STRUCTURE];
_pSearchSrcModel->removeRow(item->row());
//
_mapItems[id].erase(EM_STRUCTURE);
if(_mapItems[id].empty())
_mapItems.erase(id);
}
}
// update -> delete first
for(const auto& it : arUserInfo)
{
std::string id = it.XmppId;
if(_structure.find(id) != _structure.end())
_structure.erase(id);
//
if(_mapItems.find(id) != _mapItems.end() && _mapItems[id].find(EM_STRUCTURE) != _mapItems[id].end())
{
auto* item = _mapItems[id][EM_STRUCTURE];
_pSearchSrcModel->removeRow(item->row());
//
_mapItems[id].erase(EM_STRUCTURE);
if(_mapItems[id].empty())
_mapItems.erase(id);
}
}
// update -> add
for(const auto& it : arUserInfo)
{
std::string xmppId = it.XmppId;
_structure[xmppId] = std::make_shared<QTalk::Entity::ImUserInfo>(it);
std::string name = QTalk::getUserNameNoMask(_structure[xmppId]);
_mapUserName[xmppId.data()] = name.data();
auto* mainItem = new QStandardItem;
mainItem->setData(xmppId.data(), EM_SEARCH_DATATYPE_XMPPID);
mainItem->setData(QTalk::Entity::JID(xmppId).username().data(),
EM_SEARCH_DATATYPE_USERID);
mainItem->setData(false, EM_SEARCH_DATATYPE_CHECKSTATE);
mainItem->setData(it.SearchIndex.data(), EM_SEARCH_DATATYPE_INDEX);
QString defaultPath;
#ifdef _STARTALK
defaultPath = ":/QTalk/image1/StarTalk_defaultHead.png";
#else
defaultPath = ":/QTalk/image1/headPortrait.png";
#endif
QString iconPath = it.HeaderSrc.empty() ? defaultPath :
QString::fromStdString(QTalk::GetHeadPathByUrl(it.HeaderSrc));
if (!QFile::exists(iconPath) || QFileInfo(iconPath).isDir())
{
iconPath = defaultPath;
}
mainItem->setData(_mapUserName[xmppId.data()], EM_SEARCH_DATATYPE_TEXT);
mainItem->setData(iconPath, EM_SEARCH_DATATYPE_ICONPATH);
mainItem->setData(it.DescInfo.data(), EM_SEARCH_DATATYPE_STAFF);
mainItem->setData(it.DescInfo.data(), Qt::ToolTipRole);
_mapItems[xmppId][EM_STRUCTURE] = mainItem;
_pSearchSrcModel->appendRow(mainItem);
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化