/// <summary> /// Deletes file or send it to recycle bin /// </summary> /// <param name="file"></param> /// <param name="Recycle"></param> /// <returns></returns> internal static bool TryDeleteFile(string file, bool Recycle) { /// Need to add function to remove from PicGallery if (!File.Exists(file)) { return(false); } try { var recycle = Recycle ? RecycleOption.SendToRecycleBin : RecycleOption.DeletePermanently; FileSystem.DeleteFile(file, UIOption.OnlyErrorDialogs, recycle); Pics.Remove(file); } #if DEBUG catch (Exception e) { Trace.WriteLine("Delete exception \n" + e.Message); return(false); } #else catch (Exception) { return(false); } #endif return(true); }
/// <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(string file, bool Recyclebin) { if (TryDeleteFile(file, Recyclebin)) { var filename = Path.GetFileName(file); Pics.Remove(filename); filename = filename.Length >= 25 ? Shorten(filename, 21) : filename; ToolTipStyle(Recyclebin ? "Sent " + filename + " to the recyle bin" : "Deleted " + filename); PreloadCount = reverse ? PreloadCount - 1 : PreloadCount + 1; // Go to next image if (!reverse) { Pic(FolderIndex); } else if (FolderIndex - 2 >= 0) { Pic(FolderIndex - 2); } else { Unload(); } } else { ToolTipStyle("An error occured when deleting " + file); } }
internal static void HandleRename() { if (FileFunctions.RenameFile(Pics[FolderIndex], ConfigureWindows.GetMainWindow.TitleText.Text)) { // Check if the file is not in the same folder if (Path.GetDirectoryName(ConfigureWindows.GetMainWindow.TitleText.Text) != Path.GetDirectoryName(Pics[FolderIndex])) { Pics.Remove(Pics[FolderIndex]); if (Pics.Count > 0) { Pic(); } else { Pic(ConfigureWindows.GetMainWindow.TitleText.Text); } } else { Pics[FolderIndex] = ConfigureWindows.GetMainWindow.TitleText.Text; ConfigureWindows.GetMainWindow.Title = ConfigureWindows.GetMainWindow.Title.Replace(Path.GetFileName(Pics[FolderIndex]), Pics[FolderIndex], System.StringComparison.InvariantCultureIgnoreCase); ConfigureWindows.GetMainWindow.TitleText.Text = ConfigureWindows.GetMainWindow.TitleText.Text.Replace(Path.GetFileName(Pics[FolderIndex]), Pics[FolderIndex], System.StringComparison.InvariantCultureIgnoreCase); ConfigureWindows.GetMainWindow.TitleText.ToolTip = ConfigureWindows.GetMainWindow.TitleText.ToolTip.ToString().Replace(Path.GetFileName(Pics[FolderIndex]), Pics[FolderIndex], System.StringComparison.InvariantCultureIgnoreCase); } Refocus(false); } else { Tooltip.ShowTooltipMessage(Application.Current.Resources["AnErrorOccuredMovingFile"]); Refocus(); } }
/// <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"]); }
/// <summary> /// Add file to preloader from index /// </summary> /// <param name="i">Index of Pics</param> internal static void Add(int i) { if (i >= Pics.Count || i < 0) { return; } IsLoading = true; //#if DEBUG //System.Threading.Thread.Sleep(500); //#endif if (File.Exists(Pics[i])) { if (!Contains(Pics[i])) { Add(Pics[i]); } } else { Pics.Remove(Pics[i]); IsLoading = false; #if DEBUG Trace.WriteLine("Preloader removed = " + Pics[i] + " from Pics, index " + i); #endif } }
/// <summary> /// Add file to preloader from index /// </summary> /// <param name="i">Index of Pics</param> internal static void Add(int i) { if (i >= Pics.Count || i < 0) { return; } if (File.Exists(Pics[i])) { if (!Contains(Pics[i])) { Add(Pics[i]); } #if DEBUG else { Trace.WriteLine("Skipped at index " + i); } #endif } else { Pics.Remove(Pics[i]); #if DEBUG Trace.WriteLine($"Preloader removed: {Pics[i]} from Pics, index {i}"); #endif } }
//public void RemovePic(Picture picture) //{ // var pic = Pics.FirstOrDefault(p => p.Id == picture.Id); // if(pic != null) // { // Pics.Remove(pic); // }; //} protected async Task DeletePicture(string picId) { var result = await PictureService.DeleteAsync(picId); if (!string.IsNullOrEmpty(result.Id.ToString())) { var pic = Article.Pictures.FirstOrDefault(p => p.Id == Guid.Parse(picId)); if (pic != null) { Article.Pictures.Remove(pic); } Pics.Remove(Pics.FirstOrDefault(p => p.Id == Guid.Parse(picId))); StateHasChanged(); } }
/// <summary> /// Attemps to fix erros and prevent crashes /// </summary> /// <param name="x">The index to start from</param> internal static async Task <BitmapSource> PicErrorFix(int x) { BitmapSource pic = null; #if DEBUG Trace.WriteLine("Entered PicErrorFix"); #endif if (Pics == null) { Reload(true); return(null); } if (x == -1) { await GetValues(Pics[0]).ConfigureAwait(true); } if (Pics.Count < 0) { ShowTooltipMessage(Application.Current.Resources["UnexpectedError"], true, TimeSpan.FromSeconds(3)); Unload(); return(null); } else if (x >= Pics.Count) { if (Pics.Count > 0) { if (x < Pics.Count) { pic = RenderToBitmapSource(Pics[x]); if (pic != null) { return(pic); } } } else { Unload(); return(null); } } else if (x < 0) { pic = RenderToBitmapSource(Pics[x]); if (pic != null) { return(pic); } else { Pics = FileList(Path.GetDirectoryName(Pics[x])); Pics.Remove(Pics[x]); x--; if (x < 0) { Unload(); return(null); } } } var file = Pics[x]; if (file == null) { ShowTooltipMessage(Application.Current.Resources["UnexpectedError"], true, TimeSpan.FromSeconds(3)); Unload(); return(null); } // Retry if exists, fixes rare error if (File.Exists(file)) { pic = RenderToBitmapSource(file); if (pic != null) { return(pic); } return(null); } // Continue to remove file if can't be rendered Pics.Remove(file); // Check if there's still images in folder if (Pics.Count < 0) { ShowTooltipMessage(Application.Current.Resources["No images"], true, TimeSpan.FromSeconds(3)); Unload(); return(null); } // Go to next image if (Properties.Settings.Default.Looping) { x = x == Pics.Count - 1 ? 0 : FolderIndex; } else { x = x == Pics.Count - 1 ? Pics.Count - 2 : FolderIndex; } // Repeat process if the next image was not found if (x > 0 && x < Pics.Count) { await PicErrorFix(x).ConfigureAwait(false); } return(null); }
/// <summary> /// Attemps to fix erros and prevent crashes /// </summary> /// <param name="x">The index to start from</param> internal static async Task <BitmapSource> PicErrorFix(int x) { BitmapSource pic = null; #if DEBUG Trace.WriteLine("Entered PicErrorFix"); // We don't want to go in here #endif if (Pics == null) { Reload(true); return(null); } if (Pics.Count < 0) { ToolTipStyle("Unexpected error", true, TimeSpan.FromSeconds(3)); Unload(); return(null); } else if (x >= Pics.Count) { if (Pics.Count > 0) { if (x < Pics.Count) { await Task.Run(() => pic = RenderToBitmapSource(Pics[x])).ConfigureAwait(false); if (pic != null) { return(pic); } } } else { Unload(); return(null); } } else if (x < 0) { await Task.Run(() => pic = RenderToBitmapSource(Pics[FolderIndex])).ConfigureAwait(false); if (pic != null) { return(pic); } else { Pics = FileList(Path.GetDirectoryName(Pics[FolderIndex])); Pics.Remove(Pics[FolderIndex]); x--; if (x < 0) { Unload(); return(null); } } } var file = Pics[x]; if (file == null) { ToolTipStyle("Unexpected error", true, TimeSpan.FromSeconds(3)); Unload(); return(null); } // Retry if exists, fixes rare error if (File.Exists(file)) { await Task.Run(() => pic = RenderToBitmapSource(file)).ConfigureAwait(false); if (pic != null) { return(pic); } return(null); } // Continue to remove file if can't be rendered Pics.Remove(file); // Check if there's still images in folder if (Pics.Count < 0) { ToolTipStyle("No images in folder", true, TimeSpan.FromSeconds(3)); Unload(); return(null); } // Go to next image if (Properties.Settings.Default.Looping) { FolderIndex = FolderIndex == Pics.Count - 1 ? 0 : FolderIndex; } else { FolderIndex = FolderIndex == Pics.Count - 1 ? Pics.Count - 2 : FolderIndex; } if (File.Exists(file)) { ToolTipStyle("File not found or unable to render, " + file, false, TimeSpan.FromSeconds(2.5)); } AjaxLoadingEnd(); // Repeat process if the next image was not found if (FolderIndex > 0 && FolderIndex < Pics.Count) { await PicErrorFix(FolderIndex).ConfigureAwait(false); } return(null); }