OSDN Git Service

WinGui:
[handbrake-jp/handbrake-jp-git.git] / win / C# / Functions / Main.cs
1 /*  Main.cs $\r
2     This file is part of the HandBrake source code.\r
3     Homepage: <http://handbrake.fr>.\r
4     It may be used under the terms of the GNU General Public License. */\r
5 \r
6 namespace Handbrake.Functions\r
7 {\r
8     using System;\r
9     using System.Collections.Generic;\r
10     using System.Diagnostics;\r
11     using System.IO;\r
12     using System.Linq;\r
13     using System.Security.Cryptography;\r
14     using System.Text;\r
15     using System.Text.RegularExpressions;\r
16     using System.Windows.Forms;\r
17     using System.Xml.Serialization;\r
18 \r
19     using HandBrake.Framework.Services;\r
20     using HandBrake.Framework.Services.Interfaces;\r
21     using HandBrake.ApplicationServices.Model;\r
22     using HandBrake.ApplicationServices.Parsing;\r
23     using HandBrake.ApplicationServices.Services.Interfaces;\r
24     using Model;\r
25 \r
26     /// <summary>\r
27     /// Useful functions which various screens can use.\r
28     /// </summary>\r
29     public static class Main\r
30     {\r
31         /// <summary>\r
32         /// The Error Service\r
33         /// </summary>\r
34         private static readonly IErrorService errorService = new ErrorService();\r
35 \r
36         /// <summary>\r
37         /// The XML Serializer\r
38         /// </summary>\r
39         private static readonly XmlSerializer Ser = new XmlSerializer(typeof(List<QueueTask>));\r
40 \r
41         /// <summary>\r
42         /// Calculate the duration of the selected title and chapters\r
43         /// </summary>\r
44         /// <param name="chapterStart">\r
45         /// The chapter Start.\r
46         /// </param>\r
47         /// <param name="chapterEnd">\r
48         /// The chapter End.\r
49         /// </param>\r
50         /// <param name="selectedTitle">\r
51         /// The selected Title.\r
52         /// </param>\r
53         /// <returns>\r
54         /// The calculated duration.\r
55         /// </returns>\r
56         public static TimeSpan CalculateDuration(int chapterStart, int chapterEnd, Title selectedTitle)\r
57         {\r
58             TimeSpan duration = TimeSpan.FromSeconds(0.0);\r
59             chapterStart++;\r
60             chapterEnd++;\r
61             if (chapterStart != 0 && chapterEnd != 0 && chapterEnd <= selectedTitle.Chapters.Count)\r
62             {\r
63                 for (int i = chapterStart; i <= chapterEnd; i++)\r
64                     duration += selectedTitle.Chapters[i - 1].Duration;\r
65             }\r
66 \r
67             return duration;\r
68         }\r
69 \r
70         /// <summary>\r
71         /// Set's up the DataGridView on the Chapters tab (frmMain)\r
72         /// </summary>\r
73         /// <param name="title">\r
74         /// The currently selected title object.\r
75         /// This will be used to get chapter names if they exist.\r
76         /// </param>\r
77         /// <param name="dataChpt">\r
78         /// The DataGridView Control\r
79         /// </param>\r
80         /// <param name="chapterEnd">\r
81         /// The chapter End.\r
82         /// </param>\r
83         /// <returns>\r
84         /// The chapter naming.\r
85         /// </returns>\r
86         public static DataGridView ChapterNaming(Title title, DataGridView dataChpt, string chapterEnd)\r
87         {\r
88             int i = 0, finish = 0;\r
89 \r
90             if (chapterEnd != "Auto")\r
91                 int.TryParse(chapterEnd, out finish);\r
92 \r
93             while (i < finish)\r
94             {\r
95                 string chapterName = string.Empty;\r
96                 if (title != null)\r
97                 {\r
98                     if (title.Chapters.Count <= i && title.Chapters[i] != null)\r
99                     {\r
100                         chapterName = title.Chapters[i].ChapterName;\r
101                     }\r
102                 }\r
103 \r
104                 int n = dataChpt.Rows.Add();\r
105                 dataChpt.Rows[n].Cells[0].Value = i + 1;\r
106                 dataChpt.Rows[n].Cells[1].Value = string.IsNullOrEmpty(chapterName) ? "Chapter " + (i + 1) : chapterName;\r
107                 dataChpt.Rows[n].Cells[0].ValueType = typeof(int);\r
108                 dataChpt.Rows[n].Cells[1].ValueType = typeof(string);\r
109                 i++;\r
110             }\r
111 \r
112             return dataChpt;\r
113         }\r
114 \r
115         /// <summary>\r
116         /// Import a CSV file which contains Chapter Names\r
117         /// </summary>\r
118         /// <param name="dataChpt">\r
119         /// The DataGridView Control\r
120         /// </param>\r
121         /// <param name="filename">\r
122         /// The filepath and name\r
123         /// </param>\r
124         /// <returns>A Populated DataGridView</returns>\r
125         public static DataGridView ImportChapterNames(DataGridView dataChpt, string filename)\r
126         {\r
127             IDictionary<int, string> chapterMap = new Dictionary<int, string>();\r
128             try\r
129             {\r
130                 StreamReader sr = new StreamReader(filename);\r
131                 string csv = sr.ReadLine();\r
132                 while (csv != null)\r
133                 {\r
134                     if (csv.Trim() != string.Empty)\r
135                     {\r
136                         csv = csv.Replace("\\,", "<!comma!>");\r
137                         string[] contents = csv.Split(',');\r
138                         int chapter;\r
139                         int.TryParse(contents[0], out chapter);\r
140                         chapterMap.Add(chapter, contents[1].Replace("<!comma!>", ","));\r
141                     }\r
142                     csv = sr.ReadLine();\r
143                 }\r
144             }\r
145             catch (Exception)\r
146             {\r
147                 return null;\r
148             }\r
149 \r
150             foreach (DataGridViewRow item in dataChpt.Rows)\r
151             {\r
152                 string name;\r
153                 chapterMap.TryGetValue((int)item.Cells[0].Value, out name);\r
154                 item.Cells[1].Value = name ?? "Chapter " + item.Cells[0].Value;\r
155             }\r
156 \r
157             return dataChpt;\r
158         }\r
159 \r
160         /// <summary>\r
161         /// Create a CSV file with the data from the Main Window Chapters tab\r
162         /// </summary>\r
163         /// <param name="mainWindow">Main Window</param>\r
164         /// <param name="filePathName">Path to save the csv file</param>\r
165         /// <returns>True if successful </returns>\r
166         public static bool SaveChapterMarkersToCsv(frmMain mainWindow, string filePathName)\r
167         {\r
168             try\r
169             {\r
170                 string csv = string.Empty;\r
171 \r
172                 foreach (DataGridViewRow row in mainWindow.data_chpt.Rows)\r
173                 {\r
174                     csv += row.Cells[0].Value.ToString();\r
175                     csv += ",";\r
176                     csv += row.Cells[1].Value.ToString().Replace(",", "\\,");\r
177                     csv += Environment.NewLine;\r
178                 }\r
179                 StreamWriter file = new StreamWriter(filePathName);\r
180                 file.Write(csv);\r
181                 file.Close();\r
182                 file.Dispose();\r
183                 return true;\r
184             }\r
185             catch (Exception exc)\r
186             {\r
187                 ShowExceptiowWindow("Unable to save Chapter Makrers file! \nChapter marker names will NOT be saved in your encode", exc.ToString());\r
188                 return false;\r
189             }\r
190         }\r
191 \r
192         /// <summary>\r
193         /// Function which generates the filename and path automatically based on \r
194         /// the Source Name, DVD title and DVD Chapters\r
195         /// </summary>\r
196         /// <param name="mainWindow">\r
197         /// The main Window.\r
198         /// </param>\r
199         /// <returns>\r
200         /// The Generated FileName\r
201         /// </returns>\r
202         public static string AutoName(frmMain mainWindow)\r
203         {\r
204             string autoNamePath = string.Empty;\r
205             if (mainWindow.drp_dvdtitle.Text != "Automatic")\r
206             {\r
207                 // Get the Source Name and remove any invalid characters\r
208 \r
209                 string sourceName = Path.GetInvalidFileNameChars().Aggregate(Path.GetFileNameWithoutExtension(mainWindow.SourceName), (current, character) => current.Replace(character.ToString(), string.Empty));\r
210 \r
211                 if (Properties.Settings.Default.AutoNameRemoveUnderscore)\r
212                     sourceName = sourceName.Replace("_", " ");\r
213 \r
214                 if (Properties.Settings.Default.AutoNameTitleCase)\r
215                     sourceName = TitleCase(sourceName);\r
216 \r
217                 // Get the Selected Title Number\r
218                 string[] titlesplit = mainWindow.drp_dvdtitle.Text.Split(' ');\r
219                 string dvdTitle = titlesplit[0].Replace("Automatic", string.Empty);\r
220 \r
221                 // Get the Chapter Start and Chapter End Numbers\r
222                 string chapterStart = mainWindow.drop_chapterStart.Text.Replace("Auto", string.Empty);\r
223                 string chapterFinish = mainWindow.drop_chapterFinish.Text.Replace("Auto", string.Empty);\r
224                 string combinedChapterTag = chapterStart;\r
225                 if (chapterFinish != chapterStart && chapterFinish != string.Empty)\r
226                     combinedChapterTag = chapterStart + "-" + chapterFinish;\r
227 \r
228                 // Get the destination filename.\r
229                 string destinationFilename;\r
230                 if (Properties.Settings.Default.autoNameFormat != string.Empty)\r
231                 {\r
232                     destinationFilename = Properties.Settings.Default.autoNameFormat;\r
233                     destinationFilename = destinationFilename.Replace("{source}", sourceName)\r
234                                                              .Replace("{title}", dvdTitle)\r
235                                                              .Replace("{chapters}", combinedChapterTag);\r
236                 }\r
237                 else\r
238                     destinationFilename = sourceName + "_T" + dvdTitle + "_C" + combinedChapterTag;\r
239 \r
240                 // Add the appropriate file extension\r
241                 if (mainWindow.drop_format.SelectedIndex == 0)\r
242                 {\r
243                     destinationFilename += Properties.Settings.Default.useM4v == 0 || Properties.Settings.Default.useM4v == 2 || mainWindow.Check_ChapterMarkers.Checked ||\r
244                                            mainWindow.AudioSettings.RequiresM4V() || mainWindow.Subtitles.RequiresM4V()\r
245                                                ? ".m4v"\r
246                                                : ".mp4";\r
247                 }\r
248                 else if (mainWindow.drop_format.SelectedIndex == 1)\r
249                     destinationFilename += ".mkv";\r
250 \r
251                 // Now work out the path where the file will be stored.\r
252                 // First case: If the destination box doesn't already contain a path, make one.\r
253                 if (!mainWindow.text_destination.Text.Contains(Path.DirectorySeparatorChar.ToString()))\r
254                 {\r
255                     // If there is an auto name path, use it...\r
256                     if (Properties.Settings.Default.autoNamePath.Trim() == "{source_path}" && !string.IsNullOrEmpty(mainWindow.sourcePath))\r
257                     {\r
258                         autoNamePath = Path.Combine(Path.GetDirectoryName(mainWindow.sourcePath), destinationFilename);\r
259                         if (autoNamePath == mainWindow.sourcePath)\r
260                         {\r
261                             // Append out_ to files that already exist or is the source file\r
262                             autoNamePath = Path.Combine(Path.GetDirectoryName(mainWindow.sourcePath), "output_" + destinationFilename);\r
263                         }\r
264                     }\r
265                     else if (Properties.Settings.Default.autoNamePath.Trim() != string.Empty && Properties.Settings.Default.autoNamePath.Trim() != "Click 'Browse' to set the default location")\r
266                     {\r
267                         autoNamePath = Path.Combine(Properties.Settings.Default.autoNamePath, destinationFilename);\r
268                     }\r
269                     else // ...otherwise, output to the source directory\r
270                         autoNamePath = null;\r
271                 }\r
272                 else // Otherwise, use the path that is already there.\r
273                 {\r
274                     // Use the path and change the file extension to match the previous destination\r
275                     autoNamePath = Path.Combine(Path.GetDirectoryName(mainWindow.text_destination.Text), destinationFilename);\r
276 \r
277                     if (Path.HasExtension(mainWindow.text_destination.Text))\r
278                         autoNamePath = Path.ChangeExtension(autoNamePath,\r
279                                                             Path.GetExtension(mainWindow.text_destination.Text));\r
280                 }\r
281             }\r
282 \r
283             return autoNamePath;\r
284         }\r
285 \r
286         /// <summary>\r
287         /// Get's HandBrakes version data from the CLI.\r
288         /// </summary>\r
289         public static void SetCliVersionData()\r
290         {\r
291             string line;\r
292 \r
293             // 0 = SVN Build / Version\r
294             // 1 = Build Date\r
295 \r
296             // Get the SHA1 Hash of HandBrakeCLI\r
297             byte[] hash;\r
298             using (Stream stream = File.OpenRead(Path.Combine(Application.StartupPath, "HandBrakeCLI.exe")))\r
299             {\r
300                 hash = SHA1.Create().ComputeHash(stream);\r
301             }\r
302             string base64Hash = Convert.ToBase64String(hash);\r
303          \r
304             // Compare the hash with the last known hash. If it's the same, return.\r
305             if (Properties.Settings.Default.CliExeHash == base64Hash)\r
306             {\r
307                 return;\r
308             }\r
309 \r
310             // It's not the same, so start the CLI to get it's version data.\r
311             Process cliProcess = new Process();\r
312             ProcessStartInfo handBrakeCli = new ProcessStartInfo("HandBrakeCLI.exe", " -u -v0")\r
313                                                 {\r
314                                                     UseShellExecute = false,\r
315                                                     RedirectStandardError = true,\r
316                                                     RedirectStandardOutput = true,\r
317                                                     CreateNoWindow = true\r
318                                                 };\r
319             cliProcess.StartInfo = handBrakeCli;\r
320 \r
321             try\r
322             {\r
323                 cliProcess.Start();\r
324 \r
325                 // Retrieve standard output and report back to parent thread until the process is complete\r
326                 TextReader stdOutput = cliProcess.StandardError;\r
327 \r
328                 while (!cliProcess.HasExited)\r
329                 {\r
330                     line = stdOutput.ReadLine() ?? string.Empty;\r
331                     Match m = Regex.Match(line, @"HandBrake ([svnM0-9.]*) \(([0-9]*)\)");\r
332                     Match platform = Regex.Match(line, @"- ([A-Za-z0-9\s ]*) -");\r
333 \r
334                     if (m.Success)\r
335                     {\r
336                         string version = m.Groups[1].Success ? m.Groups[1].Value : string.Empty;\r
337                         string build = m.Groups[2].Success ? m.Groups[2].Value : string.Empty;\r
338 \r
339                         int buildValue;\r
340                         int.TryParse(build, out buildValue);\r
341 \r
342                         Properties.Settings.Default.hb_build = buildValue;\r
343                         Properties.Settings.Default.hb_version = version;\r
344                     }\r
345 \r
346                     if (platform.Success)\r
347                     {\r
348                         Properties.Settings.Default.hb_platform = platform.Value.Replace("-", string.Empty).Trim();\r
349                     }\r
350 \r
351                     if (cliProcess.TotalProcessorTime.Seconds > 10) // Don't wait longer than 10 seconds.\r
352                     {\r
353                         Process cli = Process.GetProcessById(cliProcess.Id);\r
354                         if (!cli.HasExited)\r
355                         {\r
356                             cli.Kill();\r
357                         }\r
358                     }\r
359                 }\r
360 \r
361                 Properties.Settings.Default.CliExeHash = base64Hash;\r
362 \r
363                 Properties.Settings.Default.Save();\r
364             }\r
365             catch (Exception e)\r
366             {\r
367                 Properties.Settings.Default.hb_build = 0;\r
368                 Properties.Settings.Default.CliExeHash = null;\r
369                 Properties.Settings.Default.Save();\r
370 \r
371                 ShowExceptiowWindow("Unable to retrieve version information from the CLI.", e.ToString());\r
372             }\r
373         }\r
374 \r
375         /// <summary>\r
376         /// Check if the queue recovery file contains records.\r
377         /// If it does, it means the last queue did not complete before HandBrake closed.\r
378         /// So, return a boolean if true. \r
379         /// </summary>\r
380         /// <returns>\r
381         /// True if there is a queue to recover.\r
382         /// </returns>\r
383         public static List<string> CheckQueueRecovery()\r
384         {\r
385             try\r
386             {\r
387                 string tempPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"HandBrake\");\r
388                 List<string> queueFiles = new List<string>();\r
389 \r
390                 DirectoryInfo info = new DirectoryInfo(tempPath);\r
391                 FileInfo[] logFiles = info.GetFiles("*.xml");\r
392                 foreach (FileInfo file in logFiles)\r
393                 {\r
394                     if (!file.Name.Contains("hb_queue_recovery"))\r
395                         continue;\r
396 \r
397                     using (FileStream strm = new FileStream(Path.Combine(file.DirectoryName, file.Name), FileMode.Open, FileAccess.Read))\r
398                     {\r
399                         List<QueueTask> list = Ser.Deserialize(strm) as List<QueueTask>;\r
400                         if (list != null)\r
401                         {\r
402                             if (list.Count != 0)\r
403                             {\r
404                                 queueFiles.Add(file.Name);\r
405                             }\r
406                         }\r
407                     }\r
408                 }\r
409 \r
410                 return queueFiles;\r
411             }\r
412             catch (Exception)\r
413             {\r
414                 return new List<string>(); // Keep quiet about the error.\r
415             }\r
416         }\r
417 \r
418         /// <summary>\r
419         /// Recover a queue from file.\r
420         /// </summary>\r
421         /// <param name="encodeQueue">\r
422         /// The encode Queue.\r
423         /// </param>\r
424         public static void RecoverQueue(IQueueProcessor encodeQueue)\r
425         {\r
426             string appDataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"HandBrake\");\r
427 \r
428             DialogResult result = DialogResult.None;\r
429             List<string> queueFiles = CheckQueueRecovery();\r
430             if (queueFiles.Count == 1)\r
431             {\r
432                 result = MessageBox.Show(\r
433                         "HandBrake has detected unfinished items on the queue from the last time the application was launched. Would you like to recover these?",\r
434                         "Queue Recovery Possible", MessageBoxButtons.YesNo, MessageBoxIcon.Question);\r
435             }\r
436             else if (queueFiles.Count > 1)\r
437             {\r
438                 result = MessageBox.Show(\r
439                         "HandBrake has detected multiple unfinished queue files. These will be from multiple instances of HandBrake running. Would you like to recover all unfinished jobs?",\r
440                         "Queue Recovery Possible", MessageBoxButtons.YesNo, MessageBoxIcon.Question);\r
441             }\r
442 \r
443             if (result == DialogResult.Yes)\r
444             {\r
445                 foreach (string file in queueFiles)\r
446                 {\r
447                     encodeQueue.QueueManager.RestoreQueue(appDataPath + file); // Start Recovery\r
448                 }\r
449             }\r
450             else\r
451             {\r
452                 if (IsMultiInstance) return; // Don't tamper with the files if we are multi instance\r
453 \r
454                 foreach (string file in queueFiles)\r
455                 {\r
456                     if (File.Exists(Path.Combine(appDataPath, file)))\r
457                         File.Delete(Path.Combine(appDataPath, file));\r
458                 }\r
459             }\r
460         }\r
461 \r
462         /// <summary>\r
463         /// Gets a value indicating whether HandBrake is running in multi instance mode\r
464         /// </summary>\r
465         /// <returns>True if the UI has another instance running</returns>\r
466         public static bool IsMultiInstance\r
467         {\r
468             get\r
469             {\r
470                 return Process.GetProcessesByName("HandBrake").Length > 0 ? true : false;\r
471             }\r
472         }\r
473 \r
474         /// <summary>\r
475         /// Map languages and their iso639_2 value into a IDictionary\r
476         /// </summary>\r
477         /// <returns>A Dictionary containing the language and iso code</returns>\r
478         public static IDictionary<string, string> MapLanguages()\r
479         {\r
480             IDictionary<string, string> languageMap = new Dictionary<string, string>\r
481                                                           {\r
482                                                               {"Any", "und"}, \r
483                                                               {"Afar", "aar"}, \r
484                                                               {"Abkhazian", "abk"}, \r
485                                                               {"Afrikaans", "afr"}, \r
486                                                               {"Akan", "aka"}, \r
487                                                               {"Albanian", "sqi"}, \r
488                                                               {"Amharic", "amh"}, \r
489                                                               {"Arabic", "ara"}, \r
490                                                               {"Aragonese", "arg"}, \r
491                                                               {"Armenian", "hye"}, \r
492                                                               {"Assamese", "asm"}, \r
493                                                               {"Avaric", "ava"}, \r
494                                                               {"Avestan", "ave"}, \r
495                                                               {"Aymara", "aym"}, \r
496                                                               {"Azerbaijani", "aze"}, \r
497                                                               {"Bashkir", "bak"}, \r
498                                                               {"Bambara", "bam"}, \r
499                                                               {"Basque", "eus"}, \r
500                                                               {"Belarusian", "bel"}, \r
501                                                               {"Bengali", "ben"}, \r
502                                                               {"Bihari", "bih"}, \r
503                                                               {"Bislama", "bis"}, \r
504                                                               {"Bosnian", "bos"}, \r
505                                                               {"Breton", "bre"}, \r
506                                                               {"Bulgarian", "bul"}, \r
507                                                               {"Burmese", "mya"}, \r
508                                                               {"Catalan", "cat"}, \r
509                                                               {"Chamorro", "cha"}, \r
510                                                               {"Chechen", "che"}, \r
511                                                               {"Chinese", "zho"}, \r
512                                                               {"Church Slavic", "chu"}, \r
513                                                               {"Chuvash", "chv"}, \r
514                                                               {"Cornish", "cor"}, \r
515                                                               {"Corsican", "cos"}, \r
516                                                               {"Cree", "cre"}, \r
517                                                               {"Czech", "ces"}, \r
518                                                               {"Dansk", "dan"}, \r
519                                                               {"Divehi", "div"}, \r
520                                                               {"Nederlands", "nld"}, \r
521                                                               {"Dzongkha", "dzo"}, \r
522                                                               {"English", "eng"}, \r
523                                                               {"Esperanto", "epo"}, \r
524                                                               {"Estonian", "est"}, \r
525                                                               {"Ewe", "ewe"}, \r
526                                                               {"Faroese", "fao"}, \r
527                                                               {"Fijian", "fij"}, \r
528                                                               {"Suomi", "fin"}, \r
529                                                               {"Francais", "fra"}, \r
530                                                               {"Western Frisian", "fry"}, \r
531                                                               {"Fulah", "ful"}, \r
532                                                               {"Georgian", "kat"}, \r
533                                                               {"Deutsch", "deu"}, \r
534                                                               {"Gaelic (Scots)", "gla"}, \r
535                                                               {"Irish", "gle"}, \r
536                                                               {"Galician", "glg"}, \r
537                                                               {"Manx", "glv"}, \r
538                                                               {"Greek Modern", "ell"}, \r
539                                                               {"Guarani", "grn"}, \r
540                                                               {"Gujarati", "guj"}, \r
541                                                               {"Haitian", "hat"}, \r
542                                                               {"Hausa", "hau"}, \r
543                                                               {"Hebrew", "heb"}, \r
544                                                               {"Herero", "her"}, \r
545                                                               {"Hindi", "hin"}, \r
546                                                               {"Hiri Motu", "hmo"}, \r
547                                                               {"Magyar", "hun"}, \r
548                                                               {"Igbo", "ibo"}, \r
549                                                               {"Islenska", "isl"}, \r
550                                                               {"Ido", "ido"}, \r
551                                                               {"Sichuan Yi", "iii"}, \r
552                                                               {"Inuktitut", "iku"}, \r
553                                                               {"Interlingue", "ile"}, \r
554                                                               {"Interlingua", "ina"}, \r
555                                                               {"Indonesian", "ind"}, \r
556                                                               {"Inupiaq", "ipk"}, \r
557                                                               {"Italiano", "ita"}, \r
558                                                               {"Javanese", "jav"}, \r
559                                                               {"Japanese", "jpn"}, \r
560                                                               {"Kalaallisut", "kal"}, \r
561                                                               {"Kannada", "kan"}, \r
562                                                               {"Kashmiri", "kas"}, \r
563                                                               {"Kanuri", "kau"}, \r
564                                                               {"Kazakh", "kaz"}, \r
565                                                               {"Central Khmer", "khm"}, \r
566                                                               {"Kikuyu", "kik"}, \r
567                                                               {"Kinyarwanda", "kin"}, \r
568                                                               {"Kirghiz", "kir"}, \r
569                                                               {"Komi", "kom"}, \r
570                                                               {"Kongo", "kon"}, \r
571                                                               {"Korean", "kor"}, \r
572                                                               {"Kuanyama", "kua"}, \r
573                                                               {"Kurdish", "kur"}, \r
574                                                               {"Lao", "lao"}, \r
575                                                               {"Latin", "lat"}, \r
576                                                               {"Latvian", "lav"}, \r
577                                                               {"Limburgan", "lim"}, \r
578                                                               {"Lingala", "lin"}, \r
579                                                               {"Lithuanian", "lit"}, \r
580                                                               {"Luxembourgish", "ltz"}, \r
581                                                               {"Luba-Katanga", "lub"}, \r
582                                                               {"Ganda", "lug"}, \r
583                                                               {"Macedonian", "mkd"}, \r
584                                                               {"Marshallese", "mah"}, \r
585                                                               {"Malayalam", "mal"}, \r
586                                                               {"Maori", "mri"}, \r
587                                                               {"Marathi", "mar"}, \r
588                                                               {"Malay", "msa"}, \r
589                                                               {"Malagasy", "mlg"}, \r
590                                                               {"Maltese", "mlt"}, \r
591                                                               {"Moldavian", "mol"}, \r
592                                                               {"Mongolian", "mon"}, \r
593                                                               {"Nauru", "nau"}, \r
594                                                               {"Navajo", "nav"}, \r
595                                                               {"Ndebele, South", "nbl"}, \r
596                                                               {"Ndebele, North", "nde"}, \r
597                                                               {"Ndonga", "ndo"}, \r
598                                                               {"Nepali", "nep"}, \r
599                                                               {"Norwegian Nynorsk", "nno"}, \r
600                                                               {"Norwegian Bokmål", "nob"}, \r
601                                                               {"Norsk", "nor"}, \r
602                                                               {"Chichewa; Nyanja", "nya"}, \r
603                                                               {"Occitan", "oci"}, \r
604                                                               {"Ojibwa", "oji"}, \r
605                                                               {"Oriya", "ori"}, \r
606                                                               {"Oromo", "orm"}, \r
607                                                               {"Ossetian", "oss"}, \r
608                                                               {"Panjabi", "pan"}, \r
609                                                               {"Persian", "fas"}, \r
610                                                               {"Pali", "pli"}, \r
611                                                               {"Polish", "pol"}, \r
612                                                               {"Portugues", "por"}, \r
613                                                               {"Pushto", "pus"}, \r
614                                                               {"Quechua", "que"}, \r
615                                                               {"Romansh", "roh"}, \r
616                                                               {"Romanian", "ron"}, \r
617                                                               {"Rundi", "run"}, \r
618                                                               {"Russian", "rus"}, \r
619                                                               {"Sango", "sag"}, \r
620                                                               {"Sanskrit", "san"}, \r
621                                                               {"Serbian", "srp"}, \r
622                                                               {"Hrvatski", "hrv"}, \r
623                                                               {"Sinhala", "sin"}, \r
624                                                               {"Slovak", "slk"}, \r
625                                                               {"Slovenian", "slv"}, \r
626                                                               {"Northern Sami", "sme"}, \r
627                                                               {"Samoan", "smo"}, \r
628                                                               {"Shona", "sna"}, \r
629                                                               {"Sindhi", "snd"}, \r
630                                                               {"Somali", "som"}, \r
631                                                               {"Sotho Southern", "sot"}, \r
632                                                               {"Espanol", "spa"}, \r
633                                                               {"Sardinian", "srd"}, \r
634                                                               {"Swati", "ssw"}, \r
635                                                               {"Sundanese", "sun"}, \r
636                                                               {"Swahili", "swa"}, \r
637                                                               {"Svenska", "swe"}, \r
638                                                               {"Tahitian", "tah"}, \r
639                                                               {"Tamil", "tam"}, \r
640                                                               {"Tatar", "tat"}, \r
641                                                               {"Telugu", "tel"}, \r
642                                                               {"Tajik", "tgk"}, \r
643                                                               {"Tagalog", "tgl"}, \r
644                                                               {"Thai", "tha"}, \r
645                                                               {"Tibetan", "bod"}, \r
646                                                               {"Tigrinya", "tir"}, \r
647                                                               {"Tonga", "ton"}, \r
648                                                               {"Tswana", "tsn"}, \r
649                                                               {"Tsonga", "tso"}, \r
650                                                               {"Turkmen", "tuk"}, \r
651                                                               {"Turkish", "tur"}, \r
652                                                               {"Twi", "twi"}, \r
653                                                               {"Uighur", "uig"}, \r
654                                                               {"Ukrainian", "ukr"}, \r
655                                                               {"Urdu", "urd"}, \r
656                                                               {"Uzbek", "uzb"}, \r
657                                                               {"Venda", "ven"}, \r
658                                                               {"Vietnamese", "vie"}, \r
659                                                               {"Volapük", "vol"}, \r
660                                                               {"Welsh", "cym"}, \r
661                                                               {"Walloon", "wln"}, \r
662                                                               {"Wolof", "wol"}, \r
663                                                               {"Xhosa", "xho"}, \r
664                                                               {"Yiddish", "yid"}, \r
665                                                               {"Yoruba", "yor"}, \r
666                                                               {"Zhuang", "zha"}, \r
667                                                               {"Zulu", "zul"}\r
668                                                           };\r
669             return languageMap;\r
670         }\r
671 \r
672         /// <summary>\r
673         /// Change a string to Title Case/\r
674         /// </summary>\r
675         /// <param name="input">\r
676         /// The input.\r
677         /// </param>\r
678         /// <returns>\r
679         /// A string in title case.\r
680         /// </returns>\r
681         public static string TitleCase(string input)\r
682         {\r
683             string[] tokens = input.Split(' ');\r
684             StringBuilder sb = new StringBuilder(input.Length);\r
685             foreach (string s in tokens)\r
686             {\r
687                 if (!string.IsNullOrEmpty(s))\r
688                 {\r
689                     sb.Append(s[0].ToString().ToUpper());\r
690                     sb.Append(s.Substring(1).ToLower());\r
691                     sb.Append(" ");\r
692                 }\r
693             }\r
694 \r
695             return sb.ToString().Trim();\r
696         }\r
697 \r
698         /// <summary>\r
699         /// Show the Exception Window\r
700         /// </summary>\r
701         /// <param name="shortError">\r
702         /// The short error.\r
703         /// </param>\r
704         /// <param name="longError">\r
705         /// The long error.\r
706         /// </param>\r
707         public static void ShowExceptiowWindow(string shortError, string longError)\r
708         {\r
709             errorService.ShowError(shortError, longError);\r
710         }\r
711 \r
712         /// <summary>\r
713         /// Get The Source from the CLI Query\r
714         /// </summary>\r
715         /// <param name="query">Full CLI Query</param>\r
716         /// <returns>The Source Path</returns>\r
717         public static string GetSourceFromQuery(string query)\r
718         {\r
719             int startIndex = query.IndexOf("-i \"");\r
720             if (startIndex != -1)\r
721             {\r
722                 string input = query.Substring(startIndex).Replace("-i \"", string.Empty).Trim();\r
723 \r
724                 int closeIndex = input.IndexOf('"');\r
725 \r
726                 return closeIndex == -1 ? "Unknown" : input.Substring(0, closeIndex);\r
727             }\r
728 \r
729             return "Unknown";\r
730         }\r
731 \r
732         /// <summary>\r
733         /// Get the Destination from the CLI Query\r
734         /// </summary>\r
735         /// <param name="query">Full CLI Query</param>\r
736         /// <returns>The Destination path</returns>\r
737         public static string GetDestinationFromQuery(string query)\r
738         {\r
739             int startIndex = query.IndexOf("-o \"");\r
740             if (startIndex != -1)\r
741             {\r
742                 string output = query.Substring(startIndex).Replace("-o \"", string.Empty).Trim();\r
743 \r
744                 int closeIndex = output.IndexOf('"');\r
745 \r
746                 return closeIndex == -1 ? "Unknown" : output.Substring(0, closeIndex);\r
747             }\r
748 \r
749             return "Unknown";\r
750         }\r
751     }\r
752 }