You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

521 lines
17 KiB

1 year ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Globalization;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Management;
  10. using System.Net.Http;
  11. using System.Reflection;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. using System.Windows.Forms;
  15. using AxSHDocVw;
  16. using CDPShared;
  17. using CircleSDK.Model;
  18. using CircleViewer.Dialogs;
  19. using Newtonsoft.Json;
  20. using Syncfusion.Windows.Forms;
  21. using Syncfusion.Windows.Forms.PdfViewer;
  22. using WMPLib;
  23. namespace CircleViewer
  24. {
  25. public partial class Form1 : Form
  26. {
  27. private CDPWorker _cdp;
  28. private string _circleFile;
  29. private string _viewingFile = "";
  30. private string _outFile = "";
  31. private Boolean _bCurrentStatus = false;
  32. List<string> _toDelete = new List<string>();
  33. public Form1()
  34. {
  35. using (LogMethod.Log(MethodBase.GetCurrentMethod().ReflectedType.Name))
  36. {
  37. _cdp = new CDPWorker();
  38. string[] args = Environment.GetCommandLineArgs();
  39. for (int i = 0; i < args.Length; i++)
  40. MinLogging.LogIt($"args[{i}] == {args[i]}");
  41. if ((args.Length > 1) && (File.Exists(args[1])))
  42. _circleFile = args[1];
  43. InitializeComponent();
  44. // hide the borders on the viewers, they are only there so they show up in design view.
  45. pbImageViewer.BorderStyle = BorderStyle.None;
  46. pdfViewer.BorderStyle = BorderStyle.None;
  47. // the pdfViewer will attempt to build the Pdfium.dll on the fly and Windows isn't going to like that
  48. // switching the SyncFusion render
  49. pdfViewer.RenderingEngine = PdfRenderingEngine.SfPdf;
  50. pbLoading.Visible = true;
  51. //pbLoading.Dock = DockStyle.Fill;
  52. pbLoading.SizeMode = PictureBoxSizeMode.StretchImage;
  53. HideAllViewers();
  54. }
  55. }
  56. private void Form1_Load(object sender, EventArgs e)
  57. {
  58. }
  59. async void OnConnectionStatuChanged(Boolean bNewStatus)
  60. {
  61. if (bNewStatus)
  62. {
  63. tssConnectionStatus.Text = "Connected";
  64. if (string.IsNullOrEmpty(_viewingFile) && (!string.IsNullOrEmpty(_circleFile)))
  65. {
  66. ViewFileAsync(_circleFile);
  67. }
  68. else
  69. {
  70. tssConnectionStatus.Text = "Ready";
  71. pbLoading.Visible = false;
  72. #if DEBUG
  73. tssbSettings.Visible = true;
  74. #endif
  75. }
  76. }
  77. else
  78. tssConnectionStatus.Text = "Not connected";
  79. }
  80. private void tm_updateStatus_Tick(object sender, EventArgs e)
  81. {
  82. if (!_cdp.Ready)
  83. tssConnectionStatus.Text = _cdp.Status;
  84. if (_bCurrentStatus != _cdp.Ready)
  85. {
  86. _bCurrentStatus = _cdp.Ready;
  87. OnConnectionStatuChanged(_bCurrentStatus);
  88. OnReady();
  89. }
  90. }
  91. async void OnReady()
  92. {
  93. await LoadCircleList();
  94. if (string.IsNullOrEmpty(Properties.Settings.Default.DefaultCircleId))
  95. return;
  96. _cdp.DefaultCircleId = Properties.Settings.Default.DefaultCircleId;
  97. CircleInfo ci = await _cdp.GetCircle(Properties.Settings.Default.DefaultCircleId);
  98. if (ci == null)
  99. return;
  100. tsddCircle.Text = ci.CircleName;
  101. TopicInfo ti = await _cdp.GetTopic(ci.CircleId, Properties.Settings.Default.DefaultTopicId);
  102. if (ti == null)
  103. return;
  104. _cdp.DefaultTopicId = Properties.Settings.Default.DefaultTopicId;
  105. tsddTopic.Text = ti.TopicName;
  106. }
  107. private async Task LoadCircleList()
  108. {
  109. tsddCircle.DropDownItems.Clear();
  110. List<CircleInfo> lci = _cdp.Circles;
  111. if (lci == null) return;
  112. if (lci.Count == 0) return;
  113. tsddCircle.Visible = true; // we're a member of at least one circle, show the drop down.
  114. lci = lci.OrderBy(r => r.CircleName).ToList();
  115. foreach (CircleInfo info in lci)
  116. {
  117. ToolStripItem tsi = new ToolStripButton(info.CircleName);
  118. tsi.DisplayStyle = ToolStripItemDisplayStyle.Text;
  119. tsi.Tag = info;
  120. tsi.Click += async (o, args) =>
  121. {
  122. Properties.Settings.Default.DefaultCircleId = info.CircleId;
  123. Properties.Settings.Default.DefaultTopicId = "87654321-a314-4202-b959-c981a6bc3c24"; // default to the general topic on Circle change.
  124. Properties.Settings.Default.Save();
  125. tsddCircle.Text = info.CircleName;
  126. tsddTopic.Text = "General";
  127. LoadTopicList();
  128. // what was I thinking here?!
  129. //await SetDisplayName(info.CircleId, Environment.UserName);
  130. };
  131. tsddCircle.DropDownItems.Add(tsi);
  132. }
  133. }
  134. async void ViewFileAsync(string fileToLoad)
  135. {
  136. using (LogMethod.Log(MethodBase.GetCurrentMethod().ReflectedType.Name))
  137. {
  138. _viewingFile = fileToLoad;
  139. YellowFile yf = new YellowFile(_viewingFile);
  140. DecryptReply result = null;
  141. try
  142. {
  143. tssConnectionStatus.Text = "Decrypting contents";
  144. result = await _cdp.Decrypt(fileToLoad);
  145. if (result.Status.Result.GetValueOrDefault(false))
  146. {
  147. byte[] buf = Convert.FromBase64String(result.DecryptedData);
  148. string ext = Path.GetExtension(result.FileName);
  149. await Task.Run(async () =>
  150. {
  151. await DoTrackingStuff(yf, result.FileName, fileToLoad);
  152. });
  153. switch (ext.ToLower())
  154. {
  155. case ".gif":
  156. case ".jpeg":
  157. case ".jpg":
  158. case ".png":
  159. case ".bmp":
  160. ViewImage(buf);
  161. break;
  162. case ".mp4":
  163. ViewMedia(buf, ext);
  164. break;
  165. case ".pdf":
  166. default: // default to pdf
  167. ViewPdf(buf);
  168. break;
  169. }
  170. tssConnectionStatus.Text = "Ready";
  171. #if DEBUG
  172. tssbSettings.Visible = true;
  173. #endif
  174. }
  175. else
  176. {
  177. if (result.Status.ErrorCode == 2) // CircleConstants.ErrocCodes.CircleNotFound
  178. {
  179. await Task.Run(async () =>
  180. {
  181. await _cdp.TrackUnauthorizedAccess(yf, fileToLoad);
  182. });
  183. MessageBox.Show("You are not authorized to view this file.", "Circle for Data Protection");
  184. }
  185. else
  186. MessageBox.Show(result.Status.Message, "Circle for Data Protection");
  187. tssConnectionStatus.Text = "Unable to view secure contents";
  188. }
  189. }
  190. catch (Exception ex)
  191. {
  192. MessageBox.Show(result.Status.Message, "Circle for Data Protection");
  193. MinLogging.LogIt(ex.Message);
  194. }
  195. pbLoading.Visible = false;
  196. }
  197. }
  198. async Task DoTrackingStuff(YellowFile yf, string fileName, string sourcePath)
  199. {
  200. using (LogMethod.Log(MethodBase.GetCurrentMethod().ReflectedType.Name))
  201. {
  202. try
  203. {
  204. Dictionary<string, string> meta = new Dictionary<string, string>();
  205. meta[$"Full path"] = sourcePath;
  206. await _cdp.TrackAuthorizedAccess(yf, fileName, meta);
  207. }
  208. catch (Exception e)
  209. {
  210. MinLogging.LogIt(e.Message);
  211. }
  212. }
  213. }
  214. void HideAllViewers()
  215. {
  216. // hide the PDF Viewer
  217. pdfViewer.Visible = false;
  218. // hide the image viewer
  219. pbImageViewer.Visible = false;
  220. // hide the webbrowser
  221. wmpViewer.Visible = false;
  222. }
  223. void ViewImage(byte[] buf)
  224. {
  225. HideAllViewers();
  226. // show and configure the image viewer
  227. pbImageViewer.Visible = true;
  228. pbImageViewer.Dock = DockStyle.Fill;
  229. pbImageViewer.Image = Image.FromStream(new MemoryStream(buf));
  230. }
  231. void ViewMedia(byte[] buf, string extention)
  232. {
  233. HideAllViewers();
  234. // show and configure the media viewer
  235. string outPath = Path.GetTempFileName();
  236. outPath = Path.ChangeExtension(outPath, extention);
  237. File.WriteAllBytes(outPath, buf);
  238. wmpViewer.URL = outPath;
  239. wmpViewer.Visible = true;
  240. wmpViewer.Dock = DockStyle.Fill;
  241. }
  242. void ViewPdf(byte[] buf)
  243. {
  244. HideAllViewers();
  245. // show and configure the pdf viewer
  246. pdfViewer.Visible = true;
  247. pdfViewer.Dock = DockStyle.Fill;
  248. pdfViewer.Load(new MemoryStream(buf));
  249. }
  250. async Task<List<string>> GetFileList(string[] files)
  251. {
  252. List<string> retList = new List<string>();
  253. foreach (string f in files)
  254. {
  255. try
  256. {
  257. FileAttributes attr = File.GetAttributes(f);
  258. if (attr.HasFlag(FileAttributes.Directory))
  259. {
  260. retList.AddRange(Directory.GetFiles(f, "*.*", SearchOption.AllDirectories));
  261. }
  262. else
  263. retList.Add(f);
  264. }
  265. catch (Exception e)
  266. {
  267. continue;
  268. }
  269. }
  270. return retList;
  271. }
  272. private List<string> _fList = null;
  273. async void CommonDragOver(DragEventArgs e)
  274. {
  275. if (e.Data.GetDataPresent(DataFormats.FileDrop))
  276. {
  277. string[] files = e.Data.GetData(DataFormats.FileDrop) as string[]; // get all files dropped
  278. if ((files == null) | (files.Length == 0)) return;
  279. if (Path.GetExtension(files[0]).ToLower() == ".cir")
  280. {
  281. e.Effect = DragDropEffects.Link;
  282. return;
  283. }
  284. _fList = await GetFileList(files);
  285. foreach (string fPath in _fList)
  286. {
  287. string ext = Path.GetExtension(fPath).ToLower();
  288. if (string.IsNullOrEmpty(ext))
  289. continue;
  290. if (Constants.SupportedFiles.Contains(ext))
  291. {
  292. e.Effect = DragDropEffects.Copy;
  293. return;
  294. }
  295. }
  296. }
  297. _fList = null;
  298. e.Effect = DragDropEffects.None;
  299. }
  300. async void CommonDragDrop(DragEventArgs e)
  301. {
  302. if (e.Data.GetDataPresent(DataFormats.FileDrop))
  303. {
  304. string[] files = e.Data.GetData(DataFormats.FileDrop) as string[]; // get all files dropped
  305. if ((files == null) | (files.Length == 0)) return;
  306. if (Path.GetExtension(files[0].ToLower()) == ".cir")
  307. {
  308. ViewFileAsync(files[0]);
  309. return;
  310. }
  311. // _fList should still be good.
  312. await ProtectFiles(_fList);
  313. }
  314. }
  315. async Task ProtectFiles(List<string> lFiles)
  316. {
  317. List<string> toProcess = new List<string>();
  318. foreach (string f in _fList)
  319. {
  320. string ext = Path.GetExtension(f);
  321. if (Constants.SupportedFiles.Contains(ext.ToLower()))
  322. {
  323. toProcess.Add(f);
  324. }
  325. }
  326. MinLogging.LogIt($"About to protect {toProcess.Count} files.");
  327. ProtectCmd cmd = new ProtectCmd(_cdp);
  328. foreach (string f in toProcess)
  329. {
  330. await cmd.Process(f);
  331. }
  332. }
  333. private void pdfViewer_DragOver(object sender, DragEventArgs e)
  334. {
  335. CommonDragOver(e);
  336. }
  337. private void pbImageViewer_DragOver(object sender, DragEventArgs e)
  338. {
  339. CommonDragOver(e);
  340. }
  341. private void pdfViewer_DragDrop(object sender, DragEventArgs e)
  342. {
  343. CommonDragDrop(e);
  344. }
  345. private void pbImageViewer_DragDrop(object sender, DragEventArgs e)
  346. {
  347. CommonDragDrop(e);
  348. }
  349. private void encryptToolStripMenuItem_Click(object sender, EventArgs e)
  350. {
  351. SecureFileDlg dlg = new SecureFileDlg(_cdp);
  352. dlg.ShowDialog();
  353. }
  354. private void Form1_DragOver(object sender, DragEventArgs e)
  355. {
  356. CommonDragOver(e);
  357. }
  358. private void Form1_DragDrop(object sender, DragEventArgs e)
  359. {
  360. CommonDragDrop(e);
  361. }
  362. private void Form1_FormClosed(object sender, FormClosedEventArgs e)
  363. {
  364. wmpViewer.URL = "";
  365. foreach (var s in _toDelete)
  366. {
  367. try
  368. {
  369. File.Delete(s);
  370. }
  371. catch (Exception ex)
  372. {
  373. }
  374. }
  375. }
  376. private void protectFilesToolStripMenuItem_Click(object sender, EventArgs e)
  377. {
  378. SecureFileDlg dlg = new SecureFileDlg(_cdp);
  379. dlg.ShowDialog();
  380. }
  381. private void activityToolStripMenuItem_Click(object sender, EventArgs e)
  382. {
  383. ActivityDlgDefault dlg = new ActivityDlgDefault(_cdp);
  384. dlg.ShowDialog();
  385. }
  386. private async void LoadTopicList()
  387. {
  388. tsddTopic.DropDownItems.Clear();
  389. List<TopicInfo> lti = await _cdp.EnumTopics(Properties.Settings.Default.DefaultCircleId);
  390. if (lti == null) return;
  391. foreach (TopicInfo info in lti)
  392. {
  393. if ((info.IsPrivate.GetValueOrDefault(false)) || (info.TopicId == "87654321-a314-4202-b959-c981a6bc3c24"))
  394. {
  395. ToolStripItem tsi = new ToolStripButton(info.TopicName);
  396. tsi.Tag = info;
  397. tsi.DisplayStyle = ToolStripItemDisplayStyle.Text;
  398. tsi.Click += (o, args) =>
  399. {
  400. Properties.Settings.Default.DefaultTopicId = info.TopicId;
  401. Properties.Settings.Default.Save();
  402. tsddTopic.Text = info.TopicName;
  403. };
  404. tsddTopic.DropDownItems.Add(tsi);
  405. }
  406. }
  407. }
  408. private void createNewCircleToolStripMenuItem_Click(object sender, EventArgs e)
  409. {
  410. CreateCircleDlg dlg = new CreateCircleDlg(_cdp);
  411. dlg.ShowDialog();
  412. }
  413. private void joinCircleToolStripMenuItem_Click(object sender, EventArgs e)
  414. {
  415. JoinCircleDlg dlg = new JoinCircleDlg(_cdp);
  416. dlg.ShowDialog();
  417. }
  418. private void inviteMemberToolStripMenuItem1_Click(object sender, EventArgs e)
  419. {
  420. InviteMemberDlg dlg = new InviteMemberDlg(_cdp);
  421. dlg.ShowDialog();
  422. }
  423. private void revokeMemberToolStripMenuItem1_Click(object sender, EventArgs e)
  424. {
  425. RevokeMemberDlg dlg = new RevokeMemberDlg(_cdp);
  426. dlg.ShowDialog();
  427. }
  428. private void revokeDeviceToolStripMenuItem1_Click(object sender, EventArgs e)
  429. {
  430. RevokeDeviceDlg dlg = new RevokeDeviceDlg(_cdp);
  431. dlg.ShowDialog();
  432. }
  433. private void registerLicenseToolStripMenuItem_Click(object sender, EventArgs e)
  434. {
  435. }
  436. private void tssbSettings_ButtonClick(object sender, EventArgs e)
  437. {
  438. ActivityDlgDefault dlg = new ActivityDlgDefault(_cdp);
  439. dlg.ShowDialog();
  440. }
  441. }
  442. }