-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebdavconfigdialog.cpp
More file actions
340 lines (281 loc) · 11.2 KB
/
webdavconfigdialog.cpp
File metadata and controls
340 lines (281 loc) · 11.2 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#include "webdavconfigdialog.h"
#include "ui_webdavconfigdialog.h"
WebDAVConfigDialog::WebDAVConfigDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::WebDAVConfigDialog),
m_syncManager(nullptr)
{
ui->setupUi(this);
// 连接信号槽
connect(ui->testConnectionButton, &QPushButton::clicked, this, &WebDAVConfigDialog::onTestConnection);
connect(ui->syncNowButton, &QPushButton::clicked, this, &WebDAVConfigDialog::onSyncNowClicked);
// 同步方向选择
connect(ui->twoWayRadio, &QRadioButton::toggled, this, &WebDAVConfigDialog::updateSyncDirectionFromUI);
connect(ui->uploadRadio, &QRadioButton::toggled, this, &WebDAVConfigDialog::updateSyncDirectionFromUI);
connect(ui->downloadRadio, &QRadioButton::toggled, this, &WebDAVConfigDialog::updateSyncDirectionFromUI);
// 初始化UI
ui->syncProgressBar->setValue(0);
ui->syncProgressBar->setFormat("%p% %v");
ui->connectionStatusLabel->setText(tr("未配置"));
ui->lastSyncLabel->setText(tr("上次同步: 从未同步"));
ui->syncStatusLabel->setText(tr("同步状态: 未配置"));
// 设置合理的默认值
ui->serverUrlEdit->setText("");
ui->portSpinBox->setValue(0);
ui->sslCheckBox->setChecked(true);
ui->remoteFolderEdit->setText("/");
ui->usernameEdit->setText("");
ui->passwordEdit->setText("");
ui->autoSyncCheckBox->setChecked(false);
ui->syncIntervalSpinBox->setValue(30);
ui->twoWayRadio->setChecked(true);
// 同步按钮初始禁用
ui->syncNowButton->setEnabled(false);
setWindowTitle(tr("WebDAV云同步配置"));
}
WebDAVConfigDialog::~WebDAVConfigDialog()
{
delete ui;
}
void WebDAVConfigDialog::setSyncManager(WebDAVSyncManager *syncManager)
{
if (!syncManager)
return;
// 解除旧的连接
if (m_syncManager) {
disconnect(m_syncManager, &WebDAVSyncManager::syncStatusChanged, this, &WebDAVConfigDialog::onSyncStatusChanged);
disconnect(m_syncManager, &WebDAVSyncManager::syncProgress, this, &WebDAVConfigDialog::onSyncProgress);
disconnect(m_syncManager, &WebDAVSyncManager::syncFinished, this, &WebDAVConfigDialog::onSyncFinished);
disconnect(m_syncManager, &WebDAVSyncManager::syncError, this, &WebDAVConfigDialog::onSyncError);
}
m_syncManager = syncManager;
// 连接新的信号槽
connect(m_syncManager, &WebDAVSyncManager::syncStatusChanged, this, &WebDAVConfigDialog::onSyncStatusChanged);
connect(m_syncManager, &WebDAVSyncManager::syncProgress, this, &WebDAVConfigDialog::onSyncProgress);
connect(m_syncManager, &WebDAVSyncManager::syncFinished, this, &WebDAVConfigDialog::onSyncFinished);
connect(m_syncManager, &WebDAVSyncManager::syncError, this, &WebDAVConfigDialog::onSyncError);
// 更新UI
updateUIFromSettings();
}
void WebDAVConfigDialog::accept()
{
if (!m_syncManager)
return QDialog::accept();
// 保存配置
QString serverUrl = ui->serverUrlEdit->text().trimmed();
QString username = ui->usernameEdit->text().trimmed();
QString password = ui->passwordEdit->text();
QString remoteFolder = ui->remoteFolderEdit->text().trimmed();
int port = ui->portSpinBox->value();
bool useSSL = ui->sslCheckBox->isChecked();
// 基本验证
if (serverUrl.isEmpty()) {
QMessageBox::warning(this, tr("配置错误"), tr("请输入服务器地址。"));
ui->serverUrlEdit->setFocus();
return;
}
if (username.isEmpty()) {
QMessageBox::warning(this, tr("配置错误"), tr("请输入用户名。"));
ui->usernameEdit->setFocus();
return;
}
if (password.isEmpty()) {
QMessageBox::warning(this, tr("配置错误"), tr("请输入密码。"));
ui->passwordEdit->setFocus();
return;
}
// 配置同步管理器
m_syncManager->configure(serverUrl, username, password, remoteFolder, port, useSSL);
// 配置同步选项
bool autoSync = ui->autoSyncCheckBox->isChecked();
int syncInterval = ui->syncIntervalSpinBox->value();
WebDAVSyncManager::SyncDirection direction = WebDAVSyncManager::TwoWay;
if (ui->uploadRadio->isChecked()) {
direction = WebDAVSyncManager::LocalToRemote;
} else if (ui->downloadRadio->isChecked()) {
direction = WebDAVSyncManager::RemoteToLocal;
}
m_syncManager->setSyncOptions(autoSync, syncInterval, direction);
QDialog::accept();
}
void WebDAVConfigDialog::onTestConnection()
{
if (!m_syncManager)
return;
// 保存当前配置到临时同步管理器
QString serverUrl = ui->serverUrlEdit->text().trimmed();
QString username = ui->usernameEdit->text().trimmed();
QString password = ui->passwordEdit->text();
QString remoteFolder = ui->remoteFolderEdit->text().trimmed();
int port = ui->portSpinBox->value();
bool useSSL = ui->sslCheckBox->isChecked();
// 基本验证
if (serverUrl.isEmpty()) {
QMessageBox::warning(this, tr("配置错误"), tr("请输入服务器地址。"));
ui->serverUrlEdit->setFocus();
return;
}
if (username.isEmpty()) {
QMessageBox::warning(this, tr("配置错误"), tr("请输入用户名。"));
ui->usernameEdit->setFocus();
return;
}
if (password.isEmpty()) {
QMessageBox::warning(this, tr("配置错误"), tr("请输入密码。"));
ui->passwordEdit->setFocus();
return;
}
// 禁用UI
setUIEnabled(false);
// 更新状态
ui->connectionStatusLabel->setText(tr("正在测试连接..."));
// 配置同步管理器
m_syncManager->configure(serverUrl, username, password, remoteFolder, port, useSSL);
// 测试连接
bool testStarted = m_syncManager->testConnection();
if (!testStarted) {
ui->connectionStatusLabel->setText(tr("连接测试失败: %1").arg(m_syncManager->lastError()));
setUIEnabled(true);
}
}
void WebDAVConfigDialog::onSyncNowClicked()
{
if (!m_syncManager || !m_syncManager->isConfigured())
return;
// 禁用UI
setUIEnabled(false);
// 启动同步
bool syncStarted = m_syncManager->startSync();
if (!syncStarted) {
ui->syncStatusLabel->setText(tr("同步启动失败: %1").arg(m_syncManager->lastError()));
setUIEnabled(true);
}
}
void WebDAVConfigDialog::updateSyncDirectionFromUI()
{
if (!m_syncManager)
return;
WebDAVSyncManager::SyncDirection direction = WebDAVSyncManager::TwoWay;
if (ui->uploadRadio->isChecked()) {
direction = WebDAVSyncManager::LocalToRemote;
} else if (ui->downloadRadio->isChecked()) {
direction = WebDAVSyncManager::RemoteToLocal;
}
m_syncManager->setSyncDirection(direction);
}
void WebDAVConfigDialog::updateUIFromSettings()
{
if (!m_syncManager)
return;
// 更新服务器设置
ui->serverUrlEdit->setText(m_syncManager->serverUrl());
ui->portSpinBox->setValue(m_syncManager->port());
ui->sslCheckBox->setChecked(m_syncManager->useSSL());
ui->remoteFolderEdit->setText(m_syncManager->remoteFolder());
ui->usernameEdit->setText(m_syncManager->username());
ui->passwordEdit->setText(m_syncManager->password());
// 更新同步选项
ui->autoSyncCheckBox->setChecked(m_syncManager->autoSync());
ui->syncIntervalSpinBox->setValue(m_syncManager->syncInterval());
// 更新同步方向
WebDAVSyncManager::SyncDirection direction = m_syncManager->syncDirection();
ui->twoWayRadio->setChecked(direction == WebDAVSyncManager::TwoWay);
ui->uploadRadio->setChecked(direction == WebDAVSyncManager::LocalToRemote);
ui->downloadRadio->setChecked(direction == WebDAVSyncManager::RemoteToLocal);
// 更新状态信息
updateSyncStatusUI();
// 启用/禁用同步按钮
ui->syncNowButton->setEnabled(m_syncManager->isConfigured());
}
void WebDAVConfigDialog::onSyncStatusChanged(WebDAVSyncManager::SyncStatus status)
{
updateSyncStatusUI();
}
void WebDAVConfigDialog::onSyncProgress(int percent, const QString &message)
{
ui->syncProgressBar->setValue(percent);
ui->syncStatusLabel->setText(message);
}
void WebDAVConfigDialog::onSyncFinished(bool success)
{
// 更新状态
updateSyncStatusUI();
// 启用UI
setUIEnabled(true);
// 只在同步成功且当前对话框为活动窗口时显示消息框
if (success && isActiveWindow()) {
QMessageBox::information(this, tr("同步完成"), tr("同步操作已成功完成。"));
}
}
void WebDAVConfigDialog::onSyncError(const QString &error)
{
ui->syncStatusLabel->setText(tr("同步错误: %1").arg(error));
setUIEnabled(true);
}
void WebDAVConfigDialog::setUIEnabled(bool enabled)
{
// 禁用/启用UI元素
ui->serverUrlEdit->setEnabled(enabled);
ui->portSpinBox->setEnabled(enabled);
ui->sslCheckBox->setEnabled(enabled);
ui->remoteFolderEdit->setEnabled(enabled);
ui->usernameEdit->setEnabled(enabled);
ui->passwordEdit->setEnabled(enabled);
ui->testConnectionButton->setEnabled(enabled);
ui->syncNowButton->setEnabled(enabled && m_syncManager && m_syncManager->isConfigured());
ui->autoSyncCheckBox->setEnabled(enabled);
ui->syncIntervalSpinBox->setEnabled(enabled);
ui->twoWayRadio->setEnabled(enabled);
ui->uploadRadio->setEnabled(enabled);
ui->downloadRadio->setEnabled(enabled);
ui->buttonBox->setEnabled(enabled);
}
void WebDAVConfigDialog::updateSyncStatusUI()
{
if (!m_syncManager)
return;
// 更新连接状态
bool isConfigured = m_syncManager->isConfigured();
WebDAVSyncManager::SyncStatus status = m_syncManager->status();
// 更新连接状态文本
if (!isConfigured) {
ui->connectionStatusLabel->setText(tr("未配置"));
} else {
switch (status) {
case WebDAVSyncManager::Idle:
ui->connectionStatusLabel->setText(tr("已连接"));
break;
case WebDAVSyncManager::Syncing:
ui->connectionStatusLabel->setText(tr("同步中..."));
break;
case WebDAVSyncManager::Error:
ui->connectionStatusLabel->setText(tr("错误: %1").arg(m_syncManager->lastError()));
break;
default:
ui->connectionStatusLabel->setText(tr("未知状态"));
break;
}
}
// 更新上次同步时间
QDateTime lastSync = m_syncManager->lastSyncTime();
if (lastSync.isValid()) {
ui->lastSyncLabel->setText(tr("上次同步: %1").arg(lastSync.toString("yyyy-MM-dd hh:mm:ss")));
} else {
ui->lastSyncLabel->setText(tr("上次同步: 从未同步"));
}
// 更新同步状态
switch (status) {
case WebDAVSyncManager::NotConfigured:
ui->syncStatusLabel->setText(tr("同步状态: 未配置"));
break;
case WebDAVSyncManager::Idle:
ui->syncStatusLabel->setText(tr("同步状态: 就绪"));
break;
case WebDAVSyncManager::Syncing:
ui->syncStatusLabel->setText(tr("同步状态: 正在同步..."));
break;
case WebDAVSyncManager::Error:
ui->syncStatusLabel->setText(tr("同步状态: 错误 - %1").arg(m_syncManager->lastError()));
break;
}
}