Attachments.xaml.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Input;
  8. using System.Windows.Media.Imaging;
  9. using InABox.Core;
  10. using InABox.WPF;
  11. using Microsoft.Win32;
  12. namespace InABox.DynamicGrid
  13. {
  14. /// <summary>
  15. /// Interaction logic for KanbanAttachments.xaml
  16. /// </summary>
  17. public partial class Attachments : UserControl
  18. {
  19. private readonly List<Tuple<string, BitmapImage, byte[], int>> Items = new();
  20. private readonly List<string> ViewedFiles = new();
  21. public Attachments(Type type, Guid reference)
  22. {
  23. InitializeComponent();
  24. FileList.Items.Clear();
  25. FileList.ItemsSource = Items;
  26. //for (int i = 0; i < files.Count(); i++)
  27. //{
  28. // var file = files[i];
  29. // Items.Add(new Tuple<string, BitmapImage, byte[], int>(file.FileName, GetBitmapImage(file.FileName), file.Data, i));
  30. //}
  31. }
  32. private BitmapImage GetBitmapImage(string filename)
  33. {
  34. var bmp = Properties.Resources.doc_misc;
  35. var ext = Path.GetExtension(filename).ToUpper();
  36. if (ext.Equals(".PDF"))
  37. bmp = Properties.Resources.doc_pdf;
  38. else if (ext.Equals(".DOC") || ext.Equals(".DOCX") || ext.Equals(".RTF"))
  39. bmp = Properties.Resources.doc_rtf;
  40. else if (ext.Equals(".XLS") || ext.Equals(".XLSX"))
  41. bmp = Properties.Resources.doc_xls;
  42. else if (ext.Equals(".TXT"))
  43. bmp = Properties.Resources.doc_txt;
  44. else if (ext.Equals(".BMP"))
  45. bmp = Properties.Resources.doc_bmp;
  46. else if (ext.Equals(".PNG"))
  47. bmp = Properties.Resources.doc_png;
  48. else if (ext.Equals(".JPG") || ext.Equals(".JPEG"))
  49. bmp = Properties.Resources.doc_jpg;
  50. return bmp.AsBitmapImage().Resize(64, 64);
  51. }
  52. // public void UnloadAttachments(List<MiscellaneousDataFile> files)
  53. // {
  54. // // First remove any deleted files
  55. // for (int i = files.Count - 1; i >= 0; i--)
  56. // {
  57. // if (!Items.Where(x => x.Item4.Equals(i)).Any())
  58. // files.RemoveAt(i);
  59. // }
  60. // // now add new files
  61. // foreach (var item in Items)
  62. // {
  63. // if (item.Item4.Equals(-1))
  64. // files.Add(new MiscellaneousDataFile() { FileName = item.Item1, Data = item.Item3 });
  65. // }
  66. //}
  67. ~Attachments()
  68. {
  69. foreach (var file in ViewedFiles)
  70. if (File.Exists(file))
  71. try
  72. {
  73. File.Delete(file);
  74. }
  75. catch (Exception e)
  76. {
  77. Logger.Send(LogType.Error, "", string.Format("*** Unknown Error: {0}\n{1}", e.Message, e.StackTrace));
  78. }
  79. }
  80. private void AddFile_Click(object sender, RoutedEventArgs e)
  81. {
  82. var dlg = new OpenFileDialog();
  83. dlg.Filter = ""; // new MiscellaneousDataFile().Filter();
  84. if (dlg.ShowDialog() == true)
  85. {
  86. var tuple = new Tuple<string, BitmapImage, byte[], int>(
  87. Path.GetFileName(dlg.FileName),
  88. GetBitmapImage(dlg.FileName),
  89. File.ReadAllBytes(dlg.FileName),
  90. -1
  91. );
  92. Items.Add(tuple);
  93. FileList.ItemsSource = null;
  94. FileList.ItemsSource = Items;
  95. }
  96. }
  97. private void ViewFile(object tag)
  98. {
  99. if (tag is Tuple<string, BitmapImage, byte[], int>)
  100. {
  101. var tuple = (Tuple<string, BitmapImage, byte[], int>)tag;
  102. var filename = Path.ChangeExtension(Path.GetTempFileName(), Path.GetExtension(tuple.Item1));
  103. File.WriteAllBytes(filename, tuple.Item3);
  104. ViewedFiles.Add(filename);
  105. var gsProcessInfo = new ProcessStartInfo();
  106. gsProcessInfo.Verb = "open";
  107. gsProcessInfo.WindowStyle = ProcessWindowStyle.Normal;
  108. gsProcessInfo.FileName = filename;
  109. gsProcessInfo.UseShellExecute = true;
  110. Process.Start(gsProcessInfo);
  111. }
  112. }
  113. private void ViewFile_Click(object sender, RoutedEventArgs e)
  114. {
  115. var tag = (sender as MenuItem).Tag;
  116. ViewFile(tag);
  117. }
  118. private void DeleteFile_Click(object sender, RoutedEventArgs e)
  119. {
  120. var tag = (sender as MenuItem).Tag;
  121. if (tag is Tuple<string, BitmapImage, byte[], int>)
  122. {
  123. var tuple = (Tuple<string, BitmapImage, byte[], int>)tag;
  124. if (MessageBox.Show("Are you sure you wish to delete this file?", "Confirm File Delete", MessageBoxButton.YesNo)
  125. .Equals(MessageBoxResult.Yes))
  126. {
  127. Items.Remove(tuple);
  128. FileList.ItemsSource = null;
  129. FileList.ItemsSource = Items;
  130. }
  131. }
  132. }
  133. private void Item_MouseDown(object sender, MouseButtonEventArgs e)
  134. {
  135. if (e.ChangedButton == MouseButton.Left && e.ClickCount == 2)
  136. {
  137. var tag = (sender as FrameworkElement).Tag;
  138. ViewFile(tag);
  139. }
  140. }
  141. }
  142. }