From f59a67f08a03c1475f65bf69ef0dc32329f11372 Mon Sep 17 00:00:00 2001 From: apprb Date: Mon, 24 Nov 2014 00:22:29 +0900 Subject: [PATCH] Group class refactoring --- src/group.cpp | 62 +++++++++++++++++++++++++++++++ src/group.h | 28 +++++++++++--- src/widget/form/groupchatform.cpp | 40 ++++++++++---------- src/widget/form/tabcompleter.cpp | 2 +- src/widget/friendwidget.cpp | 4 +- src/widget/groupwidget.cpp | 20 +++++----- src/widget/widget.cpp | 50 +++++++++++++------------ 7 files changed, 144 insertions(+), 62 deletions(-) diff --git a/src/group.cpp b/src/group.cpp index 0dbd2ad6b..62eef3650 100644 --- a/src/group.cpp +++ b/src/group.cpp @@ -79,3 +79,65 @@ void Group::setName(const QString& name) if (widget->isActive()) Widget::getInstance()->setWindowTitle(name); } + +void Group::regeneratePeerList() +{ + QList peerLst = Core::getInstance()->getGroupPeerNames(groupId); + peers.clear(); + nPeers = peerLst.size(); + for (int i = 0; i < peerLst.size(); i++) + peers[i] = peerLst.at(i); + + widget->onUserListChanged(); + chatForm->onUserListChanged(); +} + +bool Group::isAvGroupchat() const +{ + return avGroupchat; +} + +int Group::getGroupId() const +{ + return groupId; +} + +int Group::getPeersCount() const +{ + return nPeers; +} + +GroupChatForm *Group::getChatForm() +{ + return chatForm; +} + +GroupWidget *Group::getGroupWidget() +{ + return widget; +} + +QStringList Group::getPeerList() const +{ + return peers.values(); +} + +void Group::setEventFlag(int f) +{ + hasNewMessages = f; +} + +int Group::getEventFlag() const +{ + return hasNewMessages; +} + +void Group::setMentionedFlag(int f) +{ + userWasMentioned = f; +} + +int Group::getMentionedFlag() const +{ + return userWasMentioned; +} diff --git a/src/group.h b/src/group.h index 86790b61c..6161f6e7e 100644 --- a/src/group.h +++ b/src/group.h @@ -31,20 +31,38 @@ class Group : public QObject Q_OBJECT public: Group(int GroupId, QString Name, bool IsAvGroupchat); - ~Group(); + virtual ~Group(); + + bool isAvGroupchat() const; + int getGroupId() const; + int getPeersCount() const; + void regeneratePeerList(); + QStringList getPeerList() const; + + GroupChatForm *getChatForm(); + GroupWidget *getGroupWidget(); + + void setEventFlag(int f); + int getEventFlag() const; + + void setMentionedFlag(int f); + int getMentionedFlag() const; + void addPeer(int peerId, QString name); void removePeer(int peerId); + void updatePeer(int peerId, QString newName); void setName(const QString& name); -public: - int groupId; - QMap peers; - int nPeers; +private: GroupWidget* widget; GroupChatForm* chatForm; + QMap peers; int hasNewMessages, userWasMentioned; + int groupId; + int nPeers; bool avGroupchat; + }; #endif // GROUP_H diff --git a/src/widget/form/groupchatform.cpp b/src/widget/form/groupchatform.cpp index ff0c66dbb..cbb41df6b 100644 --- a/src/widget/form/groupchatform.cpp +++ b/src/widget/form/groupchatform.cpp @@ -38,7 +38,7 @@ GroupChatForm::GroupChatForm(Group* chatGroup) tabber = new TabCompleter(msgEdit, group); fileButton->setEnabled(false); - if (group->avGroupchat) + if (group->isAvGroupchat()) { videoButton->setEnabled(false); videoButton->setObjectName("grey"); @@ -51,10 +51,10 @@ GroupChatForm::GroupChatForm(Group* chatGroup) micButton->setVisible(false); } - nameLabel->setText(group->widget->getName()); + nameLabel->setText(group->getGroupWidget()->getName()); nusersLabel->setFont(Style::getFont(Style::Medium)); - nusersLabel->setText(GroupChatForm::tr("%1 users in chat","Number of users in chat").arg(group->peers.size())); + nusersLabel->setText(GroupChatForm::tr("%1 users in chat","Number of users in chat").arg(group->getPeersCount())); nusersLabel->setObjectName("statusLabel"); avatar->setPixmap(QPixmap(":/img/group_dark.png"), Qt::transparent); @@ -62,7 +62,7 @@ GroupChatForm::GroupChatForm(Group* chatGroup) msgEdit->setObjectName("group"); namesListLayout = new FlowLayout(0,5,0); - QStringList names(group->peers.values()); + QStringList names(group->getPeerList()); for (const QString& name : names) namesListLayout->addWidget(new QLabel(name)); @@ -81,7 +81,7 @@ GroupChatForm::GroupChatForm(Group* chatGroup) connect(micButton, SIGNAL(clicked()), this, SLOT(onMicMuteToggle())); connect(volButton, SIGNAL(clicked()), this, SLOT(onVolMuteToggle())); connect(nameLabel, &CroppingLabel::textChanged, this, [=](QString text, QString orig) - {if (text != orig) emit groupTitleChanged(group->groupId, text.left(128));} ); + {if (text != orig) emit groupTitleChanged(group->getGroupId(), text.left(128));} ); setAcceptDrops(true); } @@ -97,15 +97,15 @@ void GroupChatForm::onSendTriggered() if (msg.startsWith("/me ")) { msg = msg.right(msg.length() - 4); - emit sendAction(group->groupId, msg); + emit sendAction(group->getGroupId(), msg); } else { - emit sendMessage(group->groupId, msg); + emit sendMessage(group->getGroupId(), msg); } } void GroupChatForm::onUserListChanged() { - nusersLabel->setText(tr("%1 users in chat").arg(group->nPeers)); + nusersLabel->setText(tr("%1 users in chat").arg(group->getPeersCount())); QLayoutItem *child; while ((child = namesListLayout->takeAt(0))) @@ -115,7 +115,7 @@ void GroupChatForm::onUserListChanged() delete child; } - QStringList names(group->peers.values()); + QStringList names(group->getPeerList()); unsigned nNames = names.size(); for (unsigned i=0; imimeData()->hasFormat("friend")) { int friendId = ev->mimeData()->data("friend").toInt(); - Core::getInstance()->groupInviteFriend(friendId, group->groupId); + Core::getInstance()->groupInviteFriend(friendId, group->getGroupId()); } } @@ -149,12 +149,12 @@ void GroupChatForm::onMicMuteToggle() { if (micButton->objectName() == "red") { - Core::getInstance()->enableGroupCallMic(group->groupId); + Core::getInstance()->enableGroupCallMic(group->getGroupId()); micButton->setObjectName("green"); } else { - Core::getInstance()->disableGroupCallMic(group->groupId); + Core::getInstance()->disableGroupCallMic(group->getGroupId()); micButton->setObjectName("red"); } @@ -168,12 +168,12 @@ void GroupChatForm::onVolMuteToggle() { if (volButton->objectName() == "red") { - Core::getInstance()->enableGroupCallVol(group->groupId); + Core::getInstance()->enableGroupCallVol(group->getGroupId()); volButton->setObjectName("green"); } else { - Core::getInstance()->disableGroupCallVol(group->groupId); + Core::getInstance()->disableGroupCallVol(group->getGroupId()); volButton->setObjectName("red"); } @@ -185,7 +185,7 @@ void GroupChatForm::onCallClicked() { if (!inCall) { - Core::getInstance()->joinGroupCall(group->groupId); + Core::getInstance()->joinGroupCall(group->getGroupId()); audioInputFlag = true; audioOutputFlag = true; callButton->setObjectName("red"); @@ -194,7 +194,7 @@ void GroupChatForm::onCallClicked() } else { - Core::getInstance()->leaveGroupCall(group->groupId); + Core::getInstance()->leaveGroupCall(group->getGroupId()); audioInputFlag = false; audioOutputFlag = false; micButton->setObjectName("green"); @@ -216,9 +216,9 @@ void GroupChatForm::keyPressEvent(QKeyEvent* ev) if (ev->key() == Qt::Key_P && inCall) { Core* core = Core::getInstance(); - if (!core->isGroupCallMicEnabled(group->groupId)) + if (!core->isGroupCallMicEnabled(group->getGroupId())) { - core->enableGroupCallMic(group->groupId); + core->enableGroupCallMic(group->getGroupId()); micButton->setObjectName("green"); micButton->style()->polish(micButton); Style::repolish(micButton); @@ -235,9 +235,9 @@ void GroupChatForm::keyReleaseEvent(QKeyEvent* ev) if (ev->key() == Qt::Key_P && inCall) { Core* core = Core::getInstance(); - if (core->isGroupCallMicEnabled(group->groupId)) + if (core->isGroupCallMicEnabled(group->getGroupId())) { - core->disableGroupCallMic(group->groupId); + core->disableGroupCallMic(group->getGroupId()); micButton->setObjectName("red"); micButton->style()->polish(micButton); Style::repolish(micButton); diff --git a/src/widget/form/tabcompleter.cpp b/src/widget/form/tabcompleter.cpp index fa4691e13..73d3ffeef 100644 --- a/src/widget/form/tabcompleter.cpp +++ b/src/widget/form/tabcompleter.cpp @@ -52,7 +52,7 @@ void TabCompleter::buildCompletionList() // that section is then used as the completion regex QRegExp regex(QString("^[-_\\[\\]{}|`^.\\\\]*").append(QRegExp::escape(tabAbbrev)), Qt::CaseInsensitive); - for(auto name : group->peers.values()) + for(auto name : group->getPeerList()) if (regex.indexIn(name) > -1) completionMap[name.toLower()] = name; diff --git a/src/widget/friendwidget.cpp b/src/widget/friendwidget.cpp index e06568078..7ceb47df0 100644 --- a/src/widget/friendwidget.cpp +++ b/src/widget/friendwidget.cpp @@ -58,7 +58,7 @@ void FriendWidget::contextMenuEvent(QContextMenuEvent * event) for (Group* group : GroupList::getAllGroups()) { - QAction* groupAction = inviteMenu->addAction(group->widget->getName()); + QAction* groupAction = inviteMenu->addAction(group->getGroupWidget()->getName()); groupActions[groupAction] = group; } @@ -115,7 +115,7 @@ void FriendWidget::contextMenuEvent(QContextMenuEvent * event) else if (groupActions.contains(selectedItem)) { Group* group = groupActions[selectedItem]; - Core::getInstance()->groupInviteFriend(friendId, group->groupId); + Core::getInstance()->groupInviteFriend(friendId, group->getGroupId()); } } } diff --git a/src/widget/groupwidget.cpp b/src/widget/groupwidget.cpp index 94c2d16c2..4d1b33628 100644 --- a/src/widget/groupwidget.cpp +++ b/src/widget/groupwidget.cpp @@ -40,7 +40,7 @@ GroupWidget::GroupWidget(int GroupId, QString Name) Group* g = GroupList::findGroup(groupId); if (g) - statusMessageLabel->setText(GroupWidget::tr("%1 users in chat").arg(g->peers.size())); + statusMessageLabel->setText(GroupWidget::tr("%1 users in chat").arg(g->getPeersCount())); else statusMessageLabel->setText(GroupWidget::tr("0 users in chat")); @@ -68,7 +68,7 @@ void GroupWidget::contextMenuEvent(QContextMenuEvent * event) nameLabel->fullText(), &ok); if (ok && alias != nameLabel->fullText()) - emit g->chatForm->groupTitleChanged(groupId, alias.left(128)); + emit g->getChatForm()->groupTitleChanged(groupId, alias.left(128)); } } } @@ -77,7 +77,7 @@ void GroupWidget::onUserListChanged() { Group* g = GroupList::findGroup(groupId); if (g) - statusMessageLabel->setText(tr("%1 users in chat").arg(g->nPeers)); + statusMessageLabel->setText(tr("%1 users in chat").arg(g->getPeersCount())); else statusMessageLabel->setText(tr("0 users in chat")); } @@ -98,13 +98,13 @@ void GroupWidget::updateStatusLight() { Group *g = GroupList::findGroup(groupId); - if (g->hasNewMessages == 0) + if (!g->getEventFlag()) { statusPic.setPixmap(QPixmap(":img/status/dot_online.png")); } else { - if (g->userWasMentioned == 0) + if (!g->getMentionedFlag()) statusPic.setPixmap(QPixmap(":img/status/dot_online_notification.png")); else statusPic.setPixmap(QPixmap(":img/status/dot_online_notification.png")); @@ -114,14 +114,14 @@ void GroupWidget::updateStatusLight() void GroupWidget::setChatForm(Ui::MainWindow &ui) { Group* g = GroupList::findGroup(groupId); - g->chatForm->show(ui); + g->getChatForm()->show(ui); } void GroupWidget::resetEventFlags() { Group* g = GroupList::findGroup(groupId); - g->hasNewMessages = 0; - g->userWasMentioned = 0; + g->setEventFlag(false); + g->setMentionedFlag(false); } void GroupWidget::dragEnterEvent(QDragEnterEvent *ev) @@ -143,7 +143,7 @@ void GroupWidget::keyPressEvent(QKeyEvent* ev) { Group* g = GroupList::findGroup(groupId); if (g) - g->chatForm->keyPressEvent(ev); + g->getChatForm()->keyPressEvent(ev); } @@ -151,7 +151,7 @@ void GroupWidget::keyReleaseEvent(QKeyEvent* ev) { Group* g = GroupList::findGroup(groupId); if (g) - g->chatForm->keyReleaseEvent(ev); + g->getChatForm()->keyReleaseEvent(ev); } void GroupWidget::setName(const QString& name) diff --git a/src/widget/widget.cpp b/src/widget/widget.cpp index cc3afb3ce..9c20e1839 100644 --- a/src/widget/widget.cpp +++ b/src/widget/widget.cpp @@ -920,19 +920,19 @@ void Widget::onGroupMessageReceived(int groupnumber, const QString& message, con bool targeted = (author != name) && message.contains(name, Qt::CaseInsensitive); if (targeted) - g->chatForm->addAlertMessage(author, message, QDateTime::currentDateTime()); + g->getChatForm()->addAlertMessage(author, message, QDateTime::currentDateTime()); else - g->chatForm->addMessage(author, message, isAction, QDateTime::currentDateTime()); + g->getChatForm()->addMessage(author, message, isAction, QDateTime::currentDateTime()); - if ((static_cast(g->widget) != activeChatroomWidget) || isMinimized() || !isActiveWindow()) + if ((static_cast(g->getGroupWidget()) != activeChatroomWidget) || isMinimized() || !isActiveWindow()) { - g->hasNewMessages = 1; + g->setEventFlag(true); if (targeted) { - newMessageAlert(g->widget); - g->userWasMentioned = 1; // useful for highlighting line or desktop notifications + newMessageAlert(g->getGroupWidget()); + g->setMentionedFlag(true); // useful for highlighting line or desktop notifications } - g->widget->updateStatusLight(); + g->getGroupWidget()->updateStatusLight(); } } @@ -951,14 +951,16 @@ void Widget::onGroupNamelistChanged(int groupnumber, int peernumber, uint8_t Cha { if (name.isEmpty()) name = tr("", "Placeholder when we don't know someone's name in a group chat"); - g->addPeer(peernumber,name); + // g->addPeer(peernumber,name); + g->regeneratePeerList(); //g->chatForm->addSystemInfoMessage(tr("%1 has joined the chat").arg(name), "green"); // we can't display these messages until irungentoo fixes peernumbers // https://github.com/irungentoo/toxcore/issues/1128 } else if (change == TOX_CHAT_CHANGE_PEER_DEL) { - g->removePeer(peernumber); + // g->removePeer(peernumber); + g->regeneratePeerList(); //g->chatForm->addSystemInfoMessage(tr("%1 has left the chat").arg(name), "silver"); } else if (change == TOX_CHAT_CHANGE_PEER_NAME) // core overwrites old name before telling us it changed... @@ -973,19 +975,19 @@ void Widget::onGroupTitleChanged(int groupnumber, const QString& author, const Q g->setName(title); if (!author.isEmpty()) - g->chatForm->addSystemInfoMessage(tr("%1 has set the title to %2").arg(author, title), "silver", QDateTime::currentDateTime()); + g->getChatForm()->addSystemInfoMessage(tr("%1 has set the title to %2").arg(author, title), "silver", QDateTime::currentDateTime()); } void Widget::removeGroup(Group* g, bool fake) { - g->widget->setAsInactiveChatroom(); - if (static_cast(g->widget) == activeChatroomWidget) + g->getGroupWidget()->setAsInactiveChatroom(); + if (static_cast(g->getGroupWidget()) == activeChatroomWidget) { activeChatroomWidget = nullptr; onAddClicked(); } - GroupList::removeGroup(g->groupId, fake); - core->removeGroup(g->groupId, fake); + GroupList::removeGroup(g->getGroupId(), fake); + core->removeGroup(g->getGroupId(), fake); delete g; if (ui->mainHead->layout()->isEmpty()) onAddClicked(); @@ -1016,15 +1018,15 @@ Group *Widget::createGroup(int groupId) QString groupName = QString("Groupchat #%1").arg(groupId); Group* newgroup = GroupList::addGroup(groupId, groupName, true); QLayout* layout = contactListWidget->getGroupLayout(); - layout->addWidget(newgroup->widget); - newgroup->widget->updateStatusLight(); + layout->addWidget(newgroup->getGroupWidget()); + newgroup->getGroupWidget()->updateStatusLight(); - connect(newgroup->widget, SIGNAL(chatroomWidgetClicked(GenericChatroomWidget*)), this, SLOT(onChatroomWidgetClicked(GenericChatroomWidget*))); - connect(newgroup->widget, SIGNAL(removeGroup(int)), this, SLOT(removeGroup(int))); - connect(newgroup->widget, SIGNAL(chatroomWidgetClicked(GenericChatroomWidget*)), newgroup->chatForm, SLOT(focusInput())); - connect(newgroup->chatForm, SIGNAL(sendMessage(int,QString)), core, SLOT(sendGroupMessage(int,QString))); - connect(newgroup->chatForm, SIGNAL(sendAction(int,QString)), core, SLOT(sendGroupAction(int,QString))); - connect(newgroup->chatForm, &GroupChatForm::groupTitleChanged, core, &Core::changeGroupTitle); + connect(newgroup->getGroupWidget(), SIGNAL(chatroomWidgetClicked(GenericChatroomWidget*)), this, SLOT(onChatroomWidgetClicked(GenericChatroomWidget*))); + connect(newgroup->getGroupWidget(), SIGNAL(removeGroup(int)), this, SLOT(removeGroup(int))); + connect(newgroup->getGroupWidget(), SIGNAL(chatroomWidgetClicked(GenericChatroomWidget*)), newgroup->getChatForm(), SLOT(focusInput())); + connect(newgroup->getChatForm(), SIGNAL(sendMessage(int,QString)), core, SLOT(sendGroupMessage(int,QString))); + connect(newgroup->getChatForm(), SIGNAL(sendAction(int,QString)), core, SLOT(sendGroupAction(int,QString))); + connect(newgroup->getChatForm(), &GroupChatForm::groupTitleChanged, core, &Core::changeGroupTitle); return newgroup; } @@ -1122,7 +1124,7 @@ void Widget::onGroupSendResult(int groupId, const QString& message, int result) return; if (result == -1) - g->chatForm->addSystemInfoMessage(tr("Message failed to send"), "red", QDateTime::currentDateTime()); + g->getChatForm()->addSystemInfoMessage(tr("Message failed to send"), "red", QDateTime::currentDateTime()); } void Widget::getPassword(QString info, int passtype, uint8_t* salt) @@ -1214,5 +1216,5 @@ void Widget::reloadTheme() f->getFriendWidget()->reloadTheme(); for (Group* g : GroupList::getAllGroups()) - g->widget->reloadTheme(); + g->getGroupWidget()->reloadTheme(); }