From abf65a5060031751015536ece3dafe20694a1ae2 Mon Sep 17 00:00:00 2001 From: tux3 Date: Thu, 4 Jun 2015 11:37:54 +0200 Subject: [PATCH] Remove deprecated profile management functions --- src/core/core.cpp | 99 +++--------------- src/core/core.h | 14 +-- src/core/coreencryption.cpp | 106 ------------------- src/misc/settings.cpp | 8 ++ src/misc/settings.h | 1 + src/nexus.cpp | 1 - src/profile.cpp | 5 + src/profile.h | 3 +- src/widget/form/profileform.cpp | 124 +++-------------------- src/widget/form/profileform.h | 16 +-- src/widget/form/profileform.ui | 94 +++++++---------- src/widget/form/settings/generalform.cpp | 4 +- src/widget/form/settings/privacyform.cpp | 2 +- src/widget/widget.h | 1 - 14 files changed, 85 insertions(+), 393 deletions(-) diff --git a/src/core/core.cpp b/src/core/core.cpp index 2cba81039..27883c19d 100644 --- a/src/core/core.cpp +++ b/src/core/core.cpp @@ -103,7 +103,7 @@ Core::~Core() { qDebug() << "Deleting Core"; - saveConfiguration(); + profile.saveToxSave(); toxTimer->stop(); coreThread->exit(0); while (coreThread->isRunning()) @@ -339,7 +339,7 @@ void Core::start() // This is useful for e.g. the profileForm that searches for saves. if (isNewProfile) { - saveConfiguration(); + profile.saveToxSave(); emit idSet(getSelfId().toString()); } @@ -582,7 +582,7 @@ void Core::acceptFriendRequest(const QString& userId) } else { - saveConfiguration(); + profile.saveToxSave(); emit friendAdded(friendId, userId); } } @@ -627,7 +627,7 @@ void Core::requestFriendship(const QString& friendAddress, const QString& messag emit friendAdded(friendId, userId); } } - saveConfiguration(); + profile.saveToxSave(); } int Core::sendMessage(uint32_t friendId, const QString& message) @@ -742,7 +742,7 @@ void Core::removeFriend(uint32_t friendId, bool fake) } else { - saveConfiguration(); + profile.saveToxSave(); emit friendRemoved(friendId); } } @@ -780,7 +780,7 @@ void Core::setUsername(const QString& username) else { emit usernameSet(username); - saveConfiguration(); + profile.saveToxSave(); } } @@ -844,7 +844,7 @@ void Core::setStatusMessage(const QString& message) } else { - saveConfiguration(); + profile.saveToxSave(); emit statusMessageSet(message); } } @@ -869,7 +869,7 @@ void Core::setStatus(Status status) } tox_self_set_status(tox, userstatus); - saveConfiguration(); + profile.saveToxSave(); emit statusSet(status); } @@ -890,84 +890,13 @@ QString Core::sanitize(QString name) return name; } -void Core::saveConfiguration() +QByteArray Core::getToxSaveData() { - if (QThread::currentThread() != coreThread) - return (void) QMetaObject::invokeMethod(this, "saveConfiguration"); - - if (!isReady()) - return; - - ProfileLocker::assertLock(); - - QString dir = Settings::getSettingsDirPath(); - QDir directory(dir); - if (!directory.exists() && !directory.mkpath(directory.absolutePath())) - { - qCritical() << "Error while creating directory " << dir; - return; - } - - QString profile = Settings::getInstance().getCurrentProfile(); - - if (profile == "") - { // no profile active; this should only happen on startup, if at all - profile = sanitize(getUsername()); - - if (profile == "") // happens on creation of a new Tox ID - profile = getIDString(); - - Settings::getInstance().switchProfile(profile); - } - - QString path = directory.filePath(profile + TOX_EXT); - - saveConfiguration(path); -} - -void Core::switchConfiguration(const QString& _profile) -{ - QString profile = QFileInfo(_profile).baseName(); - // If we can't get a lock, then another instance is already using that profile - while (!profile.isEmpty() && !ProfileLocker::lock(profile)) - { - qWarning() << "Profile "<stop(); - deadifyTox(); - - emit selfAvatarChanged(QPixmap(":/img/contact_dark.svg")); - emit blockingClearContacts(); // we need this to block, but signals are required for thread safety - - //if (profile.isEmpty()) - //loadPath = ""; - //else - // loadPath = QDir(Settings::getSettingsDirPath()).filePath(profile + TOX_EXT); - - Settings::getInstance().switchProfile(profile); - HistoryKeeper::resetInstance(); - - start(); + uint32_t fileSize = tox_get_savedata_size(tox); + QByteArray data; + data.resize(fileSize); + tox_get_savedata(tox, (uint8_t*)data.data()); + return data; } void Core::loadFriends() diff --git a/src/core/core.h b/src/core/core.h index 6b8dcd0d6..2d22867d9 100644 --- a/src/core/core.h +++ b/src/core/core.h @@ -92,9 +92,7 @@ public slots: void process(); ///< Processes toxcore events and ensure we stay connected, called by its own timer void bootstrapDht(); ///< Connects us to the Tox network - void saveConfiguration(); - void saveConfiguration(const QString& path); - void switchConfiguration(const QString& profile); ///< Load a different profile and restart the core + QByteArray getToxSaveData(); ///< Returns the unencrypted tox save data void acceptFriendRequest(const QString& userId); void requestFriendship(const QString& friendAddress, const QString& message); @@ -303,16 +301,6 @@ private: TOX_PASS_KEY* pwsaltedkeys[PasswordType::ptCounter] = {nullptr}; // use the pw's hash as the "pw" - // Hack for reloading current profile if switching to an encrypted one fails. - // Testing the passwords before killing the current profile is perfectly doable, - // however it would require major refactoring; - // the Core class as a whole also requires major refactoring (especially to support multiple IDs at once), - // so I'm punting on this until then, when it would get fixed anyways - TOX_PASS_KEY* backupkeys[PasswordType::ptCounter] = {nullptr}; - QString* backupProfile = nullptr; - void saveCurrentInformation(); - QString loadOldInformation(); - static const int videobufsize; static uint8_t* videobuf; diff --git a/src/core/coreencryption.cpp b/src/core/coreencryption.cpp index 5a7cff718..2a1a7a0ec 100644 --- a/src/core/coreencryption.cpp +++ b/src/core/coreencryption.cpp @@ -62,43 +62,6 @@ void Core::clearPassword(PasswordType passtype) pwsaltedkeys[passtype] = nullptr; } -// part of a hack, see core.h -void Core::saveCurrentInformation() -{ - if (pwsaltedkeys[ptMain]) - { - backupkeys[ptMain] = new TOX_PASS_KEY; - std::copy(pwsaltedkeys[ptMain], pwsaltedkeys[ptMain]+1, backupkeys[ptMain]); - } - if (pwsaltedkeys[ptHistory]) - { - backupkeys[ptHistory] = new TOX_PASS_KEY; - std::copy(pwsaltedkeys[ptHistory], pwsaltedkeys[ptHistory]+1, backupkeys[ptHistory]); - } - backupProfile = new QString(Settings::getInstance().getCurrentProfile()); -} - -QString Core::loadOldInformation() -{ - QString out; - if (backupProfile) - { - out = *backupProfile; - delete backupProfile; - backupProfile = nullptr; - } - backupProfile = nullptr; - clearPassword(ptMain); - clearPassword(ptHistory); - // we can just copy the pointer, as long as we null out backupkeys - // (if backupkeys was null anyways, then this is a null-op) - pwsaltedkeys[ptMain] = backupkeys[ptMain]; - pwsaltedkeys[ptHistory] = backupkeys[ptHistory]; - backupkeys[ptMain] = nullptr; - backupkeys[ptHistory] = nullptr; - return out; -} - QByteArray Core::encryptData(const QByteArray& data, PasswordType passtype) { if (!pwsaltedkeys[passtype]) @@ -228,72 +191,3 @@ void Core::checkEncryptedHistory() dialogtxt = a + "\n" + c + "\n" + b; } while (error); } - -void Core::saveConfiguration(const QString& path) -{ - if (QThread::currentThread() != coreThread) - return (void) QMetaObject::invokeMethod(this, "saveConfiguration", Q_ARG(const QString&, path)); - - if (!isReady()) - { - qWarning() << "saveConfiguration: Tox not started, aborting!"; - return; - } - - QSaveFile configurationFile(path); - if (!configurationFile.open(QIODevice::WriteOnly)) - { - qCritical() << "File " << path << " cannot be opened"; - return; - } - - qDebug() << "writing tox_save to " << path; - - uint32_t fileSize = tox_get_savedata_size(tox); - bool encrypt = Settings::getInstance().getEncryptTox(); - - if (fileSize > 0 && fileSize <= std::numeric_limits::max()) - { - uint8_t *data = new uint8_t[fileSize]; - - if (encrypt) - { - if (!pwsaltedkeys[ptMain]) - { - // probably zero chance event - GUI::showWarning(tr("NO Password"), tr("Local file encryption is enabled, but there is no password! It will be disabled.")); - Settings::getInstance().setEncryptTox(false); - tox_get_savedata(tox, data); - } - else - { - tox_get_savedata(tox, data); - uint8_t* newData = new uint8_t[fileSize+TOX_PASS_ENCRYPTION_EXTRA_LENGTH]; - if (tox_pass_key_encrypt(data, fileSize, pwsaltedkeys[ptMain], newData, nullptr)) - { - delete[] data; - data = newData; - fileSize+=TOX_PASS_ENCRYPTION_EXTRA_LENGTH; - } - else - { - delete[] newData; - delete[] data; - qCritical() << "Core::saveConfiguration(QString): Encryption failed, couldn't save"; - configurationFile.cancelWriting(); - return; - } - } - } - else - { - tox_get_savedata(tox, data); - } - - configurationFile.write(reinterpret_cast(data), fileSize); - configurationFile.commit(); - delete[] data; - } - - Settings::getInstance().save(); -} diff --git a/src/misc/settings.cpp b/src/misc/settings.cpp index 1e93431d6..27586ef0b 100644 --- a/src/misc/settings.cpp +++ b/src/misc/settings.cpp @@ -1282,3 +1282,11 @@ void Settings::createPersonal(QString basename) ps.beginGroup("Privacy"); ps.endGroup(); } + +void Settings::createSettingsDir() +{ + QString dir = Settings::getSettingsDirPath(); + QDir directory(dir); + if (!directory.exists() && !directory.mkpath(directory.absolutePath())) + qCritical() << "Error while creating directory " << dir; +} diff --git a/src/misc/settings.h b/src/misc/settings.h index 5ae8e2132..28feccb80 100644 --- a/src/misc/settings.h +++ b/src/misc/settings.h @@ -36,6 +36,7 @@ public: QString detectProfile(); QList searchProfiles(); QString askProfiles(); + void createSettingsDir(); ///< Creates a path to the settings dir, if it doesn't already exist void createPersonal(QString basename); ///< Write a default personnal settings file for a profile diff --git a/src/nexus.cpp b/src/nexus.cpp index c712d2e1c..1aadc87a8 100644 --- a/src/nexus.cpp +++ b/src/nexus.cpp @@ -148,7 +148,6 @@ void Nexus::showMainGUI() connect(widget, &Widget::statusSet, core, &Core::setStatus); connect(widget, &Widget::friendRequested, core, &Core::requestFriendship); connect(widget, &Widget::friendRequestAccepted, core, &Core::acceptFriendRequest); - connect(widget, &Widget::changeProfile, core, &Core::switchConfiguration); #endif profile->startCore(); diff --git a/src/profile.cpp b/src/profile.cpp index 4ead3976d..05c78da62 100644 --- a/src/profile.cpp +++ b/src/profile.cpp @@ -184,6 +184,11 @@ fail: return data; } +void Profile::saveToxSave() +{ + saveToxSave(core->getToxSaveData()); +} + void Profile::saveToxSave(QByteArray data) { ProfileLocker::assertLock(); diff --git a/src/profile.h b/src/profile.h index 81c426514..7d3f07861 100644 --- a/src/profile.h +++ b/src/profile.h @@ -27,7 +27,8 @@ public: void startCore(); ///< Starts the Core thread bool isNewProfile(); QByteArray loadToxSave(); ///< Loads the profile's .tox save from file, unencrypted - void saveToxSave(QByteArray data); ///< Saves the profile's .tox save, encrypted if needed + 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 /// Scan for profile, automatically importing them if needed /// NOT thread-safe diff --git a/src/widget/form/profileform.cpp b/src/widget/form/profileform.cpp index 9e4d273eb..df6495e7c 100644 --- a/src/widget/form/profileform.cpp +++ b/src/widget/form/profileform.cpp @@ -26,6 +26,7 @@ #include "src/historykeeper.h" #include "src/misc/style.h" #include "src/profilelocker.h" +#include "src/profile.h" #include #include #include @@ -35,17 +36,6 @@ #include #include -void ProfileForm::refreshProfiles() -{ - bodyUI->profiles->clear(); - for (QString profile : Settings::getInstance().searchProfiles()) - bodyUI->profiles->addItem(profile); - - QString current = Settings::getInstance().getCurrentProfile(); - if (current != "") - bodyUI->profiles->setCurrentText(current); -} - ProfileForm::ProfileForm(QWidget *parent) : QWidget{parent}, qr{nullptr} { @@ -98,25 +88,12 @@ ProfileForm::ProfileForm(QWidget *parent) : connect(bodyUI->toxIdLabel, SIGNAL(clicked()), this, SLOT(copyIdClicked())); connect(toxId, SIGNAL(clicked()), this, SLOT(copyIdClicked())); connect(core, &Core::idSet, this, &ProfileForm::setToxId); - connect(core, &Core::statusSet, this, &ProfileForm::onStatusSet); connect(bodyUI->userName, SIGNAL(editingFinished()), this, SLOT(onUserNameEdited())); connect(bodyUI->statusMessage, SIGNAL(editingFinished()), this, SLOT(onStatusMessageEdited())); - connect(bodyUI->loadButton, &QPushButton::clicked, this, &ProfileForm::onLoadClicked); connect(bodyUI->renameButton, &QPushButton::clicked, this, &ProfileForm::onRenameClicked); connect(bodyUI->exportButton, &QPushButton::clicked, this, &ProfileForm::onExportClicked); connect(bodyUI->deleteButton, &QPushButton::clicked, this, &ProfileForm::onDeleteClicked); - connect(bodyUI->importButton, &QPushButton::clicked, this, &ProfileForm::onImportClicked); - connect(bodyUI->newButton, &QPushButton::clicked, this, &ProfileForm::onNewClicked); - - connect(core, &Core::avStart, this, &ProfileForm::disableSwitching); - connect(core, &Core::avStarting, this, &ProfileForm::disableSwitching); - connect(core, &Core::avInvite, this, &ProfileForm::disableSwitching); - connect(core, &Core::avRinging, this, &ProfileForm::disableSwitching); - connect(core, &Core::avCancel, this, &ProfileForm::enableSwitching); - connect(core, &Core::avEnd, this, &ProfileForm::enableSwitching); - connect(core, &Core::avEnding, this, &ProfileForm::enableSwitching); - connect(core, &Core::avPeerTimeout, this, &ProfileForm::enableSwitching); - connect(core, &Core::avRequestTimeout, this, &ProfileForm::enableSwitching); + connect(bodyUI->logoutButton, &QPushButton::clicked, this, &ProfileForm::onLogoutClicked); connect(core, &Core::usernameSet, this, [=](const QString& val) { bodyUI->userName->setText(val); }); connect(core, &Core::statusMessageSet, this, [=](const QString& val) { bodyUI->statusMessage->setText(val); }); @@ -187,7 +164,6 @@ void ProfileForm::setToxId(const QString& id) qr = new QRWidget(); qr->setQRData("tox:"+id); bodyUI->qrCode->setPixmap(QPixmap::fromImage(qr->getImage()->scaledToWidth(150))); - refreshProfiles(); } void ProfileForm::onAvatarClicked() @@ -250,22 +226,9 @@ void ProfileForm::onAvatarClicked() Nexus::getCore()->setAvatar(bytes); } -void ProfileForm::onLoadClicked() -{ - if (bodyUI->profiles->currentText() != Settings::getInstance().getCurrentProfile()) - { - if (Core::getInstance()->anyActiveCalls()) - GUI::showWarning(tr("Call active", "popup title"), - tr("You can't switch profiles while a call is active!", "popup text")); - else - emit Widget::getInstance()->changeProfile(bodyUI->profiles->currentText()); - // I think by directly calling the function, I may have been causing thread issues - } -} - void ProfileForm::onRenameClicked() { - QString cur = bodyUI->profiles->currentText(); + /** TODO: Create a rename (low level) function in Profile, use it QString title = tr("Rename \"%1\"", "renaming a profile").arg(cur); do { @@ -297,11 +260,12 @@ void ProfileForm::onRenameClicked() break; } } while (true); + */ } void ProfileForm::onExportClicked() { - QString current = bodyUI->profiles->currentText() + Core::TOX_EXT; + QString current = Nexus::getProfile()->getName() + Core::TOX_EXT; QString path = QFileDialog::getSaveFileName(0, tr("Export profile", "save dialog title"), QDir::home().filePath(current), tr("Tox save file (*.tox)", "save dialog filter")); @@ -319,6 +283,7 @@ void ProfileForm::onExportClicked() void ProfileForm::onDeleteClicked() { + /** Add a delete function in profile if (Settings::getInstance().getCurrentProfile() == bodyUI->profiles->currentText()) { 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")); @@ -340,76 +305,22 @@ void ProfileForm::onDeleteClicked() bodyUI->profiles->setCurrentText(Settings::getInstance().getCurrentProfile()); } } + */ } -void ProfileForm::onImportClicked() +void ProfileForm::onLogoutClicked() { - QString path = QFileDialog::getOpenFileName(0, - tr("Import profile", "import dialog title"), - QDir::homePath(), - tr("Tox save file (*.tox)", "import dialog filter")); - if (path.isEmpty()) - return; - - QFileInfo info(path); - QString profile = info.completeBaseName(); - - if (info.suffix() != "tox") - { - GUI::showWarning(tr("Ignoring non-Tox file", "popup title"), - tr("Warning: you've chosen a file that is not a Tox save file; ignoring.", "popup text")); - return; - } - - QString profilePath = QDir(Settings::getSettingsDirPath()).filePath(profile + Core::TOX_EXT); - - if (QFileInfo(profilePath).exists() && !GUI::askQuestion(tr("Profile already exists", "import confirm title"), - tr("A profile named \"%1\" already exists. Do you want to erase it?", "import confirm text").arg(profile))) - return; - - QFile::copy(path, profilePath); - bodyUI->profiles->addItem(profile); + /// TODO: Save and call Nexus show login? } -void ProfileForm::onStatusSet(Status) -{ - refreshProfiles(); -} - -void ProfileForm::onNewClicked() -{ - emit Widget::getInstance()->changeProfile(QString()); -} - -void ProfileForm::disableSwitching() -{ - bodyUI->loadButton->setEnabled(false); - bodyUI->newButton->setEnabled(false); -} - -void ProfileForm::enableSwitching() -{ - if (!core->anyActiveCalls()) - { - bodyUI->loadButton->setEnabled(true); - bodyUI->newButton->setEnabled(true); - } -} - -void ProfileForm::showEvent(QShowEvent *event) -{ - refreshProfiles(); - QWidget::showEvent(event); -} - -void ProfileForm::on_copyQr_clicked() +void ProfileForm::onCopyQrClicked() { QApplication::clipboard()->setImage(*qr->getImage()); } -void ProfileForm::on_saveQr_clicked() +void ProfileForm::onSaveQrClicked() { - QString current = bodyUI->profiles->currentText() + ".png"; + QString current = Nexus::getProfile()->getName() + ".png"; QString path = QFileDialog::getSaveFileName(0, tr("Save", "save qr image"), QDir::home().filePath(current), tr("Save QrCode (*.png)", "save dialog filter")); @@ -424,14 +335,3 @@ void ProfileForm::on_saveQr_clicked() GUI::showWarning(tr("Failed to copy file"), tr("The file you chose could not be written to.")); } } - -bool ProfileForm::eventFilter(QObject *o, QEvent *e) -{ - if ((e->type() == QEvent::Wheel) && - (qobject_cast(o) || qobject_cast(o) )) - { - e->ignore(); - return true; - } - return QWidget::eventFilter(o, e); -} diff --git a/src/widget/form/profileform.h b/src/widget/form/profileform.h index 62e8c2244..e4828b9a2 100644 --- a/src/widget/form/profileform.h +++ b/src/widget/form/profileform.h @@ -56,7 +56,6 @@ signals: public slots: void onSelfAvatarLoaded(const QPixmap &pic); - void onStatusSet(Status status); private slots: void setToxId(const QString& id); @@ -64,21 +63,12 @@ private slots: void onAvatarClicked(); void onUserNameEdited(); void onStatusMessageEdited(); - void onLoadClicked(); void onRenameClicked(); void onExportClicked(); void onDeleteClicked(); - void onImportClicked(); - void onNewClicked(); - void disableSwitching(); - void enableSwitching(); - void on_copyQr_clicked(); - - void on_saveQr_clicked(); - -protected: - virtual void showEvent(QShowEvent *); - bool eventFilter(QObject *o, QEvent *e); + void onLogoutClicked(); + void onCopyQrClicked(); + void onSaveQrClicked(); private: void refreshProfiles(); diff --git a/src/widget/form/profileform.ui b/src/widget/form/profileform.ui index 03c73d16f..f02ae7155 100644 --- a/src/widget/form/profileform.ui +++ b/src/widget/form/profileform.ui @@ -37,10 +37,10 @@ - 0 + -3 0 - 565 - 617 + 630 + 625 @@ -160,55 +160,44 @@ Share it with your friends to communicate. - Profiles + Profile - - - - - - Available profiles: - - - - - - - - 0 - 0 - - - - Currently selected profile. - - - - - - - - Load selected profile and switch to it. + + + Qt::Horizontal - - Load + + + 40 + 20 + - + - Rename selected profile. + Rename profile. Rename + + + + Delete profile. + + + Delete + + + @@ -221,38 +210,27 @@ Profile does not contain your history. - + - Delete selected profile. + Go back to the login screen - Delete - - - - - - - - - - - Import Tox profile from a .tox file. - - - Import a profile + Logout - - - Create new Tox ID and switch to it. + + + Qt::Horizontal - - New Tox ID + + + 40 + 20 + - + diff --git a/src/widget/form/settings/generalform.cpp b/src/widget/form/settings/generalform.cpp index 630023ffe..067f5edf6 100644 --- a/src/widget/form/settings/generalform.cpp +++ b/src/widget/form/settings/generalform.cpp @@ -349,9 +349,9 @@ void GeneralForm::onReconnectClicked() { if (Core::getInstance()->anyActiveCalls()) QMessageBox::warning(this, tr("Call active", "popup title"), - tr("You can't disconnect while a call is active!", "popup text")); + tr("You can't disconnect while a call is active!", "popup text")); else - emit Widget::getInstance()->changeProfile(Settings::getInstance().getCurrentProfile()); + ; /// TODO: Add a reset function in Profile to save then restart toxcore } void GeneralForm::reloadSmiles() diff --git a/src/widget/form/settings/privacyform.cpp b/src/widget/form/settings/privacyform.cpp index c5c8d6521..f2d99d347 100644 --- a/src/widget/form/settings/privacyform.cpp +++ b/src/widget/form/settings/privacyform.cpp @@ -218,7 +218,7 @@ bool PrivacyForm::setToxPassword() core->setPassword(newpw, Core::ptMain); Settings::getInstance().setEncryptTox(true); - core->saveConfiguration(); + Settings::getInstance().save(); return true; } else diff --git a/src/widget/widget.h b/src/widget/widget.h index cb1cd0870..09ec716e2 100644 --- a/src/widget/widget.h +++ b/src/widget/widget.h @@ -131,7 +131,6 @@ signals: void statusSelected(Status status); void usernameChanged(const QString& username); void statusMessageChanged(const QString& statusMessage); - void changeProfile(const QString& profile); void resized(); private slots: