diff --git a/src/net/toxme.cpp b/src/net/toxme.cpp index d912a3857..5a91d1e29 100644 --- a/src/net/toxme.cpp +++ b/src/net/toxme.cpp @@ -39,7 +39,7 @@ QByteArray Toxme::makeJsonRequest(QString url, QString json, QNetworkReply::Netw netman.setProxy(Settings::getInstance().getProxy()); QNetworkRequest request{url}; request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json"); - QNetworkReply* reply = netman.post(request,json.toUtf8()); + QNetworkReply* reply = netman.post(request, json.toUtf8()); while (!reply->isFinished()) { @@ -47,14 +47,9 @@ QByteArray Toxme::makeJsonRequest(QString url, QString json, QNetworkReply::Netw qApp->processEvents(); } - error = reply->error(); - if (error) - { - qWarning() << "makeJsonRequest: A network error occured:" << reply->errorString(); - return QByteArray(); - } - - return reply->readAll(); + QByteArray result = reply->readAll(); + delete reply; + return result; } QByteArray Toxme::getServerPubkey(QString url, QNetworkReply::NetworkError &error) @@ -86,6 +81,7 @@ QByteArray Toxme::getServerPubkey(QString url, QNetworkReply::NetworkError &erro static const QByteArray pattern{"key\":\""}; QString json = reply->readAll(); + delete reply; json = json.remove(' '); int start = json.indexOf(pattern) + pattern.length(); int end = json.indexOf("\"", start); @@ -231,15 +227,14 @@ QString Toxme::createAddress(ExecCode &code, QString server, ToxId id, QString a "\"bio\":\""+bio+"\"," "\"timestamp\":"+QString().setNum(time(0))+"}"}; - qDebug() << payload; QString pubkeyUrl = server + "/pk"; QString apiUrl = server + "/api"; QNetworkReply::NetworkError error = QNetworkReply::NoError; - QByteArray response = makeJsonRequest(apiUrl, prepareEncryptedJson(pubkeyUrl, 1, payload), error); - qDebug() << response; + QByteArray encrypted = prepareEncryptedJson(pubkeyUrl, 1, payload); + QByteArray response = makeJsonRequest(apiUrl, encrypted, error); code = extractError(response); - if ((code != Registered && code != Updated) || error != QNetworkReply::NoError) + if ((code != Ok && code != Updated) || error != QNetworkReply::NoError) return QString(); return getPass(response, code); @@ -276,7 +271,7 @@ QString Toxme::getPass(QString json, ExecCode &code) { return json; } -int Toxme::deleteAddress(QString server, ToxId id) +Toxme::ExecCode Toxme::deleteAddress(QString server, ToxId id) { const QString payload{"{\"public_key\":\""+id.toString().left(64)+"\"," "\"timestamp\":"+QString().setNum(time(0))+"}"}; diff --git a/src/net/toxme.h b/src/net/toxme.h index 9d5a2e709..4ca630fb3 100644 --- a/src/net/toxme.h +++ b/src/net/toxme.h @@ -38,7 +38,7 @@ class Toxme public: enum ExecCode { ExecError = -50, - Registered = 0, + Ok = 0, Updated = 1, ServerError = 2, IncorrectResponse = 3, @@ -54,7 +54,7 @@ public: static QString createAddress(ExecCode &code, QString server, ToxId id, QString address, bool keepPrivate=true, QString bio=QString()); /// Deletes the address associated with your current Tox ID - static int deleteAddress(QString server, ToxId id); + static ExecCode deleteAddress(QString server, ToxId id); /// Return string of the corresponding error code static QString getErrorMessage(int errorCode); diff --git a/src/persistence/settings.cpp b/src/persistence/settings.cpp index 8c5a40559..3dcbfcf5f 100644 --- a/src/persistence/settings.cpp +++ b/src/persistence/settings.cpp @@ -359,6 +359,13 @@ void Settings::loadPersonal(Profile* profile) } ps.endArray(); ps.endGroup(); + + ps.beginGroup("Toxme"); + toxmeInfo = ps.value("info", "").toString(); + toxmeBio = ps.value("bio", "").toString(); + toxmePriv = ps.value("priv", false).toBool(); + toxmePass = ps.value("pass", "").toString(); + ps.endGroup(); } void Settings::saveGlobal() @@ -555,6 +562,13 @@ void Settings::savePersonal(QString profileName, QString password) ps.setValue("enableLogging", enableLogging); ps.endGroup(); + ps.beginGroup("Toxme"); + ps.setValue("info", toxmeInfo); + ps.setValue("bio", toxmeBio); + ps.setValue("priv", toxmePriv); + ps.setValue("pass", toxmePass); + ps.endGroup(); + ps.save(); } @@ -838,6 +852,72 @@ void Settings::setTranslation(QString newValue) translation = newValue; } +void Settings::deleteToxme() +{ + setToxmeInfo(""); + setToxmeBio(""); + setToxmePriv(""); + setToxmePass(""); +} + +void Settings::setToxme(QString name, QString server, QString bio, bool priv, QString pass) +{ + setToxmeInfo(name + "@" + server); + setToxmeBio(bio); + setToxmePriv(priv); + if (!pass.isEmpty()) + setToxmePass(pass); +} + +QString Settings::getToxmeInfo() const +{ + QMutexLocker locker{&bigLock}; + return toxmeInfo; +} + +void Settings::setToxmeInfo(QString info) +{ + QMutexLocker locker{&bigLock}; + if (info.split("@").size() == 2) + toxmeInfo = info; +} + +QString Settings::getToxmeBio() const +{ + QMutexLocker locker{&bigLock}; + return toxmeBio; +} + +void Settings::setToxmeBio(QString bio) +{ + QMutexLocker locker{&bigLock}; + toxmeBio = bio; +} + +bool Settings::getToxmePriv() const +{ + QMutexLocker locker{&bigLock}; + return toxmePriv; +} + +void Settings::setToxmePriv(bool priv) +{ + QMutexLocker locker{&bigLock}; + toxmePriv = priv; +} + +QString Settings::getToxmePass() const +{ + QMutexLocker locker{&bigLock}; + return toxmePass; +} + +void Settings::setToxmePass(QString pass) +{ + QMutexLocker locker{&bigLock}; + toxmePass = pass; +} + bool Settings::getForceTCP() const { QMutexLocker locker{&bigLock}; diff --git a/src/persistence/settings.h b/src/persistence/settings.h index d029085c9..4e2ed014a 100644 --- a/src/persistence/settings.h +++ b/src/persistence/settings.h @@ -108,6 +108,21 @@ public: QString getTranslation() const; void setTranslation(QString newValue); + // Toxme + void deleteToxme(); + void setToxme(QString name, QString server, QString bio, bool priv, QString pass = ""); + QString getToxmeInfo() const; + void setToxmeInfo(QString info); + + QString getToxmeBio() const; + void setToxmeBio(QString bio); + + bool getToxmePriv() const; + void setToxmePriv(bool priv); + + QString getToxmePass() const; + void setToxmePass(QString pass); + void setAutoSaveEnabled(bool newValue); bool getAutoSaveEnabled() const; @@ -359,6 +374,12 @@ private: QString currentProfile; uint32_t currentProfileId; + // Toxme Info + QString toxmeInfo; // name@server + QString toxmeBio; + bool toxmePriv; + QString toxmePass; + bool enableLogging; int autoAwayTime; diff --git a/src/widget/form/addfriendform.cpp b/src/widget/form/addfriendform.cpp index a98430466..4ca5731f1 100644 --- a/src/widget/form/addfriendform.cpp +++ b/src/widget/form/addfriendform.cpp @@ -18,7 +18,6 @@ */ #include "addfriendform.h" - #include #include #include @@ -32,6 +31,7 @@ #include "src/core/core.h" #include "src/core/cdata.h" #include "src/net/toxdns.h" +#include "src/net/toxme.h" #include "src/persistence/settings.h" #include "src/widget/gui.h" #include "src/widget/translator.h" diff --git a/src/widget/form/profileform.cpp b/src/widget/form/profileform.cpp index f03747b5a..4e5d83dad 100644 --- a/src/widget/form/profileform.cpp +++ b/src/widget/form/profileform.cpp @@ -24,15 +24,16 @@ #include "src/widget/form/settingswidget.h" #include "src/widget/maskablepixmapwidget.h" #include "src/widget/form/setpassworddialog.h" -#include "src/persistence/settings.h" #include "src/widget/contentlayout.h" #include "src/widget/tool/croppinglabel.h" #include "src/widget/widget.h" #include "src/widget/gui.h" #include "src/widget/style.h" +#include "src/widget/translator.h" #include "src/persistence/profilelocker.h" #include "src/persistence/profile.h" -#include "src/widget/translator.h" +#include "src/persistence/settings.h" +#include "src/net/toxme.h" #include #include #include @@ -81,8 +82,20 @@ ProfileForm::ProfileForm(QWidget *parent) : delete toxIdGroup->replaceWidget(bodyUI->toxId, toxId); // Original toxId is in heap, delete it bodyUI->toxId->hide(); + /* Toxme section init */ + bodyUI->toxmeServersList->addItem("toxme.io"); + QString toxmeInfo = Settings::getInstance().getToxmeInfo(); + if (toxmeInfo.isEmpty()) // User not registered + showRegisterToxme(); + else + showExistingToxme(); + bodyUI->qrLabel->setWordWrap(true); + QRegExp re("[^@ ]+"); + QRegExpValidator* validator = new QRegExpValidator(re); + bodyUI->toxmeUsername->setValidator(validator); + profilePicture = new MaskablePixmapWidget(this, QSize(64, 64), ":/img/avatar_mask.svg"); profilePicture->setPixmap(QPixmap(":/img/contact_dark.svg")); profilePicture->setContextMenuPolicy(Qt::CustomContextMenu); @@ -111,6 +124,8 @@ ProfileForm::ProfileForm(QWidget *parent) : connect(bodyUI->changePassButton, &QPushButton::clicked, this, &ProfileForm::onChangePassClicked); connect(bodyUI->saveQr, &QPushButton::clicked, this, &ProfileForm::onSaveQrClicked); connect(bodyUI->copyQr, &QPushButton::clicked, this, &ProfileForm::onCopyQrClicked); + connect(bodyUI->toxmeRegisterButton, &QPushButton::clicked, this, &ProfileForm::onRegisterButtonClicked); + connect(bodyUI->toxmeUpdateButton, &QPushButton::clicked, this, &ProfileForm::onRegisterButtonClicked); connect(core, &Core::usernameSet, this, [=](const QString& val) { bodyUI->userName->setText(val); }); connect(core, &Core::statusMessageSet, this, [=](const QString& val) { bodyUI->statusMessage->setText(val); }); @@ -416,3 +431,85 @@ void ProfileForm::retranslateUi() // We have to add the toxId tooltip here and not in the .ui or Qt won't know how to translate it dynamically toxId->setToolTip(tr("This bunch of characters tells other Tox clients how to contact you.\nShare it with your friends to communicate.")); } + +void ProfileForm::showRegisterToxme() +{ + bodyUI->toxmeUsername->setText(""); + bodyUI->toxmeBio->setText(""); + bodyUI->toxmePrivacy->setChecked(false); + + bodyUI->toxmeRegisterButton->show(); + bodyUI->toxmeUpdateButton->hide(); + bodyUI->toxmePassword->hide(); + bodyUI->toxmePasswordLabel->hide(); +} + +void ProfileForm::showExistingToxme() +{ + QStringList info = Settings::getInstance().getToxmeInfo().split("@"); + bodyUI->toxmeUsername->setText(info[0]); + bodyUI->toxmeServersList->addItem(info[1]); + + QString bio = Settings::getInstance().getToxmeBio(); + bodyUI->toxmeBio->setText(bio); + + bool priv = Settings::getInstance().getToxmePriv(); + bodyUI->toxmePrivacy->setChecked(priv); + + QString pass = Settings::getInstance().getToxmePass(); + bodyUI->toxmePassword->setText(pass); + bodyUI->toxmePassword->show(); + bodyUI->toxmePasswordLabel->show(); + + bodyUI->toxmeRegisterButton->hide(); + bodyUI->toxmeUpdateButton->show(); +} + +void ProfileForm::onRegisterButtonClicked() +{ + QString name = bodyUI->toxmeUsername->text(); + if (name.isEmpty()) + return; + + bodyUI->toxmeRegisterButton->setEnabled(false); + bodyUI->toxmeUpdateButton->setEnabled(false); + bodyUI->toxmeRegisterButton->setText(tr("Register (processing)")); + bodyUI->toxmeUpdateButton->setText(tr("Update (processing)")); + + QString id = toxId->text(); + QString bio = bodyUI->toxmeBio->text(); + QString server = bodyUI->toxmeServersList->currentText(); + bool privacy = bodyUI->toxmePrivacy->isChecked(); + + Core* oldCore = Core::getInstance(); + + Toxme::ExecCode code = Toxme::ExecCode::Ok; + QString response = Toxme::createAddress(code, server, id, name, privacy, bio); + + Core* newCore = Core::getInstance(); + // Make sure the user didn't logout (or logout and login) + // before the request is finished, else qTox will crash. + if (oldCore == newCore) + { + switch (code) { + case Toxme::Updated: + GUI::showInfo(tr("Done!"), tr("Account %1@%2 updated successfully").arg(name, server)); + Settings::getInstance().setToxme(name, server, bio, privacy); + showExistingToxme(); + break; + case Toxme::Ok: + GUI::showInfo(tr("Done!"), tr("Successfully added %1@%2 to the database. Save your password").arg(name, server)); + Settings::getInstance().setToxme(name, server, bio, privacy, response); + showExistingToxme(); + break; + default: + QString errorMessage = Toxme::getErrorMessage(code); + GUI::showWarning(tr("Toxme error"), errorMessage); + } + + bodyUI->toxmeRegisterButton->setEnabled(true); + bodyUI->toxmeUpdateButton->setEnabled(true); + bodyUI->toxmeRegisterButton->setText(tr("Register")); + bodyUI->toxmeUpdateButton->setText(tr("Update")); + } +} diff --git a/src/widget/form/profileform.h b/src/widget/form/profileform.h index 1f6ee0942..9575655cc 100644 --- a/src/widget/form/profileform.h +++ b/src/widget/form/profileform.h @@ -80,8 +80,10 @@ private slots: void onChangePassClicked(); void onAvatarClicked(); void showProfilePictureContextMenu(const QPoint &point); + void onRegisterButtonClicked(); private: + void showExistingToxme(); void retranslateUi(); void prFileLabelUpdate(); @@ -97,6 +99,7 @@ private: bool hasCheck = false; QRWidget *qr; ClickableTE* toxId; + void showRegisterToxme(); }; #endif diff --git a/src/widget/form/profileform.ui b/src/widget/form/profileform.ui index 88ba39849..9118a4c6c 100644 --- a/src/widget/form/profileform.ui +++ b/src/widget/form/profileform.ui @@ -6,7 +6,7 @@ 0 0 - 442 + 574 659 @@ -39,11 +39,11 @@ 0 0 - 585 - 625 + 536 + 844 - + 9 @@ -104,7 +104,11 @@ Share it with your friends to communicate. - + + + true + + @@ -149,6 +153,123 @@ Share it with your friends to communicate. + + + + Toxme register + + + + + + + + Username + + + + + + + false + + + + + + + Biography + + + + + + + + + + Server + + + + + + + true + + + true + + + -1 + + + + + + + Hide my name from the public list + + + + + + + true + + + Register + + + false + + + false + + + false + + + + + + + true + + + Your password + + + + + + + false + + + true + + + false + + + + + + + + + Update + + + + + + + + + + @@ -318,6 +439,26 @@ Profile does not contain your history.
src/widget/tool/croppinglabel.h
+ + scrollArea + userName + statusMessage + toxmeUsername + toxmeBio + toxmeServersList + toxmePrivacy + toxmeRegisterButton + toxmePassword + toxId + saveQr + copyQr + renameButton + deleteButton + logoutButton + exportButton + deletePassButton + changePassButton +