Пример #1
0
        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");
        }
Пример #2
0
        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");
            }
        }
Пример #3
0
        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");
        }