FileManager.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. using System.Diagnostics;
  2. using PicView.Avalonia.ImageHandling;
  3. using PicView.Avalonia.UI;
  4. using PicView.Avalonia.ViewModels;
  5. using PicView.Avalonia.Views.UC.PopUps;
  6. using PicView.Core.FileHandling;
  7. using PicView.Core.Localization;
  8. namespace PicView.Avalonia.FileSystem;
  9. public static class FileManager
  10. {
  11. /// <summary>
  12. /// Deletes the current file, either permanently or by moving to recycle bin
  13. /// </summary>
  14. public static async Task DeleteFile(bool recycle, MainViewModel vm)
  15. {
  16. if (vm.PicViewer.FileInfo is null)
  17. {
  18. return;
  19. }
  20. try
  21. {
  22. string? errorMsg = null;
  23. if (!recycle)
  24. {
  25. var prompt = TranslationManager.GetTranslation("DeleteFilePermanently");
  26. var deleteDialog = new DeleteDialog(prompt, vm.PicViewer.FileInfo.FullName);
  27. UIHelper.GetMainView.MainGrid.Children.Add(deleteDialog);
  28. // Dialog handles the deletion
  29. }
  30. else
  31. {
  32. errorMsg = await Task.FromResult(FileDeletionHelper.DeleteFileWithErrorMsg(vm.PicViewer.FileInfo.FullName, recycle));
  33. }
  34. if (!string.IsNullOrEmpty(errorMsg))
  35. {
  36. await TooltipHelper.ShowTooltipMessageAsync(errorMsg, true);
  37. }
  38. }
  39. catch (Exception ex)
  40. {
  41. await LogAndShowError(ex, nameof(DeleteFile));
  42. }
  43. }
  44. /// <summary>
  45. /// Shows properties dialog for the specified file
  46. /// </summary>
  47. public static async Task ShowFileProperties(string path, MainViewModel vm)
  48. {
  49. if (!ValidateParameters(path, vm.PlatformService))
  50. {
  51. return;
  52. }
  53. try
  54. {
  55. await Task.Run(() => vm.PlatformService!.ShowFileProperties(path));
  56. }
  57. catch (Exception ex)
  58. {
  59. await LogAndShowError(ex, nameof(ShowFileProperties));
  60. }
  61. }
  62. /// <summary>
  63. /// Prints the specified image file
  64. /// </summary>
  65. public static async Task Print(string? path, MainViewModel vm)
  66. {
  67. if (!ValidateParameters(path, vm.PlatformService))
  68. {
  69. return;
  70. }
  71. try
  72. {
  73. vm.IsLoading = true;
  74. await ExecutePlatformServiceOperationAsync(path!, vm,
  75. (platformService, file) => platformService.Print(file));
  76. }
  77. catch (Exception ex)
  78. {
  79. await LogAndShowError(ex, nameof(Print));
  80. }
  81. finally
  82. {
  83. vm.IsLoading = false;
  84. }
  85. }
  86. /// <summary>
  87. /// Opens the file location in file explorer
  88. /// </summary>
  89. public static async Task LocateOnDisk(string path, MainViewModel vm)
  90. {
  91. if (!ValidateParameters(path, vm.PlatformService))
  92. {
  93. return;
  94. }
  95. try
  96. {
  97. await Task.Run(() => vm.PlatformService!.LocateOnDisk(path));
  98. }
  99. catch (Exception ex)
  100. {
  101. await LogAndShowError(ex, nameof(LocateOnDisk));
  102. }
  103. }
  104. /// <summary>
  105. /// Shows the dialog to open the file with another application
  106. /// </summary>
  107. public static async Task OpenWith(string path, MainViewModel vm)
  108. {
  109. if (!ValidateParameters(path, vm.PlatformService))
  110. {
  111. return;
  112. }
  113. try
  114. {
  115. await Task.Run(() => vm.PlatformService!.OpenWith(path));
  116. }
  117. catch (Exception ex)
  118. {
  119. await LogAndShowError(ex, nameof(LocateOnDisk));
  120. }
  121. }
  122. #region Private Helper Methods
  123. /// <summary>
  124. /// Validates common parameters for file operations
  125. /// </summary>
  126. private static bool ValidateParameters(string? path, object? platformService)
  127. {
  128. return !string.IsNullOrWhiteSpace(path) && platformService != null;
  129. }
  130. /// <summary>
  131. /// Helper method to handle common platform service operations that might require file conversion
  132. /// </summary>
  133. private static async Task ExecutePlatformServiceOperationAsync(string path, MainViewModel vm,
  134. Action<dynamic, string> platformServiceAction)
  135. {
  136. var file = await ImageFormatConverter.ConvertToCommonSupportedFormatAsync(path, vm)
  137. .ConfigureAwait(false);
  138. if (string.IsNullOrWhiteSpace(file))
  139. {
  140. await TooltipHelper.ShowTooltipMessageAsync(TranslationManager.Translation.UnexpectedError);
  141. return;
  142. }
  143. await Task.Run(() => platformServiceAction(vm.PlatformService!, file));
  144. }
  145. /// <summary>
  146. /// Logs errors and shows appropriate error messages
  147. /// </summary>
  148. private static async Task LogAndShowError(Exception ex, string methodName)
  149. {
  150. #if DEBUG
  151. Debug.WriteLine($"{nameof(FileManager)}.{methodName}: {ex.Message}");
  152. Debug.WriteLine($"Stack trace: {ex.StackTrace}");
  153. #endif
  154. await TooltipHelper.ShowTooltipMessageAsync(ex.Message);
  155. }
  156. #endregion
  157. }