94 lines
3.3 KiB
C++
94 lines
3.3 KiB
C++
#include <QCoreApplication>
|
|
#include "renamedialog.h"
|
|
#include "../db/db_manager.h"
|
|
#include <QVBoxLayout>
|
|
#include <QHBoxLayout>
|
|
#include <QPushButton>
|
|
#include <QLabel>
|
|
|
|
RenameDialog::RenameDialog(DbManager* db, const QString& channelId,
|
|
quint64 nodeId, const QString& displayName,
|
|
QWidget* parent)
|
|
: QDialog(parent)
|
|
, m_db(db)
|
|
, m_channelId(channelId)
|
|
, m_nodeId(nodeId)
|
|
{
|
|
setWindowTitle(QCoreApplication::translate("ChatGui", "Rename"));
|
|
setMinimumWidth(300);
|
|
setModal(true);
|
|
|
|
QString curNick;
|
|
if (m_db->m_db) {
|
|
QString tbl = m_db->peersTableName(channelId);
|
|
QByteArray sql = QString(
|
|
"SELECT local_nick FROM \"%1\" WHERE node_id=?"
|
|
).arg(tbl).toUtf8();
|
|
sqlite3_stmt* st = nullptr;
|
|
if (sqlite3_prepare_v2(m_db->m_db, sql.constData(), -1, &st, nullptr) == SQLITE_OK) {
|
|
sqlite3_bind_int64(st, 1, (sqlite3_int64)nodeId);
|
|
if (sqlite3_step(st) == SQLITE_ROW)
|
|
curNick = DbManager::colText(st, 0);
|
|
sqlite3_finalize(st);
|
|
}
|
|
}
|
|
|
|
auto* layout = new QVBoxLayout(this);
|
|
layout->setSpacing(12);
|
|
layout->setContentsMargins(20, 16, 20, 16);
|
|
|
|
auto* title = new QLabel(displayName, this);
|
|
title->setStyleSheet("font-weight: bold; font-size: 13px;");
|
|
layout->addWidget(title);
|
|
|
|
m_edit = new QLineEdit(this);
|
|
m_edit->setText(curNick);
|
|
m_edit->setPlaceholderText(displayName);
|
|
layout->addWidget(m_edit);
|
|
|
|
layout->addStretch();
|
|
|
|
auto* btnLayout = new QHBoxLayout();
|
|
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, &RenameDialog::onSave);
|
|
btnLayout->addWidget(saveBtn);
|
|
|
|
layout->addLayout(btnLayout);
|
|
}
|
|
|
|
void RenameDialog::onSave() {
|
|
if (!m_db->m_db) return;
|
|
QString tbl = m_db->peersTableName(m_channelId);
|
|
QString nick = m_edit->text().trimmed();
|
|
QByteArray sql = QString(
|
|
"UPDATE \"%1\" SET local_nick=? WHERE node_id=?"
|
|
).arg(tbl).toUtf8();
|
|
sqlite3_stmt* st = nullptr;
|
|
if (sqlite3_prepare_v2(m_db->m_db, sql.constData(), -1, &st, nullptr) == SQLITE_OK) {
|
|
sqlite3_bind_text(st, 1, nick.isEmpty() ? nullptr : nick.toUtf8().constData(),
|
|
-1, nick.isEmpty() ? SQLITE_STATIC : SQLITE_TRANSIENT);
|
|
sqlite3_bind_int64(st, 2, (sqlite3_int64)m_nodeId);
|
|
sqlite3_step(st);
|
|
sqlite3_finalize(st);
|
|
}
|
|
accept();
|
|
}
|