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);
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())
{
QByteArray data;
@ -218,12 +219,25 @@ void Core::start()
buffer.open(QIODevice::WriteOnly);
pic.save(&buffer, "PNG");
buffer.close();
if (tox_set_avatar(tox, TOX_AVATAR_FORMAT_PNG, (uint8_t*)data.constData(), data.size()) != 0)
qWarning() << "Core:start: Error setting avatar, size:"<<data.size();
emit selfAvatarChanged(pic);
setAvatar(TOX_AVATAR_FORMAT_PNG, data);
}
else
qDebug() << "Core: Error loading self avatar";
{
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
qDebug() << "Core: Error loading self avatar";
}
bootstrapDht();
@ -476,7 +490,8 @@ void Core::onAvatarDataCallback(Tox*, int32_t friendnumber, uint8_t,
else
{
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);
}
}
@ -789,13 +804,10 @@ void Core::setAvatar(uint8_t format, const QByteArray& data)
QPixmap pic;
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);
// 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);
// according to tox.h, we need not broadcast ourselves, that's done in tox_set_avatar
}
ToxID Core::getSelfId()

View File

@ -27,7 +27,6 @@
#include <QList>
const QString Settings::FILENAME = "settings.ini";
const QString Settings::AVATAR_FILENAME = "avatar.dat";
bool Settings::makeToxPortable{false};
Settings::Settings() :
@ -260,15 +259,27 @@ QString Settings::getSettingsDirPath()
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;
pic.load(filePath);
if (!info.exists())
{
QString filePath = dir.filePath("avatar_"+ownerId);
pic.load(filePath);
saveAvatar(pic, ownerId);
QFile::remove(filePath);
}
else
pic.load(filePath);
return pic;
}
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");
}

View File

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

View File

@ -477,7 +477,7 @@ void Widget::addFriend(int friendId, const QString &userId)
connect(core, &Core::friendAvatarRemoved, newfriend->widget, &FriendWidget::onAvatarRemoved);
// 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())
{
newfriend->chatForm->onAvatarChange(friendId, avatar);