/// <returns>\r
/// True if there is a queue to recover.\r
/// </returns>\r
- public static bool CheckQueueRecovery()\r
+ public static List<string> CheckQueueRecovery()\r
{\r
try\r
{\r
- string tempPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"HandBrake\hb_queue_recovery.xml");\r
- if (File.Exists(tempPath))\r
+ string tempPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"HandBrake\");\r
+ List<string> queueFiles = new List<string>();\r
+\r
+ DirectoryInfo info = new DirectoryInfo(tempPath);\r
+ FileInfo[] logFiles = info.GetFiles("*.xml");\r
+ foreach (FileInfo file in logFiles)\r
{\r
- using (FileStream strm = new FileStream(tempPath, FileMode.Open, FileAccess.Read))\r
+ if (!file.Name.Contains("hb_queue_recovery"))\r
+ continue;\r
+\r
+ using (FileStream strm = new FileStream(Path.Combine(file.DirectoryName, file.Name), FileMode.Open, FileAccess.Read))\r
{\r
List<Job> list = Ser.Deserialize(strm) as List<Job>;\r
if (list != null)\r
+ {\r
if (list.Count != 0)\r
- return true;\r
+ {\r
+ queueFiles.Add(file.Name);\r
+ }\r
+ }\r
}\r
}\r
- return false;\r
+\r
+ return queueFiles;\r
}\r
catch (Exception)\r
{\r
- return false; // Keep quiet about the error.\r
+ return new List<string>(); // Keep quiet about the error.\r
}\r
}\r
\r
/// <summary>\r
- /// Get the Process ID of HandBrakeCLI for the current instance.\r
+ /// Checks if this HandBrake is running multiple instances\r
/// </summary>\r
- /// <param name="before">List of processes before the new process was started</param>\r
- /// <returns>Int - Process ID</returns>\r
- public static int GetCliProcess(Process[] before)\r
+ /// <returns>True if the UI has another instance running</returns>\r
+ public static bool IsMultiInstance\r
{\r
- // This is a bit of a cludge. Maybe someone has a better idea on how to impliment this.\r
- // Since we used CMD to start HandBrakeCLI, we don't get the process ID from hbProc.\r
- // Instead we take the processes before and after, and get the ID of HandBrakeCLI.exe\r
- // avoiding any previous instances of HandBrakeCLI.exe in before.\r
- // Kill the current process.\r
-\r
- DateTime startTime = DateTime.Now;\r
- TimeSpan duration;\r
-\r
- Process[] hbProcesses = Process.GetProcessesByName("HandBrakeCLI");\r
- while (hbProcesses.Length == 0)\r
+ get\r
{\r
- hbProcesses = Process.GetProcessesByName("HandBrakeCLI");\r
- duration = DateTime.Now - startTime;\r
- if (duration.Seconds > 5 && hbProcesses.Length == 0)\r
- // Make sure we don't wait forever if the process doesn't start\r
- return -1;\r
+ return Process.GetProcessesByName("HandBrake").Length > 0 ? true : false;\r
}\r
-\r
- Process hbProcess = null;\r
- foreach (Process process in hbProcesses)\r
- {\r
- bool found = false;\r
- // Check if the current CLI instance was running before we started the current one\r
- foreach (Process bprocess in before)\r
- {\r
- if (process.Id == bprocess.Id)\r
- found = true;\r
- }\r
-\r
- // If it wasn't running before, we found the process we want.\r
- if (!found)\r
- {\r
- hbProcess = process;\r
- break;\r
- }\r
- }\r
- if (hbProcess != null)\r
- return hbProcess.Id;\r
-\r
- return -1;\r
}\r
\r
/// <summary>\r
FileInfo[] logFiles = info.GetFiles("*.txt");\r
foreach (FileInfo file in logFiles)\r
{\r
- if (!file.Name.Contains("last_scan_log") && !file.Name.Contains("last_encode_log") &&\r
- !file.Name.Contains("tmp_appReadable_log.txt"))\r
+ if (!file.Name.Contains("last_scan_log") && !file.Name.Contains("last_encode_log"))\r
File.Delete(file.FullName);\r
}\r
}\r
{\r
if (file.LastWriteTime < DateTime.Now.AddDays(-30))\r
{\r
- if (!file.Name.Contains("last_scan_log") && !file.Name.Contains("last_encode_log") &&\r
- !file.Name.Contains("tmp_appReadable_log.txt"))\r
+ if (!file.Name.Contains("last_scan_log.txt") && !file.Name.Contains("last_encode_log.txt"))\r
File.Delete(file.FullName);\r
}\r
}\r
/// <summary>\r
/// Setup the Settings used by the applicaiton with this library\r
/// </summary>\r
- /// <param name="cli_minimized">\r
- /// The cli_minimized.\r
+ /// <param name="instanceId">\r
+ /// The Instance ID\r
/// </param>\r
/// <param name="completionOption">\r
/// The completion option.\r
/// <param name="disableDvdNav">\r
/// The disable dvd nav.\r
/// </param>\r
- /// <param name="enocdeStatusInGui">\r
- /// The enocde status in gui.\r
- /// </param>\r
/// <param name="growlEncode">\r
/// The growl encode.\r
/// </param>\r
/// <param name="preventSleep">\r
/// Prevent the system from sleeping\r
/// </param>\r
- public static void SetupSettings(string completionOption, bool disableDvdNav,\r
+ public static void SetupSettings(int instanceId, string completionOption, bool disableDvdNav,\r
bool growlEncode, bool growlQueue, string processPriority, string saveLogPath, bool saveLogToSpecifiedPath,\r
bool saveLogWithVideo, bool showCliForInGuiEncodeStatus, bool preventSleep)\r
{\r
+ InstanceId = instanceId;\r
Properties.Settings.Default.CompletionOption = completionOption;\r
Properties.Settings.Default.disableDvdNav = disableDvdNav;\r
Properties.Settings.Default.growlEncode = growlEncode;\r
{\r
return Assembly.GetExecutingAssembly().GetName().Version;\r
}\r
+\r
+ public static int InstanceId;\r
}\r
}\r
\r
string handbrakeCLIPath = Path.Combine(Application.StartupPath, "HandBrakeCLI.exe");\r
ProcessStartInfo cliStart = new ProcessStartInfo(handbrakeCLIPath, encJob.Query)\r
- { \r
+ {\r
RedirectStandardOutput = true,\r
RedirectStandardError = enableLogging ? true : false,\r
UseShellExecute = false,\r
CreateNoWindow = !Settings.Default.showCliForInGuiEncodeStatus ? true : false\r
};\r
- \r
+\r
this.HbProcess = Process.Start(cliStart);\r
\r
if (enableLogging)\r
}\r
catch (Exception exc)\r
{\r
- Main.ShowExceptiowWindow("It would appear that HandBrakeCLI has not started correctly."+ \r
- "You should take a look at the Activity log as it may indicate the reason why.\n\nDetailed Error Information: error occured in runCli()", \r
+ Main.ShowExceptiowWindow("It would appear that HandBrakeCLI has not started correctly." +\r
+ "You should take a look at the Activity log as it may indicate the reason why.\n\nDetailed Error Information: error occured in runCli()",\r
exc.ToString());\r
}\r
}\r
try\r
{\r
if (this.HbProcess != null) this.HbProcess.Kill();\r
-\r
- Process[] list = Process.GetProcessesByName("HandBrakeCLI");\r
- foreach (Process process in list)\r
- process.Kill();\r
- } \r
+ }\r
catch (Exception exc)\r
{\r
Main.ShowExceptiowWindow("Unable to stop HandBrakeCLI. It may not be running.", exc.ToString());\r
/// </param>\r
private static string CreateCliLogHeader(Job encJob)\r
{\r
- try\r
- {\r
- //string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) +\r
- // "\\HandBrake\\logs";\r
- //string logPath = Path.Combine(logDir, "last_encode_log.txt");\r
-\r
- //var reader = new StreamReader(File.Open(logPath, FileMode.Open, FileAccess.Read, FileShare.Read));\r
- //string log = reader.ReadToEnd();\r
- //reader.Close();\r
-\r
- //var writer = new StreamWriter(File.Create(logPath));\r
-\r
+ StringBuilder logHeader = new StringBuilder();\r
+ logHeader.AppendLine("# CLI Query: " + encJob.Query);\r
+ logHeader.AppendLine("# User Query: " + encJob.CustomQuery);\r
+ logHeader.AppendLine("-------------------------------------------");\r
\r
- //writer.WriteLine("### CLI Query: " + encJob.Query);\r
- //writer.WriteLine("### User Query: " + encJob.CustomQuery);\r
- //writer.WriteLine("#########################################");\r
- //writer.WriteLine(log);\r
- //writer.Flush();\r
- //writer.Close();\r
- StringBuilder logHeader = new StringBuilder();\r
- logHeader.AppendLine("### CLI Query: " + encJob.Query);\r
- logHeader.AppendLine("### User Query: " + encJob.CustomQuery);\r
- logHeader.AppendLine("#########################################");\r
-\r
- return logHeader.ToString();\r
- }\r
- catch (Exception)\r
- {\r
- return string.Empty;\r
- }\r
+ return logHeader.ToString();\r
}\r
\r
/// <summary>\r
{\r
string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) +\r
"\\HandBrake\\logs";\r
- string tempLogFile = Path.Combine(logDir, "last_encode_log.txt");\r
+ string tempLogFile = Path.Combine(logDir, string.Format("last_encode_log{0}.txt", Init.InstanceId));\r
\r
string encodeDestinationPath = Path.GetDirectoryName(destination);\r
string destinationFile = Path.GetFileName(destination);\r
{\r
if (fileWriter != null)\r
fileWriter.Close();\r
- } \r
- catch(Exception exc)\r
+ }\r
+ catch (Exception exc)\r
{\r
Main.ShowExceptiowWindow("Unable to close the log file wrtier", exc.ToString());\r
}\r
// 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),\r
// we'll need to make a copy of it.\r
string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs";\r
- string logFile = Path.Combine(logDir, "last_encode_log.txt");\r
- string logFile2 = Path.Combine(logDir, "tmp_appReadable_log.txt");\r
+ string logFile = Path.Combine(logDir, string.Format("last_encode_log{0}.txt", Init.InstanceId));\r
+ string logFile2 = Path.Combine(logDir, string.Format("tmp_appReadable_log{0}.txt", Init.InstanceId));\r
int logFilePosition = 0;\r
\r
try\r
File.Copy(logFile, logFile2, true);\r
else\r
return;\r
- \r
+\r
// Start the Reader\r
// Only use text which continues on from the last read line\r
StreamReader sr = new StreamReader(logFile2);\r
private void SetupLogging(Job encodeJob)\r
{\r
string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs";\r
- string logFile = Path.Combine(logDir, "last_encode_log.txt");\r
- string logFile2 = Path.Combine(logDir, "tmp_appReadable_log.txt");\r
+ string logFile = Path.Combine(logDir, string.Format("last_encode_log{0}.txt", Init.InstanceId));\r
+ string logFile2 = Path.Combine(logDir, string.Format("tmp_appReadable_log{0}.txt", Init.InstanceId));\r
\r
try\r
{\r
catch (Exception exc)\r
{\r
Main.ShowExceptiowWindow("Unable to write log data...", exc.ToString());\r
- } \r
+ }\r
}\r
}\r
\r
this.LastEncode = job;\r
this.Remove(0); // Remove the item which we are about to pass out.\r
\r
- this.WriteQueueStateToFile("hb_queue_recovery.xml");\r
+ this.SaveQueue();\r
\r
return job;\r
}\r
};\r
\r
this.queue.Add(newJob);\r
- this.WriteQueueStateToFile("hb_queue_recovery.xml");\r
+ this.SaveQueue();\r
\r
if (this.QueueListChanged != null)\r
this.QueueListChanged(this, new EventArgs());\r
public void Remove(int index)\r
{\r
this.queue.RemoveAt(index);\r
- this.WriteQueueStateToFile("hb_queue_recovery.xml");\r
+ this.SaveQueue();\r
\r
if (this.QueueListChanged != null)\r
this.QueueListChanged(this, new EventArgs());\r
queue.Insert((index - 1), item);\r
}\r
\r
- WriteQueueStateToFile("hb_queue_recovery.xml"); // Update the queue recovery file\r
+ this.SaveQueue(); // Update the queue recovery file\r
\r
if (this.QueueListChanged != null)\r
this.QueueListChanged(this, new EventArgs());\r
this.queue.Insert((index + 1), item);\r
}\r
\r
- this.WriteQueueStateToFile("hb_queue_recovery.xml"); // Update the queue recovery file\r
+ this.SaveQueue(); // Update the queue recovery file\r
\r
if (this.QueueListChanged != null)\r
this.QueueListChanged(this, new EventArgs());\r
}\r
+ \r
+ /// <summary>\r
+ /// Save any updates to the main queue file.\r
+ /// </summary>\r
+ public void SaveQueue()\r
+ {\r
+ string file = Init.InstanceId == 0\r
+ ? "hb_queue_recovery.xml"\r
+ : string.Format("hb_queue_recovery{0}.xml", Init.InstanceId);\r
+ this.WriteQueueStateToFile(file);\r
+ }\r
\r
/// <summary>\r
/// Writes the current state of the queue to a file.\r
/// <param name="file">The location of the file to write the queue to.</param>\r
public void WriteQueueStateToFile(string file)\r
{\r
- string appDataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),\r
- @"HandBrake\hb_queue_recovery.xml");\r
- string tempPath = file == "hb_queue_recovery.xml" ? appDataPath : file;\r
+ string appDataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"HandBrake\");\r
+ string tempPath = file.Contains("hb_queue_recovery") ? appDataPath + file : file;\r
\r
try\r
{\r
/// <param name="file">The location of the file to read the queue from.</param>\r
public void LoadQueueFromFile(string file)\r
{\r
- string appDataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),\r
- @"HandBrake\hb_queue_recovery.xml");\r
- string tempPath = file == "hb_queue_recovery.xml" ? appDataPath : file;\r
+ string appDataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"HandBrake\");\r
+ string tempPath = file.Contains("hb_queue_recovery") ? appDataPath + file : file;\r
\r
if (File.Exists(tempPath))\r
{\r
foreach (Job item in list)\r
this.queue.Add(item);\r
\r
- if (file != "hb_queue_recovery.xml")\r
- this.WriteQueueStateToFile("hb_queue_recovery.xml");\r
+ if (!file.Contains("hb_queue_recovery"))\r
+ this.SaveQueue();\r
\r
if (this.QueueListChanged != null)\r
this.QueueListChanged(this, new EventArgs());\r
while (this.Count != 0)\r
{\r
Job encJob = this.GetNextJob();\r
- this.WriteQueueStateToFile("hb_queue_recovery.xml"); // Update the queue recovery file\r
+ this.SaveQueue(); // Update the queue recovery file\r
\r
Run(encJob, true);\r
\r
string handbrakeCLIPath = Path.Combine(Application.StartupPath, "HandBrakeCLI.exe");\r
string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) +\r
"\\HandBrake\\logs";\r
- string dvdInfoPath = Path.Combine(logDir, "last_scan_log.txt");\r
+ string dvdInfoPath = Path.Combine(logDir, string.Format("last_scan_log{0}.txt", Init.InstanceId));\r
\r
// Make we don't pick up a stale last_encode_log.txt (and that we have rights to the file)\r
if (File.Exists(dvdInfoPath))\r
// we'll need to make a copy of it.\r
string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) +\r
"\\HandBrake\\logs";\r
- string logFile = Path.Combine(logDir, "last_scan_log.txt");\r
- string logFile2 = Path.Combine(logDir, "tmp_appReadable_log.txt");\r
+ string logFile = Path.Combine(logDir, string.Format("last_scan_log{0}.txt", Init.InstanceId));\r
+ string logFile2 = Path.Combine(logDir, string.Format("tmp_appReadable_log{0}.txt", Init.InstanceId));\r
\r
try\r
{\r
namespace Handbrake\r
{\r
using System;\r
+ using System.Diagnostics;\r
using System.IO;\r
using System.Windows.Forms;\r
\r
[STAThread]\r
public static void Main(string[] args)\r
{\r
+ InstanceId = Process.GetProcessesByName("HandBrake").Length;\r
+\r
// Handle any unhandled exceptions\r
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);\r
\r
/// </summary>\r
private static void InitializeApplicationServices()\r
{\r
- Init.SetupSettings(Settings.Default.CompletionOption, Settings.Default.noDvdNav,\r
+ Init.SetupSettings(InstanceId, Settings.Default.CompletionOption, Settings.Default.noDvdNav,\r
Settings.Default.growlEncode, Settings.Default.growlQueue,\r
Settings.Default.processPriority, Settings.Default.saveLogPath, Settings.Default.saveLogToSpecifiedPath,\r
Settings.Default.saveLogWithVideo, Settings.Default.showCliForInGuiEncodeStatus, Settings.Default.preventSleep);\r
MessageBoxIcon.Error);\r
}\r
}\r
+\r
+\r
+ public static int InstanceId = 0;\r
+\r
+\r
}\r
}
\ No newline at end of file
// Startup Functions \r
private void queueRecovery()\r
{\r
- if (Main.CheckQueueRecovery())\r
+ DialogResult result = DialogResult.None;\r
+ List<string> queueFiles = Main.CheckQueueRecovery();\r
+ if (queueFiles.Count == 1)\r
{\r
- DialogResult result =\r
- MessageBox.Show(\r
+ result = MessageBox.Show(\r
"HandBrake has detected unfinished items on the queue from the last time the application was launched. Would you like to recover these?",\r
"Queue Recovery Possible", MessageBoxButtons.YesNo, MessageBoxIcon.Question);\r
+ }\r
+ else if (queueFiles.Count > 1)\r
+ {\r
+ result = MessageBox.Show(\r
+ "HandBrake has detected multiple unfinished queue files. These will be from multiple instances of HandBrake running. Would you like to recover all unfinished jobs?",\r
+ "Queue Recovery Possible", MessageBoxButtons.YesNo, MessageBoxIcon.Question);\r
+ }\r
\r
- if (result == DialogResult.Yes)\r
- encodeQueue.LoadQueueFromFile("hb_queue_recovery.xml"); // Start Recovery\r
- else\r
+ if (result == DialogResult.Yes)\r
+ {\r
+ foreach (string file in queueFiles)\r
+ {\r
+ encodeQueue.LoadQueueFromFile(file); // Start Recovery\r
+ }\r
+ }\r
+ else\r
+ {\r
+ if (Main.IsMultiInstance) return; // Don't tamper with the files if we are multi instance\r
+\r
+ string tempPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"HandBrake\");\r
+ foreach (string file in queueFiles)\r
{\r
- // Remove the Queue recovery file if the user doesn't want to recovery the last queue.\r
- string queuePath = Path.Combine(Path.GetTempPath(), "hb_queue_recovery.xml");\r
- if (File.Exists(queuePath))\r
- File.Delete(queuePath);\r
+ if (File.Exists(Path.Combine(tempPath, file)))\r
+ File.Delete(Path.Combine(tempPath, file));\r
}\r
}\r
}\r
/// </summary>\r
private static void UpdateApplicationServicesSettings()\r
{\r
- Init.SetupSettings(Settings.Default.CompletionOption, Settings.Default.noDvdNav,\r
+ Init.SetupSettings(Program.InstanceId, Settings.Default.CompletionOption, Settings.Default.noDvdNav,\r
Settings.Default.growlEncode, Settings.Default.growlQueue,\r
Settings.Default.processPriority, Settings.Default.saveLogPath, Settings.Default.saveLogToSpecifiedPath,\r
Settings.Default.saveLogWithVideo, Settings.Default.showCliForInGuiEncodeStatus, Settings.Default.preventSleep);\r