From 4ca8348a7507187698bc59220731b94e397e5f36 Mon Sep 17 00:00:00 2001 From: evgeny Date: Sun, 16 Aug 2026 22:34:14 +0300 Subject: [PATCH] =?UTF-8?q?chatgui:=20=D1=85=D1=80=D0=B0=D0=BD=D0=B8=D1=82?= =?UTF-8?q?=D1=8C=20last=5Fchannel=5Fid=20=D0=B2=20=D0=91=D0=94=20(ui=5Fst?= =?UTF-8?q?ate)=20=D0=B2=D0=BC=D0=B5=D1=81=D1=82=D0=BE=20QSettings?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Восстановление канала только если он существует; запись из utun-потока через trampoline, чтобы не трогать sqlite из GUI-потока. --- tools/chatgui/src/mainwindow.cpp | 37 +++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/tools/chatgui/src/mainwindow.cpp b/tools/chatgui/src/mainwindow.cpp index 6c607479..b957f72c 100644 --- a/tools/chatgui/src/mainwindow.cpp +++ b/tools/chatgui/src/mainwindow.cpp @@ -30,7 +30,6 @@ extern "C" { #include #include #include -#include #include #include #include @@ -48,6 +47,19 @@ void chat_core_save_ui_state_trampoline(void* arg); static MainWindow* s_mainWindow = nullptr; +/* Сохранение last_channel_id в БД (ui_state), как все остальные UI-настройки. + Пишется из utun-потока через trampoline, чтобы не трогать sqlite из GUI-потока. */ +static void saveLastChannelId(const QString& channelId) { + const char key[] = "last_channel_id"; + QByteArray val = channelId.toUtf8(); + size_t klen = sizeof(key) - 1, vlen = (size_t)val.size(), total = klen + 1 + vlen + 1; + if (total > 256) return; + void* arg = u_malloc(total); + memcpy(arg, key, klen + 1); + memcpy((char*)arg + klen + 1, val.constData(), vlen + 1); + gui_bridge_post_uasync_fn(chat_core_save_ui_state_trampoline, arg); +} + static void onMsgReceivedCallback(const char* ch_id, int ch_id_len, uint64_t author) { if (s_mainWindow) s_mainWindow->onMessageReceived(ch_id, ch_id_len, author); @@ -293,12 +305,21 @@ void MainWindow::setupBridgeCallbacks() { if (!s_mainWindow || !s_mainWindow->m_db) return; s_mainWindow->m_db->setDb(chat_core_get_db()); s_mainWindow->m_channelList->loadChannels(); - QString lastCid = QSettings().value("last_channel_id").toString(); + QString lastCid = s_mainWindow->m_db->getUiState("last_channel_id"); if (!lastCid.isEmpty()) { - s_mainWindow->m_channelList->selectChannel(lastCid); - s_mainWindow->m_accountList->setChannel(lastCid); - s_mainWindow->m_currentChannelId = lastCid; - s_mainWindow->m_messageList->loadChannel(lastCid); + /* восстанавливаем канал только если он реально существует в БД — + иначе не дёргаем подключение к несуществующей группе */ + 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) { @@ -318,7 +339,7 @@ void MainWindow::setupBridgeCallbacks() { void MainWindow::setupConnects() { connect(m_channelList, &ChannelList::channelSelected, this, [this](const QString& channelId) { - QSettings().setValue("last_channel_id", channelId); + saveLastChannelId(channelId); m_currentChannelId = channelId; m_messageList->loadChannel(channelId); m_accountList->setChannel(channelId); @@ -518,7 +539,7 @@ void MainWindow::reloadChannels() { m_channelList->selectChannel(m_pendingChannelSelect); m_accountList->setChannel(m_pendingChannelSelect); m_messageList->loadChannel(m_pendingChannelSelect); - QSettings().setValue("last_channel_id", m_pendingChannelSelect); + saveLastChannelId(m_pendingChannelSelect); m_currentChannelId = m_pendingChannelSelect; m_pendingChannelSelect.clear(); }