1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00
qTox/src/model/group.cpp

187 lines
4.3 KiB
C++

/*
Copyright © 2014-2017 by The qTox Project Contributors
This file is part of qTox, a Qt-based graphical interface for Tox.
qTox is libre software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
qTox is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with qTox. If not, see <http://www.gnu.org/licenses/>.
*/
#include "group.h"
#include "friend.h"
#include "src/friendlist.h"
#include "src/core/core.h"
#include "src/persistence/settings.h"
#include "src/widget/form/groupchatform.h"
#include "src/widget/groupwidget.h"
#include "src/widget/gui.h"
#include <QDebug>
#include <QTimer>
static const int MAX_GROUP_TITLE_LENGTH = 128;
Group::Group(int groupId, const QString& name, bool isAvGroupchat)
: title{name}
, groupId(groupId)
, nPeers{0}
, avGroupchat{isAvGroupchat}
{
bool compact = Settings::getInstance().getCompactLayout();
widget = new GroupWidget(groupId, name, compact);
chatForm = new GroupChatForm(this);
// in groupchats, we only notify on messages containing your name <-- dumb
// sound notifications should be on all messages, but system popup notification
// on naming is appropriate
hasNewMessages = 0;
userWasMentioned = 0;
}
Group::~Group()
{
delete chatForm;
widget->deleteLater();
}
void Group::updatePeer(int peerId, QString name)
{
ToxPk peerKey = Core::getInstance()->getGroupPeerPk(groupId, peerId);
QByteArray peerPk = peerKey.getKey();
peers[peerId] = name;
toxids[peerPk] = name;
Friend* f = FriendList::findFriend(peerKey);
if (f != nullptr && f->hasAlias()) {
peers[peerId] = f->getDisplayedName();
toxids[peerPk] = f->getDisplayedName();
} else {
widget->onUserListChanged();
chatForm->onUserListChanged();
emit userListChanged(groupId, toxids);
}
}
void Group::setName(const QString& name)
{
if (!name.isEmpty() && title != name) {
title = name.left(MAX_GROUP_TITLE_LENGTH);
emit titleChanged(groupId, title);
}
}
QString Group::getName() const
{
return title;
}
QString Group::getDisplayedName() const
{
return getName();
}
void Group::regeneratePeerList()
{
const Core* core = Core::getInstance();
peers = core->getGroupPeerNames(groupId);
toxids.clear();
nPeers = peers.size();
for (int i = 0; i < nPeers; ++i) {
ToxPk id = core->getGroupPeerPk(groupId, i);
ToxPk self = core->getSelfId().getPublicKey();
if (id == self)
selfPeerNum = i;
QByteArray peerPk = id.getKey();
toxids[peerPk] = peers[i];
if (toxids[peerPk].isEmpty())
toxids[peerPk] =
tr("<Empty>", "Placeholder when someone's name in a group chat is empty");
Friend* f = FriendList::findFriend(id);
if (f != nullptr && f->hasAlias()) {
peers[i] = f->getDisplayedName();
toxids[peerPk] = f->getDisplayedName();
}
}
widget->onUserListChanged();
chatForm->onUserListChanged();
emit userListChanged(groupId, toxids);
}
bool Group::isAvGroupchat() const
{
return avGroupchat;
}
uint32_t Group::getId() 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;
}
bool Group::isSelfPeerNumber(int num) const
{
return num == selfPeerNum;
}
void Group::setEventFlag(bool f)
{
hasNewMessages = f;
}
bool Group::getEventFlag() const
{
return hasNewMessages;
}
void Group::setMentionedFlag(bool f)
{
userWasMentioned = f;
}
bool Group::getMentionedFlag() const
{
return userWasMentioned;
}
QString Group::resolveToxId(const ToxPk& id) const
{
QByteArray key = id.getKey();
auto it = toxids.find(key);
if (it != toxids.end())
return *it;
return QString();
}