AuthFormOptions.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. using FastReport.Forms;
  2. using FastReport.Utils;
  3. using System;
  4. using System.Diagnostics;
  5. using System.Drawing;
  6. using System.Drawing.Drawing2D;
  7. using System.Windows.Forms;
  8. namespace FastReport.Auth
  9. {
  10. /// <summary>
  11. /// Form for showing personalization information
  12. /// </summary>
  13. public partial class AuthFormOptions : DesignerOptionsPage
  14. {
  15. #region Private Fields
  16. private string expiresFormat;
  17. #endregion Private Fields
  18. #region Public Constructors
  19. /// <summary>
  20. /// Default constructor
  21. /// </summary>
  22. public AuthFormOptions()
  23. {
  24. InitializeComponent();
  25. Localize();
  26. AuthService.Instance.Refresh();
  27. UpdateInfo();
  28. RTL();
  29. comboBoxServerServer.SelectedIndex = 0;
  30. }
  31. #endregion Public Constructors
  32. #region Private Methods
  33. internal void UpdateCaptionFont(int dpi)
  34. {
  35. float scale = dpi == 0? 1f : dpi / 96f;
  36. lbCaption.Font = new Font("Calibri Light", 19.5f * scale );
  37. lbCaptionServer.Font = new Font("Calibri Light", 19.5f * scale );
  38. return;
  39. }
  40. private void btnSignIn_Click(object sender, EventArgs e)
  41. {
  42. AuthService.Instance.SignIn();
  43. UpdateInfo();
  44. }
  45. private void lbCreateOne_Click(object sender, EventArgs e)
  46. {
  47. ProcessHelper.StartProcess("https://id.fast-report.com/");
  48. }
  49. private void lbManage_Click(object sender, EventArgs e)
  50. {
  51. ProcessHelper.StartProcess("https://id.fast-report.com/");
  52. }
  53. private void lbSignOut_Click(object sender, EventArgs e)
  54. {
  55. AuthService.Instance.SignOut();
  56. UpdateInfo();
  57. }
  58. private void lbUpdate_Click(object sender, EventArgs e)
  59. {
  60. AuthService.Instance.SignIn();
  61. UpdateInfo();
  62. }
  63. //
  64. //server
  65. private void comboBoxServerServer_SelectedValueChanged(object sender, EventArgs e)
  66. {
  67. var comboBox = sender as ComboBox;
  68. var value = comboBox.SelectedItem as string;
  69. if (value == Res.Get("Forms,ServerWindow,comboBoxServer")) // pre-written server
  70. {
  71. textBoxCustomHostServer.Text = Res.Get("Forms,ServerWindow,comboBoxText");
  72. textBoxCustomHostServer.Enabled = false;
  73. }
  74. else // Custom
  75. {
  76. textBoxCustomHostServer.Enabled = true;
  77. textBoxCustomHostServer.Text = "";
  78. }
  79. }
  80. private void textBoxCustomHostServer_TextChanged(object sender, EventArgs e)
  81. {
  82. var textBox = sender as TextBox;
  83. if (!textBox.Enabled)
  84. return;
  85. var requestHost = textBox.Text;
  86. if (string.IsNullOrEmpty(requestHost))
  87. {
  88. }
  89. else
  90. {
  91. AuthService.Instance.Settings.BackendHost = requestHost;
  92. }
  93. }
  94. private void textBoxApiKeyServer_TextChanged(object sender, EventArgs e)
  95. {
  96. var textBox = sender as TextBox;
  97. var text = textBox.Text;
  98. if (text != null)
  99. {
  100. var apiKey = text.Trim();
  101. if (apiKey != "")
  102. AuthService.Instance.User.ApiKey = apiKey;
  103. }
  104. }
  105. private void Localize()
  106. {
  107. MyRes res = new MyRes("Forms,AccountWindow");
  108. this.Text = res.Get("");
  109. this.lbCaption.Text = res.Get("Title");
  110. this.btnSignIn.Text = res.Get("SignIn");
  111. this.lbNoAccount.Text = res.Get("NoAccount");
  112. this.lbCreateOne.Text = res.Get("Create");
  113. this.lbMessage.Text = res.Get("Intro") + res.Get("Testing");
  114. this.expiresFormat = res.Get("Expires");
  115. this.lbManage.Text = res.Get("Profile");
  116. this.lbSignOut.Text = res.Get("SignOut");
  117. this.lbText.Text = res.Get("Message");
  118. this.lbUpdate.Text = res.Get("SessionExpired");
  119. MyRes res2 = new MyRes("Forms,ServerWindow");
  120. this.label1Server.Text = res2.Get("Server");
  121. this.label2Server.Text = res2.Get("ApiKeyword");
  122. this.lbCaptionServer.Text = res2.Get("Caption");
  123. this.comboBoxServerServer.Items[0] = res2.Get("comboBoxServer");
  124. this.comboBoxServerServer.Items[1] = res2.Get("comboBoxCustom");
  125. this.tab2.Text = res2.Get("");
  126. }
  127. private void RightToLeftInternal(Control control)
  128. {
  129. if (control.HasChildren)
  130. {
  131. foreach (Control child in control.Controls)
  132. {
  133. child.Left = control.Width - child.Left - child.Width;
  134. RightToLeftInternal(child);
  135. }
  136. }
  137. }
  138. private Image RoundCorners(Image fromImage, float cornerRadius, Color backgroundColor)
  139. {
  140. cornerRadius *= 2;
  141. Bitmap result = new Bitmap(fromImage.Width, fromImage.Height);
  142. using (Graphics g = Graphics.FromImage(result))
  143. {
  144. g.Clear(backgroundColor);
  145. g.SmoothingMode = SmoothingMode.AntiAlias;
  146. using (Brush brush = new TextureBrush(fromImage))
  147. {
  148. using (GraphicsPath gp = new GraphicsPath())
  149. {
  150. gp.AddArc(0, 0, cornerRadius, cornerRadius, 180, 90);
  151. gp.AddArc(0 + result.Width - cornerRadius - 1, 0, cornerRadius, cornerRadius, 270, 90);
  152. gp.AddArc(0 + result.Width - cornerRadius - 1, 0 + result.Height - cornerRadius - 1, cornerRadius, cornerRadius, 0, 90);
  153. gp.AddArc(0, 0 + result.Height - cornerRadius, cornerRadius - 1, cornerRadius, 90, 90);
  154. g.FillPath(brush, gp);
  155. return result;
  156. }
  157. }
  158. }
  159. }
  160. private void RTL()
  161. {
  162. if (RightToLeftLayout)
  163. {
  164. RightToLeftInternal(this);
  165. }
  166. }
  167. private void UpdateInfo()
  168. {
  169. AuthService auth = AuthService.Instance;
  170. if (auth.User.IsAuthenticated)
  171. {
  172. BeforeSignIn(false);
  173. AfterSignIn(true);
  174. lbUsername.Text = auth.User.DisplayName;
  175. lbEmail.Text = auth.User.DisplayEmail;
  176. pbAvatar.Image = RoundCorners(auth.User.DisplayAvatar, 15, Color.Transparent);
  177. if (auth.CanRefresh)
  178. {
  179. if (auth.User.ExpiresIn < DateTime.Now)
  180. {
  181. lbTime.Visible = false;
  182. lbUpdate.Visible = true;
  183. }
  184. else
  185. {
  186. lbUpdate.Visible = false;
  187. lbTime.Visible = true;
  188. lbTime.Text = String.Format(expiresFormat, auth.User.ExpiresIn.ToShortTimeString());
  189. }
  190. }
  191. }
  192. else
  193. {
  194. this.SuspendLayout();
  195. BeforeSignIn(true);
  196. AfterSignIn(false);
  197. }
  198. }
  199. private void BeforeSignIn(bool visible)
  200. {
  201. this.tab1.SuspendLayout();
  202. lbMessage.Visible = visible;
  203. btnSignIn.Visible = visible;
  204. lbCreateOne.Visible = visible;
  205. lbNoAccount.Visible = visible;
  206. this.tab1.PerformLayout();
  207. }
  208. private void AfterSignIn(bool visible)
  209. {
  210. this.tab1.SuspendLayout();
  211. lbUpdate.Visible = visible;
  212. lbTime.Visible = visible;
  213. pbAvatar.Visible = visible;
  214. lbText.Visible = visible;
  215. lbSignOut.Visible = visible;
  216. lbManage.Visible = visible;
  217. lbUsername.Visible = visible;
  218. lbEmail.Visible = visible;
  219. tab1.ResumeLayout(true);
  220. }
  221. #endregion Private Methods
  222. }
  223. }