chatgui: preserve active channel selection on reload, members sync, profile settings, member list model
This commit is contained in:
1 parent
ce6de7307a
commit
618cfe9bc5
28 files changed
+1895
-91
No files matched your search
@@ -1,39 +1,20 @@
|
||||
#include "accountlist.h"
|
||||
#include "memberlistmodel.h"
|
||||
#include "../db/db_manager.h"
|
||||
#include <QVBoxLayout>
|
||||
#include <QLabel>
|
||||
#include <QPainter>
|
||||
#include <QPixmap>
|
||||
|
||||
static QPixmap makeAvatar(const QColor &color, const QString &letter) {
|
||||
QPixmap pix(24, 24);
|
||||
pix.fill(Qt::transparent);
|
||||
QPainter p(&pix);
|
||||
p.setRenderHint(QPainter::Antialiasing);
|
||||
p.setBrush(color);
|
||||
p.setPen(Qt::NoPen);
|
||||
p.drawEllipse(0, 0, 24, 24);
|
||||
p.setPen(Qt::white);
|
||||
QFont f = p.font();
|
||||
f.setBold(true);
|
||||
f.setPointSize(10);
|
||||
p.setFont(f);
|
||||
p.drawText(QRect(0, 0, 24, 24), Qt::AlignCenter, letter);
|
||||
p.end();
|
||||
return pix;
|
||||
}
|
||||
|
||||
AccountList::AccountList(DbManager* db, QWidget *parent)
|
||||
: QWidget(parent)
|
||||
, m_listView(new QListView(this))
|
||||
, m_model(new QStandardItemModel(this))
|
||||
, m_model(new MemberListModel(db, this))
|
||||
, m_db(db)
|
||||
{
|
||||
auto *layout = new QVBoxLayout(this);
|
||||
layout->setContentsMargins(0, 0, 0, 0);
|
||||
layout->setSpacing(0);
|
||||
|
||||
auto *header = new QLabel("Accounts", this);
|
||||
auto *header = new QLabel("Members", this);
|
||||
header->setAlignment(Qt::AlignCenter);
|
||||
header->setStyleSheet("font-weight: bold; padding: 8px; background: palette(window);");
|
||||
layout->addWidget(header);
|
||||
@@ -47,15 +28,5 @@ AccountList::AccountList(DbManager* db, QWidget *parent)
|
||||
layout->addWidget(m_listView);
|
||||
}
|
||||
|
||||
void AccountList::loadAccounts() {
|
||||
m_model->clear();
|
||||
if (!m_db || !m_db->isOpen()) return;
|
||||
|
||||
auto accounts = m_db->getAccounts(true);
|
||||
for (const auto& a : accounts) {
|
||||
auto *item = new QStandardItem();
|
||||
item->setText(a.displayName);
|
||||
item->setIcon(QIcon(makeAvatar(QColor(a.avatarColor), a.avatarLetter)));
|
||||
m_model->appendRow(item);
|
||||
}
|
||||
}
|
||||
void AccountList::setChannel(const QString& channelId) { m_model->setChannel(channelId); }
|
||||
void AccountList::refresh() { m_model->refresh(); }
|
||||
@@ -2,8 +2,8 @@
|
||||
|
||||
#include <QWidget>
|
||||
#include <QListView>
|
||||
#include <QStandardItemModel>
|
||||
|
||||
class MemberListModel;
|
||||
class DbManager;
|
||||
|
||||
class AccountList : public QWidget {
|
||||
@@ -11,10 +11,11 @@ class AccountList : public QWidget {
|
||||
public:
|
||||
explicit AccountList(DbManager* db = nullptr, QWidget *parent = nullptr);
|
||||
|
||||
void loadAccounts();
|
||||
void setChannel(const QString& channelId);
|
||||
void refresh();
|
||||
|
||||
private:
|
||||
QListView *m_listView;
|
||||
QStandardItemModel *m_model;
|
||||
DbManager *m_db;
|
||||
QListView *m_listView;
|
||||
MemberListModel *m_model;
|
||||
DbManager *m_db;
|
||||
};
|
||||
@@ -99,6 +99,14 @@ ChannelList::ChannelList(DbManager* db, QWidget *parent)
|
||||
}
|
||||
|
||||
void ChannelList::loadChannels() {
|
||||
QModelIndex curIdx = m_listView->currentIndex();
|
||||
QString selectedCid;
|
||||
int selectedRow = -1;
|
||||
if (curIdx.isValid()) {
|
||||
selectedCid = curIdx.data(ChannelChannelIdRole).toString();
|
||||
selectedRow = curIdx.row();
|
||||
}
|
||||
|
||||
m_model->clear();
|
||||
if (!m_db || !m_db->isOpen()) return;
|
||||
|
||||
@@ -121,8 +129,15 @@ void ChannelList::loadChannels() {
|
||||
ci++;
|
||||
}
|
||||
|
||||
if (m_model->rowCount() > 0)
|
||||
m_listView->setCurrentIndex(m_model->index(0, 0));
|
||||
if (!selectedCid.isEmpty())
|
||||
selectChannel(selectedCid);
|
||||
if (!m_listView->currentIndex().isValid()) {
|
||||
int fallbackRow = (selectedRow >= 0) ? selectedRow : 0;
|
||||
if (fallbackRow >= m_model->rowCount())
|
||||
fallbackRow = m_model->rowCount() - 1;
|
||||
if (fallbackRow >= 0)
|
||||
m_listView->setCurrentIndex(m_model->index(fallbackRow, 0));
|
||||
}
|
||||
}
|
||||
|
||||
void ChannelList::selectChannel(const QString& channelId) {
|
||||
|
||||
@@ -40,6 +40,12 @@ static void onChannelUpdatedCallback(const char* ch_id, int ch_id_len) {
|
||||
s_mainWindow->reloadChannels();
|
||||
}
|
||||
|
||||
static void onMembersChangedCallback(const char* ch_id, int ch_id_len) {
|
||||
Q_UNUSED(ch_id_len);
|
||||
if (s_mainWindow)
|
||||
s_mainWindow->onMembersChanged(ch_id);
|
||||
}
|
||||
|
||||
MainWindow::MainWindow(QWidget *parent, DbManager* db, const QString& cfgPath,
|
||||
const QString& dbPath, const QString& debugFile,
|
||||
const QString& debugLevel, const QString& debugCategories)
|
||||
@@ -65,6 +71,11 @@ MainWindow::MainWindow(QWidget *parent, DbManager* db, const QString& cfgPath,
|
||||
s_mainWindow = this;
|
||||
gui_bridge_set_msg_received_cb(onMsgReceivedCallback);
|
||||
gui_bridge_set_channel_updated_cb(onChannelUpdatedCallback);
|
||||
gui_bridge_set_members_changed_cb(onMembersChangedCallback);
|
||||
gui_bridge_set_my_node_id_cb([](uint64_t nid) {
|
||||
qDebug("MainWindow: MY_NODE_ID setMyNodeId(%016llx)", (unsigned long long)nid);
|
||||
if (s_mainWindow && s_mainWindow->m_db) s_mainWindow->m_db->setMyNodeId((quint64)nid);
|
||||
});
|
||||
|
||||
setupNode();
|
||||
setupMessaging();
|
||||
@@ -73,6 +84,7 @@ MainWindow::MainWindow(QWidget *parent, DbManager* db, const QString& cfgPath,
|
||||
this, [this](const QString& channelId) {
|
||||
m_db->setUiState("last_channel_id", channelId);
|
||||
m_messageList->loadChannel(channelId);
|
||||
m_accountList->setChannel(channelId);
|
||||
});
|
||||
|
||||
connect(m_channelList, &ChannelList::inviteRequested,
|
||||
@@ -85,11 +97,12 @@ MainWindow::MainWindow(QWidget *parent, DbManager* db, const QString& cfgPath,
|
||||
this, &MainWindow::showSettings);
|
||||
|
||||
m_channelList->loadChannels();
|
||||
m_accountList->loadAccounts();
|
||||
|
||||
QString lastCid = m_db->getUiState("last_channel_id");
|
||||
if (!lastCid.isEmpty())
|
||||
if (!lastCid.isEmpty()) {
|
||||
m_channelList->selectChannel(lastCid);
|
||||
m_accountList->setChannel(lastCid);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -239,9 +252,23 @@ void MainWindow::onMessageReceived(const char* ch_id) {
|
||||
m_messageList->refresh();
|
||||
}
|
||||
|
||||
void MainWindow::onMembersChanged(const char* ch_id) {
|
||||
QString cur = m_db->getUiState("last_channel_id");
|
||||
if (cur == QString::fromUtf8(ch_id))
|
||||
m_accountList->refresh();
|
||||
}
|
||||
|
||||
void MainWindow::reloadChannels() {
|
||||
if (m_channelList)
|
||||
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);
|
||||
m_db->setUiState("last_channel_id", m_pendingChannelSelect);
|
||||
m_pendingChannelSelect.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void MainWindow::onCreateGroupRequested() {
|
||||
@@ -344,6 +371,7 @@ void MainWindow::onCreateGroupRequested() {
|
||||
memcpy(req->signature, sig, 64);
|
||||
|
||||
gui_bridge_post_uasync_fn(chat_core_create_channel_trampoline, req);
|
||||
m_pendingChannelSelect = channelId;
|
||||
|
||||
DEBUG_INFO(DEBUG_CATEGORY_DEBUG, "MainWindow: create group request posted ch=%s name=%s",
|
||||
qPrintable(channelId), qPrintable(name));
|
||||
|
||||
@@ -32,6 +32,7 @@ private slots:
|
||||
|
||||
public:
|
||||
void onMessageReceived(const char* ch_id);
|
||||
void onMembersChanged(const char* ch_id);
|
||||
void reloadChannels();
|
||||
|
||||
private:
|
||||
@@ -52,4 +53,5 @@ private:
|
||||
QString m_cfgDebugFile;
|
||||
QString m_cfgDebugLevel;
|
||||
QString m_cfgDebugCategories;
|
||||
QString m_pendingChannelSelect;
|
||||
};
|
||||
@@ -0,0 +1,112 @@
|
||||
#include "memberlistmodel.h"
|
||||
#include "../db/db_manager.h"
|
||||
#include <QPainter>
|
||||
#include <QFont>
|
||||
#include <QTimer>
|
||||
|
||||
MemberListModel::MemberListModel(DbManager* db, QObject* parent)
|
||||
: QAbstractListModel(parent), m_db(db) {}
|
||||
|
||||
int MemberListModel::rowCount(const QModelIndex& parent) const {
|
||||
(void)parent;
|
||||
return m_totalCount;
|
||||
}
|
||||
|
||||
QVariant MemberListModel::data(const QModelIndex& index, int role) const {
|
||||
if (!index.isValid() || index.row() < 0 || index.row() >= m_totalCount)
|
||||
return {};
|
||||
|
||||
auto it = m_cache.find(index.row());
|
||||
if (it != m_cache.end()) {
|
||||
if (role == Qt::DisplayRole) return it->name;
|
||||
if (role == Qt::DecorationRole) return QVariant::fromValue(QIcon(it->avatar));
|
||||
if (role == Qt::ToolTipRole)
|
||||
return QString(it->online ? "online" : "offline") + " | 0x" +
|
||||
QString("%1").arg(it->nodeId, 16, 16, QChar('0'));
|
||||
return {};
|
||||
}
|
||||
|
||||
if (role == Qt::DisplayRole) {
|
||||
scheduleBatch(index.row());
|
||||
return QStringLiteral("\u2026");
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
void MemberListModel::setChannel(const QString& channelId) {
|
||||
beginResetModel();
|
||||
m_channelId = channelId;
|
||||
m_totalCount = m_db ? m_db->getChannelMemberCount(channelId) : 0;
|
||||
m_cache.clear();
|
||||
m_pendingFrom = -1;
|
||||
endResetModel();
|
||||
if (m_totalCount > 0) scheduleBatch(0);
|
||||
}
|
||||
|
||||
void MemberListModel::refresh() { if (!m_channelId.isEmpty()) setChannel(m_channelId); }
|
||||
|
||||
void MemberListModel::scheduleBatch(int centerRow) const {
|
||||
if (m_pendingFrom >= 0) return;
|
||||
int from = centerRow - BATCH / 2;
|
||||
if (from < 0) from = 0;
|
||||
int maxFrom = m_totalCount - BATCH;
|
||||
if (from > maxFrom) from = maxFrom > 0 ? maxFrom : 0;
|
||||
const_cast<MemberListModel*>(this)->m_pendingFrom = from;
|
||||
const_cast<MemberListModel*>(this)->m_pendingCount = BATCH;
|
||||
QTimer::singleShot(0, const_cast<MemberListModel*>(this), &MemberListModel::loadPendingBatch);
|
||||
}
|
||||
|
||||
void MemberListModel::loadPendingBatch() {
|
||||
if (m_pendingFrom < 0) return;
|
||||
int from = m_pendingFrom, count = m_pendingCount;
|
||||
m_pendingFrom = -1;
|
||||
loadBatch(from, count);
|
||||
}
|
||||
|
||||
void MemberListModel::loadBatch(int from, int count) {
|
||||
if (!m_db || m_channelId.isEmpty()) return;
|
||||
int maxFrom = m_totalCount - count;
|
||||
if (from > maxFrom) from = maxFrom > 0 ? maxFrom : 0;
|
||||
|
||||
auto members = m_db->getChannelMembers(m_channelId, from, count);
|
||||
for (int i = 0; i < members.size(); i++) {
|
||||
int row = from + i;
|
||||
MemberListItem& item = m_cache[row];
|
||||
item.nodeId = members[i].nodeId;
|
||||
item.online = members[i].online;
|
||||
QString shortId = QString("%1").arg(members[i].nodeId, 8, 16, QChar('0')).right(4).toUpper();
|
||||
item.name = members[i].name.isEmpty() ? shortId : members[i].name;
|
||||
QColor c = members[i].online ? QColor("#4CAF50") : QColor("#9E9E9E");
|
||||
item.avatar = makeAvatar(c, item.name[0]);
|
||||
}
|
||||
evictDistant(from + BATCH / 2);
|
||||
|
||||
int last = from + (int)members.size() - 1;
|
||||
if (last >= from) emit dataChanged(index(from), index(last));
|
||||
}
|
||||
|
||||
void MemberListModel::evictDistant(int centerRow) {
|
||||
if (m_cache.size() <= MAX_CACHE) return;
|
||||
int half = MAX_CACHE / 2;
|
||||
QList<int> toRemove;
|
||||
for (auto it = m_cache.begin(); it != m_cache.end(); ++it)
|
||||
if (qAbs(it.key() - centerRow) > half)
|
||||
toRemove.append(it.key());
|
||||
for (int k : toRemove) m_cache.remove(k);
|
||||
}
|
||||
|
||||
QPixmap MemberListModel::makeAvatar(QColor color, QChar letter) const {
|
||||
QPixmap pm(24, 24);
|
||||
pm.fill(Qt::transparent);
|
||||
QPainter p(&pm);
|
||||
p.setRenderHint(QPainter::Antialiasing);
|
||||
p.setBrush(color);
|
||||
p.setPen(Qt::NoPen);
|
||||
p.drawEllipse(2, 2, 20, 20);
|
||||
p.setPen(Qt::white);
|
||||
QFont f; f.setBold(true); f.setPixelSize(12);
|
||||
p.setFont(f);
|
||||
p.drawText(QRect(0, 0, 24, 24), Qt::AlignCenter, QString(letter));
|
||||
p.end();
|
||||
return pm;
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
#ifndef MEMBERLISTMODEL_H
|
||||
#define MEMBERLISTMODEL_H
|
||||
|
||||
#include <QAbstractListModel>
|
||||
#include <QColor>
|
||||
#include <QMap>
|
||||
#include <QString>
|
||||
#include <QIcon>
|
||||
#include <QPixmap>
|
||||
|
||||
class DbManager;
|
||||
|
||||
struct MemberListItem {
|
||||
quint64 nodeId = 0;
|
||||
bool online = false;
|
||||
QPixmap avatar;
|
||||
QString name;
|
||||
};
|
||||
|
||||
class MemberListModel : public QAbstractListModel {
|
||||
Q_OBJECT
|
||||
public:
|
||||
static const int BATCH = 40;
|
||||
static const int MAX_CACHE = 120;
|
||||
|
||||
explicit MemberListModel(DbManager* db, QObject* parent = nullptr);
|
||||
|
||||
int rowCount(const QModelIndex& parent = QModelIndex()) const override;
|
||||
QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
|
||||
|
||||
void setChannel(const QString& channelId);
|
||||
void refresh();
|
||||
|
||||
private slots:
|
||||
void loadPendingBatch();
|
||||
|
||||
private:
|
||||
void scheduleBatch(int centerRow) const;
|
||||
void loadBatch(int from, int count);
|
||||
void evictDistant(int centerRow);
|
||||
QPixmap makeAvatar(QColor color, QChar letter) const;
|
||||
|
||||
DbManager* m_db;
|
||||
QString m_channelId;
|
||||
int m_totalCount = 0;
|
||||
mutable QMap<int, MemberListItem> m_cache;
|
||||
mutable int m_pendingFrom = -1;
|
||||
mutable int m_pendingCount = 0;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -125,21 +125,6 @@ MessageList::MessageList(DbManager* db, QWidget *parent)
|
||||
QByteArray textData = text.toUtf8();
|
||||
auto ts = QDateTime::currentMSecsSinceEpoch();
|
||||
|
||||
uint64_t datahash = 0;
|
||||
QByteArray chainHash;
|
||||
bool ok = m_db->insertMessage(m_currentChannelId, m_db->myNodeId(),
|
||||
"text/plain", textData, ts, true,
|
||||
&datahash, &chainHash);
|
||||
if (!ok) {
|
||||
qWarning("MessageList: insertMessage FAILED ch=%s ts=%lld",
|
||||
qPrintable(m_currentChannelId), ts);
|
||||
return;
|
||||
}
|
||||
qDebug("MessageList: msg stored ch=%s ts=%lld dh=0x%016llx",
|
||||
qPrintable(m_currentChannelId), ts, (unsigned long long)datahash);
|
||||
|
||||
loadChannel(m_currentChannelId);
|
||||
|
||||
struct chat_msg_submit* req = (struct chat_msg_submit*)
|
||||
u_malloc(sizeof(struct chat_msg_submit) + textData.size());
|
||||
memset(req, 0, sizeof(*req));
|
||||
@@ -150,7 +135,7 @@ MessageList::MessageList(DbManager* db, QWidget *parent)
|
||||
req->data_len = (uint32_t)textData.size();
|
||||
memcpy(req->data, textData.constData(), textData.size());
|
||||
req->timestamp = (uint64_t)ts;
|
||||
gui_bridge_post_uasync_fn(chat_core_push_trampoline, req);
|
||||
gui_bridge_post_uasync_fn(chat_core_submit_trampoline, req);
|
||||
});
|
||||
|
||||
connect(AnimTimer::instance(), &AnimTimer::ticked, this, [this]() {
|
||||
@@ -192,8 +177,7 @@ void MessageList::loadChannel(const QString& channelId) {
|
||||
|
||||
for (const auto& m : msgs) {
|
||||
AccountRow acc = m_db->getAccount(m.authorNodeId);
|
||||
QString author = acc.displayName.isEmpty()
|
||||
? QString("node_%1").arg(m.authorNodeId) : acc.displayName;
|
||||
QString author = m_db->getDisplayName(m.authorNodeId);
|
||||
QString letter = acc.avatarLetter.isEmpty() ? author.mid(0, 1) : acc.avatarLetter;
|
||||
QColor color(acc.avatarColor);
|
||||
|
||||
@@ -226,8 +210,7 @@ void MessageList::addMessage(const QString& channelId, quint64 authorNodeId,
|
||||
if (channelId != m_currentChannelId) return;
|
||||
|
||||
AccountRow acc = m_db ? m_db->getAccount(authorNodeId) : AccountRow();
|
||||
QString author = acc.displayName.isEmpty()
|
||||
? QString("node_%1").arg(authorNodeId) : acc.displayName;
|
||||
QString author = m_db ? m_db->getDisplayName(authorNodeId) : QString("node_%1").arg(authorNodeId);
|
||||
QString letter = acc.avatarLetter.isEmpty() ? author.mid(0, 1) : acc.avatarLetter;
|
||||
QColor color(acc.avatarColor);
|
||||
auto avatar = makeAvatar(color, letter);
|
||||
|
||||
@@ -1,10 +1,19 @@
|
||||
// settingsdialog.cpp — Settings dialog with category list
|
||||
#include "settingsdialog.h"
|
||||
#include "networksettingspage.h"
|
||||
#include "../transport/gui_bridge.h"
|
||||
#include "../../lib/mem.h"
|
||||
extern "C" {
|
||||
#include "../transport/chat_core.h"
|
||||
}
|
||||
#include <QVBoxLayout>
|
||||
#include <QHBoxLayout>
|
||||
#include <QPushButton>
|
||||
#include <QLabel>
|
||||
#include <QFormLayout>
|
||||
#include <QLineEdit>
|
||||
#include <QFile>
|
||||
#include <QTextStream>
|
||||
|
||||
SettingsDialog::SettingsDialog(const QString& configPath, QWidget* parent)
|
||||
: QDialog(parent)
|
||||
@@ -26,6 +35,7 @@ SettingsDialog::SettingsDialog(const QString& configPath, QWidget* parent)
|
||||
" background: palette(window); font-size: 13px; }"
|
||||
"QListWidget::item { padding: 10px 14px; }"
|
||||
"QListWidget::item:selected { background: #3390EC; color: white; }");
|
||||
m_categoryList->addItem("Profile");
|
||||
m_categoryList->addItem("Network");
|
||||
mainLayout->addWidget(m_categoryList);
|
||||
|
||||
@@ -36,6 +46,22 @@ SettingsDialog::SettingsDialog(const QString& configPath, QWidget* parent)
|
||||
rightLayout->setSpacing(0);
|
||||
|
||||
m_pages = new QStackedWidget(rightPanel);
|
||||
|
||||
// Profile page
|
||||
auto* profilePage = new QWidget(m_pages);
|
||||
auto* profLayout = new QFormLayout(profilePage);
|
||||
profLayout->setContentsMargins(0, 12, 0, 0);
|
||||
auto* nameLabel = new QLabel("Display name:", profilePage);
|
||||
m_nicknameEdit = new QLineEdit(profilePage);
|
||||
m_nicknameEdit->setPlaceholderText("Your name (shown to others)");
|
||||
m_nicknameEdit->setMaxLength(32);
|
||||
m_nicknameEdit->setStyleSheet(
|
||||
"QLineEdit { border: 1px solid palette(mid); border-radius: 4px;"
|
||||
" padding: 6px 10px; font-size: 13px; background: palette(base); }");
|
||||
profLayout->addRow(nameLabel, m_nicknameEdit);
|
||||
m_pages->addWidget(profilePage);
|
||||
|
||||
// Network page
|
||||
m_networkPage = new NetworkSettingsPage(m_pages);
|
||||
m_pages->addWidget(m_networkPage);
|
||||
rightLayout->addWidget(m_pages, 1);
|
||||
@@ -71,15 +97,68 @@ SettingsDialog::SettingsDialog(const QString& configPath, QWidget* parent)
|
||||
connect(m_categoryList, &QListWidget::currentRowChanged,
|
||||
this, &SettingsDialog::onCategoryChanged);
|
||||
|
||||
// Load current values
|
||||
loadProfileFromConfig(m_configPath);
|
||||
m_networkPage->loadFromConfig(m_configPath);
|
||||
m_categoryList->setCurrentRow(0);
|
||||
}
|
||||
|
||||
void SettingsDialog::loadProfileFromConfig(const QString& path) {
|
||||
QFile f(path);
|
||||
if (!f.open(QIODevice::ReadOnly | QIODevice::Text)) return;
|
||||
QTextStream in(&f);
|
||||
while (!in.atEnd()) {
|
||||
QString line = in.readLine().trimmed();
|
||||
if (line.startsWith("my_node_name=")) {
|
||||
QString nm = line.mid(QString("my_node_name=").size()).trimmed();
|
||||
m_nicknameEdit->setText(nm);
|
||||
m_oldNickname = nm;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void SettingsDialog::onCategoryChanged(int row) {
|
||||
m_pages->setCurrentIndex(row);
|
||||
}
|
||||
|
||||
void SettingsDialog::onSave() {
|
||||
QString newName = m_nicknameEdit->text().trimmed();
|
||||
|
||||
/* update name in uTun if changed */
|
||||
if (newName != m_oldNickname) {
|
||||
char* name_copy = u_strdup(newName.toUtf8().constData());
|
||||
if (name_copy) gui_bridge_post_uasync_fn(chat_core_update_my_name_trampoline, name_copy);
|
||||
m_oldNickname = newName;
|
||||
}
|
||||
|
||||
// Save nickname to config
|
||||
QFile f(m_configPath);
|
||||
if (!f.open(QIODevice::ReadOnly | QIODevice::Text)) { accept(); return; }
|
||||
QString content = f.readAll();
|
||||
f.close();
|
||||
|
||||
QTextStream ts(&content);
|
||||
QStringList lines;
|
||||
while (!ts.atEnd()) lines << ts.readLine();
|
||||
|
||||
bool found = false;
|
||||
for (int i = 0; i < lines.size(); i++) {
|
||||
if (lines[i].trimmed().startsWith("my_node_name=")) {
|
||||
lines[i] = "my_node_name=" + newName;
|
||||
found = true;
|
||||
} else if (!found && lines[i].trimmed().startsWith("[gui]")) {
|
||||
// insert before [gui] if not found yet
|
||||
lines.insert(i, "my_node_name=" + newName);
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
if (!found) lines << "my_node_name=" + newName;
|
||||
|
||||
f.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text);
|
||||
f.write(lines.join('\n').toUtf8());
|
||||
f.close();
|
||||
|
||||
m_networkPage->saveToConfig(m_configPath);
|
||||
accept();
|
||||
}
|
||||
@@ -5,6 +5,7 @@
|
||||
#include <QListWidget>
|
||||
#include <QStackedWidget>
|
||||
#include <QPushButton>
|
||||
#include <QLineEdit>
|
||||
#include <QString>
|
||||
|
||||
class NetworkSettingsPage;
|
||||
@@ -19,9 +20,13 @@ private slots:
|
||||
void onSave();
|
||||
|
||||
private:
|
||||
void loadProfileFromConfig(const QString& path);
|
||||
|
||||
QString m_configPath;
|
||||
QListWidget* m_categoryList;
|
||||
QStackedWidget* m_pages;
|
||||
QPushButton* m_saveBtn;
|
||||
QLineEdit* m_nicknameEdit;
|
||||
QString m_oldNickname;
|
||||
NetworkSettingsPage* m_networkPage;
|
||||
};
|
||||
Reference in new issue
Block a user