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

conform to standard, fix bug related to nospam in ID

This commit is contained in:
dubslow 2014-09-27 16:37:39 -05:00
parent 4b61c0f188
commit 28d2680fec
4 changed files with 40 additions and 18 deletions

View File

@ -210,7 +210,8 @@ void Core::start()
tox_get_address(tox, friendAddress); tox_get_address(tox, friendAddress);
emit friendAddressGenerated(CFriendAddress::toString(friendAddress)); emit friendAddressGenerated(CFriendAddress::toString(friendAddress));
QPixmap pic = Settings::getInstance().getSavedAvatar(getSelfId().toString()); QPixmap pic;
pic.load(QDir(Settings::getInstance().getSettingsDirPath()).filePath("avatar.png"));
if (!pic.isNull() && !pic.size().isEmpty()) if (!pic.isNull() && !pic.size().isEmpty())
{ {
QByteArray data; QByteArray data;
@ -218,12 +219,25 @@ void Core::start()
buffer.open(QIODevice::WriteOnly); buffer.open(QIODevice::WriteOnly);
pic.save(&buffer, "PNG"); pic.save(&buffer, "PNG");
buffer.close(); buffer.close();
if (tox_set_avatar(tox, TOX_AVATAR_FORMAT_PNG, (uint8_t*)data.constData(), data.size()) != 0) setAvatar(TOX_AVATAR_FORMAT_PNG, data);
qWarning() << "Core:start: Error setting avatar, size:"<<data.size(); }
emit selfAvatarChanged(pic); else
{
QPixmap pic = Settings::getInstance().getSavedAvatar(getSelfId().toString());
qDebug() << "self avatar missing, trying by id";
// this will leave a avatars/<selfid>.png duplicate of avatar.png, but whatever
if (!pic.isNull() && !pic.size().isEmpty())
{
QByteArray data;
QBuffer buffer(&data);
buffer.open(QIODevice::WriteOnly);
pic.save(&buffer, "PNG");
buffer.close();
setAvatar(TOX_AVATAR_FORMAT_PNG, data);
} }
else else
qDebug() << "Core: Error loading self avatar"; qDebug() << "Core: Error loading self avatar";
}
bootstrapDht(); bootstrapDht();
@ -476,7 +490,8 @@ void Core::onAvatarDataCallback(Tox*, int32_t friendnumber, uint8_t,
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).left(64));
// ignore nospam (good idea, and also the addFriend funcs which call getAvatar don't have it)
emit static_cast<Core*>(core)->friendAvatarChanged(friendnumber, pic); emit static_cast<Core*>(core)->friendAvatarChanged(friendnumber, pic);
} }
} }
@ -789,13 +804,10 @@ void Core::setAvatar(uint8_t format, const QByteArray& data)
QPixmap pic; QPixmap pic;
pic.loadFromData(data); pic.loadFromData(data);
Settings::getInstance().saveAvatar(pic, getSelfId().toString()); QString path = QDir(Settings::getInstance().getSettingsDirPath()).filePath("avatar.png");
pic.save(path, "png");
emit selfAvatarChanged(pic); emit selfAvatarChanged(pic);
// according to tox.h, we need not broadcast ourselves, that's done in tox_set_avatar
// Broadcast our new avatar!
const uint32_t friendCount = tox_count_friendlist(tox);;
for (unsigned i=0; i<friendCount; i++)
tox_send_avatar_info(tox, i);
} }
ToxID Core::getSelfId() ToxID Core::getSelfId()

View File

@ -27,7 +27,6 @@
#include <QList> #include <QList>
const QString Settings::FILENAME = "settings.ini"; const QString Settings::FILENAME = "settings.ini";
const QString Settings::AVATAR_FILENAME = "avatar.dat";
bool Settings::makeToxPortable{false}; bool Settings::makeToxPortable{false};
Settings::Settings() : Settings::Settings() :
@ -260,15 +259,27 @@ QString Settings::getSettingsDirPath()
QPixmap Settings::getSavedAvatar(const QString &ownerId) QPixmap Settings::getSavedAvatar(const QString &ownerId)
{ {
QString filePath = QDir(getSettingsDirPath()).filePath("avatar_"+ownerId); QDir dir(getSettingsDirPath());
QString filePath = dir.filePath("avatars/"+ownerId+".png");
QFileInfo info(filePath);
QPixmap pic; QPixmap pic;
if (!info.exists())
{
QString filePath = dir.filePath("avatar_"+ownerId);
pic.load(filePath);
saveAvatar(pic, ownerId);
QFile::remove(filePath);
}
else
pic.load(filePath); pic.load(filePath);
return pic; return pic;
} }
void Settings::saveAvatar(QPixmap& pic, const QString& ownerId) void Settings::saveAvatar(QPixmap& pic, const QString& ownerId)
{ {
QString filePath = QDir(getSettingsDirPath()).filePath("avatar_"+ownerId); QDir dir(getSettingsDirPath());
dir.mkdir("avatars/"); // remove this in a week or two hopefully
QString filePath = dir.filePath("avatars/"+ownerId+".png");
pic.save(filePath, "png"); pic.save(filePath, "png");
} }

View File

@ -142,7 +142,6 @@ private:
Settings& operator=(const Settings&) = delete; Settings& operator=(const Settings&) = delete;
static const QString FILENAME; static const QString FILENAME;
static const QString AVATAR_FILENAME;
bool loaded; bool loaded;

View File

@ -477,7 +477,7 @@ void Widget::addFriend(int friendId, const QString &userId)
connect(core, &Core::friendAvatarRemoved, newfriend->widget, &FriendWidget::onAvatarRemoved); connect(core, &Core::friendAvatarRemoved, newfriend->widget, &FriendWidget::onAvatarRemoved);
// Try to get the avatar from the cache // Try to get the avatar from the cache
QPixmap avatar = Settings::getInstance().getSavedAvatar(userId); QPixmap avatar = Settings::getInstance().getSavedAvatar(userId.left(64)); // just to be safe
if (!avatar.isNull()) if (!avatar.isNull())
{ {
newfriend->chatForm->onAvatarChange(friendId, avatar); newfriend->chatForm->onAvatarChange(friendId, avatar);