OSDN Git Service

8ef4b3b8d111a2833a48ede06c03c57af3de7a35
[handbrake-jp/handbrake-jp-git.git] / win / C# / Functions / Common.cs
1 /*  Common.cs $\r
2         \r
3            This file is part of the HandBrake source code.\r
4            Homepage: <http://handbrake.fr>.\r
5            It may be used under the terms of the GNU General Public License. */\r
6 \r
7 using System;\r
8 using System.Collections;\r
9 using System.Text;\r
10 using System.Windows.Forms;\r
11 using System.Globalization;\r
12 using System.IO;\r
13 using System.Drawing;\r
14 using System.Diagnostics;\r
15 using System.Text.RegularExpressions;\r
16 \r
17 namespace Handbrake.Functions\r
18 {\r
19     class Common\r
20     {\r
21         #region Presets\r
22 \r
23         /// <summary>\r
24         /// Update the presets.dat file with the latest version of HandBrak's presets from the CLI\r
25         /// </summary>\r
26         public void grabCLIPresets()\r
27         {\r
28             string handbrakeCLIPath = Path.Combine(Application.StartupPath, "HandBrakeCLI.exe");\r
29             string presetsPath = Path.Combine(Application.StartupPath, "presets.dat");\r
30 \r
31             string strCmdLine = String.Format(@"cmd /c """"{0}"" --preset-list >""{1}"" 2>&1""", handbrakeCLIPath, presetsPath);\r
32 \r
33             ProcessStartInfo hbGetPresets = new ProcessStartInfo("CMD.exe", strCmdLine);\r
34             hbGetPresets.WindowStyle = ProcessWindowStyle.Hidden;\r
35 \r
36             Process hbproc = Process.Start(hbGetPresets);\r
37             hbproc.WaitForExit();\r
38             hbproc.Dispose();\r
39             hbproc.Close();\r
40         }\r
41 \r
42         /// <summary>\r
43         /// This function takes in a Query which has been parsed by QueryParser and\r
44         /// set's all the GUI widgets correctly.\r
45         /// </summary>\r
46         /// <param name="mainWindow"></param>\r
47         /// <param name="presetQuery">The Parsed CLI Query</param>\r
48         /// <param name="name">Name of the preset</param>\r
49         public void presetLoader(frmMain mainWindow, Functions.QueryParser presetQuery, string name)\r
50         {\r
51             // ---------------------------\r
52             // Setup the GUI\r
53             // ---------------------------\r
54 \r
55             // Source tab\r
56             #region source\r
57             // Reset some vaules to stock first to prevent errors.\r
58             mainWindow.check_iPodAtom.CheckState = CheckState.Unchecked;\r
59 \r
60             // Now load all the new settings onto the main window\r
61             mainWindow.drp_dvdtitle.Text = "Automatic";\r
62             mainWindow.drop_chapterStart.Text = "Auto";\r
63             mainWindow.drop_chapterFinish.Text = "Auto";\r
64 \r
65             if (presetQuery.Format != null)\r
66             {\r
67                 string destination = mainWindow.text_destination.Text;\r
68                 destination = destination.Replace(".mp4", "." + presetQuery.Format);\r
69                 destination = destination.Replace(".m4v", "." + presetQuery.Format);\r
70                 destination = destination.Replace(".avi", "." + presetQuery.Format);\r
71                 destination = destination.Replace(".mkv", "." + presetQuery.Format);\r
72                 destination = destination.Replace(".ogm", "." + presetQuery.Format);\r
73                 mainWindow.text_destination.Text = destination;\r
74             }\r
75 \r
76             #endregion\r
77 \r
78             // Destination tab\r
79             #region destination\r
80 \r
81             mainWindow.drp_videoEncoder.Text = presetQuery.VideoEncoder;\r
82 \r
83             if (presetQuery.Format != null)\r
84             {\r
85                 if (presetQuery.Format == "mp4")\r
86                     mainWindow.drop_format.SelectedIndex = 0;\r
87                 else if (presetQuery.Format == "m4v")\r
88                     mainWindow.drop_format.SelectedIndex = 1;\r
89                 else if (presetQuery.Format == "mkv")\r
90                     mainWindow.drop_format.SelectedIndex = 2;\r
91                 else if (presetQuery.Format == "avi")\r
92                     mainWindow.drop_format.SelectedIndex = 3;\r
93                 else if (presetQuery.Format == "ogm")\r
94                     mainWindow.drop_format.SelectedIndex = 4;\r
95             }\r
96 \r
97             if (presetQuery.IpodAtom == true)\r
98                 mainWindow.check_iPodAtom.CheckState = CheckState.Checked;\r
99             else\r
100                 mainWindow.check_iPodAtom.CheckState = CheckState.Unchecked;\r
101 \r
102             if (presetQuery.OptimizeMP4 == true)\r
103                 mainWindow.check_optimiseMP4.CheckState = CheckState.Checked;\r
104             else\r
105                 mainWindow.check_optimiseMP4.CheckState = CheckState.Unchecked;\r
106 \r
107             #endregion\r
108 \r
109             // Picture Settings Tab\r
110             #region Picture\r
111 \r
112             if (presetQuery.CropTop == "0" && presetQuery.CropBottom == "0" && presetQuery.CropLeft == "0" && presetQuery.CropRight == "0")\r
113             {\r
114                 mainWindow.check_customCrop.Checked = true;\r
115                 mainWindow.text_top.Text = "0";\r
116                 mainWindow.text_bottom.Text = "0";\r
117                 mainWindow.text_left.Text = "0";\r
118                 mainWindow.text_right.Text = "0";\r
119             }\r
120             else if (presetQuery.CropTop != null && presetQuery.CropBottom != null && presetQuery.CropLeft != null && presetQuery.CropRight != null)\r
121             {\r
122                 mainWindow.check_customCrop.Checked = true;\r
123                 mainWindow.text_top.Text = presetQuery.CropTop;\r
124                 mainWindow.text_bottom.Text = presetQuery.CropBottom;\r
125                 mainWindow.text_left.Text = presetQuery.CropLeft;\r
126                 mainWindow.text_right.Text = presetQuery.CropRight;\r
127             }\r
128             else\r
129                 mainWindow.check_autoCrop.Checked = true;\r
130 \r
131             mainWindow.drp_deInterlace_option.Text = presetQuery.DeInterlace;\r
132             mainWindow.drp_deNoise.Text = presetQuery.DeNoise;\r
133 \r
134             if (presetQuery.Decomb == true)\r
135                 mainWindow.check_decomb.CheckState = CheckState.Checked;\r
136             else\r
137                 mainWindow.check_decomb.CheckState = CheckState.Unchecked;\r
138 \r
139             if (presetQuery.DeTelecine == true)\r
140                 mainWindow.check_detelecine.CheckState = CheckState.Checked;\r
141             else\r
142                 mainWindow.check_detelecine.CheckState = CheckState.Unchecked;\r
143 \r
144             if (presetQuery.DeBlock != 0)\r
145             {\r
146                 mainWindow.slider_deblock.Value = presetQuery.DeBlock;\r
147                 mainWindow.lbl_deblockVal.Text = presetQuery.DeBlock.ToString();\r
148             }\r
149             else\r
150             {\r
151                 mainWindow.slider_deblock.Value = 0;\r
152                 mainWindow.lbl_deblockVal.Text = "0";\r
153             }\r
154 \r
155 \r
156 \r
157             if (presetQuery.Anamorphic == true)\r
158                 mainWindow.drp_anamorphic.SelectedIndex = 1;\r
159             else\r
160                 mainWindow.drp_anamorphic.SelectedIndex = 0;\r
161 \r
162             if (presetQuery.LooseAnamorphic == true)\r
163                 mainWindow.drp_anamorphic.SelectedIndex = 2;\r
164             else\r
165             {\r
166                 if (presetQuery.Anamorphic != true)\r
167                     mainWindow.drp_anamorphic.SelectedIndex = 0;\r
168             }\r
169 \r
170             if (presetQuery.Width != 0)\r
171                 mainWindow.text_width.Text = presetQuery.Width.ToString();\r
172             else\r
173             {\r
174                 mainWindow.text_width.Text = "";\r
175             }\r
176 \r
177             if (presetQuery.Height != 0)\r
178                 mainWindow.text_height.Text = presetQuery.Height.ToString();\r
179             else\r
180             {\r
181                 mainWindow.text_height.Text = "";\r
182             }\r
183 \r
184             if (presetQuery.VFR == true)\r
185                 mainWindow.check_vfr.CheckState = CheckState.Checked;\r
186             else\r
187                 mainWindow.check_vfr.CheckState = CheckState.Unchecked;\r
188             #endregion\r
189 \r
190             // Video Settings Tab\r
191             #region video\r
192             mainWindow.text_bitrate.Text = presetQuery.AverageVideoBitrate;\r
193             mainWindow.text_filesize.Text = presetQuery.VideoTargetSize;\r
194             mainWindow.slider_videoQuality.Value = presetQuery.VideoQuality;\r
195             if (mainWindow.slider_videoQuality.Value != 0)\r
196             {\r
197                 int ql = presetQuery.VideoQuality;\r
198                 mainWindow.SliderValue.Text = ql.ToString() + "%";\r
199             }\r
200 \r
201             if (presetQuery.TwoPass == true)\r
202                 mainWindow.check_2PassEncode.CheckState = CheckState.Checked;\r
203             else\r
204                 mainWindow.check_2PassEncode.CheckState = CheckState.Unchecked;\r
205 \r
206             if (presetQuery.Grayscale == true)\r
207                 mainWindow.check_grayscale.CheckState = CheckState.Checked;\r
208             else\r
209                 mainWindow.check_grayscale.CheckState = CheckState.Unchecked;\r
210 \r
211             mainWindow.drp_videoFramerate.Text = presetQuery.VideoFramerate;\r
212 \r
213             if (presetQuery.TurboFirstPass == true)\r
214                 mainWindow.check_turbo.CheckState = CheckState.Checked;\r
215             else\r
216                 mainWindow.check_turbo.CheckState = CheckState.Unchecked;\r
217 \r
218             if (presetQuery.LargeMP4 == true)\r
219                 mainWindow.check_largeFile.CheckState = CheckState.Checked;\r
220             else\r
221             {\r
222                 mainWindow.check_largeFile.CheckState = CheckState.Unchecked;\r
223                 mainWindow.check_largeFile.BackColor = Color.Transparent;\r
224             }\r
225 \r
226 \r
227 \r
228             #endregion\r
229 \r
230             // Chapter Markers Tab\r
231             #region Chapter Markers\r
232 \r
233             if (presetQuery.ChapterMarkers == true)\r
234             {\r
235                 mainWindow.Check_ChapterMarkers.CheckState = CheckState.Checked;\r
236                 mainWindow.text_destination.Text = mainWindow.text_destination.Text.Replace(".mp4", ".m4v");\r
237             }\r
238             else\r
239                 mainWindow.Check_ChapterMarkers.CheckState = CheckState.Unchecked;\r
240 \r
241             #endregion\r
242 \r
243             // Audio Settings Tab\r
244             #region Audio\r
245 \r
246             // Handle Track 1\r
247             mainWindow.drp_track1Audio.Text = "Automatic";\r
248 \r
249             // Handle Track 2\r
250             if (presetQuery.AudioEncoder2 != null)  // Fix for loading in built in presets. Where 2 encoders but no tracks in the preset.\r
251             {\r
252                 mainWindow.drp_track2Audio.Enabled = true;\r
253                 mainWindow.drp_audsr_2.Enabled = true;\r
254                 mainWindow.drp_audmix_2.Enabled = true;\r
255                 mainWindow.drp_audenc_2.Enabled = true;\r
256                 mainWindow.drp_audbit_2.Enabled = true;\r
257                 mainWindow.drp_audsr_2.Text = "48";\r
258                 mainWindow.drp_track2Audio.Text = "Automatic";\r
259             }\r
260             else if (presetQuery.AudioTrack2 == "None")\r
261             {\r
262                 mainWindow.drp_track2Audio.Text = "None";\r
263                 mainWindow.drp_track2Audio.SelectedIndex = 0;\r
264                 mainWindow.drp_audsr_2.Enabled = false;\r
265                 mainWindow.drp_audmix_2.Enabled = false;\r
266                 mainWindow.drp_audenc_2.Enabled = false;\r
267                 mainWindow.drp_audbit_2.Enabled = false;\r
268             }\r
269             else\r
270             {\r
271                 mainWindow.drp_track2Audio.Text = presetQuery.AudioTrack2;\r
272                 mainWindow.drp_audsr_2.Enabled = true;\r
273                 mainWindow.drp_audmix_2.Enabled = true;\r
274                 mainWindow.drp_audenc_2.Enabled = true;\r
275                 mainWindow.drp_audbit_2.Enabled = true;\r
276             }\r
277 \r
278             // Handle Track 3\r
279             if (presetQuery.AudioTrack3 == "None")\r
280             {\r
281                 mainWindow.drp_track3Audio.SelectedIndex = 0;\r
282                 mainWindow.drp_audsr_3.Enabled = false;\r
283                 mainWindow.drp_audmix_3.Enabled = false;\r
284                 mainWindow.drp_audenc_3.Enabled = false;\r
285                 mainWindow.drp_audbit_3.Enabled = false;\r
286                 mainWindow.trackBar3.Enabled = false;\r
287 \r
288                 mainWindow.drp_track3Audio.Text = "None";\r
289                 mainWindow.drp_audsr_3.Text = "";\r
290                 mainWindow.drp_audmix_3.Text = "Automatic";\r
291                 mainWindow.drp_audenc_3.Text = "";\r
292                 mainWindow.drp_audbit_3.Text = "";\r
293                 mainWindow.trackBar3.Value = 0;\r
294 \r
295             }\r
296             else\r
297             {\r
298                 mainWindow.drp_track3Audio.Text = presetQuery.AudioTrack3;\r
299                 mainWindow.drp_audsr_3.Enabled = true;\r
300                 mainWindow.drp_audmix_3.Enabled = true;\r
301                 mainWindow.drp_audenc_3.Enabled = true;\r
302                 mainWindow.drp_audbit_3.Enabled = true;\r
303                 mainWindow.trackBar3.Enabled = true;\r
304             }\r
305 \r
306             // Handle Track 4\r
307             if (presetQuery.AudioTrack4 == "None")\r
308             {\r
309                 mainWindow.drp_track4Audio.SelectedIndex = 0;\r
310                 mainWindow.drp_audsr_4.Enabled = false;\r
311                 mainWindow.drp_audmix_4.Enabled = false;\r
312                 mainWindow.drp_audenc_4.Enabled = false;\r
313                 mainWindow.drp_audbit_4.Enabled = false;\r
314                 mainWindow.trackBar4.Enabled = false;\r
315 \r
316                 mainWindow.drp_track4Audio.Text = "None";\r
317                 mainWindow.drp_audsr_4.Text = "";\r
318                 mainWindow.drp_audmix_4.Text = "Automatic";\r
319                 mainWindow.drp_audenc_4.Text = "";\r
320                 mainWindow.drp_audbit_4.Text = "";\r
321                 mainWindow.trackBar4.Value = 0;\r
322             }\r
323             else\r
324             {\r
325                 mainWindow.drp_track4Audio.Text = presetQuery.AudioTrack4;\r
326                 mainWindow.drp_audsr_4.Enabled = true;\r
327                 mainWindow.drp_audmix_4.Enabled = true;\r
328                 mainWindow.drp_audenc_4.Enabled = true;\r
329                 mainWindow.drp_audbit_4.Enabled = true;\r
330                 mainWindow.trackBar4.Enabled = true;\r
331             }\r
332 \r
333             // Now lets start setting stuff\r
334             if (presetQuery.AudioEncoder1 != null)\r
335                 mainWindow.drp_audenc_1.Text = presetQuery.AudioEncoder1;\r
336             mainWindow.drp_audenc_2.Text = presetQuery.AudioEncoder2;\r
337             mainWindow.drp_audenc_3.Text = presetQuery.AudioEncoder3;\r
338             mainWindow.drp_audenc_4.Text = presetQuery.AudioEncoder4;\r
339 \r
340             if (presetQuery.AudioBitrate1 != null)\r
341                 mainWindow.drp_audbit_1.Text = presetQuery.AudioBitrate1;\r
342             mainWindow.drp_audbit_2.Text = presetQuery.AudioBitrate2;\r
343             mainWindow.drp_audbit_3.Text = presetQuery.AudioBitrate4;\r
344             mainWindow.drp_audbit_3.Text = presetQuery.AudioBitrate4;\r
345 \r
346             if (presetQuery.AudioSamplerate1 != null)\r
347                 mainWindow.drp_audsr_1.Text = presetQuery.AudioSamplerate1;\r
348             mainWindow.drp_audsr_2.Text = presetQuery.AudioSamplerate2;\r
349             mainWindow.drp_audsr_3.Text = presetQuery.AudioSamplerate3;\r
350             mainWindow.drp_audsr_4.Text = presetQuery.AudioSamplerate4;\r
351 \r
352             mainWindow.drp_audmix_1.Text = presetQuery.AudioTrackMix1;\r
353             mainWindow.drp_audmix_2.Text = presetQuery.AudioTrackMix2;\r
354             mainWindow.drp_audmix_3.Text = presetQuery.AudioTrackMix3;\r
355             mainWindow.drp_audmix_4.Text = presetQuery.AudioTrackMix4;\r
356 \r
357 \r
358             // Dynamic Range Compression (Should be a float but we use double for ease)\r
359             double value = 0;\r
360             double actualValue = 0;\r
361 \r
362             value = presetQuery.DRC1;\r
363             if (value > 0)\r
364                 value = value - 10;\r
365             mainWindow.trackBar1.Value = int.Parse(value.ToString());\r
366             actualValue = presetQuery.DRC1 / 10;\r
367             mainWindow.lbl_drc1.Text = actualValue.ToString();\r
368 \r
369             value = presetQuery.DRC2;\r
370             if (value > 0)\r
371                 value = value - 10;\r
372             mainWindow.trackBar2.Value = int.Parse(value.ToString());\r
373             actualValue = presetQuery.DRC2 / 10;\r
374             mainWindow.lbl_drc2.Text = actualValue.ToString();\r
375 \r
376             value = presetQuery.DRC3;\r
377             if (value > 0)\r
378                 value = value - 10;\r
379             mainWindow.trackBar3.Value = int.Parse(value.ToString());\r
380             actualValue = presetQuery.DRC3 / 10;\r
381             mainWindow.lbl_drc3.Text = actualValue.ToString();\r
382 \r
383             value = presetQuery.DRC4;\r
384             if (value > 0)\r
385                 value = value - 10;\r
386             mainWindow.trackBar4.Value = int.Parse(value.ToString());\r
387             actualValue = presetQuery.DRC4 / 10;\r
388             mainWindow.lbl_drc4.Text = actualValue.ToString();\r
389 \r
390 \r
391             // Subtitle Stuff\r
392             mainWindow.drp_subtitle.Text = presetQuery.Subtitles;\r
393 \r
394             if (presetQuery.ForcedSubtitles == true)\r
395             {\r
396                 mainWindow.check_forced.CheckState = CheckState.Checked;\r
397                 mainWindow.check_forced.Enabled = true;\r
398             }\r
399             else\r
400                 mainWindow.check_forced.CheckState = CheckState.Unchecked;\r
401 \r
402 \r
403             #endregion\r
404 \r
405             // H264 Tab & Preset Name\r
406             #region other\r
407             mainWindow.rtf_x264Query.Text = presetQuery.H264Query;\r
408 \r
409             // Set the preset name\r
410             mainWindow.groupBox_output.Text = "Output Settings (Preset: " + name + ")";\r
411             #endregion\r
412         }\r
413 \r
414         #endregion\r
415 \r
416         #region Query Generator Functions\r
417 \r
418         /// <summary>\r
419         /// Generates a CLI query based on the GUI widgets.\r
420         /// </summary>\r
421         /// <param name="mainWindow"></param>\r
422         /// <returns>The CLI String</returns>\r
423         public string GenerateTheQuery(frmMain mainWindow)\r
424         {\r
425             // Source tab\r
426             #region source\r
427             string query = "";\r
428 \r
429             if ((mainWindow.text_source.Text != "") && (mainWindow.text_source.Text.Trim() != "Click 'Source' to continue"))\r
430                 query = " -i " + '"' + mainWindow.text_source.Text + '"';\r
431 \r
432             if (mainWindow.drp_dvdtitle.Text != "Automatic")\r
433             {\r
434                 string[] titleInfo = mainWindow.drp_dvdtitle.Text.Split(' ');\r
435                 query += " -t " + titleInfo[0];\r
436             }\r
437 \r
438             if (mainWindow.drop_chapterFinish.Text == mainWindow.drop_chapterStart.Text && mainWindow.drop_chapterStart.Text != "Auto")\r
439                 query += " -c " + mainWindow.drop_chapterStart.Text;\r
440             else if (mainWindow.drop_chapterStart.Text == "Auto" && mainWindow.drop_chapterFinish.Text != "Auto")\r
441                 query += " -c " + "0-" + mainWindow.drop_chapterFinish.Text;\r
442             else if (mainWindow.drop_chapterStart.Text != "Auto" && mainWindow.drop_chapterFinish.Text != "Auto")\r
443                 query += " -c " + mainWindow.drop_chapterStart.Text + "-" + mainWindow.drop_chapterFinish.Text;\r
444 \r
445             #endregion\r
446 \r
447             // Destination tab\r
448             #region Destination\r
449             if (mainWindow.text_destination.Text != "")\r
450                 query += " -o " + '"' + mainWindow.text_destination.Text + '"';\r
451             #endregion\r
452 \r
453             query += generateTabbedComponentsQuery(mainWindow, mainWindow.text_source.Text);\r
454             return query;\r
455         }\r
456 \r
457         /// <summary>\r
458         /// Generates a CLI query for the preview function.\r
459         /// This basically forces a shortened version of the encdode.\r
460         /// </summary>\r
461         /// <param name="mainWindow"></param>\r
462         /// <returns>Returns a CLI query String.</returns>\r
463         public string GeneratePreviewQuery(frmMain mainWindow)\r
464         {\r
465             // Source tab\r
466             #region source\r
467             string query = "";\r
468 \r
469             if ((mainWindow.text_source.Text != "") && (mainWindow.text_source.Text.Trim() != "Click 'Source' to continue"))\r
470                 query = " -i " + '"' + mainWindow.text_source.Text + '"';\r
471 \r
472             if (mainWindow.drp_dvdtitle.Text != "Automatic")\r
473             {\r
474                 string[] titleInfo = mainWindow.drp_dvdtitle.Text.Split(' ');\r
475                 query += " -t " + titleInfo[0];\r
476             }\r
477 \r
478             query += " -c 2";\r
479             #endregion\r
480 \r
481             // Destination tab\r
482             #region Destination\r
483             if (mainWindow.text_destination.Text != "")\r
484                 query += " -o " + '"' + mainWindow.text_destination.Text.Replace(".m", "_sample.m").Replace(".avi", "_sample.avi").Replace(".ogm", "_sample.ogm") + '"';\r
485 \r
486             #endregion\r
487 \r
488             query += generateTabbedComponentsQuery(mainWindow, mainWindow.text_source.Text);\r
489             return query;\r
490         }\r
491 \r
492         /// <summary>\r
493         /// Generates part of the CLI query, for the tabbed components only.\r
494         /// </summary>\r
495         /// <param name="mainWindow"></param>\r
496         /// <param name="source"></param>\r
497         /// <returns></returns>\r
498         private string generateTabbedComponentsQuery(frmMain mainWindow, string source)\r
499         {\r
500             string query = "";\r
501 \r
502             // Picture Settings Tab\r
503             #region Picture Settings Tab\r
504 \r
505             switch (mainWindow.drp_videoEncoder.Text)\r
506             {\r
507                 case "MPEG-4 (FFmpeg)":\r
508                     query += " -e ffmpeg";\r
509                     break;\r
510                 case "MPEG-4 (XviD)":\r
511                     query += " -e xvid";\r
512                     break;\r
513                 case "H.264 (x264)":\r
514                     query += " -e x264";\r
515                     break;\r
516                 case "VP3 (Theora)":\r
517                     query += " -e theora";\r
518                     break;\r
519                 default:\r
520                     query += " -e x264";\r
521                     break;\r
522             }\r
523 \r
524             if (mainWindow.text_width.Text != "")\r
525                 query += " -w " + mainWindow.text_width.Text;\r
526 \r
527             if (mainWindow.text_height.Text != "")\r
528                 query += " -l " + mainWindow.text_height.Text;\r
529 \r
530             string cropTop = mainWindow.text_top.Text;\r
531             string cropBottom = mainWindow.text_bottom.Text;\r
532             string cropLeft = mainWindow.text_left.Text;\r
533             string cropRight = mainWindow.text_right.Text;\r
534 \r
535             if (mainWindow.check_customCrop.Checked)\r
536             {\r
537                 if (mainWindow.text_top.Text == string.Empty)\r
538                     cropTop = "0";\r
539                 if (mainWindow.text_bottom.Text == string.Empty)\r
540                     cropBottom = "0";\r
541                 if (mainWindow.text_left.Text == string.Empty)\r
542                     cropLeft = "0";\r
543                 if (mainWindow.text_right.Text == string.Empty)\r
544                     cropRight = "0";\r
545 \r
546                 query += " --crop " + cropTop + ":" + cropBottom + ":" + cropLeft + ":" + cropRight;\r
547             }\r
548 \r
549             switch (mainWindow.drp_deInterlace_option.Text)\r
550             {\r
551                 case "None":\r
552                     query += "";\r
553                     break;\r
554                 case "Fast":\r
555                     query += " --deinterlace=\"fast\"";\r
556                     break;\r
557                 case "Slow":\r
558                     query += " --deinterlace=\"slow\"";\r
559                     break;\r
560                 case "Slower":\r
561                     query += " --deinterlace=\"slower\"";\r
562                     break;\r
563                 case "Slowest":\r
564                     query += " --deinterlace=\"slowest\"";\r
565                     break;\r
566                 default:\r
567                     query += "";\r
568                     break;\r
569             }\r
570 \r
571             if (mainWindow.check_decomb.Checked)\r
572             {\r
573                 string decombValue = Properties.Settings.Default.decomb;\r
574                 if (decombValue != "" && decombValue != Properties.Settings.Default.default_decomb)\r
575                     query += " --decomb=\"" + decombValue + "\"";\r
576                 else\r
577                     query += " --decomb ";\r
578             }\r
579 \r
580             if (mainWindow.check_grayscale.Checked)\r
581                 query += " -g ";\r
582 \r
583             if (mainWindow.drp_anamorphic.SelectedIndex == 1)\r
584                 query += " -p ";\r
585             else if (mainWindow.drp_anamorphic.SelectedIndex == 2)\r
586                 query += " -P ";\r
587 \r
588             if (mainWindow.slider_deblock.Value != 0)\r
589                 query += " --deblock=" + mainWindow.slider_deblock.Value;\r
590 \r
591             if (mainWindow.check_detelecine.Checked)\r
592                 query += " --detelecine";\r
593 \r
594             if (mainWindow.check_vfr.Checked)\r
595                 query += " -V ";\r
596             #endregion\r
597 \r
598             // Video Settings Tab\r
599             #region Video Settings Tab\r
600             // These are output settings features\r
601             if (mainWindow.check_largeFile.Checked)\r
602                 query += " -4 ";\r
603 \r
604             if (mainWindow.check_iPodAtom.Checked)\r
605                 query += " -I ";\r
606 \r
607             if (mainWindow.check_optimiseMP4.Checked)\r
608                 query += " -O ";\r
609 \r
610             // Video Settings\r
611             if (mainWindow.text_bitrate.Text != "")\r
612                 query += " -b " + mainWindow.text_bitrate.Text;\r
613 \r
614             if (mainWindow.text_filesize.Text != "")\r
615                 query += " -S " + mainWindow.text_filesize.Text;\r
616 \r
617             // Video Quality Setting\r
618             double videoQuality = mainWindow.slider_videoQuality.Value;\r
619             if (videoQuality != 0)\r
620             {\r
621                 videoQuality = videoQuality / 100;\r
622                 query += " -q " + videoQuality.ToString(new CultureInfo("en-US"));\r
623             }\r
624 \r
625             if (mainWindow.check_2PassEncode.Checked)\r
626                 query += " -2 ";\r
627 \r
628             if (mainWindow.drp_videoFramerate.Text != "Same as source")\r
629             {\r
630                 if (!mainWindow.check_vfr.Checked)\r
631                     query += " -r " + mainWindow.drp_videoFramerate.Text;\r
632             }\r
633 \r
634             if (mainWindow.check_turbo.Checked)\r
635                 query += " -T ";\r
636 \r
637 \r
638 \r
639             switch (mainWindow.drp_deNoise.Text)\r
640             {\r
641                 case "None":\r
642                     query += "";\r
643                     break;\r
644                 case "Weak":\r
645                     query += " --denoise=\"weak\"";\r
646                     break;\r
647                 case "Medium":\r
648                     query += " --denoise=\"medium\"";\r
649                     break;\r
650                 case "Strong":\r
651                     query += " --denoise=\"strong\"";\r
652                     break;\r
653                 default:\r
654                     query += "";\r
655                     break;\r
656             }\r
657             #endregion\r
658 \r
659             // Audio Settings Tab\r
660             #region Audio Settings Tab\r
661             // Track 1\r
662             string track1 = mainWindow.drp_track1Audio.Text;\r
663             string aencoder1 = mainWindow.drp_audenc_1.Text;\r
664             string audioBitrate1 = mainWindow.drp_audbit_1.Text;\r
665             string audioSampleRate1 = mainWindow.drp_audsr_1.Text;\r
666             string Mixdown1 = mainWindow.drp_audmix_1.Text;\r
667             string drc1 = mainWindow.trackBar1.Value.ToString();\r
668 \r
669             // Track 2\r
670             string track2 = mainWindow.drp_track2Audio.Text;\r
671             string aencoder2 = mainWindow.drp_audenc_2.Text;\r
672             string audioBitrate2 = mainWindow.drp_audbit_2.Text;\r
673             string audioSampleRate2 = mainWindow.drp_audsr_2.Text;\r
674             string Mixdown2 = mainWindow.drp_audmix_2.Text;\r
675             string drc2 = mainWindow.trackBar2.Value.ToString();\r
676 \r
677             // Track 3\r
678             string track3 = mainWindow.drp_track3Audio.Text;\r
679             string aencoder3 = mainWindow.drp_audenc_3.Text;\r
680             string audioBitrate3 = mainWindow.drp_audbit_3.Text;\r
681             string audioSampleRate3 = mainWindow.drp_audsr_3.Text;\r
682             string Mixdown3 = mainWindow.drp_audmix_3.Text;\r
683             string drc3 = mainWindow.trackBar3.Value.ToString();\r
684 \r
685             // Track 4\r
686             string track4 = mainWindow.drp_track4Audio.Text;\r
687             string aencoder4 = mainWindow.drp_audenc_4.Text;\r
688             string audioBitrate4 = mainWindow.drp_audbit_4.Text;\r
689             string audioSampleRate4 = mainWindow.drp_audsr_4.Text;\r
690             string Mixdown4 = mainWindow.drp_audmix_4.Text;\r
691             string drc4 = mainWindow.trackBar4.Value.ToString();\r
692 \r
693 \r
694             //\r
695             // Audio Track Selections\r
696             //\r
697             if (track1 == "Automatic")\r
698                 query += " -a 1";\r
699             else if (track1 != "None")\r
700             {\r
701                 string[] tempSub = track1.Split(' ');\r
702                 query += " -a " + tempSub[0];\r
703             }\r
704 \r
705             if (track2 == "Automatic")\r
706                 query += ",1";\r
707             else if (track2 != "None")\r
708             {\r
709                 string[] tempSub;\r
710                 tempSub = track2.Split(' ');\r
711 \r
712                 if (track1 == "None")\r
713                     query += " -a none," + tempSub[0];\r
714                 else\r
715                     query += "," + tempSub[0];\r
716             }\r
717 \r
718             if (track3 != "None")\r
719             {\r
720                 string[] tempSub;\r
721                 tempSub = track3.Split(' ');\r
722                 query += "," + tempSub[0];\r
723             }\r
724 \r
725             if (track4 != "None")\r
726             {\r
727                 string[] tempSub;\r
728                 tempSub = track4.Split(' ');\r
729                 query += "," + tempSub[0];\r
730             }\r
731 \r
732             //\r
733             // Audio Encoder\r
734             //\r
735             if (aencoder1 != "")\r
736                 query += " -E " + getAudioEncoder(aencoder1);\r
737 \r
738             if (aencoder2 != "")\r
739             {\r
740                 if (aencoder1 == string.Empty)\r
741                     query += " -E faac," + getAudioEncoder(aencoder2);\r
742                 else\r
743                     query += "," + getAudioEncoder(aencoder2);\r
744             }\r
745 \r
746             if (aencoder3 != "")\r
747                 query += "," + getAudioEncoder(aencoder3);\r
748 \r
749             if (aencoder4 != "")\r
750                 query += "," + getAudioEncoder(aencoder4);\r
751 \r
752             //\r
753             // Audio Bitrate Selections\r
754             //\r
755             if (audioBitrate1 != "")\r
756                 query += " -B " + audioBitrate1;\r
757 \r
758             if (audioBitrate2 != "")\r
759             {\r
760                 if (audioBitrate1 == string.Empty)\r
761                     query += " -B 160," + audioBitrate2;\r
762                 else\r
763                     query += "," + audioBitrate2;\r
764             }\r
765 \r
766             if (audioBitrate3 != "")\r
767                 query += "," + audioBitrate3;\r
768 \r
769             if (audioBitrate4 != "")\r
770                 query += "," + audioBitrate4;\r
771 \r
772 \r
773             //Audio Sample Rate   - audioSampleRate\r
774             if (audioSampleRate1 != "")\r
775                 query += " -R " + audioSampleRate1.Replace("Auto", "0");\r
776 \r
777             if (audioSampleRate2 != "")\r
778             {\r
779                 if (audioSampleRate1 == string.Empty)\r
780                     query += " -R 0," + audioSampleRate2.Replace("Auto", "0");\r
781                 else\r
782                     query += "," + audioSampleRate2.Replace("Auto", "0");\r
783             }\r
784             else\r
785             {\r
786                 // All this is a hack, because when AppleTV is selected, there is no sample rate selected. so just add a 48\r
787                 // It should probably be setup later so the GUI widget has the value 48 in it.\r
788 \r
789                 if ((track2 != "") && (track2 != "None"))\r
790                 {\r
791                     if (audioSampleRate1 == string.Empty)\r
792                         query += " -R 0,0";\r
793                     else\r
794                         query += ",0";\r
795                 }\r
796             }\r
797 \r
798             if (audioSampleRate3 != "")\r
799                 query += "," + audioSampleRate3.Replace("Auto", "0");\r
800 \r
801             if (audioSampleRate4 != "")\r
802                 query += "," + audioSampleRate4.Replace("Auto", "0");\r
803 \r
804             //\r
805             // Audio Mixdown Selections\r
806             //\r
807 \r
808             if (Mixdown1 != "")\r
809                 query += " -6 " + getMixDown(Mixdown1);\r
810             else\r
811                 query += " -6 dpl2";\r
812 \r
813             if (Mixdown2 != "" && track2 != "None")\r
814                 query += "," + getMixDown(Mixdown2);\r
815 \r
816             if (Mixdown3 != "" && track3 != "None" && track2 != "None")\r
817                 query += "," + getMixDown(Mixdown3);\r
818 \r
819             if (Mixdown4 != "" && track4 != "None" && track3 != "None")\r
820                 query += "," + getMixDown(Mixdown4);\r
821 \r
822 \r
823             //\r
824             // DRC\r
825             //\r
826             double value = 0;\r
827 \r
828             value = mainWindow.trackBar1.Value / 10.0;\r
829             value++;\r
830 \r
831             if (value > 1.0)\r
832                 query += " -D " + value;\r
833             else\r
834                 query += " -D 1";\r
835 \r
836             value = mainWindow.trackBar2.Value / 10.0;\r
837             value++;\r
838             if (track2 != "None" && drc2 != "0")\r
839                 query += "," + value;\r
840             else if (track2 != "None" && drc2 == "0")\r
841                 query += ",1";\r
842 \r
843             value = mainWindow.trackBar3.Value / 10.0;\r
844             value++;\r
845             if (track3 != "None" && drc3 != "0")\r
846                 query += "," + value;\r
847             else if (track3 != "None" && drc3 == "0")\r
848                 query += ",1";\r
849 \r
850             value = mainWindow.trackBar4.Value / 10.0;\r
851             value++;\r
852             if (track4 != "None" && drc4 != "0")\r
853                 query += "," + value;\r
854             else if (track4 != "None" && drc4 == "0")\r
855                 query += ",1";\r
856 \r
857             // Subtitles\r
858             string subtitles = mainWindow.drp_subtitle.Text;\r
859             if (subtitles == "Autoselect")\r
860                 query += " -U ";\r
861             else if (subtitles != "" && subtitles != "None")\r
862             {\r
863                 string[] tempSub;\r
864                 tempSub = subtitles.Split(' ');\r
865                 query += " -s " + tempSub[0];\r
866             }\r
867 \r
868             if (mainWindow.check_forced.Checked)\r
869                 query += " -F ";\r
870 \r
871             #endregion\r
872 \r
873             // Chapter Markers Tab\r
874             #region Chapter Markers\r
875 \r
876             // Attach Source name and dvd title to the start of the chapters.csv filename.\r
877             // This is for the queue. It allows different chapter name files for each title.\r
878             string source_name = mainWindow.text_source.Text;\r
879             string[] sourceName = source.Split('\\');\r
880             source_name = sourceName[sourceName.Length - 1].Replace(".iso", "").Replace(".mpg", "").Replace(".ts", "").Replace(".ps", "");\r
881             source_name = source_name.Replace("\"", "");\r
882 \r
883             string source_title = mainWindow.drp_dvdtitle.Text;\r
884             string[] titlesplit = source_title.Split(' ');\r
885             source_title = titlesplit[0];\r
886 \r
887             if (mainWindow.Check_ChapterMarkers.Checked)\r
888             {\r
889                 if ((source_name.Trim().Replace("-i ", "") != "Click 'Browse' to continue") && (source_name.Trim().Replace("-i ", "") != ""))\r
890                 {\r
891                     if (source_title != "Automatic")\r
892                     {\r
893                         string filename = source_name + "-" + source_title + "-chapters.csv";\r
894                         string path = Path.Combine(Path.GetTempPath(), filename);\r
895 \r
896                         Boolean saveCSV = chapterCSVSave(mainWindow, path);\r
897                         if (saveCSV == false)\r
898                             query += " -m ";\r
899                         else\r
900                             query += " --markers=" + "\"" + path + "\"";\r
901                     }\r
902                     else\r
903                     {\r
904                         string filename = source_name + "-chapters.csv";\r
905                         string path = Path.Combine(Path.GetTempPath(), filename);\r
906 \r
907                         Boolean saveCSV = chapterCSVSave(mainWindow, path);\r
908                         if (saveCSV == false)\r
909                             query += " -m ";\r
910                         else\r
911                             query += " --markers=" + "\"" + path + "\"";\r
912                     }\r
913                 }\r
914                 else\r
915                 {\r
916                     string path = Path.Combine(Path.GetTempPath(), "chapters.csv");\r
917                     query += " --markers=" + "\"" + path + "\"";\r
918                 }\r
919             }\r
920             #endregion\r
921 \r
922             // H264 Tab\r
923             #region  H264 Tab\r
924             if (mainWindow.rtf_x264Query.Text != "")\r
925                 query += " -x " + mainWindow.rtf_x264Query.Text;\r
926             #endregion\r
927 \r
928             // Other\r
929             #region Processors / Other\r
930             string processors = Properties.Settings.Default.Processors;\r
931             if (processors != "Automatic")\r
932                 query += " -C " + processors + " ";\r
933 \r
934             query += " -v ";\r
935             #endregion\r
936 \r
937             return query;\r
938         }\r
939 \r
940         /// <summary>\r
941         /// Get the CLI equive of the audio mixdown from the widget name.\r
942         /// </summary>\r
943         /// <param name="selectedAudio"></param>\r
944         /// <returns></returns>\r
945         /// \r
946         private string getMixDown(string selectedAudio)\r
947         {\r
948             switch (selectedAudio)\r
949             {\r
950                 case "Automatic":\r
951                     return "dpl2";\r
952                 case "Mono":\r
953                     return "mono";\r
954                 case "Stereo":\r
955                     return "stereo";\r
956                 case "Dolby Surround":\r
957                     return "dpl1";\r
958                 case "Dolby Pro Logic II":\r
959                     return "dpl2";\r
960                 case "6 Channel Discrete":\r
961                     return "6ch";\r
962                 default:\r
963                     return "dpl2";\r
964             }\r
965         }\r
966 \r
967         /// <summary>\r
968         /// Get the CLI equiv of the audio encoder from the widget name.\r
969         /// </summary>\r
970         /// <param name="selectedEncoder"></param>\r
971         /// <returns></returns>\r
972         /// \r
973         private string getAudioEncoder(string selectedEncoder)\r
974         {\r
975             switch (selectedEncoder)\r
976             {\r
977                 case "AAC":\r
978                     return "faac";\r
979                 case "MP3":\r
980                     return "lame";\r
981                 case "Vorbis":\r
982                     return "vorbis";\r
983                 case "AC3":\r
984                     return "ac3";\r
985                 default:\r
986                     return "";\r
987             }\r
988         }\r
989 \r
990         /// <summary>\r
991         /// This function saves the data in the chapters tab, dataGridView into a CSV file called chapters.csv\r
992         /// in a directory specified by file_path_name\r
993         /// </summary>\r
994         /// <param name="mainWindow"></param>\r
995         /// <param name="file_path_name"></param>\r
996         /// <returns></returns>\r
997         private Boolean chapterCSVSave(frmMain mainWindow, string file_path_name)\r
998         {\r
999             try\r
1000             {\r
1001                 StringBuilder csv = new StringBuilder();\r
1002 \r
1003                 foreach (DataGridViewRow row in mainWindow.data_chpt.Rows)\r
1004                 {\r
1005                     csv.Append(row.Cells[0].Value.ToString());\r
1006                     csv.Append(",");\r
1007                     csv.Append(row.Cells[1].Value.ToString());\r
1008                     csv.Append(Environment.NewLine);\r
1009                 }\r
1010                 StreamWriter file = new StreamWriter(file_path_name);\r
1011                 file.Write(csv.ToString());\r
1012                 file.Close();\r
1013                 file.Dispose();\r
1014                 return true;\r
1015 \r
1016             }\r
1017             catch (Exception exc)\r
1018             {\r
1019                 MessageBox.Show("Unable to save Chapter Makrers file! \nChapter marker names will NOT be saved in your encode \n\n" + exc.ToString(), "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
1020                 return false;\r
1021             }\r
1022         }\r
1023 \r
1024         #endregion\r
1025 \r
1026         #region Actions, Versioning etc\r
1027 \r
1028         /// <summary>\r
1029         /// Select the longest title in the DVD title dropdown menu on frmMain\r
1030         /// </summary>\r
1031         public void selectLongestTitle(frmMain mainWindow)\r
1032         {\r
1033             int current_largest = 0;\r
1034             Handbrake.Parsing.Title title2Select;\r
1035 \r
1036             // Check if there are titles in the DVD title dropdown menu and make sure, it's not just "Automatic"\r
1037             if (mainWindow.drp_dvdtitle.Items[0].ToString() != "Automatic")\r
1038                 title2Select = (Handbrake.Parsing.Title)mainWindow.drp_dvdtitle.Items[0];\r
1039             else\r
1040                 title2Select = null;\r
1041 \r
1042             // So, If there are titles in the DVD Title dropdown menu, lets select the longest.\r
1043             if (title2Select != null)\r
1044             {\r
1045                 foreach (Handbrake.Parsing.Title x in mainWindow.drp_dvdtitle.Items)\r
1046                 {\r
1047                     string title = x.ToString();\r
1048                     if (title != "Automatic")\r
1049                     {\r
1050                         string[] y = title.Split(' ');\r
1051                         string time = y[1].Replace("(", "").Replace(")", "");\r
1052                         string[] z = time.Split(':');\r
1053 \r
1054                         int hours = int.Parse(z[0]) * 60 * 60;\r
1055                         int minutes = int.Parse(z[1]) * 60;\r
1056                         int seconds = int.Parse(z[2]);\r
1057                         int total_sec = hours + minutes + seconds;\r
1058 \r
1059                         if (current_largest == 0)\r
1060                         {\r
1061                             current_largest = hours + minutes + seconds;\r
1062                             title2Select = x;\r
1063                         }\r
1064                         else\r
1065                         {\r
1066                             if (total_sec > current_largest)\r
1067                             {\r
1068                                 current_largest = total_sec;\r
1069                                 title2Select = x;\r
1070                             }\r
1071                         }\r
1072                     }\r
1073                 }\r
1074 \r
1075                 // Now set the longest title in the gui.\r
1076                 mainWindow.drp_dvdtitle.SelectedItem = title2Select;\r
1077             }\r
1078         }\r
1079 \r
1080         /// <summary>\r
1081         /// Set's up the DataGridView on the Chapters tab (frmMain)\r
1082         /// </summary>\r
1083         /// <param name="mainWindow"></param>\r
1084         public void chapterNaming(frmMain mainWindow)\r
1085         {\r
1086             try\r
1087             {\r
1088                 mainWindow.data_chpt.Rows.Clear();\r
1089                 int i = 0;\r
1090                 int rowCount = 0;\r
1091                 int start = 0;\r
1092                 int finish = 0;\r
1093                 if (mainWindow.drop_chapterFinish.Text != "Auto")\r
1094                     finish = int.Parse(mainWindow.drop_chapterFinish.Text);\r
1095 \r
1096                 if (mainWindow.drop_chapterStart.Text != "Auto")\r
1097                     start = int.Parse(mainWindow.drop_chapterStart.Text);\r
1098 \r
1099                 rowCount = finish - (start - 1);\r
1100 \r
1101                 while (i < rowCount)\r
1102                 {\r
1103                     DataGridViewRow row = new DataGridViewRow();\r
1104 \r
1105                     mainWindow.data_chpt.Rows.Insert(i, row);\r
1106                     mainWindow.data_chpt.Rows[i].Cells[0].Value = (i + 1);\r
1107                     mainWindow.data_chpt.Rows[i].Cells[1].Value = "Chapter " + (i + 1);\r
1108                     i++;\r
1109                 }\r
1110             }\r
1111             catch (Exception exc)\r
1112             {\r
1113                 MessageBox.Show("chapterNaming() Error has occured: \n" + exc.ToString());\r
1114             }\r
1115         }\r
1116 \r
1117         /// <summary>\r
1118         /// Function which generates the filename and path automatically based on \r
1119         /// the Source Name, DVD title and DVD Chapters\r
1120         /// </summary>\r
1121         /// <param name="mainWindow"></param>\r
1122         public void autoName(frmMain mainWindow)\r
1123         {\r
1124             if (Properties.Settings.Default.autoNaming == "Checked")\r
1125             {\r
1126                 if (mainWindow.drp_dvdtitle.Text != "Automatic")\r
1127                 {\r
1128                     // Todo: This code is a tad messy. Clean it up at some point.\r
1129                     // Get the Source Name\r
1130                     string source = mainWindow.text_source.Text;\r
1131                     string[] sourceName = source.Split('\\');\r
1132                     source = sourceName[sourceName.Length - 1].Replace(".iso", "").Replace(".mpg", "").Replace(".ts", "").Replace(".ps", "");\r
1133 \r
1134                     // Get the Selected Title Number\r
1135                     string title = mainWindow.drp_dvdtitle.Text;\r
1136                     string[] titlesplit = title.Split(' ');\r
1137                     title = titlesplit[0];\r
1138 \r
1139                     // Get the Chapter Start and Chapter End Numbers\r
1140                     string cs = mainWindow.drop_chapterStart.Text;\r
1141                     string cf = mainWindow.drop_chapterFinish.Text;\r
1142 \r
1143                     // Just incase the above are set to their default Automatic values, set the varible to ""\r
1144                     if (title == "Automatic")\r
1145                         title = "";\r
1146                     if (cs == "Auto")\r
1147                         cs = "";\r
1148                     if (cf == "Auto")\r
1149                         cf = "";\r
1150 \r
1151                     // If both CS and CF are populated, set the dash varible\r
1152                     string dash = "";\r
1153                     if (cf != "Auto")\r
1154                         dash = "-";\r
1155 \r
1156                     // Get the destination filename.\r
1157                     string destination_filename = "";\r
1158                     if (Properties.Settings.Default.autoNameFormat != "")\r
1159                     {\r
1160                         destination_filename = Properties.Settings.Default.autoNameFormat;\r
1161                         destination_filename = destination_filename.Replace("{source}", source).Replace("{title}", title).Replace("{chapters}", cs + dash + cf);\r
1162                     }\r
1163                     else\r
1164                         destination_filename = source + "_T" + title + "_C" + cs + dash + cf;\r
1165 \r
1166                     // If the text box is blank\r
1167                     if (!mainWindow.text_destination.Text.Contains("\\"))\r
1168                     {\r
1169                         string filePath = "";\r
1170                         if (Properties.Settings.Default.autoNamePath.Trim() != "")\r
1171                         {\r
1172                             if (Properties.Settings.Default.autoNamePath.Trim() != "Click 'Browse' to set the default location")\r
1173                                 filePath = Properties.Settings.Default.autoNamePath + "\\";\r
1174                         }\r
1175                         mainWindow.text_destination.Text = filePath + destination_filename + ".mp4";\r
1176                     }\r
1177                     else // If the text box already has a path and file\r
1178                     {\r
1179                         string dest = mainWindow.text_destination.Text;\r
1180                         string[] destName = dest.Split('\\');\r
1181                         string[] extension = dest.Split('.');\r
1182                         string ext = extension[extension.Length - 1];\r
1183 \r
1184                         destName[destName.Length - 1] = destination_filename + "." + ext;\r
1185 \r
1186                         string fullDest = "";\r
1187                         foreach (string part in destName)\r
1188                         {\r
1189                             if (fullDest != "")\r
1190                                 fullDest = fullDest + "\\" + part;\r
1191                             else\r
1192                                 fullDest = fullDest + part;\r
1193                         }\r
1194 \r
1195                         mainWindow.text_destination.Text = fullDest;\r
1196                     }\r
1197                 }\r
1198             }\r
1199         }\r
1200 \r
1201         /// <summary>\r
1202         /// Checks for updates and returns true if an update is available.\r
1203         /// </summary>\r
1204         /// <param name="debug">Turns on debug mode. Don't use on program startup</param>\r
1205         /// <returns>Boolean True = Update available</returns>\r
1206         public Boolean updateCheck(Boolean debug)\r
1207         {\r
1208             try\r
1209             {\r
1210                 Functions.AppcastReader rssRead = new Functions.AppcastReader();\r
1211                 string build = rssRead.build();\r
1212 \r
1213                 int latest = int.Parse(build);\r
1214                 int current = Properties.Settings.Default.hb_build;\r
1215                 int skip = Properties.Settings.Default.skipversion;\r
1216 \r
1217                 if (latest == skip)\r
1218                     return false;\r
1219                 else\r
1220                 {\r
1221                     Boolean update = (latest > current);\r
1222                     return update;\r
1223                 }\r
1224             }\r
1225             catch (Exception exc)\r
1226             {\r
1227                 if (debug == true)\r
1228                     MessageBox.Show("Unable to check for updates, Please try again later. \n" + exc.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
1229                 return false;\r
1230             }\r
1231         }\r
1232 \r
1233         /// <summary>\r
1234         /// Get's HandBrakes version data from the CLI.\r
1235         /// </summary>\r
1236         /// <returns>Arraylist of Version Data. 0 = hb_version 1 = hb_build</returns>\r
1237         public ArrayList getCliVersionData()\r
1238         {\r
1239             ArrayList cliVersionData = new ArrayList();\r
1240             // 0 = SVN Build / Version\r
1241             // 1 = Build Date\r
1242 \r
1243             Process cliProcess = new Process();\r
1244             ProcessStartInfo handBrakeCLI = new ProcessStartInfo("HandBrakeCLI.exe", " -u");\r
1245             handBrakeCLI.UseShellExecute = false;\r
1246             handBrakeCLI.RedirectStandardError = true;\r
1247             handBrakeCLI.RedirectStandardOutput = true;\r
1248             handBrakeCLI.CreateNoWindow = true;\r
1249             cliProcess.StartInfo = handBrakeCLI;\r
1250             cliProcess.Start();\r
1251 \r
1252             // Retrieve standard output and report back to parent thread until the process is complete\r
1253             String line;\r
1254             TextReader stdOutput = cliProcess.StandardError;\r
1255 \r
1256             while (!cliProcess.HasExited)\r
1257             {\r
1258                 line = stdOutput.ReadLine();\r
1259                 Match m = Regex.Match(line, @"HandBrake [0-9\.]*svn[0-9]*[M]* \([0-9]*\)");\r
1260                 if (m.Success != false)\r
1261                 {\r
1262                     string data = line.Replace("(", "").Replace(")", "").Replace("HandBrake ", "");\r
1263                     string[] arr = data.Split(' ');\r
1264                     cliVersionData.Add(arr[0]);\r
1265                     cliVersionData.Add(arr[1]);\r
1266                     return cliVersionData;\r
1267                 }\r
1268             }\r
1269             return null;\r
1270         }\r
1271 \r
1272         /// <summary>\r
1273         /// Check if the queue recovery file contains records.\r
1274         /// If it does, it means the last queue did not complete before HandBrake closed.\r
1275         /// So, return a boolean if true. \r
1276         /// </summary>\r
1277         public Boolean check_queue_recovery()\r
1278         {\r
1279             try\r
1280             {\r
1281                 string tempPath = Path.Combine(Path.GetTempPath(), "hb_queue_recovery.dat");\r
1282                 using (StreamReader reader = new StreamReader(tempPath))\r
1283                 {\r
1284                     string queue_item = reader.ReadLine();\r
1285                     if (queue_item == null)\r
1286                     {\r
1287                         reader.Close();\r
1288                         reader.Dispose();\r
1289                         return false;\r
1290                     }\r
1291                     else // There exists an item in the recovery queue file, so try and recovr it.\r
1292                     {\r
1293                         reader.Close();\r
1294                         reader.Dispose();\r
1295                         return true;\r
1296                     }\r
1297                 }\r
1298             }\r
1299             catch (Exception)\r
1300             {\r
1301                 // Keep quiet about the error.\r
1302                 return false;\r
1303             }\r
1304         }\r
1305 \r
1306         #endregion\r
1307 \r
1308     }\r
1309 }