/// <summary> /// Delete file or move it to recycle bin, navigate to next pic /// and display information /// </summary> /// <param name="Recyclebin"></param> internal static void DeleteFile(bool Recyclebin) { if (!TryDeleteFile(Pics[FolderIndex], Recyclebin)) { ShowTooltipMessage(Application.Current.Resources["AnErrorOccuredWhenDeleting"] + Environment.NewLine + Pics[FolderIndex]); return; } // Sync with gallery if (UC.GetPicGallery != null) { UC.GetPicGallery.Container.Children.RemoveAt(Pics.IndexOf(Pics[FolderIndex])); } // Sync with preloader Preloader.Remove(Pics[FolderIndex]); Pics.Remove(Pics[FolderIndex]); if (Pics.Count <= 0) { Unload(); return; } Pic(false); ShowTooltipMessage(Recyclebin ? Application.Current.Resources["SentFileToRecycleBin"] : Application.Current.Resources["Deleted"]); }
//internal static int Count { get { return Sources.Count; } } /// <summary> /// Add file to prelader /// </summary> /// <param name="file">file path</param> internal static void Add(string file) { if (Contains(file)) { IsLoading = false; return; } IsLoading = true; var pic = ImageDecoder.RenderToBitmapSource(file); if (pic == null) { IsLoading = false; return; } if (!pic.IsFrozen) { pic.Freeze(); } Sources.TryAdd(file, pic); IsLoading = false; #if DEBUG Trace.WriteLine("Added = " + file + " to Preloader, index " + Pics.IndexOf(file)); #endif }
/// <summary> /// Removes the key, after checking if it exists /// </summary> /// <param name="key"></param> internal static void Remove(string key) { if (key == null) { #if DEBUG Trace.WriteLine("Preloader.Remove key null, " + key); #endif return; } if (!Contains(key)) { #if DEBUG Trace.WriteLine("Preloader.Remove does not contain " + key); #endif return; } _ = Sources[key]; #if DEBUG if (!Sources.TryRemove(key, out _)) { Trace.WriteLine($"Failed to Remove {key} from Preloader, index {Pics.IndexOf(key)}"); } #else Sources.TryRemove(key, out _); #endif }
/// <summary> /// Removes the key, after checking if it exists /// </summary> /// <param name="key"></param> internal static void Remove(string key) { if (key == null) { return; } if (!Contains(key)) { return; } _ = Sources[key]; #if DEBUG if (Sources.TryRemove(key, out _)) { Trace.WriteLine("Removed = " + key + " from Preloader, index " + Pics.IndexOf(key)); } else { Trace.WriteLine("Failed to Remove = " + key + " from Preloader, index " + Pics.IndexOf(key)); } #else Sources.TryRemove(key, out _); #endif //GC.Collect(); // Need to force this, else too high memory usage? }
internal static void Add(BitmapSource bmp, string key) { if (string.IsNullOrWhiteSpace(key)) { return; } if (Contains(key)) { return; } if (bmp == null) { return; } if (!bmp.IsFrozen) { bmp.Freeze(); } #if DEBUG if (Sources.TryAdd(key, bmp)) { Trace.WriteLine("Manually added = " + key + " to Preloader, index " + Pics.IndexOf(key)); } else { Trace.WriteLine("Preloader failed to add = " + key + " , index " + Pics.IndexOf(key)); } #else Sources.TryAdd(key, bmp); #endif }
public async Task <string> Vote(BoringPic boring, bool isLike) { var b = Pics; var msg = await _api.Vote(boring.PicID, isLike); if (string.IsNullOrEmpty(msg)) { return(null); } else if (msg.Contains("THANK YOU")) { if (isLike) { b[Pics.IndexOf(boring)].VotePositive++; } else { b[Pics.IndexOf(boring)].VoteNegative++; } } Pics = b; return(msg); }
/// <summary> /// Add file to prelader /// </summary> /// <param name="file">file path</param> internal static Task Add(string file) { return(Task.Run(() => { var pic = ImageDecoder.RenderToBitmapSource(file); #if DEBUG Trace.WriteLine($"Added {file} to Preloader, index {Pics.IndexOf(file)}"); #endif Sources.TryAdd(file, pic); })); }
/// <summary> /// Gets values and extracts archives /// </summary> /// <param name="path"></param> /// <returns></returns> internal static Task GetValues(string path) { return(Task.Run(() => { // Determine if archive to be extracted or not bool zipped = false; var extension = Path.GetExtension(path); extension = extension.ToLower(CultureInfo.CurrentCulture); switch (extension) { // Archives case ".zip": case ".7zip": case ".7z": case ".rar": case ".cbr": case ".cb7": case ".cbt": case ".cbz": case ".xz": case ".bzip2": case ".gzip": case ".tar": case ".wim": case ".iso": case ".cab": zipped = Extract(path); if (!zipped) { Pics = new List <string>(); FolderIndex = -1; } return; } // Set files to Pics and get index Pics = FileList(Path.GetDirectoryName(path)); if (Pics == null) { return; } FolderIndex = Pics.IndexOf(path); })); }
/// <summary> /// Loads a picture from a given file path and does extra error checking /// </summary> /// <param name="path"></param> internal static async void Pic(string path) { // Set Loading SetLoadingString(); // Handle if from web if (!File.Exists(path)) { if (Uri.IsWellFormedUriString(path, UriKind.Absolute)) { LoadFromWeb.PicWeb(path); return; } else if (Directory.Exists(path)) { ChangeFolder(true); await GetValues(path).ConfigureAwait(true); } else { Unload(); return; } } // If count not correct or just started, get values if (Pics.Count <= FolderIndex || FolderIndex < 0 || FreshStartup) { await GetValues(path).ConfigureAwait(true); } // If the file is in the same folder, navigate to it. If not, start manual loading procedure. else if (!string.IsNullOrWhiteSpace(Pics[FolderIndex]) && Path.GetDirectoryName(path) != Path.GetDirectoryName(Pics[FolderIndex])) { // Reset old values and get new ChangeFolder(true); await GetValues(path).ConfigureAwait(true); } FolderIndex = Pics.IndexOf(path); // Fix large archive extraction error if (FolderIndex == -1) { var recovery = await RecoverFailedArchiveAsync().ConfigureAwait(true); if (!recovery) { Reload(true); return; } else { LoadWindows.GetMainWindow.TitleText.Text = Application.Current.Resources["Unzipping"] as string; LoadWindows.GetMainWindow.TitleText.ToolTip = LoadWindows.GetMainWindow.TitleText.Text; FolderIndex = 0; } LoadWindows.GetMainWindow.Focus(); } if (!FreshStartup) { Preloader.Clear(); } #if DEBUG if (FreshStartup) { Trace.WriteLine("Pic(string path) entering Pic(int x)"); } #endif // Navigate to picture using obtained index Pic(FolderIndex); // Load new gallery values, if changing folder if (GetPicGallery != null && Properties.Settings.Default.PicGallery == 2 && !GalleryFunctions.IsLoading) { await GalleryLoad.Load().ConfigureAwait(false); } }
/// <summary> /// Loads a picture from a given file path and does extra error checking /// </summary> /// <param name="path"></param> internal static async Task LoadPiFrom(string path) { // Set Loading SetLoadingString(); // Handle if from web if (!File.Exists(path)) { if (Uri.IsWellFormedUriString(path, UriKind.Absolute)) { LoadFromWeb.PicWeb(path); return; } else if (Directory.Exists(path)) { ChangeFolder(true); await GetValues(path).ConfigureAwait(true); } else { Unload(); return; } } // If count not correct or just started, get values if (Pics.Count <= FolderIndex || FolderIndex < 0 || FreshStartup) { await GetValues(path).ConfigureAwait(true); } // If the file is in the same folder, navigate to it. If not, start manual loading procedure. else if (!string.IsNullOrWhiteSpace(Pics[FolderIndex]) && Path.GetDirectoryName(path) != Path.GetDirectoryName(Pics[FolderIndex])) { // Reset old values and get new ChangeFolder(true); await GetValues(path).ConfigureAwait(true); } FolderIndex = Pics.IndexOf(path); if (!FreshStartup) { Preloader.Clear(); } if (FolderIndex != -1) // if it is -1, it means it being extracted and need to wait for it instead { // Navigate to picture using obtained index await LoadPicAt(FolderIndex).ConfigureAwait(false); } await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Background, (Action)(async() => { // Load new gallery values, if changing folder if (GetPicGallery != null && Properties.Settings.Default.FullscreenGallery) { if (GetPicGallery.Container.Children.Count == 0) { await GalleryLoad.Load().ConfigureAwait(false); } } })); }
/// <summary> /// Loads a picture from a given file path and does extra error checking /// </summary> /// <param name="path"></param> internal static async void Pic(string path) { // Set Loading SetLoadingString(); // Handle if from web if (!File.Exists(path)) { if (Uri.IsWellFormedUriString(path, UriKind.Absolute)) { LoadFromWeb.PicWeb(path); return; } else if (Directory.Exists(path)) { ChangeFolder(true); await GetValues(path).ConfigureAwait(true); } else { Unload(); return; } } // If count not correct or just started, get values if (Pics.Count <= FolderIndex || FolderIndex < 0 || FreshStartup) { await GetValues(path).ConfigureAwait(true); } // If the file is in the same folder, navigate to it. If not, start manual loading procedure. else if (!string.IsNullOrWhiteSpace(Pics[FolderIndex]) && Path.GetDirectoryName(path) != Path.GetDirectoryName(Pics[FolderIndex])) { // Reset old values and get new ChangeFolder(true); await GetValues(path).ConfigureAwait(true); } FolderIndex = Pics.IndexOf(path); if (!FreshStartup) { Preloader.Clear(); } #if DEBUG if (FreshStartup) { Trace.WriteLine("Pic(string path) entering Pic(int x)"); } #endif // Navigate to picture using obtained index Pic(FolderIndex == -1 ? 0 : FolderIndex); // Load new gallery values, if changing folder if (GetPicGallery != null && Properties.Settings.Default.PicGallery == 2) { if (GetPicGallery.Container.Children.Count == 0) { await GalleryLoad.Load().ConfigureAwait(false); } } }
internal static void 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])) { Pic(Pics.IndexOf(x)); } else { Pic(x); } } else { Pic(x); } 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(), (string)Application.Current.Resources["ClipboardImage"]); return; } // text/string/adddress var s = Clipboard.GetText(TextDataFormat.Text); if (string.IsNullOrEmpty(s)) { return; } if (Base64.IsBase64String(s)) { Pic64(s); return; } if (FilePathHasInvalidChars(s)) { MakeValidFileName(s); } s = s.Replace("\"", ""); s = s.Trim(); if (File.Exists(s)) { Pic(s); } else if (Directory.Exists(s)) { ChangeFolder(); Pics = FileList(s); if (Pics.Count > 0) { Pic(Pics[0]); } else if (Pics.Count == 0) { Unload(); } else if (!string.IsNullOrWhiteSpace(Pics[FolderIndex])) { Pic(Pics[FolderIndex]); } else { Unload(); } } else if (Uri.IsWellFormedUriString(s, UriKind.Absolute)) // Check if from web { LoadFromWeb.PicWeb(s); } }
internal static void ChangeSorting(short sorting) { if (Properties.Settings.Default.SortPreference == sorting) { return; } Properties.Settings.Default.SortPreference = sorting; var tmp = Pics[FolderIndex]; if (!string.IsNullOrWhiteSpace(tmp)) { Pics = FileList(Path.GetDirectoryName(tmp)); Pic(Pics.IndexOf(tmp)); } var sortcm = cm.Items[6] as MenuItem; var sort0 = sortcm.Items[0] as MenuItem; var sort0Header = sort0.Header as RadioButton; var sort1 = sortcm.Items[1] as MenuItem; var sort1Header = sort1.Header as RadioButton; var sort2 = sortcm.Items[2] as MenuItem; var sort2Header = sort2.Header as RadioButton; var sort3 = sortcm.Items[3] as MenuItem; var sort3Header = sort3.Header as RadioButton; var sort4 = sortcm.Items[4] as MenuItem; var sort4Header = sort4.Header as RadioButton; var sort5 = sortcm.Items[5] as MenuItem; var sort5Header = sort5.Header as RadioButton; var sort6 = sortcm.Items[6] as MenuItem; var sort6Header = sort6.Header as RadioButton; switch (sorting) { default: case 0: sort0Header.IsChecked = true; sort1Header.IsChecked = false; sort2Header.IsChecked = false; sort3Header.IsChecked = false; sort4Header.IsChecked = false; sort5Header.IsChecked = false; sort6Header.IsChecked = false; break; case 1: sort0Header.IsChecked = false; sort1Header.IsChecked = true; sort2Header.IsChecked = false; sort3Header.IsChecked = false; sort4Header.IsChecked = false; sort5Header.IsChecked = false; sort6Header.IsChecked = false; break; case 2: sort0Header.IsChecked = false; sort1Header.IsChecked = false; sort2Header.IsChecked = true; sort3Header.IsChecked = false; sort4Header.IsChecked = false; sort5Header.IsChecked = false; sort6Header.IsChecked = false; break; case 3: sort0Header.IsChecked = false; sort1Header.IsChecked = false; sort2Header.IsChecked = false; sort3Header.IsChecked = true; sort4Header.IsChecked = false; sort5Header.IsChecked = false; sort6Header.IsChecked = false; break; case 4: sort0Header.IsChecked = false; sort1Header.IsChecked = false; sort2Header.IsChecked = false; sort3Header.IsChecked = false; sort4Header.IsChecked = true; sort5Header.IsChecked = false; sort6Header.IsChecked = false; break; case 5: sort0Header.IsChecked = false; sort1Header.IsChecked = false; sort2Header.IsChecked = false; sort3Header.IsChecked = false; sort4Header.IsChecked = false; sort5Header.IsChecked = true; sort6Header.IsChecked = false; break; case 6: sort0Header.IsChecked = false; sort1Header.IsChecked = false; sort2Header.IsChecked = false; sort3Header.IsChecked = false; sort4Header.IsChecked = false; sort5Header.IsChecked = false; sort6Header.IsChecked = true; break; } }
/// <summary> /// Loads a picture from a given file path and does extra error checking /// </summary> /// <param name="path"></param> internal static async void Pic(string path) { // Set Loading mainWindow.Title = mainWindow.Bar.Text = Loading; mainWindow.Bar.ToolTip = Loading; if (mainWindow.img.Source == null) { AjaxLoadingStart(); } // Handle if from web if (!File.Exists(path)) { if (Uri.IsWellFormedUriString(path, UriKind.Absolute)) { LoadFromWeb.PicWeb(path); } else { Unload(); } return; } // If count not correct or just started, get values if (Pics.Count <= FolderIndex || FolderIndex < 0 || freshStartup) { await GetValues(path).ConfigureAwait(true); } // If the file is in the same folder, navigate to it. If not, start manual loading procedure. else if (!string.IsNullOrWhiteSpace(Pics[FolderIndex]) && Path.GetDirectoryName(path) != Path.GetDirectoryName(Pics[FolderIndex])) { //// Reset zipped values //if (!string.IsNullOrWhiteSpace(TempZipPath)) //{ // DeleteTempFiles(); // TempZipPath = string.Empty; // RecentFiles.SetZipped(string.Empty, false); //} // Reset old values and get new ChangeFolder(true); await GetValues(path).ConfigureAwait(true); } // If no need to reset values, get index else if (Pics != null) { FolderIndex = Pics.IndexOf(path); } if (Pics != null) { // Fix large archive extraction error if (Pics.Count == 0) { var recovery = await RecoverFailedArchiveAsync().ConfigureAwait(true); if (!recovery) { if (sexyToolTip.Opacity == 0) { ToolTipStyle("Archive could not be processed"); } Reload(true); return; } mainWindow.Focus(); } } else { Reload(true); return; } if (File.Exists(Pics[FolderIndex])) { if (!freshStartup) { Preloader.Clear(); } // Navigate to picture using obtained index Pic(FolderIndex); } else { Reload(true); return; } // Load images for PicGallery if enabled if (Properties.Settings.Default.PicGallery > 0) { if (!PicGalleryLogic.IsLoading) { await PicGalleryLoad.Load().ConfigureAwait(true); } } prevPicResource = null; // Make sure to not waste memory }