qTox  Version: nightly | Commit: bc751c8e1cac455f9690654fcfe0f560d2d7dfdd
toxuri.cpp
Go to the documentation of this file.
1 /*
2  Copyright © 2014-2019 by The qTox Project Contributors
3 
4  This file is part of qTox, a Qt-based graphical interface for Tox.
5 
6  qTox is libre software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  qTox is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with qTox. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #include "src/net/toxuri.h"
21 #include "src/core/core.h"
22 #include "src/widget/gui.h"
23 #include <QByteArray>
24 #include <QCoreApplication>
25 #include <QDialogButtonBox>
26 #include <QLabel>
27 #include <QLineEdit>
28 #include <QMessageBox>
29 #include <QPlainTextEdit>
30 #include <QPushButton>
31 #include <QString>
32 #include <QThread>
33 #include <QVBoxLayout>
34 
41 bool ToxURIDialog::handleToxURI(const QString& toxURI)
42 {
43  QString toxaddr = toxURI.mid(4);
44 
45  ToxId toxId(toxaddr);
46  QString error = QString();
47  if (!toxId.isValid()) {
48  error = QMessageBox::tr("%1 is not a valid Tox address.").arg(toxaddr);
49  } else if (toxId == core.getSelfId()) {
50  error = QMessageBox::tr("You can't add yourself as a friend!",
51  "When trying to add your own Tox ID as friend");
52  }
53 
54  if (!error.isEmpty()) {
55  GUI::showWarning(QMessageBox::tr("Couldn't add friend"), error);
56  return false;
57  }
58 
59  setUserId(toxURI);
60 
61  int result = exec();
62  if (result == QDialog::Accepted) {
64  }
65 
66  return true;
67 }
68 
69 void ToxURIDialog::setUserId(const QString& userId)
70 {
71  friendsLabel->setText(tr("Do you want to add %1 as a friend?").arg(userId));
72  userIdEdit->setText(userId);
73 }
74 
75 ToxURIDialog::ToxURIDialog(QWidget* parent, Core& _core)
76  : QDialog(parent)
77  , core{_core}
78 {
79  const QString defaultMessage =
80  QObject::tr("%1 here! Tox me maybe?",
81  "Default message in Tox URI friend requests. Write something appropriate!");
82  const QString username = core.getUsername();
83  const QString message = defaultMessage.arg(username);
84 
85  setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
86  setWindowTitle(tr("Add a friend", "Title of the window to add a friend through Tox URI"));
87 
88  friendsLabel = new QLabel("", this);
89  userIdEdit = new QLineEdit("", this);
90  userIdEdit->setCursorPosition(0);
91  userIdEdit->setReadOnly(true);
92 
93  QLabel* userIdLabel = new QLabel(tr("User ID:"), this);
94  QLabel* messageLabel = new QLabel(tr("Friend request message:"), this);
95  messageEdit = new QPlainTextEdit(message, this);
96 
97  QDialogButtonBox* buttonBox = new QDialogButtonBox(Qt::Horizontal, this);
98 
99  buttonBox->addButton(tr("Send", "Send a friend request"), QDialogButtonBox::AcceptRole);
100  buttonBox->addButton(tr("Cancel", "Don't send a friend request"), QDialogButtonBox::RejectRole);
101 
102  connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
103  connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
104 
105  QVBoxLayout* layout = new QVBoxLayout(this);
106 
107  layout->addWidget(friendsLabel);
108  layout->addSpacing(12);
109  layout->addWidget(userIdLabel);
110  layout->addWidget(userIdEdit);
111  layout->addWidget(messageLabel);
112  layout->addWidget(messageEdit);
113  layout->addWidget(buttonBox);
114 
115  resize(300, 200);
116 }
117 
119 {
120  return messageEdit->toPlainText();
121 }
ToxURIDialog::ToxURIDialog
ToxURIDialog(QWidget *parent, Core &_core)
Definition: toxuri.cpp:75
ToxId::isValid
bool isValid() const
Check it it's a valid Tox ID by verifying the checksum.
Definition: toxid.cpp:239
ToxURIDialog::getRequestMessage
QString getRequestMessage()
Definition: toxuri.cpp:118
ToxURIDialog::core
Core & core
Definition: toxuri.h:46
Core::getSelfId
ToxId getSelfId() const override
Returns our Tox ID.
Definition: core.cpp:1258
toxuri.h
HistMessageContentType::message
@ message
Core::requestFriendship
void requestFriendship(const ToxId &friendAddress, const QString &message)
Definition: core.cpp:1059
ToxId
This class represents a Tox ID.
Definition: toxid.h:29
ToxURIDialog::messageEdit
QPlainTextEdit * messageEdit
Definition: toxuri.h:43
GUI::showWarning
static void showWarning(const QString &title, const QString &msg)
Show a warning to the user.
Definition: gui.cpp:130
core.h
ToxURIDialog::handleToxURI
bool handleToxURI(const QString &toxURI)
Shows a dialog asking whether or not to add this tox address as a friend.
Definition: toxuri.cpp:41
gui.h
ToxURIDialog::setUserId
void setUserId(const QString &userId)
Definition: toxuri.cpp:69
ToxURIDialog::userIdEdit
QLineEdit * userIdEdit
Definition: toxuri.h:45
ToxURIDialog::friendsLabel
QLabel * friendsLabel
Definition: toxuri.h:44
Core
Definition: core.h:59