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

refactor(core): Move setAvatar to profile from core

This commit is contained in:
Diadlo 2017-05-06 22:59:46 +03:00
parent 4664c6249a
commit 65b1463933
No known key found for this signature in database
GPG Key ID: 5AF9F2E29107C727
9 changed files with 43 additions and 33 deletions

View File

@ -22,7 +22,6 @@
#include "corefile.h" #include "corefile.h"
#include "src/core/coreav.h" #include "src/core/coreav.h"
#include "src/core/toxstring.h" #include "src/core/toxstring.h"
#include "src/net/avatarbroadcaster.h"
#include "src/nexus.h" #include "src/nexus.h"
#include "src/persistence/profile.h" #include "src/persistence/profile.h"
#include "src/persistence/settings.h" #include "src/persistence/settings.h"
@ -315,13 +314,6 @@ void Core::start(const QByteArray& savedata)
tox_callback_file_recv_chunk(tox, CoreFile::onFileRecvChunkCallback); tox_callback_file_recv_chunk(tox, CoreFile::onFileRecvChunkCallback);
tox_callback_file_recv_control(tox, CoreFile::onFileControlCallback); tox_callback_file_recv_control(tox, CoreFile::onFileControlCallback);
QByteArray data = profile.loadAvatarData(getSelfPublicKey().toString());
if (data.isEmpty()) {
qDebug() << "Self avatar not found, will broadcast empty avatar to friends";
}
setAvatar(data);
ready = true; ready = true;
if (isNewProfile) { if (isNewProfile) {
@ -838,21 +830,6 @@ void Core::setUsername(const QString& username)
} }
} }
void Core::setAvatar(const QByteArray& data)
{
if (!data.isEmpty()) {
QPixmap pic;
pic.loadFromData(data);
profile.saveAvatar(data, getSelfPublicKey().toString());
emit selfAvatarChanged(pic);
} else {
emit selfAvatarChanged(QPixmap(":/img/contact_dark.svg"));
}
AvatarBroadcaster::setAvatar(data);
AvatarBroadcaster::enableAutoBroadcast();
}
/** /**
* @brief Returns our Tox ID * @brief Returns our Tox ID
*/ */

View File

@ -94,7 +94,6 @@ public slots:
void setStatus(Status status); void setStatus(Status status);
void setUsername(const QString& username); void setUsername(const QString& username);
void setStatusMessage(const QString& message); void setStatusMessage(const QString& message);
void setAvatar(const QByteArray& data);
int sendMessage(uint32_t friendId, const QString& message); int sendMessage(uint32_t friendId, const QString& message);
void sendGroupMessage(int groupId, const QString& message); void sendGroupMessage(int groupId, const QString& message);
@ -145,7 +144,6 @@ signals:
void statusMessageSet(const QString& message); void statusMessageSet(const QString& message);
void statusSet(Status status); void statusSet(Status status);
void idSet(const ToxId& id); void idSet(const ToxId& id);
void selfAvatarChanged(const QPixmap& pic);
void messageSentResult(uint32_t friendId, const QString& message, int messageId); void messageSentResult(uint32_t friendId, const QString& message, int messageId);
void groupSentResult(int groupId, const QString& message, int result); void groupSentResult(int groupId, const QString& message, int result);

View File

@ -198,6 +198,8 @@ void Nexus::showMainGUI()
GUI::setEnabled(false); GUI::setEnabled(false);
// Connections // Connections
connect(profile, &Profile::selfAvatarChanged, widget, &Widget::onSelfAvatarLoaded);
Core* core = profile->getCore(); Core* core = profile->getCore();
connect(core, &Core::requestSent, profile, &Profile::onRequestSent); connect(core, &Core::requestSent, profile, &Profile::onRequestSent);
@ -209,7 +211,6 @@ void Nexus::showMainGUI()
connect(core, &Core::statusSet, widget, &Widget::onStatusSet); connect(core, &Core::statusSet, widget, &Widget::onStatusSet);
connect(core, &Core::usernameSet, widget, &Widget::setUsername); connect(core, &Core::usernameSet, widget, &Widget::setUsername);
connect(core, &Core::statusMessageSet, widget, &Widget::setStatusMessage); connect(core, &Core::statusMessageSet, widget, &Widget::setStatusMessage);
connect(core, &Core::selfAvatarChanged, widget, &Widget::onSelfAvatarLoaded);
connect(core, &Core::friendAdded, widget, &Widget::addFriend); connect(core, &Core::friendAdded, widget, &Widget::addFriend);
connect(core, &Core::failedToAddFriend, widget, &Widget::addFriendFailed); connect(core, &Core::failedToAddFriend, widget, &Widget::addFriendFailed);
connect(core, &Core::friendUsernameChanged, widget, &Widget::onFriendUsernameChanged); connect(core, &Core::friendUsernameChanged, widget, &Widget::onFriendUsernameChanged);

View File

@ -32,6 +32,7 @@
#include "profilelocker.h" #include "profilelocker.h"
#include "settings.h" #include "settings.h"
#include "src/core/core.h" #include "src/core/core.h"
#include "src/net/avatarbroadcaster.h"
#include "src/nexus.h" #include "src/nexus.h"
#include "src/widget/gui.h" #include "src/widget/gui.h"
#include "src/widget/widget.h" #include "src/widget/widget.h"
@ -65,7 +66,17 @@ Profile::Profile(QString name, const QString& password, bool isNewProfile, const
[this, password](const ToxId& id) { loadDatabase(id, password); }, [this, password](const ToxId& id) { loadDatabase(id, password); },
Qt::QueuedConnection); Qt::QueuedConnection);
core->moveToThread(coreThread); core->moveToThread(coreThread);
QObject::connect(coreThread, &QThread::started, core, [=]() { core->start(toxsave); }); QObject::connect(coreThread, &QThread::started, core, [=]() {
core->start(toxsave);
QString selfPk = core->getSelfPublicKey().toString();
QByteArray data = loadAvatarData(selfPk);
if (data.isEmpty()) {
qDebug() << "Self avatar not found, will broadcast empty avatar to friends";
}
setAvatar(data, core->getSelfPublicKey().toString());
});
} }
/** /**
@ -439,6 +450,22 @@ void Profile::loadDatabase(const ToxId& id, QString password)
} }
} }
void Profile::setAvatar(QByteArray pic, const QString& ownerId)
{
QPixmap pixmap;
if (!pic.isEmpty()) {
pixmap.loadFromData(pic);
} else {
pixmap.load(":/img/contact_dark.svg");
}
saveAvatar(pic, ownerId);
emit selfAvatarChanged(pixmap);
AvatarBroadcaster::setAvatar(pic);
AvatarBroadcaster::enableAutoBroadcast();
}
/** /**
* @brief Adds history message about friendship request attempt if history is enabled * @brief Adds history message about friendship request attempt if history is enabled
* @param friendPk Pk of a friend which request is destined to * @param friendPk Pk of a friend which request is destined to
@ -530,8 +557,9 @@ History* Profile::getHistory()
void Profile::removeAvatar(const QString& ownerId) void Profile::removeAvatar(const QString& ownerId)
{ {
QFile::remove(avatarPath(ownerId)); QFile::remove(avatarPath(ownerId));
if (ownerId == core->getSelfId().getPublicKey().toString()) if (ownerId == core->getSelfId().getPublicKey().toString()) {
core->setAvatar({}); setAvatar({}, core->getSelfPublicKey().toString());
}
} }
bool Profile::exists(QString name) bool Profile::exists(QString name)

View File

@ -61,6 +61,7 @@ public:
QPixmap loadAvatar(); QPixmap loadAvatar();
QPixmap loadAvatar(const QString& ownerId); QPixmap loadAvatar(const QString& ownerId);
QByteArray loadAvatarData(const QString& ownerId); QByteArray loadAvatarData(const QString& ownerId);
void setAvatar(QByteArray pic, const QString& ownerId);
void saveAvatar(QByteArray pic, const QString& ownerId); void saveAvatar(QByteArray pic, const QString& ownerId);
QByteArray getAvatarHash(const QString& ownerId); QByteArray getAvatarHash(const QString& ownerId);
void removeAvatar(const QString& ownerId); void removeAvatar(const QString& ownerId);
@ -80,6 +81,9 @@ public:
static bool isEncrypted(QString name); static bool isEncrypted(QString name);
static QString getDbPath(const QString& profileName); static QString getDbPath(const QString& profileName);
signals:
void selfAvatarChanged(const QPixmap& pixmap);
public slots: public slots:
void onRequestSent(const ToxPk& friendPk, const QString& message); void onRequestSent(const ToxPk& friendPk, const QString& message);

View File

@ -144,7 +144,7 @@ GroupNetCamView::GroupNetCamView(int group, QWidget* parent)
connect(timer, &QTimer::timeout, this, &GroupNetCamView::onUpdateActivePeer); connect(timer, &QTimer::timeout, this, &GroupNetCamView::onUpdateActivePeer);
timer->start(); timer->start();
connect(Core::getInstance(), &Core::selfAvatarChanged, [this](const QPixmap& pixmap) { connect(Nexus::getProfile(), &Profile::selfAvatarChanged, [this](const QPixmap& pixmap) {
selfVideoSurface->getVideoSurface()->setAvatar(pixmap); selfVideoSurface->getVideoSurface()->setAvatar(pixmap);
setActive(); setActive();
}); });

View File

@ -73,7 +73,7 @@ NetCamView::NetCamView(int friendId, QWidget* parent)
selfFrame->resetBoundary(boundingRect); selfFrame->resetBoundary(boundingRect);
}); });
connections += connect(Core::getInstance(), &Core::selfAvatarChanged, connections += connect(Nexus::getProfile(), &Profile::selfAvatarChanged,
[this](const QPixmap& pixmap) { selfVideoSurface->setAvatar(pixmap); }); [this](const QPixmap& pixmap) { selfVideoSurface->setAvatar(pixmap); });
connections += connect(Core::getInstance(), &Core::friendAvatarChanged, connections += connect(Core::getInstance(), &Core::friendAvatarChanged,

View File

@ -305,7 +305,7 @@ void ProfileForm::onAvatarClicked()
return; return;
} }
Nexus::getCore()->setAvatar(bytes); Nexus::getProfile()->setAvatar(bytes, core->getSelfPublicKey().toString());
} }
void ProfileForm::onRenameClicked() void ProfileForm::onRenameClicked()

View File

@ -232,6 +232,9 @@ void Widget::init()
// connect logout tray menu action // connect logout tray menu action
connect(actionLogout, &QAction::triggered, profileForm, &ProfileForm::onLogoutClicked); connect(actionLogout, &QAction::triggered, profileForm, &ProfileForm::onLogoutClicked);
Profile* profile = Nexus::getProfile();
connect(profile, &Profile::selfAvatarChanged, profileForm, &ProfileForm::onSelfAvatarLoaded);
const Settings& s = Settings::getInstance(); const Settings& s = Settings::getInstance();
Core* core = Nexus::getCore(); Core* core = Nexus::getCore();
CoreAV* av = core->getAv(); CoreAV* av = core->getAv();
@ -239,7 +242,6 @@ void Widget::init()
connect(core, &Core::fileDownloadFinished, filesForm, &FilesForm::onFileDownloadComplete); connect(core, &Core::fileDownloadFinished, filesForm, &FilesForm::onFileDownloadComplete);
connect(core, &Core::fileUploadFinished, filesForm, &FilesForm::onFileUploadComplete); connect(core, &Core::fileUploadFinished, filesForm, &FilesForm::onFileUploadComplete);
connect(core, &Core::selfAvatarChanged, profileForm, &ProfileForm::onSelfAvatarLoaded);
connect(ui->addButton, &QPushButton::clicked, this, &Widget::onAddClicked); connect(ui->addButton, &QPushButton::clicked, this, &Widget::onAddClicked);
connect(ui->groupButton, &QPushButton::clicked, this, &Widget::onGroupClicked); connect(ui->groupButton, &QPushButton::clicked, this, &Widget::onGroupClicked);
connect(ui->transferButton, &QPushButton::clicked, this, &Widget::onTransferClicked); connect(ui->transferButton, &QPushButton::clicked, this, &Widget::onTransferClicked);