chatgui: хранить last_channel_id в БД (ui_state) вместо QSettings
Восстановление канала только если он существует; запись из utun-потока через trampoline, чтобы не трогать sqlite из GUI-потока.
This commit is contained in:
1 parent
eb24736e27
commit
4ca8348a75
1 file changed
+29
-8
@@ -30,7 +30,6 @@ extern "C" {
|
||||
#include <QStyle>
|
||||
#include <QMessageBox>
|
||||
#include <QDebug>
|
||||
#include <QSettings>
|
||||
#include <QFile>
|
||||
#include <QTextStream>
|
||||
#include <QDir>
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user