S3ClientForm.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Windows.Forms;
  4. using FastReport.Export;
  5. using FastReport.Utils;
  6. namespace FastReport.Cloud.StorageClient.S3
  7. {
  8. /// <summary>
  9. /// Represents form of SkyDrive storage client.
  10. /// </summary>
  11. public partial class S3ClientForm : CloudStorageClientForm
  12. {
  13. #region Fields
  14. private S3StorageClient client;
  15. private List<ExportBase> exports;
  16. private Report report;
  17. #endregion // Fields
  18. #region Properties
  19. public S3StorageClient Client
  20. {
  21. get { return client; }
  22. }
  23. #endregion // Properties
  24. #region Constructors
  25. /// <summary>
  26. /// Initializes a new instance of the <see cref="S3ClientForm"/> class.
  27. /// </summary>
  28. /// <param name="report">The report template.</param>
  29. public S3ClientForm(string accessKeyId, string secretAccessKey, string region, string host, Report report) : base(report)
  30. {
  31. client = new S3StorageClient();
  32. client.InitSigner(accessKeyId, secretAccessKey, region);
  33. client.Host = host;
  34. this.report = report;
  35. InitializeComponent();
  36. Localize();
  37. Init();
  38. UIUtils.CheckRTL(this);
  39. UpdateDpiDependencies();
  40. }
  41. #endregion // Constructors
  42. #region Private Methods
  43. private void Init()
  44. {
  45. exports = new List<ExportBase>();
  46. List<ObjectInfo> list = new List<ObjectInfo>();
  47. RegisteredObjects.Exports.EnumItems(list);
  48. cbFileType.Items.Add(Res.Get("Preview,SaveNative"));
  49. exports.Add(null);
  50. foreach (ObjectInfo info in list)
  51. {
  52. if (info.Object != null)
  53. {
  54. cbFileType.Items.Add(Res.TryGet(info.Text));
  55. exports.Add(Activator.CreateInstance(info.Object) as ExportBase);
  56. }
  57. }
  58. cbFileType.SelectedIndex = 0;
  59. tbFileName.Text = report.GetReportName;
  60. XmlItem xi = Config.Root.FindItem("S3").FindItem("StorageSettings");
  61. tbServer.Text = xi.GetProp("Server");
  62. tbPort.Text = xi.GetProp("Port");
  63. tbUsername.Text = xi.GetProp("Username");
  64. tbPassword.Text = xi.GetProp("Password");
  65. }
  66. protected override void OnShown(EventArgs e)
  67. {
  68. try
  69. {
  70. cbBucket.Items.AddRange(client.GetListBuckets().ToArray());
  71. if (cbBucket.Items.Count == 0)
  72. {
  73. MessageBox.Show(Res.Get("Cloud,S3,MissingBuckets"), Res.Get("Messages,Error"), MessageBoxButtons.OK, MessageBoxIcon.Error);
  74. Close();
  75. }
  76. cbBucket.SelectedIndex = 0;
  77. }
  78. catch (Exception ex)
  79. {
  80. MessageBox.Show(ex.Message, Res.Get("Messages,Error"), MessageBoxButtons.OK, MessageBoxIcon.Error);
  81. Close();
  82. }
  83. base.OnShown(e);
  84. }
  85. private bool IsNumeric(string str)
  86. {
  87. if (!String.IsNullOrEmpty(str))
  88. {
  89. try
  90. {
  91. Convert.ToInt32(str);
  92. }
  93. catch
  94. {
  95. return false;
  96. }
  97. }
  98. return true;
  99. }
  100. private CloudProxySettings GetProxySettings()
  101. {
  102. CloudProxySettings proxySettings = null;
  103. if (!String.IsNullOrEmpty(tbServer.Text))
  104. {
  105. int port = 0;
  106. if (!IsNumeric(tbPort.Text))
  107. {
  108. FRMessageBox.Error(Res.Get("Cloud,CloudStorage,PortError"));
  109. }
  110. else
  111. {
  112. port = Convert.ToInt32(tbPort.Text);
  113. }
  114. proxySettings = new CloudProxySettings(ProxyType.Http, tbServer.Text, port, tbUsername.Text, tbPassword.Text);
  115. }
  116. return proxySettings;
  117. }
  118. private bool Done()
  119. {
  120. if (!String.IsNullOrEmpty(tbPort.Text))
  121. {
  122. if (!IsNumeric(tbPort.Text))
  123. {
  124. FRMessageBox.Error(Res.Get("Cloud,CloudStorage,PortError"));
  125. tbPort.Focus();
  126. return false;
  127. }
  128. }
  129. XmlItem xi = Config.Root.FindItem("S3").FindItem("StorageSettings");
  130. xi.SetProp("Server", tbServer.Text);
  131. xi.SetProp("Port", tbPort.Text);
  132. xi.SetProp("Username", tbUsername.Text);
  133. xi.SetProp("Password", tbPassword.Text);
  134. return true;
  135. }
  136. #endregion // Private Methods
  137. #region Public Methods
  138. /// <inheritdoc/>
  139. public override void Localize()
  140. {
  141. base.Localize();
  142. MyRes res = new MyRes("Cloud,CloudStorage");
  143. pgFile.Text = res.Get("File");
  144. pgProxy.Text = res.Get("Proxy");
  145. labelFileType.Text = res.Get("FileType");
  146. buttonSettings.Text = res.Get("Settings");
  147. labelServer.Text = res.Get("Server");
  148. labelUsername.Text = res.Get("Username");
  149. labelPassword.Text = res.Get("Password");
  150. this.Text = Res.Get("Cloud,S3");
  151. lbBucket.Text = Res.Get("Cloud,S3,Bucket");
  152. lbFileName.Text = Res.Get("Cloud,S3,FileName");
  153. }
  154. #endregion // Public Methods
  155. #region Events Handlers
  156. private void cbFileType_SelectedIndexChanged(object sender, EventArgs e)
  157. {
  158. buttonSettings.Enabled = cbFileType.SelectedIndex != 0;
  159. }
  160. private void buttonSettings_Click(object sender, EventArgs e)
  161. {
  162. ExportBase export = exports[cbFileType.SelectedIndex];
  163. export.SetReport(report);
  164. export.ShowDialog();
  165. }
  166. private void S3ClientForm_FormClosing(object sender, FormClosingEventArgs e)
  167. {
  168. if (DialogResult == DialogResult.OK)
  169. {
  170. if (!Done())
  171. {
  172. e.Cancel = true;
  173. }
  174. }
  175. }
  176. private void btnOk_Click(object sender, EventArgs e)
  177. {
  178. if (cbFileType.SelectedIndex == 0)
  179. client.FileName = tbFileName.Text + ".fpx";
  180. else
  181. client.FileName = tbFileName.Text + exports[cbFileType.SelectedIndex].GetFileExtension();
  182. client.CurrentBucket = cbBucket.SelectedItem.ToString();
  183. Cursor = Cursors.WaitCursor;
  184. client.ProxySettings = GetProxySettings();
  185. try
  186. {
  187. client.SaveReport(report, exports[cbFileType.SelectedIndex]);
  188. }
  189. catch (CloudStorageException ex)
  190. {
  191. MessageBox.Show(ex.Message, Res.Get("Messages,Error"), MessageBoxButtons.OK, MessageBoxIcon.Error);
  192. return;
  193. }
  194. DialogResult = DialogResult.OK;
  195. Close();
  196. }
  197. #endregion // Events Handlers
  198. }
  199. }