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 #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[]) int main(int argc, char* argv[])
{ {
#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)) #if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
@ -387,8 +403,11 @@ int main(int argc, char* argv[])
if (returnval == QDialog::Rejected) { if (returnval == QDialog::Rejected) {
return -1; return -1;
} }
profile = nexus.getProfile();
} }
uriDialog = std::unique_ptr<ToxURIDialog>(new ToxURIDialog(nullptr, profile->getCore()));
if (ipc.isAttached()) { if (ipc.isAttached()) {
// Start to accept Inter-process communication // Start to accept Inter-process communication
ipc.registerEventHandler("uri", &toxURIEventHandler); 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 // Event was not handled by already running instance therefore we handle it ourselves
if (eventType == "uri") if (eventType == "uri") {
handleToxURI(firstParam.toUtf8()); uriDialog->handleToxURI(firstParam.toUtf8());
else if (eventType == "save") } else if (eventType == "save") {
handleToxSave(firstParam.toUtf8()); handleToxSave(firstParam.toUtf8());
}
QObject::connect(a.get(), &QApplication::aboutToQuit, cleanup); QObject::connect(a.get(), &QApplication::aboutToQuit, cleanup);

View File

@ -19,7 +19,6 @@
#include "src/net/toxuri.h" #include "src/net/toxuri.h"
#include "src/core/core.h" #include "src/core/core.h"
#include "src/nexus.h"
#include "src/widget/gui.h" #include "src/widget/gui.h"
#include "src/widget/tool/friendrequestdialog.h" #include "src/widget/tool/friendrequestdialog.h"
#include <QByteArray> #include <QByteArray>
@ -34,39 +33,21 @@
#include <QThread> #include <QThread>
#include <QVBoxLayout> #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. * @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. * @note Will wait until the core is ready first.
* @param toxURI Tox URI to try to add. * @param toxURI Tox URI to try to add.
* @return True, if tox URI is correct, false otherwise. * @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); QString toxaddr = toxURI.mid(4);
ToxId toxId(toxaddr); ToxId toxId(toxaddr);
QString error = QString(); QString error = QString();
if (!toxId.isValid()) { if (!toxId.isValid()) {
error = QMessageBox::tr("%1 is not a valid Tox address.").arg(toxaddr); 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!", error = QMessageBox::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");
} }
@ -76,35 +57,41 @@ bool handleToxURI(const QString& toxURI)
return false; return false;
} }
const QString defaultMessage = setUserId(toxURI);
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) {
if (result == QDialog::Accepted) {
Core::getInstance()->requestFriendship(toxId, dialog->getRequestMessage());
}
dialog->deleteLater(); int result = exec();
}); if (result == QDialog::Accepted) {
core.requestFriendship(toxId, getRequestMessage());
dialog->open(); }
return true; return true;
} }
ToxURIDialog::ToxURIDialog(QWidget* parent, const QString& userId, const QString& message) void ToxURIDialog::setUserId(const QString& userId)
: QDialog(parent)
{ {
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); setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
setWindowTitle(tr("Add a friend", "Title of the window to add a friend through Tox URI")); 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); friendsLabel = new QLabel("", this);
QLabel* userIdLabel = new QLabel(tr("User ID:"), this); userIdEdit = new QLineEdit("", this);
QLineEdit* userIdEdit = new QLineEdit(userId, this);
userIdEdit->setCursorPosition(0); userIdEdit->setCursorPosition(0);
userIdEdit->setReadOnly(true); userIdEdit->setReadOnly(true);
QLabel* userIdLabel = new QLabel(tr("User ID:"), this);
QLabel* messageLabel = new QLabel(tr("Friend request message:"), this); QLabel* messageLabel = new QLabel(tr("Friend request message:"), this);
messageEdit = new QPlainTextEdit(message, this); messageEdit = new QPlainTextEdit(message, this);

View File

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