Copy-paste.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. using PicView.ChangeImage;
  2. using PicView.ImageHandling;
  3. using System;
  4. using System.Diagnostics;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Reflection;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Media.Imaging;
  11. using static PicView.ChangeImage.Error_Handling;
  12. using static PicView.ChangeImage.Navigation;
  13. using static PicView.FileHandling.FileFunctions;
  14. using static PicView.FileHandling.FileLists;
  15. using static PicView.Library.Fields;
  16. using static PicView.UI.Tooltip;
  17. namespace PicView.FileHandling
  18. {
  19. internal static class Copy_Paste
  20. {
  21. /// <summary>
  22. /// Copy image location to clipboard
  23. /// </summary>
  24. internal static void CopyText()
  25. {
  26. Clipboard.SetText(Pics[FolderIndex]);
  27. ShowTooltipMessage(TxtCopy);
  28. }
  29. /// <summary>
  30. /// Add file to clipboard
  31. /// </summary>
  32. internal static void Copyfile()
  33. {
  34. if (Pics == null)
  35. {
  36. return;
  37. }
  38. if (Pics.Count == 0)
  39. {
  40. CopyBitmap();
  41. return;
  42. }
  43. // Copy pic if from web
  44. if (string.IsNullOrWhiteSpace(Pics[FolderIndex]) || Uri.IsWellFormedUriString(Pics[FolderIndex], UriKind.Absolute))
  45. {
  46. CopyBitmap();
  47. }
  48. else
  49. {
  50. Copyfile(Pics[FolderIndex]);
  51. }
  52. }
  53. /// <summary>
  54. /// Add file to clipboard
  55. /// </summary>
  56. internal static void Copyfile(string path)
  57. {
  58. var paths = new System.Collections.Specialized.StringCollection { path };
  59. Clipboard.SetFileDropList(paths);
  60. ShowTooltipMessage(FileCopy);
  61. }
  62. internal static void CopyBitmap()
  63. {
  64. if (Pics.Count == 0 && mainWindow.img.Source != null)
  65. {
  66. Clipboard.SetImage((BitmapSource)mainWindow.img.Source);
  67. }
  68. else if (Preloader.Contains(Pics[FolderIndex]))
  69. {
  70. Clipboard.SetImage(Preloader.Load(Pics[FolderIndex]));
  71. }
  72. else if (mainWindow.img.Source != null)
  73. {
  74. Clipboard.SetImage((BitmapSource)mainWindow.img.Source);
  75. }
  76. else
  77. {
  78. return;
  79. }
  80. ShowTooltipMessage("Copied Image to clipboard");
  81. }
  82. /// <summary>
  83. /// Retrieves the data from the clipboard and attemps to load image, if possible
  84. /// </summary>
  85. [System.Diagnostics.CodeAnalysis.SuppressMessage("Globalization", "CA1307:Specify StringComparison", Justification = "<Pending>")]
  86. internal static async Task Paste()
  87. {
  88. // file
  89. if (Clipboard.ContainsFileDropList()) // If Clipboard has one or more files
  90. {
  91. var files = Clipboard.GetFileDropList().Cast<string>().ToArray();
  92. if (files != null)
  93. {
  94. var x = files[0];
  95. if (Pics.Count != 0)
  96. {
  97. // If from same folder
  98. if (!string.IsNullOrWhiteSpace(Pics[FolderIndex]) && Path.GetDirectoryName(x) == Path.GetDirectoryName(Pics[FolderIndex]))
  99. {
  100. await Pic(Pics.IndexOf(x)).ConfigureAwait(false);
  101. }
  102. else
  103. {
  104. await Pic(x).ConfigureAwait(false);
  105. }
  106. }
  107. else
  108. {
  109. await Pic(x).ConfigureAwait(false);
  110. }
  111. if (files.Length > 1)
  112. {
  113. for (int i = 1; i < files.Length; i++)
  114. {
  115. using var n = new Process();
  116. n.StartInfo.FileName = Assembly.GetExecutingAssembly().Location;
  117. n.StartInfo.Arguments = files[i];
  118. n.Start();
  119. }
  120. }
  121. return;
  122. }
  123. }
  124. // Clipboard Image
  125. if (Clipboard.ContainsImage())
  126. {
  127. Pic(Clipboard.GetImage(), "Clipboard Image");
  128. return;
  129. }
  130. // text/string/adddress
  131. var s = Clipboard.GetText(TextDataFormat.Text);
  132. if (string.IsNullOrEmpty(s))
  133. {
  134. return;
  135. }
  136. if (Base64.IsBase64String(s))
  137. {
  138. await Pic64(s).ConfigureAwait(false);
  139. return;
  140. }
  141. if (FilePathHasInvalidChars(s))
  142. {
  143. MakeValidFileName(s);
  144. }
  145. s = s.Replace("\"", "");
  146. s = s.Trim();
  147. if (File.Exists(s))
  148. {
  149. await Pic(s).ConfigureAwait(false);
  150. }
  151. else if (Directory.Exists(s))
  152. {
  153. ChangeFolder();
  154. Pics = FileList(s);
  155. if (Pics.Count > 0)
  156. {
  157. await Pic(Pics[0]).ConfigureAwait(false);
  158. }
  159. else if (Pics.Count == 0)
  160. {
  161. Unload();
  162. }
  163. else if (!string.IsNullOrWhiteSpace(Pics[FolderIndex]))
  164. {
  165. await Pic(Pics[FolderIndex]).ConfigureAwait(false);
  166. }
  167. else
  168. {
  169. Unload();
  170. }
  171. }
  172. else if (Uri.IsWellFormedUriString(s, UriKind.Absolute)) // Check if from web
  173. {
  174. await LoadFromWeb.PicWeb(s).ConfigureAwait(false);
  175. }
  176. else
  177. {
  178. ShowTooltipMessage("An error occured while trying to paste file");
  179. }
  180. }
  181. /// <summary>
  182. /// Add file to move/paste clipboard
  183. /// </summary>
  184. /// <param name="path"></param>
  185. internal static void Cut(string path)
  186. {
  187. var x = new System.Collections.Specialized.StringCollection
  188. {
  189. path
  190. };
  191. byte[] moveEffect = new byte[] { 2, 0, 0, 0 };
  192. using (var dropEffect = new MemoryStream())
  193. {
  194. dropEffect.Write(moveEffect, 0, moveEffect.Length);
  195. DataObject data = new DataObject();
  196. data.SetFileDropList(x);
  197. data.SetData("Preferred DropEffect", dropEffect);
  198. Clipboard.Clear();
  199. Clipboard.SetDataObject(data, true);
  200. }
  201. // Force Preloader to add new images, to minimize slowdown errors
  202. PreloadCount = 4;
  203. ShowTooltipMessage(ImageCut);
  204. }
  205. }
  206. }