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