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

refactor: remove getInstance from toxuri.cpp

This commit is contained in:
sudden6 2020-06-19 20:46:31 +02:00
parent def038a897
commit 811a754edd
No known key found for this signature in database
GPG Key ID: 279509B499E032B9
3 changed files with 60 additions and 46 deletions

View File

@ -161,6 +161,22 @@ void logMessageHandler(QtMsgType type, const QMessageLogContext& ctxt, const QSt
#endif
}
static std::unique_ptr<ToxURIDialog> uriDialog;
static bool toxURIEventHandler(const QByteArray& eventData)
{
if (!eventData.startsWith("tox:")) {
return false;
}
if (!uriDialog) {
return false;
}
uriDialog->handleToxURI(eventData);
return true;
}
int main(int argc, char* argv[])
{
#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
@ -387,8 +403,11 @@ int main(int argc, char* argv[])
if (returnval == QDialog::Rejected) {
return -1;
}
profile = nexus.getProfile();
}
uriDialog = std::unique_ptr<ToxURIDialog>(new ToxURIDialog(nullptr, profile->getCore()));
if (ipc.isAttached()) {
// Start to accept Inter-process communication
ipc.registerEventHandler("uri", &toxURIEventHandler);
@ -397,10 +416,11 @@ int main(int argc, char* argv[])
}
// Event was not handled by already running instance therefore we handle it ourselves
if (eventType == "uri")
handleToxURI(firstParam.toUtf8());
else if (eventType == "save")
if (eventType == "uri") {
uriDialog->handleToxURI(firstParam.toUtf8());
} else if (eventType == "save") {
handleToxSave(firstParam.toUtf8());
}
QObject::connect(a.get(), &QApplication::aboutToQuit, cleanup);

View File

@ -19,7 +19,6 @@
#include "src/net/toxuri.h"
#include "src/core/core.h"
#include "src/nexus.h"
#include "src/widget/gui.h"
#include "src/widget/tool/friendrequestdialog.h"
#include <QByteArray>
@ -34,39 +33,21 @@
#include <QThread>
#include <QVBoxLayout>
bool toxURIEventHandler(const QByteArray& eventData)
{
if (!eventData.startsWith("tox:"))
return false;
handleToxURI(eventData);
return true;
}
/**
* @brief Shows a dialog asking whether or not to add this tox address as a friend.
* @note Will wait until the core is ready first.
* @param toxURI Tox URI to try to add.
* @return True, if tox URI is correct, false otherwise.
*/
bool handleToxURI(const QString& toxURI)
bool ToxURIDialog::handleToxURI(const QString& toxURI)
{
Nexus& nexus = Nexus::getInstance();
Core* core = nexus.getCore();
while (!core) {
core = nexus.getCore();
qApp->processEvents();
QThread::msleep(10);
}
QString toxaddr = toxURI.mid(4);
ToxId toxId(toxaddr);
QString error = QString();
if (!toxId.isValid()) {
error = QMessageBox::tr("%1 is not a valid Tox address.").arg(toxaddr);
} else if (toxId == core->getSelfId()) {
} else if (toxId == core.getSelfId()) {
error = QMessageBox::tr("You can't add yourself as a friend!",
"When trying to add your own Tox ID as friend");
}
@ -76,35 +57,41 @@ bool handleToxURI(const QString& toxURI)
return false;
}
const QString defaultMessage =
QObject::tr("%1 here! Tox me maybe?",
"Default message in Tox URI friend requests. Write something appropriate!");
const QString username = Nexus::getCore()->getUsername();
ToxURIDialog* dialog = new ToxURIDialog(nullptr, toxaddr, defaultMessage.arg(username));
QObject::connect(dialog, &ToxURIDialog::finished, [=](int result) {
setUserId(toxURI);
int result = exec();
if (result == QDialog::Accepted) {
Core::getInstance()->requestFriendship(toxId, dialog->getRequestMessage());
core.requestFriendship(toxId, getRequestMessage());
}
dialog->deleteLater();
});
dialog->open();
return true;
}
ToxURIDialog::ToxURIDialog(QWidget* parent, const QString& userId, const QString& message)
: QDialog(parent)
void ToxURIDialog::setUserId(const QString& userId)
{
friendsLabel->setText(tr("Do you want to add %1 as a friend?").arg(userId));
userIdEdit->setText(userId);
}
ToxURIDialog::ToxURIDialog(QWidget* parent, Core& _core)
: QDialog(parent)
, core{_core}
{
const QString defaultMessage =
QObject::tr("%1 here! Tox me maybe?",
"Default message in Tox URI friend requests. Write something appropriate!");
const QString username = core.getUsername();
const QString message = defaultMessage.arg(username);
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
setWindowTitle(tr("Add a friend", "Title of the window to add a friend through Tox URI"));
QLabel* friendsLabel = new QLabel(tr("Do you want to add %1 as a friend?").arg(userId), this);
QLabel* userIdLabel = new QLabel(tr("User ID:"), this);
QLineEdit* userIdEdit = new QLineEdit(userId, this);
friendsLabel = new QLabel("", this);
userIdEdit = new QLineEdit("", this);
userIdEdit->setCursorPosition(0);
userIdEdit->setReadOnly(true);
QLabel* userIdLabel = new QLabel(tr("User ID:"), this);
QLabel* messageLabel = new QLabel(tr("Friend request message:"), this);
messageEdit = new QPlainTextEdit(message, this);

View File

@ -22,19 +22,26 @@
#include <QDialog>
bool handleToxURI(const QString& toxURI);
class Core;
// Internals
class QByteArray;
class QLabel;
class QLineEdit;
class QPlainTextEdit;
bool toxURIEventHandler(const QByteArray& eventData);
class ToxURIDialog : public QDialog
{
Q_OBJECT
public:
explicit ToxURIDialog(QWidget* parent, const QString& userId, const QString& message);
explicit ToxURIDialog(QWidget* parent, Core& _core);
QString getRequestMessage();
bool handleToxURI(const QString& toxURI);
private:
void setUserId(const QString& userId);
private:
QPlainTextEdit* messageEdit;
QLabel* friendsLabel;
QLineEdit* userIdEdit;
Core& core;
};