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

implement avatar hash caching instead of getting full avatar each time

This commit is contained in:
dubslow 2014-09-28 20:54:28 -05:00
parent 84f56b81a7
commit 940cdfebba
3 changed files with 47 additions and 9 deletions

View File

@ -280,7 +280,7 @@ void Core::onUserStatusChanged(Tox*/* tox*/, int friendId, uint8_t userstatus, v
} }
if (status == Status::Online || status == Status::Away) if (status == Status::Online || status == Status::Away)
tox_request_avatar_data(static_cast<Core*>(core)->tox, friendId); tox_request_avatar_info(static_cast<Core*>(core)->tox, friendId);
emit static_cast<Core*>(core)->friendStatusChanged(friendId, status); emit static_cast<Core*>(core)->friendStatusChanged(friendId, status);
} }
@ -453,28 +453,40 @@ void Core::onFileDataCallback(Tox*, int32_t friendnumber, uint8_t filenumber, co
} }
void Core::onAvatarInfoCallback(Tox*, int32_t friendnumber, uint8_t format, void Core::onAvatarInfoCallback(Tox*, int32_t friendnumber, uint8_t format,
uint8_t *, void* core) uint8_t* hash, void* _core)
{ {
qDebug() << "Core: Got avatar info from "<<friendnumber Core* core = static_cast<Core*>(_core);
<<": format "<<format;
if (format == TOX_AVATAR_FORMAT_NONE) if (format == TOX_AVATAR_FORMAT_NONE)
emit static_cast<Core*>(core)->friendAvatarRemoved(friendnumber); {
qDebug() << "Core: Got null avatar info from" << friendnumber;
emit core->friendAvatarRemoved(friendnumber);
}
else else
tox_request_avatar_data(static_cast<Core*>(core)->tox, friendnumber); {
QByteArray oldHash = Settings::getInstance().getAvatarHash(core->getFriendAddress(friendnumber));
if (QByteArray((char*)hash, TOX_HASH_LENGTH) != oldHash) // comparison failed miserably if I didn't convert hash to QByteArray
{
qDebug() << "Core: got different avatar hash from" << friendnumber;
tox_request_avatar_data(core->tox, friendnumber);
}
else
qDebug() << "Core: Got old avatar info from" << friendnumber;
}
} }
void Core::onAvatarDataCallback(Tox*, int32_t friendnumber, uint8_t, void Core::onAvatarDataCallback(Tox*, int32_t friendnumber, uint8_t,
uint8_t *, uint8_t *data, uint32_t datalen, void *core) uint8_t *hash, uint8_t *data, uint32_t datalen, void *core)
{ {
QPixmap pic; QPixmap pic;
pic.loadFromData((uchar*)data, datalen); pic.loadFromData((uchar*)data, datalen);
if (pic.isNull()) if (pic.isNull())
qDebug() << "Core: Got invalid avatar data from "<<friendnumber; qDebug() << "Core: Got null avatar from "<<friendnumber;
else else
{ {
qDebug() << "Core: Got avatar data from "<<friendnumber<<", size:"<<pic.size(); qDebug() << "Core: Got avatar data from "<<friendnumber<<", size:"<<pic.size();
Settings::getInstance().saveAvatar(pic, static_cast<Core*>(core)->getFriendAddress(friendnumber)); Settings::getInstance().saveAvatar(pic, static_cast<Core*>(core)->getFriendAddress(friendnumber));
Settings::getInstance().saveAvatarHash(QByteArray((char*)hash, TOX_HASH_LENGTH), static_cast<Core*>(core)->getFriendAddress(friendnumber));
emit static_cast<Core*>(core)->friendAvatarChanged(friendnumber, pic); emit static_cast<Core*>(core)->friendAvatarChanged(friendnumber, pic);
} }
} }

View File

@ -280,12 +280,35 @@ QPixmap Settings::getSavedAvatar(const QString &ownerId)
void Settings::saveAvatar(QPixmap& pic, const QString& ownerId) void Settings::saveAvatar(QPixmap& pic, const QString& ownerId)
{ {
QDir dir(getSettingsDirPath()); QDir dir(getSettingsDirPath());
dir.mkdir("avatars/"); // remove this in a week or two hopefully dir.mkdir("avatars/");
// ignore nospam (good idea, and also the addFriend funcs which call getAvatar don't have it) // ignore nospam (good idea, and also the addFriend funcs which call getAvatar don't have it)
QString filePath = dir.filePath("avatars/"+ownerId.left(64)+".png"); QString filePath = dir.filePath("avatars/"+ownerId.left(64)+".png");
pic.save(filePath, "png"); pic.save(filePath, "png");
} }
void Settings::saveAvatarHash(const QByteArray& hash, const QString& ownerId)
{
QDir dir(getSettingsDirPath());
dir.mkdir("avatars/");
QFile file(dir.filePath("avatars/"+ownerId.left(64)+".hash"));
if (!file.open(QIODevice::WriteOnly))
return;
file.write(hash);
file.close();
}
QByteArray Settings::getAvatarHash(const QString& ownerId)
{
QDir dir(getSettingsDirPath());
dir.mkdir("avatars/");
QFile file(dir.filePath("avatars/"+ownerId.left(64)+".hash"));
if (!file.open(QIODevice::ReadOnly))
return QByteArray();
QByteArray out = file.readAll();
file.close();
return out;
}
const QList<Settings::DhtServer>& Settings::getDhtServerList() const const QList<Settings::DhtServer>& Settings::getDhtServerList() const
{ {
return dhtServerList; return dhtServerList;

View File

@ -61,6 +61,9 @@ public:
QPixmap getSavedAvatar(const QString& ownerId); QPixmap getSavedAvatar(const QString& ownerId);
void saveAvatar(QPixmap& pic, const QString& ownerId); void saveAvatar(QPixmap& pic, const QString& ownerId);
QByteArray getAvatarHash(const QString& ownerId);
void saveAvatarHash(const QByteArray& hash, const QString& ownerId);
// Assume all widgets have unique names // Assume all widgets have unique names
// Don't use it to save every single thing you want to save, use it // Don't use it to save every single thing you want to save, use it
// for some general purpose widgets, such as MainWindows or Splitters, // for some general purpose widgets, such as MainWindows or Splitters,