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
8 using System.Collections;
\r
10 using System.Windows.Forms;
\r
11 using System.Globalization;
\r
13 using System.Drawing;
\r
14 using System.Diagnostics;
\r
15 using System.Text.RegularExpressions;
\r
16 using System.Collections.Generic;
\r
17 using System.Xml.Serialization;
\r
19 namespace Handbrake.Functions
\r
23 // Private Variables
\r
24 private static XmlSerializer ser = new XmlSerializer(typeof(List<Queue.QueueItem>));
\r
27 /// Calculate the duration of the selected title and chapters
\r
29 public TimeSpan calculateDuration(string chapter_start, string chapter_end, Parsing.Title selectedTitle)
\r
31 TimeSpan Duration = TimeSpan.FromSeconds(0.0);
\r
33 // Get the durations between the 2 chapter points and add them together.
\r
34 if (chapter_start != "Auto" && chapter_end != "Auto")
\r
36 int start_chapter, end_chapter = 0;
\r
37 int.TryParse(chapter_start, out start_chapter);
\r
38 int.TryParse(chapter_end, out end_chapter);
\r
40 int position = start_chapter - 1;
\r
42 if (start_chapter <= end_chapter)
\r
44 if (end_chapter > selectedTitle.Chapters.Count)
\r
45 end_chapter = selectedTitle.Chapters.Count;
\r
47 while (position != end_chapter)
\r
49 TimeSpan dur = selectedTitle.Chapters[position].Duration;
\r
50 Duration = Duration + dur;
\r
59 /// Calculate the non-anamorphic resoltuion of the source
\r
61 /// <param name="width"></param>
\r
62 /// <returns></returns>
\r
63 public int cacluateNonAnamorphicHeight(int width, decimal top, decimal bottom, decimal left, decimal right, Parsing.Title selectedTitle)
\r
65 float aspect = selectedTitle.AspectRatio;
\r
68 if (aspect.ToString() == "1.78")
\r
79 double a = width * selectedTitle.Resolution.Width * ah * (selectedTitle.Resolution.Height - (double)top - (double)bottom);
\r
80 double b = selectedTitle.Resolution.Height * aw * (selectedTitle.Resolution.Width - (double)left - (double)right);
\r
84 // If it's not Mod 16, make it mod 16
\r
87 double mod16 = y % 16;
\r
100 //double z = ( 16 * (( y + 8 ) / 16 ) );
\r
101 int x = int.Parse(y.ToString());
\r
106 /// Select the longest title in the DVD title dropdown menu on frmMain
\r
108 public Handbrake.Parsing.Title selectLongestTitle(ComboBox drp_dvdtitle)
\r
110 int current_largest = 0;
\r
111 Handbrake.Parsing.Title title2Select;
\r
113 // Check if there are titles in the DVD title dropdown menu and make sure, it's not just "Automatic"
\r
114 if (drp_dvdtitle.Items[0].ToString() != "Automatic")
\r
115 title2Select = (Handbrake.Parsing.Title)drp_dvdtitle.Items[0];
\r
117 title2Select = null;
\r
119 // So, If there are titles in the DVD Title dropdown menu, lets select the longest.
\r
120 if (title2Select != null)
\r
122 foreach (Handbrake.Parsing.Title x in drp_dvdtitle.Items)
\r
124 string title = x.ToString();
\r
125 if (title != "Automatic")
\r
127 string[] y = title.Split(' ');
\r
128 string time = y[1].Replace("(", "").Replace(")", "");
\r
129 string[] z = time.Split(':');
\r
131 int hours = int.Parse(z[0]) * 60 * 60;
\r
132 int minutes = int.Parse(z[1]) * 60;
\r
133 int seconds = int.Parse(z[2]);
\r
134 int total_sec = hours + minutes + seconds;
\r
136 if (current_largest == 0)
\r
138 current_largest = hours + minutes + seconds;
\r
143 if (total_sec > current_largest)
\r
145 current_largest = total_sec;
\r
152 return title2Select;
\r
156 /// Set's up the DataGridView on the Chapters tab (frmMain)
\r
158 /// <param name="mainWindow"></param>
\r
159 public DataGridView chapterNaming(DataGridView data_chpt, string chapter_start, string chapter_end)
\r
167 if (chapter_end != "Auto")
\r
168 finish = int.Parse(chapter_end);
\r
170 if (chapter_start != "Auto")
\r
171 start = int.Parse(chapter_start);
\r
173 rowCount = finish - (start - 1);
\r
175 while (i < rowCount)
\r
177 DataGridViewRow row = new DataGridViewRow();
\r
179 data_chpt.Rows.Insert(i, row);
\r
180 data_chpt.Rows[i].Cells[0].Value = (i + 1);
\r
181 data_chpt.Rows[i].Cells[1].Value = "Chapter " + (i + 1);
\r
186 catch (Exception exc)
\r
188 MessageBox.Show("chapterNaming() Error has occured: \n" + exc.ToString());
\r
194 /// Function which generates the filename and path automatically based on
\r
195 /// the Source Name, DVD title and DVD Chapters
\r
197 /// <param name="mainWindow"></param>
\r
198 public string autoName(ComboBox drp_dvdtitle, string chapter_start, string chatper_end, string source, string dest, int format)
\r
201 string AutoNamePath = string.Empty;
\r
203 if (drp_dvdtitle.Text != "Automatic")
\r
205 // Todo: This code is a tad messy. Clean it up at some point.
\r
206 // Get the Source Name
\r
207 string[] sourceName = source.Split('\\');
\r
208 source = sourceName[sourceName.Length - 1].Replace(".iso", "").Replace(".mpg", "").Replace(".ts", "").Replace(".ps", "");
\r
210 // Get the Selected Title Number
\r
211 string title = drp_dvdtitle.Text;
\r
212 string[] titlesplit = title.Split(' ');
\r
213 title = titlesplit[0];
\r
215 // Get the Chapter Start and Chapter End Numbers
\r
216 string cs = chapter_start;
\r
217 string cf = chatper_end;
\r
219 // Just incase the above are set to their default Automatic values, set the varible to ""
\r
220 if (title == "Automatic")
\r
227 // If both CS and CF are populated, set the dash varible
\r
232 // Get the destination filename.
\r
233 string destination_filename = "";
\r
234 if (Properties.Settings.Default.autoNameFormat != "")
\r
236 destination_filename = Properties.Settings.Default.autoNameFormat;
\r
237 destination_filename = destination_filename.Replace("{source}", source).Replace("{title}", title).Replace("{chapters}", cs + dash + cf);
\r
240 destination_filename = source + "_T" + title + "_C" + cs + dash + cf;
\r
242 // If the text box is blank
\r
243 if (!dest.Contains("\\"))
\r
245 string filePath = "";
\r
246 if (Properties.Settings.Default.autoNamePath.Trim() != "")
\r
248 if (Properties.Settings.Default.autoNamePath.Trim() != "Click 'Browse' to set the default location")
\r
249 filePath = Properties.Settings.Default.autoNamePath + "\\";
\r
253 AutoNamePath = filePath + destination_filename + ".mp4";
\r
254 else if (format == 1)
\r
255 AutoNamePath = filePath + destination_filename + ".m4v";
\r
256 else if (format == 2)
\r
257 AutoNamePath = filePath + destination_filename + ".mkv";
\r
258 else if (format == 3)
\r
259 AutoNamePath = filePath + destination_filename + ".avi";
\r
260 else if (format == 4)
\r
261 AutoNamePath = filePath + destination_filename + ".ogm";
\r
263 else // If the text box already has a path and file
\r
265 string destination = AutoNamePath;
\r
266 string[] destName = dest.Split('\\');
\r
267 string[] extension = dest.Split('.');
\r
268 string ext = extension[extension.Length - 1];
\r
270 destName[destName.Length - 1] = destination_filename + "." + ext;
\r
272 string fullDest = "";
\r
273 foreach (string part in destName)
\r
275 if (fullDest != "")
\r
276 fullDest = fullDest + "\\" + part;
\r
278 fullDest = fullDest + part;
\r
283 return AutoNamePath;
\r
287 /// Checks for updates and returns true if an update is available.
\r
289 /// <param name="debug">Turns on debug mode. Don't use on program startup</param>
\r
290 /// <returns>Boolean True = Update available</returns>
\r
291 public Boolean updateCheck(Boolean debug)
\r
295 Functions.AppcastReader rssRead = new Functions.AppcastReader();
\r
296 rssRead.getInfo(); // Initializes the class.
\r
297 string build = rssRead.build();
\r
299 int latest = int.Parse(build);
\r
300 int current = Properties.Settings.Default.hb_build;
\r
301 int skip = Properties.Settings.Default.skipversion;
\r
303 if (latest == skip)
\r
307 Boolean update = (latest > current);
\r
311 catch (Exception exc)
\r
314 MessageBox.Show("Unable to check for updates, Please try again later. \n" + exc.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
\r
320 /// Get's HandBrakes version data from the CLI.
\r
322 /// <returns>Arraylist of Version Data. 0 = hb_version 1 = hb_build</returns>
\r
323 public ArrayList getCliVersionData()
\r
325 ArrayList cliVersionData = new ArrayList();
\r
326 // 0 = SVN Build / Version
\r
329 Process cliProcess = new Process();
\r
330 ProcessStartInfo handBrakeCLI = new ProcessStartInfo("HandBrakeCLI.exe", " -u");
\r
331 handBrakeCLI.UseShellExecute = false;
\r
332 handBrakeCLI.RedirectStandardError = true;
\r
333 handBrakeCLI.RedirectStandardOutput = true;
\r
334 handBrakeCLI.CreateNoWindow = true;
\r
335 cliProcess.StartInfo = handBrakeCLI;
\r
336 cliProcess.Start();
\r
338 // Retrieve standard output and report back to parent thread until the process is complete
\r
340 TextReader stdOutput = cliProcess.StandardError;
\r
342 while (!cliProcess.HasExited)
\r
344 line = stdOutput.ReadLine();
\r
345 if (line == null) line = "";
\r
346 Match m = Regex.Match(line, @"HandBrake ([0-9\.]*)*(svn[0-9]*[M]*)* \([0-9]*\)");
\r
348 if (m.Success != false)
\r
350 string data = line.Replace("(", "").Replace(")", "").Replace("HandBrake ", "");
\r
351 string[] arr = data.Split(' ');
\r
352 cliVersionData.Add(arr[0]);
\r
353 cliVersionData.Add(arr[1]);
\r
354 return cliVersionData;
\r
361 /// Check if the queue recovery file contains records.
\r
362 /// If it does, it means the last queue did not complete before HandBrake closed.
\r
363 /// So, return a boolean if true.
\r
365 public Boolean check_queue_recovery()
\r
369 string tempPath = Path.Combine(Path.GetTempPath(), "hb_queue_recovery.xml");
\r
370 if (File.Exists(tempPath))
\r
372 using (FileStream strm = new FileStream(tempPath, FileMode.Open, FileAccess.Read))
\r
374 List<Queue.QueueItem> list = ser.Deserialize(strm) as List<Queue.QueueItem>;
\r
375 if (list.Count != 0)
\r
383 return false; // Keep quiet about the error.
\r