From: sr55 Date: Mon, 13 Oct 2008 21:54:27 +0000 (+0000) Subject: WinGui: X-Git-Tag: 0.9.3~114 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=18daea7ae350040ce3307d18e3cedcb9e9c5f5f2;p=handbrake WinGui: - Started to decouple this code a bit. Common.cs (now Main.cs) no longer relies on the frmMain window being passed into it. frmAddPreset no longer requires to access QueryGenerator.cs - QueryGenerator.cs, x264Panel.cs, PresetLoader.cs all moved into their own "frmMain" folder as that is the only place they should be used. - Rearranged the base of frmMain.cs a bit. git-svn-id: svn://svn.handbrake.fr/HandBrake/trunk@1830 b64f7644-9d1e-0410-96f1-a4d463321fa5 --- diff --git a/win/C#/Functions/Main.cs b/win/C#/Functions/Main.cs new file mode 100644 index 000000000..1fba55ebf --- /dev/null +++ b/win/C#/Functions/Main.cs @@ -0,0 +1,405 @@ +/* Common.cs $ + + This file is part of the HandBrake source code. + Homepage: . + It may be used under the terms of the GNU General Public License. */ + +using System; +using System.Collections; +using System.Text; +using System.Windows.Forms; +using System.Globalization; +using System.IO; +using System.Drawing; +using System.Diagnostics; +using System.Text.RegularExpressions; + +namespace Handbrake.Functions +{ + class Main + { + /// + /// Take in a File destination and change it's file extension to a new Extension + /// + /// + /// + /// String of the new file path and extension + public string setExtension(string destination, string newExtension) + { + destination.Replace(".mp4", newExtension); + destination.Replace(".m4v", newExtension); + destination.Replace(".mkv", newExtension); + destination.Replace(".avi", newExtension); + destination.Replace(".ogm", newExtension); + + return destination; + } + + /// + /// Calculate the duration of the selected title and chapters + /// + public TimeSpan calculateDuration(string chapter_start, string chapter_end, Parsing.Title selectedTitle) + { + TimeSpan Duration = TimeSpan.FromSeconds(0.0); + + // Get the durations between the 2 chapter points and add them together. + if (chapter_start != "Auto" && chapter_end != "Auto") + { + int start_chapter, end_chapter = 0; + int.TryParse(chapter_start, out start_chapter); + int.TryParse(chapter_end, out end_chapter); + + int position = start_chapter - 1; + + if (start_chapter <= end_chapter) + { + if (end_chapter > selectedTitle.Chapters.Count) + end_chapter = selectedTitle.Chapters.Count; + + while (position != end_chapter) + { + TimeSpan dur = selectedTitle.Chapters[position].Duration; + Duration = Duration + dur; + position++; + } + } + } + return Duration; + } + + /// + /// Calculate the non-anamorphic resoltuion of the source + /// + /// + /// + public int cacluateNonAnamorphicHeight(int width, decimal top, decimal bottom, decimal left, decimal right, Parsing.Title selectedTitle) + { + float aspect = selectedTitle.AspectRatio; + int aw; + int ah; + if (aspect.ToString() == "1.78") + { + aw = 16; + ah = 9; + } + else + { + aw = 4; + ah = 3; + } + + double a = width * selectedTitle.Resolution.Width * ah * (selectedTitle.Resolution.Height - (double)top - (double)bottom); + double b = selectedTitle.Resolution.Height * aw * (selectedTitle.Resolution.Width - (double)left - (double)right); + + double y = a / b; + + // If it's not Mod 16, make it mod 16 + if ((y % 16) != 0) + { + double mod16 = y % 16; + if (mod16 >= 8) + { + mod16 = 16 - mod16; + y = y + mod16; + } + else + { + y = y - mod16; + } + } + + //16 * (421 / 16) + //double z = ( 16 * (( y + 8 ) / 16 ) ); + int x = int.Parse(y.ToString()); + return x; + } + + /// + /// Select the longest title in the DVD title dropdown menu on frmMain + /// + public Handbrake.Parsing.Title selectLongestTitle(ComboBox drp_dvdtitle) + { + int current_largest = 0; + Handbrake.Parsing.Title title2Select; + + // Check if there are titles in the DVD title dropdown menu and make sure, it's not just "Automatic" + if (drp_dvdtitle.Items[0].ToString() != "Automatic") + title2Select = (Handbrake.Parsing.Title)drp_dvdtitle.Items[0]; + else + title2Select = null; + + // So, If there are titles in the DVD Title dropdown menu, lets select the longest. + if (title2Select != null) + { + foreach (Handbrake.Parsing.Title x in drp_dvdtitle.Items) + { + string title = x.ToString(); + if (title != "Automatic") + { + string[] y = title.Split(' '); + string time = y[1].Replace("(", "").Replace(")", ""); + string[] z = time.Split(':'); + + int hours = int.Parse(z[0]) * 60 * 60; + int minutes = int.Parse(z[1]) * 60; + int seconds = int.Parse(z[2]); + int total_sec = hours + minutes + seconds; + + if (current_largest == 0) + { + current_largest = hours + minutes + seconds; + title2Select = x; + } + else + { + if (total_sec > current_largest) + { + current_largest = total_sec; + title2Select = x; + } + } + } + } + } + return title2Select; + } + + /// + /// Set's up the DataGridView on the Chapters tab (frmMain) + /// + /// + public DataGridView chapterNaming(DataGridView data_chpt, string chapter_start, string chapter_end) + { + try + { + int i = 0; + int rowCount = 0; + int start = 0; + int finish = 0; + if (chapter_end != "Auto") + finish = int.Parse(chapter_end); + + if (chapter_start != "Auto") + start = int.Parse(chapter_start); + + rowCount = finish - (start - 1); + + while (i < rowCount) + { + DataGridViewRow row = new DataGridViewRow(); + + data_chpt.Rows.Insert(i, row); + data_chpt.Rows[i].Cells[0].Value = (i + 1); + data_chpt.Rows[i].Cells[1].Value = "Chapter " + (i + 1); + i++; + } + return data_chpt; + } + catch (Exception exc) + { + MessageBox.Show("chapterNaming() Error has occured: \n" + exc.ToString()); + return null; + } + } + + /// + /// Function which generates the filename and path automatically based on + /// the Source Name, DVD title and DVD Chapters + /// + /// + public string autoName(ComboBox drp_dvdtitle, string chapter_start, string chatper_end, string source, string dest, int format) + { + + string AutoNamePath = string.Empty; + + if (drp_dvdtitle.Text != "Automatic") + { + // Todo: This code is a tad messy. Clean it up at some point. + // Get the Source Name + string[] sourceName = source.Split('\\'); + source = sourceName[sourceName.Length - 1].Replace(".iso", "").Replace(".mpg", "").Replace(".ts", "").Replace(".ps", ""); + + // Get the Selected Title Number + string title = drp_dvdtitle.Text; + string[] titlesplit = title.Split(' '); + title = titlesplit[0]; + + // Get the Chapter Start and Chapter End Numbers + string cs = chapter_start; + string cf = chatper_end; + + // Just incase the above are set to their default Automatic values, set the varible to "" + if (title == "Automatic") + title = ""; + if (cs == "Auto") + cs = ""; + if (cf == "Auto") + cf = ""; + + // If both CS and CF are populated, set the dash varible + string dash = ""; + if (cf != "Auto") + dash = "-"; + + // Get the destination filename. + string destination_filename = ""; + if (Properties.Settings.Default.autoNameFormat != "") + { + destination_filename = Properties.Settings.Default.autoNameFormat; + destination_filename = destination_filename.Replace("{source}", source).Replace("{title}", title).Replace("{chapters}", cs + dash + cf); + } + else + destination_filename = source + "_T" + title + "_C" + cs + dash + cf; + + // If the text box is blank + if (!dest.Contains("\\")) + { + string filePath = ""; + if (Properties.Settings.Default.autoNamePath.Trim() != "") + { + if (Properties.Settings.Default.autoNamePath.Trim() != "Click 'Browse' to set the default location") + filePath = Properties.Settings.Default.autoNamePath + "\\"; + } + + if (format == 0) + AutoNamePath = filePath + destination_filename + ".mp4"; + else if (format == 1) + AutoNamePath = filePath + destination_filename + ".m4v"; + else if (format == 2) + AutoNamePath = filePath + destination_filename + ".mkv"; + else if (format == 3) + AutoNamePath = filePath + destination_filename + ".avi"; + else if (format == 4) + AutoNamePath = filePath + destination_filename + ".ogm"; + } + else // If the text box already has a path and file + { + string destination = AutoNamePath; + string[] destName = dest.Split('\\'); + string[] extension = dest.Split('.'); + string ext = extension[extension.Length - 1]; + + destName[destName.Length - 1] = destination_filename + "." + ext; + + string fullDest = ""; + foreach (string part in destName) + { + if (fullDest != "") + fullDest = fullDest + "\\" + part; + else + fullDest = fullDest + part; + } + return fullDest; + } + } + return AutoNamePath; + } + + /// + /// Checks for updates and returns true if an update is available. + /// + /// Turns on debug mode. Don't use on program startup + /// Boolean True = Update available + public Boolean updateCheck(Boolean debug) + { + try + { + Functions.AppcastReader rssRead = new Functions.AppcastReader(); + rssRead.getInfo(); // Initializes the class. + string build = rssRead.build(); + + int latest = int.Parse(build); + int current = Properties.Settings.Default.hb_build; + int skip = Properties.Settings.Default.skipversion; + + if (latest == skip) + return false; + else + { + Boolean update = (latest > current); + return update; + } + } + catch (Exception exc) + { + if (debug == true) + MessageBox.Show("Unable to check for updates, Please try again later. \n" + exc.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + return false; + } + } + + /// + /// Get's HandBrakes version data from the CLI. + /// + /// Arraylist of Version Data. 0 = hb_version 1 = hb_build + public ArrayList getCliVersionData() + { + ArrayList cliVersionData = new ArrayList(); + // 0 = SVN Build / Version + // 1 = Build Date + + Process cliProcess = new Process(); + ProcessStartInfo handBrakeCLI = new ProcessStartInfo("HandBrakeCLI.exe", " -u"); + handBrakeCLI.UseShellExecute = false; + handBrakeCLI.RedirectStandardError = true; + handBrakeCLI.RedirectStandardOutput = true; + handBrakeCLI.CreateNoWindow = true; + cliProcess.StartInfo = handBrakeCLI; + cliProcess.Start(); + + // Retrieve standard output and report back to parent thread until the process is complete + String line; + TextReader stdOutput = cliProcess.StandardError; + + while (!cliProcess.HasExited) + { + line = stdOutput.ReadLine(); + Match m = Regex.Match(line, @"HandBrake [0-9\.]*svn[0-9]*[M]* \([0-9]*\)"); + if (m.Success != false) + { + string data = line.Replace("(", "").Replace(")", "").Replace("HandBrake ", ""); + string[] arr = data.Split(' '); + cliVersionData.Add(arr[0]); + cliVersionData.Add(arr[1]); + return cliVersionData; + } + } + return null; + } + + /// + /// Check if the queue recovery file contains records. + /// If it does, it means the last queue did not complete before HandBrake closed. + /// So, return a boolean if true. + /// + public Boolean check_queue_recovery() + { + try + { + string tempPath = Path.Combine(Path.GetTempPath(), "hb_queue_recovery.dat"); + using (StreamReader reader = new StreamReader(tempPath)) + { + string queue_item = reader.ReadLine(); + if (queue_item == null) + { + reader.Close(); + reader.Dispose(); + return false; + } + else // There exists an item in the recovery queue file, so try and recovr it. + { + reader.Close(); + reader.Dispose(); + return true; + } + } + } + catch (Exception) + { + // Keep quiet about the error. + return false; + } + } + + } +} \ No newline at end of file diff --git a/win/C#/HandBrakeCS.csproj b/win/C#/HandBrakeCS.csproj index a1809da1d..5a4c605b9 100644 --- a/win/C#/HandBrakeCS.csproj +++ b/win/C#/HandBrakeCS.csproj @@ -146,15 +146,16 @@ frmUpdater.cs - + + - + - + diff --git a/win/C#/frmAddPreset.cs b/win/C#/frmAddPreset.cs index c2f754ffe..bc2743771 100644 --- a/win/C#/frmAddPreset.cs +++ b/win/C#/frmAddPreset.cs @@ -17,21 +17,20 @@ namespace Handbrake { public partial class frmAddPreset : Form { - Functions.QueryGenerator queryGen = new Functions.QueryGenerator(); private frmMain frmMainWindow; Functions.Presets presetCode; - - public frmAddPreset(frmMain fmw, Functions.Presets presetHandler) + private string query = ""; + + public frmAddPreset(frmMain fmw, string query_string, Functions.Presets presetHandler) { InitializeComponent(); frmMainWindow = fmw; presetCode = presetHandler; + this.query = query_string; } private void btn_add_Click(object sender, EventArgs e) { - String query = queryGen.generateTabbedComponentsQuery(frmMainWindow); - if (presetCode.addPreset(txt_preset_name.Text.Trim(), query) == true) { frmMainWindow.loadPresetPanel(); diff --git a/win/C#/frmMain.Designer.cs b/win/C#/frmMain.Designer.cs index 09ecf5ec1..ec9953948 100644 --- a/win/C#/frmMain.Designer.cs +++ b/win/C#/frmMain.Designer.cs @@ -38,7 +38,7 @@ namespace Handbrake System.Windows.Forms.Label Label38; System.Windows.Forms.ContextMenuStrip notifyIconMenu; System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(frmMain)); - System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle2 = new System.Windows.Forms.DataGridViewCellStyle(); + System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle(); this.btn_restore = new System.Windows.Forms.ToolStripMenuItem(); this.DVD_Save = new System.Windows.Forms.SaveFileDialog(); this.File_Save = new System.Windows.Forms.SaveFileDialog(); @@ -641,9 +641,9 @@ namespace Handbrake // // number // - dataGridViewCellStyle2.Format = "N0"; - dataGridViewCellStyle2.NullValue = null; - this.number.DefaultCellStyle = dataGridViewCellStyle2; + dataGridViewCellStyle1.Format = "N0"; + dataGridViewCellStyle1.NullValue = null; + this.number.DefaultCellStyle = dataGridViewCellStyle1; this.number.HeaderText = "Chapter Number"; this.number.MaxInputLength = 3; this.number.Name = "number"; @@ -3384,7 +3384,6 @@ namespace Handbrake private System.Windows.Forms.ToolStripMenuItem btn_new_preset; private System.Windows.Forms.ToolStripMenuItem mnu_handbrake_forums; private System.Windows.Forms.ToolStripMenuItem mnu_user_guide; - private System.Windows.Forms.ToolStripMenuItem mnu_dvd_drive; private System.Windows.Forms.ToolStripDropDownButton btn_source; private System.Windows.Forms.ToolStripSeparator toolStripSeparator1; private System.Windows.Forms.ToolStripMenuItem btn_dvd_source; @@ -3402,6 +3401,7 @@ namespace Handbrake internal System.Windows.Forms.Label label8; internal System.Windows.Forms.OpenFileDialog ISO_Open; internal System.Windows.Forms.FolderBrowserDialog DVD_Open; + internal System.Windows.Forms.ToolStripMenuItem mnu_dvd_drive; } } \ No newline at end of file diff --git a/win/C#/frmMain.cs b/win/C#/frmMain.cs index 5c74029e8..7b91e2e16 100644 --- a/win/C#/frmMain.cs +++ b/win/C#/frmMain.cs @@ -22,13 +22,18 @@ namespace Handbrake public partial class frmMain : Form { // Declarations ******************************************************* - Functions.Common hb_common_func = new Functions.Common(); - Functions.x264Panel x264PanelFunctions = new Functions.x264Panel(); + // Objects which may be used by one or more other objects + Functions.Main hb_common_func = new Functions.Main(); Functions.Encode cliObj = new Functions.Encode(); Functions.Queue encodeQueue = new Functions.Queue(); Functions.Presets presetHandler = new Functions.Presets(); - Functions.QueryGenerator queryGen = new Functions.QueryGenerator(); Parsing.Title selectedTitle; + + // Objects belonging to this window only + PresetLoader presetLoader = new PresetLoader(); + x264Panel x264PanelFunctions = new x264Panel(); + QueryGenerator queryGen = new QueryGenerator(); + internal Process hbProc; private Parsing.DVD thisDVD; private frmQueue queueWindow; @@ -47,7 +52,6 @@ namespace Handbrake // Initialize the queue window. queueWindow = new frmQueue(this); - //Create a label that can be updated from the parent thread. Label lblStatus = new Label(); lblStatus.Size = new Size(250, 20); @@ -166,7 +170,7 @@ namespace Handbrake else { Functions.QueryParser presetQuery = Functions.QueryParser.Parse(userDefaults); - hb_common_func.presetLoader(this, presetQuery, "User Defaults "); + presetLoader.presetLoader(this, presetQuery, "User Defaults "); } } private void queueRecovery() @@ -250,7 +254,7 @@ namespace Handbrake } private void btn_new_preset_Click(object sender, EventArgs e) { - Form preset = new frmAddPreset(this, presetHandler); + Form preset = new frmAddPreset(this, queryGen.GenerateTheQuery(this), presetHandler); preset.ShowDialog(); } #endregion @@ -528,16 +532,21 @@ namespace Handbrake } // Run the autoName & chapterNaming functions - hb_common_func.autoName(this); - hb_common_func.chapterNaming(this); + if (Properties.Settings.Default.autoNaming == "Checked") + text_destination.Text = hb_common_func.autoName(drp_dvdtitle, drop_chapterStart.Text, drop_chapterFinish.Text, text_source.Text, text_destination.Text, drop_format.SelectedIndex); + + data_chpt.Rows.Clear(); + DataGridView chapterGridView = hb_common_func.chapterNaming(data_chpt, drop_chapterStart.Text, drop_chapterFinish.Text); + if (chapterGridView != null) + data_chpt = chapterGridView; } private void drop_chapterStart_SelectedIndexChanged(object sender, EventArgs e) { int c_start, c_end = 1; if (drop_chapterFinish.Text == "Auto" && drop_chapterFinish.Items.Count != 0) - drop_chapterFinish.SelectedIndex = drop_chapterFinish.Items.Count-1; - + drop_chapterFinish.SelectedIndex = drop_chapterFinish.Items.Count - 1; + int.TryParse(drop_chapterStart.Text, out c_start); int.TryParse(drop_chapterFinish.Text, out c_end); @@ -547,10 +556,11 @@ namespace Handbrake drop_chapterFinish.Text = c_start.ToString(); } - calculateDuration(); + lbl_duration.Text = hb_common_func.calculateDuration(drop_chapterStart.Text, drop_chapterFinish.Text, selectedTitle).ToString(); // Run the Autonaming function - hb_common_func.autoName(this); + if (Properties.Settings.Default.autoNaming == "Checked") + text_destination.Text = hb_common_func.autoName(drp_dvdtitle, drop_chapterStart.Text, drop_chapterFinish.Text, text_source.Text, text_destination.Text, drop_format.SelectedIndex); } private void drop_chapterFinish_SelectedIndexChanged(object sender, EventArgs e) { @@ -568,10 +578,11 @@ namespace Handbrake drop_chapterFinish.Text = c_start.ToString(); } - calculateDuration(); + lbl_duration.Text = hb_common_func.calculateDuration(drop_chapterStart.Text, drop_chapterFinish.Text, selectedTitle).ToString(); // Run the Autonaming function - hb_common_func.autoName(this); + if (Properties.Settings.Default.autoNaming == "Checked") + text_destination.Text = hb_common_func.autoName(drp_dvdtitle, drop_chapterStart.Text, drop_chapterFinish.Text, text_source.Text, text_destination.Text, drop_format.SelectedIndex); } //Destination @@ -617,17 +628,16 @@ namespace Handbrake // Output Settings private void drop_format_SelectedIndexChanged(object sender, EventArgs e) { - if (drop_format.SelectedIndex == 0) - setExtension(".mp4"); + text_destination.Text = hb_common_func.setExtension(text_destination.Text, ".mp4"); else if (drop_format.SelectedIndex == 1) - setExtension(".m4v"); + text_destination.Text = hb_common_func.setExtension(text_destination.Text, ".m4v"); else if (drop_format.SelectedIndex == 2) - setExtension(".mkv"); + text_destination.Text = hb_common_func.setExtension(text_destination.Text, ".mkv"); else if (drop_format.SelectedIndex == 3) - setExtension(".avi"); + text_destination.Text = hb_common_func.setExtension(text_destination.Text, ".avi"); else if (drop_format.SelectedIndex == 4) - setExtension(".ogm"); + text_destination.Text = hb_common_func.setExtension(text_destination.Text, ".ogm"); } //Video Tab @@ -730,7 +740,7 @@ namespace Handbrake { if (drp_anamorphic.Text == "None") { - int height = cacluateNonAnamorphicHeight(width); + int height = hb_common_func.cacluateNonAnamorphicHeight(width, text_top.Value, text_bottom.Value,text_left.Value, text_right.Value, selectedTitle); text_height.Text = height.ToString(); } } @@ -1212,7 +1222,9 @@ namespace Handbrake text_destination.Text = destination; data_chpt.Rows.Clear(); data_chpt.Enabled = true; - hb_common_func.chapterNaming(this); + DataGridView chapterGridView = hb_common_func.chapterNaming(data_chpt, drop_chapterStart.Text, drop_chapterFinish.Text); + if (chapterGridView != null) + data_chpt = chapterGridView; } else { @@ -1337,7 +1349,7 @@ namespace Handbrake // Presets private void btn_addPreset_Click(object sender, EventArgs e) { - Form preset = new frmAddPreset(this, presetHandler); + Form preset = new frmAddPreset(this, queryGen.GenerateTheQuery(this), presetHandler); preset.ShowDialog(); } private void btn_removePreset_Click(object sender, EventArgs e) @@ -1373,7 +1385,7 @@ namespace Handbrake Functions.QueryParser presetQuery = Functions.QueryParser.Parse(query); // Now load the preset - hb_common_func.presetLoader(this, presetQuery, presetName); + presetLoader.presetLoader(this, presetQuery, presetName); // The x264 widgets will need updated, so do this now: x264PanelFunctions.X264_StandardizeOptString(this); @@ -1397,97 +1409,60 @@ namespace Handbrake #endregion #region Functions - // Replace File extenstion. - public void setExtension(string newExtension) - { - text_destination.Text = text_destination.Text.Replace(".mp4", newExtension); - text_destination.Text = text_destination.Text.Replace(".m4v", newExtension); - text_destination.Text = text_destination.Text.Replace(".mkv", newExtension); - text_destination.Text = text_destination.Text.Replace(".avi", newExtension); - text_destination.Text = text_destination.Text.Replace(".ogm", newExtension); - } - - // DVD Parsing - public void setStreamReader(Parsing.DVD dvd) + private void loadNormalPreset() { - this.thisDVD = dvd; + foreach (TreeNode treenode in treeView_presets.Nodes) + { + if (treenode.Text.ToString().Equals("Normal")) + treeView_presets.SelectedNode = treeView_presets.Nodes[treenode.Index]; + } } - - // Chapter Selection Duration calculation - public void calculateDuration() + #endregion + + #region Drive Detection + // Source Button Drive Detection + private delegate void ProgressUpdateHandler(); + private void getDriveInfoThread() { - TimeSpan Duration = TimeSpan.FromSeconds(0.0); - - // Get the durations between the 2 chapter points and add them together. - if (drop_chapterStart.Text != "Auto" && drop_chapterFinish.Text != "Auto") + try { - int start_chapter, end_chapter = 0; - int.TryParse(drop_chapterStart.Text, out start_chapter); - int.TryParse(drop_chapterFinish.Text, out end_chapter); - - int position = start_chapter - 1; - - if (start_chapter <= end_chapter) + if (this.InvokeRequired) { - if (end_chapter > selectedTitle.Chapters.Count) - end_chapter = selectedTitle.Chapters.Count; + this.BeginInvoke(new ProgressUpdateHandler(getDriveInfoThread)); + return; + } - while (position != end_chapter) + Boolean foundDrive = false; + DriveInfo[] theCollectionOfDrives = DriveInfo.GetDrives(); + foreach (DriveInfo curDrive in theCollectionOfDrives) + { + if (curDrive.DriveType == DriveType.CDRom) { - TimeSpan dur = selectedTitle.Chapters[position].Duration; - Duration = Duration + dur; - position++; + if (curDrive.IsReady) + { + if (File.Exists(curDrive.RootDirectory.ToString() + "VIDEO_TS\\VIDEO_TS.IFO")) + mnu_dvd_drive.Text = curDrive.RootDirectory.ToString() + "VIDEO_TS (" + curDrive.VolumeLabel + ")"; + else + mnu_dvd_drive.Text = "[No DVD Drive Ready]"; + + foundDrive = true; + + } } } - } - // Set the Duration - lbl_duration.Text = Duration.ToString(); - } - public int cacluateNonAnamorphicHeight(int width) - { - float aspect = selectedTitle.AspectRatio; - int aw; - int ah; - if (aspect.ToString() == "1.78") - { - aw = 16; - ah = 9; - } - else - { - aw = 4; - ah = 3; + if (foundDrive == false) + mnu_dvd_drive.Text = "[No DVD Drive Ready]"; } - - double a = width * selectedTitle.Resolution.Width * ah * (selectedTitle.Resolution.Height - (double)text_top.Value - (double)text_bottom.Value); - double b = selectedTitle.Resolution.Height * aw * (selectedTitle.Resolution.Width - (double)text_left.Value - (double)text_right.Value); - - double y = a / b; - - // If it's not Mod 16, make it mod 16 - if ((y % 16) != 0) + catch (Exception) { - double mod16 = y % 16; - if (mod16 >= 8) - { - mod16 = 16 - mod16; - y = y + mod16; - } - else - { - y = y - mod16; - } + mnu_dvd_drive.Text = "[No DVD Drive Ready / Found]"; } - - //16 * (421 / 16) - //double z = ( 16 * (( y + 8 ) / 16 ) ); - int x = int.Parse(y.ToString()); - return x; } + #endregion - // Audio system functions - private void setAudioByContainer(String path) + #region Audio Panel Stuff + public void setAudioByContainer(String path) { string oldval = ""; @@ -1636,7 +1611,7 @@ namespace Handbrake } } } - private void setVideoByContainer(String path) + public void setVideoByContainer(String path) { string oldval = ""; @@ -1688,7 +1663,7 @@ namespace Handbrake drp_videoEncoder.Text = oldval; } } - private void setBitrateSelections384(ComboBox dropDown) + public void setBitrateSelections384(ComboBox dropDown) { dropDown.Items.Clear(); dropDown.Items.Add("32"); @@ -1707,7 +1682,7 @@ namespace Handbrake dropDown.Items.Add("320"); dropDown.Items.Add("384"); } - private void setBitrateSelections320(ComboBox dropDown) + public void setBitrateSelections320(ComboBox dropDown) { dropDown.Items.Clear(); dropDown.Items.Add("32"); @@ -1725,7 +1700,7 @@ namespace Handbrake dropDown.Items.Add("256"); dropDown.Items.Add("320"); } - private void setBitrateSelections160(ComboBox dropDown) + public void setBitrateSelections160(ComboBox dropDown) { dropDown.Items.Clear(); dropDown.Items.Add("32"); @@ -1739,84 +1714,6 @@ namespace Handbrake dropDown.Items.Add("128"); dropDown.Items.Add("160"); } - - // Preset system functions - private void loadNormalPreset() - { - foreach (TreeNode treenode in treeView_presets.Nodes) - { - if (treenode.Text.ToString().Equals("Normal")) - treeView_presets.SelectedNode = treeView_presets.Nodes[treenode.Index]; - } - } - public void loadPresetPanel() - { - presetHandler.loadPresetFiles(); - - treeView_presets.Nodes.Clear(); - List presetNameList = new List(); - TreeNode preset_treeview = new TreeNode(); - - presetNameList = presetHandler.getBuildInPresetNames(); - foreach (string preset in presetNameList) - { - preset_treeview = new TreeNode(preset); - - // Now Fill Out List View with Items - treeView_presets.Nodes.Add(preset_treeview); - } - - presetNameList = presetHandler.getUserPresetNames(); - foreach (string preset in presetNameList) - { - preset_treeview = new TreeNode(preset); - preset_treeview.ForeColor = Color.Black; - - // Now Fill Out List View with Items - treeView_presets.Nodes.Add(preset_treeview); - } - } - - // Source Button Drive Detection - private delegate void ProgressUpdateHandler(); - private void getDriveInfoThread() - { - try - { - if (this.InvokeRequired) - { - this.BeginInvoke(new ProgressUpdateHandler(getDriveInfoThread)); - return; - } - - Boolean foundDrive = false; - DriveInfo[] theCollectionOfDrives = DriveInfo.GetDrives(); - foreach (DriveInfo curDrive in theCollectionOfDrives) - { - if (curDrive.DriveType == DriveType.CDRom) - { - if (curDrive.IsReady) - { - if (File.Exists(curDrive.RootDirectory.ToString() + "VIDEO_TS\\VIDEO_TS.IFO")) - mnu_dvd_drive.Text = curDrive.RootDirectory.ToString() + "VIDEO_TS (" + curDrive.VolumeLabel + ")"; - else - mnu_dvd_drive.Text = "[No DVD Drive Ready]"; - - foundDrive = true; - - } - } - } - - if (foundDrive == false) - mnu_dvd_drive.Text = "[No DVD Drive Ready]"; - } - catch (Exception) - { - mnu_dvd_drive.Text = "[No DVD Drive Ready / Found]"; - } - } - #endregion #region Encoding @@ -1876,6 +1773,7 @@ namespace Handbrake else return true; } + /// /// Action can be "encode" or "scan" /// Set the last action varible in the main window. @@ -1886,6 +1784,48 @@ namespace Handbrake { this.lastAction = last; } + + /// + /// DVD parseing. Pass in a parsed DVD. + /// + /// + public void setStreamReader(Parsing.DVD dvd) + { + this.thisDVD = dvd; + } + + + /// + /// Reload the preset panel display + /// + public void loadPresetPanel() + { + presetHandler.loadPresetFiles(); + + treeView_presets.Nodes.Clear(); + List presetNameList = new List(); + TreeNode preset_treeview = new TreeNode(); + + presetNameList = presetHandler.getBuildInPresetNames(); + foreach (string preset in presetNameList) + { + preset_treeview = new TreeNode(preset); + + // Now Fill Out List View with Items + treeView_presets.Nodes.Add(preset_treeview); + } + + presetNameList = presetHandler.getUserPresetNames(); + foreach (string preset in presetNameList) + { + preset_treeview = new TreeNode(preset); + preset_treeview.ForeColor = Color.Black; + + // Now Fill Out List View with Items + treeView_presets.Nodes.Add(preset_treeview); + } + } + #endregion #region Taskbar Tray Icon diff --git a/win/C#/frmMain.resx b/win/C#/frmMain.resx index ed1b7ad13..7636871d6 100644 --- a/win/C#/frmMain.resx +++ b/win/C#/frmMain.resx @@ -155,12 +155,6 @@ Make sure you have selected a "Title" from the "Source" box above otherwise the list will not be populated with the correct amount of chapters. Note: Do not change any of the chapter numbers! - - True - - - True - CABAC, or context adaptive binary arithmetic coding, is used by x264 to reduce the bitrate needed for a given quality by 15%. This makes it very cool and very useful, and it should be left on whenever possible. However, it is incompatible with the iPod 5.5G, and makes the AppleTV struggle. diff --git a/win/C#/Functions/Common.cs b/win/C#/frmMain/PresetLoader.cs similarity index 51% rename from win/C#/Functions/Common.cs rename to win/C#/frmMain/PresetLoader.cs index 27484000b..c42f8202e 100644 --- a/win/C#/Functions/Common.cs +++ b/win/C#/frmMain/PresetLoader.cs @@ -1,24 +1,13 @@ -/* Common.cs $ - - This file is part of the HandBrake source code. - Homepage: . - It may be used under the terms of the GNU General Public License. */ - -using System; -using System.Collections; +using System; +using System.Collections.Generic; using System.Text; using System.Windows.Forms; -using System.Globalization; -using System.IO; using System.Drawing; -using System.Diagnostics; -using System.Text.RegularExpressions; -namespace Handbrake.Functions +namespace Handbrake { - class Common + class PresetLoader { - /// /// This function takes in a Query which has been parsed by QueryParser and /// set's all the GUI widgets correctly. @@ -358,295 +347,5 @@ namespace Handbrake.Functions mainWindow.groupBox_output.Text = "Output Settings (Preset: " + name + ")"; #endregion } - - /// - /// Select the longest title in the DVD title dropdown menu on frmMain - /// - public void selectLongestTitle(frmMain mainWindow) - { - int current_largest = 0; - Handbrake.Parsing.Title title2Select; - - // Check if there are titles in the DVD title dropdown menu and make sure, it's not just "Automatic" - if (mainWindow.drp_dvdtitle.Items[0].ToString() != "Automatic") - title2Select = (Handbrake.Parsing.Title)mainWindow.drp_dvdtitle.Items[0]; - else - title2Select = null; - - // So, If there are titles in the DVD Title dropdown menu, lets select the longest. - if (title2Select != null) - { - foreach (Handbrake.Parsing.Title x in mainWindow.drp_dvdtitle.Items) - { - string title = x.ToString(); - if (title != "Automatic") - { - string[] y = title.Split(' '); - string time = y[1].Replace("(", "").Replace(")", ""); - string[] z = time.Split(':'); - - int hours = int.Parse(z[0]) * 60 * 60; - int minutes = int.Parse(z[1]) * 60; - int seconds = int.Parse(z[2]); - int total_sec = hours + minutes + seconds; - - if (current_largest == 0) - { - current_largest = hours + minutes + seconds; - title2Select = x; - } - else - { - if (total_sec > current_largest) - { - current_largest = total_sec; - title2Select = x; - } - } - } - } - - // Now set the longest title in the gui. - mainWindow.drp_dvdtitle.SelectedItem = title2Select; - } - } - - /// - /// Set's up the DataGridView on the Chapters tab (frmMain) - /// - /// - public void chapterNaming(frmMain mainWindow) - { - try - { - mainWindow.data_chpt.Rows.Clear(); - int i = 0; - int rowCount = 0; - int start = 0; - int finish = 0; - if (mainWindow.drop_chapterFinish.Text != "Auto") - finish = int.Parse(mainWindow.drop_chapterFinish.Text); - - if (mainWindow.drop_chapterStart.Text != "Auto") - start = int.Parse(mainWindow.drop_chapterStart.Text); - - rowCount = finish - (start - 1); - - while (i < rowCount) - { - DataGridViewRow row = new DataGridViewRow(); - - mainWindow.data_chpt.Rows.Insert(i, row); - mainWindow.data_chpt.Rows[i].Cells[0].Value = (i + 1); - mainWindow.data_chpt.Rows[i].Cells[1].Value = "Chapter " + (i + 1); - i++; - } - } - catch (Exception exc) - { - MessageBox.Show("chapterNaming() Error has occured: \n" + exc.ToString()); - } - } - - /// - /// Function which generates the filename and path automatically based on - /// the Source Name, DVD title and DVD Chapters - /// - /// - public void autoName(frmMain mainWindow) - { - if (Properties.Settings.Default.autoNaming == "Checked") - { - if (mainWindow.drp_dvdtitle.Text != "Automatic") - { - // Todo: This code is a tad messy. Clean it up at some point. - // Get the Source Name - string source = mainWindow.text_source.Text; - string[] sourceName = source.Split('\\'); - source = sourceName[sourceName.Length - 1].Replace(".iso", "").Replace(".mpg", "").Replace(".ts", "").Replace(".ps", ""); - - // Get the Selected Title Number - string title = mainWindow.drp_dvdtitle.Text; - string[] titlesplit = title.Split(' '); - title = titlesplit[0]; - - // Get the Chapter Start and Chapter End Numbers - string cs = mainWindow.drop_chapterStart.Text; - string cf = mainWindow.drop_chapterFinish.Text; - - // Just incase the above are set to their default Automatic values, set the varible to "" - if (title == "Automatic") - title = ""; - if (cs == "Auto") - cs = ""; - if (cf == "Auto") - cf = ""; - - // If both CS and CF are populated, set the dash varible - string dash = ""; - if (cf != "Auto") - dash = "-"; - - // Get the destination filename. - string destination_filename = ""; - if (Properties.Settings.Default.autoNameFormat != "") - { - destination_filename = Properties.Settings.Default.autoNameFormat; - destination_filename = destination_filename.Replace("{source}", source).Replace("{title}", title).Replace("{chapters}", cs + dash + cf); - } - else - destination_filename = source + "_T" + title + "_C" + cs + dash + cf; - - // If the text box is blank - if (!mainWindow.text_destination.Text.Contains("\\")) - { - string filePath = ""; - if (Properties.Settings.Default.autoNamePath.Trim() != "") - { - if (Properties.Settings.Default.autoNamePath.Trim() != "Click 'Browse' to set the default location") - filePath = Properties.Settings.Default.autoNamePath + "\\"; - } - - if (mainWindow.drop_format.SelectedIndex == 0) - mainWindow.text_destination.Text = filePath + destination_filename + ".mp4"; - else if (mainWindow.drop_format.SelectedIndex == 1) - mainWindow.text_destination.Text = filePath + destination_filename + ".m4v"; - else if (mainWindow.drop_format.SelectedIndex == 2) - mainWindow.text_destination.Text = filePath + destination_filename + ".mkv"; - else if (mainWindow.drop_format.SelectedIndex == 3) - mainWindow.text_destination.Text = filePath + destination_filename + ".avi"; - else if (mainWindow.drop_format.SelectedIndex == 4) - mainWindow.text_destination.Text = filePath + destination_filename + ".ogm"; - } - else // If the text box already has a path and file - { - string dest = mainWindow.text_destination.Text; - string[] destName = dest.Split('\\'); - string[] extension = dest.Split('.'); - string ext = extension[extension.Length - 1]; - - destName[destName.Length - 1] = destination_filename + "." + ext; - - string fullDest = ""; - foreach (string part in destName) - { - if (fullDest != "") - fullDest = fullDest + "\\" + part; - else - fullDest = fullDest + part; - } - - mainWindow.text_destination.Text = fullDest; - } - } - } - } - - /// - /// Checks for updates and returns true if an update is available. - /// - /// Turns on debug mode. Don't use on program startup - /// Boolean True = Update available - public Boolean updateCheck(Boolean debug) - { - try - { - Functions.AppcastReader rssRead = new Functions.AppcastReader(); - rssRead.getInfo(); // Initializes the class. - string build = rssRead.build(); - - int latest = int.Parse(build); - int current = Properties.Settings.Default.hb_build; - int skip = Properties.Settings.Default.skipversion; - - if (latest == skip) - return false; - else - { - Boolean update = (latest > current); - return update; - } - } - catch (Exception exc) - { - if (debug == true) - MessageBox.Show("Unable to check for updates, Please try again later. \n" + exc.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); - return false; - } - } - - /// - /// Get's HandBrakes version data from the CLI. - /// - /// Arraylist of Version Data. 0 = hb_version 1 = hb_build - public ArrayList getCliVersionData() - { - ArrayList cliVersionData = new ArrayList(); - // 0 = SVN Build / Version - // 1 = Build Date - - Process cliProcess = new Process(); - ProcessStartInfo handBrakeCLI = new ProcessStartInfo("HandBrakeCLI.exe", " -u"); - handBrakeCLI.UseShellExecute = false; - handBrakeCLI.RedirectStandardError = true; - handBrakeCLI.RedirectStandardOutput = true; - handBrakeCLI.CreateNoWindow = true; - cliProcess.StartInfo = handBrakeCLI; - cliProcess.Start(); - - // Retrieve standard output and report back to parent thread until the process is complete - String line; - TextReader stdOutput = cliProcess.StandardError; - - while (!cliProcess.HasExited) - { - line = stdOutput.ReadLine(); - Match m = Regex.Match(line, @"HandBrake [0-9\.]*svn[0-9]*[M]* \([0-9]*\)"); - if (m.Success != false) - { - string data = line.Replace("(", "").Replace(")", "").Replace("HandBrake ", ""); - string[] arr = data.Split(' '); - cliVersionData.Add(arr[0]); - cliVersionData.Add(arr[1]); - return cliVersionData; - } - } - return null; - } - - /// - /// Check if the queue recovery file contains records. - /// If it does, it means the last queue did not complete before HandBrake closed. - /// So, return a boolean if true. - /// - public Boolean check_queue_recovery() - { - try - { - string tempPath = Path.Combine(Path.GetTempPath(), "hb_queue_recovery.dat"); - using (StreamReader reader = new StreamReader(tempPath)) - { - string queue_item = reader.ReadLine(); - if (queue_item == null) - { - reader.Close(); - reader.Dispose(); - return false; - } - else // There exists an item in the recovery queue file, so try and recovr it. - { - reader.Close(); - reader.Dispose(); - return true; - } - } - } - catch (Exception) - { - // Keep quiet about the error. - return false; - } - } - } -} \ No newline at end of file +} diff --git a/win/C#/Functions/QueryGenerator.cs b/win/C#/frmMain/QueryGenerator.cs similarity index 97% rename from win/C#/Functions/QueryGenerator.cs rename to win/C#/frmMain/QueryGenerator.cs index aa4bdd383..b66e3f041 100644 --- a/win/C#/Functions/QueryGenerator.cs +++ b/win/C#/frmMain/QueryGenerator.cs @@ -5,7 +5,7 @@ using System.Windows.Forms; using System.Globalization; using System.IO; -namespace Handbrake.Functions +namespace Handbrake { class QueryGenerator { diff --git a/win/C#/Functions/x264Panel.cs b/win/C#/frmMain/x264Panel.cs similarity index 97% rename from win/C#/Functions/x264Panel.cs rename to win/C#/frmMain/x264Panel.cs index 2701757c7..13580665c 100644 --- a/win/C#/Functions/x264Panel.cs +++ b/win/C#/frmMain/x264Panel.cs @@ -3,11 +3,10 @@ using System.Collections.Generic; using System.Text; using System.Windows.Forms; -namespace Handbrake.Functions +namespace Handbrake { class x264Panel { - /// /// Reset all components to defaults and clears the x264 rtf box /// diff --git a/win/C#/frmReadDVD.cs b/win/C#/frmReadDVD.cs index ec9b318d1..47639341d 100644 --- a/win/C#/frmReadDVD.cs +++ b/win/C#/frmReadDVD.cs @@ -26,7 +26,7 @@ namespace Handbrake private Parsing.DVD thisDvd; private delegate void UpdateUIHandler(); Process hbproc; - Functions.Common hb_common_func = new Functions.Common(); + Functions.Main hb_common_func = new Functions.Main(); Functions.Encode process = new Functions.Encode(); public frmReadDVD(string inputFile, frmMain parent) @@ -49,6 +49,7 @@ namespace Handbrake MessageBox.Show("frmReadDVD.cs - startScan " + exc.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } + private void startProc(object state) { try @@ -112,7 +113,7 @@ namespace Handbrake // Now select the longest title if (thisDvd.Titles.Count != 0) - hb_common_func.selectLongestTitle(mainWindow); + mainWindow.drp_dvdtitle.SelectedItem = hb_common_func.selectLongestTitle(mainWindow.drp_dvdtitle); this.Close(); } @@ -122,6 +123,7 @@ namespace Handbrake this.Close(); } } + private void closeWindowAfterError() { try