Add signed device groups and compact NODEINFO packing
This commit is contained in:
1 parent
ef34f81c90
commit
d7006aecb5
52 files changed
+1979
-94
No files matched your search
@@ -0,0 +1,116 @@
|
||||
#include <QCoreApplication>
|
||||
#include <algorithm>
|
||||
#include "devgroupdialog.h"
|
||||
#include "../transport/gui_bridge.h"
|
||||
#include "debug_ui.h"
|
||||
#include <QDialog>
|
||||
#include <QVBoxLayout>
|
||||
#include <QLabel>
|
||||
#include <QPushButton>
|
||||
#include <QLineEdit>
|
||||
#include <QListWidget>
|
||||
#include <QInputDialog>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QJsonArray>
|
||||
#include <QTimer>
|
||||
#include <QPointer>
|
||||
#include <QHash>
|
||||
extern "C" {
|
||||
#include "../../../src/chat/chat_devgroup.h"
|
||||
#include "../../../lib/mem.h"
|
||||
}
|
||||
class DevgroupDialog;
|
||||
static QPointer<DevgroupDialog> active;
|
||||
static QList<QPointer<QPushButton>> buttons;
|
||||
static quint64 nextRequest = 0;
|
||||
static void devgroupResult(const uint8_t* data, int len);
|
||||
class DevgroupDialog : public QDialog {
|
||||
public:
|
||||
static QString tr(const char* text) { return QCoreApplication::translate("DevgroupDialog", text); }
|
||||
void refresh() { send(CHAT_DEVGROUP_STATUS); }
|
||||
DevgroupDialog(QWidget* parent, const QString& channel, quint64 peer) : QDialog(parent), channel(channel), peer(peer) {
|
||||
setAttribute(Qt::WA_DeleteOnClose); setWindowTitle(tr("My devices")); resize(480, 360);
|
||||
auto* layout = new QVBoxLayout(this);
|
||||
title = new QLabel(this); layout->addWidget(title);
|
||||
name = new QLineEdit(this); name->setPlaceholderText(tr("Device group name")); layout->addWidget(name);
|
||||
auto* rename = new QPushButton(tr("Create / rename"), this); layout->addWidget(rename);
|
||||
connect(rename, &QPushButton::clicked, this, [this] { send(CHAT_DEVGROUP_RENAME, name->text()); });
|
||||
if (peer) {
|
||||
auto* invite = new QPushButton(tr("Add device %1").arg(QString::number(peer, 16)), this); layout->addWidget(invite);
|
||||
connect(invite, &QPushButton::clicked, this, [this] { send(CHAT_DEVGROUP_INVITE); });
|
||||
}
|
||||
status = new QLabel(this); status->setWordWrap(true); status->setTextInteractionFlags(Qt::TextSelectableByMouse); layout->addWidget(status);
|
||||
list = new QListWidget(this); layout->addWidget(list);
|
||||
auto* accept = new QPushButton(tr("Enter PIN for selected invitation"), this); layout->addWidget(accept);
|
||||
connect(accept, &QPushButton::clicked, this, [this] {
|
||||
const auto* item = list->currentItem(); if (!item || item->data(Qt::UserRole).toString().isEmpty()) return;
|
||||
const QString id = item->data(Qt::UserRole).toString(); bool ok = false;
|
||||
const QString pin = QInputDialog::getText(this, tr("Add device"), tr("PIN shown on the inviting device"), QLineEdit::Normal, {}, &ok);
|
||||
if (ok) send(CHAT_DEVGROUP_ACCEPT, id, pin);
|
||||
});
|
||||
auto* leave = new QPushButton(tr("Leave device group"), this); layout->addWidget(leave);
|
||||
connect(leave, &QPushButton::clicked, this, [this] { send(CHAT_DEVGROUP_LEAVE); });
|
||||
auto* timer = new QTimer(this); timer->setInterval(1000);
|
||||
connect(timer, &QTimer::timeout, this, [this] { if (!gui_bridge_is_uasync_ready()) pending.clear();
|
||||
else if (!pending.values().contains(CHAT_DEVGROUP_STATUS)) send(CHAT_DEVGROUP_STATUS); });
|
||||
timer->start(); send(CHAT_DEVGROUP_STATUS);
|
||||
}
|
||||
void result(const uint8_t* data, int len) {
|
||||
if (len < 9) return;
|
||||
quint64 id = 0; for (int i = 0; i < 8; ++i) id |= quint64(data[i]) << (8*i);
|
||||
if (!pending.contains(id)) return;
|
||||
const int action = pending.take(id);
|
||||
if (!data[8]) { status->setText(tr("Operation failed. Check connection and invitation status.")); return; }
|
||||
const auto json = QJsonDocument::fromJson(QByteArray(reinterpret_cast<const char*>(data+9), len-9)).object();
|
||||
if (action == CHAT_DEVGROUP_INVITE) { status->setText(tr("PIN: %1 — enter it on the other device within 2 minutes.").arg(json["pin"].toString())); return; }
|
||||
title->setText(json["group"].toString().isEmpty() ? tr("No device group") : json["name"].toString());
|
||||
const QString selected = list->currentItem() ? list->currentItem()->data(Qt::UserRole).toString() : QString();
|
||||
list->clear();
|
||||
const QStringList states = {{}, tr("Waiting for PIN"), tr("Checking PIN"), tr("Transferring keys"), tr("Joined"),
|
||||
tr("Rejected"), tr("Expired"), tr("Result unknown")};
|
||||
for (const auto value : json["invitations"].toArray()) {
|
||||
const auto invitation = value.toObject(); const int state = invitation["state"].toInt();
|
||||
auto* item = new QListWidgetItem(invitation["peer"].toString() + " — " + states.value(state), list);
|
||||
if (!invitation["outgoing"].toInt() && state == 1) {
|
||||
item->setData(Qt::UserRole, invitation["id"].toString());
|
||||
if (item->data(Qt::UserRole).toString() == selected) list->setCurrentItem(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
private:
|
||||
void send(chat_devgroup_action action, const QString& text = {}, const QString& pin = {}) {
|
||||
const auto bytes = text.toUtf8(), code = pin.toUtf8();
|
||||
if (bytes.size() >= 128 || bytes.contains('\0') || code.size() >= 5 || code.contains('\0')) {
|
||||
status->setText(tr("Invalid name or PIN")); return;
|
||||
}
|
||||
auto* req = static_cast<chat_devgroup_request*>(u_calloc(1, sizeof(chat_devgroup_request)));
|
||||
if (!req) { DEBUG_ERROR(DEBUG_CATEGORY_CHAT, "devgroup UI: allocation failed"); return; }
|
||||
req->inst = gui_bridge_get_inst(); req->request_id = ++nextRequest; req->group = channel.toULongLong(); req->peer = peer;
|
||||
req->action = action; memcpy(req->text, bytes.constData(), bytes.size()); memcpy(req->pin, code.constData(), code.size());
|
||||
const quint64 id = req->request_id;
|
||||
if (gui_bridge_post_uasync_owned_fn(chat_devgroup_trampoline, req) < 0) {
|
||||
memset(req, 0, sizeof(*req)); u_free(req); status->setText(tr("Core unavailable"));
|
||||
DEBUG_WARN(DEBUG_CATEGORY_CHAT, "devgroup UI: core unavailable"); return;
|
||||
}
|
||||
pending.insert(id, action);
|
||||
}
|
||||
QString channel; quint64 peer;
|
||||
QLabel *title, *status; QLineEdit* name; QListWidget* list;
|
||||
QHash<quint64, int> pending;
|
||||
};
|
||||
static void devgroupResult(const uint8_t* data, int len) {
|
||||
if (!len) {
|
||||
for (const auto& button : buttons) if (button) button->setText(QCoreApplication::translate("AccountList", "My devices") + " •");
|
||||
if (active) active->refresh();
|
||||
} else if (active) active->result(data, len);
|
||||
}
|
||||
void watchDevgroupButton(QPushButton* button) {
|
||||
buttons.erase(std::remove_if(buttons.begin(), buttons.end(), [](const QPointer<QPushButton>& b) { return b.isNull(); }), buttons.end());
|
||||
buttons.append(button); gui_bridge_set_devgroup_result_cb(devgroupResult);
|
||||
}
|
||||
void showDevgroupDialog(QWidget* parent, const QString& channel, quint64 peer) {
|
||||
if (active) { active->close(); active = nullptr; }
|
||||
gui_bridge_set_devgroup_result_cb(devgroupResult);
|
||||
active = new DevgroupDialog(parent, channel, peer); active->show();
|
||||
}
|
||||
Reference in new issue
Block a user