Error_Handling.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. using PicView.PreLoading;
  2. using System;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Windows.Media.Imaging;
  6. using static PicView.DeleteFiles;
  7. using static PicView.Fields;
  8. using static PicView.FileLists;
  9. using static PicView.Helper;
  10. using static PicView.ImageManager;
  11. using static PicView.Interface;
  12. using static PicView.Navigation;
  13. using static PicView.Resize_and_Zoom;
  14. using static PicView.Rotate_and_Flip;
  15. namespace PicView
  16. {
  17. internal static class Error_Handling
  18. {
  19. /// <summary>
  20. /// Attemps to fix erros and prevent crashes
  21. /// </summary>
  22. /// <param name="x">The index to start from</param>
  23. internal static bool PicErrorFix(int x)
  24. {
  25. #if DEBUG
  26. Trace.WriteLine("Entered PicErrorFix"); // We don't want to go in here
  27. #endif
  28. if (Pics == null)
  29. {
  30. Reload(true);
  31. return false;
  32. }
  33. if (Pics.Count < 0)
  34. {
  35. ToolTipStyle("Unexpected error", true, TimeSpan.FromSeconds(3));
  36. Unload();
  37. return false;
  38. }
  39. else if (x >= Pics.Count)
  40. {
  41. if (Pics.Count > 0)
  42. {
  43. Pic(Pics[0]);
  44. return true;
  45. }
  46. else
  47. {
  48. Unload();
  49. return false;
  50. }
  51. }
  52. else if (x < 0)
  53. {
  54. var img = RenderToBitmapSource(PicPath);
  55. if (img != null)
  56. {
  57. Pic(PicPath);
  58. return true;
  59. }
  60. else
  61. {
  62. Pics = FileList(Path.GetDirectoryName(PicPath));
  63. Pics.Remove(PicPath);
  64. x--;
  65. if (x < 0)
  66. {
  67. Unload();
  68. return false;
  69. }
  70. }
  71. }
  72. var file = Pics[x];
  73. if (file == null)
  74. {
  75. ToolTipStyle("Unexpected error", true, TimeSpan.FromSeconds(3));
  76. Unload();
  77. return false;
  78. }
  79. // Retry if exists, fixes rare error
  80. if (File.Exists(file))
  81. {
  82. //Preloader.Add(file);
  83. BitmapSource pic = Preloader.Load(file);
  84. if (pic != null)
  85. {
  86. Pic(file);
  87. return true;
  88. }
  89. return false;
  90. }
  91. // Continue to remove file if can't be rendered
  92. Pics.Remove(file);
  93. // Check if there's still images in folder
  94. if (Pics.Count < 0)
  95. {
  96. ToolTipStyle("No images in folder", true, TimeSpan.FromSeconds(3));
  97. Unload();
  98. return false;
  99. }
  100. // Go to next image
  101. if (Properties.Settings.Default.Looping)
  102. FolderIndex = FolderIndex == Pics.Count - 1 ? 0 : FolderIndex;
  103. else
  104. FolderIndex = FolderIndex == Pics.Count - 1 ? Pics.Count - 2 : FolderIndex;
  105. if (File.Exists(file))
  106. ToolTipStyle("File not found or unable to render, " + file, false, TimeSpan.FromSeconds(2.5));
  107. AjaxLoadingEnd();
  108. // Repeat process if the next image was not found
  109. PicErrorFix(FolderIndex);
  110. return false;
  111. }
  112. /// <summary>
  113. /// Clears data, to free objects no longer necessary to store in memory and allow changing folder without error.
  114. /// </summary>
  115. internal static void ChangeFolder()
  116. {
  117. Pics.Clear();
  118. Preloader.Clear();
  119. DeleteTempFiles();
  120. PreloadCount = 0;
  121. freshStartup = true;
  122. if (Properties.Settings.Default.PicGallery > 0)
  123. PicGalleryLogic.Clear();
  124. }
  125. /// <summary>
  126. /// Refresh the current list of pics and reload them if there is some missing or changes.
  127. /// </summary>
  128. internal static void Reload(bool fromBackup = false)
  129. {
  130. if (fromBackup && string.IsNullOrWhiteSpace(xPicPath))
  131. {
  132. Unload();
  133. return;
  134. }
  135. var x = fromBackup ? xPicPath : PicPath;
  136. if (File.Exists(x))
  137. {
  138. // Force reloading values by setting freshStartup to true
  139. freshStartup = true;
  140. Pic(x);
  141. // Reset
  142. if (isZoomed)
  143. ResetZoom();
  144. if (Flipped)
  145. Flip();
  146. if (Rotateint != 0)
  147. Rotate(0);
  148. }
  149. else
  150. {
  151. Unload();
  152. ToolTipStyle("Unknown error occured");
  153. }
  154. }
  155. /// <summary>
  156. /// Reset to default state
  157. /// </summary>
  158. internal static void Unload()
  159. {
  160. mainWindow.Bar.ToolTip = mainWindow.Bar.Text = NoImage;
  161. mainWindow.Title = NoImage + " - " + AppName;
  162. canNavigate = false;
  163. mainWindow.img.Source = null;
  164. freshStartup = true;
  165. if (Pics != null)
  166. Pics.Clear();
  167. PreloadCount = 0;
  168. Preloader.Clear();
  169. PicPath = string.Empty;
  170. FolderIndex = 0;
  171. mainWindow.img.Width = mainWindow.Scroller.Width = mainWindow.Scroller.Height =
  172. mainWindow.img.Height = double.NaN;
  173. xWidth = xHeight = 0;
  174. prevPicResource = null;
  175. if (!string.IsNullOrWhiteSpace(TempZipPath))
  176. {
  177. DeleteTempFiles();
  178. TempZipPath = string.Empty;
  179. }
  180. NoProgress();
  181. AnimationHelper.Fade(ajaxLoading, 0, TimeSpan.FromSeconds(.2));
  182. }
  183. }
  184. }