ConfirmationFormWeb.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. using System;
  2. using System.Drawing;
  3. using System.Threading.Tasks;
  4. using System.Windows.Forms;
  5. using SteamAuth;
  6. using System.Drawing.Drawing2D;
  7. namespace Steam_Desktop_Authenticator
  8. {
  9. public partial class ConfirmationFormWeb : Form
  10. {
  11. private SteamGuardAccount steamAccount;
  12. public ConfirmationFormWeb(SteamGuardAccount steamAccount)
  13. {
  14. InitializeComponent();
  15. this.steamAccount = steamAccount;
  16. this.Text = String.Format("Trade Confirmations - {0}", steamAccount.AccountName);
  17. }
  18. private async Task LoadData()
  19. {
  20. this.splitContainer1.Panel2.Controls.Clear();
  21. // Check for a valid refresh token first
  22. if (steamAccount.Session.IsRefreshTokenExpired())
  23. {
  24. MessageBox.Show("Your session has expired. Use the login again button under the selected account menu.", "Trade Confirmations", MessageBoxButtons.OK, MessageBoxIcon.Error);
  25. this.Close();
  26. return;
  27. }
  28. // Check for a valid access token, refresh it if needed
  29. if (steamAccount.Session.IsAccessTokenExpired())
  30. {
  31. try
  32. {
  33. await steamAccount.Session.RefreshAccessToken();
  34. }
  35. catch (Exception ex)
  36. {
  37. MessageBox.Show(ex.Message, "Steam Login Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  38. this.Close();
  39. return;
  40. }
  41. }
  42. try
  43. {
  44. var confirmations = await steamAccount.FetchConfirmationsAsync();
  45. if (confirmations == null || confirmations.Length == 0)
  46. {
  47. Label errorLabel = new Label() { Text = "Nothing to confirm/cancel", AutoSize = true, ForeColor = Color.Black, Location = new Point(150, 20) };
  48. this.splitContainer1.Panel2.Controls.Add(errorLabel);
  49. }
  50. foreach (var confirmation in confirmations)
  51. {
  52. Panel panel = new Panel() { Dock = DockStyle.Top, Height = 120 };
  53. panel.Paint += (s, e) =>
  54. {
  55. using (LinearGradientBrush brush = new LinearGradientBrush(panel.ClientRectangle, Color.Black, Color.DarkCyan, 90F))
  56. {
  57. e.Graphics.FillRectangle(brush, panel.ClientRectangle);
  58. }
  59. };
  60. if (!string.IsNullOrEmpty(confirmation.Icon))
  61. {
  62. PictureBox pictureBox = new PictureBox() { Width = 60, Height = 60, Location = new Point(20, 20), SizeMode = PictureBoxSizeMode.Zoom };
  63. try
  64. {
  65. pictureBox.Load(confirmation.Icon);
  66. }
  67. catch (Exception ex)
  68. {
  69. Console.WriteLine("Failed to load avatar: " + ex.Message);
  70. }
  71. panel.Controls.Add(pictureBox);
  72. }
  73. Label nameLabel = new Label()
  74. {
  75. Text = $"{confirmation.Headline}\n{confirmation.Creator.ToString()}",
  76. AutoSize = true,
  77. ForeColor = Color.Snow,
  78. Location = new Point(90, 20),
  79. BackColor = Color.Transparent
  80. };
  81. panel.Controls.Add(nameLabel);
  82. ConfirmationButton acceptButton = new ConfirmationButton()
  83. {
  84. Text = confirmation.Accept,
  85. Location = new Point(90, 50),
  86. FlatStyle = FlatStyle.Flat,
  87. FlatAppearance = { BorderSize = 0 },
  88. BackColor = Color.Black,
  89. ForeColor = Color.Snow,
  90. Confirmation = confirmation
  91. };
  92. acceptButton.Click += btnAccept_Click;
  93. panel.Controls.Add(acceptButton);
  94. ConfirmationButton cancelButton = new ConfirmationButton()
  95. {
  96. Text = confirmation.Cancel,
  97. Location = new Point(180, 50),
  98. FlatStyle = FlatStyle.Flat,
  99. FlatAppearance = { BorderSize = 0 },
  100. BackColor = Color.Black,
  101. ForeColor = Color.Snow,
  102. Confirmation = confirmation
  103. };
  104. cancelButton.Click += btnCancel_Click;
  105. panel.Controls.Add(cancelButton);
  106. Label summaryLabel = new Label()
  107. {
  108. Text = String.Join("\n", confirmation.Summary),
  109. AutoSize = true,
  110. ForeColor = Color.Snow,
  111. Location = new Point(90, 80),
  112. BackColor = Color.Transparent
  113. };
  114. panel.Controls.Add(summaryLabel);
  115. this.splitContainer1.Panel2.Controls.Add(panel);
  116. }
  117. }
  118. catch (Exception ex)
  119. {
  120. Label errorLabel = new Label() { Text = "Something went wrong:\n" + ex.Message, AutoSize = true, ForeColor = Color.Red, Location = new Point(20, 20) };
  121. this.splitContainer1.Panel2.Controls.Add(errorLabel);
  122. }
  123. }
  124. private async void btnAccept_Click(object sender, EventArgs e)
  125. {
  126. var button = (ConfirmationButton)sender;
  127. var confirmation = button.Confirmation;
  128. bool result = await steamAccount.AcceptConfirmation(confirmation);
  129. await this.LoadData();
  130. }
  131. private async void btnCancel_Click(object sender, EventArgs e)
  132. {
  133. var button = (ConfirmationButton)sender;
  134. var confirmation = button.Confirmation;
  135. bool result = await steamAccount.DenyConfirmation(confirmation);
  136. await this.LoadData();
  137. }
  138. private async void btnRefresh_Click(object sender, EventArgs e)
  139. {
  140. this.btnRefresh.Enabled = false;
  141. this.btnRefresh.Text = "Refreshing...";
  142. await this.LoadData();
  143. this.btnRefresh.Enabled = true;
  144. this.btnRefresh.Text = "Refresh";
  145. }
  146. private async void ConfirmationFormWeb_Shown(object sender, EventArgs e)
  147. {
  148. this.btnRefresh.Enabled = false;
  149. this.btnRefresh.Text = "Refreshing...";
  150. await this.LoadData();
  151. this.btnRefresh.Enabled = true;
  152. this.btnRefresh.Text = "Refresh";
  153. }
  154. }
  155. }