mirror of
https://github.com/qTox/qTox.git
synced 2024-03-22 14:00:36 +08:00
Merge pull request #342 from dubslow/avatarhash
implement avatar hash caching instead of getting full avatar each time
This commit is contained in:
commit
c266c317dd
34
core.cpp
34
core.cpp
|
@ -15,9 +15,9 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "core.h"
|
#include "core.h"
|
||||||
#include "cdata.h"
|
#include "misc/cdata.h"
|
||||||
#include "cstring.h"
|
#include "misc/cstring.h"
|
||||||
#include "settings.h"
|
#include "misc/settings.h"
|
||||||
#include "widget/widget.h"
|
#include "widget/widget.h"
|
||||||
|
|
||||||
#include <tox/tox.h>
|
#include <tox/tox.h>
|
||||||
|
@ -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);
|
||||||
}
|
}
|
||||||
|
@ -507,28 +507,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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,7 +29,7 @@
|
||||||
uint FileTransferInstance::Idconter = 0;
|
uint FileTransferInstance::Idconter = 0;
|
||||||
|
|
||||||
FileTransferInstance::FileTransferInstance(ToxFile File)
|
FileTransferInstance::FileTransferInstance(ToxFile File)
|
||||||
: lastUpdate{QDateTime::currentDateTime()}, lastBytesSent{0},
|
: lastBytesSent{0},
|
||||||
fileNum{File.fileNum}, friendId{File.friendId}, direction{File.direction}
|
fileNum{File.fileNum}, friendId{File.friendId}, direction{File.direction}
|
||||||
{
|
{
|
||||||
id = Idconter++;
|
id = Idconter++;
|
||||||
|
@ -77,16 +77,16 @@ void FileTransferInstance::onFileTransferInfo(int FriendId, int FileNum, int64_t
|
||||||
|
|
||||||
// state = tsProcessing;
|
// state = tsProcessing;
|
||||||
QDateTime newtime = QDateTime::currentDateTime();
|
QDateTime newtime = QDateTime::currentDateTime();
|
||||||
int timediff = lastUpdate.secsTo(newtime);
|
int timediff = started.secsTo(newtime);
|
||||||
if (timediff <= 0)
|
if (timediff <= 0)
|
||||||
return;
|
return;
|
||||||
qint64 diff = BytesSent - lastBytesSent;
|
qint64 totalbytes = BytesSent + lastBytesSent; // bytes sent so far
|
||||||
if (diff < 0)
|
if (totalbytes < 0)
|
||||||
{
|
{
|
||||||
qWarning() << "FileTransferInstance::onFileTransferInfo: Negative transfer speed !";
|
qWarning() << "FileTransferInstance::onFileTransferInfo: Negative transfer speed !";
|
||||||
diff = 0;
|
totalbytes = 0;
|
||||||
}
|
}
|
||||||
long rawspeed = diff / timediff;
|
long rawspeed = totalbytes / timediff;
|
||||||
speed = getHumanReadableSize(rawspeed)+"/s";
|
speed = getHumanReadableSize(rawspeed)+"/s";
|
||||||
size = getHumanReadableSize(Filesize);
|
size = getHumanReadableSize(Filesize);
|
||||||
totalBytes = Filesize;
|
totalBytes = Filesize;
|
||||||
|
@ -96,8 +96,7 @@ void FileTransferInstance::onFileTransferInfo(int FriendId, int FileNum, int64_t
|
||||||
QTime etaTime(0,0);
|
QTime etaTime(0,0);
|
||||||
etaTime = etaTime.addSecs(etaSecs);
|
etaTime = etaTime.addSecs(etaSecs);
|
||||||
eta = etaTime.toString("mm:ss");
|
eta = etaTime.toString("mm:ss");
|
||||||
lastUpdate = newtime;
|
lastBytesSent = totalbytes;
|
||||||
lastBytesSent = BytesSent;
|
|
||||||
emit stateUpdated();
|
emit stateUpdated();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -202,7 +201,7 @@ void FileTransferInstance::acceptRecvRequest()
|
||||||
QString path;
|
QString path;
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
path = QFileDialog::getSaveFileName(0, tr("Save a file","Title of the file saving dialog"), QDir::current().filePath(filename));
|
path = QFileDialog::getSaveFileName(0, tr("Save a file","Title of the file saving dialog"), QDir::home().filePath(filename));
|
||||||
if (path.isEmpty())
|
if (path.isEmpty())
|
||||||
return;
|
return;
|
||||||
else
|
else
|
||||||
|
@ -222,6 +221,8 @@ void FileTransferInstance::acceptRecvRequest()
|
||||||
Core::getInstance()->acceptFileRecvRequest(friendId, fileNum, path);
|
Core::getInstance()->acceptFileRecvRequest(friendId, fileNum, path);
|
||||||
state = tsProcessing;
|
state = tsProcessing;
|
||||||
|
|
||||||
|
started = QDateTime::currentDateTime();
|
||||||
|
|
||||||
emit stateUpdated();
|
emit stateUpdated();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -74,7 +74,7 @@ private:
|
||||||
QImage pic;
|
QImage pic;
|
||||||
QString filename, size, speed, eta;
|
QString filename, size, speed, eta;
|
||||||
QString filenameElided;
|
QString filenameElided;
|
||||||
QDateTime lastUpdate;
|
QDateTime started;
|
||||||
long long lastBytesSent, totalBytes;
|
long long lastBytesSent, totalBytes;
|
||||||
int fileNum;
|
int fileNum;
|
||||||
int friendId;
|
int friendId;
|
||||||
|
|
2
main.cpp
2
main.cpp
|
@ -15,7 +15,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "widget/widget.h"
|
#include "widget/widget.h"
|
||||||
#include "settings.h"
|
#include "misc/settings.h"
|
||||||
#include <QApplication>
|
#include <QApplication>
|
||||||
#include <QFontDatabase>
|
#include <QFontDatabase>
|
||||||
#include <QTranslator>
|
#include <QTranslator>
|
||||||
|
|
|
@ -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;
|
|
@ -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,
|
20
qtox.pro
20
qtox.pro
|
@ -91,17 +91,17 @@ HEADERS += widget/form/addfriendform.h \
|
||||||
friend.h \
|
friend.h \
|
||||||
group.h \
|
group.h \
|
||||||
grouplist.h \
|
grouplist.h \
|
||||||
settings.h \
|
misc/settings.h \
|
||||||
core.h \
|
core.h \
|
||||||
friendlist.h \
|
friendlist.h \
|
||||||
cdata.h \
|
misc/cdata.h \
|
||||||
cstring.h \
|
misc/cstring.h \
|
||||||
widget/selfcamview.h \
|
widget/selfcamview.h \
|
||||||
widget/camera.h \
|
widget/camera.h \
|
||||||
widget/netcamview.h \
|
widget/netcamview.h \
|
||||||
smileypack.h \
|
misc/smileypack.h \
|
||||||
widget/emoticonswidget.h \
|
widget/emoticonswidget.h \
|
||||||
style.h \
|
misc/style.h \
|
||||||
widget/adjustingscrollarea.h \
|
widget/adjustingscrollarea.h \
|
||||||
widget/croppinglabel.h \
|
widget/croppinglabel.h \
|
||||||
widget/friendlistwidget.h \
|
widget/friendlistwidget.h \
|
||||||
|
@ -135,15 +135,15 @@ SOURCES += \
|
||||||
group.cpp \
|
group.cpp \
|
||||||
grouplist.cpp \
|
grouplist.cpp \
|
||||||
main.cpp \
|
main.cpp \
|
||||||
settings.cpp \
|
misc/settings.cpp \
|
||||||
cdata.cpp \
|
misc/cdata.cpp \
|
||||||
cstring.cpp \
|
misc/cstring.cpp \
|
||||||
widget/selfcamview.cpp \
|
widget/selfcamview.cpp \
|
||||||
widget/camera.cpp \
|
widget/camera.cpp \
|
||||||
widget/netcamview.cpp \
|
widget/netcamview.cpp \
|
||||||
smileypack.cpp \
|
misc/smileypack.cpp \
|
||||||
widget/emoticonswidget.cpp \
|
widget/emoticonswidget.cpp \
|
||||||
style.cpp \
|
misc/style.cpp \
|
||||||
widget/adjustingscrollarea.cpp \
|
widget/adjustingscrollarea.cpp \
|
||||||
widget/croppinglabel.cpp \
|
widget/croppinglabel.cpp \
|
||||||
widget/friendlistwidget.cpp \
|
widget/friendlistwidget.cpp \
|
||||||
|
|
|
@ -15,8 +15,8 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "emoticonswidget.h"
|
#include "emoticonswidget.h"
|
||||||
#include "smileypack.h"
|
#include "misc/smileypack.h"
|
||||||
#include "style.h"
|
#include "misc/style.h"
|
||||||
|
|
||||||
#include <QPushButton>
|
#include <QPushButton>
|
||||||
#include <QRadioButton>
|
#include <QRadioButton>
|
||||||
|
|
|
@ -17,11 +17,11 @@
|
||||||
#include "genericchatform.h"
|
#include "genericchatform.h"
|
||||||
#include "ui_mainwindow.h"
|
#include "ui_mainwindow.h"
|
||||||
#include <QFileDialog>
|
#include <QFileDialog>
|
||||||
#include "smileypack.h"
|
#include "misc/smileypack.h"
|
||||||
#include "widget/emoticonswidget.h"
|
#include "widget/emoticonswidget.h"
|
||||||
#include "style.h"
|
#include "misc/style.h"
|
||||||
#include "widget/widget.h"
|
#include "widget/widget.h"
|
||||||
#include "settings.h"
|
#include "misc/settings.h"
|
||||||
#include "widget/tool/chatactions/messageaction.h"
|
#include "widget/tool/chatactions/messageaction.h"
|
||||||
#include "widget/tool/chatactions/systemmessageaction.h"
|
#include "widget/tool/chatactions/systemmessageaction.h"
|
||||||
#include "widget/chatareawidget.h"
|
#include "widget/chatareawidget.h"
|
||||||
|
|
|
@ -17,7 +17,7 @@
|
||||||
#include "groupwidget.h"
|
#include "groupwidget.h"
|
||||||
#include "grouplist.h"
|
#include "grouplist.h"
|
||||||
#include "group.h"
|
#include "group.h"
|
||||||
#include "settings.h"
|
#include "misc/settings.h"
|
||||||
#include "widget/form/groupchatform.h"
|
#include "widget/form/groupchatform.h"
|
||||||
#include "widget/maskablepixmapwidget.h"
|
#include "widget/maskablepixmapwidget.h"
|
||||||
#include <QPalette>
|
#include <QPalette>
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
#include "settingsdialog.h"
|
#include "settingsdialog.h"
|
||||||
#include "settings.h"
|
#include "misc/settings.h"
|
||||||
#include "widget.h"
|
#include "widget.h"
|
||||||
#include "camera.h"
|
#include "camera.h"
|
||||||
#include "selfcamview.h"
|
#include "selfcamview.h"
|
||||||
#include "core.h"
|
#include "core.h"
|
||||||
#include "smileypack.h"
|
#include "misc/smileypack.h"
|
||||||
|
|
||||||
#include <QListWidget>
|
#include <QListWidget>
|
||||||
#include <QListWidgetItem>
|
#include <QListWidgetItem>
|
||||||
|
|
|
@ -15,7 +15,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "messageaction.h"
|
#include "messageaction.h"
|
||||||
#include "smileypack.h"
|
#include "misc/smileypack.h"
|
||||||
|
|
||||||
MessageAction::MessageAction(const QString &author, const QString &message, const QString &date, const bool &me) :
|
MessageAction::MessageAction(const QString &author, const QString &message, const QString &date, const bool &me) :
|
||||||
ChatAction(me, author, date),
|
ChatAction(me, author, date),
|
||||||
|
|
|
@ -17,7 +17,7 @@
|
||||||
#include "widget.h"
|
#include "widget.h"
|
||||||
#include "ui_mainwindow.h"
|
#include "ui_mainwindow.h"
|
||||||
#include "core.h"
|
#include "core.h"
|
||||||
#include "settings.h"
|
#include "misc/settings.h"
|
||||||
#include "friend.h"
|
#include "friend.h"
|
||||||
#include "friendlist.h"
|
#include "friendlist.h"
|
||||||
#include "widget/tool/friendrequestdialog.h"
|
#include "widget/tool/friendrequestdialog.h"
|
||||||
|
@ -26,7 +26,7 @@
|
||||||
#include "group.h"
|
#include "group.h"
|
||||||
#include "widget/groupwidget.h"
|
#include "widget/groupwidget.h"
|
||||||
#include "widget/form/groupchatform.h"
|
#include "widget/form/groupchatform.h"
|
||||||
#include "style.h"
|
#include "misc/style.h"
|
||||||
#include "selfcamview.h"
|
#include "selfcamview.h"
|
||||||
#include "widget/friendlistwidget.h"
|
#include "widget/friendlistwidget.h"
|
||||||
#include "camera.h"
|
#include "camera.h"
|
||||||
|
|
Loading…
Reference in New Issue
Block a user