OSDN Git Service

27484000bf2057e393a9b6fea3aa241cb9c1ea20
[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 \r
22         /// <summary>\r
23         /// This function takes in a Query which has been parsed by QueryParser and\r
24         /// set's all the GUI widgets correctly.\r
25         /// </summary>\r
26         /// <param name="mainWindow"></param>\r
27         /// <param name="presetQuery">The Parsed CLI Query</param>\r
28         /// <param name="name">Name of the preset</param>\r
29         public void presetLoader(frmMain mainWindow, Functions.QueryParser presetQuery, string name)\r
30         {\r
31             // ---------------------------\r
32             // Setup the GUI\r
33             // ---------------------------\r
34 \r
35             // Source tab\r
36             #region source\r
37             // Reset some vaules to stock first to prevent errors.\r
38             mainWindow.check_iPodAtom.CheckState = CheckState.Unchecked;\r
39 \r
40             // Now load all the new settings onto the main window\r
41             mainWindow.drp_dvdtitle.Text = "Automatic";\r
42             mainWindow.drop_chapterStart.Text = "Auto";\r
43             mainWindow.drop_chapterFinish.Text = "Auto";\r
44 \r
45             if (presetQuery.Format != null)\r
46             {\r
47                 string destination = mainWindow.text_destination.Text;\r
48                 destination = destination.Replace(".mp4", "." + presetQuery.Format);\r
49                 destination = destination.Replace(".m4v", "." + presetQuery.Format);\r
50                 destination = destination.Replace(".avi", "." + presetQuery.Format);\r
51                 destination = destination.Replace(".mkv", "." + presetQuery.Format);\r
52                 destination = destination.Replace(".ogm", "." + presetQuery.Format);\r
53                 mainWindow.text_destination.Text = destination;\r
54             }\r
55 \r
56             #endregion\r
57 \r
58             // Destination tab\r
59             #region destination\r
60 \r
61             mainWindow.drp_videoEncoder.Text = presetQuery.VideoEncoder;\r
62 \r
63             if (presetQuery.Format != null)\r
64             {\r
65                 if (presetQuery.Format == "mp4")\r
66                     mainWindow.drop_format.SelectedIndex = 0;\r
67                 else if (presetQuery.Format == "m4v")\r
68                     mainWindow.drop_format.SelectedIndex = 1;\r
69                 else if (presetQuery.Format == "mkv")\r
70                     mainWindow.drop_format.SelectedIndex = 2;\r
71                 else if (presetQuery.Format == "avi")\r
72                     mainWindow.drop_format.SelectedIndex = 3;\r
73                 else if (presetQuery.Format == "ogm")\r
74                     mainWindow.drop_format.SelectedIndex = 4;\r
75             }\r
76 \r
77             if (presetQuery.IpodAtom == true)\r
78                 mainWindow.check_iPodAtom.CheckState = CheckState.Checked;\r
79             else\r
80                 mainWindow.check_iPodAtom.CheckState = CheckState.Unchecked;\r
81 \r
82             if (presetQuery.OptimizeMP4 == true)\r
83                 mainWindow.check_optimiseMP4.CheckState = CheckState.Checked;\r
84             else\r
85                 mainWindow.check_optimiseMP4.CheckState = CheckState.Unchecked;\r
86 \r
87             #endregion\r
88 \r
89             // Picture Settings Tab\r
90             #region Picture\r
91             mainWindow.check_autoCrop.Checked = true;\r
92             mainWindow.drp_deInterlace_option.Text = presetQuery.DeInterlace;\r
93             mainWindow.drp_deNoise.Text = presetQuery.DeNoise;\r
94 \r
95             if (presetQuery.Decomb == true)\r
96                 mainWindow.check_decomb.CheckState = CheckState.Checked;\r
97             else\r
98                 mainWindow.check_decomb.CheckState = CheckState.Unchecked;\r
99 \r
100             if (presetQuery.DeTelecine == true)\r
101                 mainWindow.check_detelecine.CheckState = CheckState.Checked;\r
102             else\r
103                 mainWindow.check_detelecine.CheckState = CheckState.Unchecked;\r
104 \r
105             if (presetQuery.DeBlock != 0)\r
106             {\r
107                 mainWindow.slider_deblock.Value = presetQuery.DeBlock;\r
108                 mainWindow.lbl_deblockVal.Text = presetQuery.DeBlock.ToString();\r
109             }\r
110             else\r
111             {\r
112                 mainWindow.slider_deblock.Value = 4;\r
113                 mainWindow.lbl_deblockVal.Text = "Off";\r
114             }\r
115 \r
116             if (presetQuery.Anamorphic == true)\r
117                 mainWindow.drp_anamorphic.SelectedIndex = 1;\r
118             else\r
119                 mainWindow.drp_anamorphic.SelectedIndex = 0;\r
120 \r
121             if (presetQuery.LooseAnamorphic == true)\r
122                 mainWindow.drp_anamorphic.SelectedIndex = 2;\r
123             else\r
124             {\r
125                 if (presetQuery.Anamorphic != true)\r
126                     mainWindow.drp_anamorphic.SelectedIndex = 0;\r
127             }\r
128 \r
129             if (presetQuery.Width != 0)\r
130                 mainWindow.text_width.Text = presetQuery.Width.ToString();\r
131             else\r
132             {\r
133                 mainWindow.text_width.Text = "";\r
134             }\r
135 \r
136             if (presetQuery.Height != 0)\r
137                 mainWindow.text_height.Text = presetQuery.Height.ToString();\r
138             else\r
139             {\r
140                 mainWindow.text_height.Text = "";\r
141             }\r
142 \r
143             #endregion\r
144 \r
145             // Video Settings Tab\r
146             #region video\r
147             mainWindow.text_bitrate.Text = presetQuery.AverageVideoBitrate;\r
148             mainWindow.text_filesize.Text = presetQuery.VideoTargetSize;\r
149             mainWindow.slider_videoQuality.Value = presetQuery.VideoQuality;\r
150             if (mainWindow.slider_videoQuality.Value != 0)\r
151             {\r
152                 int ql = presetQuery.VideoQuality;\r
153                 mainWindow.SliderValue.Text = ql.ToString() + "%";\r
154             }\r
155 \r
156             if (presetQuery.TwoPass == true)\r
157                 mainWindow.check_2PassEncode.CheckState = CheckState.Checked;\r
158             else\r
159                 mainWindow.check_2PassEncode.CheckState = CheckState.Unchecked;\r
160 \r
161             if (presetQuery.Grayscale == true)\r
162                 mainWindow.check_grayscale.CheckState = CheckState.Checked;\r
163             else\r
164                 mainWindow.check_grayscale.CheckState = CheckState.Unchecked;\r
165 \r
166             mainWindow.drp_videoFramerate.Text = presetQuery.VideoFramerate;\r
167 \r
168             if (presetQuery.TurboFirstPass == true)\r
169                 mainWindow.check_turbo.CheckState = CheckState.Checked;\r
170             else\r
171                 mainWindow.check_turbo.CheckState = CheckState.Unchecked;\r
172 \r
173             if (presetQuery.LargeMP4 == true)\r
174                 mainWindow.check_largeFile.CheckState = CheckState.Checked;\r
175             else\r
176             {\r
177                 mainWindow.check_largeFile.CheckState = CheckState.Unchecked;\r
178                 mainWindow.check_largeFile.BackColor = Color.Transparent;\r
179             }\r
180             #endregion\r
181 \r
182             // Chapter Markers Tab\r
183             #region Chapter Markers\r
184 \r
185             if (presetQuery.ChapterMarkers == true)\r
186                 mainWindow.Check_ChapterMarkers.CheckState = CheckState.Checked;\r
187             else\r
188                 mainWindow.Check_ChapterMarkers.CheckState = CheckState.Unchecked;\r
189 \r
190             #endregion\r
191 \r
192             // Audio Settings Tab\r
193             #region Audio\r
194 \r
195             // Handle Track 1\r
196             mainWindow.drp_track1Audio.Text = "Automatic";\r
197 \r
198             // Handle Track 2\r
199             if (presetQuery.AudioEncoder2 != null)  // Fix for loading in built in presets. Where 2 encoders but no tracks in the preset.\r
200             {\r
201                 mainWindow.drp_track2Audio.Enabled = true;\r
202                 mainWindow.drp_audsr_2.Enabled = true;\r
203                 mainWindow.drp_audmix_2.Enabled = true;\r
204                 mainWindow.drp_audenc_2.Enabled = true;\r
205                 mainWindow.drp_audbit_2.Enabled = true;\r
206                 mainWindow.drp_audsr_2.Text = "48";\r
207                 mainWindow.drp_track2Audio.Text = "Automatic";\r
208             }\r
209             else if (presetQuery.AudioTrack2 == "None")\r
210             {\r
211                 mainWindow.drp_track2Audio.Text = "None";\r
212                 mainWindow.drp_track2Audio.SelectedIndex = 0;\r
213                 mainWindow.drp_audsr_2.Enabled = false;\r
214                 mainWindow.drp_audmix_2.Enabled = false;\r
215                 mainWindow.drp_audenc_2.Enabled = false;\r
216                 mainWindow.drp_audbit_2.Enabled = false;\r
217             }\r
218             else\r
219             {\r
220                 mainWindow.drp_track2Audio.Text = presetQuery.AudioTrack2;\r
221                 mainWindow.drp_audsr_2.Enabled = true;\r
222                 mainWindow.drp_audmix_2.Enabled = true;\r
223                 mainWindow.drp_audenc_2.Enabled = true;\r
224                 mainWindow.drp_audbit_2.Enabled = true;\r
225             }\r
226 \r
227             // Handle Track 3\r
228             if (presetQuery.AudioTrack3 == "None")\r
229             {\r
230                 mainWindow.drp_track3Audio.SelectedIndex = 0;\r
231                 mainWindow.drp_audsr_3.Enabled = false;\r
232                 mainWindow.drp_audmix_3.Enabled = false;\r
233                 mainWindow.drp_audenc_3.Enabled = false;\r
234                 mainWindow.drp_audbit_3.Enabled = false;\r
235                 mainWindow.trackBar3.Enabled = false;\r
236 \r
237                 mainWindow.drp_track3Audio.Text = "None";\r
238                 mainWindow.drp_audsr_3.Text = "";\r
239                 mainWindow.drp_audmix_3.Text = "Automatic";\r
240                 mainWindow.drp_audenc_3.Text = "";\r
241                 mainWindow.drp_audbit_3.Text = "";\r
242                 mainWindow.trackBar3.Value = 0;\r
243 \r
244             }\r
245             else\r
246             {\r
247                 mainWindow.drp_track3Audio.Text = presetQuery.AudioTrack3;\r
248                 mainWindow.drp_audsr_3.Enabled = true;\r
249                 mainWindow.drp_audmix_3.Enabled = true;\r
250                 mainWindow.drp_audenc_3.Enabled = true;\r
251                 mainWindow.drp_audbit_3.Enabled = true;\r
252                 mainWindow.trackBar3.Enabled = true;\r
253             }\r
254 \r
255             // Handle Track 4\r
256             if (presetQuery.AudioTrack4 == "None")\r
257             {\r
258                 mainWindow.drp_track4Audio.SelectedIndex = 0;\r
259                 mainWindow.drp_audsr_4.Enabled = false;\r
260                 mainWindow.drp_audmix_4.Enabled = false;\r
261                 mainWindow.drp_audenc_4.Enabled = false;\r
262                 mainWindow.drp_audbit_4.Enabled = false;\r
263                 mainWindow.trackBar4.Enabled = false;\r
264 \r
265                 mainWindow.drp_track4Audio.Text = "None";\r
266                 mainWindow.drp_audsr_4.Text = "";\r
267                 mainWindow.drp_audmix_4.Text = "Automatic";\r
268                 mainWindow.drp_audenc_4.Text = "";\r
269                 mainWindow.drp_audbit_4.Text = "";\r
270                 mainWindow.trackBar4.Value = 0;\r
271             }\r
272             else\r
273             {\r
274                 mainWindow.drp_track4Audio.Text = presetQuery.AudioTrack4;\r
275                 mainWindow.drp_audsr_4.Enabled = true;\r
276                 mainWindow.drp_audmix_4.Enabled = true;\r
277                 mainWindow.drp_audenc_4.Enabled = true;\r
278                 mainWindow.drp_audbit_4.Enabled = true;\r
279                 mainWindow.trackBar4.Enabled = true;\r
280             }\r
281 \r
282             // Now lets start setting stuff\r
283             if (presetQuery.AudioEncoder1 != null)\r
284                 mainWindow.drp_audenc_1.Text = presetQuery.AudioEncoder1;\r
285             mainWindow.drp_audenc_2.Text = presetQuery.AudioEncoder2;\r
286             mainWindow.drp_audenc_3.Text = presetQuery.AudioEncoder3;\r
287             mainWindow.drp_audenc_4.Text = presetQuery.AudioEncoder4;\r
288 \r
289             mainWindow.drp_audmix_1.Text = presetQuery.AudioTrackMix1;\r
290             mainWindow.drp_audmix_2.Text = presetQuery.AudioTrackMix2;\r
291             mainWindow.drp_audmix_3.Text = presetQuery.AudioTrackMix3;\r
292             mainWindow.drp_audmix_4.Text = presetQuery.AudioTrackMix4;\r
293 \r
294             if (presetQuery.AudioBitrate1 != null)\r
295                 mainWindow.drp_audbit_1.Text = presetQuery.AudioBitrate1;\r
296             mainWindow.drp_audbit_2.Text = presetQuery.AudioBitrate2;\r
297             mainWindow.drp_audbit_3.Text = presetQuery.AudioBitrate4;\r
298             mainWindow.drp_audbit_3.Text = presetQuery.AudioBitrate4;\r
299 \r
300             if (presetQuery.AudioSamplerate1 != null)\r
301                 mainWindow.drp_audsr_1.Text = presetQuery.AudioSamplerate1;\r
302             mainWindow.drp_audsr_2.Text = presetQuery.AudioSamplerate2;\r
303             mainWindow.drp_audsr_3.Text = presetQuery.AudioSamplerate3;\r
304             mainWindow.drp_audsr_4.Text = presetQuery.AudioSamplerate4;\r
305 \r
306             // Dynamic Range Compression (Should be a float but we use double for ease)\r
307             double value = 0;\r
308             double actualValue = 0;\r
309 \r
310             value = presetQuery.DRC1;\r
311             if (value > 0)\r
312                 value = value - 10;\r
313             mainWindow.trackBar1.Value = int.Parse(value.ToString());\r
314             actualValue = presetQuery.DRC1 / 10;\r
315             mainWindow.lbl_drc1.Text = actualValue.ToString();\r
316 \r
317             value = presetQuery.DRC2;\r
318             if (value > 0)\r
319                 value = value - 10;\r
320             mainWindow.trackBar2.Value = int.Parse(value.ToString());\r
321             actualValue = presetQuery.DRC2 / 10;\r
322             mainWindow.lbl_drc2.Text = actualValue.ToString();\r
323 \r
324             value = presetQuery.DRC3;\r
325             if (value > 0)\r
326                 value = value - 10;\r
327             mainWindow.trackBar3.Value = int.Parse(value.ToString());\r
328             actualValue = presetQuery.DRC3 / 10;\r
329             mainWindow.lbl_drc3.Text = actualValue.ToString();\r
330 \r
331             value = presetQuery.DRC4;\r
332             if (value > 0)\r
333                 value = value - 10;\r
334             mainWindow.trackBar4.Value = int.Parse(value.ToString());\r
335             actualValue = presetQuery.DRC4 / 10;\r
336             mainWindow.lbl_drc4.Text = actualValue.ToString();\r
337 \r
338 \r
339             // Subtitle Stuff\r
340             mainWindow.drp_subtitle.Text = presetQuery.Subtitles;\r
341 \r
342             if (presetQuery.ForcedSubtitles == true)\r
343             {\r
344                 mainWindow.check_forced.CheckState = CheckState.Checked;\r
345                 mainWindow.check_forced.Enabled = true;\r
346             }\r
347             else\r
348                 mainWindow.check_forced.CheckState = CheckState.Unchecked;\r
349 \r
350 \r
351             #endregion\r
352 \r
353             // H264 Tab & Preset Name\r
354             #region other\r
355             mainWindow.rtf_x264Query.Text = presetQuery.H264Query;\r
356 \r
357             // Set the preset name\r
358             mainWindow.groupBox_output.Text = "Output Settings (Preset: " + name + ")";\r
359             #endregion\r
360         }\r
361 \r
362         /// <summary>\r
363         /// Select the longest title in the DVD title dropdown menu on frmMain\r
364         /// </summary>\r
365         public void selectLongestTitle(frmMain mainWindow)\r
366         {\r
367             int current_largest = 0;\r
368             Handbrake.Parsing.Title title2Select;\r
369 \r
370             // Check if there are titles in the DVD title dropdown menu and make sure, it's not just "Automatic"\r
371             if (mainWindow.drp_dvdtitle.Items[0].ToString() != "Automatic")\r
372                 title2Select = (Handbrake.Parsing.Title)mainWindow.drp_dvdtitle.Items[0];\r
373             else\r
374                 title2Select = null;\r
375 \r
376             // So, If there are titles in the DVD Title dropdown menu, lets select the longest.\r
377             if (title2Select != null)\r
378             {\r
379                 foreach (Handbrake.Parsing.Title x in mainWindow.drp_dvdtitle.Items)\r
380                 {\r
381                     string title = x.ToString();\r
382                     if (title != "Automatic")\r
383                     {\r
384                         string[] y = title.Split(' ');\r
385                         string time = y[1].Replace("(", "").Replace(")", "");\r
386                         string[] z = time.Split(':');\r
387 \r
388                         int hours = int.Parse(z[0]) * 60 * 60;\r
389                         int minutes = int.Parse(z[1]) * 60;\r
390                         int seconds = int.Parse(z[2]);\r
391                         int total_sec = hours + minutes + seconds;\r
392 \r
393                         if (current_largest == 0)\r
394                         {\r
395                             current_largest = hours + minutes + seconds;\r
396                             title2Select = x;\r
397                         }\r
398                         else\r
399                         {\r
400                             if (total_sec > current_largest)\r
401                             {\r
402                                 current_largest = total_sec;\r
403                                 title2Select = x;\r
404                             }\r
405                         }\r
406                     }\r
407                 }\r
408 \r
409                 // Now set the longest title in the gui.\r
410                 mainWindow.drp_dvdtitle.SelectedItem = title2Select;\r
411             }\r
412         }\r
413 \r
414         /// <summary>\r
415         /// Set's up the DataGridView on the Chapters tab (frmMain)\r
416         /// </summary>\r
417         /// <param name="mainWindow"></param>\r
418         public void chapterNaming(frmMain mainWindow)\r
419         {\r
420             try\r
421             {\r
422                 mainWindow.data_chpt.Rows.Clear();\r
423                 int i = 0;\r
424                 int rowCount = 0;\r
425                 int start = 0;\r
426                 int finish = 0;\r
427                 if (mainWindow.drop_chapterFinish.Text != "Auto")\r
428                     finish = int.Parse(mainWindow.drop_chapterFinish.Text);\r
429 \r
430                 if (mainWindow.drop_chapterStart.Text != "Auto")\r
431                     start = int.Parse(mainWindow.drop_chapterStart.Text);\r
432 \r
433                 rowCount = finish - (start - 1);\r
434 \r
435                 while (i < rowCount)\r
436                 {\r
437                     DataGridViewRow row = new DataGridViewRow();\r
438 \r
439                     mainWindow.data_chpt.Rows.Insert(i, row);\r
440                     mainWindow.data_chpt.Rows[i].Cells[0].Value = (i + 1);\r
441                     mainWindow.data_chpt.Rows[i].Cells[1].Value = "Chapter " + (i + 1);\r
442                     i++;\r
443                 }\r
444             }\r
445             catch (Exception exc)\r
446             {\r
447                 MessageBox.Show("chapterNaming() Error has occured: \n" + exc.ToString());\r
448             }\r
449         }\r
450 \r
451         /// <summary>\r
452         /// Function which generates the filename and path automatically based on \r
453         /// the Source Name, DVD title and DVD Chapters\r
454         /// </summary>\r
455         /// <param name="mainWindow"></param>\r
456         public void autoName(frmMain mainWindow)\r
457         {\r
458             if (Properties.Settings.Default.autoNaming == "Checked")\r
459             {\r
460                 if (mainWindow.drp_dvdtitle.Text != "Automatic")\r
461                 {\r
462                     // Todo: This code is a tad messy. Clean it up at some point.\r
463                     // Get the Source Name\r
464                     string source = mainWindow.text_source.Text;\r
465                     string[] sourceName = source.Split('\\');\r
466                     source = sourceName[sourceName.Length - 1].Replace(".iso", "").Replace(".mpg", "").Replace(".ts", "").Replace(".ps", "");\r
467 \r
468                     // Get the Selected Title Number\r
469                     string title = mainWindow.drp_dvdtitle.Text;\r
470                     string[] titlesplit = title.Split(' ');\r
471                     title = titlesplit[0];\r
472 \r
473                     // Get the Chapter Start and Chapter End Numbers\r
474                     string cs = mainWindow.drop_chapterStart.Text;\r
475                     string cf = mainWindow.drop_chapterFinish.Text;\r
476 \r
477                     // Just incase the above are set to their default Automatic values, set the varible to ""\r
478                     if (title == "Automatic")\r
479                         title = "";\r
480                     if (cs == "Auto")\r
481                         cs = "";\r
482                     if (cf == "Auto")\r
483                         cf = "";\r
484 \r
485                     // If both CS and CF are populated, set the dash varible\r
486                     string dash = "";\r
487                     if (cf != "Auto")\r
488                         dash = "-";\r
489 \r
490                     // Get the destination filename.\r
491                     string destination_filename = "";\r
492                     if (Properties.Settings.Default.autoNameFormat != "")\r
493                     {\r
494                         destination_filename = Properties.Settings.Default.autoNameFormat;\r
495                         destination_filename = destination_filename.Replace("{source}", source).Replace("{title}", title).Replace("{chapters}", cs + dash + cf);\r
496                     }\r
497                     else\r
498                         destination_filename = source + "_T" + title + "_C" + cs + dash + cf;\r
499 \r
500                     // If the text box is blank\r
501                     if (!mainWindow.text_destination.Text.Contains("\\"))\r
502                     {\r
503                         string filePath = "";\r
504                         if (Properties.Settings.Default.autoNamePath.Trim() != "")\r
505                         {\r
506                             if (Properties.Settings.Default.autoNamePath.Trim() != "Click 'Browse' to set the default location")\r
507                                 filePath = Properties.Settings.Default.autoNamePath + "\\";\r
508                         }\r
509 \r
510                         if (mainWindow.drop_format.SelectedIndex == 0)\r
511                             mainWindow.text_destination.Text = filePath + destination_filename + ".mp4";\r
512                         else if (mainWindow.drop_format.SelectedIndex == 1)\r
513                             mainWindow.text_destination.Text = filePath + destination_filename + ".m4v";\r
514                         else if (mainWindow.drop_format.SelectedIndex == 2)\r
515                             mainWindow.text_destination.Text = filePath + destination_filename + ".mkv";\r
516                         else if (mainWindow.drop_format.SelectedIndex == 3)\r
517                             mainWindow.text_destination.Text = filePath + destination_filename + ".avi";\r
518                         else if (mainWindow.drop_format.SelectedIndex == 4)\r
519                             mainWindow.text_destination.Text = filePath + destination_filename + ".ogm";\r
520                     }\r
521                     else // If the text box already has a path and file\r
522                     {\r
523                         string dest = mainWindow.text_destination.Text;\r
524                         string[] destName = dest.Split('\\');\r
525                         string[] extension = dest.Split('.');\r
526                         string ext = extension[extension.Length - 1];\r
527 \r
528                         destName[destName.Length - 1] = destination_filename + "." + ext;\r
529 \r
530                         string fullDest = "";\r
531                         foreach (string part in destName)\r
532                         {\r
533                             if (fullDest != "")\r
534                                 fullDest = fullDest + "\\" + part;\r
535                             else\r
536                                 fullDest = fullDest + part;\r
537                         }\r
538 \r
539                         mainWindow.text_destination.Text = fullDest;\r
540                     }\r
541                 }\r
542             }\r
543         }\r
544 \r
545         /// <summary>\r
546         /// Checks for updates and returns true if an update is available.\r
547         /// </summary>\r
548         /// <param name="debug">Turns on debug mode. Don't use on program startup</param>\r
549         /// <returns>Boolean True = Update available</returns>\r
550         public Boolean updateCheck(Boolean debug)\r
551         {\r
552             try\r
553             {\r
554                 Functions.AppcastReader rssRead = new Functions.AppcastReader();\r
555                 rssRead.getInfo(); // Initializes the class.\r
556                 string build = rssRead.build();\r
557 \r
558                 int latest = int.Parse(build);\r
559                 int current = Properties.Settings.Default.hb_build;\r
560                 int skip = Properties.Settings.Default.skipversion;\r
561 \r
562                 if (latest == skip)\r
563                     return false;\r
564                 else\r
565                 {\r
566                     Boolean update = (latest > current);\r
567                     return update;\r
568                 }\r
569             }\r
570             catch (Exception exc)\r
571             {\r
572                 if (debug == true)\r
573                     MessageBox.Show("Unable to check for updates, Please try again later. \n" + exc.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
574                 return false;\r
575             }\r
576         }\r
577 \r
578         /// <summary>\r
579         /// Get's HandBrakes version data from the CLI.\r
580         /// </summary>\r
581         /// <returns>Arraylist of Version Data. 0 = hb_version 1 = hb_build</returns>\r
582         public ArrayList getCliVersionData()\r
583         {\r
584             ArrayList cliVersionData = new ArrayList();\r
585             // 0 = SVN Build / Version\r
586             // 1 = Build Date\r
587 \r
588             Process cliProcess = new Process();\r
589             ProcessStartInfo handBrakeCLI = new ProcessStartInfo("HandBrakeCLI.exe", " -u");\r
590             handBrakeCLI.UseShellExecute = false;\r
591             handBrakeCLI.RedirectStandardError = true;\r
592             handBrakeCLI.RedirectStandardOutput = true;\r
593             handBrakeCLI.CreateNoWindow = true;\r
594             cliProcess.StartInfo = handBrakeCLI;\r
595             cliProcess.Start();\r
596 \r
597             // Retrieve standard output and report back to parent thread until the process is complete\r
598             String line;\r
599             TextReader stdOutput = cliProcess.StandardError;\r
600 \r
601             while (!cliProcess.HasExited)\r
602             {\r
603                 line = stdOutput.ReadLine();\r
604                 Match m = Regex.Match(line, @"HandBrake [0-9\.]*svn[0-9]*[M]* \([0-9]*\)");\r
605                 if (m.Success != false)\r
606                 {\r
607                     string data = line.Replace("(", "").Replace(")", "").Replace("HandBrake ", "");\r
608                     string[] arr = data.Split(' ');\r
609                     cliVersionData.Add(arr[0]);\r
610                     cliVersionData.Add(arr[1]);\r
611                     return cliVersionData;\r
612                 }\r
613             }\r
614             return null;\r
615         }\r
616 \r
617         /// <summary>\r
618         /// Check if the queue recovery file contains records.\r
619         /// If it does, it means the last queue did not complete before HandBrake closed.\r
620         /// So, return a boolean if true. \r
621         /// </summary>\r
622         public Boolean check_queue_recovery()\r
623         {\r
624             try\r
625             {\r
626                 string tempPath = Path.Combine(Path.GetTempPath(), "hb_queue_recovery.dat");\r
627                 using (StreamReader reader = new StreamReader(tempPath))\r
628                 {\r
629                     string queue_item = reader.ReadLine();\r
630                     if (queue_item == null)\r
631                     {\r
632                         reader.Close();\r
633                         reader.Dispose();\r
634                         return false;\r
635                     }\r
636                     else // There exists an item in the recovery queue file, so try and recovr it.\r
637                     {\r
638                         reader.Close();\r
639                         reader.Dispose();\r
640                         return true;\r
641                     }\r
642                 }\r
643             }\r
644             catch (Exception)\r
645             {\r
646                 // Keep quiet about the error.\r
647                 return false;\r
648             }\r
649         }\r
650 \r
651     }\r
652 }