29 #include <QApplication>
31 #include <QErrorMessage>
32 #include <QFileDialog>
34 #include <QMessageBox>
35 #include <QRegularExpression>
36 #include <QScrollArea>
37 #include <QSignalMapper>
43 QString getToxId(
const QString&
id)
45 const QString toxUriPrefix{
"tox:"};
46 QString strippedId =
id.trimmed();
47 if (strippedId.startsWith(toxUriPrefix)) {
48 strippedId.remove(0, toxUriPrefix.length());
53 bool checkIsValidId(
const QString&
id)
67 tabWidget =
new QTabWidget();
68 main =
new QWidget(tabWidget);
72 headLabel.setFont(bold);
73 toxIdLabel.setTextFormat(Qt::RichText);
75 main->setLayout(&layout);
76 layout.addWidget(&toxIdLabel);
77 layout.addWidget(&toxId);
78 layout.addWidget(&messageLabel);
79 layout.addWidget(&message);
80 layout.addWidget(&sendButton);
81 tabWidget->addTab(
main, QString());
83 importContacts =
new QWidget(tabWidget);
84 importContacts->setLayout(&importContactsLayout);
85 importFileLine.addWidget(&importFileLabel);
86 importFileLine.addStretch();
87 importFileLine.addWidget(&importFileButton);
88 importContactsLayout.addLayout(&importFileLine);
89 importContactsLayout.addWidget(&importMessageLabel);
90 importContactsLayout.addWidget(&importMessage);
91 importContactsLayout.addWidget(&importSendButton);
92 tabWidget->addTab(importContacts, QString());
94 QScrollArea* scrollArea =
new QScrollArea(tabWidget);
95 QWidget* requestWidget =
new QWidget(tabWidget);
96 scrollArea->setWidget(requestWidget);
97 scrollArea->setWidgetResizable(
true);
98 requestsLayout =
new QVBoxLayout(requestWidget);
99 requestsLayout->addStretch(1);
100 tabWidget->addTab(scrollArea, QString());
102 head->setLayout(&headLayout);
103 headLayout.addWidget(&headLabel);
114 toxIdLabel.setAccessibleDescription(
115 tr(
"Tox ID, 76 hexadecimal characters"));
116 toxId.setAccessibleDescription(tr(
"Type in Tox ID of your friend"));
117 messageLabel.setAccessibleDescription(tr(
"Friend request message"));
118 message.setAccessibleDescription(tr(
119 "Type message to send with the friend request or leave empty to send a default message"));
120 message.setTabChangesFocus(
true);
126 for (
int i = 0; i < size; ++i) {
141 if (
head->isVisible()) {
142 head->window()->windowHandle()->alert(0);
160 const int index =
tabWidget->currentIndex();
166 const QString msg =
message.toPlainText();
167 return !msg.isEmpty() ? msg :
message.placeholderText();
173 return msg.isEmpty() ?
importMessage.placeholderText() : msg;
202 ToxId friendId(idText);
206 tr(
"%1 Tox ID is invalid",
"Tox address error").arg(idText));
211 if (friendId ==
ownId) {
214 tr(
"You can't add yourself as a friend!"));
222 const QString
id = getToxId(
toxId.text());
242 const QString path = QFileDialog::getOpenFileName(Q_NULLPTR, tr(
"Open contact list"));
243 if (path.isEmpty()) {
247 QFile contactFile(path);
248 if (!contactFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
251 tr(
"Couldn't open the contact file"));
256 qDebug() <<
"Import list:";
258 const QString
id = getToxId(*it);
259 if (checkIsValidId(
id)) {
265 qDebug() <<
"Invalid ID:" << *it;
273 tr(
"We couldn't find any contacts to import in this file!"));
281 const QString strippedId = getToxId(
id);
283 const bool isValidId = checkIsValidId(strippedId);
284 const bool isValidOrEmpty = strippedId.isEmpty() || isValidId;
287 const QString toxIdText(tr(
"Tox ID"));
289 const QString toxIdComment(tr(
"76 hexadecimal characters"));
291 const QString labelText =
292 isValidId ? QStringLiteral(
"%1 (%2)") : QStringLiteral(
"%1 <font color='red'>(%2)</font>");
293 toxIdLabel.setText(labelText.arg(toxIdText, toxIdComment));
294 toxId.setStyleSheet(isValidOrEmpty ? QStringLiteral(
"")
296 toxId.setToolTip(isValidOrEmpty ? QStringLiteral(
"") : tr(
"Invalid Tox ID format"));
303 const QClipboard* clipboard = QApplication::clipboard();
304 const QString trimmedId = clipboard->text().trimmed();
305 const QString strippedId = getToxId(trimmedId);
307 if (!strippedId.isEmpty() &&
ToxId::isToxId(strippedId) && isSelf) {
308 toxId.setText(trimmedId);
315 for (
int i = 0; i < size; ++i) {
326 QPushButton* acceptButton =
static_cast<QPushButton*
>(sender());
327 QWidget* friendWidget = acceptButton->parentWidget();
339 QPushButton* rejectButton =
static_cast<QPushButton*
>(sender());
340 QWidget* friendWidget = rejectButton->parentWidget();
361 static const QString messageLabelText = tr(
"Message");
367 sendButton.setText(tr(
"Send friend request"));
374 ? tr(
"Import a list of contacts, one Tox ID per line")
376 : tr(
"Ready to import %n contact(s), click send to confirm",
"",
contactsToImport.size()));
395 QWidget* friendWidget =
new QWidget(
tabWidget);
396 QHBoxLayout* friendLayout =
new QHBoxLayout(friendWidget);
397 QVBoxLayout* horLayout =
new QVBoxLayout();
398 horLayout->setMargin(0);
399 friendLayout->addLayout(horLayout);
402 friendLabel->setTextInteractionFlags(Qt::TextBrowserInteraction);
403 friendLabel->
setText(
"<b>" + friendAddress +
"</b>");
404 horLayout->addWidget(friendLabel);
408 messageLabel->setTextInteractionFlags(Qt::TextSelectableByMouse | Qt::TextSelectableByKeyboard);
413 QPushButton* acceptButton =
new QPushButton(friendWidget);
416 friendLayout->addWidget(acceptButton);
419 QPushButton* rejectButton =
new QPushButton(friendWidget);
422 friendLayout->addWidget(rejectButton);
434 friendWidget->deleteLater();
439 acceptButton->setText(tr(
"Accept"));
444 rejectButton->setText(tr(
"Reject"));