#include #include "channelsettingsdialog.h" #include "avatars/groupavatarmanager.h" #include "../db/db_manager.h" #include "../transport/gui_bridge.h" #include "../../lib/mem.h" #include "../../lib/debug_config.h" extern "C" { #include "chat/chat_core.h" } #include #include #include #include #include #include #include ChannelSettingsDialog::ChannelSettingsDialog(const QString& channelId, DbManager* db, QWidget* parent) : QDialog(parent) , m_channelId(channelId) , m_db(db) { setWindowTitle(QCoreApplication::translate("ChatGui", "Channel Settings")); setMinimumWidth(340); setModal(true); auto* layout = new QVBoxLayout(this); layout->setContentsMargins(20, 16, 20, 16); layout->setSpacing(12); auto* titleLabel = new QLabel(QCoreApplication::translate("ChatGui", "Settings for channel"), this); titleLabel->setStyleSheet("font-weight: bold; font-size: 14px;"); layout->addWidget(titleLabel); auto* chIdLabel = new QLabel(channelId, this); chIdLabel->setStyleSheet("color: gray; font-size: 11px;"); chIdLabel->setWordWrap(true); layout->addWidget(chIdLabel); if (auto* avatars = GroupAvatarManager::instance()) { auto* preview = new QLabel(this); preview->setFixedSize(64, 64); layout->addWidget(preview); auto* buttons = new QHBoxLayout; auto* upload = new QPushButton(QCoreApplication::translate("ChatGui", "Upload group avatar"), this); auto* remove = new QPushButton(QCoreApplication::translate("ChatGui", "Remove group avatar"), this); upload->setObjectName("uploadGroupAvatar"); remove->setObjectName("removeGroupAvatar"); bool hasKey = false; if (db && db->isOpen()) for (const auto& row : db->getChannels()) if (row.channelId == channelId) hasKey = row.ed25519_privkey.size() == 32; upload->setEnabled(hasKey); remove->setEnabled(hasKey); auto* info = new QLabel(QCoreApplication::translate("ChatGui", "Group avatar: up to 16 KiB. Changes apply immediately and require the group key."), this); info->setWordWrap(true); auto* result = new QLabel(this); result->setWordWrap(true); buttons->addWidget(upload); buttons->addWidget(remove); layout->addLayout(buttons); layout->addWidget(info); layout->addWidget(result); auto refresh = [avatars, preview, channelId] { const QImage image = avatars->image(channelId); if (image.isNull()) { preview->setPixmap(QPixmap()); preview->setText(QCoreApplication::translate("ChatGui", "No avatar")); } else preview->setPixmap(QPixmap::fromImage(image).scaled(64, 64, Qt::KeepAspectRatio, Qt::SmoothTransformation)); }; connect(avatars, &GroupAvatarManager::changed, this, [channelId, refresh](const QString& channel) { if (channel == channelId) refresh(); }); connect(avatars, &GroupAvatarManager::status, this, [channelId, result, refresh](const QString& channel, const QString& message) { if (channel == channelId) { result->setText(message); refresh(); } }); connect(upload, &QPushButton::clicked, this, [this, avatars, channelId] { const QString file = QFileDialog::getOpenFileName(this, QCoreApplication::translate("ChatGui", "Choose avatar"), {}, QCoreApplication::translate("ChatGui", "Images (*.png *.jpg *.jpeg *.webp)")); if (!file.isEmpty()) avatars->upload(channelId, file); }); connect(remove, &QPushButton::clicked, this, [avatars, channelId] { avatars->remove(channelId); }); refresh(); } layout->addSpacing(8); m_autoPlayVoice = new QCheckBox(QCoreApplication::translate("ChatGui", "Auto-play voice messages on receive"), this); m_autoPlayVoice->setToolTip(QCoreApplication::translate("ChatGui", "Voice messages will play automatically when received. Multiple messages queue up and play " "sequentially without interrupting current playback.")); layout->addWidget(m_autoPlayVoice); m_soundOnMessage = new QCheckBox(QCoreApplication::translate("ChatGui", "Play sound on incoming message"), this); m_soundOnMessage->setToolTip(QCoreApplication::translate("ChatGui", "Play a notification sound when a message arrives in this channel.")); layout->addWidget(m_soundOnMessage); layout->addStretch(); auto* btnLayout = new QHBoxLayout(); btnLayout->setSpacing(8); auto* deleteBtn = new QPushButton(QCoreApplication::translate("ChatGui", "Delete channel"), this); deleteBtn->setObjectName("deleteChannel"); deleteBtn->setAutoDefault(false); deleteBtn->setStyleSheet("QPushButton { color: #EF5350; padding: 6px 12px; }"); deleteBtn->setEnabled(m_db && m_db->isOpen() && channelId.toULongLong() != 0 && gui_bridge_is_uasync_ready()); connect(deleteBtn, &QPushButton::clicked, this, &ChannelSettingsDialog::onDelete); btnLayout->addWidget(deleteBtn); btnLayout->addStretch(); auto* cancelBtn = new QPushButton(QCoreApplication::translate("ChatGui", "Cancel"), this); cancelBtn->setMinimumWidth(90); cancelBtn->setStyleSheet( "QPushButton { border: 1px solid palette(mid); border-radius: 4px;" " padding: 6px 16px; font-size: 13px; background: palette(button); }" "QPushButton:hover { background: palette(light); }"); connect(cancelBtn, &QPushButton::clicked, this, &QDialog::reject); btnLayout->addWidget(cancelBtn); auto* saveBtn = new QPushButton(QCoreApplication::translate("ChatGui", "Save"), this); saveBtn->setMinimumWidth(90); saveBtn->setDefault(true); saveBtn->setStyleSheet( "QPushButton { border: none; border-radius: 4px;" " padding: 6px 16px; font-size: 13px; font-weight: bold;" " background: #3390EC; color: white; }" "QPushButton:hover { background: #2B7ED4; }"); connect(saveBtn, &QPushButton::clicked, this, &ChannelSettingsDialog::onSave); btnLayout->addWidget(saveBtn); layout->addLayout(btnLayout); if (m_db && m_db->isOpen()) { QString autoPlayKey = QStringLiteral("ch_%1_auto_play_voice").arg(channelId); m_autoPlayVoice->setChecked(m_db->getUiStateInt(autoPlayKey, 0) != 0); QString soundKey = QStringLiteral("ch_%1_sound_on_message").arg(channelId); m_soundOnMessage->setChecked(m_db->getUiStateInt(soundKey, 1) != 0); } } /* Как в Android: удаляются только локальные данные; ядро уведомит GUI по завершении. */ void ChannelSettingsDialog::onDelete() { QString name = m_channelId; if (m_db && m_db->isOpen()) for (const auto& channel : m_db->getChannels()) if (channel.channelId == m_channelId) { name = channel.name; break; } QMessageBox confirmation(QMessageBox::Warning, QCoreApplication::translate("ChatGui", "Delete channel"), QCoreApplication::translate("ChatGui", "Delete “%1” and all its local messages and files? Other members keep the channel.") .arg(name), QMessageBox::NoButton, this); confirmation.setTextFormat(Qt::PlainText); auto* remove = confirmation.addButton(QCoreApplication::translate("ChatGui", "Delete"), QMessageBox::DestructiveRole); remove->setObjectName("confirmDeleteChannel"); auto* cancel = confirmation.addButton(QMessageBox::Cancel); confirmation.setDefaultButton(cancel); confirmation.setEscapeButton(cancel); confirmation.exec(); if (confirmation.clickedButton() != remove) return; auto* inst = gui_bridge_get_inst(); if (!inst || !gui_bridge_is_uasync_ready()) { DEBUG_ERROR(DEBUG_CATEGORY_CHAT, "channel delete: core unavailable channel=%s", qPrintable(m_channelId)); QMessageBox::warning(this, confirmation.windowTitle(), QCoreApplication::translate("ChatGui", "The core is not running.")); return; } const QByteArray channel = m_channelId.toUtf8(); auto* request = static_cast(u_calloc(1, sizeof(chat_delete_channel_req))); if (!request) { DEBUG_ERROR(DEBUG_CATEGORY_CHAT, "channel delete: request allocation failed channel=%s", channel.constData()); QMessageBox::warning(this, confirmation.windowTitle(), QCoreApplication::translate("ChatGui", "Failed to delete channel.")); return; } request->inst = inst; snprintf(request->ch_id, sizeof(request->ch_id), "%s", channel.constData()); DEBUG_INFO(DEBUG_CATEGORY_CHAT, "channel delete confirmed: channel=%s scope=local", channel.constData()); gui_bridge_post_uasync_fn(chat_core_delete_channel_trampoline, request); accept(); } void ChannelSettingsDialog::onSave() { auto saveKey = [&](const char* suffix, int val) { QString key = QStringLiteral("ch_%1_%2").arg(m_channelId, QString::fromUtf8(suffix)); QByteArray keyBA = key.toUtf8(); QByteArray valBA = QByteArray::number(val); size_t klen = (size_t)keyBA.size(), vlen = (size_t)valBA.size(); size_t total = klen + 1 + vlen + 1; if (total <= 256) { struct save_ui_state_arg* arg = (struct save_ui_state_arg*)u_malloc(sizeof(struct save_ui_state_arg)); if (!arg) return; arg->inst = gui_bridge_get_inst(); memcpy(arg->data, keyBA.constData(), klen + 1); memcpy(arg->data + klen + 1, valBA.constData(), vlen + 1); gui_bridge_post_uasync_fn(chat_core_save_ui_state_trampoline, arg); } }; saveKey("auto_play_voice", m_autoPlayVoice->isChecked() ? 1 : 0); /* sound_on_message: сохранить + разослать пирам (троттлинг db_sync на сервере) */ { int on = m_soundOnMessage->isChecked() ? 1 : 0; QByteArray chIdBA = m_channelId.toUtf8(); struct chat_sound_arg* arg = (struct chat_sound_arg*)u_malloc(sizeof(struct chat_sound_arg)); if (arg) { memset(arg, 0, sizeof(*arg)); arg->inst = gui_bridge_get_inst(); strncpy(arg->ch_id, chIdBA.constData(), sizeof(arg->ch_id) - 1); arg->on = on; gui_bridge_post_uasync_fn(chat_core_set_sound_on_message_trampoline, arg); } } accept(); }