Browse Source

refactor(core): reimplement restart and small cleanups

reviewable/pr5194/r4
sudden6 7 years ago
parent
commit
82a7141e31
No known key found for this signature in database
GPG Key ID: 279509B499E032B9
  1. 3
      src/core/core.cpp
  2. 64
      src/persistence/profile.cpp
  3. 5
      src/persistence/profile.h

3
src/core/core.cpp

@ -65,8 +65,9 @@ Core::~Core()
coreThread->exit(0); coreThread->exit(0);
// need to reset av first, because it uses tox
av.reset(); av.reset();
tox_kill(tox.get()); tox.reset();
} }
/** /**

64
src/persistence/profile.cpp

@ -52,15 +52,8 @@
QStringList Profile::profiles; QStringList Profile::profiles;
Profile::Profile(QString name, const QString& password, bool isNewProfile, const QByteArray& toxsave) void Profile::initCore(const QByteArray& toxsave, ICoreSettings& s)
: name{name}
, newProfile{isNewProfile}
, isRemoved{false}
{ {
Settings& s = Settings::getInstance();
s.setCurrentProfile(name);
s.saveGlobal();
Core::ToxCoreErrors err; Core::ToxCoreErrors err;
core = Core::makeToxCore(toxsave, &s, &err); core = Core::makeToxCore(toxsave, &s, &err);
if(!core) { if(!core) {
@ -84,7 +77,20 @@ Profile::Profile(QString name, const QString& password, bool isNewProfile, const
// react to avatar changes // react to avatar changes
connect(core.get(), &Core::friendAvatarRemoved, this, &Profile::removeAvatar); connect(core.get(), &Core::friendAvatarRemoved, this, &Profile::removeAvatar);
connect(core.get(), &Core::friendAvatarData, this, &Profile::saveAvatar); 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(); const ToxId& selfId = core->getSelfId();
loadDatabase(selfId, password); loadDatabase(selfId, password);
@ -299,7 +305,7 @@ void Profile::startCore()
const ToxId& selfId = core->getSelfId(); const ToxId& selfId = core->getSelfId();
const ToxPk& selfPk = selfId.getPublicKey(); const ToxPk& selfPk = selfId.getPublicKey();
QByteArray data = loadAvatarData(selfPk); const QByteArray data = loadAvatarData(selfPk);
if (data.isEmpty()) { if (data.isEmpty()) {
qDebug() << "Self avatar not found, will broadcast empty avatar to friends"; 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. * @brief Write the .tox save, encrypted if needed.
* @param data Byte array of profile save. * @param data Byte array of profile save.
* @return true if successfully saved, false otherwise
* @warning Invalid on deleted profiles. * @warning Invalid on deleted profiles.
*/ */
void Profile::saveToxSave(QByteArray data) bool Profile::saveToxSave(QByteArray data)
{ {
assert(!isRemoved); assert(!isRemoved);
ProfileLocker::assertLock(); ProfileLocker::assertLock();
@ -350,7 +357,7 @@ void Profile::saveToxSave(QByteArray data)
QSaveFile saveFile(path); QSaveFile saveFile(path);
if (!saveFile.open(QIODevice::WriteOnly)) { if (!saveFile.open(QIODevice::WriteOnly)) {
qCritical() << "Tox save file " << path << " couldn't be opened"; qCritical() << "Tox save file " << path << " couldn't be opened";
return; return false;
} }
if (encrypted) { if (encrypted) {
@ -358,7 +365,7 @@ void Profile::saveToxSave(QByteArray data)
if (data.isEmpty()) { if (data.isEmpty()) {
qCritical() << "Failed to encrypt, can't save!"; qCritical() << "Failed to encrypt, can't save!";
saveFile.cancelWriting(); saveFile.cancelWriting();
return; return false;
} }
} }
@ -371,7 +378,9 @@ void Profile::saveToxSave(QByteArray data)
} else { } else {
saveFile.cancelWriting(); saveFile.cancelWriting();
qCritical() << "Failed to write, can't save!"; 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 pic Picture to save.
* @param owner PK of avatar owner. * @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()) { bool needEncrypt = encrypted && !avatar.isEmpty();
pic = passkey->encrypt(pic); const QByteArray& pic = needEncrypt ? passkey->encrypt(avatar): avatar;
}
QString path = avatarPath(owner); QString path = avatarPath(owner);
QDir(Settings::getInstance().getSettingsDirPath()).mkdir("avatars"); QDir(Settings::getInstance().getSettingsDirPath()).mkdir("avatars");
@ -746,13 +754,25 @@ const ToxEncrypt* Profile::getPasskey() const
*/ */
void Profile::restartCore() void Profile::restartCore()
{ {
/* TODO(sudden6): rethink this
GUI::setEnabled(false); // Core::reset re-enables it 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);
} }
/** /**

5
src/persistence/profile.h

@ -86,7 +86,7 @@ public slots:
private slots: private slots:
void loadDatabase(const ToxId& id, QString password); 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 removeAvatar(const ToxPk& owner);
void onSaveToxSave(); void onSaveToxSave();
// TODO(sudden6): use ToxPk instead of friendId // TODO(sudden6): use ToxPk instead of friendId
@ -96,7 +96,8 @@ private:
Profile(QString name, const QString& password, bool newProfile, const QByteArray& toxsave); Profile(QString name, const QString& password, bool newProfile, const QByteArray& toxsave);
static QStringList getFilesByExt(QString extension); static QStringList getFilesByExt(QString extension);
QString avatarPath(const ToxPk& owner, bool forceUnencrypted = false); QString avatarPath(const ToxPk& owner, bool forceUnencrypted = false);
void saveToxSave(QByteArray data); bool saveToxSave(QByteArray data);
void initCore(const QByteArray &toxsave, ICoreSettings &s);
private: private:
std::unique_ptr<Core> core = nullptr; std::unique_ptr<Core> core = nullptr;

Loading…
Cancel
Save