void FillList() { listView1.Items.Clear(); if (Servers == null) { return; } for (int i = 0; i < Servers.Count; i++) { dp2Server server = (dp2Server)Servers[i]; ListViewItem item = new ListViewItem(server.Name, 0); listView1.Items.Add(item); // item.Tag = server; ListViewUtil.ChangeItemText(item, COLUMN_URL, server.Url); ListViewUtil.ChangeItemText(item, COLUMN_USERNAME, server.DefaultUserName); ListViewUtil.ChangeItemText(item, COLUMN_SAVEPASSWORD, server.SavePassword == true ? "是" : "否"); ListViewUtil.ChangeItemText(item, COLUMN_UID, server.UID); ListViewUtil.ChangeItemText(item, COLUMN_LINENO, (i + 1).ToString()); //item.SubItems.Add(server.Url); //item.SubItems.Add(server.DefaultUserName); //item.SubItems.Add(server.SavePassword == true ? "是" : "否"); } }
// 创建一个新的Server对象 // return: // -1 出错 // 0 加入了 // 1 发现重复,没有加入 public int NewServer( string strName, string strUrl, int nInsertPos) { dp2Server server = null; // 暂时不去重 server = new dp2Server(); server.Url = strUrl; server.Name = strName; if (nInsertPos == -1) { this.Add(server); } else { this.Insert(nInsertPos, server); } m_bChanged = true; dp2ServerChangedEventArgs e = new dp2ServerChangedEventArgs(); e.Url = strUrl; e.ServerChangeAction = dp2ServerChangeAction.Add; OnServerChanged(this, e); return(0); }
void menu_copyJSONtoClipboard(object sender, System.EventArgs e) { string strError; if (listView1.SelectedIndices.Count == 0) { strError = "尚未选择要刷新 UID 的事项 ..."; goto ERROR1; } List <CopyServer> servers = new List <CopyServer>(); foreach (int index in this.listView1.SelectedIndices) { dp2Server server = Servers[index] as dp2Server; servers.Add(CopyServer.From(server)); } string value = JsonConvert.SerializeObject(servers, Formatting.Indented); Clipboard.SetText(value); return; ERROR1: MessageBox.Show(this, strError); }
async void menu_refreshUID(object sender, System.EventArgs e) { string strError = ""; if (listView1.SelectedIndices.Count == 0) { strError = "尚未选择要刷新 UID 的事项 ..."; goto ERROR1; } int change_count = 0; List <string> errors = new List <string>(); using (MessageBar bar = MessageBar.Create(this, "正在刷新 UID")) { foreach (int index in this.listView1.SelectedIndices) { dp2Server server = Servers[index] as dp2Server; // 获得服务器 UID string uid = ""; bar.SetMessageText($"正在获取服务器 {server.Url} 的 UID ..."); var result = await ServerDlg.GetServerUID(server.Url); if (result.Value == -1) { errors.Add($"针对服务器 {server.Url} 获取服务器 UID 时出错: {result.ErrorInfo}"); continue; } else { uid = result.ErrorCode; } if (server.UID != uid) { server.UID = uid; change_count++; } } // TODO: 刷新后如果发现发生了 UID 重复,怎么处理? if (change_count > 0) { Servers.Changed = true; FillList(); } } if (errors.Count > 0) { strError = $"刷新 UID 时出错:\r\n{StringUtil.MakePathList(errors, "\r\n")}"; goto ERROR1; } return; ERROR1: MessageDlg.Show(this, strError, "ServersDlg"); }
public void SetAllVerified(bool bVerified) { for (int i = 0; i < this.Count; i++) { dp2Server server = (dp2Server)this[i]; server.Verified = bVerified; } }
// 拷贝构造函数 public dp2Server(dp2Server refServer) { this.Name = refServer.Name; this.Url = refServer.Url; this.DefaultUserName = refServer.DefaultUserName; this.StorageDefaultPassword = refServer.StorageDefaultPassword; this.SavePassword = refServer.SavePassword; this.Verified = refServer.Verified; }
public void CopyTo(dp2Server server) { var source = this; server.Name = source.Name; server.Url = source.Url; server.UID = source.UID; server.DefaultUserName = source.UserName; server.SavePassword = source.SavePassword; server.DefaultPassword = source.GetPassword(); }
// 从 Windows 剪贴板中粘贴 JSON 格式 void menu_pasteFromJSONClipboard(object sender, System.EventArgs e) { string strError; try { // 插入位置 int index = this.listView1.Items.IndexOf(this.listView1.FocusedItem); if (index == -1 && this.listView1.SelectedIndices.Count > 0) { index = this.listView1.SelectedIndices[0]; } List <int> indices = new List <int>(); string value = Clipboard.GetText(); // Newtonsoft.Json.JsonReaderException var servers = JsonConvert.DeserializeObject <List <CopyServer> >(value); foreach (var source in servers) { dp2Server server = Servers.NewServer(index); source.CopyTo(server); indices.Add(index); index++; } if (indices.Count > 0) { Servers.Changed = true; FillList(); foreach (int i in indices) { var item = this.listView1.Items[i]; item.Selected = true; item.EnsureVisible(); } } return; } catch (Newtonsoft.Json.JsonReaderException) { strError = "剪贴板中的内容不是特定格式"; goto ERROR1; } catch (Exception ex) { strError = ex.Message; goto ERROR1; } ERROR1: MessageBox.Show(this, strError); }
public static CopyServer From(dp2Server server) { CopyServer result = new CopyServer { Name = server.Name, Url = server.Url, UID = server.UID, UserName = server.DefaultUserName, SavePassword = server.SavePassword, EncryptPassword = server.SavePassword == false ? null : Cryptography.Encrypt(server.DefaultPassword, GetKey()), }; return(result); }
// 2007/9/14 public dp2Server GetServerByName(string strServerName) { dp2Server server = null; for (int i = 0; i < this.Count; i++) { server = (dp2Server)this[i]; if (server.Name == strServerName) { return(server); } } return(null); }
// 克隆。 // 新数组中的对象完全是新创建的。 public dp2ServerCollectionNew Dup() { dp2ServerCollectionNew newServers = new dp2ServerCollectionNew(); foreach (var server in _servers) { dp2Server newServer = new dp2Server(server); newServers.Add(newServer); } newServers.FileName = this.FileName; newServers.Changed = this.Changed; // newServers.ownerForm = this.ownerForm; return(newServers); }
// 克隆。 // 新数组中的对象完全是新创建的。 public dp2ServerCollection Dup() { dp2ServerCollection newServers = new dp2ServerCollection(); for (int i = 0; i < this.Count; i++) { dp2Server newServer = new dp2Server((dp2Server)this[i]); newServers.Add(newServer); } newServers.m_strFileName = this.m_strFileName; newServers.m_bChanged = this.m_bChanged; newServers.ownerForm = this.ownerForm; return(newServers); }
// 创建一个新的Server对象 // return: public dp2Server NewServer(int nInsertPos) { dp2Server server = null; server = new dp2Server(); if (nInsertPos == -1) { this.Add(server); } else { this.Insert(nInsertPos, server); } m_bChanged = true; return(server); }
/// <summary> /// 根据 URL 获得 dp2Server 对象 /// </summary> /// <param name="strUrl">服务器 URL</param> /// <returns>dp2Server 对象</returns> public dp2Server GetServer(string strUrl) { strUrl = StringUtil.CanonicalizeHostUrl(strUrl); dp2Server server = null; for (int i = 0; i < this.Count; i++) { server = (dp2Server)this[i]; string strCurrentUrl = StringUtil.CanonicalizeHostUrl(server.Url); if (String.Compare(strCurrentUrl, strUrl, true) == 0) { return(server); } } return(null); }
void FillList() { listView1.Items.Clear(); if (Servers == null) { return; } for (int i = 0; i < Servers.Count; i++) { dp2Server server = (dp2Server)Servers[i]; ListViewItem item = new ListViewItem(server.Name, 0); listView1.Items.Add(item); item.SubItems.Add(server.Url); item.SubItems.Add(server.DefaultUserName); item.SubItems.Add(server.SavePassword == true ? "是" : "否"); } }
async void menu_refreshServerName(object sender, System.EventArgs e) { string strError = ""; if (listView1.SelectedIndices.Count == 0) { strError = "尚未选择要刷新服务器名的事项 ..."; goto ERROR1; } int change_count = 0; List <string> errors = new List <string>(); using (MessageBar bar = MessageBar.Create(this, "正在刷新服务器名")) { foreach (int index in this.listView1.SelectedIndices) { dp2Server server = Servers[index] as dp2Server; // 获得服务器 UID string server_name = ""; bar.SetMessageText($"正在获取服务器 {server.Url} 的图书馆名 ..."); var result = await ServerDlg.GetServerName(server.Url); if (result.Value == -1) { errors.Add($"针对服务器 {server.Url} 获取图书馆名时出错: {result.ErrorInfo}"); continue; } else { server_name = result.ErrorCode; } if (server.Name != server_name) { server.Name = server_name; change_count++; } } // TODO: 刷新后如果发现发生了服务器名重复,怎么处理? if (change_count > 0) { Servers.Changed = true; // 刷新前保存选择位置 var indices = ListViewUtil.GetSelectedIndices(this.listView1); this.listView1.BeginUpdate(); FillList(); // 刷新后恢复选择位置 ListViewUtil.SelectItems(this.listView1, indices); this.listView1.EndUpdate(); } } if (errors.Count > 0) { strError = $"刷新服务器名时出错:\r\n{StringUtil.MakePathList(errors, "\r\n")}"; goto ERROR1; } return; ERROR1: // TODO: MessageDlg 应可以指定左上角图标 MessageDlg.Show(this, strError, "ServersDlg"); }
// 创建一个新的Server对象 // return: public dp2Server NewServer(int nInsertPos) { dp2Server server = null; server = new dp2Server(); if (nInsertPos == -1) this.Add(server); else this.Insert(nInsertPos, server); m_bChanged = true; return server; }
// 创建一个新的Server对象 // return: // -1 出错 // 0 加入了 // 1 发现重复,没有加入 public int NewServer( string strName, string strUrl, int nInsertPos) { dp2Server server = null; // 暂时不去重 server = new dp2Server(); server.Url = strUrl; server.Name = strName; if (nInsertPos == -1) this.Add(server); else this.Insert(nInsertPos, server); m_bChanged = true; dp2ServerChangedEventArgs e = new dp2ServerChangedEventArgs(); e.Url = strUrl; e.ServerChangeAction = dp2ServerChangeAction.Add; OnServerChanged(this, e); return 0; }
// 克隆。 // 新数组中的对象完全是新创建的。 public dp2ServerCollection Dup() { dp2ServerCollection newServers = new dp2ServerCollection(); for (int i = 0; i < this.Count; i++) { dp2Server newServer = new dp2Server((dp2Server)this[i]); newServers.Add(newServer); } newServers.m_strFileName = this.m_strFileName; newServers.m_bChanged = this.m_bChanged; newServers.ownerForm = this.ownerForm; return newServers; }
public void Insert(int pos, dp2Server server) { _servers.Insert(pos, server); this.m_bChanged = true; }
async void menu_newServer(object sender, System.EventArgs e) { List <string> errors = new List <string>(); int nActiveLine = -1; if (listView1.SelectedIndices.Count != 0) { nActiveLine = listView1.SelectedIndices[0]; } ServerDlg dlg = new ServerDlg(); // GuiUtil.AutoSetDefaultFont(dlg); GuiUtil.SetControlFont(dlg, this.Font); dlg.Text = "新增服务器地址和默认帐户"; if (nActiveLine == -1) { // 无参考事项情形的新增 #if NO dlg.ServerName = "社科院联合编目中心"; dlg.ServerUrl = "http://ssucs.org/dp2library"; dlg.UserName = "******"; #endif dlg.ServerName = "单机版服务器"; dlg.ServerUrl = "net.pipe://localhost/dp2library/xe"; dlg.UserName = "******"; } else { dp2Server server = (dp2Server)Servers[nActiveLine]; dlg.ServerName = server.Name; dlg.Password = server.DefaultPassword; dlg.ServerUrl = server.Url; dlg.UserName = server.DefaultUserName; dlg.SavePassword = server.SavePassword; } dlg.ShowDialog(this); if (dlg.DialogResult != DialogResult.OK) { return; } List <string> urls = new List <string>(); if (dlg.ServerUrl.IndexOf("\r\n") == -1) { urls.Add(dlg.ServerUrl); } else { urls = StringUtil.SplitList(dlg.ServerUrl.Replace("\r\n", "\r"), '\r'); StringUtil.RemoveBlank(ref urls); StringUtil.RemoveDupNoSort(ref urls); } using (MessageBar bar = MessageBar.Create(this, "正在添加服务器节点")) { // 允许一次创建多个服务器节点 int i = 0; foreach (string url in urls) { // 获得服务器 UID string uid = ""; bar.SetMessageText($"正在获取服务器 {url} 的 UID ..."); var result = await ServerDlg.GetServerUID(url); if (result.Value == -1) { errors.Add($"针对服务器 {url} 获取服务器 UID 时出错: {result.ErrorInfo}"); } else { uid = result.ErrorCode; } // 对 UID 进行查重 if (string.IsNullOrEmpty(uid) == false) { var dup_list = Servers.FindServerByUID(uid); if (dup_list.Count > 0) { errors.Add($"拟添加的新服务器节点 '{url}' 因其 UID '{uid}' 和已有的服务器节点({dup_list[0].Name})重复,无法添加"); continue; } } dp2Server server = Servers.NewServer(nActiveLine); // TODO: 建议用 public 账户尝试从 dp2library 服务器获得服务器名字符串 string name = dlg.ServerName; if (i > 0) { name = dlg.ServerName + (i + 1).ToString(); } server.Name = name; server.DefaultPassword = dlg.Password; server.Url = url; server.DefaultUserName = dlg.UserName; server.SavePassword = dlg.SavePassword; server.UID = uid; i++; } } Servers.Changed = true; FillList(); // 选择一行 // parameters: // nIndex 要设置选择标记的行。如果==-1,表示清除全部选择标记但不选择。 // bMoveFocus 是否同时移动focus标志到所选择行 ListViewUtil.SelectLine(listView1, Servers.Count - 1, true); listView1.EnsureVisible(Servers.Count - 1); m_bChanged = true; if (errors.Count > 0) { MessageDlg.Show(this, $"新增服务器节点时出错:\r\n{StringUtil.MakePathList(errors, "\r\n")}", "ServersDlg"); } }
async void menu_modifyServer(object sender, System.EventArgs e) { string strError; if (listView1.SelectedIndices.Count == 0) { strError = "尚未选择要修改的事项 ..."; goto ERROR1; } int nActiveLine = listView1.SelectedIndices[0]; // ListViewItem item = listView1.Items[nActiveLine]; ServerDlg dlg = new ServerDlg(); // GuiUtil.AutoSetDefaultFont(dlg); GuiUtil.SetControlFont(dlg, this.Font); dlg.Text = "修改缺省帐户参数"; dlg.ServerName = ((dp2Server)Servers[nActiveLine]).Name; dlg.Password = ((dp2Server)Servers[nActiveLine]).DefaultPassword; dlg.ServerUrl = ((dp2Server)Servers[nActiveLine]).Url; dlg.UserName = ((dp2Server)Servers[nActiveLine]).DefaultUserName; dlg.SavePassword = ((dp2Server)Servers[nActiveLine]).SavePassword; dlg.ShowDialog(this); if (dlg.DialogResult != DialogResult.OK) { return; } dp2Server server = ((dp2Server)Servers[nActiveLine]); server.Name = dlg.ServerName; server.DefaultPassword = dlg.Password; server.Url = dlg.ServerUrl; server.DefaultUserName = dlg.UserName; server.SavePassword = dlg.SavePassword; // 获取 UID using (MessageBar bar = MessageBar.Create(this, "获取 UID", $"正在获取服务器 {server.Url} 的 UID ...")) { var result = await ServerDlg.GetServerUID(server.Url); if (result.Value == -1) { strError = result.ErrorInfo; goto ERROR1; } server.UID = result.ErrorCode; } Servers.Changed = true; FillList(); // 选择一行 // parameters: // nIndex 要设置选择标记的行。如果==-1,表示清除全部选择标记但不选择。 // bMoveFocus 是否同时移动focus标志到所选择行 ListViewUtil.SelectLine(listView1, nActiveLine, true); m_bChanged = true; return; ERROR1: MessageBox.Show(this, strError); }
void menu_newServer(object sender, System.EventArgs e) { int nActiveLine = -1; if (listView1.SelectedIndices.Count != 0) { nActiveLine = listView1.SelectedIndices[0]; } ServerDlg dlg = new ServerDlg(); // GuiUtil.AutoSetDefaultFont(dlg); GuiUtil.SetControlFont(dlg, this.Font); dlg.Text = "新增服务器地址和缺省帐户"; if (nActiveLine == -1) { // 无参考事项情形的新增 dlg.ServerName = "社科院联合编目中心"; dlg.ServerUrl = "http://ssucs.org/dp2library"; dlg.UserName = "******"; } else { dlg.ServerName = ((dp2Server)Servers[nActiveLine]).Name; dlg.Password = ((dp2Server)Servers[nActiveLine]).DefaultPassword; dlg.ServerUrl = ((dp2Server)Servers[nActiveLine]).Url; dlg.UserName = ((dp2Server)Servers[nActiveLine]).DefaultUserName; dlg.SavePassword = ((dp2Server)Servers[nActiveLine]).SavePassword; } dlg.ShowDialog(this); if (dlg.DialogResult != DialogResult.OK) { return; } dp2Server server = Servers.NewServer(nActiveLine); server.Name = dlg.ServerName; server.DefaultPassword = dlg.Password; server.Url = dlg.ServerUrl; server.DefaultUserName = dlg.UserName; server.SavePassword = dlg.SavePassword; Servers.Changed = true; FillList(); // 选择一行 // parameters: // nIndex 要设置选择标记的行。如果==-1,表示清除全部选择标记但不选择。 // bMoveFocus 是否同时移动focus标志到所选择行 ListViewUtil.SelectLine(listView1, Servers.Count - 1, true); m_bChanged = true; }
public void Add(dp2Server server) { _servers.Add(server); this.m_bChanged = true; }