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

Implement profile deletion

This commit is contained in:
tux3 2015-06-04 12:43:28 +02:00
parent f7546a731f
commit 21db31c215
5 changed files with 62 additions and 32 deletions

View File

@ -104,7 +104,13 @@ Core::~Core()
{
qDebug() << "Deleting Core";
QMetaObject::invokeMethod(this, "stopTimers", Qt::BlockingQueuedConnection);
if (coreThread->isRunning())
{
if (QThread::currentThread() == coreThread)
stopTimers();
else
QMetaObject::invokeMethod(this, "stopTimers", Qt::BlockingQueuedConnection);
}
delete toxTimer;
coreThread->exit(0);
while (coreThread->isRunning())
@ -781,7 +787,8 @@ void Core::setUsername(const QString& username)
else
{
emit usernameSet(username);
profile.saveToxSave();
if (ready)
profile.saveToxSave();
}
}
@ -845,7 +852,8 @@ void Core::setStatusMessage(const QString& message)
}
else
{
profile.saveToxSave();
if (ready)
profile.saveToxSave();
emit statusMessageSet(message);
}
}

View File

@ -2,6 +2,7 @@
#include "profilelocker.h"
#include "src/misc/settings.h"
#include "src/core/core.h"
#include "src/historykeeper.h"
#include <cassert>
#include <QDir>
#include <QFileInfo>
@ -12,7 +13,8 @@
QVector<QString> Profile::profiles;
Profile::Profile(QString name, QString password, bool isNewProfile)
: name{name}, password{password}, newProfile{isNewProfile}
: name{name}, password{password},
newProfile{isNewProfile}, isRemoved{false}
{
coreThread = new QThread();
coreThread->setObjectName("qTox Core");
@ -64,7 +66,8 @@ Profile* Profile::createProfile(QString name, QString password)
Profile::~Profile()
{
saveToxSave();
if (!isRemoved && core->isReady())
saveToxSave();
delete core;
delete coreThread;
ProfileLocker::assertLock();
@ -130,6 +133,8 @@ bool Profile::isNewProfile()
QByteArray Profile::loadToxSave()
{
assert(!isRemoved);
/// TODO: Cache the data, invalidate it only when we save
QByteArray data;
@ -188,11 +193,15 @@ fail:
void Profile::saveToxSave()
{
saveToxSave(core->getToxSaveData());
assert(core->isReady());
QByteArray data = core->getToxSaveData();
assert(data.size());
saveToxSave(data);
}
void Profile::saveToxSave(QByteArray data)
{
assert(!isRemoved);
ProfileLocker::assertLock();
assert(ProfileLocker::getCurLockName() == name);
@ -244,3 +253,22 @@ bool Profile::isProfileEncrypted(QString name)
return tox_is_data_encrypted(data);
}
void Profile::remove()
{
if (isRemoved)
{
qWarning() << "Profile "<<name<<" is already removed!";
return;
}
isRemoved = true;
qDebug() << "Removing profile"<<name;
profiles.removeAll(name);
QString path = Settings::getSettingsDirPath() + QDir::separator() + name;
QFile::remove(path+".tox");
QFile::remove(path+".ini");
QFile::remove(HistoryKeeper::getHistoryPath(name, 0));
QFile::remove(HistoryKeeper::getHistoryPath(name, 1));
}

View File

@ -27,8 +27,13 @@ public:
void startCore(); ///< Starts the Core thread
bool isNewProfile();
QByteArray loadToxSave(); ///< Loads the profile's .tox save from file, unencrypted
void saveToxSave(); ///< Saves the profile's .tox save, encrypted if needed
void saveToxSave(QByteArray data); ///< Saves the profile's .tox save with this data, encrypted if needed
void saveToxSave(); ///< Saves the profile's .tox save, encrypted if needed. Invalid on deleted profiles.
void saveToxSave(QByteArray data); ///< Write the .tox save, encrypted if needed. Invalid on deleted profiles.
/// Removes the profile permanently
/// It is invalid to call loadToxSave or saveToxSave on a deleted profile
/// Updates the profiles vector
void remove();
/// Scan for profile, automatically importing them if needed
/// NOT thread-safe
@ -51,8 +56,9 @@ private:
Core* core;
QThread* coreThread;
QString name, password;
static QVector<QString> profiles;
bool newProfile; ///< True if this is a newly created profile, with no .tox save file yet.
bool isRemoved; ///< True if the profile has been removed by remove()
static QVector<QString> profiles;
/// How much data we need to read to check if the file is encrypted
/// Must be >= TOX_ENC_SAVE_MAGIC_LENGTH (8), which isn't publicly defined
static constexpr int encryptHeaderSize = 8;

View File

@ -283,29 +283,13 @@ void ProfileForm::onExportClicked()
void ProfileForm::onDeleteClicked()
{
/** Add a delete function in profile
if (Settings::getInstance().getCurrentProfile() == bodyUI->profiles->currentText())
if (GUI::askQuestion(tr("Really delete profile?","deletion confirmation title"),
tr("Are you sure you want to delete this profile?","deletion confirmation text")))
{
GUI::showWarning(tr("Profile currently loaded","current profile deletion warning title"), tr("This profile is currently in use. Please load a different profile before deleting this one.","current profile deletion warning text"));
Nexus& nexus = Nexus::getInstance();
nexus.getProfile()->remove();
nexus.showLogin();
}
else
{
if (GUI::askQuestion(tr("Deletion imminent!","deletion confirmation title"),
tr("Are you sure you want to delete this profile?","deletion confirmation text")))
{
QString profile = bodyUI->profiles->currentText();
QDir dir(Settings::getSettingsDirPath());
QFile::remove(dir.filePath(profile + Core::TOX_EXT));
QFile::remove(dir.filePath(profile + ".ini"));
QFile::remove(HistoryKeeper::getHistoryPath(profile, 0));
QFile::remove(HistoryKeeper::getHistoryPath(profile, 1));
bodyUI->profiles->removeItem(bodyUI->profiles->currentIndex());
bodyUI->profiles->setCurrentText(Settings::getInstance().getCurrentProfile());
}
}
*/
}
void ProfileForm::onLogoutClicked()

View File

@ -281,7 +281,7 @@
<bool>true</bool>
</property>
<property name="currentIndex">
<number>1</number>
<number>0</number>
</property>
<widget class="QWidget" name="newPage">
<layout class="QHBoxLayout" name="horizontalLayout_5">
@ -394,7 +394,11 @@ margin-bottom:45px;</string>
</widget>
</item>
<item>
<widget class="QLineEdit" name="newPassConfirm"/>
<widget class="QLineEdit" name="newPassConfirm">
<property name="echoMode">
<enum>QLineEdit::Password</enum>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer">