From 6c9b4d2c4cf5928bb2b472beca76c2edc4c41f03 Mon Sep 17 00:00:00 2001 From: sr55 Date: Thu, 15 Jul 2010 20:15:08 +0000 Subject: WinGui: - Multiple instance support. Each instance has a unique id. id = 0 will use the standard log/queue files. id = 1,2...n will use standard file names but with the instance id added to the end. This means that each instance will have it's own queue. Queue is not shared between Instances If multiple queue files are found when starting up, it will offer to load them all in. git-svn-id: svn://svn.handbrake.fr/HandBrake/trunk@3440 b64f7644-9d1e-0410-96f1-a4d463321fa5 --- win/C#/Functions/Main.cs | 80 +++++++--------------- win/C#/HandBrake.ApplicationServices/Init.cs | 12 ++-- .../Services/Encode.cs | 66 +++++------------- .../Services/Queue.cs | 37 ++++++---- .../HandBrake.ApplicationServices/Services/Scan.cs | 6 +- win/C#/Program.cs | 10 ++- win/C#/frmMain.cs | 36 +++++++--- win/C#/frmOptions.cs | 2 +- 8 files changed, 113 insertions(+), 136 deletions(-) (limited to 'win/C#') diff --git a/win/C#/Functions/Main.cs b/win/C#/Functions/Main.cs index 2bf6e6f5c..15714ee75 100644 --- a/win/C#/Functions/Main.cs +++ b/win/C#/Functions/Main.cs @@ -372,77 +372,51 @@ namespace Handbrake.Functions /// /// True if there is a queue to recover. /// - public static bool CheckQueueRecovery() + public static List CheckQueueRecovery() { try { - string tempPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"HandBrake\hb_queue_recovery.xml"); - if (File.Exists(tempPath)) + string tempPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"HandBrake\"); + List queueFiles = new List(); + + DirectoryInfo info = new DirectoryInfo(tempPath); + FileInfo[] logFiles = info.GetFiles("*.xml"); + foreach (FileInfo file in logFiles) { - using (FileStream strm = new FileStream(tempPath, FileMode.Open, FileAccess.Read)) + if (!file.Name.Contains("hb_queue_recovery")) + continue; + + using (FileStream strm = new FileStream(Path.Combine(file.DirectoryName, file.Name), FileMode.Open, FileAccess.Read)) { List list = Ser.Deserialize(strm) as List; if (list != null) + { if (list.Count != 0) - return true; + { + queueFiles.Add(file.Name); + } + } } } - return false; + + return queueFiles; } catch (Exception) { - return false; // Keep quiet about the error. + return new List(); // Keep quiet about the error. } } /// - /// Get the Process ID of HandBrakeCLI for the current instance. + /// Checks if this HandBrake is running multiple instances /// - /// List of processes before the new process was started - /// Int - Process ID - public static int GetCliProcess(Process[] before) + /// True if the UI has another instance running + public static bool IsMultiInstance { - // This is a bit of a cludge. Maybe someone has a better idea on how to impliment this. - // Since we used CMD to start HandBrakeCLI, we don't get the process ID from hbProc. - // Instead we take the processes before and after, and get the ID of HandBrakeCLI.exe - // avoiding any previous instances of HandBrakeCLI.exe in before. - // Kill the current process. - - DateTime startTime = DateTime.Now; - TimeSpan duration; - - Process[] hbProcesses = Process.GetProcessesByName("HandBrakeCLI"); - while (hbProcesses.Length == 0) + get { - hbProcesses = Process.GetProcessesByName("HandBrakeCLI"); - duration = DateTime.Now - startTime; - if (duration.Seconds > 5 && hbProcesses.Length == 0) - // Make sure we don't wait forever if the process doesn't start - return -1; + return Process.GetProcessesByName("HandBrake").Length > 0 ? true : false; } - - Process hbProcess = null; - foreach (Process process in hbProcesses) - { - bool found = false; - // Check if the current CLI instance was running before we started the current one - foreach (Process bprocess in before) - { - if (process.Id == bprocess.Id) - found = true; - } - - // If it wasn't running before, we found the process we want. - if (!found) - { - hbProcess = process; - break; - } - } - if (hbProcess != null) - return hbProcess.Id; - - return -1; } /// @@ -457,8 +431,7 @@ namespace Handbrake.Functions FileInfo[] logFiles = info.GetFiles("*.txt"); foreach (FileInfo file in logFiles) { - if (!file.Name.Contains("last_scan_log") && !file.Name.Contains("last_encode_log") && - !file.Name.Contains("tmp_appReadable_log.txt")) + if (!file.Name.Contains("last_scan_log") && !file.Name.Contains("last_encode_log")) File.Delete(file.FullName); } } @@ -479,8 +452,7 @@ namespace Handbrake.Functions { if (file.LastWriteTime < DateTime.Now.AddDays(-30)) { - if (!file.Name.Contains("last_scan_log") && !file.Name.Contains("last_encode_log") && - !file.Name.Contains("tmp_appReadable_log.txt")) + if (!file.Name.Contains("last_scan_log.txt") && !file.Name.Contains("last_encode_log.txt")) File.Delete(file.FullName); } } diff --git a/win/C#/HandBrake.ApplicationServices/Init.cs b/win/C#/HandBrake.ApplicationServices/Init.cs index c8f15c3fd..7f1e1ed72 100644 --- a/win/C#/HandBrake.ApplicationServices/Init.cs +++ b/win/C#/HandBrake.ApplicationServices/Init.cs @@ -16,8 +16,8 @@ namespace HandBrake.ApplicationServices /// /// Setup the Settings used by the applicaiton with this library /// - /// - /// The cli_minimized. + /// + /// The Instance ID /// /// /// The completion option. @@ -25,9 +25,6 @@ namespace HandBrake.ApplicationServices /// /// The disable dvd nav. /// - /// - /// The enocde status in gui. - /// /// /// The growl encode. /// @@ -52,10 +49,11 @@ namespace HandBrake.ApplicationServices /// /// Prevent the system from sleeping /// - public static void SetupSettings(string completionOption, bool disableDvdNav, + public static void SetupSettings(int instanceId, string completionOption, bool disableDvdNav, bool growlEncode, bool growlQueue, string processPriority, string saveLogPath, bool saveLogToSpecifiedPath, bool saveLogWithVideo, bool showCliForInGuiEncodeStatus, bool preventSleep) { + InstanceId = instanceId; Properties.Settings.Default.CompletionOption = completionOption; Properties.Settings.Default.disableDvdNav = disableDvdNav; Properties.Settings.Default.growlEncode = growlEncode; @@ -80,5 +78,7 @@ namespace HandBrake.ApplicationServices { return Assembly.GetExecutingAssembly().GetName().Version; } + + public static int InstanceId; } } diff --git a/win/C#/HandBrake.ApplicationServices/Services/Encode.cs b/win/C#/HandBrake.ApplicationServices/Services/Encode.cs index e5dc74cec..f0a2b1fe4 100644 --- a/win/C#/HandBrake.ApplicationServices/Services/Encode.cs +++ b/win/C#/HandBrake.ApplicationServices/Services/Encode.cs @@ -159,13 +159,13 @@ namespace HandBrake.ApplicationServices.Services string handbrakeCLIPath = Path.Combine(Application.StartupPath, "HandBrakeCLI.exe"); ProcessStartInfo cliStart = new ProcessStartInfo(handbrakeCLIPath, encJob.Query) - { + { RedirectStandardOutput = true, RedirectStandardError = enableLogging ? true : false, UseShellExecute = false, CreateNoWindow = !Settings.Default.showCliForInGuiEncodeStatus ? true : false }; - + this.HbProcess = Process.Start(cliStart); if (enableLogging) @@ -213,8 +213,8 @@ namespace HandBrake.ApplicationServices.Services } catch (Exception exc) { - Main.ShowExceptiowWindow("It would appear that HandBrakeCLI has not started correctly."+ - "You should take a look at the Activity log as it may indicate the reason why.\n\nDetailed Error Information: error occured in runCli()", + Main.ShowExceptiowWindow("It would appear that HandBrakeCLI has not started correctly." + + "You should take a look at the Activity log as it may indicate the reason why.\n\nDetailed Error Information: error occured in runCli()", exc.ToString()); } } @@ -227,11 +227,7 @@ namespace HandBrake.ApplicationServices.Services try { if (this.HbProcess != null) this.HbProcess.Kill(); - - Process[] list = Process.GetProcessesByName("HandBrakeCLI"); - foreach (Process process in list) - process.Kill(); - } + } catch (Exception exc) { Main.ShowExceptiowWindow("Unable to stop HandBrakeCLI. It may not be running.", exc.ToString()); @@ -272,36 +268,12 @@ namespace HandBrake.ApplicationServices.Services /// private static string CreateCliLogHeader(Job encJob) { - try - { - //string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + - // "\\HandBrake\\logs"; - //string logPath = Path.Combine(logDir, "last_encode_log.txt"); - - //var reader = new StreamReader(File.Open(logPath, FileMode.Open, FileAccess.Read, FileShare.Read)); - //string log = reader.ReadToEnd(); - //reader.Close(); - - //var writer = new StreamWriter(File.Create(logPath)); - + StringBuilder logHeader = new StringBuilder(); + logHeader.AppendLine("# CLI Query: " + encJob.Query); + logHeader.AppendLine("# User Query: " + encJob.CustomQuery); + logHeader.AppendLine("-------------------------------------------"); - //writer.WriteLine("### CLI Query: " + encJob.Query); - //writer.WriteLine("### User Query: " + encJob.CustomQuery); - //writer.WriteLine("#########################################"); - //writer.WriteLine(log); - //writer.Flush(); - //writer.Close(); - StringBuilder logHeader = new StringBuilder(); - logHeader.AppendLine("### CLI Query: " + encJob.Query); - logHeader.AppendLine("### User Query: " + encJob.CustomQuery); - logHeader.AppendLine("#########################################"); - - return logHeader.ToString(); - } - catch (Exception) - { - return string.Empty; - } + return logHeader.ToString(); } /// @@ -317,7 +289,7 @@ namespace HandBrake.ApplicationServices.Services { string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs"; - string tempLogFile = Path.Combine(logDir, "last_encode_log.txt"); + string tempLogFile = Path.Combine(logDir, string.Format("last_encode_log{0}.txt", Init.InstanceId)); string encodeDestinationPath = Path.GetDirectoryName(destination); string destinationFile = Path.GetFileName(destination); @@ -378,8 +350,8 @@ namespace HandBrake.ApplicationServices.Services { if (fileWriter != null) fileWriter.Close(); - } - catch(Exception exc) + } + catch (Exception exc) { Main.ShowExceptiowWindow("Unable to close the log file wrtier", exc.ToString()); } @@ -399,8 +371,8 @@ namespace HandBrake.ApplicationServices.Services // last_encode_log.txt is the primary log file. Since .NET can't read this file whilst the CLI is outputing to it (Not even in read only mode), // we'll need to make a copy of it. string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs"; - string logFile = Path.Combine(logDir, "last_encode_log.txt"); - string logFile2 = Path.Combine(logDir, "tmp_appReadable_log.txt"); + string logFile = Path.Combine(logDir, string.Format("last_encode_log{0}.txt", Init.InstanceId)); + string logFile2 = Path.Combine(logDir, string.Format("tmp_appReadable_log{0}.txt", Init.InstanceId)); int logFilePosition = 0; try @@ -414,7 +386,7 @@ namespace HandBrake.ApplicationServices.Services File.Copy(logFile, logFile2, true); else return; - + // Start the Reader // Only use text which continues on from the last read line StreamReader sr = new StreamReader(logFile2); @@ -445,8 +417,8 @@ namespace HandBrake.ApplicationServices.Services private void SetupLogging(Job encodeJob) { string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs"; - string logFile = Path.Combine(logDir, "last_encode_log.txt"); - string logFile2 = Path.Combine(logDir, "tmp_appReadable_log.txt"); + string logFile = Path.Combine(logDir, string.Format("last_encode_log{0}.txt", Init.InstanceId)); + string logFile2 = Path.Combine(logDir, string.Format("tmp_appReadable_log{0}.txt", Init.InstanceId)); try { @@ -493,7 +465,7 @@ namespace HandBrake.ApplicationServices.Services catch (Exception exc) { Main.ShowExceptiowWindow("Unable to write log data...", exc.ToString()); - } + } } } diff --git a/win/C#/HandBrake.ApplicationServices/Services/Queue.cs b/win/C#/HandBrake.ApplicationServices/Services/Queue.cs index da32d2503..7ba1d9d31 100644 --- a/win/C#/HandBrake.ApplicationServices/Services/Queue.cs +++ b/win/C#/HandBrake.ApplicationServices/Services/Queue.cs @@ -104,7 +104,7 @@ namespace HandBrake.ApplicationServices.Services this.LastEncode = job; this.Remove(0); // Remove the item which we are about to pass out. - this.WriteQueueStateToFile("hb_queue_recovery.xml"); + this.SaveQueue(); return job; } @@ -140,7 +140,7 @@ namespace HandBrake.ApplicationServices.Services }; this.queue.Add(newJob); - this.WriteQueueStateToFile("hb_queue_recovery.xml"); + this.SaveQueue(); if (this.QueueListChanged != null) this.QueueListChanged(this, new EventArgs()); @@ -153,7 +153,7 @@ namespace HandBrake.ApplicationServices.Services public void Remove(int index) { this.queue.RemoveAt(index); - this.WriteQueueStateToFile("hb_queue_recovery.xml"); + this.SaveQueue(); if (this.QueueListChanged != null) this.QueueListChanged(this, new EventArgs()); @@ -186,7 +186,7 @@ namespace HandBrake.ApplicationServices.Services queue.Insert((index - 1), item); } - WriteQueueStateToFile("hb_queue_recovery.xml"); // Update the queue recovery file + this.SaveQueue(); // Update the queue recovery file if (this.QueueListChanged != null) this.QueueListChanged(this, new EventArgs()); @@ -206,11 +206,22 @@ namespace HandBrake.ApplicationServices.Services this.queue.Insert((index + 1), item); } - this.WriteQueueStateToFile("hb_queue_recovery.xml"); // Update the queue recovery file + this.SaveQueue(); // Update the queue recovery file if (this.QueueListChanged != null) this.QueueListChanged(this, new EventArgs()); } + + /// + /// Save any updates to the main queue file. + /// + public void SaveQueue() + { + string file = Init.InstanceId == 0 + ? "hb_queue_recovery.xml" + : string.Format("hb_queue_recovery{0}.xml", Init.InstanceId); + this.WriteQueueStateToFile(file); + } /// /// Writes the current state of the queue to a file. @@ -218,9 +229,8 @@ namespace HandBrake.ApplicationServices.Services /// The location of the file to write the queue to. public void WriteQueueStateToFile(string file) { - string appDataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), - @"HandBrake\hb_queue_recovery.xml"); - string tempPath = file == "hb_queue_recovery.xml" ? appDataPath : file; + string appDataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"HandBrake\"); + string tempPath = file.Contains("hb_queue_recovery") ? appDataPath + file : file; try { @@ -285,9 +295,8 @@ namespace HandBrake.ApplicationServices.Services /// The location of the file to read the queue from. public void LoadQueueFromFile(string file) { - string appDataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), - @"HandBrake\hb_queue_recovery.xml"); - string tempPath = file == "hb_queue_recovery.xml" ? appDataPath : file; + string appDataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"HandBrake\"); + string tempPath = file.Contains("hb_queue_recovery") ? appDataPath + file : file; if (File.Exists(tempPath)) { @@ -304,8 +313,8 @@ namespace HandBrake.ApplicationServices.Services foreach (Job item in list) this.queue.Add(item); - if (file != "hb_queue_recovery.xml") - this.WriteQueueStateToFile("hb_queue_recovery.xml"); + if (!file.Contains("hb_queue_recovery")) + this.SaveQueue(); if (this.QueueListChanged != null) this.QueueListChanged(this, new EventArgs()); @@ -378,7 +387,7 @@ namespace HandBrake.ApplicationServices.Services while (this.Count != 0) { Job encJob = this.GetNextJob(); - this.WriteQueueStateToFile("hb_queue_recovery.xml"); // Update the queue recovery file + this.SaveQueue(); // Update the queue recovery file Run(encJob, true); diff --git a/win/C#/HandBrake.ApplicationServices/Services/Scan.cs b/win/C#/HandBrake.ApplicationServices/Services/Scan.cs index 6aefa03fd..039cb3f3d 100644 --- a/win/C#/HandBrake.ApplicationServices/Services/Scan.cs +++ b/win/C#/HandBrake.ApplicationServices/Services/Scan.cs @@ -154,7 +154,7 @@ namespace HandBrake.ApplicationServices.Services string handbrakeCLIPath = Path.Combine(Application.StartupPath, "HandBrakeCLI.exe"); string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs"; - string dvdInfoPath = Path.Combine(logDir, "last_scan_log.txt"); + string dvdInfoPath = Path.Combine(logDir, string.Format("last_scan_log{0}.txt", Init.InstanceId)); // Make we don't pick up a stale last_encode_log.txt (and that we have rights to the file) if (File.Exists(dvdInfoPath)) @@ -218,8 +218,8 @@ namespace HandBrake.ApplicationServices.Services // we'll need to make a copy of it. string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs"; - string logFile = Path.Combine(logDir, "last_scan_log.txt"); - string logFile2 = Path.Combine(logDir, "tmp_appReadable_log.txt"); + string logFile = Path.Combine(logDir, string.Format("last_scan_log{0}.txt", Init.InstanceId)); + string logFile2 = Path.Combine(logDir, string.Format("tmp_appReadable_log{0}.txt", Init.InstanceId)); try { diff --git a/win/C#/Program.cs b/win/C#/Program.cs index 6480b50b1..f54a775f9 100644 --- a/win/C#/Program.cs +++ b/win/C#/Program.cs @@ -6,6 +6,7 @@ namespace Handbrake { using System; + using System.Diagnostics; using System.IO; using System.Windows.Forms; @@ -26,6 +27,8 @@ namespace Handbrake [STAThread] public static void Main(string[] args) { + InstanceId = Process.GetProcessesByName("HandBrake").Length; + // Handle any unhandled exceptions AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException); @@ -88,7 +91,7 @@ namespace Handbrake /// private static void InitializeApplicationServices() { - Init.SetupSettings(Settings.Default.CompletionOption, Settings.Default.noDvdNav, + Init.SetupSettings(InstanceId, Settings.Default.CompletionOption, Settings.Default.noDvdNav, Settings.Default.growlEncode, Settings.Default.growlQueue, Settings.Default.processPriority, Settings.Default.saveLogPath, Settings.Default.saveLogToSpecifiedPath, Settings.Default.saveLogWithVideo, Settings.Default.showCliForInGuiEncodeStatus, Settings.Default.preventSleep); @@ -116,5 +119,10 @@ namespace Handbrake MessageBoxIcon.Error); } } + + + public static int InstanceId = 0; + + } } \ No newline at end of file diff --git a/win/C#/frmMain.cs b/win/C#/frmMain.cs index 7f46139a2..91ed40423 100644 --- a/win/C#/frmMain.cs +++ b/win/C#/frmMain.cs @@ -211,21 +211,37 @@ namespace Handbrake // Startup Functions private void queueRecovery() { - if (Main.CheckQueueRecovery()) + DialogResult result = DialogResult.None; + List queueFiles = Main.CheckQueueRecovery(); + if (queueFiles.Count == 1) { - DialogResult result = - MessageBox.Show( + result = MessageBox.Show( "HandBrake has detected unfinished items on the queue from the last time the application was launched. Would you like to recover these?", "Queue Recovery Possible", MessageBoxButtons.YesNo, MessageBoxIcon.Question); + } + else if (queueFiles.Count > 1) + { + result = MessageBox.Show( + "HandBrake has detected multiple unfinished queue files. These will be from multiple instances of HandBrake running. Would you like to recover all unfinished jobs?", + "Queue Recovery Possible", MessageBoxButtons.YesNo, MessageBoxIcon.Question); + } - if (result == DialogResult.Yes) - encodeQueue.LoadQueueFromFile("hb_queue_recovery.xml"); // Start Recovery - else + if (result == DialogResult.Yes) + { + foreach (string file in queueFiles) + { + encodeQueue.LoadQueueFromFile(file); // Start Recovery + } + } + else + { + if (Main.IsMultiInstance) return; // Don't tamper with the files if we are multi instance + + string tempPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"HandBrake\"); + foreach (string file in queueFiles) { - // Remove the Queue recovery file if the user doesn't want to recovery the last queue. - string queuePath = Path.Combine(Path.GetTempPath(), "hb_queue_recovery.xml"); - if (File.Exists(queuePath)) - File.Delete(queuePath); + if (File.Exists(Path.Combine(tempPath, file))) + File.Delete(Path.Combine(tempPath, file)); } } } diff --git a/win/C#/frmOptions.cs b/win/C#/frmOptions.cs index 492ee3577..7ca71dc30 100644 --- a/win/C#/frmOptions.cs +++ b/win/C#/frmOptions.cs @@ -473,7 +473,7 @@ namespace Handbrake /// private static void UpdateApplicationServicesSettings() { - Init.SetupSettings(Settings.Default.CompletionOption, Settings.Default.noDvdNav, + Init.SetupSettings(Program.InstanceId, Settings.Default.CompletionOption, Settings.Default.noDvdNav, Settings.Default.growlEncode, Settings.Default.growlQueue, Settings.Default.processPriority, Settings.Default.saveLogPath, Settings.Default.saveLogToSpecifiedPath, Settings.Default.saveLogWithVideo, Settings.Default.showCliForInGuiEncodeStatus, Settings.Default.preventSleep); -- cgit v1.2.3