PageRenderTime 19ms CodeModel.GetById 9ms app.highlight 5ms RepoModel.GetById 2ms app.codeStats 0ms

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

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