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