PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/Mercurial.Net/Gui/Clients/TortoiseHgClient.cs

#
C# | 118 lines | 60 code | 12 blank | 46 comment | 7 complexity | e4ee803c5b30adbb2abd98d73220b053 MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. namespace Mercurial.Gui.Clients
  8. {
  9. /// <summary>
  10. /// This class, and its descendants implement specific handling for the specific type of clients.
  11. /// </summary>
  12. internal abstract class TortoiseHgClient
  13. {
  14. /// <summary>
  15. /// Gets the current <see cref="TortoiseHgClient"/> implementation in use.
  16. /// </summary>
  17. public static TortoiseHgClient Current
  18. {
  19. get;
  20. private set;
  21. }
  22. /// <summary>
  23. /// Assigns the <see cref="Current"/> <see cref="TortoiseHgClient"/> implementation based on the
  24. /// <see cref="GuiClientType"/> specified.
  25. /// </summary>
  26. /// <param name="clientType">
  27. /// The <see cref="GuiClientType"/> to assign the client for.
  28. /// </param>
  29. /// <exception cref="InvalidOperationException">Internal error, unknown client type passed to TortoiseHgClient.AssignCurrent</exception>
  30. internal static void AssignCurrent(GuiClientType clientType)
  31. {
  32. switch (clientType)
  33. {
  34. case GuiClientType.PyGTK:
  35. Current = new TortoiseHgPyGTKClient();
  36. break;
  37. case GuiClientType.PyQT:
  38. Current = new TortoiseHgPyQTClient();
  39. break;
  40. default:
  41. throw new InvalidOperationException("Internal error, unknown client type passed to TortoiseHgClient.AssignCurrent");
  42. }
  43. }
  44. /// <summary>
  45. /// Saves the file list to a file with the specified filename.
  46. /// </summary>
  47. /// <param name="files">
  48. /// The names of all the files to save to the file.
  49. /// </param>
  50. /// <param name="filename">
  51. /// The full path to and name of the file to write the <paramref name="files"/> to.
  52. /// </param>
  53. /// <exception cref="ArgumentNullException">
  54. /// <para><paramref name="files"/> is <c>null</c> or empty.</para>
  55. /// </exception>
  56. public void SaveFileList(IEnumerable<string> files, string filename)
  57. {
  58. if (files == null || !files.Any())
  59. throw new ArgumentNullException("files");
  60. File.WriteAllLines(filename, files.ToArray(), FileListEncoding);
  61. }
  62. /// <summary>
  63. /// Saves the file list to disk in a temporary file, and returns the options necessary to pass to the
  64. /// TortoiseHg command line client to read back that list.
  65. /// </summary>
  66. /// <param name="files">
  67. /// The names of all the files to save to the file.
  68. /// </param>
  69. /// <param name="filename">
  70. /// Upon return from this method, will either be the full path to and name of the file
  71. /// the files was written to, or <c>null</c> if the file was not saved.
  72. /// </param>
  73. /// <returns>
  74. /// The options to pass to the TortoiseHg command line client.
  75. /// </returns>
  76. public string[] GetFileListArguments(IEnumerable<string> files, out string filename)
  77. {
  78. filename = null;
  79. if (files != null && files.Any())
  80. {
  81. filename = Path.GetTempFileName();
  82. SaveFileList(files, filename);
  83. return new[]
  84. {
  85. FileListOption,
  86. string.Format(CultureInfo.InvariantCulture, "\"{0}\"", filename),
  87. };
  88. }
  89. return new string[0];
  90. }
  91. /// <summary>
  92. /// Gets the option to pass to the TortoiseHg command line client to read in the file list.
  93. /// </summary>
  94. public abstract string FileListOption
  95. {
  96. get;
  97. }
  98. /// <summary>
  99. /// Gets the <see cref="Encoding"/> to use when saving the file list to disk.
  100. /// </summary>
  101. public abstract Encoding FileListEncoding
  102. {
  103. get;
  104. }
  105. }
  106. }