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

refactor: Remove FriendWidget from Friend class

This commit is contained in:
Diadlo 2016-08-24 03:13:26 +03:00
parent bf4259b229
commit bff25ce8b1
No known key found for this signature in database
GPG Key ID: 5AF9F2E29107C727
8 changed files with 104 additions and 88 deletions

View File

@ -19,15 +19,13 @@
#include "friend.h"
#include "widget/form/chatform.h"
#include "widget/friendwidget.h"
#include "widget/gui.h"
#include "src/core/core.h"
#include "src/group.h"
#include "src/grouplist.h"
#include "src/nexus.h"
#include "src/persistence/settings.h"
#include "src/persistence/profile.h"
#include "src/nexus.h"
#include "src/grouplist.h"
#include "src/group.h"
#include "src/widget/form/chatform.h"
Friend::Friend(uint32_t friendId, const ToxPk& friendPk)
: userName{Core::getInstance()->getPeerName(friendPk)}
@ -50,7 +48,6 @@ Friend::Friend(uint32_t friendId, const ToxPk& friendPk)
Friend::~Friend()
{
delete chatForm;
delete widget;
}
/**
@ -61,52 +58,41 @@ void Friend::loadHistory()
if (Nexus::getProfile()->isHistoryEnabled())
{
chatForm->loadHistory(QDateTime::currentDateTime().addDays(-7), true);
widget->historyLoaded = true;
}
emit loadChatHistory();
}
void Friend::setName(QString name)
{
if (name.isEmpty())
name = friendPk.toString();
userName = name;
if (userAlias.size() == 0)
if (name.isEmpty())
{
widget->setName(name);
chatForm->setName(name);
name = friendPk.toString();
}
if (widget->isActive())
GUI::setWindowTitle(name);
emit displayedNameChanged(getFriendWidget(), getStatus(), hasNewEvents);
if (userName != name)
{
userName = name;
emit nameChanged(friendId, name);
}
}
void Friend::setAlias(QString name)
void Friend::setAlias(QString alias)
{
userAlias = name;
QString dispName = userAlias.isEmpty() ? userName : userAlias;
widget->setName(dispName);
chatForm->setName(dispName);
if (widget->isActive())
GUI::setWindowTitle(dispName);
emit displayedNameChanged(getFriendWidget(), getStatus(), hasNewEvents);
for (Group *g : GroupList::getAllGroups())
if (userAlias != alias)
{
g->regeneratePeerList();
userAlias = alias;
emit aliasChanged(friendId, alias);
}
}
void Friend::setStatusMessage(QString message)
{
statusMessage = message;
widget->setStatusMsg(message);
chatForm->setStatusMessage(message);
if (statusMessage != message)
{
statusMessage = message;
emit statusMessageChanged(friendId, message);
}
}
QString Friend::getStatusMessage()
@ -116,7 +102,12 @@ QString Friend::getStatusMessage()
QString Friend::getDisplayedName() const
{
return userAlias.isEmpty() ? userName : userAlias;
if (userAlias.isEmpty())
{
return userName;
}
return userAlias;
}
bool Friend::hasAlias() const
@ -146,7 +137,11 @@ bool Friend::getEventFlag() const
void Friend::setStatus(Status s)
{
friendStatus = s;
if (friendStatus != s)
{
friendStatus = s;
emit statusChanged(friendId, friendStatus);
}
}
Status Friend::getStatus() const
@ -154,22 +149,7 @@ Status Friend::getStatus() const
return friendStatus;
}
void Friend::setFriendWidget(FriendWidget *widget)
{
this->widget = widget;
}
ChatForm *Friend::getChatForm()
{
return chatForm;
}
FriendWidget *Friend::getFriendWidget()
{
return widget;
}
const FriendWidget *Friend::getFriendWidget() const
{
return widget;
}

View File

@ -33,9 +33,9 @@ class Friend : public QObject
Q_OBJECT
public:
Friend(uint32_t FriendId, const ToxPk& FriendPk);
Friend(const Friend& other)=delete;
Friend(const Friend& other) = delete;
~Friend();
Friend& operator=(const Friend& other)=delete;
Friend& operator=(const Friend& other) = delete;
void loadHistory();
@ -58,12 +58,15 @@ public:
ChatForm *getChatForm();
void setFriendWidget(FriendWidget* widget);
FriendWidget *getFriendWidget();
const FriendWidget *getFriendWidget() const;
signals:
void displayedNameChanged(FriendWidget* widget, Status s, int hasNewEvents);
// TODO: move signals to DB object
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 loadChatHistory();
public slots:
private:
QString userName;

View File

@ -169,7 +169,7 @@ FriendWidget* ContentDialog::addFriend(int friendId, QString id)
Friend* frnd = friendWidget->getFriend();
connect(frnd, &Friend::displayedNameChanged, this, &ContentDialog::updateFriendWidget);
connect(frnd, &Friend::aliasChanged, this, &ContentDialog::updateFriendWidget);
connect(friendWidget, &FriendWidget::chatroomWidgetClicked, this, &ContentDialog::onChatroomWidgetClicked);
connect(friendWidget, SIGNAL(chatroomWidgetClicked(GenericChatroomWidget*)), frnd->getChatForm(), SLOT(focusInput()));
connect(Core::getInstance(), &Core::friendAvatarChanged, friendWidget, &FriendWidget::onAvatarChange);
@ -217,7 +217,8 @@ void ContentDialog::removeFriend(int friendId)
return;
FriendWidget* chatroomWidget = static_cast<FriendWidget*>(std::get<1>(iter.value()));
disconnect(chatroomWidget->getFriend(), &Friend::displayedNameChanged, this, &ContentDialog::updateFriendWidget);
disconnect(chatroomWidget->getFriend(), &Friend::aliasChanged,
this, &ContentDialog::updateFriendWidget);
// Need to find replacement to show here instead.
if (activeChatroomWidget == chatroomWidget)
@ -682,11 +683,15 @@ void ContentDialog::onChatroomWidgetClicked(GenericChatroomWidget *widget, bool
updateTitle(widget);
}
void ContentDialog::updateFriendWidget(FriendWidget *w, Status s)
void ContentDialog::updateFriendWidget(uint32_t friendId, QString alias)
{
FriendWidget* friendWidget = static_cast<FriendWidget*>(std::get<1>(friendList.find(w->friendId).value()));
friendWidget->setName(w->getName());
friendLayout->addFriendWidget(friendWidget, s);
Friend *f = FriendList::findFriend(friendId);
GenericChatroomWidget *widget = std::get<1>(friendList.find(friendId).value());
FriendWidget* friendWidget = static_cast<FriendWidget*>(widget);
friendWidget->setName(alias);
Status status = f->getStatus();
friendLayout->addFriendWidget(friendWidget, status);
}
void ContentDialog::updateGroupWidget(GroupWidget *w)

View File

@ -93,7 +93,7 @@ protected:
private slots:
void onChatroomWidgetClicked(GenericChatroomWidget* widget, bool group);
void updateFriendWidget(FriendWidget* w, Status s);
void updateFriendWidget(uint32_t friendId, QString alias);
void updateGroupWidget(GroupWidget* w);
void onGroupchatPositionChanged(bool top);

View File

@ -182,7 +182,7 @@ ChatForm::~ChatForm()
delete netcam;
}
void ChatForm::setStatusMessage(QString newMessage)
void ChatForm::setStatusMessage(const QString& newMessage)
{
statusMessageLabel->setText(newMessage);
// for long messsages

View File

@ -44,7 +44,7 @@ class ChatForm : public GenericChatForm
public:
explicit ChatForm(Friend* chatFriend);
~ChatForm();
void setStatusMessage(QString newMessage);
void setStatusMessage(const QString& newMessage);
void loadHistory(QDateTime since, bool processUndelivered = false);
void dischargeReceipt(int receipt);

View File

@ -1004,7 +1004,6 @@ void Widget::addFriend(int friendId, const ToxPk& friendPk)
friendWidgets[friendId] = widget;
newfriend->setFriendWidget(widget);
newfriend->loadHistory();
const Settings& s = Settings::getInstance();
@ -1018,15 +1017,27 @@ void Widget::addFriend(int friendId, const ToxPk& friendPk)
contactListWidget->addFriendWidget(widget, Status::Offline, s.getFriendCircleID(friendPk));
connect(newfriend, &Friend::displayedNameChanged, this, &Widget::onFriendDisplayChanged);
connect(widget, &FriendWidget::chatroomWidgetClicked, this, &Widget::onChatroomWidgetClicked);
connect(widget, &FriendWidget::chatroomWidgetClicked, friendForm, &ChatForm::focusInput);
connect(widget, &FriendWidget::copyFriendIdToClipboard, this, &Widget::copyFriendIdToClipboard);
connect(widget, &FriendWidget::contextMenuCalled, widget, &FriendWidget::onContextMenuCalled);
connect(widget, SIGNAL(removeFriend(int)), this, SLOT(removeFriend(int)));
connect(newfriend, &Friend::aliasChanged,
this, &Widget::onFriendAliasChanged);
connect(newfriend, &Friend::nameChanged,
this, &Widget::onFriendAliasChanged);
connect(widget, &FriendWidget::chatroomWidgetClicked,
this, &Widget::onChatroomWidgetClicked);
connect(widget, &FriendWidget::chatroomWidgetClicked,
friendForm, &ChatForm::focusInput);
connect(widget, &FriendWidget::copyFriendIdToClipboard,
this, &Widget::copyFriendIdToClipboard);
connect(widget, &FriendWidget::contextMenuCalled,
widget, &FriendWidget::onContextMenuCalled);
connect(widget, SIGNAL(removeFriend(int)),
this, SLOT(removeFriend(int)));
Core* core = Core::getInstance();
connect(core, &Core::friendAvatarChanged, widget, &FriendWidget::onAvatarChange);
connect(core, &Core::friendAvatarRemoved, widget, &FriendWidget::onAvatarRemoved);
connect(core, &Core::friendAvatarChanged,
widget, &FriendWidget::onAvatarChange);
connect(core, &Core::friendAvatarRemoved,
widget, &FriendWidget::onAvatarRemoved);
// Try to get the avatar from the cache
QPixmap avatar = Nexus::getProfile()->loadAvatar(friendPk.toString());
@ -1099,11 +1110,12 @@ void Widget::onFriendStatusMessageChanged(int friendId, const QString& message)
}
QString str = message;
str.replace('\n', ' ');
str.remove('\r');
str.remove(QChar((char)0)); // null terminator...
str.replace('\n', ' ').remove('\r').remove(QChar('\0'));
f->setStatusMessage(str);
friendWidgets[friendId]->setStatusMsg(str);
f->getChatForm()->setStatusMessage(str);
ContentDialog::updateFriendStatusMessage(friendId, message);
}
@ -1116,19 +1128,35 @@ void Widget::onFriendUsernameChanged(int friendId, const QString& username)
}
QString str = username;
str.replace('\n', ' ');
str.remove('\r');
str.remove(QChar((char)0)); // null terminator...
str.replace('\n', ' ').remove('\r').remove(QChar('\0'));
f->setName(str);
}
void Widget::onFriendDisplayChanged(FriendWidget *friendWidget, Status s)
void Widget::onFriendAliasChanged(uint32_t friendId, const QString& alias)
{
Friend *f = FriendList::findFriend(friendId);
FriendWidget *friendWidget = friendWidgets[friendId];
friendWidget->setName(alias);
if (friendWidget->isActive())
{
GUI::setWindowTitle(alias);
}
Status s = f->getStatus();
contactListWidget->moveWidget(friendWidget, s);
int criteria = getFilterCriteria();
bool filter = s == Status::Offline ?
filterOffline(criteria) : filterOnline(criteria);
bool filter = s == Status::Offline ? filterOffline(criteria)
: filterOnline(criteria);
friendWidget->searchName(ui->searchContactText->text(), filter);
ChatForm* friendForm = f->getChatForm();
friendForm->setName(alias);
for (Group *g : GroupList::getAllGroups())
{
g->regeneratePeerList();
}
}
void Widget::onChatroomWidgetClicked(GenericChatroomWidget *widget, bool group)

View File

@ -132,7 +132,7 @@ public slots:
void onFriendStatusChanged(int friendId, Status status);
void onFriendStatusMessageChanged(int friendId, const QString& message);
void onFriendUsernameChanged(int friendId, const QString& username);
void onFriendDisplayChanged(FriendWidget* friendWidget, Status s);
void onFriendAliasChanged(uint32_t friendId, const QString& alias);
void onFriendMessageReceived(int friendId, const QString& message, bool isAction);
void onFriendRequestReceived(const ToxPk& friendPk, const QString& message);
void updateFriendActivity(Friend* frnd);