Repository navigation
Expand file tree
/
Copy pathcertdialog.cpp
More file actions
146 lines (129 loc) · 5.42 KB
/
Copy pathcertdialog.cpp
File metadata and controls
146 lines (129 loc) · 5.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
* Copyright (C) 2026 zhutianhao <zhutianhao75@hotmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "certdialog.h"
#include <QFileDialog>
#include <QFormLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QVBoxLayout>
CertDialog::CertDialog(FileServer *server, QWidget *parent)
: Dtk::Widget::DDialog(parent)
, m_server(server)
{
setTitle(tr("HTTPS 证书"));
setMessage(tr("授权模式默认使用 HTTPS。导入正式证书后,浏览器与手机扫码不再提示证书"
"不受信任;未配置时会用 openssl 生成自签名证书。配置只记录文件路径,"
"不复制私钥内容。"));
setOnButtonClickedClose(false);
auto *content = new QWidget;
auto *form = new QFormLayout(content);
form->setContentsMargins(0, 0, 0, 0);
form->setSpacing(8);
m_certEdit = new Dtk::Widget::DFileChooserEdit;
m_certEdit->setFileMode(QFileDialog::ExistingFile);
m_certEdit->setNameFilters({ tr("证书文件 (*.pem *.crt *.cer)"), tr("所有文件 (*)") });
m_certEdit->setText(m_server->userCertPath());
m_keyEdit = new Dtk::Widget::DFileChooserEdit;
m_keyEdit->setFileMode(QFileDialog::ExistingFile);
m_keyEdit->setNameFilters({ tr("私钥文件 (*.key *.pem)"), tr("所有文件 (*)") });
m_keyEdit->setText(m_server->userKeyPath());
form->addRow(tr("证书文件:"), m_certEdit);
form->addRow(tr("私钥文件:"), m_keyEdit);
m_statusLabel = new QLabel;
m_statusLabel->setWordWrap(true);
m_statusLabel->setMinimumWidth(420);
form->addRow(QString(), m_statusLabel);
m_clearBtn = new Dtk::Widget::DPushButton(tr("清除证书,改用自签名"));
m_clearBtn->setEnabled(m_server->hasUserCert());
auto *clearRow = new QHBoxLayout;
clearRow->setContentsMargins(0, 0, 0, 0);
clearRow->addWidget(m_clearBtn);
clearRow->addStretch(1);
form->addRow(QString(), clearRow);
addContent(content);
addButton(tr("取消"), false, DDialog::ButtonNormal); // 索引 0
addButton(tr("导入"), true, DDialog::ButtonRecommend); // 索引 1
// 即时校验:选完文件或编辑结束后做,避免输入过程中反复读文件与握手自检。
connect(m_certEdit, &Dtk::Widget::DFileChooserEdit::fileChoosed, this,
[this]() { refreshStatus(); });
connect(m_keyEdit, &Dtk::Widget::DFileChooserEdit::fileChoosed, this,
[this]() { refreshStatus(); });
connect(m_certEdit, &Dtk::Widget::DLineEdit::editingFinished, this,
[this]() { refreshStatus(); });
connect(m_keyEdit, &Dtk::Widget::DLineEdit::editingFinished, this,
[this]() { refreshStatus(); });
connect(m_clearBtn, &Dtk::Widget::DPushButton::clicked, this, &CertDialog::clearCert);
connect(this, &DDialog::buttonClicked, this, [this](int index, const QString &) {
if (index == 1)
applyCert();
else
reject();
});
refreshStatus();
}
void CertDialog::refreshStatus(const QString &prefix)
{
m_info = m_server->probeCert(m_certEdit->text().trimmed(),
m_keyEdit->text().trimmed());
QString text;
bool usable = false;
switch (m_info.status) {
case FileServer::CertStatus::Ok:
case FileServer::CertStatus::ExpiringSoon:
case FileServer::CertStatus::Unknown:
usable = true;
text = tr("可用:%1").arg(m_info.subject.isEmpty() ? tr("(无 CN)") : m_info.subject);
if (m_info.cert.expiryDate().isValid())
text += tr(",有效期至 %1(剩余 %2 天)")
.arg(m_info.cert.expiryDate().toString(QStringLiteral("yyyy-MM-dd")))
.arg(m_info.daysLeft);
if (m_info.status == FileServer::CertStatus::Unknown)
text += QLatin1Char('\n') + m_info.detail;
else if (m_info.status == FileServer::CertStatus::ExpiringSoon)
text += QLatin1Char('\n') + m_info.detail;
break;
case FileServer::CertStatus::NoUserCert:
text = tr("尚未选择证书与私钥");
break;
default:
text = tr("不可用:%1").arg(m_info.detail);
break;
}
if (!prefix.isEmpty())
text = prefix + QLatin1Char('\n') + text;
m_statusLabel->setText(text);
// 「导入」是 DDialog 的第 2 个按钮(索引 1),不可用时禁用。
if (QAbstractButton *btn = getButton(1))
btn->setEnabled(usable);
}
void CertDialog::applyCert()
{
const QString cert = m_certEdit->text().trimmed();
const QString key = m_keyEdit->text().trimmed();
if (m_server->setUserCert(cert, key)) {
m_changed = true;
accept();
return;
}
refreshStatus(tr("导入失败:%1").arg(m_server->lastCertError()));
}
void CertDialog::clearCert()
{
m_server->clearUserCert();
m_changed = true;
accept();
}