mirror of
https://github.com/qTox/qTox.git
synced 2024-03-22 14:00:36 +08:00
refactor(core): reimplement restart and small cleanups
This commit is contained in:
parent
5a6606e351
commit
82a7141e31
@ -65,8 +65,9 @@ Core::~Core()
|
||||
|
||||
coreThread->exit(0);
|
||||
|
||||
// need to reset av first, because it uses tox
|
||||
av.reset();
|
||||
tox_kill(tox.get());
|
||||
tox.reset();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -52,15 +52,8 @@
|
||||
|
||||
QStringList Profile::profiles;
|
||||
|
||||
Profile::Profile(QString name, const QString& password, bool isNewProfile, const QByteArray& toxsave)
|
||||
: name{name}
|
||||
, newProfile{isNewProfile}
|
||||
, isRemoved{false}
|
||||
void Profile::initCore(const QByteArray& toxsave, ICoreSettings& s)
|
||||
{
|
||||
Settings& s = Settings::getInstance();
|
||||
s.setCurrentProfile(name);
|
||||
s.saveGlobal();
|
||||
|
||||
Core::ToxCoreErrors err;
|
||||
core = Core::makeToxCore(toxsave, &s, &err);
|
||||
if(!core) {
|
||||
@ -84,7 +77,20 @@ Profile::Profile(QString name, const QString& password, bool isNewProfile, const
|
||||
// react to avatar changes
|
||||
connect(core.get(), &Core::friendAvatarRemoved, this, &Profile::removeAvatar);
|
||||
connect(core.get(), &Core::friendAvatarData, this, &Profile::saveAvatar);
|
||||
connect(core.get(), &Core::fileAvatarOfferReceived, this, &Profile::onAvatarOfferReceived, Qt::ConnectionType::BlockingQueuedConnection);
|
||||
connect(core.get(), &Core::fileAvatarOfferReceived, this, &Profile::onAvatarOfferReceived,
|
||||
Qt::ConnectionType::BlockingQueuedConnection);
|
||||
}
|
||||
|
||||
Profile::Profile(QString name, const QString& password, bool isNewProfile, const QByteArray& toxsave)
|
||||
: name{name}
|
||||
, newProfile{isNewProfile}
|
||||
, isRemoved{false}
|
||||
{
|
||||
Settings& s = Settings::getInstance();
|
||||
s.setCurrentProfile(name);
|
||||
s.saveGlobal();
|
||||
|
||||
initCore(toxsave, s);
|
||||
|
||||
const ToxId& selfId = core->getSelfId();
|
||||
loadDatabase(selfId, password);
|
||||
@ -299,7 +305,7 @@ void Profile::startCore()
|
||||
|
||||
const ToxId& selfId = core->getSelfId();
|
||||
const ToxPk& selfPk = selfId.getPublicKey();
|
||||
QByteArray data = loadAvatarData(selfPk);
|
||||
const QByteArray data = loadAvatarData(selfPk);
|
||||
if (data.isEmpty()) {
|
||||
qDebug() << "Self avatar not found, will broadcast empty avatar to friends";
|
||||
}
|
||||
@ -337,9 +343,10 @@ void Profile::onAvatarOfferReceived(uint32_t friendId, uint32_t fileId, const QB
|
||||
/**
|
||||
* @brief Write the .tox save, encrypted if needed.
|
||||
* @param data Byte array of profile save.
|
||||
* @return true if successfully saved, false otherwise
|
||||
* @warning Invalid on deleted profiles.
|
||||
*/
|
||||
void Profile::saveToxSave(QByteArray data)
|
||||
bool Profile::saveToxSave(QByteArray data)
|
||||
{
|
||||
assert(!isRemoved);
|
||||
ProfileLocker::assertLock();
|
||||
@ -350,7 +357,7 @@ void Profile::saveToxSave(QByteArray data)
|
||||
QSaveFile saveFile(path);
|
||||
if (!saveFile.open(QIODevice::WriteOnly)) {
|
||||
qCritical() << "Tox save file " << path << " couldn't be opened";
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (encrypted) {
|
||||
@ -358,7 +365,7 @@ void Profile::saveToxSave(QByteArray data)
|
||||
if (data.isEmpty()) {
|
||||
qCritical() << "Failed to encrypt, can't save!";
|
||||
saveFile.cancelWriting();
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@ -371,7 +378,9 @@ void Profile::saveToxSave(QByteArray data)
|
||||
} else {
|
||||
saveFile.cancelWriting();
|
||||
qCritical() << "Failed to write, can't save!";
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -545,11 +554,10 @@ void Profile::onRequestSent(const ToxPk& friendPk, const QString& message)
|
||||
* @param pic Picture to save.
|
||||
* @param owner PK of avatar owner.
|
||||
*/
|
||||
void Profile::saveAvatar(const ToxPk& owner, QByteArray pic)
|
||||
void Profile::saveAvatar(const ToxPk& owner, const QByteArray& avatar)
|
||||
{
|
||||
if (encrypted && !pic.isEmpty()) {
|
||||
pic = passkey->encrypt(pic);
|
||||
}
|
||||
bool needEncrypt = encrypted && !avatar.isEmpty();
|
||||
const QByteArray& pic = needEncrypt ? passkey->encrypt(avatar): avatar;
|
||||
|
||||
QString path = avatarPath(owner);
|
||||
QDir(Settings::getInstance().getSettingsDirPath()).mkdir("avatars");
|
||||
@ -746,13 +754,25 @@ const ToxEncrypt* Profile::getPasskey() const
|
||||
*/
|
||||
void Profile::restartCore()
|
||||
{
|
||||
/* TODO(sudden6): rethink this
|
||||
GUI::setEnabled(false); // Core::reset re-enables it
|
||||
if (!isRemoved && core->isReady()) {
|
||||
saveToxSave();
|
||||
|
||||
if(core && !isRemoved) {
|
||||
// TODO(sudden6): there's a potential race condition between unlocking the core loop
|
||||
// and killing the core
|
||||
const QByteArray& savedata = core->getToxSaveData();
|
||||
// save to disk just in case
|
||||
bool saved = saveToxSave(savedata);
|
||||
|
||||
if(saved) {
|
||||
qDebug() << "Restarting Core";
|
||||
initCore(savedata, Settings::getInstance());
|
||||
core->start();
|
||||
} else {
|
||||
qCritical() << "Failed to save, not restarting core";
|
||||
}
|
||||
}
|
||||
QMetaObject::invokeMethod(core, "reset");
|
||||
*/
|
||||
|
||||
GUI::setEnabled(true);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -86,7 +86,7 @@ public slots:
|
||||
|
||||
private slots:
|
||||
void loadDatabase(const ToxId& id, QString password);
|
||||
void saveAvatar(const ToxPk& owner, QByteArray pic);
|
||||
void saveAvatar(const ToxPk& owner, const QByteArray &avatar);
|
||||
void removeAvatar(const ToxPk& owner);
|
||||
void onSaveToxSave();
|
||||
// TODO(sudden6): use ToxPk instead of friendId
|
||||
@ -96,7 +96,8 @@ private:
|
||||
Profile(QString name, const QString& password, bool newProfile, const QByteArray& toxsave);
|
||||
static QStringList getFilesByExt(QString extension);
|
||||
QString avatarPath(const ToxPk& owner, bool forceUnencrypted = false);
|
||||
void saveToxSave(QByteArray data);
|
||||
bool saveToxSave(QByteArray data);
|
||||
void initCore(const QByteArray &toxsave, ICoreSettings &s);
|
||||
|
||||
private:
|
||||
std::unique_ptr<Core> core = nullptr;
|
||||
|
Loading…
x
Reference in New Issue
Block a user