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:
parent
84f56b81a7
commit
940cdfebba
28
core.cpp
28
core.cpp
|
@ -280,7 +280,7 @@ void Core::onUserStatusChanged(Tox*/* tox*/, int friendId, uint8_t userstatus, v
|
|||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
@ -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,
|
||||
uint8_t *, void* core)
|
||||
uint8_t* hash, void* _core)
|
||||
{
|
||||
qDebug() << "Core: Got avatar info from "<<friendnumber
|
||||
<<": format "<<format;
|
||||
Core* core = static_cast<Core*>(_core);
|
||||
|
||||
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
|
||||
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,
|
||||
uint8_t *, uint8_t *data, uint32_t datalen, void *core)
|
||||
uint8_t *hash, uint8_t *data, uint32_t datalen, void *core)
|
||||
{
|
||||
QPixmap pic;
|
||||
pic.loadFromData((uchar*)data, datalen);
|
||||
if (pic.isNull())
|
||||
qDebug() << "Core: Got invalid avatar data from "<<friendnumber;
|
||||
qDebug() << "Core: Got null avatar from "<<friendnumber;
|
||||
else
|
||||
{
|
||||
qDebug() << "Core: Got avatar data from "<<friendnumber<<", size:"<<pic.size();
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
25
settings.cpp
25
settings.cpp
|
@ -280,12 +280,35 @@ QPixmap Settings::getSavedAvatar(const QString &ownerId)
|
|||
void Settings::saveAvatar(QPixmap& pic, const QString& ownerId)
|
||||
{
|
||||
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)
|
||||
QString filePath = dir.filePath("avatars/"+ownerId.left(64)+".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
|
||||
{
|
||||
return dhtServerList;
|
||||
|
|
|
@ -61,6 +61,9 @@ public:
|
|||
QPixmap getSavedAvatar(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
|
||||
// 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,
|
||||
|
|
Loading…
Reference in New Issue
Block a user