123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- using PicView.ChangeImage;
- using PicView.ImageHandling;
- using System;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- using System.Reflection;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Media.Imaging;
- using static PicView.ChangeImage.Error_Handling;
- using static PicView.ChangeImage.Navigation;
- using static PicView.FileHandling.FileFunctions;
- using static PicView.FileHandling.FileLists;
- using static PicView.Library.Fields;
- using static PicView.UI.Tooltip;
- namespace PicView.FileHandling
- {
- internal static class Copy_Paste
- {
- /// <summary>
- /// Copy image location to clipboard
- /// </summary>
- internal static void CopyText()
- {
- Clipboard.SetText(Pics[FolderIndex]);
- ShowTooltipMessage(TxtCopy);
- }
- /// <summary>
- /// Add file to clipboard
- /// </summary>
- internal static void Copyfile()
- {
- if (Pics == null)
- {
- return;
- }
- if (Pics.Count == 0)
- {
- CopyBitmap();
- return;
- }
- // Copy pic if from web
- if (string.IsNullOrWhiteSpace(Pics[FolderIndex]) || Uri.IsWellFormedUriString(Pics[FolderIndex], UriKind.Absolute))
- {
- CopyBitmap();
- }
- else
- {
- Copyfile(Pics[FolderIndex]);
- }
- }
- /// <summary>
- /// Add file to clipboard
- /// </summary>
- internal static void Copyfile(string path)
- {
- var paths = new System.Collections.Specialized.StringCollection { path };
- Clipboard.SetFileDropList(paths);
- ShowTooltipMessage(FileCopy);
- }
- internal static void CopyBitmap()
- {
- if (Pics.Count == 0 && mainWindow.img.Source != null)
- {
- Clipboard.SetImage((BitmapSource)mainWindow.img.Source);
- }
- else if (Preloader.Contains(Pics[FolderIndex]))
- {
- Clipboard.SetImage(Preloader.Load(Pics[FolderIndex]));
- }
- else if (mainWindow.img.Source != null)
- {
- Clipboard.SetImage((BitmapSource)mainWindow.img.Source);
- }
- else
- {
- return;
- }
- ShowTooltipMessage("Copied Image to clipboard");
- }
- /// <summary>
- /// Retrieves the data from the clipboard and attemps to load image, if possible
- /// </summary>
- [System.Diagnostics.CodeAnalysis.SuppressMessage("Globalization", "CA1307:Specify StringComparison", Justification = "<Pending>")]
- internal static async Task Paste()
- {
- // file
- if (Clipboard.ContainsFileDropList()) // If Clipboard has one or more files
- {
- var files = Clipboard.GetFileDropList().Cast<string>().ToArray();
- if (files != null)
- {
- var x = files[0];
- if (Pics.Count != 0)
- {
- // If from same folder
- if (!string.IsNullOrWhiteSpace(Pics[FolderIndex]) && Path.GetDirectoryName(x) == Path.GetDirectoryName(Pics[FolderIndex]))
- {
- await Pic(Pics.IndexOf(x)).ConfigureAwait(false);
- }
- else
- {
- await Pic(x).ConfigureAwait(false);
- }
- }
- else
- {
- await Pic(x).ConfigureAwait(false);
- }
- if (files.Length > 1)
- {
- for (int i = 1; i < files.Length; i++)
- {
- using var n = new Process();
- n.StartInfo.FileName = Assembly.GetExecutingAssembly().Location;
- n.StartInfo.Arguments = files[i];
- n.Start();
- }
- }
- return;
- }
- }
- // Clipboard Image
- if (Clipboard.ContainsImage())
- {
- Pic(Clipboard.GetImage(), "Clipboard Image");
- return;
- }
- // text/string/adddress
- var s = Clipboard.GetText(TextDataFormat.Text);
- if (string.IsNullOrEmpty(s))
- {
- return;
- }
- if (Base64.IsBase64String(s))
- {
- await Pic64(s).ConfigureAwait(false);
- return;
- }
- if (FilePathHasInvalidChars(s))
- {
- MakeValidFileName(s);
- }
- s = s.Replace("\"", "");
- s = s.Trim();
- if (File.Exists(s))
- {
- await Pic(s).ConfigureAwait(false);
- }
- else if (Directory.Exists(s))
- {
- ChangeFolder();
- Pics = FileList(s);
- if (Pics.Count > 0)
- {
- await Pic(Pics[0]).ConfigureAwait(false);
- }
- else if (Pics.Count == 0)
- {
- Unload();
- }
- else if (!string.IsNullOrWhiteSpace(Pics[FolderIndex]))
- {
- await Pic(Pics[FolderIndex]).ConfigureAwait(false);
- }
- else
- {
- Unload();
- }
- }
- else if (Uri.IsWellFormedUriString(s, UriKind.Absolute)) // Check if from web
- {
- await LoadFromWeb.PicWeb(s).ConfigureAwait(false);
- }
- else
- {
- ShowTooltipMessage("An error occured while trying to paste file");
- }
- }
- /// <summary>
- /// Add file to move/paste clipboard
- /// </summary>
- /// <param name="path"></param>
- internal static void Cut(string path)
- {
- var x = new System.Collections.Specialized.StringCollection
- {
- path
- };
- byte[] moveEffect = new byte[] { 2, 0, 0, 0 };
- using (var dropEffect = new MemoryStream())
- {
- dropEffect.Write(moveEffect, 0, moveEffect.Length);
- DataObject data = new DataObject();
- data.SetFileDropList(x);
- data.SetData("Preferred DropEffect", dropEffect);
- Clipboard.Clear();
- Clipboard.SetDataObject(data, true);
- }
- // Force Preloader to add new images, to minimize slowdown errors
- PreloadCount = 4;
- ShowTooltipMessage(ImageCut);
- }
- }
- }
|