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

refactor(id): use toxPk and groupId instead of core numbers

Allows creating classes for blocked friends which have toxPks but no
core number.
This commit is contained in:
Anthony Bilinski 2019-04-16 00:40:14 -07:00
parent ec500b6673
commit f7603c294b
No known key found for this signature in database
GPG Key ID: 2AA8E0DA1B31FB3C
26 changed files with 337 additions and 390 deletions

View File

@ -28,9 +28,9 @@ protected:
QByteArray id;
};
inline uint qHash(const std::shared_ptr<const ContactId> id)
inline uint qHash(const ContactId& id)
{
return qHash(id->getByteArray());
return qHash(id.getByteArray());
}
using ContactIdPtr = std::shared_ptr<const ContactId>;

View File

@ -20,31 +20,33 @@
#include "friendlist.h"
#include "src/model/friend.h"
#include "src/persistence/settings.h"
#include "src/core/contactid.h"
#include "src/core/toxpk.h"
#include <QDebug>
#include <QHash>
#include <QMenu>
QHash<uint32_t, Friend*> FriendList::friendList;
QHash<QByteArray, uint32_t> FriendList::key2id;
QHash<ToxPk, Friend*> FriendList::friendList;
QHash<uint32_t, ToxPk> FriendList::id2key;
Friend* FriendList::addFriend(uint32_t friendId, const ToxPk& friendPk)
{
auto friendChecker = friendList.find(friendId);
auto friendChecker = friendList.find(friendPk);
if (friendChecker != friendList.end()) {
qWarning() << "addFriend: friendId already taken";
qWarning() << "addFriend: friendPk already taken";
}
QString alias = Settings::getInstance().getFriendAlias(friendPk);
Friend* newfriend = new Friend(friendId, friendPk, alias);
friendList[friendId] = newfriend;
key2id[friendPk.getByteArray()] = friendId;
friendList[friendPk] = newfriend;
id2key[friendId] = friendPk;
return newfriend;
}
Friend* FriendList::findFriend(uint32_t friendId)
Friend* FriendList::findFriend(const ToxPk& friendPk)
{
auto f_it = friendList.find(friendId);
auto f_it = friendList.find(friendPk);
if (f_it != friendList.end()) {
return *f_it;
}
@ -52,9 +54,14 @@ Friend* FriendList::findFriend(uint32_t friendId)
return nullptr;
}
void FriendList::removeFriend(uint32_t friendId, bool fake)
const ToxPk& FriendList::id2Key(uint32_t friendId)
{
auto f_it = friendList.find(friendId);
return id2key[friendId];
}
void FriendList::removeFriend(const ToxPk& friendPk, bool fake)
{
auto f_it = friendList.find(friendPk);
if (f_it != friendList.end()) {
if (!fake)
Settings::getInstance().removeFriendSettings(f_it.value()->getPublicKey());
@ -69,28 +76,14 @@ void FriendList::clear()
friendList.clear();
}
Friend* FriendList::findFriend(const ToxPk& friendPk)
{
auto id = key2id.find(friendPk.getByteArray());
if (id != key2id.end()) {
Friend* f = findFriend(*id);
if (!f)
return nullptr;
if (f->getPublicKey() == friendPk)
return f;
}
return nullptr;
}
QList<Friend*> FriendList::getAllFriends()
{
return friendList.values();
}
QString FriendList::decideNickname(ToxPk peerPk, const QString origName)
QString FriendList::decideNickname(const ToxPk& friendPk, const QString origName)
{
Friend* f = FriendList::findFriend(peerPk);
Friend* f = FriendList::findFriend(friendPk);
if (f != nullptr && f->hasAlias()) {
return f->getDisplayedName();
} else {

View File

@ -35,16 +35,16 @@ class FriendList
{
public:
static Friend* addFriend(uint32_t friendId, const ToxPk& friendPk);
static Friend* findFriend(uint32_t friendId);
static Friend* findFriend(const ToxPk& friendPk);
static const ToxPk& id2Key(uint32_t friendId);
static QList<Friend*> getAllFriends();
static void removeFriend(uint32_t friendId, bool fake = false);
static void removeFriend(const ToxPk& friendPk, bool fake = false);
static void clear();
static QString decideNickname(ToxPk peerPk, const QString origName);
static QString decideNickname(const ToxPk& friendPk, const QString origName);
private:
static QHash<uint32_t, Friend*> friendList;
static QHash<QByteArray, uint32_t> key2id;
static QHash<ToxPk, Friend*> friendList;
static QHash<uint32_t, ToxPk> id2key;
};
#endif // FRIENDLIST_H

View File

@ -22,22 +22,22 @@
#include <QDebug>
#include <QHash>
QHash<int, Group*> GroupList::groupList;
Group* GroupList::addGroup(int groupId, const GroupId& persistentGroupId, const QString& name, bool isAvGroupchat,
QHash<const GroupId, Group*> GroupList::groupList;
QHash<uint32_t, GroupId> GroupList::id2key;
Group* GroupList::addGroup(int groupNum, const GroupId& 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, persistentGroupId, name, isAvGroupchat, selfName);
Group* newGroup = new Group(groupNum, groupId, name, isAvGroupchat, selfName);
groupList[groupId] = newGroup;
id2key[groupNum] = groupId;
return newGroup;
}
Group* GroupList::findGroup(int groupId)
Group* GroupList::findGroup(const GroupId& groupId)
{
auto g_it = groupList.find(groupId);
if (g_it != groupList.end())
@ -46,7 +46,12 @@ Group* GroupList::findGroup(int groupId)
return nullptr;
}
void GroupList::removeGroup(int groupId, bool /*fake*/)
const GroupId& GroupList::id2Key(uint32_t groupNum)
{
return id2key[groupNum];
}
void GroupList::removeGroup(const GroupId& groupId, bool /*fake*/)
{
auto g_it = groupList.find(groupId);
if (g_it != groupList.end()) {

View File

@ -33,13 +33,15 @@ class GroupList
{
public:
static Group* addGroup(int groupId, const GroupId& persistentGroupId, const QString& name, bool isAvGroupchat, const QString& selfName);
static Group* findGroup(int groupId);
static void removeGroup(int groupId, bool fake = false);
static Group* findGroup(const GroupId& groupId);
static const GroupId& id2Key(uint32_t groupNum);
static void removeGroup(const GroupId& groupId, bool fake = false);
static QList<Group*> getAllGroups();
static void clear();
private:
static QHash<int, Group*> groupList;
static QHash<const GroupId, Group*> groupList;
static QHash<uint32_t, GroupId> id2key;
};
#endif // GROUPLIST_H

View File

@ -57,7 +57,7 @@ void Friend::setName(const QString& _name)
}
if (userName != name) {
userName = name;
emit nameChanged(friendId, name);
emit nameChanged(friendPk, name);
}
const auto newDisplayed = getDisplayedName();
@ -74,7 +74,7 @@ void Friend::setAlias(const QString& alias)
if (userAlias == alias) {
return;
}
emit aliasChanged(friendId, alias);
emit aliasChanged(friendPk, alias);
// save old displayed name to be able to compare for changes
const auto oldDisplayed = getDisplayedName();
@ -98,7 +98,7 @@ void Friend::setStatusMessage(const QString& message)
{
if (statusMessage != message) {
statusMessage = message;
emit statusMessageChanged(friendId, message);
emit statusMessageChanged(friendPk, message);
}
}
@ -156,7 +156,7 @@ void Friend::setStatus(Status s)
{
if (friendStatus != s) {
friendStatus = s;
emit statusChanged(friendId, friendStatus);
emit statusChanged(friendPk, friendStatus);
}
}

View File

@ -55,10 +55,10 @@ public:
bool isOnline() const;
signals:
void nameChanged(uint32_t friendId, const QString& name);
void aliasChanged(uint32_t friendId, QString alias);
void statusChanged(uint32_t friendId, Status status);
void statusMessageChanged(uint32_t friendId, const QString& message);
void nameChanged(const ToxPk& friendId, const QString& name);
void aliasChanged(const ToxPk& friendId, QString alias);
void statusChanged(const ToxPk& friendId, Status status);
void statusMessageChanged(const ToxPk& friendId, const QString& message);
void loadChatHistory();
public slots:

View File

@ -52,7 +52,7 @@ void Group::updatePeer(int peerId, QString name)
ToxPk peerKey = Core::getInstance()->getGroupPeerPk(groupId, peerId);
toxpks[peerKey] = name;
qDebug() << "name change: " + name;
emit userListChanged(groupId, toxpks);
emit userListChanged(persistentGroupId, toxpks);
}
void Group::setName(const QString& newTitle)
@ -61,8 +61,8 @@ void Group::setName(const QString& newTitle)
if (!shortTitle.isEmpty() && title != shortTitle) {
title = shortTitle;
emit displayedNameChanged(title);
emit titleChangedByUser(groupId, title);
emit titleChanged(groupId, selfName, title);
emit titleChangedByUser(persistentGroupId, title);
emit titleChanged(persistentGroupId, selfName, title);
}
}
@ -72,7 +72,7 @@ void Group::setTitle(const QString& author, const QString& newTitle)
if (!shortTitle.isEmpty() && title != shortTitle) {
title = shortTitle;
emit displayedNameChanged(title);
emit titleChanged(groupId, author, title);
emit titleChanged(persistentGroupId, author, title);
}
}
@ -114,7 +114,7 @@ void Group::regeneratePeerList()
if (avGroupchat) {
stopAudioOfDepartedPeers(oldPeers, toxpks);
}
emit userListChanged(groupId, toxpks);
emit userListChanged(persistentGroupId, toxpks);
}
bool Group::peerHasNickname(ToxPk pk)
@ -125,7 +125,7 @@ bool Group::peerHasNickname(ToxPk pk)
void Group::updateUsername(ToxPk pk, const QString newName)
{
toxpks[pk] = newName;
emit userListChanged(groupId, toxpks);
emit userListChanged(persistentGroupId, toxpks);
}
bool Group::isAvGroupchat() const
@ -138,7 +138,7 @@ uint32_t Group::getId() const
return groupId;
}
const ContactId& Group::getPersistentId() const
const GroupId& Group::getPersistentId() const
{
return persistentGroupId;
}

View File

@ -37,7 +37,7 @@ public:
Group(int groupId, const GroupId persistentGroupId, const QString& name, bool isAvGroupchat, const QString& selfName);
bool isAvGroupchat() const;
uint32_t getId() const override;
const ContactId& getPersistentId() const override;
const GroupId& getPersistentId() const override;
int getPeersCount() const;
void regeneratePeerList();
const QMap<ToxPk, QString>& getPeerList() const;
@ -60,9 +60,9 @@ public:
QString getSelfName() const;
signals:
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<ToxPk, QString>& toxpks);
void titleChangedByUser(const GroupId& groupId, const QString& title);
void titleChanged(const GroupId& groupId, const QString& author, const QString& title);
void userListChanged(const GroupId& groupId, const QMap<ToxPk, QString>& toxpks);
private:
void stopAudioOfDepartedPeers(const QList<ToxPk>& oldPks, const QMap<ToxPk, QString>& newPks);

View File

@ -31,10 +31,10 @@
#include <QFrame>
#include <QLabel>
NetCamView::NetCamView(int friendId, QWidget* parent)
NetCamView::NetCamView(ToxPk friendPk, QWidget* parent)
: GenericNetCamView(parent)
, selfFrame{nullptr}
, friendPk{FriendList::findFriend(friendId)->getPublicKey()}
, friendPk{friendPk}
, e(false)
{
videoSurface = new VideoSurface(Nexus::getProfile()->loadAvatar(friendPk), this);

View File

@ -35,7 +35,7 @@ class NetCamView : public GenericNetCamView
Q_OBJECT
public:
NetCamView(int friendId, QWidget* parent = nullptr);
NetCamView(ToxPk friendPk, QWidget* parent = nullptr);
~NetCamView();
virtual void show(VideoSource* source, const QString& title);

View File

@ -150,11 +150,11 @@ FriendWidget* ContentDialog::addFriend(std::shared_ptr<FriendChatroom> chatroom,
{
const auto compact = Settings::getInstance().getCompactLayout();
auto frnd = chatroom->getFriend();
auto friendId = frnd->getId();
auto friendPk = frnd->getPublicKey();
auto friendWidget = new FriendWidget(chatroom, compact);
friendWidgets[friendId] = friendWidget;
contactWidgets[friendPk] = friendWidget;
friendLayout->addFriendWidget(friendWidget, frnd->getStatus());
friendChatForms[friendId] = form;
contactChatForms[friendPk] = form;
// TODO(sudden6): move this connection to the Friend::displayedNameChanged signal
connect(frnd, &Friend::aliasChanged, this, &ContentDialog::updateFriendWidget);
@ -170,12 +170,12 @@ FriendWidget* ContentDialog::addFriend(std::shared_ptr<FriendChatroom> chatroom,
GroupWidget* ContentDialog::addGroup(std::shared_ptr<GroupChatroom> chatroom, GenericChatForm* form)
{
const auto g = chatroom->getGroup();
const auto groupId = g->getId();
const auto groupId = g->getPersistentId();
const auto compact = Settings::getInstance().getCompactLayout();
auto groupWidget = new GroupWidget(chatroom, compact);
groupWidgets[groupId] = groupWidget;
contactWidgets[groupId] = groupWidget;
groupLayout.addSortedWidget(groupWidget);
groupChatForms[groupId] = form;
contactChatForms[groupId] = form;
connect(groupWidget, &GroupWidget::chatroomWidgetClicked, this, &ContentDialog::activate);
@ -185,9 +185,9 @@ GroupWidget* ContentDialog::addGroup(std::shared_ptr<GroupChatroom> chatroom, Ge
return groupWidget;
}
void ContentDialog::removeFriend(int friendId)
void ContentDialog::removeFriend(const ToxPk& friendPk)
{
auto chatroomWidget = qobject_cast<FriendWidget*>(friendWidgets[friendId]);
auto chatroomWidget = qobject_cast<FriendWidget*>(contactWidgets[friendPk]);
disconnect(chatroomWidget->getFriend(), &Friend::aliasChanged, this,
&ContentDialog::updateFriendWidget);
@ -209,14 +209,14 @@ void ContentDialog::removeFriend(int friendId)
update();
}
friendWidgets.remove(friendId);
friendChatForms.remove(friendId);
contactWidgets.remove(friendPk);
contactChatForms.remove(friendPk);
closeIfEmpty();
}
void ContentDialog::removeGroup(int groupId)
void ContentDialog::removeGroup(const GroupId& groupId)
{
auto chatroomWidget = qobject_cast<GroupWidget*>(groupWidgets[groupId]);
auto chatroomWidget = qobject_cast<GroupWidget*>(contactWidgets[groupId]);
// Need to find replacement to show here instead.
if (activeChatroomWidget == chatroomWidget) {
cycleContacts(true, false);
@ -233,14 +233,14 @@ void ContentDialog::removeGroup(int groupId)
update();
}
groupWidgets.remove(groupId);
groupChatForms.remove(groupId);
contactWidgets.remove(groupId);
contactChatForms.remove(groupId);
closeIfEmpty();
}
void ContentDialog::closeIfEmpty()
{
if (friendWidgets.isEmpty() && groupWidgets.isEmpty()) {
if (contactWidgets.isEmpty()) {
close();
}
}
@ -464,10 +464,10 @@ void ContentDialog::dragEnterEvent(QDragEnterEvent* event)
return;
}
int friendId = contact->getId();
ToxPk friendId = contact->getPublicKey();
// If friend is already in a dialog then you can't drop friend where it already is.
if (!hasFriendWidget(friendId)) {
if (!hasContactWidget(friendId)) {
event->acceptProposedAction();
}
} else if (group) {
@ -476,13 +476,13 @@ void ContentDialog::dragEnterEvent(QDragEnterEvent* event)
return;
}
int groupId = event->mimeData()->data("groupId").toInt();
GroupId groupId = GroupId{event->mimeData()->data("groupId")};
Group* contact = GroupList::findGroup(groupId);
if (!contact) {
return;
}
if (!hasGroupWidget(groupId)) {
if (!hasContactWidget(groupId)) {
event->acceptProposedAction();
}
}
@ -548,17 +548,12 @@ void ContentDialog::keyPressEvent(QKeyEvent* event)
}
}
void ContentDialog::focusFriend(int friendId)
void ContentDialog::focusContact(const ContactId& contactId)
{
focusCommon(friendId, friendWidgets);
focusCommon(contactId, contactWidgets);
}
void ContentDialog::focusGroup(int groupId)
{
focusCommon(groupId, groupWidgets);
}
void ContentDialog::focusCommon(int id, QHash<int, GenericChatroomWidget*> list)
void ContentDialog::focusCommon(const ContactId& id, QHash<const ContactId&, GenericChatroomWidget*> list)
{
auto it = list.find(id);
if (it == list.end()) {
@ -586,15 +581,8 @@ void ContentDialog::activate(GenericChatroomWidget* widget)
}
activeChatroomWidget = widget;
const FriendWidget* const friendWidget = qobject_cast<FriendWidget*>(widget);
if (friendWidget) {
auto friendId = friendWidget->getFriend()->getId();
friendChatForms[friendId]->show(contentLayout);
} else {
auto groupId = widget->getGroup()->getId();
groupChatForms[groupId]->show(contentLayout);
}
const Contact* contact = widget->getContact();
contactChatForms[contact->getPersistentId()]->show(contentLayout);
widget->setAsActiveChatroom();
widget->resetEventFlags();
@ -602,51 +590,28 @@ void ContentDialog::activate(GenericChatroomWidget* widget)
updateTitleAndStatusIcon();
}
bool ContentDialog::containsFriend(int friendId) const
bool ContentDialog::containsContact(const ContactId& contactId) const
{
return friendWidgets.contains(friendId);
return contactWidgets.contains(contactId);
}
bool ContentDialog::containsGroup(int groupId) const
void ContentDialog::updateFriendStatus(const ToxPk& friendPk, Status status)
{
return groupWidgets.contains(groupId);
}
void ContentDialog::updateFriendStatus(int friendId, Status status)
{
auto widget = qobject_cast<FriendWidget*>(friendWidgets.value(friendId));
auto widget = qobject_cast<FriendWidget*>(contactWidgets.value(friendPk));
addFriendWidget(widget, status);
}
void ContentDialog::updateFriendStatusLight(int friendId)
void ContentDialog::updateContactStatusLight(const ContactId& contactId)
{
auto widget = friendWidgets.value(friendId);
auto widget = contactWidgets.value(contactId);
if (widget != nullptr) {
widget->updateStatusLight();
}
}
void ContentDialog::updateGroupStatusLight(int groupId)
bool ContentDialog::isContactWidgetActive(const ContactId& contactId)
{
auto widget = groupWidgets.value(groupId);
if (widget != nullptr) {
widget->updateStatusLight();
}
}
bool ContentDialog::isFriendWidgetActive(int friendId)
{
auto widget = friendWidgets.value(friendId);
if (widget == nullptr) {
return false;
}
return widget->isActive();
}
bool ContentDialog::isGroupWidgetActive(int groupId)
{
auto widget = friendWidgets.value(groupId);
auto widget = contactWidgets.value(contactId);
if (widget == nullptr) {
return false;
}
@ -655,9 +620,9 @@ bool ContentDialog::isGroupWidgetActive(int groupId)
}
// TODO: Connect to widget directly
void ContentDialog::setStatusMessage(int friendId, const QString& message)
void ContentDialog::setStatusMessage(const ToxPk& friendPk, const QString& message)
{
auto widget = friendWidgets.value(friendId);
auto widget = contactWidgets.value(friendPk);
if (widget != nullptr) {
widget->setStatusMsg(message);
}
@ -668,10 +633,10 @@ void ContentDialog::setStatusMessage(int friendId, const QString& message)
* @param friendId Friend Id.
* @param alias Alias to display on widget.
*/
void ContentDialog::updateFriendWidget(uint32_t friendId, QString alias)
void ContentDialog::updateFriendWidget(const ToxPk& friendPk, QString alias)
{
Friend* f = FriendList::findFriend(friendId);
FriendWidget* friendWidget = qobject_cast<FriendWidget*>(friendWidgets[friendId]);
Friend* f = FriendList::findFriend(friendPk);
FriendWidget* friendWidget = qobject_cast<FriendWidget*>(contactWidgets[friendPk]);
Status status = f->getStatus();
friendLayout->addFriendWidget(friendWidget, status);
@ -713,14 +678,9 @@ void ContentDialog::saveSplitterState()
Settings::getInstance().setDialogSplitterState(splitter->saveState());
}
bool ContentDialog::hasFriendWidget(int friendId) const
bool ContentDialog::hasContactWidget(const ContactId& contactId) const
{
return friendWidgets.contains(friendId);
}
bool ContentDialog::hasGroupWidget(int groupId) const
{
return groupWidgets.contains(groupId);
return contactWidgets.contains(contactId);
}
/**

View File

@ -58,8 +58,8 @@ public:
FriendWidget* addFriend(std::shared_ptr<FriendChatroom> chatroom, GenericChatForm* form);
GroupWidget* addGroup(std::shared_ptr<GroupChatroom> chatroom, GenericChatForm* form);
void removeFriend(int friendId);
void removeGroup(int groupId);
void removeFriend(const ToxPk& friendPk);
void removeGroup(const GroupId& groupId);
int chatroomWidgetCount() const;
void ensureSplitterVisible();
void updateTitleAndStatusIcon();
@ -71,23 +71,14 @@ public:
void addFriendWidget(FriendWidget* widget, Status status);
bool isActiveWidget(GenericChatroomWidget* widget);
bool hasFriendWidget(int friendId) const;
bool hasGroupWidget(int groupId) const;
bool hasContactWidget(const ContactId& contactId) const;
void focusContact(const ContactId& friendPk);
bool containsContact(const ContactId& friendPk) const;
void updateFriendStatus(const ToxPk& friendPk, Status status);
void updateContactStatusLight(const ContactId& contactId);
bool isContactWidgetActive(const ContactId& contactId);
void focusFriend(int friendId);
void focusGroup(int groupId);
bool containsFriend(int friendId) const;
bool containsGroup(int groupId) const;
void updateFriendStatus(int friendId, Status status);
void updateFriendStatusLight(int friendId);
void updateGroupStatusLight(int groupId);
bool isFriendWidgetActive(int friendId);
bool isGroupWidgetActive(int groupId);
void setStatusMessage(int friendId, const QString& message);
void setStatusMessage(const ToxPk& friendPk, const QString& message);
signals:
void friendDialogShown(const Friend* f);
@ -114,7 +105,7 @@ public slots:
void activate(GenericChatroomWidget* widget);
private slots:
void updateFriendWidget(uint32_t friendId, QString alias);
void updateFriendWidget(const ToxPk& friendPk, QString alias);
void onGroupchatPositionChanged(bool top);
private:
@ -126,7 +117,7 @@ private:
void saveSplitterState();
QLayout* nextLayout(QLayout* layout, bool forward) const;
int getCurrentLayout(QLayout*& layout);
void focusCommon(int id, QHash<int, GenericChatroomWidget*> list);
void focusCommon(const ContactId& id, QHash<const ContactId&, GenericChatroomWidget*> list);
private:
@ -139,11 +130,8 @@ private:
QSize videoSurfaceSize;
int videoCount;
QHash<int, GenericChatroomWidget*> friendWidgets;
QHash<int, GenericChatroomWidget*> groupWidgets;
QHash<int, GenericChatForm*> friendChatForms;
QHash<int, GenericChatForm*> groupChatForms;
QHash<const ContactId&, GenericChatroomWidget*> contactWidgets;
QHash<const ContactId&, GenericChatForm*> contactChatForms;
QString username;
};

View File

@ -11,7 +11,7 @@
namespace
{
void removeDialog(ContentDialog* dialog, QHash<int, ContentDialog*>& dialogs)
void removeDialog(ContentDialog* dialog, QHash<const ContactId&, ContentDialog*>& dialogs)
{
for (auto it = dialogs.begin(); it != dialogs.end();) {
if (*it == dialog) {
@ -30,38 +30,28 @@ ContentDialog* ContentDialogManager::current()
return currentDialog;
}
bool ContentDialogManager::friendWidgetExists(int friendId)
bool ContentDialogManager::contactWidgetExists(const ContactId& contactId)
{
const auto dialog = friendDialogs.value(friendId, nullptr);
const auto dialog = contactDialogs.value(contactId, nullptr);
if (dialog == nullptr) {
return false;
}
return dialog->containsFriend(friendId);
}
bool ContentDialogManager::groupWidgetExists(int groupId)
{
const auto dialog = groupDialogs.value(groupId, nullptr);
if (dialog == nullptr) {
return false;
}
return dialog->containsGroup(groupId);
return dialog->containsContact(contactId);
}
FriendWidget* ContentDialogManager::addFriendToDialog(ContentDialog* dialog,
std::shared_ptr<FriendChatroom> chatroom, GenericChatForm* form)
{
auto friendWidget = dialog->addFriend(chatroom, form);
auto friendId = friendWidget->getFriend()->getId();
const auto friendPk = friendWidget->getFriend()->getPublicKey();
ContentDialog* lastDialog = getFriendDialog(friendId);
ContentDialog* lastDialog = getFriendDialog(friendPk);
if (lastDialog) {
lastDialog->removeFriend(friendId);
lastDialog->removeFriend(friendPk);
}
friendDialogs[friendId] = dialog;
contactDialogs[friendPk] = dialog;
return friendWidget;
}
@ -69,30 +59,22 @@ GroupWidget* ContentDialogManager::addGroupToDialog(ContentDialog* dialog,
std::shared_ptr<GroupChatroom> chatroom, GenericChatForm* form)
{
auto groupWidget = dialog->addGroup(chatroom, form);
auto groupId = groupWidget->getGroup()->getId();
const auto& groupId = groupWidget->getGroup()->getPersistentId();
ContentDialog* lastDialog = getGroupDialog(groupId);
if (lastDialog) {
lastDialog->removeGroup(groupId);
}
groupDialogs[groupId] = dialog;
contactDialogs[groupId] = dialog;
return groupWidget;
}
void ContentDialogManager::focusFriend(int friendId)
void ContentDialogManager::focusContact(const ContactId& contactId)
{
auto dialog = focusDialog(friendId, friendDialogs);
auto dialog = focusDialog(contactId, contactDialogs);
if (dialog != nullptr) {
dialog->focusFriend(friendId);
}
}
void ContentDialogManager::focusGroup(int groupId)
{
auto dialog = focusDialog(groupId, groupDialogs);
if (dialog != nullptr) {
dialog->focusGroup(groupId);
dialog->focusContact(contactId);
}
}
@ -102,7 +84,7 @@ void ContentDialogManager::focusGroup(int groupId)
* @param list List with dialogs
* @return ContentDialog if found, nullptr otherwise
*/
ContentDialog* ContentDialogManager::focusDialog(int id, const QHash<int, ContentDialog*>& list)
ContentDialog* ContentDialogManager::focusDialog(const ContactId& id, const QHash<const ContactId&, ContentDialog*>& list)
{
auto iter = list.find(id);
if (iter == list.end()) {
@ -119,63 +101,53 @@ ContentDialog* ContentDialogManager::focusDialog(int id, const QHash<int, Conten
return dialog;
}
void ContentDialogManager::updateFriendStatus(int friendId)
void ContentDialogManager::updateFriendStatus(const ToxPk& friendPk)
{
auto dialog = friendDialogs.value(friendId);
auto dialog = contactDialogs.value(friendPk);
if (dialog == nullptr) {
return;
}
dialog->updateFriendStatusLight(friendId);
if (dialog->isFriendWidgetActive(friendId)) {
dialog->updateContactStatusLight(friendPk);
if (dialog->isContactWidgetActive(friendPk)) {
dialog->updateTitleAndStatusIcon();
}
Friend* f = FriendList::findFriend(friendId);
dialog->updateFriendStatus(friendId, f->getStatus());
Friend* f = FriendList::findFriend(friendPk);
dialog->updateFriendStatus(friendPk, f->getStatus());
}
void ContentDialogManager::updateGroupStatus(int groupId)
void ContentDialogManager::updateGroupStatus(const GroupId& groupId)
{
auto dialog = friendDialogs.value(groupId);
auto dialog = contactDialogs.value(groupId);
if (dialog == nullptr) {
return;
}
dialog->updateGroupStatusLight(groupId);
if (dialog->isGroupWidgetActive(groupId)) {
dialog->updateContactStatusLight(groupId);
if (dialog->isContactWidgetActive(groupId)) {
dialog->updateTitleAndStatusIcon();
}
}
bool ContentDialogManager::isFriendWidgetActive(int friendId)
bool ContentDialogManager::isContactWidgetActive(const ContactId& contactId)
{
const auto dialog = friendDialogs.value(friendId);
const auto dialog = contactDialogs.value(contactId);
if (dialog == nullptr) {
return false;
}
return dialog->isFriendWidgetActive(friendId);
return dialog->isContactWidgetActive(contactId);
}
bool ContentDialogManager::isGroupWidgetActive(int groupId)
ContentDialog* ContentDialogManager::getFriendDialog(const ToxPk& friendPk) const
{
const auto dialog = groupDialogs.value(groupId);
if (dialog == nullptr) {
return false;
}
return dialog->isGroupWidgetActive(groupId);
return contactDialogs.value(friendPk);
}
ContentDialog* ContentDialogManager::getFriendDialog(int friendId) const
ContentDialog* ContentDialogManager::getGroupDialog(const GroupId& groupId) const
{
return friendDialogs.value(friendId);
}
ContentDialog* ContentDialogManager::getGroupDialog(int groupId) const
{
return groupDialogs.value(groupId);
return contactDialogs.value(groupId);
}
ContentDialogManager* ContentDialogManager::getInstance()
@ -207,6 +179,5 @@ void ContentDialogManager::onDialogClose()
currentDialog = nullptr;
}
removeDialog(dialog, friendDialogs);
removeDialog(dialog, groupDialogs);
removeDialog(dialog, contactDialogs);
}

View File

@ -20,9 +20,13 @@
#ifndef _CONTENT_DIALOG_MANAGER_H_
#define _CONTENT_DIALOG_MANAGER_H_
#include <QObject>
#include "src/core/contactid.h"
#include "src/core/toxpk.h"
#include "src/core/groupid.h"
#include "contentdialog.h"
#include <QObject>
/**
* @breaf Manage all content dialogs
*/
@ -31,16 +35,13 @@ class ContentDialogManager : public QObject
Q_OBJECT
public:
ContentDialog* current();
bool friendWidgetExists(int friendId);
bool groupWidgetExists(int groupId);
void focusFriend(int friendId);
void focusGroup(int groupId);
void updateFriendStatus(int friendId);
void updateGroupStatus(int groupId);
bool isFriendWidgetActive(int friendId);
bool isGroupWidgetActive(int groupId);
ContentDialog* getFriendDialog(int friendId) const;
ContentDialog* getGroupDialog(int groupId) const;
bool contactWidgetExists(const ContactId& groupId);
void focusContact(const ContactId& contactId);
void updateFriendStatus(const ToxPk& friendPk);
void updateGroupStatus(const GroupId& friendPk);
bool isContactWidgetActive(const ContactId& contactId);
ContentDialog* getFriendDialog(const ToxPk& friendPk) const;
ContentDialog* getGroupDialog(const GroupId& friendPk) const;
FriendWidget* addFriendToDialog(ContentDialog* dialog, std::shared_ptr<FriendChatroom> chatroom, GenericChatForm* form);
GroupWidget* addGroupToDialog(ContentDialog* dialog, std::shared_ptr<GroupChatroom> chatroom, GenericChatForm* form);
@ -54,12 +55,11 @@ private slots:
void onDialogActivate();
private:
ContentDialog* focusDialog(int id, const QHash<int, ContentDialog*>& list);
ContentDialog* focusDialog(const ContactId& id, const QHash<const ContactId&, ContentDialog*>& list);
ContentDialog* currentDialog = nullptr;
QHash<int, ContentDialog*> friendDialogs;
QHash<int, ContentDialog*> groupDialogs;
QHash<const ContactId&, ContentDialog*> contactDialogs;
static ContentDialogManager* instance;
};

View File

@ -340,7 +340,7 @@ void ChatForm::onFileRecvRequest(ToxFile file)
return;
}
Widget::getInstance()->newFriendMessageAlert(file.friendId);
Widget::getInstance()->newFriendMessageAlert(f->getPublicKey());
QString name;
ToxPk friendId = f->getPublicKey();
if (friendId != previousId) {
@ -694,7 +694,7 @@ GenericNetCamView* ChatForm::createNetcam()
{
qDebug() << "creating netcam";
uint32_t friendId = f->getId();
NetCamView* view = new NetCamView(friendId, this);
NetCamView* view = new NetCamView(f->getPublicKey(), this);
CoreAV* av = Core::getInstance()->getAv();
VideoSource* source = av->getVideoSourceFromCall(friendId);
view->show(source, f->getDisplayedName());

View File

@ -22,6 +22,7 @@
#include "tabcompleter.h"
#include "src/core/core.h"
#include "src/core/coreav.h"
#include "src/core/groupid.h"
#include "src/chatlog/chatlog.h"
#include "src/chatlog/content/text.h"
#include "src/model/friend.h"
@ -187,7 +188,7 @@ void GroupChatForm::onUserListChanged()
}
}
void GroupChatForm::onTitleChanged(uint32_t groupId, const QString& author, const QString& title)
void GroupChatForm::onTitleChanged(const GroupId& groupId, const QString& author, const QString& title)
{
Q_UNUSED(groupId);
if (author.isEmpty()) {

View File

@ -31,6 +31,7 @@ class Group;
class TabCompleter;
class FlowLayout;
class QTimer;
class GroupId;
class GroupChatForm : public GenericChatForm
{
@ -49,7 +50,7 @@ private slots:
void onVolMuteToggle();
void onCallClicked();
void onUserListChanged();
void onTitleChanged(uint32_t groupId, const QString& author, const QString& title);
void onTitleChanged(const GroupId& groupId, const QString& author, const QString& title);
void searchInBegin(const QString& phrase, const ParameterSearch& parameter) override;
void onSearchUp(const QString& phrase, const ParameterSearch& parameter) override;
void onSearchDown(const QString& phrase, const ParameterSearch& parameter) override;

View File

@ -282,7 +282,7 @@ void FriendListWidget::addGroupWidget(GroupWidget* widget)
groupLayout.addSortedWidget(widget);
Group* g = widget->getGroup();
connect(g, &Group::titleChanged,
[=](uint32_t groupId, const QString& author, const QString& name) {
[=](const GroupId& groupId, const QString& author, const QString& name) {
Q_UNUSED(groupId);
Q_UNUSED(author);
renameGroupWidget(widget, name);

View File

@ -105,8 +105,8 @@ void FriendWidget::onContextMenuCalled(QContextMenuEvent* event)
QMenu menu;
const auto frnd = chatroom->getFriend();
const auto friendId = frnd->getId();
const auto contentDialog = ContentDialogManager::getInstance()->getFriendDialog(friendId);
const auto friendPk = frnd->getPublicKey();
const auto contentDialog = ContentDialogManager::getInstance()->getFriendDialog(friendPk);
// TODO: move to model
if (!contentDialog || contentDialog->chatroomWidgetCount() > 1) {
@ -115,7 +115,7 @@ void FriendWidget::onContextMenuCalled(QContextMenuEvent* event)
}
// TODO: move to model
if (contentDialog && contentDialog->hasFriendWidget(friendId)) {
if (contentDialog && contentDialog->hasContactWidget(friendPk)) {
const auto removeChatWindow = menu.addAction(tr("Remove chat from this window"));
connect(removeChatWindow, &QAction::triggered, this, &FriendWidget::removeChatWindow);
}
@ -170,10 +170,10 @@ void FriendWidget::onContextMenuCalled(QContextMenuEvent* event)
menu.addSeparator();
// TODO: move to model
if (!contentDialog || !contentDialog->hasFriendWidget(friendId)) {
if (!contentDialog || !contentDialog->hasContactWidget(friendPk)) {
const auto removeAction =
menu.addAction(tr("Remove friend", "Menu to remove the friend from our friendlist"));
connect(removeAction, &QAction::triggered, this, [=]() { emit removeFriend(friendId); },
connect(removeAction, &QAction::triggered, this, [=]() { emit removeFriend(friendPk); },
Qt::QueuedConnection);
}
@ -194,9 +194,9 @@ void FriendWidget::onContextMenuCalled(QContextMenuEvent* event)
void FriendWidget::removeChatWindow()
{
const auto frnd = chatroom->getFriend();
const auto friendId = frnd->getId();
ContentDialog* contentDialog = ContentDialogManager::getInstance()->getFriendDialog(friendId);
contentDialog->removeFriend(friendId);
const auto friendPk = frnd->getPublicKey();
ContentDialog* contentDialog = ContentDialogManager::getInstance()->getFriendDialog(friendPk);
contentDialog->removeFriend(friendPk);
}
namespace {
@ -361,6 +361,11 @@ const Friend* FriendWidget::getFriend() const
return chatroom->getFriend();
}
const Contact* FriendWidget::getContact() const
{
return getFriend();
}
void FriendWidget::search(const QString& searchString, bool hide)
{
const auto frnd = chatroom->getFriend();

View File

@ -40,13 +40,14 @@ public:
void resetEventFlags() override final;
QString getStatusString() const override final;
const Friend* getFriend() const override final;
const Contact* getContact() const override final;
void search(const QString& searchString, bool hide = false);
signals:
void friendWidgetClicked(FriendWidget* widget);
void removeFriend(int friendId);
void copyFriendIdToClipboard(int friendId);
void removeFriend(const ToxPk& friendPk);
void copyFriendIdToClipboard(const ToxPk& friendPk);
void contextMenuCalled(QContextMenuEvent* event);
void friendHistoryRemoved();
void friendWidgetRenamed(FriendWidget* friendWidget);

View File

@ -29,7 +29,7 @@ class QHBoxLayout;
class ContentLayout;
class Friend;
class Group;
class Contact;
class GenericChatroomWidget : public GenericChatItemWidget
{
Q_OBJECT
@ -42,6 +42,7 @@ public slots:
virtual void updateStatusLight() = 0;
virtual void resetEventFlags() = 0;
virtual QString getStatusString() const = 0;
virtual const Contact* getContact() const = 0;
virtual const Friend* getFriend() const
{
return nullptr;

View File

@ -44,7 +44,7 @@
GroupWidget::GroupWidget(std::shared_ptr<GroupChatroom> chatroom, bool compact)
: GenericChatroomWidget(compact)
, groupId{static_cast<int>(chatroom->getGroup()->getId())}
, groupId{chatroom->getGroup()->getPersistentId()}
, chatroom{chatroom}
{
avatar->setPixmap(Style::scaleSvgImage(":img/group.svg", avatar->width(), avatar->height()));
@ -68,7 +68,7 @@ GroupWidget::~GroupWidget()
Translator::unregister(this);
}
void GroupWidget::updateTitle(uint32_t groupId, const QString& author, const QString& newName)
void GroupWidget::updateTitle(const GroupId& groupId, const QString& author, const QString& newName)
{
Q_UNUSED(groupId);
Q_UNUSED(author);
@ -96,7 +96,7 @@ void GroupWidget::contextMenuEvent(QContextMenuEvent* event)
openChatWindow = menu.addAction(tr("Open chat in new window"));
}
if (contentDialog && contentDialog->hasGroupWidget(groupId)) {
if (contentDialog && contentDialog->hasContactWidget(groupId)) {
removeChatWindow = menu.addAction(tr("Remove chat from this window"));
}
@ -204,6 +204,11 @@ Group* GroupWidget::getGroup() const
return chatroom->getGroup();
}
const Contact* GroupWidget::getContact() const
{
return getGroup();
}
void GroupWidget::resetEventFlags()
{
chatroom->resetEventFlags();

View File

@ -23,6 +23,7 @@
#include "genericchatroomwidget.h"
#include "src/model/chatroom/groupchatroom.h"
#include "src/core/groupid.h"
#include <memory>
@ -38,12 +39,13 @@ public:
void resetEventFlags() final override;
QString getStatusString() const final override;
Group* getGroup() const final override;
const Contact* getContact() const final override;
void setName(const QString& name);
void editName();
signals:
void groupWidgetClicked(GroupWidget* widget);
void removeGroup(int groupId);
void removeGroup(const GroupId& groupId);
protected:
void contextMenuEvent(QContextMenuEvent* event) final override;
@ -55,11 +57,11 @@ protected:
private slots:
void retranslateUi();
void updateTitle(uint32_t groupId, const QString& author, const QString& newName);
void updateTitle(const GroupId& groupId, const QString& author, const QString& newName);
void updateUserCount();
public:
int groupId;
GroupId groupId;
private:
std::shared_ptr<GroupChatroom> chatroom;

View File

@ -929,12 +929,13 @@ void Widget::setStatusMessage(const QString& statusMessage)
void Widget::reloadHistory()
{
for (auto f : FriendList::getAllFriends()) {
chatForms[f->getId()]->loadHistoryDefaultNum(true);
chatForms[f->getPublicKey()]->loadHistoryDefaultNum(true);
}
}
void Widget::incomingNotification(uint32_t friendId)
void Widget::incomingNotification(uint32_t friendnumber)
{
const auto& friendId = FriendList::id2Key(friendnumber);
newFriendMessageAlert(friendId, false);
Audio& audio = Audio::getInstance();
@ -980,9 +981,9 @@ void Widget::addFriend(uint32_t friendId, const ToxPk& friendPk)
auto history = Nexus::getProfile()->getHistory();
auto friendForm = new ChatForm(newfriend, history);
friendChatrooms[friendId] = chatroom;
friendWidgets[friendId] = widget;
chatForms[friendId] = friendForm;
friendChatrooms[friendPk] = chatroom;
friendWidgets[friendPk] = widget;
chatForms[friendPk] = friendForm;
QDate activityDate = settings.getFriendActivity(friendPk);
QDate chatDate = friendForm->getLatestDate();
@ -1007,7 +1008,7 @@ void Widget::addFriend(uint32_t friendId, const ToxPk& friendPk)
connect(widget, &FriendWidget::friendHistoryRemoved, friendForm, &ChatForm::clearChatArea);
connect(widget, &FriendWidget::copyFriendIdToClipboard, this, &Widget::copyFriendIdToClipboard);
connect(widget, &FriendWidget::contextMenuCalled, widget, &FriendWidget::onContextMenuCalled);
connect(widget, SIGNAL(removeFriend(int)), this, SLOT(removeFriend(int)));
connect(widget, SIGNAL(removeFriend(const ToxPk&)), this, SLOT(removeFriend(const ToxPk&)));
Profile* profile = Nexus::getProfile();
connect(profile, &Profile::friendAvatarSet, widget, &FriendWidget::onAvatarSet);
@ -1038,14 +1039,15 @@ void Widget::addFriendFailed(const ToxPk&, const QString& errorInfo)
void Widget::onFriendStatusChanged(int friendId, Status status)
{
Friend* f = FriendList::findFriend(friendId);
const auto& friendPk = FriendList::id2Key(friendId);
Friend* f = FriendList::findFriend(friendPk);
if (!f) {
return;
}
bool isActualChange = f->getStatus() != status;
FriendWidget* widget = friendWidgets[friendId];
FriendWidget* widget = friendWidgets[f->getPublicKey()];
if (isActualChange) {
if (!f->isOnline()) {
contactListWidget->moveWidget(widget, Status::Online);
@ -1060,12 +1062,13 @@ void Widget::onFriendStatusChanged(int friendId, Status status)
setWindowTitle(widget->getTitle());
}
ContentDialogManager::getInstance()->updateFriendStatus(friendId);
ContentDialogManager::getInstance()->updateFriendStatus(friendPk);
}
void Widget::onFriendStatusMessageChanged(int friendId, const QString& message)
{
Friend* f = FriendList::findFriend(friendId);
const auto& friendPk = FriendList::id2Key(friendId);
Friend* f = FriendList::findFriend(friendPk);
if (!f) {
return;
}
@ -1074,14 +1077,14 @@ void Widget::onFriendStatusMessageChanged(int friendId, const QString& message)
str.replace('\n', ' ').remove('\r').remove(QChar('\0'));
f->setStatusMessage(str);
friendWidgets[friendId]->setStatusMsg(str);
chatForms[friendId]->setStatusMessage(str);
friendWidgets[friendPk]->setStatusMsg(str);
chatForms[friendPk]->setStatusMessage(str);
}
void Widget::onFriendDisplayedNameChanged(const QString& displayed)
{
Friend* f = qobject_cast<Friend*>(sender());
FriendWidget* friendWidget = friendWidgets[f->getId()];
FriendWidget* friendWidget = friendWidgets[f->getPublicKey()];
if (friendWidget->isActive()) {
GUI::setWindowTitle(displayed);
@ -1090,7 +1093,8 @@ void Widget::onFriendDisplayedNameChanged(const QString& displayed)
void Widget::onFriendUsernameChanged(int friendId, const QString& username)
{
Friend* f = FriendList::findFriend(friendId);
const auto& friendPk = FriendList::id2Key(friendId);
Friend* f = FriendList::findFriend(friendPk);
if (!f) {
return;
}
@ -1100,20 +1104,19 @@ void Widget::onFriendUsernameChanged(int friendId, const QString& username)
f->setName(str);
}
void Widget::onFriendAliasChanged(uint32_t friendId, const QString& alias)
void Widget::onFriendAliasChanged(const ToxPk& friendId, const QString& alias)
{
Friend* f = qobject_cast<Friend*>(sender());
// TODO(sudden6): don't update the contact list here, make it update itself
FriendWidget* friendWidget = friendWidgets[f->getId()];
FriendWidget* friendWidget = friendWidgets[friendId];
Status status = f->getStatus();
contactListWidget->moveWidget(friendWidget, status);
FilterCriteria criteria = getFilterCriteria();
bool filter = status == Status::Offline ? filterOffline(criteria) : filterOnline(criteria);
friendWidget->searchName(ui->searchContactText->text(), filter);
const ToxPk& pk = f->getPublicKey();
settings.setFriendAlias(pk, alias);
settings.setFriendAlias(friendId, alias);
settings.savePersonal();
}
@ -1132,26 +1135,20 @@ void Widget::openDialog(GenericChatroomWidget* widget, bool newWindow)
widget->resetEventFlags();
widget->updateStatusLight();
uint32_t id;
GenericChatForm* form;
GroupId id;
const Friend* frnd = widget->getFriend();
const Group* group = widget->getGroup();
if (frnd) {
id = frnd->getId();
form = chatForms[id];
form = chatForms[frnd->getPublicKey()];
} else {
id = group->getId();
id = group->getPersistentId();
form = groupChatForms[id].data();
}
bool chatFormIsSet;
if (frnd) {
ContentDialogManager::getInstance()->focusFriend(id);
chatFormIsSet = ContentDialogManager::getInstance()->friendWidgetExists(id);
} else {
ContentDialogManager::getInstance()->focusGroup(id);
chatFormIsSet = ContentDialogManager::getInstance()->groupWidgetExists(id);
}
ContentDialogManager::getInstance()->focusContact(id);
chatFormIsSet = ContentDialogManager::getInstance()->contactWidgetExists(id);
if ((chatFormIsSet || form->isVisible()) && !newWindow) {
return;
@ -1182,17 +1179,18 @@ void Widget::openDialog(GenericChatroomWidget* widget, bool newWindow)
} else {
hideMainForms(widget);
if (frnd) {
chatForms[frnd->getId()]->show(contentLayout);
chatForms[frnd->getPublicKey()]->show(contentLayout);
} else {
groupChatForms[group->getId()]->show(contentLayout);
groupChatForms[group->getPersistentId()]->show(contentLayout);
}
widget->setAsActiveChatroom();
setWindowTitle(widget->getTitle());
}
}
void Widget::onFriendMessageReceived(int friendId, const QString& message, bool isAction)
void Widget::onFriendMessageReceived(uint32_t friendnumber, const QString& message, bool isAction)
{
const auto& friendId = FriendList::id2Key(friendnumber);
Friend* f = FriendList::findFriend(friendId);
if (!f) {
return;
@ -1216,28 +1214,29 @@ void Widget::onFriendMessageReceived(int friendId, const QString& message, bool
void Widget::addFriendDialog(const Friend* frnd, ContentDialog* dialog)
{
uint32_t friendId = frnd->getId();
ContentDialog* contentDialog = ContentDialogManager::getInstance()->getFriendDialog(friendId);
const ToxPk& friendPk = frnd->getPublicKey();
ContentDialog* contentDialog = ContentDialogManager::getInstance()->getFriendDialog(friendPk);
bool isSeparate = settings.getSeparateWindow();
FriendWidget* widget = friendWidgets[friendId];
FriendWidget* widget = friendWidgets[friendPk];
bool isCurrent = activeChatroomWidget == widget;
if (!contentDialog && !isSeparate && isCurrent) {
onAddClicked();
}
auto form = chatForms[friendId];
auto chatroom = friendChatrooms[friendId];
auto form = chatForms[friendPk];
auto chatroom = friendChatrooms[friendPk];
FriendWidget* friendWidget = ContentDialogManager::getInstance()->addFriendToDialog(dialog, chatroom, form);
friendWidget->setStatusMsg(widget->getStatusMsg());
#if (QT_VERSION >= QT_VERSION_CHECK(5, 7, 0))
auto widgetRemoveFriend = QOverload<int>::of(&Widget::removeFriend);
auto widgetRemoveFriend = QOverload<const ToxPk&>::of(&Widget::removeFriend);
#else
auto widgetRemoveFriend = static_cast<void (Widget::*)(int)>(&Widget::removeFriend);
auto widgetRemoveFriend = static_cast<void (Widget::*)(const ToxPk&)>(&Widget::removeFriend);
#endif
connect(friendWidget, &FriendWidget::removeFriend, this, widgetRemoveFriend);
connect(friendWidget, &FriendWidget::middleMouseClicked, dialog,
[=]() { dialog->removeFriend(friendId); });
[=]() { dialog->removeFriend(friendPk); });
connect(friendWidget, &FriendWidget::copyFriendIdToClipboard, this,
&Widget::copyFriendIdToClipboard);
connect(friendWidget, &FriendWidget::newWindowOpened, this, &Widget::openNewDialog);
@ -1271,7 +1270,7 @@ void Widget::addFriendDialog(const Friend* frnd, ContentDialog* dialog)
void Widget::addGroupDialog(Group* group, ContentDialog* dialog)
{
int groupId = group->getId();
const GroupId& groupId = group->getPersistentId();
ContentDialog* groupDialog = ContentDialogManager::getInstance()->getGroupDialog(groupId);
bool separated = settings.getSeparateWindow();
GroupWidget* widget = groupWidgets[groupId];
@ -1285,9 +1284,9 @@ void Widget::addGroupDialog(Group* group, ContentDialog* dialog)
auto groupWidget = ContentDialogManager::getInstance()->addGroupToDialog(dialog, chatroom, chatForm);
#if (QT_VERSION >= QT_VERSION_CHECK(5, 7, 0))
auto removeGroup = QOverload<int>::of(&Widget::removeGroup);
auto removeGroup = QOverload<const GroupId&>::of(&Widget::removeGroup);
#else
auto removeGroup = static_cast<void (Widget::*)(int)>(&Widget::removeGroup);
auto removeGroup = static_cast<void (Widget::*)(const GroupId&)>(&Widget::removeGroup);
#endif
connect(groupWidget, &GroupWidget::removeGroup, this, removeGroup);
connect(groupWidget, &GroupWidget::chatroomWidgetClicked, chatForm, &GroupChatForm::focusInput);
@ -1313,7 +1312,7 @@ void Widget::addGroupDialog(Group* group, ContentDialog* dialog)
emit widget->chatroomWidgetClicked(widget);
}
bool Widget::newFriendMessageAlert(int friendId, bool sound)
bool Widget::newFriendMessageAlert(const ToxPk& friendId, bool sound)
{
bool hasActive;
QWidget* currentWindow;
@ -1322,7 +1321,7 @@ bool Widget::newFriendMessageAlert(int friendId, bool sound)
if (contentDialog != nullptr) {
currentWindow = contentDialog->window();
hasActive = ContentDialogManager::getInstance()->isFriendWidgetActive(friendId);
hasActive = ContentDialogManager::getInstance()->isContactWidgetActive(friendId);
} else {
if (settings.getSeparateWindow() && settings.getShowWindow()) {
if (settings.getDontGroupWindows()) {
@ -1336,7 +1335,7 @@ bool Widget::newFriendMessageAlert(int friendId, bool sound)
addFriendDialog(f, contentDialog);
currentWindow = contentDialog->window();
hasActive = ContentDialogManager::getInstance()->isFriendWidgetActive(friendId);
hasActive = ContentDialogManager::getInstance()->isContactWidgetActive(friendId);
} else {
currentWindow = window();
FriendWidget* widget = friendWidgets[friendId];
@ -1367,7 +1366,7 @@ bool Widget::newFriendMessageAlert(int friendId, bool sound)
return false;
}
bool Widget::newGroupMessageAlert(int groupId, bool notify)
bool Widget::newGroupMessageAlert(const GroupId& groupId, bool notify)
{
bool hasActive;
QWidget* currentWindow;
@ -1377,7 +1376,7 @@ bool Widget::newGroupMessageAlert(int groupId, bool notify)
if (contentDialog != nullptr) {
currentWindow = contentDialog->window();
hasActive = ContentDialogManager::getInstance()->isGroupWidgetActive(groupId);
hasActive = ContentDialogManager::getInstance()->isContactWidgetActive(groupId);
} else {
currentWindow = window();
hasActive = widget == activeChatroomWidget;
@ -1480,7 +1479,7 @@ void Widget::updateFriendActivity(const Friend* frnd)
// Update old activity before after new one. Store old date first.
QDate oldDate = settings.getFriendActivity(pk);
settings.setFriendActivity(pk, QDate::currentDate());
FriendWidget* widget = friendWidgets[frnd->getId()];
FriendWidget* widget = friendWidgets[frnd->getPublicKey()];
contactListWidget->moveWidget(widget, frnd->getStatus());
contactListWidget->updateActivityDate(oldDate);
}
@ -1501,8 +1500,8 @@ void Widget::removeFriend(Friend* f, bool fake)
}
}
const uint32_t friendId = f->getId();
auto widget = friendWidgets[friendId];
const ToxPk friendPk = f->getPublicKey();
auto widget = friendWidgets[friendPk];
widget->setAsInactiveChatroom();
if (widget == activeChatroomWidget) {
activeChatroomWidget = nullptr;
@ -1511,21 +1510,21 @@ void Widget::removeFriend(Friend* f, bool fake)
contactListWidget->removeFriendWidget(widget);
ContentDialog* lastDialog = ContentDialogManager::getInstance()->getFriendDialog(friendId);
ContentDialog* lastDialog = ContentDialogManager::getInstance()->getFriendDialog(friendPk);
if (lastDialog != nullptr) {
lastDialog->removeFriend(friendId);
lastDialog->removeFriend(friendPk);
}
FriendList::removeFriend(friendId, fake);
FriendList::removeFriend(friendPk, fake);
if (!fake) {
core->removeFriend(friendId);
core->removeFriend(f->getId());
}
friendWidgets.remove(friendId);
friendWidgets.remove(friendPk);
delete widget;
auto chatForm = chatForms[friendId];
chatForms.remove(friendId);
auto chatForm = chatForms[friendPk];
chatForms.remove(friendPk);
delete chatForm;
delete f;
@ -1536,7 +1535,7 @@ void Widget::removeFriend(Friend* f, bool fake)
contactListWidget->reDraw();
}
void Widget::removeFriend(int friendId)
void Widget::removeFriend(const ToxPk& friendId)
{
removeFriend(FriendList::findFriend(friendId), false);
}
@ -1552,13 +1551,12 @@ void Widget::onDialogShown(GenericChatroomWidget* widget)
void Widget::onFriendDialogShown(const Friend* f)
{
int friendId = f->getId();
onDialogShown(friendWidgets[friendId]);
onDialogShown(friendWidgets[f->getPublicKey()]);
}
void Widget::onGroupDialogShown(Group* g)
{
uint32_t groupId = g->getId();
const GroupId& groupId = g->getPersistentId();
onDialogShown(groupWidgets[groupId]);
}
@ -1675,20 +1673,20 @@ ContentLayout* Widget::createContentDialog(DialogType type) const
return contentLayoutDialog;
}
void Widget::copyFriendIdToClipboard(int friendId)
void Widget::copyFriendIdToClipboard(const ToxPk& friendId)
{
Friend* f = FriendList::findFriend(friendId);
if (f != nullptr) {
QClipboard* clipboard = QApplication::clipboard();
const ToxPk& pk = core->getFriendPublicKey(f->getId());
clipboard->setText(pk.toString(), QClipboard::Clipboard);
clipboard->setText(friendId.toString(), QClipboard::Clipboard);
}
}
void Widget::onGroupInviteReceived(const GroupInvite& inviteInfo)
{
const uint32_t friendId = inviteInfo.getFriendId();
const Friend* f = FriendList::findFriend(friendId);
const ToxPk& friendPk = FriendList::id2Key(friendId);
const Friend* f = FriendList::findFriend(friendPk);
updateFriendActivity(f);
const uint8_t confType = inviteInfo.getType();
@ -1725,7 +1723,8 @@ void Widget::onGroupInviteAccepted(const GroupInvite& inviteInfo)
void Widget::onGroupMessageReceived(int groupnumber, int peernumber, const QString& message,
bool isAction)
{
Group* g = GroupList::findGroup(groupnumber);
const GroupId& groupId = GroupList::id2Key(groupnumber);
Group* g = GroupList::findGroup(groupId);
assert(g);
ToxPk author = core->getGroupPeerPk(groupnumber, peernumber);
@ -1738,7 +1737,6 @@ void Widget::onGroupMessageReceived(int groupnumber, int peernumber, const QStri
const auto mention = !core->getUsername().isEmpty() && (message.contains(nameMention) || message.contains(sanitizedNameMention));
const auto targeted = !isSelf && mention;
const auto groupId = g->getId();
const auto date = QDateTime::currentDateTime();
auto form = groupChatForms[groupId].data();
@ -1751,16 +1749,18 @@ void Widget::onGroupMessageReceived(int groupnumber, int peernumber, const QStri
newGroupMessageAlert(groupId, targeted || settings.getGroupAlwaysNotify());
}
void Widget::onGroupPeerlistChanged(int groupnumber)
void Widget::onGroupPeerlistChanged(uint32_t groupnumber)
{
Group* g = GroupList::findGroup(groupnumber);
const GroupId& groupId = GroupList::id2Key(groupnumber);
Group* g = GroupList::findGroup(groupId);
assert(g);
g->regeneratePeerList();
}
void Widget::onGroupPeerNameChanged(int groupnumber, int peernumber, const QString& newName)
void Widget::onGroupPeerNameChanged(uint32_t groupnumber, int peernumber, const QString& newName)
{
Group* g = GroupList::findGroup(groupnumber);
const GroupId& groupId = GroupList::id2Key(groupnumber);
Group* g = GroupList::findGroup(groupId);
assert(g);
QString setName = newName;
@ -1771,12 +1771,13 @@ void Widget::onGroupPeerNameChanged(int groupnumber, int peernumber, const QStri
g->updatePeer(peernumber, setName);
}
void Widget::onGroupTitleChanged(int groupnumber, const QString& author, const QString& title)
void Widget::onGroupTitleChanged(uint32_t groupnumber, const QString& author, const QString& title)
{
Group* g = GroupList::findGroup(groupnumber);
const GroupId& groupId = GroupList::id2Key(groupnumber);
Group* g = GroupList::findGroup(groupId);
assert(g);
GroupWidget* widget = groupWidgets[groupnumber];
GroupWidget* widget = groupWidgets[groupId];
if (widget->isActive()) {
GUI::setWindowTitle(title);
}
@ -1786,21 +1787,29 @@ void Widget::onGroupTitleChanged(int groupnumber, const QString& author, const Q
widget->searchName(ui->searchContactText->text(), filterGroups(filter));
}
void Widget::titleChangedByUser(const GroupId& groupId, const QString& title)
{
const auto& group = GroupList::findGroup(groupId);
emit changeGroupTitle(group->getId(), title);
}
void Widget::onGroupPeerAudioPlaying(int groupnumber, ToxPk peerPk)
{
Group* g = GroupList::findGroup(groupnumber);
const GroupId& groupId = GroupList::id2Key(groupnumber);
Group* g = GroupList::findGroup(groupId);
assert(g);
auto form = groupChatForms[g->getId()].data();
auto form = groupChatForms[groupId].data();
form->peerAudioPlaying(peerPk);
}
void Widget::removeGroup(Group* g, bool fake)
{
auto groupId = g->getId();
const auto& groupId = g->getPersistentId();
const auto groupnumber = g->getId();
auto groupWidgetIt = groupWidgets.find(groupId);
if (groupWidgetIt == groupWidgets.end()) {
qWarning() << "Tried to remove group" << groupId << "but GroupWidget doesn't exist";
qWarning() << "Tried to remove group" << groupnumber << "but GroupWidget doesn't exist";
return;
}
auto widget = groupWidgetIt.value();
@ -1817,14 +1826,14 @@ void Widget::removeGroup(Group* g, bool fake)
}
if (!fake) {
core->removeGroup(groupId);
core->removeGroup(groupnumber);
}
contactListWidget->removeGroupWidget(widget); // deletes widget
groupWidgets.remove(groupId);
auto groupChatFormIt = groupChatForms.find(groupId);
if (groupChatFormIt == groupChatForms.end()) {
qWarning() << "Tried to remove group" << groupId << "but GroupChatForm doesn't exist";
qWarning() << "Tried to remove group" << groupnumber << "but GroupChatForm doesn't exist";
return;
}
groupChatForms.erase(groupChatFormIt);
@ -1836,12 +1845,12 @@ void Widget::removeGroup(Group* g, bool fake)
contactListWidget->reDraw();
}
void Widget::removeGroup(int groupId)
void Widget::removeGroup(const GroupId& groupId)
{
removeGroup(GroupList::findGroup(groupId));
}
Group* Widget::createGroup(int groupId, const GroupId& groupPersistentId)
Group* Widget::createGroup(uint32_t groupnumber, const GroupId& groupId)
{
Group* g = GroupList::findGroup(groupId);
if (g) {
@ -1849,9 +1858,9 @@ Group* Widget::createGroup(int groupId, const GroupId& groupPersistentId)
return g;
}
const auto groupName = tr("Groupchat #%1").arg(groupId);
bool enabled = core->getGroupAvEnabled(groupId);
Group* newgroup = GroupList::addGroup(groupId, groupPersistentId, groupName, enabled, core->getUsername());
const auto groupName = tr("Groupchat #%1").arg(groupnumber);
bool enabled = core->getGroupAvEnabled(groupnumber);
Group* newgroup = GroupList::addGroup(groupnumber, groupId, groupName, enabled, core->getUsername());
std::shared_ptr<GroupChatroom> chatroom(new GroupChatroom(newgroup));
const auto compact = settings.getCompactLayout();
auto widget = new GroupWidget(chatroom, compact);
@ -1867,16 +1876,17 @@ Group* Widget::createGroup(int groupId, const GroupId& groupPersistentId)
connect(widget, &GroupWidget::chatroomWidgetClicked, this, &Widget::onChatroomWidgetClicked);
connect(widget, &GroupWidget::newWindowOpened, this, &Widget::openNewDialog);
#if (QT_VERSION >= QT_VERSION_CHECK(5, 7, 0))
auto widgetRemoveGroup = QOverload<int>::of(&Widget::removeGroup);
auto widgetRemoveGroup = QOverload<const GroupId&>::of(&Widget::removeGroup);
#else
auto widgetRemoveGroup = static_cast<void (Widget::*)(int)>(&Widget::removeGroup);
auto widgetRemoveGroup = static_cast<void (Widget::*)(const GroupId&)>(&Widget::removeGroup);
#endif
connect(widget, &GroupWidget::removeGroup, this, widgetRemoveGroup);
connect(widget, &GroupWidget::middleMouseClicked, this, [=]() { removeGroup(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::titleChangedByUser, core, &Core::changeGroupTitle);
connect(newgroup, &Group::titleChangedByUser, this, &Widget::titleChangedByUser);
connect(this, &Widget::changeGroupTitle, core, &Core::changeGroupTitle);
connect(core, &Core::usernameSet, newgroup, &Group::setSelfName);
FilterCriteria filter = getFilterCriteria();
@ -1885,9 +1895,9 @@ Group* Widget::createGroup(int groupId, const GroupId& groupPersistentId)
return newgroup;
}
void Widget::onEmptyGroupCreated(int groupId, const GroupId& groupPersistentId, const QString& title)
void Widget::onEmptyGroupCreated(uint32_t groupnumber, const GroupId& groupId, const QString& title)
{
Group* group = createGroup(groupId, groupPersistentId);
Group* group = createGroup(groupnumber, groupId);
if (!group) {
return;
}
@ -2053,25 +2063,27 @@ void Widget::setStatusBusy()
core->setStatus(Status::Busy);
}
void Widget::onGroupSendFailed(int groupId)
void Widget::onGroupSendFailed(uint32_t groupnumber)
{
const auto& groupId = GroupList::id2Key(groupnumber);
Group* g = GroupList::findGroup(groupId);
assert(g);
const auto message = tr("Message failed to send");
const auto curTime = QDateTime::currentDateTime();
auto form = groupChatForms[g->getId()].data();
auto form = groupChatForms[groupId].data();
form->addSystemInfoMessage(message, ChatMessage::INFO, curTime);
}
void Widget::onFriendTypingChanged(int friendId, bool isTyping)
void Widget::onFriendTypingChanged(uint32_t friendnumber, bool isTyping)
{
const auto& friendId = FriendList::id2Key(friendnumber);
Friend* f = FriendList::findFriend(friendId);
if (!f) {
return;
}
chatForms[friendId]->setFriendTyping(isTyping);
chatForms[f->getPublicKey()]->setFriendTyping(isTyping);
}
void Widget::onSetShowSystemTray(bool newValue)
@ -2143,7 +2155,7 @@ void Widget::clearAllReceipts()
{
QList<Friend*> frnds = FriendList::getAllFriends();
for (Friend* f : frnds) {
chatForms[f->getId()]->getOfflineMsgEngine()->removeAllMessages();
chatForms[f->getPublicKey()]->getOfflineMsgEngine()->removeAllMessages();
}
}
@ -2165,22 +2177,20 @@ void Widget::reloadTheme()
}
for (Friend* f : FriendList::getAllFriends()) {
uint32_t friendId = f->getId();
friendWidgets[friendId]->reloadTheme();
friendWidgets[f->getPublicKey()]->reloadTheme();
}
for (Group* g : GroupList::getAllGroups()) {
uint32_t groupId = g->getId();
groupWidgets[groupId]->reloadTheme();
groupWidgets[g->getPersistentId()]->reloadTheme();
}
for (auto f : FriendList::getAllFriends()) {
chatForms[f->getId()]->reloadTheme();
chatForms[f->getPublicKey()]->reloadTheme();
}
for (auto g : GroupList::getAllGroups()) {
groupChatForms[g->getId()]->reloadTheme();
groupChatForms[g->getPersistentId()]->reloadTheme();
}
}
@ -2481,9 +2491,9 @@ void Widget::focusChatInput()
{
if (activeChatroomWidget) {
if (const Friend* f = activeChatroomWidget->getFriend()) {
chatForms[f->getId()]->focusInput();
chatForms[f->getPublicKey()]->focusInput();
} else if (Group* g = activeChatroomWidget->getGroup()) {
groupChatForms[g->getId()]->focusInput();
groupChatForms[g->getPersistentId()]->focusInput();
}
}
}

View File

@ -120,8 +120,8 @@ public:
void showUpdateDownloadProgress();
void addFriendDialog(const Friend* frnd, ContentDialog* dialog);
void addGroupDialog(Group* group, ContentDialog* dialog);
bool newFriendMessageAlert(int friendId, bool sound = true);
bool newGroupMessageAlert(int groupId, bool notify);
bool newFriendMessageAlert(const ToxPk& friendId, bool sound = true);
bool newGroupMessageAlert(const GroupId& groupId, bool notify);
bool getIsWindowMinimized();
void updateIcons();
@ -166,21 +166,22 @@ public slots:
void onFriendStatusMessageChanged(int friendId, const QString& message);
void onFriendDisplayedNameChanged(const QString& displayed);
void onFriendUsernameChanged(int friendId, const QString& username);
void onFriendAliasChanged(uint32_t friendId, const QString& alias);
void onFriendMessageReceived(int friendId, const QString& message, bool isAction);
void onFriendAliasChanged(const ToxPk& friendId, const QString& alias);
void onFriendMessageReceived(uint32_t friendnumber, const QString& message, bool isAction);
void onFriendRequestReceived(const ToxPk& friendPk, const QString& message);
void updateFriendActivity(const Friend* frnd);
void onEmptyGroupCreated(int groupNum, const GroupId& groupId, const QString& title);
void onEmptyGroupCreated(uint32_t groupnumber, const GroupId& groupId, const QString& title);
void onGroupJoined(int groupNum, const GroupId& groupId);
void onGroupInviteReceived(const GroupInvite& inviteInfo);
void onGroupInviteAccepted(const GroupInvite& inviteInfo);
void onGroupMessageReceived(int groupnumber, int peernumber, const QString& message, bool isAction);
void onGroupPeerlistChanged(int groupnumber);
void onGroupPeerNameChanged(int groupnumber, int peernumber, const QString& newName);
void onGroupTitleChanged(int groupnumber, const QString& author, const QString& title);
void onGroupPeerlistChanged(uint32_t groupnumber);
void onGroupPeerNameChanged(uint32_t groupnumber, int peernumber, const QString& newName);
void onGroupTitleChanged(uint32_t groupnumber, const QString& author, const QString& title);
void titleChangedByUser(const GroupId& groupId, const QString& title);
void onGroupPeerAudioPlaying(int groupnumber, ToxPk peerPk);
void onGroupSendFailed(int groupId);
void onFriendTypingChanged(int friendId, bool isTyping);
void onGroupSendFailed(uint32_t groupnumber);
void onFriendTypingChanged(uint32_t friendnumber, bool isTyping);
void nextContact();
void previousContact();
void onFriendDialogShown(const Friend* f);
@ -195,6 +196,7 @@ signals:
void statusSet(Status status);
void statusSelected(Status status);
void usernameChanged(const QString& username);
void changeGroupTitle(uint32_t groupnumber, const QString& title);
void statusMessageChanged(const QString& statusMessage);
void resized();
void windowStateChanged(Qt::WindowStates states);
@ -207,9 +209,9 @@ private slots:
void openNewDialog(GenericChatroomWidget* widget);
void onChatroomWidgetClicked(GenericChatroomWidget* widget);
void onStatusMessageChanged(const QString& newStatusMessage);
void removeFriend(int friendId);
void copyFriendIdToClipboard(int friendId);
void removeGroup(int groupId);
void removeFriend(const ToxPk& friendId);
void copyFriendIdToClipboard(const ToxPk& friendId);
void removeGroup(const GroupId& groupId);
void setStatusOnline();
void setStatusAway();
void setStatusBusy();
@ -242,7 +244,7 @@ private:
bool newMessageAlert(QWidget* currentWindow, bool isActive, bool sound = true, bool notify = true);
void setActiveToolMenuButton(ActiveToolMenuButton newActiveButton);
void hideMainForms(GenericChatroomWidget* chatroomWidget);
Group* createGroup(int groupId, const GroupId& groupPersistentId);
Group* createGroup(uint32_t groupnumber, const GroupId& groupId);
void removeFriend(Friend* f, bool fake = false);
void removeGroup(Group* g, bool fake = false);
void saveWindowGeometry();
@ -312,13 +314,13 @@ private:
int icon_size;
Settings& settings;
QMap<uint32_t, FriendWidget*> friendWidgets;
QMap<uint32_t, std::shared_ptr<FriendChatroom>> friendChatrooms;
QMap<uint32_t, ChatForm*> chatForms;
QMap<ToxPk, FriendWidget*> friendWidgets;
QMap<ToxPk, std::shared_ptr<FriendChatroom>> friendChatrooms;
QMap<ToxPk, ChatForm*> chatForms;
QMap<uint32_t, GroupWidget*> groupWidgets;
QMap<uint32_t, std::shared_ptr<GroupChatroom>> groupChatrooms;
QMap<uint32_t, QSharedPointer<GroupChatForm>> groupChatForms;
QMap<GroupId, GroupWidget*> groupWidgets;
QMap<GroupId, std::shared_ptr<GroupChatroom>> groupChatrooms;
QMap<GroupId, QSharedPointer<GroupChatForm>> groupChatForms;
Core* core = nullptr;
#if DESKTOP_NOTIFICATIONS