-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathLibraryXmlDialog.cs
209 lines (177 loc) · 6.25 KB
/
LibraryXmlDialog.cs
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using DigitalPlatform.GUI;
using DigitalPlatform.Text;
namespace DigitalPlatform.LibraryServer
{
/// <summary>
/// 服务器同步参数对话框
/// </summary>
public partial class LibraryXmlDialog : Form
{
bool _changed = false;
public bool Changed
{
get
{
return this._changed;
}
set
{
this._changed = value;
}
}
public string LibraryXmlFileName { get; set; }
XmlDocument _dom = new XmlDocument();
public LibraryXmlDialog()
{
InitializeComponent();
}
private void LibraryXmlDialog_Load(object sender, EventArgs e)
{
this.Invoke(new Action(LoadLibraryXml));
}
private void button_OK_Click(object sender, EventArgs e)
{
if (this.Changed)
SaveLibraryXml();
this.DialogResult = System.Windows.Forms.DialogResult.OK;
this.Close();
}
private void button_Cancel_Click(object sender, EventArgs e)
{
this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.Close();
}
private void button_editMasterServer_Click(object sender, EventArgs e)
{
ConfirmSupervisorDialog dlg = new ConfirmSupervisorDialog();
GuiUtil.AutoSetDefaultFont(dlg);
dlg.Text = "主服务器";
dlg.Comment = "请设置主服务器的地址和用户名";
dlg.ServerUrl = this.m_strUrl;
dlg.UserName = this.m_strUserName;
dlg.Password = this.m_strPassword;
dlg.PhoneNumberVisible = false;
dlg.ServerUrlReadOnly = false;
dlg.StartPosition = FormStartPosition.CenterScreen;
REDO_INPUT:
dlg.ShowDialog(this);
if (dlg.DialogResult == DialogResult.Cancel)
return;
if (string.IsNullOrEmpty(dlg.ServerUrl))
{
MessageBox.Show(this, "尚未输入服务器 URL");
goto REDO_INPUT;
}
if (string.IsNullOrEmpty(dlg.UserName))
{
MessageBox.Show(this, "尚未输入用户名");
goto REDO_INPUT;
}
if (dlg.ServerUrl != this.m_strUrl
|| dlg.UserName != this.m_strUserName
|| dlg.Password != this.m_strPassword)
{
this.m_strUrl = dlg.ServerUrl;
this.m_strUserName = dlg.UserName;
this.m_strPassword = dlg.Password;
RefreshMasterServerSummary();
this.Changed = true;
}
}
string m_strUrl = "";
string m_strUserName = "";
string m_strPassword = "";
void LoadLibraryXml()
{
if (string.IsNullOrEmpty(this.LibraryXmlFileName))
return;
string strError = "";
_dom = new XmlDocument();
try
{
_dom.Load(this.LibraryXmlFileName);
}
catch (Exception ex)
{
strError = "装载配置文件 '" + this.LibraryXmlFileName + "' 进入 XMLDOM 时出错: " + ex.Message;
goto ERROR1;
}
XmlElement server = _dom.DocumentElement.SelectSingleNode("serverReplication") as XmlElement;
if (server == null)
return;
this.m_strUrl = server.GetAttribute("url");
this.m_strUserName = server.GetAttribute("username");
try
{
this.m_strPassword = DecryptPassword(server.GetAttribute("password"));
}
catch (System.Security.Cryptography.CryptographicException)
{
strError = "library.xml 中 serverReplication 元素的 password 属性值不合法";
goto ERROR1;
}
RefreshMasterServerSummary();
return;
ERROR1:
MessageBox.Show(this, strError);
}
const string EncryptKey = "dp2circulationpassword";
// 加密明文
public static string EncryptPassword(string PlainText)
{
return Cryptography.Encrypt(PlainText, EncryptKey);
}
// 解密加密过的文字
public static string DecryptPassword(string EncryptText)
{
return Cryptography.Decrypt(EncryptText, EncryptKey);
}
void SaveLibraryXml()
{
XmlElement server = _dom.DocumentElement.SelectSingleNode("serverReplication") as XmlElement;
if (server == null)
{
server = _dom.CreateElement("serverReplication");
_dom.DocumentElement.AppendChild(server);
}
server.SetAttribute("url", this.m_strUrl);
server.SetAttribute("username", this.m_strUserName);
server.SetAttribute("password", EncryptPassword(this.m_strPassword));
_dom.Save(this.LibraryXmlFileName);
this.Changed = false;
}
void RefreshMasterServerSummary()
{
this.textBox_masterServer.Text = "dp2library URL = " + this.m_strUrl
+ "; UserName = " + this.m_strUserName
+ "; Password = " + new string('*', this.m_strPassword.Length);
}
private void LibraryXmlDialog_FormClosing(object sender, FormClosingEventArgs e)
{
if (this.Changed == true)
{
// 警告尚未保存
DialogResult result = MessageBox.Show(this,
"当前有信息被修改后尚未保存。若此时关闭窗口,现有未保存信息将丢失。\r\n\r\n确实要关闭窗口? ",
"LibraryXmlDialog",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question,
MessageBoxDefaultButton.Button2);
if (result != DialogResult.Yes)
{
e.Cancel = true;
return;
}
}
}
}
}