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

Merge pull request #3127

Diadlo (12):
      feat(toxme): Add ToxMe registration
      fix(toxme): Translation fixs
      fix(profileform): Fix tab order, fix loop
      fix(toxme): Use format strings
      feat(profileform): Added ability to change toxme server
      feat(toxme): Add save toxme info
      fix(toxme): Fix possible segfault
      fix(toxme): Fixed potential memory leaks
      fix(profileform): Fixed segfault on logut
      fix(profileform): Fixed very quick relogin segfault
      fix(toxme): Delete extra check
      refactor(toxme): Deleted old debug messages

Polshakov Dmitry (4):
      fix(profileform): Deleted extra check and extra url
      fix(profileform): Add toxme username limitation
      style(profileform): Changed local include brackets type
      refactor(profileform): Small changes
This commit is contained in:
sudden6 2016-04-16 00:55:01 +02:00
commit a5611705be
No known key found for this signature in database
GPG Key ID: 279509B499E032B9
8 changed files with 361 additions and 24 deletions

View File

@ -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))+"}"};

View File

@ -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);

View File

@ -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};

View File

@ -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;

View File

@ -18,7 +18,6 @@
*/
#include "addfriendform.h"
#include <QFont>
#include <QMessageBox>
#include <QErrorMessage>
@ -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"

View File

@ -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 <QLabel>
#include <QLineEdit>
#include <QGroupBox>
@ -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"));
}
}

View File

@ -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

View File

@ -6,7 +6,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>442</width>
<width>574</width>
<height>659</height>
</rect>
</property>
@ -39,11 +39,11 @@
<rect>
<x>0</x>
<y>0</y>
<width>585</width>
<height>625</height>
<width>536</width>
<height>844</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_4" stretch="0,0,1">
<layout class="QVBoxLayout" name="verticalLayout_4" stretch="0,0,0,1">
<property name="spacing">
<number>9</number>
</property>
@ -104,7 +104,11 @@ Share it with your friends to communicate.</string>
</widget>
</item>
<item>
<widget class="QLineEdit" name="toxId"/>
<widget class="QLineEdit" name="toxId">
<property name="frame">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QFrame" name="verticalFrame">
@ -149,6 +153,123 @@ Share it with your friends to communicate.</string>
</layout>
</widget>
</item>
<item>
<widget class="QGroupBox" name="horizontalGroupBox">
<property name="title">
<string>Toxme register</string>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<layout class="QFormLayout" name="formLayout">
<item row="0" column="0">
<widget class="QLabel" name="toxmeUsernameLabel">
<property name="text">
<string>Username</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="toxmeUsername">
<property name="autoFillBackground">
<bool>false</bool>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="toxmeBioLabel">
<property name="text">
<string>Biography</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QLineEdit" name="toxmeBio"/>
</item>
<item row="3" column="0">
<widget class="QLabel" name="toxmeServerLabel">
<property name="text">
<string>Server</string>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QComboBox" name="toxmeServersList">
<property name="enabled">
<bool>true</bool>
</property>
<property name="editable">
<bool>true</bool>
</property>
<property name="currentIndex">
<number>-1</number>
</property>
</widget>
</item>
<item row="4" column="1">
<widget class="QCheckBox" name="toxmePrivacy">
<property name="text">
<string>Hide my name from the public list</string>
</property>
</widget>
</item>
<item row="5" column="1">
<widget class="QPushButton" name="toxmeRegisterButton">
<property name="enabled">
<bool>true</bool>
</property>
<property name="text">
<string>Register</string>
</property>
<property name="autoDefault">
<bool>false</bool>
</property>
<property name="default">
<bool>false</bool>
</property>
<property name="flat">
<bool>false</bool>
</property>
</widget>
</item>
<item row="7" column="0">
<widget class="QLabel" name="toxmePasswordLabel">
<property name="enabled">
<bool>true</bool>
</property>
<property name="text">
<string>Your password</string>
</property>
</widget>
</item>
<item row="7" column="1">
<widget class="QLineEdit" name="toxmePassword">
<property name="frame">
<bool>false</bool>
</property>
<property name="readOnly">
<bool>true</bool>
</property>
<property name="clearButtonEnabled">
<bool>false</bool>
</property>
</widget>
</item>
<item row="6" column="1">
<layout class="QHBoxLayout" name="horizontalLayout_6">
<item>
<widget class="QPushButton" name="toxmeUpdateButton">
<property name="text">
<string>Update</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</item>
</layout>
</widget>
</item>
<item alignment="Qt::AlignTop">
<widget class="QGroupBox" name="profilesGroup">
<property name="title">
@ -318,6 +439,26 @@ Profile does not contain your history.</string>
<header location="global">src/widget/tool/croppinglabel.h</header>
</customwidget>
</customwidgets>
<tabstops>
<tabstop>scrollArea</tabstop>
<tabstop>userName</tabstop>
<tabstop>statusMessage</tabstop>
<tabstop>toxmeUsername</tabstop>
<tabstop>toxmeBio</tabstop>
<tabstop>toxmeServersList</tabstop>
<tabstop>toxmePrivacy</tabstop>
<tabstop>toxmeRegisterButton</tabstop>
<tabstop>toxmePassword</tabstop>
<tabstop>toxId</tabstop>
<tabstop>saveQr</tabstop>
<tabstop>copyQr</tabstop>
<tabstop>renameButton</tabstop>
<tabstop>deleteButton</tabstop>
<tabstop>logoutButton</tabstop>
<tabstop>exportButton</tabstop>
<tabstop>deletePassButton</tabstop>
<tabstop>changePassButton</tabstop>
</tabstops>
<resources/>
<connections/>
</ui>