Files
utun2/tools/chatgui/src/mainwindow.cpp
T
evgeny f016d494b2 Add BLE ETCP links and GATT invitations for desktop and Android
Attach packet drivers to initially empty ETCP connections. Share BLE neighbor
tracking and group fallback between desktop and Android in cgathui_common.

Deliver join links through a dedicated Write/Indicate characteristic with
independent fragmentation queues, application acknowledgements, deduplication
and bounded retries. Merge LAN/BLE discovery and add a separate Bluetooth
invitation reception setting to both clients.

Validation: 230 core tests, BLE transport/BlueZ/UI tests, ASAN/UBSAN,
Linux GUI and Android builds, and Windows backend cross-compilation.
Physical Android/Linux invitation exchange passed with either GATT initiator.
The Android-initiated run also verified all 32 ETCP packets of 1500 bytes;
the desktop-initiated load run reached 7/32 within its 45-second time limit.
2026-10-29 16:41:04 +01:00

1591 lines
78 KiB
C++

#include <QCoreApplication>
#include "mainwindow.h"
#include "single_instance.h"
#include "streamplayer_window.h"
#include "streamgrid_window.h"
#include "stream_publish_engine.h"
#include "stream_publish_window.h"
#include "channellist.h"
#include "messagelist.h"
#include "messagedelegate.h"
#include "accountlist.h"
#include "memberlistmodel.h"
#include "invitedialog.h"
#include "dminvitedialog.h"
#include "debug_ui.h"
#include "inviteby.h"
#include <QInputDialog>
#include "joindialog.h"
#include "settingsdialog.h"
#include "channelsettingsdialog.h"
#include "connmonitorwindow.h"
#include "videoplayer_window.h"
#include "imageviewer_window.h"
#include "member_files/browser.h"
#include "avatars/avatarmanager.h"
#include "avatars/groupavatarmanager.h"
extern "C" {
#include "chat/chat_member.h"
}
#include "creategroupdialog.h"
#include "invite_link.h"
#include "sound_manager.h"
#include "voiceplayback.h"
#include "audiorecorder.h"
#include "voicemessageencoder.h"
#include "callwindow.h"
#include "call_audio_engine.h"
#include "radio_audio_engine.h"
#include "ptt_hotkey_manager.h"
#include "message_notification.h"
extern "C" {
#include "../../lib/audio_compressor.h"
}
#include "animtimer.h"
#include "../db/db_manager.h"
#include "../transport/utun_node.h"
#include "../transport/gui_bridge.h"
#include <QSplitter>
#include <QCloseEvent>
#include <QElapsedTimer>
#include <QApplication>
#include <QScreen>
#include <QClipboard>
#include <QStyle>
#include <QMessageBox>
#include <QPointer>
#include <QScopeGuard>
#include <QPushButton>
#include <QDesktopServices>
#include <QUrl>
#include <QDebug>
#include <QFile>
#include <QTextStream>
#include <QDir>
#ifdef _WIN32
#include <QFileInfo>
#endif
#include <cmath>
#include <QJsonDocument>
#include <QJsonObject>
#include <QRandomGenerator>
#include <csignal>
extern "C" {
#include "chat/chat_core.h"
#include "chat/chat_attachment.h"
#include "call/call.h"
#include "call/call_proto.h"
#include "radio/radio_call.h"
#include "dm/dm_core.h"
#include "../../lib/mem.h"
#include "../../lib/debug_config.h"
}
static MainWindow* s_mainWindow = nullptr;
#ifndef _WIN32
static volatile std::sig_atomic_t g_signalFlag = 0;
static void unixSignalHandler(int) { g_signalFlag = 1; }
#endif
/* Bridge копирует key/value и владеет запросом до записи ui_state в utun-потоке либо отмены. */
static void postUiStateKv(const char* key, const QString& value) {
if (gui_bridge_post_setting(GUI_SETTING_UI_STATE, key, value.toUtf8().constData()) < 0)
DEBUG_WARN(DEBUG_CATEGORY_CONFIG, "UI setting was not queued key=%s", key);
}
static void saveLastChannelId(const QString& channelId) {
postUiStateKv("last_channel_id", channelId);
}
static void onMsgReceivedCallback(const char* ch_id, int ch_id_len, uint64_t author, int initial_sync) {
if (s_mainWindow)
s_mainWindow->onMessageReceived(ch_id, ch_id_len, author, initial_sync != 0);
}
static void onChannelUpdatedCallback(const char* ch_id, int ch_id_len) {
Q_UNUSED(ch_id_len);
Q_UNUSED(ch_id);
if (s_mainWindow)
s_mainWindow->reloadChannels();
if (AvatarManager::instance()) AvatarManager::instance()->refreshMembers();
}
static void onMemberListCallback(const uint8_t* data, int len) {
if (len < 3) return;
uint8_t cl = data[0];
if (len < 1 + (int)cl + 2) return;
uint16_t count; memcpy(&count, data + 1 + cl, 2);
if (s_mainWindow)
s_mainWindow->onMemberList(QString::fromUtf8((const char*)data + 1, cl), count, data + 1 + cl + 2, len - 1 - cl - 2);
}
static void onMemberUpdatedCallback(const uint8_t* data, int len) {
if (len < 1) return;
uint8_t cl = data[0];
if (len < 1 + (int)cl + 1 + CHAT_MEMBER_DISPLAY_SIZE) return;
if (s_mainWindow)
s_mainWindow->onMemberUpdated(QString::fromUtf8((const char*)data + 1, cl), data + 1 + cl + 1);
}
static void onMemberRemovedCallback(const uint8_t* data, int len) {
if (len < 1) return;
uint8_t cl = data[0];
if (len < 1 + (int)cl + 8) return;
uint64_t nid; memcpy(&nid, data + 1 + cl, 8);
if (s_mainWindow)
s_mainWindow->onMemberRemoved(QString::fromUtf8((const char*)data + 1, cl), nid);
}
static void onAutoConnectStatusCallback(uint8_t status, uint16_t total_tried,
uint16_t node_count, uint16_t connected) {
if (s_mainWindow)
s_mainWindow->onAutoConnectStatus(status, total_tried, node_count, connected);
}
static void onChannelPeersOnlineCallback(const char* ch_id, int ch_id_len, uint16_t online) {
if (s_mainWindow)
s_mainWindow->onChannelPeersOnline(ch_id, ch_id_len, online);
}
MainWindow::MainWindow(QWidget *parent, DbManager* db, const QString& cfgPath,
const QString& dbPath, const QString& debugFile,
const QString& debugLevel, const QString& debugCategories)
: QMainWindow(parent)
, m_channelList(nullptr), m_messageList(nullptr), m_accountList(nullptr)
, m_trayIcon(nullptr), m_trayMenu(nullptr)
, m_db(db), m_cfgPath(cfgPath), m_cfgDbPath(dbPath)
, m_cfgDebugFile(debugFile), m_cfgDebugLevel(debugLevel)
, m_cfgDebugCategories(debugCategories)
, m_recorder(nullptr)
, m_autoPlayTimer(new QTimer(this))
{
setWindowTitle("vibechat");
resize(900, 600);
if (m_db) m_db->setDbPath(m_cfgDbPath);
new AvatarManager(m_db, this);
new GroupAvatarManager(m_db, this);
setupUi();
setupTray();
setupSoundFromConfig();
if (m_db) {
setupBridgeCallbacks();
setupMessaging();
setupNode();
if (m_node->isRunning()) setupConnects();
}
connect(m_autoPlayTimer, &QTimer::timeout, this, &MainWindow::autoPlayTimerTick);
m_cpuLoadTimer = new QTimer(this);
m_cpuLoadTimer->setTimerType(Qt::PreciseTimer);
connect(m_cpuLoadTimer, &QTimer::timeout, this, [this] {
if (!m_node || !m_node->isRunning() || !gui_bridge_is_uasync_ready()) {
m_cpuLoadPending = false; m_channelList->setCpuLoad(0); return;
}
if (m_cpuLoadPending) return;
m_cpuLoadPending = gui_bridge_request_cpu_load() != 0;
});
m_cpuLoadTimer->start(333);
DEBUG_DEBUG(DEBUG_CATEGORY_SYS, "desktop CPU indicator polling started: period=333ms");
#ifndef _WIN32
std::signal(SIGINT, unixSignalHandler);
std::signal(SIGTERM, unixSignalHandler);
m_shutdownTimer = new QTimer(this);
connect(m_shutdownTimer, &QTimer::timeout, this, &MainWindow::onShutdownCheck);
m_shutdownTimer->start(200);
#endif
}
MainWindow::~MainWindow() {
gui_bridge_set_radio_call_cb(nullptr);
if (m_radioCallWindow) { m_radioCallId = 0; delete m_radioCallWindow.data(); }
if (m_streamPublishWindow) delete m_streamPublishWindow.data();
delete m_streamPublisher; m_streamPublisher = nullptr;
if (m_streamPlayer) delete m_streamPlayer.data();
if (m_streamGrid) delete m_streamGrid.data();
gui_bridge_set_stream_sources_cb(nullptr);
s_mainWindow = nullptr;
m_cpuLoadTimer->stop();
gui_bridge_set_cpu_load_cb(nullptr);
gui_bridge_set_local_invite_cb(nullptr);
DEBUG_DEBUG(DEBUG_CATEGORY_SYS, "desktop CPU indicator polling stopped");
if (m_messageNotification) m_messageNotification->dismiss("shutdown");
m_messageNotification.reset();
delete AvatarManager::instance();
delete GroupAvatarManager::instance();
gui_bridge_set_group_avatar_result_cb(nullptr);
gui_bridge_set_feedback_changed_cb(nullptr);
gui_bridge_set_attachment_keys_cb(nullptr);
gui_bridge_set_feedback_result_cb(nullptr);
gui_bridge_set_msg_submitted_cb(nullptr);
gui_bridge_set_avatar_result_cb(nullptr);
if (m_fileBrowser) delete m_fileBrowser.data();
gui_bridge_set_member_files_response_cb(nullptr);
gui_bridge_set_member_files_transfer_cb(nullptr);
m_autoPlayTimer->stop();
gui_bridge_set_network_changed_cb(nullptr);
gui_bridge_set_dm_call_route_cb(nullptr);
gui_bridge_set_dm_conv_updated_cb(nullptr);
gui_bridge_set_dm_msg_received_cb(nullptr);
gui_bridge_set_dm_messages_cb(nullptr);
gui_bridge_set_dm_delete_result_cb(nullptr);
gui_bridge_set_msg_received_cb(nullptr);
gui_bridge_set_channel_updated_cb(nullptr);
gui_bridge_set_member_list_cb(nullptr);
gui_bridge_set_member_updated_cb(nullptr);
gui_bridge_set_member_removed_cb(nullptr);
gui_bridge_set_radio_talk_cb(nullptr);
gui_bridge_set_my_node_id_cb(nullptr);
gui_bridge_set_auto_connect_status_cb(nullptr);
gui_bridge_set_channel_peers_online_cb(nullptr);
gui_bridge_set_db_ready_cb(nullptr);
gui_bridge_set_attachment_downloaded_cb(nullptr);
CallAudioEngine::instance()->stop();
RadioAudioEngine::instance()->stop();
if (m_recorder) { m_recorder->shutdown(); m_recorder = nullptr; }
SoundManager::instance()->shutdown();
DEBUG_DEBUG(DEBUG_CATEGORY_GENERAL, "MainWindow: ~MainWindow — saving channel state");
if (m_messageList && m_node && m_node->isRunning()) m_messageList->saveChannelState();
DEBUG_DEBUG(DEBUG_CATEGORY_GENERAL, "MainWindow: ~MainWindow — stopping utun node");
if (m_node) { m_node->stop(); delete m_node; m_node = nullptr; }
DEBUG_DEBUG(DEBUG_CATEGORY_GENERAL, "MainWindow: ~MainWindow — node stop+delete done");
AnimTimer::shutdown();
MessageList::cleanupAnimations();
DEBUG_DEBUG(DEBUG_CATEGORY_GENERAL, "MainWindow: ~MainWindow — animations cleaned");
DEBUG_DEBUG(DEBUG_CATEGORY_GENERAL, "MainWindow: ~MainWindow done");
}
void MainWindow::setupUi() {
auto *splitter = new QSplitter(Qt::Horizontal, this);
m_splitter = splitter;
m_channelList = new ChannelList(m_db, splitter);
m_recorder = new AudioRecorder(this);
m_messageList = new MessageList(m_db, m_recorder, splitter);
m_accountList = new AccountList(m_db, splitter);
splitter->addWidget(m_channelList);
splitter->addWidget(m_messageList);
splitter->addWidget(m_accountList);
splitter->setStretchFactor(0, 2);
splitter->setStretchFactor(1, 5);
splitter->setStretchFactor(2, 1);
splitter->setSizes({220, 480, 180});
setCentralWidget(splitter);
}
void MainWindow::saveWindowGeometry() {
QRect g = normalGeometry();
if (g.isEmpty()) g = geometry();
postUiStateKv("window_geometry",
QStringLiteral("%1,%2,%3,%4").arg(g.width()).arg(g.height()).arg(g.x()).arg(g.y()));
postUiStateKv("window_maximized", isMaximized() ? QStringLiteral("1") : QStringLiteral("0"));
if (m_splitter) {
const QList<int> sizes = m_splitter->sizes();
if (sizes.size() == 3)
postUiStateKv("window_splitter",
QStringLiteral("%1,%2,%3").arg(sizes[0]).arg(sizes[1]).arg(sizes[2]));
}
}
void MainWindow::restoreWindowGeometry() {
if (!m_db) return;
const QStringList p = m_db->getUiState("window_geometry").split(',');
if (p.size() != 4) return;
int w = p[0].toInt(), h = p[1].toInt(), x = p[2].toInt(), y = p[3].toInt();
if (w <= 0 || h <= 0) return;
QRect target(x, y, w, h);
bool visible = false;
for (const QScreen* s : QGuiApplication::screens()) {
if (s->availableGeometry().intersects(target)) { visible = true; break; }
}
if (!visible) target.moveTo(0, 0);
setGeometry(target);
// Поздний ответ БД сохраняет свёрнутость при --minimized и не показывает скрытое в tray окно.
if (m_db->getUiStateInt("window_maximized", 0))
setWindowState(windowState() | Qt::WindowMaximized);
GUI_DEBUG("window geometry restored: maximized=%d minimized=%d visible=%d", isMaximized(), isMinimized(), isVisible());
if (m_splitter) {
const QStringList sp = m_db->getUiState("window_splitter").split(',');
if (sp.size() == 3) {
int s0 = sp[0].toInt(), s1 = sp[1].toInt(), s2 = sp[2].toInt();
if (s0 > 0 && s1 > 0 && s2 > 0)
m_splitter->setSizes({s0, s1, s2});
}
}
}
void MainWindow::setupNode() {
m_node = new UtunNode(this);
connect(m_node, &UtunNode::error, this, [](const QString& msg) {
qWarning("UtunNode error: %s", qPrintable(msg));
});
connect(m_node, &UtunNode::started, this, [this]() {
qDebug("UtunNode started");
});
connect(m_node, &UtunNode::stopping, this, [this] {
m_autoPlayTimer->stop(); m_autoPlayQueue.clear(); m_autoPlayDownloading.clear(); m_lastAutoPlayId.clear();
MessageDelegate::stopAllAutoPlay();
if (m_db) m_db->setDb(nullptr);
m_messageList->loadChannel(QString()); m_accountList->setChannel(QString()); m_currentChannelId.clear();
DEBUG_INFO(DEBUG_CATEGORY_SYS, "desktop GUI database detached before core teardown");
});
connect(m_node, &UtunNode::stopped, this, [this]() {
qDebug("UtunNode stopped");
qApp->quit();
});
if (!m_cfgDebugFile.isEmpty())
m_node->setDebugFile(m_cfgDebugFile);
if (!m_cfgDebugLevel.isEmpty())
m_node->setDebugLevel(m_cfgDebugLevel);
if (!m_cfgDebugCategories.isEmpty())
m_node->setDebugCategories(m_cfgDebugCategories);
if (!m_cfgDbPath.isEmpty())
m_node->setDbPath(m_cfgDbPath);
if (!m_node->start(m_cfgPath))
DEBUG_ERROR(DEBUG_CATEGORY_SYS, "desktop startup failed: %s", qPrintable(m_node->startupError()));
else
AnimTimer::instance()->logRegisteredIcons();
}
void MainWindow::setupMessaging() {
gui_bridge_init();
}
void MainWindow::setupSoundFromConfig() {
SoundManager* sm = SoundManager::instance();
if (!sm->init()) return;
QString soundDir, msgInFile, msgOutFile, connectFile, disconnectFile, callInFile;
QFile cfgFile(m_cfgPath);
if (cfgFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
bool inGui = false;
QTextStream in(&cfgFile);
while (!in.atEnd()) {
QString line = in.readLine().trimmed();
if (line == "[gui]") { inGui = true; continue; }
if (line.startsWith("[") && line.endsWith("]")) inGui = false;
if (!inGui) continue;
if (line.startsWith("sound_dir=")) soundDir = line.mid(10).trimmed();
else if (line.startsWith("sound_msg_incoming=")) msgInFile = line.mid(19).trimmed();
else if (line.startsWith("sound_msg_outgoing=")) msgOutFile = line.mid(19).trimmed();
else if (line.startsWith("sound_connect=")) connectFile = line.mid(14).trimmed();
else if (line.startsWith("sound_disconnect=")) disconnectFile = line.mid(17).trimmed();
else if (line.startsWith("sound_call_incoming=")) callInFile = line.mid(20).trimmed();
}
cfgFile.close();
}
if (soundDir.isEmpty()) soundDir = "sounds";
#ifdef _WIN32
QDir binDir(QFileInfo(m_cfgPath).absolutePath());
#else
QDir binDir(QApplication::applicationDirPath());
#endif
QString baseDir = QDir::isAbsolutePath(soundDir) ? soundDir : binDir.absoluteFilePath(soundDir);
auto resolvePath = [&baseDir](const QString& f) { return f.isEmpty() ? QString() : QDir::cleanPath(baseDir + "/" + f); };
if (msgInFile.isEmpty())
sm->loadSound("msg_incoming", ":/sounds/msg_incoming.mp3");
else
sm->loadSound("msg_incoming", resolvePath(msgInFile));
if (callInFile.isEmpty())
sm->loadSound("call_incoming", ":/sounds/call_incoming.mp3");
else
sm->loadSound("call_incoming", resolvePath(callInFile));
sm->loadSound("call_connecting", ":/sounds/call_connecting.mp3");
sm->loadSound("radio_incoming", ":/sounds/radio_incoming.mp3");
sm->loadSound("call_ringing", ":/sounds/call_ringing.mp3");
if (!msgOutFile.isEmpty()) sm->loadSound("msg_outgoing", resolvePath(msgOutFile));
if (!connectFile.isEmpty()) sm->loadSound("connect", resolvePath(connectFile));
if (!disconnectFile.isEmpty()) sm->loadSound("disconnect", resolvePath(disconnectFile));
if (m_db && m_db->isOpen()) {
sm->setDevices(m_db->getUiState("audio_output_id"), m_db->getUiState("audio_input_id"));
sm->setEnabled(m_db->getUiStateInt("sound_enabled", 1));
sm->setVolume(m_db->getUiStateInt("sound_volume", 80) / 100.0f);
QString se[] = {"msg_incoming", "msg_outgoing", "connect", "disconnect",
"call_incoming", "call_connecting", "call_ringing", "radio_incoming"};
for (int i = 0; i < 8; i++) {
const QString& ek = se[i];
sm->setEvent(ek, m_db->getUiStateInt(QStringLiteral("sound_%1_on").arg(ek), (i == 0 || i >= 4) ? 1 : 0),
m_db->getUiStateInt(QStringLiteral("sound_%1_vol").arg(ek), 80) / 100.0f);
}
}
if (m_recorder) {
m_recorder->init();
if (m_db && m_db->isOpen()) {
VoiceEncoder::setPreset(m_db->getUiStateInt("opus_codec_preset", 1));
int compEnabled = m_db->getUiStateInt("compressor_enabled", 0);
m_recorder->setCompressorEnabled(compEnabled != 0);
{
audio_compressor_config_t compCfg = {0};
compCfg.sample_rate = 48000;
compCfg.channels = 1;
compCfg.max_gain_db = (float)m_db->getUiStateInt("compressor_max_gain_db", 25);
compCfg.lookback_ms = 200;
compCfg.lookahead_ms = 100;
compCfg.rise_rate_per_sec = (float)m_db->getUiStateInt("compressor_rise_rate", 10);
compCfg.target_level = 1.0f;
m_recorder->configureCompressor(compCfg);
}
}
DEBUG_DEBUG(DEBUG_CATEGORY_MEDIA, "AudioRecorder initialized (opusPreset=%d, compressor=%d)",
VoiceEncoder::preset(),
m_recorder->isCompressorEnabled() ? 1 : 0);
}
}
void MainWindow::setupBridgeCallbacks() {
s_mainWindow = this;
gui_bridge_set_local_invite_cb([](const uint8_t* data, int len) {
if (!s_mainWindow || len < 10 || 9 + data[8] >= len) {
DEBUG_WARN(DEBUG_CATEGORY_DISCOVERY, "invalid local invitation event bytes=%d", len); return;
}
QString name = QString::fromUtf8((const char*)data + 9, data[8]);
QString link = QString::fromUtf8((const char*)data + 9 + data[8], len - 9 - data[8]);
auto* window = s_mainWindow;
if (window->findChildren<QMessageBox*>(QStringLiteral("localInvitePrompt")).size() >= 8) {
DEBUG_WARN(DEBUG_CATEGORY_DISCOVERY, "incoming local invitation prompt limit reached"); return;
}
auto* prompt = new QMessageBox(QMessageBox::Question, QCoreApplication::translate("ChatGui", "Invitation"),
QCoreApplication::translate("ChatGui", "%1 sent an invitation. Connect?").arg(name),
QMessageBox::Yes | QMessageBox::No, window);
prompt->setTextFormat(Qt::PlainText); prompt->setAttribute(Qt::WA_DeleteOnClose);
prompt->setObjectName(QStringLiteral("localInvitePrompt"));
QObject::connect(prompt, &QMessageBox::finished, window, [window, link](int result) {
if (result == QMessageBox::Yes) window->onMessageLinkActivated(link);
});
prompt->open();
});
gui_bridge_set_cpu_load_cb([](int percent) {
if (!s_mainWindow) return;
s_mainWindow->m_cpuLoadPending = false;
s_mainWindow->m_channelList->setCpuLoad(percent);
});
gui_bridge_set_member_files_response_cb([](const uint8_t* data, int length) {
if (s_mainWindow && s_mainWindow->m_fileBrowser) s_mainWindow->m_fileBrowser->onResponse(data, length);
if (AvatarManager::instance()) AvatarManager::instance()->onResponse(data, length);
});
gui_bridge_set_member_files_transfer_cb([](const uint8_t* data, int length) {
if (s_mainWindow && s_mainWindow->m_fileBrowser) s_mainWindow->m_fileBrowser->onTransfer(data, length);
if (AvatarManager::instance()) AvatarManager::instance()->onTransfer(data, length);
});
gui_bridge_set_avatar_result_cb([](const uint8_t* data, int length) {
if (AvatarManager::instance()) AvatarManager::instance()->onPublished(data, length);
});
gui_bridge_set_group_avatar_result_cb([](const uint8_t* data, int length) {
if (GroupAvatarManager::instance()) GroupAvatarManager::instance()->onResult(data, length);
});
gui_bridge_set_msg_received_cb(onMsgReceivedCallback);
gui_bridge_set_msg_updated_cb([](const uint8_t* data, int length) {
if (s_mainWindow && s_mainWindow->m_messageList) s_mainWindow->m_messageList->onMessageUpdated(data, length);
});
gui_bridge_set_msg_submitted_cb([](const uint8_t* data, int length) {
if (s_mainWindow && s_mainWindow->m_messageList) s_mainWindow->m_messageList->onMessageSubmitted(data, length);
});
gui_bridge_set_feedback_changed_cb([](const uint8_t* data, int length) {
if (s_mainWindow && s_mainWindow->m_messageList) s_mainWindow->m_messageList->onFeedbackChanged(data, length);
});
gui_bridge_set_attachment_keys_cb([](const uint8_t* data, int length) {
if (s_mainWindow && s_mainWindow->m_messageList) s_mainWindow->m_messageList->onAttachmentKeysChanged(data, length);
});
gui_bridge_set_feedback_result_cb([](const uint8_t* data, int length) {
if (s_mainWindow && s_mainWindow->m_messageList) s_mainWindow->m_messageList->onFeedbackResult(data, length);
});
gui_bridge_set_channel_updated_cb(onChannelUpdatedCallback);
gui_bridge_set_member_list_cb(onMemberListCallback);
gui_bridge_set_member_updated_cb(onMemberUpdatedCallback);
gui_bridge_set_member_removed_cb(onMemberRemovedCallback);
gui_bridge_set_my_node_id_cb([](uint64_t nid) {
DEBUG_DEBUG(DEBUG_CATEGORY_CHAT, "MainWindow: MY_NODE_ID setMyNodeId(%016llx)", (unsigned long long)nid);
if (s_mainWindow && s_mainWindow->m_db) s_mainWindow->m_db->setMyNodeId((quint64)nid);
if (AvatarManager::instance()) AvatarManager::instance()->initialize();
});
gui_bridge_set_auto_connect_status_cb(onAutoConnectStatusCallback);
gui_bridge_set_channel_peers_online_cb(onChannelPeersOnlineCallback);
gui_bridge_set_db_ready_cb([]() {
if (!s_mainWindow || !s_mainWindow->m_db) return;
auto* node = s_mainWindow->m_node;
auto* database = node ? node->databaseHandle() : nullptr;
if (!database) { DEBUG_WARN(DEBUG_CATEGORY_SYS, "desktop DB_READY ignored while core unavailable/stopping"); return; }
s_mainWindow->m_db->setDb(database);
s_mainWindow->m_db->setDbPath(node->databasePath());
s_mainWindow->m_db->setMyNodeId(node->nodeIdHex().toULongLong(nullptr, 16));
if (AvatarManager::instance()) AvatarManager::instance()->initialize();
RadioAudioEngine::instance()->loadSettings(s_mainWindow->m_cfgPath);
s_mainWindow->m_channelList->loadChannels();
s_mainWindow->restoreWindowGeometry();
PttHotkeyManager::instance()->init(s_mainWindow->m_db); /* глобальные PTT-хоткеи */
QString lastCid = s_mainWindow->m_db->getUiState("last_channel_id");
if (!lastCid.isEmpty()) {
/* восстанавливаем канал только если он реально существует в БД —
иначе не дёргаем подключение к несуществующей группе */
bool exists = false;
const auto chs = s_mainWindow->m_db->getChannels();
for (const auto& ch : chs) { if (ch.channelId == lastCid) { exists = true; break; } }
if (exists) {
s_mainWindow->m_channelList->selectChannel(lastCid);
s_mainWindow->m_accountList->setChannel(lastCid);
s_mainWindow->m_currentChannelId = lastCid;
s_mainWindow->m_messageList->loadChannel(lastCid);
} else {
saveLastChannelId(QString());
}
}
});
gui_bridge_set_attachment_downloaded_cb([](const char* ch_id, int ch_id_len, int64_t msg_id) {
if (!s_mainWindow) return;
QString chId = QString::fromUtf8(ch_id, ch_id_len);
s_mainWindow->m_channelList->loadChannels();
s_mainWindow->m_messageList->onAttachmentDownloaded(chId, msg_id);
s_mainWindow->onAttachmentDownloadedForAutoPlay(chId, msg_id);
});
gui_bridge_set_download_progress_cb([](const char* ch_id, int ch_id_len, int64_t msg_id, int blocks_done, int num_blocks) {
if (!s_mainWindow) return;
QString chId = QString::fromUtf8(ch_id, ch_id_len);
s_mainWindow->m_messageList->updateMessageProgress(chId, msg_id, blocks_done, num_blocks);
});
gui_bridge_set_admin_key_received_cb([](const uint8_t* data, int len) {
if (!s_mainWindow || len < 2) return;
uint8_t chLen = data[0];
if (len < 1 + chLen + 8) return;
QString chId = QString::fromUtf8((const char*)data + 1, chLen);
uint64_t from; memcpy(&from, data + 1 + chLen, 8);
s_mainWindow->m_trayIcon->showMessage(
"vibechat",
QCoreApplication::translate("ChatGui", "You are now a channel co-owner.\nReceived admin key for channel %1 from 0x%2")
.arg(chId).arg(QString::number(from, 16)),
QSystemTrayIcon::Information, 5000);
if (s_mainWindow->m_currentChannelId == chId)
s_mainWindow->m_accountList->setChannel(chId);
});
/* ── звонки ── */
gui_bridge_set_call_incoming_cb([](const char* ch_id, int ch_id_len, uint64_t caller, uint64_t call_id) {
if (!s_mainWindow) return;
s_mainWindow->onCallIncoming(QString::fromUtf8(ch_id, ch_id_len), caller, call_id);
});
gui_bridge_set_call_ringing_cb([](uint64_t call_id) {
if (s_mainWindow) s_mainWindow->onCallRinging(call_id);
});
gui_bridge_set_call_phone_busy_cb([](uint64_t call_id) {
if (!s_mainWindow) return;
auto* window = s_mainWindow->m_activeCalls.value(call_id, nullptr);
if (!window || window->state() != CallWindow::Outgoing) return;
SoundManager::instance()->stopLoop();
window->setState(CallWindow::WaitDecision);
window->show(); window->raise();
});
gui_bridge_set_call_accepted_cb([](uint64_t call_id) {
if (s_mainWindow) s_mainWindow->onCallAccepted(call_id);
});
gui_bridge_set_call_ended_cb([](uint64_t call_id, uint8_t reason) {
if (s_mainWindow) s_mainWindow->onCallEnded(call_id, reason);
});
gui_bridge_set_call_declined_cb([](uint64_t call_id, uint8_t reason) {
if (s_mainWindow) s_mainWindow->onCallEnded(call_id, reason);
});
gui_bridge_set_call_error_cb([](uint64_t call_id, uint8_t err, const char* text, int len) {
Q_UNUSED(text); Q_UNUSED(len);
if (s_mainWindow) s_mainWindow->onCallError(call_id, err);
});
gui_bridge_set_call_stats_cb([](uint64_t id, int rtt, int buffer, int tempo, int dropped,
int underruns, int minimum, int maximum, int reserve) {
if (!s_mainWindow) return;
auto* w = s_mainWindow->m_activeCalls.value(id, nullptr);
if (w) w->setAudioStats(rtt, buffer, tempo, dropped, underruns, minimum, maximum, reserve);
});
gui_bridge_set_call_path_cb([](uint64_t call_id, const char* text, int len) {
if (s_mainWindow) s_mainWindow->onCallPath(call_id, QString::fromUtf8(text, len));
});
gui_bridge_set_call_connection_cb([](uint64_t id, uint8_t type, uint64_t nextHop) {
if (!s_mainWindow) return;
auto* w = s_mainWindow->m_activeCalls.value(id, nullptr);
if (w && w->state() != CallWindow::Ended) w->setConnectionType(type, nextHop);
});
/* ── рация: индикация говорящего ── */
gui_bridge_set_radio_talk_cb([](uint64_t group_id, uint64_t src_node_id, uint8_t on) {
if (s_mainWindow && s_mainWindow->m_accountList)
s_mainWindow->m_accountList->model()->onRadioTalk(group_id, src_node_id, on != 0);
if (s_mainWindow && s_mainWindow->m_messageList)
s_mainWindow->m_messageList->onRadioTalk((quint64)group_id, (quint64)src_node_id, on != 0);
});
/* ── DM (p2p чат) ── */
gui_bridge_set_dm_conv_updated_cb([](const char* conv_id, int conv_id_len, uint64_t generation) {
if (s_mainWindow)
s_mainWindow->onDmConvUpdated(QString::fromUtf8(conv_id, conv_id_len), generation);
});
gui_bridge_set_dm_delete_result_cb([](const uint8_t* data, int len) {
if (!s_mainWindow || len != 25) return;
quint64 request, conv, generation;
memcpy(&request, data, 8); memcpy(&conv, data + 8, 8); memcpy(&generation, data + 16, 8);
s_mainWindow->onDmDeleteResult(request, QString::number(conv), generation, data[24] != 0);
});
gui_bridge_set_dm_msg_received_cb([](const char* conv_id, int conv_id_len, uint64_t author) {
if (s_mainWindow)
s_mainWindow->onDmMsgReceived(QString::fromUtf8(conv_id, conv_id_len), author);
});
gui_bridge_set_network_changed_cb([](int type) {
if (!s_mainWindow) return;
if (type == 13) {
const auto pending = s_mainWindow->m_dmDeleteRequests;
for (auto it = pending.cbegin(); it != pending.cend(); ++it)
s_mainWindow->onDmDeleteResult(it.key(), it.value().first, it.value().second, false);
s_mainWindow->m_cpuLoadPending = false;
s_mainWindow->m_channelList->setCpuLoad(0);
s_mainWindow->m_accountList->setChannel(QString());
s_mainWindow->m_messageList->clearDmCallRoute();
s_mainWindow->m_messageList->clearStreams();
if (s_mainWindow->m_streamPlayer) s_mainWindow->m_streamPlayer->close();
if (s_mainWindow->m_streamGrid) s_mainWindow->m_streamGrid->close();
s_mainWindow->m_channelList->clearDmPresence();
} else {
if (type == 12) s_mainWindow->m_messageList->watchStreams();
s_mainWindow->m_channelList->refreshDmPresence();
}
});
gui_bridge_set_dm_call_route_cb([](const uint8_t* data, int len) {
if (!s_mainWindow) return;
s_mainWindow->m_messageList->onDmCallRoute(data, len);
s_mainWindow->m_channelList->onDmCallRoute(data, len);
});
gui_bridge_set_dm_messages_cb([](const uint8_t* data, int len) {
if (s_mainWindow)
s_mainWindow->m_messageList->onDmMessages(data, len);
});
gui_bridge_set_stream_sources_cb([](const uint8_t* data, int len) {
if (!s_mainWindow) return;
s_mainWindow->m_messageList->onStreamSources(data, len);
if (s_mainWindow->m_streamGrid) s_mainWindow->m_streamGrid->onSources(data, len);
});
gui_bridge_set_attachment_preparation_cb([](const uint8_t* data, int len) {
if (s_mainWindow) s_mainWindow->m_messageList->onAttachmentPreparation(data, len);
});
}
/* Один publisher обслуживает шапку и свою плитку; окно управления не владеет сеткой и её readers. */
void MainWindow::showStreamPublisher(quint64 group, StreamGridWindow* returnTo) {
QElapsedTimer elapsed; elapsed.start();
if (!m_streamPublisher) {
m_streamPublisher = new StreamPublishEngine(this);
connect(m_streamPublisher, &StreamPublishEngine::stateChanged, this, [this](const QString& state) {
m_messageList->setStreamPublisherGroup(m_streamPublisher->active() ? m_streamPublisher->group() : 0);
if (m_trayIcon) m_trayIcon->setToolTip(m_streamPublisher->active() ? QStringLiteral("vibechat · ") + state : QStringLiteral("vibechat"));
});
connect(m_streamPublisher, &StreamPublishEngine::previewVideoChanged, this, [this](const QVariant& frame) {
if (m_streamGrid) m_streamGrid->setLocalPreview(m_streamPublisher->group(), frame);
});
}
qint64 engineUs = elapsed.nsecsElapsed() / 1000;
if (m_streamPublishWindow) delete m_streamPublishWindow.data();
qint64 removedUs = elapsed.nsecsElapsed() / 1000;
m_streamPublishWindow = new StreamPublishWindow(m_streamPublisher, m_streamPublisher->active() ? m_streamPublisher->group() : group,
this, m_db ? m_db->getUiState("stream_capture_settings") : QString());
qint64 constructedUs = elapsed.nsecsElapsed() / 1000;
connect(m_streamPublishWindow, &StreamPublishWindow::settingsChanged, this,
[](const QString& settings) { postUiStateKv("stream_capture_settings", settings); });
if (returnTo) connect(m_streamPublishWindow, &StreamPublishWindow::closed, returnTo, [returnTo] {
if (!returnTo->isVisible()) return;
returnTo->raise(); returnTo->activateWindow();
DEBUG_INFO(DEBUG_CATEGORY_STREAM, "desktop publisher returns to multiview group=%llx tiles=%d",
(unsigned long long)returnTo->group(), returnTo->tileCount());
});
DEBUG_INFO(DEBUG_CATEGORY_STREAM, "desktop publisher controls opened group=%llx return_to_grid=%d",
(unsigned long long)group, returnTo != nullptr);
m_streamPublishWindow->show(); m_streamPublishWindow->raise(); m_streamPublishWindow->activateWindow();
qint64 shownUs = elapsed.nsecsElapsed() / 1000;
DEBUG_DEBUG(DEBUG_CATEGORY_DEBUG,
"desktop publisher UI group=%llx active=%d return_to_grid=%d engine_us=%lld remove_us=%lld construct_us=%lld show_us=%lld total_us=%lld",
(unsigned long long)group, m_streamPublisher->active(), returnTo != nullptr, (long long)engineUs,
(long long)(removedUs - engineUs), (long long)(constructedUs - removedUs),
(long long)(shownUs - constructedUs), (long long)shownUs);
}
void MainWindow::setupConnects() {
connect(m_accountList, &AccountList::radioCallRequested, this, &MainWindow::onRadioCallRequested);
gui_bridge_set_radio_call_cb([](const uint8_t* data, int len) {
if (s_mainWindow) s_mainWindow->onRadioCallEvent(data, len);
});
connect(m_messageList, &MessageList::streamPublishRequested, this, [this](quint64 group) { showStreamPublisher(group); });
connect(m_messageList, &MessageList::streamRequested, this,
[this](quint64 group, quint64 source, quint64 epoch, const QString& name) {
if (m_streamGrid) delete m_streamGrid.data();
if (m_streamPlayer) delete m_streamPlayer.data();
m_streamPlayer = new StreamPlayerWindow(group, source, epoch, name);
m_streamPlayer->setAttribute(Qt::WA_DeleteOnClose); m_streamPlayer->show();
});
connect(m_messageList, &MessageList::allStreamsRequested, this, [this](quint64 group) {
if (m_streamPlayer) delete m_streamPlayer.data();
if (m_streamGrid && m_streamGrid->group() != group) delete m_streamGrid.data();
if (!m_streamGrid) {
m_streamGrid = new StreamGridWindow(group);
m_streamGrid->setAttribute(Qt::WA_DeleteOnClose);
connect(m_streamGrid, &StreamGridWindow::publishRequested, this, [this](quint64 group) {
showStreamPublisher(group, m_streamGrid);
});
if (m_streamPublisher) m_streamGrid->setLocalPreview(m_streamPublisher->group(), m_streamPublisher->previewFrame());
}
m_streamGrid->show(); m_streamGrid->raise(); m_streamGrid->activateWindow();
});
connect(RadioAudioEngine::instance(), &RadioAudioEngine::stateChanged, this, [this] {
auto* engine = RadioAudioEngine::instance();
m_accountList->model()->setRadioState(engine->groupId(), engine->isActive(), engine->isTransmitting());
});
connect(m_channelList, &ChannelList::channelSelected,
this, [this](const QString& channelId) {
saveLastChannelId(channelId);
m_currentChannelId = channelId;
m_messageList->loadChannel(channelId);
if (channelId.startsWith(QStringLiteral("dm:"))) {
/* DM: беседа уже создана/обслуживается dm_core */
m_accountList->setChannel(QString());
m_channelList->setChannelUnreadCount(channelId, 0);
return;
}
m_accountList->setChannel(channelId);
struct chat_create_auto_arg* arg = (struct chat_create_auto_arg*)u_calloc(1, sizeof(*arg));
if (arg) {
snprintf(arg->name, sizeof(arg->name), "%s", channelId.toUtf8().constData());
if (gui_bridge_post_bound_fn(chat_core_connect_channel_trampoline, arg, &arg->inst) < 0) u_free(arg);
} else DEBUG_ERROR(DEBUG_CATEGORY_CHAT, "channel connect request allocation failed channel=%s", qPrintable(channelId));
});
connect(m_messageList, &MessageList::shareInviteRequested, this, &MainWindow::showShareInviteDialog);
connect(m_messageList, &MessageList::inviteByLinkRequested, this, &MainWindow::showInviteByLinkDialog);
connect(m_channelList, &ChannelList::joinRequested, this, &MainWindow::showJoinDialog);
connect(m_channelList, &ChannelList::createGroupRequested, this, &MainWindow::onCreateGroupRequested);
connect(m_channelList, &ChannelList::settingsRequested, this, &MainWindow::showSettings);
connect(m_channelList, &ChannelList::connMonitorRequested, this, [] { ConnMonitorWindow::showMonitor(); });
connect(m_channelList, &ChannelList::dmDeleteRequested, this, &MainWindow::onDmDeleteRequested);
connect(m_messageList, &MessageList::channelSettingsRequested, this, &MainWindow::showChannelSettings);
connect(m_accountList, &AccountList::memberRenamed,
m_messageList, &MessageList::reloadLocalNicks);
connect(m_accountList, &AccountList::filesRequested, this, &MainWindow::showMemberFiles);
connect(m_channelList, &ChannelList::filesRequested, this, [this](const QString& channel) { showMemberFiles(channel); });
connect(m_messageList, &MessageList::filesRequested, this, [this](const QString& channel) { showMemberFiles(channel); });
connect(m_messageList, &MessageList::ownFilesRequested, this, [this](const QString& channel) {
quint64 owner = m_db ? m_db->myNodeId() : 0;
if (!channel.toULongLong() || !owner) {
DEBUG_WARN(DEBUG_CATEGORY_MEDIA, "file browser: own files unavailable channel=%s owner=%llu",
qPrintable(channel), (unsigned long long)owner);
return;
}
DEBUG_INFO(DEBUG_CATEGORY_MEDIA, "file browser: opening own files channel=%s owner=%llu",
qPrintable(channel), (unsigned long long)owner);
showMemberFiles(channel, owner);
});
connect(m_channelList, &ChannelList::ownFilesDropped, this, [this](const QString& channel, const QStringList& paths) {
if (!m_fileBrowser) m_fileBrowser = new MemberFileBrowser(m_db, this);
m_fileBrowser->show(); m_fileBrowser->raise(); m_fileBrowser->activateWindow();
m_fileBrowser->uploadOwnFiles(channel, paths);
});
connect(m_accountList, &AccountList::callRequested, this, &MainWindow::onCallRequested);
connect(m_messageList, &MessageList::callRequested, this, &MainWindow::onCallRequested);
connect(m_accountList, &AccountList::pmRequested, this, &MainWindow::onPmRequested);
connect(m_accountList, &AccountList::inviteRequested, this, &MainWindow::onInviteToPmRequested);
connect(m_messageList, &MessageList::inviteRequested, this, &MainWindow::onInviteToPmRequested);
connect(m_messageList, &MessageList::linkActivated, this, &MainWindow::onMessageLinkActivated);
connect(m_messageList, &MessageList::readPositionChanged,
this, [this](const QString& channelId, qint64 lastReadMsgId) {
Q_UNUSED(lastReadMsgId);
if (m_channelList && m_db)
m_channelList->setChannelUnreadCount(channelId,
m_db->getUnreadCount(channelId, lastReadMsgId));
});
connect(m_messageList, &MessageList::openVideoRequested, this, &MainWindow::openVideo);
connect(m_messageList, &MessageList::openImageRequested, this, &MainWindow::openImage);
}
/* ── звонки ── */
void MainWindow::showMemberFiles(const QString& channel, quint64 owner) {
if (channel.startsWith(QStringLiteral("dm:"))) return;
if (!m_fileBrowser) m_fileBrowser = new MemberFileBrowser(m_db, this);
m_fileBrowser->setChannel(channel, owner); m_fileBrowser->show(); m_fileBrowser->raise(); m_fileBrowser->activateWindow();
}
/* GUI передаёт мосту значения; ядро и время жизни запроса выбирает мост. */
void MainWindow::onRadioCallRequested(const QString& channel, quint64 peer) {
if (m_radioCallWindow) { m_radioCallWindow->show(); m_radioCallWindow->raise(); return; }
quint64 id = QRandomGenerator::global()->generate64();
if (gui_bridge_radio_call(GUI_RADIO_CALL_START, id ? id : 1, channel.toULongLong(), peer) < 0)
DEBUG_WARN(DEBUG_CATEGORY_RADIO, "desktop radio call start was not queued channel=%s peer=%llu",
qPrintable(channel), (unsigned long long)peer);
}
/* Один диалог на незавершённое приглашение; принятая рация живёт своим обычным циклом. */
void MainWindow::onRadioCallEvent(const uint8_t* data, int len) {
if (!data || len != 27 || data[24] < RADIO_CALL_OUTGOING || data[24] > RADIO_CALL_WAIT_DECISION ||
data[25] > RADIO_CALL_ALREADY_ACTIVE || data[26] > 1) {
DEBUG_ERROR(DEBUG_CATEGORY_RADIO, "desktop invalid radio call event bytes=%d", len); return;
}
quint64 id, group, peer;
memcpy(&id, data, 8); memcpy(&group, data + 8, 8); memcpy(&peer, data + 16, 8);
int state = data[24], reason = data[25]; bool incoming = data[26];
const QString channel = QString::number(group);
if (state == RADIO_CALL_FINISHED) {
if (m_radioCallId == id) {
m_radioCallId = 0;
SoundManager::instance()->stopLoop();
if (m_radioCallWindow) m_radioCallWindow->close();
m_radioCallWindow = nullptr;
}
if (reason == RADIO_CALL_ACCEPTED) {
if (!incoming) {
m_channelList->selectChannel(channel);
if (m_currentChannelId != channel || !m_messageList->enableRadio()) reason = RADIO_CALL_AUDIO_ERROR;
}
if (reason == RADIO_CALL_ACCEPTED) return;
}
if ((incoming && reason != RADIO_CALL_AUDIO_ERROR) || reason == RADIO_CALL_CANCELLED) return;
const char* text = reason == RADIO_CALL_ALREADY_ACTIVE ? "Subscriber's radio is already on in this channel" :
reason == RADIO_CALL_PHONE_BUSY ? "Subscriber is on a phone call" : reason == RADIO_CALL_DECLINED ? "Radio call declined" :
reason == RADIO_CALL_BUSY ? "Subscriber is busy" : reason == RADIO_CALL_NO_ANSWER ? "No answer" :
reason == RADIO_CALL_AUDIO_ERROR ? "Could not start channel radio" : "Subscriber is unavailable";
auto* notice = new QMessageBox(QMessageBox::Information, QCoreApplication::translate("ChatGui", "Radio call"),
QCoreApplication::translate("ChatGui", text), QMessageBox::Ok, this);
notice->setAttribute(Qt::WA_DeleteOnClose); notice->show();
if (m_trayIcon && (!isVisible() || isMinimized()))
m_trayIcon->showMessage(QCoreApplication::translate("ChatGui", "Radio call"), QCoreApplication::translate("ChatGui", text));
return;
}
if (state == RADIO_CALL_RINGING) {
if (m_radioCallId == id && m_radioCallWindow) {
m_radioCallWindow->setInformativeText(QCoreApplication::translate("ChatGui", "Ringing…"));
SoundManager::instance()->playLoop("call_ringing");
}
return;
}
if (state == RADIO_CALL_WAIT_DECISION && incoming) return;
if (state == RADIO_CALL_WAIT_DECISION && m_radioCallId == id) {
m_radioCallId = 0;
SoundManager::instance()->stopLoop();
if (m_radioCallWindow) m_radioCallWindow->close();
m_radioCallWindow = nullptr;
}
if (m_radioCallId == id) return;
if (m_radioCallId || !m_activeCalls.isEmpty()) { gui_bridge_radio_call(GUI_RADIO_CALL_REJECT_BUSY, id, 0, 0); return; }
QString name = m_db->getDisplayName(peer);
if (name.isEmpty()) name = QStringLiteral("0x%1").arg(peer, 16, 16, QChar('0'));
QString channelName = channel;
for (const auto& row : m_db->getChannels()) if (row.channelId == channel) { channelName = row.name; break; }
auto* box = new QMessageBox(QMessageBox::Question, QCoreApplication::translate("ChatGui", "Radio call"),
QCoreApplication::translate("ChatGui", incoming ? "Incoming radio call from %1\nChannel: %2" : "Calling %1 by radio\nChannel: %2")
.arg(name, channelName), QMessageBox::NoButton, this);
box->setTextFormat(Qt::PlainText);
box->setAttribute(Qt::WA_DeleteOnClose);
m_radioCallId = id; m_radioCallWindow = box; m_radioCallAnswered = false;
if (incoming) {
auto* accept = box->addButton(QCoreApplication::translate("ChatGui", "Accept"), QMessageBox::AcceptRole);
box->addButton(QCoreApplication::translate("ChatGui", "Decline"), QMessageBox::RejectRole);
connect(box, &QMessageBox::buttonClicked, this, [this, id, channel, accept](QAbstractButton* button) {
if (m_radioCallId != id) return;
m_radioCallAnswered = true;
if (button != accept) { gui_bridge_radio_call(GUI_RADIO_CALL_DECLINE, id, 0, 0); return; }
SoundManager::instance()->stopLoop();
m_channelList->selectChannel(channel);
bool ready = m_currentChannelId == channel && m_messageList->enableRadio();
gui_bridge_radio_call(ready ? GUI_RADIO_CALL_ACCEPT : GUI_RADIO_CALL_FAIL, id, 0, 0);
});
} else if (state == RADIO_CALL_WAIT_DECISION) {
box->setInformativeText(QCoreApplication::translate("ChatGui", "Subscriber is on a phone call"));
auto* proceed = box->addButton(QCoreApplication::translate("ChatGui", "Call anyway"), QMessageBox::AcceptRole);
box->addButton(QCoreApplication::translate("ChatGui", "End call"), QMessageBox::RejectRole);
connect(box, &QMessageBox::buttonClicked, this, [this, id, proceed](QAbstractButton* button) {
if (m_radioCallId != id || button != proceed) return;
if (gui_bridge_radio_call(GUI_RADIO_CALL_CONTINUE, id, 0, 0) < 0) return;
m_radioCallAnswered = true;
m_radioCallId = 0;
DEBUG_INFO(DEBUG_CATEGORY_RADIO, "desktop caller overrides phone busy id=%016llx", (unsigned long long)id);
});
} else {
box->addButton(QMessageBox::Cancel);
box->setInformativeText(QCoreApplication::translate("ChatGui", "Checking subscriber availability…"));
}
connect(box, &QDialog::finished, this, [this, id, incoming] {
if (m_radioCallId == id && !m_radioCallAnswered) {
m_radioCallAnswered = true;
SoundManager::instance()->stopLoop();
gui_bridge_radio_call(incoming ? GUI_RADIO_CALL_DECLINE : GUI_RADIO_CALL_CANCEL, id, 0, 0);
}
});
box->show(); box->raise(); box->activateWindow();
if (state != RADIO_CALL_WAIT_DECISION) SoundManager::instance()->playLoop(incoming ? "radio_incoming" : "call_connecting");
if (incoming) {
if (m_trayIcon && (!isVisible() || isMinimized()))
m_trayIcon->showMessage(QCoreApplication::translate("ChatGui", "Radio call"), box->text());
gui_bridge_radio_call(GUI_RADIO_CALL_READY, id, 0, 0);
}
}
void MainWindow::onCallRequested(const QString& chId, quint64 nodeId, const QString& name) {
if (chId.startsWith(QStringLiteral("dm:")) && !m_activeCalls.isEmpty()) {
auto* w = m_activeCalls.constBegin().value();
DEBUG_INFO(DEBUG_CATEGORY_CALL, "PM call: existing call window id=%llu", (unsigned long long)w->callId());
w->show(); w->raise(); w->activateWindow();
return;
}
quint64 callId = QRandomGenerator::global()->generate64();
if (!callId) callId = 1;
if (gui_bridge_call_start(chId.toUtf8().constData(), nodeId, callId) < 0) {
DEBUG_WARN(DEBUG_CATEGORY_CALL, "desktop call start was not queued id=%llu", (unsigned long long)callId); return;
}
CallWindow* w = new CallWindow(this);
w->setCallId(callId);
w->setPeerName(name);
w->setState(CallWindow::Outgoing);
connect(w, &CallWindow::closed, this, &MainWindow::closeCallWindow);
m_activeCalls.insert(callId, w);
w->show();
SoundManager::instance()->playLoop("call_connecting");
}
void MainWindow::onCallIncoming(const QString& chId, quint64 caller, quint64 callId) {
Q_UNUSED(chId);
if (m_activeCalls.contains(callId)) return;
QString name = m_db->getDisplayName(caller);
if (name.isEmpty()) name = QStringLiteral("0x%1").arg(caller, 16, 16, QChar('0'));
SoundManager::instance()->playLoop("call_incoming");
if (!isVisible() || isMinimized()) {
m_trayIcon->showMessage("vibechat", QCoreApplication::translate("ChatGui", "Incoming call from %1").arg(name),
QSystemTrayIcon::Information, 5000);
}
CallWindow* w = new CallWindow(this);
w->setCallId(callId);
w->setPeerName(name);
w->setState(CallWindow::Incoming);
connect(w, &CallWindow::closed, this, &MainWindow::closeCallWindow);
m_activeCalls.insert(callId, w);
w->show();
w->raise();
w->activateWindow();
}
void MainWindow::onCallRinging(quint64 callId) {
/* caller: INVITE доставлен, у пира идёт вызов — фаза 2 (гудки) */
CallWindow* w = m_activeCalls.value(callId, nullptr);
if (w && w->state() == CallWindow::Outgoing)
w->setState(CallWindow::Ringing);
SoundManager::instance()->playLoop("call_ringing");
}
void MainWindow::onCallAccepted(quint64 callId) {
SoundManager::instance()->stopLoop();
CallWindow* w = m_activeCalls.value(callId, nullptr);
if (w) w->setState(CallWindow::Active);
if (!s_mainWindow) return;
if (CallAudioEngine::instance()->start(callId) != 0) {
DEBUG_WARN(DEBUG_CATEGORY_CALL, "desktop call cannot acquire audio; hangup id=%llu", (unsigned long long)callId);
if (gui_bridge_call_action(GUI_CALL_HANGUP, callId) < 0)
DEBUG_WARN(DEBUG_CATEGORY_CALL, "desktop failed call hangup was not queued id=%llu", (unsigned long long)callId);
closeCallWindow(callId);
QMessageBox::information(this, QStringLiteral("vibechat"), QCoreApplication::translate("ChatGui", "The microphone is used by another operation"));
}
}
void MainWindow::onCallEnded(quint64 callId, uint8_t reason) {
const bool phoneBusy = reason == CALL_REASON_PHONE_BUSY && m_activeCalls.contains(callId);
/* тон завершения (~450мс) → авто-teardown аудио-движка */
if (CallAudioEngine::instance()->callId() == callId)
CallAudioEngine::instance()->beginEnd(callId);
closeCallWindow(callId);
if (phoneBusy) {
auto* notice = new QMessageBox(QMessageBox::Information, QStringLiteral("vibechat"),
QCoreApplication::translate("ChatGui", "Subscriber is on a phone call"), QMessageBox::Ok, this);
notice->setAttribute(Qt::WA_DeleteOnClose); notice->show();
}
}
void MainWindow::onCallError(quint64 callId, uint8_t err) {
/* пир офлайн / нет маршрута — звонок не состоялся */
DEBUG_WARN(DEBUG_CATEGORY_CALL, "MainWindow: call error id=%016llx err=%u", (unsigned long long)callId, (unsigned)err);
closeCallWindow(callId);
}
void MainWindow::onCallPath(quint64 callId, const QString& path) {
CallWindow* w = m_activeCalls.value(callId, nullptr);
if (w) w->setBgpPath(path);
}
void MainWindow::closeCallWindow(quint64 callId) {
SoundManager::instance()->stopLoop();
CallWindow* w = m_activeCalls.take(callId);
if (!w) return;
w->disconnect(this);
w->deleteLater();
}
/* ── DM (p2p чат) ── */
void MainWindow::onPmRequested(const QString& chId, quint64 nodeId, const QString& name) {
struct dm_start_req* r = (struct dm_start_req*)u_malloc(sizeof(*r));
if (!r) { DEBUG_ERROR(DEBUG_CATEGORY_DM, "PM start request allocation failed peer=%llu", (unsigned long long)nodeId); return; }
memset(r, 0, sizeof(*r));
r->peer_node_id = nodeId;
strncpy(r->source_ch_id, chId.toUtf8().constData(), sizeof(r->source_ch_id) - 1);
strncpy(r->peer_name, name.toUtf8().constData(), sizeof(r->peer_name) - 1);
if (gui_bridge_post_bound_fn(dm_start_trampoline, r, &r->inst) < 0) u_free(r);
/* открытие беседы произойдёт по CHAT_EVT_DM_CONV_UPDATED (onDmConvUpdated) */
}
void MainWindow::onDmConvUpdated(const QString& convId, quint64 generation) {
if (!m_channelList || !m_db) return;
bool visible = false;
for (const auto& conv : m_db->getDmConversations()) if (conv.convId == convId && conv.generation == generation) visible = true;
if (!visible) {
GUI_INFO("PM update ignored: hidden/stale conv=%s generation=%llu", qPrintable(convId), (unsigned long long)generation); return;
}
QString key = QStringLiteral("dm:") + convId;
m_channelList->loadChannels();
if (m_currentChannelId == key) { m_messageList->loadChannel(key); return; }
if (qobject_cast<DmInviteDialog*>(QApplication::activeModalWidget())) return;
m_channelList->selectChannel(key);
}
/* Диалог фиксирует поколение строки до вложенного Qt event loop; окончательную проверку выполняет ядро. */
void MainWindow::onDmDeleteRequested(const QString& convId, quint64 generation, const QString& name) {
for (const auto& pending : m_dmDeleteRequests) if (pending.first == convId) return;
QMessageBox box(QMessageBox::Warning, QCoreApplication::translate("ChatGui", "Delete conversation"),
QCoreApplication::translate("ChatGui", "Delete the conversation with %1 and its local files? The other person keeps their history.")
.arg(name), QMessageBox::NoButton, this);
auto* remove = box.addButton(QCoreApplication::translate("ChatGui", "Delete"), QMessageBox::DestructiveRole);
auto* cancel = box.addButton(QCoreApplication::translate("ChatGui", "Cancel"), QMessageBox::RejectRole);
box.setDefaultButton(cancel); box.setEscapeButton(cancel); box.exec();
if (box.clickedButton() != remove) return;
const quint64 request = ++m_dmDeleteSequence;
m_dmDeleteRequests.insert(request, {convId, generation});
m_channelList->setDmDeleting(convId, generation, true);
m_messageList->setDmDeleting(convId, generation, true);
if (gui_bridge_delete_dm(qPrintable(convId), generation, request)) onDmDeleteResult(request, convId, generation, false);
}
/* Только подтверждённый commit очищает беседу. Старый результат не закрывает PM нового поколения. */
void MainWindow::onDmDeleteResult(quint64 request, const QString& convId, quint64 generation, bool success) {
auto expected = m_dmDeleteRequests.constFind(request);
if (expected == m_dmDeleteRequests.cend()) {
GUI_INFO("PM delete: ignored obsolete result request=%llu", (unsigned long long)request); return;
}
if (expected->first != convId || expected->second != generation) {
GUI_ERROR("PM delete: result target mismatch request=%llu conv=%s generation=%llu", (unsigned long long)request,
qPrintable(convId), (unsigned long long)generation); return;
}
m_dmDeleteRequests.remove(request);
GUI_INFO("PM delete completed: request=%llu conv=%s generation=%llu success=%d", (unsigned long long)request,
qPrintable(convId), (unsigned long long)generation, success);
if (success && m_messageList->forgetDmHistory(convId, generation)) {
m_currentChannelId.clear(); saveLastChannelId(QString()); m_accountList->setChannel(QString());
}
m_channelList->setDmDeleting(convId, generation, false);
m_messageList->setDmDeleting(convId, generation, false);
m_channelList->loadChannels();
if (!success) {
GUI_ERROR("PM delete failed: conv=%s generation=%llu request=%llu", qPrintable(convId),
(unsigned long long)generation, (unsigned long long)request);
auto* error = new QMessageBox(QMessageBox::Warning, QCoreApplication::translate("ChatGui", "Delete conversation"),
QCoreApplication::translate("ChatGui", "Could not delete the conversation. Please try again."), QMessageBox::Ok, this);
error->setAttribute(Qt::WA_DeleteOnClose); error->open();
}
}
void MainWindow::onInviteToPmRequested(const QString& source, quint64 peer, const QString& name) {
if (!m_db || !m_db->isOpen() || !m_node || !m_node->isRunning()) {
GUI_ERROR("invite PM: node unavailable");
QMessageBox::warning(this, QCoreApplication::translate("ChatGui",
"Invitation"), QCoreApplication::translate("ChatGui",
"The core is not running.")); return;
}
if (!peer || peer == m_db->myNodeId()) { GUI_ERROR("invite PM: invalid recipient"); return; }
DmInviteDialog dialog(m_db, peer, name, source, this);
if (dialog.exec() == QDialog::Accepted) {
m_channelList->loadChannels();
m_channelList->selectChannel(QStringLiteral("dm:") + dialog.conversationId());
}
}
void MainWindow::onMessageLinkActivated(const QString& link) {
if (!link.startsWith(QStringLiteral("utun://"))) {
const QUrl url(link, QUrl::StrictMode);
if (!url.isValid() || url.host().isEmpty() || (url.scheme() != "http" && url.scheme() != "https")) {
GUI_ERROR("message link: invalid web address bytes=%d", (int)link.size());
QMessageBox::warning(this, QCoreApplication::translate("ChatGui",
"Link"), QCoreApplication::translate("ChatGui",
"Invalid link.")); return;
}
if (!QDesktopServices::openUrl(url)) {
GUI_ERROR("message link: external open failed scheme=%s host=%s", qPrintable(url.scheme()), qPrintable(url.host()));
QMessageBox::warning(this, QCoreApplication::translate("ChatGui",
"Link"), QCoreApplication::translate("ChatGui",
"Failed to open the link. Check that a browser is installed.")); return;
}
GUI_INFO("message link: external open requested scheme=%s host=%s", qPrintable(url.scheme()), qPrintable(url.host()));
return;
}
const auto invite = decodeInviteLink(link);
if (!invite.error.isEmpty()) {
GUI_ERROR("invite link: invalid error=%s", qPrintable(invite.error));
QMessageBox::warning(this, QCoreApplication::translate("ChatGui",
"Invitation"), QCoreApplication::translate("ChatGui",
"Invalid link: ") + invite.error); return;
}
if (!m_db || !m_db->isOpen() || !m_node || !m_node->isRunning()) {
GUI_ERROR("invite link: node unavailable");
QMessageBox::warning(this, QCoreApplication::translate("ChatGui",
"Invitation"), QCoreApplication::translate("ChatGui",
"The core is not running.")); return;
}
const QString channelId = QString::number(invite.channelId);
if (invite.kind == INVITE_KIND_SERVER) {
QStringList ids, labels;
for (const auto& channel : m_db->getChannels()) {
if (!m_db->isChannelMember(channel.channelId, m_db->myNodeId())) continue;
ids.append(channel.channelId); labels.append(channel.name + " (" + channel.channelId + ")");
}
bool accepted = false;
const QString selected = QInputDialog::getItem(this, QCoreApplication::translate("ChatGui", "Add server to channel"),
QCoreApplication::translate("ChatGui", "Channel:"), labels, qMax(0, ids.indexOf(m_currentChannelId)), false, &accepted);
int index = labels.indexOf(selected);
if (!accepted || index < 0) return;
InviteByLinkDialog dialog(ids[index], m_node, this, link); dialog.exec(); return;
}
for (const auto& channel : m_db->getChannels()) {
if (channel.channelId == channelId && m_db->isChannelMember(channelId, m_db->myNodeId())) {
GUI_INFO("invite link: opening existing group ch=%s", qPrintable(channelId));
m_channelList->selectChannel(channelId); return;
}
}
JoinDialog dialog(m_node, m_db, this, link);
connect(&dialog, &JoinDialog::joined, this, [this](const QString& channel) {
reloadChannels(); m_channelList->selectChannel(channel);
});
dialog.exec();
}
void MainWindow::onDmMsgReceived(const QString& convId, quint64 author) {
if (!m_db) return;
bool visible = false;
for (const auto& conv : m_db->getDmConversations()) if (conv.convId == convId) visible = true;
if (!visible) { GUI_DEBUG("PM message event ignored: hidden conv=%s", qPrintable(convId)); return; }
QString key = QStringLiteral("dm:") + convId;
showIncomingMessageNotification(key, author);
if (m_currentChannelId == key) {
m_messageList->refresh();
return;
}
if (author == m_db->myNodeId()) return;
if (m_channelList)
m_channelList->setChannelUnreadCount(key, m_channelList->getChannelUnreadCount(key) + 1);
}
void MainWindow::setupTray() {
m_trayIcon = new QSystemTrayIcon(this);
m_trayIcon->setIcon(QIcon(":/icons/app_icon.png"));
m_trayIcon->setToolTip("vibechat");
m_messageNotification = std::make_unique<MessageNotification>();
connect(m_messageNotification.get(), &MessageNotification::activated, this, [this](const QString& target) {
showAndActivateWindow(this);
reloadChannels();
m_channelList->selectChannel(target);
if (m_currentChannelId != target) GUI_WARN("notification: target no longer available target=%s", qPrintable(target));
else GUI_INFO("notification: opened chat target=%s", qPrintable(target));
});
m_trayMenu = new QMenu(this);
QAction *settingsAction = m_trayMenu->addAction(QCoreApplication::translate("ChatGui", "Settings"));
connect(settingsAction, &QAction::triggered, this, &MainWindow::showSettings);
m_trayMenu->addSeparator();
QAction *showAction = m_trayMenu->addAction(QCoreApplication::translate("ChatGui", "Show"));
connect(showAction, &QAction::triggered, this, [this]() {
showAndActivateWindow(this);
});
QAction *quitAction = m_trayMenu->addAction(QCoreApplication::translate("ChatGui", "Quit"));
connect(quitAction, &QAction::triggered, this, &MainWindow::requestShutdown);
m_trayIcon->setContextMenu(m_trayMenu);
connect(m_trayIcon, &QSystemTrayIcon::activated,
this, &MainWindow::onTrayActivated);
m_trayIcon->show();
}
void MainWindow::showIncomingMessageNotification(const QString& target, quint64 author) {
if (!m_db || !m_db->isOpen() || !m_messageNotification) {
GUI_WARN("notification: GUI not ready target=%s author=%llu", qPrintable(target), (unsigned long long)author);
return;
}
if (author == m_db->myNodeId() || isActiveWindow()) {
GUI_DEBUG("notification: skipped target=%s own=%d window_active=%d",
qPrintable(target), int(author == m_db->myNodeId()), int(isActiveWindow()));
return;
}
QString channel;
if (target.startsWith(QStringLiteral("dm:"))) channel = QCoreApplication::translate("ChatGui", "Direct messages");
else {
for (const auto& row : m_db->getChannels()) {
if (row.channelId == target) { channel = row.name.isEmpty() ? target : row.name; break; }
}
if (channel.isEmpty()) {
GUI_WARN("notification: channel no longer available target=%s", qPrintable(target));
return;
}
}
m_messageNotification->showMessage(target, m_db->getDisplayName(author), channel, m_trayIcon->geometry(), screen());
}
void MainWindow::closeEvent(QCloseEvent *event) {
DEBUG_DEBUG(DEBUG_CATEGORY_GENERAL, "MainWindow: closeEvent — hiding to tray");
saveWindowGeometry();
hide();
event->ignore();
}
void MainWindow::requestShutdown() {
if (!m_node || !m_node->isRunning()) {
DEBUG_INFO(DEBUG_CATEGORY_SYS, "desktop shutdown: core is not running; exiting GUI");
qApp->exit(1);
return;
}
DEBUG_DEBUG(DEBUG_CATEGORY_GENERAL, "MainWindow: requestShutdown — saving geometry and stopping node");
if (m_messageNotification) m_messageNotification->dismiss("shutdown");
saveWindowGeometry();
if (m_messageList) m_messageList->saveChannelState();
// FIFO: stop исполняется после уже принятых записей UI. Node живёт до join его worker.
auto** request = static_cast<UtunNode**>(u_malloc(sizeof(UtunNode*)));
if (!request) {
DEBUG_ERROR(DEBUG_CATEGORY_SYS, "desktop shutdown queue allocation failed; stopping directly"); m_node->requestStop(); return;
}
*request = m_node;
if (gui_bridge_post_uasync_owned_fn([](void* arg) {
auto* node = *static_cast<UtunNode**>(arg); u_free(arg); node->requestStop();
}, request) < 0) {
u_free(request); DEBUG_WARN(DEBUG_CATEGORY_SYS, "desktop shutdown queue unavailable; stopping directly"); m_node->requestStop();
} else DEBUG_INFO(DEBUG_CATEGORY_SYS, "desktop shutdown queued after UI state writes");
}
void MainWindow::onShutdownCheck() {
#ifndef _WIN32
if (g_signalFlag)
requestShutdown();
#endif
}
void MainWindow::onTrayActivated(QSystemTrayIcon::ActivationReason reason) {
if (reason == QSystemTrayIcon::Trigger ||
reason == QSystemTrayIcon::DoubleClick) {
showAndActivateWindow(this);
}
}
void MainWindow::showShareInviteDialog(const QString& channelId) {
if (!m_db || !m_db->isOpen()) return;
QPointer<ShareInviteDialog> dialog = new ShareInviteDialog(channelId, m_db, this);
auto release = qScopeGuard([&] { delete dialog.data(); }); dialog->exec();
}
void MainWindow::showInviteByLinkDialog(const QString& channelId) {
if (!m_node || !m_node->isRunning()) {
QMessageBox::warning(this, QCoreApplication::translate("ChatGui", "Error"), QCoreApplication::translate("ChatGui", "Node is not running."));
return;
}
QPointer<InviteByLinkDialog> dialog = new InviteByLinkDialog(channelId, m_node, this);
auto release = qScopeGuard([&] { delete dialog.data(); }); dialog->exec();
}
void MainWindow::showJoinDialog() {
if (!m_node || !m_node->isRunning()) {
QMessageBox::warning(this, QCoreApplication::translate("ChatGui", "Error"), QCoreApplication::translate("ChatGui", "Node is not running."));
return;
}
QString autoLink;
QClipboard* cb = QApplication::clipboard();
QString clipText = cb->text(QClipboard::Clipboard).trimmed();
if (clipText.startsWith("utun://") && decodeInviteLink(clipText).error.isEmpty())
autoLink = clipText;
else if (cb->supportsSelection()) {
QString selText = cb->text(QClipboard::Selection).trimmed();
if (selText.startsWith("utun://") && decodeInviteLink(selText).error.isEmpty())
autoLink = selText;
}
if (!autoLink.isEmpty() && decodeInviteLink(autoLink).kind == INVITE_KIND_SERVER) {
onMessageLinkActivated(autoLink); return;
}
QPointer<JoinDialog> dialog = new JoinDialog(m_node, m_db, this, autoLink);
auto release = qScopeGuard([&] { delete dialog.data(); }); dialog->exec();
}
void MainWindow::showSettings() {
QPointer<MainWindow> self(this);
QPointer<SettingsDialog> dialog = new SettingsDialog(m_cfgPath, m_db, m_node, m_recorder, this);
auto release = qScopeGuard([&] { delete dialog.data(); }); dialog->exec();
if (self) reloadChannels();
}
void MainWindow::openVideo(const QString& filePath) {
if (!m_videoPlayer) {
m_videoPlayer = new VideoPlayerWindow(nullptr);
connect(m_videoPlayer, &VideoPlayerWindow::closed, this, [this] {
m_videoPlayer->deleteLater();
m_videoPlayer = nullptr;
});
} else {
m_videoPlayer->raise();
m_videoPlayer->activateWindow();
}
m_videoPlayer->playFile(filePath);
}
void MainWindow::openImage(const QString& filePath) {
if (!m_imageViewer) {
m_imageViewer = new ImageViewerWindow(nullptr);
connect(m_imageViewer, &ImageViewerWindow::closed, this, [this] {
m_imageViewer->deleteLater();
m_imageViewer = nullptr;
});
} else {
m_imageViewer->raise();
m_imageViewer->activateWindow();
}
m_imageViewer->loadFile(filePath);
m_imageViewer->showFullScreen();
}
void MainWindow::onMessageReceived(const char* ch_id, int ch_id_len, uint64_t author, bool initialSync) {
QString cid = QString::fromUtf8(ch_id, ch_id_len);
if (!initialSync) showIncomingMessageNotification(cid, author);
if (!initialSync && m_db && author != m_db->myNodeId()
&& m_db->getUiStateInt(QStringLiteral("ch_%1_sound_on_message").arg(cid), 1))
SoundManager::instance()->play("msg_incoming");
if (m_messageList) {
if (cid == m_currentChannelId) {
m_messageList->refresh();
} else if (m_channelList && !initialSync && m_db && author != m_db->myNodeId()) {
int unread = m_channelList->getChannelUnreadCount(cid) + 1;
DEBUG_DEBUG(DEBUG_CATEGORY_CHAT, "MainWindow::onMessageReceived increment unread ch=%s %d->%d",
qPrintable(cid), unread - 1, unread);
m_channelList->setChannelUnreadCount(cid, unread);
}
}
if (m_db && m_db->getUiStateInt("channel_sort", 0) == 1)
reloadChannels();
if (!initialSync && m_db && author != m_db->myNodeId() && m_db->isOpen()
&& m_db->getUiStateInt(QStringLiteral("ch_%1_auto_play_voice").arg(cid), 0)) {
QTimer::singleShot(100, this, [this, cid]() { autoPlayCheckNewMessage(cid); });
}
}
void MainWindow::onMemberList(const QString& chId, int count, const uint8_t* data, int dataLen) {
if (AvatarManager::instance()) AvatarManager::instance()->onMembers(chId, count, data, dataLen);
m_accountList->onMemberList(chId, count, data, dataLen);
}
void MainWindow::onMemberUpdated(const QString& chId, const uint8_t* data) {
if (AvatarManager::instance()) AvatarManager::instance()->onMembers(chId, 1, data, CHAT_MEMBER_DISPLAY_SIZE, false);
m_accountList->onMemberUpdated(chId, data);
}
void MainWindow::onMemberRemoved(const QString& chId, uint64_t nodeId) {
if (AvatarManager::instance()) AvatarManager::instance()->removeMember(chId, nodeId);
m_accountList->onMemberRemoved(chId, nodeId);
}
void MainWindow::onAutoConnectStatus(uint8_t status, uint16_t /*total_tried*/,
uint16_t /*node_count*/, uint16_t /*connected*/) {
if (m_channelList) {
m_channelList->setAutoConnectActive(status == 0);
}
}
void MainWindow::onChannelPeersOnline(const char* ch_id, int ch_id_len, uint16_t online) {
if (m_channelList) {
QString cid = QString::fromUtf8(ch_id, ch_id_len);
m_channelList->setChannelPeersOnline(cid, (int)online);
}
}
void MainWindow::reloadChannels() {
if (m_db && m_db->isOpen() && !m_currentChannelId.isEmpty() && !m_currentChannelId.startsWith(QStringLiteral("dm:"))) {
bool exists = false;
for (const auto& channel : m_db->getChannels()) if (channel.channelId == m_currentChannelId) { exists = true; break; }
if (!exists) {
DEBUG_INFO(DEBUG_CATEGORY_CHAT, "channel removed from GUI: channel=%s", qPrintable(m_currentChannelId));
for (int i = m_autoPlayQueue.size() - 1; i >= 0; --i) if (m_autoPlayQueue[i].channelId == m_currentChannelId) {
m_autoPlayDownloading.remove(qMakePair(m_autoPlayQueue[i].channelId, m_autoPlayQueue[i].msgId));
m_autoPlayQueue.removeAt(i);
}
if (m_pendingChannelSelect == m_currentChannelId) m_pendingChannelSelect.clear();
m_messageList->loadChannel(QString());
m_accountList->setChannel(QString());
m_currentChannelId.clear();
saveLastChannelId(QString());
if (m_messageNotification) m_messageNotification->dismiss("channel deleted");
}
}
if (m_channelList) {
m_channelList->loadChannels();
if (!m_pendingChannelSelect.isEmpty()) {
m_channelList->selectChannel(m_pendingChannelSelect);
m_accountList->setChannel(m_pendingChannelSelect);
m_messageList->loadChannel(m_pendingChannelSelect);
saveLastChannelId(m_pendingChannelSelect);
m_currentChannelId = m_pendingChannelSelect;
m_pendingChannelSelect.clear();
}
}
}
void MainWindow::onCreateGroupRequested() {
if (!m_node || !m_node->isRunning()) {
QMessageBox::warning(this, QCoreApplication::translate("ChatGui", "Error"), QCoreApplication::translate("ChatGui", "Node is not running."));
return;
}
if (!gui_bridge_is_uasync_ready()) {
QMessageBox::warning(this, QCoreApplication::translate("ChatGui",
"Error"), QCoreApplication::translate("ChatGui",
"uTun thread is not ready yet. Please wait and try again."));
return;
}
QPointer<MainWindow> self(this); QPointer<CreateGroupDialog> dialog = new CreateGroupDialog(this);
auto release = qScopeGuard([&] { delete dialog.data(); });
const int result = dialog->exec();
if (!self || !dialog || result != QDialog::Accepted) return;
QString name = dialog->groupName();
if (name.isEmpty()) return;
auto* arg = static_cast<chat_create_auto_arg*>(u_calloc(1, sizeof(chat_create_auto_arg)));
if (!arg) { DEBUG_ERROR(DEBUG_CATEGORY_CHAT, "create group request allocation failed"); return; }
const QByteArray bytes = name.toUtf8();
if (bytes.size() >= int(sizeof(arg->name))) {
DEBUG_ERROR(DEBUG_CATEGORY_CHAT, "create group name exceeds request capacity: bytes=%d", int(bytes.size())); u_free(arg); return;
}
memcpy(arg->name, bytes.constData(), size_t(bytes.size()) + 1);
if (gui_bridge_post_bound_fn(chat_core_create_channel_auto_trampoline, arg, &arg->inst) < 0) { u_free(arg); return; }
DEBUG_INFO(DEBUG_CATEGORY_CHAT, "MainWindow: create group request posted name=%s", qPrintable(name));
}
void MainWindow::showChannelSettings(const QString& channelId) {
QPointer<ChannelSettingsDialog> dialog = new ChannelSettingsDialog(channelId, m_db, this);
auto release = qScopeGuard([&] { delete dialog.data(); });
dialog->exec();
}
QString MainWindow::autoPlayMediaDir() const {
if (m_db && m_db->isOpen())
return m_db->dbPath() + QStringLiteral("/media");
return QString();
}
/* Для encrypted-вложения MIME и plaintext известны только после проверки/decrypt; неизвестный файл не загружаем как voice. */
QString MainWindow::autoPlayFilePath(const QString& channelId, const MessageRow& message) const {
if (!m_db) return {};
if (message.contentType == QLatin1String(ENCRYPTED_ATTACHMENT_TYPE)) {
const auto snapshot = m_db->getAttachment(channelId, message.messageId);
if (!snapshot.ready || snapshot.error || snapshot.contentType != "audio/opus") return {};
struct attachment_info info = {};
if (attachment_decode((const uint8_t*)snapshot.info.constData(), snapshot.info.size(), &info) || info.kind != ATTACHMENT_VOICE) {
DEBUG_WARN(DEBUG_CATEGORY_MEDIA, "auto-play: invalid voice metadata channel=%s row=%lld", qPrintable(channelId), (long long)message.id);
return {};
}
return snapshot.path;
}
if (message.contentType != "audio/opus") return {};
const QString file = QJsonDocument::fromJson(message.localAttrs).object().value("fp").toString();
return file.isEmpty() ? QString() : autoPlayMediaDir() + "/" + channelId + "/" + file;
}
void MainWindow::autoPlayCheckNewMessage(const QString& channelId, qint64 msgId) {
if (!m_db || !m_db->isOpen() || !m_db->getUiStateInt(QStringLiteral("ch_%1_auto_play_voice").arg(channelId), 0)) return;
MessageRow message;
if (msgId > 0) message = m_db->getMessageById(channelId, msgId);
else {
const auto messages = m_db->getMessages(channelId, 1);
if (messages.isEmpty()) return;
message = messages.first();
}
if (!message.id || message.isOutgoing || message.id <= m_lastAutoPlayId.value(channelId)) return;
const QString path = autoPlayFilePath(channelId, message);
if (message.contentType != "audio/opus" && path.isEmpty()) return;
AutoPlayItem item; item.channelId = channelId; item.msgId = message.id;
if (path.isEmpty() || !QFile::exists(path)) {
if (gui_bridge_download_attachment(channelId.toUtf8().constData(), message.id) < 0) return;
item.downloadStarted = true; m_autoPlayDownloading.insert(qMakePair(channelId, message.id));
}
m_lastAutoPlayId.insert(channelId, message.id); m_autoPlayQueue.enqueue(item);
DEBUG_DEBUG(DEBUG_CATEGORY_MEDIA, "auto-play: queued channel=%s row=%lld download=%d",
qPrintable(channelId), (long long)message.id, item.downloadStarted);
m_autoPlayTimer->start(200); autoPlayTimerTick();
}
void MainWindow::autoPlayTimerTick() {
if (!m_db || !m_db->isOpen()) {
m_autoPlayTimer->stop(); m_autoPlayQueue.clear(); m_autoPlayDownloading.clear(); return;
}
if (VoicePlayer::isPreparing() || VoicePlayer::isPlaying()) return;
while (!m_autoPlayQueue.isEmpty()) {
AutoPlayItem& item = m_autoPlayQueue.head();
const auto key = qMakePair(item.channelId, item.msgId);
const MessageRow message = m_db->getMessageById(item.channelId, item.msgId);
if (!message.id || !m_db->getUiStateInt(QStringLiteral("ch_%1_auto_play_voice").arg(item.channelId), 0)) {
DEBUG_DEBUG(DEBUG_CATEGORY_MEDIA, "auto-play: removed unavailable/disabled channel=%s row=%lld",
qPrintable(item.channelId), (long long)item.msgId);
m_autoPlayDownloading.remove(key); m_autoPlayQueue.dequeue(); continue;
}
const QString path = autoPlayFilePath(item.channelId, message);
if (!path.isEmpty() && QFile::exists(path)) {
DEBUG_DEBUG(DEBUG_CATEGORY_MEDIA, "auto-play: playing channel=%s row=%lld", qPrintable(item.channelId), (long long)item.msgId);
m_autoPlayDownloading.remove(key); m_autoPlayQueue.dequeue();
MessageDelegate::stopAllAutoPlay();
VoicePlayer::playOpusFile(path, this, [path](float duration, uint64_t playbackId) {
MessageDelegate::startAutoPlay(path, duration, playbackId);
});
return;
}
if (!item.downloadStarted) {
if (gui_bridge_download_attachment(item.channelId.toUtf8().constData(), item.msgId) < 0) {
m_autoPlayDownloading.remove(key); m_autoPlayQueue.dequeue(); continue;
}
item.downloadStarted = true; m_autoPlayDownloading.insert(key);
}
return;
}
m_autoPlayTimer->stop();
}
void MainWindow::onAttachmentDownloadedForAutoPlay(const QString& chId, int64_t msgId) {
m_autoPlayDownloading.remove(qMakePair(chId, qint64(msgId)));
autoPlayCheckNewMessage(chId, msgId);
if (!m_autoPlayQueue.isEmpty()) { m_autoPlayTimer->start(200); autoPlayTimerTick(); }
}