From f82f57ec91884ee1f8aa4c20966b75b78defcac7 Mon Sep 17 00:00:00 2001 From: sudden6 Date: Fri, 29 Sep 2017 00:32:59 +0200 Subject: [PATCH] refactor: don't pass ToxPk as QString --- src/core/corefile.cpp | 9 ++--- src/model/about/aboutfriend.cpp | 2 +- src/persistence/profile.cpp | 66 +++++++++++++++++---------------- src/persistence/profile.h | 14 +++---- src/video/groupnetcamview.cpp | 2 +- src/video/netcamview.cpp | 4 +- src/widget/form/profileform.cpp | 2 +- src/widget/widget.cpp | 4 +- 8 files changed, 52 insertions(+), 51 deletions(-) diff --git a/src/core/corefile.cpp b/src/core/corefile.cpp index 80a8d5ca2..64d0e1002 100644 --- a/src/core/corefile.cpp +++ b/src/core/corefile.cpp @@ -280,20 +280,19 @@ void CoreFile::onFileReceiveCallback(Tox*, uint32_t friendId, uint32_t fileId, u Core* core = static_cast(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 { diff --git a/src/model/about/aboutfriend.cpp b/src/model/about/aboutfriend.cpp index 6d5f97add..2ee40054e 100644 --- a/src/model/about/aboutfriend.cpp +++ b/src/model/about/aboutfriend.cpp @@ -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; diff --git a/src/persistence/profile.cpp b/src/persistence/profile.cpp index fcd4bf758..ce3a8a793 100644 --- a/src/persistence/profile.cpp +++ b/src/persistence/profile.cpp @@ -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 friendList = core->getFriendList(); QVectorIterator 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; diff --git a/src/persistence/profile.h b/src/persistence/profile.h index d543ba21a..9496a6578 100644 --- a/src/persistence/profile.h +++ b/src/persistence/profile.h @@ -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 getFilesByExt(QString extension); - QString avatarPath(const QString& ownerId, bool forceUnencrypted = false); + QString avatarPath(const ToxPk& owner, bool forceUnencrypted = false); private: Core* core; diff --git a/src/video/groupnetcamview.cpp b/src/video/groupnetcamview.cpp index 227ce7ed7..ea0fe1c41 100644 --- a/src/video/groupnetcamview.cpp +++ b/src/video/groupnetcamview.cpp @@ -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); diff --git a/src/video/netcamview.cpp b/src/video/netcamview.cpp index 1575d9406..078ff0b42 100644 --- a/src/video/netcamview.cpp +++ b/src/video/netcamview.cpp @@ -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); diff --git a/src/widget/form/profileform.cpp b/src/widget/form/profileform.cpp index 6c8528fd6..e725fde30 100644 --- a/src/widget/form/profileform.cpp +++ b/src/widget/form/profileform.cpp @@ -304,7 +304,7 @@ void ProfileForm::onAvatarClicked() return; } - Nexus::getProfile()->setAvatar(bytes, core->getSelfPublicKey().toString()); + Nexus::getProfile()->setAvatar(bytes, core->getSelfPublicKey()); } void ProfileForm::onRenameClicked() diff --git a/src/widget/widget.cpp b/src/widget/widget.cpp index b8df26467..9a99b6574 100644 --- a/src/widget/widget.cpp +++ b/src/widget/widget.cpp @@ -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); }