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

refactor(profile): reduce code duplication

This commit is contained in:
sudden6 2017-03-25 09:42:11 +01:00
parent c7375e7f3d
commit 3e50757ddb
No known key found for this signature in database
GPG Key ID: 279509B499E032B9
4 changed files with 68 additions and 135 deletions

View File

@ -1,6 +1,6 @@
/*
Copyright (C) 2013 by Maxim Biro <nurupo.contributions@gmail.com>
Copyright © 2014-2015 by The qTox Project Contributors
Copyright © 2014-2017 by The qTox Project Contributors
This file is part of qTox, a Qt-based graphical interface for Tox.
@ -231,7 +231,7 @@ void Core::makeTox(QByteArray savedata)
/**
* @brief Initializes the core, must be called before anything else
*/
void Core::start()
void Core::start(const QByteArray& savedata)
{
bool isNewProfile = profile.isNewProfile();
if (isNewProfile) {
@ -241,7 +241,6 @@ void Core::start()
setUsername(profile.getName());
} else {
qDebug() << "Loading user profile";
QByteArray savedata = profile.loadToxSave();
if (savedata.isEmpty()) {
emit failedToStart();
return;
@ -436,7 +435,8 @@ void Core::onFriendMessage(Tox* /* tox*/, uint32_t friendId, TOX_MESSAGE_TYPE ty
void Core::onFriendNameChange(Tox* /* tox*/, uint32_t friendId, const uint8_t* cName,
size_t cNameSize, void* core)
{
emit static_cast<Core*>(core)->friendUsernameChanged(friendId, ToxString(cName, cNameSize).getQString());
emit static_cast<Core*>(core)->friendUsernameChanged(friendId,
ToxString(cName, cNameSize).getQString());
}
void Core::onFriendTypingChange(Tox* /* tox*/, uint32_t friendId, bool isTyping, void* core)
@ -447,8 +447,8 @@ void Core::onFriendTypingChange(Tox* /* tox*/, uint32_t friendId, bool isTyping,
void Core::onStatusMessageChanged(Tox* /* tox*/, uint32_t friendId, const uint8_t* cMessage,
size_t cMessageSize, void* core)
{
emit static_cast<Core*>(core)->friendStatusMessageChanged(friendId,
ToxString(cMessage, cMessageSize).getQString());
emit static_cast<Core*>(core)
->friendStatusMessageChanged(friendId, ToxString(cMessage, cMessageSize).getQString());
}
void Core::onUserStatusChanged(Tox* /* tox*/, uint32_t friendId, TOX_USER_STATUS userstatus, void* core)
@ -479,8 +479,8 @@ void Core::onConnectionStatusChanged(Tox* /* tox*/, uint32_t friendId, TOX_CONNE
if (friendStatus == Status::Offline) {
emit static_cast<Core*>(core)->friendStatusChanged(friendId, friendStatus);
static_cast<Core*>(core)->checkLastOnline(friendId);
CoreFile::onConnectionStatusChanged(static_cast<Core*>(core), friendId,
friendStatus != Status::Offline);
CoreFile::onConnectionStatusChanged(static_cast<Core*>(core), friendId,
friendStatus != Status::Offline);
}
}
@ -505,7 +505,8 @@ void Core::onGroupMessage(Tox*, uint32_t groupId, uint32_t peerId, TOX_MESSAGE_T
{
Core* core = static_cast<Core*>(vCore);
bool isAction = type == TOX_MESSAGE_TYPE_ACTION;
emit core->groupMessageReceived(groupId, peerId, ToxString(cMessage, length).getQString(), isAction);
emit core->groupMessageReceived(groupId, peerId, ToxString(cMessage, length).getQString(),
isAction);
}
void Core::onGroupNamelistChange(Tox*, uint32_t groupId, uint32_t peerId,
@ -962,8 +963,8 @@ void Core::loadFriends()
if (statusMessageSize != SIZE_MAX) {
uint8_t* statusMessage = new uint8_t[statusMessageSize];
if (tox_friend_get_status_message(tox, ids[i], statusMessage, nullptr)) {
emit friendStatusMessageChanged(ids[i], ToxString(statusMessage,
statusMessageSize).getQString());
emit friendStatusMessageChanged(
ids[i], ToxString(statusMessage, statusMessageSize).getQString());
}
delete[] statusMessage;
}
@ -1388,6 +1389,7 @@ void Core::killTimers(bool onlyStop)
void Core::reset()
{
assert(QThread::currentThread() == coreThread);
QByteArray toxsave = getToxSaveData();
ready = false;
killTimers(true);
@ -1396,5 +1398,5 @@ void Core::reset()
emit selfAvatarChanged(QPixmap(":/img/contact_dark.svg"));
GUI::clearContacts();
start();
start(toxsave);
}

View File

@ -1,6 +1,6 @@
/*
Copyright (C) 2013 by Maxim Biro <nurupo.contributions@gmail.com>
Copyright © 2014-2015 by The qTox Project Contributors
Copyright © 2014-2017 by The qTox Project Contributors
This file is part of qTox, a Qt-based graphical interface for Tox.
@ -83,7 +83,7 @@ public:
void sendFile(uint32_t friendId, QString filename, QString filePath, long long filesize);
public slots:
void start();
void start(const QByteArray& savedata);
void reset();
void process();
void bootstrapDht();

View File

@ -1,5 +1,5 @@
/*
Copyright © 2015-2016 by The qTox Project Contributors
Copyright © 2015-2017 by The qTox Project Contributors
This file is part of qTox, a Qt-based graphical interface for Tox.
@ -49,7 +49,7 @@
QVector<QString> Profile::profiles;
Profile::Profile(QString name, const QString& password, bool isNewProfile)
Profile::Profile(QString name, const QString& password, bool isNewProfile, const QByteArray& toxsave)
: name{name}
, password{password}
, newProfile{isNewProfile}
@ -64,7 +64,7 @@ Profile::Profile(QString name, const QString& password, bool isNewProfile)
core = new Core(coreThread, *this);
QObject::connect(core, &Core::idSet, this, &Profile::loadDatabase, Qt::QueuedConnection);
core->moveToThread(coreThread);
QObject::connect(coreThread, &QThread::started, core, &Core::start);
QObject::connect(coreThread, &QThread::started, core, [=]() { core->start(toxsave); });
}
/**
@ -89,64 +89,65 @@ Profile* Profile::loadProfile(QString name, const QString& password)
}
std::unique_ptr<ToxEncrypt> tmpKey;
QByteArray data = QByteArray();
Profile* p = nullptr;
qint64 fileSize = 0;
// Check password
{
QString path = Settings::getInstance().getSettingsDirPath() + name + ".tox";
QFile saveFile(path);
qDebug() << "Loading tox save " << path;
QString path = Settings::getInstance().getSettingsDirPath() + name + ".tox";
QFile saveFile(path);
qDebug() << "Loading tox save " << path;
if (!saveFile.exists()) {
qWarning() << "The tox save file " << path << " was not found";
ProfileLocker::unlock();
return nullptr;
if (!saveFile.exists()) {
qWarning() << "The tox save file " << path << " was not found";
goto fail;
}
if (!saveFile.open(QIODevice::ReadOnly)) {
qCritical() << "The tox save file " << path << " couldn't' be opened";
goto fail;
}
fileSize = saveFile.size();
if (fileSize <= 0) {
qWarning() << "The tox save file" << path << " is empty!";
goto fail;
}
data = saveFile.readAll();
if (ToxEncrypt::isEncrypted(data)) {
if (password.isEmpty()) {
qCritical() << "The tox save file is encrypted, but we don't have a password!";
goto fail;
}
if (!saveFile.open(QIODevice::ReadOnly)) {
qCritical() << "The tox save file " << path << " couldn't' be opened";
ProfileLocker::unlock();
return nullptr;
tmpKey = ToxEncrypt::makeToxEncrypt(password, data);
if (!tmpKey) {
qCritical() << "Failed to derive key of the tox save file";
goto fail;
}
qint64 fileSize = saveFile.size();
if (fileSize <= 0) {
qWarning() << "The tox save file" << path << " is empty!";
ProfileLocker::unlock();
return nullptr;
data = tmpKey->decrypt(data);
if (data.isEmpty()) {
qCritical() << "Failed to decrypt the tox save file";
goto fail;
}
QByteArray data = saveFile.readAll();
if (ToxEncrypt::isEncrypted(data)) {
if (password.isEmpty()) {
qCritical() << "The tox save file is encrypted, but we don't have a password!";
ProfileLocker::unlock();
return nullptr;
}
tmpKey = ToxEncrypt::makeToxEncrypt(password, data);
if (!tmpKey) {
qCritical() << "Failed to derive key of the tox save file";
ProfileLocker::unlock();
return nullptr;
}
data = tmpKey->decrypt(data);
if (data.isEmpty()) {
qCritical() << "Failed to decrypt the tox save file";
ProfileLocker::unlock();
return nullptr;
}
} else {
if (!password.isEmpty()) {
qWarning() << "We have a password, but the tox save file is not encrypted";
}
} else {
if (!password.isEmpty()) {
qWarning() << "We have a password, but the tox save file is not encrypted";
}
}
Profile* p = new Profile(name, password, false);
saveFile.close();
p = new Profile(name, password, false, data);
p->passkey = std::move(tmpKey);
return p;
// cleanup in case of error
fail:
saveFile.close();
ProfileLocker::unlock();
return nullptr;
}
/**
@ -185,7 +186,7 @@ Profile* Profile::createProfile(QString name, QString password)
}
Settings::getInstance().createPersonal(name);
Profile* p = new Profile(name, password, true);
Profile* p = new Profile(name, password, true, QByteArray());
p->passkey = std::move(tmpKey);
return p;
@ -276,61 +277,6 @@ bool Profile::isNewProfile()
return newProfile;
}
/**
* @brief Loads the profile's .tox save from file, unencrypted
* @return Byte array of loaded profile save.
*/
QByteArray Profile::loadToxSave()
{
assert(!isRemoved);
QByteArray data;
QString path = Settings::getInstance().getSettingsDirPath() + name + ".tox";
QFile saveFile(path);
qint64 fileSize;
qDebug() << "Loading tox save " << path;
if (!saveFile.exists()) {
qWarning() << "The tox save file " << path << " was not found";
goto fail;
}
if (!saveFile.open(QIODevice::ReadOnly)) {
qCritical() << "The tox save file " << path << " couldn't' be opened";
goto fail;
}
fileSize = saveFile.size();
if (fileSize <= 0) {
qWarning() << "The tox save file" << path << " is empty!";
goto fail;
}
data = saveFile.readAll();
if (ToxEncrypt::isEncrypted(data)) {
if (password.isEmpty()) {
qCritical() << "The tox save file is encrypted, but we don't have a password!";
data.clear();
goto fail;
}
data = passkey->decrypt(data);
if (data.isEmpty()) {
qCritical() << "Failed to decrypt the tox save file";
data.clear();
goto fail;
}
} else {
if (!password.isEmpty()) {
qWarning() << "We have a password, but the tox save file is not encrypted";
}
}
fail:
saveFile.close();
return data;
}
/**
* @brief Saves the profile's .tox save, encrypted if needed.
* @warning Invalid on deleted profiles.
@ -694,19 +640,6 @@ bool Profile::rename(QString newName)
return true;
}
/**
* @brief Checks whether the password is valid.
* @return True, if password is valid, false otherwise.
*/
bool Profile::checkPassword()
{
if (isRemoved) {
return false;
}
return !loadToxSave().isEmpty();
}
QString Profile::getPassword() const
{
return password;

View File

@ -1,5 +1,5 @@
/*
Copyright © 2015-2016 by The qTox Project Contributors
Copyright © 2015-2017 by The qTox Project Contributors
This file is part of qTox, a Qt-based graphical interface for Tox.
@ -52,12 +52,10 @@ public:
void restartCore();
bool isNewProfile();
bool isEncrypted() const;
bool checkPassword();
QString getPassword() const;
void setPassword(const QString& newPassword);
const ToxEncrypt& getPasskey() const;
QByteArray loadToxSave();
void saveToxSave();
void saveToxSave(QByteArray data);
@ -88,7 +86,7 @@ private slots:
void loadDatabase(const ToxId& id);
private:
Profile(QString name, const QString& password, bool newProfile);
Profile(QString name, const QString& password, bool newProfile, const QByteArray& toxsave);
static QVector<QString> getFilesByExt(QString extension);
QString avatarPath(const QString& ownerId, bool forceUnencrypted = false);