From 1c88dbe1af5b6c29dc88626dc34e746936f49fe6 Mon Sep 17 00:00:00 2001 From: sr55 Date: Fri, 7 Jun 2019 19:55:56 +0100 Subject: WinGui: Re-implement queue import/export functionality for the GUI. Note, this is incompatible with the CLI Queue parser. There is an existing separate option for exporting into a CLI friendly JSON format. Format subject to change in the future. For now, this is a quick way of getting this functionality back. #898 --- .../Services/Queue/Interfaces/IQueueService.cs | 13 +- win/CS/HandBrakeWPF/Services/Queue/QueueService.cs | 240 +++------------------ 2 files changed, 40 insertions(+), 213 deletions(-) (limited to 'win/CS/HandBrakeWPF/Services') diff --git a/win/CS/HandBrakeWPF/Services/Queue/Interfaces/IQueueService.cs b/win/CS/HandBrakeWPF/Services/Queue/Interfaces/IQueueService.cs index add20a27d..18e74989d 100644 --- a/win/CS/HandBrakeWPF/Services/Queue/Interfaces/IQueueService.cs +++ b/win/CS/HandBrakeWPF/Services/Queue/Interfaces/IQueueService.cs @@ -102,13 +102,14 @@ namespace HandBrakeWPF.Services.Queue.Interfaces void BackupQueue(string exportPath); /// - /// Export the Queue the standardised JSON format. + /// Export the windows queue in JSON format. + /// Note: Note compatible with CLI. /// /// /// The export path. /// void ExportJson(string exportPath); - + /// /// Restore a JSON queue file. /// @@ -117,6 +118,14 @@ namespace HandBrakeWPF.Services.Queue.Interfaces /// void ImportJson(string path); + /// + /// Export the Queue the standardised JSON format for the CLI + /// + /// + /// The export path. + /// + void ExportCliJson(string exportPath); + /// /// Checks the current queue for an existing instance of the specified destination. /// diff --git a/win/CS/HandBrakeWPF/Services/Queue/QueueService.cs b/win/CS/HandBrakeWPF/Services/Queue/QueueService.cs index 48c29f4f4..3d8af90b3 100644 --- a/win/CS/HandBrakeWPF/Services/Queue/QueueService.cs +++ b/win/CS/HandBrakeWPF/Services/Queue/QueueService.cs @@ -44,29 +44,12 @@ namespace HandBrakeWPF.Services.Queue public class QueueService : Interfaces.IQueueService { - #region Constants and Fields private static readonly object QueueLock = new object(); private readonly IUserSettingService userSettingService; private readonly ObservableCollection queue = new ObservableCollection(); private readonly string queueFile; private bool clearCompleted; - #endregion - - #region Constructors and Destructors - - /// - /// Initializes a new instance of the class. - /// - /// - /// The encode Service. - /// - /// - /// The user settings service. - /// - /// - /// Services are not setup - /// public QueueService(IEncode encodeService, IUserSettingService userSettingService) { this.userSettingService = userSettingService; @@ -76,64 +59,18 @@ namespace HandBrakeWPF.Services.Queue this.queueFile = string.Format("{0}{1}.json", QueueRecoveryHelper.QueueFileName, GeneralUtilities.ProcessId); } - #endregion - - #region Delegates - - /// - /// Queue Progress Status - /// - /// - /// The sender. - /// - /// - /// The QueueProgressEventArgs. - /// public delegate void QueueProgressStatus(object sender, QueueProgressEventArgs e); - /// - /// The queue completed. - /// - /// - /// The sender. - /// - /// - /// The e. - /// public delegate void QueueCompletedEventDelegate(object sender, QueueCompletedEventArgs e); - #endregion - - #region Events - - /// - /// Fires when the Queue has started - /// public event QueueProgressStatus JobProcessingStarted; - /// - /// Fires when a job is Added, Removed or Re-Ordered. - /// Should be used for triggering an update of the Queue Window. - /// public event EventHandler QueueChanged; - /// - /// Fires when the entire encode queue has completed. - /// public event QueueCompletedEventDelegate QueueCompleted; - /// - /// Fires when a pause to the encode queue has been requested. - /// public event EventHandler QueuePaused; - #endregion - - #region Properties - - /// - /// Gets the number of jobs in the queue; - /// public int Count { get @@ -142,9 +79,6 @@ namespace HandBrakeWPF.Services.Queue } } - /// - /// The number of errors detected. - /// public int ErrorCount { get @@ -153,25 +87,12 @@ namespace HandBrakeWPF.Services.Queue } } - /// - /// Gets the IEncodeService instance. - /// public IEncode EncodeService { get; private set; } - /// - /// Gets a value indicating whether IsProcessing. - /// public bool IsProcessing { get; private set; } - /// - /// Gets or sets Last Processed Job. - /// This is set when the job is poped of the queue by GetNextJobForProcessing(); - /// public QueueTask LastProcessedJob { get; set; } - /// - /// Gets The current queue. - /// public ObservableCollection Queue { get @@ -180,17 +101,7 @@ namespace HandBrakeWPF.Services.Queue } } - #endregion - - #region Public Methods - /// - /// Add a job to the Queue. - /// This method is Thread Safe. - /// - /// - /// The encode Job object. - /// public void Add(QueueTask job) { lock (QueueLock) @@ -200,12 +111,6 @@ namespace HandBrakeWPF.Services.Queue } } - /// - /// Backup any changes to the queue file - /// - /// - /// If this is not null or empty, this will be used instead of the standard backup location. - /// public void BackupQueue(string exportPath) { Stopwatch watch = Stopwatch.StartNew(); @@ -237,7 +142,7 @@ namespace HandBrakeWPF.Services.Queue Debug.WriteLine("Queue Save (ms): " + watch.ElapsedMilliseconds); } - public void ExportJson(string exportPath) + public void ExportCliJson(string exportPath) { List jobs = this.queue.Where(item => item.Status != QueueItemStatus.Completed).ToList(); List workUnits = jobs.Select(job => job.Task).ToList(); @@ -253,38 +158,47 @@ namespace HandBrakeWPF.Services.Queue } } + public void ExportJson(string exportPath) + { + List jobs = this.queue.Where(item => item.Status != QueueItemStatus.Completed).ToList(); + + JsonSerializerSettings settings = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }; + + string json = JsonConvert.SerializeObject(jobs, Formatting.Indented, settings); + + using (var strm = new StreamWriter(exportPath, false)) + { + strm.Write(json); + strm.Close(); + strm.Dispose(); + } + } + public void ImportJson(string path) { List tasks; using (StreamReader reader = new StreamReader(path)) { string fileContent = reader.ReadToEnd(); - tasks = QueueFactory.GetQueue(fileContent); + if (string.IsNullOrEmpty(fileContent)) + { + return; + } + + List reloadedQueue = JsonConvert.DeserializeObject>(fileContent); - if (tasks != null) + if (reloadedQueue == null) { - foreach (Task task in tasks) - { - // TODO flesh out. - EncodeTask encodeTask = EncodeTaskImportFactory.Create(task.Job); - QueueTask queueTask = new QueueTask(); - queueTask.Task = encodeTask; + return; + } - this.queue.Add(queueTask); - } + foreach (QueueTask task in reloadedQueue) + { + this.queue.Add(task); } } } - /// - /// Checks the current queue for an existing instance of the specified destination. - /// - /// - /// The destination of the encode. - /// - /// - /// Whether or not the supplied destination is already in the queue. - /// public bool CheckForDestinationPathDuplicates(string destination) { foreach (QueueTask job in this.queue) @@ -301,9 +215,6 @@ namespace HandBrakeWPF.Services.Queue return false; } - /// - /// Clear down all Queue Items - /// public void Clear() { List deleteList = this.queue.ToList(); @@ -314,9 +225,6 @@ namespace HandBrakeWPF.Services.Queue this.InvokeQueueChanged(EventArgs.Empty); } - /// - /// Clear down the Queue´s completed items - /// public void ClearCompleted() { Execute.OnUIThread( @@ -332,13 +240,6 @@ namespace HandBrakeWPF.Services.Queue }); } - /// - /// Get the first job on the queue for processing. - /// This also removes the job from the Queue and sets the LastProcessedJob - /// - /// - /// An encode Job object. - /// public QueueTask GetNextJobForProcessing() { if (this.queue.Count > 0) @@ -349,12 +250,6 @@ namespace HandBrakeWPF.Services.Queue return null; } - /// - /// Moves an item down one position in the queue. - /// - /// - /// The zero-based location of the job in the queue. - /// public void MoveDown(int index) { if (index < this.queue.Count - 1) @@ -368,12 +263,6 @@ namespace HandBrakeWPF.Services.Queue this.InvokeQueueChanged(EventArgs.Empty); } - /// - /// Moves an item up one position in the queue. - /// - /// - /// The zero-based location of the job in the queue. - /// public void MoveUp(int index) { if (index > 0) @@ -387,13 +276,6 @@ namespace HandBrakeWPF.Services.Queue this.InvokeQueueChanged(EventArgs.Empty); } - /// - /// Remove a job from the Queue. - /// This method is Thread Safe - /// - /// - /// The job. - /// public void Remove(QueueTask job) { lock (QueueLock) @@ -403,12 +285,6 @@ namespace HandBrakeWPF.Services.Queue } } - /// - /// Reset a Queued Item from Error or Completed to Waiting - /// - /// - /// The job. - /// public void ResetJobStatusToWaiting(QueueTask job) { if (job.Status != QueueItemStatus.Error && job.Status != QueueItemStatus.Completed) @@ -420,12 +296,6 @@ namespace HandBrakeWPF.Services.Queue job.Status = QueueItemStatus.Waiting; } - /// - /// Restore a Queue from file or from the queue backup file. - /// - /// - /// The import path. String.Empty or null will result in the default file being loaded. - /// public void RestoreQueue(string importPath) { string appDataPath = DirectoryUtilities.GetUserStoragePath(VersionHelper.IsNightly()); @@ -477,9 +347,6 @@ namespace HandBrakeWPF.Services.Queue } } - /// - /// Requests a pause of the encode queue. - /// public void Pause() { this.IsProcessing = false; @@ -497,13 +364,6 @@ namespace HandBrakeWPF.Services.Queue this.Pause(); } - /// - /// Starts encoding the first job in the queue and continues encoding until all jobs - /// have been encoded. - /// - /// - /// The is Clear Completed. - /// public void Start(bool isClearCompleted) { if (this.IsProcessing) @@ -540,16 +400,6 @@ namespace HandBrakeWPF.Services.Queue this.InvokeQueuePaused(EventArgs.Empty); } - #endregion - - #region Methods - - /// - /// The on queue completed. - /// - /// - /// The e. - /// protected virtual void OnQueueCompleted(QueueCompletedEventArgs e) { QueueCompletedEventDelegate handler = this.QueueCompleted; @@ -561,15 +411,6 @@ namespace HandBrakeWPF.Services.Queue this.IsProcessing = false; } - /// - /// After an encode is complete, move onto the next job. - /// - /// - /// The sender. - /// - /// - /// The EncodeCompletedEventArgs. - /// private void EncodeServiceEncodeCompleted(object sender, EncodeCompletedEventArgs e) { this.LastProcessedJob.Status = QueueItemStatus.Completed; @@ -601,12 +442,6 @@ namespace HandBrakeWPF.Services.Queue } } - /// - /// Invoke the JobProcessingStarted event - /// - /// - /// The QueueProgressEventArgs. - /// private void InvokeJobProcessingStarted(QueueProgressEventArgs e) { QueueProgressStatus handler = this.JobProcessingStarted; @@ -616,12 +451,6 @@ namespace HandBrakeWPF.Services.Queue } } - /// - /// Invoke the Queue Changed Event - /// - /// - /// The e. - /// private void InvokeQueueChanged(EventArgs e) { try @@ -640,12 +469,6 @@ namespace HandBrakeWPF.Services.Queue } } - /// - /// Invoke the QueuePaused event - /// - /// - /// The EventArgs. - /// private void InvokeQueuePaused(EventArgs e) { this.IsProcessing = false; @@ -657,9 +480,6 @@ namespace HandBrakeWPF.Services.Queue } } - /// - /// Run through all the jobs on the queue. - /// private void ProcessNextJob() { QueueTask job = this.GetNextJobForProcessing(); @@ -702,7 +522,5 @@ namespace HandBrakeWPF.Services.Queue this.OnQueueCompleted(new QueueCompletedEventArgs(false)); } } - - #endregion } } \ No newline at end of file -- cgit v1.2.3