From 204fe1d3dec00c0f4408f8a98c100377759f1218 Mon Sep 17 00:00:00 2001 From: Diadlo Date: Sun, 24 Jan 2016 00:50:57 +0300 Subject: [PATCH] feat(toxme): Add save toxme info --- src/net/toxme.cpp | 7 +- src/net/toxme.h | 4 +- src/persistence/settings.cpp | 80 +++++++++++ src/persistence/settings.h | 21 +++ src/widget/form/profileform.cpp | 49 ++++++- src/widget/form/profileform.h | 2 + src/widget/form/profileform.ui | 234 +++++++++++++++++--------------- 7 files changed, 278 insertions(+), 119 deletions(-) diff --git a/src/net/toxme.cpp b/src/net/toxme.cpp index 979d89764..835c778f1 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()) { @@ -239,7 +239,7 @@ QString Toxme::createAddress(ExecCode &code, QString server, ToxId id, QString a qDebug() << response; 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 +276,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))+"}"}; @@ -285,6 +285,7 @@ int Toxme::deleteAddress(QString server, ToxId id) if (!server.contains("://")) server = "https://" + server; + qDebug() << payload; QString pubkeyUrl = server + "/pk"; QString apiUrl = server + "/api"; QNetworkReply::NetworkError error = QNetworkReply::NoError; 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/profileform.cpp b/src/widget/form/profileform.cpp index a9906b254..30f0f57e1 100644 --- a/src/widget/form/profileform.cpp +++ b/src/widget/form/profileform.cpp @@ -81,9 +81,15 @@ ProfileForm::ProfileForm(QWidget *parent) : QVBoxLayout *toxIdGroup = qobject_cast(bodyUI->toxGroup->layout()); delete toxIdGroup->replaceWidget(bodyUI->toxId, toxId); // Original toxId is in heap, delete it bodyUI->toxId->hide(); - bodyUI->password->hide(); - bodyUI->passwordLabel->hide(); - bodyUI->serversList->addItem("toxme.io"); + + /* Toxme section init */ + bodyUI->toxmeServersList->addItem("toxme.io"); + bodyUI->toxmeServersList->addItem("toxme.io"); + QString toxmeInfo = Settings::getInstance().getToxmeInfo(); + if (toxmeInfo.isEmpty()) // User not registered + showRegisterToxme(); + else + showExistenToxme(); bodyUI->qrLabel->setWordWrap(true); @@ -115,7 +121,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->registerButton, &QPushButton::clicked, this, &ProfileForm::onRegisterButtonClicked); + 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); }); @@ -422,6 +429,39 @@ void ProfileForm::retranslateUi() 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::showExistenToxme() +{ + 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(); @@ -458,6 +498,7 @@ void ProfileForm::onRegisterButtonClicked() QString errorMessage = Toxme::getErrorMessage(code); QMessageBox::warning(this, tr("Toxme error"), errorMessage, "Ok"); } + bodyUI->toxmeRegisterButton->setEnabled(true); bodyUI->toxmeUpdateButton->setEnabled(true); bodyUI->toxmeRegisterButton->setText(tr("Register")); diff --git a/src/widget/form/profileform.h b/src/widget/form/profileform.h index 3187f454c..450ffe491 100644 --- a/src/widget/form/profileform.h +++ b/src/widget/form/profileform.h @@ -83,6 +83,7 @@ private slots: void onRegisterButtonClicked(); private: + void showExistenToxme(); void retranslateUi(); void prFileLabelUpdate(); @@ -98,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 9c21bec52..9118a4c6c 100644 --- a/src/widget/form/profileform.ui +++ b/src/widget/form/profileform.ui @@ -40,7 +40,7 @@ 0 0 536 - 802 + 844 @@ -86,109 +86,6 @@ - - - - Toxme register - - - - - - - - Biography - - - - - - - false - - - true - - - false - - - - - - - true - - - Your password - - - - - - - Hide my name from the public list - - - - - - - Register - - - false - - - false - - - false - - - - - - - Username - - - - - - - true - - - true - - - -1 - - - - - - - Server - - - - - - - false - - - - - - - - - - - @@ -256,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 + + + + + + + + + + @@ -429,12 +443,12 @@ Profile does not contain your history. scrollArea userName statusMessage - dnsUsername - bio - serversList - privacy - registerButton - password + toxmeUsername + toxmeBio + toxmeServersList + toxmePrivacy + toxmeRegisterButton + toxmePassword toxId saveQr copyQr