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

fix(ui): allow adding friends with Tox URI

Fix #5385
This commit is contained in:
Anthony Bilinski 2018-10-13 12:22:42 -07:00
parent bc51e89807
commit dec90ad84d
No known key found for this signature in database
GPG Key ID: 2AA8E0DA1B31FB3C

View File

@ -38,6 +38,25 @@
#include <QTabWidget> #include <QTabWidget>
#include <QWindow> #include <QWindow>
namespace
{
QString getToxId(const QString& id)
{
const QString toxUriPrefix{"tox:"};
QString strippedId = id.trimmed();
if (strippedId.startsWith(toxUriPrefix)) {
strippedId.remove(0, toxUriPrefix.length());
}
return strippedId;
}
bool checkIsValidId(const QString& id)
{
static const QRegularExpression dnsIdExpression("^\\S+@\\S+$");
return ToxId::isToxId(id) || id.contains(dnsIdExpression);
}
}
/** /**
* @var QString AddFriendForm::lastUsername * @var QString AddFriendForm::lastUsername
* @brief Cached username so we can retranslate the invite message * @brief Cached username so we can retranslate the invite message
@ -202,8 +221,8 @@ void AddFriendForm::addFriend(const QString& idText)
void AddFriendForm::onSendTriggered() void AddFriendForm::onSendTriggered()
{ {
const QString idText = toxId.text().trimmed(); const QString id = getToxId(toxId.text());
addFriend(idText); addFriend(id);
this->toxId.clear(); this->toxId.clear();
this->message.clear(); this->message.clear();
@ -211,8 +230,8 @@ void AddFriendForm::onSendTriggered()
void AddFriendForm::onImportSendClicked() void AddFriendForm::onImportSendClicked()
{ {
for (const QString& idText : contactsToImport) { for (const QString& id : contactsToImport) {
addFriend(idText); addFriend(id);
} }
contactsToImport.clear(); contactsToImport.clear();
@ -220,12 +239,6 @@ void AddFriendForm::onImportSendClicked()
retranslateUi(); // Update the importFileLabel retranslateUi(); // Update the importFileLabel
} }
static inline bool checkIsValidId(const QString& id)
{
static const QRegularExpression dnsIdExpression("^\\S+@\\S+$");
return ToxId::isToxId(id) || id.contains(dnsIdExpression);
}
void AddFriendForm::onImportOpenClicked() void AddFriendForm::onImportOpenClicked()
{ {
const QString path = QFileDialog::getOpenFileName(Q_NULLPTR, tr("Open contact list")); const QString path = QFileDialog::getOpenFileName(Q_NULLPTR, tr("Open contact list"));
@ -244,7 +257,7 @@ void AddFriendForm::onImportOpenClicked()
contactsToImport = QString::fromUtf8(contactFile.readAll()).split('\n'); contactsToImport = QString::fromUtf8(contactFile.readAll()).split('\n');
qDebug() << "Import list:"; qDebug() << "Import list:";
for (auto it = contactsToImport.begin(); it != contactsToImport.end();) { for (auto it = contactsToImport.begin(); it != contactsToImport.end();) {
const QString id = it->trimmed(); const QString id = getToxId(*it);
if (checkIsValidId(id)) { if (checkIsValidId(id)) {
*it = id; *it = id;
qDebug() << *it; qDebug() << *it;
@ -267,8 +280,10 @@ void AddFriendForm::onImportOpenClicked()
void AddFriendForm::onIdChanged(const QString& id) void AddFriendForm::onIdChanged(const QString& id)
{ {
const QString tId = id.trimmed(); const QString strippedId = getToxId(id);
const bool isValidId = tId.isEmpty() || checkIsValidId(tId);
const bool isValidId = checkIsValidId(strippedId);
const bool isValidOrEmpty = strippedId.isEmpty() || isValidId;
//: Tox ID of the person you're sending a friend request to //: Tox ID of the person you're sending a friend request to
const QString toxIdText(tr("Tox ID")); const QString toxIdText(tr("Tox ID"));
@ -278,21 +293,22 @@ void AddFriendForm::onIdChanged(const QString& id)
const QString labelText = const QString labelText =
isValidId ? QStringLiteral("%1 (%2)") : QStringLiteral("%1 <font color='red'>(%2)</font>"); isValidId ? QStringLiteral("%1 (%2)") : QStringLiteral("%1 <font color='red'>(%2)</font>");
toxIdLabel.setText(labelText.arg(toxIdText, toxIdComment)); toxIdLabel.setText(labelText.arg(toxIdText, toxIdComment));
toxId.setStyleSheet(isValidId ? QStringLiteral("") toxId.setStyleSheet(isValidOrEmpty ? QStringLiteral("")
: QStringLiteral("QLineEdit { background-color: #FFC1C1; }")); : QStringLiteral("QLineEdit { background-color: #FFC1C1; }"));
toxId.setToolTip(isValidId ? QStringLiteral("") : tr("Invalid Tox ID format")); toxId.setToolTip(isValidOrEmpty ? QStringLiteral("") : tr("Invalid Tox ID format"));
sendButton.setEnabled(isValidId && !tId.isEmpty()); sendButton.setEnabled(isValidId);
} }
void AddFriendForm::setIdFromClipboard() void AddFriendForm::setIdFromClipboard()
{ {
const QClipboard* clipboard = QApplication::clipboard(); const QClipboard* clipboard = QApplication::clipboard();
const QString id = clipboard->text().trimmed(); const QString trimmedId = clipboard->text().trimmed();
const QString strippedId = getToxId(trimmedId);
const Core* core = Core::getInstance(); const Core* core = Core::getInstance();
const bool isSelf = core->isReady() && ToxId(id) != core->getSelfId(); const bool isSelf = core->isReady() && ToxId(strippedId) != core->getSelfId();
if (!id.isEmpty() && ToxId::isToxId(id) && isSelf) { if (!strippedId.isEmpty() && ToxId::isToxId(strippedId) && isSelf) {
toxId.setText(id); toxId.setText(trimmedId);
} }
} }