using System; using System.Diagnostics; using System.Drawing; using System.Windows.Forms; namespace DbxNotifier { internal static class Browser { /// 기본 브라우저로 연다. 실패하면 주소를 클립보드에 복사하고 false. public static bool Open(string url) { try { Process.Start(new ProcessStartInfo(url) { UseShellExecute = true }); return true; } catch { try { Clipboard.SetText(url); } catch { /* 클립보드 사용 중 */ } return false; } } } /// /// PC 연결 창 — [브라우저에서 연결] → 서버가 준 코드를 크게 보여 주고 브라우저로 승인 화면(/project/notifier?code=)을 연다. /// 사용자가 브라우저(이미 로그인)에서 [연결]을 누르면 2초마다 확인하던 이 창이 토큰을 받아 닫힌다. /// internal sealed class ConnectForm : Form { public string Server { get; private set; } = ""; public string Token { get; private set; } = ""; public string Email { get; private set; } = ""; public bool AutoStartChecked => _auto.Checked; private readonly Api _api; private readonly TextBox _server; private readonly CheckBox _auto; private readonly Button _go; private readonly Label _code, _status; private readonly Timer _poll = new Timer { Interval = 2000 }; private string _pollSecret = "", _approveUrl = ""; private DateTime _expires; private bool _busy; public ConnectForm(Api api, string server, bool autoStart) { _api = api; Text = "DBX 알림 — PC 연결"; Icon = Gfx.AppIcon(new Size(32, 32)); Font = new Font("Malgun Gothic", 9.75f); AutoScaleDimensions = new SizeF(96f, 96f); AutoScaleMode = AutoScaleMode.Dpi; FormBorderStyle = FormBorderStyle.FixedDialog; MaximizeBox = MinimizeBox = false; StartPosition = FormStartPosition.CenterScreen; ClientSize = new Size(440, 344); BackColor = Color.White; Controls.Add(new Label { Text = "이 PC 에 ERP 알림(나를 태그·작업 배정)을 띄우려면\n내 ERP 계정에 연결하세요.", Location = new Point(20, 18), AutoSize = true, MaximumSize = new Size(400, 0), }); Controls.Add(new Label { Text = "ERP 주소", Location = new Point(20, 84), AutoSize = true }); _server = new TextBox { Text = server, Location = new Point(20, 106), Width = 400 }; Controls.Add(_server); _auto = new CheckBox { Text = "Windows 시작 시 자동 실행", Checked = autoStart, Location = new Point(20, 140), AutoSize = true }; Controls.Add(_auto); _go = new Button { Text = "브라우저에서 연결", Location = new Point(20, 174), Size = new Size(170, 34), BackColor = Gfx.Hex("#7b68ee"), ForeColor = Color.White, FlatStyle = FlatStyle.Flat, }; _go.FlatAppearance.BorderSize = 0; _go.Click += (s, e) => OnGo(); Controls.Add(_go); _code = new Label { Location = new Point(200, 172), Size = new Size(220, 38), Visible = false, Font = new Font("Consolas", 18f, FontStyle.Bold), ForeColor = Gfx.Hex("#6453d6"), TextAlign = ContentAlignment.MiddleCenter, BackColor = Gfx.Hex("#f4f1ff"), }; Controls.Add(_code); _status = new Label { Location = new Point(20, 222), Size = new Size(400, 66), ForeColor = Gfx.Hex("#636363") }; Controls.Add(_status); var close = new Button { Text = "닫기", Location = new Point(340, 298), Size = new Size(80, 30), DialogResult = DialogResult.Cancel }; Controls.Add(close); CancelButton = close; AcceptButton = _go; _poll.Tick += (s, e) => OnPoll(); } private async void OnGo() { if (_approveUrl.Length > 0) // 이미 코드를 받았으면 브라우저만 다시 { OpenApprove(); return; } string server; try { server = Api.NormalizeServer(_server.Text); } catch (ArgumentException ex) { _status.Text = ex.Message; return; } _go.Enabled = false; _server.Enabled = false; _status.Text = "연결 요청 중…"; try { var r = await _api.StartLink(server, Environment.MachineName); Server = server; _pollSecret = r.Poll; _approveUrl = server + r.ApprovePath; _expires = DateTime.Now.AddSeconds(r.ExpiresIn > 0 ? r.ExpiresIn : 600); _code.Text = r.Code; _code.Visible = true; _go.Text = "브라우저 다시 열기"; OpenApprove(); _poll.Start(); } catch (Exception ex) { _status.Text = "연결 요청 실패: " + ex.Message; _server.Enabled = true; } finally { _go.Enabled = true; } } private void OpenApprove() { _status.Text = Browser.Open(_approveUrl) ? "브라우저 화면의 연결 코드가 오른쪽 코드와 같은지 확인하고 [연결]을 누르세요.\n(10분 안에 — 연결되면 이 창은 저절로 닫힙니다.)" : "브라우저를 열지 못해 주소를 복사했습니다. 브라우저 주소창에 붙여 넣으세요.\n" + _approveUrl; } private void Reset(string message) { _poll.Stop(); _approveUrl = _pollSecret = ""; _code.Visible = false; _go.Text = "브라우저에서 연결"; _server.Enabled = true; _status.Text = message; } private async void OnPoll() { if (_busy) return; if (DateTime.Now > _expires) { Reset("연결 코드가 만료됐습니다. [브라우저에서 연결]을 다시 누르세요."); return; } _busy = true; try { var r = await _api.PollLink(Server, _pollSecret); if (r.Status == "ok" && r.Token.Length > 0) { _poll.Stop(); Token = r.Token; Email = r.Email; DialogResult = DialogResult.OK; Close(); } else if (r.Status == "expired") { Reset("연결 코드가 만료됐습니다. [브라우저에서 연결]을 다시 누르세요."); } } catch { // 네트워크 잠깐 끊김 — 다음 확인 때 다시 } finally { _busy = false; } } protected override void Dispose(bool disposing) { if (disposing) _poll.Dispose(); base.Dispose(disposing); } } }