|
|
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Globalization; using System.IO; using System.Linq; using System.Management; using System.Net.Http; using System.Reflection; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using AxSHDocVw; using CDPShared; using CircleSDK.Model; using CircleViewer.Dialogs; using Newtonsoft.Json; using Syncfusion.Windows.Forms; using Syncfusion.Windows.Forms.PdfViewer; using WMPLib;
namespace CircleViewer { public partial class Form1 : Form { private CDPWorker _cdp;
private string _circleFile; private string _viewingFile = ""; private string _outFile = ""; private Boolean _bCurrentStatus = false;
List<string> _toDelete = new List<string>();
public Form1() { using (LogMethod.Log(MethodBase.GetCurrentMethod().ReflectedType.Name)) { _cdp = new CDPWorker();
string[] args = Environment.GetCommandLineArgs(); for (int i = 0; i < args.Length; i++) MinLogging.LogIt($"args[{i}] == {args[i]}");
if ((args.Length > 1) && (File.Exists(args[1]))) _circleFile = args[1];
InitializeComponent();
// hide the borders on the viewers, they are only there so they show up in design view.
pbImageViewer.BorderStyle = BorderStyle.None; pdfViewer.BorderStyle = BorderStyle.None;
// the pdfViewer will attempt to build the Pdfium.dll on the fly and Windows isn't going to like that
// switching the SyncFusion render
pdfViewer.RenderingEngine = PdfRenderingEngine.SfPdf;
pbLoading.Visible = true; //pbLoading.Dock = DockStyle.Fill;
pbLoading.SizeMode = PictureBoxSizeMode.StretchImage;
HideAllViewers(); } }
private void Form1_Load(object sender, EventArgs e) { }
async void OnConnectionStatuChanged(Boolean bNewStatus) { if (bNewStatus) { tssConnectionStatus.Text = "Connected"; if (string.IsNullOrEmpty(_viewingFile) && (!string.IsNullOrEmpty(_circleFile))) { ViewFileAsync(_circleFile); } else { tssConnectionStatus.Text = "Ready"; pbLoading.Visible = false; #if DEBUG
tssbSettings.Visible = true; #endif
} } else tssConnectionStatus.Text = "Not connected"; }
private void tm_updateStatus_Tick(object sender, EventArgs e) { if (!_cdp.Ready) tssConnectionStatus.Text = _cdp.Status;
if (_bCurrentStatus != _cdp.Ready) { _bCurrentStatus = _cdp.Ready; OnConnectionStatuChanged(_bCurrentStatus); OnReady(); } }
async void OnReady() { await LoadCircleList();
if (string.IsNullOrEmpty(Properties.Settings.Default.DefaultCircleId)) return;
_cdp.DefaultCircleId = Properties.Settings.Default.DefaultCircleId; CircleInfo ci = await _cdp.GetCircle(Properties.Settings.Default.DefaultCircleId); if (ci == null) return;
tsddCircle.Text = ci.CircleName;
TopicInfo ti = await _cdp.GetTopic(ci.CircleId, Properties.Settings.Default.DefaultTopicId); if (ti == null) return;
_cdp.DefaultTopicId = Properties.Settings.Default.DefaultTopicId;
tsddTopic.Text = ti.TopicName; } private async Task LoadCircleList() { tsddCircle.DropDownItems.Clear();
List<CircleInfo> lci = _cdp.Circles; if (lci == null) return; if (lci.Count == 0) return;
tsddCircle.Visible = true; // we're a member of at least one circle, show the drop down.
lci = lci.OrderBy(r => r.CircleName).ToList();
foreach (CircleInfo info in lci) { ToolStripItem tsi = new ToolStripButton(info.CircleName); tsi.DisplayStyle = ToolStripItemDisplayStyle.Text; tsi.Tag = info; tsi.Click += async (o, args) => { Properties.Settings.Default.DefaultCircleId = info.CircleId; Properties.Settings.Default.DefaultTopicId = "87654321-a314-4202-b959-c981a6bc3c24"; // default to the general topic on Circle change.
Properties.Settings.Default.Save(); tsddCircle.Text = info.CircleName; tsddTopic.Text = "General"; LoadTopicList(); // what was I thinking here?!
//await SetDisplayName(info.CircleId, Environment.UserName);
}; tsddCircle.DropDownItems.Add(tsi); } }
async void ViewFileAsync(string fileToLoad) { using (LogMethod.Log(MethodBase.GetCurrentMethod().ReflectedType.Name)) { _viewingFile = fileToLoad; YellowFile yf = new YellowFile(_viewingFile);
DecryptReply result = null; try { tssConnectionStatus.Text = "Decrypting contents";
result = await _cdp.Decrypt(fileToLoad); if (result.Status.Result.GetValueOrDefault(false)) { byte[] buf = Convert.FromBase64String(result.DecryptedData); string ext = Path.GetExtension(result.FileName);
await Task.Run(async () => { await DoTrackingStuff(yf, result.FileName, fileToLoad); });
switch (ext.ToLower()) { case ".gif": case ".jpeg": case ".jpg": case ".png": case ".bmp": ViewImage(buf); break; case ".mp4": ViewMedia(buf, ext); break; case ".pdf": default: // default to pdf
ViewPdf(buf); break; } tssConnectionStatus.Text = "Ready"; #if DEBUG
tssbSettings.Visible = true; #endif
} else { if (result.Status.ErrorCode == 2) // CircleConstants.ErrocCodes.CircleNotFound
{ await Task.Run(async () => { await _cdp.TrackUnauthorizedAccess(yf, fileToLoad); }); MessageBox.Show("You are not authorized to view this file.", "Circle for Data Protection"); } else MessageBox.Show(result.Status.Message, "Circle for Data Protection");
tssConnectionStatus.Text = "Unable to view secure contents"; } } catch (Exception ex) { MessageBox.Show(result.Status.Message, "Circle for Data Protection"); MinLogging.LogIt(ex.Message); } pbLoading.Visible = false; } }
async Task DoTrackingStuff(YellowFile yf, string fileName, string sourcePath) { using (LogMethod.Log(MethodBase.GetCurrentMethod().ReflectedType.Name)) { try { Dictionary<string, string> meta = new Dictionary<string, string>(); meta[$"Full path"] = sourcePath; await _cdp.TrackAuthorizedAccess(yf, fileName, meta); } catch (Exception e) { MinLogging.LogIt(e.Message); } } }
void HideAllViewers() { // hide the PDF Viewer
pdfViewer.Visible = false; // hide the image viewer
pbImageViewer.Visible = false; // hide the webbrowser
wmpViewer.Visible = false; }
void ViewImage(byte[] buf) { HideAllViewers();
// show and configure the image viewer
pbImageViewer.Visible = true; pbImageViewer.Dock = DockStyle.Fill; pbImageViewer.Image = Image.FromStream(new MemoryStream(buf)); } void ViewMedia(byte[] buf, string extention) { HideAllViewers();
// show and configure the media viewer
string outPath = Path.GetTempFileName(); outPath = Path.ChangeExtension(outPath, extention); File.WriteAllBytes(outPath, buf);
wmpViewer.URL = outPath;
wmpViewer.Visible = true; wmpViewer.Dock = DockStyle.Fill; }
void ViewPdf(byte[] buf) { HideAllViewers();
// show and configure the pdf viewer
pdfViewer.Visible = true; pdfViewer.Dock = DockStyle.Fill; pdfViewer.Load(new MemoryStream(buf)); }
async Task<List<string>> GetFileList(string[] files) { List<string> retList = new List<string>(); foreach (string f in files) { try { FileAttributes attr = File.GetAttributes(f); if (attr.HasFlag(FileAttributes.Directory)) { retList.AddRange(Directory.GetFiles(f, "*.*", SearchOption.AllDirectories)); } else retList.Add(f); } catch (Exception e) { continue; } }
return retList; }
private List<string> _fList = null; async void CommonDragOver(DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats.FileDrop)) { string[] files = e.Data.GetData(DataFormats.FileDrop) as string[]; // get all files dropped
if ((files == null) | (files.Length == 0)) return; if (Path.GetExtension(files[0]).ToLower() == ".cir") { e.Effect = DragDropEffects.Link; return; }
_fList = await GetFileList(files); foreach (string fPath in _fList) { string ext = Path.GetExtension(fPath).ToLower(); if (string.IsNullOrEmpty(ext)) continue;
if (Constants.SupportedFiles.Contains(ext)) { e.Effect = DragDropEffects.Copy; return; } } }
_fList = null;
e.Effect = DragDropEffects.None; }
async void CommonDragDrop(DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats.FileDrop)) { string[] files = e.Data.GetData(DataFormats.FileDrop) as string[]; // get all files dropped
if ((files == null) | (files.Length == 0)) return;
if (Path.GetExtension(files[0].ToLower()) == ".cir") { ViewFileAsync(files[0]); return; }
// _fList should still be good.
await ProtectFiles(_fList); } }
async Task ProtectFiles(List<string> lFiles) { List<string> toProcess = new List<string>(); foreach (string f in _fList) { string ext = Path.GetExtension(f);
if (Constants.SupportedFiles.Contains(ext.ToLower())) { toProcess.Add(f); } } MinLogging.LogIt($"About to protect {toProcess.Count} files.");
ProtectCmd cmd = new ProtectCmd(_cdp); foreach (string f in toProcess) { await cmd.Process(f); } }
private void pdfViewer_DragOver(object sender, DragEventArgs e) { CommonDragOver(e); }
private void pbImageViewer_DragOver(object sender, DragEventArgs e) { CommonDragOver(e); }
private void pdfViewer_DragDrop(object sender, DragEventArgs e) { CommonDragDrop(e); }
private void pbImageViewer_DragDrop(object sender, DragEventArgs e) { CommonDragDrop(e); }
private void encryptToolStripMenuItem_Click(object sender, EventArgs e) { SecureFileDlg dlg = new SecureFileDlg(_cdp); dlg.ShowDialog(); }
private void Form1_DragOver(object sender, DragEventArgs e) { CommonDragOver(e); }
private void Form1_DragDrop(object sender, DragEventArgs e) { CommonDragDrop(e); }
private void Form1_FormClosed(object sender, FormClosedEventArgs e) { wmpViewer.URL = ""; foreach (var s in _toDelete) { try { File.Delete(s); } catch (Exception ex) {
} } }
private void protectFilesToolStripMenuItem_Click(object sender, EventArgs e) { SecureFileDlg dlg = new SecureFileDlg(_cdp); dlg.ShowDialog(); }
private void activityToolStripMenuItem_Click(object sender, EventArgs e) { ActivityDlgDefault dlg = new ActivityDlgDefault(_cdp); dlg.ShowDialog(); }
private async void LoadTopicList() { tsddTopic.DropDownItems.Clear(); List<TopicInfo> lti = await _cdp.EnumTopics(Properties.Settings.Default.DefaultCircleId); if (lti == null) return;
foreach (TopicInfo info in lti) { if ((info.IsPrivate.GetValueOrDefault(false)) || (info.TopicId == "87654321-a314-4202-b959-c981a6bc3c24")) { ToolStripItem tsi = new ToolStripButton(info.TopicName); tsi.Tag = info; tsi.DisplayStyle = ToolStripItemDisplayStyle.Text; tsi.Click += (o, args) => { Properties.Settings.Default.DefaultTopicId = info.TopicId; Properties.Settings.Default.Save(); tsddTopic.Text = info.TopicName; }; tsddTopic.DropDownItems.Add(tsi); } }
}
private void createNewCircleToolStripMenuItem_Click(object sender, EventArgs e) { CreateCircleDlg dlg = new CreateCircleDlg(_cdp); dlg.ShowDialog(); }
private void joinCircleToolStripMenuItem_Click(object sender, EventArgs e) { JoinCircleDlg dlg = new JoinCircleDlg(_cdp); dlg.ShowDialog(); }
private void inviteMemberToolStripMenuItem1_Click(object sender, EventArgs e) { InviteMemberDlg dlg = new InviteMemberDlg(_cdp); dlg.ShowDialog(); }
private void revokeMemberToolStripMenuItem1_Click(object sender, EventArgs e) { RevokeMemberDlg dlg = new RevokeMemberDlg(_cdp); dlg.ShowDialog(); }
private void revokeDeviceToolStripMenuItem1_Click(object sender, EventArgs e) { RevokeDeviceDlg dlg = new RevokeDeviceDlg(_cdp); dlg.ShowDialog(); }
private void registerLicenseToolStripMenuItem_Click(object sender, EventArgs e) {
}
private void tssbSettings_ButtonClick(object sender, EventArgs e) { ActivityDlgDefault dlg = new ActivityDlgDefault(_cdp); dlg.ShowDialog(); } } }
|