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

feat: UI to import a list of contacts from a file

Resolves #4181
This commit is contained in:
tux3 2017-02-17 03:14:16 +01:00 committed by Diadlo
parent e606d3cb55
commit 0974da543a
No known key found for this signature in database
GPG Key ID: 5AF9F2E29107C727
2 changed files with 150 additions and 21 deletions

View File

@ -31,6 +31,7 @@
#include <QClipboard> #include <QClipboard>
#include <QErrorMessage> #include <QErrorMessage>
#include <QFont> #include <QFont>
#include <QFileDialog>
#include <QMessageBox> #include <QMessageBox>
#include <QRegularExpression> #include <QRegularExpression>
#include <QScrollArea> #include <QScrollArea>
@ -53,7 +54,25 @@ AddFriendForm::AddFriendForm()
headLabel.setFont(bold); headLabel.setFont(bold);
toxIdLabel.setTextFormat(Qt::RichText); toxIdLabel.setTextFormat(Qt::RichText);
main->setLayout(&layout);
layout.addWidget(&toxIdLabel);
layout.addWidget(&toxId);
layout.addWidget(&messageLabel);
layout.addWidget(&message);
layout.addWidget(&sendButton);
tabWidget->addTab(main, QString()); tabWidget->addTab(main, QString());
importContacts = new QWidget(tabWidget);
importContacts->setLayout(&importContactsLayout);
importFileLine.addWidget(&importFileLabel);
importFileLine.addStretch();
importFileLine.addWidget(&importFileButton);
importContactsLayout.addLayout(&importFileLine);
importContactsLayout.addWidget(&importMessageLabel);
importContactsLayout.addWidget(&importMessage);
importContactsLayout.addWidget(&importSendButton);
tabWidget->addTab(importContacts, QString());
QScrollArea* scrollArea = new QScrollArea(tabWidget); QScrollArea* scrollArea = new QScrollArea(tabWidget);
QWidget* requestWidget = new QWidget(tabWidget); QWidget* requestWidget = new QWidget(tabWidget);
scrollArea->setWidget(requestWidget); scrollArea->setWidget(requestWidget);
@ -62,20 +81,15 @@ AddFriendForm::AddFriendForm()
requestsLayout->addStretch(1); requestsLayout->addStretch(1);
tabWidget->addTab(scrollArea, QString()); tabWidget->addTab(scrollArea, QString());
main->setLayout(&layout);
layout.addWidget(&toxIdLabel);
layout.addWidget(&toxId);
layout.addWidget(&messageLabel);
layout.addWidget(&message);
layout.addWidget(&sendButton);
head->setLayout(&headLayout); head->setLayout(&headLayout);
headLayout.addWidget(&headLabel); headLayout.addWidget(&headLabel);
connect(&toxId, &QLineEdit::returnPressed, this, &AddFriendForm::onSendTriggered); connect(&toxId, &QLineEdit::returnPressed, this, &AddFriendForm::onSendTriggered);
connect(&toxId, &QLineEdit::textChanged, this, &AddFriendForm::onIdChanged); connect(&toxId, &QLineEdit::textChanged, this, &AddFriendForm::onIdChanged);
connect(tabWidget, &QTabWidget::currentChanged, this, &AddFriendForm::onCurrentChanged); connect(tabWidget, &QTabWidget::currentChanged, this, &AddFriendForm::onCurrentChanged);
connect(&sendButton, SIGNAL(clicked()), this, SLOT(onSendTriggered())); connect(&sendButton, &QPushButton::clicked, this, &AddFriendForm::onSendTriggered);
connect(&importSendButton, &QPushButton::clicked, this, &AddFriendForm::onImportSendClicked);
connect(&importFileButton, &QPushButton::clicked, this, &AddFriendForm::onImportOpenClicked);
connect(Nexus::getCore(), &Core::usernameSet, this, &AddFriendForm::onUsernameSet); connect(Nexus::getCore(), &Core::usernameSet, this, &AddFriendForm::onUsernameSet);
// accessibility stuff // accessibility stuff
@ -136,6 +150,12 @@ QString AddFriendForm::getMessage() const
return !msg.isEmpty() ? msg : message.placeholderText(); return !msg.isEmpty() ? msg : message.placeholderText();
} }
QString AddFriendForm::getImportMessage() const
{
const QString msg = importMessage.toPlainText();
return msg.isEmpty() ? importMessage.placeholderText() : msg;
}
void AddFriendForm::setMode(Mode mode) void AddFriendForm::setMode(Mode mode)
{ {
tabWidget->setCurrentIndex(mode); tabWidget->setCurrentIndex(mode);
@ -145,8 +165,9 @@ bool AddFriendForm::addFriendRequest(const QString& friendAddress, const QString
{ {
if (Settings::getInstance().addFriendRequest(friendAddress, message)) { if (Settings::getInstance().addFriendRequest(friendAddress, message)) {
addFriendRequestWidget(friendAddress, message); addFriendRequestWidget(friendAddress, message);
if (isShown()) if (isShown()) {
onCurrentChanged(tabWidget->currentIndex()); onCurrentChanged(tabWidget->currentIndex());
}
return true; return true;
} }
@ -174,22 +195,94 @@ void AddFriendForm::onSendTriggered()
} }
deleteFriendRequest(friendId); deleteFriendRequest(friendId);
if (friendId == Core::getInstance()->getSelfId()) if (friendId == Core::getInstance()->getSelfId()) {
GUI::showWarning(tr("Couldn't add friend"), GUI::showWarning(tr("Couldn't add friend"),
tr("You can't add yourself as a friend!", tr("You can't add yourself as a friend!",
"When trying to add your own Tox ID as friend")); "When trying to add your own Tox ID as friend"));
else } else {
emit friendRequested(friendId, getMessage()); emit friendRequested(friendId, getMessage());
}
this->toxId.clear(); this->toxId.clear();
this->message.clear(); this->message.clear();
} }
void AddFriendForm::onImportSendClicked()
{
for (QString& idText : contactsToImport)
{
ToxId friendId(idText);
if (!friendId.isValid())
{
friendId = Toxme::lookup(idText); // Try Toxme
if (!friendId.isValid()) {
continue;
}
}
deleteFriendRequest(friendId);
if (friendId == Core::getInstance()->getSelfId()) {
GUI::showWarning(tr("Couldn't add friend"),
tr("You can't add yourself as a friend!",
"When trying to add your own Tox ID as friend"));
} else {
emit friendRequested(friendId, getImportMessage());
}
}
contactsToImport.clear();
importMessage.clear();
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()
{
const QString path = QFileDialog::getOpenFileName(tabWidget, tr("Open contact list"));
if (path.isEmpty()) {
return;
}
QFile contactFile(path);
if (!contactFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
GUI::showWarning(tr("Couldn't open file"),
tr("Couldn't open the contact file",
"Error message when trying to open a contact list file to import"));
return;
}
contactsToImport = QString::fromUtf8(contactFile.readAll()).split('\n');
QMutableListIterator<QString> it(contactsToImport);
qDebug() << "Import list:";
while (it.hasNext()) {
const QString id = it.value().trimmed();
const bool valid = !id.isEmpty() && checkIsValidId(id);
if (valid) {
it.value() = id;
} else {
it.remove();
}
qDebug() << it.next();
}
if (contactsToImport.isEmpty()) {
GUI::showWarning(tr("Invalid file"),
tr("We couldn't find any contacts to import in this file!"));
}
retranslateUi(); // Update the importFileLabel to show how many contacts we have
}
void AddFriendForm::onIdChanged(const QString& id) void AddFriendForm::onIdChanged(const QString& id)
{ {
QString tId = id.trimmed(); QString tId = id.trimmed();
QRegularExpression dnsIdExpression("^\\S+@\\S+$"); bool isValidId = tId.isEmpty() || checkIsValidId(tId);
bool isValidId = tId.isEmpty() || ToxId::isToxId(tId) || tId.contains(dnsIdExpression);
QString toxIdText(tr("Tox ID", "Tox ID of the person you're sending a friend request to")); QString toxIdText(tr("Tox ID", "Tox ID of the person you're sending a friend request to"));
QString toxIdComment( QString toxIdComment(
@ -266,23 +359,40 @@ void AddFriendForm::onCurrentChanged(int index)
void AddFriendForm::retranslateUi() void AddFriendForm::retranslateUi()
{ {
headLabel.setText(tr("Add Friends")); headLabel.setText(tr("Add Friends"));
messageLabel.setText(tr("Message", "The message you send in friend requests")); static const QString messageLabelText = tr("Message",
"The message you send in friend requests");
messageLabel.setText(messageLabelText);
importMessageLabel.setText(messageLabelText);
importFileButton.setText(tr("Open", "Button to choose a file with a list of contacts to import"));
importSendButton.setText(tr("Send friend requests"));
sendButton.setText(tr("Send friend request")); sendButton.setText(tr("Send friend request"));
message.setPlaceholderText(tr("%1 here! Tox me maybe?", "Default message in friend requests if " message.setPlaceholderText(tr("%1 here! Tox me maybe?", "Default message in friend requests if "
"the field is left blank. Write " "the field is left blank. Write "
"something appropriate!") "something appropriate!")
.arg(lastUsername)); .arg(lastUsername));
importMessage.setPlaceholderText(message.placeholderText());
if (contactsToImport.isEmpty()) {
importFileLabel.setText(tr("Import a list of contacts, one Tox ID per line"));
} else {
importFileLabel.setText(tr("Ready to import %n contact(s), click send to confirm",
"Shows the number of contacts we're about to import from a file"
" (at least one)", contactsToImport.size()));
}
onIdChanged(toxId.text()); onIdChanged(toxId.text());
tabWidget->setTabText(0, tr("Add a friend")); tabWidget->setTabText(0, tr("Add a friend"));
tabWidget->setTabText(1, tr("Friend requests")); tabWidget->setTabText(1, tr("Import contacts"));
tabWidget->setTabText(2, tr("Friend requests"));
for (QPushButton* acceptButton : acceptButtons) for (QPushButton* acceptButton : acceptButtons) {
retranslateAcceptButton(acceptButton); retranslateAcceptButton(acceptButton);
}
for (QPushButton* rejectButton : rejectButtons) for (QPushButton* rejectButton : rejectButtons) {
retranslateRejectButton(rejectButton); retranslateRejectButton(rejectButton);
}
} }
void AddFriendForm::addFriendRequestWidget(const QString& friendAddress, const QString& message) void AddFriendForm::addFriendRequestWidget(const QString& friendAddress, const QString& message)

View File

@ -22,6 +22,8 @@
#include "src/core/toxid.h" #include "src/core/toxid.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel> #include <QLabel>
#include <QLineEdit> #include <QLineEdit>
#include <QPushButton> #include <QPushButton>
@ -51,7 +53,6 @@ public:
bool isShown() const; bool isShown() const;
void show(ContentLayout* contentLayout); void show(ContentLayout* contentLayout);
QString getMessage() const;
void setMode(Mode mode); void setMode(Mode mode);
bool addFriendRequest(const QString& friendAddress, const QString& message); bool addFriendRequest(const QString& friendAddress, const QString& message);
@ -67,6 +68,8 @@ public slots:
private slots: private slots:
void onSendTriggered(); void onSendTriggered();
void onIdChanged(const QString& id); void onIdChanged(const QString& id);
void onImportSendClicked();
void onImportOpenClicked();
void onFriendRequestAccepted(); void onFriendRequestAccepted();
void onFriendRequestRejected(); void onFriendRequestRejected();
void onCurrentChanged(int index); void onCurrentChanged(int index);
@ -79,19 +82,35 @@ private:
void retranslateRejectButton(QPushButton* rejectButton); void retranslateRejectButton(QPushButton* rejectButton);
void deleteFriendRequest(const ToxId& toxId); void deleteFriendRequest(const ToxId& toxId);
void setIdFromClipboard(); void setIdFromClipboard();
QString getMessage() const;
QString getImportMessage() const;
private: private:
QLabel headLabel, toxIdLabel, messageLabel; QLabel headLabel;
QLabel toxIdLabel;
QLabel messageLabel;
QLabel importFileLabel;
QLabel importMessageLabel;
QPushButton sendButton; QPushButton sendButton;
QPushButton importFileButton;
QPushButton importSendButton;
QLineEdit toxId; QLineEdit toxId;
QTextEdit message; QTextEdit message;
QVBoxLayout layout, headLayout; QTextEdit importMessage;
QWidget *head, *main; QVBoxLayout layout;
QVBoxLayout headLayout;
QVBoxLayout importContactsLayout;
QHBoxLayout importFileLine;
QWidget* head;
QWidget* main;
QWidget* importContacts;
QString lastUsername; QString lastUsername;
QTabWidget* tabWidget; QTabWidget* tabWidget;
QVBoxLayout* requestsLayout; QVBoxLayout* requestsLayout;
QList<QPushButton*> acceptButtons; QList<QPushButton*> acceptButtons;
QList<QPushButton*> rejectButtons; QList<QPushButton*> rejectButtons;
QList<QString> contactsToImport;
}; };
#endif // ADDFRIENDFORM_H #endif // ADDFRIENDFORM_H