Skip to content
This repository has been archived by the owner on Feb 12, 2023. It is now read-only.

Commit

Permalink
fix(group): Add second signal for titile changed
Browse files Browse the repository at this point in the history
Fix #4800.

In old implementation after Core title change notification, Group
updates self name and emit signal, core as subscribed on this signal and try to
change title twice.

In new implementation was added new signal to Group:
  1. To notify core about user changes.
  2. To notify views about core changes.
  • Loading branch information
Diadlo committed Nov 15, 2017
1 parent 07ceb15 commit 2f9ba3c
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 26 deletions.
5 changes: 3 additions & 2 deletions src/grouplist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@

QHash<int, Group*> GroupList::groupList;

Group* GroupList::addGroup(int groupId, const QString& name, bool isAvGroupchat)
Group* GroupList::addGroup(int groupId, const QString& name, bool isAvGroupchat,
const QString& selfName)
{
auto checker = groupList.find(groupId);
if (checker != groupList.end())
qWarning() << "addGroup: groupId already taken";

Group* newGroup = new Group(groupId, name, isAvGroupchat);
Group* newGroup = new Group(groupId, name, isAvGroupchat, selfName);
groupList[groupId] = newGroup;

return newGroup;
Expand Down
2 changes: 1 addition & 1 deletion src/grouplist.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class QString;
class GroupList
{
public:
static Group* addGroup(int groupId, const QString& name, bool isAvGroupchat);
static Group* addGroup(int groupId, const QString& name, bool isAvGroupchat, const QString& selfName);
static Group* findGroup(int groupId);
static void removeGroup(int groupId, bool fake = false);
static QList<Group*> getAllGroups();
Expand Down
25 changes: 20 additions & 5 deletions src/model/group.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@

static const int MAX_GROUP_TITLE_LENGTH = 128;

Group::Group(int groupId, const QString& name, bool isAvGroupchat)
Group::Group(int groupId, const QString& name, bool isAvGroupchat, const QString& selfName)
: title{name}
, selfName{selfName}
, groupId(groupId)
, nPeers{0}
, avGroupchat{isAvGroupchat}
Expand Down Expand Up @@ -66,11 +67,20 @@ void Group::updatePeer(int peerId, QString name)
}
}

void Group::setName(const QString& name)
void Group::setName(const QString& newTitle)
{
if (!name.isEmpty() && title != name) {
title = name.left(MAX_GROUP_TITLE_LENGTH);
emit titleChanged(groupId, title);
if (!newTitle.isEmpty() && title != newTitle) {
title = newTitle.left(MAX_GROUP_TITLE_LENGTH);
emit titleChangedByUser(groupId, title);
emit titleChanged(groupId, selfName, title);
}
}

void Group::onTitleChanged(const QString& author, const QString& newTitle)
{
if (!newTitle.isEmpty() && title != newTitle) {
title = newTitle.left(MAX_GROUP_TITLE_LENGTH);
emit titleChanged(groupId, author, title);
}
}

Expand Down Expand Up @@ -172,3 +182,8 @@ QString Group::resolveToxId(const ToxPk& id) const

return QString();
}

void Group::setSelfName(const QString& name)
{
selfName = name;
}
10 changes: 7 additions & 3 deletions src/model/group.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class Group : public Contact
{
Q_OBJECT
public:
Group(int groupId, const QString& name, bool isAvGroupchat);
Group(int groupId, const QString& name, bool isAvGroupchat, const QString& selfName);
~Group() override;

bool isAvGroupchat() const;
Expand All @@ -54,17 +54,21 @@ class Group : public Contact
bool getMentionedFlag() const;

void updatePeer(int peerId, QString newName);
void setName(const QString& name) override;
void setName(const QString& newTitle) override;
void onTitleChanged(const QString& author, const QString& newTitle);
QString getName() const;
QString getDisplayedName() const override;

QString resolveToxId(const ToxPk& id) const;
void setSelfName(const QString& name);

signals:
void titleChanged(uint32_t groupId, const QString& title);
void titleChangedByUser(uint32_t groupId, const QString& title);
void titleChanged(uint32_t groupId, const QString& author, const QString& title);
void userListChanged(uint32_t groupId, const QMap<QByteArray, QString>& toxids);

private:
QString selfName;
QString title;
GroupChatForm* chatForm;
QStringList peers;
Expand Down
14 changes: 14 additions & 0 deletions src/widget/form/groupchatform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ GroupChatForm::GroupChatForm(Group* chatGroup)
chatGroup->setName(newName);
});
connect(group, &Group::userListChanged, this, &GroupChatForm::onUserListChanged);
connect(group, &Group::titleChanged, this, &GroupChatForm::onTitleChanged);

setAcceptDrops(true);
Translator::registerHandler(std::bind(&GroupChatForm::retranslateUi, this), this);
Expand Down Expand Up @@ -172,6 +173,19 @@ void GroupChatForm::onUserListChanged()
}
}

void GroupChatForm::onTitleChanged(uint32_t groupId, const QString& author, const QString& title)
{
Q_UNUSED(groupId);
setName(title);
if (author.isEmpty()) {
return;
}

const QString message = tr("%1 has set the title to %2").arg(author, title);
const QDateTime curTime = QDateTime::currentDateTime();
addSystemInfoMessage(message, ChatMessage::INFO, curTime);
}

/**
* @brief Updates user names' labels at the top of group chat
*/
Expand Down
1 change: 1 addition & 0 deletions src/widget/form/groupchatform.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ private slots:
void onVolMuteToggle();
void onCallClicked();
void onUserListChanged();
void onTitleChanged(uint32_t groupId, const QString& author, const QString& title);

protected:
virtual GenericNetCamView* createNetcam() final override;
Expand Down
4 changes: 3 additions & 1 deletion src/widget/friendlistwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,10 @@ void FriendListWidget::addGroupWidget(GroupWidget* widget)
{
groupLayout.addSortedWidget(widget);
Group* g = widget->getGroup();
connect(g, &Group::titleChanged, [=](uint32_t groupId, const QString& name) {
connect(g, &Group::titleChanged,
[=](uint32_t groupId, const QString& author, const QString& name) {
Q_UNUSED(groupId);
Q_UNUSED(author);
renameGroupWidget(widget, name);
});
}
Expand Down
3 changes: 2 additions & 1 deletion src/widget/groupwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,10 @@ void GroupWidget::setTitle(const QString& newName)
g->setName(newName);
}

void GroupWidget::updateTitle(uint32_t groupId, const QString& newName)
void GroupWidget::updateTitle(uint32_t groupId, const QString& author, const QString& newName)
{
Q_UNUSED(groupId);
Q_UNUSED(author);
nameLabel->setText(newName);
}

Expand Down
2 changes: 1 addition & 1 deletion src/widget/groupwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class GroupWidget final : public GenericChatroomWidget
private slots:
void retranslateUi();
void setTitle(const QString& newName);
void updateTitle(uint32_t groupId, const QString& newName);
void updateTitle(uint32_t groupId, const QString& author, const QString& newName);
void updateUserCount();

public:
Expand Down
16 changes: 4 additions & 12 deletions src/widget/widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1756,21 +1756,12 @@ void Widget::onGroupTitleChanged(int groupnumber, const QString& author, const Q
return;
}

if (!author.isEmpty()) {
QString message = tr("%1 has set the title to %2").arg(author, title);
QDateTime curTime = QDateTime::currentDateTime();
g->getChatForm()->addSystemInfoMessage(message, ChatMessage::INFO, curTime);
}

GroupWidget* widget = groupWidgets[groupnumber];
contactListWidget->renameGroupWidget(widget, title);
g->getChatForm()->setName(title);

if (widget->isActive()) {
GUI::setWindowTitle(title);
}

g->setName(title);
g->onTitleChanged(author, title);
FilterCriteria filter = getFilterCriteria();
widget->searchName(ui->searchContactText->text(), filterGroups(filter));
}
Expand Down Expand Up @@ -1843,7 +1834,7 @@ Group* Widget::createGroup(int groupId)
}

bool enabled = coreAv->isGroupAvEnabled(groupId);
Group* newgroup = GroupList::addGroup(groupId, groupName, enabled);
Group* newgroup = GroupList::addGroup(groupId, groupName, enabled, core->getUsername());
bool compact = Settings::getInstance().getCompactLayout();
GroupWidget* widget = new GroupWidget(groupId, groupName, compact);
groupWidgets[groupId] = widget;
Expand All @@ -1859,7 +1850,8 @@ Group* Widget::createGroup(int groupId)
connect(widget, &GroupWidget::chatroomWidgetClicked, form, &ChatForm::focusInput);
connect(form, &GroupChatForm::sendMessage, core, &Core::sendGroupMessage);
connect(form, &GroupChatForm::sendAction, core, &Core::sendGroupAction);
connect(newgroup, &Group::titleChanged, core, &Core::changeGroupTitle);
connect(newgroup, &Group::titleChangedByUser, core, &Core::changeGroupTitle);
connect(core, &Core::usernameSet, newgroup, &Group::setSelfName);

FilterCriteria filter = getFilterCriteria();
widget->searchName(ui->searchContactText->text(), filterGroups(filter));
Expand Down

0 comments on commit 2f9ba3c

Please sign in to comment.