1
0
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:
Tux3 / Mlkj / !Lev.uXFMLA 2014-09-29 20:32:54 +02:00
commit c266c317dd
21 changed files with 83 additions and 44 deletions

View File

@ -15,9 +15,9 @@
*/
#include "core.h"
#include "cdata.h"
#include "cstring.h"
#include "settings.h"
#include "misc/cdata.h"
#include "misc/cstring.h"
#include "misc/settings.h"
#include "widget/widget.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)
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);
}
@ -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,
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);
}
}

View File

@ -29,7 +29,7 @@
uint FileTransferInstance::Idconter = 0;
FileTransferInstance::FileTransferInstance(ToxFile File)
: lastUpdate{QDateTime::currentDateTime()}, lastBytesSent{0},
: lastBytesSent{0},
fileNum{File.fileNum}, friendId{File.friendId}, direction{File.direction}
{
id = Idconter++;
@ -77,16 +77,16 @@ void FileTransferInstance::onFileTransferInfo(int FriendId, int FileNum, int64_t
// state = tsProcessing;
QDateTime newtime = QDateTime::currentDateTime();
int timediff = lastUpdate.secsTo(newtime);
int timediff = started.secsTo(newtime);
if (timediff <= 0)
return;
qint64 diff = BytesSent - lastBytesSent;
if (diff < 0)
qint64 totalbytes = BytesSent + lastBytesSent; // bytes sent so far
if (totalbytes < 0)
{
qWarning() << "FileTransferInstance::onFileTransferInfo: Negative transfer speed !";
diff = 0;
totalbytes = 0;
}
long rawspeed = diff / timediff;
long rawspeed = totalbytes / timediff;
speed = getHumanReadableSize(rawspeed)+"/s";
size = getHumanReadableSize(Filesize);
totalBytes = Filesize;
@ -96,8 +96,7 @@ void FileTransferInstance::onFileTransferInfo(int FriendId, int FileNum, int64_t
QTime etaTime(0,0);
etaTime = etaTime.addSecs(etaSecs);
eta = etaTime.toString("mm:ss");
lastUpdate = newtime;
lastBytesSent = BytesSent;
lastBytesSent = totalbytes;
emit stateUpdated();
}
@ -202,7 +201,7 @@ void FileTransferInstance::acceptRecvRequest()
QString path;
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())
return;
else
@ -222,6 +221,8 @@ void FileTransferInstance::acceptRecvRequest()
Core::getInstance()->acceptFileRecvRequest(friendId, fileNum, path);
state = tsProcessing;
started = QDateTime::currentDateTime();
emit stateUpdated();
}

View File

@ -74,7 +74,7 @@ private:
QImage pic;
QString filename, size, speed, eta;
QString filenameElided;
QDateTime lastUpdate;
QDateTime started;
long long lastBytesSent, totalBytes;
int fileNum;
int friendId;

View File

@ -15,7 +15,7 @@
*/
#include "widget/widget.h"
#include "settings.h"
#include "misc/settings.h"
#include <QApplication>
#include <QFontDatabase>
#include <QTranslator>

View File

@ -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;

View File

@ -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,

View File

@ -91,17 +91,17 @@ HEADERS += widget/form/addfriendform.h \
friend.h \
group.h \
grouplist.h \
settings.h \
misc/settings.h \
core.h \
friendlist.h \
cdata.h \
cstring.h \
misc/cdata.h \
misc/cstring.h \
widget/selfcamview.h \
widget/camera.h \
widget/netcamview.h \
smileypack.h \
misc/smileypack.h \
widget/emoticonswidget.h \
style.h \
misc/style.h \
widget/adjustingscrollarea.h \
widget/croppinglabel.h \
widget/friendlistwidget.h \
@ -135,15 +135,15 @@ SOURCES += \
group.cpp \
grouplist.cpp \
main.cpp \
settings.cpp \
cdata.cpp \
cstring.cpp \
misc/settings.cpp \
misc/cdata.cpp \
misc/cstring.cpp \
widget/selfcamview.cpp \
widget/camera.cpp \
widget/netcamview.cpp \
smileypack.cpp \
misc/smileypack.cpp \
widget/emoticonswidget.cpp \
style.cpp \
misc/style.cpp \
widget/adjustingscrollarea.cpp \
widget/croppinglabel.cpp \
widget/friendlistwidget.cpp \

View File

@ -15,8 +15,8 @@
*/
#include "emoticonswidget.h"
#include "smileypack.h"
#include "style.h"
#include "misc/smileypack.h"
#include "misc/style.h"
#include <QPushButton>
#include <QRadioButton>

View File

@ -17,11 +17,11 @@
#include "genericchatform.h"
#include "ui_mainwindow.h"
#include <QFileDialog>
#include "smileypack.h"
#include "misc/smileypack.h"
#include "widget/emoticonswidget.h"
#include "style.h"
#include "misc/style.h"
#include "widget/widget.h"
#include "settings.h"
#include "misc/settings.h"
#include "widget/tool/chatactions/messageaction.h"
#include "widget/tool/chatactions/systemmessageaction.h"
#include "widget/chatareawidget.h"

View File

@ -17,7 +17,7 @@
#include "groupwidget.h"
#include "grouplist.h"
#include "group.h"
#include "settings.h"
#include "misc/settings.h"
#include "widget/form/groupchatform.h"
#include "widget/maskablepixmapwidget.h"
#include <QPalette>

View File

@ -1,10 +1,10 @@
#include "settingsdialog.h"
#include "settings.h"
#include "misc/settings.h"
#include "widget.h"
#include "camera.h"
#include "selfcamview.h"
#include "core.h"
#include "smileypack.h"
#include "misc/smileypack.h"
#include <QListWidget>
#include <QListWidgetItem>

View File

@ -15,7 +15,7 @@
*/
#include "messageaction.h"
#include "smileypack.h"
#include "misc/smileypack.h"
MessageAction::MessageAction(const QString &author, const QString &message, const QString &date, const bool &me) :
ChatAction(me, author, date),

View File

@ -17,7 +17,7 @@
#include "widget.h"
#include "ui_mainwindow.h"
#include "core.h"
#include "settings.h"
#include "misc/settings.h"
#include "friend.h"
#include "friendlist.h"
#include "widget/tool/friendrequestdialog.h"
@ -26,7 +26,7 @@
#include "group.h"
#include "widget/groupwidget.h"
#include "widget/form/groupchatform.h"
#include "style.h"
#include "misc/style.h"
#include "selfcamview.h"
#include "widget/friendlistwidget.h"
#include "camera.h"