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