DefaultFileOperations.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.IO.Abstractions;
  5. using System.Linq;
  6. using Terminal.Gui.Resources;
  7. namespace Terminal.Gui {
  8. /// <summary>
  9. /// Default file operation handlers using modal dialogs.
  10. /// </summary>
  11. public class DefaultFileOperations : IFileOperations {
  12. /// <inheritdoc/>
  13. public bool Delete (IEnumerable<IFileSystemInfo> toDelete)
  14. {
  15. // Default implementation does not allow deleting multiple files
  16. if (toDelete.Count () != 1) {
  17. return false;
  18. }
  19. var d = toDelete.Single ();
  20. var adjective = d.Name;
  21. int result = MessageBox.Query (
  22. string.Format (Strings.fdDeleteTitle, adjective),
  23. string.Format (Strings.fdDeleteBody, adjective),
  24. Strings.btnYes, Strings.btnNo);
  25. try {
  26. if (result == 0) {
  27. if (d is IFileInfo) {
  28. d.Delete ();
  29. } else {
  30. ((IDirectoryInfo)d).Delete (true);
  31. }
  32. return true;
  33. }
  34. } catch (Exception ex) {
  35. MessageBox.ErrorQuery (Strings.fdDeleteFailedTitle, ex.Message, Strings.btnOk);
  36. }
  37. return false;
  38. }
  39. private bool Prompt (string title, string defaultText, out string result)
  40. {
  41. bool confirm = false;
  42. var btnOk = new Button (Strings.btnOk) {
  43. IsDefault = true,
  44. };
  45. btnOk.Clicked += (s, e) => {
  46. confirm = true;
  47. Application.RequestStop ();
  48. };
  49. var btnCancel = new Button (Strings.btnCancel);
  50. btnCancel.Clicked += (s, e) => {
  51. confirm = false;
  52. Application.RequestStop ();
  53. };
  54. var lbl = new Label (Strings.fdRenamePrompt);
  55. var tf = new TextField (defaultText) {
  56. X = Pos.Right (lbl),
  57. Width = Dim.Fill (),
  58. };
  59. tf.SelectAll ();
  60. var dlg = new Dialog () {
  61. Title = title,
  62. Width = Dim.Percent (50),
  63. Height = 4
  64. };
  65. dlg.Add (lbl);
  66. dlg.Add (tf);
  67. // Add buttons last so tab order is friendly
  68. // and TextField gets focus
  69. dlg.AddButton (btnOk);
  70. dlg.AddButton (btnCancel);
  71. Application.Run (dlg);
  72. result = tf.Text;
  73. return confirm;
  74. }
  75. /// <inheritdoc/>
  76. public IFileSystemInfo Rename (IFileSystem fileSystem, IFileSystemInfo toRename)
  77. {
  78. // Don't allow renaming C: or D: or / (on linux) etc
  79. if (toRename is IDirectoryInfo dir && dir.Parent == null) {
  80. return null;
  81. }
  82. if (Prompt (Strings.fdRenameTitle, toRename.Name, out var newName)) {
  83. if (!string.IsNullOrWhiteSpace (newName)) {
  84. try {
  85. if (toRename is IFileInfo f) {
  86. var newLocation = fileSystem.FileInfo.New (Path.Combine (f.Directory.FullName, newName));
  87. f.MoveTo (newLocation.FullName);
  88. return newLocation;
  89. } else {
  90. var d = (IDirectoryInfo)toRename;
  91. var newLocation = fileSystem.DirectoryInfo.New (Path.Combine (d.Parent.FullName, newName));
  92. d.MoveTo (newLocation.FullName);
  93. return newLocation;
  94. }
  95. } catch (Exception ex) {
  96. MessageBox.ErrorQuery (Strings.fdRenameFailedTitle, ex.Message, "Ok");
  97. }
  98. }
  99. }
  100. return null;
  101. }
  102. /// <inheritdoc/>
  103. public IFileSystemInfo New (IFileSystem fileSystem, IDirectoryInfo inDirectory)
  104. {
  105. if (Prompt (Strings.fdNewTitle, "", out var named)) {
  106. if (!string.IsNullOrWhiteSpace (named)) {
  107. try {
  108. var newDir = fileSystem.DirectoryInfo.New (Path.Combine (inDirectory.FullName, named));
  109. newDir.Create ();
  110. return newDir;
  111. } catch (Exception ex) {
  112. MessageBox.ErrorQuery (Strings.fdNewFailed, ex.Message, "Ok");
  113. }
  114. }
  115. }
  116. return null;
  117. }
  118. }
  119. }