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

refactor: don't pass ToxPk as QString

This commit is contained in:
sudden6 2017-09-29 00:32:59 +02:00
parent 61b36d1bce
commit f82f57ec91
No known key found for this signature in database
GPG Key ID: 279509B499E032B9
8 changed files with 52 additions and 51 deletions

View File

@ -280,20 +280,19 @@ void CoreFile::onFileReceiveCallback(Tox*, uint32_t friendId, uint32_t fileId, u
Core* core = static_cast<Core*>(vCore);
if (kind == TOX_FILE_KIND_AVATAR) {
// TODO: port this to ToxPk
QString friendAddr = core->getFriendPublicKey(friendId).toString();
const ToxPk friendPk = core->getFriendPublicKey(friendId);
if (!filesize) {
qDebug() << QString("Received empty avatar request %1:%2").arg(friendId).arg(fileId);
// Avatars of size 0 means explicitely no avatar
emit core->friendAvatarRemoved(friendId);
core->profile.removeAvatar(friendAddr);
core->profile.removeAvatar(friendPk);
return;
} else {
static_assert(TOX_HASH_LENGTH <= TOX_FILE_ID_LENGTH,
"TOX_HASH_LENGTH > TOX_FILE_ID_LENGTH!");
uint8_t avatarHash[TOX_FILE_ID_LENGTH];
tox_file_get_file_id(core->tox, friendId, fileId, avatarHash, nullptr);
if (core->profile.getAvatarHash(friendAddr)
if (core->profile.getAvatarHash(friendPk)
== QByteArray((char*)avatarHash, TOX_HASH_LENGTH)) {
// If it's an avatar but we already have it cached, cancel
qDebug() << QString(
@ -429,7 +428,7 @@ void CoreFile::onFileRecvChunkCallback(Tox* tox, uint32_t friendId, uint32_t fil
if (!pic.isNull()) {
qDebug() << "Got" << file->avatarData.size() << "bytes of avatar data from" << friendId;
core->profile.saveAvatar(file->avatarData,
core->getFriendPublicKey(friendId).toString());
core->getFriendPublicKey(friendId));
emit core->friendAvatarChanged(friendId, pic);
}
} else {

View File

@ -42,7 +42,7 @@ QString AboutFriend::getPublicKey() const
QPixmap AboutFriend::getAvatar() const
{
const QString pk = f->getPublicKey().toString();
const ToxPk pk = f->getPublicKey();
const QPixmap avatar = Nexus::getProfile()->loadAvatar(pk);
return avatar.isNull() ? QPixmap(QStringLiteral(":/img/contact_dark.svg"))
: avatar;

View File

@ -69,13 +69,13 @@ Profile::Profile(QString name, const QString& password, bool isNewProfile, const
QObject::connect(coreThread, &QThread::started, core, [=]() {
core->start(toxsave);
QString selfPk = core->getSelfPublicKey().toString();
const ToxPk selfPk= core->getSelfPublicKey();
QByteArray data = loadAvatarData(selfPk);
if (data.isEmpty()) {
qDebug() << "Self avatar not found, will broadcast empty avatar to friends";
}
setAvatar(data, core->getSelfPublicKey().toString());
setAvatar(data, selfPk);
});
}
@ -350,17 +350,19 @@ void Profile::saveToxSave(QByteArray data)
/**
* @brief Gets the path of the avatar file cached by this profile and corresponding to this owner
* ID.
* @param ownerId Path to avatar of friend with this ID will returned.
* @param owner Path to avatar of friend with this PK will returned.
* @param forceUnencrypted If true, return the path to the plaintext file even if this is an
* encrypted profile.
* @return Path to the avatar.
*/
QString Profile::avatarPath(const QString& ownerId, bool forceUnencrypted)
QString Profile::avatarPath(const ToxPk& owner, bool forceUnencrypted)
{
if (!encrypted || forceUnencrypted)
return Settings::getInstance().getSettingsDirPath() + "avatars/" + ownerId + ".png";
QString ownerStr = owner.toString();
if (!encrypted || forceUnencrypted) {
return Settings::getInstance().getSettingsDirPath() + "avatars/" + ownerStr + ".png";
}
QByteArray idData = ownerId.toUtf8();
QByteArray idData = ownerStr.toUtf8();
QByteArray pubkeyData = core->getSelfId().getPublicKey().getKey();
constexpr int hashSize = TOX_PUBLIC_KEY_SIZE;
static_assert(hashSize >= crypto_generichash_BYTES_MIN && hashSize <= crypto_generichash_BYTES_MAX,
@ -381,34 +383,34 @@ QString Profile::avatarPath(const QString& ownerId, bool forceUnencrypted)
*/
QPixmap Profile::loadAvatar()
{
return loadAvatar(core->getSelfId().getPublicKey().toString());
return loadAvatar(core->getSelfId().getPublicKey());
}
/**
* @brief Get a contact's avatar from cache.
* @param ownerId Friend ID to load avatar.
* @param owner Friend PK to load avatar.
* @return Avatar as QPixmap.
*/
QPixmap Profile::loadAvatar(const QString& ownerId)
QPixmap Profile::loadAvatar(const ToxPk &owner)
{
QPixmap pic;
pic.loadFromData(loadAvatarData(ownerId));
pic.loadFromData(loadAvatarData(owner));
return pic;
}
/**
* @brief Get a contact's avatar from cache.
* @param ownerId Friend ID to load avatar.
* @param owner Friend PK to load avatar.
* @return Avatar as QByteArray.
*/
QByteArray Profile::loadAvatarData(const QString& ownerId)
QByteArray Profile::loadAvatarData(const ToxPk& owner)
{
QString path = avatarPath(ownerId);
QString path = avatarPath(owner);
bool avatarEncrypted = encrypted;
// If the encrypted avatar isn't found, try loading the unencrypted one for the same ID
if (avatarEncrypted && !QFile::exists(path)) {
avatarEncrypted = false;
path = avatarPath(ownerId, true);
path = avatarPath(owner, true);
}
QFile file(path);
@ -450,7 +452,7 @@ void Profile::loadDatabase(const ToxId& id, QString password)
}
}
void Profile::setAvatar(QByteArray pic, const QString& ownerId)
void Profile::setAvatar(QByteArray pic, const ToxPk& owner)
{
QPixmap pixmap;
if (!pic.isEmpty()) {
@ -459,7 +461,7 @@ void Profile::setAvatar(QByteArray pic, const QString& ownerId)
pixmap.load(":/img/contact_dark.svg");
}
saveAvatar(pic, ownerId);
saveAvatar(pic, owner);
emit selfAvatarChanged(pixmap);
AvatarBroadcaster::setAvatar(pic);
@ -487,15 +489,15 @@ void Profile::onRequestSent(const ToxPk& friendPk, const QString& message)
/**
* @brief Save an avatar to cache.
* @param pic Picture to save.
* @param ownerId ID of avatar owner.
* @param owner PK of avatar owner.
*/
void Profile::saveAvatar(QByteArray pic, const QString& ownerId)
void Profile::saveAvatar(QByteArray pic, const ToxPk &owner)
{
if (encrypted && !pic.isEmpty()) {
pic = passkey->encrypt(pic);
}
QString path = avatarPath(ownerId);
QString path = avatarPath(owner);
QDir(Settings::getInstance().getSettingsDirPath()).mkdir("avatars");
if (pic.isEmpty()) {
QFile::remove(path);
@ -512,12 +514,12 @@ void Profile::saveAvatar(QByteArray pic, const QString& ownerId)
/**
* @brief Get the tox hash of a cached avatar.
* @param ownerId Friend ID to get hash.
* @param owner Friend PK to get hash.
* @return Avatar tox hash.
*/
QByteArray Profile::getAvatarHash(const QString& ownerId)
QByteArray Profile::getAvatarHash(const ToxPk& owner)
{
QByteArray pic = loadAvatarData(ownerId);
QByteArray pic = loadAvatarData(owner);
QByteArray avatarHash(TOX_HASH_LENGTH, 0);
tox_hash((uint8_t*)avatarHash.data(), (uint8_t*)pic.data(), pic.size());
return avatarHash;
@ -528,7 +530,7 @@ QByteArray Profile::getAvatarHash(const QString& ownerId)
*/
void Profile::removeAvatar()
{
removeAvatar(core->getSelfId().getPublicKey().toString());
removeAvatar(core->getSelfId().getPublicKey());
}
/**
@ -552,13 +554,13 @@ History* Profile::getHistory()
/**
* @brief Removes a cached avatar.
* @param ownerId Friend ID whose avater to delete.
* @param owner Friend PK whose avater to delete.
*/
void Profile::removeAvatar(const QString& ownerId)
void Profile::removeAvatar(const ToxPk& owner)
{
QFile::remove(avatarPath(ownerId));
if (ownerId == core->getSelfId().getPublicKey().toString()) {
setAvatar({}, core->getSelfPublicKey().toString());
QFile::remove(avatarPath(owner));
if (owner == core->getSelfId().getPublicKey()) {
setAvatar({}, core->getSelfPublicKey());
}
}
@ -741,13 +743,13 @@ QString Profile::setPassword(const QString& newPassword)
Nexus::getDesktopGUI()->reloadHistory();
QByteArray avatar = loadAvatarData(core->getSelfId().getPublicKey().toString());
saveAvatar(avatar, core->getSelfId().getPublicKey().toString());
QByteArray avatar = loadAvatarData(core->getSelfId().getPublicKey());
saveAvatar(avatar, core->getSelfId().getPublicKey());
QVector<uint32_t> friendList = core->getFriendList();
QVectorIterator<uint32_t> i(friendList);
while (i.hasNext()) {
QString friendPublicKey = core->getFriendPublicKey(i.next()).toString();
const ToxPk friendPublicKey = core->getFriendPublicKey(i.next());
saveAvatar(loadAvatarData(friendPublicKey), friendPublicKey);
}
return error;

View File

@ -59,12 +59,12 @@ public:
void saveToxSave(QByteArray data);
QPixmap loadAvatar();
QPixmap loadAvatar(const QString& ownerId);
QByteArray loadAvatarData(const QString& ownerId);
void setAvatar(QByteArray pic, const QString& ownerId);
void saveAvatar(QByteArray pic, const QString& ownerId);
QByteArray getAvatarHash(const QString& ownerId);
void removeAvatar(const QString& ownerId);
QPixmap loadAvatar(const ToxPk& owner);
QByteArray loadAvatarData(const ToxPk& owner);
void setAvatar(QByteArray pic, const ToxPk& owner);
void saveAvatar(QByteArray pic, const ToxPk& owner);
QByteArray getAvatarHash(const ToxPk& owner);
void removeAvatar(const ToxPk& owner);
void removeAvatar();
bool isHistoryEnabled();
@ -93,7 +93,7 @@ private slots:
private:
Profile(QString name, const QString& password, bool newProfile, const QByteArray& toxsave);
static QVector<QString> getFilesByExt(QString extension);
QString avatarPath(const QString& ownerId, bool forceUnencrypted = false);
QString avatarPath(const ToxPk& owner, bool forceUnencrypted = false);
private:
Core* core;

View File

@ -169,7 +169,7 @@ void GroupNetCamView::clearPeers()
void GroupNetCamView::addPeer(int peer, const QString& name)
{
QPixmap groupAvatar =
Nexus::getProfile()->loadAvatar(Core::getInstance()->getGroupPeerPk(group, peer).toString());
Nexus::getProfile()->loadAvatar(Core::getInstance()->getGroupPeerPk(group, peer));
LabeledVideo* labeledVideo = new LabeledVideo(groupAvatar, this);
labeledVideo->setText(name);
horLayout->insertWidget(horLayout->count() - 1, labeledVideo);

View File

@ -37,8 +37,8 @@ NetCamView::NetCamView(int friendId, QWidget* parent)
, friendId{friendId}
, e(false)
{
QString id = FriendList::findFriend(friendId)->getPublicKey().toString();
videoSurface = new VideoSurface(Nexus::getProfile()->loadAvatar(id), this);
const ToxPk pk = FriendList::findFriend(friendId)->getPublicKey();
videoSurface = new VideoSurface(Nexus::getProfile()->loadAvatar(pk), this);
videoSurface->setMinimumHeight(256);
videoSurface->setContentsMargins(6, 6, 6, 6);

View File

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

View File

@ -1007,7 +1007,7 @@ void Widget::addFriend(int friendId, const ToxPk& friendPk)
connect(core, &Core::friendAvatarRemoved, widget, &FriendWidget::onAvatarRemoved);
// Try to get the avatar from the cache
QPixmap avatar = Nexus::getProfile()->loadAvatar(friendPk.toString());
QPixmap avatar = Nexus::getProfile()->loadAvatar(friendPk);
if (!avatar.isNull()) {
friendForm->onAvatarChange(friendId, avatar);
widget->onAvatarChange(friendId, avatar);
@ -1249,7 +1249,7 @@ void Widget::addFriendDialog(const Friend* frnd, ContentDialog* dialog)
connect(core, &Core::friendAvatarChanged, friendWidget, &FriendWidget::onAvatarChange);
connect(core, &Core::friendAvatarRemoved, friendWidget, &FriendWidget::onAvatarRemoved);
QPixmap avatar = Nexus::getProfile()->loadAvatar(frnd->getPublicKey().toString());
QPixmap avatar = Nexus::getProfile()->loadAvatar(frnd->getPublicKey());
if (!avatar.isNull()) {
friendWidget->onAvatarChange(frnd->getId(), avatar);
}