diff --git a/src/main.cpp b/src/main.cpp index 756774b86..dada33c57 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -161,6 +161,22 @@ void logMessageHandler(QtMsgType type, const QMessageLogContext& ctxt, const QSt #endif } +static std::unique_ptr 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(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); diff --git a/src/net/toxuri.cpp b/src/net/toxuri.cpp index be8859d1b..9bf0bb28b 100644 --- a/src/net/toxuri.cpp +++ b/src/net/toxuri.cpp @@ -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 @@ -34,39 +33,21 @@ #include #include -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) { - if (result == QDialog::Accepted) { - Core::getInstance()->requestFriendship(toxId, dialog->getRequestMessage()); - } + setUserId(toxURI); - dialog->deleteLater(); - }); - - dialog->open(); + int result = exec(); + if (result == QDialog::Accepted) { + core.requestFriendship(toxId, getRequestMessage()); + } 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); diff --git a/src/net/toxuri.h b/src/net/toxuri.h index 947ecc80f..d51d87234 100644 --- a/src/net/toxuri.h +++ b/src/net/toxuri.h @@ -22,19 +22,26 @@ #include -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; };