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; 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>; using ContactIdPtr = std::shared_ptr<const ContactId>;

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -22,6 +22,7 @@
#include "tabcompleter.h" #include "tabcompleter.h"
#include "src/core/core.h" #include "src/core/core.h"
#include "src/core/coreav.h" #include "src/core/coreav.h"
#include "src/core/groupid.h"
#include "src/chatlog/chatlog.h" #include "src/chatlog/chatlog.h"
#include "src/chatlog/content/text.h" #include "src/chatlog/content/text.h"
#include "src/model/friend.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); Q_UNUSED(groupId);
if (author.isEmpty()) { if (author.isEmpty()) {

View File

@ -31,6 +31,7 @@ class Group;
class TabCompleter; class TabCompleter;
class FlowLayout; class FlowLayout;
class QTimer; class QTimer;
class GroupId;
class GroupChatForm : public GenericChatForm class GroupChatForm : public GenericChatForm
{ {
@ -49,7 +50,7 @@ private slots:
void onVolMuteToggle(); void onVolMuteToggle();
void onCallClicked(); void onCallClicked();
void onUserListChanged(); 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 searchInBegin(const QString& phrase, const ParameterSearch& parameter) override;
void onSearchUp(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; void onSearchDown(const QString& phrase, const ParameterSearch& parameter) override;

View File

@ -282,7 +282,7 @@ void FriendListWidget::addGroupWidget(GroupWidget* widget)
groupLayout.addSortedWidget(widget); groupLayout.addSortedWidget(widget);
Group* g = widget->getGroup(); Group* g = widget->getGroup();
connect(g, &Group::titleChanged, 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(groupId);
Q_UNUSED(author); Q_UNUSED(author);
renameGroupWidget(widget, name); renameGroupWidget(widget, name);

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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