summaryrefslogtreecommitdiffstats
path: root/win/C#/HandBrake.ApplicationServices/Services
diff options
context:
space:
mode:
Diffstat (limited to 'win/C#/HandBrake.ApplicationServices/Services')
-rw-r--r--win/C#/HandBrake.ApplicationServices/Services/Encode.cs39
-rw-r--r--win/C#/HandBrake.ApplicationServices/Services/Interfaces/IQueue.cs146
-rw-r--r--win/C#/HandBrake.ApplicationServices/Services/Queue.cs465
-rw-r--r--win/C#/HandBrake.ApplicationServices/Services/QueueManager.cs22
-rw-r--r--win/C#/HandBrake.ApplicationServices/Services/QueueProcessor.cs2
5 files changed, 27 insertions, 647 deletions
diff --git a/win/C#/HandBrake.ApplicationServices/Services/Encode.cs b/win/C#/HandBrake.ApplicationServices/Services/Encode.cs
index e2046da91..bc5220e1e 100644
--- a/win/C#/HandBrake.ApplicationServices/Services/Encode.cs
+++ b/win/C#/HandBrake.ApplicationServices/Services/Encode.cs
@@ -131,9 +131,9 @@ namespace HandBrake.ApplicationServices.Services
{
try
{
- QueueTask QueueTask = encodeQueueTask as QueueTask;
+ QueueTask queueTask = encodeQueueTask;
- if (QueueTask == null)
+ if (queueTask == null)
{
throw new ArgumentNullException("QueueTask was null");
}
@@ -147,14 +147,14 @@ namespace HandBrake.ApplicationServices.Services
if (enableLogging)
{
- if (!SetupLogging(QueueTask))
+ try
{
- if (this.EncodeCompleted != null)
- this.EncodeCompleted(
- this,
- new EncodeCompletedEventArgs(false, null, "Unable to Start Encode Logging process."));
-
- return;
+ SetupLogging(queueTask);
+ }
+ catch (Exception)
+ {
+ IsEncoding = false;
+ throw;
}
}
@@ -164,7 +164,7 @@ namespace HandBrake.ApplicationServices.Services
}
string handbrakeCLIPath = Path.Combine(Application.StartupPath, "HandBrakeCLI.exe");
- ProcessStartInfo cliStart = new ProcessStartInfo(handbrakeCLIPath, QueueTask.Query)
+ ProcessStartInfo cliStart = new ProcessStartInfo(handbrakeCLIPath, queueTask.Query)
{
RedirectStandardOutput = true,
RedirectStandardError = enableLogging ? true : false,
@@ -346,9 +346,7 @@ namespace HandBrake.ApplicationServices.Services
private void HbProcessExited(object sender, EventArgs e)
{
IsEncoding = false;
- if (this.EncodeCompleted != null)
- this.EncodeCompleted(this, new EncodeCompletedEventArgs(true, null, string.Empty));
-
+
if (windowsSeven.IsWindowsSeven)
{
windowsSeven.SetTaskBarProgressToNoProgress();
@@ -371,6 +369,9 @@ namespace HandBrake.ApplicationServices.Services
{
// This exception doesn't warrent user interaction, but it should be logged (TODO)
}
+
+ if (this.EncodeCompleted != null)
+ this.EncodeCompleted(this, new EncodeCompletedEventArgs(true, null, string.Empty));
}
/// <summary>
@@ -429,10 +430,7 @@ namespace HandBrake.ApplicationServices.Services
/// <param name="encodeQueueTask">
/// The encode QueueTask.
/// </param>
- /// <returns>
- /// The setup logging.
- /// </returns>
- private bool SetupLogging(QueueTask encodeQueueTask)
+ private void SetupLogging(QueueTask encodeQueueTask)
{
string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs";
string logFile = Path.Combine(logDir, string.Format("last_encode_log{0}.txt", Init.InstanceId));
@@ -450,18 +448,15 @@ namespace HandBrake.ApplicationServices.Services
fileWriter.WriteLine(Logging.CreateCliLogHeader(encodeQueueTask));
logBuffer.AppendLine(Logging.CreateCliLogHeader(encodeQueueTask));
-
- return true;
}
- catch (Exception exc)
+ catch (Exception)
{
if (fileWriter != null)
{
fileWriter.Close();
fileWriter.Dispose();
}
-
- return false;
+ throw;
}
}
diff --git a/win/C#/HandBrake.ApplicationServices/Services/Interfaces/IQueue.cs b/win/C#/HandBrake.ApplicationServices/Services/Interfaces/IQueue.cs
deleted file mode 100644
index ed649fd39..000000000
--- a/win/C#/HandBrake.ApplicationServices/Services/Interfaces/IQueue.cs
+++ /dev/null
@@ -1,146 +0,0 @@
-/* IQueue.cs $
- This file is part of the HandBrake source code.
- Homepage: <http://handbrake.fr/>.
- It may be used under the terms of the GNU General Public License. */
-
-namespace HandBrake.ApplicationServices.Services.Interfaces
-{
- using System;
- using System.Collections.ObjectModel;
-
- using HandBrake.ApplicationServices.Model;
-
- /// <summary>
- /// The IQueue Interface
- /// </summary>
- public interface IQueue : IEncode
- {
- /// <summary>
- /// Fires when the Queue has started
- /// </summary>
- event EventHandler QueueStarted;
-
- /// <summary>
- /// Fires when a job is Added, Removed or Re-Ordered.
- /// Should be used for triggering an update of the Queue Window.
- /// </summary>
- event EventHandler QueueListChanged;
-
- /// <summary>
- /// Fires when a pause to the encode queue has been requested.
- /// </summary>
- event EventHandler QueuePauseRequested;
-
- /// <summary>
- /// Fires when the entire encode queue has completed.
- /// </summary>
- event EventHandler QueueCompleted;
-
- /// <summary>
- /// Gets or sets the last encode that was processed.
- /// </summary>
- /// <returns></returns>
- QueueTask LastEncode { get; set; }
-
- /// <summary>
- /// Gets a value indicating whether Request Pause
- /// </summary>
- bool Paused { get; }
-
- /// <summary>
- /// Gets the current state of the encode queue.
- /// </summary>
- ReadOnlyCollection<QueueTask> CurrentQueue { get; }
-
- /// <summary>
- /// Gets the number of items in the queue.
- /// </summary>
- int Count { get; }
-
- /// <summary>
- /// Adds an item to the queue.
- /// </summary>
- /// <param name="query">
- /// The query that will be passed to the HandBrake CLI.
- /// </param>
- /// <param name="title">
- /// The title.
- /// </param>
- /// <param name="source">
- /// The location of the source video.
- /// </param>
- /// <param name="destination">
- /// The location where the encoded video will be.
- /// </param>
- /// <param name="customJob">
- /// Custom job
- /// </param>
- void Add(string query, int title, string source, string destination, bool customJob);
-
- /// <summary>
- /// Removes an item from the queue.
- /// </summary>
- /// <param name="index">The zero-based location of the job in the queue.</param>
- void Remove(int index);
-
- /// <summary>
- /// Retrieve a job from the queue
- /// </summary>
- /// <param name="index">the job id</param>
- /// <returns>A job for the given index or blank job object</returns>
- QueueTask GetJob(int index);
-
- /// <summary>
- /// Moves an item up one position in the queue.
- /// </summary>
- /// <param name="index">The zero-based location of the job in the queue.</param>
- void MoveUp(int index);
-
- /// <summary>
- /// Moves an item down one position in the queue.
- /// </summary>
- /// <param name="index">The zero-based location of the job in the queue.</param>
- void MoveDown(int index);
-
- /// <summary>
- /// Writes the current state of the queue to a file.
- /// </summary>
- /// <param name="file">The location of the file to write the queue to.</param>
- void WriteQueueStateToFile(string file);
-
- /// <summary>
- /// Writes the current state of the queue in the form of a batch (.bat) file.
- /// </summary>
- /// <param name="file">
- /// The location of the file to write the batch file to.
- /// </param>
- /// <returns>
- /// The write batch script to file.
- /// </returns>
- bool WriteBatchScriptToFile(string file);
-
- /// <summary>
- /// Reads a serialized XML file that represents a queue of encoding jobs.
- /// </summary>
- /// <param name="file">The location of the file to read the queue from.</param>
- void LoadQueueFromFile(string file);
-
- /// <summary>
- /// Checks the current queue for an existing instance of the specified destination.
- /// </summary>
- /// <param name="destination">The destination of the encode.</param>
- /// <returns>Whether or not the supplied destination is already in the queue.</returns>
- bool CheckForDestinationDuplicate(string destination);
-
- /// <summary>
- /// Starts encoding the first job in the queue and continues encoding until all jobs
- /// have been encoded.
- /// </summary>
- void Start();
-
- /// <summary>
- /// Requests a pause of the encode queue.
- /// </summary>
- void Pause();
- }
-} \ No newline at end of file
diff --git a/win/C#/HandBrake.ApplicationServices/Services/Queue.cs b/win/C#/HandBrake.ApplicationServices/Services/Queue.cs
deleted file mode 100644
index 937e37a78..000000000
--- a/win/C#/HandBrake.ApplicationServices/Services/Queue.cs
+++ /dev/null
@@ -1,465 +0,0 @@
-/* Queue.cs $
- This file is part of the HandBrake source code.
- Homepage: <http://handbrake.fr/>.
- It may be used under the terms of the GNU General Public License. */
-
-namespace HandBrake.ApplicationServices.Services
-{
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- using System.Threading;
- using System.Windows.Forms;
- using System.Xml.Serialization;
-
- using HandBrake.ApplicationServices.Functions;
- using HandBrake.ApplicationServices.Model;
- using HandBrake.ApplicationServices.Services.Interfaces;
-
- /// <summary>
- /// The HandBrake Queue
- /// </summary>
- public class Queue : Encode, IQueue
- {
- /// <summary>
- /// The Queue Job List
- /// </summary>
- private readonly List<QueueTask> queue = new List<QueueTask>();
-
- /// <summary>
- /// An XML Serializer
- /// </summary>
- private static XmlSerializer serializer;
-
- /// <summary>
- /// The Next Job ID
- /// </summary>
- private int nextJobId;
-
- #region Events
- /// <summary>
- /// Fires when the Queue has started
- /// </summary>
- public event EventHandler QueueStarted;
-
- /// <summary>
- /// Fires when a job is Added, Removed or Re-Ordered.
- /// Should be used for triggering an update of the Queue Window.
- /// </summary>
- public event EventHandler QueueListChanged;
-
- /// <summary>
- /// Fires when a pause to the encode queue has been requested.
- /// </summary>
- public event EventHandler QueuePauseRequested;
-
- /// <summary>
- /// Fires when the entire encode queue has completed.
- /// </summary>
- public event EventHandler QueueCompleted;
- #endregion
-
- #region Properties
- /// <summary>
- /// Gets or sets the last encode that was processed.
- /// </summary>
- /// <returns></returns>
- public QueueTask LastEncode { get; set; }
-
- /// <summary>
- /// Gets a value indicating whether Request Pause
- /// </summary>
- public bool Paused { get; private set; }
-
- /// <summary>
- /// Gets the current state of the encode queue.
- /// </summary>
- public ReadOnlyCollection<QueueTask> CurrentQueue
- {
- get { return this.queue.AsReadOnly(); }
- }
-
- /// <summary>
- /// Gets the number of items in the queue.
- /// </summary>
- public int Count
- {
- get { return this.queue.Count; }
- }
- #endregion
-
- #region Queue
-
- /// <summary>
- /// Gets and removes the next job in the queue.
- /// </summary>
- /// <returns>The job that was removed from the queue.</returns>
- private QueueTask GetNextJob()
- {
- QueueTask job = this.queue[0];
- this.LastEncode = job;
- this.Remove(0); // Remove the item which we are about to pass out.
-
- this.SaveQueue();
-
- return job;
- }
-
- /// <summary>
- /// Adds an item to the queue.
- /// </summary>
- /// <param name="query">
- /// The query that will be passed to the HandBrake CLI.
- /// </param>
- /// <param name="title">
- /// The title.
- /// </param>
- /// <param name="source">
- /// The location of the source video.
- /// </param>
- /// <param name="destination">
- /// The location where the encoded video will be.
- /// </param>
- /// <param name="customJob">
- /// Custom job
- /// </param>
- public void Add(string query, int title, string source, string destination, bool customJob)
- {
- QueueTask newJob = new QueueTask
- {
- Id = this.nextJobId++,
- Title = title,
- Query = query,
- Source = source,
- Destination = destination,
- CustomQuery = customJob
- };
-
- this.queue.Add(newJob);
- this.SaveQueue();
-
- if (this.QueueListChanged != null)
- this.QueueListChanged(this, new EventArgs());
- }
-
- /// <summary>
- /// Removes an item from the queue.
- /// </summary>
- /// <param name="index">The zero-based location of the job in the queue.</param>
- public void Remove(int index)
- {
- this.queue.RemoveAt(index);
- this.SaveQueue();
-
- if (this.QueueListChanged != null)
- this.QueueListChanged(this, new EventArgs());
- }
-
- /// <summary>
- /// Retrieve a job from the queue
- /// </summary>
- /// <param name="index">the job id</param>
- /// <returns>A job for the given index or blank job object</returns>
- public QueueTask GetJob(int index)
- {
- if (this.queue.Count >= (index + 1))
- return this.queue[index];
-
- return new QueueTask();
- }
-
- /// <summary>
- /// Moves an item up one position in the queue.
- /// </summary>
- /// <param name="index">The zero-based location of the job in the queue.</param>
- public void MoveUp(int index)
- {
- if (index > 0)
- {
- QueueTask item = queue[index];
-
- queue.RemoveAt(index);
- queue.Insert((index - 1), item);
- }
-
- this.SaveQueue(); // Update the queue recovery file
-
- if (this.QueueListChanged != null)
- this.QueueListChanged(this, new EventArgs());
- }
-
- /// <summary>
- /// Moves an item down one position in the queue.
- /// </summary>
- /// <param name="index">The zero-based location of the job in the queue.</param>
- public void MoveDown(int index)
- {
- if (index < this.queue.Count - 1)
- {
- QueueTask item = this.queue[index];
-
- this.queue.RemoveAt(index);
- this.queue.Insert((index + 1), item);
- }
-
- this.SaveQueue(); // Update the queue recovery file
-
- if (this.QueueListChanged != null)
- this.QueueListChanged(this, new EventArgs());
- }
-
- /// <summary>
- /// Save any updates to the main queue file.
- /// </summary>
- public void SaveQueue()
- {
- string file = Init.InstanceId == 0
- ? "hb_queue_recovery.xml"
- : string.Format("hb_queue_recovery{0}.xml", Init.InstanceId);
- this.WriteQueueStateToFile(file);
- }
-
- /// <summary>
- /// Writes the current state of the queue to a file.
- /// </summary>
- /// <param name="file">The location of the file to write the queue to.</param>
- public void WriteQueueStateToFile(string file)
- {
- string appDataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"HandBrake\");
- string tempPath = file.Contains("hb_queue_recovery") ? appDataPath + file : file;
-
- try
- {
- using (FileStream strm = new FileStream(tempPath, FileMode.Create, FileAccess.Write))
- {
- if (serializer == null)
- serializer = new XmlSerializer(typeof(List<QueueTask>));
- serializer.Serialize(strm, queue);
- strm.Close();
- strm.Dispose();
- }
- }
- catch (Exception)
- {
- return;
- }
- }
-
- /// <summary>
- /// Writes the current state of the queue in the form of a batch (.bat) file.
- /// </summary>
- /// <param name="file">
- /// The location of the file to write the batch file to.
- /// </param>
- /// <returns>
- /// The write batch script to file.
- /// </returns>
- public bool WriteBatchScriptToFile(string file)
- {
- string queries = string.Empty;
- foreach (QueueTask queueItem in this.queue)
- {
- string qItem = queueItem.Query;
- string fullQuery = '"' + Application.StartupPath + "\\HandBrakeCLI.exe" + '"' + qItem;
-
- if (queries == string.Empty)
- queries = queries + fullQuery;
- else
- queries = queries + " && " + fullQuery;
- }
- string strCmdLine = queries;
-
- if (file != string.Empty)
- {
- try
- {
- // Create a StreamWriter and open the file, Write the batch file query to the file and
- // Close the stream
- using (StreamWriter line = new StreamWriter(file))
- {
- line.WriteLine(strCmdLine);
- }
-
- return true;
- }
- catch (Exception exc)
- {
- throw new Exception("Unable to write to the file. Please make sure that the location has the correct permissions for file writing.", exc);
- }
- }
- return false;
- }
-
- /// <summary>
- /// Reads a serialized XML file that represents a queue of encoding jobs.
- /// </summary>
- /// <param name="file">The location of the file to read the queue from.</param>
- public void LoadQueueFromFile(string 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))
- {
- using (FileStream strm = new FileStream(tempPath, FileMode.Open, FileAccess.Read))
- {
- if (strm.Length != 0)
- {
- if (serializer == null)
- serializer = new XmlSerializer(typeof(List<QueueTask>));
-
- List<QueueTask> list = serializer.Deserialize(strm) as List<QueueTask>;
-
- if (list != null)
- foreach (QueueTask item in list)
- this.queue.Add(item);
-
- if (!file.Contains("hb_queue_recovery"))
- this.SaveQueue();
-
- if (this.QueueListChanged != null)
- this.QueueListChanged(this, new EventArgs());
- }
- }
- }
- }
-
- /// <summary>
- /// Checks the current queue for an existing instance of the specified destination.
- /// </summary>
- /// <param name="destination">The destination of the encode.</param>
- /// <returns>Whether or not the supplied destination is already in the queue.</returns>
- public bool CheckForDestinationDuplicate(string destination)
- {
- return this.queue.Any(checkItem => checkItem.Destination.Contains(destination.Replace("\\\\", "\\")));
- }
-
- #endregion
-
- #region Encoding
-
- /// <summary>
- /// Starts encoding the first job in the queue and continues encoding until all jobs
- /// have been encoded.
- /// </summary>
- public void Start()
- {
- if (this.Count != 0)
- {
- if (this.Paused)
- this.Paused = false;
- else
- {
- this.Paused = false;
- try
- {
- Thread theQueue = new Thread(this.StartQueue) { IsBackground = true };
- theQueue.Start();
-
- if (this.QueueStarted != null)
- this.QueueStarted(this, new EventArgs());
- }
- catch (Exception exc)
- {
- throw new Exception("Unable to Start Queue", exc);
- }
- }
- }
- }
-
- /// <summary>
- /// Requests a pause of the encode queue.
- /// </summary>
- public void Pause()
- {
- this.Paused = true;
-
- if (this.QueuePauseRequested != null)
- this.QueuePauseRequested(this, new EventArgs());
- }
-
- /// <summary>
- /// Run through all the jobs on the queue.
- /// </summary>
- /// <param name="state">Object State</param>
- private void StartQueue(object state)
- {
- // Run through each item on the queue
- while (this.Count != 0)
- {
- QueueTask encJob = this.GetNextJob();
- this.SaveQueue(); // Update the queue recovery file
-
- Start(encJob, true);
-
- if (HbProcess == null)
- {
- return;
- }
- HbProcess.WaitForExit();
-
- this.CopyLog(this.LastEncode.Destination);
-
- HbProcess.Close();
- HbProcess.Dispose();
-
- // Growl
- if (Init.GrowlEncode)
- GrowlCommunicator.Notify("Encode Completed",
- "Put down that cocktail...\nyour Handbrake encode is done.");
-
- while (this.Paused) // Need to find a better way of doing this.
- {
- Thread.Sleep(2000);
- }
- }
- this.LastEncode = new QueueTask();
-
- if (this.QueueCompleted != null)
- this.QueueCompleted(this, new EventArgs());
-
- // After the encode is done, we may want to shutdown, suspend etc.
- Finish();
- }
-
- /// <summary>
- /// Perform an action after an encode. e.g a shutdown, standby, restart etc.
- /// </summary>
- private void Finish()
- {
- // Growl
- if (Init.GrowlQueue)
- GrowlCommunicator.Notify("Queue Completed", "Put down that cocktail...\nyour Handbrake queue is done.");
-
- // Do something whent he encode ends.
- switch (Init.CompletionOption)
- {
- case "Shutdown":
- Process.Start("Shutdown", "-s -t 60");
- break;
- case "Log Off":
- Win32.ExitWindowsEx(0, 0);
- break;
- case "Suspend":
- Application.SetSuspendState(PowerState.Suspend, true, true);
- break;
- case "Hibernate":
- Application.SetSuspendState(PowerState.Hibernate, true, true);
- break;
- case "Lock System":
- Win32.LockWorkStation();
- break;
- case "Quit HandBrake":
- Application.Exit();
- break;
- default:
- break;
- }
- }
-
- #endregion
- }
-} \ No newline at end of file
diff --git a/win/C#/HandBrake.ApplicationServices/Services/QueueManager.cs b/win/C#/HandBrake.ApplicationServices/Services/QueueManager.cs
index 5edbf39b0..f244e0156 100644
--- a/win/C#/HandBrake.ApplicationServices/Services/QueueManager.cs
+++ b/win/C#/HandBrake.ApplicationServices/Services/QueueManager.cs
@@ -22,6 +22,7 @@
/*
* TODO
* - Rewrite the batch script generator.
+ * - QueueTask, switch everything to use the Task property, which is a model of all settings.
*/
#region Private Variables
@@ -163,9 +164,7 @@
{
lock (QueueLock)
{
- // Tag the job with an ID
- job.Id = lastJobId++;
- queue.Add(job);
+ queue.Remove(job);
InvokeQueueChanged(EventArgs.Empty);
}
}
@@ -185,7 +184,7 @@
this.LastProcessedJob = job;
this.Remove(job); // Remove the item which we are about to pass out.
- return job;
+ return job;
}
return null;
@@ -236,15 +235,12 @@
string appDataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"HandBrake\");
string tempPath = !string.IsNullOrEmpty(exportPath) ? exportPath : appDataPath + string.Format(this.queueFile, string.Empty);
- if (File.Exists(tempPath))
+ using (FileStream strm = new FileStream(tempPath, FileMode.Create, FileAccess.Write))
{
- using (FileStream strm = new FileStream(tempPath, FileMode.Create, FileAccess.Write))
- {
- XmlSerializer serializer = new XmlSerializer(typeof(List<QueueTask>));
- serializer.Serialize(strm, queue);
- strm.Close();
- strm.Dispose();
- }
+ XmlSerializer serializer = new XmlSerializer(typeof(List<QueueTask>));
+ serializer.Serialize(strm, queue);
+ strm.Close();
+ strm.Dispose();
}
}
@@ -286,7 +282,7 @@
/// <returns>Whether or not the supplied destination is already in the queue.</returns>
public bool CheckForDestinationPathDuplicates(string destination)
{
- return this.queue.Any(checkItem => checkItem.Task.Destination.Contains(destination.Replace("\\\\", "\\")));
+ return this.queue.Any(checkItem => checkItem.Destination.Contains(destination.Replace("\\\\", "\\")));
}
/// <summary>
diff --git a/win/C#/HandBrake.ApplicationServices/Services/QueueProcessor.cs b/win/C#/HandBrake.ApplicationServices/Services/QueueProcessor.cs
index 162527164..ff865e2ee 100644
--- a/win/C#/HandBrake.ApplicationServices/Services/QueueProcessor.cs
+++ b/win/C#/HandBrake.ApplicationServices/Services/QueueProcessor.cs
@@ -249,7 +249,7 @@ namespace HandBrake.ApplicationServices.Services
case "Shutdown":
Process.Start("Shutdown", "-s -t 60");
break;
- case "Log Off":
+ case "Log off":
Win32.ExitWindowsEx(0, 0);
break;
case "Suspend":